2 * main.c: command line parsing and top level
10 static void dbg_prtsource(paragraph
*sourceform
);
11 static void dbg_prtwordlist(int level
, word
*w
);
12 static void dbg_prtkws(keywordlist
*kws
);
14 static const struct pre_backend
{
15 void *(*func
)(paragraph
*, keywordlist
*, indexdata
*);
18 {paper_pre_backend
, 0x0001}
21 static const struct backend
{
23 void (*func
)(paragraph
*, keywordlist
*, indexdata
*, void *);
24 paragraph
*(*filename
)(char *filename
);
25 int bitfield
, prebackend_bitfield
;
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},
39 int main(int argc
, char **argv
) {
46 int backendbits
, prebackbits
;
48 paragraph
*cfg
, *cfg_tail
;
49 void *pre_backend_data
[16];
52 * Set up initial (default) parameters.
54 infiles
= mknewa(char *, argc
);
60 cfg
= cfg_tail
= NULL
;
68 * Parse command line arguments.
85 opt
= p
++; /* opt will have _one_ leading - */
86 while (*p
&& *p
!= '=')
87 p
++; /* find end of option */
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
;
99 paragraph
*p
= backends
[k
].filename(val
);
111 if (k
< (int)lenof(backends
)) {
113 } else if (!strcmp(opt
, "-help")) {
116 } else if (!strcmp(opt
, "-version")) {
119 } else if (!strcmp(opt
, "-licence") ||
120 !strcmp(opt
, "-license")) {
123 } else if (!strcmp(opt
, "-precise")) {
126 errs
= TRUE
, error(err_nosuchopt
, opt
);
137 * Option requiring no parameter.
162 * Option requiring parameter.
171 errs
= TRUE
, error(err_optnoarg
, opt
);
174 * Now c is the option and p is the parameter.
179 * -C means we split our argument up into
180 * colon-separated chunks and assemble them
181 * into a config paragraph.
184 char *s
= dupstr(p
), *q
, *r
;
187 para
= cmdline_cfg_new();
193 cmdline_cfg_add(para
, s
);
196 if (*q
== '\\' && q
[1])
203 cmdline_cfg_add(para
, s
);
206 cfg_tail
->next
= para
;
213 p
= NULL
; /* prevent continued processing */
217 * Unrecognised option.
223 errs
= TRUE
, error(err_nosuchopt
, opt
);
229 * A non-option argument.
231 infiles
[nfiles
++] = p
;
251 paragraph
*sourceform
, *p
;
253 keywordlist
*keywords
;
255 in
.filenames
= infiles
;
259 in
.npushback
= in
.pushbacksize
= 0;
261 in
.reportcols
= reportcols
;
263 in
.defcharset
= CS_ASCII
;
267 sourceform
= read_input(&in
, idx
);
272 * Append the config directives acquired from the command
279 while (end
&& end
->next
)
288 mark_attr_ends(sourceform
);
292 keywords
= get_keywords(sourceform
);
295 gen_citations(sourceform
, keywords
);
296 subst_keywords(sourceform
, keywords
);
298 for (p
= sourceform
; p
; p
= p
->next
)
299 if (p
->type
== para_IM
)
300 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
306 dbg_prtkws(keywords
);
307 dbg_prtsource(sourceform
);
311 * Select and run the pre-backends.
314 for (k
= 0; k
< (int)lenof(backends
); k
++)
315 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
316 prebackbits
|= backends
[k
].prebackend_bitfield
;
317 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
318 if (prebackbits
& pre_backends
[k
].bitfield
) {
319 assert(k
< (int)lenof(pre_backend_data
));
320 pre_backend_data
[k
] =
321 pre_backends
[k
].func(sourceform
, keywords
, idx
);
325 * Run the selected set of backends.
327 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
328 if (b
!= backends
[k
].bitfield
) {
329 b
= backends
[k
].bitfield
;
330 if (backendbits
== 0 || (backendbits
& b
)) {
332 int pbb
= backends
[k
].prebackend_bitfield
;
335 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
336 if (pbb
& pre_backends
[m
].bitfield
) {
337 assert(m
< (int)lenof(pre_backend_data
));
338 pbd
= pre_backend_data
[m
];
342 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
346 free_para_list(sourceform
);
347 free_keywords(keywords
);
354 static void dbg_prtsource(paragraph
*sourceform
) {
356 * Output source form in debugging format.
360 for (p
= sourceform
; p
; p
= p
->next
) {
362 printf("para %d ", p
->type
);
374 printf("(no keyword)");
376 dbg_prtwordlist(1, p
->words
);
381 static void dbg_prtkws(keywordlist
*kws
) {
383 * Output keywords in debugging format.
389 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
402 dbg_prtwordlist(1, kw
->text
);
407 static void dbg_prtwordlist(int level
, word
*w
) {
408 for (; w
; w
= w
->next
) {
410 printf("%*sword %d ", level
*4, "", w
->type
);
413 for (wp
= w
->text
; *wp
; wp
++)
419 printf(" alt = {\n");
420 dbg_prtwordlist(level
+1, w
->alt
);
421 printf("%*s}", level
*4, "");