2 * main.c: command line parsing and top level
11 static void dbg_prtsource(paragraph
*sourceform
);
12 static void dbg_prtwordlist(int level
, word
*w
);
13 static void dbg_prtkws(keywordlist
*kws
);
15 static const struct pre_backend
{
16 void *(*func
)(paragraph
*, keywordlist
*, indexdata
*);
19 {paper_pre_backend
, 0x0001}
22 static const struct backend
{
24 void (*func
)(paragraph
*, keywordlist
*, indexdata
*, void *);
25 paragraph
*(*filename
)(char *filename
);
26 int bitfield
, prebackend_bitfield
;
28 {"text", text_backend
, text_config_filename
, 0x0001, 0},
29 {"xhtml", html_backend
, html_config_filename
, 0x0002, 0},
30 {"html", html_backend
, html_config_filename
, 0x0002, 0},
31 {"hlp", whlp_backend
, whlp_config_filename
, 0x0004, 0},
32 {"whlp", whlp_backend
, whlp_config_filename
, 0x0004, 0},
33 {"winhelp", whlp_backend
, whlp_config_filename
, 0x0004, 0},
34 {"man", man_backend
, man_config_filename
, 0x0008, 0},
35 {"info", info_backend
, info_config_filename
, 0x0010, 0},
36 {"ps", ps_backend
, ps_config_filename
, 0x0020, 0x0001},
37 {"pdf", pdf_backend
, pdf_config_filename
, 0x0040, 0x0001},
40 int main(int argc
, char **argv
) {
48 int backendbits
, prebackbits
;
50 paragraph
*cfg
, *cfg_tail
;
51 void *pre_backend_data
[16];
54 * Use the specified locale everywhere. It'll be used for
55 * output of error messages, and as the default character set
56 * for input files if one is not explicitly specified.
58 * However, we need to use standard numeric formatting for
59 * output of things like PDF.
61 setlocale(LC_ALL
, "");
62 setlocale(LC_NUMERIC
, "C");
65 * Set up initial (default) parameters.
67 infiles
= snewn(argc
, char *);
71 input_charset
= CS_ASCII
;
74 cfg
= cfg_tail
= NULL
;
82 * Parse command line arguments.
99 opt
= p
++; /* opt will have _one_ leading - */
100 while (*p
&& *p
!= '=')
101 p
++; /* find end of option */
108 assert(opt
[0] == '-');
109 for (k
= 0; k
< (int)lenof(backends
); k
++)
110 if (!strcmp(opt
+1, backends
[k
].name
)) {
111 backendbits
|= backends
[k
].bitfield
;
113 paragraph
*p
= backends
[k
].filename(val
);
125 if (k
< (int)lenof(backends
)) {
127 } else if (!strcmp(opt
, "-input-charset")) {
129 errs
= TRUE
, error(err_optnoarg
, opt
);
131 int charset
= charset_from_localenc(val
);
132 if (charset
== CS_NONE
) {
133 errs
= TRUE
, error(err_cmdcharset
, val
);
135 input_charset
= charset
;
138 } else if (!strcmp(opt
, "-help")) {
141 } else if (!strcmp(opt
, "-version")) {
144 } else if (!strcmp(opt
, "-licence") ||
145 !strcmp(opt
, "-license")) {
148 } else if (!strcmp(opt
, "-list-charsets")) {
151 } else if (!strcmp(opt
, "-precise")) {
154 errs
= TRUE
, error(err_nosuchopt
, opt
);
165 * Option requiring no parameter.
190 * Option requiring parameter.
199 errs
= TRUE
, error(err_optnoarg
, opt
);
202 * Now c is the option and p is the parameter.
207 * -C means we split our argument up into
208 * colon-separated chunks and assemble them
209 * into a config paragraph.
212 char *s
= dupstr(p
), *q
, *r
;
215 para
= cmdline_cfg_new();
221 /* XXX ad-hoc diagnostic */
222 if (!strcmp(s
, "input-charset"))
223 error(err_futileopt
, "Cinput-charset",
224 "; use --input-charset");
225 cmdline_cfg_add(para
, s
);
228 if (*q
== '\\' && q
[1])
235 cmdline_cfg_add(para
, s
);
238 cfg_tail
->next
= para
;
245 p
= NULL
; /* prevent continued processing */
249 * Unrecognised option.
255 errs
= TRUE
, error(err_nosuchopt
, opt
);
261 * A non-option argument.
263 infiles
[nfiles
++] = p
;
283 paragraph
*sourceform
, *p
;
285 keywordlist
*keywords
;
287 in
.filenames
= infiles
;
291 in
.npushback
= in
.pushbacksize
= 0;
293 in
.reportcols
= reportcols
;
295 in
.defcharset
= input_charset
;
299 sourceform
= read_input(&in
, idx
);
304 * Append the config directives acquired from the command
311 while (end
&& end
->next
)
322 keywords
= get_keywords(sourceform
);
325 gen_citations(sourceform
, keywords
);
326 subst_keywords(sourceform
, keywords
);
328 for (p
= sourceform
; p
; p
= p
->next
)
329 if (p
->type
== para_IM
)
330 index_merge(idx
, TRUE
, p
->keyword
, p
->words
, &p
->fpos
);
335 * Set up attr_First / attr_Last / attr_Always, in the main
336 * document and in the index entries.
338 for (p
= sourceform
; p
; p
= p
->next
)
339 mark_attr_ends(p
->words
);
344 for (i
= 0; (entry
= index234(idx
->entries
, i
)) != NULL
; i
++)
345 mark_attr_ends(entry
->text
);
350 dbg_prtkws(keywords
);
351 dbg_prtsource(sourceform
);
355 * Select and run the pre-backends.
358 for (k
= 0; k
< (int)lenof(backends
); k
++)
359 if (backendbits
== 0 || (backendbits
& backends
[k
].bitfield
))
360 prebackbits
|= backends
[k
].prebackend_bitfield
;
361 for (k
= 0; k
< (int)lenof(pre_backends
); k
++)
362 if (prebackbits
& pre_backends
[k
].bitfield
) {
363 assert(k
< (int)lenof(pre_backend_data
));
364 pre_backend_data
[k
] =
365 pre_backends
[k
].func(sourceform
, keywords
, idx
);
369 * Run the selected set of backends.
371 for (k
= b
= 0; k
< (int)lenof(backends
); k
++)
372 if (b
!= backends
[k
].bitfield
) {
373 b
= backends
[k
].bitfield
;
374 if (backendbits
== 0 || (backendbits
& b
)) {
376 int pbb
= backends
[k
].prebackend_bitfield
;
379 for (m
= 0; m
< (int)lenof(pre_backends
); m
++)
380 if (pbb
& pre_backends
[m
].bitfield
) {
381 assert(m
< (int)lenof(pre_backend_data
));
382 pbd
= pre_backend_data
[m
];
386 backends
[k
].func(sourceform
, keywords
, idx
, pbd
);
390 free_para_list(sourceform
);
391 free_keywords(keywords
);
398 static void dbg_prtsource(paragraph
*sourceform
) {
400 * Output source form in debugging format.
404 for (p
= sourceform
; p
; p
= p
->next
) {
406 printf("para %d ", p
->type
);
418 printf("(no keyword)");
420 dbg_prtwordlist(1, p
->words
);
425 static void dbg_prtkws(keywordlist
*kws
) {
427 * Output keywords in debugging format.
433 for (i
= 0; (kw
= index234(kws
->keys
, i
)) != NULL
; i
++) {
446 dbg_prtwordlist(1, kw
->text
);
451 static void dbg_prtwordlist(int level
, word
*w
) {
452 for (; w
; w
= w
->next
) {
454 printf("%*sword %d ", level
*4, "", w
->type
);
457 for (wp
= w
->text
; *wp
; wp
++)
465 printf(" alt = {\n");
466 dbg_prtwordlist(level
+1, w
->alt
);
467 printf("%*s}", level
*4, "");