Don't forget to mention the default setting for \cfg{input-charset}.
[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
e34ba5c3 9#include "charset.h"
10
d7482997 11#ifdef __GNUC__
12#define NORETURN __attribute__((__noreturn__))
13#else
14#define NORETURN /* nothing */
15#endif
16
17#ifndef TRUE
18#define TRUE 1
19#endif
20#ifndef FALSE
21#define FALSE 0
22#endif
23
24/* For suppressing unused-parameter warnings */
25#define IGNORE(x) ( (x) = (x) )
26
27#include "tree234.h"
28
29/*
e4ea58f8 30 * FIXME: Charset temporary workarounds
31 */
32#define CS_FIXME CS_ISO8859_1
33#define CS_LOCAL CS_ISO8859_1
34
35/*
d7482997 36 * Structure tags
37 */
38typedef struct input_Tag input;
39typedef struct filepos_Tag filepos;
40typedef struct paragraph_Tag paragraph;
41typedef struct word_Tag word;
42typedef struct keywordlist_Tag keywordlist;
43typedef struct keyword_Tag keyword;
44typedef struct userstyle_Tag userstyle;
45typedef struct numberstate_Tag numberstate;
46typedef struct indexdata_Tag indexdata;
47typedef struct indextag_Tag indextag;
48typedef struct indexentry_Tag indexentry;
49typedef struct macrostack_Tag macrostack;
50
51/*
52 * Data structure to hold a file name and index, a line and a
53 * column number, for reporting errors
54 */
55struct filepos_Tag {
56 char *filename;
57 int line, col;
58};
59
60/*
61 * Data structure to hold all the file names etc for input
62 */
63typedef struct pushback_Tag {
64 int chr;
65 filepos pos;
66} pushback;
67struct input_Tag {
68 char **filenames; /* complete list of input files */
69 int nfiles; /* how many in the list */
70 FILE *currfp; /* the currently open one */
71 int currindex; /* which one is that in the list */
72 pushback *pushback; /* pushed-back input characters */
73 int npushback, pushbacksize;
74 filepos pos;
75 int reportcols; /* report column numbers in errors */
76 macrostack *stack; /* macro expansions in force */
e34ba5c3 77 int defcharset, charset; /* character sets for input files */
78 charset_state csstate;
79 wchar_t wc[16]; /* wide chars from input conversion */
80 int nwc, wcpos; /* size of, and position in, wc[] */
e4ea58f8 81 char *pushback_chars; /* used to save input-encoding data */
d7482997 82};
83
84/*
85 * Data structure to hold the input form of the source, ie a linked
86 * list of paragraphs
87 */
88struct paragraph_Tag {
89 paragraph *next;
90 int type;
91 wchar_t *keyword; /* for most special paragraphs */
e4ea58f8 92 char *origkeyword; /* same again in original charset */
d7482997 93 word *words; /* list of words in paragraph */
94 int aux; /* number, in a numbered paragraph
95 * or subsection level
96 */
97 word *kwtext; /* chapter/section indication */
98 word *kwtext2; /* numeric-only form of kwtext */
99 filepos fpos;
100
101 paragraph *parent, *child, *sibling; /* for hierarchy navigation */
102
103 void *private_data; /* for temp use in backends */
104};
105enum {
106 para_IM, /* index merge */
107 para_BR, /* bibliography rewrite */
108 para_Rule, /* random horizontal rule */
109 para_Chapter,
110 para_Appendix,
111 para_UnnumberedChapter,
112 para_Heading,
113 para_Subsect,
114 para_Normal,
115 para_Biblio, /* causes no output unless turned ... */
116 para_BiblioCited, /* ... into this paragraph type */
117 para_Bullet,
118 para_NumberedList,
7136a6c7 119 para_DescribedThing,
120 para_Description,
d7482997 121 para_Code,
122 para_Copyright,
d7482997 123 para_NoCite,
124 para_Title,
125 para_VersionID,
126 para_Config, /* configuration directive */
7136a6c7 127 para_LcontPush, /* begin continuation of list item */
128 para_LcontPop, /* end continuation of list item */
2614b01d 129 para_QuotePush, /* begin block quote */
130 para_QuotePop, /* end block quote */
3f3d1acc 131 /*
132 * Back ends may define their own paragraph types beyond here,
133 * in case they need to use them internally.
134 */
d7482997 135 para_NotParaType /* placeholder value */
136};
137
138/*
139 * Data structure to hold an individual word
140 */
141struct word_Tag {
142 word *next, *alt;
143 int type;
144 int aux;
145 int breaks; /* can a line break after it? */
146 wchar_t *text;
147 filepos fpos;
5dd44dce 148
149 void *private_data; /* for temp use in backends */
d7482997 150};
151enum {
152 /* ORDERING CONSTRAINT: these normal-word types ... */
153 word_Normal,
154 word_Emph,
155 word_Code, /* monospaced; `quoted' in text */
156 word_WeakCode, /* monospaced, normal in text */
157 /* ... must be in the same order as these space types ... */
158 word_WhiteSpace, /* text is NULL or ignorable */
159 word_EmphSpace, /* WhiteSpace when emphasised */
160 word_CodeSpace, /* WhiteSpace when code */
161 word_WkCodeSpace, /* WhiteSpace when weak code */
162 /* ... and must be in the same order as these quote types ... */
163 word_Quote, /* text is NULL or ignorable */
164 word_EmphQuote, /* Quote when emphasised */
165 word_CodeQuote, /* (can't happen) */
166 word_WkCodeQuote, /* (can't happen) */
167 /* END ORDERING CONSTRAINT */
168 word_internal_endattrs,
169 word_UpperXref, /* \K */
170 word_LowerXref, /* \k */
171 word_XrefEnd, /* (invisible; no text) */
172 word_IndexRef, /* (always an invisible one) */
173 word_HyperLink, /* (invisible) */
3f3d1acc 174 word_HyperEnd, /* (also invisible; no text) */
175 /*
176 * Back ends may define their own word types beyond here, in
177 * case they need to use them internally.
178 */
179 word_NotWordType /* placeholder value */
d7482997 180};
181/* aux values for attributed words */
182enum {
183 attr_Only = 0x0000, /* a lone word with the attribute */
184 attr_First = 0x0001, /* the first of a series */
185 attr_Last = 0x0002, /* the last of a series */
186 attr_Always = 0x0003, /* any other part of a series */
187 attr_mask = 0x0003,
188};
189/* aux values for quote-type words */
190enum {
191 quote_Open = 0x0010,
192 quote_Close = 0x0020,
193 quote_mask = 0x0030,
194};
195#define isattr(x) ( ( (x) > word_Normal && (x) < word_WhiteSpace ) || \
196 ( (x) > word_WhiteSpace && (x) < word_internal_endattrs ) )
197#define sameattr(x,y) ( (((x)-(y)) & 3) == 0 )
198#define towordstyle(x) ( word_Normal + ((x) & 3) )
199#define tospacestyle(x) ( word_WhiteSpace + ((x) & 3) )
200#define toquotestyle(x) ( word_Quote + ((x) & 3) )
201#define removeattr(x) ( word_Normal + ((x) &~ 3) )
202
203#define attraux(x) ( (x) & attr_mask )
204#define quoteaux(x) ( (x) & quote_mask )
205
206/*
207 * error.c
208 */
209void fatal(int code, ...) NORETURN;
210void error(int code, ...);
211enum {
212 err_nomemory, /* out of memory */
213 err_optnoarg, /* option `-%s' requires an argument */
214 err_nosuchopt, /* unrecognised option `-%s' */
215 err_noinput, /* no input files */
216 err_cantopen, /* unable to open input file `%s' */
217 err_nodata, /* no data in input files */
218 err_brokencodepara, /* line in codepara didn't begin `\c' */
219 err_kwunclosed, /* expected `}' after keyword */
220 err_kwillegal, /* paragraph type expects no keyword */
221 err_kwexpected, /* paragraph type expects a keyword */
222 err_kwtoomany, /* paragraph type expects only 1 */
223 err_bodyillegal, /* paragraph type expects only kws! */
224 err_badparatype, /* invalid command at start of para */
225 err_badmidcmd, /* invalid command in mid-para */
226 err_unexbrace, /* unexpected brace */
227 err_explbr, /* expected `{' after command */
228 err_commenteof, /* EOF inside braced comment */
229 err_kwexprbr, /* expected `}' after cross-ref */
230 err_missingrbrace, /* unclosed braces at end of para */
7136a6c7 231 err_missingrbrace2, /* unclosed braces at end of file */
d7482997 232 err_nestedstyles, /* unable to nest text styles */
233 err_nestedindex, /* unable to nest `\i' thingys */
234 err_nosuchkw, /* unresolved cross-reference */
235 err_multiBR, /* multiple \BRs on same keyword */
236 err_nosuchidxtag, /* \IM on unknown index tag (warning) */
237 err_cantopenw, /* can't open output file for write */
238 err_macroexists, /* this macro already exists */
239 err_sectjump, /* jump a heading level, eg \C -> \S */
240 err_winhelp_ctxclash, /* WinHelp context ID hash clash */
241 err_multikw, /* keyword clash in sections */
7136a6c7 242 err_misplacedlcont, /* \lcont not after a list item */
2614b01d 243 err_sectmarkerinblock, /* section marker appeared in block */
d4c7e130 244 err_infodirentry, /* \cfg{info-dir-entry} missing param */
f4551933 245 err_infonodechar, /* colon/comma in node name in info */
d7482997 246 err_whatever /* random error of another type */
247};
248
249/*
250 * malloc.c
251 */
252#ifdef LOGALLOC
253void *smalloc(char *file, int line, int size);
254void *srealloc(char *file, int line, void *p, int size);
255void sfree(char *file, int line, void *p);
256#define smalloc(x) smalloc(__FILE__, __LINE__, x)
257#define srealloc(x, y) srealloc(__FILE__, __LINE__, x, y)
258#define sfree(x) sfree(__FILE__, __LINE__, x)
259#else
260void *smalloc(int size);
261void *srealloc(void *p, int size);
262void sfree(void *p);
263#endif
264void free_word_list(word *w);
265void free_para_list(paragraph *p);
266word *dup_word_list(word *w);
267char *dupstr(char *s);
268
269#define mknew(type) ( (type *) smalloc (sizeof (type)) )
270#define mknewa(type, number) ( (type *) smalloc ((number) * sizeof (type)) )
271#define resize(array, len) ( srealloc ((array), (len) * sizeof (*(array))) )
272#define lenof(array) ( sizeof(array) / sizeof(*(array)) )
273
274/*
275 * ustring.c
276 */
e4ea58f8 277wchar_t *ustrdup(wchar_t const *s);
278char *ustrtoa(wchar_t const *s, char *outbuf, int size, int charset);
279char *ustrtoa_careful(wchar_t const *s, char *outbuf, int size, int charset);
280wchar_t *ustrfroma(char const *s, wchar_t *outbuf, int size, int charset);
281char *utoa_dup(wchar_t const *s, int charset);
282char *utoa_dup_len(wchar_t const *s, int charset, int *len);
283char *utoa_careful_dup(wchar_t const *s, int charset);
284wchar_t *ufroma_dup(char const *s, int charset);
5dd44dce 285int ustrlen(wchar_t const *s);
d7482997 286wchar_t *uadv(wchar_t *s);
5dd44dce 287wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source);
d7482997 288wchar_t utolower(wchar_t);
831da32e 289int uisalpha(wchar_t);
d7482997 290int ustrcmp(wchar_t *lhs, wchar_t *rhs);
291int ustricmp(wchar_t *lhs, wchar_t *rhs);
292int utoi(wchar_t *);
293int utob(wchar_t *);
294int uisdigit(wchar_t);
295wchar_t *ustrlow(wchar_t *s);
296wchar_t *ustrftime(wchar_t *fmt, struct tm *timespec);
297
298/*
299 * help.c
300 */
301void help(void);
302void usage(void);
303void showversion(void);
304
305/*
306 * licence.c
307 */
308void licence(void);
309
310/*
311 * version.c
312 */
313const char *const version;
314
315/*
316 * misc.c
317 */
e4ea58f8 318char *adv(char *s);
319
d7482997 320typedef struct stackTag *stack;
321stack stk_new(void);
322void stk_free(stack);
323void stk_push(stack, void *);
324void *stk_pop(stack);
7136a6c7 325void *stk_top(stack);
d7482997 326
327typedef struct tagRdstring rdstring;
328struct tagRdstring {
329 int pos, size;
330 wchar_t *text;
331};
332typedef struct tagRdstringc rdstringc;
333struct tagRdstringc {
334 int pos, size;
335 char *text;
336};
337extern const rdstring empty_rdstring;
338extern const rdstringc empty_rdstringc;
339void rdadd(rdstring *rs, wchar_t c);
5dd44dce 340void rdadds(rdstring *rs, wchar_t const *p);
d7482997 341wchar_t *rdtrim(rdstring *rs);
342void rdaddc(rdstringc *rs, char c);
5dd44dce 343void rdaddsc(rdstringc *rs, char const *p);
d7482997 344char *rdtrimc(rdstringc *rs);
345
346int compare_wordlists(word *a, word *b);
347
348void mark_attr_ends(paragraph *sourceform);
349
350typedef struct tagWrappedLine wrappedline;
351struct tagWrappedLine {
352 wrappedline *next;
353 word *begin, *end; /* first & last words of line */
354 int nspaces; /* number of whitespaces in line */
355 int shortfall; /* how much shorter than max width */
356};
43341922 357wrappedline *wrap_para(word *, int, int, int (*)(void *, word *), void *, int);
d7482997 358void wrap_free(wrappedline *);
e4ea58f8 359void cmdline_cfg_add(paragraph *cfg, char *string);
360paragraph *cmdline_cfg_new(void);
361paragraph *cmdline_cfg_simple(char *string, ...);
d7482997 362
363/*
364 * input.c
365 */
366paragraph *read_input(input *in, indexdata *idx);
367
368/*
369 * keywords.c
370 */
371struct keywordlist_Tag {
372 int nkeywords;
373 int size;
374 tree234 *keys; /* sorted by `key' field */
375 word **looseends; /* non-keyword list element numbers */
376 int nlooseends;
377 int looseendssize;
378};
379struct keyword_Tag {
380 wchar_t *key; /* the keyword itself */
381 word *text; /* "Chapter 2", "Appendix Q"... */
382 /* (NB: filepos are not set) */
383 paragraph *para; /* the paragraph referenced */
384};
385keyword *kw_lookup(keywordlist *, wchar_t *);
386keywordlist *get_keywords(paragraph *);
387void free_keywords(keywordlist *);
388void subst_keywords(paragraph *, keywordlist *);
389
390/*
391 * index.c
392 */
393
394/*
395 * Data structure to hold both sides of the index.
396 */
397struct indexdata_Tag {
398 tree234 *tags; /* holds type `indextag' */
399 tree234 *entries; /* holds type `indexentry' */
400};
401
402/*
403 * Data structure to hold an index tag (LHS of index).
404 */
405struct indextag_Tag {
406 wchar_t *name;
407 word *implicit_text;
f4551933 408 filepos implicit_fpos;
d7482997 409 word **explicit_texts;
f4551933 410 filepos *explicit_fpos;
d7482997 411 int nexplicit, explicit_size;
412 int nrefs;
413 indexentry **refs; /* array of entries referenced by tag */
414};
415
416/*
417 * Data structure to hold an index entry (RHS of index).
418 */
419struct indexentry_Tag {
420 word *text;
421 void *backend_data; /* private to back end */
f4551933 422 filepos fpos;
d7482997 423};
424
425indexdata *make_index(void);
426void cleanup_index(indexdata *);
427/* index_merge takes responsibility for freeing arg 3 iff implicit; never
428 * takes responsibility for arg 2 */
f4551933 429void index_merge(indexdata *, int is_explicit, wchar_t *, word *, filepos *);
d7482997 430void build_index(indexdata *);
431void index_debug(indexdata *);
432indextag *index_findtag(indexdata *idx, wchar_t *name);
433
434/*
435 * contents.c
436 */
437numberstate *number_init(void);
438void number_cfg(numberstate *, paragraph *);
96f3af16 439word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *);
d7482997 440void number_free(numberstate *);
441
442/*
443 * biblio.c
444 */
445void gen_citations(paragraph *, keywordlist *);
446
447/*
448 * style.c
449 */
450struct userstyle_Tag {
451};
452
453/*
454 * bk_text.c
455 */
43341922 456void text_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 457paragraph *text_config_filename(char *filename);
d7482997 458
459/*
460 * bk_xhtml.c
461 */
43341922 462void xhtml_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 463paragraph *xhtml_config_filename(char *filename);
d7482997 464
465/*
466 * bk_whlp.c
467 */
43341922 468void whlp_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 469paragraph *whlp_config_filename(char *filename);
d7482997 470
7136a6c7 471/*
472 * bk_man.c
473 */
43341922 474void man_backend(paragraph *, keywordlist *, indexdata *, void *);
ba9c1487 475paragraph *man_config_filename(char *filename);
7136a6c7 476
5dd44dce 477/*
478 * bk_info.c
479 */
43341922 480void info_backend(paragraph *, keywordlist *, indexdata *, void *);
5dd44dce 481paragraph *info_config_filename(char *filename);
482
43341922 483/*
484 * bk_paper.c
485 */
486void *paper_pre_backend(paragraph *, keywordlist *, indexdata *);
487
488/*
489 * bk_ps.c
490 */
491void ps_backend(paragraph *, keywordlist *, indexdata *, void *);
492paragraph *ps_config_filename(char *filename);
493
494/*
495 * bk_pdf.c
496 */
497void pdf_backend(paragraph *, keywordlist *, indexdata *, void *);
498paragraph *pdf_config_filename(char *filename);
499
d7482997 500#endif