Implemented an index. Good _grief_, that was hard work to get all
[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;
5dd44dce 130
131 void *private_data; /* for temp use in backends */
d7482997 132};
133enum {
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 */
159enum {
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 */
167enum {
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 */
186void fatal(int code, ...) NORETURN;
187void error(int code, ...);
188enum {
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 */
7136a6c7 208 err_missingrbrace2, /* unclosed braces at end of file */
d7482997 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 */
7136a6c7 219 err_misplacedlcont, /* \lcont not after a list item */
2614b01d 220 err_sectmarkerinblock, /* section marker appeared in block */
d4c7e130 221 err_infodirentry, /* \cfg{info-dir-entry} missing param */
f4551933 222 err_infonodechar, /* colon/comma in node name in info */
d7482997 223 err_whatever /* random error of another type */
224};
225
226/*
227 * malloc.c
228 */
229#ifdef LOGALLOC
230void *smalloc(char *file, int line, int size);
231void *srealloc(char *file, int line, void *p, int size);
232void sfree(char *file, int line, void *p);
233#define smalloc(x) smalloc(__FILE__, __LINE__, x)
234#define srealloc(x, y) srealloc(__FILE__, __LINE__, x, y)
235#define sfree(x) sfree(__FILE__, __LINE__, x)
236#else
237void *smalloc(int size);
238void *srealloc(void *p, int size);
239void sfree(void *p);
240#endif
241void free_word_list(word *w);
242void free_para_list(paragraph *p);
243word *dup_word_list(word *w);
244char *dupstr(char *s);
245
246#define mknew(type) ( (type *) smalloc (sizeof (type)) )
247#define mknewa(type, number) ( (type *) smalloc ((number) * sizeof (type)) )
248#define resize(array, len) ( srealloc ((array), (len) * sizeof (*(array))) )
249#define lenof(array) ( sizeof(array) / sizeof(*(array)) )
250
251/*
252 * ustring.c
253 */
254wchar_t *ustrdup(wchar_t *s);
255char *ustrtoa(wchar_t *s, char *outbuf, int size);
ba9c1487 256wchar_t *ustrfroma(char *s, wchar_t *outbuf, int size);
50d6b4bd 257char *utoa_dup(wchar_t *s);
ba9c1487 258wchar_t *ufroma_dup(char *s);
5dd44dce 259int ustrlen(wchar_t const *s);
d7482997 260wchar_t *uadv(wchar_t *s);
5dd44dce 261wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source);
d7482997 262wchar_t utolower(wchar_t);
831da32e 263int uisalpha(wchar_t);
d7482997 264int ustrcmp(wchar_t *lhs, wchar_t *rhs);
265int ustricmp(wchar_t *lhs, wchar_t *rhs);
266int utoi(wchar_t *);
267int utob(wchar_t *);
268int uisdigit(wchar_t);
269wchar_t *ustrlow(wchar_t *s);
270wchar_t *ustrftime(wchar_t *fmt, struct tm *timespec);
271
272/*
273 * help.c
274 */
275void help(void);
276void usage(void);
277void showversion(void);
278
279/*
280 * licence.c
281 */
282void licence(void);
283
284/*
285 * version.c
286 */
287const char *const version;
288
289/*
290 * misc.c
291 */
292typedef struct stackTag *stack;
293stack stk_new(void);
294void stk_free(stack);
295void stk_push(stack, void *);
296void *stk_pop(stack);
7136a6c7 297void *stk_top(stack);
d7482997 298
299typedef struct tagRdstring rdstring;
300struct tagRdstring {
301 int pos, size;
302 wchar_t *text;
303};
304typedef struct tagRdstringc rdstringc;
305struct tagRdstringc {
306 int pos, size;
307 char *text;
308};
309extern const rdstring empty_rdstring;
310extern const rdstringc empty_rdstringc;
311void rdadd(rdstring *rs, wchar_t c);
5dd44dce 312void rdadds(rdstring *rs, wchar_t const *p);
d7482997 313wchar_t *rdtrim(rdstring *rs);
314void rdaddc(rdstringc *rs, char c);
5dd44dce 315void rdaddsc(rdstringc *rs, char const *p);
d7482997 316char *rdtrimc(rdstringc *rs);
317
318int compare_wordlists(word *a, word *b);
319
320void mark_attr_ends(paragraph *sourceform);
321
322typedef struct tagWrappedLine wrappedline;
323struct tagWrappedLine {
324 wrappedline *next;
325 word *begin, *end; /* first & last words of line */
326 int nspaces; /* number of whitespaces in line */
327 int shortfall; /* how much shorter than max width */
328};
43341922 329wrappedline *wrap_para(word *, int, int, int (*)(void *, word *), void *, int);
d7482997 330void wrap_free(wrappedline *);
331
332/*
333 * input.c
334 */
335paragraph *read_input(input *in, indexdata *idx);
336
337/*
338 * keywords.c
339 */
340struct keywordlist_Tag {
341 int nkeywords;
342 int size;
343 tree234 *keys; /* sorted by `key' field */
344 word **looseends; /* non-keyword list element numbers */
345 int nlooseends;
346 int looseendssize;
347};
348struct keyword_Tag {
349 wchar_t *key; /* the keyword itself */
350 word *text; /* "Chapter 2", "Appendix Q"... */
351 /* (NB: filepos are not set) */
352 paragraph *para; /* the paragraph referenced */
353};
354keyword *kw_lookup(keywordlist *, wchar_t *);
355keywordlist *get_keywords(paragraph *);
356void free_keywords(keywordlist *);
357void subst_keywords(paragraph *, keywordlist *);
358
359/*
360 * index.c
361 */
362
363/*
364 * Data structure to hold both sides of the index.
365 */
366struct indexdata_Tag {
367 tree234 *tags; /* holds type `indextag' */
368 tree234 *entries; /* holds type `indexentry' */
369};
370
371/*
372 * Data structure to hold an index tag (LHS of index).
373 */
374struct indextag_Tag {
375 wchar_t *name;
376 word *implicit_text;
f4551933 377 filepos implicit_fpos;
d7482997 378 word **explicit_texts;
f4551933 379 filepos *explicit_fpos;
d7482997 380 int nexplicit, explicit_size;
381 int nrefs;
382 indexentry **refs; /* array of entries referenced by tag */
383};
384
385/*
386 * Data structure to hold an index entry (RHS of index).
387 */
388struct indexentry_Tag {
389 word *text;
390 void *backend_data; /* private to back end */
f4551933 391 filepos fpos;
d7482997 392};
393
394indexdata *make_index(void);
395void cleanup_index(indexdata *);
396/* index_merge takes responsibility for freeing arg 3 iff implicit; never
397 * takes responsibility for arg 2 */
f4551933 398void index_merge(indexdata *, int is_explicit, wchar_t *, word *, filepos *);
d7482997 399void build_index(indexdata *);
400void index_debug(indexdata *);
401indextag *index_findtag(indexdata *idx, wchar_t *name);
402
403/*
404 * contents.c
405 */
406numberstate *number_init(void);
407void number_cfg(numberstate *, paragraph *);
96f3af16 408word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *);
d7482997 409void number_free(numberstate *);
410
411/*
412 * biblio.c
413 */
414void gen_citations(paragraph *, keywordlist *);
415
416/*
417 * style.c
418 */
419struct userstyle_Tag {
420};
421
422/*
423 * bk_text.c
424 */
43341922 425void text_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 426paragraph *text_config_filename(char *filename);
d7482997 427
428/*
429 * bk_xhtml.c
430 */
43341922 431void xhtml_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 432paragraph *xhtml_config_filename(char *filename);
d7482997 433
434/*
435 * bk_whlp.c
436 */
43341922 437void whlp_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 438paragraph *whlp_config_filename(char *filename);
d7482997 439
7136a6c7 440/*
441 * bk_man.c
442 */
43341922 443void man_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 444paragraph *man_config_filename(char *filename);
7136a6c7 445
5dd44dce 446/*
447 * bk_info.c
448 */
43341922 449void info_backend(paragraph *, keywordlist *, indexdata *, void *);
5dd44dce 450paragraph *info_config_filename(char *filename);
451
43341922 452/*
453 * bk_paper.c
454 */
455void *paper_pre_backend(paragraph *, keywordlist *, indexdata *);
456
457/*
458 * bk_ps.c
459 */
460void ps_backend(paragraph *, keywordlist *, indexdata *, void *);
461paragraph *ps_config_filename(char *filename);
462
463/*
464 * bk_pdf.c
465 */
466void pdf_backend(paragraph *, keywordlist *, indexdata *, void *);
467paragraph *pdf_config_filename(char *filename);
468
d7482997 469#endif