Debian Codegraph @

texk/makeindexk/genind.c

raw
1 /*
2 *
3 * This file is part of
4 * MakeIndex - A formatter and format independent index processor
5 *
6 * Copyright (C) 1998-2011 by the TeX Live project.
7 * Copyright (C) 1989 by Chen & Harrison International Systems, Inc.
8 * Copyright (C) 1988 by Olivetti Research Center
9 * Copyright (C) 1987 by Regents of the University of California
10 *
11 * Author:
12 * Pehong Chen
13 * Chen & Harrison International Systems, Inc.
14 * Palo Alto, California
15 * USA
16 *
17 * Contributors:
18 * Please refer to the CONTRIB file that comes with this release
19 * for a list of people who have contributed to this and/or previous
20 * release(s) of MakeIndex.
21 *
22 * All rights reserved by the copyright holders. See the copyright
23 * notice distributed with this software for a complete description of
24 * the conditions under which it is made available.
25 *
26 */
27
28 #include "mkind.h"
29 #include "genind.h"
30
31 #ifdef HAVE_LOCALE_H
32 #include <locale.h>
33 #endif
34
35 static FIELD_PTR curr = NULL;
36 static FIELD_PTR prev = NULL;
37 static FIELD_PTR begin = NULL;
38 static FIELD_PTR the_end = NULL;
39 static FIELD_PTR range_ptr;
40 static int level = 0;
41 static int prev_level = 0;
42 static char *encap = NULL;
43 static char *prev_encap = NULL;
44 static int in_range = FALSE;
45 static int encap_range = FALSE;
46 static char buff[2 * ARGUMENT_MAX];
47 static char line[2 * ARGUMENT_MAX]; /* output buffer */
48 static int ind_lc = 0; /* overall line count */
49 static int ind_ec = 0; /* erroneous line count */
50 static int ind_indent;
51
52 static void flush_line (int print);
53 static void insert_page (void);
54 static int make_entry (int n);
55 static void make_item (const char* term);
56 static unsigned char first_letter (char* term);
57 static void new_entry (void);
58 static void old_entry (void);
59 static int page_diff (struct KFIELD *a,struct KFIELD *b);
60 static void put_header (int let);
61 static void wrap_line (int print);
62
63 void
64 gen_ind(void)
65 {
66 int n;
67 int tmp_lc;
68
69 MESSAGE1("Generating output file %s...", ind_fn);
70 PUT(preamble);
71 ind_lc += prelen;
72 if (init_page)
73 insert_page();
74
75 /* reset counters for putting out dots */
76 idx_dc = 0;
77 for (n = 0; n < idx_gt; n++) {
78 if (idx_key[n]->type != DUPLICATE)
79 if (make_entry(n)) {
80 IDX_DOT(DOT_MAX);
81 }
82 }
83 tmp_lc = ind_lc;
84 if (in_range) {
85 curr = range_ptr;
86 IND_ERROR1("Unmatched range opening operator %c.\n", idx_ropen);
87 }
88 prev = curr;
89 flush_line(TRUE);
90 PUT(delim_t);
91 PUT(postamble);
92 tmp_lc = ind_lc + postlen;
93 if (ind_ec == 1) {
94 DONE(tmp_lc, "lines written", ind_ec, "warning");
95 } else {
96 DONE(tmp_lc, "lines written", ind_ec, "warnings");
97 }
98 }
99
100
101 static int
102 make_entry(int n)
103 {
104 int let;
105
106 /* determine current and previous pointer */
107 prev = curr;
108 curr = idx_key[n];
109 /* check if current entry is in range */
110 if ((*curr->encap == idx_ropen) || (*curr->encap == idx_rclose))
111 encap = &(curr->encap[1]);
112 else
113 encap = curr->encap;
114
115 /* determine the current nesting level */
116 if (n == 0) {
117 prev_level = level = 0;
118 let = *curr->sf[0];
119 put_header(let);
120 make_item(NIL);
121 } else {
122 prev_level = level;
123 for (level = 0; level < FIELD_MAX; level++)
124 if (STRNEQ(curr->sf[level], prev->sf[level]) ||
125 STRNEQ(curr->af[level], prev->af[level]))
126 break;
127 if (level < FIELD_MAX)
128 new_entry();
129
130 /* Repeat test from just below to see if we are already in an
131 open range. If so, we don't want to output anything. (It
132 ends up being output as an erroneous \(. See
133 tests/nested-range-bb.tex.) */
134 else if (! (*curr->encap == idx_ropen && in_range)) {
135 old_entry();
136 }
137 }
138
139 if (*curr->encap == idx_ropen)
140 if (in_range) {
141 IND_ERROR1("Extra range opening operator %c.\n", idx_ropen);
142 } else {
143 in_range = TRUE;
144 range_ptr = curr;
145 }
146 else if (*curr->encap == idx_rclose)
147 if (in_range) {
148 in_range = FALSE;
149 if (STRNEQ(&(curr->encap[1]), "") &&
150 STRNEQ(prev_encap, &(curr->encap[1]))) {
151 IND_ERROR1("Range closing operator has an inconsistent encapsulator %s.\n",
152 &(curr->encap[1]));
153 }
154 } else {
155 IND_ERROR1("Unmatched range closing operator %c.\n", idx_rclose);
156 }
157 else if ((*curr->encap != NUL) &&
158 STRNEQ(curr->encap, prev_encap) && in_range)
159 IND_ERROR1("Inconsistent page encapsulator %s within range.\n",
160 curr->encap);
161 return (1);
162 }
163
164
165 static void
166 make_item(const char *term)
167 {
168 int i;
169
170 if (level > prev_level) {
171 /* ascending level */
172 if (*curr->af[level] == NUL)
173 sprintf(line, "%s%s%s", term, item_u[level], curr->sf[level]);
174 else
175 sprintf(line, "%s%s%s", term, item_u[level], curr->af[level]);
176 ind_lc += ilen_u[level];
177 } else {
178 /* same or descending level */
179 if (*curr->af[level] == NUL)
180 sprintf(line, "%s%s%s", term, item_r[level], curr->sf[level]);
181 else
182 sprintf(line, "%s%s%s", term, item_r[level], curr->af[level]);
183 ind_lc += ilen_r[level];
184 }
185
186 i = level + 1;
187 while (i < FIELD_MAX && *curr->sf[i] != NUL) {
188 PUT(line);
189 if (*curr->af[i] == NUL)
190 sprintf(line, "%s%s", item_x[i], curr->sf[i]);
191 else
192 sprintf(line, "%s%s", item_x[i], curr->af[i]);
193 ind_lc += ilen_x[i];
194 level = i; /* Added at 2.11 <[email protected]> */
195 i++;
196 }
197
198 ind_indent = 0;
199 strcat(line, delim_p[level]);
200 SAVE;
201 }
202
203 static unsigned char
204 first_letter(char *term)
205 {
206 if (thai_sort)
207 return strchr("\340\341\342\343\344", term[0]) ? term[1] : term[0];
208
209 return TOLOWER(term[0]);
210 }
211
212 static void
213 new_entry(void)
214 {
215 int let = -1; /* see comment below */
216 FIELD_PTR ptr;
217 #ifdef HAVE_SETLOCALE
218 char *prev_locale;
219 prev_locale = setlocale(LC_CTYPE, NULL);
220 setlocale(LC_CTYPE, "");
221 #endif
222
223 if (in_range) {
224 ptr = curr;
225 curr = range_ptr;
226 IND_ERROR1("Unmatched range opening operator %c.\n", idx_ropen);
227 in_range = FALSE;
228 curr = ptr;
229 }
230 flush_line(TRUE);
231
232 /* beginning of a new group? */
233
234 if (((curr->group != ALPHA) && (curr->group != prev->group) &&
235 (prev->group == SYMBOL)) ||
236 ((curr->group == ALPHA) &&
237 ((unsigned char)(let = first_letter(curr->sf[0]))
238 != first_letter(prev->sf[0]))) ||
239 (german_sort &&
240 (curr->group != ALPHA) && (prev->group == ALPHA))) {
241 PUT(delim_t);
242 PUT(group_skip);
243 ind_lc += skiplen;
244 /* beginning of a new letter? */
245 /* Although we may use let unassigned here, it doesn't matter,
246 because put_header will not use its arg except in case
247 ALPHA, when it is assigned in the midst of condition above.
248 Let's not perturb the logic, just initialize it (above) to
249 avoid the warning. */
250 put_header(let);
251 make_item(NIL);
252 } else
253 make_item(delim_t);
254 #ifdef HAVE_SETLOCALE
255 setlocale(LC_CTYPE, prev_locale);
256 #endif
257 }
258
259
260 static void
261 old_entry(void)
262 {
263 int diff;
264
265 /* current entry identical to previous one: append pages */
266 diff = page_diff(the_end, curr);
267
268 if ((prev->type == curr->type) && (diff != -1) &&
269 (((diff == 0) && (prev_encap != NULL) && STREQ(encap, prev_encap)) ||
270 (merge_page && (diff == 1) &&
271 (prev_encap != NULL) && STREQ(encap, prev_encap)) ||
272 in_range)) {
273 the_end = curr;
274 /* extract in-range encaps out */
275 if (in_range &&
276 (*curr->encap != NUL) &&
277 (*curr->encap != idx_rclose) &&
278 STRNEQ(curr->encap, prev_encap)) {
279 sprintf(buff, "%s%s%s%s%s", encap_p, curr->encap,
280 encap_i, curr->lpg, encap_s);
281 wrap_line(FALSE);
282 }
283 if (in_range)
284 encap_range = TRUE;
285 } else {
286 flush_line(FALSE);
287 if ((diff == 0) && (prev->type == curr->type)) {
288 IND_ERROR(
289 "Conflicting entries: multiple encaps for the same page under same key.\n");
290 } else if (in_range && (prev->type != curr->type)) {
291 IND_ERROR(
292 "Illegal range formation: starting & ending pages are of different types.\n");
293 } else if (in_range && (diff == -1)) {
294 IND_ERROR(
295 "Illegal range formation: starting & ending pages cross chap/sec breaks.\n");
296 }
297 SAVE;
298 }
299 }
300
301
302 static int
303 page_diff(FIELD_PTR a,FIELD_PTR b)
304 {
305 short i;
306
307 if (a->count != b->count)
308 return (-1);
309 for (i = 0; i < a->count - 1; i++)
310 if (a->npg[i] != b->npg[i])
311 return (-1);
312 return (b->npg[b->count - 1] - a->npg[a->count - 1]);
313 }
314
315 static void
316 put_header(int let)
317 {
318 if (headings_flag)
319 {
320 PUT(heading_pre);
321 ind_lc += headprelen;
322 switch (curr->group)
323 {
324 case SYMBOL:
325 if (headings_flag > 0)
326 {
327 PUT(symhead_pos);
328 }
329 else
330 {
331 PUT(symhead_neg);
332 }
333 break;
334 case ALPHA:
335 if (headings_flag > 0)
336 {
337 let = TOUPPER(let);
338 PUTC(let);
339 }
340 else
341 {
342 let = TOLOWER(let);
343 PUTC(let);
344 }
345 break;
346 default:
347 if (headings_flag > 0)
348 {
349 PUT(numhead_pos);
350 }
351 else
352 {
353 PUT(numhead_neg);
354 }
355 break;
356 }
357 PUT(heading_suf);
358 ind_lc += headsuflen;
359 }
360 }
361
362
363 /* changes for 2.12 (May 20, 1993) by Julian Reschke ([email protected]):
364 Use keywords suffix_2p, suffix_3p or suffix_mp for one, two or
365 multiple page ranges (when defined) */
366
367 static void
368 flush_line(int print)
369 {
370 char tmp[sizeof(buff)];
371
372 if (page_diff(begin, the_end) != 0)
373 if (encap_range || (page_diff(begin, prev) > (*suffix_2p ? 0 : 1)))
374 {
375 int diff = page_diff(begin, the_end);
376
377 if ((diff == 1) && *suffix_2p)
378 sprintf(buff, "%s%s", begin->lpg, suffix_2p);
379 else if ((diff == 2) && *suffix_3p)
380 sprintf(buff, "%s%s", begin->lpg, suffix_3p);
381 else if ((diff >= 2) && *suffix_mp)
382 sprintf(buff, "%s%s", begin->lpg, suffix_mp);
383 else
384 sprintf(buff, "%s%s%s", begin->lpg, delim_r, the_end->lpg);
385
386 encap_range = FALSE;
387 }
388 else
389 sprintf(buff, "%s%s%s", begin->lpg, delim_n, the_end->lpg);
390 else
391 {
392 encap_range = FALSE; /* might be true from page range on same page */
393 strcpy(buff, begin->lpg);
394 }
395
396 if (*prev_encap != NUL)
397 {
398 strcpy(tmp, buff);
399 sprintf(buff, "%s%s%s%s%s",
400 encap_p, prev_encap, encap_i, tmp, encap_s);
401 }
402 wrap_line(print);
403 }
404
405 static void
406 wrap_line(int print)
407 {
408 int len;
409
410 len = strlen(line) + strlen(buff) + ind_indent;
411 if (print) {
412 if (len > linemax) {
413 PUTLN(line);
414 PUT(indent_space);
415 ind_indent = indent_length;
416 } else
417 PUT(line);
418 PUT(buff);
419 } else {
420 if (len > linemax) {
421 PUTLN(line);
422 sprintf(line, "%s%s%s", indent_space, buff, delim_n);
423 ind_indent = indent_length;
424 } else {
425 strcat(buff, delim_n);
426 strcat(line, buff);
427 }
428 }
429 }
430
431
432 static void
433 insert_page(void)
434 {
435 int i = 0;
436 int j = 0;
437 int page = 0;
438
439 if (even_odd >= 0) {
440 /* find the rightmost digit */
441 while (pageno[i++] != NUL);
442 j = --i;
443 /* find the leftmost digit */
444 while (isdigit((unsigned char)pageno[--i]) && i > 0);
445 if (!isdigit((unsigned char)pageno[i]))
446 i++;
447 /* convert page from literal to numeric */
448 page = strtoint(&pageno[i]) + 1;
449 /* check even-odd numbering */
450 if (((even_odd == 1) && (page % 2 == 0)) ||
451 ((even_odd == 2) && (page % 2 == 1)))
452 page++;
453 pageno[j + 1] = NUL;
454 /* convert page back to literal */
455 while (page >= 10) {
456 pageno[j--] = TOASCII(page % 10);
457 page = page / 10;
458 }
459 pageno[j] = TOASCII(page);
460 if (i < j) {
461 while (pageno[j] != NUL)
462 pageno[i++] = pageno[j++];
463 pageno[i] = NUL;
464 }
465 }
466 PUT(setpage_open);
467 PUT(pageno);
468 PUT(setpage_close);
469 ind_lc += setpagelen;
470 }