For some reason I decided it was better to say `output format'
[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,
7136a6c7 104 para_DescribedThing,
105 para_Description,
d7482997 106 para_Code,
107 para_Copyright,
d7482997 108 para_NoCite,
109 para_Title,
110 para_VersionID,
111 para_Config, /* configuration directive */
7136a6c7 112 para_LcontPush, /* begin continuation of list item */
113 para_LcontPop, /* end continuation of list item */
2614b01d 114 para_QuotePush, /* begin block quote */
115 para_QuotePop, /* end block quote */
d7482997 116 para_NotParaType /* placeholder value */
117};
118
119/*
120 * Data structure to hold an individual word
121 */
122struct word_Tag {
123 word *next, *alt;
124 int type;
125 int aux;
126 int breaks; /* can a line break after it? */
127 wchar_t *text;
128 filepos fpos;
129};
130enum {
131 /* ORDERING CONSTRAINT: these normal-word types ... */
132 word_Normal,
133 word_Emph,
134 word_Code, /* monospaced; `quoted' in text */
135 word_WeakCode, /* monospaced, normal in text */
136 /* ... must be in the same order as these space types ... */
137 word_WhiteSpace, /* text is NULL or ignorable */
138 word_EmphSpace, /* WhiteSpace when emphasised */
139 word_CodeSpace, /* WhiteSpace when code */
140 word_WkCodeSpace, /* WhiteSpace when weak code */
141 /* ... and must be in the same order as these quote types ... */
142 word_Quote, /* text is NULL or ignorable */
143 word_EmphQuote, /* Quote when emphasised */
144 word_CodeQuote, /* (can't happen) */
145 word_WkCodeQuote, /* (can't happen) */
146 /* END ORDERING CONSTRAINT */
147 word_internal_endattrs,
148 word_UpperXref, /* \K */
149 word_LowerXref, /* \k */
150 word_XrefEnd, /* (invisible; no text) */
151 word_IndexRef, /* (always an invisible one) */
152 word_HyperLink, /* (invisible) */
153 word_HyperEnd /* (also invisible; no text) */
154};
155/* aux values for attributed words */
156enum {
157 attr_Only = 0x0000, /* a lone word with the attribute */
158 attr_First = 0x0001, /* the first of a series */
159 attr_Last = 0x0002, /* the last of a series */
160 attr_Always = 0x0003, /* any other part of a series */
161 attr_mask = 0x0003,
162};
163/* aux values for quote-type words */
164enum {
165 quote_Open = 0x0010,
166 quote_Close = 0x0020,
167 quote_mask = 0x0030,
168};
169#define isattr(x) ( ( (x) > word_Normal && (x) < word_WhiteSpace ) || \
170 ( (x) > word_WhiteSpace && (x) < word_internal_endattrs ) )
171#define sameattr(x,y) ( (((x)-(y)) & 3) == 0 )
172#define towordstyle(x) ( word_Normal + ((x) & 3) )
173#define tospacestyle(x) ( word_WhiteSpace + ((x) & 3) )
174#define toquotestyle(x) ( word_Quote + ((x) & 3) )
175#define removeattr(x) ( word_Normal + ((x) &~ 3) )
176
177#define attraux(x) ( (x) & attr_mask )
178#define quoteaux(x) ( (x) & quote_mask )
179
180/*
181 * error.c
182 */
183void fatal(int code, ...) NORETURN;
184void error(int code, ...);
185enum {
186 err_nomemory, /* out of memory */
187 err_optnoarg, /* option `-%s' requires an argument */
188 err_nosuchopt, /* unrecognised option `-%s' */
189 err_noinput, /* no input files */
190 err_cantopen, /* unable to open input file `%s' */
191 err_nodata, /* no data in input files */
192 err_brokencodepara, /* line in codepara didn't begin `\c' */
193 err_kwunclosed, /* expected `}' after keyword */
194 err_kwillegal, /* paragraph type expects no keyword */
195 err_kwexpected, /* paragraph type expects a keyword */
196 err_kwtoomany, /* paragraph type expects only 1 */
197 err_bodyillegal, /* paragraph type expects only kws! */
198 err_badparatype, /* invalid command at start of para */
199 err_badmidcmd, /* invalid command in mid-para */
200 err_unexbrace, /* unexpected brace */
201 err_explbr, /* expected `{' after command */
202 err_commenteof, /* EOF inside braced comment */
203 err_kwexprbr, /* expected `}' after cross-ref */
204 err_missingrbrace, /* unclosed braces at end of para */
7136a6c7 205 err_missingrbrace2, /* unclosed braces at end of file */
d7482997 206 err_nestedstyles, /* unable to nest text styles */
207 err_nestedindex, /* unable to nest `\i' thingys */
208 err_nosuchkw, /* unresolved cross-reference */
209 err_multiBR, /* multiple \BRs on same keyword */
210 err_nosuchidxtag, /* \IM on unknown index tag (warning) */
211 err_cantopenw, /* can't open output file for write */
212 err_macroexists, /* this macro already exists */
213 err_sectjump, /* jump a heading level, eg \C -> \S */
214 err_winhelp_ctxclash, /* WinHelp context ID hash clash */
215 err_multikw, /* keyword clash in sections */
7136a6c7 216 err_misplacedlcont, /* \lcont not after a list item */
2614b01d 217 err_sectmarkerinblock, /* section marker appeared in block */
d7482997 218 err_whatever /* random error of another type */
219};
220
221/*
222 * malloc.c
223 */
224#ifdef LOGALLOC
225void *smalloc(char *file, int line, int size);
226void *srealloc(char *file, int line, void *p, int size);
227void sfree(char *file, int line, void *p);
228#define smalloc(x) smalloc(__FILE__, __LINE__, x)
229#define srealloc(x, y) srealloc(__FILE__, __LINE__, x, y)
230#define sfree(x) sfree(__FILE__, __LINE__, x)
231#else
232void *smalloc(int size);
233void *srealloc(void *p, int size);
234void sfree(void *p);
235#endif
236void free_word_list(word *w);
237void free_para_list(paragraph *p);
238word *dup_word_list(word *w);
239char *dupstr(char *s);
240
241#define mknew(type) ( (type *) smalloc (sizeof (type)) )
242#define mknewa(type, number) ( (type *) smalloc ((number) * sizeof (type)) )
243#define resize(array, len) ( srealloc ((array), (len) * sizeof (*(array))) )
244#define lenof(array) ( sizeof(array) / sizeof(*(array)) )
245
246/*
247 * ustring.c
248 */
249wchar_t *ustrdup(wchar_t *s);
250char *ustrtoa(wchar_t *s, char *outbuf, int size);
ba9c1487 251wchar_t *ustrfroma(char *s, wchar_t *outbuf, int size);
50d6b4bd 252char *utoa_dup(wchar_t *s);
ba9c1487 253wchar_t *ufroma_dup(char *s);
d7482997 254int ustrlen(wchar_t *s);
255wchar_t *uadv(wchar_t *s);
256wchar_t *ustrcpy(wchar_t *dest, wchar_t *source);
257wchar_t utolower(wchar_t);
831da32e 258int uisalpha(wchar_t);
d7482997 259int ustrcmp(wchar_t *lhs, wchar_t *rhs);
260int ustricmp(wchar_t *lhs, wchar_t *rhs);
261int utoi(wchar_t *);
262int utob(wchar_t *);
263int uisdigit(wchar_t);
264wchar_t *ustrlow(wchar_t *s);
265wchar_t *ustrftime(wchar_t *fmt, struct tm *timespec);
266
267/*
268 * help.c
269 */
270void help(void);
271void usage(void);
272void showversion(void);
273
274/*
275 * licence.c
276 */
277void licence(void);
278
279/*
280 * version.c
281 */
282const char *const version;
283
284/*
285 * misc.c
286 */
287typedef struct stackTag *stack;
288stack stk_new(void);
289void stk_free(stack);
290void stk_push(stack, void *);
291void *stk_pop(stack);
7136a6c7 292void *stk_top(stack);
d7482997 293
294typedef struct tagRdstring rdstring;
295struct tagRdstring {
296 int pos, size;
297 wchar_t *text;
298};
299typedef struct tagRdstringc rdstringc;
300struct tagRdstringc {
301 int pos, size;
302 char *text;
303};
304extern const rdstring empty_rdstring;
305extern const rdstringc empty_rdstringc;
306void rdadd(rdstring *rs, wchar_t c);
307void rdadds(rdstring *rs, wchar_t *p);
308wchar_t *rdtrim(rdstring *rs);
309void rdaddc(rdstringc *rs, char c);
310void rdaddsc(rdstringc *rs, char *p);
311char *rdtrimc(rdstringc *rs);
312
313int compare_wordlists(word *a, word *b);
314
315void mark_attr_ends(paragraph *sourceform);
316
317typedef struct tagWrappedLine wrappedline;
318struct tagWrappedLine {
319 wrappedline *next;
320 word *begin, *end; /* first & last words of line */
321 int nspaces; /* number of whitespaces in line */
322 int shortfall; /* how much shorter than max width */
323};
324wrappedline *wrap_para(word *, int, int, int (*)(word *));
325void wrap_free(wrappedline *);
326
327/*
328 * input.c
329 */
330paragraph *read_input(input *in, indexdata *idx);
331
332/*
333 * keywords.c
334 */
335struct keywordlist_Tag {
336 int nkeywords;
337 int size;
338 tree234 *keys; /* sorted by `key' field */
339 word **looseends; /* non-keyword list element numbers */
340 int nlooseends;
341 int looseendssize;
342};
343struct keyword_Tag {
344 wchar_t *key; /* the keyword itself */
345 word *text; /* "Chapter 2", "Appendix Q"... */
346 /* (NB: filepos are not set) */
347 paragraph *para; /* the paragraph referenced */
348};
349keyword *kw_lookup(keywordlist *, wchar_t *);
350keywordlist *get_keywords(paragraph *);
351void free_keywords(keywordlist *);
352void subst_keywords(paragraph *, keywordlist *);
353
354/*
355 * index.c
356 */
357
358/*
359 * Data structure to hold both sides of the index.
360 */
361struct indexdata_Tag {
362 tree234 *tags; /* holds type `indextag' */
363 tree234 *entries; /* holds type `indexentry' */
364};
365
366/*
367 * Data structure to hold an index tag (LHS of index).
368 */
369struct indextag_Tag {
370 wchar_t *name;
371 word *implicit_text;
372 word **explicit_texts;
373 int nexplicit, explicit_size;
374 int nrefs;
375 indexentry **refs; /* array of entries referenced by tag */
376};
377
378/*
379 * Data structure to hold an index entry (RHS of index).
380 */
381struct indexentry_Tag {
382 word *text;
383 void *backend_data; /* private to back end */
384};
385
386indexdata *make_index(void);
387void cleanup_index(indexdata *);
388/* index_merge takes responsibility for freeing arg 3 iff implicit; never
389 * takes responsibility for arg 2 */
390void index_merge(indexdata *, int is_explicit, wchar_t *, word *);
391void build_index(indexdata *);
392void index_debug(indexdata *);
393indextag *index_findtag(indexdata *idx, wchar_t *name);
394
395/*
396 * contents.c
397 */
398numberstate *number_init(void);
399void number_cfg(numberstate *, paragraph *);
96f3af16 400word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *);
d7482997 401void number_free(numberstate *);
402
403/*
404 * biblio.c
405 */
406void gen_citations(paragraph *, keywordlist *);
407
408/*
409 * style.c
410 */
411struct userstyle_Tag {
412};
413
414/*
415 * bk_text.c
416 */
417void text_backend(paragraph *, keywordlist *, indexdata *);
ba9c1487 418paragraph *text_config_filename(char *filename);
d7482997 419
420/*
421 * bk_xhtml.c
422 */
423void xhtml_backend(paragraph *, keywordlist *, indexdata *);
ba9c1487 424paragraph *xhtml_config_filename(char *filename);
d7482997 425
426/*
427 * bk_whlp.c
428 */
429void whlp_backend(paragraph *, keywordlist *, indexdata *);
ba9c1487 430paragraph *whlp_config_filename(char *filename);
d7482997 431
7136a6c7 432/*
433 * bk_man.c
434 */
435void man_backend(paragraph *, keywordlist *, indexdata *);
ba9c1487 436paragraph *man_config_filename(char *filename);
7136a6c7 437
d7482997 438#endif