| 1 | /* |
| 2 | * main.c: command line parsing and top level |
| 3 | */ |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include <locale.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include "halibut.h" |
| 10 | |
| 11 | static void dbg_prtsource(paragraph *sourceform); |
| 12 | static void dbg_prtwordlist(int level, word *w); |
| 13 | static void dbg_prtkws(keywordlist *kws); |
| 14 | |
| 15 | static const struct pre_backend { |
| 16 | void *(*func)(paragraph *, keywordlist *, indexdata *); |
| 17 | int bitfield; |
| 18 | } pre_backends[] = { |
| 19 | {paper_pre_backend, 0x0001} |
| 20 | }; |
| 21 | |
| 22 | static const struct backend { |
| 23 | char *name; |
| 24 | void (*func)(paragraph *, keywordlist *, indexdata *, void *); |
| 25 | paragraph *(*filename)(char *filename); |
| 26 | int bitfield, prebackend_bitfield; |
| 27 | } backends[] = { |
| 28 | {"text", text_backend, text_config_filename, 0x0001, 0}, |
| 29 | {"xhtml", html_backend, html_config_filename, 0x0002, 0}, |
| 30 | {"html", html_backend, html_config_filename, 0x0002, 0}, |
| 31 | {"hlp", whlp_backend, whlp_config_filename, 0x0004, 0}, |
| 32 | {"whlp", whlp_backend, whlp_config_filename, 0x0004, 0}, |
| 33 | {"winhelp", whlp_backend, whlp_config_filename, 0x0004, 0}, |
| 34 | {"man", man_backend, man_config_filename, 0x0008, 0}, |
| 35 | {"info", info_backend, info_config_filename, 0x0010, 0}, |
| 36 | {"ps", ps_backend, ps_config_filename, 0x0020, 0x0001}, |
| 37 | {"pdf", pdf_backend, pdf_config_filename, 0x0040, 0x0001}, |
| 38 | }; |
| 39 | |
| 40 | int main(int argc, char **argv) { |
| 41 | char **infiles; |
| 42 | int nfiles; |
| 43 | int nogo; |
| 44 | int errs; |
| 45 | int reportcols; |
| 46 | int list_fonts; |
| 47 | int input_charset; |
| 48 | int debug; |
| 49 | int backendbits, prebackbits; |
| 50 | int k, b; |
| 51 | paragraph *cfg, *cfg_tail; |
| 52 | void *pre_backend_data[16]; |
| 53 | |
| 54 | /* |
| 55 | * Use the specified locale everywhere. It'll be used for |
| 56 | * output of error messages, and as the default character set |
| 57 | * for input files if one is not explicitly specified. |
| 58 | * |
| 59 | * However, we need to use standard numeric formatting for |
| 60 | * output of things like PDF. |
| 61 | */ |
| 62 | setlocale(LC_ALL, ""); |
| 63 | setlocale(LC_NUMERIC, "C"); |
| 64 | |
| 65 | /* |
| 66 | * Set up initial (default) parameters. |
| 67 | */ |
| 68 | infiles = snewn(argc, char *); |
| 69 | nfiles = 0; |
| 70 | nogo = errs = FALSE; |
| 71 | reportcols = 0; |
| 72 | list_fonts = 0; |
| 73 | input_charset = CS_ASCII; |
| 74 | debug = 0; |
| 75 | backendbits = 0; |
| 76 | cfg = cfg_tail = NULL; |
| 77 | |
| 78 | if (argc == 1) { |
| 79 | usage(); |
| 80 | exit(EXIT_SUCCESS); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Parse command line arguments. |
| 85 | */ |
| 86 | while (--argc) { |
| 87 | char *p = *++argv; |
| 88 | if (*p == '-') { |
| 89 | /* |
| 90 | * An option. |
| 91 | */ |
| 92 | while (p && *++p) { |
| 93 | char c = *p; |
| 94 | switch (c) { |
| 95 | case '-': |
| 96 | /* |
| 97 | * Long option. |
| 98 | */ |
| 99 | { |
| 100 | char *opt, *val; |
| 101 | opt = p++; /* opt will have _one_ leading - */ |
| 102 | while (*p && *p != '=') |
| 103 | p++; /* find end of option */ |
| 104 | if (*p == '=') { |
| 105 | *p++ = '\0'; |
| 106 | val = p; |
| 107 | } else |
| 108 | val = NULL; |
| 109 | |
| 110 | assert(opt[0] == '-'); |
| 111 | for (k = 0; k < (int)lenof(backends); k++) |
| 112 | if (!strcmp(opt+1, backends[k].name)) { |
| 113 | backendbits |= backends[k].bitfield; |
| 114 | if (val) { |
| 115 | paragraph *p = backends[k].filename(val); |
| 116 | assert(p); |
| 117 | if (cfg_tail) |
| 118 | cfg_tail->next = p; |
| 119 | else |
| 120 | cfg = p; |
| 121 | while (p->next) |
| 122 | p = p->next; |
| 123 | cfg_tail = p; |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | if (k < (int)lenof(backends)) { |
| 128 | /* do nothing */; |
| 129 | } else if (!strcmp(opt, "-input-charset")) { |
| 130 | if (!val) { |
| 131 | errs = TRUE, error(err_optnoarg, opt); |
| 132 | } else { |
| 133 | int charset = charset_from_localenc(val); |
| 134 | if (charset == CS_NONE) { |
| 135 | errs = TRUE, error(err_cmdcharset, val); |
| 136 | } else { |
| 137 | input_charset = charset; |
| 138 | } |
| 139 | } |
| 140 | } else if (!strcmp(opt, "-help")) { |
| 141 | help(); |
| 142 | nogo = TRUE; |
| 143 | } else if (!strcmp(opt, "-version")) { |
| 144 | showversion(); |
| 145 | nogo = TRUE; |
| 146 | } else if (!strcmp(opt, "-licence") || |
| 147 | !strcmp(opt, "-license")) { |
| 148 | licence(); |
| 149 | nogo = TRUE; |
| 150 | } else if (!strcmp(opt, "-list-charsets")) { |
| 151 | listcharsets(); |
| 152 | nogo = TRUE; |
| 153 | } else if (!strcmp(opt, "-list-fonts")) { |
| 154 | list_fonts = TRUE; |
| 155 | } else if (!strcmp(opt, "-precise")) { |
| 156 | reportcols = 1; |
| 157 | } else { |
| 158 | errs = TRUE, error(err_nosuchopt, opt); |
| 159 | } |
| 160 | } |
| 161 | p = NULL; |
| 162 | break; |
| 163 | case 'h': |
| 164 | case 'V': |
| 165 | case 'L': |
| 166 | case 'P': |
| 167 | case 'd': |
| 168 | /* |
| 169 | * Option requiring no parameter. |
| 170 | */ |
| 171 | switch (c) { |
| 172 | case 'h': |
| 173 | help(); |
| 174 | nogo = TRUE; |
| 175 | break; |
| 176 | case 'V': |
| 177 | showversion(); |
| 178 | nogo = TRUE; |
| 179 | break; |
| 180 | case 'L': |
| 181 | licence(); |
| 182 | nogo = TRUE; |
| 183 | break; |
| 184 | case 'P': |
| 185 | reportcols = 1; |
| 186 | break; |
| 187 | case 'd': |
| 188 | debug = TRUE; |
| 189 | break; |
| 190 | } |
| 191 | break; |
| 192 | case 'C': |
| 193 | /* |
| 194 | * Option requiring parameter. |
| 195 | */ |
| 196 | p++; |
| 197 | if (!*p && argc > 1) |
| 198 | --argc, p = *++argv; |
| 199 | else if (!*p) { |
| 200 | char opt[2]; |
| 201 | opt[0] = c; |
| 202 | opt[1] = '\0'; |
| 203 | errs = TRUE, error(err_optnoarg, opt); |
| 204 | } |
| 205 | /* |
| 206 | * Now c is the option and p is the parameter. |
| 207 | */ |
| 208 | switch (c) { |
| 209 | case 'C': |
| 210 | /* |
| 211 | * -C means we split our argument up into |
| 212 | * colon-separated chunks and assemble them |
| 213 | * into a config paragraph. |
| 214 | */ |
| 215 | { |
| 216 | char *s = dupstr(p), *q, *r; |
| 217 | paragraph *para; |
| 218 | |
| 219 | para = cmdline_cfg_new(); |
| 220 | |
| 221 | q = r = s; |
| 222 | while (*q) { |
| 223 | if (*q == ':') { |
| 224 | *r = '\0'; |
| 225 | /* XXX ad-hoc diagnostic */ |
| 226 | if (!strcmp(s, "input-charset")) |
| 227 | error(err_futileopt, "Cinput-charset", |
| 228 | "; use --input-charset"); |
| 229 | cmdline_cfg_add(para, s); |
| 230 | r = s; |
| 231 | } else { |
| 232 | if (*q == '\\' && q[1]) |
| 233 | q++; |
| 234 | *r++ = *q; |
| 235 | } |
| 236 | q++; |
| 237 | } |
| 238 | *r = '\0'; |
| 239 | cmdline_cfg_add(para, s); |
| 240 | |
| 241 | if (cfg_tail) |
| 242 | cfg_tail->next = para; |
| 243 | else |
| 244 | cfg = para; |
| 245 | cfg_tail = para; |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | p = NULL; /* prevent continued processing */ |
| 250 | break; |
| 251 | default: |
| 252 | /* |
| 253 | * Unrecognised option. |
| 254 | */ |
| 255 | { |
| 256 | char opt[2]; |
| 257 | opt[0] = c; |
| 258 | opt[1] = '\0'; |
| 259 | errs = TRUE, error(err_nosuchopt, opt); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } else { |
| 264 | /* |
| 265 | * A non-option argument. |
| 266 | */ |
| 267 | infiles[nfiles++] = p; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (errs) |
| 272 | exit(EXIT_FAILURE); |
| 273 | if (nogo) |
| 274 | exit(EXIT_SUCCESS); |
| 275 | |
| 276 | /* |
| 277 | * Do the work. |
| 278 | */ |
| 279 | if (nfiles == 0 && !list_fonts) { |
| 280 | error(err_noinput); |
| 281 | usage(); |
| 282 | exit(EXIT_FAILURE); |
| 283 | } |
| 284 | |
| 285 | { |
| 286 | input in; |
| 287 | paragraph *sourceform, *p; |
| 288 | indexdata *idx; |
| 289 | keywordlist *keywords; |
| 290 | |
| 291 | in.filenames = infiles; |
| 292 | in.nfiles = nfiles; |
| 293 | in.currfp = NULL; |
| 294 | in.currindex = 0; |
| 295 | in.npushback = in.pushbacksize = 0; |
| 296 | in.pushback = NULL; |
| 297 | in.reportcols = reportcols; |
| 298 | in.stack = NULL; |
| 299 | in.defcharset = input_charset; |
| 300 | |
| 301 | idx = make_index(); |
| 302 | |
| 303 | sourceform = read_input(&in, idx); |
| 304 | if (list_fonts) { |
| 305 | listfonts(); |
| 306 | exit(EXIT_SUCCESS); |
| 307 | } |
| 308 | if (!sourceform) |
| 309 | exit(EXIT_FAILURE); |
| 310 | |
| 311 | /* |
| 312 | * Append the config directives acquired from the command |
| 313 | * line. |
| 314 | */ |
| 315 | { |
| 316 | paragraph *end; |
| 317 | |
| 318 | end = sourceform; |
| 319 | while (end && end->next) |
| 320 | end = end->next; |
| 321 | assert(end); |
| 322 | |
| 323 | end->next = cfg; |
| 324 | } |
| 325 | |
| 326 | sfree(in.pushback); |
| 327 | |
| 328 | sfree(infiles); |
| 329 | |
| 330 | keywords = get_keywords(sourceform); |
| 331 | if (!keywords) |
| 332 | exit(EXIT_FAILURE); |
| 333 | gen_citations(sourceform, keywords); |
| 334 | subst_keywords(sourceform, keywords); |
| 335 | |
| 336 | for (p = sourceform; p; p = p->next) |
| 337 | if (p->type == para_IM) |
| 338 | index_merge(idx, TRUE, p->keyword, p->words, &p->fpos); |
| 339 | |
| 340 | build_index(idx); |
| 341 | |
| 342 | /* |
| 343 | * Set up attr_First / attr_Last / attr_Always, in the main |
| 344 | * document and in the index entries. |
| 345 | */ |
| 346 | for (p = sourceform; p; p = p->next) |
| 347 | mark_attr_ends(p->words); |
| 348 | { |
| 349 | int i; |
| 350 | indexentry *entry; |
| 351 | |
| 352 | for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) |
| 353 | mark_attr_ends(entry->text); |
| 354 | } |
| 355 | |
| 356 | if (debug) { |
| 357 | index_debug(idx); |
| 358 | dbg_prtkws(keywords); |
| 359 | dbg_prtsource(sourceform); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Select and run the pre-backends. |
| 364 | */ |
| 365 | prebackbits = 0; |
| 366 | for (k = 0; k < (int)lenof(backends); k++) |
| 367 | if (backendbits == 0 || (backendbits & backends[k].bitfield)) |
| 368 | prebackbits |= backends[k].prebackend_bitfield; |
| 369 | for (k = 0; k < (int)lenof(pre_backends); k++) |
| 370 | if (prebackbits & pre_backends[k].bitfield) { |
| 371 | assert(k < (int)lenof(pre_backend_data)); |
| 372 | pre_backend_data[k] = |
| 373 | pre_backends[k].func(sourceform, keywords, idx); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Run the selected set of backends. |
| 378 | */ |
| 379 | for (k = b = 0; k < (int)lenof(backends); k++) |
| 380 | if (b != backends[k].bitfield) { |
| 381 | b = backends[k].bitfield; |
| 382 | if (backendbits == 0 || (backendbits & b)) { |
| 383 | void *pbd = NULL; |
| 384 | int pbb = backends[k].prebackend_bitfield; |
| 385 | int m; |
| 386 | |
| 387 | for (m = 0; m < (int)lenof(pre_backends); m++) |
| 388 | if (pbb & pre_backends[m].bitfield) { |
| 389 | assert(m < (int)lenof(pre_backend_data)); |
| 390 | pbd = pre_backend_data[m]; |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | backends[k].func(sourceform, keywords, idx, pbd); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | free_para_list(sourceform); |
| 399 | free_keywords(keywords); |
| 400 | cleanup_index(idx); |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static void dbg_prtsource(paragraph *sourceform) { |
| 407 | /* |
| 408 | * Output source form in debugging format. |
| 409 | */ |
| 410 | |
| 411 | paragraph *p; |
| 412 | for (p = sourceform; p; p = p->next) { |
| 413 | wchar_t *wp; |
| 414 | printf("para %d ", p->type); |
| 415 | if (p->keyword) { |
| 416 | wp = p->keyword; |
| 417 | while (*wp) { |
| 418 | putchar('\"'); |
| 419 | for (; *wp; wp++) |
| 420 | putchar(*wp); |
| 421 | putchar('\"'); |
| 422 | if (*++wp) |
| 423 | printf(", "); |
| 424 | } |
| 425 | } else |
| 426 | printf("(no keyword)"); |
| 427 | printf(" {\n"); |
| 428 | dbg_prtwordlist(1, p->words); |
| 429 | printf("}\n"); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static void dbg_prtkws(keywordlist *kws) { |
| 434 | /* |
| 435 | * Output keywords in debugging format. |
| 436 | */ |
| 437 | |
| 438 | int i; |
| 439 | keyword *kw; |
| 440 | |
| 441 | for (i = 0; (kw = index234(kws->keys, i)) != NULL; i++) { |
| 442 | wchar_t *wp; |
| 443 | printf("keyword "); |
| 444 | wp = kw->key; |
| 445 | while (*wp) { |
| 446 | putchar('\"'); |
| 447 | for (; *wp; wp++) |
| 448 | putchar(*wp); |
| 449 | putchar('\"'); |
| 450 | if (*++wp) |
| 451 | printf(", "); |
| 452 | } |
| 453 | printf(" {\n"); |
| 454 | dbg_prtwordlist(1, kw->text); |
| 455 | printf("}\n"); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | static void dbg_prtwordlist(int level, word *w) { |
| 460 | for (; w; w = w->next) { |
| 461 | wchar_t *wp; |
| 462 | printf("%*sword %d ", level*4, "", w->type); |
| 463 | if (w->text) { |
| 464 | printf("\""); |
| 465 | for (wp = w->text; *wp; wp++) |
| 466 | putchar(*wp); |
| 467 | printf("\""); |
| 468 | } else |
| 469 | printf("(no text)"); |
| 470 | if (w->breaks) |
| 471 | printf(" [breaks]"); |
| 472 | if (w->alt) { |
| 473 | printf(" alt = {\n"); |
| 474 | dbg_prtwordlist(level+1, w->alt); |
| 475 | printf("%*s}", level*4, ""); |
| 476 | } |
| 477 | printf("\n"); |
| 478 | } |
| 479 | } |