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

Annotation of texi2mdoc/main.c, Revision 1.34

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

CVSweb