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

Annotation of texi2mdoc/main.c, Revision 1.33

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

CVSweb