[BACK]Return to main.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / texi2mdoc

Annotation of texi2mdoc/main.c, Revision 1.16

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

CVSweb