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