Revamp of the Halibut error handling mechanism.
[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 int wantclose; /* does the current file want closing */
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 */
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[] */
75 char *pushback_chars; /* used to save input-encoding data */
76 };
77
78 /*
79 * Data structure to hold the input form of the source, ie a linked
80 * list of paragraphs
81 */
82 struct paragraph_Tag {
83 paragraph *next;
84 int type;
85 wchar_t *keyword; /* for most special paragraphs */
86 char *origkeyword; /* same again in original charset */
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 };
99 enum {
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,
113 para_DescribedThing,
114 para_Description,
115 para_Code,
116 para_Copyright,
117 para_NoCite,
118 para_Title,
119 para_VersionID,
120 para_Config, /* configuration directive */
121 para_LcontPush, /* begin continuation of list item */
122 para_LcontPop, /* end continuation of list item */
123 para_QuotePush, /* begin block quote */
124 para_QuotePop, /* end block quote */
125 /*
126 * Back ends may define their own paragraph types beyond here,
127 * in case they need to use them internally.
128 */
129 para_NotParaType /* placeholder value */
130 };
131
132 /*
133 * Data structure to hold an individual word
134 */
135 struct 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;
142
143 void *private_data; /* for temp use in backends */
144 };
145 enum {
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) */
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 */
174 };
175 /* aux values for attributed words */
176 enum {
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 */
181 attr_mask = 0x0003
182 };
183 /* aux values for quote-type words */
184 enum {
185 quote_Open = 0x0010,
186 quote_Close = 0x0020,
187 quote_mask = 0x0030
188 };
189 #define isvis(x) ( ( (x) >= word_Normal && (x) <= word_LowerXref ) )
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 */
204 /* out of memory */
205 void fatalerr_nomemory(void) NORETURN;
206 /* option `-%s' requires an argument */
207 void err_optnoarg(const char *sp);
208 /* unrecognised option `-%s' */
209 void err_nosuchopt(const char *sp);
210 /* unrecognised charset %s (cmdline) */
211 void err_cmdcharset(const char *sp);
212 /* futile option `-%s'%s */
213 void err_futileopt(const char *sp, const char *sp2);
214 /* no input files */
215 void err_noinput(void);
216 /* unable to open input file `%s' */
217 void err_cantopen(const char *sp);
218 /* no data in input files */
219 void err_nodata(void);
220 /* line in codepara didn't begin `\c' */
221 void err_brokencodepara(const filepos *fpos);
222 /* expected `}' after keyword */
223 void err_kwunclosed(const filepos *fpos);
224 /* paragraph type expects no keyword */
225 void err_kwexpected(const filepos *fpos);
226 /* paragraph type expects a keyword */
227 void err_kwillegal(const filepos *fpos);
228 /* paragraph type expects only 1 */
229 void err_kwtoomany(const filepos *fpos);
230 /* paragraph type expects only kws! */
231 void err_bodyillegal(const filepos *fpos);
232 /* invalid command at start of para */
233 void err_badparatype(const wchar_t *wsp, const filepos *fpos);
234 /* invalid command in mid-para */
235 void err_badmidcmd(const wchar_t *wsp, const filepos *fpos);
236 /* unexpected brace */
237 void err_unexbrace(const filepos *fpos);
238 /* expected `{' after command */
239 void err_explbr(const filepos *fpos);
240 /* EOF inside braced comment */
241 void err_commenteof(const filepos *fpos);
242 /* expected `}' after cross-ref */
243 void err_kwexprbr(const filepos *fpos);
244 /* \q within \c is not supported */
245 void err_codequote(const filepos *fpos);
246 /* unclosed braces at end of para */
247 void err_missingrbrace(const filepos *fpos);
248 /* unclosed braces at end of file */
249 void err_missingrbrace2(const filepos *fpos);
250 /* unable to nest text styles */
251 void err_nestedstyles(const filepos *fpos);
252 /* unable to nest `\i' thingys */
253 void err_nestedindex(const filepos *fpos);
254 /* two \i differing only in case */
255 void err_indexcase(const filepos *fpos, const wchar_t *wsp,
256 const filepos *fpos2, const wchar_t *wsp2);
257 /* unresolved cross-reference */
258 void err_nosuchkw(const filepos *fpos, const wchar_t *wsp);
259 /* multiple \BRs on same keyword */
260 void err_multiBR(const filepos *fpos, const wchar_t *wsp);
261 /* \IM on unknown index tag (warning) */
262 void err_nosuchidxtag(const filepos *fpos, const wchar_t *wsp);
263 /* can't open output file for write */
264 void err_cantopenw(const char *sp);
265 /* this macro already exists */
266 void err_macroexists(const filepos *fpos, const wchar_t *wsp);
267 /* jump a heading level, eg \C -> \S */
268 void err_sectjump(const filepos *fpos);
269 /* WinHelp context ID hash clash */
270 void err_winhelp_ctxclash(const filepos *fpos, const char *sp, const char *sp2);
271 /* keyword clash in sections */
272 void err_multikw(const filepos *fpos, const filepos *fpos2, const wchar_t *wsp);
273 /* \lcont not after a list item */
274 void err_misplacedlcont(const filepos *fpos);
275 /* section marker appeared in block */
276 void err_sectmarkerinblock(const filepos *fpos, const char *sp);
277 /* \cfg{%s} insufficient args (<%d) */
278 void err_cfginsufarg(const filepos *fpos, const char *sp, int i);
279 /* colon/comma in node name in info */
280 void err_infonodechar(const filepos *fpos, char c) /* fpos might be NULL */;
281 /* \c line too long in text backend */
282 void err_text_codeline(const filepos *fpos, int i, int j);
283 /* unrecognised HTML version keyword */
284 void err_htmlver(const filepos *fpos, const wchar_t *wsp);
285 /* unrecognised character set name */
286 void err_charset(const filepos *fpos, const wchar_t *wsp);
287 /* unrecognised font name */
288 void err_nofont(const filepos *fpos, const wchar_t *wsp);
289 /* eof in AFM file */
290 void err_afmeof(const filepos *fpos);
291 /* missing expected keyword in AFM */
292 void err_afmkey(const filepos *fpos, const char *sp);
293 /* unsupported AFM version */
294 void err_afmvers(const filepos *fpos);
295 /* missing value(s) for AFM key */
296 void err_afmval(const filepos *fpos, const char *sp, int i);
297 /* eof in Type 1 font file */
298 void err_pfeof(const filepos *fpos);
299 /* bad Type 1 header line */
300 void err_pfhead(const filepos *fpos);
301 /* otherwise invalide Type 1 font */
302 void err_pfbad(const filepos *fpos);
303 /* Type 1 font but no AFM */
304 void err_pfnoafm(const filepos *fpos, const char *sp);
305 /* need both or neither of hhp+chm */
306 void err_chmnames(void);
307 /* required sfnt table missing */
308 void err_sfntnotable(const filepos *fpos, const char *sp);
309 /* sfnt has no PostScript name */
310 void err_sfntnopsname(const filepos *fpos);
311 /* sfnt table not valid */
312 void err_sfntbadtable(const filepos *fpos, const char *sp);
313 /* sfnt has no UCS-2 cmap */
314 void err_sfntnounicmap(const filepos *fpos);
315 /* sfnt table version unknown */
316 void err_sfnttablevers(const filepos *fpos, const char *sp);
317 /* sfnt has bad header */
318 void err_sfntbadhdr(const filepos *fpos);
319 /* sfnt cmap references bad glyph */
320 void err_sfntbadglyph(const filepos *fpos, unsigned wc);
321
322 /*
323 * malloc.c
324 */
325 #ifdef LOGALLOC
326 void *smalloc(char *file, int line, int size);
327 void *srealloc(char *file, int line, void *p, int size);
328 void 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
333 void *smalloc(int size);
334 void *srealloc(void *p, int size);
335 void sfree(void *p);
336 #endif
337 void free_word_list(word *w);
338 void free_para_list(paragraph *p);
339 word *dup_word_list(word *w);
340 char *dupstr(char const *s);
341
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)) )
346 #define lenof(array) ( sizeof(array) / sizeof(*(array)) )
347
348 /*
349 * ustring.c
350 */
351 wchar_t *ustrdup(wchar_t const *s);
352 char *ustrtoa(wchar_t const *s, char *outbuf, int size, int charset);
353 char *ustrtoa_careful(wchar_t const *s, char *outbuf, int size, int charset);
354 wchar_t *ustrfroma(char const *s, wchar_t *outbuf, int size, int charset);
355 char *utoa_dup(wchar_t const *s, int charset);
356 char *utoa_dup_len(wchar_t const *s, int charset, int *len);
357 char *utoa_careful_dup(wchar_t const *s, int charset);
358 wchar_t *ufroma_dup(char const *s, int charset);
359 char *utoa_locale_dup(wchar_t const *s);
360 wchar_t *ufroma_locale_dup(char const *s);
361 int ustrlen(wchar_t const *s);
362 wchar_t *uadv(wchar_t *s);
363 wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source);
364 wchar_t *ustrncpy(wchar_t *dest, wchar_t const *source, int n);
365 wchar_t utolower(wchar_t);
366 int uisalpha(wchar_t);
367 int ustrcmp(wchar_t *lhs, wchar_t *rhs);
368 int ustricmp(wchar_t const *lhs, wchar_t const *rhs);
369 int ustrnicmp(wchar_t const *lhs, wchar_t const *rhs, int maxlen);
370 int utoi(wchar_t const *);
371 double utof(wchar_t const *);
372 int utob(wchar_t const *);
373 int uisdigit(wchar_t);
374 wchar_t *ustrlow(wchar_t *s);
375 wchar_t *ustrftime(const wchar_t *wfmt, const struct tm *timespec);
376 int cvt_ok(int charset, const wchar_t *s);
377 int charset_from_ustr(filepos *fpos, const wchar_t *name);
378
379 /*
380 * wcwidth.c
381 */
382 int strwid(char const *s, int charset);
383 int ustrwid(wchar_t const *s, int charset);
384
385 /*
386 * help.c
387 */
388 void help(void);
389 void usage(void);
390 void showversion(void);
391 void listcharsets(void);
392
393 /*
394 * licence.c
395 */
396 void licence(void);
397
398 /*
399 * version.c
400 */
401 extern const char *const version;
402
403 /*
404 * misc.c
405 */
406 char *adv(char *s);
407
408 typedef struct stackTag *stack;
409 stack stk_new(void);
410 void stk_free(stack);
411 void stk_push(stack, void *);
412 void *stk_pop(stack);
413 void *stk_top(stack);
414
415 typedef struct tagRdstring rdstring;
416 struct tagRdstring {
417 int pos, size;
418 wchar_t *text;
419 };
420 typedef struct tagRdstringc rdstringc;
421 struct tagRdstringc {
422 int pos, size;
423 char *text;
424 };
425 extern const rdstring empty_rdstring;
426 extern const rdstringc empty_rdstringc;
427 void rdadd(rdstring *rs, wchar_t c);
428 void rdadds(rdstring *rs, wchar_t const *p);
429 wchar_t *rdtrim(rdstring *rs);
430 void rdaddc(rdstringc *rs, char c);
431 void rdaddsc(rdstringc *rs, char const *p);
432 void rdaddsn(rdstringc *rc, char const *p, int len);
433 char *rdtrimc(rdstringc *rs);
434
435 int compare_wordlists(word *a, word *b);
436
437 void mark_attr_ends(word *words);
438
439 typedef struct tagWrappedLine wrappedline;
440 struct 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 };
446 wrappedline *wrap_para(word *, int, int, int (*)(void *, word *), void *, int);
447 void wrap_free(wrappedline *);
448 void cmdline_cfg_add(paragraph *cfg, char *string);
449 paragraph *cmdline_cfg_new(void);
450 paragraph *cmdline_cfg_simple(char *string, ...);
451
452 /*
453 * input.c
454 */
455 paragraph *read_input(input *in, indexdata *idx);
456
457 /*
458 * in_afm.c
459 */
460 void read_afm_file(input *in);
461
462 /*
463 * in_pf.c
464 */
465 void read_pfa_file(input *in);
466 void read_pfb_file(input *in);
467
468 /*
469 * in_sfnt.c
470 */
471 void read_sfnt_file(input *in);
472
473 /*
474 * keywords.c
475 */
476 struct 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 };
484 struct 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 };
490 keyword *kw_lookup(keywordlist *, wchar_t *);
491 keywordlist *get_keywords(paragraph *);
492 void free_keywords(keywordlist *);
493 void subst_keywords(paragraph *, keywordlist *);
494
495 /*
496 * index.c
497 */
498
499 /*
500 * Data structure to hold both sides of the index.
501 */
502 struct 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 */
510 struct indextag_Tag {
511 wchar_t *name;
512 word *implicit_text;
513 filepos implicit_fpos;
514 word **explicit_texts;
515 filepos *explicit_fpos;
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 */
524 struct indexentry_Tag {
525 word *text;
526 void *backend_data; /* private to back end */
527 filepos fpos;
528 };
529
530 indexdata *make_index(void);
531 void cleanup_index(indexdata *);
532 /* index_merge takes responsibility for freeing arg 3 iff implicit; never
533 * takes responsibility for arg 2 */
534 void index_merge(indexdata *, int is_explicit, wchar_t *, word *, filepos *);
535 void build_index(indexdata *);
536 void index_debug(indexdata *);
537 indextag *index_findtag(indexdata *idx, wchar_t *name);
538
539 /*
540 * contents.c
541 */
542 numberstate *number_init(void);
543 void number_cfg(numberstate *, paragraph *);
544 word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *);
545 void number_free(numberstate *);
546
547 /*
548 * biblio.c
549 */
550 void gen_citations(paragraph *, keywordlist *);
551
552 /*
553 * bk_text.c
554 */
555 void text_backend(paragraph *, keywordlist *, indexdata *, void *);
556 paragraph *text_config_filename(char *filename);
557
558 /*
559 * bk_html.c
560 */
561 void html_backend(paragraph *, keywordlist *, indexdata *, void *);
562 paragraph *html_config_filename(char *filename);
563
564 /*
565 * bk_whlp.c
566 */
567 void whlp_backend(paragraph *, keywordlist *, indexdata *, void *);
568 paragraph *whlp_config_filename(char *filename);
569
570 /*
571 * bk_man.c
572 */
573 void man_backend(paragraph *, keywordlist *, indexdata *, void *);
574 paragraph *man_config_filename(char *filename);
575
576 /*
577 * bk_info.c
578 */
579 void info_backend(paragraph *, keywordlist *, indexdata *, void *);
580 paragraph *info_config_filename(char *filename);
581
582 /*
583 * bk_paper.c
584 */
585 void *paper_pre_backend(paragraph *, keywordlist *, indexdata *);
586 void listfonts(void);
587
588 /*
589 * bk_ps.c
590 */
591 void ps_backend(paragraph *, keywordlist *, indexdata *, void *);
592 paragraph *ps_config_filename(char *filename);
593
594 /*
595 * bk_pdf.c
596 */
597 void pdf_backend(paragraph *, keywordlist *, indexdata *, void *);
598 paragraph *pdf_config_filename(char *filename);
599
600 #endif