18d644c4120881d84b38834fbf3b33cc3c1ab0e9
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", xhtml_backend
, xhtml_config_filename
, 0x0002, 0},
30 {"html", xhtml_backend
, xhtml_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
) {
47 int backendbits
, prebackbits
;
49 paragraph
*cfg
, *cfg_tail
;
50 void *pre_backend_data
[16];
52 setlocale(LC_ALL
, "");
55 * Set up initial (default) parameters.
57 infiles
= mknewa(char *, argc
);
63 cfg
= cfg_tail
= NULL
;
71 * Parse command line arguments.
88 opt
= p
++; /* opt will have _one_ leading - */
89 while (*p
&& *p
!= '=')
90 p
++; /* find end of option */
97 assert(opt
[0] == '-');
98 for (k
= 0; k
< (int)lenof(backends
); k
++)
99 if (!strcmp(opt
+1, backends
[k
].name
)) {
100 backendbits
|= backends
[k
].bitfield
;
102 paragraph
*p
= backends
[k
].filename(val
);
114 if (k
< (int)lenof(backends
)) {
116 } else if (!strcmp(opt
, "-help")) {
119 } else if (!strcmp(opt
, "-version")) {
122 } else if (!strcmp(opt
, "-licence") ||
123 !strcmp(opt
, "-license")) {
126 } else if (!strcmp(opt
, "-precise")) {
129 errs
= TRUE
, error(err_nosuchopt
, opt
);
140 * Option requiring no parameter.
165 * Option requiring parameter.
174 errs
= TRUE
, error(err_optnoarg
, opt
);
177 * Now c is the option and p is the parameter.
182 * -C means we split our argument up into
183 * colon-separated chunks and assemble them
184 * into a config paragraph.
187 char *s
= dupstr(p
), *q
, *r
;
190 para
= cmdline_cfg_new();
196 cmdline_cfg_add(para
, s
);
199 if (*q
== '\\' && q
[1])
206 cmdline_cfg_add(para
, s
);
209 cfg_tail
->next
= para
;
216 p
= NULL
; /* prevent continued processing */
220 * Unrecognised option.
226 errs
= TRUE
, error(err_nosuchopt
, opt
);
232 * A non-option argument.
234 infiles
[nfiles
++] = p
;
254 paragraph
*sourceform
, *p
;
256 keywordlist
*keywords
;
258 in
.filenames
= infiles
;
262 in
.npushback
= in
.pushbacksize
= 0;
264 in
.reportcols
= reportcols
;
266 in
.defcharset
= charset_from_locale();
270 sourceform
= read_input(&in
, idx
);
275 * Append the config directives acquired from the command
282 while (end
&& end
->next
)
291 mark_attr_ends(sourceform
);
295 keywords
= get_keywords(sourceform
);
298 gen_citations(sourceform
, keywords
);
299 subst_keywords(sourceform
, keywords
);
301 for (p
= sourceform
; p
; p
= p
->next
)
302 if (p
->type
== para_IM
)
303 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
309 dbg_prtkws(keywords
);
310 dbg_prtsource(sourceform
);
314 * Select and run the pre-backends.
317 for (k
= 0; k
< (int)lenof(backends
); k
++)
318 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
319 prebackbits
|= backends
[k
].prebackend_bitfield
;
320 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
321 if (prebackbits
& pre_backends
[k
].bitfield
) {
322 assert(k
< (int)lenof(pre_backend_data
));
323 pre_backend_data
[k
] =
324 pre_backends
[k
].func(sourceform
, keywords
, idx
);
328 * Run the selected set of backends.
330 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
331 if (b
!= backends
[k
].bitfield
) {
332 b
= backends
[k
].bitfield
;
333 if (backendbits
== 0 || (backendbits
& b
)) {
335 int pbb
= backends
[k
].prebackend_bitfield
;
338 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
339 if (pbb
& pre_backends
[m
].bitfield
) {
340 assert(m
< (int)lenof(pre_backend_data
));
341 pbd
= pre_backend_data
[m
];
345 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
349 free_para_list(sourceform
);
350 free_keywords(keywords
);
357 static void dbg_prtsource(paragraph
*sourceform
) {
359 * Output source form in debugging format.
363 for (p
= sourceform
; p
; p
= p
->next
) {
365 printf("para %d ", p
->type
);
377 printf("(no keyword)");
379 dbg_prtwordlist(1, p
->words
);
384 static void dbg_prtkws(keywordlist
*kws
) {
386 * Output keywords in debugging format.
392 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
405 dbg_prtwordlist(1, kw
->text
);
410 static void dbg_prtwordlist(int level
, word
*w
) {
411 for (; w
; w
= w
->next
) {
413 printf("%*sword %d ", level
*4, "", w
->type
);
416 for (wp
= w
->text
; *wp
; wp
++)
422 printf(" alt = {\n");
423 dbg_prtwordlist(level
+1, w
->alt
);
424 printf("%*s}", level
*4, "");