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