2 * main.c: command line parsing and top level
11 static void dbg_prtsource(paragraph
*sourceform
);
12 static void dbg_prtwordlist(int level
, word
*w
);
13 static void dbg_prtkws(keywordlist
*kws
);
15 static const struct pre_backend
{
16 void *(*func
)(paragraph
*, keywordlist
*, indexdata
*);
19 {paper_pre_backend
, 0x0001}
22 static const struct backend
{
24 void (*func
)(paragraph
*, keywordlist
*, indexdata
*, void *);
25 paragraph
*(*filename
)(char *filename
);
26 int bitfield
, prebackend_bitfield
;
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},
40 int main(int argc
, char **argv
) {
48 int backendbits
, prebackbits
;
50 paragraph
*cfg
, *cfg_tail
;
51 void *pre_backend_data
[16];
53 setlocale(LC_ALL
, "");
56 * Set up initial (default) parameters.
58 infiles
= snewn(argc
, char *);
62 input_charset
= CS_ASCII
;
65 cfg
= cfg_tail
= NULL
;
73 * Parse command line arguments.
90 opt
= p
++; /* opt will have _one_ leading - */
91 while (*p
&& *p
!= '=')
92 p
++; /* find end of option */
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
;
104 paragraph
*p
= backends
[k
].filename(val
);
116 if (k
< (int)lenof(backends
)) {
118 } else if (!strcmp(opt
, "-input-charset")) {
120 errs
= TRUE
, error(err_optnoarg
, opt
);
122 int charset
= charset_from_localenc(val
);
123 if (charset
== CS_NONE
) {
124 errs
= TRUE
, error(err_cmdcharset
, val
);
126 input_charset
= charset
;
129 } else if (!strcmp(opt
, "-help")) {
132 } else if (!strcmp(opt
, "-version")) {
135 } else if (!strcmp(opt
, "-licence") ||
136 !strcmp(opt
, "-license")) {
139 } else if (!strcmp(opt
, "-list-charsets")) {
142 } else if (!strcmp(opt
, "-precise")) {
145 errs
= TRUE
, error(err_nosuchopt
, opt
);
156 * Option requiring no parameter.
181 * Option requiring parameter.
190 errs
= TRUE
, error(err_optnoarg
, opt
);
193 * Now c is the option and p is the parameter.
198 * -C means we split our argument up into
199 * colon-separated chunks and assemble them
200 * into a config paragraph.
203 char *s
= dupstr(p
), *q
, *r
;
206 para
= cmdline_cfg_new();
212 /* XXX ad-hoc diagnostic */
213 if (!strcmp(s
, "input-charset"))
214 error(err_futileopt
, "Cinput-charset",
215 "; use --input-charset");
216 cmdline_cfg_add(para
, s
);
219 if (*q
== '\\' && q
[1])
226 cmdline_cfg_add(para
, s
);
229 cfg_tail
->next
= para
;
236 p
= NULL
; /* prevent continued processing */
240 * Unrecognised option.
246 errs
= TRUE
, error(err_nosuchopt
, opt
);
252 * A non-option argument.
254 infiles
[nfiles
++] = p
;
274 paragraph
*sourceform
, *p
;
276 keywordlist
*keywords
;
278 in
.filenames
= infiles
;
282 in
.npushback
= in
.pushbacksize
= 0;
284 in
.reportcols
= reportcols
;
286 in
.defcharset
= input_charset
;
290 sourceform
= read_input(&in
, idx
);
295 * Append the config directives acquired from the command
302 while (end
&& end
->next
)
313 keywords
= get_keywords(sourceform
);
316 gen_citations(sourceform
, keywords
);
317 subst_keywords(sourceform
, keywords
);
319 for (p
= sourceform
; p
; p
= p
->next
)
320 if (p
->type
== para_IM
)
321 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
326 * Set up attr_First / attr_Last / attr_Always, in the main
327 * document and in the index entries.
329 for (p
= sourceform
; p
; p
= p
->next
)
330 mark_attr_ends(p
->words
);
335 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++)
336 mark_attr_ends(entry
->text
);
341 dbg_prtkws(keywords
);
342 dbg_prtsource(sourceform
);
346 * Select and run the pre-backends.
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
);
360 * Run the selected set of backends.
362 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
363 if (b
!= backends
[k
].bitfield
) {
364 b
= backends
[k
].bitfield
;
365 if (backendbits
== 0 || (backendbits
& b
)) {
367 int pbb
= backends
[k
].prebackend_bitfield
;
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
];
377 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
381 free_para_list(sourceform
);
382 free_keywords(keywords
);
389 static void dbg_prtsource(paragraph
*sourceform
) {
391 * Output source form in debugging format.
395 for (p
= sourceform
; p
; p
= p
->next
) {
397 printf("para %d ", p
->type
);
409 printf("(no keyword)");
411 dbg_prtwordlist(1, p
->words
);
416 static void dbg_prtkws(keywordlist
*kws
) {
418 * Output keywords in debugging format.
424 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
437 dbg_prtwordlist(1, kw
->text
);
442 static void dbg_prtwordlist(int level
, word
*w
) {
443 for (; w
; w
= w
->next
) {
445 printf("%*sword %d ", level
*4, "", w
->type
);
448 for (wp
= w
->text
; *wp
; wp
++)
454 printf(" alt = {\n");
455 dbg_prtwordlist(level
+1, w
->alt
);
456 printf("%*s}", level
*4, "");