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
;
273 in
.defcharset
= CS_ASCII
;
277 sourceform
= read_input(&in
, idx
);
282 * Append the config directives acquired from the command
289 while (end
&& end
->next
)
298 mark_attr_ends(sourceform
);
302 keywords
= get_keywords(sourceform
);
305 gen_citations(sourceform
, keywords
);
306 subst_keywords(sourceform
, keywords
);
308 for (p
= sourceform
; p
; p
= p
->next
)
309 if (p
->type
== para_IM
)
310 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
316 dbg_prtkws(keywords
);
317 dbg_prtsource(sourceform
);
321 * Select and run the pre-backends.
324 for (k
= 0; k
< (int)lenof(backends
); k
++)
325 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
326 prebackbits
|= backends
[k
].prebackend_bitfield
;
327 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
328 if (prebackbits
& pre_backends
[k
].bitfield
) {
329 assert(k
< (int)lenof(pre_backend_data
));
330 pre_backend_data
[k
] =
331 pre_backends
[k
].func(sourceform
, keywords
, idx
);
335 * Run the selected set of backends.
337 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
338 if (b
!= backends
[k
].bitfield
) {
339 b
= backends
[k
].bitfield
;
340 if (backendbits
== 0 || (backendbits
& b
)) {
342 int pbb
= backends
[k
].prebackend_bitfield
;
345 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
346 if (pbb
& pre_backends
[m
].bitfield
) {
347 assert(m
< (int)lenof(pre_backend_data
));
348 pbd
= pre_backend_data
[m
];
352 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
356 free_para_list(sourceform
);
357 free_keywords(keywords
);
364 static void dbg_prtsource(paragraph
*sourceform
) {
366 * Output source form in debugging format.
370 for (p
= sourceform
; p
; p
= p
->next
) {
372 printf("para %d ", p
->type
);
384 printf("(no keyword)");
386 dbg_prtwordlist(1, p
->words
);
391 static void dbg_prtkws(keywordlist
*kws
) {
393 * Output keywords in debugging format.
399 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
412 dbg_prtwordlist(1, kw
->text
);
417 static void dbg_prtwordlist(int level
, word
*w
) {
418 for (; w
; w
= w
->next
) {
420 printf("%*sword %d ", level
*4, "", w
->type
);
423 for (wp
= w
->text
; *wp
; wp
++)
429 printf(" alt = {\n");
430 dbg_prtwordlist(level
+1, w
->alt
);
431 printf("%*s}", level
*4, "");