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

Annotation of texi2mdoc/main.c, Revision 1.41

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

CVSweb