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

Annotation of texi2mdoc/main.c, Revision 1.30

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

CVSweb