Annotation of texi2mdoc/main.c, Revision 1.28
1.28 ! kristaps 1: /* $Id: main.c,v 1.27 2015/02/21 17:00:33 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");
! 1017: p->valstack[p->valstackpos++] = cp;
! 1018: parseeof(p, cp, strlen(cp));
! 1019: p->valstackpos--;
! 1020: } else
1.25 kristaps 1021: texiputchars(p, "{No value}");
1.24 kristaps 1022: } else if (TEXICMD_IFCLEAR == cmd) {
1.26 kristaps 1023: if (NULL != valuellookup(p, buf, sz, pos))
1024: doignblock(p, cmd, buf, sz, pos);
1025: else
1026: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.25 kristaps 1027: } else if (TEXICMD_CLEAR == cmd)
1028: valuelclear(p, buf, sz, pos);
1.3 kristaps 1029: }
1030:
1.1 kristaps 1031: static void
1.8 kristaps 1032: dolink(struct texi *p, enum texicmd cmd,
1.1 kristaps 1033: const char *buf, size_t sz, size_t *pos)
1034: {
1.8 kristaps 1035: int c;
1.1 kristaps 1036:
1037: switch (cmd) {
1038: case (TEXICMD_EMAIL):
1.5 kristaps 1039: teximacroopen(p, "Mt");
1.1 kristaps 1040: break;
1.3 kristaps 1041: case (TEXICMD_UREF):
1.1 kristaps 1042: case (TEXICMD_URL):
1.5 kristaps 1043: teximacroopen(p, "Lk");
1.1 kristaps 1044: break;
1.8 kristaps 1045: case (TEXICMD_XREF):
1046: texiputchars(p, "See Section");
1047: teximacroopen(p, "Qq");
1048: break;
1049: case (TEXICMD_PXREF):
1050: texiputchars(p, "see Section");
1051: teximacroopen(p, "Qq");
1052: break;
1.1 kristaps 1053: default:
1.8 kristaps 1054: abort();
1.1 kristaps 1055: }
1.8 kristaps 1056:
1057: c = parsearg(p, buf, sz, pos, 0);
1058: p->ign++;
1059: while (c > 0)
1060: c = parsearg(p, buf, sz, pos, 1);
1061: p->ign--;
1062:
1.11 kristaps 1063: texipunctuate(p, buf, sz, pos);
1.8 kristaps 1064: teximacroclose(p);
1065: }
1066:
1067: static void
1068: doignargn(struct texi *p, enum texicmd cmd,
1069: const char *buf, size_t sz, size_t *pos)
1070: {
1071: int c;
1072:
1073: c = parsearg(p, buf, sz, pos, 0);
1074: p->ign++;
1075: while (c > 0)
1076: c = parsearg(p, buf, sz, pos, 1);
1077: p->ign--;
1.1 kristaps 1078: }
1079:
1.23 kristaps 1080: /*
1081: * Sections can be made subsections and so on by way of the
1082: * @raiseections and @lowersections commands.
1083: * Perform this check here and return the actual section number adjusted
1084: * to the raise level.
1085: */
1086: static int
1087: sectioner(struct texi *p, int sec)
1088: {
1089:
1090: if ((sec -= p->secoffs) < 0) {
1091: texiwarn(p, "section below minimum, clamping");
1092: return(0);
1093: } else if (sec >= SECTSZ) {
1094: texiwarn(p, "section above maximum, clamping");
1095: return(SECTSZ - 1);
1096: }
1097: return(sec);
1098: }
1099:
1100: static void
1101: dosubsubsection(struct texi *p, enum texicmd cmd,
1102: const char *buf, size_t sz, size_t *pos)
1103: {
1104: int sec;
1105:
1106: sec = sectioner(p, 3);
1107:
1108: /* We don't have a subsubsubsection, so make one up. */
1109: texivspace(p);
1110: teximacroopen(p, sects[sec]);
1111: parseeoln(p, buf, sz, pos);
1112: teximacroclose(p);
1113: texivspace(p);
1114: }
1115:
1.1 kristaps 1116: static void
1117: dosubsection(struct texi *p, enum texicmd cmd,
1118: const char *buf, size_t sz, size_t *pos)
1119: {
1.23 kristaps 1120: int sec;
1121:
1122: sec = sectioner(p, 2);
1.13 kristaps 1123:
1124: if (p->outmacro)
1.23 kristaps 1125: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13 kristaps 1126: else if (p->literal)
1.23 kristaps 1127: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1 kristaps 1128:
1.21 kristaps 1129: /* We don't have a subsubsection, so make one up. */
1.11 kristaps 1130: texivspace(p);
1.23 kristaps 1131: teximacroopen(p, sects[sec]);
1.3 kristaps 1132: parseeoln(p, buf, sz, pos);
1.5 kristaps 1133: teximacroclose(p);
1.11 kristaps 1134: texivspace(p);
1.1 kristaps 1135: }
1136:
1137: static void
1.23 kristaps 1138: dosecoffs(struct texi *p, enum texicmd cmd,
1139: const char *buf, size_t sz, size_t *pos)
1140: {
1141:
1142: if (TEXICMD_RAISESECTIONS == cmd)
1143: p->secoffs++;
1144: else
1145: p->secoffs--;
1146: }
1147:
1148: static void
1.1 kristaps 1149: dosection(struct texi *p, enum texicmd cmd,
1.23 kristaps 1150: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 1151: {
1.23 kristaps 1152: int sec;
1.12 kristaps 1153:
1154: switch (cmd) {
1155: case (TEXICMD_APPENDIX):
1156: case (TEXICMD_CHAPTER):
1157: case (TEXICMD_TOP):
1158: case (TEXICMD_UNNUMBERED):
1.23 kristaps 1159: sec = sectioner(p, 0);
1.12 kristaps 1160: break;
1161: case (TEXICMD_APPENDIXSEC):
1162: case (TEXICMD_HEADING):
1163: case (TEXICMD_SECTION):
1164: case (TEXICMD_UNNUMBEREDSEC):
1.23 kristaps 1165: sec = sectioner(p, 1);
1.12 kristaps 1166: break;
1167: default:
1168: abort();
1169: }
1.1 kristaps 1170:
1.3 kristaps 1171: if (p->outmacro)
1.23 kristaps 1172: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3 kristaps 1173: else if (p->literal)
1.23 kristaps 1174: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3 kristaps 1175:
1.23 kristaps 1176: teximacroopen(p, sects[sec]);
1.3 kristaps 1177: parseeoln(p, buf, sz, pos);
1178: teximacroclose(p);
1.11 kristaps 1179: p->seenvs = 1;
1.3 kristaps 1180: }
1181:
1182: static void
1183: dosp(struct texi *p, enum texicmd cmd,
1184: const char *buf, size_t sz, size_t *pos)
1185: {
1186:
1.11 kristaps 1187: texivspace(p);
1.12 kristaps 1188: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1189: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1190: }
1191:
1192: static void
1193: dotop(struct texi *p, enum texicmd cmd,
1194: const char *buf, size_t sz, size_t *pos)
1195: {
1.10 kristaps 1196: const char *cp;
1197: time_t t;
1198: char date[32];
1199:
1.26 kristaps 1200: if (--p->ign)
1201: texierr(p, "@top command while ignoring (%d)", p->ign);
1202:
1.18 kristaps 1203: /*
1204: * Here we print our standard mdoc(7) prologue.
1205: * We use the title set with @settitle for the `Nd' description
1206: * and the source document filename (the first one as invoked on
1207: * the command line) for the title.
1208: * The date is set to the current date.
1209: */
1.10 kristaps 1210: t = time(NULL);
1211: strftime(date, sizeof(date), "%F", localtime(&t));
1.1 kristaps 1212:
1.10 kristaps 1213: teximacroopen(p, "Dd");
1214: texiputchars(p, date);
1215: teximacroclose(p);
1216: teximacroopen(p, "Dt");
1217: for (cp = p->title; '\0' != *cp; cp++)
1218: texiputchar(p, toupper(*cp));
1.11 kristaps 1219: texiputchars(p, " 7");
1.10 kristaps 1220: teximacroclose(p);
1.5 kristaps 1221: teximacro(p, "Os");
1222: teximacro(p, "Sh NAME");
1.10 kristaps 1223: teximacroopen(p, "Nm");
1224: texiputchars(p, p->title);
1225: teximacroclose(p);
1226: teximacroopen(p, "Nd");
1227: texiputchars(p, NULL != p->subtitle ?
1228: p->subtitle : "Unknown description");
1229: teximacroclose(p);
1.11 kristaps 1230: p->seenvs = 1;
1.12 kristaps 1231: dosection(p, cmd, buf, sz, pos);
1.1 kristaps 1232: }
1233:
1234: static void
1235: doitem(struct texi *p, enum texicmd cmd,
1236: const char *buf, size_t sz, size_t *pos)
1237: {
1238:
1.18 kristaps 1239: /* Multitable is using raw tbl(7). */
1240: if (TEXILIST_TABLE == p->list) {
1241: texiputchar(p, '\n');
1242: return;
1243: }
1244:
1.3 kristaps 1245: if (p->outmacro)
1246: texierr(p, "item in open line scope!?");
1247: else if (p->literal)
1248: texierr(p, "item in a literal scope!?");
1249:
1250: switch (p->list) {
1251: case (TEXILIST_ITEM):
1.5 kristaps 1252: teximacroopen(p, "It");
1.3 kristaps 1253: break;
1254: case (TEXILIST_NOITEM):
1.5 kristaps 1255: teximacro(p, "It");
1.3 kristaps 1256: break;
1257: default:
1.11 kristaps 1258: texivspace(p);
1.3 kristaps 1259: break;
1260: }
1.18 kristaps 1261:
1262: /* Trick so we don't start with Pp. */
1.11 kristaps 1263: p->seenvs = 1;
1.3 kristaps 1264: parseeoln(p, buf, sz, pos);
1.1 kristaps 1265:
1.3 kristaps 1266: if (TEXILIST_ITEM == p->list)
1267: teximacroclose(p);
1.9 kristaps 1268: else if (p->outcol > 0)
1.1 kristaps 1269: texiputchar(p, '\n');
1.18 kristaps 1270: }
1271:
1272: static void
1273: dotab(struct texi *p, enum texicmd cmd,
1274: const char *buf, size_t sz, size_t *pos)
1275: {
1276:
1277: /* This command is only useful in @multitable. */
1278: if (TEXILIST_TABLE == p->list)
1279: texiputchar(p, '\t');
1280: }
1281:
1282: static void
1283: domultitable(struct texi *p, enum texicmd cmd,
1284: const char *buf, size_t sz, size_t *pos)
1285: {
1286: enum texilist sv = p->list;
1287: enum texicmd type;
1288: size_t i, end, columns;
1289:
1290: p->list = TEXILIST_TABLE;
1291: teximacro(p, "TS");
1292: columns = 0;
1293:
1294: /* Advance to the first argument... */
1295: while (*pos < sz && isws(buf[*pos]))
1296: advance(p, buf, pos);
1297:
1298: /* Make sure we don't print anything when scanning. */
1299: p->ign++;
1300: if ('@' == buf[*pos]) {
1301: /*
1302: * Look for @columnfractions.
1303: * We ignore these, but we do use the number of
1304: * arguments to set the number of columns that we'll
1305: * have.
1306: */
1307: type = texicmd(p, buf, *pos, sz, &end);
1308: advanceto(p, buf, pos, end);
1309: if (TEXICMD_COLUMNFRACTIONS != type)
1.25 kristaps 1310: texierr(p, "unknown multitable command");
1.18 kristaps 1311: while (*pos < sz && '\n' != buf[*pos]) {
1312: while (*pos < sz && isws(buf[*pos]))
1313: advance(p, buf, pos);
1314: while (*pos < sz && ! isws(buf[*pos])) {
1315: if ('\n' == buf[*pos])
1316: break;
1317: advance(p, buf, pos);
1318: }
1319: columns++;
1320: }
1321: } else
1322: /*
1323: * We have arguments.
1324: * We could parse these, but it's easier to just let
1325: * tbl(7) figure it out.
1326: * So use this only to count arguments.
1327: */
1328: while (parselinearg(p, buf, sz, pos) > 0)
1329: columns++;
1330: p->ign--;
1331:
1332: /* Left-justify each table entry. */
1333: for (i = 0; i < columns; i++) {
1334: if (i > 0)
1335: texiputchar(p, ' ');
1336: texiputchar(p, 'l');
1337: }
1338: texiputchars(p, ".\n");
1339: p->outmacro++;
1340: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1341: p->outmacro--;
1342: teximacro(p, "TE");
1343: p->list = sv;
1.1 kristaps 1344: }
1345:
1346: static void
1347: dotable(struct texi *p, enum texicmd cmd,
1348: const char *buf, size_t sz, size_t *pos)
1349: {
1.3 kristaps 1350: enum texilist sv = p->list;
1351:
1352: p->list = TEXILIST_ITEM;
1.5 kristaps 1353: teximacro(p, "Bl -tag -width Ds");
1.12 kristaps 1354: /* FIXME: ignore and parseeoln. */
1355: advanceeoln(p, buf, sz, pos, 1);
1.11 kristaps 1356: p->seenvs = 1;
1.20 kristaps 1357: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 1358: teximacro(p, "El");
1.3 kristaps 1359: p->list = sv;
1.1 kristaps 1360: }
1361:
1362: static void
1.2 kristaps 1363: doenumerate(struct texi *p, enum texicmd cmd,
1364: const char *buf, size_t sz, size_t *pos)
1365: {
1.3 kristaps 1366: enum texilist sv = p->list;
1.2 kristaps 1367:
1.3 kristaps 1368: p->list = TEXILIST_NOITEM;
1.5 kristaps 1369: teximacro(p, "Bl -enum");
1.11 kristaps 1370: p->seenvs = 1;
1.12 kristaps 1371: /* FIXME: ignore and parseeoln. */
1372: advanceeoln(p, buf, sz, pos, 1);
1.2 kristaps 1373: parseto(p, buf, sz, pos, "enumerate");
1.5 kristaps 1374: teximacro(p, "El");
1.3 kristaps 1375: p->list = sv;
1.2 kristaps 1376: }
1377:
1378: static void
1.1 kristaps 1379: doitemize(struct texi *p, enum texicmd cmd,
1380: const char *buf, size_t sz, size_t *pos)
1381: {
1.3 kristaps 1382: enum texilist sv = p->list;
1.1 kristaps 1383:
1.21 kristaps 1384: p->list = TEXILIST_NOITEM;
1.5 kristaps 1385: teximacro(p, "Bl -bullet");
1.11 kristaps 1386: p->seenvs = 1;
1.12 kristaps 1387: /* FIXME: ignore and parseeoln. */
1388: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1389: parseto(p, buf, sz, pos, "itemize");
1.5 kristaps 1390: teximacro(p, "El");
1.3 kristaps 1391: p->list = sv;
1.1 kristaps 1392: }
1393:
1394: static void
1395: doignbracket(struct texi *p, enum texicmd cmd,
1396: const char *buf, size_t sz, size_t *pos)
1397: {
1398:
1.3 kristaps 1399: p->ign++;
1.1 kristaps 1400: parsebracket(p, buf, sz, pos);
1.3 kristaps 1401: p->ign--;
1.1 kristaps 1402: }
1403:
1404: static void
1405: doignline(struct texi *p, enum texicmd cmd,
1406: const char *buf, size_t sz, size_t *pos)
1407: {
1408:
1.12 kristaps 1409: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1410: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1411: }
1412:
1.8 kristaps 1413: /*
1414: * Parse colon-separated directories from "cp" (if not NULL) and returns
1415: * the array of pointers.
1416: * Prepends "base" to the array.
1417: * This does NOT sanitise the directories!
1418: */
1.5 kristaps 1419: static char **
1.27 kristaps 1420: parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz)
1.5 kristaps 1421: {
1422: char *tok, *str, *tofree;
1423: const char *cpp;
1424: size_t i;
1425: char **dirs;
1426:
1427: *sz = NULL != (cpp = cp) ? 2 : 1;
1428: if (*sz > 1)
1429: for ( ; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1430: cpp++;
1431:
1.27 kristaps 1432: if (NULL == (dirs = calloc(*sz, sizeof(char *))))
1433: texiabort(p, NULL);
1434: else if (NULL == (dirs[0] = strdup(base)))
1435: texiabort(p, NULL);
1.5 kristaps 1436:
1437: if (NULL == cp)
1438: return(dirs);
1439:
1.27 kristaps 1440: if (NULL == (tofree = tok = str = strdup(cp)))
1441: texiabort(p, NULL);
1.5 kristaps 1442:
1443: for (i = 1; NULL != (tok = strsep(&str, ":")); i++)
1.27 kristaps 1444: if (NULL == (dirs[i] = strdup(tok)))
1445: texiabort(p, NULL);
1.5 kristaps 1446:
1447: free(tofree);
1448: return(dirs);
1449: }
1450:
1.1 kristaps 1451: int
1452: main(int argc, char *argv[])
1453: {
1454: struct texi texi;
1.2 kristaps 1455: int c;
1456: char *path, *dir;
1.10 kristaps 1457: const char *progname, *Idir, *cp;
1.1 kristaps 1458:
1459: progname = strrchr(argv[0], '/');
1460: if (progname == NULL)
1461: progname = argv[0];
1462: else
1463: ++progname;
1464:
1.10 kristaps 1465: memset(&texi, 0, sizeof(struct texi));
1.5 kristaps 1466: Idir = NULL;
1.10 kristaps 1467:
1.5 kristaps 1468: while (-1 != (c = getopt(argc, argv, "I:")))
1.1 kristaps 1469: switch (c) {
1.5 kristaps 1470: case ('I'):
1471: Idir = optarg;
1472: break;
1.1 kristaps 1473: default:
1474: goto usage;
1475: }
1476:
1477: argv += optind;
1478: if (0 == (argc -= optind))
1479: goto usage;
1480:
1.27 kristaps 1481: if (NULL == (path = strdup(argv[0])))
1482: texiabort(&texi, NULL);
1483: else if (NULL == (dir = dirname(path)))
1484: texiabort(&texi, NULL);
1485:
1.2 kristaps 1486: free(path);
1487:
1.10 kristaps 1488: if (NULL != (cp = strrchr(argv[0], '/')))
1489: texi.title = strdup(cp + 1);
1490: else
1491: texi.title = strdup(argv[0]);
1492:
1.27 kristaps 1493: if (NULL == texi.title)
1494: texiabort(&texi, NULL);
1495: else if (NULL != (path = strchr(texi.title, '.')))
1.10 kristaps 1496: *path = '\0';
1497:
1.3 kristaps 1498: texi.ign = 1;
1.27 kristaps 1499: texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
1.16 kristaps 1500: parsefile(&texi, argv[0], 1);
1.5 kristaps 1501: /* We shouldn't get here. */
1.2 kristaps 1502: texiexit(&texi);
1503: return(EXIT_FAILURE);
1.1 kristaps 1504: usage:
1.8 kristaps 1505: fprintf(stderr, "usage: %s [-Idirs] file\n", progname);
1.1 kristaps 1506: return(EXIT_FAILURE);
1507: }
CVSweb