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