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