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