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 backend
{
16 void (*func
)(paragraph
*, keywordlist
*, indexdata
*);
17 paragraph
*(*filename
)(char *filename
);
20 {"text", text_backend
, text_config_filename
, 0x0001},
21 {"xhtml", xhtml_backend
, xhtml_config_filename
, 0x0002},
22 {"html", xhtml_backend
, xhtml_config_filename
, 0x0002},
23 {"hlp", whlp_backend
, whlp_config_filename
, 0x0004},
24 {"whlp", whlp_backend
, whlp_config_filename
, 0x0004},
25 {"winhelp", whlp_backend
, whlp_config_filename
, 0x0004},
26 {"man", man_backend
, man_config_filename
, 0x0008},
27 {"info", info_backend
, info_config_filename
, 0x0010},
30 int main(int argc
, char **argv
) {
39 paragraph
*cfg
, *cfg_tail
;
42 * Set up initial (default) parameters.
44 infiles
= mknewa(char *, argc
);
50 cfg
= cfg_tail
= NULL
;
58 * Parse command line arguments.
75 opt
= p
++; /* opt will have _one_ leading - */
76 while (*p
&& *p
!= '=')
77 p
++; /* find end of option */
84 assert(opt
[0] == '-');
85 for (k
= 0; k
< (int)lenof(backends
); k
++)
86 if (!strcmp(opt
+1, backends
[k
].name
)) {
87 backendbits
|= backends
[k
].bitfield
;
89 paragraph
*p
= backends
[k
].filename(val
);
101 if (k
< (int)lenof(backends
)) {
103 } else if (!strcmp(opt
, "-help")) {
106 } else if (!strcmp(opt
, "-version")) {
109 } else if (!strcmp(opt
, "-licence") ||
110 !strcmp(opt
, "-license")) {
113 } else if (!strcmp(opt
, "-precise")) {
116 errs
= TRUE
, error(err_nosuchopt
, opt
);
127 * Option requiring no parameter.
152 * Option requiring parameter.
161 errs
= TRUE
, error(err_optnoarg
, opt
);
164 * Now c is the option and p is the parameter.
169 * -C means we split our argument up into
170 * colon-separated chunks and assemble them
171 * into a config paragraph.
179 keywords
= mknewa(wchar_t, 2+strlen(p
));
188 if (*q
== '\\' && q
[1])
190 /* FIXME: lacks charset flexibility */
197 para
= mknew(paragraph
);
198 memset(para
, 0, sizeof(*para
));
199 para
->type
= para_Config
;
200 para
->keyword
= keywords
;
202 para
->fpos
.filename
= "<command line>";
203 para
->fpos
.line
= para
->fpos
.col
= -1;
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
;
266 sourceform
= read_input(&in
, idx
);
271 * Append the config directives acquired from the command
278 while (end
&& end
->next
)
287 mark_attr_ends(sourceform
);
291 keywords
= get_keywords(sourceform
);
294 gen_citations(sourceform
, keywords
);
295 subst_keywords(sourceform
, keywords
);
297 for (p
= sourceform
; p
; p
= p
->next
)
298 if (p
->type
== para_IM
)
299 index_merge(idx
, TRUE
, p
->keyword
, p
->words
);
305 dbg_prtkws(keywords
);
306 dbg_prtsource(sourceform
);
310 * Run the selected set of backends.
312 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
313 if (b
!= backends
[k
].bitfield
) {
314 b
= backends
[k
].bitfield
;
315 if (backendbits
== 0 || (backendbits
& b
))
316 backends
[k
].func(sourceform
, keywords
, idx
);
319 free_para_list(sourceform
);
320 free_keywords(keywords
);
327 static void dbg_prtsource(paragraph
*sourceform
) {
329 * Output source form in debugging format.
333 for (p
= sourceform
; p
; p
= p
->next
) {
335 printf("para %d ", p
->type
);
347 printf("(no keyword)");
349 dbg_prtwordlist(1, p
->words
);
354 static void dbg_prtkws(keywordlist
*kws
) {
356 * Output keywords in debugging format.
362 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
375 dbg_prtwordlist(1, kw
->text
);
380 static void dbg_prtwordlist(int level
, word
*w
) {
381 for (; w
; w
= w
->next
) {
383 printf("%*sword %d ", level
*4, "", w
->type
);
386 for (wp
= w
->text
; *wp
; wp
++)
392 printf(" alt = {\n");
393 dbg_prtwordlist(level
+1, w
->alt
);
394 printf("%*s}", level
*4, "");