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