| 1 |
#ifndef HALIBUT_HALIBUT_H |
| 2 |
#define HALIBUT_HALIBUT_H |
| 3 |
|
| 4 |
#if defined(_WIN32) && !defined(_CRT_SECURE_NO_DEPRECATE) |
| 5 |
#define _CRT_SECURE_NO_DEPRECATE 1 |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <wchar.h> |
| 10 |
#include <time.h> |
| 11 |
|
| 12 |
#ifdef __GNUC__ |
| 13 |
#define NORETURN __attribute__((__noreturn__)) |
| 14 |
#else |
| 15 |
#define NORETURN /* nothing */ |
| 16 |
#endif |
| 17 |
|
| 18 |
#ifndef TRUE |
| 19 |
#define TRUE 1 |
| 20 |
#endif |
| 21 |
#ifndef FALSE |
| 22 |
#define FALSE 0 |
| 23 |
#endif |
| 24 |
|
| 25 |
/* For suppressing unused-parameter warnings */ |
| 26 |
#define IGNORE(x) ( (x) = (x) ) |
| 27 |
|
| 28 |
#include "tree234.h" |
| 29 |
|
| 30 |
/* |
| 31 |
* Structure tags |
| 32 |
*/ |
| 33 |
typedef struct input_Tag input; |
| 34 |
typedef struct filepos_Tag filepos; |
| 35 |
typedef struct paragraph_Tag paragraph; |
| 36 |
typedef struct word_Tag word; |
| 37 |
typedef struct keywordlist_Tag keywordlist; |
| 38 |
typedef struct keyword_Tag keyword; |
| 39 |
typedef struct userstyle_Tag userstyle; |
| 40 |
typedef struct numberstate_Tag numberstate; |
| 41 |
typedef struct indexdata_Tag indexdata; |
| 42 |
typedef struct indextag_Tag indextag; |
| 43 |
typedef struct indexentry_Tag indexentry; |
| 44 |
typedef struct macrostack_Tag macrostack; |
| 45 |
|
| 46 |
typedef struct ustr_slist_Tag { |
| 47 |
struct ustr_slist_Tag *next; |
| 48 |
wchar_t string[1]; |
| 49 |
} ustr_slist; |
| 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 |
}; |
| 78 |
|
| 79 |
/* |
| 80 |
* Data structure to hold the input form of the source, ie a linked |
| 81 |
* list of paragraphs |
| 82 |
*/ |
| 83 |
struct paragraph_Tag { |
| 84 |
paragraph *next; |
| 85 |
int type; |
| 86 |
wchar_t *keyword; /* for most special paragraphs */ |
| 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 |
#define initpara(p) ( (p).type = para_NotParaType, (p).keyword = NULL, (p).words = NULL ) |
| 100 |
enum { |
| 101 |
para_IM, /* index merge */ |
| 102 |
para_BR, /* bibliography rewrite */ |
| 103 |
para_Rule, /* random horizontal rule */ |
| 104 |
para_Chapter, |
| 105 |
para_Appendix, |
| 106 |
para_UnnumberedChapter, |
| 107 |
para_Heading, |
| 108 |
para_Subsect, |
| 109 |
para_Normal, |
| 110 |
para_Biblio, /* causes no output unless turned ... */ |
| 111 |
para_BiblioCited, /* ... into this paragraph type */ |
| 112 |
para_Bullet, |
| 113 |
para_NumberedList, |
| 114 |
para_Code, |
| 115 |
para_Copyright, |
| 116 |
para_Preamble, |
| 117 |
para_NoCite, |
| 118 |
para_Title, |
| 119 |
para_VersionID, |
| 120 |
para_Config, /* configuration directive */ |
| 121 |
para_NotParaType /* placeholder value */ |
| 122 |
}; |
| 123 |
|
| 124 |
/* |
| 125 |
* Data structure to hold an individual word |
| 126 |
*/ |
| 127 |
struct word_Tag { |
| 128 |
word *next, *alt; |
| 129 |
int type; |
| 130 |
int aux; |
| 131 |
int breaks; /* can a line break after it? */ |
| 132 |
wchar_t *text; |
| 133 |
filepos fpos; |
| 134 |
}; |
| 135 |
enum { |
| 136 |
/* ORDERING CONSTRAINT: these normal-word types ... */ |
| 137 |
word_Normal, |
| 138 |
word_Emph, |
| 139 |
word_Strong, |
| 140 |
word_Bold, |
| 141 |
word_Code, /* monospaced; `quoted' in text */ |
| 142 |
word_WeakCode, /* monospaced, normal in text */ |
| 143 |
/* ... must be in the same order as these space types ... */ |
| 144 |
word_WhiteSpace, /* text is NULL or ignorable */ |
| 145 |
word_EmphSpace, /* WhiteSpace when emphasised */ |
| 146 |
word_StrongSpace, /* WhiteSpace when strong */ |
| 147 |
word_BoldSpace, |
| 148 |
word_CodeSpace, /* WhiteSpace when code */ |
| 149 |
word_WkCodeSpace, /* WhiteSpace when weak code */ |
| 150 |
/* ... and must be in the same order as these quote types ... */ |
| 151 |
word_Quote, /* text is NULL or ignorable */ |
| 152 |
word_EmphQuote, /* Quote when emphasised */ |
| 153 |
word_StrongQuote, /* Quote when strong */ |
| 154 |
word_BoldQuote, |
| 155 |
word_CodeQuote, /* (can't happen) */ |
| 156 |
word_WkCodeQuote, /* (can't happen) */ |
| 157 |
/* END ORDERING CONSTRAINT */ |
| 158 |
word_internal_endattrs, |
| 159 |
word_UpperXref, /* \K */ |
| 160 |
word_LowerXref, /* \k */ |
| 161 |
word_XrefEnd, /* (invisible; no text) */ |
| 162 |
word_IndexRef, /* (always an invisible one) */ |
| 163 |
word_HyperLink, /* (invisible) */ |
| 164 |
word_HyperEnd, /* (also invisible; no text) */ |
| 165 |
word_LocalHyperLink, /* (invisible) */ |
| 166 |
word_FreeTextXref, /* \R */ |
| 167 |
word_Html, |
| 168 |
word_NotWordType /* placeholder value */ |
| 169 |
}; |
| 170 |
/* aux values for attributed words */ |
| 171 |
enum { |
| 172 |
attr_Only = 0x0000, /* a lone word with the attribute */ |
| 173 |
attr_First = 0x0001, /* the first of a series */ |
| 174 |
attr_Last = 0x0002, /* the last of a series */ |
| 175 |
attr_Always = 0x0003, /* any other part of a series */ |
| 176 |
attr_mask = 0x0003, |
| 177 |
}; |
| 178 |
/* aux values for quote-type words */ |
| 179 |
enum { |
| 180 |
quote_Open = 0x0010, |
| 181 |
quote_Close = 0x0020, |
| 182 |
quote_mask = 0x0030, |
| 183 |
}; |
| 184 |
#define isattr(x) ( ( (x) > word_Normal && (x) < word_WhiteSpace ) || \ |
| 185 |
( (x) > word_WhiteSpace && (x) < word_internal_endattrs ) ) |
| 186 |
#define NATTRS (word_WhiteSpace - word_Normal) |
| 187 |
#define sameattr(x,y) ( (((x)-(y)) % NATTRS) == 0 ) |
| 188 |
#define towordstyle(x) ( word_Normal + ((x) % NATTRS) ) |
| 189 |
#define tospacestyle(x) ( word_WhiteSpace + ((x) % NATTRS) ) |
| 190 |
#define toquotestyle(x) ( word_Quote + ((x) % NATTRS) ) |
| 191 |
#define removeattr(x) ( word_Normal + ((x)/NATTRS * NATTRS) ) |
| 192 |
|
| 193 |
#define attraux(x) ( (x) & attr_mask ) |
| 194 |
#define quoteaux(x) ( (x) & quote_mask ) |
| 195 |
|
| 196 |
/* |
| 197 |
* error.c |
| 198 |
*/ |
| 199 |
void fatal(int code, ...) NORETURN; |
| 200 |
void error(int code, ...); |
| 201 |
#define err_zerochar(fpos) error(err_unicodezero, (fpos)) |
| 202 |
enum { |
| 203 |
err_nomemory, /* out of memory */ |
| 204 |
err_optnoarg, /* option `-%s' requires an argument */ |
| 205 |
err_nosuchopt, /* unrecognised option `-%s' */ |
| 206 |
err_noinput, /* no input files */ |
| 207 |
err_cantopen, /* unable to open input file `%s' */ |
| 208 |
err_nodata, /* no data in input files */ |
| 209 |
err_brokencodepara, /* line in codepara didn't begin `\c' */ |
| 210 |
err_kwunclosed, /* expected `}' after keyword */ |
| 211 |
err_kwillegal, /* paragraph type expects no keyword */ |
| 212 |
err_kwexpected, /* paragraph type expects a keyword */ |
| 213 |
err_kwtoomany, /* paragraph type expects only 1 */ |
| 214 |
err_bodyillegal, /* paragraph type expects only kws! */ |
| 215 |
err_badparatype, /* invalid command at start of para */ |
| 216 |
err_badmidcmd, /* invalid command in mid-para */ |
| 217 |
err_unexbrace, /* unexpected brace */ |
| 218 |
err_explbr, /* expected `{' after command */ |
| 219 |
err_commenteof, /* EOF inside braced comment */ |
| 220 |
err_kwexprbr, /* expected `}' after cross-ref */ |
| 221 |
err_missingrbrace, /* unclosed braces at end of para */ |
| 222 |
err_nestedstyles, /* unable to nest text styles */ |
| 223 |
err_nestedindex, /* unable to nest `\i' thingys */ |
| 224 |
err_nosuchkw, /* unresolved cross-reference */ |
| 225 |
err_multiBR, /* multiple \BRs on same keyword */ |
| 226 |
err_nosuchidxtag, /* \IM on unknown index tag (warning) */ |
| 227 |
err_cantopenw, /* can't open output file for write */ |
| 228 |
err_macroexists, /* this macro already exists */ |
| 229 |
err_sectjump, /* jump a heading level, eg \C -> \S */ |
| 230 |
err_winhelp_ctxclash, /* WinHelp context ID hash clash */ |
| 231 |
err_multikw, /* keyword clash in sections */ |
| 232 |
err_unicodezero, |
| 233 |
err_whatever /* random error of another type */ |
| 234 |
}; |
| 235 |
|
| 236 |
/* |
| 237 |
* malloc.c |
| 238 |
*/ |
| 239 |
#ifdef LOGALLOC |
| 240 |
void *smalloc(char *file, int line, int size); |
| 241 |
void *srealloc(char *file, int line, void *p, int size); |
| 242 |
void sfree(char *file, int line, void *p); |
| 243 |
#define smalloc(x) smalloc(__FILE__, __LINE__, x) |
| 244 |
#define srealloc(x, y) srealloc(__FILE__, __LINE__, x, y) |
| 245 |
#define sfree(x) sfree(__FILE__, __LINE__, x) |
| 246 |
#else |
| 247 |
void *smalloc(int size); |
| 248 |
void *srealloc(void *p, int size); |
| 249 |
void sfree(void *p); |
| 250 |
#endif |
| 251 |
void free_list(void*p); |
| 252 |
void free_word_list(word * w); |
| 253 |
void free_para_list(paragraph * p); |
| 254 |
word *dup_word_list(word * w); |
| 255 |
char *dupstr(char *s); |
| 256 |
|
| 257 |
#define mknew(type) ( (type *) smalloc (sizeof (type)) ) |
| 258 |
#define mknewa(type, number) ( (type *) smalloc ((number) * sizeof (type)) ) |
| 259 |
#define resize(array, len) ( srealloc ((array), (len) * sizeof (*(array))) ) |
| 260 |
#define lenof(array) ( sizeof(array) / sizeof(*(array)) ) |
| 261 |
|
| 262 |
/* |
| 263 |
* ustring.c |
| 264 |
*/ |
| 265 |
#define asciistrdef(s, d) ( (s) ? (s) : (d) ) |
| 266 |
#define ustrdef(s, d) ( (s) ? (s) : (d) ) |
| 267 |
wchar_t *ustrdup(const wchar_t * s); |
| 268 |
wchar_t *ustrreplacedup(wchar_t **dest, const wchar_t *src); |
| 269 |
char *ustrtoa(const wchar_t * s, char *outbuf, int size); |
| 270 |
int ustrlen(const wchar_t * s); |
| 271 |
wchar_t *uadv(const wchar_t * s); |
| 272 |
wchar_t *ustrcpy(wchar_t * dest, const wchar_t * source); |
| 273 |
wchar_t utolower(wchar_t); |
| 274 |
int ustrcmp(const wchar_t * lhs, const wchar_t * rhs); |
| 275 |
int ustricmp(const wchar_t * lhs, const wchar_t * rhs); |
| 276 |
void ultou(unsigned long v, wchar_t *o); |
| 277 |
int utoi(const wchar_t *); |
| 278 |
int utob(const wchar_t *); |
| 279 |
int uisdigit(wchar_t); |
| 280 |
wchar_t *ustrlow(wchar_t * s); |
| 281 |
wchar_t *ustrftime(wchar_t * fmt, struct tm *timespec); |
| 282 |
#define free_ustr_slist free_list |
| 283 |
ustr_slist* ustr_slist_append(ustr_slist**headaddr, const wchar_t*str); |
| 284 |
|
| 285 |
/* |
| 286 |
* help.c |
| 287 |
*/ |
| 288 |
void usage(void); |
| 289 |
void showversion(void); |
| 290 |
|
| 291 |
/* |
| 292 |
* licence.c |
| 293 |
*/ |
| 294 |
void licence(void); |
| 295 |
|
| 296 |
/* |
| 297 |
* version.c |
| 298 |
*/ |
| 299 |
void initversionstring(void); |
| 300 |
extern const char *const version; |
| 301 |
|
| 302 |
/* |
| 303 |
* misc.c |
| 304 |
*/ |
| 305 |
typedef struct stackTag *stack; |
| 306 |
stack stk_new(void); |
| 307 |
void stk_free(stack); |
| 308 |
void stk_push(stack, void *); |
| 309 |
void *stk_pop(stack); |
| 310 |
|
| 311 |
typedef struct tagRdstring rdstring; |
| 312 |
struct tagRdstring { |
| 313 |
int pos, size; |
| 314 |
wchar_t *text; |
| 315 |
}; |
| 316 |
typedef struct tagRdstringc rdstringc; |
| 317 |
struct tagRdstringc { |
| 318 |
int pos, size; |
| 319 |
char *text; |
| 320 |
}; |
| 321 |
extern const rdstring empty_rdstring; |
| 322 |
extern const rdstringc empty_rdstringc; |
| 323 |
void rdadd(rdstring * rs, wchar_t c); |
| 324 |
void rdadds(rdstring * rs, wchar_t * p); |
| 325 |
wchar_t *rdtrim(rdstring * rs); |
| 326 |
void rdaddc(rdstringc * rs, char c); |
| 327 |
void rdaddsc(rdstringc * rs, char *p); |
| 328 |
char *rdtrimc(rdstringc * rs); |
| 329 |
|
| 330 |
int compare_wordlists(word * a, word * b); |
| 331 |
|
| 332 |
void mark_attr_ends(paragraph * sourceform); |
| 333 |
|
| 334 |
typedef struct tagWrappedLine wrappedline; |
| 335 |
struct tagWrappedLine { |
| 336 |
wrappedline *next; |
| 337 |
word *begin, *end; /* first & last words of line */ |
| 338 |
int nspaces; /* number of whitespaces in line */ |
| 339 |
int shortfall; /* how much shorter than max width */ |
| 340 |
}; |
| 341 |
wrappedline *wrap_para(word *, int, int, int (*)(word *)); |
| 342 |
void wrap_free(wrappedline *); |
| 343 |
unsigned long getutcunixtime(); |
| 344 |
time_t current_time(void); /* use in place of time(NULL) */ |
| 345 |
|
| 346 |
/* |
| 347 |
* input.c |
| 348 |
*/ |
| 349 |
paragraph *read_input(input * in, indexdata * idx); |
| 350 |
|
| 351 |
/* |
| 352 |
* keywords.c |
| 353 |
*/ |
| 354 |
struct keywordlist_Tag { |
| 355 |
int nkeywords; |
| 356 |
int size; |
| 357 |
tree234 *keys; /* sorted by `key' field */ |
| 358 |
word **looseends; /* non-keyword list element numbers */ |
| 359 |
int nlooseends; |
| 360 |
int looseendssize; |
| 361 |
}; |
| 362 |
struct keyword_Tag { |
| 363 |
wchar_t *key; /* the keyword itself */ |
| 364 |
word *text; /* "Chapter 2", "Appendix Q"... */ |
| 365 |
/* (NB: filepos are not set) */ |
| 366 |
paragraph *para; /* the paragraph referenced */ |
| 367 |
}; |
| 368 |
keyword *kw_lookup(keywordlist *, wchar_t *); |
| 369 |
keywordlist *get_keywords(paragraph *); |
| 370 |
void free_keywords(keywordlist *); |
| 371 |
void subst_keywords(paragraph *, keywordlist *); |
| 372 |
|
| 373 |
/* |
| 374 |
* index.c |
| 375 |
*/ |
| 376 |
|
| 377 |
/* |
| 378 |
* Data structure to hold both sides of the index. |
| 379 |
*/ |
| 380 |
struct indexdata_Tag { |
| 381 |
tree234 *tags; /* holds type `indextag' */ |
| 382 |
tree234 *entries; /* holds type `indexentry' */ |
| 383 |
}; |
| 384 |
|
| 385 |
/* |
| 386 |
* Data structure to hold an index tag (LHS of index). |
| 387 |
*/ |
| 388 |
struct indextag_Tag { |
| 389 |
wchar_t *name; |
| 390 |
word *implicit_text; |
| 391 |
word **explicit_texts; |
| 392 |
int nexplicit, explicit_size; |
| 393 |
int nrefs; |
| 394 |
indexentry **refs; /* array of entries referenced by tag */ |
| 395 |
}; |
| 396 |
|
| 397 |
/* |
| 398 |
* Data structure to hold an index entry (RHS of index). |
| 399 |
*/ |
| 400 |
struct indexentry_Tag { |
| 401 |
word *text; |
| 402 |
void *backend_data; /* private to back end */ |
| 403 |
}; |
| 404 |
|
| 405 |
indexdata *make_index(void); |
| 406 |
void cleanup_index(indexdata *); |
| 407 |
/* index_merge takes responsibility for freeing arg 3 iff implicit; never |
| 408 |
* takes responsibility for arg 2 */ |
| 409 |
void index_merge(indexdata *, int is_explicit, wchar_t *, word *); |
| 410 |
void build_index(indexdata *); |
| 411 |
void index_debug(indexdata *); |
| 412 |
indextag *index_findtag(indexdata * idx, wchar_t * name); |
| 413 |
|
| 414 |
/* |
| 415 |
* contents.c |
| 416 |
*/ |
| 417 |
numberstate *number_init(void); |
| 418 |
void number_cfg(numberstate *, paragraph *); |
| 419 |
word *number_mktext(numberstate *, paragraph *, wchar_t *, int, int *); |
| 420 |
void number_free(numberstate *); |
| 421 |
|
| 422 |
/* |
| 423 |
* biblio.c |
| 424 |
*/ |
| 425 |
void gen_citations(paragraph *, keywordlist *); |
| 426 |
|
| 427 |
/* |
| 428 |
* style.c |
| 429 |
*/ |
| 430 |
struct userstyle_Tag { |
| 431 |
void* empty; |
| 432 |
}; |
| 433 |
|
| 434 |
/* |
| 435 |
* bk_xhtml.c |
| 436 |
*/ |
| 437 |
void xhtml_backend(paragraph *, keywordlist *, indexdata *); |
| 438 |
|
| 439 |
#endif |