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

Annotation of texi2mdoc/main.c, Revision 1.40

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

CVSweb