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 printf("%d\n", backendbits
);
98 backendbits
|= backends
[k
].bitfield
;
99 printf("%d\n", backendbits
);
101 paragraph
*p
= backends
[k
].filename(val
);
113 if (k
< (int)lenof(backends
)) {
115 } else if (!strcmp(opt
, "-help")) {
118 } else if (!strcmp(opt
, "-version")) {
121 } else if (!strcmp(opt
, "-licence") ||
122 !strcmp(opt
, "-license")) {
125 } else if (!strcmp(opt
, "-precise")) {
128 errs
= TRUE
, error(err_nosuchopt
, opt
);
139 * Option requiring no parameter.
164 * Option requiring parameter.
173 errs
= TRUE
, error(err_optnoarg
, opt
);
176 * Now c is the option and p is the parameter.
181 * -C means we split our argument up into
182 * colon-separated chunks and assemble them
183 * into a config paragraph.
191 keywords
= mknewa(wchar_t, 2+strlen(p
));
200 if (*q
== '\\' && q
[1])
202 /* FIXME: lacks charset flexibility */
209 para
= mknew(paragraph
);
210 memset(para
, 0, sizeof(*para
));
211 para
->type
= para_Config
;
212 para
->keyword
= keywords
;
214 para
->fpos
.filename
= "<command line>";
215 para
->fpos
.line
= para
->fpos
.col
= -1;
218 cfg_tail
->next
= para
;
225 p
= NULL
; /* prevent continued processing */
229 * Unrecognised option.
235 errs
= TRUE
, error(err_nosuchopt
, opt
);
241 * A non-option argument.
243 infiles
[nfiles
++] = p
;
246 printf("%d\n", backendbits
);
264 paragraph
*sourceform
, *p
;
266 keywordlist
*keywords
;
268 in
.filenames
= infiles
;
272 in
.npushback
= in
.pushbacksize
= 0;
274 in
.reportcols
= reportcols
;
279 sourceform
= read_input(&in
, idx
);
284 * Append the config directives acquired from the command
291 while (end
&& end
->next
)
300 mark_attr_ends(sourceform
);
304 keywords
= get_keywords(sourceform
);
307 gen_citations(sourceform
, keywords
);
308 subst_keywords(sourceform
, keywords
);
310 for (p
= sourceform
; p
; p
= p
->next
)
311 if (p
->type
== para_IM
)
312 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
318 dbg_prtkws(keywords
);
319 dbg_prtsource(sourceform
);
323 * Select and run the pre-backends.
326 for (k
= 0; k
< (int)lenof(backends
); k
++)
327 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
328 prebackbits
|= backends
[k
].prebackend_bitfield
;
329 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
330 if (prebackbits
& pre_backends
[k
].bitfield
) {
331 assert(k
< (int)lenof(pre_backend_data
));
332 pre_backend_data
[k
] =
333 pre_backends
[k
].func(sourceform
, keywords
, idx
);
337 * Run the selected set of backends.
339 printf("%d\n", backendbits
);
340 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
341 if (b
!= backends
[k
].bitfield
) {
342 b
= backends
[k
].bitfield
;
343 if (backendbits
== 0 || (backendbits
& b
)) {
345 int pbb
= backends
[k
].prebackend_bitfield
;
348 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
349 if (pbb
& pre_backends
[m
].bitfield
) {
350 assert(m
< (int)lenof(pre_backend_data
));
351 pbd
= pre_backend_data
[m
];
355 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
359 free_para_list(sourceform
);
360 free_keywords(keywords
);
367 static void dbg_prtsource(paragraph
*sourceform
) {
369 * Output source form in debugging format.
373 for (p
= sourceform
; p
; p
= p
->next
) {
375 printf("para %d ", p
->type
);
387 printf("(no keyword)");
389 dbg_prtwordlist(1, p
->words
);
394 static void dbg_prtkws(keywordlist
*kws
) {
396 * Output keywords in debugging format.
402 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
415 dbg_prtwordlist(1, kw
->text
);
420 static void dbg_prtwordlist(int level
, word
*w
) {
421 for (; w
; w
= w
->next
) {
423 printf("%*sword %d ", level
*4, "", w
->type
);
426 for (wp
= w
->text
; *wp
; wp
++)
432 printf(" alt = {\n");
433 dbg_prtwordlist(level
+1, w
->alt
);
434 printf("%*s}", level
*4, "");