| 1 |
/* |
| 2 |
* input.c: read the source form |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <assert.h> |
| 7 |
#include <time.h> |
| 8 |
#include "halibut.h" |
| 9 |
|
| 10 |
#define TAB_STOP 8 /* for column number tracking */ |
| 11 |
|
| 12 |
static void setpos(input * in, char *fname) |
| 13 |
{ |
| 14 |
in->pos.filename = fname; |
| 15 |
in->pos.line = 1; |
| 16 |
in->pos.col = (in->reportcols ? 1 : -1); |
| 17 |
} |
| 18 |
|
| 19 |
static void unget(input * in, int c, filepos * pos) |
| 20 |
{ |
| 21 |
if (in->npushback >= in->pushbacksize) |
| 22 |
{ |
| 23 |
in->pushbacksize = in->npushback + 16; |
| 24 |
in->pushback = resize(in->pushback, in->pushbacksize); |
| 25 |
} |
| 26 |
in->pushback[in->npushback].chr = c; |
| 27 |
in->pushback[in->npushback].pos = *pos; /* structure copy */ |
| 28 |
in->npushback++; |
| 29 |
} |
| 30 |
|
| 31 |
/* ---------------------------------------------------------------------- */ |
| 32 |
/* |
| 33 |
* Macro subsystem |
| 34 |
*/ |
| 35 |
typedef struct macro_Tag macro; |
| 36 |
struct macro_Tag { |
| 37 |
wchar_t *name, *text; |
| 38 |
}; |
| 39 |
struct macrostack_Tag { |
| 40 |
macrostack *next; |
| 41 |
wchar_t *text; |
| 42 |
int ptr, npushback; |
| 43 |
filepos pos; |
| 44 |
}; |
| 45 |
static int macrocmp(void *av, void *bv) |
| 46 |
{ |
| 47 |
macro *a = (macro *) av, *b = (macro *) bv; |
| 48 |
return ustrcmp(a->name, b->name); |
| 49 |
} |
| 50 |
static void |
| 51 |
macrodef(tree234 * macros, wchar_t * name, wchar_t * text, filepos fpos) |
| 52 |
{ |
| 53 |
macro *m = mknew(macro); |
| 54 |
m->name = name; |
| 55 |
m->text = text; |
| 56 |
if (add234(macros, m) != m) |
| 57 |
{ |
| 58 |
error(err_macroexists, &fpos, name); |
| 59 |
sfree(name); |
| 60 |
sfree(text); |
| 61 |
} |
| 62 |
} |
| 63 |
static int |
| 64 |
macrolookup(tree234 * macros, input * in, wchar_t * name, filepos * pos) |
| 65 |
{ |
| 66 |
macro m, *gotit; |
| 67 |
m.name = name; |
| 68 |
gotit = find234(macros, &m, NULL); |
| 69 |
if (gotit) |
| 70 |
{ |
| 71 |
macrostack *expansion = mknew(macrostack); |
| 72 |
expansion->next = in->stack; |
| 73 |
expansion->text = gotit->text; |
| 74 |
expansion->pos = *pos; /* structure copy */ |
| 75 |
expansion->ptr = 0; |
| 76 |
expansion->npushback = in->npushback; |
| 77 |
in->stack = expansion; |
| 78 |
return TRUE; |
| 79 |
} else |
| 80 |
return FALSE; |
| 81 |
} |
| 82 |
static void macrocleanup(tree234 * macros) |
| 83 |
{ |
| 84 |
int ti; |
| 85 |
macro *m; |
| 86 |
for (ti = 0; (m = (macro *) index234(macros, ti)) != NULL; ti++) |
| 87 |
{ |
| 88 |
sfree(m->name); |
| 89 |
sfree(m->text); |
| 90 |
sfree(m); |
| 91 |
} |
| 92 |
freetree234(macros); |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* Can return EOF |
| 97 |
*/ |
| 98 |
static int get(input * in, filepos * pos) |
| 99 |
{ |
| 100 |
int pushbackpt = in->stack ? in->stack->npushback : 0; |
| 101 |
if (in->npushback > pushbackpt) |
| 102 |
{ |
| 103 |
--in->npushback; |
| 104 |
if (pos) |
| 105 |
*pos = in->pushback[in->npushback].pos; /* structure copy */ |
| 106 |
return in->pushback[in->npushback].chr; |
| 107 |
} else if (in->stack) |
| 108 |
{ |
| 109 |
wchar_t c = in->stack->text[in->stack->ptr]; |
| 110 |
if (pos) *pos = in->stack->pos; |
| 111 |
if (in->stack->text[++in->stack->ptr] == L'\0') |
| 112 |
{ |
| 113 |
macrostack *tmp = in->stack; |
| 114 |
in->stack = tmp->next; |
| 115 |
sfree(tmp); |
| 116 |
} |
| 117 |
return c; |
| 118 |
} else if (in->currfp) |
| 119 |
{ |
| 120 |
int c = getc(in->currfp); |
| 121 |
|
| 122 |
if (c == EOF) |
| 123 |
{ |
| 124 |
fclose(in->currfp); |
| 125 |
in->currfp = NULL; |
| 126 |
} |
| 127 |
/* Track line numbers, for error reporting */ |
| 128 |
if (pos) |
| 129 |
*pos = in->pos; |
| 130 |
if (in->reportcols) |
| 131 |
{ |
| 132 |
switch (c) |
| 133 |
{ |
| 134 |
case '\t': |
| 135 |
in->pos.col = 1 + (in->pos.col + TAB_STOP - 1) % TAB_STOP; |
| 136 |
break; |
| 137 |
case '\n': |
| 138 |
in->pos.col = 1; |
| 139 |
in->pos.line++; |
| 140 |
break; |
| 141 |
default: |
| 142 |
in->pos.col++; |
| 143 |
break; |
| 144 |
} |
| 145 |
} else |
| 146 |
{ |
| 147 |
in->pos.col = -1; |
| 148 |
if (c == '\n') |
| 149 |
in->pos.line++; |
| 150 |
} |
| 151 |
/* FIXME: do input charmap translation. We should be returning |
| 152 |
* Unicode here. */ |
| 153 |
if (c == '\0') |
| 154 |
{ |
| 155 |
err_zerochar(pos); |
| 156 |
return EOF; |
| 157 |
} |
| 158 |
return c; |
| 159 |
} else |
| 160 |
return EOF; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Lexical analysis of source files. |
| 165 |
*/ |
| 166 |
typedef struct token_Tag token; |
| 167 |
struct token_Tag { |
| 168 |
int type; |
| 169 |
int cmd, aux; |
| 170 |
wchar_t *text; |
| 171 |
filepos pos; |
| 172 |
}; |
| 173 |
enum { |
| 174 |
tok_eof, /* end of file */ |
| 175 |
tok_eop, /* end of paragraph */ |
| 176 |
tok_white, /* whitespace */ |
| 177 |
tok_word, /* a word or word fragment */ |
| 178 |
tok_cmd, /* \command */ |
| 179 |
tok_lbrace, /* { */ |
| 180 |
tok_rbrace /* } */ |
| 181 |
}; |
| 182 |
|
| 183 |
#define tokiscmd(t,c) ( (t).type == tok_cmd && (t).cmd == (c) ) |
| 184 |
|
| 185 |
/* Halibut command keywords. */ |
| 186 |
enum { |
| 187 |
c__invalid, /* invalid command */ |
| 188 |
c__comment, /* comment command (\#) */ |
| 189 |
c__escaped, /* escaped character */ |
| 190 |
c__nop, /* no-op */ |
| 191 |
c__nbsp, /* nonbreaking space */ |
| 192 |
c__midparacmd_unixnow, |
| 193 |
c_A, /* appendix heading */ |
| 194 |
c_B, /* bibliography entry */ |
| 195 |
c_BR, /* bibliography rewrite */ |
| 196 |
c_C, /* chapter heading */ |
| 197 |
c_H, /* heading */ |
| 198 |
c_I, /* invisible index mark */ |
| 199 |
c_IM, /* index merge/rewrite */ |
| 200 |
c_K, /* capitalised cross-reference */ |
| 201 |
c_S, /* aux field is 0, 1, 2, ... */ |
| 202 |
c_U, /* unnumbered-chapter heading */ |
| 203 |
c_W, /* Web hyperlink */ |
| 204 |
c_L, /* Relative/local hyperlink */ |
| 205 |
c_b, /* bulleted list */ |
| 206 |
c_bold, |
| 207 |
c_c, /* code */ |
| 208 |
c_cfg, /* configuration directive */ |
| 209 |
c_copyright, /* copyright statement */ |
| 210 |
c_cw, /* weak code */ |
| 211 |
c_date, /* document processing date */ |
| 212 |
c_define, /* macro definition */ |
| 213 |
c_e, /* emphasis */ |
| 214 |
c_html, /* html code */ |
| 215 |
c_i, /* visible index mark */ |
| 216 |
c_ii, /* uncapitalised visible index mark */ |
| 217 |
c_k, /* uncapitalised cross-reference */ |
| 218 |
c_R, /* free text cross-reference */ |
| 219 |
c_n, /* numbered list */ |
| 220 |
c_nocite, /* bibliography trickery */ |
| 221 |
c_preamble, /* document preamble text */ |
| 222 |
c_q, /* quote marks */ |
| 223 |
c_rule, /* horizontal rule */ |
| 224 |
c_s, /* strong */ |
| 225 |
c_title, /* document title */ |
| 226 |
c_u, /* aux field is char code */ |
| 227 |
c_versionid /* document RCS id */ |
| 228 |
}; |
| 229 |
|
| 230 |
#define getcmdstyle(c) \ |
| 231 |
(c) == c_c ? word_Code : \ |
| 232 |
(c) == c_cw ? word_WeakCode : \ |
| 233 |
(c) == c_e ? word_Emph : \ |
| 234 |
(c) == c_s ? word_Strong : \ |
| 235 |
(c) == c_bold ? word_Bold : \ |
| 236 |
(c) == c_html ? word_Html /* does c_html belong here? */ : \ |
| 237 |
word_Normal |
| 238 |
|
| 239 |
|
| 240 |
/* Perhaps whitespace should be defined in a more Unicode-friendly way? */ |
| 241 |
#define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 ) |
| 242 |
#define isnl(c) ( (c)==10 ) |
| 243 |
#define isdec(c) ( ((c)>='0'&&(c)<='9') ) |
| 244 |
#define fromdec(c) ( (c)-'0' ) |
| 245 |
#define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f')) |
| 246 |
#define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) ) |
| 247 |
#define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z')) |
| 248 |
|
| 249 |
/* |
| 250 |
* Keyword comparison function. Like strcmp, but between a wchar_t * |
| 251 |
* and a char *. |
| 252 |
*/ |
| 253 |
static int kwcmp(wchar_t const *p, char const *q) |
| 254 |
{ |
| 255 |
int i; |
| 256 |
do |
| 257 |
{ |
| 258 |
i = *p - *q; |
| 259 |
} |
| 260 |
while (*p++ && *q++ && !i); |
| 261 |
return i; |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* Match a keyword. |
| 266 |
*/ |
| 267 |
static void match_kw(token * tok) |
| 268 |
{ |
| 269 |
/* |
| 270 |
* FIXME. The ids are explicit in here so as to allow long-name |
| 271 |
* equivalents to the various very short keywords. |
| 272 |
* |
| 273 |
* This list must be sorted, it's searched using binary search. |
| 274 |
*/ |
| 275 |
static const struct { |
| 276 |
char const *name; |
| 277 |
int id; |
| 278 |
} keywords[] = { |
| 279 |
{ "#", c__comment }, /* comment command (\#) */ |
| 280 |
{ "-", c__escaped }, /* nonbreaking hyphen */ |
| 281 |
{ ".", c__nop }, |
| 282 |
{ "A", c_A }, /* appendix heading */ |
| 283 |
{ "B", c_B }, /* bibliography entry */ |
| 284 |
{ "BR", c_BR }, /* bibliography rewrite */ |
| 285 |
{ "C", c_C }, /* chapter heading */ |
| 286 |
{ "H", c_H }, /* heading */ |
| 287 |
{ "I", c_I }, /* invisible index mark */ |
| 288 |
{ "IM", c_IM }, /* index merge/rewrite */ |
| 289 |
{ "K", c_K }, /* capitalised cross-reference */ |
| 290 |
{ "L", c_L }, /* Relative/local hyperlink */ |
| 291 |
{ "R", c_R }, /* free text cross-reference */ |
| 292 |
{ "U", c_U }, /* unnumbered-chapter heading */ |
| 293 |
{ "W", c_W }, /* Web hyperlink */ |
| 294 |
{ "\\", c__escaped }, /* escaped backslash (\\) */ |
| 295 |
{ "_", c__nbsp }, /* nonbreaking space (\_) */ |
| 296 |
{ "b", c_b }, /* bulleted list */ |
| 297 |
{ "bold", c_bold }, |
| 298 |
{ "c", c_c }, /* code */ |
| 299 |
{ "cfg", c_cfg }, /* configuration directive */ |
| 300 |
{ "copyright", c_copyright }, /* copyright statement */ |
| 301 |
{ "cw", c_cw } , /* weak code */ |
| 302 |
{ "date", c_date }, /* document processing date */ |
| 303 |
{ "define", c_define }, /* macro definition */ |
| 304 |
{ "e", c_e }, /* emphasis */ |
| 305 |
{ "hackunixnow", c__midparacmd_unixnow }, |
| 306 |
{ "html", c_html }, |
| 307 |
{ "i", c_i }, /* visible index mark */ |
| 308 |
{ "ii", c_ii }, /* uncapitalised visible index mark */ |
| 309 |
{ "k", c_k }, /* uncapitalised cross-reference */ |
| 310 |
{ "n", c_n }, /* numbered list */ |
| 311 |
{ "nocite", c_nocite }, /* bibliography trickery */ |
| 312 |
{ "preamble", c_preamble }, /* document preamble text */ |
| 313 |
{ "q", c_q }, /* quote marks */ |
| 314 |
{ "rule", c_rule }, /* horizontal rule */ |
| 315 |
{ "s", c_s }, /* strong */ |
| 316 |
{ "title", c_title }, /* document title */ |
| 317 |
{ "versionid", c_versionid }, /* document RCS id */ |
| 318 |
{ "{", c__escaped }, /* escaped lbrace (\{) */ |
| 319 |
{ "}", c__escaped }, /* escaped rbrace (\}) */ |
| 320 |
}; |
| 321 |
int i, j, k, c; |
| 322 |
|
| 323 |
/* |
| 324 |
* Special cases: \S{0,1,2,...} and \uABCD. If the syntax |
| 325 |
* doesn't match correctly, we just fall through to the |
| 326 |
* binary-search phase. |
| 327 |
*/ |
| 328 |
if (tok->text[0] == 'S') |
| 329 |
{ |
| 330 |
/* We expect numeric characters thereafter. */ |
| 331 |
wchar_t *p = tok->text + 1; |
| 332 |
int n; |
| 333 |
if (!*p) |
| 334 |
n = 1; |
| 335 |
else |
| 336 |
{ |
| 337 |
n = 0; |
| 338 |
while (*p && isdec(*p)) |
| 339 |
{ |
| 340 |
n = 10 * n + fromdec(*p); |
| 341 |
p++; |
| 342 |
} |
| 343 |
} |
| 344 |
if (!*p) |
| 345 |
{ |
| 346 |
tok->cmd = c_S; |
| 347 |
tok->aux = n; |
| 348 |
return; |
| 349 |
} |
| 350 |
} else if (tok->text[0] == 'u') |
| 351 |
{ |
| 352 |
/* We expect hex characters thereafter. */ |
| 353 |
wchar_t *p = tok->text + 1; |
| 354 |
int n = 0, seen_a_char = 0; |
| 355 |
while (*p && ishex(*p)) |
| 356 |
{ |
| 357 |
n = 16 * n + fromhex(*p); |
| 358 |
p++, seen_a_char++; |
| 359 |
} |
| 360 |
if (!*p && seen_a_char) |
| 361 |
{ |
| 362 |
tok->cmd = c_u; |
| 363 |
tok->aux = n; |
| 364 |
return; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
i = -1; |
| 369 |
j = sizeof(keywords) / sizeof(*keywords); |
| 370 |
while (j - i > 1) |
| 371 |
{ |
| 372 |
k = (i + j) / 2; |
| 373 |
c = kwcmp(tok->text, keywords[k].name); |
| 374 |
if (c < 0) |
| 375 |
j = k; |
| 376 |
else if (c > 0) |
| 377 |
i = k; |
| 378 |
else |
| 379 |
{ /* c == 0 */ |
| 380 |
|
| 381 |
tok->cmd = keywords[k].id; |
| 382 |
return; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
tok->cmd = c__invalid; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
/* |
| 391 |
* Read a token from the input file, in the normal way (`normal' in |
| 392 |
* the sense that code paragraphs work a different way). |
| 393 |
*/ |
| 394 |
token get_token(input * in) |
| 395 |
{ |
| 396 |
int c; |
| 397 |
int nls; |
| 398 |
token ret; |
| 399 |
rdstring rs = { 0, 0, NULL }; |
| 400 |
filepos cpos; |
| 401 |
|
| 402 |
ret.cmd = c__invalid; |
| 403 |
ret.aux = FALSE; |
| 404 |
ret.text = NULL; /* default */ |
| 405 |
c = get(in, &cpos); |
| 406 |
ret.pos = cpos; |
| 407 |
if (iswhite(c)) |
| 408 |
{ /* tok_white or tok_eop */ |
| 409 |
nls = 0; |
| 410 |
do |
| 411 |
{ |
| 412 |
if (isnl(c)) |
| 413 |
nls++; |
| 414 |
} |
| 415 |
while ((c = get(in, &cpos)) != EOF && iswhite(c)); |
| 416 |
if (c == EOF) |
| 417 |
{ |
| 418 |
ret.type = tok_eof; |
| 419 |
return ret; |
| 420 |
} |
| 421 |
unget(in, c, &cpos); |
| 422 |
ret.type = (nls > 1 ? tok_eop : tok_white); |
| 423 |
return ret; |
| 424 |
} else if (c == EOF) |
| 425 |
{ /* tok_eof */ |
| 426 |
ret.type = tok_eof; |
| 427 |
return ret; |
| 428 |
} else if (c == '\\') |
| 429 |
{ /* tok_cmd */ |
| 430 |
c = get(in, &cpos); |
| 431 |
if (c == '-' || c == '\\' || c == '_' || |
| 432 |
c == '#' || c == '{' || c == '}' || c == '.') |
| 433 |
{ |
| 434 |
/* single-char command */ |
| 435 |
rdadd(&rs, (wchar_t)c); |
| 436 |
} else if (c == 'u') |
| 437 |
{ |
| 438 |
int len = 0; |
| 439 |
do |
| 440 |
{ |
| 441 |
rdadd(&rs, (wchar_t)c); |
| 442 |
len++; |
| 443 |
c = get(in, &cpos); |
| 444 |
} |
| 445 |
while (ishex(c) && len < 5); |
| 446 |
unget(in, c, &cpos); |
| 447 |
} else if (iscmd(c)) |
| 448 |
{ |
| 449 |
do |
| 450 |
{ |
| 451 |
rdadd(&rs, (wchar_t)c); |
| 452 |
c = get(in, &cpos); |
| 453 |
} |
| 454 |
while (iscmd(c)); |
| 455 |
unget(in, c, &cpos); |
| 456 |
} |
| 457 |
/* |
| 458 |
* Now match the command against the list of available |
| 459 |
* ones. |
| 460 |
*/ |
| 461 |
ret.type = tok_cmd; |
| 462 |
ret.text = ustrdup(rs.text); |
| 463 |
match_kw(&ret); |
| 464 |
sfree(rs.text); |
| 465 |
return ret; |
| 466 |
} else if (c == '{') |
| 467 |
{ /* tok_lbrace */ |
| 468 |
ret.type = tok_lbrace; |
| 469 |
return ret; |
| 470 |
} else if (c == '}') |
| 471 |
{ /* tok_rbrace */ |
| 472 |
ret.type = tok_rbrace; |
| 473 |
return ret; |
| 474 |
} else |
| 475 |
{ /* tok_word */ |
| 476 |
/* |
| 477 |
* Read a word: the longest possible contiguous sequence of |
| 478 |
* things other than whitespace, backslash, braces and |
| 479 |
* hyphen. A hyphen terminates the word but is returned as |
| 480 |
* part of it; everything else is pushed back for the next |
| 481 |
* token. The `aux' field contains TRUE if the word ends in |
| 482 |
* a hyphen. |
| 483 |
*/ |
| 484 |
ret.aux = FALSE; /* assumed for now */ |
| 485 |
while (1) |
| 486 |
{ |
| 487 |
if (iswhite(c) || c == '{' || c == '}' || c == '\\' || c == EOF) |
| 488 |
{ |
| 489 |
/* Put back the character that caused termination */ |
| 490 |
unget(in, c, &cpos); |
| 491 |
break; |
| 492 |
} else |
| 493 |
{ |
| 494 |
rdadd(&rs, (wchar_t)c); |
| 495 |
if (c == '-') |
| 496 |
{ |
| 497 |
ret.aux = TRUE; |
| 498 |
break; /* hyphen terminates word */ |
| 499 |
} |
| 500 |
} |
| 501 |
c = get(in, &cpos); |
| 502 |
} |
| 503 |
ret.type = tok_word; |
| 504 |
ret.text = ustrdup(rs.text); |
| 505 |
sfree(rs.text); |
| 506 |
return ret; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/* |
| 511 |
* Determine whether the next input character is an open brace (for |
| 512 |
* telling code paragraphs from paragraphs which merely start with |
| 513 |
* code). |
| 514 |
*/ |
| 515 |
int isbrace(input * in) |
| 516 |
{ |
| 517 |
int c; |
| 518 |
filepos cpos; |
| 519 |
|
| 520 |
c = get(in, &cpos); |
| 521 |
unget(in, c, &cpos); |
| 522 |
return (c == '{'); |
| 523 |
} |
| 524 |
|
| 525 |
/* |
| 526 |
* Read the rest of a line that starts `\c'. Including nothing at |
| 527 |
* all (tok_word with empty text). |
| 528 |
*/ |
| 529 |
token get_codepar_token(input * in) |
| 530 |
{ |
| 531 |
int c; |
| 532 |
token ret; |
| 533 |
rdstring rs = { 0, 0, NULL }; |
| 534 |
filepos cpos; |
| 535 |
|
| 536 |
ret.type = tok_word; |
| 537 |
c = get(in, &cpos); /* expect (and discard) one space */ |
| 538 |
ret.pos = cpos; |
| 539 |
if (c == ' ') |
| 540 |
{ |
| 541 |
c = get(in, &cpos); |
| 542 |
ret.pos = cpos; |
| 543 |
} |
| 544 |
while (!isnl(c) && c != EOF) |
| 545 |
{ |
| 546 |
int c2 = c; |
| 547 |
c = get(in, &cpos); |
| 548 |
/* Discard \r just before \n. */ |
| 549 |
if (c2 != 13 || !isnl(c)) |
| 550 |
rdadd(&rs, (wchar_t)c2); |
| 551 |
} |
| 552 |
unget(in, c, &cpos); |
| 553 |
ret.text = ustrdup(rs.text); |
| 554 |
sfree(rs.text); |
| 555 |
return ret; |
| 556 |
} |
| 557 |
|
| 558 |
/* |
| 559 |
* Adds a new word to a linked list |
| 560 |
*/ |
| 561 |
static word *addword(word newword, word *** hptrptr) |
| 562 |
{ |
| 563 |
word *mnewword; |
| 564 |
if (!hptrptr) |
| 565 |
return NULL; |
| 566 |
mnewword = mknew(word); |
| 567 |
*mnewword = newword; /* structure copy */ |
| 568 |
mnewword->next = NULL; |
| 569 |
**hptrptr = mnewword; |
| 570 |
*hptrptr = &mnewword->next; |
| 571 |
return mnewword; |
| 572 |
} |
| 573 |
|
| 574 |
/* |
| 575 |
* Adds a new paragraph to a linked list |
| 576 |
*/ |
| 577 |
static paragraph *addpara(paragraph newpara, paragraph *** hptrptr) |
| 578 |
{ |
| 579 |
paragraph *mnewpara = mknew(paragraph); |
| 580 |
*mnewpara = newpara; /* structure copy */ |
| 581 |
mnewpara->next = NULL; |
| 582 |
**hptrptr = mnewpara; |
| 583 |
*hptrptr = &mnewpara->next; |
| 584 |
return mnewpara; |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* Destructor before token is reassigned; should catch most memory |
| 589 |
* leaks |
| 590 |
*/ |
| 591 |
#define dtor(t) ( sfree(t.text) ) |
| 592 |
|
| 593 |
static int is_special_midpara_cmd(token*t) |
| 594 |
{ |
| 595 |
return tokiscmd(*t, c__midparacmd_unixnow); |
| 596 |
} |
| 597 |
|
| 598 |
static int handle_special_midpara_cmd(token*t, rdstring*rs, paragraph ***hptrptr) |
| 599 |
{ |
| 600 |
wchar_t wbuf[100]; |
| 601 |
paragraph par; |
| 602 |
|
| 603 |
if (t->type != tok_cmd) return 0; |
| 604 |
initpara(par); |
| 605 |
par.fpos = t->pos; |
| 606 |
switch(t->cmd) |
| 607 |
{ |
| 608 |
case c__midparacmd_unixnow: |
| 609 |
ultou(getutcunixtime(), wbuf); |
| 610 |
rdadds(rs, wbuf); |
| 611 |
return 1; |
| 612 |
} |
| 613 |
return 0; |
| 614 |
} |
| 615 |
|
| 616 |
#define stack_item_push(stck__, sitype__) do { \ |
| 617 |
struct stack_item *si__ = mknew(struct stack_item); \ |
| 618 |
si__->type = sitype__; \ |
| 619 |
stk_push((stck__), si__); \ |
| 620 |
} while(!__LINE__) |
| 621 |
|
| 622 |
|
| 623 |
/* |
| 624 |
* Reads a single file (ie until get() returns EOF) |
| 625 |
*/ |
| 626 |
static void read_file(paragraph *** ret, input * in, indexdata * idx, tree234 *macros) |
| 627 |
{ |
| 628 |
token t; |
| 629 |
paragraph par; |
| 630 |
word wd, **whptr, **idximplicit; |
| 631 |
wchar_t utext[2], *wdtext; |
| 632 |
int style, spcstyle, tmpstyle; |
| 633 |
int already; |
| 634 |
int iswhite, seenwhite; |
| 635 |
int type; |
| 636 |
struct stack_item { |
| 637 |
enum { |
| 638 |
stack_nop = 0, /* do nothing (for error recovery) */ |
| 639 |
stack_ualt = 1, /* \u alternative */ |
| 640 |
stack_style = 2, /* \e, \c, \cw */ |
| 641 |
stack_idx = 4, /* \I, \i, \ii */ |
| 642 |
stack_hyper = 8, /* \W */ |
| 643 |
stack_quote = 16, /* \q */ |
| 644 |
} type; |
| 645 |
word **whptr; /* to restore from \u alternatives */ |
| 646 |
word **idximplicit; /* to restore from \u alternatives */ |
| 647 |
} *sitem; |
| 648 |
stack parsestk; |
| 649 |
word *indexword=NULL, *uword=NULL, *iword=NULL; |
| 650 |
word *idxwordlist; |
| 651 |
rdstring indexstr; |
| 652 |
int index_downcase=0, index_visible=0, indexing=0; |
| 653 |
const rdstring nullrs = { 0, 0, NULL }; |
| 654 |
wchar_t uchr; |
| 655 |
|
| 656 |
t = get_token(in); |
| 657 |
already = TRUE; |
| 658 |
|
| 659 |
/* |
| 660 |
* Ignore tok_white if it appears at the very start of the file. |
| 661 |
* |
| 662 |
* At the start of most paragraphs, tok_white is guaranteed not to |
| 663 |
* appear, because get_token will have folded it into the |
| 664 |
* preceding tok_eop (since a tok_eop is simply a sequence of |
| 665 |
* whitespace containing at least two newlines). |
| 666 |
* |
| 667 |
* The one exception is if there isn't a preceding tok_eop, i.e. |
| 668 |
* if the very first paragraph begins with something that lexes as |
| 669 |
* a tok_white. Easiest way to get round that is to ignore it |
| 670 |
* here, by unsetting the 'already' flag which will force a new |
| 671 |
* token to be fetched below. |
| 672 |
*/ |
| 673 |
if (t.type == tok_white) |
| 674 |
already = FALSE; |
| 675 |
|
| 676 |
/* |
| 677 |
* Loop on each paragraph. |
| 678 |
*/ |
| 679 |
while (1) |
| 680 |
{ |
| 681 |
int start_cmd = c__invalid; |
| 682 |
par.words = NULL; |
| 683 |
par.keyword = NULL; |
| 684 |
whptr = &par.words; |
| 685 |
|
| 686 |
/* |
| 687 |
* Get a token. |
| 688 |
*/ |
| 689 |
if (!already) |
| 690 |
{ |
| 691 |
dtor(t), t = get_token(in); |
| 692 |
} |
| 693 |
already = FALSE; |
| 694 |
if (t.type == tok_eof) |
| 695 |
break; |
| 696 |
|
| 697 |
/* |
| 698 |
* Parse code paragraphs separately. |
| 699 |
*/ |
| 700 |
if (t.type == tok_cmd && t.cmd == c_c && !isbrace(in)) |
| 701 |
{ |
| 702 |
par.type = para_Code; |
| 703 |
par.fpos = t.pos; |
| 704 |
while (1) |
| 705 |
{ |
| 706 |
dtor(t), t = get_codepar_token(in); |
| 707 |
wd.type = word_WeakCode; |
| 708 |
wd.breaks = FALSE; /* shouldn't need this... */ |
| 709 |
wd.text = ustrdup(t.text); |
| 710 |
wd.alt = NULL; |
| 711 |
wd.fpos = t.pos; |
| 712 |
addword(wd, &whptr); |
| 713 |
dtor(t), t = get_token(in); |
| 714 |
if (t.type == tok_white) |
| 715 |
{ |
| 716 |
/* |
| 717 |
* The newline after a code-paragraph line |
| 718 |
*/ |
| 719 |
dtor(t), t = get_token(in); |
| 720 |
} |
| 721 |
if (t.type == tok_eop || t.type == tok_eof) |
| 722 |
break; |
| 723 |
else if (t.type != tok_cmd || t.cmd != c_c) |
| 724 |
{ |
| 725 |
error(err_brokencodepara, &t.pos); |
| 726 |
addpara(par, ret); |
| 727 |
while (t.type != tok_eop) /* error recovery: */ |
| 728 |
dtor(t), t = get_token(in); /* eat rest of paragraph */ |
| 729 |
goto codeparabroken; /* ick, but such is life */ |
| 730 |
} |
| 731 |
} |
| 732 |
addpara(par, ret); |
| 733 |
codeparabroken: |
| 734 |
continue; |
| 735 |
} |
| 736 |
|
| 737 |
while (t.type == tok_cmd && macrolookup(macros, in, t.text, &t.pos)) |
| 738 |
{ |
| 739 |
dtor(t), t = get_token(in); |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
/* |
| 744 |
* This token begins a paragraph. See if it's one of the |
| 745 |
* special commands that define a paragraph type. |
| 746 |
* |
| 747 |
* (note that \# is special in a way, and \nocite takes no |
| 748 |
* text) |
| 749 |
*/ |
| 750 |
par.type = para_Normal; |
| 751 |
if (t.type == tok_cmd) |
| 752 |
{ |
| 753 |
int needkw=0; |
| 754 |
int is_macro = FALSE; |
| 755 |
|
| 756 |
par.fpos = t.pos; |
| 757 |
switch (t.cmd) |
| 758 |
{ |
| 759 |
default: |
| 760 |
needkw = -1; |
| 761 |
break; |
| 762 |
case c__invalid: |
| 763 |
error(err_badparatype, t.text, &t.pos); |
| 764 |
needkw = 4; |
| 765 |
break; |
| 766 |
case c__comment: |
| 767 |
if (isbrace(in)) |
| 768 |
{ |
| 769 |
needkw = -1; // Upstream 56b96573 (r8312) |
| 770 |
break; /* `\#{': isn't a comment para */ |
| 771 |
} |
| 772 |
do |
| 773 |
{ |
| 774 |
dtor(t), t = get_token(in); |
| 775 |
} |
| 776 |
while (t.type != tok_eop && t.type != tok_eof); |
| 777 |
continue; /* next paragraph */ |
| 778 |
/* |
| 779 |
* `needkw' values: |
| 780 |
* |
| 781 |
* 1 -- exactly one keyword |
| 782 |
* 2 -- at least one keyword |
| 783 |
* 4 -- any number of keywords including zero |
| 784 |
* 8 -- at least one keyword and then nothing else |
| 785 |
* 16 -- nothing at all! no keywords, no body |
| 786 |
* 32 -- no keywords at all |
| 787 |
*/ |
| 788 |
case c_A: |
| 789 |
needkw = 2; |
| 790 |
par.type = para_Appendix; |
| 791 |
break; |
| 792 |
case c_B: |
| 793 |
needkw = 2; |
| 794 |
par.type = para_Biblio; |
| 795 |
break; |
| 796 |
case c_BR: |
| 797 |
needkw = 1; |
| 798 |
par.type = para_BR; |
| 799 |
start_cmd = c_BR; |
| 800 |
break; |
| 801 |
case c_C: |
| 802 |
needkw = 2; |
| 803 |
par.type = para_Chapter; |
| 804 |
break; |
| 805 |
case c_H: |
| 806 |
needkw = 2; |
| 807 |
par.type = para_Heading; |
| 808 |
par.aux = 0; |
| 809 |
break; |
| 810 |
case c_IM: |
| 811 |
needkw = 2; |
| 812 |
par.type = para_IM; |
| 813 |
start_cmd = c_IM; |
| 814 |
break; |
| 815 |
case c_S: |
| 816 |
needkw = 2; |
| 817 |
par.type = para_Subsect; |
| 818 |
par.aux = t.aux; |
| 819 |
break; |
| 820 |
case c_U: |
| 821 |
needkw = 32; |
| 822 |
par.type = para_UnnumberedChapter; |
| 823 |
break; |
| 824 |
/* For \b and \n the keyword is optional */ |
| 825 |
case c_b: |
| 826 |
needkw = 4; |
| 827 |
par.type = para_Bullet; |
| 828 |
break; |
| 829 |
case c_n: |
| 830 |
needkw = 4; |
| 831 |
par.type = para_NumberedList; |
| 832 |
break; |
| 833 |
case c_cfg: |
| 834 |
needkw = 8; |
| 835 |
par.type = para_Config; |
| 836 |
start_cmd = c_cfg; |
| 837 |
break; |
| 838 |
case c_copyright: |
| 839 |
needkw = 32; |
| 840 |
par.type = para_Copyright; |
| 841 |
break; |
| 842 |
case c_define: |
| 843 |
is_macro = TRUE; |
| 844 |
needkw = 1; |
| 845 |
break; |
| 846 |
/* For \nocite the keyword is _everything_ */ |
| 847 |
case c_nocite: |
| 848 |
needkw = 8; |
| 849 |
par.type = para_NoCite; |
| 850 |
break; |
| 851 |
case c_preamble: |
| 852 |
needkw = 32; |
| 853 |
par.type = para_Preamble; |
| 854 |
break; |
| 855 |
case c_rule: |
| 856 |
needkw = 16; |
| 857 |
par.type = para_Rule; |
| 858 |
break; |
| 859 |
case c_title: |
| 860 |
needkw = 32; |
| 861 |
par.type = para_Title; |
| 862 |
break; |
| 863 |
case c_versionid: |
| 864 |
needkw = 32; |
| 865 |
par.type = para_VersionID; |
| 866 |
break; |
| 867 |
} |
| 868 |
|
| 869 |
if (needkw > 0) |
| 870 |
{ |
| 871 |
rdstring rs = { 0, 0, NULL }; |
| 872 |
int nkeys = 0; |
| 873 |
filepos fp; |
| 874 |
|
| 875 |
/* Get keywords. */ |
| 876 |
dtor(t), t = get_token(in); |
| 877 |
fp = t.pos; |
| 878 |
while (t.type == tok_lbrace) |
| 879 |
{ |
| 880 |
/* This is a keyword. */ |
| 881 |
nkeys++; |
| 882 |
/* FIXME: there will be bugs if anyone specifies an |
| 883 |
* empty keyword (\foo{}), so trap this case. */ |
| 884 |
while (dtor(t), t = get_token(in), |
| 885 |
t.type == tok_word || |
| 886 |
t.type == tok_white || |
| 887 |
(t.type == tok_cmd && t.cmd == c__nbsp) || |
| 888 |
(t.type == tok_cmd && t.cmd == c__escaped) || |
| 889 |
/* TODO: Merge from upstream?: (t.type == tok_cmd && t.cmd == c_u) || */ |
| 890 |
is_special_midpara_cmd(&t)) |
| 891 |
{ |
| 892 |
if (t.type == tok_white || |
| 893 |
(t.type == tok_cmd && t.cmd == c__nbsp)) |
| 894 |
rdadd(&rs, ' '); |
| 895 |
else if (!handle_special_midpara_cmd(&t, &rs, ret)) |
| 896 |
rdadds(&rs, t.text); |
| 897 |
} |
| 898 |
if (t.type != tok_rbrace) |
| 899 |
{ |
| 900 |
error(err_kwunclosed, &t.pos); |
| 901 |
continue; |
| 902 |
} |
| 903 |
rdadd(&rs, 0); /* add string terminator */ |
| 904 |
dtor(t), t = get_token(in); /* eat right brace */ |
| 905 |
} |
| 906 |
|
| 907 |
rdadd(&rs, 0); /* add string terminator */ |
| 908 |
|
| 909 |
/* See whether we have the right number of keywords. */ |
| 910 |
if ((needkw & 48) && nkeys > 0) |
| 911 |
error(err_kwillegal, &fp); |
| 912 |
if ((needkw & 11) && nkeys == 0) |
| 913 |
error(err_kwexpected, &fp); |
| 914 |
if ((needkw & 5) && nkeys > 1) |
| 915 |
error(err_kwtoomany, &fp); |
| 916 |
|
| 917 |
if (is_macro) |
| 918 |
{ |
| 919 |
/* |
| 920 |
* Macro definition. Get the rest of the line |
| 921 |
* as a code-paragraph token, repeatedly until |
| 922 |
* there's nothing more left of it. Separate |
| 923 |
* with newlines. |
| 924 |
*/ |
| 925 |
rdstring macrotext = { 0, 0, NULL }; |
| 926 |
while (1) |
| 927 |
{ |
| 928 |
dtor(t), t = get_codepar_token(in); |
| 929 |
if (macrotext.pos > 0) |
| 930 |
rdadd(¯otext, L'\n'); |
| 931 |
rdadds(¯otext, t.text); |
| 932 |
dtor(t), t = get_token(in); |
| 933 |
if (t.type == tok_eop || t.type == tok_eof) |
| 934 |
break; |
| 935 |
} |
| 936 |
macrodef(macros, rs.text, macrotext.text, fp); |
| 937 |
continue; /* next paragraph */ |
| 938 |
} |
| 939 |
|
| 940 |
par.keyword = rdtrim(&rs); |
| 941 |
|
| 942 |
/* Move to EOP in case of needkw==8 or 16 (no body) */ |
| 943 |
if (needkw & 24) |
| 944 |
{ |
| 945 |
/* We allow whitespace even when we expect no para body */ |
| 946 |
while (t.type == tok_white) |
| 947 |
dtor(t), t = get_token(in); |
| 948 |
if (t.type != tok_eop && t.type != tok_eof && |
| 949 |
(start_cmd == c__invalid || |
| 950 |
t.type != tok_cmd || t.cmd != start_cmd)) |
| 951 |
{ |
| 952 |
error(err_bodyillegal, &t.pos); |
| 953 |
/* Error recovery: eat the rest of the paragraph */ |
| 954 |
while (t.type != tok_eop && t.type != tok_eof && |
| 955 |
(start_cmd == c__invalid || |
| 956 |
t.type != tok_cmd || t.cmd != start_cmd)) |
| 957 |
dtor(t), t = get_token(in); |
| 958 |
} |
| 959 |
if (t.type == tok_cmd) |
| 960 |
already = TRUE; /* inhibit get_token at top of loop */ |
| 961 |
addpara(par, ret); |
| 962 |
continue; /* next paragraph */ |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
/* |
| 968 |
* Now read the actual paragraph, word by word, adding to |
| 969 |
* the paragraph list. |
| 970 |
* |
| 971 |
* Mid-paragraph commands: |
| 972 |
* |
| 973 |
* \K \k |
| 974 |
* \c \cw |
| 975 |
* \e |
| 976 |
* \i \ii |
| 977 |
* \I |
| 978 |
* \u |
| 979 |
* \W |
| 980 |
* \date |
| 981 |
* \\ \{ \} |
| 982 |
*/ |
| 983 |
parsestk = stk_new(); |
| 984 |
style = word_Normal; |
| 985 |
spcstyle = word_WhiteSpace; |
| 986 |
indexing = FALSE; |
| 987 |
seenwhite = TRUE; |
| 988 |
while (t.type != tok_eop && t.type != tok_eof) |
| 989 |
{ |
| 990 |
iswhite = FALSE; |
| 991 |
already = FALSE; |
| 992 |
|
| 993 |
/* Handle implicit paragraph breaks after \IM, \BR etc */ |
| 994 |
if (start_cmd != c__invalid && |
| 995 |
t.type == tok_cmd && t.cmd == start_cmd) |
| 996 |
{ |
| 997 |
already = TRUE; /* inhibit get_token at top of loop */ |
| 998 |
break; |
| 999 |
} |
| 1000 |
|
| 1001 |
if (t.type == tok_cmd && t.cmd == c__nop) { |
| 1002 |
dtor(t), t = get_token(in); |
| 1003 |
continue; /* do nothing! */ |
| 1004 |
} |
| 1005 |
if (t.type == tok_cmd && t.cmd == c__escaped) |
| 1006 |
{ |
| 1007 |
t.type = tok_word; /* nice and simple */ |
| 1008 |
t.aux = 0; /* even if `\-' - nonbreaking! */ |
| 1009 |
} |
| 1010 |
if (t.type == tok_cmd && t.cmd == c__nbsp) |
| 1011 |
{ |
| 1012 |
t.type = tok_word; /* nice and simple */ |
| 1013 |
sfree(t.text); |
| 1014 |
t.text = ustrdup(L" "); /* text is ` ' not `_' */ |
| 1015 |
t.aux = 0; /* (nonbreaking) */ |
| 1016 |
} |
| 1017 |
switch (t.type) |
| 1018 |
{ |
| 1019 |
case tok_white: |
| 1020 |
if (whptr == &par.words) |
| 1021 |
break; /* strip whitespace at start of para */ |
| 1022 |
wd.text = NULL; |
| 1023 |
wd.type = spcstyle; |
| 1024 |
wd.alt = NULL; |
| 1025 |
wd.aux = 0; |
| 1026 |
wd.fpos = t.pos; |
| 1027 |
wd.breaks = FALSE; |
| 1028 |
|
| 1029 |
/* |
| 1030 |
* Inhibit use of whitespace if it's (probably the |
| 1031 |
* newline) before a repeat \IM / \BR type |
| 1032 |
* directive. |
| 1033 |
*/ |
| 1034 |
if (start_cmd != c__invalid) |
| 1035 |
{ |
| 1036 |
dtor(t), t = get_token(in); |
| 1037 |
already = TRUE; |
| 1038 |
if (t.type == tok_cmd && t.cmd == start_cmd) |
| 1039 |
break; |
| 1040 |
} |
| 1041 |
|
| 1042 |
if (indexing) |
| 1043 |
rdadd(&indexstr, ' '); |
| 1044 |
if (!indexing || index_visible) |
| 1045 |
addword(wd, &whptr); |
| 1046 |
if (indexing) |
| 1047 |
addword(wd, &idximplicit); |
| 1048 |
iswhite = TRUE; |
| 1049 |
break; |
| 1050 |
case tok_word: |
| 1051 |
if (indexing) |
| 1052 |
rdadds(&indexstr, t.text); |
| 1053 |
wd.type = style; |
| 1054 |
wd.alt = NULL; |
| 1055 |
wd.aux = 0; |
| 1056 |
wd.fpos = t.pos; |
| 1057 |
wd.breaks = t.aux; |
| 1058 |
if (!indexing || index_visible) |
| 1059 |
{ |
| 1060 |
wd.text = ustrdup(t.text); |
| 1061 |
addword(wd, &whptr); |
| 1062 |
} |
| 1063 |
if (indexing) |
| 1064 |
{ |
| 1065 |
wd.text = ustrdup(t.text); |
| 1066 |
addword(wd, &idximplicit); |
| 1067 |
} |
| 1068 |
break; |
| 1069 |
case tok_lbrace: |
| 1070 |
error(err_unexbrace, &t.pos); |
| 1071 |
/* Error recovery: push nop */ |
| 1072 |
sitem = mknew(struct stack_item); |
| 1073 |
sitem->type = stack_nop; |
| 1074 |
stk_push(parsestk, sitem); |
| 1075 |
break; |
| 1076 |
case tok_rbrace: |
| 1077 |
sitem = stk_pop(parsestk); |
| 1078 |
if (!sitem) |
| 1079 |
{ |
| 1080 |
#ifdef HALIBUT_UPSTREAM |
| 1081 |
/* |
| 1082 |
* This closing brace could have been an |
| 1083 |
* indication that the cross-paragraph stack |
| 1084 |
* wants popping. Accordingly, we treat it here |
| 1085 |
* as an indication that the paragraph is over. |
| 1086 |
*/ |
| 1087 |
already = TRUE; |
| 1088 |
goto finished_para; |
| 1089 |
#endif |
| 1090 |
error(err_unexbrace, &t.pos); |
| 1091 |
} |
| 1092 |
else |
| 1093 |
{ |
| 1094 |
if (sitem->type & stack_ualt) |
| 1095 |
{ |
| 1096 |
whptr = sitem->whptr; |
| 1097 |
idximplicit = sitem->idximplicit; |
| 1098 |
} |
| 1099 |
if (sitem->type & stack_style) |
| 1100 |
{ |
| 1101 |
style = word_Normal; |
| 1102 |
spcstyle = word_WhiteSpace; |
| 1103 |
} |
| 1104 |
if (sitem->type & stack_idx ) { |
| 1105 |
rdadds(&indexstr, L""); |
| 1106 |
indexword->text = ustrdup(indexstr.text); |
| 1107 |
if (index_downcase) |
| 1108 |
{ |
| 1109 |
#ifdef HALIBUT_UPSTREAM |
| 1110 |
word *w; |
| 1111 |
ustrlow(indexword->text); |
| 1112 |
ustrlow(indexstr.text); |
| 1113 |
for (w = idxwordlist; w; w = w->next) |
| 1114 |
if (w->text) |
| 1115 |
ustrlow(w->text); |
| 1116 |
#else |
| 1117 |
ustrlow(indexword->text); |
| 1118 |
#endif |
| 1119 |
} |
| 1120 |
indexing = FALSE; |
| 1121 |
rdadd(&indexstr, L'\0'); |
| 1122 |
index_merge(idx, FALSE, indexstr.text, idxwordlist); |
| 1123 |
sfree(indexstr.text); |
| 1124 |
} |
| 1125 |
if (sitem->type & stack_hyper) |
| 1126 |
{ |
| 1127 |
wd.text = NULL; |
| 1128 |
wd.type = word_HyperEnd; |
| 1129 |
wd.alt = NULL; |
| 1130 |
wd.aux = 0; |
| 1131 |
wd.fpos = t.pos; |
| 1132 |
wd.breaks = FALSE; |
| 1133 |
if (!indexing || index_visible) |
| 1134 |
addword(wd, &whptr); |
| 1135 |
if (indexing) |
| 1136 |
addword(wd, &idximplicit); |
| 1137 |
} |
| 1138 |
if (sitem->type & stack_quote) |
| 1139 |
{ |
| 1140 |
wd.text = NULL; |
| 1141 |
wd.type = toquotestyle(style); |
| 1142 |
wd.alt = NULL; |
| 1143 |
wd.aux = quote_Close; |
| 1144 |
wd.fpos = t.pos; |
| 1145 |
wd.breaks = FALSE; |
| 1146 |
if (!indexing || index_visible) |
| 1147 |
addword(wd, &whptr); |
| 1148 |
if (indexing) |
| 1149 |
{ |
| 1150 |
rdadd(&indexstr, L'"'); |
| 1151 |
addword(wd, &idximplicit); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
sfree(sitem); |
| 1156 |
break; |
| 1157 |
case tok_cmd: |
| 1158 |
switch (t.cmd) |
| 1159 |
{ |
| 1160 |
case c__comment: |
| 1161 |
/* |
| 1162 |
* In-paragraph comment: \#{ balanced braces } |
| 1163 |
* |
| 1164 |
* Anything goes here; even tok_eop. We should |
| 1165 |
* eat whitespace after the close brace _if_ |
| 1166 |
* there was whitespace before the \#. |
| 1167 |
*/ |
| 1168 |
dtor(t), t = get_token(in); |
| 1169 |
if (t.type != tok_lbrace) |
| 1170 |
{ |
| 1171 |
error(err_explbr, &t.pos); |
| 1172 |
} else |
| 1173 |
{ |
| 1174 |
int braces = 1; |
| 1175 |
while (braces > 0) |
| 1176 |
{ |
| 1177 |
dtor(t), t = get_token(in); |
| 1178 |
if (t.type == tok_lbrace) |
| 1179 |
braces++; |
| 1180 |
else if (t.type == tok_rbrace) |
| 1181 |
braces--; |
| 1182 |
else if (t.type == tok_eof) |
| 1183 |
{ |
| 1184 |
error(err_commenteof, &t.pos); |
| 1185 |
break; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
} |
| 1189 |
if (seenwhite) |
| 1190 |
{ |
| 1191 |
already = TRUE; |
| 1192 |
dtor(t), t = get_token(in); |
| 1193 |
if (t.type == tok_white) |
| 1194 |
{ |
| 1195 |
iswhite = TRUE; |
| 1196 |
already = FALSE; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
break; |
| 1200 |
case c_q: |
| 1201 |
dtor(t), t = get_token(in); |
| 1202 |
if (t.type != tok_lbrace) |
| 1203 |
{ |
| 1204 |
error(err_explbr, &t.pos); |
| 1205 |
} else |
| 1206 |
{ |
| 1207 |
wd.text = NULL; |
| 1208 |
wd.type = toquotestyle(style); |
| 1209 |
wd.alt = NULL; |
| 1210 |
wd.aux = quote_Open; |
| 1211 |
wd.fpos = t.pos; |
| 1212 |
wd.breaks = FALSE; |
| 1213 |
if (!indexing || index_visible) |
| 1214 |
addword(wd, &whptr); |
| 1215 |
if (indexing) |
| 1216 |
{ |
| 1217 |
rdadd(&indexstr, L'"'); |
| 1218 |
addword(wd, &idximplicit); |
| 1219 |
} |
| 1220 |
sitem = mknew(struct stack_item); |
| 1221 |
sitem->type = stack_quote; |
| 1222 |
stk_push(parsestk, sitem); |
| 1223 |
} |
| 1224 |
break; |
| 1225 |
case c_K: |
| 1226 |
case c_k: |
| 1227 |
case c_R: |
| 1228 |
case c_W: |
| 1229 |
case c_L: |
| 1230 |
case c_date: |
| 1231 |
/* |
| 1232 |
* Keyword, hyperlink, or \date. We expect a |
| 1233 |
* left brace, some text, and then a right |
| 1234 |
* brace. No nesting; no arguments. |
| 1235 |
*/ |
| 1236 |
wd.fpos = t.pos; |
| 1237 |
wd.breaks = FALSE; |
| 1238 |
if (t.cmd == c_K) |
| 1239 |
wd.type = word_UpperXref; |
| 1240 |
else if (t.cmd == c_k) |
| 1241 |
wd.type = word_LowerXref; |
| 1242 |
else if (t.cmd == c_R) |
| 1243 |
wd.type = word_FreeTextXref; |
| 1244 |
else if (t.cmd == c_W) |
| 1245 |
wd.type = word_HyperLink; |
| 1246 |
else if (t.cmd == c_L) |
| 1247 |
wd.type = word_LocalHyperLink; |
| 1248 |
else |
| 1249 |
wd.type = word_Normal; |
| 1250 |
dtor(t), t = get_token(in); |
| 1251 |
if (t.type != tok_lbrace) |
| 1252 |
{ |
| 1253 |
if (wd.type == word_Normal) |
| 1254 |
{ |
| 1255 |
time_t thetime = current_time(); |
| 1256 |
struct tm *broken = localtime(&thetime); |
| 1257 |
already = TRUE; |
| 1258 |
wdtext = ustrftime(NULL, broken); |
| 1259 |
wd.type = style; |
| 1260 |
} else |
| 1261 |
{ |
| 1262 |
error(err_explbr, &t.pos); |
| 1263 |
wdtext = NULL; |
| 1264 |
} |
| 1265 |
} else |
| 1266 |
{ |
| 1267 |
rdstring rs = { 0, 0, NULL }; |
| 1268 |
while (dtor(t), t = get_token(in), |
| 1269 |
t.type == tok_word || t.type == tok_white) |
| 1270 |
{ |
| 1271 |
if (t.type == tok_white) |
| 1272 |
rdadd(&rs, ' '); |
| 1273 |
else |
| 1274 |
rdadds(&rs, t.text); |
| 1275 |
} |
| 1276 |
if (wd.type == word_Normal) |
| 1277 |
{ |
| 1278 |
time_t thetime = current_time(); |
| 1279 |
struct tm *broken = localtime(&thetime); |
| 1280 |
wdtext = ustrftime(rs.text, broken); |
| 1281 |
wd.type = style; |
| 1282 |
} else |
| 1283 |
{ |
| 1284 |
wdtext = ustrdup(rs.text); |
| 1285 |
} |
| 1286 |
sfree(rs.text); |
| 1287 |
if (t.type != tok_rbrace) |
| 1288 |
{ |
| 1289 |
error(err_kwexprbr, &t.pos); |
| 1290 |
} |
| 1291 |
} |
| 1292 |
wd.alt = NULL; |
| 1293 |
wd.aux = 0; |
| 1294 |
if (!indexing || index_visible) |
| 1295 |
{ |
| 1296 |
wd.text = ustrdup(wdtext); |
| 1297 |
addword(wd, &whptr); |
| 1298 |
} |
| 1299 |
if (indexing) |
| 1300 |
{ |
| 1301 |
wd.text = ustrdup(wdtext); |
| 1302 |
addword(wd, &idximplicit); |
| 1303 |
} |
| 1304 |
sfree(wdtext); |
| 1305 |
if (wd.type == word_FreeTextXref || wd.type == word_HyperLink || wd.type == word_LocalHyperLink) |
| 1306 |
{ |
| 1307 |
/* |
| 1308 |
* Hyperlinks are different: they then |
| 1309 |
* expect another left brace, to begin |
| 1310 |
* delimiting the text marked by the link. |
| 1311 |
*/ |
| 1312 |
dtor(t), t = get_token(in); |
| 1313 |
/* |
| 1314 |
* Special cases: \W{}\c, \W{}\e, \W{}\s, \W{}\bold \W{}\cw |
| 1315 |
*/ |
| 1316 |
sitem = mknew(struct stack_item); |
| 1317 |
sitem->type = stack_hyper; |
| 1318 |
if (t.type == tok_cmd && (tmpstyle = getcmdstyle(t.cmd))) |
| 1319 |
{ |
| 1320 |
if (style != word_Normal) |
| 1321 |
error(err_nestedstyles, &t.pos); |
| 1322 |
else |
| 1323 |
{ |
| 1324 |
style = tmpstyle; |
| 1325 |
spcstyle = tospacestyle(style); |
| 1326 |
sitem->type |= stack_style; |
| 1327 |
} |
| 1328 |
dtor(t), t = get_token(in); |
| 1329 |
} |
| 1330 |
if (t.type != tok_lbrace) |
| 1331 |
{ |
| 1332 |
error(err_explbr, &t.pos); |
| 1333 |
sfree(sitem); |
| 1334 |
} else |
| 1335 |
{ |
| 1336 |
stk_push(parsestk, sitem); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
break; |
| 1340 |
case c_html: |
| 1341 |
if (style != word_Normal) fatal(err_nestedstyles, &t.pos); |
| 1342 |
type = t.cmd, dtor(t), t = get_token(in); |
| 1343 |
if (t.type == tok_lbrace || (error(err_explbr, &t.pos), FALSE)) |
| 1344 |
{ |
| 1345 |
style = word_Html, spcstyle = tospacestyle(style); |
| 1346 |
stack_item_push(parsestk, stack_style); |
| 1347 |
} |
| 1348 |
break; |
| 1349 |
case c_c: |
| 1350 |
case c_cw: |
| 1351 |
case c_e: |
| 1352 |
case c_s: |
| 1353 |
case c_bold: |
| 1354 |
type = t.cmd; |
| 1355 |
if (style != word_Normal) |
| 1356 |
{ |
| 1357 |
error(err_nestedstyles, &t.pos); |
| 1358 |
/* Error recovery: eat lbrace, push nop. */ |
| 1359 |
dtor(t), t = get_token(in); |
| 1360 |
stack_item_push(parsestk, stack_nop); |
| 1361 |
} |
| 1362 |
dtor(t), t = get_token(in); |
| 1363 |
if (t.type != tok_lbrace) |
| 1364 |
{ |
| 1365 |
error(err_explbr, &t.pos); |
| 1366 |
} else |
| 1367 |
{ |
| 1368 |
style = getcmdstyle(type), spcstyle = tospacestyle(style); |
| 1369 |
stack_item_push(parsestk, stack_style); |
| 1370 |
} |
| 1371 |
break; |
| 1372 |
case c_i: |
| 1373 |
case c_ii: |
| 1374 |
case c_I: |
| 1375 |
type = t.cmd; |
| 1376 |
if (indexing) |
| 1377 |
{ |
| 1378 |
error(err_nestedindex, &t.pos); |
| 1379 |
/* Error recovery: eat lbrace, push nop. */ |
| 1380 |
dtor(t), t = get_token(in); |
| 1381 |
stack_item_push(parsestk, stack_nop); |
| 1382 |
} |
| 1383 |
sitem = mknew(struct stack_item); |
| 1384 |
sitem->type = stack_idx; |
| 1385 |
dtor(t), t = get_token(in); |
| 1386 |
/* |
| 1387 |
* Special cases: \i\c, \i\e, \i\s, \i\bold, \i\cw |
| 1388 |
*/ |
| 1389 |
wd.fpos = t.pos; |
| 1390 |
if (t.type == tok_cmd && (tmpstyle = getcmdstyle(t.cmd))) |
| 1391 |
{ |
| 1392 |
if (style != word_Normal) |
| 1393 |
error(err_nestedstyles, &t.pos); |
| 1394 |
else |
| 1395 |
{ |
| 1396 |
style = tmpstyle; |
| 1397 |
spcstyle = tospacestyle(style); |
| 1398 |
sitem->type |= stack_style; |
| 1399 |
} |
| 1400 |
dtor(t), t = get_token(in); |
| 1401 |
} |
| 1402 |
if (t.type != tok_lbrace) |
| 1403 |
{ |
| 1404 |
sfree(sitem); |
| 1405 |
error(err_explbr, &t.pos); |
| 1406 |
} else |
| 1407 |
{ |
| 1408 |
/* Add an index-reference word with no text as yet */ |
| 1409 |
wd.type = word_IndexRef; |
| 1410 |
wd.text = NULL; |
| 1411 |
wd.alt = NULL; |
| 1412 |
wd.aux = 0; |
| 1413 |
wd.breaks = FALSE; |
| 1414 |
indexword = addword(wd, &whptr); |
| 1415 |
/* Set up a rdstring to read the index text */ |
| 1416 |
indexstr = nullrs; |
| 1417 |
/* Flags so that we do the Right Things with text */ |
| 1418 |
index_visible = (type != c_I); |
| 1419 |
index_downcase = (type == c_ii); |
| 1420 |
indexing = TRUE; |
| 1421 |
idxwordlist = NULL; |
| 1422 |
idximplicit = &idxwordlist; |
| 1423 |
/* Stack item to close the indexing on exit */ |
| 1424 |
stk_push(parsestk, sitem); |
| 1425 |
} |
| 1426 |
break; |
| 1427 |
case c_u: |
| 1428 |
uchr = t.aux; |
| 1429 |
if (uchr == 0) |
| 1430 |
{ |
| 1431 |
err_zerochar(&t.pos); |
| 1432 |
break; |
| 1433 |
} |
| 1434 |
utext[0] = uchr, utext[1] = 0; |
| 1435 |
wd.type = style; |
| 1436 |
wd.breaks = FALSE; |
| 1437 |
wd.alt = NULL; |
| 1438 |
wd.aux = 0; |
| 1439 |
wd.fpos = t.pos; |
| 1440 |
if (!indexing || index_visible) |
| 1441 |
{ |
| 1442 |
wd.text = ustrdup(utext); |
| 1443 |
uword = addword(wd, &whptr); |
| 1444 |
} else |
| 1445 |
uword = NULL; |
| 1446 |
if (indexing) |
| 1447 |
{ |
| 1448 |
wd.text = ustrdup(utext); |
| 1449 |
iword = addword(wd, &idximplicit); |
| 1450 |
} else |
| 1451 |
iword = NULL; |
| 1452 |
dtor(t), t = get_token(in); |
| 1453 |
if (t.type == tok_lbrace) |
| 1454 |
{ |
| 1455 |
/* |
| 1456 |
* \u with a left brace. Until the brace |
| 1457 |
* closes, all further words go on a |
| 1458 |
* sidetrack from the main thread of the |
| 1459 |
* paragraph. |
| 1460 |
*/ |
| 1461 |
sitem = mknew(struct stack_item); |
| 1462 |
sitem->type = stack_ualt; |
| 1463 |
sitem->whptr = whptr; |
| 1464 |
sitem->idximplicit = idximplicit; |
| 1465 |
stk_push(parsestk, sitem); |
| 1466 |
whptr = uword ? &uword->alt : NULL; |
| 1467 |
idximplicit = iword ? &iword->alt : NULL; |
| 1468 |
} else |
| 1469 |
{ |
| 1470 |
if (indexing) |
| 1471 |
rdadd(&indexstr, uchr); |
| 1472 |
already = TRUE; |
| 1473 |
} |
| 1474 |
break; |
| 1475 |
default: |
| 1476 |
if (!macrolookup(macros, in, t.text, &t.pos)) |
| 1477 |
error(err_badmidcmd, t.text, &t.pos); |
| 1478 |
break; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
if (!already) |
| 1482 |
dtor(t), t = get_token(in); |
| 1483 |
seenwhite = iswhite; |
| 1484 |
} |
| 1485 |
/* Check the stack is empty */ |
| 1486 |
if (NULL != (sitem = stk_pop(parsestk))) |
| 1487 |
{ |
| 1488 |
do |
| 1489 |
{ |
| 1490 |
sfree(sitem); |
| 1491 |
sitem = stk_pop(parsestk); |
| 1492 |
} |
| 1493 |
while (sitem); |
| 1494 |
error(err_missingrbrace, &t.pos); |
| 1495 |
} |
| 1496 |
stk_free(parsestk); |
| 1497 |
addpara(par, ret); |
| 1498 |
} |
| 1499 |
|
| 1500 |
/* |
| 1501 |
* We break to here rather than returning, because otherwise |
| 1502 |
* this cleanup doesn't happen. |
| 1503 |
*/ |
| 1504 |
dtor(t); |
| 1505 |
} |
| 1506 |
|
| 1507 |
paragraph *read_input(input * in, indexdata * idx) |
| 1508 |
{ |
| 1509 |
paragraph *head = NULL; |
| 1510 |
paragraph **hptr = &head; |
| 1511 |
tree234 *macros; |
| 1512 |
|
| 1513 |
macros = newtree234(macrocmp); |
| 1514 |
|
| 1515 |
while (in->currindex < in->nfiles) |
| 1516 |
{ |
| 1517 |
in->currfp = fopen(in->filenames[in->currindex], "r"); |
| 1518 |
if (in->currfp) |
| 1519 |
{ |
| 1520 |
setpos(in, in->filenames[in->currindex]); |
| 1521 |
read_file(&hptr, in, idx, macros); |
| 1522 |
} |
| 1523 |
in->currindex++; |
| 1524 |
} |
| 1525 |
|
| 1526 |
macrocleanup(macros); |
| 1527 |
return head; |
| 1528 |
} |