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