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