Annotation of texi2mdoc/main.c, Revision 1.20
1.20 ! kristaps 1: /* $Id: main.c,v 1.19 2015/02/19 15:40:44 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:
33: /*
34: * This defines each one of the Texinfo commands that we understand.
35: * Obviously this only refers to native commands; overriden names are a
36: * different story.
37: */
38: enum texicmd {
1.2 kristaps 39: TEXICMD_ACRONYM,
1.16 kristaps 40: TEXICMD_ACUTE,
1.1 kristaps 41: TEXICMD_A4PAPER,
42: TEXICMD_ANCHOR,
1.2 kristaps 43: TEXICMD_APPENDIX,
44: TEXICMD_APPENDIXSEC,
1.20 ! kristaps 45: TEXICMD_APPENDIXSUBSEC,
1.3 kristaps 46: TEXICMD_ASTERISK,
1.1 kristaps 47: TEXICMD_AT,
1.3 kristaps 48: TEXICMD_AUTHOR,
1.12 kristaps 49: TEXICMD_B,
1.3 kristaps 50: TEXICMD_BANG,
1.7 kristaps 51: TEXICMD_BULLET,
1.1 kristaps 52: TEXICMD_BYE,
1.5 kristaps 53: TEXICMD_CENTER,
1.1 kristaps 54: TEXICMD_CHAPTER,
55: TEXICMD_CINDEX,
1.16 kristaps 56: TEXICMD_CIRCUMFLEX,
1.3 kristaps 57: TEXICMD_CITE,
1.1 kristaps 58: TEXICMD_CODE,
1.3 kristaps 59: TEXICMD_COLON,
1.18 kristaps 60: TEXICMD_COLUMNFRACTIONS,
1.1 kristaps 61: TEXICMD_COMMAND,
62: TEXICMD_COMMENT,
1.2 kristaps 63: TEXICMD_COMMENT_LONG,
1.1 kristaps 64: TEXICMD_CONTENTS,
65: TEXICMD_COPYING,
66: TEXICMD_COPYRIGHT,
1.15 kristaps 67: TEXICMD_DEFFN,
68: TEXICMD_DEFFNX,
69: TEXICMD_DEFMAC,
70: TEXICMD_DEFMACX,
71: TEXICMD_DEFTP,
72: TEXICMD_DEFTPX,
1.3 kristaps 73: TEXICMD_DEFTYPEFN,
74: TEXICMD_DEFTYPEFNX,
75: TEXICMD_DEFTYPEFUN,
76: TEXICMD_DEFTYPEFUNX,
77: TEXICMD_DEFTYPEVAR,
1.15 kristaps 78: TEXICMD_DEFTYPEVARX,
1.3 kristaps 79: TEXICMD_DEFTYPEVR,
1.15 kristaps 80: TEXICMD_DEFTYPEVRX,
81: TEXICMD_DEFUN,
82: TEXICMD_DEFUNX,
83: TEXICMD_DEFVAR,
84: TEXICMD_DEFVARX,
85: TEXICMD_DEFVR,
86: TEXICMD_DEFVRX,
1.1 kristaps 87: TEXICMD_DETAILMENU,
1.3 kristaps 88: TEXICMD_DFN,
1.1 kristaps 89: TEXICMD_DIRCATEGORY,
90: TEXICMD_DIRENTRY,
1.3 kristaps 91: TEXICMD_DISPLAY,
1.2 kristaps 92: TEXICMD_DOTS,
1.1 kristaps 93: TEXICMD_EMAIL,
94: TEXICMD_EMPH,
95: TEXICMD_END,
1.2 kristaps 96: TEXICMD_ENUMERATE,
1.3 kristaps 97: TEXICMD_ENV,
1.15 kristaps 98: TEXICMD_ERROR,
1.1 kristaps 99: TEXICMD_EXAMPLE,
1.17 kristaps 100: TEXICMD_EXPANSION,
1.1 kristaps 101: TEXICMD_FILE,
1.17 kristaps 102: TEXICMD_FINALOUT,
1.20 ! kristaps 103: TEXICMD_FINDEX,
! 104: TEXICMD_FTABLE,
! 105: TEXICMD_FORMAT,
1.16 kristaps 106: TEXICMD_GRAVE,
1.3 kristaps 107: TEXICMD_GROUP,
1.2 kristaps 108: TEXICMD_HEADING,
1.3 kristaps 109: TEXICMD_HEADINGS,
1.18 kristaps 110: TEXICMD_HEADITEM,
1.3 kristaps 111: TEXICMD_HYPHEN,
1.1 kristaps 112: TEXICMD_I,
1.3 kristaps 113: TEXICMD_IFCLEAR,
1.14 kristaps 114: TEXICMD_IFDOCBOOK,
1.1 kristaps 115: TEXICMD_IFHTML,
1.3 kristaps 116: TEXICMD_IFINFO,
1.14 kristaps 117: TEXICMD_IFNOTDOCBOOK,
118: TEXICMD_IFNOTHTML,
119: TEXICMD_IFNOTINFO,
120: TEXICMD_IFNOTPLAINTEXT,
1.1 kristaps 121: TEXICMD_IFNOTTEX,
1.14 kristaps 122: TEXICMD_IFNOTXML,
123: TEXICMD_IFPLAINTEXT,
1.1 kristaps 124: TEXICMD_IFTEX,
1.3 kristaps 125: TEXICMD_IFSET,
1.14 kristaps 126: TEXICMD_IFXML,
1.17 kristaps 127: TEXICMD_IGNORE,
1.1 kristaps 128: TEXICMD_IMAGE,
1.2 kristaps 129: TEXICMD_INCLUDE,
1.13 kristaps 130: TEXICMD_INDENTBLOCK,
1.5 kristaps 131: TEXICMD_INSERTCOPYING,
1.1 kristaps 132: TEXICMD_ITEM,
133: TEXICMD_ITEMIZE,
1.20 ! kristaps 134: TEXICMD_ITEMX,
! 135: TEXICMD_KBD,
1.18 kristaps 136: TEXICMD_KEY,
1.20 ! kristaps 137: TEXICMD_KINDEX,
1.1 kristaps 138: TEXICMD_LATEX,
1.3 kristaps 139: TEXICMD_MATH,
1.1 kristaps 140: TEXICMD_MENU,
1.18 kristaps 141: TEXICMD_MULTITABLE,
1.15 kristaps 142: TEXICMD_NEED,
1.3 kristaps 143: TEXICMD_NEWLINE,
1.1 kristaps 144: TEXICMD_NODE,
1.3 kristaps 145: TEXICMD_NOINDENT,
1.16 kristaps 146: TEXICMD_OPTION,
1.8 kristaps 147: TEXICMD_PXREF,
1.3 kristaps 148: TEXICMD_QUESTIONMARK,
1.1 kristaps 149: TEXICMD_QUOTATION,
1.3 kristaps 150: TEXICMD_PAGE,
1.1 kristaps 151: TEXICMD_PARINDENT,
1.2 kristaps 152: TEXICMD_PRINTINDEX,
1.12 kristaps 153: TEXICMD_R,
1.1 kristaps 154: TEXICMD_REF,
1.15 kristaps 155: TEXICMD_RESULT,
1.1 kristaps 156: TEXICMD_SAMP,
1.12 kristaps 157: TEXICMD_SANSSERIF,
1.7 kristaps 158: TEXICMD_SC,
1.1 kristaps 159: TEXICMD_SECTION,
1.3 kristaps 160: TEXICMD_SET,
1.1 kristaps 161: TEXICMD_SETCHAPNEWPAGE,
162: TEXICMD_SETFILENAME,
163: TEXICMD_SETTITLE,
1.12 kristaps 164: TEXICMD_SLANTED,
1.3 kristaps 165: TEXICMD_SP,
166: TEXICMD_SPACE,
1.17 kristaps 167: TEXICMD_SMALLBOOK,
1.12 kristaps 168: TEXICMD_SMALLDISPLAY,
1.3 kristaps 169: TEXICMD_SMALLEXAMPLE,
1.20 ! kristaps 170: TEXICMD_SMALLFORMAT,
1.13 kristaps 171: TEXICMD_SMALLINDENTBLOCK,
1.3 kristaps 172: TEXICMD_SQUIGGLE_LEFT,
173: TEXICMD_SQUIGGLE_RIGHT,
1.8 kristaps 174: TEXICMD_STRONG,
1.20 ! kristaps 175: TEXICMD_SUBHEADING,
1.1 kristaps 176: TEXICMD_SUBSECTION,
1.3 kristaps 177: TEXICMD_SUBTITLE,
1.20 ! kristaps 178: TEXICMD_SYNCODEINDEX,
1.12 kristaps 179: TEXICMD_T,
1.3 kristaps 180: TEXICMD_TAB,
1.18 kristaps 181: TEXICMD_TABSYM,
1.1 kristaps 182: TEXICMD_TABLE,
183: TEXICMD_TEX,
184: TEXICMD_TEXSYM,
1.16 kristaps 185: TEXICMD_TILDE,
1.3 kristaps 186: TEXICMD_TITLE,
1.1 kristaps 187: TEXICMD_TITLEFONT,
188: TEXICMD_TITLEPAGE,
189: TEXICMD_TOP,
1.16 kristaps 190: TEXICMD_UMLAUT,
1.1 kristaps 191: TEXICMD_UNNUMBERED,
1.2 kristaps 192: TEXICMD_UNNUMBEREDSEC,
1.20 ! kristaps 193: TEXICMD_UNNUMBEREDSUBSEC,
1.3 kristaps 194: TEXICMD_UREF,
1.1 kristaps 195: TEXICMD_URL,
196: TEXICMD_VAR,
1.16 kristaps 197: TEXICMD_VERBATIMINCLUDE,
1.18 kristaps 198: TEXICMD_VINDEX,
1.9 kristaps 199: TEXICMD_VSKIP,
1.20 ! kristaps 200: TEXICMD_VTABLE,
1.3 kristaps 201: TEXICMD_W,
1.8 kristaps 202: TEXICMD_XREF,
1.1 kristaps 203: TEXICMD__MAX
204: };
205:
206: /*
207: * The file currently being parsed.
208: * This keeps track of our location within that file.
209: */
210: struct texifile {
211: const char *name; /* name of the file */
212: size_t line; /* current line (from zero) */
213: size_t col; /* current column in line (from zero) */
214: char *map; /* mmap'd file */
215: size_t mapsz; /* size of mmap */
216: };
217:
218: struct texi;
219:
1.2 kristaps 220: /*
221: * Callback for functions implementing texi commands.
222: */
1.1 kristaps 223: typedef void (*texicmdfp)(struct texi *,
224: enum texicmd, const char *, size_t, size_t *);
225:
226: /*
227: * Describes Texinfo commands, whether native or overriden.
228: */
229: struct texitok {
230: texicmdfp fp; /* callback (or NULL if none) */
231: const char *tok; /* name of the token */
232: size_t len; /* strlen(tok) */
233: };
234:
1.3 kristaps 235: enum texilist {
236: TEXILIST_NONE = 0,
237: TEXILIST_ITEM,
238: TEXILIST_NOITEM,
1.18 kristaps 239: TEXILIST_TABLE
1.3 kristaps 240: };
241:
1.1 kristaps 242: /*
243: * The main parse structure.
244: * This keeps any necessary information handy.
245: */
246: struct texi {
1.5 kristaps 247: struct texifile files[64]; /* stack of open files */
248: size_t filepos; /* number of open files */
249: size_t outcol; /* column in output line */
250: char **dirs; /* texi directories */
251: size_t dirsz; /* number of texi directories */
1.11 kristaps 252: char *title; /* title of document */
253: char *subtitle; /* subtitle of document */
254: /*
255: * The following control what we output to the screen.
256: * The complexity is required to accomodate for mdoc(7).
257: */
1.8 kristaps 258: enum texilist list; /* current list (set recursively) */
259: int outmacro; /* if >0, output is in line macro */
260: int seenws; /* ws has been seen (and ignored) */
1.11 kristaps 261: int seenvs; /* newline has been Pp'd */
1.8 kristaps 262: int ign; /* if >0, don't print anything */
263: int literal; /* if >0, literal context */
1.1 kristaps 264: };
265:
1.8 kristaps 266: /* Texi disregards spaces and tabs. */
1.2 kristaps 267: #define isws(_x) \
268: (' ' == (_x) || '\t' == (_x))
1.9 kristaps 269: #define ismspace(_x) \
1.10 kristaps 270: (isws((_x)) || '\n' == (_x))
1.1 kristaps 271:
1.16 kristaps 272: static void doaccent(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 273: static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 274: static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
275: static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.15 kristaps 276: static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 277: static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 278: static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 279: static void doexample(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12 kristaps 280: static void dofont(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 281: static void doignargn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 282: static void doignblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
283: static void doignbracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
284: static void doignline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12 kristaps 285: static void doinline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2 kristaps 286: static void doinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 287: static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *);
288: static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8 kristaps 289: static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 290: static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18 kristaps 291: static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 292: static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *);
293: static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *);
294: static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *);
295: static void dosection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3 kristaps 296: static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 297: static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
298: static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18 kristaps 299: static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.10 kristaps 300: static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.16 kristaps 301: static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1 kristaps 302:
303: static const struct texitok texitoks[TEXICMD__MAX] = {
1.20 ! kristaps 304: /* TEXICMD__BEGIN */
1.8 kristaps 305: { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */
1.16 kristaps 306: { doaccent, "'", 1 }, /* TEXICMD_ACUTE */
1.1 kristaps 307: { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */
308: { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */
1.12 kristaps 309: { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */
310: { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */
1.20 ! kristaps 311: { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */
1.3 kristaps 312: { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */
1.1 kristaps 313: { dosymbol, "@", 1 }, /* TEXICMD_AT */
1.3 kristaps 314: { doignline, "author", 6 }, /* TEXICMD_AUTHOR */
1.12 kristaps 315: { dofont, "b", 1 }, /* TEXICMD_BOLD */
1.3 kristaps 316: { dosymbol, "!", 1 }, /* TEXICMD_BANG */
1.7 kristaps 317: { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */
1.1 kristaps 318: { dobye, "bye", 3 }, /* TEXICMD_BYE */
1.12 kristaps 319: { doignline, "center", 6 }, /* TEXICMD_CENTER */
320: { dosection, "chapter", 7 }, /* TEXICMD_CHAPTER */
1.1 kristaps 321: { doignline, "cindex", 6 }, /* TEXICMD_CINDEX */
1.16 kristaps 322: { doaccent, "^", 1 }, /* TEXICMD_CIRCUMFLEX */
1.12 kristaps 323: { dofont, "code", 4 }, /* TEXICMD_CODE */
324: { dofont, "cite", 4 }, /* TEXICMD_CITE */
1.3 kristaps 325: { dosymbol, ":", 1 }, /* TEXICMD_COLON */
1.18 kristaps 326: { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */
1.12 kristaps 327: { doinline, "command", 7 }, /* TEXICMD_COMMAND */
1.1 kristaps 328: { doignline, "c", 1 }, /* TEXICMD_COMMENT */
1.2 kristaps 329: { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */
1.1 kristaps 330: { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */
331: { doignblock, "copying", 7 }, /* TEXICMD_COPYING */
332: { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */
1.15 kristaps 333: { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */
334: { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */
335: { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */
336: { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */
337: { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */
338: { dodefn, "deftpx", 6 }, /* TEXICMD_DEFTPX */
339: { dodefn, "deftypefn", 9 }, /* TEXICMD_DEFTYPEFN */
340: { dodefn, "deftypefnx", 10 }, /* TEXICMD_DEFTYPEFNX */
341: { dodefn, "deftypefun", 10 }, /* TEXICMD_DEFTYPEFUN */
342: { dodefn, "deftypefunx", 11 }, /* TEXICMD_DEFTYPEFUNX */
343: { dodefn, "deftypevar", 10 }, /* TEXICMD_DEFTYPEVAR */
344: { dodefn, "deftypevarx", 11 }, /* TEXICMD_DEFTYPEVARX */
345: { dodefn, "deftypevr", 9 }, /* TEXICMD_DEFTYPEVR */
346: { dodefn, "deftypevrx", 10 }, /* TEXICMD_DEFTYPEVRX */
347: { dodefn, "defun", 5 }, /* TEXICMD_DEFUN */
348: { dodefn, "defunx", 6 }, /* TEXICMD_DEFUNX */
349: { dodefn, "defvar", 6 }, /* TEXICMD_DEFVAR */
350: { dodefn, "defvarx", 7 }, /* TEXICMD_DEFVARX */
351: { dodefn, "defvr", 5 }, /* TEXICMD_DEFVR */
352: { dodefn, "defvrx", 6 }, /* TEXICMD_DEFVRX */
1.1 kristaps 353: { doignblock, "detailmenu", 10 }, /* TEXICMD_DETAILMENU */
1.12 kristaps 354: { dofont, "dfn", 3 }, /* TEXICMD_DFN */
1.1 kristaps 355: { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */
356: { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */
1.3 kristaps 357: { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */
1.2 kristaps 358: { dosymbol, "dots", 4 }, /* TEXICMD_DOTS */
1.8 kristaps 359: { dolink, "email", 5 }, /* TEXICMD_EMAIL */
1.12 kristaps 360: { dofont, "emph", 4 }, /* TEXICMD_EMPH */
1.1 kristaps 361: { NULL, "end", 3 }, /* TEXICMD_END */
1.2 kristaps 362: { doenumerate, "enumerate", 9 }, /* TEXICMD_ENUMERATE */
1.12 kristaps 363: { doinline, "env", 3 }, /* TEXICMD_ENV */
1.15 kristaps 364: { dosymbol, "error", 5 }, /* TEXICMD_ERROR */
1.1 kristaps 365: { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */
1.17 kristaps 366: { dosymbol, "expansion", 9 }, /* TEXICMD_EXPANSION */
1.12 kristaps 367: { doinline, "file", 4 }, /* TEXICMD_FILE */
1.17 kristaps 368: { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */
1.20 ! kristaps 369: { doignline, "findex", 6 }, /* TEXICMD_FINDEX */
! 370: { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */
! 371: { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */
1.16 kristaps 372: { doaccent, "`", 1 }, /* TEXICMD_GRAVE */
1.3 kristaps 373: { doblock, "group", 5 }, /* TEXICMD_GROUP */
1.2 kristaps 374: { dosection, "heading", 7 }, /* TEXICMD_HEADING */
1.3 kristaps 375: { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */
1.18 kristaps 376: { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */
1.3 kristaps 377: { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */
1.12 kristaps 378: { dofont, "i", 1 }, /* TEXICMD_I */
1.3 kristaps 379: { doignblock, "ifclear", 7 }, /* TEXICMD_IFCLEAR */
1.14 kristaps 380: { doignblock, "ifdocbook", 9 }, /* TEXICMD_IFDOCBOOK */
1.1 kristaps 381: { doignblock, "ifhtml", 6 }, /* TEXICMD_IFHTML */
1.3 kristaps 382: { doignblock, "ifinfo", 6 }, /* TEXICMD_IFINFO */
1.14 kristaps 383: { doblock, "ifnotdocbook", 12 }, /* TEXICMD_IFNOTDOCBOOK */
384: { doblock, "ifnothtml", 9 }, /* TEXICMD_IFNOTHTML */
385: { doblock, "ifnotinfo", 9 }, /* TEXICMD_IFNOTINFO */
386: { doignblock, "ifnotplaintext", 14 }, /* TEXICMD_IFNOTPLAINTEXT */
1.3 kristaps 387: { doblock, "ifnottex", 8 }, /* TEXICMD_IFNOTTEX */
1.14 kristaps 388: { doblock, "ifnotxml", 8 }, /* TEXICMD_IFNOTXML */
389: { doblock, "ifplaintext", 11 }, /* TEXICMD_IFPLAINTEXT */
1.1 kristaps 390: { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */
1.3 kristaps 391: { doignblock, "ifset", 5 }, /* TEXICMD_IFSET */
1.14 kristaps 392: { doignblock, "ifxml", 5 }, /* TEXICMD_IFXML */
1.17 kristaps 393: { doignblock, "ignore", 6 }, /* TEXICMD_IGNORE */
1.1 kristaps 394: { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */
1.2 kristaps 395: { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */
1.13 kristaps 396: { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */
1.5 kristaps 397: { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */
1.1 kristaps 398: { doitem, "item", 4 }, /* TEXICMD_ITEM */
399: { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */
1.20 ! kristaps 400: { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */
! 401: { dofont, "kbd", 3 }, /* TEXICMD_KBD */
1.18 kristaps 402: { dobracket, "key", 3 }, /* TEXICMD_KEY */
1.20 ! kristaps 403: { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */
1.1 kristaps 404: { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */
1.3 kristaps 405: { domath, "math", 4 }, /* TEXICMD_MATH */
1.1 kristaps 406: { doignblock, "menu", 4 }, /* TEXICMD_MENU */
1.18 kristaps 407: { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */
1.15 kristaps 408: { doignline, "need", 4 }, /* TEXICMD_NEED */
1.3 kristaps 409: { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */
1.1 kristaps 410: { doignline, "node", 4 }, /* TEXICMD_NODE */
1.3 kristaps 411: { doignline, "noindent", 8 }, /* TEXICMD_NOINDENT */
1.16 kristaps 412: { doinline, "option", 6 }, /* TEXICMD_OPTION */
1.8 kristaps 413: { dolink, "pxref", 5 }, /* TEXICMD_PXREF */
1.3 kristaps 414: { dosymbol, "?", 1 }, /* TEXICMD_QUESTIONMARK */
1.1 kristaps 415: { doquotation, "quotation", 9 }, /* TEXICMD_QUOTATION */
1.3 kristaps 416: { doignline, "page", 4 }, /* TEXICMD_PAGE */
417: { doignline, "paragraphindent", 14 }, /* TEXICMD_PARINDENT */
1.2 kristaps 418: { doignline, "printindex", 10 }, /* TEXICMD_PRINTINDEX */
1.12 kristaps 419: { dofont, "r", 1 }, /* TEXICMD_R */
1.1 kristaps 420: { dobracket, "ref", 3 }, /* TEXICMD_REF */
1.15 kristaps 421: { dosymbol, "result", 6 }, /* TEXICMD_RESULT */
1.12 kristaps 422: { dofont, "samp", 4 }, /* TEXICMD_SAMP */
423: { dofont, "sansserif", 9 }, /* TEXICMD_SANSSERIF */
1.7 kristaps 424: { dobracket, "sc", 2 }, /* TEXICMD_SC */
1.1 kristaps 425: { dosection, "section", 7 }, /* TEXICMD_SECTION */
1.3 kristaps 426: { doignline, "set", 3 }, /* TEXICMD_SET */
1.1 kristaps 427: { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */
428: { doignline, "setfilename", 11 }, /* TEXICMD_SETFILENAME */
1.10 kristaps 429: { dotitle, "settitle", 8 }, /* TEXICMD_SETTITLE */
1.12 kristaps 430: { dofont, "slanted", 7 }, /* TEXICMD_SLANTED */
1.3 kristaps 431: { dosp, "sp", 2 }, /* TEXICMD_SP */
432: { dosymbol, " ", 1 }, /* TEXICMD_SPACE */
1.17 kristaps 433: { doignline, "smallbook", 9 }, /* TEXICMD_SMALLBOOK */
1.12 kristaps 434: { dodisplay, "smalldisplay", 12 }, /* TEXICMD_SMALLDISPLAY */
1.3 kristaps 435: { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */
1.20 ! kristaps 436: { dodisplay, "smallformat", 11 }, /* TEXICMD_SMALLFORMAT */
1.13 kristaps 437: { dodisplay, "smallindentblock", 16 }, /* TEXICMD_SMALLINDENTBLOCK */
1.3 kristaps 438: { dosymbol, "{", 1 }, /* TEXICMD_SQUIGGLE_LEFT */
439: { dosymbol, "}", 1 }, /* TEXICMD_SQUIGGLE_RIGHT */
1.12 kristaps 440: { dofont, "strong", 6 }, /* TEXICMD_STRONG */
1.20 ! kristaps 441: { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */
1.1 kristaps 442: { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */
1.3 kristaps 443: { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */
1.20 ! kristaps 444: { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */
1.12 kristaps 445: { dofont, "t", 1 }, /* TEXICMD_T */
1.18 kristaps 446: { dotab, "tab", 3 }, /* TEXICMD_TAB */
447: { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */
1.1 kristaps 448: { dotable, "table", 5 }, /* TEXICMD_TABLE */
449: { doignblock, "tex", 3 }, /* TEXICMD_TEX */
450: { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */
1.16 kristaps 451: { doaccent, "~", 1 }, /* TEXICMD_TILDE */
1.3 kristaps 452: { doignline, "title", 5 }, /* TEXICMD_TITLE */
1.1 kristaps 453: { dobracket, "titlefont", 9 }, /* TEXICMD_TITLEFONT */
454: { doignblock, "titlepage", 9 }, /* TEXICMD_TITLEPAGE */
455: { dotop, "top", 3 }, /* TEXICMD_TOP */
1.16 kristaps 456: { doaccent, "\"", 1 }, /* TEXICMD_UMLAUT */
1.12 kristaps 457: { dosection, "unnumbered", 10 }, /* TEXICMD_UNNUMBERED */
1.2 kristaps 458: { dosection, "unnumberedsec", 13 }, /* TEXICMD_UNNUMBEREDSEC */
1.20 ! kristaps 459: { dosubsection, "unnumberedsubsec", 16 }, /* TEXICMD_UNNUMBEREDSUBSEC */
1.8 kristaps 460: { dolink, "uref", 4 }, /* TEXICMD_UREF */
461: { dolink, "url", 3 }, /* TEXICMD_URL */
1.12 kristaps 462: { doinline, "var", 3 }, /* TEXICMD_VAR */
1.16 kristaps 463: { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */
1.18 kristaps 464: { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */
1.9 kristaps 465: { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */
1.20 ! kristaps 466: { dotable, "vtable", 6 }, /* TEXICMD_VTABLE */
1.3 kristaps 467: { dobracket, "w", 1 }, /* TEXICMD_W */
1.8 kristaps 468: { dolink, "xref", 4 }, /* TEXICMD_XREF */
1.20 ! kristaps 469: /* TEXICMD__END */
1.1 kristaps 470: };
471:
1.2 kristaps 472: /*
473: * Unmap the top-most file that we're using.
474: */
1.1 kristaps 475: static void
476: texifilepop(struct texi *p)
477: {
478: struct texifile *f;
479:
480: assert(p->filepos > 0);
481: f = &p->files[--p->filepos];
482: munmap(f->map, f->mapsz);
483: }
484:
1.2 kristaps 485: /*
1.8 kristaps 486: * Unmap all files that we're currently using and free all resources
487: * that we've allocated during the parse.
1.2 kristaps 488: * The utility should exit(...) after this is called.
489: */
1.1 kristaps 490: static void
491: texiexit(struct texi *p)
492: {
1.5 kristaps 493: size_t i;
494:
495: if (p->outcol)
496: putchar('\n');
1.1 kristaps 497:
498: while (p->filepos > 0)
499: texifilepop(p);
1.5 kristaps 500:
501: for (i = 0; i < p->dirsz; i++)
502: free(p->dirs[i]);
1.10 kristaps 503:
1.5 kristaps 504: free(p->dirs);
1.10 kristaps 505: free(p->subtitle);
506: free(p->title);
1.1 kristaps 507: }
508:
1.2 kristaps 509: /*
510: * Fatal error: unmap all files and exit.
511: * The "errstring" is passed to perror(3).
512: */
1.1 kristaps 513: static void
1.2 kristaps 514: texiabort(struct texi *p, const char *errstring)
1.1 kristaps 515: {
516:
517: perror(errstring);
518: texiexit(p);
519: exit(EXIT_FAILURE);
520: }
521:
522: /*
523: * Print a generic warning message (to stderr) tied to our current
524: * location in the parse sequence.
525: */
526: static void
527: texiwarn(const struct texi *p, const char *fmt, ...)
528: {
529: va_list ap;
530:
1.2 kristaps 531: fprintf(stderr, "%s:%zu:%zu: warning: ",
1.1 kristaps 532: p->files[p->filepos - 1].name,
533: p->files[p->filepos - 1].line + 1,
534: p->files[p->filepos - 1].col + 1);
535: va_start(ap, fmt);
536: vfprintf(stderr, fmt, ap);
537: va_end(ap);
538: fputc('\n', stderr);
539: }
540:
1.8 kristaps 541: /*
542: * Print an error message (to stderr) tied to our current location in
543: * the parse sequence, invoke texiexit(), then die.
544: */
1.2 kristaps 545: static void
546: texierr(struct texi *p, const char *fmt, ...)
547: {
548: va_list ap;
549:
550: fprintf(stderr, "%s:%zu:%zu: error: ",
551: p->files[p->filepos - 1].name,
552: p->files[p->filepos - 1].line + 1,
553: p->files[p->filepos - 1].col + 1);
554: va_start(ap, fmt);
555: vfprintf(stderr, fmt, ap);
556: va_end(ap);
557: fputc('\n', stderr);
558: texiexit(p);
559: exit(EXIT_FAILURE);
560: }
561:
1.1 kristaps 562: /*
1.8 kristaps 563: * Put a single data character to the output if we're not ignoring.
564: * Adjusts our output status.
1.18 kristaps 565: * This shouldn't be called for macros: just for ordinary text.
1.1 kristaps 566: */
567: static void
568: texiputchar(struct texi *p, char c)
569: {
570:
1.3 kristaps 571: if (p->ign)
1.1 kristaps 572: return;
1.18 kristaps 573:
574: if ('.' == c && 0 == p->outcol)
575: fputs("\\&", stdout);
576:
1.1 kristaps 577: putchar(c);
1.11 kristaps 578: p->seenvs = 0;
1.1 kristaps 579: if ('\n' == c) {
580: p->outcol = 0;
581: p->seenws = 0;
582: } else
583: p->outcol++;
584: }
585:
586: /*
587: * Put multiple characters (see texiputchar()).
1.18 kristaps 588: * This shouldn't be called for macros: just for ordinary text.
1.1 kristaps 589: */
590: static void
591: texiputchars(struct texi *p, const char *s)
592: {
593:
594: while ('\0' != *s)
595: texiputchar(p, *s++);
596: }
597:
598: /*
1.8 kristaps 599: * Close an mdoc(7) macro opened with teximacroopen().
600: * If there are no more macros on the line, prints a newline.
1.1 kristaps 601: */
602: static void
1.3 kristaps 603: teximacroclose(struct texi *p)
604: {
605:
1.18 kristaps 606: if (p->ign)
607: return;
608:
609: if (0 == --p->outmacro) {
610: putchar('\n');
611: p->outcol = p->seenws = 0;
612: }
1.3 kristaps 613: }
614:
615: /*
1.8 kristaps 616: * Open a mdoc(7) macro.
617: * This is used for line macros, e.g., Qq [foo bar baz].
618: * It can be invoked for nested macros, e.g., Qq Li foo .
1.3 kristaps 619: */
620: static void
621: teximacroopen(struct texi *p, const char *s)
1.1 kristaps 622: {
1.18 kristaps 623: int rc;
624:
625: if (p->ign)
626: return;
627:
628: if (p->outcol && 0 == p->outmacro) {
629: putchar('\n');
630: p->outcol = 0;
631: }
1.1 kristaps 632:
1.5 kristaps 633: if (0 == p->outmacro)
1.18 kristaps 634: putchar('.');
1.5 kristaps 635: else
1.18 kristaps 636: putchar(' ');
637:
638: if (EOF != (rc = fputs(s, stdout)))
639: p->outcol += rc;
640:
641: putchar(' ');
642: p->outcol++;
1.3 kristaps 643: p->outmacro++;
1.5 kristaps 644: p->seenws = 0;
1.1 kristaps 645: }
646:
647: /*
1.8 kristaps 648: * Put a stadnalone mdoc(7) command with the trailing newline.
1.1 kristaps 649: */
650: static void
651: teximacro(struct texi *p, const char *s)
652: {
653:
1.18 kristaps 654: if (p->ign)
655: return;
656:
1.4 kristaps 657: if (p->outmacro)
658: texierr(p, "\"%s\" in open line scope!?", s);
1.18 kristaps 659: if (p->literal)
1.4 kristaps 660: texierr(p, "\"%s\" in a literal scope!?", s);
661:
1.1 kristaps 662: if (p->outcol)
1.18 kristaps 663: putchar('\n');
1.5 kristaps 664:
1.18 kristaps 665: putchar('.');
666: puts(s);
667: p->outcol = p->seenws = 0;
1.1 kristaps 668: }
669:
1.11 kristaps 670: static void
671: texivspace(struct texi *p)
672: {
673:
674: if (p->seenvs)
675: return;
676: teximacro(p, "Pp");
677: p->seenvs = 1;
678: }
679:
1.1 kristaps 680: /*
681: * Advance by a single byte in the input stream.
682: */
683: static void
684: advance(struct texi *p, const char *buf, size_t *pos)
685: {
686:
687: if ('\n' == buf[*pos]) {
688: p->files[p->filepos - 1].line++;
689: p->files[p->filepos - 1].col = 0;
690: } else
691: p->files[p->filepos - 1].col++;
692:
693: (*pos)++;
694: }
695:
1.11 kristaps 696: static void
697: texipunctuate(struct texi *p, const char *buf, size_t sz, size_t *pos)
698: {
699: size_t start, end;
700:
701: if (1 != p->outmacro)
702: return;
703:
704: for (start = end = *pos; end < sz; end++) {
705: switch (buf[end]) {
706: case (','):
707: case (')'):
708: case ('.'):
709: case ('"'):
710: case (':'):
711: case ('!'):
712: case ('?'):
713: continue;
714: default:
715: break;
716: }
717: break;
718: }
719: if (end == *pos)
720: return;
721: if (end + 1 == sz || ' ' == buf[end] || '\n' == buf[end]) {
722: for ( ; start < end; start++) {
723: texiputchar(p, ' ');
724: texiputchar(p, buf[start]);
725: advance(p, buf, pos);
726: }
727: }
728: }
729:
1.1 kristaps 730: /*
731: * Advance to the next non-whitespace word in the input stream.
732: * If we're in literal mode, then print all of the whitespace as we're
733: * doing so.
734: */
735: static size_t
736: advancenext(struct texi *p, const char *buf, size_t sz, size_t *pos)
737: {
738:
1.3 kristaps 739: if (p->literal) {
1.9 kristaps 740: while (*pos < sz && ismspace(buf[*pos])) {
1.5 kristaps 741: if (*pos && '\n' == buf[*pos] &&
742: '\\' == buf[*pos - 1])
743: texiputchar(p, 'e');
1.1 kristaps 744: texiputchar(p, buf[*pos]);
745: advance(p, buf, pos);
746: }
747: return(*pos);
748: }
749:
1.9 kristaps 750: while (*pos < sz && ismspace(buf[*pos])) {
1.1 kristaps 751: p->seenws = 1;
752: /*
753: * If it looks like we've printed a double-line, then
754: * output a paragraph.
755: * FIXME: this is stupid.
756: */
1.11 kristaps 757: if (*pos && '\n' == buf[*pos] && '\n' == buf[*pos - 1])
758: texivspace(p);
1.1 kristaps 759: advance(p, buf, pos);
760: }
761: return(*pos);
762: }
763:
764: /*
765: * Advance to the EOLN in the input stream.
766: */
767: static size_t
1.3 kristaps 768: advanceeoln(struct texi *p, const char *buf,
769: size_t sz, size_t *pos, int consumenl)
1.1 kristaps 770: {
771:
1.8 kristaps 772: /* FIXME: disregards @NEWLINE. */
1.1 kristaps 773: while (*pos < sz && '\n' != buf[*pos])
774: advance(p, buf, pos);
1.3 kristaps 775: if (*pos < sz && consumenl)
776: advance(p, buf, pos);
1.1 kristaps 777: return(*pos);
778: }
779:
780: /*
781: * Advance to position "end", which is an absolute position in the
782: * current buffer greater than or equal to the current position.
783: */
784: static void
785: advanceto(struct texi *p, const char *buf, size_t *pos, size_t end)
786: {
787:
788: assert(*pos <= end);
789: while (*pos < end)
790: advance(p, buf, pos);
791: }
792:
793: /*
794: * Output a free-form word in the input stream, progressing to the next
795: * command or white-space.
796: * This also will advance the input stream.
797: */
798: static void
1.8 kristaps 799: texiword(struct texi *p, const char *buf,
800: size_t sz, size_t *pos, char extra)
1.1 kristaps 801: {
802:
1.18 kristaps 803: if (p->seenws && 0 == p->outmacro && p->outcol > 72 && 0 == p->literal)
1.1 kristaps 804: texiputchar(p, '\n');
1.8 kristaps 805: /* FIXME: abstract this: we use it elsewhere. */
1.3 kristaps 806: if (p->seenws && p->outcol && 0 == p->literal)
1.1 kristaps 807: texiputchar(p, ' ');
808:
809: p->seenws = 0;
810:
1.9 kristaps 811: while (*pos < sz && ! ismspace(buf[*pos])) {
1.1 kristaps 812: switch (buf[*pos]) {
813: case ('@'):
814: case ('}'):
815: case ('{'):
816: return;
817: }
1.8 kristaps 818: if ('\0' != extra && buf[*pos] == extra)
819: return;
1.1 kristaps 820: if (*pos < sz - 1 &&
821: '`' == buf[*pos] &&
822: '`' == buf[*pos + 1]) {
823: texiputchars(p, "\\(lq");
824: advance(p, buf, pos);
825: } else if (*pos < sz - 1 &&
826: '\'' == buf[*pos] &&
827: '\'' == buf[*pos + 1]) {
828: texiputchars(p, "\\(rq");
829: advance(p, buf, pos);
830: } else
831: texiputchar(p, buf[*pos]);
832: advance(p, buf, pos);
833: }
834: }
835:
1.8 kristaps 836: /*
837: * Look up the command at position "pos" in the buffer, returning it (or
838: * TEXICMD__MAX if none found) and setting "end" to be the absolute
839: * index after the command name.
840: */
1.1 kristaps 841: static enum texicmd
842: texicmd(struct texi *p, const char *buf,
843: size_t pos, size_t sz, size_t *end)
844: {
845: size_t i, len;
846:
847: assert('@' == buf[pos]);
1.3 kristaps 848:
1.9 kristaps 849: if ((*end = pos) == sz)
850: return(TEXICMD__MAX);
851: else if ((*end = ++pos) == sz)
1.3 kristaps 852: return(TEXICMD__MAX);
853:
854: /* Alphabetic commands are special. */
855: if ( ! isalpha(buf[pos])) {
1.9 kristaps 856: if ((*end = pos + 1) == sz)
857: return(TEXICMD__MAX);
1.3 kristaps 858: for (i = 0; i < TEXICMD__MAX; i++) {
859: if (1 != texitoks[i].len)
860: continue;
861: if (0 == strncmp(texitoks[i].tok, &buf[pos], 1))
862: return(i);
863: }
864: texiwarn(p, "bad command: @%c", buf[pos]);
865: return(TEXICMD__MAX);
866: }
867:
1.9 kristaps 868: for (*end = pos; *end < sz && ! ismspace(buf[*end]); (*end)++)
1.3 kristaps 869: if ((*end > pos && ('@' == buf[*end] ||
870: '{' == buf[*end] || '}' == buf[*end])))
1.1 kristaps 871: break;
872:
873: len = *end - pos;
874: for (i = 0; i < TEXICMD__MAX; i++) {
875: if (len != texitoks[i].len)
876: continue;
877: if (0 == strncmp(texitoks[i].tok, &buf[pos], len))
878: return(i);
879: }
880:
1.3 kristaps 881: texiwarn(p, "bad command: @%.*s", (int)len, &buf[pos]);
1.1 kristaps 882: return(TEXICMD__MAX);
883: }
884:
1.8 kristaps 885: /*
886: * Parse an argument from a bracketed command, e.g., @url{foo, baz}.
887: * Num should be set to the argument we're currently parsing, although
888: * it suffixes for it to be zero or non-zero.
889: * This will return 1 if there are more arguments, 0 otherwise.
890: * This will stop (returning 0) in the event of EOF or if we're not at a
891: * bracket for the zeroth parse.
892: */
893: static int
894: parsearg(struct texi *p, const char *buf,
895: size_t sz, size_t *pos, size_t num)
896: {
897: size_t end;
898: enum texicmd cmd;
899:
1.9 kristaps 900: while (*pos < sz && ismspace(buf[*pos]))
1.8 kristaps 901: advance(p, buf, pos);
902: if (*pos == sz || (0 == num && '{' != buf[*pos]))
903: return(0);
904: if (0 == num)
905: advance(p, buf, pos);
906:
907: while ((*pos = advancenext(p, buf, sz, pos)) < sz) {
908: switch (buf[*pos]) {
909: case (','):
910: advance(p, buf, pos);
911: return(1);
912: case ('}'):
913: advance(p, buf, pos);
914: return(0);
915: case ('{'):
916: if (0 == p->ign)
917: texiwarn(p, "unexpected \"{\"");
918: advance(p, buf, pos);
919: continue;
920: case ('@'):
921: break;
922: default:
923: texiword(p, buf, sz, pos, ',');
924: continue;
925: }
926:
927: cmd = texicmd(p, buf, *pos, sz, &end);
928: advanceto(p, buf, pos, end);
929: if (TEXICMD__MAX == cmd)
930: continue;
931: if (NULL != texitoks[cmd].fp)
932: (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
933: }
934: return(0);
935: }
936:
937: /*
938: * Parse until the end of a bracketed statement, e.g., @foo{bar baz}.
939: * This will stop in the event of EOF or if we're not at a bracket.
940: */
1.1 kristaps 941: static void
942: parsebracket(struct texi *p, const char *buf, size_t sz, size_t *pos)
943: {
944: size_t end;
945: enum texicmd cmd;
946:
1.9 kristaps 947: while (*pos < sz && ismspace(buf[*pos]))
1.3 kristaps 948: advance(p, buf, pos);
949:
1.1 kristaps 950: if (*pos == sz || '{' != buf[*pos])
951: return;
952: advance(p, buf, pos);
953:
954: while ((*pos = advancenext(p, buf, sz, pos)) < sz) {
955: switch (buf[*pos]) {
956: case ('}'):
957: advance(p, buf, pos);
958: return;
959: case ('{'):
1.3 kristaps 960: if (0 == p->ign)
961: texiwarn(p, "unexpected \"{\"");
962: advance(p, buf, pos);
963: continue;
964: case ('@'):
965: break;
966: default:
1.8 kristaps 967: texiword(p, buf, sz, pos, '\0');
1.3 kristaps 968: continue;
969: }
970:
971: cmd = texicmd(p, buf, *pos, sz, &end);
972: advanceto(p, buf, pos, end);
973: if (TEXICMD__MAX == cmd)
974: continue;
975: if (NULL != texitoks[cmd].fp)
976: (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
977: }
978: }
979:
980: /*
981: * This should be invoked when we're on a macro line and want to process
982: * to the end of the current input line, doing all of our macros along
983: * the way.
984: */
985: static void
986: parseeoln(struct texi *p, const char *buf, size_t sz, size_t *pos)
987: {
988: size_t end;
989: enum texicmd cmd;
990:
991: while (*pos < sz && '\n' != buf[*pos]) {
992: while (*pos < sz && isws(buf[*pos])) {
993: p->seenws = 1;
1.19 kristaps 994: if (p->literal)
995: texiputchar(p, buf[*pos]);
1.3 kristaps 996: advance(p, buf, pos);
997: }
998: switch (buf[*pos]) {
999: case ('}'):
1000: if (0 == p->ign)
1001: texiwarn(p, "unexpected \"}\"");
1002: advance(p, buf, pos);
1003: continue;
1004: case ('{'):
1005: if (0 == p->ign)
1006: texiwarn(p, "unexpected \"{\"");
1.1 kristaps 1007: advance(p, buf, pos);
1008: continue;
1009: case ('@'):
1010: break;
1011: default:
1.8 kristaps 1012: texiword(p, buf, sz, pos, '\0');
1.1 kristaps 1013: continue;
1014: }
1015:
1016: cmd = texicmd(p, buf, *pos, sz, &end);
1017: advanceto(p, buf, pos, end);
1018: if (TEXICMD__MAX == cmd)
1019: continue;
1020: if (NULL != texitoks[cmd].fp)
1021: (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
1022: }
1023: }
1024:
1.8 kristaps 1025: /*
1026: * Parse a single word or command.
1027: * This will return immediately at the EOF.
1028: */
1.1 kristaps 1029: static void
1.3 kristaps 1030: parsesingle(struct texi *p, const char *buf, size_t sz, size_t *pos)
1031: {
1032: size_t end;
1033: enum texicmd cmd;
1034:
1035: if ((*pos = advancenext(p, buf, sz, pos)) >= sz)
1036: return;
1037:
1038: switch (buf[*pos]) {
1039: case ('}'):
1040: if (0 == p->ign)
1041: texiwarn(p, "unexpected \"}\"");
1042: advance(p, buf, pos);
1043: return;
1044: case ('{'):
1045: if (0 == p->ign)
1046: texiwarn(p, "unexpected \"{\"");
1047: advance(p, buf, pos);
1048: return;
1049: case ('@'):
1050: break;
1051: default:
1.8 kristaps 1052: texiword(p, buf, sz, pos, '\0');
1.3 kristaps 1053: return;
1054: }
1055:
1056: cmd = texicmd(p, buf, *pos, sz, &end);
1057: advanceto(p, buf, pos, end);
1058: if (TEXICMD__MAX == cmd)
1059: return;
1060: if (NULL != texitoks[cmd].fp)
1061: (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
1062: }
1063:
1.15 kristaps 1064: static int
1065: parselinearg(struct texi *p, const char *buf, size_t sz, size_t *pos)
1066: {
1067:
1068: while (*pos < sz && isws(buf[*pos])) {
1069: p->seenws = 1;
1070: advance(p, buf, pos);
1071: }
1072:
1073: if (*pos < sz && '{' == buf[*pos])
1074: parsebracket(p, buf, sz, pos);
1075: else if ('\n' != buf[*pos])
1076: parsesingle(p, buf, sz, pos);
1077: else
1078: return(0);
1079:
1080: return(1);
1081: }
1082:
1.8 kristaps 1083: /*
1084: * Parse til the end of the buffer.
1085: */
1.3 kristaps 1086: static void
1.7 kristaps 1087: parseeof(struct texi *p, const char *buf, size_t sz)
1088: {
1089: size_t pos;
1090:
1091: for (pos = 0; pos < sz; )
1092: parsesingle(p, buf, sz, &pos);
1093: }
1094:
1.8 kristaps 1095: /*
1096: * Parse a block sequence until we have the "@end endtoken" command
1097: * invocation.
1098: * This will return immediately at EOF.
1099: */
1.7 kristaps 1100: static void
1.1 kristaps 1101: parseto(struct texi *p, const char *buf,
1102: size_t sz, size_t *pos, const char *endtoken)
1103: {
1104: size_t end;
1105: enum texicmd cmd;
1106: size_t endtoksz;
1107:
1108: endtoksz = strlen(endtoken);
1109: assert(endtoksz > 0);
1110:
1111: while ((*pos = advancenext(p, buf, sz, pos)) < sz) {
1112: switch (buf[*pos]) {
1113: case ('}'):
1.3 kristaps 1114: if (0 == p->ign)
1115: texiwarn(p, "unexpected \"}\"");
1.1 kristaps 1116: advance(p, buf, pos);
1117: continue;
1118: case ('{'):
1.3 kristaps 1119: if (0 == p->ign)
1120: texiwarn(p, "unexpected \"{\"");
1.1 kristaps 1121: advance(p, buf, pos);
1122: continue;
1123: case ('@'):
1124: break;
1125: default:
1.8 kristaps 1126: texiword(p, buf, sz, pos, '\0');
1.1 kristaps 1127: continue;
1128: }
1129:
1130: cmd = texicmd(p, buf, *pos, sz, &end);
1131: advanceto(p, buf, pos, end);
1132: if (TEXICMD_END == cmd) {
1.2 kristaps 1133: while (*pos < sz && isws(buf[*pos]))
1.1 kristaps 1134: advance(p, buf, pos);
1135: /*
1.8 kristaps 1136: * FIXME: check the full word, not just its
1137: * initial substring!
1.1 kristaps 1138: */
1139: if (sz - *pos >= endtoksz && 0 == strncmp
1140: (&buf[*pos], endtoken, endtoksz)) {
1.3 kristaps 1141: advanceeoln(p, buf, sz, pos, 0);
1.1 kristaps 1142: break;
1143: }
1.3 kristaps 1144: if (0 == p->ign)
1145: texiwarn(p, "unexpected \"end\"");
1146: advanceeoln(p, buf, sz, pos, 0);
1.1 kristaps 1147: continue;
1148: } else if (TEXICMD__MAX != cmd)
1149: if (NULL != texitoks[cmd].fp)
1150: (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
1151: }
1152: }
1153:
1.8 kristaps 1154: /*
1155: * Memory-map the file "fname" and begin parsing it.
1156: * This can be called in a nested context.
1157: */
1.1 kristaps 1158: static void
1.16 kristaps 1159: parsefile(struct texi *p, const char *fname, int parse)
1.2 kristaps 1160: {
1.16 kristaps 1161: struct texifile *f;
1162: int fd;
1163: struct stat st;
1164: size_t i;
1.2 kristaps 1165:
1166: assert(p->filepos < 64);
1167: f = &p->files[p->filepos];
1168: memset(f, 0, sizeof(struct texifile));
1169:
1170: f->name = fname;
1171: if (-1 == (fd = open(fname, O_RDONLY, 0))) {
1172: texiabort(p, fname);
1173: } else if (-1 == fstat(fd, &st)) {
1174: close(fd);
1175: texiabort(p, fname);
1176: }
1177:
1178: f->mapsz = st.st_size;
1179: f->map = mmap(NULL, f->mapsz,
1180: PROT_READ, MAP_SHARED, fd, 0);
1181: close(fd);
1182:
1183: if (MAP_FAILED == f->map)
1184: texiabort(p, fname);
1185:
1186: p->filepos++;
1.16 kristaps 1187: if ( ! parse) {
1188: /*
1189: * We're printing verbatim output.
1190: * Make sure it doesn't get interpreted as mdoc by
1191: * escaping escapes and making sure leading dots don't
1192: * trigger mdoc(7) expansion.
1193: */
1194: for (i = 0; i < f->mapsz; i++) {
1195: if (i > 0 && '.' == f->map[i])
1196: if ('\n' == f->map[i - 1])
1197: fputs("\\&", stdout);
1198: putchar(f->map[i]);
1199: if ('\\' == f->map[i])
1200: putchar('e');
1201: }
1202: } else
1203: parseeof(p, f->map, f->mapsz);
1.2 kristaps 1204: texifilepop(p);
1205: }
1206:
1207: static void
1.15 kristaps 1208: dodefn(struct texi *p, enum texicmd cmd,
1.3 kristaps 1209: const char *buf, size_t sz, size_t *pos)
1210: {
1211: const char *blk;
1212:
1.5 kristaps 1213: blk = NULL;
1.3 kristaps 1214: switch (cmd) {
1.15 kristaps 1215: case (TEXICMD_DEFFN):
1216: case (TEXICMD_DEFTP):
1.3 kristaps 1217: case (TEXICMD_DEFTYPEFN):
1218: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 1219: case (TEXICMD_DEFTYPEVAR):
1220: case (TEXICMD_DEFTYPEVR):
1221: case (TEXICMD_DEFUN):
1222: case (TEXICMD_DEFVAR):
1223: case (TEXICMD_DEFVR):
1.5 kristaps 1224: blk = texitoks[cmd].tok;
1.3 kristaps 1225: break;
1.5 kristaps 1226: default:
1.3 kristaps 1227: break;
1228: }
1229:
1230: if (p->ign) {
1.15 kristaps 1231: NULL != blk ?
1232: parseto(p, buf, sz, pos, blk) :
1233: parseeoln(p, buf, sz, pos);
1.3 kristaps 1234: return;
1235: }
1236:
1.15 kristaps 1237: if (NULL != blk)
1238: texivspace(p);
1239:
1.3 kristaps 1240: switch (cmd) {
1.15 kristaps 1241: case (TEXICMD_DEFMAC):
1242: case (TEXICMD_DEFMACX):
1243: texiputchars(p, "Macro");
1244: break;
1245: case (TEXICMD_DEFTYPEVAR):
1246: case (TEXICMD_DEFTYPEVARX):
1247: case (TEXICMD_DEFVAR):
1248: case (TEXICMD_DEFVARX):
1249: texiputchars(p, "Variable");
1250: break;
1.3 kristaps 1251: case (TEXICMD_DEFTYPEFUN):
1.15 kristaps 1252: case (TEXICMD_DEFTYPEFUNX):
1253: case (TEXICMD_DEFUN):
1254: case (TEXICMD_DEFUNX):
1255: texiputchars(p, "Function");
1.3 kristaps 1256: break;
1257: default:
1.15 kristaps 1258: parselinearg(p, buf, sz, pos);
1.3 kristaps 1259: break;
1260: }
1.15 kristaps 1261:
1262: texiputchars(p, ":\n");
1263:
1264: switch (cmd) {
1265: case (TEXICMD_DEFMAC):
1266: case (TEXICMD_DEFMACX):
1267: teximacroopen(p, "Dv");
1268: while (parselinearg(p, buf, sz, pos))
1269: /* Spin. */ ;
1270: teximacroclose(p);
1271: break;
1272: case (TEXICMD_DEFFN):
1273: case (TEXICMD_DEFFNX):
1274: case (TEXICMD_DEFUN):
1275: case (TEXICMD_DEFUNX):
1276: teximacroopen(p, "Fo");
1277: parselinearg(p, buf, sz, pos);
1278: teximacroclose(p);
1279: teximacroopen(p, "Fa");
1280: while (parselinearg(p, buf, sz, pos))
1281: /* Spin. */ ;
1282: teximacroclose(p);
1283: teximacro(p, "Fc");
1284: break;
1285: case (TEXICMD_DEFTYPEFUN):
1286: case (TEXICMD_DEFTYPEFUNX):
1287: case (TEXICMD_DEFTYPEFN):
1288: case (TEXICMD_DEFTYPEFNX):
1289: teximacroopen(p, "Ft");
1290: parselinearg(p, buf, sz, pos);
1291: teximacroclose(p);
1292: teximacroopen(p, "Fo");
1293: parselinearg(p, buf, sz, pos);
1294: teximacroclose(p);
1295: teximacroopen(p, "Fa");
1296: while (parselinearg(p, buf, sz, pos))
1297: /* Spin. */ ;
1298: teximacroclose(p);
1299: teximacro(p, "Fc");
1300: break;
1301: case (TEXICMD_DEFTP):
1302: case (TEXICMD_DEFTPX):
1303: case (TEXICMD_DEFTYPEVAR):
1304: case (TEXICMD_DEFTYPEVARX):
1305: case (TEXICMD_DEFTYPEVR):
1306: case (TEXICMD_DEFTYPEVRX):
1307: teximacroopen(p, "Vt");
1308: while (parselinearg(p, buf, sz, pos))
1309: /* Spin. */ ;
1310: teximacroclose(p);
1311: break;
1312: case (TEXICMD_DEFVAR):
1313: case (TEXICMD_DEFVARX):
1314: case (TEXICMD_DEFVR):
1315: case (TEXICMD_DEFVRX):
1316: teximacroopen(p, "Va");
1317: while (parselinearg(p, buf, sz, pos))
1318: /* Spin. */ ;
1319: teximacroclose(p);
1320: break;
1321: default:
1322: abort();
1.3 kristaps 1323: }
1.15 kristaps 1324:
1.11 kristaps 1325: texivspace(p);
1.3 kristaps 1326: if (NULL != blk)
1327: parseto(p, buf, sz, pos, blk);
1328: }
1329:
1330: static void
1.1 kristaps 1331: doignblock(struct texi *p, enum texicmd cmd,
1332: const char *buf, size_t sz, size_t *pos)
1333: {
1334:
1.3 kristaps 1335: p->ign++;
1.5 kristaps 1336: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 1337: p->ign--;
1.1 kristaps 1338: }
1339:
1340: static void
1.3 kristaps 1341: doblock(struct texi *p, enum texicmd cmd,
1.1 kristaps 1342: const char *buf, size_t sz, size_t *pos)
1343: {
1344:
1.5 kristaps 1345: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.1 kristaps 1346: }
1347:
1348: static void
1.12 kristaps 1349: doinline(struct texi *p, enum texicmd cmd,
1350: const char *buf, size_t sz, size_t *pos)
1.1 kristaps 1351: {
1.12 kristaps 1352: const char *macro;
1353:
1354: switch (cmd) {
1355: case (TEXICMD_COMMAND):
1356: macro = "Xr";
1357: break;
1358: case (TEXICMD_ENV):
1359: macro = "Ev";
1360: break;
1361: case (TEXICMD_FILE):
1362: macro = "Pa";
1363: break;
1.16 kristaps 1364: case (TEXICMD_OPTION):
1365: macro = "Op";
1366: break;
1.12 kristaps 1367: case (TEXICMD_VAR):
1368: macro = "Va";
1369: break;
1370: default:
1371: abort();
1372: }
1373:
1374: if (p->literal) {
1375: parsebracket(p, buf, sz, pos);
1376: return;
1377: }
1.1 kristaps 1378:
1.5 kristaps 1379: teximacroopen(p, macro);
1.1 kristaps 1380: p->seenws = 0;
1381: parsebracket(p, buf, sz, pos);
1.11 kristaps 1382: texipunctuate(p, buf, sz, pos);
1.5 kristaps 1383: teximacroclose(p);
1.1 kristaps 1384: }
1385:
1386: static void
1.16 kristaps 1387: doverbinclude(struct texi *p, enum texicmd cmd,
1388: const char *buf, size_t sz, size_t *pos)
1389: {
1390: char fname[PATH_MAX], path[PATH_MAX];
1391: int rc;
1392: size_t i;
1393:
1394: while (*pos < sz && ' ' == buf[*pos])
1395: advance(p, buf, pos);
1396:
1397: /* Read in the filename. */
1398: for (i = 0; *pos < sz && '\n' != buf[*pos]; i++) {
1399: if (i == sizeof(fname) - 1)
1400: break;
1401: fname[i] = buf[*pos];
1402: advance(p, buf, pos);
1403: }
1404:
1405: if (i == 0)
1406: texierr(p, "path too short");
1407: else if ('\n' != buf[*pos])
1408: texierr(p, "path too long");
1409: else if ('/' == fname[0])
1410: texierr(p, "no absolute paths");
1411: fname[i] = '\0';
1412:
1413: if (strstr(fname, "../") || strstr(fname, "/.."))
1414: texierr(p, "insecure path");
1415:
1416: rc = snprintf(path, sizeof(path),
1417: "%s/%s", p->dirs[0], fname);
1418: if (rc < 0)
1419: texierr(p, "couldn't format path");
1420: else if ((size_t)rc >= sizeof(path))
1421: texierr(p, "path too long");
1422:
1423: parsefile(p, path, 0);
1424: }
1425:
1426: static void
1.2 kristaps 1427: doinclude(struct texi *p, enum texicmd cmd,
1428: const char *buf, size_t sz, size_t *pos)
1429: {
1430: char fname[PATH_MAX], path[PATH_MAX];
1431: size_t i;
1432: int rc;
1433:
1434: while (*pos < sz && ' ' == buf[*pos])
1435: advance(p, buf, pos);
1436:
1437: /* Read in the filename. */
1438: for (i = 0; *pos < sz && '\n' != buf[*pos]; i++) {
1439: if (i == sizeof(fname) - 1)
1440: break;
1441: fname[i] = buf[*pos];
1442: advance(p, buf, pos);
1443: }
1444:
1445: if (i == 0)
1446: texierr(p, "path too short");
1447: else if ('\n' != buf[*pos])
1448: texierr(p, "path too long");
1449: else if ('/' == fname[0])
1450: texierr(p, "no absolute paths");
1451: fname[i] = '\0';
1452:
1453: if (strstr(fname, "../") || strstr(fname, "/.."))
1454: texierr(p, "insecure path");
1455:
1.5 kristaps 1456: for (i = 0; i < p->dirsz; i++) {
1457: rc = snprintf(path, sizeof(path),
1458: "%s/%s", p->dirs[i], fname);
1459: if (rc < 0)
1460: texierr(p, "couldn't format path");
1461: else if ((size_t)rc >= sizeof(path))
1462: texierr(p, "path too long");
1463: else if (-1 == access(path, R_OK))
1464: continue;
1465:
1.16 kristaps 1466: parsefile(p, path, 1);
1.5 kristaps 1467: return;
1468: }
1.2 kristaps 1469:
1.5 kristaps 1470: texierr(p, "couldn't find %s in includes", fname);
1.2 kristaps 1471: }
1472:
1473: static void
1.12 kristaps 1474: dofont(struct texi *p, enum texicmd cmd,
1.1 kristaps 1475: const char *buf, size_t sz, size_t *pos)
1476: {
1.12 kristaps 1477: const char *font;
1478:
1479: switch (cmd) {
1480: case (TEXICMD_B):
1481: case (TEXICMD_STRONG):
1482: font = "\\fB";
1483: break;
1484: case (TEXICMD_CITE):
1485: case (TEXICMD_DFN):
1486: case (TEXICMD_EMPH):
1487: case (TEXICMD_I):
1488: case (TEXICMD_SLANTED):
1489: font = "\\fI";
1490: break;
1491: case (TEXICMD_CODE):
1492: case (TEXICMD_KBD):
1493: case (TEXICMD_R):
1494: case (TEXICMD_SAMP):
1495: case (TEXICMD_SANSSERIF):
1496: case (TEXICMD_T):
1497: font = "\\fR";
1498: break;
1499: default:
1500: abort();
1501: }
1.1 kristaps 1502:
1.18 kristaps 1503: if (p->seenws) {
1504: texiputchar(p, ' ');
1505: p->seenws = 0;
1506: }
1.12 kristaps 1507: texiputchars(p, font);
1.1 kristaps 1508: parsebracket(p, buf, sz, pos);
1509: texiputchars(p, "\\fP");
1510: }
1511:
1512: static void
1513: dobracket(struct texi *p, enum texicmd cmd,
1514: const char *buf, size_t sz, size_t *pos)
1515: {
1516:
1517: parsebracket(p, buf, sz, pos);
1518: }
1519:
1520: static void
1.3 kristaps 1521: dodisplay(struct texi *p, enum texicmd cmd,
1522: const char *buf, size_t sz, size_t *pos)
1523: {
1524:
1.20 ! kristaps 1525: switch (cmd) {
! 1526: case (TEXICMD_FORMAT):
! 1527: case (TEXICMD_SMALLFORMAT):
! 1528: teximacro(p, "Bd -filled");
! 1529: break;
! 1530: default:
! 1531: teximacro(p, "Bd -filled -offset indent");
! 1532: break;
! 1533: }
! 1534:
1.11 kristaps 1535: p->seenvs = 1;
1.12 kristaps 1536: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1537: advanceeoln(p, buf, sz, pos, 1);
1.13 kristaps 1538: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 1539: teximacro(p, "Ed");
1.3 kristaps 1540: }
1541:
1542: static void
1.1 kristaps 1543: doexample(struct texi *p, enum texicmd cmd,
1544: const char *buf, size_t sz, size_t *pos)
1545: {
1546:
1.5 kristaps 1547: teximacro(p, "Bd -literal -offset indent");
1.12 kristaps 1548: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1549: advanceeoln(p, buf, sz, pos, 1);
1550: p->literal++;
1.13 kristaps 1551: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3 kristaps 1552: p->literal--;
1.5 kristaps 1553: teximacro(p, "Ed");
1.1 kristaps 1554: }
1555:
1556: static void
1557: dobye(struct texi *p, enum texicmd cmd,
1558: const char *buf, size_t sz, size_t *pos)
1559: {
1560:
1561: texiexit(p);
1562: exit(EXIT_SUCCESS);
1563: }
1564:
1565: static void
1.10 kristaps 1566: dotitle(struct texi *p, enum texicmd cmd,
1567: const char *buf, size_t sz, size_t *pos)
1568: {
1569: size_t start, end;
1570:
1571: while (*pos < sz && isws(buf[*pos]))
1572: advance(p, buf, pos);
1573: start = end = *pos;
1574: while (end < sz && '\n' != buf[end])
1575: end++;
1576: free(p->subtitle);
1577: p->subtitle = malloc(end - start + 1);
1578: memcpy(p->subtitle, &buf[start], end - start);
1579: p->subtitle[end - start] = '\0';
1580: }
1581:
1582: static void
1.16 kristaps 1583: doaccent(struct texi *p, enum texicmd cmd,
1584: const char *buf, size_t sz, size_t *pos)
1585: {
1586:
1587: if (*pos == sz)
1588: return;
1589: advance(p, buf, pos);
1590: switch (cmd) {
1591: case (TEXICMD_ACUTE):
1592: switch (buf[*pos]) {
1593: case ('a'): case ('A'):
1594: case ('e'): case ('E'):
1595: case ('i'): case ('I'):
1596: case ('o'): case ('O'):
1597: case ('u'): case ('U'):
1598: texiputchars(p, "\\(\'");
1599: texiputchar(p, buf[*pos]);
1600: break;
1601: default:
1602: texiputchar(p, buf[*pos]);
1603: }
1604: break;
1605: case (TEXICMD_CIRCUMFLEX):
1606: switch (buf[*pos]) {
1607: case ('a'): case ('A'):
1608: case ('e'): case ('E'):
1609: case ('i'): case ('I'):
1610: case ('o'): case ('O'):
1611: case ('u'): case ('U'):
1612: texiputchars(p, "\\(^");
1613: texiputchar(p, buf[*pos]);
1614: break;
1615: default:
1616: texiputchar(p, buf[*pos]);
1617: }
1618: break;
1619: case (TEXICMD_GRAVE):
1620: switch (buf[*pos]) {
1621: case ('a'): case ('A'):
1622: case ('e'): case ('E'):
1623: case ('i'): case ('I'):
1624: case ('o'): case ('O'):
1625: case ('u'): case ('U'):
1626: texiputchars(p, "\\(`");
1627: texiputchar(p, buf[*pos]);
1628: break;
1629: default:
1630: texiputchar(p, buf[*pos]);
1631: }
1632: break;
1633: case (TEXICMD_TILDE):
1634: switch (buf[*pos]) {
1635: case ('a'): case ('A'):
1636: case ('n'): case ('N'):
1637: case ('o'): case ('O'):
1638: texiputchars(p, "\\(~");
1639: texiputchar(p, buf[*pos]);
1640: break;
1641: default:
1642: texiputchar(p, buf[*pos]);
1643: }
1644: break;
1645: case (TEXICMD_UMLAUT):
1646: switch (buf[*pos]) {
1647: case ('a'): case ('A'):
1648: case ('e'): case ('E'):
1649: case ('i'): case ('I'):
1650: case ('o'): case ('O'):
1651: case ('u'): case ('U'):
1652: case ('y'):
1653: texiputchars(p, "\\(:");
1654: texiputchar(p, buf[*pos]);
1655: break;
1656: default:
1657: texiputchar(p, buf[*pos]);
1658: }
1659: break;
1660: default:
1661: abort();
1662: }
1663: }
1664:
1665: static void
1.1 kristaps 1666: dosymbol(struct texi *p, enum texicmd cmd,
1667: const char *buf, size_t sz, size_t *pos)
1668: {
1669:
1.3 kristaps 1670: if (p->seenws && p->outcol && 0 == p->literal) {
1671: texiputchar(p, ' ');
1672: p->seenws = 0;
1673: }
1674:
1.1 kristaps 1675: switch (cmd) {
1.3 kristaps 1676: case (TEXICMD_ASTERISK):
1677: case (TEXICMD_NEWLINE):
1678: case (TEXICMD_SPACE):
1.18 kristaps 1679: case (TEXICMD_TABSYM):
1.3 kristaps 1680: texiputchar(p, ' ');
1681: break;
1.1 kristaps 1682: case (TEXICMD_AT):
1.3 kristaps 1683: texiputchar(p, '@');
1684: break;
1685: case (TEXICMD_BANG):
1686: texiputchar(p, '!');
1.7 kristaps 1687: break;
1688: case (TEXICMD_BULLET):
1689: texiputchars(p, "\\(bu");
1.1 kristaps 1690: break;
1691: case (TEXICMD_COPYRIGHT):
1692: texiputchars(p, "\\(co");
1693: break;
1.2 kristaps 1694: case (TEXICMD_DOTS):
1695: texiputchars(p, "...");
1696: break;
1.15 kristaps 1697: case (TEXICMD_ERROR):
1698: texiputchars(p, "error\\(->");
1.17 kristaps 1699: break;
1700: case (TEXICMD_EXPANSION):
1701: texiputchars(p, "\\(->");
1.15 kristaps 1702: break;
1.1 kristaps 1703: case (TEXICMD_LATEX):
1704: texiputchars(p, "LaTeX");
1705: break;
1.3 kristaps 1706: case (TEXICMD_QUESTIONMARK):
1707: texiputchar(p, '?');
1.15 kristaps 1708: break;
1709: case (TEXICMD_RESULT):
1710: texiputchars(p, "\\(rA");
1.3 kristaps 1711: break;
1712: case (TEXICMD_SQUIGGLE_LEFT):
1713: texiputchars(p, "{");
1714: break;
1715: case (TEXICMD_SQUIGGLE_RIGHT):
1716: texiputchars(p, "}");
1717: break;
1.1 kristaps 1718: case (TEXICMD_TEXSYM):
1719: texiputchars(p, "TeX");
1720: break;
1.3 kristaps 1721: case (TEXICMD_COLON):
1722: case (TEXICMD_HYPHEN):
1723: break;
1.1 kristaps 1724: default:
1.5 kristaps 1725: texiwarn(p, "sym: %d", cmd);
1.1 kristaps 1726: abort();
1727: }
1728:
1.5 kristaps 1729: if (texitoks[cmd].len > 1)
1730: doignbracket(p, cmd, buf, sz, pos);
1.1 kristaps 1731: }
1732:
1733: static void
1734: doquotation(struct texi *p, enum texicmd cmd,
1735: const char *buf, size_t sz, size_t *pos)
1736: {
1737:
1.5 kristaps 1738: teximacro(p, "Qo");
1.1 kristaps 1739: parseto(p, buf, sz, pos, "quotation");
1.5 kristaps 1740: teximacro(p, "Qc");
1.1 kristaps 1741: }
1742:
1.3 kristaps 1743: static void
1744: domath(struct texi *p, enum texicmd cmd,
1745: const char *buf, size_t sz, size_t *pos)
1746: {
1747: size_t nest;
1748:
1749: /*
1750: * Math handling is different from everything else.
1751: * We don't allow any subcomponents, and we ignore the rules in
1752: * terms of @-commands.
1753: * This departs from GNU's rules, but whatever.
1754: */
1755: while (*pos < sz && isws(buf[*pos]))
1756: advance(p, buf, pos);
1757: if (*pos == sz || '{' != buf[*pos])
1758: return;
1759: advance(p, buf, pos);
1760: if (p->seenws && p->outcol && 0 == p->literal)
1761: texiputchar(p, ' ');
1762: p->seenws = 0;
1763: for (nest = 1; *pos < sz && nest > 0; ) {
1764: if ('{' == buf[*pos])
1765: nest++;
1766: else if ('}' == buf[*pos])
1767: if (0 == --nest)
1768: continue;
1769: texiputchar(p, buf[*pos]);
1770: advance(p, buf, pos);
1771: }
1772: if (*pos == sz)
1773: return;
1774: assert('}' == buf[*pos]);
1775: advance(p, buf, pos);
1776: }
1777:
1.1 kristaps 1778: static void
1.8 kristaps 1779: dolink(struct texi *p, enum texicmd cmd,
1.1 kristaps 1780: const char *buf, size_t sz, size_t *pos)
1781: {
1.8 kristaps 1782: int c;
1.1 kristaps 1783:
1784: switch (cmd) {
1785: case (TEXICMD_EMAIL):
1.5 kristaps 1786: teximacroopen(p, "Mt");
1.1 kristaps 1787: break;
1.3 kristaps 1788: case (TEXICMD_UREF):
1.1 kristaps 1789: case (TEXICMD_URL):
1.5 kristaps 1790: teximacroopen(p, "Lk");
1.1 kristaps 1791: break;
1.8 kristaps 1792: case (TEXICMD_XREF):
1793: texiputchars(p, "See Section");
1794: teximacroopen(p, "Qq");
1795: break;
1796: case (TEXICMD_PXREF):
1797: texiputchars(p, "see Section");
1798: teximacroopen(p, "Qq");
1799: break;
1.1 kristaps 1800: default:
1.8 kristaps 1801: abort();
1.1 kristaps 1802: }
1.8 kristaps 1803:
1804: c = parsearg(p, buf, sz, pos, 0);
1805: p->ign++;
1806: while (c > 0)
1807: c = parsearg(p, buf, sz, pos, 1);
1808: p->ign--;
1809:
1.11 kristaps 1810: texipunctuate(p, buf, sz, pos);
1.8 kristaps 1811: teximacroclose(p);
1812: }
1813:
1814: static void
1815: doignargn(struct texi *p, enum texicmd cmd,
1816: const char *buf, size_t sz, size_t *pos)
1817: {
1818: int c;
1819:
1820: c = parsearg(p, buf, sz, pos, 0);
1821: p->ign++;
1822: while (c > 0)
1823: c = parsearg(p, buf, sz, pos, 1);
1824: p->ign--;
1.1 kristaps 1825: }
1826:
1827: static void
1828: dosubsection(struct texi *p, enum texicmd cmd,
1829: const char *buf, size_t sz, size_t *pos)
1830: {
1.13 kristaps 1831:
1832: if (p->outmacro)
1833: texierr(p, "\"Em\" in open line scope!?");
1834: else if (p->literal)
1835: texierr(p, "\"Em\" in a literal scope!?");
1.1 kristaps 1836:
1.11 kristaps 1837: texivspace(p);
1.5 kristaps 1838: teximacroopen(p, "Em");
1.3 kristaps 1839: parseeoln(p, buf, sz, pos);
1.5 kristaps 1840: teximacroclose(p);
1.11 kristaps 1841: texivspace(p);
1.1 kristaps 1842: }
1843:
1844: static void
1845: dosection(struct texi *p, enum texicmd cmd,
1846: const char *buf, size_t sz, size_t *pos)
1847: {
1.12 kristaps 1848: const char *blk;
1849:
1850: switch (cmd) {
1851: case (TEXICMD_APPENDIX):
1852: case (TEXICMD_CHAPTER):
1853: case (TEXICMD_TOP):
1854: case (TEXICMD_UNNUMBERED):
1855: blk = "Sh";
1856: break;
1857: case (TEXICMD_APPENDIXSEC):
1858: case (TEXICMD_HEADING):
1859: case (TEXICMD_SECTION):
1860: case (TEXICMD_UNNUMBEREDSEC):
1861: blk = "Ss";
1862: break;
1863: default:
1864: abort();
1865: }
1.1 kristaps 1866:
1.3 kristaps 1867: if (p->outmacro)
1.12 kristaps 1868: texierr(p, "%s in open line scope!?", blk);
1.3 kristaps 1869: else if (p->literal)
1.12 kristaps 1870: texierr(p, "%s in a literal scope!?", blk);
1.3 kristaps 1871:
1.12 kristaps 1872: teximacroopen(p, blk);
1.3 kristaps 1873: parseeoln(p, buf, sz, pos);
1874: teximacroclose(p);
1.11 kristaps 1875: p->seenvs = 1;
1.3 kristaps 1876: }
1877:
1878: static void
1879: dosp(struct texi *p, enum texicmd cmd,
1880: const char *buf, size_t sz, size_t *pos)
1881: {
1882:
1.11 kristaps 1883: texivspace(p);
1.12 kristaps 1884: /* FIXME: ignore and parseeoln. */
1.3 kristaps 1885: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 1886: }
1887:
1888: static void
1889: dotop(struct texi *p, enum texicmd cmd,
1890: const char *buf, size_t sz, size_t *pos)
1891: {
1.10 kristaps 1892: const char *cp;
1893: time_t t;
1894: char date[32];
1895:
1.18 kristaps 1896: /*
1897: * Here we print our standard mdoc(7) prologue.
1898: * We use the title set with @settitle for the `Nd' description
1899: * and the source document filename (the first one as invoked on
1900: * the command line) for the title.
1901: * The date is set to the current date.
1902: */
1.10 kristaps 1903: t = time(NULL);
1904: strftime(date, sizeof(date), "%F", localtime(&t));
1.1 kristaps 1905:
1.3 kristaps 1906: p->ign--;
1.10 kristaps 1907: teximacroopen(p, "Dd");
1908: texiputchars(p, date);
1909: teximacroclose(p);
1910: teximacroopen(p, "Dt");
1911: for (cp = p->title; '\0' != *cp; cp++)
1912: texiputchar(p, toupper(*cp));
1.11 kristaps 1913: texiputchars(p, " 7");
1.10 kristaps 1914: teximacroclose(p);
1.5 kristaps 1915: teximacro(p, "Os");
1916: teximacro(p, "Sh NAME");
1.10 kristaps 1917: teximacroopen(p, "Nm");
1918: texiputchars(p, p->title);
1919: teximacroclose(p);
1920: teximacroopen(p, "Nd");
1921: texiputchars(p, NULL != p->subtitle ?
1922: p->subtitle : "Unknown description");
1923: teximacroclose(p);
1.11 kristaps 1924: p->seenvs = 1;
1.12 kristaps 1925: dosection(p, cmd, buf, sz, pos);
1.1 kristaps 1926: }
1927:
1928: static void
1929: doitem(struct texi *p, enum texicmd cmd,
1930: const char *buf, size_t sz, size_t *pos)
1931: {
1932:
1.18 kristaps 1933: /* Multitable is using raw tbl(7). */
1934: if (TEXILIST_TABLE == p->list) {
1935: texiputchar(p, '\n');
1936: return;
1937: }
1938:
1.3 kristaps 1939: if (p->outmacro)
1940: texierr(p, "item in open line scope!?");
1941: else if (p->literal)
1942: texierr(p, "item in a literal scope!?");
1943:
1944: switch (p->list) {
1945: case (TEXILIST_ITEM):
1.5 kristaps 1946: teximacroopen(p, "It");
1.3 kristaps 1947: break;
1948: case (TEXILIST_NOITEM):
1.5 kristaps 1949: teximacro(p, "It");
1.3 kristaps 1950: break;
1951: default:
1.11 kristaps 1952: texivspace(p);
1.3 kristaps 1953: break;
1954: }
1.18 kristaps 1955:
1956: /* Trick so we don't start with Pp. */
1.11 kristaps 1957: p->seenvs = 1;
1.3 kristaps 1958: parseeoln(p, buf, sz, pos);
1.1 kristaps 1959:
1.3 kristaps 1960: if (TEXILIST_ITEM == p->list)
1961: teximacroclose(p);
1.9 kristaps 1962: else if (p->outcol > 0)
1.1 kristaps 1963: texiputchar(p, '\n');
1.18 kristaps 1964: }
1965:
1966: static void
1967: dotab(struct texi *p, enum texicmd cmd,
1968: const char *buf, size_t sz, size_t *pos)
1969: {
1970:
1971: /* This command is only useful in @multitable. */
1972: if (TEXILIST_TABLE == p->list)
1973: texiputchar(p, '\t');
1974: }
1975:
1976: static void
1977: domultitable(struct texi *p, enum texicmd cmd,
1978: const char *buf, size_t sz, size_t *pos)
1979: {
1980: enum texilist sv = p->list;
1981: enum texicmd type;
1982: size_t i, end, columns;
1983:
1984: p->list = TEXILIST_TABLE;
1985: teximacro(p, "TS");
1986: columns = 0;
1987:
1988: /* Advance to the first argument... */
1989: while (*pos < sz && isws(buf[*pos]))
1990: advance(p, buf, pos);
1991:
1992: /* Make sure we don't print anything when scanning. */
1993: p->ign++;
1994: if ('@' == buf[*pos]) {
1995: /*
1996: * Look for @columnfractions.
1997: * We ignore these, but we do use the number of
1998: * arguments to set the number of columns that we'll
1999: * have.
2000: */
2001: type = texicmd(p, buf, *pos, sz, &end);
2002: advanceto(p, buf, pos, end);
2003: if (TEXICMD_COLUMNFRACTIONS != type)
2004: texierr(p, "unknown multitable type");
2005: while (*pos < sz && '\n' != buf[*pos]) {
2006: while (*pos < sz && isws(buf[*pos]))
2007: advance(p, buf, pos);
2008: while (*pos < sz && ! isws(buf[*pos])) {
2009: if ('\n' == buf[*pos])
2010: break;
2011: advance(p, buf, pos);
2012: }
2013: columns++;
2014: }
2015: } else
2016: /*
2017: * We have arguments.
2018: * We could parse these, but it's easier to just let
2019: * tbl(7) figure it out.
2020: * So use this only to count arguments.
2021: */
2022: while (parselinearg(p, buf, sz, pos) > 0)
2023: columns++;
2024: p->ign--;
2025:
2026: /* Left-justify each table entry. */
2027: for (i = 0; i < columns; i++) {
2028: if (i > 0)
2029: texiputchar(p, ' ');
2030: texiputchar(p, 'l');
2031: }
2032: texiputchars(p, ".\n");
2033: p->outmacro++;
2034: parseto(p, buf, sz, pos, texitoks[cmd].tok);
2035: p->outmacro--;
2036: teximacro(p, "TE");
2037: p->list = sv;
1.1 kristaps 2038: }
2039:
2040: static void
2041: dotable(struct texi *p, enum texicmd cmd,
2042: const char *buf, size_t sz, size_t *pos)
2043: {
1.3 kristaps 2044: enum texilist sv = p->list;
2045:
2046: p->list = TEXILIST_ITEM;
1.5 kristaps 2047: teximacro(p, "Bl -tag -width Ds");
1.12 kristaps 2048: /* FIXME: ignore and parseeoln. */
2049: advanceeoln(p, buf, sz, pos, 1);
1.11 kristaps 2050: p->seenvs = 1;
1.20 ! kristaps 2051: parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5 kristaps 2052: teximacro(p, "El");
1.3 kristaps 2053: p->list = sv;
1.1 kristaps 2054: }
2055:
2056: static void
1.2 kristaps 2057: doenumerate(struct texi *p, enum texicmd cmd,
2058: const char *buf, size_t sz, size_t *pos)
2059: {
1.3 kristaps 2060: enum texilist sv = p->list;
1.2 kristaps 2061:
1.3 kristaps 2062: p->list = TEXILIST_NOITEM;
1.5 kristaps 2063: teximacro(p, "Bl -enum");
1.11 kristaps 2064: p->seenvs = 1;
1.12 kristaps 2065: /* FIXME: ignore and parseeoln. */
2066: advanceeoln(p, buf, sz, pos, 1);
1.2 kristaps 2067: parseto(p, buf, sz, pos, "enumerate");
1.5 kristaps 2068: teximacro(p, "El");
1.3 kristaps 2069: p->list = sv;
1.2 kristaps 2070: }
2071:
2072: static void
1.1 kristaps 2073: doitemize(struct texi *p, enum texicmd cmd,
2074: const char *buf, size_t sz, size_t *pos)
2075: {
1.3 kristaps 2076: enum texilist sv = p->list;
1.1 kristaps 2077:
1.3 kristaps 2078: p->list = TEXILIST_ITEM;
1.5 kristaps 2079: teximacro(p, "Bl -bullet");
1.11 kristaps 2080: p->seenvs = 1;
1.12 kristaps 2081: /* FIXME: ignore and parseeoln. */
2082: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 2083: parseto(p, buf, sz, pos, "itemize");
1.5 kristaps 2084: teximacro(p, "El");
1.3 kristaps 2085: p->list = sv;
1.1 kristaps 2086: }
2087:
2088: static void
2089: doignbracket(struct texi *p, enum texicmd cmd,
2090: const char *buf, size_t sz, size_t *pos)
2091: {
2092:
1.3 kristaps 2093: p->ign++;
1.1 kristaps 2094: parsebracket(p, buf, sz, pos);
1.3 kristaps 2095: p->ign--;
1.1 kristaps 2096: }
2097:
2098: static void
2099: doignline(struct texi *p, enum texicmd cmd,
2100: const char *buf, size_t sz, size_t *pos)
2101: {
2102:
1.12 kristaps 2103: /* FIXME: ignore and parseeoln. */
1.3 kristaps 2104: advanceeoln(p, buf, sz, pos, 1);
1.1 kristaps 2105: }
2106:
1.8 kristaps 2107: /*
2108: * Parse colon-separated directories from "cp" (if not NULL) and returns
2109: * the array of pointers.
2110: * Prepends "base" to the array.
2111: * This does NOT sanitise the directories!
2112: */
1.5 kristaps 2113: static char **
2114: parsedirs(const char *base, const char *cp, size_t *sz)
2115: {
2116: char *tok, *str, *tofree;
2117: const char *cpp;
2118: size_t i;
2119: char **dirs;
2120:
2121: *sz = NULL != (cpp = cp) ? 2 : 1;
2122: if (*sz > 1)
2123: for ( ; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
2124: cpp++;
2125:
2126: dirs = calloc(*sz, sizeof(char *));
2127: if (NULL == dirs) {
2128: perror(NULL);
2129: exit(EXIT_FAILURE);
2130: } else if (NULL == (dirs[0] = strdup(base))) {
2131: perror(NULL);
2132: exit(EXIT_FAILURE);
2133: }
2134:
2135: if (NULL == cp)
2136: return(dirs);
2137:
2138: if (NULL == (tofree = tok = str = strdup(cp))) {
2139: perror(NULL);
2140: exit(EXIT_FAILURE);
2141: }
2142:
2143: for (i = 1; NULL != (tok = strsep(&str, ":")); i++)
2144: if (NULL == (dirs[i] = strdup(tok))) {
2145: perror(NULL);
2146: exit(EXIT_FAILURE);
2147: }
2148:
2149: free(tofree);
2150: return(dirs);
2151: }
2152:
1.1 kristaps 2153: int
2154: main(int argc, char *argv[])
2155: {
2156: struct texi texi;
1.2 kristaps 2157: int c;
2158: char *path, *dir;
1.10 kristaps 2159: const char *progname, *Idir, *cp;
1.1 kristaps 2160:
2161: progname = strrchr(argv[0], '/');
2162: if (progname == NULL)
2163: progname = argv[0];
2164: else
2165: ++progname;
2166:
1.10 kristaps 2167: memset(&texi, 0, sizeof(struct texi));
1.5 kristaps 2168: Idir = NULL;
1.10 kristaps 2169:
1.5 kristaps 2170: while (-1 != (c = getopt(argc, argv, "I:")))
1.1 kristaps 2171: switch (c) {
1.5 kristaps 2172: case ('I'):
2173: Idir = optarg;
2174: break;
1.1 kristaps 2175: default:
2176: goto usage;
2177: }
2178:
2179: argv += optind;
2180: if (0 == (argc -= optind))
2181: goto usage;
2182:
1.2 kristaps 2183: if (NULL == (path = strdup(argv[0]))) {
2184: perror(NULL);
2185: exit(EXIT_FAILURE);
2186: } else if (NULL == (dir = dirname(path))) {
2187: perror(argv[0]);
2188: free(path);
2189: exit(EXIT_FAILURE);
2190: }
2191: free(path);
2192:
1.10 kristaps 2193: if (NULL != (cp = strrchr(argv[0], '/')))
2194: texi.title = strdup(cp + 1);
2195: else
2196: texi.title = strdup(argv[0]);
2197:
2198: if (NULL == texi.title) {
2199: perror(NULL);
2200: exit(EXIT_FAILURE);
2201: } else if (NULL != (path = strchr(texi.title, '.')))
2202: *path = '\0';
2203:
1.3 kristaps 2204: texi.ign = 1;
1.5 kristaps 2205: texi.dirs = parsedirs(dir, Idir, &texi.dirsz);
1.16 kristaps 2206: parsefile(&texi, argv[0], 1);
1.5 kristaps 2207: /* We shouldn't get here. */
1.2 kristaps 2208: texiexit(&texi);
2209: return(EXIT_FAILURE);
1.1 kristaps 2210: usage:
1.8 kristaps 2211: fprintf(stderr, "usage: %s [-Idirs] file\n", progname);
1.1 kristaps 2212: return(EXIT_FAILURE);
2213: }
CVSweb