Annotation of texi2mdoc/main.c, Revision 1.44
1.44 ! kristaps 1: /* $Id: main.c,v 1.43 2015/02/24 20:34:43 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);
540: blk = memmem(&buf[start], sz, endtok, endtoksz);
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.26 kristaps 569: char end[32];
570: const char *term;
571: size_t endsz, endpos;
572:
573: /*
574: * We want to completely ignore everything in these blocks, so
575: * simply jump to the @end block.
576: */
577: endsz = snprintf(end, sizeof(end),
578: "\n@end %s\n", texitoks[cmd].tok);
579: assert(endsz < sizeof(end));
580:
581: /*
582: * Look up where our end token occurs.
583: * Set our end position based on the relative offset of that
584: * from our current position, or the EOF if we don't have a
585: * proper ending point.
586: */
587: term = memmem(&buf[*pos], sz, end, endsz);
588: endpos = NULL == term ? sz :
1.36 kristaps 589: *pos + (size_t)(term - &buf[*pos]);
1.26 kristaps 590: assert(endpos <= sz);
591: while (*pos < endpos)
592: advance(p, buf, pos);
593:
594: /* Only do this if we're not already at the end. */
595: if (endpos < sz)
596: advanceto(p, buf, pos, endpos + endsz);
1.1 kristaps 597: }
598:
599: static void
1.3 kristaps 600: doblock(struct texi *p, enum texicmd cmd,
1.1 kristaps 601: const char *buf, size_t sz, size_t *pos)
602: {
603:
1.5 kristaps 604: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.1 kristaps 605: }
606:
607: static void
1.12 kristaps 608: doinline(struct texi *p, enum texicmd cmd,
609: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 610: {
1.21 kristaps 611: const char *macro = NULL;
1.12 kristaps 612:
613: switch (cmd) {
1.21 kristaps 614: case (TEXICMD_CODE):
615: case (TEXICMD_KBD):
616: case (TEXICMD_SAMP):
617: case (TEXICMD_T):
618: macro = "Li";
619: break;
620: case (TEXICMD_CITE):
621: case (TEXICMD_DFN):
622: case (TEXICMD_EMPH):
623: case (TEXICMD_I):
624: case (TEXICMD_SLANTED):
625: macro = "Em";
626: break;
627: case (TEXICMD_B):
628: case (TEXICMD_STRONG):
629: macro = "Sy";
630: break;
1.12 kristaps 631: case (TEXICMD_COMMAND):
632: macro = "Xr";
633: break;
634: case (TEXICMD_ENV):
635: macro = "Ev";
636: break;
637: case (TEXICMD_FILE):
638: macro = "Pa";
639: break;
1.16 kristaps 640: case (TEXICMD_OPTION):
641: macro = "Op";
642: break;
1.12 kristaps 643: case (TEXICMD_VAR):
644: macro = "Va";
645: break;
646: default:
1.22 kristaps 647: break;
1.12 kristaps 648: }
649:
1.25 kristaps 650: if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) {
1.12 kristaps 651: parsebracket(p, buf, sz, pos);
652: return;
653: }
1.1 kristaps 654:
1.5 kristaps 655: teximacroopen(p, macro);
1.1 kristaps 656: p->seenws = 0;
657: parsebracket(p, buf, sz, pos);
1.11 kristaps 658: texipunctuate(p, buf, sz, pos);
1.5 kristaps 659: teximacroclose(p);
1.1 kristaps 660: }
661:
662: static void
1.25 kristaps 663: doverb(struct texi *p, enum texicmd cmd,
1.33 kristaps 664: const char *buf, size_t sz, size_t *pos)
665: {
666: char delim;
1.34 kristaps 667: size_t start;
1.33 kristaps 668:
669: while (*pos < sz && isws(buf[*pos]))
670: advance(p, buf, pos);
671: if (*pos == sz || '{' != buf[*pos])
672: return;
673: advance(p, buf, pos);
674: if (*pos == sz)
675: return;
676:
677: delim = buf[*pos];
678: advance(p, buf, pos);
679: /* Make sure we flush out our initial whitespace... */
680: if (p->seenws && p->outcol && 0 == p->literal)
681: texiputchar(p, ' ');
682: p->seenws = 0;
1.34 kristaps 683: start = *pos;
1.33 kristaps 684: /* Read until we see the delimiter then end-brace. */
685: while (*pos < sz - 1) {
686: if (buf[*pos] == delim && buf[*pos + 1] == '}')
687: break;
688: advance(p, buf, pos);
689: }
690: if (*pos == sz - 1)
691: return;
1.34 kristaps 692: texiputbuf(p, buf, start, *pos);
693:
1.33 kristaps 694: /* Make sure we read after the end-brace. */
695: assert(delim == buf[*pos]);
696: advance(p, buf, pos);
697: assert('}' == buf[*pos]);
698: advance(p, buf, pos);
699: }
700:
701: static void
702: doverbatim(struct texi *p, enum texicmd cmd,
1.25 kristaps 703: const char *buf, size_t sz, size_t *pos)
704: {
705: const char *end, *term;
706: size_t endsz, endpos;
707:
1.26 kristaps 708: advanceeoln(p, buf, sz, pos, 1);
709:
1.25 kristaps 710: /* We end at exactly this token. */
711: end = "\n@end verbatim\n";
712: endsz = strlen(end);
713:
714: /*
715: * Look up where our end token occurs.
716: * Set our end position based on the relative offset of that
717: * from our current position.
718: */
719: term = memmem(&buf[*pos], sz, end, endsz);
720: endpos = NULL == term ? sz :
1.36 kristaps 721: *pos + (size_t)(term - &buf[*pos]);
1.25 kristaps 722:
723: teximacro(p, "Bd -literal -offset indent");
724: assert(endpos <= sz);
1.26 kristaps 725: while (*pos < endpos) {
1.41 kristaps 726: texiputchar(p, buf[*pos]);
1.25 kristaps 727: advance(p, buf, pos);
728: }
729: teximacro(p, "Ed");
1.26 kristaps 730: advanceto(p, buf, pos, endpos + endsz);
1.25 kristaps 731: }
732:
733: static void
1.16 kristaps 734: doverbinclude(struct texi *p, enum texicmd cmd,
735: const char *buf, size_t sz, size_t *pos)
736: {
1.25 kristaps 737: char fname[PATH_MAX], path[PATH_MAX];
738: int rc;
739: size_t i, end;
740: const char *v;
741: enum texicmd type;
1.16 kristaps 742:
743: while (*pos < sz && ' ' == buf[*pos])
744: advance(p, buf, pos);
745:
1.25 kristaps 746: for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.16 kristaps 747: if (i == sizeof(fname) - 1)
748: break;
1.25 kristaps 749: if ('@' != buf[*pos]) {
750: fname[i++] = buf[*pos];
751: advance(p, buf, pos);
752: continue;
753: }
1.30 kristaps 754: type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25 kristaps 755: advanceto(p, buf, pos, end);
756: if (TEXICMD_VALUE != type)
757: texierr(p, "unknown verbatiminclude command");
758: v = valueblookup(p, buf, sz, pos);
759: if (NULL == v)
760: continue;
761: while ('\0' != *v) {
762: if (i == sizeof(fname) - 1)
763: break;
764: fname[i++] = *v++;
765: }
766: if ('\0' != *v)
767: break;
1.16 kristaps 768: }
769:
770: if (i == 0)
771: texierr(p, "path too short");
772: else if ('\n' != buf[*pos])
773: texierr(p, "path too long");
774: else if ('/' == fname[0])
775: texierr(p, "no absolute paths");
776: fname[i] = '\0';
777:
778: if (strstr(fname, "../") || strstr(fname, "/.."))
779: texierr(p, "insecure path");
780:
781: rc = snprintf(path, sizeof(path),
782: "%s/%s", p->dirs[0], fname);
783: if (rc < 0)
784: texierr(p, "couldn't format path");
785: else if ((size_t)rc >= sizeof(path))
786: texierr(p, "path too long");
787:
788: parsefile(p, path, 0);
789: }
790:
791: static void
1.2 kristaps 792: doinclude(struct texi *p, enum texicmd cmd,
793: const char *buf, size_t sz, size_t *pos)
794: {
1.25 kristaps 795: char fname[PATH_MAX], path[PATH_MAX];
796: size_t i, end;
797: int rc;
798: const char *v;
799: enum texicmd type;
1.2 kristaps 800:
801: while (*pos < sz && ' ' == buf[*pos])
802: advance(p, buf, pos);
803:
804: /* Read in the filename. */
1.25 kristaps 805: for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.2 kristaps 806: if (i == sizeof(fname) - 1)
807: break;
1.25 kristaps 808: if ('@' != buf[*pos]) {
809: fname[i++] = buf[*pos];
810: advance(p, buf, pos);
811: continue;
812: }
1.30 kristaps 813: type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25 kristaps 814: advanceto(p, buf, pos, end);
815: if (TEXICMD_VALUE != type)
816: texierr(p, "unknown include command");
817: v = valueblookup(p, buf, sz, pos);
818: if (NULL == v)
819: continue;
820: while ('\0' != *v) {
821: if (i == sizeof(fname) - 1)
822: break;
823: fname[i++] = *v++;
824: }
825: if ('\0' != *v)
826: break;
1.2 kristaps 827: }
828:
829: if (i == 0)
830: texierr(p, "path too short");
831: else if ('\n' != buf[*pos])
832: texierr(p, "path too long");
833: else if ('/' == fname[0])
834: texierr(p, "no absolute paths");
835: fname[i] = '\0';
836:
837: if (strstr(fname, "../") || strstr(fname, "/.."))
838: texierr(p, "insecure path");
839:
1.5 kristaps 840: for (i = 0; i < p->dirsz; i++) {
841: rc = snprintf(path, sizeof(path),
842: "%s/%s", p->dirs[i], fname);
843: if (rc < 0)
844: texierr(p, "couldn't format path");
845: else if ((size_t)rc >= sizeof(path))
846: texierr(p, "path too long");
847: else if (-1 == access(path, R_OK))
848: continue;
849:
1.16 kristaps 850: parsefile(p, path, 1);
1.5 kristaps 851: return;
852: }
1.2 kristaps 853:
1.5 kristaps 854: texierr(p, "couldn't find %s in includes", fname);
1.2 kristaps 855: }
856:
857: static void
1.1 kristaps 858: dobracket(struct texi *p, enum texicmd cmd,
859: const char *buf, size_t sz, size_t *pos)
860: {
861:
862: parsebracket(p, buf, sz, pos);
863: }
864:
865: static void
1.3 kristaps 866: dodisplay(struct texi *p, enum texicmd cmd,
867: const char *buf, size_t sz, size_t *pos)
868: {
869:
1.20 kristaps 870: switch (cmd) {
871: case (TEXICMD_FORMAT):
872: case (TEXICMD_SMALLFORMAT):
873: teximacro(p, "Bd -filled");
874: break;
875: default:
876: teximacro(p, "Bd -filled -offset indent");
877: break;
878: }
879:
1.11 kristaps 880: p->seenvs = 1;
1.12 kristaps 881: /* FIXME: ignore and parseeoln. */
1.3 kristaps 882: advanceeoln(p, buf, sz, pos, 1);
1.13 kristaps 883: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 884: teximacro(p, "Ed");
1.3 kristaps 885: }
886:
887: static void
1.1 kristaps 888: doexample(struct texi *p, enum texicmd cmd,
889: const char *buf, size_t sz, size_t *pos)
890: {
891:
1.5 kristaps 892: teximacro(p, "Bd -literal -offset indent");
1.12 kristaps 893: /* FIXME: ignore and parseeoln. */
1.3 kristaps 894: advanceeoln(p, buf, sz, pos, 1);
895: p->literal++;
1.13 kristaps 896: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 897: p->literal--;
1.5 kristaps 898: teximacro(p, "Ed");
1.1 kristaps 899: }
900:
901: static void
902: dobye(struct texi *p, enum texicmd cmd,
903: const char *buf, size_t sz, size_t *pos)
904: {
905:
906: texiexit(p);
907: exit(EXIT_SUCCESS);
908: }
909:
910: static void
1.10 kristaps 911: dotitle(struct texi *p, enum texicmd cmd,
912: const char *buf, size_t sz, size_t *pos)
913: {
914: size_t start, end;
915:
916: while (*pos < sz && isws(buf[*pos]))
917: advance(p, buf, pos);
918: start = end = *pos;
919: while (end < sz && '\n' != buf[end])
920: end++;
1.26 kristaps 921: advanceeoln(p, buf, sz, pos, 1);
1.10 kristaps 922: free(p->subtitle);
923: p->subtitle = malloc(end - start + 1);
1.27 kristaps 924: if (NULL == p->subtitle)
925: texiabort(p, NULL);
1.10 kristaps 926: memcpy(p->subtitle, &buf[start], end - start);
927: p->subtitle[end - start] = '\0';
928: }
929:
930: static void
1.16 kristaps 931: doaccent(struct texi *p, enum texicmd cmd,
932: const char *buf, size_t sz, size_t *pos)
933: {
1.42 kristaps 934: int brace = 0;
1.16 kristaps 935:
1.42 kristaps 936: if (*pos == sz) {
937: texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
1.16 kristaps 938: return;
1.42 kristaps 939: }
940:
941: /* Pad us with space, if necessary. */
942: if (p->seenws && p->outcol && 0 == p->literal) {
1.34 kristaps 943: texiputchar(p, ' ');
1.42 kristaps 944: p->seenws = 0;
945: }
946:
947: /*
948: * If we're braced, then that's that.
949: * Otherwise, in a special Texinfo case: if we're a non
950: * alphabetic command of one letter, then the next character is
951: * the critical one.
952: * Otherwise, space can sit between us and our argument.
953: */
954: if ('{' == buf[*pos]) {
955: brace = 1;
956: advance(p, buf, pos);
957: } else if (isalpha(texitoks[cmd].tok[0]))
1.34 kristaps 958: while (*pos < sz && isws(buf[*pos]))
959: advance(p, buf, pos);
1.42 kristaps 960:
961: if (*pos == sz) {
962: texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
963: return;
964: }
965:
966: switch (cmd) {
1.16 kristaps 967: case (TEXICMD_ACUTE):
968: switch (buf[*pos]) {
969: case ('a'): case ('A'):
970: case ('e'): case ('E'):
971: case ('i'): case ('I'):
972: case ('o'): case ('O'):
973: case ('u'): case ('U'):
974: texiputchars(p, "\\(\'");
975: break;
976: default:
1.42 kristaps 977: texiwarn(p, "ignoring accent");
978: break;
1.16 kristaps 979: }
1.42 kristaps 980: texiputchar(p, buf[*pos]);
981: advance(p, buf, pos);
982: break;
983: case (TEXICMD_CEDILLA):
984: if ('c' == buf[*pos] || 'C' == buf[*pos])
985: texiputchars(p, "\\(,");
986: else
987: texiwarn(p, "ignoring accent");
988: texiputchar(p, buf[*pos]);
989: advance(p, buf, pos);
1.16 kristaps 990: break;
991: case (TEXICMD_CIRCUMFLEX):
992: switch (buf[*pos]) {
993: case ('a'): case ('A'):
994: case ('e'): case ('E'):
995: case ('i'): case ('I'):
996: case ('o'): case ('O'):
997: case ('u'): case ('U'):
998: texiputchars(p, "\\(^");
999: break;
1000: default:
1.42 kristaps 1001: texiwarn(p, "ignoring accent");
1.34 kristaps 1002: break;
1.16 kristaps 1003: }
1.42 kristaps 1004: texiputchar(p, buf[*pos]);
1005: advance(p, buf, pos);
1006: break;
1007: case (TEXICMD_DOTLESS):
1008: if ('i' == buf[*pos] || 'j' == buf[*pos])
1009: texiputchars(p, "\\(.");
1010: else
1011: texiwarn(p, "ignoring accent");
1012: texiputchar(p, buf[*pos]);
1013: advance(p, buf, pos);
1.16 kristaps 1014: break;
1015: case (TEXICMD_GRAVE):
1016: switch (buf[*pos]) {
1017: case ('a'): case ('A'):
1018: case ('e'): case ('E'):
1019: case ('i'): case ('I'):
1020: case ('o'): case ('O'):
1021: case ('u'): case ('U'):
1022: texiputchars(p, "\\(`");
1023: break;
1024: default:
1.42 kristaps 1025: texiwarn(p, "ignoring accent");
1026: break;
1.16 kristaps 1027: }
1.42 kristaps 1028: texiputchar(p, buf[*pos]);
1029: advance(p, buf, pos);
1.16 kristaps 1030: break;
1031: case (TEXICMD_TILDE):
1032: switch (buf[*pos]) {
1033: case ('a'): case ('A'):
1034: case ('n'): case ('N'):
1035: case ('o'): case ('O'):
1036: texiputchars(p, "\\(~");
1037: break;
1038: default:
1.42 kristaps 1039: texiwarn(p, "ignoring accent");
1.34 kristaps 1040: break;
1.16 kristaps 1041: }
1.42 kristaps 1042: texiputchar(p, buf[*pos]);
1043: advance(p, buf, pos);
1.16 kristaps 1044: break;
1045: case (TEXICMD_UMLAUT):
1046: switch (buf[*pos]) {
1047: case ('a'): case ('A'):
1048: case ('e'): case ('E'):
1049: case ('i'): case ('I'):
1050: case ('o'): case ('O'):
1051: case ('u'): case ('U'):
1052: case ('y'):
1053: texiputchars(p, "\\(:");
1054: break;
1055: default:
1.42 kristaps 1056: texiwarn(p, "ignoring accent");
1.34 kristaps 1057: break;
1.16 kristaps 1058: }
1.42 kristaps 1059: texiputchar(p, buf[*pos]);
1060: advance(p, buf, pos);
1.16 kristaps 1061: break;
1062: default:
1.42 kristaps 1063: texiputchar(p, buf[*pos]);
1064: advance(p, buf, pos);
1065: break;
1066: }
1067:
1068: if (brace) {
1069: while (*pos < sz && '}' != buf[*pos]) {
1070: texiputchar(p, buf[*pos]);
1071: advance(p, buf, pos);
1072: }
1073: if (*pos < sz)
1074: advance(p, buf, pos);
1075: }
1076:
1077: switch (cmd) {
1078: case (TEXICMD_TIEACCENT):
1079: texiputchar(p, ']');
1080: break;
1081: case (TEXICMD_DOTACCENT):
1082: texiputchar(p, '*');
1083: break;
1084: default:
1085: break;
1.16 kristaps 1086: }
1087: }
1088:
1089: static void
1.1 kristaps 1090: dosymbol(struct texi *p, enum texicmd cmd,
1091: const char *buf, size_t sz, size_t *pos)
1092: {
1093:
1.42 kristaps 1094: /* Remember to pad us. */
1.43 kristaps 1095: if (p->seenws && p->outcol && 0 == p->literal)
1.3 kristaps 1096: texiputchar(p, ' ');
1.43 kristaps 1097:
1098: p->seenws = 0;
1.3 kristaps 1099:
1.1 kristaps 1100: switch (cmd) {
1.42 kristaps 1101: case (TEXICMD_AA):
1102: texiputchars(p, "\\(oA");
1103: break;
1104: case (TEXICMD_AASMALL):
1105: texiputchars(p, "\\(oa");
1106: break;
1107: case (TEXICMD_AE):
1108: texiputchars(p, "\\(AE");
1109: break;
1110: case (TEXICMD_AESMALL):
1111: texiputchars(p, "\\(ae");
1112: break;
1.3 kristaps 1113: case (TEXICMD_ASTERISK):
1114: case (TEXICMD_NEWLINE):
1115: case (TEXICMD_SPACE):
1.18 kristaps 1116: case (TEXICMD_TABSYM):
1.3 kristaps 1117: texiputchar(p, ' ');
1118: break;
1.1 kristaps 1119: case (TEXICMD_AT):
1.3 kristaps 1120: texiputchar(p, '@');
1121: break;
1122: case (TEXICMD_BANG):
1123: texiputchar(p, '!');
1.7 kristaps 1124: break;
1125: case (TEXICMD_BULLET):
1126: texiputchars(p, "\\(bu");
1.1 kristaps 1127: break;
1.35 kristaps 1128: case (TEXICMD_COMMA):
1129: texiputchar(p, ',');
1130: break;
1.1 kristaps 1131: case (TEXICMD_COPYRIGHT):
1132: texiputchars(p, "\\(co");
1133: break;
1.42 kristaps 1134: case (TEXICMD_DH):
1135: texiputchars(p, "\\(-D");
1136: break;
1137: case (TEXICMD_DHSMALL):
1138: texiputchars(p, "\\(Sd");
1139: break;
1.2 kristaps 1140: case (TEXICMD_DOTS):
1.34 kristaps 1141: case (TEXICMD_ENDDOTS):
1.2 kristaps 1142: texiputchars(p, "...");
1143: break;
1.28 kristaps 1144: case (TEXICMD_EQUIV):
1145: texiputchars(p, "\\(==");
1146: break;
1.15 kristaps 1147: case (TEXICMD_ERROR):
1148: texiputchars(p, "error\\(->");
1.17 kristaps 1149: break;
1.43 kristaps 1150: case (TEXICMD_EURO):
1151: texiputchars(p, "\\(Eu");
1152: break;
1.42 kristaps 1153: case (TEXICMD_EXCLAMDOWN):
1154: texiputchars(p, "\\(r!");
1155: break;
1.17 kristaps 1156: case (TEXICMD_EXPANSION):
1157: texiputchars(p, "\\(->");
1.15 kristaps 1158: break;
1.43 kristaps 1159: case (TEXICMD_GEQ):
1160: texiputchars(p, "\\(>=");
1161: break;
1162: case (TEXICMD_GUILLEMETLEFT):
1163: case (TEXICMD_GUILLEMOTLEFT):
1164: texiputchars(p, "\\(Fo");
1165: break;
1166: case (TEXICMD_GUILLEMETRIGHT):
1167: case (TEXICMD_GUILLEMOTRIGHT):
1168: texiputchars(p, "\\(Fc");
1169: break;
1170: case (TEXICMD_GUILSINGLLEFT):
1171: texiputchars(p, "\\(fo");
1172: break;
1173: case (TEXICMD_GUILSINGLRIGHT):
1174: texiputchars(p, "\\(fc");
1175: break;
1.42 kristaps 1176: case (TEXICMD_L):
1177: texiputchars(p, "\\(/L");
1178: break;
1.1 kristaps 1179: case (TEXICMD_LATEX):
1180: texiputchars(p, "LaTeX");
1181: break;
1.43 kristaps 1182: case (TEXICMD_LEQ):
1183: texiputchars(p, "\\(<=");
1184: break;
1.42 kristaps 1185: case (TEXICMD_LSMALL):
1186: texiputchars(p, "\\(/l");
1187: break;
1.25 kristaps 1188: case (TEXICMD_MINUS):
1189: texiputchars(p, "\\-");
1190: break;
1.42 kristaps 1191: case (TEXICMD_O):
1192: texiputchars(p, "\\(/O");
1193: break;
1194: case (TEXICMD_OE):
1195: texiputchars(p, "\\(OE");
1196: break;
1197: case (TEXICMD_OESMALL):
1198: texiputchars(p, "\\(oe");
1199: break;
1200: case (TEXICMD_ORDF):
1201: texiputchars(p, "a");
1202: break;
1203: case (TEXICMD_ORDM):
1204: texiputchars(p, "o");
1205: break;
1206: case (TEXICMD_OSMALL):
1207: texiputchars(p, "\\(/o");
1208: break;
1.25 kristaps 1209: case (TEXICMD_PERIOD):
1210: texiputchar(p, '.');
1211: break;
1.43 kristaps 1212: case (TEXICMD_POUNDS):
1213: texiputchars(p, "\\(Po");
1214: break;
1.42 kristaps 1215: case (TEXICMD_QUESTIONDOWN):
1216: texiputchars(p, "\\(r?");
1217: break;
1.3 kristaps 1218: case (TEXICMD_QUESTIONMARK):
1219: texiputchar(p, '?');
1.15 kristaps 1220: break;
1.43 kristaps 1221: case (TEXICMD_QUOTEDBLBASE):
1222: texiputchars(p, "\\(Bq");
1223: break;
1224: case (TEXICMD_QUOTEDBLLEFT):
1225: texiputchars(p, "\\(lq");
1226: break;
1227: case (TEXICMD_QUOTEDBLRIGHT):
1228: texiputchars(p, "\\(rq");
1229: break;
1230: case (TEXICMD_QUOTESINGLBASE):
1231: texiputchars(p, "\\(bq");
1232: break;
1233: case (TEXICMD_QUOTELEFT):
1234: texiputchars(p, "\\(oq");
1235: break;
1236: case (TEXICMD_QUOTERIGHT):
1237: texiputchars(p, "\\(cq");
1238: break;
1239: case (TEXICMD_REGISTEREDSYMBOL):
1240: texiputchars(p, "\\(rg");
1241: break;
1.15 kristaps 1242: case (TEXICMD_RESULT):
1243: texiputchars(p, "\\(rA");
1.3 kristaps 1244: break;
1.34 kristaps 1245: case (TEXICMD_SLASH):
1246: texiputchar(p, '/');
1247: break;
1.35 kristaps 1248: case (TEXICMD_SS):
1249: texiputchars(p, "\\(ss");
1250: break;
1.3 kristaps 1251: case (TEXICMD_SQUIGGLE_LEFT):
1252: texiputchars(p, "{");
1253: break;
1254: case (TEXICMD_SQUIGGLE_RIGHT):
1255: texiputchars(p, "}");
1256: break;
1.38 kristaps 1257: case (TEXICMD_TEXSYM):
1.35 kristaps 1258: texiputchars(p, "TeX");
1.43 kristaps 1259: break;
1260: case (TEXICMD_TEXTDEGREE):
1261: texiputchars(p, "\\(de");
1.35 kristaps 1262: break;
1.42 kristaps 1263: case (TEXICMD_TH):
1264: texiputchars(p, "\\(TP");
1265: break;
1266: case (TEXICMD_THSMALL):
1267: texiputchars(p, "\\(Tp");
1268: break;
1.38 kristaps 1269: case (TEXICMD_TIE):
1.35 kristaps 1270: texiputchars(p, "\\ ");
1.1 kristaps 1271: break;
1.3 kristaps 1272: case (TEXICMD_COLON):
1273: case (TEXICMD_HYPHEN):
1274: break;
1.1 kristaps 1275: default:
1276: abort();
1277: }
1278:
1.42 kristaps 1279: /* Alphabetic commands have braces we ignore. */
1280: if (isalpha(texitoks[cmd].tok[0]))
1.5 kristaps 1281: doignbracket(p, cmd, buf, sz, pos);
1.1 kristaps 1282: }
1283:
1284: static void
1285: doquotation(struct texi *p, enum texicmd cmd,
1286: const char *buf, size_t sz, size_t *pos)
1287: {
1288:
1.5 kristaps 1289: teximacro(p, "Qo");
1.1 kristaps 1290: parseto(p, buf, sz, pos, "quotation");
1.5 kristaps 1291: teximacro(p, "Qc");
1.1 kristaps 1292: }
1293:
1.3 kristaps 1294: static void
1295: domath(struct texi *p, enum texicmd cmd,
1296: const char *buf, size_t sz, size_t *pos)
1297: {
1.34 kristaps 1298: size_t nest, start;
1.3 kristaps 1299:
1300: /*
1301: * Math handling is different from everything else.
1302: * We don't allow any subcomponents, and we ignore the rules in
1303: * terms of @-commands.
1304: * This departs from GNU's rules, but whatever.
1305: */
1306: while (*pos < sz && isws(buf[*pos]))
1307: advance(p, buf, pos);
1308: if (*pos == sz || '{' != buf[*pos])
1309: return;
1310: advance(p, buf, pos);
1311: if (p->seenws && p->outcol && 0 == p->literal)
1312: texiputchar(p, ' ');
1313: p->seenws = 0;
1.34 kristaps 1314: for (nest = 1, start = *pos; *pos < sz && nest > 0; ) {
1.3 kristaps 1315: if ('{' == buf[*pos])
1316: nest++;
1317: else if ('}' == buf[*pos])
1318: if (0 == --nest)
1319: continue;
1320: advance(p, buf, pos);
1321: }
1322: if (*pos == sz)
1323: return;
1324: assert('}' == buf[*pos]);
1.34 kristaps 1325: texiputbuf(p, buf, start, *pos);
1.3 kristaps 1326: advance(p, buf, pos);
1.24 kristaps 1327: }
1328:
1329: static void
1330: dovalue(struct texi *p, enum texicmd cmd,
1331: const char *buf, size_t sz, size_t *pos)
1332: {
1.28 kristaps 1333: size_t start, end, i;
1.25 kristaps 1334: char *key, *val;
1335: const char *cp;
1.24 kristaps 1336:
1337: if (TEXICMD_SET == cmd) {
1338: while (*pos < sz && isws(buf[*pos]))
1339: advance(p, buf, pos);
1340: for (start = end = *pos; end < sz; end++)
1341: if (ismspace(buf[end]))
1342: break;
1.25 kristaps 1343: /* We don't allow empty keys. */
1.24 kristaps 1344: if (start == end)
1345: return;
1.25 kristaps 1346: advanceto(p, buf, pos, end);
1.24 kristaps 1347:
1348: key = malloc(end - start + 1);
1.27 kristaps 1349: if (NULL == key)
1350: texiabort(p, NULL);
1.24 kristaps 1351: memcpy(key, &buf[start], end - start);
1352: key[end - start] = '\0';
1353:
1354: while (*pos < sz && isws(buf[*pos]))
1355: advance(p, buf, pos);
1356: for (start = end = *pos; end < sz; end++)
1357: if ('\n' == buf[end])
1358: break;
1.25 kristaps 1359: /* We do allow empty values. */
1360: advanceeoln(p, buf, sz, pos, 1);
1.24 kristaps 1361:
1362: val = malloc(end - start + 1);
1.27 kristaps 1363: if (NULL == val)
1364: texiabort(p, NULL);
1.24 kristaps 1365: memcpy(val, &buf[start], end - start);
1366: val[end - start] = '\0';
1.25 kristaps 1367: valueadd(p, key, val);
1.24 kristaps 1368: } else if (TEXICMD_VALUE == cmd) {
1369: if (p->seenws)
1370: texiputchar(p, ' ');
1371: p->seenws = 0;
1.28 kristaps 1372: if (NULL != (cp = valueblookup(p, buf, sz, pos))) {
1373: for (i = 0; i < p->valstackpos; i++)
1374: if (cp == p->valstack[i])
1375: break;
1376: if (i < p->valstackpos)
1377: texierr(p, "recursive value");
1.29 kristaps 1378: if (64 == p->valstackpos)
1379: texierr(p, "too many nested values");
1.28 kristaps 1380: p->valstack[p->valstackpos++] = cp;
1.31 kristaps 1381: parsemembuf(p, cp, strlen(cp));
1.28 kristaps 1382: p->valstackpos--;
1383: } else
1.25 kristaps 1384: texiputchars(p, "{No value}");
1.24 kristaps 1385: } else if (TEXICMD_IFCLEAR == cmd) {
1.26 kristaps 1386: if (NULL != valuellookup(p, buf, sz, pos))
1387: doignblock(p, cmd, buf, sz, pos);
1388: else
1389: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.30 kristaps 1390: } else if (TEXICMD_IFSET == cmd) {
1391: if (NULL == valuellookup(p, buf, sz, pos))
1392: doignblock(p, cmd, buf, sz, pos);
1393: else
1394: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.25 kristaps 1395: } else if (TEXICMD_CLEAR == cmd)
1396: valuelclear(p, buf, sz, pos);
1.3 kristaps 1397: }
1398:
1.1 kristaps 1399: static void
1.8 kristaps 1400: dolink(struct texi *p, enum texicmd cmd,
1.1 kristaps 1401: const char *buf, size_t sz, size_t *pos)
1402: {
1.8 kristaps 1403: int c;
1.1 kristaps 1404:
1405: switch (cmd) {
1406: case (TEXICMD_EMAIL):
1.5 kristaps 1407: teximacroopen(p, "Mt");
1.1 kristaps 1408: break;
1.3 kristaps 1409: case (TEXICMD_UREF):
1.1 kristaps 1410: case (TEXICMD_URL):
1.44 ! kristaps 1411: case (TEXICMD_INDICATEURL):
1.5 kristaps 1412: teximacroopen(p, "Lk");
1.1 kristaps 1413: break;
1.8 kristaps 1414: case (TEXICMD_XREF):
1415: texiputchars(p, "See Section");
1.39 kristaps 1416: teximacroopen(p, "Dq");
1.8 kristaps 1417: break;
1418: case (TEXICMD_PXREF):
1419: texiputchars(p, "see Section");
1.39 kristaps 1420: teximacroopen(p, "Dq");
1.8 kristaps 1421: break;
1.34 kristaps 1422: case (TEXICMD_INFOREF):
1423: texiputchars(p, "See Info file node");
1.39 kristaps 1424: teximacroopen(p, "Dq");
1.34 kristaps 1425: break;
1.1 kristaps 1426: default:
1.8 kristaps 1427: abort();
1.1 kristaps 1428: }
1.8 kristaps 1429:
1430: c = parsearg(p, buf, sz, pos, 0);
1431: p->ign++;
1432: while (c > 0)
1433: c = parsearg(p, buf, sz, pos, 1);
1434: p->ign--;
1435:
1.11 kristaps 1436: texipunctuate(p, buf, sz, pos);
1.8 kristaps 1437: teximacroclose(p);
1438: }
1439:
1440: static void
1441: doignargn(struct texi *p, enum texicmd cmd,
1442: const char *buf, size_t sz, size_t *pos)
1443: {
1444: int c;
1445:
1446: c = parsearg(p, buf, sz, pos, 0);
1447: p->ign++;
1448: while (c > 0)
1449: c = parsearg(p, buf, sz, pos, 1);
1450: p->ign--;
1.1 kristaps 1451: }
1452:
1.23 kristaps 1453: /*
1454: * Sections can be made subsections and so on by way of the
1455: * @raiseections and @lowersections commands.
1456: * Perform this check here and return the actual section number adjusted
1457: * to the raise level.
1458: */
1459: static int
1460: sectioner(struct texi *p, int sec)
1461: {
1462:
1463: if ((sec -= p->secoffs) < 0) {
1464: texiwarn(p, "section below minimum, clamping");
1465: return(0);
1466: } else if (sec >= SECTSZ) {
1467: texiwarn(p, "section above maximum, clamping");
1468: return(SECTSZ - 1);
1469: }
1470: return(sec);
1471: }
1472:
1473: static void
1474: dosubsubsection(struct texi *p, enum texicmd cmd,
1475: const char *buf, size_t sz, size_t *pos)
1476: {
1477: int sec;
1478:
1479: sec = sectioner(p, 3);
1480:
1481: /* We don't have a subsubsubsection, so make one up. */
1482: texivspace(p);
1483: teximacroopen(p, sects[sec]);
1484: parseeoln(p, buf, sz, pos);
1485: teximacroclose(p);
1486: texivspace(p);
1487: }
1488:
1.1 kristaps 1489: static void
1490: dosubsection(struct texi *p, enum texicmd cmd,
1491: const char *buf, size_t sz, size_t *pos)
1492: {
1.23 kristaps 1493: int sec;
1494:
1495: sec = sectioner(p, 2);
1.13 kristaps 1496:
1497: if (p->outmacro)
1.23 kristaps 1498: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13 kristaps 1499: else if (p->literal)
1.23 kristaps 1500: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1 kristaps 1501:
1.21 kristaps 1502: /* We don't have a subsubsection, so make one up. */
1.34 kristaps 1503: if (sec > 1)
1504: texivspace(p);
1.23 kristaps 1505: teximacroopen(p, sects[sec]);
1.3 kristaps 1506: parseeoln(p, buf, sz, pos);
1.5 kristaps 1507: teximacroclose(p);
1.34 kristaps 1508: if (sec > 1)
1509: texivspace(p);
1.1 kristaps 1510: }
1511:
1512: static void
1.23 kristaps 1513: dosecoffs(struct texi *p, enum texicmd cmd,
1514: const char *buf, size_t sz, size_t *pos)
1515: {
1516:
1517: if (TEXICMD_RAISESECTIONS == cmd)
1518: p->secoffs++;
1519: else
1520: p->secoffs--;
1521: }
1522:
1523: static void
1.1 kristaps 1524: dosection(struct texi *p, enum texicmd cmd,
1.23 kristaps 1525: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 1526: {
1.23 kristaps 1527: int sec;
1.12 kristaps 1528:
1529: switch (cmd) {
1530: case (TEXICMD_APPENDIX):
1531: case (TEXICMD_CHAPTER):
1532: case (TEXICMD_TOP):
1533: case (TEXICMD_UNNUMBERED):
1.23 kristaps 1534: sec = sectioner(p, 0);
1.12 kristaps 1535: break;
1536: case (TEXICMD_APPENDIXSEC):
1537: case (TEXICMD_HEADING):
1538: case (TEXICMD_SECTION):
1539: case (TEXICMD_UNNUMBEREDSEC):
1.23 kristaps 1540: sec = sectioner(p, 1);
1.12 kristaps 1541: break;
1542: default:
1543: abort();
1544: }
1.1 kristaps 1545:
1.3 kristaps 1546: if (p->outmacro)
1.23 kristaps 1547: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3 kristaps 1548: else if (p->literal)
1.23 kristaps 1549: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3 kristaps 1550:
1.23 kristaps 1551: teximacroopen(p, sects[sec]);
1.3 kristaps 1552: parseeoln(p, buf, sz, pos);
1553: teximacroclose(p);
1.11 kristaps 1554: p->seenvs = 1;
1.3 kristaps 1555: }
1556:
1557: static void
1558: dosp(struct texi *p, enum texicmd cmd,
1559: const char *buf, size_t sz, size_t *pos)
1560: {
1561:
1.37 kristaps 1562: if (p->literal)
1563: texiputchar(p, '\n');
1564: else
1565: texivspace(p);
1.12 kristaps 1566: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1567: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1568: }
1569:
1570: static void
1571: dotop(struct texi *p, enum texicmd cmd,
1572: const char *buf, size_t sz, size_t *pos)
1573: {
1.10 kristaps 1574: const char *cp;
1575: time_t t;
1576: char date[32];
1577:
1.26 kristaps 1578: if (--p->ign)
1579: texierr(p, "@top command while ignoring (%d)", p->ign);
1580:
1.18 kristaps 1581: /*
1582: * Here we print our standard mdoc(7) prologue.
1583: * We use the title set with @settitle for the `Nd' description
1584: * and the source document filename (the first one as invoked on
1585: * the command line) for the title.
1586: * The date is set to the current date.
1587: */
1.10 kristaps 1588: t = time(NULL);
1589: strftime(date, sizeof(date), "%F", localtime(&t));
1.1 kristaps 1590:
1.10 kristaps 1591: teximacroopen(p, "Dd");
1592: texiputchars(p, date);
1593: teximacroclose(p);
1594: teximacroopen(p, "Dt");
1595: for (cp = p->title; '\0' != *cp; cp++)
1596: texiputchar(p, toupper(*cp));
1.11 kristaps 1597: texiputchars(p, " 7");
1.10 kristaps 1598: teximacroclose(p);
1.5 kristaps 1599: teximacro(p, "Os");
1600: teximacro(p, "Sh NAME");
1.10 kristaps 1601: teximacroopen(p, "Nm");
1.41 kristaps 1602: for (cp = p->title; '\0' != *cp; cp++)
1603: texiputchar(p, *cp);
1.10 kristaps 1604: teximacroclose(p);
1605: teximacroopen(p, "Nd");
1.41 kristaps 1606: if (NULL != p->subtitle)
1607: for (cp = p->subtitle; '\0' != *cp; cp++)
1608: texiputchar(p, *cp);
1609: else
1610: texiputchars(p, "Unknown description");
1.10 kristaps 1611: teximacroclose(p);
1.11 kristaps 1612: p->seenvs = 1;
1.12 kristaps 1613: dosection(p, cmd, buf, sz, pos);
1.1 kristaps 1614: }
1615:
1616: static void
1617: doitem(struct texi *p, enum texicmd cmd,
1618: const char *buf, size_t sz, size_t *pos)
1619: {
1620:
1.18 kristaps 1621: /* Multitable is using raw tbl(7). */
1622: if (TEXILIST_TABLE == p->list) {
1623: texiputchar(p, '\n');
1624: return;
1625: }
1626:
1.3 kristaps 1627: if (p->outmacro)
1628: texierr(p, "item in open line scope!?");
1629: else if (p->literal)
1630: texierr(p, "item in a literal scope!?");
1631:
1632: switch (p->list) {
1633: case (TEXILIST_ITEM):
1.5 kristaps 1634: teximacroopen(p, "It");
1.3 kristaps 1635: break;
1636: case (TEXILIST_NOITEM):
1.5 kristaps 1637: teximacro(p, "It");
1.3 kristaps 1638: break;
1639: default:
1.11 kristaps 1640: texivspace(p);
1.3 kristaps 1641: break;
1642: }
1.18 kristaps 1643:
1644: /* Trick so we don't start with Pp. */
1.11 kristaps 1645: p->seenvs = 1;
1.3 kristaps 1646: parseeoln(p, buf, sz, pos);
1.1 kristaps 1647:
1.3 kristaps 1648: if (TEXILIST_ITEM == p->list)
1649: teximacroclose(p);
1.9 kristaps 1650: else if (p->outcol > 0)
1.1 kristaps 1651: texiputchar(p, '\n');
1.18 kristaps 1652: }
1653:
1654: static void
1655: dotab(struct texi *p, enum texicmd cmd,
1656: const char *buf, size_t sz, size_t *pos)
1657: {
1658:
1659: /* This command is only useful in @multitable. */
1660: if (TEXILIST_TABLE == p->list)
1661: texiputchar(p, '\t');
1662: }
1663:
1664: static void
1665: domultitable(struct texi *p, enum texicmd cmd,
1666: const char *buf, size_t sz, size_t *pos)
1667: {
1668: enum texilist sv = p->list;
1.32 kristaps 1669: int svliteral = p->literal;
1.18 kristaps 1670: enum texicmd type;
1671: size_t i, end, columns;
1672:
1673: p->list = TEXILIST_TABLE;
1.32 kristaps 1674: /*
1675: * TS/TE blocks aren't "in mdoc(7)", so we can disregard the
1676: * fact that we're in literal mode right now.
1677: */
1678: p->literal = 0;
1.18 kristaps 1679: teximacro(p, "TS");
1680: columns = 0;
1681:
1682: /* Advance to the first argument... */
1683: while (*pos < sz && isws(buf[*pos]))
1684: advance(p, buf, pos);
1685:
1686: /* Make sure we don't print anything when scanning. */
1687: p->ign++;
1688: if ('@' == buf[*pos]) {
1689: /*
1690: * Look for @columnfractions.
1691: * We ignore these, but we do use the number of
1692: * arguments to set the number of columns that we'll
1693: * have.
1694: */
1.30 kristaps 1695: type = texicmd(p, buf, *pos, sz, &end, NULL);
1.18 kristaps 1696: advanceto(p, buf, pos, end);
1697: if (TEXICMD_COLUMNFRACTIONS != type)
1.25 kristaps 1698: texierr(p, "unknown multitable command");
1.18 kristaps 1699: while (*pos < sz && '\n' != buf[*pos]) {
1700: while (*pos < sz && isws(buf[*pos]))
1701: advance(p, buf, pos);
1702: while (*pos < sz && ! isws(buf[*pos])) {
1703: if ('\n' == buf[*pos])
1704: break;
1705: advance(p, buf, pos);
1706: }
1707: columns++;
1708: }
1709: } else
1710: /*
1711: * We have arguments.
1712: * We could parse these, but it's easier to just let
1713: * tbl(7) figure it out.
1714: * So use this only to count arguments.
1715: */
1716: while (parselinearg(p, buf, sz, pos) > 0)
1717: columns++;
1718: p->ign--;
1719:
1720: /* Left-justify each table entry. */
1721: for (i = 0; i < columns; i++) {
1722: if (i > 0)
1723: texiputchar(p, ' ');
1724: texiputchar(p, 'l');
1725: }
1726: texiputchars(p, ".\n");
1727: p->outmacro++;
1728: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1729: p->outmacro--;
1730: teximacro(p, "TE");
1.32 kristaps 1731: p->literal = svliteral;
1.18 kristaps 1732: p->list = sv;
1.1 kristaps 1733: }
1734:
1735: static void
1736: dotable(struct texi *p, enum texicmd cmd,
1737: const char *buf, size_t sz, size_t *pos)
1738: {
1.3 kristaps 1739: enum texilist sv = p->list;
1740:
1741: p->list = TEXILIST_ITEM;
1.5 kristaps 1742: teximacro(p, "Bl -tag -width Ds");
1.12 kristaps 1743: /* FIXME: ignore and parseeoln. */
1744: advanceeoln(p, buf, sz, pos, 1);
1.11 kristaps 1745: p->seenvs = 1;
1.20 kristaps 1746: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 1747: teximacro(p, "El");
1.3 kristaps 1748: p->list = sv;
1.44 ! kristaps 1749: }
! 1750:
! 1751: static void
! 1752: doend(struct texi *p, enum texicmd cmd,
! 1753: const char *buf, size_t sz, size_t *pos)
! 1754: {
! 1755: size_t start;
! 1756:
! 1757: while (*pos < sz && isws(buf[*pos]))
! 1758: advance(p, buf, pos);
! 1759: start = *pos;
! 1760: while (*pos < sz && '\n' != buf[*pos])
! 1761: advance(p, buf, pos);
! 1762:
! 1763: texiwarn(p, "unexpected \"end\": %.*s", (int)(*pos - start), &buf[start]);
! 1764: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1765: }
1766:
1767: static void
1.2 kristaps 1768: doenumerate(struct texi *p, enum texicmd cmd,
1769: const char *buf, size_t sz, size_t *pos)
1770: {
1.3 kristaps 1771: enum texilist sv = p->list;
1.2 kristaps 1772:
1.3 kristaps 1773: p->list = TEXILIST_NOITEM;
1.5 kristaps 1774: teximacro(p, "Bl -enum");
1.11 kristaps 1775: p->seenvs = 1;
1.12 kristaps 1776: /* FIXME: ignore and parseeoln. */
1777: advanceeoln(p, buf, sz, pos, 1);
1.2 kristaps 1778: parseto(p, buf, sz, pos, "enumerate");
1.5 kristaps 1779: teximacro(p, "El");
1.3 kristaps 1780: p->list = sv;
1.2 kristaps 1781: }
1782:
1783: static void
1.1 kristaps 1784: doitemize(struct texi *p, enum texicmd cmd,
1785: const char *buf, size_t sz, size_t *pos)
1786: {
1.3 kristaps 1787: enum texilist sv = p->list;
1.1 kristaps 1788:
1.21 kristaps 1789: p->list = TEXILIST_NOITEM;
1.5 kristaps 1790: teximacro(p, "Bl -bullet");
1.11 kristaps 1791: p->seenvs = 1;
1.12 kristaps 1792: /* FIXME: ignore and parseeoln. */
1793: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1794: parseto(p, buf, sz, pos, "itemize");
1.5 kristaps 1795: teximacro(p, "El");
1.3 kristaps 1796: p->list = sv;
1.1 kristaps 1797: }
1798:
1799: static void
1800: doignbracket(struct texi *p, enum texicmd cmd,
1801: const char *buf, size_t sz, size_t *pos)
1802: {
1803:
1.3 kristaps 1804: p->ign++;
1.1 kristaps 1805: parsebracket(p, buf, sz, pos);
1.3 kristaps 1806: p->ign--;
1.1 kristaps 1807: }
1808:
1809: static void
1810: doignline(struct texi *p, enum texicmd cmd,
1811: const char *buf, size_t sz, size_t *pos)
1812: {
1813:
1.12 kristaps 1814: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1815: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1816: }
1817:
1.8 kristaps 1818: /*
1819: * Parse colon-separated directories from "cp" (if not NULL) and returns
1820: * the array of pointers.
1.40 kristaps 1821: * Prepends "base" to the array, if found.
1.8 kristaps 1822: * This does NOT sanitise the directories!
1823: */
1.5 kristaps 1824: static char **
1.27 kristaps 1825: parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz)
1.5 kristaps 1826: {
1827: char *tok, *str, *tofree;
1828: const char *cpp;
1.40 kristaps 1829: size_t i = 0;
1.5 kristaps 1830: char **dirs;
1831:
1.40 kristaps 1832: /* Count up our expected arguments. */
1833: *sz = NULL != base;
1834: if (NULL != (cpp = cp))
1835: for ((*sz)++; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1.5 kristaps 1836: cpp++;
1837:
1.40 kristaps 1838: if (0 == *sz)
1839: return(NULL);
1.27 kristaps 1840: if (NULL == (dirs = calloc(*sz, sizeof(char *))))
1841: texiabort(p, NULL);
1.40 kristaps 1842: if (NULL != base && NULL == (dirs[i++] = strdup(base)))
1.27 kristaps 1843: texiabort(p, NULL);
1.5 kristaps 1844: if (NULL == cp)
1845: return(dirs);
1.27 kristaps 1846: if (NULL == (tofree = tok = str = strdup(cp)))
1847: texiabort(p, NULL);
1.5 kristaps 1848:
1.40 kristaps 1849: for ( ; NULL != (tok = strsep(&str, ":")); i++)
1.27 kristaps 1850: if (NULL == (dirs[i] = strdup(tok)))
1851: texiabort(p, NULL);
1.5 kristaps 1852:
1853: free(tofree);
1854: return(dirs);
1855: }
1856:
1.1 kristaps 1857: int
1858: main(int argc, char *argv[])
1859: {
1860: struct texi texi;
1.2 kristaps 1861: int c;
1.37 kristaps 1862: char *dirpath, *dir, *ccp;
1.10 kristaps 1863: const char *progname, *Idir, *cp;
1.1 kristaps 1864:
1865: progname = strrchr(argv[0], '/');
1866: if (progname == NULL)
1867: progname = argv[0];
1868: else
1869: ++progname;
1870:
1.10 kristaps 1871: memset(&texi, 0, sizeof(struct texi));
1.40 kristaps 1872: texi.ign = 1;
1.5 kristaps 1873: Idir = NULL;
1.10 kristaps 1874:
1.5 kristaps 1875: while (-1 != (c = getopt(argc, argv, "I:")))
1.1 kristaps 1876: switch (c) {
1.5 kristaps 1877: case ('I'):
1878: Idir = optarg;
1879: break;
1.1 kristaps 1880: default:
1881: goto usage;
1882: }
1883:
1884: argv += optind;
1.40 kristaps 1885: argc -= optind;
1.1 kristaps 1886:
1.40 kristaps 1887: if (argc > 0) {
1888: if (NULL == (dirpath = strdup(argv[0])))
1889: texiabort(&texi, NULL);
1890: if (NULL == (dir = dirname(dirpath)))
1891: texiabort(&texi, NULL);
1892: if (NULL != (cp = strrchr(argv[0], '/')))
1893: texi.title = strdup(cp + 1);
1894: else
1895: texi.title = strdup(argv[0]);
1896: if (NULL == texi.title)
1897: texiabort(&texi, NULL);
1898: else if (NULL != (ccp = strchr(texi.title, '.')))
1899: *ccp = '\0';
1900: texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
1901: free(dirpath);
1902: parsefile(&texi, argv[0], 1);
1903: } else {
1904: texi.title = strdup("Unknown Manual");
1905: texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
1906: parsestdin(&texi);
1907: }
1.27 kristaps 1908:
1.2 kristaps 1909: texiexit(&texi);
1910: return(EXIT_FAILURE);
1.1 kristaps 1911: usage:
1.40 kristaps 1912: fprintf(stderr, "usage: %s [-Idirs] [file]\n", progname);
1.1 kristaps 1913: return(EXIT_FAILURE);
1914: }
CVSweb