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