Annotation of texi2mdoc/main.c, Revision 1.25
1.25 ! kristaps 1: /* $Id: main.c,v 1.24 2015/02/20 09:58:50 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2015 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/mman.h>
18: #include <sys/stat.h>
19:
20: #include <assert.h>
21: #include <ctype.h>
22: #include <fcntl.h>
23: #include <getopt.h>
1.2 kristaps 24: #include <libgen.h>
25: #include <limits.h>
1.1 kristaps 26: #include <stdarg.h>
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
1.10 kristaps 30: #include <time.h>
1.6 kristaps 31: #include <unistd.h>
1.1 kristaps 32:
1.24 kristaps 33: #include "extern.h"
1.1 kristaps 34:
1.23 kristaps 35: #define SECTSZ 4
36: static const char *const sects[SECTSZ] = {
37: "Sh",
38: "Ss",
39: "Em",
40: "No",
41: };
42:
1.16 kristaps 43: static void doaccent(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 44: static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 45: static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
46: static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.15 kristaps 47: static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 48: static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 49: static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 50: static void doexample(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 51: static void doignargn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 52: static void doignblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
53: static void doignbracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
54: static void doignline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12 kristaps 55: static void doinline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 56: static void doinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 57: static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *);
58: static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 59: static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 60: static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18 kristaps 61: static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 62: static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *);
63: static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *);
64: static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23 kristaps 65: static void dosecoffs(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 66: static void dosection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 67: static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 68: static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23 kristaps 69: static void dosubsubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 70: static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18 kristaps 71: static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.10 kristaps 72: static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.24 kristaps 73: static void dovalue(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.25 ! kristaps 74: static void doverb(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.16 kristaps 75: static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 76:
1.24 kristaps 77: static const struct texitok __texitoks[TEXICMD__MAX] = {
1.20 kristaps 78: /* TEXICMD__BEGIN */
1.8 kristaps 79: { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */
1.16 kristaps 80: { doaccent, "'", 1 }, /* TEXICMD_ACUTE */
1.1 kristaps 81: { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */
82: { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */
1.12 kristaps 83: { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */
84: { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */
1.20 kristaps 85: { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */
1.23 kristaps 86: { dosubsubsection, "appendixsubsubsec", 17 }, /* TEXICMD_APPENDIXSUBSUBSEC */
1.25 ! kristaps 87: { doinline, "asis", 4 }, /* TEXICMD_ASIS */
1.3 kristaps 88: { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */
1.1 kristaps 89: { dosymbol, "@", 1 }, /* TEXICMD_AT */
1.3 kristaps 90: { doignline, "author", 6 }, /* TEXICMD_AUTHOR */
1.21 kristaps 91: { doinline, "b", 1 }, /* TEXICMD_BOLD */
1.3 kristaps 92: { dosymbol, "!", 1 }, /* TEXICMD_BANG */
1.7 kristaps 93: { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */
1.1 kristaps 94: { dobye, "bye", 3 }, /* TEXICMD_BYE */
1.12 kristaps 95: { doignline, "center", 6 }, /* TEXICMD_CENTER */
96: { dosection, "chapter", 7 }, /* TEXICMD_CHAPTER */
1.1 kristaps 97: { doignline, "cindex", 6 }, /* TEXICMD_CINDEX */
1.16 kristaps 98: { doaccent, "^", 1 }, /* TEXICMD_CIRCUMFLEX */
1.24 kristaps 99: { doinline, "cite", 4 }, /* TEXICMD_CITE */
100: { dovalue, "clear", 5 }, /* TEXICMD_CLEAR */
1.21 kristaps 101: { doinline, "code", 4 }, /* TEXICMD_CODE */
1.3 kristaps 102: { dosymbol, ":", 1 }, /* TEXICMD_COLON */
1.18 kristaps 103: { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */
1.12 kristaps 104: { doinline, "command", 7 }, /* TEXICMD_COMMAND */
1.1 kristaps 105: { doignline, "c", 1 }, /* TEXICMD_COMMENT */
1.2 kristaps 106: { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */
1.1 kristaps 107: { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */
108: { doignblock, "copying", 7 }, /* TEXICMD_COPYING */
109: { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */
1.15 kristaps 110: { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */
111: { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */
112: { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */
113: { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */
114: { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */
115: { dodefn, "deftpx", 6 }, /* TEXICMD_DEFTPX */
116: { dodefn, "deftypefn", 9 }, /* TEXICMD_DEFTYPEFN */
117: { dodefn, "deftypefnx", 10 }, /* TEXICMD_DEFTYPEFNX */
118: { dodefn, "deftypefun", 10 }, /* TEXICMD_DEFTYPEFUN */
119: { dodefn, "deftypefunx", 11 }, /* TEXICMD_DEFTYPEFUNX */
120: { dodefn, "deftypevar", 10 }, /* TEXICMD_DEFTYPEVAR */
121: { dodefn, "deftypevarx", 11 }, /* TEXICMD_DEFTYPEVARX */
122: { dodefn, "deftypevr", 9 }, /* TEXICMD_DEFTYPEVR */
123: { dodefn, "deftypevrx", 10 }, /* TEXICMD_DEFTYPEVRX */
124: { dodefn, "defun", 5 }, /* TEXICMD_DEFUN */
125: { dodefn, "defunx", 6 }, /* TEXICMD_DEFUNX */
126: { dodefn, "defvar", 6 }, /* TEXICMD_DEFVAR */
127: { dodefn, "defvarx", 7 }, /* TEXICMD_DEFVARX */
128: { dodefn, "defvr", 5 }, /* TEXICMD_DEFVR */
129: { dodefn, "defvrx", 6 }, /* TEXICMD_DEFVRX */
1.1 kristaps 130: { doignblock, "detailmenu", 10 }, /* TEXICMD_DETAILMENU */
1.21 kristaps 131: { doinline, "dfn", 3 }, /* TEXICMD_DFN */
1.1 kristaps 132: { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */
133: { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */
1.3 kristaps 134: { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */
1.2 kristaps 135: { dosymbol, "dots", 4 }, /* TEXICMD_DOTS */
1.8 kristaps 136: { dolink, "email", 5 }, /* TEXICMD_EMAIL */
1.21 kristaps 137: { doinline, "emph", 4 }, /* TEXICMD_EMPH */
1.1 kristaps 138: { NULL, "end", 3 }, /* TEXICMD_END */
1.2 kristaps 139: { doenumerate, "enumerate", 9 }, /* TEXICMD_ENUMERATE */
1.12 kristaps 140: { doinline, "env", 3 }, /* TEXICMD_ENV */
1.15 kristaps 141: { dosymbol, "error", 5 }, /* TEXICMD_ERROR */
1.1 kristaps 142: { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */
1.25 ! kristaps 143: { doignline, "exdent", 6 }, /* TEXICMD_EXDENT */
1.17 kristaps 144: { dosymbol, "expansion", 9 }, /* TEXICMD_EXPANSION */
1.12 kristaps 145: { doinline, "file", 4 }, /* TEXICMD_FILE */
1.17 kristaps 146: { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */
1.20 kristaps 147: { doignline, "findex", 6 }, /* TEXICMD_FINDEX */
148: { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */
149: { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */
1.16 kristaps 150: { doaccent, "`", 1 }, /* TEXICMD_GRAVE */
1.3 kristaps 151: { doblock, "group", 5 }, /* TEXICMD_GROUP */
1.2 kristaps 152: { dosection, "heading", 7 }, /* TEXICMD_HEADING */
1.3 kristaps 153: { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */
1.18 kristaps 154: { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */
1.25 ! kristaps 155: { doignblock, "html", 4 }, /* TEXICMD_HTML */
1.3 kristaps 156: { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */
1.21 kristaps 157: { doinline, "i", 1 }, /* TEXICMD_I */
1.24 kristaps 158: { dovalue, "ifclear", 7 }, /* TEXICMD_IFCLEAR */
1.14 kristaps 159: { doignblock, "ifdocbook", 9 }, /* TEXICMD_IFDOCBOOK */
1.1 kristaps 160: { doignblock, "ifhtml", 6 }, /* TEXICMD_IFHTML */
1.3 kristaps 161: { doignblock, "ifinfo", 6 }, /* TEXICMD_IFINFO */
1.14 kristaps 162: { doblock, "ifnotdocbook", 12 }, /* TEXICMD_IFNOTDOCBOOK */
163: { doblock, "ifnothtml", 9 }, /* TEXICMD_IFNOTHTML */
164: { doblock, "ifnotinfo", 9 }, /* TEXICMD_IFNOTINFO */
165: { doignblock, "ifnotplaintext", 14 }, /* TEXICMD_IFNOTPLAINTEXT */
1.3 kristaps 166: { doblock, "ifnottex", 8 }, /* TEXICMD_IFNOTTEX */
1.14 kristaps 167: { doblock, "ifnotxml", 8 }, /* TEXICMD_IFNOTXML */
168: { doblock, "ifplaintext", 11 }, /* TEXICMD_IFPLAINTEXT */
1.1 kristaps 169: { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */
1.3 kristaps 170: { doignblock, "ifset", 5 }, /* TEXICMD_IFSET */
1.14 kristaps 171: { doignblock, "ifxml", 5 }, /* TEXICMD_IFXML */
1.17 kristaps 172: { doignblock, "ignore", 6 }, /* TEXICMD_IGNORE */
1.1 kristaps 173: { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */
1.2 kristaps 174: { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */
1.13 kristaps 175: { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */
1.5 kristaps 176: { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */
1.1 kristaps 177: { doitem, "item", 4 }, /* TEXICMD_ITEM */
178: { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */
1.20 kristaps 179: { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */
1.21 kristaps 180: { doinline, "kbd", 3 }, /* TEXICMD_KBD */
1.18 kristaps 181: { dobracket, "key", 3 }, /* TEXICMD_KEY */
1.20 kristaps 182: { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */
1.1 kristaps 183: { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */
1.23 kristaps 184: { dosecoffs, "lowersections", 13 }, /* TEXICMD_LOWERSECTIONS */
1.3 kristaps 185: { domath, "math", 4 }, /* TEXICMD_MATH */
1.1 kristaps 186: { doignblock, "menu", 4 }, /* TEXICMD_MENU */
1.25 ! kristaps 187: { dosymbol, "minus", 5 }, /* TEXICMD_MINUS */
1.18 kristaps 188: { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */
1.15 kristaps 189: { doignline, "need", 4 }, /* TEXICMD_NEED */
1.3 kristaps 190: { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */
1.1 kristaps 191: { doignline, "node", 4 }, /* TEXICMD_NODE */
1.3 kristaps 192: { doignline, "noindent", 8 }, /* TEXICMD_NOINDENT */
1.16 kristaps 193: { doinline, "option", 6 }, /* TEXICMD_OPTION */
1.8 kristaps 194: { dolink, "pxref", 5 }, /* TEXICMD_PXREF */
1.3 kristaps 195: { dosymbol, "?", 1 }, /* TEXICMD_QUESTIONMARK */
1.1 kristaps 196: { doquotation, "quotation", 9 }, /* TEXICMD_QUOTATION */
1.3 kristaps 197: { doignline, "page", 4 }, /* TEXICMD_PAGE */
1.25 ! kristaps 198: { doignline, "paragraphindent", 15 }, /* TEXICMD_PARINDENT */
! 199: { dosymbol, ".", 1 }, /* TEXICMD_PERIOD */
1.2 kristaps 200: { doignline, "printindex", 10 }, /* TEXICMD_PRINTINDEX */
1.21 kristaps 201: { doinline, "r", 1 }, /* TEXICMD_R */
1.23 kristaps 202: { dosecoffs, "raisesections", 13 }, /* TEXICMD_RAISESECTIONS */
1.1 kristaps 203: { dobracket, "ref", 3 }, /* TEXICMD_REF */
1.15 kristaps 204: { dosymbol, "result", 6 }, /* TEXICMD_RESULT */
1.21 kristaps 205: { doinline, "samp", 4 }, /* TEXICMD_SAMP */
206: { doinline, "sansserif", 9 }, /* TEXICMD_SANSSERIF */
1.7 kristaps 207: { dobracket, "sc", 2 }, /* TEXICMD_SC */
1.1 kristaps 208: { dosection, "section", 7 }, /* TEXICMD_SECTION */
1.24 kristaps 209: { dovalue, "set", 3 }, /* TEXICMD_SET */
1.1 kristaps 210: { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */
211: { doignline, "setfilename", 11 }, /* TEXICMD_SETFILENAME */
1.10 kristaps 212: { dotitle, "settitle", 8 }, /* TEXICMD_SETTITLE */
1.25 ! kristaps 213: { doignline, "shortcontents", 13 }, /* TEXICMD_SHORTCONTENTS */
1.21 kristaps 214: { doinline, "slanted", 7 }, /* TEXICMD_SLANTED */
1.3 kristaps 215: { dosp, "sp", 2 }, /* TEXICMD_SP */
216: { dosymbol, " ", 1 }, /* TEXICMD_SPACE */
1.17 kristaps 217: { doignline, "smallbook", 9 }, /* TEXICMD_SMALLBOOK */
1.12 kristaps 218: { dodisplay, "smalldisplay", 12 }, /* TEXICMD_SMALLDISPLAY */
1.3 kristaps 219: { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */
1.20 kristaps 220: { dodisplay, "smallformat", 11 }, /* TEXICMD_SMALLFORMAT */
1.13 kristaps 221: { dodisplay, "smallindentblock", 16 }, /* TEXICMD_SMALLINDENTBLOCK */
1.3 kristaps 222: { dosymbol, "{", 1 }, /* TEXICMD_SQUIGGLE_LEFT */
223: { dosymbol, "}", 1 }, /* TEXICMD_SQUIGGLE_RIGHT */
1.21 kristaps 224: { doinline, "strong", 6 }, /* TEXICMD_STRONG */
1.20 kristaps 225: { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */
1.1 kristaps 226: { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */
1.23 kristaps 227: { dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */
1.3 kristaps 228: { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */
1.25 ! kristaps 229: { doignline, "summarycontents", 15 }, /* TEXICMD_SUMMARYCONTENTS */
1.20 kristaps 230: { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */
1.21 kristaps 231: { doinline, "t", 1 }, /* TEXICMD_T */
1.18 kristaps 232: { dotab, "tab", 3 }, /* TEXICMD_TAB */
233: { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */
1.1 kristaps 234: { dotable, "table", 5 }, /* TEXICMD_TABLE */
235: { doignblock, "tex", 3 }, /* TEXICMD_TEX */
236: { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */
1.16 kristaps 237: { doaccent, "~", 1 }, /* TEXICMD_TILDE */
1.25 ! kristaps 238: { doignline, "tindex", 6 }, /* TEXICMD_TINDEX */
1.3 kristaps 239: { doignline, "title", 5 }, /* TEXICMD_TITLE */
1.1 kristaps 240: { dobracket, "titlefont", 9 }, /* TEXICMD_TITLEFONT */
241: { doignblock, "titlepage", 9 }, /* TEXICMD_TITLEPAGE */
242: { dotop, "top", 3 }, /* TEXICMD_TOP */
1.16 kristaps 243: { doaccent, "\"", 1 }, /* TEXICMD_UMLAUT */
1.12 kristaps 244: { dosection, "unnumbered", 10 }, /* TEXICMD_UNNUMBERED */
1.2 kristaps 245: { dosection, "unnumberedsec", 13 }, /* TEXICMD_UNNUMBEREDSEC */
1.20 kristaps 246: { dosubsection, "unnumberedsubsec", 16 }, /* TEXICMD_UNNUMBEREDSUBSEC */
1.23 kristaps 247: { dosubsubsection, "unnumberedsubsubsec", 19 }, /* TEXICMD_UNNUMBEREDSUBSUBSEC */
1.8 kristaps 248: { dolink, "uref", 4 }, /* TEXICMD_UREF */
249: { dolink, "url", 3 }, /* TEXICMD_URL */
1.24 kristaps 250: { dovalue, "value", 5 }, /* TEXICMD_VALUE */
1.12 kristaps 251: { doinline, "var", 3 }, /* TEXICMD_VAR */
1.25 ! kristaps 252: { doverb, "verbatim", 8 }, /* TEXICMD_VERBATIM */
1.16 kristaps 253: { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */
1.18 kristaps 254: { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */
1.9 kristaps 255: { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */
1.20 kristaps 256: { dotable, "vtable", 6 }, /* TEXICMD_VTABLE */
1.3 kristaps 257: { dobracket, "w", 1 }, /* TEXICMD_W */
1.8 kristaps 258: { dolink, "xref", 4 }, /* TEXICMD_XREF */
1.20 kristaps 259: /* TEXICMD__END */
1.1 kristaps 260: };
261:
1.24 kristaps 262: const struct texitok *const texitoks = __texitoks;
1.18 kristaps 263:
1.2 kristaps 264: static void
1.15 kristaps 265: dodefn(struct texi *p, enum texicmd cmd,
1.3 kristaps 266: const char *buf, size_t sz, size_t *pos)
267: {
268: const char *blk;
269:
1.5 kristaps 270: blk = NULL;
1.3 kristaps 271: switch (cmd) {
1.15 kristaps 272: case (TEXICMD_DEFFN):
273: case (TEXICMD_DEFTP):
1.3 kristaps 274: case (TEXICMD_DEFTYPEFN):
275: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 276: case (TEXICMD_DEFTYPEVAR):
277: case (TEXICMD_DEFTYPEVR):
278: case (TEXICMD_DEFUN):
279: case (TEXICMD_DEFVAR):
280: case (TEXICMD_DEFVR):
1.5 kristaps 281: blk = texitoks[cmd].tok;
1.3 kristaps 282: break;
1.5 kristaps 283: default:
1.3 kristaps 284: break;
285: }
286:
287: if (p->ign) {
1.15 kristaps 288: NULL != blk ?
289: parseto(p, buf, sz, pos, blk) :
290: parseeoln(p, buf, sz, pos);
1.3 kristaps 291: return;
292: }
293:
1.15 kristaps 294: if (NULL != blk)
295: texivspace(p);
296:
1.3 kristaps 297: switch (cmd) {
1.15 kristaps 298: case (TEXICMD_DEFMAC):
299: case (TEXICMD_DEFMACX):
300: texiputchars(p, "Macro");
301: break;
302: case (TEXICMD_DEFTYPEVAR):
303: case (TEXICMD_DEFTYPEVARX):
304: case (TEXICMD_DEFVAR):
305: case (TEXICMD_DEFVARX):
306: texiputchars(p, "Variable");
307: break;
1.3 kristaps 308: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 309: case (TEXICMD_DEFTYPEFUNX):
310: case (TEXICMD_DEFUN):
311: case (TEXICMD_DEFUNX):
312: texiputchars(p, "Function");
1.3 kristaps 313: break;
314: default:
1.15 kristaps 315: parselinearg(p, buf, sz, pos);
1.3 kristaps 316: break;
317: }
1.15 kristaps 318:
319: texiputchars(p, ":\n");
320:
321: switch (cmd) {
322: case (TEXICMD_DEFMAC):
323: case (TEXICMD_DEFMACX):
324: teximacroopen(p, "Dv");
325: while (parselinearg(p, buf, sz, pos))
326: /* Spin. */ ;
327: teximacroclose(p);
328: break;
329: case (TEXICMD_DEFFN):
330: case (TEXICMD_DEFFNX):
331: case (TEXICMD_DEFUN):
332: case (TEXICMD_DEFUNX):
333: teximacroopen(p, "Fo");
334: parselinearg(p, buf, sz, pos);
335: teximacroclose(p);
336: teximacroopen(p, "Fa");
337: while (parselinearg(p, buf, sz, pos))
338: /* Spin. */ ;
339: teximacroclose(p);
340: teximacro(p, "Fc");
341: break;
342: case (TEXICMD_DEFTYPEFUN):
343: case (TEXICMD_DEFTYPEFUNX):
344: case (TEXICMD_DEFTYPEFN):
345: case (TEXICMD_DEFTYPEFNX):
346: teximacroopen(p, "Ft");
347: parselinearg(p, buf, sz, pos);
348: teximacroclose(p);
349: teximacroopen(p, "Fo");
350: parselinearg(p, buf, sz, pos);
351: teximacroclose(p);
352: teximacroopen(p, "Fa");
353: while (parselinearg(p, buf, sz, pos))
354: /* Spin. */ ;
355: teximacroclose(p);
356: teximacro(p, "Fc");
357: break;
358: case (TEXICMD_DEFTP):
359: case (TEXICMD_DEFTPX):
360: case (TEXICMD_DEFTYPEVAR):
361: case (TEXICMD_DEFTYPEVARX):
362: case (TEXICMD_DEFTYPEVR):
363: case (TEXICMD_DEFTYPEVRX):
364: teximacroopen(p, "Vt");
365: while (parselinearg(p, buf, sz, pos))
366: /* Spin. */ ;
367: teximacroclose(p);
368: break;
369: case (TEXICMD_DEFVAR):
370: case (TEXICMD_DEFVARX):
371: case (TEXICMD_DEFVR):
372: case (TEXICMD_DEFVRX):
373: teximacroopen(p, "Va");
374: while (parselinearg(p, buf, sz, pos))
375: /* Spin. */ ;
376: teximacroclose(p);
377: break;
378: default:
379: abort();
1.3 kristaps 380: }
1.15 kristaps 381:
1.11 kristaps 382: texivspace(p);
1.3 kristaps 383: if (NULL != blk)
384: parseto(p, buf, sz, pos, blk);
385: }
386:
387: static void
1.1 kristaps 388: doignblock(struct texi *p, enum texicmd cmd,
389: const char *buf, size_t sz, size_t *pos)
390: {
391:
1.3 kristaps 392: p->ign++;
1.5 kristaps 393: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 394: p->ign--;
1.1 kristaps 395: }
396:
397: static void
1.3 kristaps 398: doblock(struct texi *p, enum texicmd cmd,
1.1 kristaps 399: const char *buf, size_t sz, size_t *pos)
400: {
401:
1.5 kristaps 402: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.1 kristaps 403: }
404:
405: static void
1.12 kristaps 406: doinline(struct texi *p, enum texicmd cmd,
407: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 408: {
1.21 kristaps 409: const char *macro = NULL;
1.12 kristaps 410:
411: switch (cmd) {
1.21 kristaps 412: case (TEXICMD_CODE):
413: case (TEXICMD_KBD):
414: case (TEXICMD_SAMP):
415: case (TEXICMD_T):
416: macro = "Li";
417: break;
418: case (TEXICMD_CITE):
419: case (TEXICMD_DFN):
420: case (TEXICMD_EMPH):
421: case (TEXICMD_I):
422: case (TEXICMD_SLANTED):
423: macro = "Em";
424: break;
425: case (TEXICMD_B):
426: case (TEXICMD_STRONG):
427: macro = "Sy";
428: break;
1.12 kristaps 429: case (TEXICMD_COMMAND):
430: macro = "Xr";
431: break;
432: case (TEXICMD_ENV):
433: macro = "Ev";
434: break;
435: case (TEXICMD_FILE):
436: macro = "Pa";
437: break;
1.16 kristaps 438: case (TEXICMD_OPTION):
439: macro = "Op";
440: break;
1.12 kristaps 441: case (TEXICMD_VAR):
442: macro = "Va";
443: break;
444: default:
1.22 kristaps 445: break;
1.12 kristaps 446: }
447:
1.25 ! kristaps 448: if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) {
1.12 kristaps 449: parsebracket(p, buf, sz, pos);
450: return;
451: }
1.1 kristaps 452:
1.5 kristaps 453: teximacroopen(p, macro);
1.1 kristaps 454: p->seenws = 0;
455: parsebracket(p, buf, sz, pos);
1.11 kristaps 456: texipunctuate(p, buf, sz, pos);
1.5 kristaps 457: teximacroclose(p);
1.1 kristaps 458: }
459:
460: static void
1.25 ! kristaps 461: doverb(struct texi *p, enum texicmd cmd,
! 462: const char *buf, size_t sz, size_t *pos)
! 463: {
! 464: const char *end, *term;
! 465: size_t endsz, endpos;
! 466:
! 467: /* We end at exactly this token. */
! 468: end = "\n@end verbatim\n";
! 469: endsz = strlen(end);
! 470:
! 471: /*
! 472: * Look up where our end token occurs.
! 473: * Set our end position based on the relative offset of that
! 474: * from our current position.
! 475: */
! 476: term = memmem(&buf[*pos], sz, end, endsz);
! 477: endpos = NULL == term ? sz :
! 478: *pos + term - &buf[*pos];
! 479:
! 480: teximacro(p, "Bd -literal -offset indent");
! 481: assert(endpos <= sz);
! 482: /* Run to the point inclusive the endpoint newline. */
! 483: while (*pos < endpos + 1) {
! 484: if (*pos > 0 && '.' == buf[*pos])
! 485: if ('\n' == buf[*pos - 1])
! 486: fputs("\\&", stdout);
! 487: putchar(buf[*pos]);
! 488: if ('\\' == buf[*pos])
! 489: putchar('e');
! 490: advance(p, buf, pos);
! 491: }
! 492: teximacro(p, "Ed");
! 493: advanceto(p, buf, pos, *pos + endpos + endsz);
! 494: }
! 495:
! 496: static void
1.16 kristaps 497: doverbinclude(struct texi *p, enum texicmd cmd,
498: const char *buf, size_t sz, size_t *pos)
499: {
1.25 ! kristaps 500: char fname[PATH_MAX], path[PATH_MAX];
! 501: int rc;
! 502: size_t i, end;
! 503: const char *v;
! 504: enum texicmd type;
1.16 kristaps 505:
506: while (*pos < sz && ' ' == buf[*pos])
507: advance(p, buf, pos);
508:
1.25 ! kristaps 509: for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.16 kristaps 510: if (i == sizeof(fname) - 1)
511: break;
1.25 ! kristaps 512: if ('@' != buf[*pos]) {
! 513: fname[i++] = buf[*pos];
! 514: advance(p, buf, pos);
! 515: continue;
! 516: }
! 517: type = texicmd(p, buf, *pos, sz, &end);
! 518: advanceto(p, buf, pos, end);
! 519: if (TEXICMD_VALUE != type)
! 520: texierr(p, "unknown verbatiminclude command");
! 521: v = valueblookup(p, buf, sz, pos);
! 522: if (NULL == v)
! 523: continue;
! 524: while ('\0' != *v) {
! 525: if (i == sizeof(fname) - 1)
! 526: break;
! 527: fname[i++] = *v++;
! 528: }
! 529: if ('\0' != *v)
! 530: break;
1.16 kristaps 531: }
532:
533: if (i == 0)
534: texierr(p, "path too short");
535: else if ('\n' != buf[*pos])
536: texierr(p, "path too long");
537: else if ('/' == fname[0])
538: texierr(p, "no absolute paths");
539: fname[i] = '\0';
540:
541: if (strstr(fname, "../") || strstr(fname, "/.."))
542: texierr(p, "insecure path");
543:
544: rc = snprintf(path, sizeof(path),
545: "%s/%s", p->dirs[0], fname);
546: if (rc < 0)
547: texierr(p, "couldn't format path");
548: else if ((size_t)rc >= sizeof(path))
549: texierr(p, "path too long");
550:
551: parsefile(p, path, 0);
552: }
553:
554: static void
1.2 kristaps 555: doinclude(struct texi *p, enum texicmd cmd,
556: const char *buf, size_t sz, size_t *pos)
557: {
1.25 ! kristaps 558: char fname[PATH_MAX], path[PATH_MAX];
! 559: size_t i, end;
! 560: int rc;
! 561: const char *v;
! 562: enum texicmd type;
1.2 kristaps 563:
564: while (*pos < sz && ' ' == buf[*pos])
565: advance(p, buf, pos);
566:
567: /* Read in the filename. */
1.25 ! kristaps 568: for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.2 kristaps 569: if (i == sizeof(fname) - 1)
570: break;
1.25 ! kristaps 571: if ('@' != buf[*pos]) {
! 572: fname[i++] = buf[*pos];
! 573: advance(p, buf, pos);
! 574: continue;
! 575: }
! 576: type = texicmd(p, buf, *pos, sz, &end);
! 577: advanceto(p, buf, pos, end);
! 578: if (TEXICMD_VALUE != type)
! 579: texierr(p, "unknown include command");
! 580: v = valueblookup(p, buf, sz, pos);
! 581: if (NULL == v)
! 582: continue;
! 583: while ('\0' != *v) {
! 584: if (i == sizeof(fname) - 1)
! 585: break;
! 586: fname[i++] = *v++;
! 587: }
! 588: if ('\0' != *v)
! 589: break;
1.2 kristaps 590: }
591:
592: if (i == 0)
593: texierr(p, "path too short");
594: else if ('\n' != buf[*pos])
595: texierr(p, "path too long");
596: else if ('/' == fname[0])
597: texierr(p, "no absolute paths");
598: fname[i] = '\0';
599:
600: if (strstr(fname, "../") || strstr(fname, "/.."))
601: texierr(p, "insecure path");
602:
1.5 kristaps 603: for (i = 0; i < p->dirsz; i++) {
604: rc = snprintf(path, sizeof(path),
605: "%s/%s", p->dirs[i], fname);
606: if (rc < 0)
607: texierr(p, "couldn't format path");
608: else if ((size_t)rc >= sizeof(path))
609: texierr(p, "path too long");
610: else if (-1 == access(path, R_OK))
611: continue;
612:
1.16 kristaps 613: parsefile(p, path, 1);
1.5 kristaps 614: return;
615: }
1.2 kristaps 616:
1.5 kristaps 617: texierr(p, "couldn't find %s in includes", fname);
1.2 kristaps 618: }
619:
620: static void
1.1 kristaps 621: dobracket(struct texi *p, enum texicmd cmd,
622: const char *buf, size_t sz, size_t *pos)
623: {
624:
625: parsebracket(p, buf, sz, pos);
626: }
627:
628: static void
1.3 kristaps 629: dodisplay(struct texi *p, enum texicmd cmd,
630: const char *buf, size_t sz, size_t *pos)
631: {
632:
1.20 kristaps 633: switch (cmd) {
634: case (TEXICMD_FORMAT):
635: case (TEXICMD_SMALLFORMAT):
636: teximacro(p, "Bd -filled");
637: break;
638: default:
639: teximacro(p, "Bd -filled -offset indent");
640: break;
641: }
642:
1.11 kristaps 643: p->seenvs = 1;
1.12 kristaps 644: /* FIXME: ignore and parseeoln. */
1.3 kristaps 645: advanceeoln(p, buf, sz, pos, 1);
1.13 kristaps 646: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 647: teximacro(p, "Ed");
1.3 kristaps 648: }
649:
650: static void
1.1 kristaps 651: doexample(struct texi *p, enum texicmd cmd,
652: const char *buf, size_t sz, size_t *pos)
653: {
654:
1.5 kristaps 655: teximacro(p, "Bd -literal -offset indent");
1.12 kristaps 656: /* FIXME: ignore and parseeoln. */
1.3 kristaps 657: advanceeoln(p, buf, sz, pos, 1);
658: p->literal++;
1.13 kristaps 659: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 660: p->literal--;
1.5 kristaps 661: teximacro(p, "Ed");
1.1 kristaps 662: }
663:
664: static void
665: dobye(struct texi *p, enum texicmd cmd,
666: const char *buf, size_t sz, size_t *pos)
667: {
668:
669: texiexit(p);
670: exit(EXIT_SUCCESS);
671: }
672:
673: static void
1.10 kristaps 674: dotitle(struct texi *p, enum texicmd cmd,
675: const char *buf, size_t sz, size_t *pos)
676: {
677: size_t start, end;
678:
679: while (*pos < sz && isws(buf[*pos]))
680: advance(p, buf, pos);
681: start = end = *pos;
682: while (end < sz && '\n' != buf[end])
683: end++;
684: free(p->subtitle);
685: p->subtitle = malloc(end - start + 1);
686: memcpy(p->subtitle, &buf[start], end - start);
687: p->subtitle[end - start] = '\0';
688: }
689:
690: static void
1.16 kristaps 691: doaccent(struct texi *p, enum texicmd cmd,
692: const char *buf, size_t sz, size_t *pos)
693: {
694:
695: if (*pos == sz)
696: return;
697: advance(p, buf, pos);
698: switch (cmd) {
699: case (TEXICMD_ACUTE):
700: switch (buf[*pos]) {
701: case ('a'): case ('A'):
702: case ('e'): case ('E'):
703: case ('i'): case ('I'):
704: case ('o'): case ('O'):
705: case ('u'): case ('U'):
706: texiputchars(p, "\\(\'");
707: texiputchar(p, buf[*pos]);
708: break;
709: default:
710: texiputchar(p, buf[*pos]);
711: }
712: break;
713: case (TEXICMD_CIRCUMFLEX):
714: switch (buf[*pos]) {
715: case ('a'): case ('A'):
716: case ('e'): case ('E'):
717: case ('i'): case ('I'):
718: case ('o'): case ('O'):
719: case ('u'): case ('U'):
720: texiputchars(p, "\\(^");
721: texiputchar(p, buf[*pos]);
722: break;
723: default:
724: texiputchar(p, buf[*pos]);
725: }
726: break;
727: case (TEXICMD_GRAVE):
728: switch (buf[*pos]) {
729: case ('a'): case ('A'):
730: case ('e'): case ('E'):
731: case ('i'): case ('I'):
732: case ('o'): case ('O'):
733: case ('u'): case ('U'):
734: texiputchars(p, "\\(`");
735: texiputchar(p, buf[*pos]);
736: break;
737: default:
738: texiputchar(p, buf[*pos]);
739: }
740: break;
741: case (TEXICMD_TILDE):
742: switch (buf[*pos]) {
743: case ('a'): case ('A'):
744: case ('n'): case ('N'):
745: case ('o'): case ('O'):
746: texiputchars(p, "\\(~");
747: texiputchar(p, buf[*pos]);
748: break;
749: default:
750: texiputchar(p, buf[*pos]);
751: }
752: break;
753: case (TEXICMD_UMLAUT):
754: switch (buf[*pos]) {
755: case ('a'): case ('A'):
756: case ('e'): case ('E'):
757: case ('i'): case ('I'):
758: case ('o'): case ('O'):
759: case ('u'): case ('U'):
760: case ('y'):
761: texiputchars(p, "\\(:");
762: texiputchar(p, buf[*pos]);
763: break;
764: default:
765: texiputchar(p, buf[*pos]);
766: }
767: break;
768: default:
769: abort();
770: }
771: }
772:
773: static void
1.1 kristaps 774: dosymbol(struct texi *p, enum texicmd cmd,
775: const char *buf, size_t sz, size_t *pos)
776: {
777:
1.3 kristaps 778: if (p->seenws && p->outcol && 0 == p->literal) {
779: texiputchar(p, ' ');
780: p->seenws = 0;
781: }
782:
1.1 kristaps 783: switch (cmd) {
1.3 kristaps 784: case (TEXICMD_ASTERISK):
785: case (TEXICMD_NEWLINE):
786: case (TEXICMD_SPACE):
1.18 kristaps 787: case (TEXICMD_TABSYM):
1.3 kristaps 788: texiputchar(p, ' ');
789: break;
1.1 kristaps 790: case (TEXICMD_AT):
1.3 kristaps 791: texiputchar(p, '@');
792: break;
793: case (TEXICMD_BANG):
794: texiputchar(p, '!');
1.7 kristaps 795: break;
796: case (TEXICMD_BULLET):
797: texiputchars(p, "\\(bu");
1.1 kristaps 798: break;
799: case (TEXICMD_COPYRIGHT):
800: texiputchars(p, "\\(co");
801: break;
1.2 kristaps 802: case (TEXICMD_DOTS):
803: texiputchars(p, "...");
804: break;
1.15 kristaps 805: case (TEXICMD_ERROR):
806: texiputchars(p, "error\\(->");
1.17 kristaps 807: break;
808: case (TEXICMD_EXPANSION):
809: texiputchars(p, "\\(->");
1.15 kristaps 810: break;
1.1 kristaps 811: case (TEXICMD_LATEX):
812: texiputchars(p, "LaTeX");
813: break;
1.25 ! kristaps 814: case (TEXICMD_MINUS):
! 815: texiputchars(p, "\\-");
! 816: break;
! 817: case (TEXICMD_PERIOD):
! 818: texiputchar(p, '.');
! 819: break;
1.3 kristaps 820: case (TEXICMD_QUESTIONMARK):
821: texiputchar(p, '?');
1.15 kristaps 822: break;
823: case (TEXICMD_RESULT):
824: texiputchars(p, "\\(rA");
1.3 kristaps 825: break;
826: case (TEXICMD_SQUIGGLE_LEFT):
827: texiputchars(p, "{");
828: break;
829: case (TEXICMD_SQUIGGLE_RIGHT):
830: texiputchars(p, "}");
831: break;
1.1 kristaps 832: case (TEXICMD_TEXSYM):
833: texiputchars(p, "TeX");
834: break;
1.3 kristaps 835: case (TEXICMD_COLON):
836: case (TEXICMD_HYPHEN):
837: break;
1.1 kristaps 838: default:
1.5 kristaps 839: texiwarn(p, "sym: %d", cmd);
1.1 kristaps 840: abort();
841: }
842:
1.5 kristaps 843: if (texitoks[cmd].len > 1)
844: doignbracket(p, cmd, buf, sz, pos);
1.1 kristaps 845: }
846:
847: static void
848: doquotation(struct texi *p, enum texicmd cmd,
849: const char *buf, size_t sz, size_t *pos)
850: {
851:
1.5 kristaps 852: teximacro(p, "Qo");
1.1 kristaps 853: parseto(p, buf, sz, pos, "quotation");
1.5 kristaps 854: teximacro(p, "Qc");
1.1 kristaps 855: }
856:
1.3 kristaps 857: static void
858: domath(struct texi *p, enum texicmd cmd,
859: const char *buf, size_t sz, size_t *pos)
860: {
861: size_t nest;
862:
863: /*
864: * Math handling is different from everything else.
865: * We don't allow any subcomponents, and we ignore the rules in
866: * terms of @-commands.
867: * This departs from GNU's rules, but whatever.
868: */
869: while (*pos < sz && isws(buf[*pos]))
870: advance(p, buf, pos);
871: if (*pos == sz || '{' != buf[*pos])
872: return;
873: advance(p, buf, pos);
874: if (p->seenws && p->outcol && 0 == p->literal)
875: texiputchar(p, ' ');
876: p->seenws = 0;
877: for (nest = 1; *pos < sz && nest > 0; ) {
878: if ('{' == buf[*pos])
879: nest++;
880: else if ('}' == buf[*pos])
881: if (0 == --nest)
882: continue;
883: texiputchar(p, buf[*pos]);
884: advance(p, buf, pos);
885: }
886: if (*pos == sz)
887: return;
888: assert('}' == buf[*pos]);
889: advance(p, buf, pos);
1.24 kristaps 890: }
891:
892: static void
893: dovalue(struct texi *p, enum texicmd cmd,
894: const char *buf, size_t sz, size_t *pos)
895: {
1.25 ! kristaps 896: size_t start, end;
! 897: int clr;
! 898: char *key, *val;
! 899: const char *cp;
1.24 kristaps 900:
901: if (TEXICMD_SET == cmd) {
902: while (*pos < sz && isws(buf[*pos]))
903: advance(p, buf, pos);
904: for (start = end = *pos; end < sz; end++)
905: if (ismspace(buf[end]))
906: break;
1.25 ! kristaps 907: /* We don't allow empty keys. */
1.24 kristaps 908: if (start == end)
909: return;
1.25 ! kristaps 910: advanceto(p, buf, pos, end);
1.24 kristaps 911:
912: key = malloc(end - start + 1);
913: if (NULL == key) {
914: perror(NULL);
915: exit(EXIT_FAILURE);
916: }
917: memcpy(key, &buf[start], end - start);
918: key[end - start] = '\0';
919:
920: while (*pos < sz && isws(buf[*pos]))
921: advance(p, buf, pos);
922: for (start = end = *pos; end < sz; end++)
923: if ('\n' == buf[end])
924: break;
1.25 ! kristaps 925: /* We do allow empty values. */
! 926: advanceeoln(p, buf, sz, pos, 1);
1.24 kristaps 927:
928: val = malloc(end - start + 1);
929: if (NULL == val) {
930: perror(NULL);
931: exit(EXIT_FAILURE);
932: }
933: memcpy(val, &buf[start], end - start);
934: val[end - start] = '\0';
1.25 ! kristaps 935: valueadd(p, key, val);
1.24 kristaps 936: } else if (TEXICMD_VALUE == cmd) {
937: if (p->seenws)
938: texiputchar(p, ' ');
939: p->seenws = 0;
1.25 ! kristaps 940: if (NULL == (cp = valueblookup(p, buf, sz, pos)))
! 941: texiputchars(p, "{No value}");
! 942: else
! 943: texiputchars(p, cp);
1.24 kristaps 944: } else if (TEXICMD_IFCLEAR == cmd) {
1.25 ! kristaps 945: clr = NULL != valuellookup(p, buf, sz, pos);
1.24 kristaps 946: if (clr)
947: p->ign++;
948: parseto(p, buf, sz, pos, texitoks[cmd].tok);
949: if (clr)
950: p->ign--;
1.25 ! kristaps 951: } else if (TEXICMD_CLEAR == cmd)
! 952: valuelclear(p, buf, sz, pos);
1.3 kristaps 953: }
954:
1.1 kristaps 955: static void
1.8 kristaps 956: dolink(struct texi *p, enum texicmd cmd,
1.1 kristaps 957: const char *buf, size_t sz, size_t *pos)
958: {
1.8 kristaps 959: int c;
1.1 kristaps 960:
961: switch (cmd) {
962: case (TEXICMD_EMAIL):
1.5 kristaps 963: teximacroopen(p, "Mt");
1.1 kristaps 964: break;
1.3 kristaps 965: case (TEXICMD_UREF):
1.1 kristaps 966: case (TEXICMD_URL):
1.5 kristaps 967: teximacroopen(p, "Lk");
1.1 kristaps 968: break;
1.8 kristaps 969: case (TEXICMD_XREF):
970: texiputchars(p, "See Section");
971: teximacroopen(p, "Qq");
972: break;
973: case (TEXICMD_PXREF):
974: texiputchars(p, "see Section");
975: teximacroopen(p, "Qq");
976: break;
1.1 kristaps 977: default:
1.8 kristaps 978: abort();
1.1 kristaps 979: }
1.8 kristaps 980:
981: c = parsearg(p, buf, sz, pos, 0);
982: p->ign++;
983: while (c > 0)
984: c = parsearg(p, buf, sz, pos, 1);
985: p->ign--;
986:
1.11 kristaps 987: texipunctuate(p, buf, sz, pos);
1.8 kristaps 988: teximacroclose(p);
989: }
990:
991: static void
992: doignargn(struct texi *p, enum texicmd cmd,
993: const char *buf, size_t sz, size_t *pos)
994: {
995: int c;
996:
997: c = parsearg(p, buf, sz, pos, 0);
998: p->ign++;
999: while (c > 0)
1000: c = parsearg(p, buf, sz, pos, 1);
1001: p->ign--;
1.1 kristaps 1002: }
1003:
1.23 kristaps 1004: /*
1005: * Sections can be made subsections and so on by way of the
1006: * @raiseections and @lowersections commands.
1007: * Perform this check here and return the actual section number adjusted
1008: * to the raise level.
1009: */
1010: static int
1011: sectioner(struct texi *p, int sec)
1012: {
1013:
1014: if ((sec -= p->secoffs) < 0) {
1015: texiwarn(p, "section below minimum, clamping");
1016: return(0);
1017: } else if (sec >= SECTSZ) {
1018: texiwarn(p, "section above maximum, clamping");
1019: return(SECTSZ - 1);
1020: }
1021: return(sec);
1022: }
1023:
1024: static void
1025: dosubsubsection(struct texi *p, enum texicmd cmd,
1026: const char *buf, size_t sz, size_t *pos)
1027: {
1028: int sec;
1029:
1030: sec = sectioner(p, 3);
1031:
1032: /* We don't have a subsubsubsection, so make one up. */
1033: texivspace(p);
1034: teximacroopen(p, sects[sec]);
1035: parseeoln(p, buf, sz, pos);
1036: teximacroclose(p);
1037: texivspace(p);
1038: }
1039:
1.1 kristaps 1040: static void
1041: dosubsection(struct texi *p, enum texicmd cmd,
1042: const char *buf, size_t sz, size_t *pos)
1043: {
1.23 kristaps 1044: int sec;
1045:
1046: sec = sectioner(p, 2);
1.13 kristaps 1047:
1048: if (p->outmacro)
1.23 kristaps 1049: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13 kristaps 1050: else if (p->literal)
1.23 kristaps 1051: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1 kristaps 1052:
1.21 kristaps 1053: /* We don't have a subsubsection, so make one up. */
1.11 kristaps 1054: texivspace(p);
1.23 kristaps 1055: teximacroopen(p, sects[sec]);
1.3 kristaps 1056: parseeoln(p, buf, sz, pos);
1.5 kristaps 1057: teximacroclose(p);
1.11 kristaps 1058: texivspace(p);
1.1 kristaps 1059: }
1060:
1061: static void
1.23 kristaps 1062: dosecoffs(struct texi *p, enum texicmd cmd,
1063: const char *buf, size_t sz, size_t *pos)
1064: {
1065:
1066: if (TEXICMD_RAISESECTIONS == cmd)
1067: p->secoffs++;
1068: else
1069: p->secoffs--;
1070: }
1071:
1072: static void
1.1 kristaps 1073: dosection(struct texi *p, enum texicmd cmd,
1.23 kristaps 1074: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 1075: {
1.23 kristaps 1076: int sec;
1.12 kristaps 1077:
1078: switch (cmd) {
1079: case (TEXICMD_APPENDIX):
1080: case (TEXICMD_CHAPTER):
1081: case (TEXICMD_TOP):
1082: case (TEXICMD_UNNUMBERED):
1.23 kristaps 1083: sec = sectioner(p, 0);
1.12 kristaps 1084: break;
1085: case (TEXICMD_APPENDIXSEC):
1086: case (TEXICMD_HEADING):
1087: case (TEXICMD_SECTION):
1088: case (TEXICMD_UNNUMBEREDSEC):
1.23 kristaps 1089: sec = sectioner(p, 1);
1.12 kristaps 1090: break;
1091: default:
1092: abort();
1093: }
1.1 kristaps 1094:
1.3 kristaps 1095: if (p->outmacro)
1.23 kristaps 1096: texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3 kristaps 1097: else if (p->literal)
1.23 kristaps 1098: texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3 kristaps 1099:
1.23 kristaps 1100: teximacroopen(p, sects[sec]);
1.3 kristaps 1101: parseeoln(p, buf, sz, pos);
1102: teximacroclose(p);
1.11 kristaps 1103: p->seenvs = 1;
1.3 kristaps 1104: }
1105:
1106: static void
1107: dosp(struct texi *p, enum texicmd cmd,
1108: const char *buf, size_t sz, size_t *pos)
1109: {
1110:
1.11 kristaps 1111: texivspace(p);
1.12 kristaps 1112: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1113: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1114: }
1115:
1116: static void
1117: dotop(struct texi *p, enum texicmd cmd,
1118: const char *buf, size_t sz, size_t *pos)
1119: {
1.10 kristaps 1120: const char *cp;
1121: time_t t;
1122: char date[32];
1123:
1.18 kristaps 1124: /*
1125: * Here we print our standard mdoc(7) prologue.
1126: * We use the title set with @settitle for the `Nd' description
1127: * and the source document filename (the first one as invoked on
1128: * the command line) for the title.
1129: * The date is set to the current date.
1130: */
1.10 kristaps 1131: t = time(NULL);
1132: strftime(date, sizeof(date), "%F", localtime(&t));
1.1 kristaps 1133:
1.3 kristaps 1134: p->ign--;
1.10 kristaps 1135: teximacroopen(p, "Dd");
1136: texiputchars(p, date);
1137: teximacroclose(p);
1138: teximacroopen(p, "Dt");
1139: for (cp = p->title; '\0' != *cp; cp++)
1140: texiputchar(p, toupper(*cp));
1.11 kristaps 1141: texiputchars(p, " 7");
1.10 kristaps 1142: teximacroclose(p);
1.5 kristaps 1143: teximacro(p, "Os");
1144: teximacro(p, "Sh NAME");
1.10 kristaps 1145: teximacroopen(p, "Nm");
1146: texiputchars(p, p->title);
1147: teximacroclose(p);
1148: teximacroopen(p, "Nd");
1149: texiputchars(p, NULL != p->subtitle ?
1150: p->subtitle : "Unknown description");
1151: teximacroclose(p);
1.11 kristaps 1152: p->seenvs = 1;
1.12 kristaps 1153: dosection(p, cmd, buf, sz, pos);
1.1 kristaps 1154: }
1155:
1156: static void
1157: doitem(struct texi *p, enum texicmd cmd,
1158: const char *buf, size_t sz, size_t *pos)
1159: {
1160:
1.18 kristaps 1161: /* Multitable is using raw tbl(7). */
1162: if (TEXILIST_TABLE == p->list) {
1163: texiputchar(p, '\n');
1164: return;
1165: }
1166:
1.3 kristaps 1167: if (p->outmacro)
1168: texierr(p, "item in open line scope!?");
1169: else if (p->literal)
1170: texierr(p, "item in a literal scope!?");
1171:
1172: switch (p->list) {
1173: case (TEXILIST_ITEM):
1.5 kristaps 1174: teximacroopen(p, "It");
1.3 kristaps 1175: break;
1176: case (TEXILIST_NOITEM):
1.5 kristaps 1177: teximacro(p, "It");
1.3 kristaps 1178: break;
1179: default:
1.11 kristaps 1180: texivspace(p);
1.3 kristaps 1181: break;
1182: }
1.18 kristaps 1183:
1184: /* Trick so we don't start with Pp. */
1.11 kristaps 1185: p->seenvs = 1;
1.3 kristaps 1186: parseeoln(p, buf, sz, pos);
1.1 kristaps 1187:
1.3 kristaps 1188: if (TEXILIST_ITEM == p->list)
1189: teximacroclose(p);
1.9 kristaps 1190: else if (p->outcol > 0)
1.1 kristaps 1191: texiputchar(p, '\n');
1.18 kristaps 1192: }
1193:
1194: static void
1195: dotab(struct texi *p, enum texicmd cmd,
1196: const char *buf, size_t sz, size_t *pos)
1197: {
1198:
1199: /* This command is only useful in @multitable. */
1200: if (TEXILIST_TABLE == p->list)
1201: texiputchar(p, '\t');
1202: }
1203:
1204: static void
1205: domultitable(struct texi *p, enum texicmd cmd,
1206: const char *buf, size_t sz, size_t *pos)
1207: {
1208: enum texilist sv = p->list;
1209: enum texicmd type;
1210: size_t i, end, columns;
1211:
1212: p->list = TEXILIST_TABLE;
1213: teximacro(p, "TS");
1214: columns = 0;
1215:
1216: /* Advance to the first argument... */
1217: while (*pos < sz && isws(buf[*pos]))
1218: advance(p, buf, pos);
1219:
1220: /* Make sure we don't print anything when scanning. */
1221: p->ign++;
1222: if ('@' == buf[*pos]) {
1223: /*
1224: * Look for @columnfractions.
1225: * We ignore these, but we do use the number of
1226: * arguments to set the number of columns that we'll
1227: * have.
1228: */
1229: type = texicmd(p, buf, *pos, sz, &end);
1230: advanceto(p, buf, pos, end);
1231: if (TEXICMD_COLUMNFRACTIONS != type)
1.25 ! kristaps 1232: texierr(p, "unknown multitable command");
1.18 kristaps 1233: while (*pos < sz && '\n' != buf[*pos]) {
1234: while (*pos < sz && isws(buf[*pos]))
1235: advance(p, buf, pos);
1236: while (*pos < sz && ! isws(buf[*pos])) {
1237: if ('\n' == buf[*pos])
1238: break;
1239: advance(p, buf, pos);
1240: }
1241: columns++;
1242: }
1243: } else
1244: /*
1245: * We have arguments.
1246: * We could parse these, but it's easier to just let
1247: * tbl(7) figure it out.
1248: * So use this only to count arguments.
1249: */
1250: while (parselinearg(p, buf, sz, pos) > 0)
1251: columns++;
1252: p->ign--;
1253:
1254: /* Left-justify each table entry. */
1255: for (i = 0; i < columns; i++) {
1256: if (i > 0)
1257: texiputchar(p, ' ');
1258: texiputchar(p, 'l');
1259: }
1260: texiputchars(p, ".\n");
1261: p->outmacro++;
1262: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1263: p->outmacro--;
1264: teximacro(p, "TE");
1265: p->list = sv;
1.1 kristaps 1266: }
1267:
1268: static void
1269: dotable(struct texi *p, enum texicmd cmd,
1270: const char *buf, size_t sz, size_t *pos)
1271: {
1.3 kristaps 1272: enum texilist sv = p->list;
1273:
1274: p->list = TEXILIST_ITEM;
1.5 kristaps 1275: teximacro(p, "Bl -tag -width Ds");
1.12 kristaps 1276: /* FIXME: ignore and parseeoln. */
1277: advanceeoln(p, buf, sz, pos, 1);
1.11 kristaps 1278: p->seenvs = 1;
1.20 kristaps 1279: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 1280: teximacro(p, "El");
1.3 kristaps 1281: p->list = sv;
1.1 kristaps 1282: }
1283:
1284: static void
1.2 kristaps 1285: doenumerate(struct texi *p, enum texicmd cmd,
1286: const char *buf, size_t sz, size_t *pos)
1287: {
1.3 kristaps 1288: enum texilist sv = p->list;
1.2 kristaps 1289:
1.3 kristaps 1290: p->list = TEXILIST_NOITEM;
1.5 kristaps 1291: teximacro(p, "Bl -enum");
1.11 kristaps 1292: p->seenvs = 1;
1.12 kristaps 1293: /* FIXME: ignore and parseeoln. */
1294: advanceeoln(p, buf, sz, pos, 1);
1.2 kristaps 1295: parseto(p, buf, sz, pos, "enumerate");
1.5 kristaps 1296: teximacro(p, "El");
1.3 kristaps 1297: p->list = sv;
1.2 kristaps 1298: }
1299:
1300: static void
1.1 kristaps 1301: doitemize(struct texi *p, enum texicmd cmd,
1302: const char *buf, size_t sz, size_t *pos)
1303: {
1.3 kristaps 1304: enum texilist sv = p->list;
1.1 kristaps 1305:
1.21 kristaps 1306: p->list = TEXILIST_NOITEM;
1.5 kristaps 1307: teximacro(p, "Bl -bullet");
1.11 kristaps 1308: p->seenvs = 1;
1.12 kristaps 1309: /* FIXME: ignore and parseeoln. */
1310: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1311: parseto(p, buf, sz, pos, "itemize");
1.5 kristaps 1312: teximacro(p, "El");
1.3 kristaps 1313: p->list = sv;
1.1 kristaps 1314: }
1315:
1316: static void
1317: doignbracket(struct texi *p, enum texicmd cmd,
1318: const char *buf, size_t sz, size_t *pos)
1319: {
1320:
1.3 kristaps 1321: p->ign++;
1.1 kristaps 1322: parsebracket(p, buf, sz, pos);
1.3 kristaps 1323: p->ign--;
1.1 kristaps 1324: }
1325:
1326: static void
1327: doignline(struct texi *p, enum texicmd cmd,
1328: const char *buf, size_t sz, size_t *pos)
1329: {
1330:
1.12 kristaps 1331: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1332: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1333: }
1334:
1.8 kristaps 1335: /*
1336: * Parse colon-separated directories from "cp" (if not NULL) and returns
1337: * the array of pointers.
1338: * Prepends "base" to the array.
1339: * This does NOT sanitise the directories!
1340: */
1.5 kristaps 1341: static char **
1342: parsedirs(const char *base, const char *cp, size_t *sz)
1343: {
1344: char *tok, *str, *tofree;
1345: const char *cpp;
1346: size_t i;
1347: char **dirs;
1348:
1349: *sz = NULL != (cpp = cp) ? 2 : 1;
1350: if (*sz > 1)
1351: for ( ; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1352: cpp++;
1353:
1354: dirs = calloc(*sz, sizeof(char *));
1355: if (NULL == dirs) {
1356: perror(NULL);
1357: exit(EXIT_FAILURE);
1358: } else if (NULL == (dirs[0] = strdup(base))) {
1359: perror(NULL);
1360: exit(EXIT_FAILURE);
1361: }
1362:
1363: if (NULL == cp)
1364: return(dirs);
1365:
1366: if (NULL == (tofree = tok = str = strdup(cp))) {
1367: perror(NULL);
1368: exit(EXIT_FAILURE);
1369: }
1370:
1371: for (i = 1; NULL != (tok = strsep(&str, ":")); i++)
1372: if (NULL == (dirs[i] = strdup(tok))) {
1373: perror(NULL);
1374: exit(EXIT_FAILURE);
1375: }
1376:
1377: free(tofree);
1378: return(dirs);
1379: }
1380:
1.1 kristaps 1381: int
1382: main(int argc, char *argv[])
1383: {
1384: struct texi texi;
1.2 kristaps 1385: int c;
1386: char *path, *dir;
1.10 kristaps 1387: const char *progname, *Idir, *cp;
1.1 kristaps 1388:
1389: progname = strrchr(argv[0], '/');
1390: if (progname == NULL)
1391: progname = argv[0];
1392: else
1393: ++progname;
1394:
1.10 kristaps 1395: memset(&texi, 0, sizeof(struct texi));
1.5 kristaps 1396: Idir = NULL;
1.10 kristaps 1397:
1.5 kristaps 1398: while (-1 != (c = getopt(argc, argv, "I:")))
1.1 kristaps 1399: switch (c) {
1.5 kristaps 1400: case ('I'):
1401: Idir = optarg;
1402: break;
1.1 kristaps 1403: default:
1404: goto usage;
1405: }
1406:
1407: argv += optind;
1408: if (0 == (argc -= optind))
1409: goto usage;
1410:
1.2 kristaps 1411: if (NULL == (path = strdup(argv[0]))) {
1412: perror(NULL);
1413: exit(EXIT_FAILURE);
1414: } else if (NULL == (dir = dirname(path))) {
1415: perror(argv[0]);
1416: free(path);
1417: exit(EXIT_FAILURE);
1418: }
1419: free(path);
1420:
1.10 kristaps 1421: if (NULL != (cp = strrchr(argv[0], '/')))
1422: texi.title = strdup(cp + 1);
1423: else
1424: texi.title = strdup(argv[0]);
1425:
1426: if (NULL == texi.title) {
1427: perror(NULL);
1428: exit(EXIT_FAILURE);
1429: } else if (NULL != (path = strchr(texi.title, '.')))
1430: *path = '\0';
1431:
1.3 kristaps 1432: texi.ign = 1;
1.5 kristaps 1433: texi.dirs = parsedirs(dir, Idir, &texi.dirsz);
1.16 kristaps 1434: parsefile(&texi, argv[0], 1);
1.5 kristaps 1435: /* We shouldn't get here. */
1.2 kristaps 1436: texiexit(&texi);
1437: return(EXIT_FAILURE);
1.1 kristaps 1438: usage:
1.8 kristaps 1439: fprintf(stderr, "usage: %s [-Idirs] file\n", progname);
1.1 kristaps 1440: return(EXIT_FAILURE);
1441: }
CVSweb