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

Annotation of texi2mdoc/main.c, Revision 1.38

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

CVSweb