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

Annotation of texi2mdoc/main.c, Revision 1.36

1.36    ! kristaps    1: /*     $Id: main.c,v 1.35 2015/02/23 15:09:09 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2015 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <sys/mman.h>
                     18: #include <sys/stat.h>
                     19:
                     20: #include <assert.h>
                     21: #include <ctype.h>
                     22: #include <fcntl.h>
                     23: #include <getopt.h>
1.2       kristaps   24: #include <libgen.h>
                     25: #include <limits.h>
1.1       kristaps   26: #include <stdarg.h>
                     27: #include <stdio.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
1.10      kristaps   30: #include <time.h>
1.6       kristaps   31: #include <unistd.h>
1.1       kristaps   32:
1.24      kristaps   33: #include "extern.h"
1.1       kristaps   34:
1.23      kristaps   35: #define        SECTSZ 4
                     36: static const char *const sects[SECTSZ] = {
                     37:        "Sh",
                     38:        "Ss",
                     39:        "Em",
                     40:        "No",
                     41: };
                     42:
1.16      kristaps   43: static void doaccent(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   44: static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   45: static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     46: static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.27      kristaps   47: static void dodefindex(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.15      kristaps   48: static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   49: static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2       kristaps   50: static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   51: static void doexample(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8       kristaps   52: static void doignargn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   53: static void doignblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     54: static void doignbracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     55: static void doignline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12      kristaps   56: static void doinline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2       kristaps   57: static void doinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   58: static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     59: static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8       kristaps   60: static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.30      kristaps   61: static void domacro(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   62: static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18      kristaps   63: static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   64: static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     65: static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     66: static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23      kristaps   67: static void dosecoffs(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   68: static void dosection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   69: static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   70: static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23      kristaps   71: static void dosubsubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   72: static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18      kristaps   73: static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.10      kristaps   74: static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.24      kristaps   75: static void dovalue(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.25      kristaps   76: static void doverb(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.33      kristaps   77: static void doverbatim(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.16      kristaps   78: static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   79:
1.24      kristaps   80: static const struct texitok __texitoks[TEXICMD__MAX] = {
1.20      kristaps   81:        /* TEXICMD__BEGIN */
1.8       kristaps   82:        { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */
1.16      kristaps   83:        { doaccent, "'", 1 }, /* TEXICMD_ACUTE */
1.1       kristaps   84:        { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */
                     85:        { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */
1.12      kristaps   86:        { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */
                     87:        { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */
1.20      kristaps   88:        { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */
1.23      kristaps   89:        { dosubsubsection, "appendixsubsubsec", 17 }, /* TEXICMD_APPENDIXSUBSUBSEC */
1.25      kristaps   90:        { doinline, "asis", 4 }, /* TEXICMD_ASIS */
1.3       kristaps   91:        { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */
1.1       kristaps   92:        { dosymbol, "@", 1 }, /* TEXICMD_AT */
1.3       kristaps   93:        { doignline, "author", 6 }, /* TEXICMD_AUTHOR */
1.21      kristaps   94:        { doinline, "b", 1 }, /* TEXICMD_BOLD */
1.3       kristaps   95:        { dosymbol, "!", 1 }, /* TEXICMD_BANG */
1.7       kristaps   96:        { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */
1.1       kristaps   97:        { dobye, "bye", 3 }, /* TEXICMD_BYE */
1.34      kristaps   98:        { doblock, "cartouche", 9 }, /* TEXICMD_CARTOUCHE */
                     99:        { doaccent, ",", 1 }, /* TEXICMD_CEDILLA */
1.12      kristaps  100:        { doignline, "center", 6 }, /* TEXICMD_CENTER */
                    101:        { dosection, "chapter", 7 }, /* TEXICMD_CHAPTER */
1.1       kristaps  102:        { doignline, "cindex", 6 }, /* TEXICMD_CINDEX */
1.16      kristaps  103:        { doaccent, "^", 1 }, /* TEXICMD_CIRCUMFLEX */
1.24      kristaps  104:        { doinline, "cite", 4 }, /* TEXICMD_CITE */
                    105:        { dovalue, "clear", 5 }, /* TEXICMD_CLEAR */
1.21      kristaps  106:        { doinline, "code", 4 }, /* TEXICMD_CODE */
1.3       kristaps  107:        { dosymbol, ":", 1 }, /* TEXICMD_COLON */
1.18      kristaps  108:        { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */
1.35      kristaps  109:        { dosymbol, "comma", 5 }, /* TEXICMD_COMMA */
1.12      kristaps  110:        { doinline, "command", 7 }, /* TEXICMD_COMMAND */
1.1       kristaps  111:        { doignline, "c", 1 }, /* TEXICMD_COMMENT */
1.2       kristaps  112:        { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */
1.1       kristaps  113:        { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */
                    114:        { doignblock, "copying", 7 }, /* TEXICMD_COPYING */
                    115:        { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */
1.27      kristaps  116:        { dodefindex, "defcodeindex", 12 }, /* TEXICMD_DEFCODEINDEX */
1.15      kristaps  117:        { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */
                    118:        { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */
1.27      kristaps  119:        { dodefindex, "defindex", 8 }, /* TEXICMD_DEFINDEX */
1.15      kristaps  120:        { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */
                    121:        { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */
                    122:        { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */
                    123:        { dodefn, "deftpx", 6 }, /* TEXICMD_DEFTPX */
                    124:        { dodefn, "deftypefn", 9 }, /* TEXICMD_DEFTYPEFN */
                    125:        { dodefn, "deftypefnx", 10 }, /* TEXICMD_DEFTYPEFNX */
                    126:        { dodefn, "deftypefun", 10 }, /* TEXICMD_DEFTYPEFUN */
                    127:        { dodefn, "deftypefunx", 11 }, /* TEXICMD_DEFTYPEFUNX */
1.34      kristaps  128:        { dodefn, "deftypemethod", 13 }, /* TEXICMD_DEFTYPEMETHOD */
                    129:        { dodefn, "deftypemethodx", 14 }, /* TEXICMD_DEFTYPEMETHODX */
1.15      kristaps  130:        { dodefn, "deftypevar", 10 }, /* TEXICMD_DEFTYPEVAR */
                    131:        { dodefn, "deftypevarx", 11 }, /* TEXICMD_DEFTYPEVARX */
                    132:        { dodefn, "deftypevr", 9 }, /* TEXICMD_DEFTYPEVR */
                    133:        { dodefn, "deftypevrx", 10 }, /* TEXICMD_DEFTYPEVRX */
                    134:        { dodefn, "defun", 5 }, /* TEXICMD_DEFUN */
                    135:        { dodefn, "defunx", 6 }, /* TEXICMD_DEFUNX */
                    136:        { dodefn, "defvar", 6 }, /* TEXICMD_DEFVAR */
                    137:        { dodefn, "defvarx", 7 }, /* TEXICMD_DEFVARX */
                    138:        { dodefn, "defvr", 5 }, /* TEXICMD_DEFVR */
                    139:        { dodefn, "defvrx", 6 }, /* TEXICMD_DEFVRX */
1.1       kristaps  140:        { doignblock, "detailmenu", 10 }, /* TEXICMD_DETAILMENU */
1.21      kristaps  141:        { doinline, "dfn", 3 }, /* TEXICMD_DFN */
1.1       kristaps  142:        { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */
                    143:        { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */
1.3       kristaps  144:        { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */
1.28      kristaps  145:        { doignbracket, "dmn", 3 }, /* TEXICMD_DMN */
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) {
                    672:                if (buf[*pos] == '\n')
                    673:                        p->outcol = 0;
                    674:                else
                    675:                        p->outcol++;
1.25      kristaps  676:                if (*pos > 0 && '.' == buf[*pos])
                    677:                        if ('\n' == buf[*pos - 1])
                    678:                                fputs("\\&", stdout);
                    679:                putchar(buf[*pos]);
                    680:                if ('\\' == buf[*pos])
                    681:                        putchar('e');
                    682:                advance(p, buf, pos);
                    683:        }
                    684:        teximacro(p, "Ed");
1.26      kristaps  685:        advanceto(p, buf, pos, endpos + endsz);
1.25      kristaps  686: }
                    687:
                    688: static void
1.16      kristaps  689: doverbinclude(struct texi *p, enum texicmd cmd,
                    690:        const char *buf, size_t sz, size_t *pos)
                    691: {
1.25      kristaps  692:        char             fname[PATH_MAX], path[PATH_MAX];
                    693:        int              rc;
                    694:        size_t           i, end;
                    695:        const char      *v;
                    696:        enum texicmd     type;
1.16      kristaps  697:
                    698:        while (*pos < sz && ' ' == buf[*pos])
                    699:                advance(p, buf, pos);
                    700:
1.25      kristaps  701:        for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.16      kristaps  702:                if (i == sizeof(fname) - 1)
                    703:                        break;
1.25      kristaps  704:                if ('@' != buf[*pos]) {
                    705:                        fname[i++] = buf[*pos];
                    706:                        advance(p, buf, pos);
                    707:                        continue;
                    708:                }
1.30      kristaps  709:                type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25      kristaps  710:                advanceto(p, buf, pos, end);
                    711:                if (TEXICMD_VALUE != type)
                    712:                        texierr(p, "unknown verbatiminclude command");
                    713:                v = valueblookup(p, buf, sz, pos);
                    714:                if (NULL == v)
                    715:                        continue;
                    716:                while ('\0' != *v) {
                    717:                        if (i == sizeof(fname) - 1)
                    718:                                break;
                    719:                        fname[i++] = *v++;
                    720:                }
                    721:                if ('\0' != *v)
                    722:                        break;
1.16      kristaps  723:        }
                    724:
                    725:        if (i == 0)
                    726:                texierr(p, "path too short");
                    727:        else if ('\n' != buf[*pos])
                    728:                texierr(p, "path too long");
                    729:        else if ('/' == fname[0])
                    730:                texierr(p, "no absolute paths");
                    731:        fname[i] = '\0';
                    732:
                    733:        if (strstr(fname, "../") || strstr(fname, "/.."))
                    734:                texierr(p, "insecure path");
                    735:
                    736:        rc = snprintf(path, sizeof(path),
                    737:                "%s/%s", p->dirs[0], fname);
                    738:        if (rc < 0)
                    739:                texierr(p, "couldn't format path");
                    740:        else if ((size_t)rc >= sizeof(path))
                    741:                texierr(p, "path too long");
                    742:
                    743:        parsefile(p, path, 0);
                    744: }
                    745:
                    746: static void
1.2       kristaps  747: doinclude(struct texi *p, enum texicmd cmd,
                    748:        const char *buf, size_t sz, size_t *pos)
                    749: {
1.25      kristaps  750:        char             fname[PATH_MAX], path[PATH_MAX];
                    751:        size_t           i, end;
                    752:        int              rc;
                    753:        const char      *v;
                    754:        enum texicmd     type;
1.2       kristaps  755:
                    756:        while (*pos < sz && ' ' == buf[*pos])
                    757:                advance(p, buf, pos);
                    758:
                    759:        /* Read in the filename. */
1.25      kristaps  760:        for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.2       kristaps  761:                if (i == sizeof(fname) - 1)
                    762:                        break;
1.25      kristaps  763:                if ('@' != buf[*pos]) {
                    764:                        fname[i++] = buf[*pos];
                    765:                        advance(p, buf, pos);
                    766:                        continue;
                    767:                }
1.30      kristaps  768:                type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25      kristaps  769:                advanceto(p, buf, pos, end);
                    770:                if (TEXICMD_VALUE != type)
                    771:                        texierr(p, "unknown include command");
                    772:                v = valueblookup(p, buf, sz, pos);
                    773:                if (NULL == v)
                    774:                        continue;
                    775:                while ('\0' != *v) {
                    776:                        if (i == sizeof(fname) - 1)
                    777:                                break;
                    778:                        fname[i++] = *v++;
                    779:                }
                    780:                if ('\0' != *v)
                    781:                        break;
1.2       kristaps  782:        }
                    783:
                    784:        if (i == 0)
                    785:                texierr(p, "path too short");
                    786:        else if ('\n' != buf[*pos])
                    787:                texierr(p, "path too long");
                    788:        else if ('/' == fname[0])
                    789:                texierr(p, "no absolute paths");
                    790:        fname[i] = '\0';
                    791:
                    792:        if (strstr(fname, "../") || strstr(fname, "/.."))
                    793:                texierr(p, "insecure path");
                    794:
1.5       kristaps  795:        for (i = 0; i < p->dirsz; i++) {
                    796:                rc = snprintf(path, sizeof(path),
                    797:                        "%s/%s", p->dirs[i], fname);
                    798:                if (rc < 0)
                    799:                        texierr(p, "couldn't format path");
                    800:                else if ((size_t)rc >= sizeof(path))
                    801:                        texierr(p, "path too long");
                    802:                else if (-1 == access(path, R_OK))
                    803:                        continue;
                    804:
1.16      kristaps  805:                parsefile(p, path, 1);
1.5       kristaps  806:                return;
                    807:        }
1.2       kristaps  808:
1.5       kristaps  809:        texierr(p, "couldn't find %s in includes", fname);
1.2       kristaps  810: }
                    811:
                    812: static void
1.1       kristaps  813: dobracket(struct texi *p, enum texicmd cmd,
                    814:        const char *buf, size_t sz, size_t *pos)
                    815: {
                    816:
                    817:        parsebracket(p, buf, sz, pos);
                    818: }
                    819:
                    820: static void
1.3       kristaps  821: dodisplay(struct texi *p, enum texicmd cmd,
                    822:        const char *buf, size_t sz, size_t *pos)
                    823: {
                    824:
1.20      kristaps  825:        switch (cmd) {
                    826:        case (TEXICMD_FORMAT):
                    827:        case (TEXICMD_SMALLFORMAT):
                    828:                teximacro(p, "Bd -filled");
                    829:                break;
                    830:        default:
                    831:                teximacro(p, "Bd -filled -offset indent");
                    832:                break;
                    833:        }
                    834:
1.11      kristaps  835:        p->seenvs = 1;
1.12      kristaps  836:        /* FIXME: ignore and parseeoln. */
1.3       kristaps  837:        advanceeoln(p, buf, sz, pos, 1);
1.13      kristaps  838:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5       kristaps  839:        teximacro(p, "Ed");
1.3       kristaps  840: }
                    841:
                    842: static void
1.1       kristaps  843: doexample(struct texi *p, enum texicmd cmd,
                    844:        const char *buf, size_t sz, size_t *pos)
                    845: {
                    846:
1.5       kristaps  847:        teximacro(p, "Bd -literal -offset indent");
1.12      kristaps  848:        /* FIXME: ignore and parseeoln. */
1.3       kristaps  849:        advanceeoln(p, buf, sz, pos, 1);
                    850:        p->literal++;
1.13      kristaps  851:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3       kristaps  852:        p->literal--;
1.5       kristaps  853:        teximacro(p, "Ed");
1.1       kristaps  854: }
                    855:
                    856: static void
                    857: dobye(struct texi *p, enum texicmd cmd,
                    858:        const char *buf, size_t sz, size_t *pos)
                    859: {
                    860:
                    861:        texiexit(p);
                    862:        exit(EXIT_SUCCESS);
                    863: }
                    864:
                    865: static void
1.10      kristaps  866: dotitle(struct texi *p, enum texicmd cmd,
                    867:        const char *buf, size_t sz, size_t *pos)
                    868: {
                    869:        size_t   start, end;
                    870:
                    871:        while (*pos < sz && isws(buf[*pos]))
                    872:                advance(p, buf, pos);
                    873:        start = end = *pos;
                    874:        while (end < sz && '\n' != buf[end])
                    875:                end++;
1.26      kristaps  876:        advanceeoln(p, buf, sz, pos, 1);
1.10      kristaps  877:        free(p->subtitle);
                    878:        p->subtitle = malloc(end - start + 1);
1.27      kristaps  879:        if (NULL == p->subtitle)
                    880:                texiabort(p, NULL);
1.10      kristaps  881:        memcpy(p->subtitle, &buf[start], end - start);
                    882:        p->subtitle[end - start] = '\0';
                    883: }
                    884:
                    885: static void
1.16      kristaps  886: doaccent(struct texi *p, enum texicmd cmd,
                    887:        const char *buf, size_t sz, size_t *pos)
                    888: {
                    889:
                    890:        if (*pos == sz)
                    891:                return;
1.34      kristaps  892:        if (p->seenws && p->outcol && 0 == p->literal)
                    893:                texiputchar(p, ' ');
                    894:        p->seenws = 0;
1.16      kristaps  895:        switch (cmd) {
1.34      kristaps  896:        case (TEXICMD_CEDILLA):
                    897:                /* Strange rules... */
                    898:                while (*pos < sz && isws(buf[*pos]))
                    899:                        advance(p, buf, pos);
                    900:                if (*pos == sz || '{' != buf[*pos])
                    901:                        return;
                    902:                advance(p, buf, pos);
                    903:                switch (buf[*pos]) {
                    904:                case ('c'): case ('C'):
                    905:                        texiputchars(p, "\\(,");
                    906:                        texiputchar(p, buf[*pos]);
                    907:                        break;
                    908:                default:
                    909:                        texiputchar(p, buf[*pos]);
                    910:                        break;
                    911:                }
                    912:                advance(p, buf, pos);
                    913:                break;
1.16      kristaps  914:        case (TEXICMD_ACUTE):
                    915:                switch (buf[*pos]) {
                    916:                case ('a'): case ('A'):
                    917:                case ('e'): case ('E'):
                    918:                case ('i'): case ('I'):
                    919:                case ('o'): case ('O'):
                    920:                case ('u'): case ('U'):
                    921:                        texiputchars(p, "\\(\'");
                    922:                        texiputchar(p, buf[*pos]);
                    923:                        break;
                    924:                default:
                    925:                        texiputchar(p, buf[*pos]);
                    926:                }
                    927:                break;
                    928:        case (TEXICMD_CIRCUMFLEX):
                    929:                switch (buf[*pos]) {
                    930:                case ('a'): case ('A'):
                    931:                case ('e'): case ('E'):
                    932:                case ('i'): case ('I'):
                    933:                case ('o'): case ('O'):
                    934:                case ('u'): case ('U'):
                    935:                        texiputchars(p, "\\(^");
                    936:                        texiputchar(p, buf[*pos]);
                    937:                        break;
                    938:                default:
                    939:                        texiputchar(p, buf[*pos]);
1.34      kristaps  940:                        break;
1.16      kristaps  941:                }
                    942:                break;
                    943:        case (TEXICMD_GRAVE):
                    944:                switch (buf[*pos]) {
                    945:                case ('a'): case ('A'):
                    946:                case ('e'): case ('E'):
                    947:                case ('i'): case ('I'):
                    948:                case ('o'): case ('O'):
                    949:                case ('u'): case ('U'):
                    950:                        texiputchars(p, "\\(`");
                    951:                        texiputchar(p, buf[*pos]);
                    952:                        break;
                    953:                default:
                    954:                        texiputchar(p, buf[*pos]);
                    955:                }
                    956:                break;
                    957:        case (TEXICMD_TILDE):
                    958:                switch (buf[*pos]) {
                    959:                case ('a'): case ('A'):
                    960:                case ('n'): case ('N'):
                    961:                case ('o'): case ('O'):
                    962:                        texiputchars(p, "\\(~");
                    963:                        texiputchar(p, buf[*pos]);
                    964:                        break;
                    965:                default:
                    966:                        texiputchar(p, buf[*pos]);
1.34      kristaps  967:                        break;
1.16      kristaps  968:                }
                    969:                break;
                    970:        case (TEXICMD_UMLAUT):
                    971:                switch (buf[*pos]) {
                    972:                case ('a'): case ('A'):
                    973:                case ('e'): case ('E'):
                    974:                case ('i'): case ('I'):
                    975:                case ('o'): case ('O'):
                    976:                case ('u'): case ('U'):
                    977:                case ('y'):
                    978:                        texiputchars(p, "\\(:");
                    979:                        texiputchar(p, buf[*pos]);
                    980:                        break;
                    981:                default:
                    982:                        texiputchar(p, buf[*pos]);
1.34      kristaps  983:                        break;
1.16      kristaps  984:                }
                    985:                break;
                    986:        default:
                    987:                abort();
                    988:        }
1.34      kristaps  989:        advance(p, buf, pos);
1.16      kristaps  990: }
                    991:
                    992: static void
1.1       kristaps  993: dosymbol(struct texi *p, enum texicmd cmd,
                    994:        const char *buf, size_t sz, size_t *pos)
                    995: {
                    996:
1.3       kristaps  997:        if (p->seenws && p->outcol && 0 == p->literal) {
                    998:                texiputchar(p, ' ');
                    999:                p->seenws = 0;
                   1000:        }
                   1001:
1.1       kristaps 1002:        switch (cmd) {
1.3       kristaps 1003:        case (TEXICMD_ASTERISK):
                   1004:        case (TEXICMD_NEWLINE):
                   1005:        case (TEXICMD_SPACE):
1.18      kristaps 1006:        case (TEXICMD_TABSYM):
1.3       kristaps 1007:                texiputchar(p, ' ');
                   1008:                break;
1.1       kristaps 1009:        case (TEXICMD_AT):
1.3       kristaps 1010:                texiputchar(p, '@');
                   1011:                break;
                   1012:        case (TEXICMD_BANG):
                   1013:                texiputchar(p, '!');
1.7       kristaps 1014:                break;
                   1015:        case (TEXICMD_BULLET):
                   1016:                texiputchars(p, "\\(bu");
1.1       kristaps 1017:                break;
1.35      kristaps 1018:        case (TEXICMD_COMMA):
                   1019:                texiputchar(p, ',');
                   1020:                break;
1.1       kristaps 1021:        case (TEXICMD_COPYRIGHT):
                   1022:                texiputchars(p, "\\(co");
                   1023:                break;
1.2       kristaps 1024:        case (TEXICMD_DOTS):
1.34      kristaps 1025:        case (TEXICMD_ENDDOTS):
1.2       kristaps 1026:                texiputchars(p, "...");
                   1027:                break;
1.28      kristaps 1028:        case (TEXICMD_EQUIV):
                   1029:                texiputchars(p, "\\(==");
                   1030:                break;
1.15      kristaps 1031:        case (TEXICMD_ERROR):
                   1032:                texiputchars(p, "error\\(->");
1.17      kristaps 1033:                break;
                   1034:        case (TEXICMD_EXPANSION):
                   1035:                texiputchars(p, "\\(->");
1.15      kristaps 1036:                break;
1.1       kristaps 1037:        case (TEXICMD_LATEX):
                   1038:                texiputchars(p, "LaTeX");
                   1039:                break;
1.25      kristaps 1040:        case (TEXICMD_MINUS):
                   1041:                texiputchars(p, "\\-");
                   1042:                break;
                   1043:        case (TEXICMD_PERIOD):
                   1044:                texiputchar(p, '.');
                   1045:                break;
1.3       kristaps 1046:        case (TEXICMD_QUESTIONMARK):
                   1047:                texiputchar(p, '?');
1.15      kristaps 1048:                break;
                   1049:        case (TEXICMD_RESULT):
                   1050:                texiputchars(p, "\\(rA");
1.3       kristaps 1051:                break;
1.34      kristaps 1052:        case (TEXICMD_SLASH):
                   1053:                texiputchar(p, '/');
                   1054:                break;
1.35      kristaps 1055:        case (TEXICMD_SS):
                   1056:                texiputchars(p, "\\(ss");
                   1057:                break;
1.3       kristaps 1058:        case (TEXICMD_SQUIGGLE_LEFT):
                   1059:                texiputchars(p, "{");
                   1060:                break;
                   1061:        case (TEXICMD_SQUIGGLE_RIGHT):
                   1062:                texiputchars(p, "}");
                   1063:                break;
1.35      kristaps 1064:        case (TEXICMD_TIE):
                   1065:                texiputchars(p, "TeX");
                   1066:                break;
1.1       kristaps 1067:        case (TEXICMD_TEXSYM):
1.35      kristaps 1068:                texiputchars(p, "\\ ");
1.1       kristaps 1069:                break;
1.3       kristaps 1070:        case (TEXICMD_COLON):
                   1071:        case (TEXICMD_HYPHEN):
                   1072:                break;
1.1       kristaps 1073:        default:
                   1074:                abort();
                   1075:        }
                   1076:
1.5       kristaps 1077:        if (texitoks[cmd].len > 1)
                   1078:                doignbracket(p, cmd, buf, sz, pos);
1.1       kristaps 1079: }
                   1080:
                   1081: static void
                   1082: doquotation(struct texi *p, enum texicmd cmd,
                   1083:        const char *buf, size_t sz, size_t *pos)
                   1084: {
                   1085:
1.5       kristaps 1086:        teximacro(p, "Qo");
1.1       kristaps 1087:        parseto(p, buf, sz, pos, "quotation");
1.5       kristaps 1088:        teximacro(p, "Qc");
1.1       kristaps 1089: }
                   1090:
1.3       kristaps 1091: static void
                   1092: domath(struct texi *p, enum texicmd cmd,
                   1093:        const char *buf, size_t sz, size_t *pos)
                   1094: {
1.34      kristaps 1095:        size_t   nest, start;
1.3       kristaps 1096:
                   1097:        /*
                   1098:         * Math handling is different from everything else.
                   1099:         * We don't allow any subcomponents, and we ignore the rules in
                   1100:         * terms of @-commands.
                   1101:         * This departs from GNU's rules, but whatever.
                   1102:         */
                   1103:        while (*pos < sz && isws(buf[*pos]))
                   1104:                advance(p, buf, pos);
                   1105:        if (*pos == sz || '{' != buf[*pos])
                   1106:                return;
                   1107:        advance(p, buf, pos);
                   1108:        if (p->seenws && p->outcol && 0 == p->literal)
                   1109:                texiputchar(p, ' ');
                   1110:        p->seenws = 0;
1.34      kristaps 1111:        for (nest = 1, start = *pos; *pos < sz && nest > 0; ) {
1.3       kristaps 1112:                if ('{' == buf[*pos])
                   1113:                        nest++;
                   1114:                else if ('}' == buf[*pos])
                   1115:                        if (0 == --nest)
                   1116:                                continue;
                   1117:                advance(p, buf, pos);
                   1118:        }
                   1119:        if (*pos == sz)
                   1120:                return;
                   1121:        assert('}' == buf[*pos]);
1.34      kristaps 1122:        texiputbuf(p, buf, start, *pos);
1.3       kristaps 1123:        advance(p, buf, pos);
1.24      kristaps 1124: }
                   1125:
                   1126: static void
                   1127: dovalue(struct texi *p, enum texicmd cmd,
                   1128:        const char *buf, size_t sz, size_t *pos)
                   1129: {
1.28      kristaps 1130:        size_t           start, end, i;
1.25      kristaps 1131:        char            *key, *val;
                   1132:        const char      *cp;
1.24      kristaps 1133:
                   1134:        if (TEXICMD_SET == cmd) {
                   1135:                while (*pos < sz && isws(buf[*pos]))
                   1136:                        advance(p, buf, pos);
                   1137:                for (start = end = *pos; end < sz; end++)
                   1138:                        if (ismspace(buf[end]))
                   1139:                                break;
1.25      kristaps 1140:                /* We don't allow empty keys. */
1.24      kristaps 1141:                if (start == end)
                   1142:                        return;
1.25      kristaps 1143:                advanceto(p, buf, pos, end);
1.24      kristaps 1144:
                   1145:                key = malloc(end - start + 1);
1.27      kristaps 1146:                if (NULL == key)
                   1147:                        texiabort(p, NULL);
1.24      kristaps 1148:                memcpy(key, &buf[start], end - start);
                   1149:                key[end - start] = '\0';
                   1150:
                   1151:                while (*pos < sz && isws(buf[*pos]))
                   1152:                        advance(p, buf, pos);
                   1153:                for (start = end = *pos; end < sz; end++)
                   1154:                        if ('\n' == buf[end])
                   1155:                                break;
1.25      kristaps 1156:                /* We do allow empty values. */
                   1157:                advanceeoln(p, buf, sz, pos, 1);
1.24      kristaps 1158:
                   1159:                val = malloc(end - start + 1);
1.27      kristaps 1160:                if (NULL == val)
                   1161:                        texiabort(p, NULL);
1.24      kristaps 1162:                memcpy(val, &buf[start], end - start);
                   1163:                val[end - start] = '\0';
1.25      kristaps 1164:                valueadd(p, key, val);
1.24      kristaps 1165:        } else if (TEXICMD_VALUE == cmd) {
                   1166:                if (p->seenws)
                   1167:                        texiputchar(p, ' ');
                   1168:                p->seenws = 0;
1.28      kristaps 1169:                if (NULL != (cp = valueblookup(p, buf, sz, pos))) {
                   1170:                        for (i = 0; i < p->valstackpos; i++)
                   1171:                                if (cp == p->valstack[i])
                   1172:                                        break;
                   1173:                        if (i < p->valstackpos)
                   1174:                                texierr(p, "recursive value");
1.29      kristaps 1175:                        if (64 == p->valstackpos)
                   1176:                                texierr(p, "too many nested values");
1.28      kristaps 1177:                        p->valstack[p->valstackpos++] = cp;
1.31      kristaps 1178:                        parsemembuf(p, cp, strlen(cp));
1.28      kristaps 1179:                        p->valstackpos--;
                   1180:                } else
1.25      kristaps 1181:                        texiputchars(p, "{No value}");
1.24      kristaps 1182:        } else if (TEXICMD_IFCLEAR == cmd) {
1.26      kristaps 1183:                if (NULL != valuellookup(p, buf, sz, pos))
                   1184:                        doignblock(p, cmd, buf, sz, pos);
                   1185:                else
                   1186:                        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.30      kristaps 1187:        } else if (TEXICMD_IFSET == cmd) {
                   1188:                if (NULL == valuellookup(p, buf, sz, pos))
                   1189:                        doignblock(p, cmd, buf, sz, pos);
                   1190:                else
                   1191:                        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.25      kristaps 1192:        } else if (TEXICMD_CLEAR == cmd)
                   1193:                valuelclear(p, buf, sz, pos);
1.3       kristaps 1194: }
                   1195:
1.1       kristaps 1196: static void
1.8       kristaps 1197: dolink(struct texi *p, enum texicmd cmd,
1.1       kristaps 1198:        const char *buf, size_t sz, size_t *pos)
                   1199: {
1.8       kristaps 1200:        int      c;
1.1       kristaps 1201:
                   1202:        switch (cmd) {
                   1203:        case (TEXICMD_EMAIL):
1.5       kristaps 1204:                teximacroopen(p, "Mt");
1.1       kristaps 1205:                break;
1.3       kristaps 1206:        case (TEXICMD_UREF):
1.1       kristaps 1207:        case (TEXICMD_URL):
1.5       kristaps 1208:                teximacroopen(p, "Lk");
1.1       kristaps 1209:                break;
1.8       kristaps 1210:        case (TEXICMD_XREF):
                   1211:                texiputchars(p, "See Section");
                   1212:                teximacroopen(p, "Qq");
                   1213:                break;
                   1214:        case (TEXICMD_PXREF):
                   1215:                texiputchars(p, "see Section");
                   1216:                teximacroopen(p, "Qq");
                   1217:                break;
1.34      kristaps 1218:        case (TEXICMD_INFOREF):
                   1219:                texiputchars(p, "See Info file node");
                   1220:                teximacroopen(p, "Qq");
                   1221:                break;
1.1       kristaps 1222:        default:
1.8       kristaps 1223:                abort();
1.1       kristaps 1224:        }
1.8       kristaps 1225:
                   1226:        c = parsearg(p, buf, sz, pos, 0);
                   1227:        p->ign++;
                   1228:        while (c > 0)
                   1229:                c = parsearg(p, buf, sz, pos, 1);
                   1230:        p->ign--;
                   1231:
1.11      kristaps 1232:        texipunctuate(p, buf, sz, pos);
1.8       kristaps 1233:        teximacroclose(p);
                   1234: }
                   1235:
                   1236: static void
                   1237: doignargn(struct texi *p, enum texicmd cmd,
                   1238:        const char *buf, size_t sz, size_t *pos)
                   1239: {
                   1240:        int      c;
                   1241:
                   1242:        c = parsearg(p, buf, sz, pos, 0);
                   1243:        p->ign++;
                   1244:        while (c > 0)
                   1245:                c = parsearg(p, buf, sz, pos, 1);
                   1246:        p->ign--;
1.1       kristaps 1247: }
                   1248:
1.23      kristaps 1249: /*
                   1250:  * Sections can be made subsections and so on by way of the
                   1251:  * @raiseections and @lowersections commands.
                   1252:  * Perform this check here and return the actual section number adjusted
                   1253:  * to the raise level.
                   1254:  */
                   1255: static int
                   1256: sectioner(struct texi *p, int sec)
                   1257: {
                   1258:
                   1259:        if ((sec -= p->secoffs) < 0) {
                   1260:                texiwarn(p, "section below minimum, clamping");
                   1261:                return(0);
                   1262:        } else if (sec >= SECTSZ) {
                   1263:                texiwarn(p, "section above maximum, clamping");
                   1264:                return(SECTSZ - 1);
                   1265:        }
                   1266:        return(sec);
                   1267: }
                   1268:
                   1269: static void
                   1270: dosubsubsection(struct texi *p, enum texicmd cmd,
                   1271:                const char *buf, size_t sz, size_t *pos)
                   1272: {
                   1273:        int      sec;
                   1274:
                   1275:        sec = sectioner(p, 3);
                   1276:
                   1277:        /* We don't have a subsubsubsection, so make one up. */
                   1278:        texivspace(p);
                   1279:        teximacroopen(p, sects[sec]);
                   1280:        parseeoln(p, buf, sz, pos);
                   1281:        teximacroclose(p);
                   1282:        texivspace(p);
                   1283: }
                   1284:
1.1       kristaps 1285: static void
                   1286: dosubsection(struct texi *p, enum texicmd cmd,
                   1287:                const char *buf, size_t sz, size_t *pos)
                   1288: {
1.23      kristaps 1289:        int      sec;
                   1290:
                   1291:        sec = sectioner(p, 2);
1.13      kristaps 1292:
                   1293:        if (p->outmacro)
1.23      kristaps 1294:                texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13      kristaps 1295:        else if (p->literal)
1.23      kristaps 1296:                texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1       kristaps 1297:
1.21      kristaps 1298:        /* We don't have a subsubsection, so make one up. */
1.34      kristaps 1299:        if (sec > 1)
                   1300:                texivspace(p);
1.23      kristaps 1301:        teximacroopen(p, sects[sec]);
1.3       kristaps 1302:        parseeoln(p, buf, sz, pos);
1.5       kristaps 1303:        teximacroclose(p);
1.34      kristaps 1304:        if (sec > 1)
                   1305:                texivspace(p);
1.1       kristaps 1306: }
                   1307:
                   1308: static void
1.23      kristaps 1309: dosecoffs(struct texi *p, enum texicmd cmd,
                   1310:        const char *buf, size_t sz, size_t *pos)
                   1311: {
                   1312:
                   1313:        if (TEXICMD_RAISESECTIONS == cmd)
                   1314:                p->secoffs++;
                   1315:        else
                   1316:                p->secoffs--;
                   1317: }
                   1318:
                   1319: static void
1.1       kristaps 1320: dosection(struct texi *p, enum texicmd cmd,
1.23      kristaps 1321:        const char *buf, size_t sz, size_t *pos)
1.1       kristaps 1322: {
1.23      kristaps 1323:        int              sec;
1.12      kristaps 1324:
                   1325:        switch (cmd) {
                   1326:        case (TEXICMD_APPENDIX):
                   1327:        case (TEXICMD_CHAPTER):
                   1328:        case (TEXICMD_TOP):
                   1329:        case (TEXICMD_UNNUMBERED):
1.23      kristaps 1330:                sec = sectioner(p, 0);
1.12      kristaps 1331:                break;
                   1332:        case (TEXICMD_APPENDIXSEC):
                   1333:        case (TEXICMD_HEADING):
                   1334:        case (TEXICMD_SECTION):
                   1335:        case (TEXICMD_UNNUMBEREDSEC):
1.23      kristaps 1336:                sec = sectioner(p, 1);
1.12      kristaps 1337:                break;
                   1338:        default:
                   1339:                abort();
                   1340:        }
1.1       kristaps 1341:
1.3       kristaps 1342:        if (p->outmacro)
1.23      kristaps 1343:                texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3       kristaps 1344:        else if (p->literal)
1.23      kristaps 1345:                texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3       kristaps 1346:
1.23      kristaps 1347:        teximacroopen(p, sects[sec]);
1.3       kristaps 1348:        parseeoln(p, buf, sz, pos);
                   1349:        teximacroclose(p);
1.11      kristaps 1350:        p->seenvs = 1;
1.3       kristaps 1351: }
                   1352:
                   1353: static void
                   1354: dosp(struct texi *p, enum texicmd cmd,
                   1355:        const char *buf, size_t sz, size_t *pos)
                   1356: {
                   1357:
1.11      kristaps 1358:        texivspace(p);
1.12      kristaps 1359:        /* FIXME: ignore and parseeoln. */
1.3       kristaps 1360:        advanceeoln(p, buf, sz, pos, 1);
1.1       kristaps 1361: }
                   1362:
                   1363: static void
                   1364: dotop(struct texi *p, enum texicmd cmd,
                   1365:        const char *buf, size_t sz, size_t *pos)
                   1366: {
1.10      kristaps 1367:        const char      *cp;
                   1368:        time_t           t;
                   1369:        char             date[32];
                   1370:
1.26      kristaps 1371:        if (--p->ign)
                   1372:                texierr(p, "@top command while ignoring (%d)", p->ign);
                   1373:
1.18      kristaps 1374:        /*
                   1375:         * Here we print our standard mdoc(7) prologue.
                   1376:         * We use the title set with @settitle for the `Nd' description
                   1377:         * and the source document filename (the first one as invoked on
                   1378:         * the command line) for the title.
                   1379:         * The date is set to the current date.
                   1380:         */
1.10      kristaps 1381:        t = time(NULL);
                   1382:        strftime(date, sizeof(date), "%F", localtime(&t));
1.1       kristaps 1383:
1.10      kristaps 1384:        teximacroopen(p, "Dd");
                   1385:        texiputchars(p, date);
                   1386:        teximacroclose(p);
                   1387:        teximacroopen(p, "Dt");
                   1388:        for (cp = p->title; '\0' != *cp; cp++)
                   1389:                texiputchar(p, toupper(*cp));
1.11      kristaps 1390:        texiputchars(p, " 7");
1.10      kristaps 1391:        teximacroclose(p);
1.5       kristaps 1392:        teximacro(p, "Os");
                   1393:        teximacro(p, "Sh NAME");
1.10      kristaps 1394:        teximacroopen(p, "Nm");
                   1395:        texiputchars(p, p->title);
                   1396:        teximacroclose(p);
                   1397:        teximacroopen(p, "Nd");
                   1398:        texiputchars(p, NULL != p->subtitle ?
                   1399:                p->subtitle : "Unknown description");
                   1400:        teximacroclose(p);
1.11      kristaps 1401:        p->seenvs = 1;
1.12      kristaps 1402:        dosection(p, cmd, buf, sz, pos);
1.1       kristaps 1403: }
                   1404:
                   1405: static void
                   1406: doitem(struct texi *p, enum texicmd cmd,
                   1407:        const char *buf, size_t sz, size_t *pos)
                   1408: {
                   1409:
1.18      kristaps 1410:        /* Multitable is using raw tbl(7). */
                   1411:        if (TEXILIST_TABLE == p->list) {
                   1412:                texiputchar(p, '\n');
                   1413:                return;
                   1414:        }
                   1415:
1.3       kristaps 1416:        if (p->outmacro)
                   1417:                texierr(p, "item in open line scope!?");
                   1418:        else if (p->literal)
                   1419:                texierr(p, "item in a literal scope!?");
                   1420:
                   1421:        switch (p->list) {
                   1422:        case (TEXILIST_ITEM):
1.5       kristaps 1423:                teximacroopen(p, "It");
1.3       kristaps 1424:                break;
                   1425:        case (TEXILIST_NOITEM):
1.5       kristaps 1426:                teximacro(p, "It");
1.3       kristaps 1427:                break;
                   1428:        default:
1.11      kristaps 1429:                texivspace(p);
1.3       kristaps 1430:                break;
                   1431:        }
1.18      kristaps 1432:
                   1433:        /* Trick so we don't start with Pp. */
1.11      kristaps 1434:        p->seenvs = 1;
1.3       kristaps 1435:        parseeoln(p, buf, sz, pos);
1.1       kristaps 1436:
1.3       kristaps 1437:        if (TEXILIST_ITEM == p->list)
                   1438:                teximacroclose(p);
1.9       kristaps 1439:        else if (p->outcol > 0)
1.1       kristaps 1440:                texiputchar(p, '\n');
1.18      kristaps 1441: }
                   1442:
                   1443: static void
                   1444: dotab(struct texi *p, enum texicmd cmd,
                   1445:        const char *buf, size_t sz, size_t *pos)
                   1446: {
                   1447:
                   1448:        /* This command is only useful in @multitable. */
                   1449:        if (TEXILIST_TABLE == p->list)
                   1450:                texiputchar(p, '\t');
                   1451: }
                   1452:
                   1453: static void
                   1454: domultitable(struct texi *p, enum texicmd cmd,
                   1455:        const char *buf, size_t sz, size_t *pos)
                   1456: {
                   1457:        enum texilist   sv = p->list;
1.32      kristaps 1458:        int             svliteral = p->literal;
1.18      kristaps 1459:        enum texicmd    type;
                   1460:        size_t          i, end, columns;
                   1461:
                   1462:        p->list = TEXILIST_TABLE;
1.32      kristaps 1463:        /*
                   1464:         * TS/TE blocks aren't "in mdoc(7)", so we can disregard the
                   1465:         * fact that we're in literal mode right now.
                   1466:         */
                   1467:        p->literal = 0;
1.18      kristaps 1468:        teximacro(p, "TS");
                   1469:        columns = 0;
                   1470:
                   1471:        /* Advance to the first argument... */
                   1472:        while (*pos < sz && isws(buf[*pos]))
                   1473:                advance(p, buf, pos);
                   1474:
                   1475:        /* Make sure we don't print anything when scanning. */
                   1476:        p->ign++;
                   1477:        if ('@' == buf[*pos]) {
                   1478:                /*
                   1479:                 * Look for @columnfractions.
                   1480:                 * We ignore these, but we do use the number of
                   1481:                 * arguments to set the number of columns that we'll
                   1482:                 * have.
                   1483:                 */
1.30      kristaps 1484:                type = texicmd(p, buf, *pos, sz, &end, NULL);
1.18      kristaps 1485:                advanceto(p, buf, pos, end);
                   1486:                if (TEXICMD_COLUMNFRACTIONS != type)
1.25      kristaps 1487:                        texierr(p, "unknown multitable command");
1.18      kristaps 1488:                while (*pos < sz && '\n' != buf[*pos]) {
                   1489:                        while (*pos < sz && isws(buf[*pos]))
                   1490:                                advance(p, buf, pos);
                   1491:                        while (*pos < sz && ! isws(buf[*pos])) {
                   1492:                                if ('\n' == buf[*pos])
                   1493:                                        break;
                   1494:                                advance(p, buf, pos);
                   1495:                        }
                   1496:                        columns++;
                   1497:                }
                   1498:        } else
                   1499:                /*
                   1500:                 * We have arguments.
                   1501:                 * We could parse these, but it's easier to just let
                   1502:                 * tbl(7) figure it out.
                   1503:                 * So use this only to count arguments.
                   1504:                 */
                   1505:                while (parselinearg(p, buf, sz, pos) > 0)
                   1506:                        columns++;
                   1507:        p->ign--;
                   1508:
                   1509:        /* Left-justify each table entry. */
                   1510:        for (i = 0; i < columns; i++) {
                   1511:                if (i > 0)
                   1512:                        texiputchar(p, ' ');
                   1513:                texiputchar(p, 'l');
                   1514:        }
                   1515:        texiputchars(p, ".\n");
                   1516:        p->outmacro++;
                   1517:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
                   1518:        p->outmacro--;
                   1519:        teximacro(p, "TE");
1.32      kristaps 1520:        p->literal = svliteral;
1.18      kristaps 1521:        p->list = sv;
1.1       kristaps 1522: }
                   1523:
                   1524: static void
                   1525: dotable(struct texi *p, enum texicmd cmd,
                   1526:        const char *buf, size_t sz, size_t *pos)
                   1527: {
1.3       kristaps 1528:        enum texilist   sv = p->list;
                   1529:
                   1530:        p->list = TEXILIST_ITEM;
1.5       kristaps 1531:        teximacro(p, "Bl -tag -width Ds");
1.12      kristaps 1532:        /* FIXME: ignore and parseeoln. */
                   1533:        advanceeoln(p, buf, sz, pos, 1);
1.11      kristaps 1534:        p->seenvs = 1;
1.20      kristaps 1535:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5       kristaps 1536:        teximacro(p, "El");
1.3       kristaps 1537:        p->list = sv;
1.1       kristaps 1538: }
                   1539:
                   1540: static void
1.2       kristaps 1541: doenumerate(struct texi *p, enum texicmd cmd,
                   1542:        const char *buf, size_t sz, size_t *pos)
                   1543: {
1.3       kristaps 1544:        enum texilist    sv = p->list;
1.2       kristaps 1545:
1.3       kristaps 1546:        p->list = TEXILIST_NOITEM;
1.5       kristaps 1547:        teximacro(p, "Bl -enum");
1.11      kristaps 1548:        p->seenvs = 1;
1.12      kristaps 1549:        /* FIXME: ignore and parseeoln. */
                   1550:        advanceeoln(p, buf, sz, pos, 1);
1.2       kristaps 1551:        parseto(p, buf, sz, pos, "enumerate");
1.5       kristaps 1552:        teximacro(p, "El");
1.3       kristaps 1553:        p->list = sv;
1.2       kristaps 1554: }
                   1555:
                   1556: static void
1.1       kristaps 1557: doitemize(struct texi *p, enum texicmd cmd,
                   1558:        const char *buf, size_t sz, size_t *pos)
                   1559: {
1.3       kristaps 1560:        enum texilist   sv = p->list;
1.1       kristaps 1561:
1.21      kristaps 1562:        p->list = TEXILIST_NOITEM;
1.5       kristaps 1563:        teximacro(p, "Bl -bullet");
1.11      kristaps 1564:        p->seenvs = 1;
1.12      kristaps 1565:        /* FIXME: ignore and parseeoln. */
                   1566:        advanceeoln(p, buf, sz, pos, 1);
1.1       kristaps 1567:        parseto(p, buf, sz, pos, "itemize");
1.5       kristaps 1568:        teximacro(p, "El");
1.3       kristaps 1569:        p->list = sv;
1.1       kristaps 1570: }
                   1571:
                   1572: static void
                   1573: doignbracket(struct texi *p, enum texicmd cmd,
                   1574:        const char *buf, size_t sz, size_t *pos)
                   1575: {
                   1576:
1.3       kristaps 1577:        p->ign++;
1.1       kristaps 1578:        parsebracket(p, buf, sz, pos);
1.3       kristaps 1579:        p->ign--;
1.1       kristaps 1580: }
                   1581:
                   1582: static void
                   1583: doignline(struct texi *p, enum texicmd cmd,
                   1584:        const char *buf, size_t sz, size_t *pos)
                   1585: {
                   1586:
1.12      kristaps 1587:        /* FIXME: ignore and parseeoln. */
1.3       kristaps 1588:        advanceeoln(p, buf, sz, pos, 1);
1.1       kristaps 1589: }
                   1590:
1.8       kristaps 1591: /*
                   1592:  * Parse colon-separated directories from "cp" (if not NULL) and returns
                   1593:  * the array of pointers.
                   1594:  * Prepends "base" to the array.
                   1595:  * This does NOT sanitise the directories!
                   1596:  */
1.5       kristaps 1597: static char **
1.27      kristaps 1598: parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz)
1.5       kristaps 1599: {
                   1600:        char             *tok, *str, *tofree;
                   1601:        const char       *cpp;
                   1602:        size_t            i;
                   1603:        char            **dirs;
                   1604:
                   1605:        *sz = NULL != (cpp = cp) ? 2 : 1;
                   1606:        if (*sz > 1)
                   1607:                for ( ; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
                   1608:                        cpp++;
                   1609:
1.27      kristaps 1610:        if (NULL == (dirs = calloc(*sz, sizeof(char *))))
                   1611:                texiabort(p, NULL);
                   1612:        else if (NULL == (dirs[0] = strdup(base)))
                   1613:                texiabort(p, NULL);
1.5       kristaps 1614:
                   1615:        if (NULL == cp)
                   1616:                return(dirs);
                   1617:
1.27      kristaps 1618:        if (NULL == (tofree = tok = str = strdup(cp)))
                   1619:                texiabort(p, NULL);
1.5       kristaps 1620:
                   1621:        for (i = 1; NULL != (tok = strsep(&str, ":")); i++)
1.27      kristaps 1622:                if (NULL == (dirs[i] = strdup(tok)))
                   1623:                        texiabort(p, NULL);
1.5       kristaps 1624:
                   1625:        free(tofree);
                   1626:        return(dirs);
                   1627: }
                   1628:
1.1       kristaps 1629: int
                   1630: main(int argc, char *argv[])
                   1631: {
                   1632:        struct texi      texi;
1.2       kristaps 1633:        int              c;
                   1634:        char            *path, *dir;
1.10      kristaps 1635:        const char      *progname, *Idir, *cp;
1.1       kristaps 1636:
                   1637:        progname = strrchr(argv[0], '/');
                   1638:        if (progname == NULL)
                   1639:                progname = argv[0];
                   1640:        else
                   1641:                ++progname;
                   1642:
1.10      kristaps 1643:        memset(&texi, 0, sizeof(struct texi));
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;
                   1656:        if (0 == (argc -= optind))
                   1657:                goto usage;
                   1658:
1.27      kristaps 1659:        if (NULL == (path = strdup(argv[0])))
                   1660:                texiabort(&texi, NULL);
                   1661:        else if (NULL == (dir = dirname(path)))
                   1662:                texiabort(&texi, NULL);
                   1663:
1.2       kristaps 1664:        free(path);
                   1665:
1.10      kristaps 1666:        if (NULL != (cp = strrchr(argv[0], '/')))
                   1667:                texi.title = strdup(cp + 1);
                   1668:        else
                   1669:                texi.title = strdup(argv[0]);
                   1670:
1.27      kristaps 1671:        if (NULL == texi.title)
                   1672:                texiabort(&texi, NULL);
                   1673:        else if (NULL != (path = strchr(texi.title, '.')))
1.10      kristaps 1674:                *path = '\0';
                   1675:
1.3       kristaps 1676:        texi.ign = 1;
1.27      kristaps 1677:        texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
1.16      kristaps 1678:        parsefile(&texi, argv[0], 1);
1.5       kristaps 1679:        /* We shouldn't get here. */
1.2       kristaps 1680:        texiexit(&texi);
                   1681:        return(EXIT_FAILURE);
1.1       kristaps 1682: usage:
1.8       kristaps 1683:        fprintf(stderr, "usage: %s [-Idirs] file\n", progname);
1.1       kristaps 1684:        return(EXIT_FAILURE);
                   1685: }

CVSweb