Support for the MS HTML Help system in the HTML back end. As yet I
[sgt/halibut] / halibut.h
1 #ifndef HALIBUT_HALIBUT_H
2 #define HALIBUT_HALIBUT_H
3
4 #include <stdio.h>
5 #include <wchar.h>
6 #include <time.h>
7 #include <string.h>
8
9 #include "charset.h"
10
11 #ifdef __GNUC__
12 #define NORETURN __attribute__((__noreturn__))
13 #else
14 #define NORETURN /* nothing */
15 #endif
16
17 #ifndef TRUE
18 #define TRUE 1
19 #endif
20 #ifndef FALSE
21 #define FALSE 0
22 #endif
23
24 /* For suppressing unused-parameter warnings */
25 #define IGNORE(x) ( (x) = (x) )
26
27 #include "tree234.h"
28
29 /*
30 * Structure tags
31 */
32 typedef struct input_Tag input;
33 typedef struct filepos_Tag filepos;
34 typedef struct paragraph_Tag paragraph;
35 typedef struct word_Tag word;
36 typedef struct keywordlist_Tag keywordlist;
37 typedef struct keyword_Tag keyword;
38 typedef struct numberstate_Tag numberstate;
39 typedef struct indexdata_Tag indexdata;
40 typedef struct indextag_Tag indextag;
41 typedef struct indexentry_Tag indexentry;
42 typedef struct macrostack_Tag macrostack;
43
44 /*
45 * Data structure to hold a file name and index, a line and a
46 * column number, for reporting errors
47 */
48 struct filepos_Tag {
49 char *filename;
50 int line, col;
51 };
52
53 /*
54 * Data structure to hold all the file names etc for input
55 */
56 typedef struct pushback_Tag {
57 int chr;
58 filepos pos;
59 } pushback;
60 struct input_Tag {
61 char **filenames; /* complete list of input files */
62 int nfiles; /* how many in the list */
63 FILE *currfp; /* the currently open one */
64 int currindex; /* which one is that in the list */
65 pushback *pushback; /* pushed-back input characters */
66 int npushback, pushbacksize;
67 filepos pos;
68 int reportcols; /* report column numbers in errors */
69 macrostack *stack; /* macro expansions in force */
70 int defcharset, charset; /* character sets for input files */
71 charset_state csstate;
72 wchar_t wc[16]; /* wide chars from input conversion */
73 int nwc, wcpos; /* size of, and position in, wc[] */
74 char *pushback_chars; /* used to save input-encoding data */
75 };
76
77 /*
78 * Data structure to hold the input form of the source, ie a linked
79 * list of paragraphs
80 */
81 struct paragraph_Tag {
82 paragraph *next;
83 int type;
84 wchar_t *keyword; /* for most special paragraphs */
85 char *origkeyword; /* same again in original charset */
86 word *words; /* list of words in paragraph */
87 int aux; /* number, in a numbered paragraph
88 * or subsection level
89 */
90 word *kwtext; /* chapter/section indication */
91 word *kwtext2; /* numeric-only form of kwtext */
92 filepos fpos;
93
94 paragraph *parent, *child, *sibling; /* for hierarchy navigation */
95
96 void *private_data; /* for temp use in backends */
97 };
98 enum {
99 para_IM, /* index merge */
100 para_BR, /* bibliography rewrite */
101 para_Rule, /* random horizontal rule */
102 para_Chapter,
103 para_Appendix,
104 para_UnnumberedChapter,
105 para_Heading,
106 para_Subsect,
107 para_Normal,
108 para_Biblio, /* causes no output unless turned ... */
109 para_BiblioCited, /* ... into this paragraph type */
110 para_Bullet,
111 para_NumberedList,
112 para_DescribedThing,
113 para_Description,
114 para_Code,
115 para_Copyright,
116 para_NoCite,
117 para_Title,
118 para_VersionID,
119 para_Config, /* configuration directive */
120 para_LcontPush, /* begin continuation of list item */
121 para_LcontPop, /* end continuation of list item */
122 para_QuotePush, /* begin block quote */
123 para_QuotePop, /* end block quote */
124 /*
125 * Back ends may define their own paragraph types beyond here,
126 * in case they need to use them internally.
127 */
128 para_NotParaType /* placeholder value */
129 };
130
131 /*
132 * Data structure to hold an individual word
133 */
134 struct word_Tag {
135 word *next, *alt;
136 int type;
137 int aux;
138 int breaks; /* can a line break after it? */
139 wchar_t *text;
140 filepos fpos;
141
142 void *private_data; /* for temp use in backends */
143 };
144 enum {
145 /* ORDERING CONSTRAINT: these normal-word types ... */
146 word_Normal,
147 word_Emph,
148 word_Code, /* monospaced; `quoted' in text */
149 word_WeakCode, /* monospaced, normal in text */
150 /* ... must be in the same order as these space types ... */
151 word_WhiteSpace, /* text is NULL or ignorable */
152 word_EmphSpace, /* WhiteSpace when emphasised */
153 word_CodeSpace, /* WhiteSpace when code */
154 word_WkCodeSpace, /* WhiteSpace when weak code */
155 /* ... and must be in the same order as these quote types ... */
156 word_Quote, /* text is NULL or ignorable */
157 word_EmphQuote, /* Quote when emphasised */
158 word_CodeQuote, /* (can't happen) */
159 word_WkCodeQuote, /* (can't happen) */
160 /* END ORDERING CONSTRAINT */
161 word_internal_endattrs,
162 word_UpperXref, /* \K */
163 word_LowerXref, /* \k */
164 word_XrefEnd, /* (invisible; no text) */
165 word_IndexRef, /* (always an invisible one) */
166 word_HyperLink, /* (invisible) */
167 word_HyperEnd, /* (also invisible; no text) */
168 /*
169 * Back ends may define their own word types beyond here, in
170 * case they need to use them internally.
171 */
172 word_NotWordType /* placeholder value */
173 };
174 /* aux values for attributed words */
175 enum {
176 attr_Only = 0x0000, /* a lone word with the attribute */
177 attr_First = 0x0001, /* the first of a series */
178 attr_Last = 0x0002, /* the last of a series */
179 attr_Always = 0x0003, /* any other part of a series */
180 attr_mask = 0x0003
181 };
182 /* aux values for quote-type words */
183 enum {
184 quote_Open = 0x0010,
185 quote_Close = 0x0020,
186 quote_mask = 0x0030
187 };
188 #define isattr(x) ( ( (x) > word_Normal && (x) < word_WhiteSpace ) || \
189 ( (x) > word_WhiteSpace && (x) < word_internal_endattrs ) )
190 #define sameattr(x,y) ( (((x)-(y)) & 3) == 0 )
191 #define towordstyle(x) ( word_Normal + ((x) & 3) )
192 #define tospacestyle(x) ( word_WhiteSpace + ((x) & 3) )
193 #define toquotestyle(x) ( word_Quote + ((x) & 3) )
194 #define removeattr(x) ( word_Normal + ((x) &~ 3) )
195
196 #define attraux(x) ( (x) & attr_mask )
197 #define quoteaux(x) ( (x) & quote_mask )
198
199 /*
200 * error.c
201 */
202 void fatal(int code, ...) NORETURN;
203 void error(int code, ...);
204 enum {
205 err_nomemory, /* out of memory */
206 err_optnoarg, /* option `-%s' requires an argument */
207 err_nosuchopt, /* unrecognised option `-%s' */
208 err_cmdcharset, /* unrecognised charset %s (cmdline) */
209 err_futileopt, /* futile option `-%s'%s */
210 err_noinput, /* no input files */
211 err_cantopen, /* unable to open input file `%s' */
212 err_nodata, /* no data in input files */
213 err_brokencodepara, /* line in codepara didn't begin `\c' */
214 err_kwunclosed, /* expected `}' after keyword */
215 err_kwillegal, /* paragraph type expects no keyword */
216 err_kwexpected, /* paragraph type expects a keyword */
217 err_kwtoomany, /* paragraph type expects only 1 */
218 err_bodyillegal, /* paragraph type expects only kws! */
219 err_badparatype, /* invalid command at start of para */
220 err_badmidcmd, /* invalid command in mid-para */
221 err_unexbrace, /* unexpected brace */
222 err_explbr, /* expected `{' after command */
223 err_commenteof, /* EOF inside braced comment */
224 err_kwexprbr, /* expected `}' after cross-ref */
225 err_codequote, /* \q within \c is not supported */
226 err_missingrbrace, /* unclosed braces at end of para */
227 err_missingrbrace2, /* unclosed braces at end of file */
228 err_nestedstyles, /* unable to nest text styles */
229 err_nestedindex, /* unable to nest `\i' thingys */
230 err_indexcase, /* two \i differing only in case */
231 err_nosuchkw, /* unresolved cross-reference */
232 err_multiBR, /* multiple \BRs on same keyword */
233 err_nosuchidxtag, /* \IM on unknown index tag (warning) */
234 err_cantopenw, /* can't open output file for write */
235 err_macroexists, /* this macro already exists */
236 err_sectjump, /* jump a heading level, eg \C -> \S */
237 err_winhelp_ctxclash, /* WinHelp context ID hash clash */
238 err_multikw, /* keyword clash in sections */
239 err_misplacedlcont, /* \lcont not after a list item */
240 err_sectmarkerinblock, /* section marker appeared in block */
241 err_cfginsufarg, /* \cfg{%s} insufficient args (<%d) */
242 err_infonodechar, /* colon/comma in node name in info */
243 err_text_codeline, /* \c line too long in text backend */
244 err_htmlver, /* unrecognised HTML version keyword */
245 err_charset, /* unrecognised character set name */
246 err_nofont, /* unrecognised font name */
247 err_afmeof, /* eof in AFM file */
248 err_afmkey, /* missing expected keyword in AFM */
249 err_afmvers, /* unsupported AFM version */
250 err_afmval, /* missing value(s) for AFM key */
251 err_pfeof, /* eof in Type 1 font file */
252 err_pfhead, /* bad Type 1 header line */
253 err_pfbad, /* otherwise invalide Type 1 font */
254 err_pfnoafm, /* Type 1 font but no AFM */
255 err_chmnames, /* need both or neither of hhp+chm */
256 err_whatever /* random error of another type */
257 };
258
259 /*
260 * malloc.c
261 */
262 #ifdef LOGALLOC
263 void *smalloc(char *file, int line, int size);
264 void *srealloc(char *file, int line, void *p, int size);
265 void sfree(char *file, int line, void *p);
266 #define smalloc(x) smalloc(__FILE__, __LINE__, x)
267 #define srealloc(x, y) srealloc(__FILE__, __LINE__, x, y)
268 #define sfree(x) sfree(__FILE__, __LINE__, x)
269 #else
270 void *smalloc(int size);
271 void *srealloc(void *p, int size);
272 void sfree(void *p);
273 #endif
274 void free_word_list(word *w);
275 void free_para_list(paragraph *p);
276 word *dup_word_list(word *w);
277 char *dupstr(char *s);
278
279 #define snew(type) ( (type *) smalloc (sizeof (type)) )
280 #define snewn(number, type) ( (type *) smalloc ((number) * sizeof (type)) )
281 #define sresize(array, number, type) \
282 ( (type *) srealloc ((array), (number) * sizeof (type)) )
283 #define lenof(array) ( sizeof(array) / sizeof(*(array)) )
284
285 /*
286 * ustring.c
287 */
288 wchar_t *ustrdup(wchar_t const *s);
289 char *ustrtoa(wchar_t const *s, char *outbuf, int size, int charset);
290 char *ustrtoa_careful(wchar_t const *s, char *outbuf, int size, int charset);
291 wchar_t *ustrfroma(char const *s, wchar_t *outbuf, int size, int charset);
292 char *utoa_dup(wchar_t const *s, int charset);
293 char *utoa_dup_len(wchar_t const *s, int charset, int *len);
294 char *utoa_careful_dup(wchar_t const *s, int charset);
295 wchar_t *ufroma_dup(char const *s, int charset);
296 char *utoa_locale_dup(wchar_t const *s);
297 wchar_t *ufroma_locale_dup(char const *s);
298 int ustrlen(wchar_t const *s);
299 wchar_t *uadv(wchar_t *s);
300 wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source);
301 wchar_t *ustrncpy(wchar_t *dest, wchar_t const *source, int n);
302 wchar_t utolower(wchar_t);
303 int uisalpha(wchar_t);
304 int ustrcmp(wchar_t *lhs, wchar_t *rhs);
305 int ustricmp(wchar_t const *lhs, wchar_t const *rhs);
306 int ustrnicmp(wchar_t const *lhs, wchar_t const *rhs, int maxlen);
307 int utoi(wchar_t const *);
308 double utof(wchar_t const *);
309 int utob(wchar_t const *);
310 int uisdigit(wchar_t);
311 wchar_t *ustrlow(wchar_t *s);
312 wchar_t *ustrftime(const wchar_t *wfmt, const struct tm *timespec);
313 int cvt_ok(int charset, const wchar_t *s);
314 int charset_from_ustr(filepos *fpos, const wchar_t *name);
315
316 /*
317 * wcwidth.c
318 */
319 int strwid(char const *s, int charset);
320 int ustrwid(wchar_t const *s, int charset);
321
322 /*
323 * help.c
324 */
325 void help(void);
326 void usage(void);
327 void showversion(void);
328 void listcharsets(void);
329
330 /*
331 * licence.c
332 */
333 void licence(void);
334
335 /*
336 * version.c
337 */
338 extern const char *const version;
339
340 /*
341 * misc.c
342 */
343 char *adv(char *s);
344
345 typedef struct stackTag *stack;
346 stack stk_new(void);
347 void stk_free(stack);
348 void stk_push(stack, void *);
349 void *stk_pop(stack);
350 void *stk_top(stack);
351
352 typedef struct tagRdstring rdstring;
353 struct tagRdstring {
354 int pos, size;
355 wchar_t *text;
356 };
357 typedef struct tagRdstringc rdstringc;
358 struct tagRdstringc {
359 int pos, size;
360 char *text;
361 };
362 extern const rdstring empty_rdstring;
363 extern const rdstringc empty_rdstringc;
364 void rdadd(rdstring *rs, wchar_t c);
365 void rdadds(rdstring *rs, wchar_t const *p);
366 wchar_t *rdtrim(rdstring *rs);
367 void rdaddc(rdstringc *rs, char c);
368 void rdaddsc(rdstringc *rs, char const *p);
369 void rdaddsn(rdstringc *rc, char const *p, int len);
370 char *rdtrimc(rdstringc *rs);
371
372 int compare_wordlists(word *a, word *b);
373
374 void mark_attr_ends(word *words);
375
376 typedef struct tagWrappedLine wrappedline;
377 struct tagWrappedLine {
378 wrappedline *next;
379 word *begin, *end; /* first & last words of line */
380 int nspaces; /* number of whitespaces in line */
381 int shortfall; /* how much shorter than max width */
382 };
383 wrappedline *wrap_para(word *, int, int, int (*)(void *, word *), void *, int);
384 void wrap_free(wrappedline *);
385 void cmdline_cfg_add(paragraph *cfg, char *string);
386 paragraph *cmdline_cfg_new(void);
387 paragraph *cmdline_cfg_simple(char *string, ...);
388
389 /*
390 * input.c
391 */
392 paragraph *read_input(input *in, indexdata *idx);
393
394 /*
395 * in_afm.c
396 */
397 void read_afm_file(input *in);
398
399 /*
400 * in_pf.c
401 */
402 void read_pfa_file(input *in);
403
404 /*
405 * keywords.c
406 */
407 struct keywordlist_Tag {
408 int nkeywords;
409 int size;
410 tree234 *keys; /* sorted by `key' field */
411 word **looseends; /* non-keyword list element numbers */
412 int nlooseends;
413 int looseendssize;
414 };
415 struct keyword_Tag {
416 wchar_t *key; /* the keyword itself */
417 word *text; /* "Chapter 2", "Appendix Q"... */
418 /* (NB: filepos are not set) */
419 paragraph *para; /* the paragraph referenced */
420 };
421 keyword *kw_lookup(keywordlist *, wchar_t *);
422 keywordlist *get_keywords(paragraph *);
423 void free_keywords(keywordlist *);
424 void subst_keywords(paragraph *, keywordlist *);
425
426 /*
427 * index.c
428 */
429
430 /*
431 * Data structure to hold both sides of the index.
432 */
433 struct indexdata_Tag {
434 tree234 *tags; /* holds type `indextag' */
435 tree234 *entries; /* holds type `indexentry' */
436 };
437
438 /*
439 * Data structure to hold an index tag (LHS of index).
440 */
441 struct indextag_Tag {
442 wchar_t *name;
443 word *implicit_text;
444 filepos implicit_fpos;
445 word **explicit_texts;
446 filepos *explicit_fpos;
447 int nexplicit, explicit_size;
448 int nrefs;
449 indexentry **refs; /* array of entries referenced by tag */
450 };
451
452 /*
453 * Data structure to hold an index entry (RHS of index).
454 */
455 struct indexentry_Tag {
456 word *text;
457 void *backend_data; /* private to back end */
458 filepos fpos;
459 };
460
461 indexdata *make_index(void);
462 void cleanup_index(indexdata *);
463 /* index_merge takes responsibility for freeing arg 3 iff implicit; never
464 * takes responsibility for arg 2 */
465 void index_merge(indexdata *, int is_explicit, wchar_t *, word *, filepos *);
466 void build_index(indexdata *);
467 void index_debug(indexdata *);
468 indextag *index_findtag(indexdata *idx, wchar_t *name);
469
470 /*
471 * contents.c
472 */
473 numberstate *number_init(void);
474 void number_cfg(numberstate *, paragraph *);
475 word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *);
476 void number_free(numberstate *);
477
478 /*
479 * biblio.c
480 */
481 void gen_citations(paragraph *, keywordlist *);
482
483 /*
484 * bk_text.c
485 */
486 void text_backend(paragraph *, keywordlist *, indexdata *, void *);
487 paragraph *text_config_filename(char *filename);
488
489 /*
490 * bk_html.c
491 */
492 void html_backend(paragraph *, keywordlist *, indexdata *, void *);
493 paragraph *html_config_filename(char *filename);
494
495 /*
496 * bk_whlp.c
497 */
498 void whlp_backend(paragraph *, keywordlist *, indexdata *, void *);
499 paragraph *whlp_config_filename(char *filename);
500
501 /*
502 * bk_man.c
503 */
504 void man_backend(paragraph *, keywordlist *, indexdata *, void *);
505 paragraph *man_config_filename(char *filename);
506
507 /*
508 * bk_info.c
509 */
510 void info_backend(paragraph *, keywordlist *, indexdata *, void *);
511 paragraph *info_config_filename(char *filename);
512
513 /*
514 * bk_paper.c
515 */
516 void *paper_pre_backend(paragraph *, keywordlist *, indexdata *);
517
518 /*
519 * bk_ps.c
520 */
521 void ps_backend(paragraph *, keywordlist *, indexdata *, void *);
522 paragraph *ps_config_filename(char *filename);
523
524 /*
525 * bk_pdf.c
526 */
527 void pdf_backend(paragraph *, keywordlist *, indexdata *, void *);
528 paragraph *pdf_config_filename(char *filename);
529
530 #endif