| 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 | char *pushback_chars; /* used to save input-encoding data */ |
| 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * Data structure to hold the input form of the source, ie a linked |
| 80 | * list of paragraphs |
| 81 | */ |
| 82 | struct paragraph_Tag { |
| 83 | paragraph *next; |
| 84 | int type; |
| 85 | wchar_t *keyword; /* for most special paragraphs */ |
| 86 | char *origkeyword; /* same again in original charset */ |
| 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 | }; |
| 99 | enum { |
| 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, |
| 113 | para_DescribedThing, |
| 114 | para_Description, |
| 115 | para_Code, |
| 116 | para_Copyright, |
| 117 | para_NoCite, |
| 118 | para_Title, |
| 119 | para_VersionID, |
| 120 | para_Config, /* configuration directive */ |
| 121 | para_LcontPush, /* begin continuation of list item */ |
| 122 | para_LcontPop, /* end continuation of list item */ |
| 123 | para_QuotePush, /* begin block quote */ |
| 124 | para_QuotePop, /* end block quote */ |
| 125 | /* |
| 126 | * Back ends may define their own paragraph types beyond here, |
| 127 | * in case they need to use them internally. |
| 128 | */ |
| 129 | para_NotParaType /* placeholder value */ |
| 130 | }; |
| 131 | |
| 132 | /* |
| 133 | * Data structure to hold an individual word |
| 134 | */ |
| 135 | struct 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; |
| 142 | |
| 143 | void *private_data; /* for temp use in backends */ |
| 144 | }; |
| 145 | enum { |
| 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) */ |
| 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 */ |
| 174 | }; |
| 175 | /* aux values for attributed words */ |
| 176 | enum { |
| 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 */ |
| 184 | enum { |
| 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 | */ |
| 203 | void fatal(int code, ...) NORETURN; |
| 204 | void error(int code, ...); |
| 205 | enum { |
| 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 */ |
| 224 | err_codequote, /* \q within \c is not supported */ |
| 225 | err_missingrbrace, /* unclosed braces at end of para */ |
| 226 | err_missingrbrace2, /* unclosed braces at end of file */ |
| 227 | err_nestedstyles, /* unable to nest text styles */ |
| 228 | err_nestedindex, /* unable to nest `\i' thingys */ |
| 229 | err_indexcase, /* two \i differing only in case */ |
| 230 | err_nosuchkw, /* unresolved cross-reference */ |
| 231 | err_multiBR, /* multiple \BRs on same keyword */ |
| 232 | err_nosuchidxtag, /* \IM on unknown index tag (warning) */ |
| 233 | err_cantopenw, /* can't open output file for write */ |
| 234 | err_macroexists, /* this macro already exists */ |
| 235 | err_sectjump, /* jump a heading level, eg \C -> \S */ |
| 236 | err_winhelp_ctxclash, /* WinHelp context ID hash clash */ |
| 237 | err_multikw, /* keyword clash in sections */ |
| 238 | err_misplacedlcont, /* \lcont not after a list item */ |
| 239 | err_sectmarkerinblock, /* section marker appeared in block */ |
| 240 | err_infodirentry, /* \cfg{info-dir-entry} missing param */ |
| 241 | err_infonodechar, /* colon/comma in node name in info */ |
| 242 | err_text_codeline, /* \c line too long in text backend */ |
| 243 | err_whatever /* random error of another type */ |
| 244 | }; |
| 245 | |
| 246 | /* |
| 247 | * malloc.c |
| 248 | */ |
| 249 | #ifdef LOGALLOC |
| 250 | void *smalloc(char *file, int line, int size); |
| 251 | void *srealloc(char *file, int line, void *p, int size); |
| 252 | void sfree(char *file, int line, void *p); |
| 253 | #define smalloc(x) smalloc(__FILE__, __LINE__, x) |
| 254 | #define srealloc(x, y) srealloc(__FILE__, __LINE__, x, y) |
| 255 | #define sfree(x) sfree(__FILE__, __LINE__, x) |
| 256 | #else |
| 257 | void *smalloc(int size); |
| 258 | void *srealloc(void *p, int size); |
| 259 | void sfree(void *p); |
| 260 | #endif |
| 261 | void free_word_list(word *w); |
| 262 | void free_para_list(paragraph *p); |
| 263 | word *dup_word_list(word *w); |
| 264 | char *dupstr(char *s); |
| 265 | |
| 266 | #define snew(type) ( (type *) smalloc (sizeof (type)) ) |
| 267 | #define snewn(number, type) ( (type *) smalloc ((number) * sizeof (type)) ) |
| 268 | #define sresize(array, number, type) \ |
| 269 | ( (type *) srealloc ((array), (number) * sizeof (type)) ) |
| 270 | #define lenof(array) ( sizeof(array) / sizeof(*(array)) ) |
| 271 | |
| 272 | /* |
| 273 | * ustring.c |
| 274 | */ |
| 275 | wchar_t *ustrdup(wchar_t const *s); |
| 276 | char *ustrtoa(wchar_t const *s, char *outbuf, int size, int charset); |
| 277 | char *ustrtoa_careful(wchar_t const *s, char *outbuf, int size, int charset); |
| 278 | wchar_t *ustrfroma(char const *s, wchar_t *outbuf, int size, int charset); |
| 279 | char *utoa_dup(wchar_t const *s, int charset); |
| 280 | char *utoa_dup_len(wchar_t const *s, int charset, int *len); |
| 281 | char *utoa_careful_dup(wchar_t const *s, int charset); |
| 282 | wchar_t *ufroma_dup(char const *s, int charset); |
| 283 | char *utoa_locale_dup(wchar_t const *s); |
| 284 | wchar_t *ufroma_locale_dup(char const *s); |
| 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 *ustrncpy(wchar_t *dest, wchar_t const *source, int n); |
| 289 | wchar_t utolower(wchar_t); |
| 290 | int uisalpha(wchar_t); |
| 291 | int ustrcmp(wchar_t *lhs, wchar_t *rhs); |
| 292 | int ustricmp(wchar_t const *lhs, wchar_t const *rhs); |
| 293 | int ustrnicmp(wchar_t const *lhs, wchar_t const *rhs, int maxlen); |
| 294 | int utoi(wchar_t const *); |
| 295 | double utof(wchar_t const *); |
| 296 | int utob(wchar_t const *); |
| 297 | int uisdigit(wchar_t); |
| 298 | wchar_t *ustrlow(wchar_t *s); |
| 299 | wchar_t *ustrftime(const wchar_t *wfmt, const struct tm *timespec); |
| 300 | int cvt_ok(int charset, const wchar_t *s); |
| 301 | |
| 302 | /* |
| 303 | * wcwidth.c |
| 304 | */ |
| 305 | int strwid(char const *s, int charset); |
| 306 | int ustrwid(wchar_t const *s, int charset); |
| 307 | |
| 308 | /* |
| 309 | * help.c |
| 310 | */ |
| 311 | void help(void); |
| 312 | void usage(void); |
| 313 | void showversion(void); |
| 314 | |
| 315 | /* |
| 316 | * licence.c |
| 317 | */ |
| 318 | void licence(void); |
| 319 | |
| 320 | /* |
| 321 | * version.c |
| 322 | */ |
| 323 | const char *const version; |
| 324 | |
| 325 | /* |
| 326 | * misc.c |
| 327 | */ |
| 328 | char *adv(char *s); |
| 329 | |
| 330 | typedef struct stackTag *stack; |
| 331 | stack stk_new(void); |
| 332 | void stk_free(stack); |
| 333 | void stk_push(stack, void *); |
| 334 | void *stk_pop(stack); |
| 335 | void *stk_top(stack); |
| 336 | |
| 337 | typedef struct tagRdstring rdstring; |
| 338 | struct tagRdstring { |
| 339 | int pos, size; |
| 340 | wchar_t *text; |
| 341 | }; |
| 342 | typedef struct tagRdstringc rdstringc; |
| 343 | struct tagRdstringc { |
| 344 | int pos, size; |
| 345 | char *text; |
| 346 | }; |
| 347 | extern const rdstring empty_rdstring; |
| 348 | extern const rdstringc empty_rdstringc; |
| 349 | void rdadd(rdstring *rs, wchar_t c); |
| 350 | void rdadds(rdstring *rs, wchar_t const *p); |
| 351 | wchar_t *rdtrim(rdstring *rs); |
| 352 | void rdaddc(rdstringc *rs, char c); |
| 353 | void rdaddsc(rdstringc *rs, char const *p); |
| 354 | char *rdtrimc(rdstringc *rs); |
| 355 | |
| 356 | int compare_wordlists(word *a, word *b); |
| 357 | |
| 358 | void mark_attr_ends(word *words); |
| 359 | |
| 360 | typedef struct tagWrappedLine wrappedline; |
| 361 | struct tagWrappedLine { |
| 362 | wrappedline *next; |
| 363 | word *begin, *end; /* first & last words of line */ |
| 364 | int nspaces; /* number of whitespaces in line */ |
| 365 | int shortfall; /* how much shorter than max width */ |
| 366 | }; |
| 367 | wrappedline *wrap_para(word *, int, int, int (*)(void *, word *), void *, int); |
| 368 | void wrap_free(wrappedline *); |
| 369 | void cmdline_cfg_add(paragraph *cfg, char *string); |
| 370 | paragraph *cmdline_cfg_new(void); |
| 371 | paragraph *cmdline_cfg_simple(char *string, ...); |
| 372 | |
| 373 | /* |
| 374 | * input.c |
| 375 | */ |
| 376 | paragraph *read_input(input *in, indexdata *idx); |
| 377 | |
| 378 | /* |
| 379 | * keywords.c |
| 380 | */ |
| 381 | struct keywordlist_Tag { |
| 382 | int nkeywords; |
| 383 | int size; |
| 384 | tree234 *keys; /* sorted by `key' field */ |
| 385 | word **looseends; /* non-keyword list element numbers */ |
| 386 | int nlooseends; |
| 387 | int looseendssize; |
| 388 | }; |
| 389 | struct keyword_Tag { |
| 390 | wchar_t *key; /* the keyword itself */ |
| 391 | word *text; /* "Chapter 2", "Appendix Q"... */ |
| 392 | /* (NB: filepos are not set) */ |
| 393 | paragraph *para; /* the paragraph referenced */ |
| 394 | }; |
| 395 | keyword *kw_lookup(keywordlist *, wchar_t *); |
| 396 | keywordlist *get_keywords(paragraph *); |
| 397 | void free_keywords(keywordlist *); |
| 398 | void subst_keywords(paragraph *, keywordlist *); |
| 399 | |
| 400 | /* |
| 401 | * index.c |
| 402 | */ |
| 403 | |
| 404 | /* |
| 405 | * Data structure to hold both sides of the index. |
| 406 | */ |
| 407 | struct indexdata_Tag { |
| 408 | tree234 *tags; /* holds type `indextag' */ |
| 409 | tree234 *entries; /* holds type `indexentry' */ |
| 410 | }; |
| 411 | |
| 412 | /* |
| 413 | * Data structure to hold an index tag (LHS of index). |
| 414 | */ |
| 415 | struct indextag_Tag { |
| 416 | wchar_t *name; |
| 417 | word *implicit_text; |
| 418 | filepos implicit_fpos; |
| 419 | word **explicit_texts; |
| 420 | filepos *explicit_fpos; |
| 421 | int nexplicit, explicit_size; |
| 422 | int nrefs; |
| 423 | indexentry **refs; /* array of entries referenced by tag */ |
| 424 | }; |
| 425 | |
| 426 | /* |
| 427 | * Data structure to hold an index entry (RHS of index). |
| 428 | */ |
| 429 | struct indexentry_Tag { |
| 430 | word *text; |
| 431 | void *backend_data; /* private to back end */ |
| 432 | filepos fpos; |
| 433 | }; |
| 434 | |
| 435 | indexdata *make_index(void); |
| 436 | void cleanup_index(indexdata *); |
| 437 | /* index_merge takes responsibility for freeing arg 3 iff implicit; never |
| 438 | * takes responsibility for arg 2 */ |
| 439 | void index_merge(indexdata *, int is_explicit, wchar_t *, word *, filepos *); |
| 440 | void build_index(indexdata *); |
| 441 | void index_debug(indexdata *); |
| 442 | indextag *index_findtag(indexdata *idx, wchar_t *name); |
| 443 | |
| 444 | /* |
| 445 | * contents.c |
| 446 | */ |
| 447 | numberstate *number_init(void); |
| 448 | void number_cfg(numberstate *, paragraph *); |
| 449 | word *number_mktext(numberstate *, paragraph *, wchar_t *, int *, int *); |
| 450 | void number_free(numberstate *); |
| 451 | |
| 452 | /* |
| 453 | * biblio.c |
| 454 | */ |
| 455 | void gen_citations(paragraph *, keywordlist *); |
| 456 | |
| 457 | /* |
| 458 | * style.c |
| 459 | */ |
| 460 | struct userstyle_Tag { |
| 461 | }; |
| 462 | |
| 463 | /* |
| 464 | * bk_text.c |
| 465 | */ |
| 466 | void text_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 467 | paragraph *text_config_filename(char *filename); |
| 468 | |
| 469 | /* |
| 470 | * bk_html.c |
| 471 | */ |
| 472 | void html_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 473 | paragraph *html_config_filename(char *filename); |
| 474 | |
| 475 | /* |
| 476 | * bk_whlp.c |
| 477 | */ |
| 478 | void whlp_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 479 | paragraph *whlp_config_filename(char *filename); |
| 480 | |
| 481 | /* |
| 482 | * bk_man.c |
| 483 | */ |
| 484 | void man_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 485 | paragraph *man_config_filename(char *filename); |
| 486 | |
| 487 | /* |
| 488 | * bk_info.c |
| 489 | */ |
| 490 | void info_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 491 | paragraph *info_config_filename(char *filename); |
| 492 | |
| 493 | /* |
| 494 | * bk_paper.c |
| 495 | */ |
| 496 | void *paper_pre_backend(paragraph *, keywordlist *, indexdata *); |
| 497 | |
| 498 | /* |
| 499 | * bk_ps.c |
| 500 | */ |
| 501 | void ps_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 502 | paragraph *ps_config_filename(char *filename); |
| 503 | |
| 504 | /* |
| 505 | * bk_pdf.c |
| 506 | */ |
| 507 | void pdf_backend(paragraph *, keywordlist *, indexdata *, void *); |
| 508 | paragraph *pdf_config_filename(char *filename); |
| 509 | |
| 510 | #endif |