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
) {
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
= snewn(argc
, char *);
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
)
293 keywords
= get_keywords(sourceform
);
296 gen_citations(sourceform
, keywords
);
297 subst_keywords(sourceform
, keywords
);
299 for (p
= sourceform
; p
; p
= p
->next
)
300 if (p
->type
== para_IM
)
301 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
306 * Set up attr_First / attr_Last / attr_Always, in the main
307 * document and in the index entries.
309 for (p
= sourceform
; p
; p
= p
->next
)
310 mark_attr_ends(p
->words
);
315 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++)
316 mark_attr_ends(entry
->text
);
321 dbg_prtkws(keywords
);
322 dbg_prtsource(sourceform
);
326 * Select and run the pre-backends.
329 for (k
= 0; k
< (int)lenof(backends
); k
++)
330 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
331 prebackbits
|= backends
[k
].prebackend_bitfield
;
332 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
333 if (prebackbits
& pre_backends
[k
].bitfield
) {
334 assert(k
< (int)lenof(pre_backend_data
));
335 pre_backend_data
[k
] =
336 pre_backends
[k
].func(sourceform
, keywords
, idx
);
340 * Run the selected set of backends.
342 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
343 if (b
!= backends
[k
].bitfield
) {
344 b
= backends
[k
].bitfield
;
345 if (backendbits
== 0 || (backendbits
& b
)) {
347 int pbb
= backends
[k
].prebackend_bitfield
;
350 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
351 if (pbb
& pre_backends
[m
].bitfield
) {
352 assert(m
< (int)lenof(pre_backend_data
));
353 pbd
= pre_backend_data
[m
];
357 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
361 free_para_list(sourceform
);
362 free_keywords(keywords
);
369 static void dbg_prtsource(paragraph
*sourceform
) {
371 * Output source form in debugging format.
375 for (p
= sourceform
; p
; p
= p
->next
) {
377 printf("para %d ", p
->type
);
389 printf("(no keyword)");
391 dbg_prtwordlist(1, p
->words
);
396 static void dbg_prtkws(keywordlist
*kws
) {
398 * Output keywords in debugging format.
404 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
417 dbg_prtwordlist(1, kw
->text
);
422 static void dbg_prtwordlist(int level
, word
*w
) {
423 for (; w
; w
= w
->next
) {
425 printf("%*sword %d ", level
*4, "", w
->type
);
428 for (wp
= w
->text
; *wp
; wp
++)
434 printf(" alt = {\n");
435 dbg_prtwordlist(level
+1, w
->alt
);
436 printf("%*s}", level
*4, "");