Debian Codegraph @

texk/web2c/cwebdir/refsort.w

raw
1 % usage: refsort <foo.ref >foo.sref (after first pass with ctwimac)
2 \datethis
3
4 @* Introduction. This short program sorts the mini-indexes of listings
5 prepared by \.{CTWILL}.
6
7 More precisely, suppose you have said \.{ctwill} \.{foo.w},
8 getting a file \.{foo.tex}, and that you've then said \.{tex} \.{foo.tex},
9 getting files \.{foo.dvi} and \.{foo.ref}. If you're happy with \.{foo.dvi}
10 except for the alphabetic order of the mini-indexes, you can then say
11 $$\hbox{\tt refsort <foo.ref >foo.sref}$$
12 after which \.{tex} \.{foo} will produce \.{foo.dvi} again, this time
13 with the mini-indexes in order.
14
15 Still more precisely, this program reads from standard input a file consisting
16 of groups of unsorted lines and writes to standard output a file consisting
17 of groups of sorted lines. Each input group begins with an identification
18 line whose first character is \.!; the remaining characters are a page number.
19 The other lines in the group all have the form
20 $$\.{+\ }\alpha\.{\ \\?\{}\kappa\.{\}}\omega$$
21 where $\alpha$ is a string containing no spaces, \.? is a single
22 character, $\kappa$ is a string of
23 letters, digits, and \.{\\\_}'s, and $\omega$ is an arbitrary string.
24 The output groups contain the same lines without the initial \.{+\ },
25 sorted alphabetically with respect to the $\kappa$ fields, followed by
26 a closing line that says `\.{\\donewithpage}' followed by the page number
27 copied from the original identification line.
28
29 Exception: In the case of a ``custom'' identifier, \.{\\?\{$\kappa$\}}
30 takes the alternative form \.{\$\\$\kappa$\ \$} instead.
31
32 We define limits on the number and size of mini-index entries that should
33 be plenty big enough.
34
35 @d max_key 30 /* greater than the length of the longest identifier */
36 @d max_size 100 /* greater than the length of the longest mini-index entry */
37 @d max_items 300 /* the maximum number of items in a single mini-index */
38
39 @ Here's the layout of the \Cee\ program:
40
41 @d abort(c,m) { fprintf(stderr,"%s!\n%s",m,buf); return c; }
42 @c
43 #include "stdio.h"
44 #include "strings.h"
45 #include "ctype.h"
46 @#
47 typedef struct {
48 char key[max_key];
49 char entry[max_size];
50 } item;
51 item items[max_items]; /* all items of current group */
52 item *sorted[max_items]; /* pointers to items in alphabetic order */
53 char cur_page[10]; /* page number, as a string */
54 char buf[max_size]; /* current line of input */
55 char *input_status; /* |NULL| if end of input reached, else |buf| */
56 @#
57 main()
58 {
59 register char *p,*q;
60 register int n; /* current number of items */
61 register item *x, **y;
62 input_status=fgets(buf,max_size,stdin);
63 while (input_status) {
64 @<Check that |buf| contains a valid page-number line@>;
65 @<Read and sort additional lines, until |buf| terminates a group@>;
66 @<Output the current group@>;
67 }
68 return 0; /* normal exit */
69 }
70
71 @ @<Check that |buf| contains a valid page-number line@>=
72 if (*buf!='!') abort(-1,"missing '!'");
73 if (strlen(buf+1)>11) abort (-2,"page number too long");
74 for (p=buf+1,q=cur_page;*p!='\n';p++) *q++=*p;
75 *q='\0';
76
77 @ @<Read and sort additional lines, until |buf| terminates a group@>=
78 n=0;
79 while (1) {
80 input_status=fgets(buf,max_size,stdin);
81 if (input_status==NULL || *buf!='+') break;
82 x=&items[n];
83 @<Copy |buf| to item |x|@>;
84 @<Sort the new item into its proper place@>;
85 if (++n>max_items) abort(-11,"too many lines in group");
86 }
87
88 @ @<Output the current group@>=
89 { register int k;
90 for (y=sorted;y<sorted+n;y++) printf("%s\n",(*y)->entry);
91 printf("\\donewithpage%s\n",cur_page);
92 }
93
94 @* Sorting. We convert the key to lowercase as we copy it,
95 and we omit backslashes. We also convert \.{\_} to \.{\ }.
96 Then \.{\\\_} will be alphabetically less
97 than alphabetic letters, as desired.
98
99 @<Copy |buf|...@>=
100 if (*(buf+1)!=' ') abort(-3,"missing blank after +");
101 @<Scan past $\alpha$@>;
102 if (*p!=' ') abort(-4,"missing blank after alpha");
103 if (*(p+1)=='$') @<Process a custom-formatted identifier@>@;
104 else {
105 if (*(p+1)!='\\') abort(-5,"missing backslash");
106 if (!*(p+2)) abort(-6,"missing control code");
107 if (*(p+3)!='{') abort(-7,"missing left brace");
108 for (p+=4,q=x->key;*p!='}'&&*p;p++) {
109 if (*p!='\\') {
110 if (isupper(*p)) *q++=*p+('a'-'A');
111 else if (*p=='_') *q++=' ';
112 else *q++=*p;
113 }
114 }
115 if (*p!='}') abort(-8,"missing right brace");
116 }
117 if (q>=&x->key[max_key]) abort(-9,"key too long");
118 *q='\0';
119 @<Copy the buffer to |x->entry|@>;
120 if (p==buf+max_size-1) abort(-10,"entry too long");
121 *(q-1)='\0';
122
123 @ @<Process a custom...@>=
124 {
125 if (*(p+2)!='\\') abort(-11,"missing custom backlash");
126 for (p+=3,q=x->key;*p!=' '&&*p;p++) {
127 if (isupper(*p)) *q++=*p+('a'-'A');
128 else *q++=*p;
129 }
130 if (*p!=' ') abort(-12,"missing custom space");
131 if (*(p+1)!='$') abort(-13,"missing custom dollarsign");
132 }
133
134 @ @<Sort...@>=
135 for (y=&sorted[n];
136 y>&sorted[0] && strcmp((*(y-1))->key,x->key)>0;
137 y--)
138 *y=*(y-1);
139 *y=x;
140
141 @*A bugfix.
142 The program specification had a subtle bug: There are cases where
143 $\alpha$ includes spaces that should be removed in the output.
144
145 These cases occur when a space occurs after an odd number of doublequote
146 characters. Ergo, the following routine replaced a simpler original loop.
147
148 @<Scan past $\alpha$@>=
149 { register int toggle=0;
150 for (p=buf+2;(*p!=' '||toggle)&&*p;p++)
151 if (*p=='"') toggle^=1;
152 }
153
154 @ A corresponding change to the copying loop is also needed.
155
156 @<Copy the buffer to |x->entry|@>=
157 { register int toggle=0;
158 for (p=buf+2,q=x->entry;(*p!=' '||toggle)&&*p;p++) {
159 if (*p=='"') toggle^=1;
160 if (*p!=' ') *q++=*p;
161 }
162 for (;*p;p++) *q++=*p;
163 }
164