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.
189 keywords
= mknewa(wchar_t, 2+strlen(p
));
198 if (*q
== '\\' && q
[1])
200 /* FIXME: lacks charset flexibility */
207 para
= mknew(paragraph
);
208 memset(para
, 0, sizeof(*para
));
209 para
->type
= para_Config
;
210 para
->keyword
= keywords
;
212 para
->fpos
.filename
= "<command line>";
213 para
->fpos
.line
= para
->fpos
.col
= -1;
216 cfg_tail
->next
= para
;
223 p
= NULL
; /* prevent continued processing */
227 * Unrecognised option.
233 errs
= TRUE
, error(err_nosuchopt
, opt
);
239 * A non-option argument.
241 infiles
[nfiles
++] = p
;
261 paragraph
*sourceform
, *p
;
263 keywordlist
*keywords
;
265 in
.filenames
= infiles
;
269 in
.npushback
= in
.pushbacksize
= 0;
271 in
.reportcols
= reportcols
;
276 sourceform
= read_input(&in
, idx
);
281 * Append the config directives acquired from the command
288 while (end
&& end
->next
)
297 mark_attr_ends(sourceform
);
301 keywords
= get_keywords(sourceform
);
304 gen_citations(sourceform
, keywords
);
305 subst_keywords(sourceform
, keywords
);
307 for (p
= sourceform
; p
; p
= p
->next
)
308 if (p
->type
== para_IM
)
309 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
315 dbg_prtkws(keywords
);
316 dbg_prtsource(sourceform
);
320 * Select and run the pre-backends.
323 for (k
= 0; k
< (int)lenof(backends
); k
++)
324 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
325 prebackbits
|= backends
[k
].prebackend_bitfield
;
326 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
327 if (prebackbits
& pre_backends
[k
].bitfield
) {
328 assert(k
< (int)lenof(pre_backend_data
));
329 pre_backend_data
[k
] =
330 pre_backends
[k
].func(sourceform
, keywords
, idx
);
334 * Run the selected set of backends.
336 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
337 if (b
!= backends
[k
].bitfield
) {
338 b
= backends
[k
].bitfield
;
339 if (backendbits
== 0 || (backendbits
& b
)) {
341 int pbb
= backends
[k
].prebackend_bitfield
;
344 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
345 if (pbb
& pre_backends
[m
].bitfield
) {
346 assert(m
< (int)lenof(pre_backend_data
));
347 pbd
= pre_backend_data
[m
];
351 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
355 free_para_list(sourceform
);
356 free_keywords(keywords
);
363 static void dbg_prtsource(paragraph
*sourceform
) {
365 * Output source form in debugging format.
369 for (p
= sourceform
; p
; p
= p
->next
) {
371 printf("para %d ", p
->type
);
383 printf("(no keyword)");
385 dbg_prtwordlist(1, p
->words
);
390 static void dbg_prtkws(keywordlist
*kws
) {
392 * Output keywords in debugging format.
398 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
411 dbg_prtwordlist(1, kw
->text
);
416 static void dbg_prtwordlist(int level
, word
*w
) {
417 for (; w
; w
= w
->next
) {
419 printf("%*sword %d ", level
*4, "", w
->type
);
422 for (wp
= w
->text
; *wp
; wp
++)
428 printf(" alt = {\n");
429 dbg_prtwordlist(level
+1, w
->alt
);
430 printf("%*s}", level
*4, "");