version 1.47, 2015/02/25 15:04:13 |
version 1.56, 2015/03/01 13:39:51 |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <time.h> |
|
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "extern.h" |
#include "extern.h" |
Line 296 static const struct texitok __texitoks[TEXICMD__MAX] = |
|
Line 295 static const struct texitok __texitoks[TEXICMD__MAX] = |
|
{ dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */ |
{ dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */ |
{ doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */ |
{ doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */ |
{ doignline, "summarycontents", 15 }, /* TEXICMD_SUMMARYCONTENTS */ |
{ doignline, "summarycontents", 15 }, /* TEXICMD_SUMMARYCONTENTS */ |
{ doignline, "synindex", 8 }, /* TEXICMD_SYNINDEX */ |
{ dodefindex, "synindex", 8 }, /* TEXICMD_SYNINDEX */ |
{ doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */ |
{ dodefindex, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */ |
{ doinline, "t", 1 }, /* TEXICMD_T */ |
{ doinline, "t", 1 }, /* TEXICMD_T */ |
{ dotab, "tab", 3 }, /* TEXICMD_TAB */ |
{ dotab, "tab", 3 }, /* TEXICMD_TAB */ |
{ dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */ |
{ dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */ |
Line 432 dodefn(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 431 dodefn(struct texi *p, enum texicmd cmd, size_t *pos) |
|
break; |
break; |
} |
} |
|
|
texiputchars(p, ":\n"); |
texiputchar(p, ':'); |
|
texiputchar(p, '\n'); |
|
|
switch (cmd) { |
switch (cmd) { |
case (TEXICMD_DEFMAC): |
case (TEXICMD_DEFMAC): |
Line 497 dodefn(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 497 dodefn(struct texi *p, enum texicmd cmd, size_t *pos) |
|
abort(); |
abort(); |
} |
} |
|
|
texivspace(p); |
if (NULL == blk) |
if (NULL != blk) |
return; |
parseto(p, pos, blk); |
|
|
/* |
|
* All "block" definitions have their block bodies indented |
|
* unless they have the "x" form of the command following. |
|
* E.g., |
|
* @deffn some function |
|
* @deffnx another |
|
* An explanation. |
|
* @end deffn |
|
* With this loop, we delay opening the indented block until we |
|
* skipped past conformant macros. |
|
*/ |
|
for (;;) { |
|
switch (peekcmd(p, *pos)) { |
|
case (TEXICMD_DEFFNX): |
|
case (TEXICMD_DEFMACX): |
|
case (TEXICMD_DEFTPX): |
|
case (TEXICMD_DEFTYPEFNX): |
|
case (TEXICMD_DEFTYPEFUNX): |
|
case (TEXICMD_DEFTYPEMETHODX): |
|
case (TEXICMD_DEFTYPEVARX): |
|
case (TEXICMD_DEFTYPEVRX): |
|
case (TEXICMD_DEFUNX): |
|
case (TEXICMD_DEFVARX): |
|
case (TEXICMD_DEFVRX): |
|
texivspace(p); |
|
parseeoln(p, pos); |
|
continue; |
|
default: |
|
break; |
|
} |
|
break; |
|
} |
|
teximacro(p, "Bd -filled -offset indent"); |
|
p->seenvs = 1; |
|
parseto(p, pos, blk); |
|
teximacro(p, "Ed"); |
} |
} |
|
|
static void |
static void |
Line 613 doignblock(struct texi *p, enum texicmd cmd, size_t *p |
|
Line 649 doignblock(struct texi *p, enum texicmd cmd, size_t *p |
|
* Thus, we keep track of scopes for matching "end" blocks. |
* Thus, we keep track of scopes for matching "end" blocks. |
*/ |
*/ |
while (stack > 0 && *pos < BUFSZ(p)) { |
while (stack > 0 && *pos < BUFSZ(p)) { |
if (stack > 10) |
if (stack > 64) |
abort(); |
texierr(p, "run-away nested stack?"); |
endt = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, end, esz); |
endt = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, end, esz); |
startt = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, start, ssz); |
startt = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, start, ssz); |
if (NULL == endt) { |
if (NULL == endt) { |
Line 689 doinline(struct texi *p, enum texicmd cmd, size_t *pos |
|
Line 725 doinline(struct texi *p, enum texicmd cmd, size_t *pos |
|
} |
} |
|
|
if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) { |
if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) { |
parsebracket(p, pos); |
parsebracket(p, pos, 0); |
return; |
return; |
} |
} |
|
|
teximacroopen(p, macro); |
teximacroopen(p, macro); |
p->seenws = 0; |
p->seenws = 0; |
parsebracket(p, pos); |
parsebracket(p, pos, 0); |
texipunctuate(p, pos); |
texipunctuate(p, pos); |
teximacroclose(p); |
teximacroclose(p); |
} |
} |
Line 727 doverb(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 763 doverb(struct texi *p, enum texicmd cmd, size_t *pos) |
|
break; |
break; |
advance(p, pos); |
advance(p, pos); |
} |
} |
if (*pos == BUFSZ(p) - 1) |
if (*pos >= BUFSZ(p) - 1) |
return; |
return; |
texiputbuf(p, start, *pos); |
texiputbuf(p, start, *pos); |
|
|
|
|
dobracket(struct texi *p, enum texicmd cmd, size_t *pos) |
dobracket(struct texi *p, enum texicmd cmd, size_t *pos) |
{ |
{ |
|
|
parsebracket(p, pos); |
parsebracket(p, pos, 0); |
} |
} |
|
|
static void |
static void |
Line 954 dotitle(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 990 dotitle(struct texi *p, enum texicmd cmd, size_t *pos) |
|
start = end = *pos; |
start = end = *pos; |
while (end < BUFSZ(p) && '\n' != BUF(p)[end]) |
while (end < BUFSZ(p) && '\n' != BUF(p)[end]) |
end++; |
end++; |
|
if (end < BUFSZ(p)) |
|
end++; |
advanceeoln(p, pos, 1); |
advanceeoln(p, pos, 1); |
free(p->subtitle); |
free(p->subtitle); |
p->subtitle = malloc(end - start + 1); |
p->subtitle = malloc(end - start + 1); |
Line 989 doaccent(struct texi *p, enum texicmd cmd, size_t *pos |
|
Line 1027 doaccent(struct texi *p, enum texicmd cmd, size_t *pos |
|
if ('{' == BUF(p)[*pos]) { |
if ('{' == BUF(p)[*pos]) { |
brace = 1; |
brace = 1; |
advance(p, pos); |
advance(p, pos); |
} else if (isalpha(texitoks[cmd].tok[0])) |
} else if (isalpha((unsigned char)texitoks[cmd].tok[0])) |
while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) |
while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) |
advance(p, pos); |
advance(p, pos); |
|
|
Line 1311 dosymbol(struct texi *p, enum texicmd cmd, size_t *pos |
|
Line 1349 dosymbol(struct texi *p, enum texicmd cmd, size_t *pos |
|
} |
} |
|
|
/* Alphabetic commands have braces we ignore. */ |
/* Alphabetic commands have braces we ignore. */ |
if (isalpha(texitoks[cmd].tok[0])) |
if (isalpha((unsigned char)texitoks[cmd].tok[0])) |
doignbracket(p, cmd, pos); |
doignbracket(p, cmd, pos); |
} |
} |
|
|
Line 1327 doquotation(struct texi *p, enum texicmd cmd, size_t * |
|
Line 1365 doquotation(struct texi *p, enum texicmd cmd, size_t * |
|
static void |
static void |
domath(struct texi *p, enum texicmd cmd, size_t *pos) |
domath(struct texi *p, enum texicmd cmd, size_t *pos) |
{ |
{ |
size_t nest, start; |
|
|
|
/* |
parsebracket(p, pos, 1); |
* Math handling is different from everything else. |
|
* We don't allow any subcomponents, and we ignore the rules in |
|
* terms of @-commands. |
|
* This departs from GNU's rules, but whatever. |
|
*/ |
|
while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) |
|
advance(p, pos); |
|
if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos]) |
|
return; |
|
advance(p, pos); |
|
if (p->seenws && p->outcol && 0 == p->literal) |
|
texiputchar(p, ' '); |
|
p->seenws = 0; |
|
for (nest = 1, start = *pos; *pos < BUFSZ(p) && nest > 0; ) { |
|
if ('{' == BUF(p)[*pos]) |
|
nest++; |
|
else if ('}' == BUF(p)[*pos]) |
|
if (0 == --nest) |
|
continue; |
|
advance(p, pos); |
|
} |
|
if (*pos == BUFSZ(p)) |
|
return; |
|
assert('}' == BUF(p)[*pos]); |
|
texiputbuf(p, start, *pos); |
|
advance(p, pos); |
|
} |
} |
|
|
static void |
static void |
Line 1401 dovalue(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 1412 dovalue(struct texi *p, enum texicmd cmd, size_t *pos) |
|
texiputchar(p, ' '); |
texiputchar(p, ' '); |
p->seenws = 0; |
p->seenws = 0; |
if (NULL != (cp = valueblookup(p, pos))) |
if (NULL != (cp = valueblookup(p, pos))) |
texisplice(p, cp, strlen(cp), pos); |
texisplice(p, cp, strlen(cp), *pos); |
else |
else |
texiputchars(p, "{No value}"); |
texiputchars(p, "{No value}"); |
} else if (TEXICMD_IFCLEAR == cmd) { |
} else if (TEXICMD_IFCLEAR == cmd) { |
Line 1543 dosection(struct texi *p, enum texicmd cmd, size_t *po |
|
Line 1554 dosection(struct texi *p, enum texicmd cmd, size_t *po |
|
int sec; |
int sec; |
|
|
switch (cmd) { |
switch (cmd) { |
|
case (TEXICMD_TOP): |
|
sec = 0; |
|
break; |
case (TEXICMD_APPENDIX): |
case (TEXICMD_APPENDIX): |
case (TEXICMD_CHAPTER): |
case (TEXICMD_CHAPTER): |
case (TEXICMD_TOP): |
|
case (TEXICMD_UNNUMBERED): |
case (TEXICMD_UNNUMBERED): |
sec = sectioner(p, 0); |
sec = sectioner(p, 0); |
break; |
break; |
Line 1564 dosection(struct texi *p, enum texicmd cmd, size_t *po |
|
Line 1577 dosection(struct texi *p, enum texicmd cmd, size_t *po |
|
else if (p->literal) |
else if (p->literal) |
texierr(p, "\"%s\" in a literal scope!?", sects[sec]); |
texierr(p, "\"%s\" in a literal scope!?", sects[sec]); |
|
|
|
if (0 == sec && NULL != p->chapters) { |
|
teximdocclose(p, 0); |
|
teximdocopen(p, pos); |
|
} |
|
|
teximacroopen(p, sects[sec]); |
teximacroopen(p, sects[sec]); |
parseeoln(p, pos); |
parseeoln(p, pos); |
teximacroclose(p); |
teximacroclose(p); |
Line 1571 dosection(struct texi *p, enum texicmd cmd, size_t *po |
|
Line 1589 dosection(struct texi *p, enum texicmd cmd, size_t *po |
|
} |
} |
|
|
static void |
static void |
|
dotop(struct texi *p, enum texicmd cmd, size_t *pos) |
|
{ |
|
|
|
if (--p->ign) |
|
texierr(p, "@top command while ignoring"); |
|
|
|
if (NULL == p->chapters) |
|
teximdocopen(p, pos); |
|
dosection(p, cmd, pos); |
|
} |
|
|
|
static void |
dosp(struct texi *p, enum texicmd cmd, size_t *pos) |
dosp(struct texi *p, enum texicmd cmd, size_t *pos) |
{ |
{ |
|
|
Line 1583 dosp(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 1613 dosp(struct texi *p, enum texicmd cmd, size_t *pos) |
|
} |
} |
|
|
static void |
static void |
dotop(struct texi *p, enum texicmd cmd, size_t *pos) |
|
{ |
|
const char *cp; |
|
time_t t; |
|
char date[32]; |
|
|
|
if (--p->ign) |
|
texierr(p, "@top command while ignoring"); |
|
|
|
/* |
|
* Here we print our standard mdoc(7) prologue. |
|
* We use the title set with @settitle for the `Nd' description |
|
* and the source document filename (the first one as invoked on |
|
* the command line) for the title. |
|
* The date is set to the current date. |
|
*/ |
|
t = time(NULL); |
|
strftime(date, sizeof(date), "%F", localtime(&t)); |
|
|
|
teximacroopen(p, "Dd"); |
|
texiputchars(p, date); |
|
teximacroclose(p); |
|
teximacroopen(p, "Dt"); |
|
for (cp = p->title; '\0' != *cp; cp++) |
|
texiputchar(p, toupper(*cp)); |
|
texiputchars(p, " 7"); |
|
teximacroclose(p); |
|
teximacro(p, "Os"); |
|
teximacro(p, "Sh NAME"); |
|
teximacroopen(p, "Nm"); |
|
for (cp = p->title; '\0' != *cp; cp++) |
|
texiputchar(p, *cp); |
|
teximacroclose(p); |
|
teximacroopen(p, "Nd"); |
|
if (NULL != p->subtitle) |
|
for (cp = p->subtitle; '\0' != *cp; cp++) |
|
texiputchar(p, *cp); |
|
else |
|
texiputchars(p, "Unknown description"); |
|
teximacroclose(p); |
|
p->seenvs = 1; |
|
dosection(p, cmd, pos); |
|
} |
|
|
|
static void |
|
doitem(struct texi *p, enum texicmd cmd, size_t *pos) |
doitem(struct texi *p, enum texicmd cmd, size_t *pos) |
{ |
{ |
|
|
Line 1769 doend(struct texi *p, enum texicmd cmd, size_t *pos) |
|
Line 1754 doend(struct texi *p, enum texicmd cmd, size_t *pos) |
|
while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) |
while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) |
advance(p, pos); |
advance(p, pos); |
|
|
texiwarn(p, "unexpected \"end\": %.*s", (int)(*pos - start), &BUF(p)[start]); |
texiwarn(p, "unexpected \"end\": %.*s", |
|
(int)(*pos - start), &BUF(p)[start]); |
advanceeoln(p, pos, 1); |
advanceeoln(p, pos, 1); |
} |
} |
|
|
Line 1808 doignbracket(struct texi *p, enum texicmd cmd, size_t |
|
Line 1794 doignbracket(struct texi *p, enum texicmd cmd, size_t |
|
{ |
{ |
|
|
p->ign++; |
p->ign++; |
parsebracket(p, pos); |
parsebracket(p, pos, 0); |
p->ign--; |
p->ign--; |
} |
} |
|
|
Line 1875 main(int argc, char *argv[]) |
|
Line 1861 main(int argc, char *argv[]) |
|
|
|
memset(&texi, 0, sizeof(struct texi)); |
memset(&texi, 0, sizeof(struct texi)); |
texi.ign = 1; |
texi.ign = 1; |
|
texi.outfile = stdout; |
Idir = NULL; |
Idir = NULL; |
|
|
while (-1 != (c = getopt(argc, argv, "I:"))) |
while (-1 != (c = getopt(argc, argv, "C:I:"))) |
switch (c) { |
switch (c) { |
|
case ('C'): |
|
texi.chapters = optarg; |
|
break; |
case ('I'): |
case ('I'): |
Idir = optarg; |
Idir = optarg; |
break; |
break; |
Line 1912 main(int argc, char *argv[]) |
|
Line 1902 main(int argc, char *argv[]) |
|
} |
} |
|
|
texiexit(&texi); |
texiexit(&texi); |
return(EXIT_FAILURE); |
exit(EXIT_SUCCESS); |
usage: |
usage: |
fprintf(stderr, "usage: %s [-Idirs] [file]\n", progname); |
fprintf(stderr, "usage: %s [-Cdir] [-Idirs] [file]\n", progname); |
return(EXIT_FAILURE); |
return(EXIT_FAILURE); |
} |
} |