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