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