Debian Codegraph @

texk/makeindexk/mkind.c

raw
1 /*
2 *
3 * This file is part of
4 * MakeIndex - A formatter and format independent index processor
5 *
6 * Copyright (C) 1998-2012 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 #define MKIND_C 1
29 #include "mkind.h"
30 #undef MKIND_C
31
32 int letter_ordering = FALSE;
33 int compress_blanks = FALSE;
34 int merge_page = TRUE;
35 int init_page = FALSE;
36 int even_odd = -1;
37 int verbose = TRUE;
38 int german_sort = FALSE;
39 int thai_sort = FALSE;
40 int locale_sort = FALSE;
41 int fn_no = -1; /* total number of files */
42 int idx_dot = TRUE; /* flag which shows dot in ilg being
43 * active */
44 int idx_tt = 0; /* total entry count (all files) */
45 int idx_et = 0; /* erroneous entry count (all files) */
46 int idx_gt = 0; /* good entry count (all files) */
47
48 FIELD_PTR *idx_key;
49 FILE *log_fp;
50 FILE *sty_fp;
51 FILE *idx_fp;
52 FILE *ind_fp;
53 FILE *ilg_fp;
54
55 const char *pgm_fn;
56 char sty_fn[STRING_MAX];
57 const char *idx_fn;
58 char ind[STRING_MAX];
59 const char *ind_fn;
60 char ilg[STRING_MAX];
61 const char *ilg_fn;
62 char pageno[NUMBER_MAX];
63
64 static char log_fn[STRING_MAX];
65 static char base[STRING_MAX];
66 static int need_version = TRUE;
67
68 static void check_all (char *fn,int ind_given,int ilg_given,
69 int log_given);
70 static void check_idx (char *fn,int open_fn);
71 static void find_pageno (void);
72 static void open_sty (char *fn);
73 static void prepare_idx (void);
74 static void process_idx (char * *fn,int use_stdin,int sty_given,
75 int ind_given,int ilg_given,int log_given);
76
77 #ifdef DEBUG
78 long totmem = 0L; /* for debugging memory usage */
79 #endif /* DEBUG */
80
81 /* |mk_getc|: accept either Unix or Windows line endings */
82
83 static int lookahead = -2; /* because you can't ungetc(EOF) */
84
85 int
86 mk_getc(FILE *stream)
87 {
88 int ch = (lookahead != -2 ? lookahead : getc(stream));
89 lookahead = (ch == '\r' ? getc(stream) : -2);
90 if (lookahead == LFD) {
91 ch = lookahead;
92 lookahead = -2;
93 }
94 return ch;
95 }
96
97 int
98 main(int argc, char *argv[])
99 {
100 char *fns[ARRAY_MAX];
101 char *ap;
102 int use_stdin = FALSE;
103 int sty_given = FALSE;
104 int ind_given = FALSE;
105 int ilg_given = FALSE;
106 int log_given = FALSE;
107
108 #if USE_KPATHSEA
109 kpse_set_program_name (argv[0], "makeindex");
110 pgm_fn = kpse_program_name;
111 #else
112 /* determine program name */
113 pgm_fn = strrchr(*argv, DIR_DELIM);
114 #ifdef ALT_DIR_DELIM
115 {
116 char *alt_delim = strrchr(*argv, ALT_DIR_DELIM);
117
118 if (pgm_fn == NULL || (alt_delim != NULL && alt_delim > pgm_fn))
119 pgm_fn = alt_delim;
120 }
121 #endif
122 if (pgm_fn == NULL)
123 pgm_fn = *argv;
124 else
125 pgm_fn++;
126 #endif /* USE_KPATHSEA */
127
128 /* process command line options */
129 while (--argc > 0) {
130 if (**++argv == SW_PREFIX) {
131 if (*(*argv + 1) == NUL)
132 break;
133 for (ap = ++*argv; *ap != NUL; ap++)
134 switch (*ap) {
135 /* use standard input */
136 case 'i':
137 use_stdin = TRUE;
138 break;
139
140 /* enable letter ordering */
141 case 'l':
142 letter_ordering = TRUE;
143 break;
144
145 /* disable range merge */
146 case 'r':
147 merge_page = FALSE;
148 break;
149
150 /* supress progress message -- quiet mode */
151 case 'q':
152 verbose = FALSE;
153 break;
154
155 /* compress blanks */
156 case 'c':
157 compress_blanks = TRUE;
158 break;
159
160 /* style file */
161 case 's':
162 argc--;
163 if (argc <= 0)
164 FATAL("Expected -s <stylefile>\n");
165 open_sty(*++argv);
166 sty_given = TRUE;
167 break;
168
169 /* output index file name */
170 case 'o':
171 argc--;
172 if (argc <= 0)
173 FATAL("Expected -o <ind>\n");
174 ind_fn = *++argv;
175 ind_given = TRUE;
176 break;
177
178 /* transcript file name */
179 case 't':
180 argc--;
181 if (argc <= 0)
182 FATAL("Expected -t <logfile>\n");
183 ilg_fn = *++argv;
184 ilg_given = TRUE;
185 break;
186
187 /* initial page */
188 case 'p':
189 argc--;
190 if (argc <= 0)
191 FATAL("Expected -p <num>\n");
192 if (strlen(*++argv) >= sizeof(pageno))
193 FATAL("Page number too high\n");
194 strcpy(pageno, *argv);
195 init_page = TRUE;
196 if (STREQ(pageno, EVEN)) {
197 log_given = TRUE;
198 even_odd = 2;
199 } else if (STREQ(pageno, ODD)) {
200 log_given = TRUE;
201 even_odd = 1;
202 } else if (STREQ(pageno, ANY)) {
203 log_given = TRUE;
204 even_odd = 0;
205 }
206 break;
207
208 /* enable german sort */
209 case 'g':
210 german_sort = TRUE;
211 break;
212
213 #ifdef HAVE_SETLOCALE
214 /* enable locale-based sort */
215 case 'L':
216 locale_sort = TRUE;
217 break;
218
219 /* enable Thai sort */
220 case 'T':
221 thai_sort = locale_sort = TRUE;
222 break;
223 #endif
224
225 /* bad option */
226 default:
227 FATAL1("Unknown option -%c.\n", *ap);
228 break;
229 }
230 } else {
231 if (fn_no < ARRAY_MAX) {
232 check_idx(*argv, FALSE);
233 fns[++fn_no] = *argv;
234 } else {
235 FATAL1("Too many input files (max %d).\n", ARRAY_MAX);
236 }
237 }
238 }
239
240 if (fn_no == 0 && !sty_given)
241 {
242 char tmp[STRING_MAX + 5];
243
244 /* base set by last call to check_idx */
245 snprintf (tmp, sizeof(tmp), "%s%s", base, INDEX_STY);
246 if (0 == access(tmp, R_OK)) {
247 open_sty (tmp);
248 sty_given = TRUE;
249 }
250 }
251
252 process_idx(fns, use_stdin, sty_given, ind_given, ilg_given, log_given);
253 idx_gt = idx_tt - idx_et;
254 ALL_DONE;
255 if (idx_gt > 0) {
256 prepare_idx();
257 sort_idx();
258 gen_ind();
259 MESSAGE1("Output written in %s.\n", ind_fn);
260 } else
261 MESSAGE1("Nothing written in %s.\n", ind_fn);
262
263 MESSAGE1("Transcript written in %s.\n", ilg_fn);
264 CLOSE(ind_fp);
265 CLOSE(ilg_fp);
266 EXIT(0);
267
268 return (0); /* never executed--avoids complaints */
269 /* about no return value */
270 }
271
272
273 static void
274 prepare_idx(void)
275 {
276 NODE_PTR ptr = head;
277 int i = 0;
278
279 #ifdef DEBUG
280 totmem += idx_gt * sizeof(FIELD_PTR);
281 (void)fprintf(stderr,"prepare_idx(): calloc(%d,%d)\ttotmem = %ld\n",
282 idx_gt,sizeof(FIELD_PTR),totmem);
283 #endif /* DEBUG */
284
285 if (head == (NODE_PTR)NULL)
286 FATAL("No valid index entries collected.\n");
287
288 if ((idx_key = (FIELD_PTR *) calloc(idx_gt, sizeof(FIELD_PTR))) == NULL) {
289 FATAL("Not enough core...abort.\n");
290 }
291 for (i = 0; i < idx_gt; i++) {
292 idx_key[i] = &(ptr->data);
293 ptr = ptr->next;
294 }
295 }
296
297
298 static void
299 process_idx(char *fn[], int use_stdin, int sty_given, int ind_given,
300 int ilg_given, int log_given)
301 {
302 int i;
303
304 if (fn_no == -1)
305 /* use stdin if no input files specified */
306 use_stdin = TRUE;
307 else {
308 check_all(fn[0], ind_given, ilg_given, log_given);
309 PUT_VERSION;
310 if (sty_given)
311 scan_sty();
312 if (german_sort && (idx_quote == '"'))
313 FATAL1("Option -g invalid, quote character must be different from '%c'.\n",
314 '"');
315 scan_idx();
316 ind_given = TRUE;
317 ilg_given = TRUE;
318 for (i = 1; i <= fn_no; i++) {
319 check_idx(fn[i], TRUE);
320 scan_idx();
321 }
322 }
323
324 if (use_stdin) {
325 idx_fn = "stdin";
326 idx_fp = stdin;
327 #ifdef WIN32
328 setmode(fileno(stdin), _O_BINARY);
329 #endif
330
331 if (ind_given) {
332 if (!kpse_out_name_ok(ind_fn) ||
333 (!ind_fp && ((ind_fp = OPEN_OUT(ind_fn)) == NULL)))
334 FATAL1("Can't create output index file %s.\n", ind_fn);
335 } else {
336 ind_fn = "stdout";
337 ind_fp = stdout;
338 #ifdef WIN32
339 setmode(fileno(stdout), _O_BINARY);
340 #endif
341 }
342
343 if (ilg_given) {
344 if (!kpse_out_name_ok(ilg_fn) ||
345 (!ilg_fp && ((ilg_fp = OPEN_OUT(ilg_fn)) == NULL)))
346 FATAL1("Can't create transcript file %s.\n", ilg_fn);
347 } else {
348 ilg_fn = "stderr";
349 ilg_fp = stderr;
350 /* This is already equivalent to verbose mode... */
351 verbose = FALSE;
352 }
353
354 if ((fn_no == -1) && (sty_given))
355 scan_sty();
356 if (german_sort && (idx_quote == '"'))
357 FATAL1("Option -g ignored, quote character must be different from '%c'.\n",
358 '"');
359
360 if (need_version) {
361 PUT_VERSION;
362 }
363 scan_idx();
364 fn_no++;
365 }
366 }
367
368
369 static void
370 check_idx(char *fn, int open_fn)
371 {
372 char *ptr = fn;
373 char *ext;
374 int with_ext = FALSE;
375 int i = 0;
376
377 ext = strrchr(fn, EXT_DELIM);
378 if ((ext != NULL) && (ext != fn) && (strchr(ext + 1, DIR_DELIM) == NULL)
379 #ifdef ALT_DIR_DELIM
380 && (strchr(ext + 1, ALT_DIR_DELIM) == NULL)
381 #endif
382 ) {
383 with_ext = TRUE;
384 while ((ptr != ext) && (i < STRING_MAX))
385 base[i++] = *ptr++;
386 } else
387 while ((*ptr != NUL) && (i < STRING_MAX))
388 base[i++] = *ptr++;
389
390 if (i < STRING_MAX)
391 base[i] = NUL;
392 else
393 FATAL2("Index file name %s too long (max %d).\n",
394 base, STRING_MAX);
395
396 idx_fn = fn;
397
398 if ( ( open_fn &&
399 (!kpse_in_name_ok(idx_fn) ||
400 (idx_fp = OPEN_IN(idx_fn)) == NULL)
401 ) ||
402 ((!open_fn) && (access(idx_fn, R_OK) != 0))) {
403 if (with_ext) {
404 FATAL1("Input index file %s not found.\n", idx_fn);
405 } else {
406 char *tmp_fn;
407
408 #ifdef DEBUG
409 totmem += STRING_MAX;
410 (void)fprintf(stderr,"check_idx()-2: malloc(%d)\ttotmem = %ld\n",
411 STRING_MAX,totmem);
412 #endif /* DEBUG */
413
414 if ((tmp_fn = malloc(STRING_MAX+5)) == NULL)
415 FATAL("Not enough core...abort.\n");
416 snprintf(tmp_fn, STRING_MAX+5, "%s%s", base, INDEX_IDX);
417 idx_fn = tmp_fn;
418 if ((open_fn &&
419 (!kpse_in_name_ok(idx_fn) ||
420 (idx_fp = OPEN_IN(idx_fn)) == NULL)
421 ) ||
422 ((!open_fn) && (access(idx_fn, R_OK) != 0))) {
423 FATAL2("Couldn't find input index file %s nor %s.\n", base,
424 idx_fn);
425 }
426 }
427 }
428 }
429
430
431 static void
432 check_all(char *fn, int ind_given, int ilg_given, int log_given)
433 {
434 check_idx(fn, TRUE);
435
436 /* index output file */
437 if (!ind_given) {
438 snprintf(ind, sizeof(ind), "%s%s", base, INDEX_IND);
439 ind_fn = ind;
440 }
441 if (!kpse_out_name_ok(ind_fn) ||
442 ((ind_fp = OPEN_OUT(ind_fn)) == NULL))
443 FATAL1("Can't create output index file %s.\n", ind_fn);
444
445 /* index transcript file */
446 if (!ilg_given) {
447 snprintf(ilg, sizeof(ilg), "%s%s", base, INDEX_ILG);
448 ilg_fn = ilg;
449 }
450 if (!kpse_out_name_ok(ilg_fn) ||
451 ((ilg_fp = OPEN_OUT(ilg_fn)) == NULL))
452 FATAL1("Can't create transcript file %s.\n", ilg_fn);
453
454 if (log_given) {
455 snprintf(log_fn, sizeof(log_fn), "%s%s", base, INDEX_LOG);
456 if (!kpse_in_name_ok(log_fn) ||
457 (log_fp = OPEN_IN(log_fn)) == NULL) {
458 FATAL1("Source log file %s not found.\n", log_fn);
459 } else {
460 find_pageno();
461 CLOSE(log_fp);
462 }
463 }
464 }
465
466
467 static void
468 find_pageno(void)
469 {
470 int i = 0;
471 int p, c;
472
473 fseek(log_fp, -1L, 2);
474 p = GET_CHAR(log_fp);
475 fseek(log_fp, -2L, 1);
476 do {
477 c = p;
478 p = GET_CHAR(log_fp);
479 } while (!(((p == LSQ) && isdigit(c)) || (fseek(log_fp, -2L, 1) != 0)));
480 if (p == LSQ) {
481 while ((c = GET_CHAR(log_fp)) == SPC);
482 do {
483 pageno[i++] = (char) c;
484 c = GET_CHAR(log_fp);
485 } while (isdigit(c));
486 pageno[i] = NUL;
487 } else {
488 fprintf(ilg_fp, "Couldn't find any page number in %s...ignored\n",
489 log_fn);
490 init_page = FALSE;
491 }
492 }
493
494 static void
495 open_sty(char *fn)
496 {
497 #if USE_KPATHSEA
498 char *found;
499
500 if ((found = kpse_find_file (fn, kpse_ist_format, 1)) == NULL) {
501 FATAL1("Index style file %s not found.\n", fn);
502 } else {
503 if (strlen(found) >= sizeof(sty_fn)) {
504 FATAL1("Style file name %s too long.\n", found);
505 }
506 strcpy(sty_fn,found);
507 if (!kpse_in_name_ok(sty_fn) ||
508 (sty_fp = OPEN_IN(sty_fn)) == NULL) {
509 FATAL1("Could not open style file %s.\n", sty_fn);
510 }
511 }
512 #else
513 char *path;
514 char *ptr;
515 int i;
516 int len;
517
518 if ((path = getenv(STYLE_PATH)) == NULL) {
519 if (strlen(fn) >= sizeof(sty_fn)) {
520 FATAL1("Style file name %s too long.\n", fn);
521 }
522 /* style input path not defined */
523 strcpy(sty_fn, fn);
524 sty_fp = OPEN_IN(sty_fn);
525 } else {
526 len = ARRAY_MAX - strlen(fn) - 1;
527 while (*path != NUL) {
528 ptr = strchr(path, ENV_SEPAR);
529 i = 0;
530 if (ptr == (char*)NULL)
531 {
532 int j = strlen(path);
533
534 while (i < j)
535 sty_fn[i++] = *path++;
536 }
537 else
538 {
539 while ((path != ptr) && (i < len))
540 sty_fn[i++] = *path++;
541 }
542 if (i == len) {
543 FATAL2("Path %s too long (max %d).\n", sty_fn, ARRAY_MAX);
544 } else {
545 sty_fn[i++] = DIR_DELIM;
546 sty_fn[i] = NUL;
547 strcat(sty_fn, fn);
548 if ((sty_fp = OPEN_IN(sty_fn)) == NULL)
549 path++;
550 else
551 break;
552 }
553 }
554 }
555
556 if (sty_fp == NULL)
557 FATAL1("Index style file %s not found.\n", fn);
558 #endif
559 }
560
561
562 int
563 strtoint(char *str)
564 {
565 int val = 0;
566
567 while (*str != NUL) {
568 val = 10 * val + *str - 48;
569 str++;
570 }
571 return (val);
572 }