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
, "-precise")) {
142 errs
= TRUE
, error(err_nosuchopt
, opt
);
153 * Option requiring no parameter.
178 * Option requiring parameter.
187 errs
= TRUE
, error(err_optnoarg
, opt
);
190 * Now c is the option and p is the parameter.
195 * -C means we split our argument up into
196 * colon-separated chunks and assemble them
197 * into a config paragraph.
200 char *s
= dupstr(p
), *q
, *r
;
203 para
= cmdline_cfg_new();
209 /* XXX ad-hoc diagnostic */
210 if (!strcmp(s
, "input-charset"))
211 error(err_futileopt
, "Cinput-charset",
212 "; use --input-charset");
213 cmdline_cfg_add(para
, s
);
216 if (*q
== '\\' && q
[1])
223 cmdline_cfg_add(para
, s
);
226 cfg_tail
->next
= para
;
233 p
= NULL
; /* prevent continued processing */
237 * Unrecognised option.
243 errs
= TRUE
, error(err_nosuchopt
, opt
);
249 * A non-option argument.
251 infiles
[nfiles
++] = p
;
271 paragraph
*sourceform
, *p
;
273 keywordlist
*keywords
;
275 in
.filenames
= infiles
;
279 in
.npushback
= in
.pushbacksize
= 0;
281 in
.reportcols
= reportcols
;
283 in
.defcharset
= input_charset
;
287 sourceform
= read_input(&in
, idx
);
292 * Append the config directives acquired from the command
299 while (end
&& end
->next
)
310 keywords
= get_keywords(sourceform
);
313 gen_citations(sourceform
, keywords
);
314 subst_keywords(sourceform
, keywords
);
316 for (p
= sourceform
; p
; p
= p
->next
)
317 if (p
->type
== para_IM
)
318 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
323 * Set up attr_First / attr_Last / attr_Always, in the main
324 * document and in the index entries.
326 for (p
= sourceform
; p
; p
= p
->next
)
327 mark_attr_ends(p
->words
);
332 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++)
333 mark_attr_ends(entry
->text
);
338 dbg_prtkws(keywords
);
339 dbg_prtsource(sourceform
);
343 * Select and run the pre-backends.
346 for (k
= 0; k
< (int)lenof(backends
); k
++)
347 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
348 prebackbits
|= backends
[k
].prebackend_bitfield
;
349 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
350 if (prebackbits
& pre_backends
[k
].bitfield
) {
351 assert(k
< (int)lenof(pre_backend_data
));
352 pre_backend_data
[k
] =
353 pre_backends
[k
].func(sourceform
, keywords
, idx
);
357 * Run the selected set of backends.
359 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
360 if (b
!= backends
[k
].bitfield
) {
361 b
= backends
[k
].bitfield
;
362 if (backendbits
== 0 || (backendbits
& b
)) {
364 int pbb
= backends
[k
].prebackend_bitfield
;
367 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
368 if (pbb
& pre_backends
[m
].bitfield
) {
369 assert(m
< (int)lenof(pre_backend_data
));
370 pbd
= pre_backend_data
[m
];
374 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
378 free_para_list(sourceform
);
379 free_keywords(keywords
);
386 static void dbg_prtsource(paragraph
*sourceform
) {
388 * Output source form in debugging format.
392 for (p
= sourceform
; p
; p
= p
->next
) {
394 printf("para %d ", p
->type
);
406 printf("(no keyword)");
408 dbg_prtwordlist(1, p
->words
);
413 static void dbg_prtkws(keywordlist
*kws
) {
415 * Output keywords in debugging format.
421 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
434 dbg_prtwordlist(1, kw
->text
);
439 static void dbg_prtwordlist(int level
, word
*w
) {
440 for (; w
; w
= w
->next
) {
442 printf("%*sword %d ", level
*4, "", w
->type
);
445 for (wp
= w
->text
; *wp
; wp
++)
451 printf(" alt = {\n");
452 dbg_prtwordlist(level
+1, w
->alt
);
453 printf("%*s}", level
*4, "");