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