Annotation of texi2mdoc/main.c, Revision 1.45
1.45 ! kristaps 1: /* $Id: main.c,v 1.44 2015/02/24 21:06:52 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: */
1.37 kristaps 17: #if defined(__linux__) || defined(__MINT__)
18: # define _GNU_SOURCE /* memmem */
19: #endif
1.1 kristaps 20: #include <assert.h>
21: #include <ctype.h>
22: #include <getopt.h>
1.2 kristaps 23: #include <libgen.h>
24: #include <limits.h>
1.1 kristaps 25: #include <stdarg.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
1.10 kristaps 29: #include <time.h>
1.1 kristaps 30:
1.24 kristaps 31: #include "extern.h"
1.1 kristaps 32:
1.23 kristaps 33: #define SECTSZ 4
34: static const char *const sects[SECTSZ] = {
35: "Sh",
36: "Ss",
37: "Em",
38: "No",
39: };
40:
1.16 kristaps 41: static void doaccent(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 42: static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 43: static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
44: static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.27 kristaps 45: static void dodefindex(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.15 kristaps 46: static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 47: static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.44 kristaps 48: static void doend(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 49: static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 50: static void doexample(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 51: static void doignargn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 52: static void doignblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
53: static void doignbracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
54: static void doignline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12 kristaps 55: static void doinline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 56: static void doinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 57: static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *);
58: static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 59: static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.30 kristaps 60: static void domacro(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.33 kristaps 76: static void doverbatim(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.16 kristaps 77: static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 78:
1.24 kristaps 79: static const struct texitok __texitoks[TEXICMD__MAX] = {
1.20 kristaps 80: /* TEXICMD__BEGIN */
1.42 kristaps 81: { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */
82: { dosymbol, "AA", 2 }, /* TEXICMD_AA */
83: { dosymbol, "aa", 2 }, /* TEXICMD_AASMALL */
1.8 kristaps 84: { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */
1.16 kristaps 85: { doaccent, "'", 1 }, /* TEXICMD_ACUTE */
1.42 kristaps 86: { dosymbol, "AE", 2 }, /* TEXICMD_AE */
87: { dosymbol, "ae", 2 }, /* TEXICMD_AESMALL */
1.1 kristaps 88: { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */
1.12 kristaps 89: { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */
90: { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */
1.20 kristaps 91: { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */
1.23 kristaps 92: { dosubsubsection, "appendixsubsubsec", 17 }, /* TEXICMD_APPENDIXSUBSUBSEC */
1.25 kristaps 93: { doinline, "asis", 4 }, /* TEXICMD_ASIS */
1.3 kristaps 94: { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */
1.1 kristaps 95: { dosymbol, "@", 1 }, /* TEXICMD_AT */
1.3 kristaps 96: { doignline, "author", 6 }, /* TEXICMD_AUTHOR */
1.21 kristaps 97: { doinline, "b", 1 }, /* TEXICMD_BOLD */
1.3 kristaps 98: { dosymbol, "!", 1 }, /* TEXICMD_BANG */
1.7 kristaps 99: { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */
1.1 kristaps 100: { dobye, "bye", 3 }, /* TEXICMD_BYE */
1.34 kristaps 101: { doblock, "cartouche", 9 }, /* TEXICMD_CARTOUCHE */
102: { doaccent, ",", 1 }, /* TEXICMD_CEDILLA */
1.12 kristaps 103: { doignline, "center", 6 }, /* TEXICMD_CENTER */
104: { dosection, "chapter", 7 }, /* TEXICMD_CHAPTER */
1.1 kristaps 105: { doignline, "cindex", 6 }, /* TEXICMD_CINDEX */
1.16 kristaps 106: { doaccent, "^", 1 }, /* TEXICMD_CIRCUMFLEX */
1.24 kristaps 107: { doinline, "cite", 4 }, /* TEXICMD_CITE */
108: { dovalue, "clear", 5 }, /* TEXICMD_CLEAR */
1.21 kristaps 109: { doinline, "code", 4 }, /* TEXICMD_CODE */
1.3 kristaps 110: { dosymbol, ":", 1 }, /* TEXICMD_COLON */
1.18 kristaps 111: { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */
1.35 kristaps 112: { dosymbol, "comma", 5 }, /* TEXICMD_COMMA */
1.12 kristaps 113: { doinline, "command", 7 }, /* TEXICMD_COMMAND */
1.1 kristaps 114: { doignline, "c", 1 }, /* TEXICMD_COMMENT */
1.2 kristaps 115: { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */
1.1 kristaps 116: { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */
117: { doignblock, "copying", 7 }, /* TEXICMD_COPYING */
118: { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */
1.27 kristaps 119: { dodefindex, "defcodeindex", 12 }, /* TEXICMD_DEFCODEINDEX */
1.15 kristaps 120: { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */
121: { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */
1.27 kristaps 122: { dodefindex, "defindex", 8 }, /* TEXICMD_DEFINDEX */
1.15 kristaps 123: { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */
124: { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */
125: { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */
126: { dodefn, "deftpx", 6 }, /* TEXICMD_DEFTPX */
127: { dodefn, "deftypefn", 9 }, /* TEXICMD_DEFTYPEFN */
128: { dodefn, "deftypefnx", 10 }, /* TEXICMD_DEFTYPEFNX */
129: { dodefn, "deftypefun", 10 }, /* TEXICMD_DEFTYPEFUN */
130: { dodefn, "deftypefunx", 11 }, /* TEXICMD_DEFTYPEFUNX */
1.34 kristaps 131: { dodefn, "deftypemethod", 13 }, /* TEXICMD_DEFTYPEMETHOD */
132: { dodefn, "deftypemethodx", 14 }, /* TEXICMD_DEFTYPEMETHODX */
1.15 kristaps 133: { dodefn, "deftypevar", 10 }, /* TEXICMD_DEFTYPEVAR */
134: { dodefn, "deftypevarx", 11 }, /* TEXICMD_DEFTYPEVARX */
135: { dodefn, "deftypevr", 9 }, /* TEXICMD_DEFTYPEVR */
136: { dodefn, "deftypevrx", 10 }, /* TEXICMD_DEFTYPEVRX */
137: { dodefn, "defun", 5 }, /* TEXICMD_DEFUN */
138: { dodefn, "defunx", 6 }, /* TEXICMD_DEFUNX */
139: { dodefn, "defvar", 6 }, /* TEXICMD_DEFVAR */
140: { dodefn, "defvarx", 7 }, /* TEXICMD_DEFVARX */
141: { dodefn, "defvr", 5 }, /* TEXICMD_DEFVR */
142: { dodefn, "defvrx", 6 }, /* TEXICMD_DEFVRX */
1.1 kristaps 143: { doignblock, "detailmenu", 10 }, /* TEXICMD_DETAILMENU */
1.21 kristaps 144: { doinline, "dfn", 3 }, /* TEXICMD_DFN */
1.42 kristaps 145: { dosymbol, "DH", 2 }, /* TEXICMD_DH */
146: { dosymbol, "dh", 2 }, /* TEXICMD_DHSMALL */
1.1 kristaps 147: { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */
148: { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */
1.3 kristaps 149: { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */
1.28 kristaps 150: { doignbracket, "dmn", 3 }, /* TEXICMD_DMN */
1.40 kristaps 151: { doignblock, "documentdescription", 19 }, /* TEXICMD_DOCUMENTDESCRIPTION */
152: { doignline, "documentencoding", 16 }, /* TEXICMD_DOCUMENTENCODING */
1.44 kristaps 153: { doignline, "documentlanguage", 16 }, /* TEXICMD_DOCUMENTLANGUAGE */
1.42 kristaps 154: { doaccent, "dotaccent", 9 }, /* TEXICMD_DOTACCENT */
155: { doaccent, "dotless", 7 }, /* TEXICMD_DOTLESS */
1.2 kristaps 156: { dosymbol, "dots", 4 }, /* TEXICMD_DOTS */
1.8 kristaps 157: { dolink, "email", 5 }, /* TEXICMD_EMAIL */
1.21 kristaps 158: { doinline, "emph", 4 }, /* TEXICMD_EMPH */
1.44 kristaps 159: { doend, "end", 3 }, /* TEXICMD_END */
1.34 kristaps 160: { dosymbol, "enddots", 7 }, /* TEXICMD_ENDDOTS */
1.2 kristaps 161: { doenumerate, "enumerate", 9 }, /* TEXICMD_ENUMERATE */
1.12 kristaps 162: { doinline, "env", 3 }, /* TEXICMD_ENV */
1.28 kristaps 163: { dosymbol, "equiv", 5 }, /* TEXICMD_EQUIV */
1.15 kristaps 164: { dosymbol, "error", 5 }, /* TEXICMD_ERROR */
1.43 kristaps 165: { dosymbol, "euro", 4 }, /* TEXICMD_EURO */
1.1 kristaps 166: { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */
1.42 kristaps 167: { dosymbol, "exclamdown", 10 }, /* TEXICMD_EXCLAMDOWN */
1.25 kristaps 168: { doignline, "exdent", 6 }, /* TEXICMD_EXDENT */
1.17 kristaps 169: { dosymbol, "expansion", 9 }, /* TEXICMD_EXPANSION */
1.12 kristaps 170: { doinline, "file", 4 }, /* TEXICMD_FILE */
1.17 kristaps 171: { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */
1.20 kristaps 172: { doignline, "findex", 6 }, /* TEXICMD_FINDEX */
1.44 kristaps 173: { doblock, "flushleft", 9 }, /* TEXICMD_FLUSHLEFT */
174: { doblock, "flushright", 10 }, /* TEXICMD_FLUSHRIGHT */
175: { doignline, "firstparagraphindent", 20 }, /* TEXICMD_FIRSTPARAGRAPHINDENT */
1.32 kristaps 176: { doignbracket, "footnote", 8 }, /* TEXICMD_FOOTNOTE */
1.44 kristaps 177: { doignline, "footnotestyle", 13 }, /* TEXICMD_FOOTNOTESTYLE */
1.20 kristaps 178: { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */
179: { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */
1.43 kristaps 180: { dosymbol, "geq", 3 }, /* TEXICMD_GEQ */
1.16 kristaps 181: { doaccent, "`", 1 }, /* TEXICMD_GRAVE */
1.3 kristaps 182: { doblock, "group", 5 }, /* TEXICMD_GROUP */
1.43 kristaps 183: { dosymbol, "guillemetleft", 13 }, /* TEXICMD_GUILLEMETLEFT */
184: { dosymbol, "guillemetright", 14 }, /* TEXICMD_GUILLEMETRIGHT */
185: { dosymbol, "guillemotleft", 13 }, /* TEXICMD_GUILLEMOTLEFT */
186: { dosymbol, "guillemotright", 14 }, /* TEXICMD_GUILLEMOTRIGHT */
187: { dosymbol, "guilsinglleft", 13 }, /* TEXICMD_GUILSINGLLEFT */
188: { dosymbol, "guilsinglright", 14 }, /* TEXICMD_GUILSINGLRIGHT */
1.42 kristaps 189: { doaccent, "H", 1 }, /* TEXICMD_H */
1.2 kristaps 190: { dosection, "heading", 7 }, /* TEXICMD_HEADING */
1.3 kristaps 191: { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */
1.18 kristaps 192: { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */
1.25 kristaps 193: { doignblock, "html", 4 }, /* TEXICMD_HTML */
1.3 kristaps 194: { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */
1.21 kristaps 195: { doinline, "i", 1 }, /* TEXICMD_I */
1.24 kristaps 196: { dovalue, "ifclear", 7 }, /* TEXICMD_IFCLEAR */
1.14 kristaps 197: { doignblock, "ifdocbook", 9 }, /* TEXICMD_IFDOCBOOK */
1.1 kristaps 198: { doignblock, "ifhtml", 6 }, /* TEXICMD_IFHTML */
1.26 kristaps 199: { doblock, "ifinfo", 6 }, /* TEXICMD_IFINFO */
1.14 kristaps 200: { doblock, "ifnotdocbook", 12 }, /* TEXICMD_IFNOTDOCBOOK */
201: { doblock, "ifnothtml", 9 }, /* TEXICMD_IFNOTHTML */
202: { doblock, "ifnotinfo", 9 }, /* TEXICMD_IFNOTINFO */
203: { doignblock, "ifnotplaintext", 14 }, /* TEXICMD_IFNOTPLAINTEXT */
1.3 kristaps 204: { doblock, "ifnottex", 8 }, /* TEXICMD_IFNOTTEX */
1.14 kristaps 205: { doblock, "ifnotxml", 8 }, /* TEXICMD_IFNOTXML */
206: { doblock, "ifplaintext", 11 }, /* TEXICMD_IFPLAINTEXT */
1.1 kristaps 207: { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */
1.30 kristaps 208: { dovalue, "ifset", 5 }, /* TEXICMD_IFSET */
1.14 kristaps 209: { doignblock, "ifxml", 5 }, /* TEXICMD_IFXML */
1.17 kristaps 210: { doignblock, "ignore", 6 }, /* TEXICMD_IGNORE */
1.1 kristaps 211: { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */
1.2 kristaps 212: { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */
1.13 kristaps 213: { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */
1.44 kristaps 214: { dolink, "indicateurl", 11 }, /* TEXICMD_INDICATEURL */
215: { dolink, "inforef", 7 }, /* TEXICMD_INFOREF */
1.5 kristaps 216: { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */
1.1 kristaps 217: { doitem, "item", 4 }, /* TEXICMD_ITEM */
218: { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */
1.20 kristaps 219: { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */
1.21 kristaps 220: { doinline, "kbd", 3 }, /* TEXICMD_KBD */
1.18 kristaps 221: { dobracket, "key", 3 }, /* TEXICMD_KEY */
1.20 kristaps 222: { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */
1.42 kristaps 223: { dosymbol, "L", 1 }, /* TEXICMD_L */
1.1 kristaps 224: { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */
1.43 kristaps 225: { dosymbol, "leq", 3 }, /* TEXICMD_LEQ */
1.23 kristaps 226: { dosecoffs, "lowersections", 13 }, /* TEXICMD_LOWERSECTIONS */
1.42 kristaps 227: { dosymbol, "l", 1 }, /* TEXICMD_LSMALL */
1.30 kristaps 228: { domacro, "macro", 5 }, /* TEXICMD_MACRO */
1.42 kristaps 229: { doaccent, "=", 1 }, /* TEXICMD_MACRON */
1.3 kristaps 230: { domath, "math", 4 }, /* TEXICMD_MATH */
1.1 kristaps 231: { doignblock, "menu", 4 }, /* TEXICMD_MENU */
1.25 kristaps 232: { dosymbol, "minus", 5 }, /* TEXICMD_MINUS */
1.18 kristaps 233: { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */
1.15 kristaps 234: { doignline, "need", 4 }, /* TEXICMD_NEED */
1.3 kristaps 235: { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */
1.1 kristaps 236: { doignline, "node", 4 }, /* TEXICMD_NODE */
1.3 kristaps 237: { doignline, "noindent", 8 }, /* TEXICMD_NOINDENT */
1.42 kristaps 238: { dosymbol, "O", 1 }, /* TEXICMD_O */
239: { dosymbol, "OE", 2 }, /* TEXICMD_OE */
240: { dosymbol, "oe", 2 }, /* TEXICMD_OESMALL */
241: { doaccent, "ogonek", 6 }, /* TEXICMD_OGONEK */
1.16 kristaps 242: { doinline, "option", 6 }, /* TEXICMD_OPTION */
1.42 kristaps 243: { dosymbol, "ordf", 4 }, /* TEXICMD_ORDF */
244: { dosymbol, "ordm", 4 }, /* TEXICMD_ORDM */
245: { dosymbol, "o", 1 }, /* TEXICMD_OSMALL */
1.3 kristaps 246: { doignline, "page", 4 }, /* TEXICMD_PAGE */
1.25 kristaps 247: { doignline, "paragraphindent", 15 }, /* TEXICMD_PARINDENT */
248: { dosymbol, ".", 1 }, /* TEXICMD_PERIOD */
1.28 kristaps 249: { doignline, "pindex", 6 }, /* TEXICMD_PINDEX */
1.43 kristaps 250: { dosymbol, "pounds", 6 }, /* TEXICMD_POUNDS */
1.2 kristaps 251: { doignline, "printindex", 10 }, /* TEXICMD_PRINTINDEX */
1.42 kristaps 252: { dolink, "pxref", 5 }, /* TEXICMD_PXREF */
253: { dosymbol, "questiondown", 12 }, /* TEXICMD_QUESTIONDOWN */
254: { dosymbol, "?", 1 }, /* TEXICMD_QUESTIONMARK */
255: { doquotation, "quotation", 9 }, /* TEXICMD_QUOTATION */
1.43 kristaps 256: { dosymbol, "quotedblbase", 12 }, /* TEXICMD_QUOTEDBLBASE */
257: { dosymbol, "quotedblleft", 12 }, /* TEXICMD_QUOTEDBLLEFT */
258: { dosymbol, "quotedblright", 13 }, /* TEXICMD_QUOTEDBLRIGHT */
259: { dosymbol, "quotesinglbase", 14 }, /* TEXICMD_QUOTESINGLBASE */
260: { dosymbol, "quoteleft", 9 }, /* TEXICMD_QUOTELEFT */
261: { dosymbol, "quoteright", 10 }, /* TEXICMD_QUOTERIGHT */
1.21 kristaps 262: { doinline, "r", 1 }, /* TEXICMD_R */
1.23 kristaps 263: { dosecoffs, "raisesections", 13 }, /* TEXICMD_RAISESECTIONS */
1.1 kristaps 264: { dobracket, "ref", 3 }, /* TEXICMD_REF */
1.28 kristaps 265: { doignline, "refill", 6 }, /* TEXICMD_REFILL */
1.43 kristaps 266: { dosymbol, "registeredsymbol", 16 }, /* TEXICMD_REGISTEREDSYMBOL */
1.15 kristaps 267: { dosymbol, "result", 6 }, /* TEXICMD_RESULT */
1.42 kristaps 268: { doaccent, "ringaccent", 10 }, /* TEXICMD_RINGACCENT */
1.21 kristaps 269: { doinline, "samp", 4 }, /* TEXICMD_SAMP */
270: { doinline, "sansserif", 9 }, /* TEXICMD_SANSSERIF */
1.7 kristaps 271: { dobracket, "sc", 2 }, /* TEXICMD_SC */
1.1 kristaps 272: { dosection, "section", 7 }, /* TEXICMD_SECTION */
1.24 kristaps 273: { dovalue, "set", 3 }, /* TEXICMD_SET */
1.1 kristaps 274: { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */
1.35 kristaps 275: { doignline, "setcontentsaftertitlepage", 25 }, /* TEXICMD_SETCONTENTSAFTER */
1.1 kristaps 276: { doignline, "setfilename", 11 }, /* TEXICMD_SETFILENAME */
1.10 kristaps 277: { dotitle, "settitle", 8 }, /* TEXICMD_SETTITLE */
1.25 kristaps 278: { doignline, "shortcontents", 13 }, /* TEXICMD_SHORTCONTENTS */
1.21 kristaps 279: { doinline, "slanted", 7 }, /* TEXICMD_SLANTED */
1.34 kristaps 280: { dosymbol, "/", 1 }, /* TEXICMD_SLASH */
1.3 kristaps 281: { dosp, "sp", 2 }, /* TEXICMD_SP */
282: { dosymbol, " ", 1 }, /* TEXICMD_SPACE */
1.17 kristaps 283: { doignline, "smallbook", 9 }, /* TEXICMD_SMALLBOOK */
1.12 kristaps 284: { dodisplay, "smalldisplay", 12 }, /* TEXICMD_SMALLDISPLAY */
1.3 kristaps 285: { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */
1.20 kristaps 286: { dodisplay, "smallformat", 11 }, /* TEXICMD_SMALLFORMAT */
1.13 kristaps 287: { dodisplay, "smallindentblock", 16 }, /* TEXICMD_SMALLINDENTBLOCK */
1.3 kristaps 288: { dosymbol, "{", 1 }, /* TEXICMD_SQUIGGLE_LEFT */
289: { dosymbol, "}", 1 }, /* TEXICMD_SQUIGGLE_RIGHT */
1.35 kristaps 290: { dosymbol, "ss", 2 }, /* TEXICMD_SS */
1.21 kristaps 291: { doinline, "strong", 6 }, /* TEXICMD_STRONG */
1.20 kristaps 292: { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */
1.1 kristaps 293: { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */
1.28 kristaps 294: { dosubsubsection, "subsubheading", 13 }, /* TEXICMD_SUBSUBHEADING */
1.23 kristaps 295: { dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */
1.3 kristaps 296: { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */
1.25 kristaps 297: { doignline, "summarycontents", 15 }, /* TEXICMD_SUMMARYCONTENTS */
1.27 kristaps 298: { doignline, "synindex", 8 }, /* TEXICMD_SYNINDEX */
1.20 kristaps 299: { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */
1.21 kristaps 300: { doinline, "t", 1 }, /* TEXICMD_T */
1.18 kristaps 301: { dotab, "tab", 3 }, /* TEXICMD_TAB */
302: { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */
1.1 kristaps 303: { dotable, "table", 5 }, /* TEXICMD_TABLE */
304: { doignblock, "tex", 3 }, /* TEXICMD_TEX */
305: { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */
1.43 kristaps 306: { dosymbol, "textdegree", 10 }, /* TEXICMD_TEXTDEGREE */
1.42 kristaps 307: { dosymbol, "TH", 2 }, /* TEXICMD_TH */
308: { dosymbol, "th", 2 }, /* TEXICMD_THSMALL */
1.35 kristaps 309: { dosymbol, "tie", 3 }, /* TEXICMD_TIE */
1.42 kristaps 310: { doaccent, "tieaccent", 9 }, /* TEXICMD_TIEACCENT */
1.16 kristaps 311: { doaccent, "~", 1 }, /* TEXICMD_TILDE */
1.25 kristaps 312: { doignline, "tindex", 6 }, /* TEXICMD_TINDEX */
1.3 kristaps 313: { doignline, "title", 5 }, /* TEXICMD_TITLE */
1.1 kristaps 314: { dobracket, "titlefont", 9 }, /* TEXICMD_TITLEFONT */
315: { doignblock, "titlepage", 9 }, /* TEXICMD_TITLEPAGE */
316: { dotop, "top", 3 }, /* TEXICMD_TOP */
1.42 kristaps 317: { doaccent, "u", 1 }, /* TEXICMD_U */
318: { doaccent, "ubaraccent", 10 }, /* TEXICMD_UBARACCENT */
319: { doaccent, "udotaccent", 10 }, /* TEXICMD_UDOTACCENT */
1.16 kristaps 320: { doaccent, "\"", 1 }, /* TEXICMD_UMLAUT */
1.12 kristaps 321: { dosection, "unnumbered", 10 }, /* TEXICMD_UNNUMBERED */
1.2 kristaps 322: { dosection, "unnumberedsec", 13 }, /* TEXICMD_UNNUMBEREDSEC */
1.20 kristaps 323: { dosubsection, "unnumberedsubsec", 16 }, /* TEXICMD_UNNUMBEREDSUBSEC */
1.23 kristaps 324: { dosubsubsection, "unnumberedsubsubsec", 19 }, /* TEXICMD_UNNUMBEREDSUBSUBSEC */
1.8 kristaps 325: { dolink, "uref", 4 }, /* TEXICMD_UREF */
326: { dolink, "url", 3 }, /* TEXICMD_URL */
1.34 kristaps 327: { doignline, "", 0 }, /* TEXICMD_USER_INDEX */
1.42 kristaps 328: { doaccent, "v", 1 }, /* TEXICMD_V */
1.24 kristaps 329: { dovalue, "value", 5 }, /* TEXICMD_VALUE */
1.12 kristaps 330: { doinline, "var", 3 }, /* TEXICMD_VAR */
1.33 kristaps 331: { doverb, "verb", 4 }, /* TEXICMD_VERB */
332: { doverbatim, "verbatim", 8 }, /* TEXICMD_VERBATIM */
1.16 kristaps 333: { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */
1.18 kristaps 334: { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */
1.9 kristaps 335: { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */
1.20 kristaps 336: { dotable, "vtable", 6 }, /* TEXICMD_VTABLE */
1.3 kristaps 337: { dobracket, "w", 1 }, /* TEXICMD_W */
1.8 kristaps 338: { dolink, "xref", 4 }, /* TEXICMD_XREF */
1.20 kristaps 339: /* TEXICMD__END */
1.1 kristaps 340: };
341:
1.24 kristaps 342: const struct texitok *const texitoks = __texitoks;
1.18 kristaps 343:
1.2 kristaps 344: static void
1.27 kristaps 345: dodefindex(struct texi *p, enum texicmd cmd,
346: const char *buf, size_t sz, size_t *pos)
347: {
348: size_t start, end;
349: char *cp;
350:
351: while (*pos < sz && isws(buf[*pos]))
352: advance(p, buf, pos);
353:
354: start = end = *pos;
355: while (end < sz && ! ismspace(buf[end]))
356: end++;
357:
358: if (start == end) {
359: advanceeoln(p, buf, sz, pos, 1);
360: return;
361: } else if (NULL == (cp = malloc(end - start + 1)))
362: texiabort(p, NULL);
363:
364: memcpy(cp, &buf[start], end - start);
365: cp[end - start] = '\0';
366:
367: p->indexs = realloc(p->indexs,
368: sizeof(char *) * (p->indexsz + 1));
369:
370: if (NULL == p->indexs)
371: texiabort(p, NULL);
372: p->indexs[p->indexsz++] = cp;
373: }
374:
375: static void
1.15 kristaps 376: dodefn(struct texi *p, enum texicmd cmd,
1.3 kristaps 377: const char *buf, size_t sz, size_t *pos)
378: {
379: const char *blk;
380:
1.5 kristaps 381: blk = NULL;
1.3 kristaps 382: switch (cmd) {
1.15 kristaps 383: case (TEXICMD_DEFFN):
1.35 kristaps 384: case (TEXICMD_DEFMAC):
1.15 kristaps 385: case (TEXICMD_DEFTP):
1.3 kristaps 386: case (TEXICMD_DEFTYPEFN):
387: case (TEXICMD_DEFTYPEFUN):
1.34 kristaps 388: case (TEXICMD_DEFTYPEMETHOD):
1.15 kristaps 389: case (TEXICMD_DEFTYPEVAR):
390: case (TEXICMD_DEFTYPEVR):
391: case (TEXICMD_DEFUN):
392: case (TEXICMD_DEFVAR):
393: case (TEXICMD_DEFVR):
1.5 kristaps 394: blk = texitoks[cmd].tok;
1.3 kristaps 395: break;
1.5 kristaps 396: default:
1.3 kristaps 397: break;
398: }
399:
400: if (p->ign) {
1.15 kristaps 401: NULL != blk ?
402: parseto(p, buf, sz, pos, blk) :
403: parseeoln(p, buf, sz, pos);
1.3 kristaps 404: return;
405: }
406:
1.15 kristaps 407: if (NULL != blk)
408: texivspace(p);
409:
1.3 kristaps 410: switch (cmd) {
1.34 kristaps 411: case (TEXICMD_DEFTYPEMETHOD):
412: case (TEXICMD_DEFTYPEMETHODX):
413: texiputchars(p, "Method");
414: break;
1.15 kristaps 415: case (TEXICMD_DEFMAC):
416: case (TEXICMD_DEFMACX):
417: texiputchars(p, "Macro");
418: break;
419: case (TEXICMD_DEFTYPEVAR):
420: case (TEXICMD_DEFTYPEVARX):
421: case (TEXICMD_DEFVAR):
422: case (TEXICMD_DEFVARX):
423: texiputchars(p, "Variable");
424: break;
1.3 kristaps 425: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 426: case (TEXICMD_DEFTYPEFUNX):
427: case (TEXICMD_DEFUN):
428: case (TEXICMD_DEFUNX):
429: texiputchars(p, "Function");
1.3 kristaps 430: break;
431: default:
1.15 kristaps 432: parselinearg(p, buf, sz, pos);
1.3 kristaps 433: break;
434: }
1.15 kristaps 435:
436: texiputchars(p, ":\n");
437:
438: switch (cmd) {
439: case (TEXICMD_DEFMAC):
440: case (TEXICMD_DEFMACX):
441: teximacroopen(p, "Dv");
442: while (parselinearg(p, buf, sz, pos))
443: /* Spin. */ ;
444: teximacroclose(p);
445: break;
446: case (TEXICMD_DEFFN):
447: case (TEXICMD_DEFFNX):
448: case (TEXICMD_DEFUN):
449: case (TEXICMD_DEFUNX):
450: teximacroopen(p, "Fo");
451: parselinearg(p, buf, sz, pos);
452: teximacroclose(p);
453: teximacroopen(p, "Fa");
454: while (parselinearg(p, buf, sz, pos))
455: /* Spin. */ ;
456: teximacroclose(p);
457: teximacro(p, "Fc");
458: break;
459: case (TEXICMD_DEFTYPEFUN):
460: case (TEXICMD_DEFTYPEFUNX):
461: case (TEXICMD_DEFTYPEFN):
462: case (TEXICMD_DEFTYPEFNX):
1.34 kristaps 463: case (TEXICMD_DEFTYPEMETHOD):
464: case (TEXICMD_DEFTYPEMETHODX):
1.15 kristaps 465: teximacroopen(p, "Ft");
466: parselinearg(p, buf, sz, pos);
467: teximacroclose(p);
468: teximacroopen(p, "Fo");
469: parselinearg(p, buf, sz, pos);
470: teximacroclose(p);
471: teximacroopen(p, "Fa");
472: while (parselinearg(p, buf, sz, pos))
473: /* Spin. */ ;
474: teximacroclose(p);
475: teximacro(p, "Fc");
476: break;
477: case (TEXICMD_DEFTP):
478: case (TEXICMD_DEFTPX):
479: case (TEXICMD_DEFTYPEVAR):
480: case (TEXICMD_DEFTYPEVARX):
481: case (TEXICMD_DEFTYPEVR):
482: case (TEXICMD_DEFTYPEVRX):
483: teximacroopen(p, "Vt");
484: while (parselinearg(p, buf, sz, pos))
485: /* Spin. */ ;
486: teximacroclose(p);
487: break;
488: case (TEXICMD_DEFVAR):
489: case (TEXICMD_DEFVARX):
490: case (TEXICMD_DEFVR):
491: case (TEXICMD_DEFVRX):
492: teximacroopen(p, "Va");
493: while (parselinearg(p, buf, sz, pos))
494: /* Spin. */ ;
495: teximacroclose(p);
496: break;
497: default:
498: abort();
1.3 kristaps 499: }
1.15 kristaps 500:
1.11 kristaps 501: texivspace(p);
1.3 kristaps 502: if (NULL != blk)
503: parseto(p, buf, sz, pos, blk);
504: }
505:
506: static void
1.30 kristaps 507: domacro(struct texi *p, enum texicmd cmd,
508: const char *buf, size_t sz, size_t *pos)
509: {
510: size_t start, end, endtoksz, len;
511: struct teximacro m;
512: const char *endtok, *blk;
513:
514: memset(&m, 0, sizeof(struct teximacro));
515:
516: while (*pos < sz && isws(buf[*pos]))
517: advance(p, buf, pos);
518:
519: for (start = end = *pos; end < sz; end++)
520: if (ismspace(buf[end]) || '{' == buf[end])
521: break;
522:
523: if (start == end)
524: texierr(p, "zero-length macro name");
525:
526: advanceto(p, buf, pos, end);
527:
528: m.key = malloc(end - start + 1);
529: if (NULL == m.key)
530: texiabort(p, NULL);
531: memcpy(m.key, &buf[start], end - start);
532: m.key[end - start] = '\0';
533:
1.31 kristaps 534: m.args = argparse(p, buf, sz, pos, &m.argsz, 0);
1.30 kristaps 535: advanceeoln(p, buf, sz, pos, 0);
536:
537: start = *pos;
538: endtok = "\n@end macro\n";
539: endtoksz = strlen(endtok);
1.45 ! kristaps 540: blk = memmem(&buf[start], sz - start, endtok, endtoksz);
1.30 kristaps 541: if (NULL == blk)
542: texierr(p, "unterminated macro body");
543: while (&buf[*pos] != blk)
544: advance(p, buf, pos);
545: assert('\n' == buf[*pos]);
546: advance(p, buf, pos);
547: len = blk - &buf[start];
548: m.value = malloc(len + 1);
549: if (NULL == m.value)
550: texiabort(p, NULL);
551: memcpy(m.value, &buf[start], len);
552: m.value[len] = '\0';
553:
554: p->macros = realloc
555: (p->macros,
556: (p->macrosz + 1) *
557: sizeof(struct teximacro));
558: if (NULL == p->macros)
559: texiabort(p, NULL);
560:
561: p->macros[p->macrosz++] = m;
562: advanceeoln(p, buf, sz, pos, 1);
563: }
564:
565: static void
1.1 kristaps 566: doignblock(struct texi *p, enum texicmd cmd,
567: const char *buf, size_t sz, size_t *pos)
568: {
1.45 ! kristaps 569: char end[32], start[32];
! 570: const char *endt, *startt;
! 571: size_t esz, ssz, newpos, stack;
1.26 kristaps 572:
573: /*
1.45 ! kristaps 574: * FIXME: this is cheating.
! 575: * These tokens are supposed to begin on a newline.
! 576: * However, if we do that, then we would need to check within
! 577: * the loop for trailer (or leading, as the case may be)
! 578: * newline, and that's just a bit too complicated right now.
! 579: * This is becasue
! 580: * @ifset BAR
! 581: * @ifset FOO
! 582: * @end ifset
! 583: * @end ifset
! 584: * won't work right now: we'd read after the first "@end ifset"
! 585: * to the next line, then look for the next line after that.
1.26 kristaps 586: */
1.45 ! kristaps 587: ssz = snprintf(start, sizeof(start),
! 588: "@%s", texitoks[cmd].tok);
! 589: assert(ssz < sizeof(start));
! 590: esz = snprintf(end, sizeof(end),
! 591: "@end %s\n", texitoks[cmd].tok);
! 592: assert(esz < sizeof(end));
! 593: stack = 1;
! 594:
! 595: /*
! 596: * Here we look for the end token "end" somewhere in the file in
! 597: * front of us.
! 598: * It's not that easy, of course: if we have a nested block,
! 599: * then there'll be an "end" token of the same kind between us.
! 600: * Thus, we keep track of scopes for matching "end" blocks.
1.26 kristaps 601: */
1.45 ! kristaps 602: while (stack > 0 && *pos < sz) {
! 603: if (stack > 10)
! 604: abort();
! 605: endt = memmem(&buf[*pos], sz - *pos, end, esz);
! 606: startt = memmem(&buf[*pos], sz - *pos, start, ssz);
! 607: if (NULL == endt) {
! 608: texiwarn(p, "unterminated \"%s\" "
! 609: "block", texitoks[cmd].tok);
! 610: *pos = sz;
! 611: break;
! 612: }
1.26 kristaps 613:
1.45 ! kristaps 614: newpos = *pos;
! 615: if (NULL == startt || startt > endt) {
! 616: newpos += esz + (size_t)(endt - &buf[*pos]);
! 617: stack--;
! 618: } else {
! 619: newpos += ssz + (size_t)(startt - &buf[*pos]);
! 620: stack++;
! 621: }
! 622:
! 623: assert(newpos <= sz);
! 624: while (*pos < newpos)
! 625: advance(p, buf, pos);
! 626: }
1.1 kristaps 627: }
628:
629: static void
1.3 kristaps 630: doblock(struct texi *p, enum texicmd cmd,
1.1 kristaps 631: const char *buf, size_t sz, size_t *pos)
632: {
633:
1.5 kristaps 634: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.1 kristaps 635: }
636:
637: static void
1.12 kristaps 638: doinline(struct texi *p, enum texicmd cmd,
639: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 640: {
1.21 kristaps 641: const char *macro = NULL;
1.12 kristaps 642:
643: switch (cmd) {
1.21 kristaps 644: case (TEXICMD_CODE):
645: case (TEXICMD_KBD):
646: case (TEXICMD_SAMP):
647: case (TEXICMD_T):
648: macro = "Li";
649: break;
650: case (TEXICMD_CITE):
651: case (TEXICMD_DFN):
652: case (TEXICMD_EMPH):
653: case (TEXICMD_I):
654: case (TEXICMD_SLANTED):
655: macro = "Em";
656: break;
657: case (TEXICMD_B):
658: case (TEXICMD_STRONG):
659: macro = "Sy";
660: break;
1.12 kristaps 661: case (TEXICMD_COMMAND):
662: macro = "Xr";
663: break;
664: case (TEXICMD_ENV):
665: macro = "Ev";
666: break;
667: case (TEXICMD_FILE):
668: macro = "Pa";
669: break;
1.16 kristaps 670: case (TEXICMD_OPTION):
671: macro = "Op";
672: break;
1.12 kristaps 673: case (TEXICMD_VAR):
674: macro = "Va";
675: break;
676: default:
1.22 kristaps 677: break;
1.12 kristaps 678: }
679:
1.25 kristaps 680: if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) {
1.12 kristaps 681: parsebracket(p, buf, sz, pos);
682: return;
683: }
1.1 kristaps 684:
1.5 kristaps 685: teximacroopen(p, macro);
1.1 kristaps 686: p->seenws = 0;
687: parsebracket(p, buf, sz, pos);
1.11 kristaps 688: texipunctuate(p, buf, sz, pos);
1.5 kristaps 689: teximacroclose(p);
1.1 kristaps 690: }
691:
692: static void
1.25 kristaps 693: doverb(struct texi *p, enum texicmd cmd,
1.33 kristaps 694: const char *buf, size_t sz, size_t *pos)
695: {
696: char delim;
1.34 kristaps 697: size_t start;
1.33 kristaps 698:
699: while (*pos < sz && isws(buf[*pos]))
700: advance(p, buf, pos);
701: if (*pos == sz || '{' != buf[*pos])
702: return;
703: advance(p, buf, pos);
704: if (*pos == sz)
705: return;
706:
707: delim = buf[*pos];
708: advance(p, buf, pos);
709: /* Make sure we flush out our initial whitespace... */
710: if (p->seenws && p->outcol && 0 == p->literal)
711: texiputchar(p, ' ');
712: p->seenws = 0;
1.34 kristaps 713: start = *pos;
1.33 kristaps 714: /* Read until we see the delimiter then end-brace. */
715: while (*pos < sz - 1) {
716: if (buf[*pos] == delim && buf[*pos + 1] == '}')
717: break;
718: advance(p, buf, pos);
719: }
720: if (*pos == sz - 1)
721: return;
1.34 kristaps 722: texiputbuf(p, buf, start, *pos);
723:
1.33 kristaps 724: /* Make sure we read after the end-brace. */
725: assert(delim == buf[*pos]);
726: advance(p, buf, pos);
727: assert('}' == buf[*pos]);
728: advance(p, buf, pos);
729: }
730:
731: static void
732: doverbatim(struct texi *p, enum texicmd cmd,
1.25 kristaps 733: const char *buf, size_t sz, size_t *pos)
734: {
735: const char *end, *term;
736: size_t endsz, endpos;
737:
1.45 ! kristaps 738: /* We read from the @verbatim\n newline inclusive! */
1.25 kristaps 739: end = "\n@end verbatim\n";
740: endsz = strlen(end);
1.45 ! kristaps 741: advanceeoln(p, buf, sz, pos, 0);
! 742: if (*pos == sz) {
! 743: texiwarn(p, "unexpected end of file");
! 744: return;
! 745: }
1.25 kristaps 746:
1.45 ! kristaps 747: term = memmem(&buf[*pos], sz - *pos, end, endsz);
! 748: if (NULL == term) {
! 749: texiwarn(p, "unterminated verbatim block");
! 750: endpos = sz;
! 751: } else
! 752: endpos = *pos + (size_t)(term - &buf[*pos]);
1.25 kristaps 753:
1.45 ! kristaps 754: assert(endpos <= sz);
! 755: assert('\n' == buf[*pos]);
! 756: advance(p, buf, pos);
1.25 kristaps 757: teximacro(p, "Bd -literal -offset indent");
1.26 kristaps 758: while (*pos < endpos) {
1.41 kristaps 759: texiputchar(p, buf[*pos]);
1.25 kristaps 760: advance(p, buf, pos);
761: }
762: teximacro(p, "Ed");
1.45 ! kristaps 763: if (*pos < sz)
! 764: advanceto(p, buf, pos, endpos + endsz);
1.25 kristaps 765: }
766:
767: static void
1.16 kristaps 768: doverbinclude(struct texi *p, enum texicmd cmd,
769: const char *buf, size_t sz, size_t *pos)
770: {
1.25 kristaps 771: char fname[PATH_MAX], path[PATH_MAX];
772: int rc;
773: size_t i, end;
774: const char *v;
775: enum texicmd type;
1.16 kristaps 776:
777: while (*pos < sz && ' ' == buf[*pos])
778: advance(p, buf, pos);
779:
1.25 kristaps 780: for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.16 kristaps 781: if (i == sizeof(fname) - 1)
782: break;
1.25 kristaps 783: if ('@' != buf[*pos]) {
784: fname[i++] = buf[*pos];
785: advance(p, buf, pos);
786: continue;
787: }
1.30 kristaps 788: type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25 kristaps 789: advanceto(p, buf, pos, end);
790: if (TEXICMD_VALUE != type)
791: texierr(p, "unknown verbatiminclude command");
792: v = valueblookup(p, buf, sz, pos);
793: if (NULL == v)
794: continue;
795: while ('\0' != *v) {
796: if (i == sizeof(fname) - 1)
797: break;
798: fname[i++] = *v++;
799: }
800: if ('\0' != *v)
801: break;
1.16 kristaps 802: }
803:
804: if (i == 0)
805: texierr(p, "path too short");
806: else if ('\n' != buf[*pos])
807: texierr(p, "path too long");
808: else if ('/' == fname[0])
809: texierr(p, "no absolute paths");
810: fname[i] = '\0';
811:
812: if (strstr(fname, "../") || strstr(fname, "/.."))
813: texierr(p, "insecure path");
814:
815: rc = snprintf(path, sizeof(path),
816: "%s/%s", p->dirs[0], fname);
817: if (rc < 0)
818: texierr(p, "couldn't format path");
819: else if ((size_t)rc >= sizeof(path))
820: texierr(p, "path too long");
821:
822: parsefile(p, path, 0);
823: }
824:
825: static void
1.2 kristaps 826: doinclude(struct texi *p, enum texicmd cmd,
827: const char *buf, size_t sz, size_t *pos)
828: {
1.25 kristaps 829: char fname[PATH_MAX], path[PATH_MAX];
830: size_t i, end;
831: int rc;
832: const char *v;
833: enum texicmd type;
1.2 kristaps 834:
835: while (*pos < sz && ' ' == buf[*pos])
836: advance(p, buf, pos);
837:
838: /* Read in the filename. */
1.25 kristaps 839: for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.2 kristaps 840: if (i == sizeof(fname) - 1)
841: break;
1.25 kristaps 842: if ('@' != buf[*pos]) {
843: fname[i++] = buf[*pos];
844: advance(p, buf, pos);
845: continue;
846: }
1.30 kristaps 847: type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25 kristaps 848: advanceto(p, buf, pos, end);
849: if (TEXICMD_VALUE != type)
850: texierr(p, "unknown include command");
851: v = valueblookup(p, buf, sz, pos);
852: if (NULL == v)
853: continue;
854: while ('\0' != *v) {
855: if (i == sizeof(fname) - 1)
856: break;
857: fname[i++] = *v++;
858: }
859: if ('\0' != *v)
860: break;
1.2 kristaps 861: }
862:
863: if (i == 0)
864: texierr(p, "path too short");
865: else if ('\n' != buf[*pos])
866: texierr(p, "path too long");
867: else if ('/' == fname[0])
868: texierr(p, "no absolute paths");
869: fname[i] = '\0';
870:
871: if (strstr(fname, "../") || strstr(fname, "/.."))
872: texierr(p, "insecure path");
873:
1.5 kristaps 874: for (i = 0; i < p->dirsz; i++) {
875: rc = snprintf(path, sizeof(path),
876: "%s/%s", p->dirs[i], fname);
877: if (rc < 0)
878: texierr(p, "couldn't format path");
879: else if ((size_t)rc >= sizeof(path))
880: texierr(p, "path too long");
881: else if (-1 == access(path, R_OK))
882: continue;
883:
1.16 kristaps 884: parsefile(p, path, 1);
1.5 kristaps 885: return;
886: }
1.2 kristaps 887:
1.5 kristaps 888: texierr(p, "couldn't find %s in includes", fname);
1.2 kristaps 889: }
890:
891: static void
1.1 kristaps 892: dobracket(struct texi *p, enum texicmd cmd,
893: const char *buf, size_t sz, size_t *pos)
894: {
895:
896: parsebracket(p, buf, sz, pos);
897: }
898:
899: static void
1.3 kristaps 900: dodisplay(struct texi *p, enum texicmd cmd,
901: const char *buf, size_t sz, size_t *pos)
902: {
903:
1.20 kristaps 904: switch (cmd) {
905: case (TEXICMD_FORMAT):
906: case (TEXICMD_SMALLFORMAT):
907: teximacro(p, "Bd -filled");
908: break;
909: default:
910: teximacro(p, "Bd -filled -offset indent");
911: break;
912: }
913:
1.11 kristaps 914: p->seenvs = 1;
1.12 kristaps 915: /* FIXME: ignore and parseeoln. */
1.3 kristaps 916: advanceeoln(p, buf, sz, pos, 1);
1.13 kristaps 917: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 918: teximacro(p, "Ed");
1.3 kristaps 919: }
920:
921: static void
1.1 kristaps 922: doexample(struct texi *p, enum texicmd cmd,
923: const char *buf, size_t sz, size_t *pos)
924: {
925:
1.5 kristaps 926: teximacro(p, "Bd -literal -offset indent");
1.12 kristaps 927: /* FIXME: ignore and parseeoln. */
1.3 kristaps 928: advanceeoln(p, buf, sz, pos, 1);
929: p->literal++;
1.13 kristaps 930: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 931: p->literal--;
1.5 kristaps 932: teximacro(p, "Ed");
1.1 kristaps 933: }
934:
935: static void
936: dobye(struct texi *p, enum texicmd cmd,
937: const char *buf, size_t sz, size_t *pos)
938: {
939:
940: texiexit(p);
941: exit(EXIT_SUCCESS);
942: }
943:
944: static void
1.10 kristaps 945: dotitle(struct texi *p, enum texicmd cmd,
946: const char *buf, size_t sz, size_t *pos)
947: {
948: size_t start, end;
949:
950: while (*pos < sz && isws(buf[*pos]))
951: advance(p, buf, pos);
952: start = end = *pos;
953: while (end < sz && '\n' != buf[end])
954: end++;
1.26 kristaps 955: advanceeoln(p, buf, sz, pos, 1);
1.10 kristaps 956: free(p->subtitle);
957: p->subtitle = malloc(end - start + 1);
1.27 kristaps 958: if (NULL == p->subtitle)
959: texiabort(p, NULL);
1.10 kristaps 960: memcpy(p->subtitle, &buf[start], end - start);
961: p->subtitle[end - start] = '\0';
962: }
963:
964: static void
1.16 kristaps 965: doaccent(struct texi *p, enum texicmd cmd,
966: const char *buf, size_t sz, size_t *pos)
967: {
1.42 kristaps 968: int brace = 0;
1.16 kristaps 969:
1.42 kristaps 970: if (*pos == sz) {
971: texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
1.16 kristaps 972: return;
1.42 kristaps 973: }
974:
975: /* Pad us with space, if necessary. */
976: if (p->seenws && p->outcol && 0 == p->literal) {
1.34 kristaps 977: texiputchar(p, ' ');
1.42 kristaps 978: p->seenws = 0;
979: }
980:
981: /*
982: * If we're braced, then that's that.
983: * Otherwise, in a special Texinfo case: if we're a non
984: * alphabetic command of one letter, then the next character is
985: * the critical one.
986: * Otherwise, space can sit between us and our argument.
987: */
988: if ('{' == buf[*pos]) {
989: brace = 1;
990: advance(p, buf, pos);
991: } else if (isalpha(texitoks[cmd].tok[0]))
1.34 kristaps 992: while (*pos < sz && isws(buf[*pos]))
993: advance(p, buf, pos);
1.42 kristaps 994:
995: if (*pos == sz) {
996: texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
997: return;
998: }
999:
1000: switch (cmd) {
1.16 kristaps 1001: case (TEXICMD_ACUTE):
1002: switch (buf[*pos]) {
1003: case ('a'): case ('A'):
1004: case ('e'): case ('E'):
1005: case ('i'): case ('I'):
1006: case ('o'): case ('O'):
1007: case ('u'): case ('U'):
1008: texiputchars(p, "\\(\'");
1009: break;
1010: default:
1.42 kristaps 1011: texiwarn(p, "ignoring accent");
1012: break;
1.16 kristaps 1013: }
1.42 kristaps 1014: texiputchar(p, buf[*pos]);
1015: advance(p, buf, pos);
1016: break;
1017: case (TEXICMD_CEDILLA):
1018: if ('c' == buf[*pos] || 'C' == buf[*pos])
1019: texiputchars(p, "\\(,");
1020: else
1021: texiwarn(p, "ignoring accent");
1022: texiputchar(p, buf[*pos]);
1023: advance(p, buf, pos);
1.16 kristaps 1024: break;
1025: case (TEXICMD_CIRCUMFLEX):
1026: switch (buf[*pos]) {
1027: case ('a'): case ('A'):
1028: case ('e'): case ('E'):
1029: case ('i'): case ('I'):
1030: case ('o'): case ('O'):
1031: case ('u'): case ('U'):
1032: texiputchars(p, "\\(^");
1033: break;
1034: default:
1.42 kristaps 1035: texiwarn(p, "ignoring accent");
1.34 kristaps 1036: break;
1.16 kristaps 1037: }
1.42 kristaps 1038: texiputchar(p, buf[*pos]);
1039: advance(p, buf, pos);
1040: break;
1041: case (TEXICMD_DOTLESS):
1042: if ('i' == buf[*pos] || 'j' == buf[*pos])
1043: texiputchars(p, "\\(.");
1044: else
1045: texiwarn(p, "ignoring accent");
1046: texiputchar(p, buf[*pos]);
1047: advance(p, buf, pos);
1.16 kristaps 1048: break;
1049: case (TEXICMD_GRAVE):
1050: switch (buf[*pos]) {
1051: case ('a'): case ('A'):
1052: case ('e'): case ('E'):
1053: case ('i'): case ('I'):
1054: case ('o'): case ('O'):
1055: case ('u'): case ('U'):
1056: texiputchars(p, "\\(`");
1057: break;
1058: default:
1.42 kristaps 1059: texiwarn(p, "ignoring accent");
1060: break;
1.16 kristaps 1061: }
1.42 kristaps 1062: texiputchar(p, buf[*pos]);
1063: advance(p, buf, pos);
1.16 kristaps 1064: break;
1065: case (TEXICMD_TILDE):
1066: switch (buf[*pos]) {
1067: case ('a'): case ('A'):
1068: case ('n'): case ('N'):
1069: case ('o'): case ('O'):
1070: texiputchars(p, "\\(~");
1071: break;
1072: default:
1.42 kristaps 1073: texiwarn(p, "ignoring accent");
1.34 kristaps 1074: break;
1.16 kristaps 1075: }
1.42 kristaps 1076: texiputchar(p, buf[*pos]);
1077: advance(p, buf, pos);
1.16 kristaps 1078: break;
1079: case (TEXICMD_UMLAUT):
1080: switch (buf[*pos]) {
1081: case ('a'): case ('A'):
1082: case ('e'): case ('E'):
1083: case ('i'): case ('I'):
1084: case ('o'): case ('O'):
1085: case ('u'): case ('U'):
1086: case ('y'):
1087: texiputchars(p, "\\(:");
1088: break;
1089: default:
1.42 kristaps 1090: texiwarn(p, "ignoring accent");
1.34 kristaps 1091: break;
1.16 kristaps 1092: }
1.42 kristaps 1093: texiputchar(p, buf[*pos]);
1094: advance(p, buf, pos);
1.16 kristaps 1095: break;
1096: default:
1.42 kristaps 1097: texiputchar(p, buf[*pos]);
1098: advance(p, buf, pos);
1099: break;
1100: }
1101:
1102: if (brace) {
1103: while (*pos < sz && '}' != buf[*pos]) {
1104: texiputchar(p, buf[*pos]);
1105: advance(p, buf, pos);
1106: }
1107: if (*pos < sz)
1108: advance(p, buf, pos);
1109: }
1110:
1111: switch (cmd) {
1112: case (TEXICMD_TIEACCENT):
1113: texiputchar(p, ']');
1114: break;
1115: case (TEXICMD_DOTACCENT):
1116: texiputchar(p, '*');
1117: break;
1118: default:
1119: break;
1.16 kristaps 1120: }
1121: }
1122:
1123: static void
1.1 kristaps 1124: dosymbol(struct texi *p, enum texicmd cmd,
1125: const char *buf, size_t sz, size_t *pos)
1126: {
1127:
1.42 kristaps 1128: /* Remember to pad us. */
1.43 kristaps 1129: if (p->seenws && p->outcol && 0 == p->literal)
1.3 kristaps 1130: texiputchar(p, ' ');
1.43 kristaps 1131:
1132: p->seenws = 0;
1.3 kristaps 1133:
1.1 kristaps 1134: switch (cmd) {
1.42 kristaps 1135: case (TEXICMD_AA):
1136: texiputchars(p, "\\(oA");
1137: break;
1138: case (TEXICMD_AASMALL):
1139: texiputchars(p, "\\(oa");
1140: break;
1141: case (TEXICMD_AE):
1142: texiputchars(p, "\\(AE");
1143: break;
1144: case (TEXICMD_AESMALL):
1145: texiputchars(p, "\\(ae");
1146: break;
1.3 kristaps 1147: case (TEXICMD_ASTERISK):
1148: case (TEXICMD_NEWLINE):
1149: case (TEXICMD_SPACE):
1.18 kristaps 1150: case (TEXICMD_TABSYM):
1.3 kristaps 1151: texiputchar(p, ' ');
1152: break;
1.1 kristaps 1153: case (TEXICMD_AT):
1.3 kristaps 1154: texiputchar(p, '@');
1155: break;
1156: case (TEXICMD_BANG):
1157: texiputchar(p, '!');
1.7 kristaps 1158: break;
1159: case (TEXICMD_BULLET):
1160: texiputchars(p, "\\(bu");
1.1 kristaps 1161: break;
1.35 kristaps 1162: case (TEXICMD_COMMA):
1163: texiputchar(p, ',');
1164: break;
1.1 kristaps 1165: case (TEXICMD_COPYRIGHT):
1166: texiputchars(p, "\\(co");
1167: break;
1.42 kristaps 1168: case (TEXICMD_DH):
1169: texiputchars(p, "\\(-D");
1170: break;
1171: case (TEXICMD_DHSMALL):
1172: texiputchars(p, "\\(Sd");
1173: break;
1.2 kristaps 1174: case (TEXICMD_DOTS):
1.34 kristaps 1175: case (TEXICMD_ENDDOTS):
1.2 kristaps 1176: texiputchars(p, "...");
1177: break;
1.28 kristaps 1178: case (TEXICMD_EQUIV):
1179: texiputchars(p, "\\(==");
1180: break;
1.15 kristaps 1181: case (TEXICMD_ERROR):
1182: texiputchars(p, "error\\(->");
1.17 kristaps 1183: break;
1.43 kristaps 1184: case (TEXICMD_EURO):
1185: texiputchars(p, "\\(Eu");
1186: break;
1.42 kristaps 1187: case (TEXICMD_EXCLAMDOWN):
1188: texiputchars(p, "\\(r!");
1189: break;
1.17 kristaps 1190: case (TEXICMD_EXPANSION):
1191: texiputchars(p, "\\(->");
1.15 kristaps 1192: break;
1.43 kristaps 1193: case (TEXICMD_GEQ):
1194: texiputchars(p, "\\(>=");
1195: break;
1196: case (TEXICMD_GUILLEMETLEFT):
1197: case (TEXICMD_GUILLEMOTLEFT):
1198: texiputchars(p, "\\(Fo");
1199: break;
1200: case (TEXICMD_GUILLEMETRIGHT):
1201: case (TEXICMD_GUILLEMOTRIGHT):
1202: texiputchars(p, "\\(Fc");
1203: break;
1204: case (TEXICMD_GUILSINGLLEFT):
1205: texiputchars(p, "\\(fo");
1206: break;
1207: case (TEXICMD_GUILSINGLRIGHT):
1208: texiputchars(p, "\\(fc");
1209: break;
1.42 kristaps 1210: case (TEXICMD_L):
1211: texiputchars(p, "\\(/L");
1212: break;
1.1 kristaps 1213: case (TEXICMD_LATEX):
1214: texiputchars(p, "LaTeX");
1215: break;
1.43 kristaps 1216: case (TEXICMD_LEQ):
1217: texiputchars(p, "\\(<=");
1218: break;
1.42 kristaps 1219: case (TEXICMD_LSMALL):
1220: texiputchars(p, "\\(/l");
1221: break;
1.25 kristaps 1222: case (TEXICMD_MINUS):
1223: texiputchars(p, "\\-");
1224: break;
1.42 kristaps 1225: case (TEXICMD_O):
1226: texiputchars(p, "\\(/O");
1227: break;
1228: case (TEXICMD_OE):
1229: texiputchars(p, "\\(OE");
1230: break;
1231: case (TEXICMD_OESMALL):
1232: texiputchars(p, "\\(oe");
1233: break;
1234: case (TEXICMD_ORDF):
1235: texiputchars(p, "a");
1236: break;
1237: case (TEXICMD_ORDM):
1238: texiputchars(p, "o");
1239: break;
1240: case (TEXICMD_OSMALL):
1241: texiputchars(p, "\\(/o");
1242: break;
1.25 kristaps 1243: case (TEXICMD_PERIOD):
1244: texiputchar(p, '.');
1245: break;
1.43 kristaps 1246: case (TEXICMD_POUNDS):
1247: texiputchars(p, "\\(Po");
1248: break;
1.42 kristaps 1249: case (TEXICMD_QUESTIONDOWN):
1250: texiputchars(p, "\\(r?");
1251: break;
1.3 kristaps 1252: case (TEXICMD_QUESTIONMARK):
1253: texiputchar(p, '?');
1.15 kristaps 1254: break;
1.43 kristaps 1255: case (TEXICMD_QUOTEDBLBASE):
1256: texiputchars(p, "\\(Bq");
1257: break;
1258: case (TEXICMD_QUOTEDBLLEFT):
1259: texiputchars(p, "\\(lq");
1260: break;
1261: case (TEXICMD_QUOTEDBLRIGHT):
1262: texiputchars(p, "\\(rq");
1263: break;
1264: case (TEXICMD_QUOTESINGLBASE):
1265: texiputchars(p, "\\(bq");
1266: break;
1267: case (TEXICMD_QUOTELEFT):
1268: texiputchars(p, "\\(oq");
1269: break;
1270: case (TEXICMD_QUOTERIGHT):
1271: texiputchars(p, "\\(cq");
1272: break;
1273: case (TEXICMD_REGISTEREDSYMBOL):
1274: texiputchars(p, "\\(rg");
1275: break;
1.15 kristaps 1276: case (TEXICMD_RESULT):
1277: texiputchars(p, "\\(rA");
1.3 kristaps 1278: break;
1.34 kristaps 1279: case (TEXICMD_SLASH):
1280: texiputchar(p, '/');
1281: break;
1.35 kristaps 1282: case (TEXICMD_SS):
1283: texiputchars(p, "\\(ss");
1284: break;
1.3 kristaps 1285: case (TEXICMD_SQUIGGLE_LEFT):
1286: texiputchars(p, "{");
1287: break;
1288: case (TEXICMD_SQUIGGLE_RIGHT):
1289: texiputchars(p, "}");
1290: break;
1.38 kristaps 1291: case (TEXICMD_TEXSYM):
1.35 kristaps 1292: texiputchars(p, "TeX");
1.43 kristaps 1293: break;
1294: case (TEXICMD_TEXTDEGREE):
1295: texiputchars(p, "\\(de");
1.35 kristaps 1296: break;
1.42 kristaps 1297: case (TEXICMD_TH):
1298: texiputchars(p, "\\(TP");
1299: break;
1300: case (TEXICMD_THSMALL):
1301: texiputchars(p, "\\(Tp");
1302: break;
1.38 kristaps 1303: case (TEXICMD_TIE):
1.35 kristaps 1304: texiputchars(p, "\\ ");
1.1 kristaps 1305: break;
1.3 kristaps 1306: case (TEXICMD_COLON):
1307: case (TEXICMD_HYPHEN):
1308: break;
1.1 kristaps 1309: default:
1310: abort();
1311: }
1312:
1.42 kristaps 1313: /* Alphabetic commands have braces we ignore. */
1314: if (isalpha(texitoks[cmd].tok[0]))
1.5 kristaps 1315: doignbracket(p, cmd, buf, sz, pos);
1.1 kristaps 1316: }
1317:
1318: static void
1319: doquotation(struct texi *p, enum texicmd cmd,
1320: const char *buf, size_t sz, size_t *pos)
1321: {
1322:
1.5 kristaps 1323: teximacro(p, "Qo");
1.1 kristaps 1324: parseto(p, buf, sz, pos, "quotation");
1.5 kristaps 1325: teximacro(p, "Qc");
1.1 kristaps 1326: }
1327:
1.3 kristaps 1328: static void
1329: domath(struct texi *p, enum texicmd cmd,
1330: const char *buf, size_t sz, size_t *pos)
1331: {
1.34 kristaps 1332: size_t nest, start;
1.3 kristaps 1333:
1334: /*
1335: * Math handling is different from everything else.
1336: * We don't allow any subcomponents, and we ignore the rules in
1337: * terms of @-commands.
1338: * This departs from GNU's rules, but whatever.
1339: */
1340: while (*pos < sz && isws(buf[*pos]))
1341: advance(p, buf, pos);
1342: if (*pos == sz || '{' != buf[*pos])
1343: return;
1344: advance(p, buf, pos);
1345: if (p->seenws && p->outcol && 0 == p->literal)
1346: texiputchar(p, ' ');
1347: p->seenws = 0;
1.34 kristaps 1348: for (nest = 1, start = *pos; *pos < sz && nest > 0; ) {
1.3 kristaps 1349: if ('{' == buf[*pos])
1350: nest++;
1351: else if ('}' == buf[*pos])
1352: if (0 == --nest)
1353: continue;
1354: advance(p, buf, pos);
1355: }
1356: if (*pos == sz)
1357: return;
1358: assert('}' == buf[*pos]);
1.34 kristaps 1359: texiputbuf(p, buf, start, *pos);
1.3 kristaps 1360: advance(p, buf, pos);
1.24 kristaps 1361: }
1362:
1363: static void
1364: dovalue(struct texi *p, enum texicmd cmd,
1365: const char *buf, size_t sz, size_t *pos)
1366: {
1.28 kristaps 1367: size_t start, end, i;
1.25 kristaps 1368: char *key, *val;
1369: const char *cp;
1.24 kristaps 1370:
1371: if (TEXICMD_SET == cmd) {
1372: while (*pos < sz && isws(buf[*pos]))
1373: advance(p, buf, pos);
1374: for (start = end = *pos; end < sz; end++)
1375: if (ismspace(buf[end]))
1376: break;
1.25 kristaps 1377: /* We don't allow empty keys. */
1.24 kristaps 1378: if (start == end)
1379: return;
1.25 kristaps 1380: advanceto(p, buf, pos, end);
1.24 kristaps 1381:
1382: key = malloc(end - start + 1);
1.27 kristaps 1383: if (NULL == key)
1384: texiabort(p, NULL);
1.24 kristaps 1385: memcpy(key, &buf[start], end - start);
1386: key[end - start] = '\0';
1387:
1388: while (*pos < sz && isws(buf[*pos]))
1389: advance(p, buf, pos);
1390: for (start = end = *pos; end < sz; end++)
1391: if ('\n' == buf[end])
1392: break;
1.25 kristaps 1393: /* We do allow empty values. */
1394: advanceeoln(p, buf, sz, pos, 1);
1.24 kristaps 1395:
1396: val = malloc(end - start + 1);
1.27 kristaps 1397: if (NULL == val)
1398: texiabort(p, NULL);
1.24 kristaps 1399: memcpy(val, &buf[start], end - start);
1400: val[end - start] = '\0';
1.25 kristaps 1401: valueadd(p, key, val);
1.24 kristaps 1402: } else if (TEXICMD_VALUE == cmd) {
1403: if (p->seenws)
1404: texiputchar(p, ' ');
1405: p->seenws = 0;
1.28 kristaps 1406: if (NULL != (cp = valueblookup(p, buf, sz, pos))) {
1407: for (i = 0; i < p->valstackpos; i++)
1408: if (cp == p->valstack[i])
1409: break;
1410: if (i < p->valstackpos)
1411: texierr(p, "recursive value");
1.29 kristaps 1412: if (64 == p->valstackpos)
1413: texierr(p, "too many nested values");
1.28 kristaps 1414: p->valstack[p->valstackpos++] = cp;
1.31 kristaps 1415: parsemembuf(p, cp, strlen(cp));
1.28 kristaps 1416: p->valstackpos--;
1417: } else
1.25 kristaps 1418: texiputchars(p, "{No value}");
1.24 kristaps 1419: } else if (TEXICMD_IFCLEAR == cmd) {
1.26 kristaps 1420: if (NULL != valuellookup(p, buf, sz, pos))
1421: doignblock(p, cmd, buf, sz, pos);
1422: else
1423: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.30 kristaps 1424: } else if (TEXICMD_IFSET == cmd) {
1425: if (NULL == valuellookup(p, buf, sz, pos))
1426: doignblock(p, cmd, buf, sz, pos);
1427: else
1428: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.25 kristaps 1429: } else if (TEXICMD_CLEAR == cmd)
1430: valuelclear(p, buf, sz, pos);
1.3 kristaps 1431: }
1432:
1.1 kristaps 1433: static void
1.8 kristaps 1434: dolink(struct texi *p, enum texicmd cmd,
1.1 kristaps 1435: const char *buf, size_t sz, size_t *pos)
1436: {
1.8 kristaps 1437: int c;
1.1 kristaps 1438:
1439: switch (cmd) {
1440: case (TEXICMD_EMAIL):
1.5 kristaps 1441: teximacroopen(p, "Mt");
1.1 kristaps 1442: break;
1.3 kristaps 1443: case (TEXICMD_UREF):
1.1 kristaps 1444: case (TEXICMD_URL):
1.44 kristaps 1445: case (TEXICMD_INDICATEURL):
1.5 kristaps 1446: teximacroopen(p, "Lk");
1.1 kristaps 1447: break;
1.8 kristaps 1448: case (TEXICMD_XREF):
1449: texiputchars(p, "See Section");
1.39 kristaps 1450: teximacroopen(p, "Dq");
1.8 kristaps 1451: break;
1452: case (TEXICMD_PXREF):
1453: texiputchars(p, "see Section");
1.39 kristaps 1454: teximacroopen(p, "Dq");
1.8 kristaps 1455: break;
1.34 kristaps 1456: case (TEXICMD_INFOREF):
1457: texiputchars(p, "See Info file node");
1.39 kristaps 1458: teximacroopen(p, "Dq");
1.34 kristaps 1459: break;
1.1 kristaps 1460: default:
1.8 kristaps 1461: abort();
1.1 kristaps 1462: }
1.8 kristaps 1463:
1464: c = parsearg(p, buf, sz, pos, 0);
1465: p->ign++;
1466: while (c > 0)
1467: c = parsearg(p, buf, sz, pos, 1);
1468: p->ign--;
1469:
1.11 kristaps 1470: texipunctuate(p, buf, sz, pos);
1.8 kristaps 1471: teximacroclose(p);
1472: }
1473:
1474: static void
1475: doignargn(struct texi *p, enum texicmd cmd,
1476: const char *buf, size_t sz, size_t *pos)
1477: {
1478: int c;
1479:
1480: c = parsearg(p, buf, sz, pos, 0);
1481: p->ign++;
1482: while (c > 0)
1483: c = parsearg(p, buf, sz, pos, 1);
1484: p->ign--;
1.1 kristaps 1485: }
1486:
1.23 kristaps 1487: /*
1488: * Sections can be made subsections and so on by way of the
1489: * @raiseections and @lowersections commands.
1490: * Perform this check here and return the actual section number adjusted
1491: * to the raise level.
1492: */
1493: static int
1494: sectioner(struct texi *p, int sec)
1495: {
1496:
1497: if ((sec -= p->secoffs) < 0) {
1498: texiwarn(p, "section below minimum, clamping");
1499: return(0);
1500: } else if (sec >= SECTSZ) {
1501: texiwarn(p, "section above maximum, clamping");
1502: return(SECTSZ - 1);
1503: }
1504: return(sec);
1505: }
1506:
1507: static void
1508: dosubsubsection(struct texi *p, enum texicmd cmd,
1509: const char *buf, size_t sz, size_t *pos)
1510: {
1511: int sec;
1512:
1513: sec = sectioner(p, 3);
1514:
1515: /* We don't have a subsubsubsection, so make one up. */
1516: texivspace(p);
1517: teximacroopen(p, sects[sec]);
1518: parseeoln(p, buf, sz, pos);
1519: teximacroclose(p);
1520: texivspace(p);
1521: }
1522:
1.1 kristaps 1523: static void
1524: dosubsection(struct texi *p, enum texicmd cmd,
1525: const char *buf, size_t sz, size_t *pos)
1526: {
1.23 kristaps 1527: int sec;
1528:
1529: sec = sectioner(p, 2);
1.13 kristaps 1530:
1531: if (p->outmacro)
1.23 kristaps 1532: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13 kristaps 1533: else if (p->literal)
1.23 kristaps 1534: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1 kristaps 1535:
1.21 kristaps 1536: /* We don't have a subsubsection, so make one up. */
1.34 kristaps 1537: if (sec > 1)
1538: texivspace(p);
1.23 kristaps 1539: teximacroopen(p, sects[sec]);
1.3 kristaps 1540: parseeoln(p, buf, sz, pos);
1.5 kristaps 1541: teximacroclose(p);
1.34 kristaps 1542: if (sec > 1)
1543: texivspace(p);
1.1 kristaps 1544: }
1545:
1546: static void
1.23 kristaps 1547: dosecoffs(struct texi *p, enum texicmd cmd,
1548: const char *buf, size_t sz, size_t *pos)
1549: {
1550:
1551: if (TEXICMD_RAISESECTIONS == cmd)
1552: p->secoffs++;
1553: else
1554: p->secoffs--;
1555: }
1556:
1557: static void
1.1 kristaps 1558: dosection(struct texi *p, enum texicmd cmd,
1.23 kristaps 1559: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 1560: {
1.23 kristaps 1561: int sec;
1.12 kristaps 1562:
1563: switch (cmd) {
1564: case (TEXICMD_APPENDIX):
1565: case (TEXICMD_CHAPTER):
1566: case (TEXICMD_TOP):
1567: case (TEXICMD_UNNUMBERED):
1.23 kristaps 1568: sec = sectioner(p, 0);
1.12 kristaps 1569: break;
1570: case (TEXICMD_APPENDIXSEC):
1571: case (TEXICMD_HEADING):
1572: case (TEXICMD_SECTION):
1573: case (TEXICMD_UNNUMBEREDSEC):
1.23 kristaps 1574: sec = sectioner(p, 1);
1.12 kristaps 1575: break;
1576: default:
1577: abort();
1578: }
1.1 kristaps 1579:
1.3 kristaps 1580: if (p->outmacro)
1.23 kristaps 1581: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3 kristaps 1582: else if (p->literal)
1.23 kristaps 1583: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3 kristaps 1584:
1.23 kristaps 1585: teximacroopen(p, sects[sec]);
1.3 kristaps 1586: parseeoln(p, buf, sz, pos);
1587: teximacroclose(p);
1.11 kristaps 1588: p->seenvs = 1;
1.3 kristaps 1589: }
1590:
1591: static void
1592: dosp(struct texi *p, enum texicmd cmd,
1593: const char *buf, size_t sz, size_t *pos)
1594: {
1595:
1.37 kristaps 1596: if (p->literal)
1597: texiputchar(p, '\n');
1598: else
1599: texivspace(p);
1.12 kristaps 1600: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1601: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1602: }
1603:
1604: static void
1605: dotop(struct texi *p, enum texicmd cmd,
1606: const char *buf, size_t sz, size_t *pos)
1607: {
1.10 kristaps 1608: const char *cp;
1609: time_t t;
1610: char date[32];
1611:
1.26 kristaps 1612: if (--p->ign)
1613: texierr(p, "@top command while ignoring (%d)", p->ign);
1614:
1.18 kristaps 1615: /*
1616: * Here we print our standard mdoc(7) prologue.
1617: * We use the title set with @settitle for the `Nd' description
1618: * and the source document filename (the first one as invoked on
1619: * the command line) for the title.
1620: * The date is set to the current date.
1621: */
1.10 kristaps 1622: t = time(NULL);
1623: strftime(date, sizeof(date), "%F", localtime(&t));
1.1 kristaps 1624:
1.10 kristaps 1625: teximacroopen(p, "Dd");
1626: texiputchars(p, date);
1627: teximacroclose(p);
1628: teximacroopen(p, "Dt");
1629: for (cp = p->title; '\0' != *cp; cp++)
1630: texiputchar(p, toupper(*cp));
1.11 kristaps 1631: texiputchars(p, " 7");
1.10 kristaps 1632: teximacroclose(p);
1.5 kristaps 1633: teximacro(p, "Os");
1634: teximacro(p, "Sh NAME");
1.10 kristaps 1635: teximacroopen(p, "Nm");
1.41 kristaps 1636: for (cp = p->title; '\0' != *cp; cp++)
1637: texiputchar(p, *cp);
1.10 kristaps 1638: teximacroclose(p);
1639: teximacroopen(p, "Nd");
1.41 kristaps 1640: if (NULL != p->subtitle)
1641: for (cp = p->subtitle; '\0' != *cp; cp++)
1642: texiputchar(p, *cp);
1643: else
1644: texiputchars(p, "Unknown description");
1.10 kristaps 1645: teximacroclose(p);
1.11 kristaps 1646: p->seenvs = 1;
1.12 kristaps 1647: dosection(p, cmd, buf, sz, pos);
1.1 kristaps 1648: }
1649:
1650: static void
1651: doitem(struct texi *p, enum texicmd cmd,
1652: const char *buf, size_t sz, size_t *pos)
1653: {
1654:
1.18 kristaps 1655: /* Multitable is using raw tbl(7). */
1656: if (TEXILIST_TABLE == p->list) {
1657: texiputchar(p, '\n');
1658: return;
1659: }
1660:
1.3 kristaps 1661: if (p->outmacro)
1662: texierr(p, "item in open line scope!?");
1663: else if (p->literal)
1664: texierr(p, "item in a literal scope!?");
1665:
1666: switch (p->list) {
1667: case (TEXILIST_ITEM):
1.5 kristaps 1668: teximacroopen(p, "It");
1.3 kristaps 1669: break;
1670: case (TEXILIST_NOITEM):
1.5 kristaps 1671: teximacro(p, "It");
1.3 kristaps 1672: break;
1673: default:
1.11 kristaps 1674: texivspace(p);
1.3 kristaps 1675: break;
1676: }
1.18 kristaps 1677:
1678: /* Trick so we don't start with Pp. */
1.11 kristaps 1679: p->seenvs = 1;
1.3 kristaps 1680: parseeoln(p, buf, sz, pos);
1.1 kristaps 1681:
1.3 kristaps 1682: if (TEXILIST_ITEM == p->list)
1683: teximacroclose(p);
1.9 kristaps 1684: else if (p->outcol > 0)
1.1 kristaps 1685: texiputchar(p, '\n');
1.18 kristaps 1686: }
1687:
1688: static void
1689: dotab(struct texi *p, enum texicmd cmd,
1690: const char *buf, size_t sz, size_t *pos)
1691: {
1692:
1693: /* This command is only useful in @multitable. */
1694: if (TEXILIST_TABLE == p->list)
1695: texiputchar(p, '\t');
1696: }
1697:
1698: static void
1699: domultitable(struct texi *p, enum texicmd cmd,
1700: const char *buf, size_t sz, size_t *pos)
1701: {
1702: enum texilist sv = p->list;
1.32 kristaps 1703: int svliteral = p->literal;
1.18 kristaps 1704: enum texicmd type;
1705: size_t i, end, columns;
1706:
1707: p->list = TEXILIST_TABLE;
1.32 kristaps 1708: /*
1709: * TS/TE blocks aren't "in mdoc(7)", so we can disregard the
1710: * fact that we're in literal mode right now.
1711: */
1712: p->literal = 0;
1.18 kristaps 1713: teximacro(p, "TS");
1714: columns = 0;
1715:
1716: /* Advance to the first argument... */
1717: while (*pos < sz && isws(buf[*pos]))
1718: advance(p, buf, pos);
1719:
1720: /* Make sure we don't print anything when scanning. */
1721: p->ign++;
1722: if ('@' == buf[*pos]) {
1723: /*
1724: * Look for @columnfractions.
1725: * We ignore these, but we do use the number of
1726: * arguments to set the number of columns that we'll
1727: * have.
1728: */
1.30 kristaps 1729: type = texicmd(p, buf, *pos, sz, &end, NULL);
1.18 kristaps 1730: advanceto(p, buf, pos, end);
1731: if (TEXICMD_COLUMNFRACTIONS != type)
1.25 kristaps 1732: texierr(p, "unknown multitable command");
1.18 kristaps 1733: while (*pos < sz && '\n' != buf[*pos]) {
1734: while (*pos < sz && isws(buf[*pos]))
1735: advance(p, buf, pos);
1736: while (*pos < sz && ! isws(buf[*pos])) {
1737: if ('\n' == buf[*pos])
1738: break;
1739: advance(p, buf, pos);
1740: }
1741: columns++;
1742: }
1743: } else
1744: /*
1745: * We have arguments.
1746: * We could parse these, but it's easier to just let
1747: * tbl(7) figure it out.
1748: * So use this only to count arguments.
1749: */
1750: while (parselinearg(p, buf, sz, pos) > 0)
1751: columns++;
1752: p->ign--;
1753:
1754: /* Left-justify each table entry. */
1755: for (i = 0; i < columns; i++) {
1756: if (i > 0)
1757: texiputchar(p, ' ');
1758: texiputchar(p, 'l');
1759: }
1760: texiputchars(p, ".\n");
1761: p->outmacro++;
1762: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1763: p->outmacro--;
1764: teximacro(p, "TE");
1.32 kristaps 1765: p->literal = svliteral;
1.18 kristaps 1766: p->list = sv;
1.1 kristaps 1767: }
1768:
1769: static void
1770: dotable(struct texi *p, enum texicmd cmd,
1771: const char *buf, size_t sz, size_t *pos)
1772: {
1.3 kristaps 1773: enum texilist sv = p->list;
1774:
1775: p->list = TEXILIST_ITEM;
1.5 kristaps 1776: teximacro(p, "Bl -tag -width Ds");
1.12 kristaps 1777: /* FIXME: ignore and parseeoln. */
1778: advanceeoln(p, buf, sz, pos, 1);
1.11 kristaps 1779: p->seenvs = 1;
1.20 kristaps 1780: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 1781: teximacro(p, "El");
1.3 kristaps 1782: p->list = sv;
1.44 kristaps 1783: }
1784:
1785: static void
1786: doend(struct texi *p, enum texicmd cmd,
1787: const char *buf, size_t sz, size_t *pos)
1788: {
1789: size_t start;
1790:
1791: while (*pos < sz && isws(buf[*pos]))
1792: advance(p, buf, pos);
1793: start = *pos;
1794: while (*pos < sz && '\n' != buf[*pos])
1795: advance(p, buf, pos);
1796:
1797: texiwarn(p, "unexpected \"end\": %.*s", (int)(*pos - start), &buf[start]);
1798: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1799: }
1800:
1801: static void
1.2 kristaps 1802: doenumerate(struct texi *p, enum texicmd cmd,
1803: const char *buf, size_t sz, size_t *pos)
1804: {
1.3 kristaps 1805: enum texilist sv = p->list;
1.2 kristaps 1806:
1.3 kristaps 1807: p->list = TEXILIST_NOITEM;
1.5 kristaps 1808: teximacro(p, "Bl -enum");
1.11 kristaps 1809: p->seenvs = 1;
1.12 kristaps 1810: /* FIXME: ignore and parseeoln. */
1811: advanceeoln(p, buf, sz, pos, 1);
1.2 kristaps 1812: parseto(p, buf, sz, pos, "enumerate");
1.5 kristaps 1813: teximacro(p, "El");
1.3 kristaps 1814: p->list = sv;
1.2 kristaps 1815: }
1816:
1817: static void
1.1 kristaps 1818: doitemize(struct texi *p, enum texicmd cmd,
1819: const char *buf, size_t sz, size_t *pos)
1820: {
1.3 kristaps 1821: enum texilist sv = p->list;
1.1 kristaps 1822:
1.21 kristaps 1823: p->list = TEXILIST_NOITEM;
1.5 kristaps 1824: teximacro(p, "Bl -bullet");
1.11 kristaps 1825: p->seenvs = 1;
1.12 kristaps 1826: /* FIXME: ignore and parseeoln. */
1827: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1828: parseto(p, buf, sz, pos, "itemize");
1.5 kristaps 1829: teximacro(p, "El");
1.3 kristaps 1830: p->list = sv;
1.1 kristaps 1831: }
1832:
1833: static void
1834: doignbracket(struct texi *p, enum texicmd cmd,
1835: const char *buf, size_t sz, size_t *pos)
1836: {
1837:
1.3 kristaps 1838: p->ign++;
1.1 kristaps 1839: parsebracket(p, buf, sz, pos);
1.3 kristaps 1840: p->ign--;
1.1 kristaps 1841: }
1842:
1843: static void
1844: doignline(struct texi *p, enum texicmd cmd,
1845: const char *buf, size_t sz, size_t *pos)
1846: {
1847:
1.12 kristaps 1848: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1849: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1850: }
1851:
1.8 kristaps 1852: /*
1853: * Parse colon-separated directories from "cp" (if not NULL) and returns
1854: * the array of pointers.
1.40 kristaps 1855: * Prepends "base" to the array, if found.
1.8 kristaps 1856: * This does NOT sanitise the directories!
1857: */
1.5 kristaps 1858: static char **
1.27 kristaps 1859: parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz)
1.5 kristaps 1860: {
1861: char *tok, *str, *tofree;
1862: const char *cpp;
1.40 kristaps 1863: size_t i = 0;
1.5 kristaps 1864: char **dirs;
1865:
1.40 kristaps 1866: /* Count up our expected arguments. */
1867: *sz = NULL != base;
1868: if (NULL != (cpp = cp))
1869: for ((*sz)++; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1.5 kristaps 1870: cpp++;
1871:
1.40 kristaps 1872: if (0 == *sz)
1873: return(NULL);
1.27 kristaps 1874: if (NULL == (dirs = calloc(*sz, sizeof(char *))))
1875: texiabort(p, NULL);
1.40 kristaps 1876: if (NULL != base && NULL == (dirs[i++] = strdup(base)))
1.27 kristaps 1877: texiabort(p, NULL);
1.5 kristaps 1878: if (NULL == cp)
1879: return(dirs);
1.27 kristaps 1880: if (NULL == (tofree = tok = str = strdup(cp)))
1881: texiabort(p, NULL);
1.5 kristaps 1882:
1.40 kristaps 1883: for ( ; NULL != (tok = strsep(&str, ":")); i++)
1.27 kristaps 1884: if (NULL == (dirs[i] = strdup(tok)))
1885: texiabort(p, NULL);
1.5 kristaps 1886:
1887: free(tofree);
1888: return(dirs);
1889: }
1890:
1.1 kristaps 1891: int
1892: main(int argc, char *argv[])
1893: {
1894: struct texi texi;
1.2 kristaps 1895: int c;
1.37 kristaps 1896: char *dirpath, *dir, *ccp;
1.10 kristaps 1897: const char *progname, *Idir, *cp;
1.1 kristaps 1898:
1899: progname = strrchr(argv[0], '/');
1900: if (progname == NULL)
1901: progname = argv[0];
1902: else
1903: ++progname;
1904:
1.10 kristaps 1905: memset(&texi, 0, sizeof(struct texi));
1.40 kristaps 1906: texi.ign = 1;
1.5 kristaps 1907: Idir = NULL;
1.10 kristaps 1908:
1.5 kristaps 1909: while (-1 != (c = getopt(argc, argv, "I:")))
1.1 kristaps 1910: switch (c) {
1.5 kristaps 1911: case ('I'):
1912: Idir = optarg;
1913: break;
1.1 kristaps 1914: default:
1915: goto usage;
1916: }
1917:
1918: argv += optind;
1.40 kristaps 1919: argc -= optind;
1.1 kristaps 1920:
1.40 kristaps 1921: if (argc > 0) {
1922: if (NULL == (dirpath = strdup(argv[0])))
1923: texiabort(&texi, NULL);
1924: if (NULL == (dir = dirname(dirpath)))
1925: texiabort(&texi, NULL);
1926: if (NULL != (cp = strrchr(argv[0], '/')))
1927: texi.title = strdup(cp + 1);
1928: else
1929: texi.title = strdup(argv[0]);
1930: if (NULL == texi.title)
1931: texiabort(&texi, NULL);
1932: else if (NULL != (ccp = strchr(texi.title, '.')))
1933: *ccp = '\0';
1934: texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
1935: free(dirpath);
1936: parsefile(&texi, argv[0], 1);
1937: } else {
1938: texi.title = strdup("Unknown Manual");
1939: texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
1940: parsestdin(&texi);
1941: }
1.27 kristaps 1942:
1.2 kristaps 1943: texiexit(&texi);
1944: return(EXIT_FAILURE);
1.1 kristaps 1945: usage:
1.40 kristaps 1946: fprintf(stderr, "usage: %s [-Idirs] [file]\n", progname);
1.1 kristaps 1947: return(EXIT_FAILURE);
1948: }
CVSweb