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