Annotation of texi2mdoc/main.c, Revision 1.24
1.24 ! kristaps 1: /* $Id: main.c,v 1.23 2015/02/19 20:55:56 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2015 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/mman.h>
18: #include <sys/stat.h>
19:
20: #include <assert.h>
21: #include <ctype.h>
22: #include <fcntl.h>
23: #include <getopt.h>
1.2 kristaps 24: #include <libgen.h>
25: #include <limits.h>
1.1 kristaps 26: #include <stdarg.h>
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
1.10 kristaps 30: #include <time.h>
1.6 kristaps 31: #include <unistd.h>
1.1 kristaps 32:
1.24 ! kristaps 33: #include "extern.h"
1.1 kristaps 34:
1.23 kristaps 35: #define SECTSZ 4
36: static const char *const sects[SECTSZ] = {
37: "Sh",
38: "Ss",
39: "Em",
40: "No",
41: };
42:
1.16 kristaps 43: static void doaccent(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 44: static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 45: static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
46: static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.15 kristaps 47: static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 48: static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 49: static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 50: static void doexample(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 51: static void doignargn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 52: static void doignblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
53: static void doignbracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
54: static void doignline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12 kristaps 55: static void doinline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 56: static void doinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 57: static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *);
58: static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 59: static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 60: static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18 kristaps 61: static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 62: static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *);
63: static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *);
64: static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23 kristaps 65: static void dosecoffs(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 66: static void dosection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 67: static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 68: static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23 kristaps 69: static void dosubsubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 70: static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18 kristaps 71: static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.10 kristaps 72: static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.24 ! kristaps 73: static void dovalue(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.16 kristaps 74: static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 75:
1.24 ! kristaps 76: static const struct texitok __texitoks[TEXICMD__MAX] = {
1.20 kristaps 77: /* TEXICMD__BEGIN */
1.8 kristaps 78: { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */
1.16 kristaps 79: { doaccent, "'", 1 }, /* TEXICMD_ACUTE */
1.1 kristaps 80: { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */
81: { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */
1.12 kristaps 82: { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */
83: { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */
1.20 kristaps 84: { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */
1.23 kristaps 85: { dosubsubsection, "appendixsubsubsec", 17 }, /* TEXICMD_APPENDIXSUBSUBSEC */
1.3 kristaps 86: { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */
1.1 kristaps 87: { dosymbol, "@", 1 }, /* TEXICMD_AT */
1.3 kristaps 88: { doignline, "author", 6 }, /* TEXICMD_AUTHOR */
1.21 kristaps 89: { doinline, "b", 1 }, /* TEXICMD_BOLD */
1.3 kristaps 90: { dosymbol, "!", 1 }, /* TEXICMD_BANG */
1.7 kristaps 91: { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */
1.1 kristaps 92: { dobye, "bye", 3 }, /* TEXICMD_BYE */
1.12 kristaps 93: { doignline, "center", 6 }, /* TEXICMD_CENTER */
94: { dosection, "chapter", 7 }, /* TEXICMD_CHAPTER */
1.1 kristaps 95: { doignline, "cindex", 6 }, /* TEXICMD_CINDEX */
1.16 kristaps 96: { doaccent, "^", 1 }, /* TEXICMD_CIRCUMFLEX */
1.24 ! kristaps 97: { doinline, "cite", 4 }, /* TEXICMD_CITE */
! 98: { dovalue, "clear", 5 }, /* TEXICMD_CLEAR */
1.21 kristaps 99: { doinline, "code", 4 }, /* TEXICMD_CODE */
1.3 kristaps 100: { dosymbol, ":", 1 }, /* TEXICMD_COLON */
1.18 kristaps 101: { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */
1.12 kristaps 102: { doinline, "command", 7 }, /* TEXICMD_COMMAND */
1.1 kristaps 103: { doignline, "c", 1 }, /* TEXICMD_COMMENT */
1.2 kristaps 104: { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */
1.1 kristaps 105: { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */
106: { doignblock, "copying", 7 }, /* TEXICMD_COPYING */
107: { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */
1.15 kristaps 108: { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */
109: { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */
110: { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */
111: { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */
112: { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */
113: { dodefn, "deftpx", 6 }, /* TEXICMD_DEFTPX */
114: { dodefn, "deftypefn", 9 }, /* TEXICMD_DEFTYPEFN */
115: { dodefn, "deftypefnx", 10 }, /* TEXICMD_DEFTYPEFNX */
116: { dodefn, "deftypefun", 10 }, /* TEXICMD_DEFTYPEFUN */
117: { dodefn, "deftypefunx", 11 }, /* TEXICMD_DEFTYPEFUNX */
118: { dodefn, "deftypevar", 10 }, /* TEXICMD_DEFTYPEVAR */
119: { dodefn, "deftypevarx", 11 }, /* TEXICMD_DEFTYPEVARX */
120: { dodefn, "deftypevr", 9 }, /* TEXICMD_DEFTYPEVR */
121: { dodefn, "deftypevrx", 10 }, /* TEXICMD_DEFTYPEVRX */
122: { dodefn, "defun", 5 }, /* TEXICMD_DEFUN */
123: { dodefn, "defunx", 6 }, /* TEXICMD_DEFUNX */
124: { dodefn, "defvar", 6 }, /* TEXICMD_DEFVAR */
125: { dodefn, "defvarx", 7 }, /* TEXICMD_DEFVARX */
126: { dodefn, "defvr", 5 }, /* TEXICMD_DEFVR */
127: { dodefn, "defvrx", 6 }, /* TEXICMD_DEFVRX */
1.1 kristaps 128: { doignblock, "detailmenu", 10 }, /* TEXICMD_DETAILMENU */
1.21 kristaps 129: { doinline, "dfn", 3 }, /* TEXICMD_DFN */
1.1 kristaps 130: { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */
131: { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */
1.3 kristaps 132: { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */
1.2 kristaps 133: { dosymbol, "dots", 4 }, /* TEXICMD_DOTS */
1.8 kristaps 134: { dolink, "email", 5 }, /* TEXICMD_EMAIL */
1.21 kristaps 135: { doinline, "emph", 4 }, /* TEXICMD_EMPH */
1.1 kristaps 136: { NULL, "end", 3 }, /* TEXICMD_END */
1.2 kristaps 137: { doenumerate, "enumerate", 9 }, /* TEXICMD_ENUMERATE */
1.12 kristaps 138: { doinline, "env", 3 }, /* TEXICMD_ENV */
1.15 kristaps 139: { dosymbol, "error", 5 }, /* TEXICMD_ERROR */
1.1 kristaps 140: { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */
1.17 kristaps 141: { dosymbol, "expansion", 9 }, /* TEXICMD_EXPANSION */
1.12 kristaps 142: { doinline, "file", 4 }, /* TEXICMD_FILE */
1.17 kristaps 143: { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */
1.20 kristaps 144: { doignline, "findex", 6 }, /* TEXICMD_FINDEX */
145: { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */
146: { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */
1.16 kristaps 147: { doaccent, "`", 1 }, /* TEXICMD_GRAVE */
1.3 kristaps 148: { doblock, "group", 5 }, /* TEXICMD_GROUP */
1.2 kristaps 149: { dosection, "heading", 7 }, /* TEXICMD_HEADING */
1.3 kristaps 150: { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */
1.18 kristaps 151: { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */
1.3 kristaps 152: { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */
1.21 kristaps 153: { doinline, "i", 1 }, /* TEXICMD_I */
1.24 ! kristaps 154: { dovalue, "ifclear", 7 }, /* TEXICMD_IFCLEAR */
1.14 kristaps 155: { doignblock, "ifdocbook", 9 }, /* TEXICMD_IFDOCBOOK */
1.1 kristaps 156: { doignblock, "ifhtml", 6 }, /* TEXICMD_IFHTML */
1.3 kristaps 157: { doignblock, "ifinfo", 6 }, /* TEXICMD_IFINFO */
1.14 kristaps 158: { doblock, "ifnotdocbook", 12 }, /* TEXICMD_IFNOTDOCBOOK */
159: { doblock, "ifnothtml", 9 }, /* TEXICMD_IFNOTHTML */
160: { doblock, "ifnotinfo", 9 }, /* TEXICMD_IFNOTINFO */
161: { doignblock, "ifnotplaintext", 14 }, /* TEXICMD_IFNOTPLAINTEXT */
1.3 kristaps 162: { doblock, "ifnottex", 8 }, /* TEXICMD_IFNOTTEX */
1.14 kristaps 163: { doblock, "ifnotxml", 8 }, /* TEXICMD_IFNOTXML */
164: { doblock, "ifplaintext", 11 }, /* TEXICMD_IFPLAINTEXT */
1.1 kristaps 165: { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */
1.3 kristaps 166: { doignblock, "ifset", 5 }, /* TEXICMD_IFSET */
1.14 kristaps 167: { doignblock, "ifxml", 5 }, /* TEXICMD_IFXML */
1.17 kristaps 168: { doignblock, "ignore", 6 }, /* TEXICMD_IGNORE */
1.1 kristaps 169: { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */
1.2 kristaps 170: { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */
1.13 kristaps 171: { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */
1.5 kristaps 172: { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */
1.1 kristaps 173: { doitem, "item", 4 }, /* TEXICMD_ITEM */
174: { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */
1.20 kristaps 175: { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */
1.21 kristaps 176: { doinline, "kbd", 3 }, /* TEXICMD_KBD */
1.18 kristaps 177: { dobracket, "key", 3 }, /* TEXICMD_KEY */
1.20 kristaps 178: { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */
1.1 kristaps 179: { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */
1.23 kristaps 180: { dosecoffs, "lowersections", 13 }, /* TEXICMD_LOWERSECTIONS */
1.3 kristaps 181: { domath, "math", 4 }, /* TEXICMD_MATH */
1.1 kristaps 182: { doignblock, "menu", 4 }, /* TEXICMD_MENU */
1.18 kristaps 183: { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */
1.15 kristaps 184: { doignline, "need", 4 }, /* TEXICMD_NEED */
1.3 kristaps 185: { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */
1.1 kristaps 186: { doignline, "node", 4 }, /* TEXICMD_NODE */
1.3 kristaps 187: { doignline, "noindent", 8 }, /* TEXICMD_NOINDENT */
1.16 kristaps 188: { doinline, "option", 6 }, /* TEXICMD_OPTION */
1.8 kristaps 189: { dolink, "pxref", 5 }, /* TEXICMD_PXREF */
1.3 kristaps 190: { dosymbol, "?", 1 }, /* TEXICMD_QUESTIONMARK */
1.1 kristaps 191: { doquotation, "quotation", 9 }, /* TEXICMD_QUOTATION */
1.3 kristaps 192: { doignline, "page", 4 }, /* TEXICMD_PAGE */
193: { doignline, "paragraphindent", 14 }, /* TEXICMD_PARINDENT */
1.2 kristaps 194: { doignline, "printindex", 10 }, /* TEXICMD_PRINTINDEX */
1.21 kristaps 195: { doinline, "r", 1 }, /* TEXICMD_R */
1.23 kristaps 196: { dosecoffs, "raisesections", 13 }, /* TEXICMD_RAISESECTIONS */
1.1 kristaps 197: { dobracket, "ref", 3 }, /* TEXICMD_REF */
1.15 kristaps 198: { dosymbol, "result", 6 }, /* TEXICMD_RESULT */
1.21 kristaps 199: { doinline, "samp", 4 }, /* TEXICMD_SAMP */
200: { doinline, "sansserif", 9 }, /* TEXICMD_SANSSERIF */
1.7 kristaps 201: { dobracket, "sc", 2 }, /* TEXICMD_SC */
1.1 kristaps 202: { dosection, "section", 7 }, /* TEXICMD_SECTION */
1.24 ! kristaps 203: { dovalue, "set", 3 }, /* TEXICMD_SET */
1.1 kristaps 204: { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */
205: { doignline, "setfilename", 11 }, /* TEXICMD_SETFILENAME */
1.10 kristaps 206: { dotitle, "settitle", 8 }, /* TEXICMD_SETTITLE */
1.21 kristaps 207: { doinline, "slanted", 7 }, /* TEXICMD_SLANTED */
1.3 kristaps 208: { dosp, "sp", 2 }, /* TEXICMD_SP */
209: { dosymbol, " ", 1 }, /* TEXICMD_SPACE */
1.17 kristaps 210: { doignline, "smallbook", 9 }, /* TEXICMD_SMALLBOOK */
1.12 kristaps 211: { dodisplay, "smalldisplay", 12 }, /* TEXICMD_SMALLDISPLAY */
1.3 kristaps 212: { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */
1.20 kristaps 213: { dodisplay, "smallformat", 11 }, /* TEXICMD_SMALLFORMAT */
1.13 kristaps 214: { dodisplay, "smallindentblock", 16 }, /* TEXICMD_SMALLINDENTBLOCK */
1.3 kristaps 215: { dosymbol, "{", 1 }, /* TEXICMD_SQUIGGLE_LEFT */
216: { dosymbol, "}", 1 }, /* TEXICMD_SQUIGGLE_RIGHT */
1.21 kristaps 217: { doinline, "strong", 6 }, /* TEXICMD_STRONG */
1.20 kristaps 218: { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */
1.1 kristaps 219: { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */
1.23 kristaps 220: { dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */
1.3 kristaps 221: { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */
1.20 kristaps 222: { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */
1.21 kristaps 223: { doinline, "t", 1 }, /* TEXICMD_T */
1.18 kristaps 224: { dotab, "tab", 3 }, /* TEXICMD_TAB */
225: { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */
1.1 kristaps 226: { dotable, "table", 5 }, /* TEXICMD_TABLE */
227: { doignblock, "tex", 3 }, /* TEXICMD_TEX */
228: { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */
1.16 kristaps 229: { doaccent, "~", 1 }, /* TEXICMD_TILDE */
1.3 kristaps 230: { doignline, "title", 5 }, /* TEXICMD_TITLE */
1.1 kristaps 231: { dobracket, "titlefont", 9 }, /* TEXICMD_TITLEFONT */
232: { doignblock, "titlepage", 9 }, /* TEXICMD_TITLEPAGE */
233: { dotop, "top", 3 }, /* TEXICMD_TOP */
1.16 kristaps 234: { doaccent, "\"", 1 }, /* TEXICMD_UMLAUT */
1.12 kristaps 235: { dosection, "unnumbered", 10 }, /* TEXICMD_UNNUMBERED */
1.2 kristaps 236: { dosection, "unnumberedsec", 13 }, /* TEXICMD_UNNUMBEREDSEC */
1.20 kristaps 237: { dosubsection, "unnumberedsubsec", 16 }, /* TEXICMD_UNNUMBEREDSUBSEC */
1.23 kristaps 238: { dosubsubsection, "unnumberedsubsubsec", 19 }, /* TEXICMD_UNNUMBEREDSUBSUBSEC */
1.8 kristaps 239: { dolink, "uref", 4 }, /* TEXICMD_UREF */
240: { dolink, "url", 3 }, /* TEXICMD_URL */
1.24 ! kristaps 241: { dovalue, "value", 5 }, /* TEXICMD_VALUE */
1.12 kristaps 242: { doinline, "var", 3 }, /* TEXICMD_VAR */
1.16 kristaps 243: { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */
1.18 kristaps 244: { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */
1.9 kristaps 245: { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */
1.20 kristaps 246: { dotable, "vtable", 6 }, /* TEXICMD_VTABLE */
1.3 kristaps 247: { dobracket, "w", 1 }, /* TEXICMD_W */
1.8 kristaps 248: { dolink, "xref", 4 }, /* TEXICMD_XREF */
1.20 kristaps 249: /* TEXICMD__END */
1.1 kristaps 250: };
251:
1.24 ! kristaps 252: const struct texitok *const texitoks = __texitoks;
1.18 kristaps 253:
1.24 ! kristaps 254: static char *
! 255: lineargdup(struct texi *p, const char *buf, size_t sz, size_t *pos)
1.11 kristaps 256: {
1.24 ! kristaps 257: char *key;
1.11 kristaps 258: size_t start, end;
259:
1.24 ! kristaps 260: while (*pos < sz && isws(buf[*pos]))
1.1 kristaps 261: advance(p, buf, pos);
1.24 ! kristaps 262: if (*pos == sz)
! 263: return(NULL);
! 264: for (start = end = *pos; end < sz; end++)
! 265: if ('\n' == buf[end])
1.1 kristaps 266: break;
1.3 kristaps 267: advanceto(p, buf, pos, end);
1.24 ! kristaps 268: if (*pos < sz)
1.15 kristaps 269: advance(p, buf, pos);
1.24 ! kristaps 270: key = malloc(end - start + 1);
! 271: if (NULL == key) {
! 272: perror(NULL);
! 273: exit(EXIT_FAILURE);
! 274: }
! 275: memcpy(key, &buf[start], end - start);
! 276: key[end - start] = '\0';
! 277: return(key);
1.2 kristaps 278: }
279:
280: static void
1.15 kristaps 281: dodefn(struct texi *p, enum texicmd cmd,
1.3 kristaps 282: const char *buf, size_t sz, size_t *pos)
283: {
284: const char *blk;
285:
1.5 kristaps 286: blk = NULL;
1.3 kristaps 287: switch (cmd) {
1.15 kristaps 288: case (TEXICMD_DEFFN):
289: case (TEXICMD_DEFTP):
1.3 kristaps 290: case (TEXICMD_DEFTYPEFN):
291: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 292: case (TEXICMD_DEFTYPEVAR):
293: case (TEXICMD_DEFTYPEVR):
294: case (TEXICMD_DEFUN):
295: case (TEXICMD_DEFVAR):
296: case (TEXICMD_DEFVR):
1.5 kristaps 297: blk = texitoks[cmd].tok;
1.3 kristaps 298: break;
1.5 kristaps 299: default:
1.3 kristaps 300: break;
301: }
302:
303: if (p->ign) {
1.15 kristaps 304: NULL != blk ?
305: parseto(p, buf, sz, pos, blk) :
306: parseeoln(p, buf, sz, pos);
1.3 kristaps 307: return;
308: }
309:
1.15 kristaps 310: if (NULL != blk)
311: texivspace(p);
312:
1.3 kristaps 313: switch (cmd) {
1.15 kristaps 314: case (TEXICMD_DEFMAC):
315: case (TEXICMD_DEFMACX):
316: texiputchars(p, "Macro");
317: break;
318: case (TEXICMD_DEFTYPEVAR):
319: case (TEXICMD_DEFTYPEVARX):
320: case (TEXICMD_DEFVAR):
321: case (TEXICMD_DEFVARX):
322: texiputchars(p, "Variable");
323: break;
1.3 kristaps 324: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 325: case (TEXICMD_DEFTYPEFUNX):
326: case (TEXICMD_DEFUN):
327: case (TEXICMD_DEFUNX):
328: texiputchars(p, "Function");
1.3 kristaps 329: break;
330: default:
1.15 kristaps 331: parselinearg(p, buf, sz, pos);
1.3 kristaps 332: break;
333: }
1.15 kristaps 334:
335: texiputchars(p, ":\n");
336:
337: switch (cmd) {
338: case (TEXICMD_DEFMAC):
339: case (TEXICMD_DEFMACX):
340: teximacroopen(p, "Dv");
341: while (parselinearg(p, buf, sz, pos))
342: /* Spin. */ ;
343: teximacroclose(p);
344: break;
345: case (TEXICMD_DEFFN):
346: case (TEXICMD_DEFFNX):
347: case (TEXICMD_DEFUN):
348: case (TEXICMD_DEFUNX):
349: teximacroopen(p, "Fo");
350: parselinearg(p, buf, sz, pos);
351: teximacroclose(p);
352: teximacroopen(p, "Fa");
353: while (parselinearg(p, buf, sz, pos))
354: /* Spin. */ ;
355: teximacroclose(p);
356: teximacro(p, "Fc");
357: break;
358: case (TEXICMD_DEFTYPEFUN):
359: case (TEXICMD_DEFTYPEFUNX):
360: case (TEXICMD_DEFTYPEFN):
361: case (TEXICMD_DEFTYPEFNX):
362: teximacroopen(p, "Ft");
363: parselinearg(p, buf, sz, pos);
364: teximacroclose(p);
365: teximacroopen(p, "Fo");
366: parselinearg(p, buf, sz, pos);
367: teximacroclose(p);
368: teximacroopen(p, "Fa");
369: while (parselinearg(p, buf, sz, pos))
370: /* Spin. */ ;
371: teximacroclose(p);
372: teximacro(p, "Fc");
373: break;
374: case (TEXICMD_DEFTP):
375: case (TEXICMD_DEFTPX):
376: case (TEXICMD_DEFTYPEVAR):
377: case (TEXICMD_DEFTYPEVARX):
378: case (TEXICMD_DEFTYPEVR):
379: case (TEXICMD_DEFTYPEVRX):
380: teximacroopen(p, "Vt");
381: while (parselinearg(p, buf, sz, pos))
382: /* Spin. */ ;
383: teximacroclose(p);
384: break;
385: case (TEXICMD_DEFVAR):
386: case (TEXICMD_DEFVARX):
387: case (TEXICMD_DEFVR):
388: case (TEXICMD_DEFVRX):
389: teximacroopen(p, "Va");
390: while (parselinearg(p, buf, sz, pos))
391: /* Spin. */ ;
392: teximacroclose(p);
393: break;
394: default:
395: abort();
1.3 kristaps 396: }
1.15 kristaps 397:
1.11 kristaps 398: texivspace(p);
1.3 kristaps 399: if (NULL != blk)
400: parseto(p, buf, sz, pos, blk);
401: }
402:
403: static void
1.1 kristaps 404: doignblock(struct texi *p, enum texicmd cmd,
405: const char *buf, size_t sz, size_t *pos)
406: {
407:
1.3 kristaps 408: p->ign++;
1.5 kristaps 409: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 410: p->ign--;
1.1 kristaps 411: }
412:
413: static void
1.3 kristaps 414: doblock(struct texi *p, enum texicmd cmd,
1.1 kristaps 415: const char *buf, size_t sz, size_t *pos)
416: {
417:
1.5 kristaps 418: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.1 kristaps 419: }
420:
421: static void
1.12 kristaps 422: doinline(struct texi *p, enum texicmd cmd,
423: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 424: {
1.21 kristaps 425: const char *macro = NULL;
1.12 kristaps 426:
427: switch (cmd) {
1.21 kristaps 428: case (TEXICMD_CODE):
429: case (TEXICMD_KBD):
430: case (TEXICMD_SAMP):
431: case (TEXICMD_T):
432: macro = "Li";
433: break;
434: case (TEXICMD_CITE):
435: case (TEXICMD_DFN):
436: case (TEXICMD_EMPH):
437: case (TEXICMD_I):
438: case (TEXICMD_SLANTED):
439: macro = "Em";
440: break;
441: case (TEXICMD_B):
442: case (TEXICMD_STRONG):
443: macro = "Sy";
444: break;
1.12 kristaps 445: case (TEXICMD_COMMAND):
446: macro = "Xr";
447: break;
448: case (TEXICMD_ENV):
449: macro = "Ev";
450: break;
451: case (TEXICMD_FILE):
452: macro = "Pa";
453: break;
1.16 kristaps 454: case (TEXICMD_OPTION):
455: macro = "Op";
456: break;
1.12 kristaps 457: case (TEXICMD_VAR):
458: macro = "Va";
459: break;
460: default:
1.22 kristaps 461: break;
1.12 kristaps 462: }
463:
1.21 kristaps 464: if (NULL == macro || p->literal) {
1.12 kristaps 465: parsebracket(p, buf, sz, pos);
466: return;
467: }
1.1 kristaps 468:
1.5 kristaps 469: teximacroopen(p, macro);
1.1 kristaps 470: p->seenws = 0;
471: parsebracket(p, buf, sz, pos);
1.11 kristaps 472: texipunctuate(p, buf, sz, pos);
1.5 kristaps 473: teximacroclose(p);
1.1 kristaps 474: }
475:
476: static void
1.16 kristaps 477: doverbinclude(struct texi *p, enum texicmd cmd,
478: const char *buf, size_t sz, size_t *pos)
479: {
480: char fname[PATH_MAX], path[PATH_MAX];
481: int rc;
482: size_t i;
483:
484: while (*pos < sz && ' ' == buf[*pos])
485: advance(p, buf, pos);
486:
487: /* Read in the filename. */
488: for (i = 0; *pos < sz && '\n' != buf[*pos]; i++) {
489: if (i == sizeof(fname) - 1)
490: break;
491: fname[i] = buf[*pos];
492: advance(p, buf, pos);
493: }
494:
495: if (i == 0)
496: texierr(p, "path too short");
497: else if ('\n' != buf[*pos])
498: texierr(p, "path too long");
499: else if ('/' == fname[0])
500: texierr(p, "no absolute paths");
501: fname[i] = '\0';
502:
503: if (strstr(fname, "../") || strstr(fname, "/.."))
504: texierr(p, "insecure path");
505:
506: rc = snprintf(path, sizeof(path),
507: "%s/%s", p->dirs[0], fname);
508: if (rc < 0)
509: texierr(p, "couldn't format path");
510: else if ((size_t)rc >= sizeof(path))
511: texierr(p, "path too long");
512:
513: parsefile(p, path, 0);
514: }
515:
516: static void
1.2 kristaps 517: doinclude(struct texi *p, enum texicmd cmd,
518: const char *buf, size_t sz, size_t *pos)
519: {
520: char fname[PATH_MAX], path[PATH_MAX];
521: size_t i;
522: int rc;
523:
524: while (*pos < sz && ' ' == buf[*pos])
525: advance(p, buf, pos);
526:
527: /* Read in the filename. */
528: for (i = 0; *pos < sz && '\n' != buf[*pos]; i++) {
529: if (i == sizeof(fname) - 1)
530: break;
531: fname[i] = buf[*pos];
532: advance(p, buf, pos);
533: }
534:
535: if (i == 0)
536: texierr(p, "path too short");
537: else if ('\n' != buf[*pos])
538: texierr(p, "path too long");
539: else if ('/' == fname[0])
540: texierr(p, "no absolute paths");
541: fname[i] = '\0';
542:
543: if (strstr(fname, "../") || strstr(fname, "/.."))
544: texierr(p, "insecure path");
545:
1.5 kristaps 546: for (i = 0; i < p->dirsz; i++) {
547: rc = snprintf(path, sizeof(path),
548: "%s/%s", p->dirs[i], fname);
549: if (rc < 0)
550: texierr(p, "couldn't format path");
551: else if ((size_t)rc >= sizeof(path))
552: texierr(p, "path too long");
553: else if (-1 == access(path, R_OK))
554: continue;
555:
1.16 kristaps 556: parsefile(p, path, 1);
1.5 kristaps 557: return;
558: }
1.2 kristaps 559:
1.5 kristaps 560: texierr(p, "couldn't find %s in includes", fname);
1.2 kristaps 561: }
562:
563: static void
1.1 kristaps 564: dobracket(struct texi *p, enum texicmd cmd,
565: const char *buf, size_t sz, size_t *pos)
566: {
567:
568: parsebracket(p, buf, sz, pos);
569: }
570:
571: static void
1.3 kristaps 572: dodisplay(struct texi *p, enum texicmd cmd,
573: const char *buf, size_t sz, size_t *pos)
574: {
575:
1.20 kristaps 576: switch (cmd) {
577: case (TEXICMD_FORMAT):
578: case (TEXICMD_SMALLFORMAT):
579: teximacro(p, "Bd -filled");
580: break;
581: default:
582: teximacro(p, "Bd -filled -offset indent");
583: break;
584: }
585:
1.11 kristaps 586: p->seenvs = 1;
1.12 kristaps 587: /* FIXME: ignore and parseeoln. */
1.3 kristaps 588: advanceeoln(p, buf, sz, pos, 1);
1.13 kristaps 589: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 590: teximacro(p, "Ed");
1.3 kristaps 591: }
592:
593: static void
1.1 kristaps 594: doexample(struct texi *p, enum texicmd cmd,
595: const char *buf, size_t sz, size_t *pos)
596: {
597:
1.5 kristaps 598: teximacro(p, "Bd -literal -offset indent");
1.12 kristaps 599: /* FIXME: ignore and parseeoln. */
1.3 kristaps 600: advanceeoln(p, buf, sz, pos, 1);
601: p->literal++;
1.13 kristaps 602: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 603: p->literal--;
1.5 kristaps 604: teximacro(p, "Ed");
1.1 kristaps 605: }
606:
607: static void
608: dobye(struct texi *p, enum texicmd cmd,
609: const char *buf, size_t sz, size_t *pos)
610: {
611:
612: texiexit(p);
613: exit(EXIT_SUCCESS);
614: }
615:
616: static void
1.10 kristaps 617: dotitle(struct texi *p, enum texicmd cmd,
618: const char *buf, size_t sz, size_t *pos)
619: {
620: size_t start, end;
621:
622: while (*pos < sz && isws(buf[*pos]))
623: advance(p, buf, pos);
624: start = end = *pos;
625: while (end < sz && '\n' != buf[end])
626: end++;
627: free(p->subtitle);
628: p->subtitle = malloc(end - start + 1);
629: memcpy(p->subtitle, &buf[start], end - start);
630: p->subtitle[end - start] = '\0';
631: }
632:
633: static void
1.16 kristaps 634: doaccent(struct texi *p, enum texicmd cmd,
635: const char *buf, size_t sz, size_t *pos)
636: {
637:
638: if (*pos == sz)
639: return;
640: advance(p, buf, pos);
641: switch (cmd) {
642: case (TEXICMD_ACUTE):
643: switch (buf[*pos]) {
644: case ('a'): case ('A'):
645: case ('e'): case ('E'):
646: case ('i'): case ('I'):
647: case ('o'): case ('O'):
648: case ('u'): case ('U'):
649: texiputchars(p, "\\(\'");
650: texiputchar(p, buf[*pos]);
651: break;
652: default:
653: texiputchar(p, buf[*pos]);
654: }
655: break;
656: case (TEXICMD_CIRCUMFLEX):
657: switch (buf[*pos]) {
658: case ('a'): case ('A'):
659: case ('e'): case ('E'):
660: case ('i'): case ('I'):
661: case ('o'): case ('O'):
662: case ('u'): case ('U'):
663: texiputchars(p, "\\(^");
664: texiputchar(p, buf[*pos]);
665: break;
666: default:
667: texiputchar(p, buf[*pos]);
668: }
669: break;
670: case (TEXICMD_GRAVE):
671: switch (buf[*pos]) {
672: case ('a'): case ('A'):
673: case ('e'): case ('E'):
674: case ('i'): case ('I'):
675: case ('o'): case ('O'):
676: case ('u'): case ('U'):
677: texiputchars(p, "\\(`");
678: texiputchar(p, buf[*pos]);
679: break;
680: default:
681: texiputchar(p, buf[*pos]);
682: }
683: break;
684: case (TEXICMD_TILDE):
685: switch (buf[*pos]) {
686: case ('a'): case ('A'):
687: case ('n'): case ('N'):
688: case ('o'): case ('O'):
689: texiputchars(p, "\\(~");
690: texiputchar(p, buf[*pos]);
691: break;
692: default:
693: texiputchar(p, buf[*pos]);
694: }
695: break;
696: case (TEXICMD_UMLAUT):
697: switch (buf[*pos]) {
698: case ('a'): case ('A'):
699: case ('e'): case ('E'):
700: case ('i'): case ('I'):
701: case ('o'): case ('O'):
702: case ('u'): case ('U'):
703: case ('y'):
704: texiputchars(p, "\\(:");
705: texiputchar(p, buf[*pos]);
706: break;
707: default:
708: texiputchar(p, buf[*pos]);
709: }
710: break;
711: default:
712: abort();
713: }
714: }
715:
716: static void
1.1 kristaps 717: dosymbol(struct texi *p, enum texicmd cmd,
718: const char *buf, size_t sz, size_t *pos)
719: {
720:
1.3 kristaps 721: if (p->seenws && p->outcol && 0 == p->literal) {
722: texiputchar(p, ' ');
723: p->seenws = 0;
724: }
725:
1.1 kristaps 726: switch (cmd) {
1.3 kristaps 727: case (TEXICMD_ASTERISK):
728: case (TEXICMD_NEWLINE):
729: case (TEXICMD_SPACE):
1.18 kristaps 730: case (TEXICMD_TABSYM):
1.3 kristaps 731: texiputchar(p, ' ');
732: break;
1.1 kristaps 733: case (TEXICMD_AT):
1.3 kristaps 734: texiputchar(p, '@');
735: break;
736: case (TEXICMD_BANG):
737: texiputchar(p, '!');
1.7 kristaps 738: break;
739: case (TEXICMD_BULLET):
740: texiputchars(p, "\\(bu");
1.1 kristaps 741: break;
742: case (TEXICMD_COPYRIGHT):
743: texiputchars(p, "\\(co");
744: break;
1.2 kristaps 745: case (TEXICMD_DOTS):
746: texiputchars(p, "...");
747: break;
1.15 kristaps 748: case (TEXICMD_ERROR):
749: texiputchars(p, "error\\(->");
1.17 kristaps 750: break;
751: case (TEXICMD_EXPANSION):
752: texiputchars(p, "\\(->");
1.15 kristaps 753: break;
1.1 kristaps 754: case (TEXICMD_LATEX):
755: texiputchars(p, "LaTeX");
756: break;
1.3 kristaps 757: case (TEXICMD_QUESTIONMARK):
758: texiputchar(p, '?');
1.15 kristaps 759: break;
760: case (TEXICMD_RESULT):
761: texiputchars(p, "\\(rA");
1.3 kristaps 762: break;
763: case (TEXICMD_SQUIGGLE_LEFT):
764: texiputchars(p, "{");
765: break;
766: case (TEXICMD_SQUIGGLE_RIGHT):
767: texiputchars(p, "}");
768: break;
1.1 kristaps 769: case (TEXICMD_TEXSYM):
770: texiputchars(p, "TeX");
771: break;
1.3 kristaps 772: case (TEXICMD_COLON):
773: case (TEXICMD_HYPHEN):
774: break;
1.1 kristaps 775: default:
1.5 kristaps 776: texiwarn(p, "sym: %d", cmd);
1.1 kristaps 777: abort();
778: }
779:
1.5 kristaps 780: if (texitoks[cmd].len > 1)
781: doignbracket(p, cmd, buf, sz, pos);
1.1 kristaps 782: }
783:
784: static void
785: doquotation(struct texi *p, enum texicmd cmd,
786: const char *buf, size_t sz, size_t *pos)
787: {
788:
1.5 kristaps 789: teximacro(p, "Qo");
1.1 kristaps 790: parseto(p, buf, sz, pos, "quotation");
1.5 kristaps 791: teximacro(p, "Qc");
1.1 kristaps 792: }
793:
1.3 kristaps 794: static void
795: domath(struct texi *p, enum texicmd cmd,
796: const char *buf, size_t sz, size_t *pos)
797: {
798: size_t nest;
799:
800: /*
801: * Math handling is different from everything else.
802: * We don't allow any subcomponents, and we ignore the rules in
803: * terms of @-commands.
804: * This departs from GNU's rules, but whatever.
805: */
806: while (*pos < sz && isws(buf[*pos]))
807: advance(p, buf, pos);
808: if (*pos == sz || '{' != buf[*pos])
809: return;
810: advance(p, buf, pos);
811: if (p->seenws && p->outcol && 0 == p->literal)
812: texiputchar(p, ' ');
813: p->seenws = 0;
814: for (nest = 1; *pos < sz && nest > 0; ) {
815: if ('{' == buf[*pos])
816: nest++;
817: else if ('}' == buf[*pos])
818: if (0 == --nest)
819: continue;
820: texiputchar(p, buf[*pos]);
821: advance(p, buf, pos);
822: }
823: if (*pos == sz)
824: return;
825: assert('}' == buf[*pos]);
826: advance(p, buf, pos);
1.24 ! kristaps 827: }
! 828:
! 829: static void
! 830: dovalue(struct texi *p, enum texicmd cmd,
! 831: const char *buf, size_t sz, size_t *pos)
! 832: {
! 833: size_t i, start, end;
! 834: int clr;
! 835: char *key, *val;
! 836:
! 837: if (TEXICMD_SET == cmd) {
! 838: /*
! 839: * If we're going to set a value, then first parse the
! 840: * key and value from the input stream.
! 841: * Disallow empty keys.
! 842: */
! 843: while (*pos < sz && isws(buf[*pos]))
! 844: advance(p, buf, pos);
! 845: for (start = end = *pos; end < sz; end++)
! 846: if (ismspace(buf[end]))
! 847: break;
! 848: if (start == end)
! 849: return;
! 850:
! 851: key = malloc(end - start + 1);
! 852: if (NULL == key) {
! 853: perror(NULL);
! 854: exit(EXIT_FAILURE);
! 855: }
! 856: memcpy(key, &buf[start], end - start);
! 857: key[end - start] = '\0';
! 858:
! 859: advanceto(p, buf, pos, end);
! 860:
! 861: while (*pos < sz && isws(buf[*pos]))
! 862: advance(p, buf, pos);
! 863: for (start = end = *pos; end < sz; end++)
! 864: if ('\n' == buf[end])
! 865: break;
! 866:
! 867: val = malloc(end - start + 1);
! 868: if (NULL == val) {
! 869: perror(NULL);
! 870: exit(EXIT_FAILURE);
! 871: }
! 872: memcpy(val, &buf[start], end - start);
! 873: val[end - start] = '\0';
! 874:
! 875: /*
! 876: * Now look up the key in our table of existing keys.
! 877: * If we find it, then free the local key and replace
! 878: * the value (freeing the existing) of the existing.
! 879: * If not, reallocate the structure.
! 880: */
! 881: for (i = 0; i < p->valsz; i++)
! 882: if (0 == strcmp(p->vals[i].key, key))
! 883: break;
! 884:
! 885: if (i < p->valsz) {
! 886: free(key);
! 887: free(p->vals[i].value);
! 888: p->vals[i].value = val;
! 889: } else {
! 890: p->vals = realloc(p->vals,
! 891: (p->valsz + 1) *
! 892: sizeof(struct texivalue));
! 893: if (NULL == p->vals) {
! 894: perror(NULL);
! 895: exit(EXIT_FAILURE);
! 896: }
! 897: p->vals[p->valsz].key = key;
! 898: p->vals[p->valsz].value = val;
! 899: p->valsz++;
! 900: }
! 901:
! 902: advanceeoln(p, buf, sz, pos, 1);
! 903: } else if (TEXICMD_VALUE == cmd) {
! 904: /*
! 905: * First, parse out the key from the stream, which is
! 906: * just the material within the quotes.
! 907: */
! 908: while (*pos < sz && '{' != buf[*pos])
! 909: advance(p, buf, pos);
! 910: if (*pos == sz)
! 911: return;
! 912: advance(p, buf, pos);
! 913: start = *pos;
! 914: while (*pos < sz && '}' != buf[*pos])
! 915: advance(p, buf, pos);
! 916: if ((end = *pos) < sz)
! 917: advance(p, buf, pos);
! 918:
! 919: key = malloc(end - start + 1);
! 920: if (NULL == key) {
! 921: perror(NULL);
! 922: exit(EXIT_FAILURE);
! 923: }
! 924: memcpy(key, &buf[start], end - start);
! 925: key[end - start] = '\0';
! 926:
! 927: /*
! 928: * Next, look up the key in our table.
! 929: * If it exists, print it out, being mindful all the
! 930: * while of prior whitespace.
! 931: * If it doesn't, print out a message.
! 932: */
! 933: for (i = 0; i < p->valsz; i++)
! 934: if (0 == strcmp(p->vals[i].key, key))
! 935: break;
! 936: if (p->seenws)
! 937: texiputchar(p, ' ');
! 938: p->seenws = 0;
! 939: if (i == p->valsz || NULL == p->vals[i].value) {
! 940: texiputchars(p, "{No value for \\(lq");
! 941: texiputchars(p, key);
! 942: texiputchars(p, "\\(rq}");
! 943: } else
! 944: texiputchars(p, p->vals[i].value);
! 945: free(key);
! 946: } else if (TEXICMD_CLEAR == cmd) {
! 947: key = lineargdup(p, buf, sz, pos);
! 948: for (i = 0; i < p->valsz; i++)
! 949: if (0 == strcmp(p->vals[i].key, key)) {
! 950: free(p->vals[i].value);
! 951: p->vals[i].value = NULL;
! 952: break;
! 953: }
! 954: free(key);
! 955: } else if (TEXICMD_IFCLEAR == cmd) {
! 956: key = lineargdup(p, buf, sz, pos);
! 957: for (i = 0; i < p->valsz; i++)
! 958: if (0 == strcmp(p->vals[i].key, key))
! 959: break;
! 960: free(key);
! 961: clr = i < p->valsz && NULL != p->vals[i].value;
! 962: if (clr)
! 963: p->ign++;
! 964: parseto(p, buf, sz, pos, texitoks[cmd].tok);
! 965: if (clr)
! 966: p->ign--;
! 967: }
1.3 kristaps 968: }
969:
1.1 kristaps 970: static void
1.8 kristaps 971: dolink(struct texi *p, enum texicmd cmd,
1.1 kristaps 972: const char *buf, size_t sz, size_t *pos)
973: {
1.8 kristaps 974: int c;
1.1 kristaps 975:
976: switch (cmd) {
977: case (TEXICMD_EMAIL):
1.5 kristaps 978: teximacroopen(p, "Mt");
1.1 kristaps 979: break;
1.3 kristaps 980: case (TEXICMD_UREF):
1.1 kristaps 981: case (TEXICMD_URL):
1.5 kristaps 982: teximacroopen(p, "Lk");
1.1 kristaps 983: break;
1.8 kristaps 984: case (TEXICMD_XREF):
985: texiputchars(p, "See Section");
986: teximacroopen(p, "Qq");
987: break;
988: case (TEXICMD_PXREF):
989: texiputchars(p, "see Section");
990: teximacroopen(p, "Qq");
991: break;
1.1 kristaps 992: default:
1.8 kristaps 993: abort();
1.1 kristaps 994: }
1.8 kristaps 995:
996: c = parsearg(p, buf, sz, pos, 0);
997: p->ign++;
998: while (c > 0)
999: c = parsearg(p, buf, sz, pos, 1);
1000: p->ign--;
1001:
1.11 kristaps 1002: texipunctuate(p, buf, sz, pos);
1.8 kristaps 1003: teximacroclose(p);
1004: }
1005:
1006: static void
1007: doignargn(struct texi *p, enum texicmd cmd,
1008: const char *buf, size_t sz, size_t *pos)
1009: {
1010: int c;
1011:
1012: c = parsearg(p, buf, sz, pos, 0);
1013: p->ign++;
1014: while (c > 0)
1015: c = parsearg(p, buf, sz, pos, 1);
1016: p->ign--;
1.1 kristaps 1017: }
1018:
1.23 kristaps 1019: /*
1020: * Sections can be made subsections and so on by way of the
1021: * @raiseections and @lowersections commands.
1022: * Perform this check here and return the actual section number adjusted
1023: * to the raise level.
1024: */
1025: static int
1026: sectioner(struct texi *p, int sec)
1027: {
1028:
1029: if ((sec -= p->secoffs) < 0) {
1030: texiwarn(p, "section below minimum, clamping");
1031: return(0);
1032: } else if (sec >= SECTSZ) {
1033: texiwarn(p, "section above maximum, clamping");
1034: return(SECTSZ - 1);
1035: }
1036: return(sec);
1037: }
1038:
1039: static void
1040: dosubsubsection(struct texi *p, enum texicmd cmd,
1041: const char *buf, size_t sz, size_t *pos)
1042: {
1043: int sec;
1044:
1045: sec = sectioner(p, 3);
1046:
1047: /* We don't have a subsubsubsection, so make one up. */
1048: texivspace(p);
1049: teximacroopen(p, sects[sec]);
1050: parseeoln(p, buf, sz, pos);
1051: teximacroclose(p);
1052: texivspace(p);
1053: }
1054:
1.1 kristaps 1055: static void
1056: dosubsection(struct texi *p, enum texicmd cmd,
1057: const char *buf, size_t sz, size_t *pos)
1058: {
1.23 kristaps 1059: int sec;
1060:
1061: sec = sectioner(p, 2);
1.13 kristaps 1062:
1063: if (p->outmacro)
1.23 kristaps 1064: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13 kristaps 1065: else if (p->literal)
1.23 kristaps 1066: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1 kristaps 1067:
1.21 kristaps 1068: /* We don't have a subsubsection, so make one up. */
1.11 kristaps 1069: texivspace(p);
1.23 kristaps 1070: teximacroopen(p, sects[sec]);
1.3 kristaps 1071: parseeoln(p, buf, sz, pos);
1.5 kristaps 1072: teximacroclose(p);
1.11 kristaps 1073: texivspace(p);
1.1 kristaps 1074: }
1075:
1076: static void
1.23 kristaps 1077: dosecoffs(struct texi *p, enum texicmd cmd,
1078: const char *buf, size_t sz, size_t *pos)
1079: {
1080:
1081: if (TEXICMD_RAISESECTIONS == cmd)
1082: p->secoffs++;
1083: else
1084: p->secoffs--;
1085: }
1086:
1087: static void
1.1 kristaps 1088: dosection(struct texi *p, enum texicmd cmd,
1.23 kristaps 1089: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 1090: {
1.23 kristaps 1091: int sec;
1.12 kristaps 1092:
1093: switch (cmd) {
1094: case (TEXICMD_APPENDIX):
1095: case (TEXICMD_CHAPTER):
1096: case (TEXICMD_TOP):
1097: case (TEXICMD_UNNUMBERED):
1.23 kristaps 1098: sec = sectioner(p, 0);
1.12 kristaps 1099: break;
1100: case (TEXICMD_APPENDIXSEC):
1101: case (TEXICMD_HEADING):
1102: case (TEXICMD_SECTION):
1103: case (TEXICMD_UNNUMBEREDSEC):
1.23 kristaps 1104: sec = sectioner(p, 1);
1.12 kristaps 1105: break;
1106: default:
1107: abort();
1108: }
1.1 kristaps 1109:
1.3 kristaps 1110: if (p->outmacro)
1.23 kristaps 1111: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3 kristaps 1112: else if (p->literal)
1.23 kristaps 1113: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3 kristaps 1114:
1.23 kristaps 1115: teximacroopen(p, sects[sec]);
1.3 kristaps 1116: parseeoln(p, buf, sz, pos);
1117: teximacroclose(p);
1.11 kristaps 1118: p->seenvs = 1;
1.3 kristaps 1119: }
1120:
1121: static void
1122: dosp(struct texi *p, enum texicmd cmd,
1123: const char *buf, size_t sz, size_t *pos)
1124: {
1125:
1.11 kristaps 1126: texivspace(p);
1.12 kristaps 1127: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1128: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1129: }
1130:
1131: static void
1132: dotop(struct texi *p, enum texicmd cmd,
1133: const char *buf, size_t sz, size_t *pos)
1134: {
1.10 kristaps 1135: const char *cp;
1136: time_t t;
1137: char date[32];
1138:
1.18 kristaps 1139: /*
1140: * Here we print our standard mdoc(7) prologue.
1141: * We use the title set with @settitle for the `Nd' description
1142: * and the source document filename (the first one as invoked on
1143: * the command line) for the title.
1144: * The date is set to the current date.
1145: */
1.10 kristaps 1146: t = time(NULL);
1147: strftime(date, sizeof(date), "%F", localtime(&t));
1.1 kristaps 1148:
1.3 kristaps 1149: p->ign--;
1.10 kristaps 1150: teximacroopen(p, "Dd");
1151: texiputchars(p, date);
1152: teximacroclose(p);
1153: teximacroopen(p, "Dt");
1154: for (cp = p->title; '\0' != *cp; cp++)
1155: texiputchar(p, toupper(*cp));
1.11 kristaps 1156: texiputchars(p, " 7");
1.10 kristaps 1157: teximacroclose(p);
1.5 kristaps 1158: teximacro(p, "Os");
1159: teximacro(p, "Sh NAME");
1.10 kristaps 1160: teximacroopen(p, "Nm");
1161: texiputchars(p, p->title);
1162: teximacroclose(p);
1163: teximacroopen(p, "Nd");
1164: texiputchars(p, NULL != p->subtitle ?
1165: p->subtitle : "Unknown description");
1166: teximacroclose(p);
1.11 kristaps 1167: p->seenvs = 1;
1.12 kristaps 1168: dosection(p, cmd, buf, sz, pos);
1.1 kristaps 1169: }
1170:
1171: static void
1172: doitem(struct texi *p, enum texicmd cmd,
1173: const char *buf, size_t sz, size_t *pos)
1174: {
1175:
1.18 kristaps 1176: /* Multitable is using raw tbl(7). */
1177: if (TEXILIST_TABLE == p->list) {
1178: texiputchar(p, '\n');
1179: return;
1180: }
1181:
1.3 kristaps 1182: if (p->outmacro)
1183: texierr(p, "item in open line scope!?");
1184: else if (p->literal)
1185: texierr(p, "item in a literal scope!?");
1186:
1187: switch (p->list) {
1188: case (TEXILIST_ITEM):
1.5 kristaps 1189: teximacroopen(p, "It");
1.3 kristaps 1190: break;
1191: case (TEXILIST_NOITEM):
1.5 kristaps 1192: teximacro(p, "It");
1.3 kristaps 1193: break;
1194: default:
1.11 kristaps 1195: texivspace(p);
1.3 kristaps 1196: break;
1197: }
1.18 kristaps 1198:
1199: /* Trick so we don't start with Pp. */
1.11 kristaps 1200: p->seenvs = 1;
1.3 kristaps 1201: parseeoln(p, buf, sz, pos);
1.1 kristaps 1202:
1.3 kristaps 1203: if (TEXILIST_ITEM == p->list)
1204: teximacroclose(p);
1.9 kristaps 1205: else if (p->outcol > 0)
1.1 kristaps 1206: texiputchar(p, '\n');
1.18 kristaps 1207: }
1208:
1209: static void
1210: dotab(struct texi *p, enum texicmd cmd,
1211: const char *buf, size_t sz, size_t *pos)
1212: {
1213:
1214: /* This command is only useful in @multitable. */
1215: if (TEXILIST_TABLE == p->list)
1216: texiputchar(p, '\t');
1217: }
1218:
1219: static void
1220: domultitable(struct texi *p, enum texicmd cmd,
1221: const char *buf, size_t sz, size_t *pos)
1222: {
1223: enum texilist sv = p->list;
1224: enum texicmd type;
1225: size_t i, end, columns;
1226:
1227: p->list = TEXILIST_TABLE;
1228: teximacro(p, "TS");
1229: columns = 0;
1230:
1231: /* Advance to the first argument... */
1232: while (*pos < sz && isws(buf[*pos]))
1233: advance(p, buf, pos);
1234:
1235: /* Make sure we don't print anything when scanning. */
1236: p->ign++;
1237: if ('@' == buf[*pos]) {
1238: /*
1239: * Look for @columnfractions.
1240: * We ignore these, but we do use the number of
1241: * arguments to set the number of columns that we'll
1242: * have.
1243: */
1244: type = texicmd(p, buf, *pos, sz, &end);
1245: advanceto(p, buf, pos, end);
1246: if (TEXICMD_COLUMNFRACTIONS != type)
1247: texierr(p, "unknown multitable type");
1248: while (*pos < sz && '\n' != buf[*pos]) {
1249: while (*pos < sz && isws(buf[*pos]))
1250: advance(p, buf, pos);
1251: while (*pos < sz && ! isws(buf[*pos])) {
1252: if ('\n' == buf[*pos])
1253: break;
1254: advance(p, buf, pos);
1255: }
1256: columns++;
1257: }
1258: } else
1259: /*
1260: * We have arguments.
1261: * We could parse these, but it's easier to just let
1262: * tbl(7) figure it out.
1263: * So use this only to count arguments.
1264: */
1265: while (parselinearg(p, buf, sz, pos) > 0)
1266: columns++;
1267: p->ign--;
1268:
1269: /* Left-justify each table entry. */
1270: for (i = 0; i < columns; i++) {
1271: if (i > 0)
1272: texiputchar(p, ' ');
1273: texiputchar(p, 'l');
1274: }
1275: texiputchars(p, ".\n");
1276: p->outmacro++;
1277: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1278: p->outmacro--;
1279: teximacro(p, "TE");
1280: p->list = sv;
1.1 kristaps 1281: }
1282:
1283: static void
1284: dotable(struct texi *p, enum texicmd cmd,
1285: const char *buf, size_t sz, size_t *pos)
1286: {
1.3 kristaps 1287: enum texilist sv = p->list;
1288:
1289: p->list = TEXILIST_ITEM;
1.5 kristaps 1290: teximacro(p, "Bl -tag -width Ds");
1.12 kristaps 1291: /* FIXME: ignore and parseeoln. */
1292: advanceeoln(p, buf, sz, pos, 1);
1.11 kristaps 1293: p->seenvs = 1;
1.20 kristaps 1294: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 1295: teximacro(p, "El");
1.3 kristaps 1296: p->list = sv;
1.1 kristaps 1297: }
1298:
1299: static void
1.2 kristaps 1300: doenumerate(struct texi *p, enum texicmd cmd,
1301: const char *buf, size_t sz, size_t *pos)
1302: {
1.3 kristaps 1303: enum texilist sv = p->list;
1.2 kristaps 1304:
1.3 kristaps 1305: p->list = TEXILIST_NOITEM;
1.5 kristaps 1306: teximacro(p, "Bl -enum");
1.11 kristaps 1307: p->seenvs = 1;
1.12 kristaps 1308: /* FIXME: ignore and parseeoln. */
1309: advanceeoln(p, buf, sz, pos, 1);
1.2 kristaps 1310: parseto(p, buf, sz, pos, "enumerate");
1.5 kristaps 1311: teximacro(p, "El");
1.3 kristaps 1312: p->list = sv;
1.2 kristaps 1313: }
1314:
1315: static void
1.1 kristaps 1316: doitemize(struct texi *p, enum texicmd cmd,
1317: const char *buf, size_t sz, size_t *pos)
1318: {
1.3 kristaps 1319: enum texilist sv = p->list;
1.1 kristaps 1320:
1.21 kristaps 1321: p->list = TEXILIST_NOITEM;
1.5 kristaps 1322: teximacro(p, "Bl -bullet");
1.11 kristaps 1323: p->seenvs = 1;
1.12 kristaps 1324: /* FIXME: ignore and parseeoln. */
1325: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1326: parseto(p, buf, sz, pos, "itemize");
1.5 kristaps 1327: teximacro(p, "El");
1.3 kristaps 1328: p->list = sv;
1.1 kristaps 1329: }
1330:
1331: static void
1332: doignbracket(struct texi *p, enum texicmd cmd,
1333: const char *buf, size_t sz, size_t *pos)
1334: {
1335:
1.3 kristaps 1336: p->ign++;
1.1 kristaps 1337: parsebracket(p, buf, sz, pos);
1.3 kristaps 1338: p->ign--;
1.1 kristaps 1339: }
1340:
1341: static void
1342: doignline(struct texi *p, enum texicmd cmd,
1343: const char *buf, size_t sz, size_t *pos)
1344: {
1345:
1.12 kristaps 1346: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1347: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1348: }
1349:
1.8 kristaps 1350: /*
1351: * Parse colon-separated directories from "cp" (if not NULL) and returns
1352: * the array of pointers.
1353: * Prepends "base" to the array.
1354: * This does NOT sanitise the directories!
1355: */
1.5 kristaps 1356: static char **
1357: parsedirs(const char *base, const char *cp, size_t *sz)
1358: {
1359: char *tok, *str, *tofree;
1360: const char *cpp;
1361: size_t i;
1362: char **dirs;
1363:
1364: *sz = NULL != (cpp = cp) ? 2 : 1;
1365: if (*sz > 1)
1366: for ( ; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1367: cpp++;
1368:
1369: dirs = calloc(*sz, sizeof(char *));
1370: if (NULL == dirs) {
1371: perror(NULL);
1372: exit(EXIT_FAILURE);
1373: } else if (NULL == (dirs[0] = strdup(base))) {
1374: perror(NULL);
1375: exit(EXIT_FAILURE);
1376: }
1377:
1378: if (NULL == cp)
1379: return(dirs);
1380:
1381: if (NULL == (tofree = tok = str = strdup(cp))) {
1382: perror(NULL);
1383: exit(EXIT_FAILURE);
1384: }
1385:
1386: for (i = 1; NULL != (tok = strsep(&str, ":")); i++)
1387: if (NULL == (dirs[i] = strdup(tok))) {
1388: perror(NULL);
1389: exit(EXIT_FAILURE);
1390: }
1391:
1392: free(tofree);
1393: return(dirs);
1394: }
1395:
1.1 kristaps 1396: int
1397: main(int argc, char *argv[])
1398: {
1399: struct texi texi;
1.2 kristaps 1400: int c;
1401: char *path, *dir;
1.10 kristaps 1402: const char *progname, *Idir, *cp;
1.1 kristaps 1403:
1404: progname = strrchr(argv[0], '/');
1405: if (progname == NULL)
1406: progname = argv[0];
1407: else
1408: ++progname;
1409:
1.10 kristaps 1410: memset(&texi, 0, sizeof(struct texi));
1.5 kristaps 1411: Idir = NULL;
1.10 kristaps 1412:
1.5 kristaps 1413: while (-1 != (c = getopt(argc, argv, "I:")))
1.1 kristaps 1414: switch (c) {
1.5 kristaps 1415: case ('I'):
1416: Idir = optarg;
1417: break;
1.1 kristaps 1418: default:
1419: goto usage;
1420: }
1421:
1422: argv += optind;
1423: if (0 == (argc -= optind))
1424: goto usage;
1425:
1.2 kristaps 1426: if (NULL == (path = strdup(argv[0]))) {
1427: perror(NULL);
1428: exit(EXIT_FAILURE);
1429: } else if (NULL == (dir = dirname(path))) {
1430: perror(argv[0]);
1431: free(path);
1432: exit(EXIT_FAILURE);
1433: }
1434: free(path);
1435:
1.10 kristaps 1436: if (NULL != (cp = strrchr(argv[0], '/')))
1437: texi.title = strdup(cp + 1);
1438: else
1439: texi.title = strdup(argv[0]);
1440:
1441: if (NULL == texi.title) {
1442: perror(NULL);
1443: exit(EXIT_FAILURE);
1444: } else if (NULL != (path = strchr(texi.title, '.')))
1445: *path = '\0';
1446:
1.3 kristaps 1447: texi.ign = 1;
1.5 kristaps 1448: texi.dirs = parsedirs(dir, Idir, &texi.dirsz);
1.16 kristaps 1449: parsefile(&texi, argv[0], 1);
1.5 kristaps 1450: /* We shouldn't get here. */
1.2 kristaps 1451: texiexit(&texi);
1452: return(EXIT_FAILURE);
1.1 kristaps 1453: usage:
1.8 kristaps 1454: fprintf(stderr, "usage: %s [-Idirs] file\n", progname);
1.1 kristaps 1455: return(EXIT_FAILURE);
1456: }
CVSweb