| 1 |
\datethis |
| 2 |
|
| 3 |
@*Introduction. This short program compiles a master index for a |
| 4 |
set of programs that have been processed by \.{CTWILL}. |
| 5 |
To use it, you say, e.g., \.{twinx} \.{*.tex} \.{>index.tex}. |
| 6 |
The individual programs should define their names with a line of |
| 7 |
the form `\.{\\def\\title\{NAME\}}'. |
| 8 |
|
| 9 |
@c |
| 10 |
#include <stdio.h> |
| 11 |
@<Type definitions@>@; |
| 12 |
@<Global variables@>@; |
| 13 |
@<Procedures@>@; |
| 14 |
@# |
| 15 |
main(argc,argv) |
| 16 |
int argc; |
| 17 |
char *argv[]; |
| 18 |
{ @<Local variables@>; |
| 19 |
@<Initialize the data structures@>; |
| 20 |
while (--argc) { |
| 21 |
f=fopen(*++argv,"r"); |
| 22 |
if (!f) |
| 23 |
fprintf(stderr,"twinx: Couldn't open file %s for reading!\n",*argv); |
| 24 |
else { |
| 25 |
@<Scan file |f| until coming to the title@>; |
| 26 |
fclose(f); |
| 27 |
strncpy(*argv+strlen(*argv)-3,"idx",3); |
| 28 |
f=fopen(*argv,"r"); |
| 29 |
if (!f) |
| 30 |
fprintf(stderr,"twinx: Couldn't open file %s for reading!\n",*argv); |
| 31 |
else { |
| 32 |
@<Copy the index file |f| into the data structures@>; |
| 33 |
fclose(f); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
@<Output the data structures to make a master index@>; |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
@ @d buf_size 100 /* input lines won't be this long */ |
| 42 |
|
| 43 |
@<Glob...@>= |
| 44 |
FILE *f; |
| 45 |
char buf[buf_size]; |
| 46 |
char title[buf_size]; |
| 47 |
char cur_name[buf_size]; |
| 48 |
|
| 49 |
@ @<Scan file |f|...@>= |
| 50 |
while (1) { |
| 51 |
if (fgets(buf,buf_size,f)==NULL) { |
| 52 |
fprintf(stderr,"twinx: (no title found in file %s)\n",*argv); |
| 53 |
title[0]='\0'; |
| 54 |
break; |
| 55 |
} |
| 56 |
if (strncmp(buf,"\\def\\title\{",11)==0) {@+register char *p,*q; |
| 57 |
for (p=buf+11,q=title;*p && *p!='}';p++) *q++=*p; |
| 58 |
*q='\0'; |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@* Data structures. |
| 64 |
Our main task is to collate a bunch of texts associated with keys that |
| 65 |
have already been sorted. It seems easiest to do this by repeatedly merging |
| 66 |
the new data into the old, even though this means we'll be passing over |
| 67 |
some of the same keys 30 times or more; the computer is fast, and this |
| 68 |
program won't be run often. |
| 69 |
|
| 70 |
Further examination shows that a merging strategy isn't so easy after all, |
| 71 |
because the sorting done by \.{CTWILL} (and by \.{CWEAVE}) is weird |
| 72 |
in certain cases. When two index entries agree except for their |
| 73 |
``ilk,'' the order in which they appear in the index depends on the order in |
| 74 |
which they appear in the program. Thus, they might well appear in |
| 75 |
different order in two of the indexes we are merging. |
| 76 |
(There's also another glitch, although not quite as devasting: |
| 77 |
When two index entries have the |
| 78 |
same letters and the same ilk, but differ with respect to uppercase |
| 79 |
versus lowercase, the order in which they appear depends on the |
| 80 |
hash code used in \.{CWEB}'s \.{common.w} code!) |
| 81 |
|
| 82 |
So we'll use Plan B: All index entries will first be copied into a long |
| 83 |
list. The list will almost always consist of many sorted sublists, |
| 84 |
but we will not assume anything about its order. After all the copying |
| 85 |
has been done, we will use a list-merge sort to finish the job. |
| 86 |
|
| 87 |
The data structure is built from nodes that each contain three pointers. |
| 88 |
The first pointer is to an |id| string; the third pointer is to the |
| 89 |
|next| node; and the second pointer is either |data.s|, a pointer to |
| 90 |
a string of text, or |data.n|, a pointer to a node. In the main list, |
| 91 |
the |id| fields are the keys of the index, and the |data.n| fields |
| 92 |
point to lists of associated texts. In the latter lists, the |id| fields |
| 93 |
are the individual program titles, while the |data.s| fields are the |
| 94 |
texts. |
| 95 |
|
| 96 |
@<Type...@>= |
| 97 |
typedef union { |
| 98 |
char *s; |
| 99 |
struct node_struct *n; |
| 100 |
} mixed; |
| 101 |
typedef struct node_struct { |
| 102 |
char *id; |
| 103 |
mixed data; |
| 104 |
struct node_struct *next; |
| 105 |
} node; |
| 106 |
|
| 107 |
@ We copy strings into blocks of storage that are allocated as needed. |
| 108 |
Here's a routine that stashes away a given string. It makes no attempt to |
| 109 |
handle extremely long strings, because such strings will arise only |
| 110 |
if the input is all screwed up. |
| 111 |
|
| 112 |
@d string_block_size 8192 /* number of bytes per string block */ |
| 113 |
|
| 114 |
@<Proc...@>= |
| 115 |
char *save_string(s) |
| 116 |
char *s; |
| 117 |
{ |
| 118 |
register char *p,*q; register int l; |
| 119 |
for (p=s;*p;p++) ; |
| 120 |
l=p-s+1; |
| 121 |
if (l>string_block_size) { |
| 122 |
fprintf(stderr,"twinx: Huge string `%.20s...' will be truncated!\n",s); |
| 123 |
l=string_block_size; |
| 124 |
s[l-1]='\0'; |
| 125 |
} |
| 126 |
if (next_string+l>=bad_string) { |
| 127 |
next_string=(char*)malloc(string_block_size); |
| 128 |
if (next_string==NULL) { |
| 129 |
fprintf(stderr,"twinx: Not enough room for strings!\n"); |
| 130 |
exit(-1); |
| 131 |
} |
| 132 |
bad_string=next_string+string_block_size; |
| 133 |
} |
| 134 |
for (p=s,q=next_string;*p;p++) *q++=*p; |
| 135 |
*q='\0'; |
| 136 |
next_string=q+1; |
| 137 |
return next_string-l; |
| 138 |
} |
| 139 |
|
| 140 |
@ Nodes are allocated with a similar but simpler mechanism. |
| 141 |
|
| 142 |
@d nodes_per_block 340 |
| 143 |
|
| 144 |
@<Proc...@>= |
| 145 |
node *new_node() |
| 146 |
{ |
| 147 |
if (next_node==bad_node) { |
| 148 |
next_node=(node*)calloc(nodes_per_block,sizeof(node)); |
| 149 |
if (next_node==NULL) { |
| 150 |
fprintf(stderr,"twinx: Not enough room for nodes!\n"); |
| 151 |
exit(-2); |
| 152 |
} |
| 153 |
bad_node=next_node+nodes_per_block; |
| 154 |
} |
| 155 |
next_node++; |
| 156 |
return next_node-1; |
| 157 |
} |
| 158 |
|
| 159 |
@ @<Glob...@>= |
| 160 |
char *next_string,*bad_string; |
| 161 |
node *next_node,*bad_node; |
| 162 |
node header; /* the main list begins at |header.next| */ |
| 163 |
node sentinel; /* intermediate lists will end at this node */ |
| 164 |
|
| 165 |
@ We don't really have to initialize the string and node storage |
| 166 |
pointers, because global variables are zero already. But we might as |
| 167 |
well be tidy and state the initial conditions explicitly. |
| 168 |
|
| 169 |
It will be convenient to have extremely small and large keys in |
| 170 |
the dummy nodes. |
| 171 |
|
| 172 |
@<Initialize the data structures@>= |
| 173 |
next_string=bad_string=NULL; |
| 174 |
next_node=bad_node=NULL; |
| 175 |
header.next=NULL; |
| 176 |
header.id=" {"; /* smaller than any valid |id| */ |
| 177 |
sentinel.id=" {\200}"; /* larger than any valid |id| */ |
| 178 |
main_node=&header; |
| 179 |
|
| 180 |
@ @<Local v...@>= |
| 181 |
register node* main_node; /* current end of main list */ |
| 182 |
|
| 183 |
@* Copying. |
| 184 |
Lines in the index file |f| that we're reading either begin a new |
| 185 |
entry or continue a long entry. In the first case, the line begins with |
| 186 |
\.{\\I} and then either \.{\\\\\{key\}} or \.{\\\char'174\{key\}} or |
| 187 |
\.{\\.\{key\}} or \.{\\\&\{key\}} or \.{\\\$\{key\}} or \.{\\9\{key\}} |
| 188 |
or just \.{\ \ \{key\}}. (These correspond to multi-character italic, |
| 189 |
single-digit italic, typewriter, bold, custom, variable, and roman styles.) |
| 190 |
In the second case, the line begins with a page number or \.{\\[}; |
| 191 |
however, we recognize the second case by the fact that the previous line |
| 192 |
did not end with a period. |
| 193 |
|
| 194 |
@<Copy the index...@>= |
| 195 |
while (1) {@+register node *cur_node; |
| 196 |
if (fgets(buf,buf_size,f)==NULL) break; /* end of file */ |
| 197 |
if (strncmp(buf,"\\I",2)==0) { |
| 198 |
@<Copy a new index entry into |cur_name| and |cur_node|@>; |
| 199 |
main_node->next=new_node();@+main_node=main_node->next; |
| 200 |
main_node->id=save_string(cur_name); |
| 201 |
main_node->data.n=cur_node; |
| 202 |
} |
| 203 |
else if (buf[0]!='\n') |
| 204 |
fprintf(stderr,"twinx: couldn't deal with `%.10s...' in file %s!\n", |
| 205 |
buf,*argv); |
| 206 |
} |
| 207 |
|
| 208 |
@ @<Copy a new index entry...@>= |
| 209 |
if (buf[4]!='{') { |
| 210 |
fprintf(stderr,"twinx: missing brace in file %s: `%.20s...'\n",*argv,buf); |
| 211 |
break; |
| 212 |
} |
| 213 |
{@+register char *p,*q;@+register int bal=1; |
| 214 |
cur_name[0]=buf[2];@+cur_name[1]=buf[3];@+cur_name[2]='{'; |
| 215 |
for (p=buf+5,q=cur_name+3;*p&&(bal||*p=='{');p++) { |
| 216 |
if (*p=='{') bal++; |
| 217 |
else if (*p=='}') bal--; |
| 218 |
*q++=*p; |
| 219 |
} |
| 220 |
if (bal) { |
| 221 |
fprintf(stderr,"twinx: unbalanced entry in file %s: `%.20s...'\n",*argv,buf); |
| 222 |
break; |
| 223 |
} |
| 224 |
if (*p++!=',') { |
| 225 |
fprintf(stderr,"twinx: missing comma in file %s: `%.20s...'\n",*argv,buf); |
| 226 |
break; |
| 227 |
} |
| 228 |
if (*p++!=' ') { |
| 229 |
fprintf(stderr,"twinx: missing space in file %s: `%.20s...'\n",*argv,buf); |
| 230 |
break; |
| 231 |
} |
| 232 |
*q='\0'; |
| 233 |
@<Copy the text part of the index entry into |cur_node|@>; |
| 234 |
} |
| 235 |
|
| 236 |
@ When we get here, |p| points to the beginning of the text following a key |
| 237 |
in the index. The index entry ends with the next period, possibly several |
| 238 |
lines hence. In the multiple-line case, |cur_node| will point to the |
| 239 |
final line, which points to the penultimate line, etc. |
| 240 |
|
| 241 |
@<Copy the text part of the index entry...@>= |
| 242 |
{@+int period_sensed=0; |
| 243 |
node *continuation; |
| 244 |
cur_node=new_node(); |
| 245 |
cur_node->id=save_string(title); |
| 246 |
do@+{ |
| 247 |
for (q=p;*q&&*q!='\n'&&*q!='.';q++) ; |
| 248 |
if (*q=='.') period_sensed=1; |
| 249 |
*q='\0'; |
| 250 |
cur_node->data.s=save_string(p); |
| 251 |
if (period_sensed) break; |
| 252 |
continuation=new_node(); /* the |id| field is |NULL| */ |
| 253 |
continuation->next=cur_node; |
| 254 |
cur_node=continuation; |
| 255 |
p=buf; |
| 256 |
}@+while(fgets(buf,buf_size,f)); |
| 257 |
if (!period_sensed) { |
| 258 |
fprintf(stderr,"twinx: File %s ended in middle of entry for %s!\n", |
| 259 |
*argv,cur_name); |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
@* Sorting. |
| 265 |
Let us opt for simplicity instead of tuning up for speed. The idea in |
| 266 |
this step is to take a list that contains $k$ ascending runs and reduce it |
| 267 |
to a list that contains $\lceil k/2\rceil$ runs, repeating until $k=1$. |
| 268 |
We could make the program about twice as fast if we took the trouble to |
| 269 |
remember the boundaries of runs on the previous pass; here, every |
| 270 |
pass will be the same. |
| 271 |
|
| 272 |
@<Output the data structures to make a master index@>= |
| 273 |
@<Sort the main list, collapsing entries with the same |id|@>; |
| 274 |
@<Output the main list in suitable \TeX\ format@>; |
| 275 |
|
| 276 |
@ The |compare| subroutine, which specifies the relative order of |
| 277 |
|id| fields in two nodes, appears below. Let's get the sorting logic |
| 278 |
right first. |
| 279 |
|
| 280 |
The algorithm is, in fact, rather pretty---I hate to say cute, but |
| 281 |
that's the word that comes to mind. Some day I must write out the |
| 282 |
nice invariant relations in these loops. Too bad it's not more efficient. |
| 283 |
|
| 284 |
Remember that |header.id| is $-\infty$ and |sentinel.id| is $+\infty$. |
| 285 |
Also remember that the main list begins and ends at the header node. |
| 286 |
|
| 287 |
@<Sort the main list, collapsing entries with the same |id|@>= |
| 288 |
main_node->next=&header; |
| 289 |
while (1) {@+register node *p,*q,*r,*s,*t; |
| 290 |
t=&header; |
| 291 |
r=t->next; |
| 292 |
while (1) { |
| 293 |
if (r==&header) break; |
| 294 |
p=s=r; |
| 295 |
@<Advance |s| until it exceeds |r=s->next|@>; |
| 296 |
if (r==&header) break; |
| 297 |
s->next=&sentinel; |
| 298 |
q=s=r; |
| 299 |
@<Advance |s| until it exceeds |r=s->next|@>; |
| 300 |
s->next=&sentinel; |
| 301 |
@<Merge |p| and |q|, appending to |t|@>; |
| 302 |
t->next=r; |
| 303 |
} |
| 304 |
if (t==&header) break; |
| 305 |
} |
| 306 |
|
| 307 |
@ @<Advance...@>= |
| 308 |
do@+{@+register int d; |
| 309 |
r=s->next; |
| 310 |
d=compare(s,r); |
| 311 |
if (d>0) break; /* |s->id>r->id| */ |
| 312 |
if (d==0) { /* |s->id=r->id| */ |
| 313 |
collapse(s,r); /* put |r|'s data into |s|'s list */ |
| 314 |
s->next=r->next; /* node |r| will be unclaimed garbage */ |
| 315 |
} else s=r; /* this is the normal case, |s->id<r->id| */ |
| 316 |
}@+while(1); |
| 317 |
|
| 318 |
@ Merging takes place in such a way that sorting is stable. |
| 319 |
Thus, index entries for a key that appears in different programs |
| 320 |
will remain in the order of the \.{.tex} files on the command line. |
| 321 |
|
| 322 |
@<Merge...@>= |
| 323 |
do@+{@+register int d; |
| 324 |
d=compare(p,q); |
| 325 |
if (d>0) { /* |p->id>q->id| */ |
| 326 |
t->next=q; |
| 327 |
t=q; |
| 328 |
q=q->next; |
| 329 |
} else if (d<0) { /* |p->id<q->id| */ |
| 330 |
t->next=p; /* |p->id<q->id| */ |
| 331 |
t=p; |
| 332 |
p=p->next; |
| 333 |
} else if (p==&sentinel) break; |
| 334 |
else { |
| 335 |
collapse(p,q); /* put |q|'s data into |p|'s list */ |
| 336 |
q=q->next; |
| 337 |
} |
| 338 |
}@+while(1); |
| 339 |
|
| 340 |
@ Comparison is a three-stage process in general. First we compare the |
| 341 |
keys without regarding case or format type. If they are equal with |
| 342 |
respect to that criterion, we try again, with case significant. |
| 343 |
If they are still equal, we look at the format characters (the first |
| 344 |
two characters of the |id| field). |
| 345 |
|
| 346 |
@<Proc...@>= |
| 347 |
int compare(p,q) |
| 348 |
node *p,*q; |
| 349 |
{@+register unsigned char *pp,*qq; |
| 350 |
for (pp=(unsigned char*)p->id+3,qq=(unsigned char*)q->id+3; |
| 351 |
*pp&&ord[*pp]==ord[*qq];pp++,qq++) ; |
| 352 |
if (*pp || *qq) return ord[*pp]-ord[*qq]; |
| 353 |
for (pp=(unsigned char*)p->id+3,qq=(unsigned char*)q->id+3; |
| 354 |
*pp&&*pp==*qq;pp++,qq++) ; |
| 355 |
if (*pp || *qq) return (int)*pp-(int)*qq; |
| 356 |
if (p->id[0]!=q->id[0]) return p->id[0]-q->id[0]; |
| 357 |
return p->id[1]-q->id[1]; |
| 358 |
} |
| 359 |
|
| 360 |
@ The collation order follows a string copied from \.{CWEAVE}. |
| 361 |
|
| 362 |
@<Glob...@>= |
| 363 |
char collate[102]; /* collation order */ |
| 364 |
char ord[256]; /* rank in collation order */ |
| 365 |
|
| 366 |
@ The right brace is placed lowest in collating order, because each |
| 367 |
key is actually followed by a right brace when we are sorting. |
| 368 |
|
| 369 |
Apology: I haven't had time to update this part of the program to |
| 370 |
allow 8-bit characters. At present the data is assumed to be |
| 371 |
7-bit ASCII, as it was in the early versions of \.{CWEAVE}. |
| 372 |
|
| 373 |
@<Init...@>= |
| 374 |
collate[0]=0; strcpy(collate+1,"} \1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\ |
| 375 |
\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\ |
| 376 |
!\42#$%&'()*+,-./:;<=>?@@[\\]^`{|~_\ |
| 377 |
abcdefghijklmnopqrstuvwxyz0123456789"); |
| 378 |
{@+register int j; |
| 379 |
for (j=1;collate[j];j++) ord[collate[j]]=j; |
| 380 |
ord[128]=j; /* this affects the ordering of |sentinel.id| */ |
| 381 |
for (j='A';j<='Z';j++) ord[j]=ord[tolower(j)]; |
| 382 |
} |
| 383 |
|
| 384 |
@ When two lists are combined, we put the data from the second node |
| 385 |
before the data from the first node, because we are going to reverse |
| 386 |
the order when printing. After this procedure has acted, the field |
| 387 |
|q->data.n| should not be considered an active pointer. |
| 388 |
|
| 389 |
@<Proc...@>= |
| 390 |
collapse(p,q) |
| 391 |
node *p,*q; |
| 392 |
{@+register node *x; |
| 393 |
for (x=q->data.n;x->next;x=x->next) ; |
| 394 |
x->next=p->data.n; |
| 395 |
p->data.n=q->data.n; |
| 396 |
} |
| 397 |
|
| 398 |
@ The only remaining trick is to format the underline characters |
| 399 |
properly, especially in the ``custom'' format when they must become |
| 400 |
\.x's. |
| 401 |
|
| 402 |
@<Output the main list in suitable \TeX\ format@>= |
| 403 |
{@+register node *x; |
| 404 |
printf("\\input cttwinxmac\n"); |
| 405 |
for (x=header.next;x!=&header;x=x->next) { |
| 406 |
printf("\\I"); |
| 407 |
@<Output |x->id| in suitable \TeX\ format@>; |
| 408 |
@<Output the lines of |x->data.n| in reverse order@>; |
| 409 |
} |
| 410 |
printf("\\fin\n"); |
| 411 |
} |
| 412 |
|
| 413 |
@ @<Output |x->id|...@>= |
| 414 |
{@+register char *p=x->id; |
| 415 |
if (*p==' ') { |
| 416 |
if (*(p+1)!=' ') goto unknown; |
| 417 |
goto known; |
| 418 |
} |
| 419 |
if (*p!='\\') goto unknown; |
| 420 |
switch (*(p+1)) { |
| 421 |
case '\\': case '|': case '.': case '&': case '9': |
| 422 |
printf("\\%c",*(p+1)); |
| 423 |
goto known; |
| 424 |
case '$': printf("$\\"); |
| 425 |
for (p+=3;*p!='}';p++) |
| 426 |
if (*p=='_') putchar('x'); |
| 427 |
else putchar(*p); |
| 428 |
putchar('$'); |
| 429 |
goto done; |
| 430 |
default: goto unknown; |
| 431 |
} |
| 432 |
unknown: fprintf(stderr,"twinx: `%s' has unknown format!\n",p); |
| 433 |
known: for (p+=2;*p;p++) { |
| 434 |
if (*p=='_') putchar('\\'); |
| 435 |
putchar(*p); |
| 436 |
} |
| 437 |
done:; |
| 438 |
} |
| 439 |
|
| 440 |
@ @<Output the lines of |x->data.n| in reverse order@>= |
| 441 |
{@+register node *y=x->data.n,*z=NULL; |
| 442 |
while (y) {@+register node *w; |
| 443 |
w=y->next; |
| 444 |
y->next=z; |
| 445 |
z=y; |
| 446 |
y=w; |
| 447 |
} |
| 448 |
while (z) { |
| 449 |
if (z->id) printf("\\unskip, {\\sc %s}~",z->id); |
| 450 |
fputs(z->data.s,stdout); |
| 451 |
z=z->next; |
| 452 |
if (z) putchar('\n'); |
| 453 |
else puts("."); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
@* Index. |