Colin spotted a sizeof/lenof confusion which we think has been
[sgt/halibut] / winhelp.c
CommitLineData
d7482997 1/*
2 * winhelp.c a module to generate Windows .HLP files
3 *
4 * Documentation of the .HLP file format comes from the excellent
5 * HELPFILE.TXT, published alongside the Help decompiler HELPDECO
6 * by Manfred Winterhoff. This code would not have been possible
7 * without his efforts. Many thanks.
8 */
9
10/*
11 * Potential future features:
12 *
13 * - perhaps LZ77 compression? This appears to cause a phase order
14 * problem: it's hard to do the compression until the data to be
15 * compressed is finalised, and yet you can't finalise the data
16 * to be compressed until you know how much of it is going into
17 * which TOPICBLOCK in order to work out the offsets in the
18 * topic headers - for which you have to have already done the
19 * compression. Perhaps the thing to do is to implement an LZ77
20 * compressor that can guarantee to leave particular bytes in
21 * the stream as literals, and then go back and fix the offsets
22 * up later. Not pleasant.
23 *
24 * - It would be good to find out what relation (if any) the LCID
25 * record in the |SYSTEM section bears to the codepage used in
26 * the actual help text, so as to be able to vary that if the
27 * user needs it. For the moment I suspect we're stuck with
28 * Win1252.
29 *
30 * - tables might be nice.
31 *
32 * Unlikely future features:
33 *
34 * - Phrase compression sounds harder. It's reasonably easy
35 * (though space-costly) to analyse all the text in the file to
36 * determine the one key phrase which would save most space if
37 * replaced by a reference everywhere it appears; but finding
38 * the _1024_ most effective phrases seems much harder since a
39 * naive analysis might find lots of phrases that all overlap
40 * (so you wouldn't get the saving you expected, as after taking
41 * out the first phrase the rest would never crop up). In
42 * addition, MS hold US patent number 4955066 which may cover
43 * phrase compression, so perhaps it's best just to leave it.
44 *
45 * Cleanup work:
46 *
47 * - sort out begin_topic. Ideally we should have a separate
48 * topic_macro function that adds to the existing linkdata for
49 * the topic, because that's more flexible than a variadic
50 * function. This will be fiddly, though: if it's called before
51 * whlp_begin_topic then we must buffer macros, and if it's
52 * called afterwards then we must be able to go back and modify
53 * the linkdata2 of the topic start block. Foo.
54 *
55 * - find out what should happen if a single topiclink crosses
56 * _two_ topicblock boundaries.
57 *
58 * - What is the BlockSize in a topic header (first 4 bytes of
59 * LinkData1 in a type 2 record) supposed to mean? How on earth
60 * is it measured? The help file doesn't become perceptibly
61 * corrupt if I frob it randomly; and on some occasions taking a
62 * bit _out_ of the help file _increases_ that value. I have a
63 * feeling it's completely made up and/or vestigial, so for the
64 * moment I'm just making up a plausible value as I go along.
65 */
66
67#include <stdlib.h>
68#include <stdio.h>
69#include <string.h>
70#include <assert.h>
71#include <time.h>
72#include <stdarg.h>
73
74#include "halibut.h"
75#include "winhelp.h"
76#include "tree234.h"
77
78#ifdef TESTMODE
79/*
80 * This lot is useful for testing. Something like it will also be
81 * needed to use this module standalone.
82 */
83#define smalloc malloc
84#define srealloc realloc
85#define sfree free
f1530049 86#define snew(type) ( (type *) smalloc (sizeof (type)) )
87#define snewn(number, type) ( (type *) smalloc ((number) * sizeof (type)) )
88#define sresize(array, len, type) \
89 ( (type *) srealloc ((array), (len) * sizeof (type)) )
d7482997 90#define lenof(array) ( sizeof(array) / sizeof(*(array)) )
91char *dupstr(char *s) {
f1530049 92 char *r = snewn(1+strlen(s), char); strcpy(r,s); return r;
d7482997 93}
94#endif
95
96#define UNUSEDARG(x) ( (x) = (x) )
97
98#define GET_32BIT_LSB_FIRST(cp) \
99 (((unsigned long)(unsigned char)(cp)[0]) | \
100 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
101 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
102 ((unsigned long)(unsigned char)(cp)[3] << 24))
103
104#define PUT_32BIT_LSB_FIRST(cp, value) do { \
105 (cp)[0] = 0xFF & (value); \
106 (cp)[1] = 0xFF & ((value) >> 8); \
107 (cp)[2] = 0xFF & ((value) >> 16); \
108 (cp)[3] = 0xFF & ((value) >> 24); } while (0)
109
110#define GET_16BIT_LSB_FIRST(cp) \
111 (((unsigned long)(unsigned char)(cp)[0]) | \
112 ((unsigned long)(unsigned char)(cp)[1] << 8))
113
114#define PUT_16BIT_LSB_FIRST(cp, value) do { \
115 (cp)[0] = 0xFF & (value); \
116 (cp)[1] = 0xFF & ((value) >> 8); } while (0)
117
118#define MAX_PAGE_SIZE 0x800 /* max page size in any B-tree */
119#define TOPIC_BLKSIZE 4096 /* implied by version/flags combo */
120
121typedef struct WHLP_TOPIC_tag context;
122
123struct file {
124 char *name; /* file name, will need freeing */
125 unsigned char *data; /* file data, will need freeing */
126 int pos; /* position for adding data */
127 int len; /* # of meaningful bytes in data */
128 int size; /* # of allocated bytes in data */
129 int fileoffset; /* offset in the real .HLP file */
130};
131
132struct indexrec {
133 char *term; /* index term, will need freeing */
134 context *topic; /* topic it links to */
135 int count, offset; /* used when building |KWDATA */
136};
137
138struct topiclink {
139 int topicoffset, topicpos; /* for referencing from elsewhere */
140 int recordtype;
141 int len1, len2;
142 unsigned char *data1, *data2;
143 context *context;
144 struct topiclink *nonscroll, *scroll, *nexttopic;
145 int block_size; /* for the topic header - *boggle* */
146};
147
148struct WHLP_TOPIC_tag {
149 char *name; /* needs freeing */
150 unsigned long hash;
151 struct topiclink *link; /* this provides TOPICOFFSET */
152 context *browse_next, *browse_prev;
153 char *title; /* needs freeing */
154 int index; /* arbitrary number */
155};
156
157struct fontdesc {
158 char *font;
159 int family, rendition, halfpoints;
160 int r, g, b;
161};
162
163struct WHLP_tag {
164 tree234 *files; /* stores `struct file' */
165 tree234 *pre_contexts; /* stores `context' */
166 tree234 *contexts; /* also stores `context' */
167 tree234 *titles; /* _also_ stores `context' */
168 tree234 *text; /* stores `struct topiclink' */
169 tree234 *index; /* stores `struct indexrec' */
170 tree234 *tabstops; /* stores `int' */
171 tree234 *fontnames; /* stores `char *' */
172 tree234 *fontdescs; /* stores `struct fontdesc' */
173 struct file *systemfile; /* the |SYSTEM internal file */
174 context *ptopic; /* primary topic */
175 struct topiclink *prevtopic; /* to link type-2 records together */
176 struct topiclink *link; /* while building a topiclink */
177 unsigned char linkdata1[TOPIC_BLKSIZE]; /* while building a topiclink */
178 unsigned char linkdata2[TOPIC_BLKSIZE]; /* while building a topiclink */
179 int topicblock_remaining; /* while building |TOPIC section */
180 int lasttopiclink; /* while building |TOPIC section */
181 int firsttopiclink_offset; /* while building |TOPIC section */
182 int lasttopicstart; /* while building |TOPIC section */
183 int para_flags;
184 int para_attrs[7];
185 int ncontexts;
186};
187
188/* Functions to return the index and leaf data for B-tree contents. */
189typedef int (*bt_index_fn)(const void *item, unsigned char *outbuf);
190typedef int (*bt_leaf_fn)(const void *item, unsigned char *outbuf);
191
192/* Forward references. */
193static void whlp_para_reset(WHLP h);
194static struct file *whlp_new_file(WHLP h, char *name);
195static void whlp_file_add(struct file *f, const void *data, int len);
196static void whlp_file_add_char(struct file *f, int data);
197static void whlp_file_add_short(struct file *f, int data);
198static void whlp_file_add_long(struct file *f, int data);
199static void whlp_file_fill(struct file *f, int len);
200static void whlp_file_seek(struct file *f, int pos, int whence);
201static int whlp_file_offset(struct file *f);
202
203/* ----------------------------------------------------------------------
204 * Fiddly little functions: B-tree compare, index and leaf functions.
205 */
206
207/* The master index maps file names to help-file offsets. */
208
209static int filecmp(void *av, void *bv)
210{
211 const struct file *a = (const struct file *)av;
212 const struct file *b = (const struct file *)bv;
213 return strcmp(a->name, b->name);
214}
215
216static int fileindex(const void *av, unsigned char *outbuf)
217{
218 const struct file *a = (const struct file *)av;
219 int len = 1+strlen(a->name);
220 memcpy(outbuf, a->name, len);
221 return len;
222}
223
224static int fileleaf(const void *av, unsigned char *outbuf)
225{
226 const struct file *a = (const struct file *)av;
227 int len = 1+strlen(a->name);
228 memcpy(outbuf, a->name, len);
229 PUT_32BIT_LSB_FIRST(outbuf+len, a->fileoffset);
230 return len+4;
231}
232
233/* The |CONTEXT internal file maps help context hashes to TOPICOFFSETs. */
234
235static int ctxcmp(void *av, void *bv)
236{
237 const context *a = (const context *)av;
238 const context *b = (const context *)bv;
239 if ((signed long)a->hash < (signed long)b->hash)
240 return -1;
241 if ((signed long)a->hash > (signed long)b->hash)
242 return +1;
243 return 0;
244}
245
246static int ctxindex(const void *av, unsigned char *outbuf)
247{
248 const context *a = (const context *)av;
249 PUT_32BIT_LSB_FIRST(outbuf, a->hash);
250 return 4;
251}
252
253static int ctxleaf(const void *av, unsigned char *outbuf)
254{
255 const context *a = (const context *)av;
256 PUT_32BIT_LSB_FIRST(outbuf, a->hash);
257 PUT_32BIT_LSB_FIRST(outbuf+4, a->link->topicoffset);
258 return 8;
259}
260
261/* The |TTLBTREE internal file maps TOPICOFFSETs to title strings. */
262
263static int ttlcmp(void *av, void *bv)
264{
265 const context *a = (const context *)av;
266 const context *b = (const context *)bv;
267 if (a->link->topicoffset < b->link->topicoffset)
268 return -1;
269 if (a->link->topicoffset > b->link->topicoffset)
270 return +1;
271 return 0;
272}
273
274static int ttlindex(const void *av, unsigned char *outbuf)
275{
276 const context *a = (const context *)av;
277 PUT_32BIT_LSB_FIRST(outbuf, a->link->topicoffset);
278 return 4;
279}
280
281static int ttlleaf(const void *av, unsigned char *outbuf)
282{
283 const context *a = (const context *)av;
284 int slen;
285 PUT_32BIT_LSB_FIRST(outbuf, a->link->topicoffset);
286 slen = 1+strlen(a->title);
287 memcpy(outbuf+4, a->title, slen);
288 return 4+slen;
289}
290
291/* The |KWBTREE internal file maps index strings to TOPICOFFSETs. */
292
293static int idxcmp(void *av, void *bv)
294{
295 const struct indexrec *a = (const struct indexrec *)av;
296 const struct indexrec *b = (const struct indexrec *)bv;
297 int cmp;
298 if ( (cmp = strcmp(a->term, b->term)) != 0)
299 return cmp;
300 /* Now sort on the index field of the topics. */
301 if (a->topic->index < b->topic->index)
302 return -1;
303 if (a->topic->index > b->topic->index)
304 return +1;
305 return 0;
306}
307
308static int idxindex(const void *av, unsigned char *outbuf)
309{
310 const struct indexrec *a = (const struct indexrec *)av;
311 int len = 1+strlen(a->term);
312 memcpy(outbuf, a->term, len);
313 return len;
314}
315
316static int idxleaf(const void *av, unsigned char *outbuf)
317{
318 const struct indexrec *a = (const struct indexrec *)av;
319 int len = 1+strlen(a->term);
320 memcpy(outbuf, a->term, len);
321 PUT_16BIT_LSB_FIRST(outbuf+len, a->count);
322 PUT_32BIT_LSB_FIRST(outbuf+len+2, a->offset);
323 return len+6;
324}
325
326/*
327 * The internal `tabstops' B-tree stores pointers-to-int. Sorting
328 * is by the low 16 bits of the number (above that is flags).
329 */
330
331static int tabcmp(void *av, void *bv)
332{
333 const int *a = (const int *)av;
334 const int *b = (const int *)bv;
335 if ((*a & 0xFFFF) < (*b & 0xFFFF))
336 return -1;
337 if ((*a & 0xFFFF) > (*b & 0xFFFF))
338 return +1;
339 return 0;
340}
341
342/* The internal `fontnames' B-tree stores strings. */
343static int fontcmp(void *av, void *bv)
344{
345 const char *a = (const char *)av;
346 const char *b = (const char *)bv;
347 return strcmp(a,b);
348}
349
350/* ----------------------------------------------------------------------
351 * Manage help contexts and topics.
352 */
353
354/*
355 * This is the code to compute the hash of a context name. Copied
356 * straight from Winterhoff's documentation.
357 */
358static unsigned long context_hash(char *context)
359{
360 signed char bytemapping[256] =
361 "\x00\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF"
362 "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF"
363 "\xF0\x0B\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\x0C\xFF"
364 "\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
365 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
366 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x0B\x0C\x0D\x0E\x0D"
367 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
368 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F"
369 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F"
370 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F"
371 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F"
372 "\x80\x81\x82\x83\x0B\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
373 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
374 "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
375 "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF"
376 "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF";
377 unsigned long hash;
378
d7482997 379 /*
380 * The hash algorithm starts the hash at 0 and updates it with
381 * each character. Therefore, logically, the hash of an empty
382 * string should be 0 (it starts at 0 and is never updated);
383 * but Winterhoff says it is in fact 1. Shouldn't matter, since
384 * I never plan to use empty context names, but I'll stick the
385 * special case in here anyway.
386 */
387 if (!*context)
388 return 1;
389
390 /*
391 * Now compute the hash in the normal way.
392 */
393 hash = 0;
394 while (*context) {
9ae3b6f0 395 /*
396 * Be careful of overflowing `unsigned long', for maximum
397 * portability.
398 */
399
400 /*
401 * Multiply `hash' by 43.
402 */
403 {
404 unsigned long bottom, top;
405 bottom = (hash & 0xFFFFUL) * 43;
406 top = ((hash >> 16) & 0xFFFFUL) * 43;
407 top += (bottom >> 16);
408 bottom &= 0xFFFFUL;
409 top &= 0xFFFFUL;
410 hash = (top << 16) | bottom;
411 }
412
413 /*
414 * Add the mapping value for this byte to `hash'.
415 */
416 {
417 int val = bytemapping[(unsigned char)*context];
418
419 if (val > 0 && hash > (0xFFFFFFFFUL - val)) {
420 hash -= (0xFFFFFFFFUL - val) + 1;
421 } else if (val < 0 && hash < -val) {
422 hash += (0xFFFFFFFFUL + val) + 1;
423 } else
424 hash += val;
425 }
426
d7482997 427 context++;
428 }
429 return hash;
430}
431
432WHLP_TOPIC whlp_register_topic(WHLP h, char *context_name, char **clash)
433{
f1530049 434 context *ctx = snew(context);
d7482997 435 context *otherctx;
436
437 /*
438 * Index contexts in order of creation, just so there's some
439 * sort of non-arbitrary ordering in the index B-tree. Call me
440 * fussy, but I don't like indexing on pointer values because I
441 * prefer the code to be deterministic when run under different
442 * C libraries.
443 */
444 ctx->index = h->ncontexts++;
445 ctx->browse_prev = ctx->browse_next = NULL;
446
447 if (context_name) {
448 /*
449 * We have a context name, which means we can put this
450 * context straight into the `contexts' tree.
451 */
452 ctx->name = dupstr(context_name);
453 ctx->hash = context_hash(context_name);
454 otherctx = add234(h->contexts, ctx);
455 if (otherctx != ctx) {
456 /*
457 * Hash clash. Destroy the new context and return NULL,
458 * providing the clashing string.
459 */
460 sfree(ctx->name);
461 sfree(ctx);
462 if (clash) *clash = otherctx->name;
463 return NULL;
464 }
465 } else {
466 /*
467 * We have no context name yet. Enter this into the
468 * pre_contexts tree of anonymous topics, which we will go
469 * through later and allocate unique context names and hash
470 * values.
471 */
472 ctx->name = NULL;
473 addpos234(h->pre_contexts, ctx, count234(h->pre_contexts));
474 }
475 return ctx;
476}
477
478void whlp_prepare(WHLP h)
479{
480 /*
481 * We must go through pre_contexts and allocate a context ID to
482 * each anonymous context, making sure it doesn't clash with
483 * the existing contexts.
484 *
485 * Our own context IDs will just be of the form `t00000001',
486 * and we'll increment the number each time and skip over any
487 * IDs that clash with existing context names.
488 */
489 int ctx_num = 0;
490 context *ctx, *otherctx;
491
492 while ( (ctx = index234(h->pre_contexts, 0)) != NULL ) {
493 delpos234(h->pre_contexts, 0);
f1530049 494 ctx->name = snewn(20, char);
d7482997 495 do {
496 sprintf(ctx->name, "t%08d", ctx_num++);
497 ctx->hash = context_hash(ctx->name);
498 otherctx = add234(h->contexts, ctx);
499 } while (otherctx != ctx);
500 }
501
502 /*
503 * Ensure paragraph attributes are clear for the start of text
504 * output.
505 */
506 whlp_para_reset(h);
507}
508
509char *whlp_topic_id(WHLP_TOPIC topic)
510{
511 return topic->name;
512}
513
514void whlp_begin_topic(WHLP h, WHLP_TOPIC topic, char *title, ...)
515{
f1530049 516 struct topiclink *link = snew(struct topiclink);
d7482997 517 int len, slen;
518 char *macro;
519 va_list ap;
520
521 link->nexttopic = NULL;
522 if (h->prevtopic)
523 h->prevtopic->nexttopic = link;
524 h->prevtopic = link;
525
526 link->nonscroll = link->scroll = NULL;
527 link->context = topic;
528 link->block_size = 0;
529
530 link->recordtype = 2; /* topic header */
531 link->len1 = 4*7; /* standard linkdata1 size */
f1530049 532 link->data1 = snewn(link->len1, unsigned char);
d7482997 533
534 slen = strlen(title);
535 assert(slen+1 <= TOPIC_BLKSIZE);
536 memcpy(h->linkdata2, title, slen+1);
537 len = slen+1;
538
539 va_start(ap, title);
540 while ( (macro = va_arg(ap, char *)) != NULL) {
541 slen = strlen(macro);
542 assert(len+slen+1 <= TOPIC_BLKSIZE);
543 memcpy(h->linkdata2+len, macro, slen+1);
544 len += slen+1;
545 }
546 va_end(ap);
547 len--; /* lose the last \0 on the last macro */
548
549 link->len2 = len;
f1530049 550 link->data2 = snewn(link->len2, unsigned char);
d7482997 551 memcpy(link->data2, h->linkdata2, link->len2);
552
553 topic->title = dupstr(title);
554 topic->link = link;
555
556 addpos234(h->text, link, count234(h->text));
557}
558
559void whlp_browse_link(WHLP h, WHLP_TOPIC before, WHLP_TOPIC after)
560{
561 UNUSEDARG(h);
562
563 /*
564 * See if the `before' topic is already linked to another one,
565 * and break the link to that if so. Likewise the `after'
566 * topic.
567 */
568 if (before->browse_next)
569 before->browse_next->browse_prev = NULL;
570 if (after->browse_prev)
571 after->browse_prev->browse_next = NULL;
572 before->browse_next = after;
573 after->browse_prev = before;
574}
575
576/* ----------------------------------------------------------------------
577 * Manage the actual generation of paragraph and text records.
578 */
579
580static void whlp_linkdata(WHLP h, int which, int c)
581{
582 int *len = (which == 1 ? &h->link->len1 : &h->link->len2);
583 char *data = (which == 1 ? h->linkdata1 : h->linkdata2);
584 assert(*len < TOPIC_BLKSIZE);
585 data[(*len)++] = c;
586}
587
588static void whlp_linkdata_short(WHLP h, int which, int data)
589{
590 whlp_linkdata(h, which, data & 0xFF);
591 whlp_linkdata(h, which, (data >> 8) & 0xFF);
592}
593
594static void whlp_linkdata_long(WHLP h, int which, int data)
595{
596 whlp_linkdata(h, which, data & 0xFF);
597 whlp_linkdata(h, which, (data >> 8) & 0xFF);
598 whlp_linkdata(h, which, (data >> 16) & 0xFF);
599 whlp_linkdata(h, which, (data >> 24) & 0xFF);
600}
601
602static void whlp_linkdata_cushort(WHLP h, int which, int data)
603{
604 if (data <= 0x7F) {
605 whlp_linkdata(h, which, data*2);
606 } else {
607 whlp_linkdata(h, which, 1 + (data%128 * 2));
608 whlp_linkdata(h, which, data/128);
609 }
610}
611
612static void whlp_linkdata_csshort(WHLP h, int which, int data)
613{
614 if (data >= -0x40 && data <= 0x3F)
615 whlp_linkdata_cushort(h, which, data+64);
616 else
617 whlp_linkdata_cushort(h, which, data+16384);
618}
619
620static void whlp_linkdata_culong(WHLP h, int which, int data)
621{
622 if (data <= 0x7FFF) {
623 whlp_linkdata_short(h, which, data*2);
624 } else {
625 whlp_linkdata_short(h, which, 1 + (data%32768 * 2));
626 whlp_linkdata_short(h, which, data/32768);
627 }
628}
629
630static void whlp_linkdata_cslong(WHLP h, int which, int data)
631{
632 if (data >= -0x4000 && data <= 0x3FFF)
633 whlp_linkdata_culong(h, which, data+16384);
634 else
635 whlp_linkdata_culong(h, which, data+67108864);
636}
637
638static void whlp_para_reset(WHLP h)
639{
640 int *p;
641
642 h->para_flags = 0;
643
644 while ( (p = index234(h->tabstops, 0)) != NULL) {
645 delpos234(h->tabstops, 0);
646 sfree(p);
647 }
648}
649
650void whlp_para_attr(WHLP h, int attr_id, int attr_param)
651{
652 if (attr_id >= WHLP_PARA_SPACEABOVE &&
653 attr_id <= WHLP_PARA_FIRSTLINEINDENT) {
654 h->para_flags |= 1 << attr_id;
655 h->para_attrs[attr_id] = attr_param;
656 } else if (attr_id == WHLP_PARA_ALIGNMENT) {
657 h->para_flags &= ~0xC00;
658 if (attr_param == WHLP_ALIGN_RIGHT)
659 h->para_flags |= 0x400;
660 else if (attr_param == WHLP_ALIGN_CENTRE)
661 h->para_flags |= 0x800;
662 }
663}
664
665void whlp_set_tabstop(WHLP h, int tabstop, int alignment)
666{
667 int *p;
668
669 if (alignment == WHLP_ALIGN_CENTRE)
670 tabstop |= 0x20000;
671 if (alignment == WHLP_ALIGN_RIGHT)
672 tabstop |= 0x10000;
673
f1530049 674 p = snew(int);
d7482997 675 *p = tabstop;
676 add234(h->tabstops, p);
677 h->para_flags |= 0x0200;
678}
679
680void whlp_begin_para(WHLP h, int para_type)
681{
f1530049 682 struct topiclink *link = snew(struct topiclink);
d7482997 683 int i;
684
685 /*
686 * Clear these to NULL out of paranoia, although in records
687 * that aren't type 2 they should never actually be needed.
688 */
689 link->nexttopic = NULL;
690 link->context = NULL;
691 link->nonscroll = link->scroll = NULL;
692
693 link->recordtype = 32; /* text record */
694
695 h->link = link;
696 link->len1 = link->len2 = 0;
697 link->data1 = h->linkdata1;
698 link->data2 = h->linkdata2;
699
700 if (para_type == WHLP_PARA_NONSCROLL && h->prevtopic &&
701 !h->prevtopic->nonscroll)
702 h->prevtopic->nonscroll = link;
703 if (para_type == WHLP_PARA_SCROLL && h->prevtopic &&
704 !h->prevtopic->scroll)
705 h->prevtopic->scroll = link;
706
707 /*
708 * Now we're ready to start accumulating stuff in linkdata1 and
709 * linkdata2. Next we build up the paragraph info. Note that
710 * the TopicSize (cslong: size of LinkData1 minus the topicsize
711 * and topiclength fields) and TopicLength (cushort: size of
712 * LinkData2) fields are missing; we will put those on when we
713 * end the paragraph.
714 */
715 whlp_linkdata(h, 1, 0); /* must-be-0x00 */
716 whlp_linkdata(h, 1, 0x80); /* must-be-0x80 */
717 whlp_linkdata_short(h, 1, 0); /* Winterhoff says `id'; always 0 AFAICT */
718 whlp_linkdata_short(h, 1, h->para_flags);
719 for (i = WHLP_PARA_SPACEABOVE; i <= WHLP_PARA_FIRSTLINEINDENT; i++) {
720 if (h->para_flags & (1<<i))
721 whlp_linkdata_csshort(h, 1, h->para_attrs[i]);
722 }
723 if (h->para_flags & 0x0200) {
724 int ntabs;
725 /*
726 * Write out tab stop data.
727 */
728 ntabs = count234(h->tabstops);
729 whlp_linkdata_csshort(h, 1, ntabs);
730 for (i = 0; i < ntabs; i++) {
731 int tab, *tabp;
732 tabp = index234(h->tabstops, i);
733 tab = *tabp;
734 if (tab & 0x30000)
735 tab |= 0x4000;
736 whlp_linkdata_cushort(h, 1, tab & 0xFFFF);
737 if (tab & 0x4000)
738 whlp_linkdata_cushort(h, 1, tab >> 16);
739 }
740 }
741
742 /*
743 * Fine. Now we're ready to start writing actual text and
744 * formatting commands.
745 */
746}
747
748void whlp_set_font(WHLP h, int font_id)
749{
750 /*
751 * Write a NUL into linkdata2 to cause the reader to flip over
752 * to linkdata1 to see the formatting command.
753 */
754 whlp_linkdata(h, 2, 0);
755 /*
756 * Now the formatting command is 0x80 followed by a short.
757 */
758 whlp_linkdata(h, 1, 0x80);
759 whlp_linkdata_short(h, 1, font_id);
760}
761
762void whlp_start_hyperlink(WHLP h, WHLP_TOPIC target)
763{
764 /*
765 * Write a NUL into linkdata2.
766 */
767 whlp_linkdata(h, 2, 0);
768 /*
769 * Now the formatting command is 0xE3 followed by the context
770 * hash.
771 */
772 whlp_linkdata(h, 1, 0xE3);
773 whlp_linkdata_long(h, 1, target->hash);
774}
775
776void whlp_end_hyperlink(WHLP h)
777{
778 /*
779 * Write a NUL into linkdata2.
780 */
781 whlp_linkdata(h, 2, 0);
782 /*
783 * Now the formatting command is 0x89.
784 */
785 whlp_linkdata(h, 1, 0x89);
786}
787
788void whlp_tab(WHLP h)
789{
790 /*
791 * Write a NUL into linkdata2.
792 */
793 whlp_linkdata(h, 2, 0);
794 /*
795 * Now the formatting command is 0x83.
796 */
797 whlp_linkdata(h, 1, 0x83);
798}
799
800void whlp_text(WHLP h, char *text)
801{
802 while (*text) {
803 whlp_linkdata(h, 2, *text++);
804 }
805}
806
807void whlp_end_para(WHLP h)
808{
809 int data1cut;
810
811 /*
812 * Round off the paragraph with 0x82 and 0xFF formatting
813 * commands. Each requires a NUL in linkdata2.
814 */
815 whlp_linkdata(h, 2, 0);
816 whlp_linkdata(h, 1, 0x82);
817 whlp_linkdata(h, 2, 0);
818 whlp_linkdata(h, 1, 0xFF);
819
820 /*
821 * Now finish up: create the header of linkdata1 (TopicLength
822 * and TopicSize fields), allocate the real linkdata1 and
823 * linkdata2 fields, and copy them out of the buffers in h.
824 * Then insert the finished topiclink into the `text' tree, and
825 * clean up.
826 */
827 data1cut = h->link->len1;
828 whlp_linkdata_cslong(h, 1, data1cut);
829 whlp_linkdata_cushort(h, 1, h->link->len2);
830
f1530049 831 h->link->data1 = snewn(h->link->len1, unsigned char);
d7482997 832 memcpy(h->link->data1, h->linkdata1 + data1cut, h->link->len1 - data1cut);
833 memcpy(h->link->data1 + h->link->len1 - data1cut, h->linkdata1, data1cut);
f1530049 834 h->link->data2 = snewn(h->link->len2, unsigned char);
d7482997 835 memcpy(h->link->data2, h->linkdata2, h->link->len2);
836
837 addpos234(h->text, h->link, count234(h->text));
838
839 /* Hack: accumulate the `blocksize' parameter in the topic header. */
840 if (h->prevtopic)
841 h->prevtopic->block_size += 21 + h->link->len1 + h->link->len2;
842
843 h->link = NULL; /* this is now in the tree */
844
845 whlp_para_reset(h);
846}
847
848/* ----------------------------------------------------------------------
849 * Manage the layout and generation of the |TOPIC section.
850 */
851
852static void whlp_topicsect_write(WHLP h, struct file *f, void *data, int len,
853 int can_break)
854{
855 unsigned char *p = (unsigned char *)data;
856
857 if (h->topicblock_remaining <= 0 ||
858 h->topicblock_remaining < can_break) {
859 /*
860 * Start a new block.
861 */
862 if (h->topicblock_remaining > 0)
863 whlp_file_fill(f, h->topicblock_remaining);
864 whlp_file_add_long(f, h->lasttopiclink);
865 h->firsttopiclink_offset = whlp_file_offset(f);
866 whlp_file_add_long(f, -1L); /* this will be filled in later */
867 whlp_file_add_long(f, h->lasttopicstart);
868 h->topicblock_remaining = TOPIC_BLKSIZE - 12;
869 }
870 while (len > 0) {
871 int thislen = (h->topicblock_remaining < len ?
872 h->topicblock_remaining : len);
873 whlp_file_add(f, p, thislen);
874 p += thislen;
875 len -= thislen;
876 h->topicblock_remaining -= thislen;
877 if (len > 0 && h->topicblock_remaining <= 0) {
878 /*
879 * Start a new block.
880 */
881 whlp_file_add_long(f, h->lasttopiclink);
882 h->firsttopiclink_offset = whlp_file_offset(f);
883 whlp_file_add_long(f, -1L); /* this will be filled in later */
884 whlp_file_add_long(f, h->lasttopicstart);
885 h->topicblock_remaining = TOPIC_BLKSIZE - 12;
886 }
887 }
888}
889
890static void whlp_topic_layout(WHLP h)
891{
892 int block, offset, pos;
893 int i, nlinks, size;
894 int topicnum;
895 struct topiclink *link;
896 struct file *f;
897
898 /*
899 * Create a final TOPICLINK containing no usable data.
900 */
f1530049 901 link = snew(struct topiclink);
d7482997 902 link->nexttopic = NULL;
903 if (h->prevtopic)
904 h->prevtopic->nexttopic = link;
905 h->prevtopic = link;
f1530049 906 link->data1 = snewn(0x1c, unsigned char);
d7482997 907 link->block_size = 0;
908 link->data2 = NULL;
909 link->len1 = 0x1c;
910 link->len2 = 0;
911 link->nexttopic = NULL;
912 link->recordtype = 2;
913 link->nonscroll = link->scroll = NULL;
914 link->context = NULL;
915 addpos234(h->text, link, count234(h->text));
916
917 /*
918 * Each TOPICBLOCK has space for TOPIC_BLKSIZE-12 bytes. The
919 * size of each TOPICLINK is 21 bytes plus the combined lengths
920 * of LinkData1 and LinkData2. So we can now go through and
921 * break up the TOPICLINKs into TOPICBLOCKs, and also set up
922 * the TOPICOFFSET and TOPICPOS of each one while we do so.
923 */
924
925 block = 0;
926 offset = 0;
927 pos = 12;
928 nlinks = count234(h->text);
929 for (i = 0; i < nlinks; i++) {
930 link = index234(h->text, i);
931 size = 21 + link->len1 + link->len2;
932 /*
933 * We can't split within the topicblock header or within
934 * linkdata1. So if the split would fall in that area,
935 * start a new block _now_.
936 */
937 if (TOPIC_BLKSIZE - pos < 21 + link->len1) {
938 block++;
939 offset = 0;
940 pos = 12;
941 }
942 link->topicoffset = block * 0x8000 + offset;
943 link->topicpos = block * 0x4000 + pos;
944 pos += size;
945 if (link->recordtype != 2) /* TOPICOFFSET doesn't count titles */
946 offset += link->len2;
947 while (pos > TOPIC_BLKSIZE) {
948 block++;
949 offset = 0;
950 pos -= TOPIC_BLKSIZE - 12;
951 }
952 }
953
954 /*
955 * Now we have laid out the TOPICLINKs into blocks, and
956 * determined the final TOPICOFFSET and TOPICPOS of each one.
957 * So now we can go through and write the headers of the type-2
958 * records.
959 */
960
961 topicnum = 0;
962 for (i = 0; i < nlinks; i++) {
963 link = index234(h->text, i);
964 if (link->recordtype != 2)
965 continue;
966
967 PUT_32BIT_LSB_FIRST(link->data1 + 0, link->block_size);
968 if (link->context && link->context->browse_prev)
969 PUT_32BIT_LSB_FIRST(link->data1 + 4,
970 link->context->browse_prev->link->topicoffset);
971 else
972 PUT_32BIT_LSB_FIRST(link->data1 + 4, 0xFFFFFFFFL);
973 if (link->context && link->context->browse_next)
974 PUT_32BIT_LSB_FIRST(link->data1 + 8,
975 link->context->browse_next->link->topicoffset);
976 else
977 PUT_32BIT_LSB_FIRST(link->data1 + 8, 0xFFFFFFFFL);
978 PUT_32BIT_LSB_FIRST(link->data1 + 12, topicnum);
979 topicnum++;
980 if (link->nonscroll)
981 PUT_32BIT_LSB_FIRST(link->data1 + 16, link->nonscroll->topicpos);
982 else
983 PUT_32BIT_LSB_FIRST(link->data1 + 16, 0xFFFFFFFFL);
984 if (link->scroll)
985 PUT_32BIT_LSB_FIRST(link->data1 + 20, link->scroll->topicpos);
986 else
987 PUT_32BIT_LSB_FIRST(link->data1 + 20, 0xFFFFFFFFL);
988 if (link->nexttopic)
989 PUT_32BIT_LSB_FIRST(link->data1 + 24, link->nexttopic->topicpos);
990 else
991 PUT_32BIT_LSB_FIRST(link->data1 + 24, 0xFFFFFFFFL);
992 }
993
994 /*
995 * Having done all _that_, we're now finally ready to go
996 * through and create the |TOPIC section in its final form.
997 */
998
999 h->lasttopiclink = -1L;
1000 h->lasttopicstart = 0L;
1001 f = whlp_new_file(h, "|TOPIC");
1002 h->topicblock_remaining = -1;
1003 whlp_topicsect_write(h, f, NULL, 0, 0); /* start the first block */
1004 for (i = 0; i < nlinks; i++) {
1005 unsigned char header[21];
1006 struct topiclink *otherlink;
1007
1008 link = index234(h->text, i);
1009
1010 /*
1011 * Create and output the TOPICLINK header.
1012 */
1013 PUT_32BIT_LSB_FIRST(header + 0, 21 + link->len1 + link->len2);
1014 PUT_32BIT_LSB_FIRST(header + 4, link->len2);
1015 if (i == 0) {
1016 PUT_32BIT_LSB_FIRST(header + 8, 0xFFFFFFFFL);
1017 } else {
1018 otherlink = index234(h->text, i-1);
1019 PUT_32BIT_LSB_FIRST(header + 8, otherlink->topicpos);
1020 }
1021 if (i+1 >= nlinks) {
1022 PUT_32BIT_LSB_FIRST(header + 12, 0xFFFFFFFFL);
1023 } else {
1024 otherlink = index234(h->text, i+1);
1025 PUT_32BIT_LSB_FIRST(header + 12, otherlink->topicpos);
1026 }
1027 PUT_32BIT_LSB_FIRST(header + 16, 21 + link->len1);
1028 header[20] = link->recordtype;
1029 whlp_topicsect_write(h, f, header, 21, 21 + link->len1);
1030
1031 /*
1032 * Fill in the `first topiclink' pointer in the block
1033 * header if appropriate. (We do this _after_ outputting
1034 * the header because then we can be sure we'll be in the
1035 * same block as we think we are.)
1036 */
1037 if (h->firsttopiclink_offset > 0) {
1038 whlp_file_seek(f, h->firsttopiclink_offset, 0);
1039 whlp_file_add_long(f, link->topicpos);
1040 h->firsttopiclink_offset = 0;
1041 whlp_file_seek(f, 0, 2);
1042 }
1043
1044 /*
1045 * Update the `last topiclink', and possibly `last
1046 * topicstart', pointers.
1047 */
1048 h->lasttopiclink = link->topicpos;
1049 if (link->recordtype == 2)
1050 h->lasttopicstart = link->topicpos;
1051
1052
1053 /*
1054 * Output LinkData1 and LinkData2.
1055 */
1056 whlp_topicsect_write(h, f, link->data1, link->len1, link->len1);
1057 whlp_topicsect_write(h, f, link->data2, link->len2, 0);
1058
1059 /*
1060 * Output the block header.
1061 */
1062
1063 link = index234(h->text, i);
1064
1065 }
1066}
1067
1068/* ----------------------------------------------------------------------
1069 * Manage the index sections (|KWDATA, |KWMAP, |KWBTREE).
1070 */
1071
1072void whlp_index_term(WHLP h, char *index, WHLP_TOPIC topic)
1073{
f1530049 1074 struct indexrec *idx = snew(struct indexrec);
d7482997 1075
1076 idx->term = dupstr(index);
1077 idx->topic = topic;
1078 /*
1079 * If this reference is already in the tree, just silently drop
1080 * the duplicate.
1081 */
1082 if (add234(h->index, idx) != idx) {
1083 sfree(idx->term);
1084 sfree(idx);
1085 }
1086}
1087
1088static void whlp_build_kwdata(WHLP h)
1089{
1090 struct file *f;
1091 int i;
1092 struct indexrec *first, *next;
1093
1094 f = whlp_new_file(h, "|KWDATA");
1095
1096 /*
1097 * Go through the index B-tree, condensing all sequences of
1098 * records with the same term into a single one with a valid
1099 * (count,offset) pair, and building up the KWDATA section.
1100 */
1101 i = 0;
1102 while ( (first = index234(h->index, i)) != NULL) {
1103 first->count = 1;
1104 first->offset = whlp_file_offset(f);
1105 whlp_file_add_long(f, first->topic->link->topicoffset);
1106 i++;
1107 while ( (next = index234(h->index, i)) != NULL &&
1108 !strcmp(first->term, next->term)) {
1109 /*
1110 * The next index record has the same term. Fold it
1111 * into this one and remove from the tree.
1112 */
1113 whlp_file_add_long(f, next->topic->link->topicoffset);
1114 first->count++;
1115 delpos234(h->index, i);
1116 sfree(next->term);
1117 sfree(next);
1118 }
1119 }
1120
1121 /*
1122 * Now we should have `index' in a form that's ready to
1123 * construct |KWBTREE. So we can return.
1124 */
1125}
1126
1127/* ----------------------------------------------------------------------
1128 * Standard chunks of data for the |SYSTEM and |FONT sections.
1129 */
1130
1131static void whlp_system_record(struct file *f, int id,
1132 const void *data, int length)
1133{
1134 whlp_file_add_short(f, id);
1135 whlp_file_add_short(f, length);
1136 whlp_file_add(f, data, length);
1137}
1138
1139static void whlp_standard_systemsection(struct file *f)
1140{
1141 const char lcid[] = { 0, 0, 0, 0, 0, 0, 0, 0, 9, 4 };
1142 const char charset[] = { 0, 0, 0, 2, 0 };
1143
1144 whlp_file_add_short(f, 0x36C); /* magic number */
1145 whlp_file_add_short(f, 33); /* minor version: HCW 4.00 Win95+ */
1146 whlp_file_add_short(f, 1); /* major version */
1147 whlp_file_add_long(f, time(NULL)); /* generation date */
1148 whlp_file_add_short(f, 0); /* flags=0 means no compression */
1149
1150 /*
1151 * Add some magic locale identifier information. (We ought to
1152 * find out something about what all this means; see the TODO
1153 * list at the top of the file.)
1154 */
1155 whlp_system_record(f, 9, lcid, sizeof(lcid));
1156 whlp_system_record(f, 11, charset, sizeof(charset));
1157}
1158
1159void whlp_title(WHLP h, char *title)
1160{
1161 whlp_system_record(h->systemfile, 1, title, 1+strlen(title));
1162}
1163
1164void whlp_copyright(WHLP h, char *copyright)
1165{
1166 whlp_system_record(h->systemfile, 2, copyright, 1+strlen(copyright));
1167}
1168
1169void whlp_start_macro(WHLP h, char *macro)
1170{
1171 whlp_system_record(h->systemfile, 4, macro, 1+strlen(macro));
1172}
1173
1174void whlp_primary_topic(WHLP h, WHLP_TOPIC t)
1175{
1176 h->ptopic = t;
1177}
1178
1179static void whlp_do_primary_topic(WHLP h)
1180{
1181 unsigned char firsttopic[4];
1182 PUT_32BIT_LSB_FIRST(firsttopic, h->ptopic->link->topicoffset);
1183 whlp_system_record(h->systemfile, 3, firsttopic, sizeof(firsttopic));
1184}
1185
1186int whlp_create_font(WHLP h, char *font, int family, int halfpoints,
1187 int rendition, int r, int g, int b)
1188{
1189 char *fontname = dupstr(font);
1190 struct fontdesc *fontdesc;
1191 int index;
1192
1193 font = add234(h->fontnames, fontname);
1194 if (font != fontname) {
1195 /* The font name was already present. Free the new copy. */
1196 sfree(fontname);
1197 }
1198
f1530049 1199 fontdesc = snew(struct fontdesc);
d7482997 1200 fontdesc->font = font;
1201 fontdesc->family = family;
1202 fontdesc->halfpoints = halfpoints;
1203 fontdesc->rendition = rendition;
1204 fontdesc->r = r;
1205 fontdesc->g = g;
1206 fontdesc->b = b;
1207
1208 index = count234(h->fontdescs);
1209 addpos234(h->fontdescs, fontdesc, index);
1210 return index;
1211}
1212
1213static void whlp_make_fontsection(WHLP h, struct file *f)
1214{
1215 int i;
1216 char *fontname;
1217 struct fontdesc *fontdesc;
1218
1219 /*
1220 * Header block: number of font names, number of font
1221 * descriptors, offset to font names, and offset to font
1222 * descriptors.
1223 */
1224 whlp_file_add_short(f, count234(h->fontnames));
1225 whlp_file_add_short(f, count234(h->fontdescs));
1226 whlp_file_add_short(f, 8);
1227 whlp_file_add_short(f, 8 + 32 * count234(h->fontnames));
1228
1229 /*
1230 * Font names.
1231 */
1232 for (i = 0; (fontname = index234(h->fontnames, i)) != NULL; i++) {
1233 char data[32];
1234 memset(data, i, sizeof(data));
1235 strncpy(data, fontname, sizeof(data));
1236 whlp_file_add(f, data, sizeof(data));
1237 }
1238
1239 /*
1240 * Font descriptors.
1241 */
1242 for (i = 0; (fontdesc = index234(h->fontdescs, i)) != NULL; i++) {
1243 int fontpos;
1244 void *ret;
1245
1246 ret = findpos234(h->fontnames, fontdesc->font, NULL, &fontpos);
1247 assert(ret != NULL);
1248
1249 whlp_file_add_char(f, fontdesc->rendition);
1250 whlp_file_add_char(f, fontdesc->halfpoints);
1251 whlp_file_add_char(f, fontdesc->family);
1252 whlp_file_add_short(f, fontpos);
1253 /* Foreground RGB */
1254 whlp_file_add_char(f, fontdesc->r);
1255 whlp_file_add_char(f, fontdesc->g);
1256 whlp_file_add_char(f, fontdesc->b);
1257 /* Background RGB is apparently unused and always set to zero */
1258 whlp_file_add_char(f, 0);
1259 whlp_file_add_char(f, 0);
1260 whlp_file_add_char(f, 0);
1261 }
1262
1263}
1264
1265/* ----------------------------------------------------------------------
1266 * Routines to manage a B-tree type file.
1267 */
1268
1269static void whlp_make_btree(struct file *f, int flags, int pagesize,
1270 char *dataformat, tree234 *tree,
1271 struct file *map,
1272 bt_index_fn indexfn, bt_leaf_fn leaffn)
1273{
1274 void **page_elements = NULL;
1275 int npages = 0, pagessize = 0;
1276 int npages_this_level, nentries, nlevels;
1277 int total_leaf_entries;
1278 char btdata[MAX_PAGE_SIZE];
1279 int btlen;
1280 int page_start, fixups_offset, unused_bytes;
1281 void *element;
1282 int index;
1283
1284 assert(pagesize <= MAX_PAGE_SIZE);
1285
1286 /*
1287 * Start with the B-tree header. We'll have to come back and
1288 * fill in a few bits later.
1289 */
1290 whlp_file_add_short(f, 0x293B); /* magic number */
1291 whlp_file_add_short(f, flags);
1292 whlp_file_add_short(f, pagesize);
1293 {
1294 char data[16];
1295 memset(data, 0, sizeof(data));
1296 assert(strlen(dataformat) <= sizeof(data));
1297 memcpy(data, dataformat, strlen(dataformat));
1298 whlp_file_add(f, data, sizeof(data));
1299 }
1300 whlp_file_add_short(f, 0); /* must-be-zero */
1301 fixups_offset = whlp_file_offset(f);
1302 whlp_file_add_short(f, 0); /* page splits; fix up later */
1303 whlp_file_add_short(f, 0); /* root page index; fix up later */
1304 whlp_file_add_short(f, -1); /* must-be-minus-one */
1305 whlp_file_add_short(f, 0); /* total number of pages; fix later */
1306 whlp_file_add_short(f, 0); /* number of levels; fix later */
1307 whlp_file_add_long(f, count234(tree));/* total B-tree entries */
1308
1309 /*
1310 * If we have a map section, leave space at the start for its
1311 * element count.
1312 */
1313 if (map) {
1314 whlp_file_add_short(map, 0);
1315 }
1316
1317 /*
1318 * Now create the leaf pages.
1319 */
1320 index = 0;
1321
1322 npages_this_level = 0;
1323 total_leaf_entries = 0;
1324
1325 element = index234(tree, index);
1326 while (element) {
1327 /*
1328 * Make a new leaf page.
1329 */
1330 npages_this_level++;
1331 if (npages >= pagessize) {
1332 pagessize = npages + 32;
f1530049 1333 page_elements = sresize(page_elements, pagessize, void *);
d7482997 1334 }
1335 page_elements[npages++] = element;
1336
1337 /*
1338 * Leave space in the leaf page for the header. We'll
1339 * come back and add it later.
1340 */
1341 page_start = whlp_file_offset(f);
1342 whlp_file_add(f, "12345678", 8);
1343 unused_bytes = pagesize - 8;
1344 nentries = 0;
1345
1346 /*
1347 * Now add leaf entries until we run out of room, or out of
1348 * elements.
1349 */
1350 while (element) {
1351 btlen = leaffn(element, btdata);
1352 if (btlen > unused_bytes)
1353 break;
1354 whlp_file_add(f, btdata, btlen);
1355 unused_bytes -= btlen;
1356 nentries++;
1357 index++;
1358 element = index234(tree, index);
1359 }
1360
1361 /*
1362 * Now add the unused bytes, and then go back and put
1363 * in the header.
1364 */
1365 whlp_file_fill(f, unused_bytes);
1366 whlp_file_seek(f, page_start, 0);
1367 whlp_file_add_short(f, unused_bytes);
1368 whlp_file_add_short(f, nentries);
1369 /* Previous-page indicator will automatically go to -1 when
1370 * absent. */
1371 whlp_file_add_short(f, npages-2);
1372 /* Next-page indicator must be -1 if we're at the end. */
1373 if (!element)
1374 whlp_file_add_short(f, -1);
1375 else
1376 whlp_file_add_short(f, npages);
1377 whlp_file_seek(f, 0, 2);
1378
1379 /*
1380 * If we have a map section, add a map entry.
1381 */
1382 if (map) {
1383 whlp_file_add_long(map, total_leaf_entries);
1384 whlp_file_add_short(map, npages_this_level-1);
1385 }
1386 total_leaf_entries += nentries;
1387 }
1388
1389 /*
1390 * If we have a map section, write the total number of map
1391 * entries into it.
1392 */
1393 if (map) {
1394 whlp_file_seek(map, 0, 0);
1395 whlp_file_add_short(map, npages_this_level);
1396 whlp_file_seek(map, 0, 2);
1397 }
1398
1399 /*
1400 * Now create further levels until we're down to one page.
1401 */
1402 nlevels = 1;
1403 while (npages_this_level > 1) {
1404 int first = npages - npages_this_level;
1405 int last = npages - 1;
1406 int current;
1407
1408 nlevels++;
1409 npages_this_level = 0;
1410
1411 current = first;
1412 while (current <= last) {
1413 /*
1414 * Make a new index page.
1415 */
1416 npages_this_level++;
1417 if (npages >= pagessize) {
1418 pagessize = npages + 32;
f1530049 1419 page_elements = sresize(page_elements, pagessize, void *);
d7482997 1420 }
1421 page_elements[npages++] = page_elements[current];
1422
1423 /*
1424 * Leave space for some of the header, but we can put
1425 * in the PreviousPage link already.
1426 */
1427 page_start = whlp_file_offset(f);
1428 whlp_file_add(f, "1234", 4);
1429 whlp_file_add_short(f, current);
1430 unused_bytes = pagesize - 6;
1431
1432 /*
1433 * Now add index entries until we run out of either
1434 * space or pages.
1435 */
1436 current++;
1437 nentries = 0;
1438 while (current <= last) {
1439 btlen = indexfn(page_elements[current], btdata);
1440 if (btlen + 2 > unused_bytes)
1441 break;
1442 whlp_file_add(f, btdata, btlen);
1443 whlp_file_add_short(f, current);
1444 unused_bytes -= btlen+2;
1445 nentries++;
1446 current++;
1447 }
1448
1449 /*
1450 * Now add the unused bytes, and then go back and put
1451 * in the header.
1452 */
1453 whlp_file_fill(f, unused_bytes);
1454 whlp_file_seek(f, page_start, 0);
1455 whlp_file_add_short(f, unused_bytes);
1456 whlp_file_add_short(f, nentries);
1457 whlp_file_seek(f, 0, 2);
1458 }
1459 }
1460
1461 /*
1462 * Now we have all our pages ready, and we know where our root
1463 * page is. Fix up the main B-tree header.
1464 */
1465 whlp_file_seek(f, fixups_offset, 0);
1466 /* Creation of every page requires a split unless it's the first in
1467 * a new level. Hence, page splits equals pages minus levels. */
1468 whlp_file_add_short(f, npages - nlevels);
1469 whlp_file_add_short(f, npages-1); /* root page index */
1470 whlp_file_add_short(f, -1); /* must-be-minus-one */
1471 whlp_file_add_short(f, npages); /* total number of pages */
1472 whlp_file_add_short(f, nlevels); /* number of levels */
1473
1474 /* Just for tidiness, seek to the end of the file :-) */
1475 whlp_file_seek(f, 0, 2);
1476
1477 /* Clean up. */
1478 sfree(page_elements);
1479}
1480
1481
1482/* ----------------------------------------------------------------------
1483 * Routines to manage the `internal file' structure.
1484 */
1485
1486static struct file *whlp_new_file(WHLP h, char *name)
1487{
1488 struct file *f;
f1530049 1489 f = snew(struct file);
d7482997 1490 f->data = NULL;
1491 f->pos = f->len = f->size = 0;
1492 if (name) {
1493 f->name = dupstr(name);
1494 add234(h->files, f);
1495 } else {
1496 f->name = NULL;
1497 }
1498 return f;
1499}
1500
1501static void whlp_free_file(struct file *f)
1502{
1503 sfree(f->data);
1504 sfree(f->name); /* may be NULL */
1505 sfree(f);
1506}
1507
1508static void whlp_file_add(struct file *f, const void *data, int len)
1509{
1510 if (f->pos + len > f->size) {
1511 f->size = f->pos + len + 1024;
f1530049 1512 f->data = sresize(f->data, f->size, unsigned char);
d7482997 1513 }
1514 memcpy(f->data + f->pos, data, len);
1515 f->pos += len;
1516 if (f->len < f->pos)
1517 f->len = f->pos;
1518}
1519
1520static void whlp_file_add_char(struct file *f, int data)
1521{
1522 unsigned char s;
1523 s = data & 0xFF;
1524 whlp_file_add(f, &s, 1);
1525}
1526
1527static void whlp_file_add_short(struct file *f, int data)
1528{
1529 unsigned char s[2];
1530 PUT_16BIT_LSB_FIRST(s, data);
1531 whlp_file_add(f, s, 2);
1532}
1533
1534static void whlp_file_add_long(struct file *f, int data)
1535{
1536 unsigned char s[4];
1537 PUT_32BIT_LSB_FIRST(s, data);
1538 whlp_file_add(f, s, 4);
1539}
1540
1541static void whlp_file_fill(struct file *f, int len)
1542{
1543 if (f->pos + len > f->size) {
1544 f->size = f->pos + len + 1024;
f1530049 1545 f->data = sresize(f->data, f->size, unsigned char);
d7482997 1546 }
1547 memset(f->data + f->pos, 0, len);
1548 f->pos += len;
1549 if (f->len < f->pos)
1550 f->len = f->pos;
1551}
1552
1553static void whlp_file_seek(struct file *f, int pos, int whence)
1554{
1555 f->pos = (whence == 0 ? 0 : whence == 1 ? f->pos : f->len) + pos;
1556}
1557
1558static int whlp_file_offset(struct file *f)
1559{
1560 return f->pos;
1561}
1562
1563/* ----------------------------------------------------------------------
1564 * Open and close routines; final wrapper around everything.
1565 */
1566
1567WHLP whlp_new(void)
1568{
1569 WHLP ret;
1570 struct file *f;
1571
f1530049 1572 ret = snew(struct WHLP_tag);
d7482997 1573
1574 /*
1575 * Internal B-trees.
1576 */
1577 ret->files = newtree234(filecmp);
1578 ret->pre_contexts = newtree234(NULL);
1579 ret->contexts = newtree234(ctxcmp);
1580 ret->titles = newtree234(ttlcmp);
1581 ret->text = newtree234(NULL);
1582 ret->index = newtree234(idxcmp);
1583 ret->tabstops = newtree234(tabcmp);
1584 ret->fontnames = newtree234(fontcmp);
1585 ret->fontdescs = newtree234(NULL);
1586
1587 /*
1588 * Some standard files.
1589 */
1590 f = whlp_new_file(ret, "|CTXOMAP");
1591 whlp_file_add_short(f, 0); /* dummy section */
1592 f = whlp_new_file(ret, "|SYSTEM");
1593 whlp_standard_systemsection(f);
1594 ret->systemfile = f;
1595
1596 /*
1597 * Other variables.
1598 */
1599 ret->prevtopic = NULL;
1600 ret->ncontexts = 0;
1601 ret->link = NULL;
1602
1603 return ret;
1604}
1605
1606void whlp_close(WHLP h, char *filename)
1607{
1608 FILE *fp;
1609 int filecount, offset, index, filelen;
1610 struct file *file, *map, *md;
1611 context *ctx;
1612 int has_index;
1613
1614 /*
1615 * Lay out the topic section.
1616 */
1617 whlp_topic_layout(h);
1618
1619 /*
1620 * Finish off the system section.
1621 */
1622 whlp_do_primary_topic(h);
1623
1624 /*
1625 * Assemble the font section.
1626 */
1627 file = whlp_new_file(h, "|FONT");
1628 whlp_make_fontsection(h, file);
1629
1630 /*
1631 * Set up the index.
1632 */
1633 has_index = (count234(h->index) != 0);
1634 if (has_index)
1635 whlp_build_kwdata(h);
1636
1637 /*
1638 * Set up the `titles' B-tree for the |TTLBTREE section.
1639 */
1640 for (index = 0; (ctx = index234(h->contexts, index)) != NULL; index++)
1641 add234(h->titles, ctx);
1642
1643 /*
1644 * Construct the various B-trees.
1645 */
1646 file = whlp_new_file(h, "|CONTEXT");
1647 whlp_make_btree(file, 0x0002, 0x0800, "L4",
1648 h->contexts, NULL, ctxindex, ctxleaf);
1649
1650 file = whlp_new_file(h, "|TTLBTREE");
1651 whlp_make_btree(file, 0x0002, 0x0800, "Lz",
1652 h->titles, NULL, ttlindex, ttlleaf);
1653
1654 if (has_index) {
1655 file = whlp_new_file(h, "|KWBTREE");
1656 map = whlp_new_file(h, "|KWMAP");
1657 whlp_make_btree(file, 0x0002, 0x0800, "F24",
1658 h->index, map, idxindex, idxleaf);
1659 }
1660
1661 /*
1662 * Open the output file.
1663 */
1664 fp = fopen(filename, "wb");
1665 if (!fp) {
1666 whlp_abandon(h);
1667 return;
1668 }
1669
1670 /*
1671 * Work out all the file offsets.
1672 */
1673 filecount = count234(h->files);
1674 offset = 16; /* just after header */
1675 for (index = 0; index < filecount; index++) {
1676 file = index234(h->files, index);
1677 file->fileoffset = offset;
1678 offset += 9 + file->len; /* 9 is size of file header */
1679 }
1680 /* Now `offset' holds what will be the offset of the master directory. */
1681
1682 md = whlp_new_file(h, NULL); /* master directory file */
1683 whlp_make_btree(md, 0x0402, 0x0400, "z4",
1684 h->files, NULL, fileindex, fileleaf);
1685
1686 filelen = offset + 9 + md->len;
1687
1688 /*
1689 * Write out the file header.
1690 */
1691 {
1692 unsigned char header[16];
1693 PUT_32BIT_LSB_FIRST(header+0, 0x00035F3FL); /* magic */
1694 PUT_32BIT_LSB_FIRST(header+4, offset); /* offset to directory */
1695 PUT_32BIT_LSB_FIRST(header+8, 0xFFFFFFFFL); /* first free block */
1696 PUT_32BIT_LSB_FIRST(header+12, filelen); /* total file length */
1697 fwrite(header, 1, 16, fp);
1698 }
1699
1700 /*
1701 * Now write out each file.
1702 */
1703 for (index = 0; index <= filecount; index++) {
1704 int used, reserved;
1705 unsigned char header[9];
1706
1707 if (index == filecount)
1708 file = md; /* master directory comes last */
1709 else
1710 file = index234(h->files, index);
1711
1712 used = file->len;
1713 reserved = used + 9;
1714
1715 /* File header. */
1716 PUT_32BIT_LSB_FIRST(header+0, reserved);
1717 PUT_32BIT_LSB_FIRST(header+4, used);
1718 header[8] = 0; /* flags */
1719 fwrite(header, 1, 9, fp);
1720
1721 /* File data. */
1722 fwrite(file->data, 1, file->len, fp);
1723 }
1724
1725 fclose(fp);
1726
1727 whlp_free_file(md);
1728
1729 whlp_abandon(h); /* now free everything */
1730}
1731
1732void whlp_abandon(WHLP h)
1733{
1734 struct file *f;
1735 struct indexrec *idx;
1736 struct topiclink *link;
1737 struct fontdesc *fontdesc;
1738 char *fontname;
1739 context *ctx;
1740
1741 /* Get rid of any lingering tab stops. */
1742 whlp_para_reset(h);
1743
1744 /* Delete the (now empty) tabstops tree. */
1745 freetree234(h->tabstops);
1746
1747 /* Delete the index tree and all its entries. */
1748 while ( (idx = index234(h->index, 0)) != NULL) {
1749 delpos234(h->index, 0);
1750 sfree(idx->term);
1751 sfree(idx);
1752 }
1753 freetree234(h->index);
1754
1755 /* Delete the text tree and all its topiclinks. */
1756 while ( (link = index234(h->text, 0)) != NULL) {
1757 delpos234(h->text, 0);
1758 sfree(link->data1); /* may be NULL */
1759 sfree(link->data2); /* may be NULL */
1760 sfree(link);
1761 }
1762 freetree234(h->text);
1763
1764 /* Delete the fontdescs tree and all its entries. */
1765 while ( (fontdesc = index234(h->fontdescs, 0)) != NULL) {
1766 delpos234(h->fontdescs, 0);
1767 sfree(fontdesc);
1768 }
1769 freetree234(h->fontdescs);
1770
1771 /* Delete the fontnames tree and all its entries. */
1772 while ( (fontname = index234(h->fontnames, 0)) != NULL) {
1773 delpos234(h->fontnames, 0);
1774 sfree(fontname);
1775 }
1776 freetree234(h->fontnames);
1777
1778 /* There might be an unclosed paragraph in h->link. */
1779 if (h->link)
1780 sfree(h->link); /* if so it won't have data1 or data2 */
1781
1782 /*
1783 * `titles' contains copies of the `contexts' entries, so we
1784 * don't need to free them here.
1785 */
1786 freetree234(h->titles);
1787
1788 /*
1789 * `contexts' and `pre_contexts' _both_ contain contexts that
1790 * need freeing. (pre_contexts shouldn't contain any, unless
1791 * the help generation was abandoned half-way through.)
1792 */
1793 while ( (ctx = index234(h->pre_contexts, 0)) != NULL) {
1794 delpos234(h->index, 0);
1795 sfree(ctx->name);
1796 sfree(ctx->title);
1797 sfree(ctx);
1798 }
1799 freetree234(h->pre_contexts);
1800 while ( (ctx = index234(h->contexts, 0)) != NULL) {
1801 delpos234(h->contexts, 0);
1802 sfree(ctx->name);
1803 sfree(ctx->title);
1804 sfree(ctx);
1805 }
1806 freetree234(h->contexts);
1807
1808 /*
1809 * Free all the internal files.
1810 */
1811 while ( (f = index234(h->files, 0)) != NULL ) {
1812 delpos234(h->files, 0);
1813 whlp_free_file(f);
1814 }
1815 freetree234(h->files);
1816
1817 sfree(h);
1818}
1819
1820#ifdef TESTMODE
1821
1822int main(void)
1823{
1824 WHLP h;
1825 WHLP_TOPIC t1, t2, t3;
1826 char *e;
1827 char mymacro[100];
1828
1829 h = whlp_new();
1830
1831 whlp_title(h, "Test Help File");
1832 whlp_copyright(h, "This manual is copyright \251 2001 Simon Tatham."
1833 " All rights reversed.");
1834 whlp_start_macro(h, "CB(\"btn_about\",\"&About\",\"About()\")");
1835 whlp_start_macro(h, "CB(\"btn_up\",\"&Up\",\"Contents()\")");
1836 whlp_start_macro(h, "BrowseButtons()");
1837
1838 whlp_create_font(h, "Arial", WHLP_FONTFAM_SANS, 30,
1839 0, 0, 0, 0);
1840 whlp_create_font(h, "Times New Roman", WHLP_FONTFAM_SERIF, 24,
1841 WHLP_FONT_STRIKEOUT, 0, 0, 0);
1842 whlp_create_font(h, "Times New Roman", WHLP_FONTFAM_SERIF, 24,
1843 WHLP_FONT_ITALIC, 0, 0, 0);
1844 whlp_create_font(h, "Courier New", WHLP_FONTFAM_FIXED, 24,
1845 0, 0, 0, 0);
1846
1847 t1 = whlp_register_topic(h, "foobar", &e);
1848 assert(t1 != NULL);
1849 t2 = whlp_register_topic(h, "M359HPEHGW", &e);
1850 assert(t2 != NULL);
1851 t3 = whlp_register_topic(h, "Y5VQEXZQVJ", &e);
1852 assert(t3 == NULL && !strcmp(e, "M359HPEHGW"));
1853 t3 = whlp_register_topic(h, NULL, NULL);
1854 assert(t3 != NULL);
1855
1856 whlp_primary_topic(h, t2);
1857
1858 whlp_prepare(h);
1859
1860 whlp_begin_topic(h, t1, "First Topic", "DB(\"btn_up\")", NULL);
1861
1862 whlp_begin_para(h, WHLP_PARA_NONSCROLL);
1863 whlp_set_font(h, 0);
1864 whlp_text(h, "Foobar");
1865 whlp_end_para(h);
1866
1867 whlp_begin_para(h, WHLP_PARA_SCROLL);
1868 whlp_set_font(h, 1);
1869 whlp_text(h, "This is a silly paragraph with ");
1870 whlp_set_font(h, 3);
1871 whlp_text(h, "code");
1872 whlp_set_font(h, 1);
1873 whlp_text(h, " in it.");
1874 whlp_end_para(h);
1875
1876 whlp_para_attr(h, WHLP_PARA_SPACEABOVE, 12);
1877 whlp_begin_para(h, WHLP_PARA_SCROLL);
1878 whlp_set_font(h, 1);
1879 whlp_text(h, "This second, equally silly, paragraph has ");
1880 whlp_set_font(h, 2);
1881 whlp_text(h, "emphasis");
1882 whlp_set_font(h, 1);
1883 whlp_text(h, " just to prove we can do it.");
1884 whlp_end_para(h);
1885
1886 whlp_begin_para(h, WHLP_PARA_SCROLL);
1887 whlp_set_font(h, 1);
1888 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1889 " to make some wrapping happen, and also to make the topicblock"
1890 " go across its boundaries. This is going to take a fair amount"
1891 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1892 whlp_end_para(h);
1893
1894 whlp_begin_para(h, WHLP_PARA_SCROLL);
1895 whlp_set_font(h, 1);
1896 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1897 " to make some wrapping happen, and also to make the topicblock"
1898 " go across its boundaries. This is going to take a fair amount"
1899 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1900 whlp_end_para(h);
1901
1902 whlp_begin_para(h, WHLP_PARA_SCROLL);
1903 whlp_set_font(h, 1);
1904 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1905 " to make some wrapping happen, and also to make the topicblock"
1906 " go across its boundaries. This is going to take a fair amount"
1907 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1908 whlp_end_para(h);
1909
1910 whlp_begin_para(h, WHLP_PARA_SCROLL);
1911 whlp_set_font(h, 1);
1912 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1913 " to make some wrapping happen, and also to make the topicblock"
1914 " go across its boundaries. This is going to take a fair amount"
1915 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1916 whlp_end_para(h);
1917
1918 whlp_begin_para(h, WHLP_PARA_SCROLL);
1919 whlp_set_font(h, 1);
1920 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1921 " to make some wrapping happen, and also to make the topicblock"
1922 " go across its boundaries. This is going to take a fair amount"
1923 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1924 whlp_end_para(h);
1925
1926 whlp_begin_para(h, WHLP_PARA_SCROLL);
1927 whlp_set_font(h, 1);
1928 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1929 " to make some wrapping happen, and also to make the topicblock"
1930 " go across its boundaries. This is going to take a fair amount"
1931 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1932 whlp_end_para(h);
1933
1934 whlp_begin_para(h, WHLP_PARA_SCROLL);
1935 whlp_set_font(h, 1);
1936 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1937 " to make some wrapping happen, and also to make the topicblock"
1938 " go across its boundaries. This is going to take a fair amount"
1939 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1940 whlp_end_para(h);
1941
1942 whlp_begin_para(h, WHLP_PARA_SCROLL);
1943 whlp_set_font(h, 1);
1944 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1945 " to make some wrapping happen, and also to make the topicblock"
1946 " go across its boundaries. This is going to take a fair amount"
1947 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1948 whlp_end_para(h);
1949
1950 whlp_begin_para(h, WHLP_PARA_SCROLL);
1951 whlp_set_font(h, 1);
1952 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1953 " to make some wrapping happen, and also to make the topicblock"
1954 " go across its boundaries. This is going to take a fair amount"
1955 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1956 whlp_end_para(h);
1957
1958 whlp_begin_para(h, WHLP_PARA_SCROLL);
1959 whlp_set_font(h, 1);
1960 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1961 " to make some wrapping happen, and also to make the topicblock"
1962 " go across its boundaries. This is going to take a fair amount"
1963 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1964 whlp_end_para(h);
1965
1966 whlp_begin_para(h, WHLP_PARA_SCROLL);
1967 whlp_set_font(h, 1);
1968 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1969 " to make some wrapping happen, and also to make the topicblock"
1970 " go across its boundaries. This is going to take a fair amount"
1971 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1972 whlp_end_para(h);
1973
1974 whlp_begin_para(h, WHLP_PARA_SCROLL);
1975 whlp_set_font(h, 1);
1976 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1977 " to make some wrapping happen, and also to make the topicblock"
1978 " go across its boundaries. This is going to take a fair amount"
1979 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1980 whlp_end_para(h);
1981
1982 whlp_begin_para(h, WHLP_PARA_SCROLL);
1983 whlp_set_font(h, 1);
1984 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1985 " to make some wrapping happen, and also to make the topicblock"
1986 " go across its boundaries. This is going to take a fair amount"
1987 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1988 whlp_end_para(h);
1989
1990 whlp_begin_para(h, WHLP_PARA_SCROLL);
1991 whlp_set_font(h, 1);
1992 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
1993 " to make some wrapping happen, and also to make the topicblock"
1994 " go across its boundaries. This is going to take a fair amount"
1995 " of text, so I'll just have to cheat and c'n'p a lot of it.");
1996 whlp_end_para(h);
1997
1998 whlp_begin_para(h, WHLP_PARA_SCROLL);
1999 whlp_set_font(h, 1);
2000 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2001 " to make some wrapping happen, and also to make the topicblock"
2002 " go across its boundaries. This is going to take a fair amount"
2003 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2004 whlp_end_para(h);
2005
2006 whlp_begin_para(h, WHLP_PARA_SCROLL);
2007 whlp_set_font(h, 1);
2008 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2009 " to make some wrapping happen, and also to make the topicblock"
2010 " go across its boundaries. This is going to take a fair amount"
2011 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2012 whlp_end_para(h);
2013
2014 whlp_begin_para(h, WHLP_PARA_SCROLL);
2015 whlp_set_font(h, 1);
2016 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2017 " to make some wrapping happen, and also to make the topicblock"
2018 " go across its boundaries. This is going to take a fair amount"
2019 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2020 whlp_end_para(h);
2021
2022 whlp_begin_para(h, WHLP_PARA_SCROLL);
2023 whlp_set_font(h, 1);
2024 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2025 " to make some wrapping happen, and also to make the topicblock"
2026 " go across its boundaries. This is going to take a fair amount"
2027 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2028 whlp_end_para(h);
2029
2030 whlp_begin_para(h, WHLP_PARA_SCROLL);
2031 whlp_set_font(h, 1);
2032 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2033 " to make some wrapping happen, and also to make the topicblock"
2034 " go across its boundaries. This is going to take a fair amount"
2035 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2036 whlp_end_para(h);
2037
2038 whlp_begin_para(h, WHLP_PARA_SCROLL);
2039 whlp_set_font(h, 1);
2040 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2041 " to make some wrapping happen, and also to make the topicblock"
2042 " go across its boundaries. This is going to take a fair amount"
2043 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2044 whlp_end_para(h);
2045
2046 whlp_begin_para(h, WHLP_PARA_SCROLL);
2047 whlp_set_font(h, 1);
2048 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2049 " to make some wrapping happen, and also to make the topicblock"
2050 " go across its boundaries. This is going to take a fair amount"
2051 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2052 whlp_end_para(h);
2053
2054 whlp_begin_para(h, WHLP_PARA_SCROLL);
2055 whlp_set_font(h, 1);
2056 whlp_text(h, "Now I'm going to waffle on indefinitely, in a vague attempt"
2057 " to make some wrapping happen, and also to make the topicblock"
2058 " go across its boundaries. This is going to take a fair amount"
2059 " of text, so I'll just have to cheat and c'n'p a lot of it.");
2060 whlp_end_para(h);
2061
2062 whlp_begin_para(h, WHLP_PARA_SCROLL);
2063 whlp_set_font(h, 1);
2064 whlp_text(h, "Have a ");
2065 whlp_start_hyperlink(h, t2);
2066 whlp_text(h, "hyperlink");
2067 whlp_end_hyperlink(h);
2068 whlp_text(h, " to another topic.");
2069 whlp_end_para(h);
2070
2071 sprintf(mymacro, "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
2072 whlp_topic_id(t3));
2073
2074 whlp_begin_topic(h, t2, "Second Topic", mymacro, NULL);
2075
2076 whlp_begin_para(h, WHLP_PARA_SCROLL);
2077 whlp_set_font(h, 1);
2078 whlp_text(h, "This topic contains no non-scrolling region. I would"
2079 " illustrate this with a ludicrously long paragraph, but that"
2080 " would get very tedious very quickly. Instead I'll just waffle"
2081 " on pointlessly for a little bit and then shut up.");
2082 whlp_end_para(h);
2083
2084 whlp_set_tabstop(h, 36, WHLP_ALIGN_LEFT);
2085 whlp_para_attr(h, WHLP_PARA_LEFTINDENT, 36);
2086 whlp_para_attr(h, WHLP_PARA_FIRSTLINEINDENT, -36);
2087 whlp_para_attr(h, WHLP_PARA_SPACEABOVE, 12);
2088 whlp_begin_para(h, WHLP_PARA_SCROLL);
2089 whlp_set_font(h, 1);
2090 whlp_text(h, "\225"); /* bullet */
2091 whlp_tab(h);
2092 whlp_text(h, "This is a paragraph with a bullet. With any luck it should"
2093 " work exactly like it used to in the old NASM help file.");
2094 whlp_end_para(h);
2095
2096 whlp_set_tabstop(h, 128, WHLP_ALIGN_RIGHT);
2097 whlp_set_tabstop(h, 256, WHLP_ALIGN_CENTRE);
2098 whlp_set_tabstop(h, 384, WHLP_ALIGN_LEFT);
2099 whlp_para_attr(h, WHLP_PARA_SPACEABOVE, 12);
2100 whlp_begin_para(h, WHLP_PARA_SCROLL);
2101 whlp_set_font(h, 1);
2102 whlp_text(h, "Ooh:"); whlp_tab(h);
2103 whlp_text(h, "Right?"); whlp_tab(h);
2104 whlp_text(h, "Centre?"); whlp_tab(h);
2105 whlp_text(h, "Left?");
2106 whlp_end_para(h);
2107
2108 whlp_set_tabstop(h, 128, WHLP_ALIGN_RIGHT);
2109 whlp_set_tabstop(h, 256, WHLP_ALIGN_CENTRE);
2110 whlp_set_tabstop(h, 384, WHLP_ALIGN_LEFT);
2111 whlp_begin_para(h, WHLP_PARA_SCROLL);
2112 whlp_set_font(h, 1);
2113 whlp_text(h, "Aah:"); whlp_tab(h);
2114 whlp_text(h, "R?"); whlp_tab(h);
2115 whlp_text(h, "C?"); whlp_tab(h);
2116 whlp_text(h, "L?");
2117 whlp_end_para(h);
2118
2119 sprintf(mymacro, "CBB(\"btn_up\",\"JI(`',`%s')\");EB(\"btn_up\")",
2120 whlp_topic_id(t1));
2121
2122 whlp_begin_topic(h, t3, "Third Topic", mymacro, NULL);
2123
2124 whlp_begin_para(h, WHLP_PARA_SCROLL);
2125 whlp_set_font(h, 1);
2126 whlp_text(h, "This third topic is almost as boring as the first. Woo!");
2127 whlp_end_para(h);
2128
2129 /*
2130 * Browse sequence.
2131 */
2132 whlp_browse_link(h, t1, t2);
2133 whlp_browse_link(h, t2, t3);
2134
2135 /*
2136 * Index terms.
2137 */
2138 whlp_index_term(h, "foobarbaz", t1);
2139 whlp_index_term(h, "foobarbaz", t2);
2140 whlp_index_term(h, "foobarbaz", t3);
2141 whlp_index_term(h, "foobar", t1);
2142 whlp_index_term(h, "foobar", t2);
2143 whlp_index_term(h, "foobaz", t1);
2144 whlp_index_term(h, "foobaz", t3);
2145 whlp_index_term(h, "barbaz", t2);
2146 whlp_index_term(h, "barbaz", t3);
2147 whlp_index_term(h, "foo", t1);
2148 whlp_index_term(h, "bar", t2);
2149 whlp_index_term(h, "baz", t3);
2150
2151 whlp_close(h, "test.hlp");
2152 return 0;
2153}
2154
2155#endif