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