Infrastructure changes for character set support. ustrtoa,
[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 * FIXME: Charset temporary workarounds
31 */
32 #define CS_FIXME CS_ISO8859_1
33 #define CS_LOCAL CS_ISO8859_1
34
35 /*
36 * Structure tags
37 */
38 typedef struct input_Tag input;
39 typedef struct filepos_Tag filepos;
40 typedef struct paragraph_Tag paragraph;
41 typedef struct word_Tag word;
42 typedef struct keywordlist_Tag keywordlist;
43 typedef struct keyword_Tag keyword;
44 typedef struct userstyle_Tag userstyle;
45 typedef struct numberstate_Tag numberstate;
46 typedef struct indexdata_Tag indexdata;
47 typedef struct indextag_Tag indextag;
48 typedef struct indexentry_Tag indexentry;
49 typedef 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 */
55 struct 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 */
63 typedef struct pushback_Tag {
64 int chr;
65 filepos pos;
66 } pushback;
67 struct 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 */
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[] */
81 char *pushback_chars; /* used to save input-encoding data */
82 };
83
84 /*
85 * Data structure to hold the input form of the source, ie a linked
86 * list of paragraphs
87 */
88 struct paragraph_Tag {
89 paragraph *next;
90 int type;
91 wchar_t *keyword; /* for most special paragraphs */
92 char *origkeyword; /* same again in original charset */
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 };
105 enum {
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,
119 para_DescribedThing,
120 para_Description,
121 para_Code,
122 para_Copyright,
123 para_NoCite,
124 para_Title,
125 para_VersionID,
126 para_Config, /* configuration directive */
127 para_LcontPush, /* begin continuation of list item */
128 para_LcontPop, /* end continuation of list item */
129 para_QuotePush, /* begin block quote */
130 para_QuotePop, /* end block quote */
131 /*
132 * Back ends may define their own paragraph types beyond here,
133 * in case they need to use them internally.
134 */
135 para_NotParaType /* placeholder value */
136 };
137
138 /*
139 * Data structure to hold an individual word
140 */
141 struct 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;
148
149 void *private_data; /* for temp use in backends */
150 };
151 enum {
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) */
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 */
180 };
181 /* aux values for attributed words */
182 enum {
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 */
190 enum {
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 */
209 void fatal(int code, ...) NORETURN;
210 void error(int code, ...);
211 enum {
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 */
231 err_missingrbrace2, /* unclosed braces at end of file */
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 */
242 err_misplacedlcont, /* \lcont not after a list item */
243 err_sectmarkerinblock, /* section marker appeared in block */
244 err_infodirentry, /* \cfg{info-dir-entry} missing param */
245 err_infonodechar, /* colon/comma in node name in info */
246 err_whatever /* random error of another type */
247 };
248
249 /*
250 * malloc.c
251 */
252 #ifdef LOGALLOC
253 void *smalloc(char *file, int line, int size);
254 void *srealloc(char *file, int line, void *p, int size);
255 void 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
260 void *smalloc(int size);
261 void *srealloc(void *p, int size);
262 void sfree(void *p);
263 #endif
264 void free_word_list(word *w);
265 void free_para_list(paragraph *p);
266 word *dup_word_list(word *w);
267 char *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 */
277 wchar_t *ustrdup(wchar_t const *s);
278 char *ustrtoa(wchar_t const *s, char *outbuf, int size, int charset);
279 char *ustrtoa_careful(wchar_t const *s, char *outbuf, int size, int charset);
280 wchar_t *ustrfroma(char const *s, wchar_t *outbuf, int size, int charset);
281 char *utoa_dup(wchar_t const *s, int charset);
282 char *utoa_dup_len(wchar_t const *s, int charset, int *len);
283 char *utoa_careful_dup(wchar_t const *s, int charset);
284 wchar_t *ufroma_dup(char const *s, int charset);
285 int ustrlen(wchar_t const *s);
286 wchar_t *uadv(wchar_t *s);
287 wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source);
288 wchar_t utolower(wchar_t);
289 int uisalpha(wchar_t);
290 int ustrcmp(wchar_t *lhs, wchar_t *rhs);
291 int ustricmp(wchar_t *lhs, wchar_t *rhs);
292 int utoi(wchar_t *);
293 int utob(wchar_t *);
294 int uisdigit(wchar_t);
295 wchar_t *ustrlow(wchar_t *s);
296 wchar_t *ustrftime(wchar_t *fmt, struct tm *timespec);
297
298 /*
299 * help.c
300 */
301 void help(void);
302 void usage(void);
303 void showversion(void);
304
305 /*
306 * licence.c
307 */
308 void licence(void);
309
310 /*
311 * version.c
312 */
313 const char *const version;
314
315 /*
316 * misc.c
317 */
318 char *adv(char *s);
319
320 typedef struct stackTag *stack;
321 stack stk_new(void);
322 void stk_free(stack);
323 void stk_push(stack, void *);
324 void *stk_pop(stack);
325 void *stk_top(stack);
326
327 typedef struct tagRdstring rdstring;
328 struct tagRdstring {
329 int pos, size;
330 wchar_t *text;
331 };
332 typedef struct tagRdstringc rdstringc;
333 struct tagRdstringc {
334 int pos, size;
335 char *text;
336 };
337 extern const rdstring empty_rdstring;
338 extern const rdstringc empty_rdstringc;
339 void rdadd(rdstring *rs, wchar_t c);
340 void rdadds(rdstring *rs, wchar_t const *p);
341 wchar_t *rdtrim(rdstring *rs);
342 void rdaddc(rdstringc *rs, char c);
343 void rdaddsc(rdstringc *rs, char const *p);
344 char *rdtrimc(rdstringc *rs);
345
346 int compare_wordlists(word *a, word *b);
347
348 void mark_attr_ends(paragraph *sourceform);
349
350 typedef struct tagWrappedLine wrappedline;
351 struct 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 };
357 wrappedline *wrap_para(word *, int, int, int (*)(void *, word *), void *, int);
358 void wrap_free(wrappedline *);
359 void cmdline_cfg_add(paragraph *cfg, char *string);
360 paragraph *cmdline_cfg_new(void);
361 paragraph *cmdline_cfg_simple(char *string, ...);
362
363 /*
364 * input.c
365 */
366 paragraph *read_input(input *in, indexdata *idx);
367
368 /*
369 * keywords.c
370 */
371 struct 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 };
379 struct 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 };
385 keyword *kw_lookup(keywordlist *, wchar_t *);
386 keywordlist *get_keywords(paragraph *);
387 void free_keywords(keywordlist *);
388 void subst_keywords(paragraph *, keywordlist *);
389
390 /*
391 * index.c
392 */
393
394 /*
395 * Data structure to hold both sides of the index.
396 */
397 struct 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 */
405 struct indextag_Tag {
406 wchar_t *name;
407 word *implicit_text;
408 filepos implicit_fpos;
409 word **explicit_texts;
410 filepos *explicit_fpos;
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 */
419 struct indexentry_Tag {
420 word *text;
421 void *backend_data; /* private to back end */
422 filepos fpos;
423 };
424
425 indexdata *make_index(void);
426 void cleanup_index(indexdata *);
427 /* index_merge takes responsibility for freeing arg 3 iff implicit; never
428 * takes responsibility for arg 2 */
429 void index_merge(indexdata *, int is_explicit, wchar_t *, word *, filepos *);
430 void build_index(indexdata *);
431 void index_debug(indexdata *);
432 indextag *index_findtag(indexdata *idx, wchar_t *name);
433
434 /*
435 * contents.c
436 */
437 numberstate *number_init(void);
438 void number_cfg(numberstate *, paragraph *);
439 word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *);
440 void number_free(numberstate *);
441
442 /*
443 * biblio.c
444 */
445 void gen_citations(paragraph *, keywordlist *);
446
447 /*
448 * style.c
449 */
450 struct userstyle_Tag {
451 };
452
453 /*
454 * bk_text.c
455 */
456 void text_backend(paragraph *, keywordlist *, indexdata *, void *);
457 paragraph *text_config_filename(char *filename);
458
459 /*
460 * bk_xhtml.c
461 */
462 void xhtml_backend(paragraph *, keywordlist *, indexdata *, void *);
463 paragraph *xhtml_config_filename(char *filename);
464
465 /*
466 * bk_whlp.c
467 */
468 void whlp_backend(paragraph *, keywordlist *, indexdata *, void *);
469 paragraph *whlp_config_filename(char *filename);
470
471 /*
472 * bk_man.c
473 */
474 void man_backend(paragraph *, keywordlist *, indexdata *, void *);
475 paragraph *man_config_filename(char *filename);
476
477 /*
478 * bk_info.c
479 */
480 void info_backend(paragraph *, keywordlist *, indexdata *, void *);
481 paragraph *info_config_filename(char *filename);
482
483 /*
484 * bk_paper.c
485 */
486 void *paper_pre_backend(paragraph *, keywordlist *, indexdata *);
487
488 /*
489 * bk_ps.c
490 */
491 void ps_backend(paragraph *, keywordlist *, indexdata *, void *);
492 paragraph *ps_config_filename(char *filename);
493
494 /*
495 * bk_pdf.c
496 */
497 void pdf_backend(paragraph *, keywordlist *, indexdata *, void *);
498 paragraph *pdf_config_filename(char *filename);
499
500 #endif