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