1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
/*
*
* This file is part of
* MakeIndex - A formatter and format independent index processor
*
* Copyright (C) 1998-2011 by the TeX Live project.
* Copyright (C) 1989 by Chen & Harrison International Systems, Inc.
* Copyright (C) 1988 by Olivetti Research Center
* Copyright (C) 1987 by Regents of the University of California
*
* Author:
* Pehong Chen
* Chen & Harrison International Systems, Inc.
* Palo Alto, California
* USA
*
* Contributors:
* Please refer to the CONTRIB file that comes with this release
* for a list of people who have contributed to this and/or previous
* release(s) of MakeIndex.
*
* All rights reserved by the copyright holders. See the copyright
* notice distributed with this software for a complete description of
* the conditions under which it is made available.
*
*/
#include "mkind.h"
#include "scanid.h"
#define CHECK_LENGTH() if (i > len_field) goto FIELD_OVERFLOW
int idx_lc; /* line count */
int idx_tc; /* total entry count */
int idx_ec; /* erroneous entry count */
int idx_dc; /* number of dots printed so far */
static int first_entry = TRUE;
static int comp_len;
static char key[ARGUMENT_MAX];
static char no[NUMBER_MAX];
NODE_PTR head;
NODE_PTR tail;
static void flush_to_eol (void);
static int make_key (void);
static void make_string (char **ppstr,int n);
static int scan_alpha_lower (char *no,int *npg,short *count);
static int scan_alpha_upper (char *no,int *npg,short *count);
static int scan_arabic (char *no,int *npg,short *count);
static int scan_arg1 (void);
static int scan_arg2 (void);
static int scan_field (int *n,char field[],int len_field,
int ck_level, int ck_encap,int ck_actual);
static int scan_key (struct KFIELD *data);
static int scan_no (char *no,int *npg,short *count,short *type);
static int scan_roman_lower (char *no,int *npg,short *count);
static int scan_roman_upper (char *no,int *npg,short *count);
static void search_quote (char **sort_key,char **actual_key);
void
scan_idx(void)
{
char keyword[ARRAY_MAX];
int c;
int i = 0;
int not_eof = TRUE;
int arg_count = -1;
MESSAGE1("Scanning input file %s...", idx_fn);
idx_lc = idx_tc = idx_ec = idx_dc = 0;
comp_len = strlen(page_comp);
while (not_eof) {
switch (c = GET_CHAR(idx_fp)) {
case EOF:
if (arg_count == 2) {
idx_lc++;
if (make_key())
IDX_DOT(DOT_MAX);
arg_count = -1;
} else
not_eof = FALSE;
break;
case LFD:
idx_lc++;
if (arg_count == 2) {
if (make_key())
IDX_DOT(DOT_MAX);
arg_count = -1;
} else if (arg_count > -1) {
IDX_ERROR("Missing arguments -- need two (premature LFD).\n");
arg_count = -1;
}
case TAB:
case SPC:
break;
default:
switch (arg_count) {
case -1:
i = 0;
keyword[i++] = (char) c;
arg_count++;
idx_tc++;
break;
case 0:
if (c == idx_aopen) {
arg_count++;
keyword[i] = NUL;
if (STREQ(keyword, idx_keyword)) {
if (!scan_arg1()) {
arg_count = -1;
}
} else {
IDX_SKIPLINE;
IDX_ERROR1("Unknown index keyword %s.\n", keyword);
}
} else {
if (i < ARRAY_MAX)
keyword[i++] = (char) c;
else {
IDX_SKIPLINE;
IDX_ERROR2("Index keyword %s too long (max %d).\n",
keyword, ARRAY_MAX);
}
}
break;
case 1:
if (c == idx_aopen) {
arg_count++;
if (!scan_arg2()) {
arg_count = -1;
}
} else {
IDX_SKIPLINE;
IDX_ERROR1(
"No opening delimiter for second argument (illegal character `%c').\n", c);
}
break;
case 2:
IDX_SKIPLINE;
IDX_ERROR1(
"No closing delimiter for second argument (illegal character `%c').\n", c);
break;
}
break;
}
}
/* fixup the total counts */
idx_tt += idx_tc;
idx_et += idx_ec;
DONE(idx_tc - idx_ec, "entries accepted", idx_ec, "rejected");
CLOSE(idx_fp);
}
static void
flush_to_eol(void)
{ /* flush to end-of-line, or end-of-file, whichever is first */
int a;
while ( ((a = GET_CHAR(idx_fp)) != LFD) && (a != EOF) )
/* NO-OP */;
}
static int
make_key(void)
{
NODE_PTR ptr;
int i;
static size_t len = sizeof(NODE);
/* allocate and initialize a node */
#ifdef DEBUG
totmem += len;
(void)fprintf(stderr,"make_key(): malloc(%d)\ttotmem = %ld\n",
len,totmem);
#endif /* DEBUG */
if ((ptr = malloc(len)) == NULL)
fprintf (stderr, "Not enough core...abort.\n");
memset(ptr, 0, len);
/* Initialize dangling pointers
* Note: it would be a nice work to redesign the code to use NULL for
* comparision ... nevertheless, using local "" in a global environment
* isn't the hit because the allocation of the keys with german_sort may
* crash with a nitpicking memory allocation (Linux, libc-5.4.46).
* <werner@suse.de>
*/
for (i = 0; i < FIELD_MAX; i++)
{
make_string(&(ptr->data.sf[i]), 1);
make_string(&(ptr->data.af[i]), 1);
}
make_string(&(ptr->data.encap), 1);
ptr->data.lpg[0] = NUL;
ptr->data.count = 0;
ptr->data.type = EMPTY;
/* process index key */
if (!scan_key(&(ptr->data)))
return (FALSE);
/* determine group type */
ptr->data.group = group_type(ptr->data.sf[0]);
/* process page number */
strcpy(ptr->data.lpg, no);
if (!scan_no(no, ptr->data.npg, &(ptr->data.count), &(ptr->data.type)))
return (FALSE);
if (first_entry) {
head = tail = ptr;
first_entry = FALSE;
} else {
tail->next = ptr;
tail = ptr;
}
ptr->data.lc = idx_lc;
ptr->data.fn = idx_fn;
tail->next = NULL;
return (TRUE);
}
static void
make_string(char **ppstr, int n)
{
if (*ppstr) /* Do NOT leaking here, <werner@suse.de> */
free(*ppstr);
/* Note: we have always allocate the correct amount of memory to
* avoid chrashes because the string length of the actual and
* sort key may differ at german_sort and strange indexentries
* <werner@suse.de>
*/
(*ppstr) = malloc(n);
if (!(*ppstr))
fprintf (stderr, "Not enough core...abort.\n");
memset(*ppstr, 0, n); /* Always zero anything, <werner@suse.de> */
}
static int
scan_key(FIELD_PTR data)
{
int i = 0; /* current level */
int n = 0; /* index to the key[] array */
int second_round = FALSE;
int last = FIELD_MAX - 1;
size_t len;
while (TRUE) {
if (key[n] == NUL)
break;
len = strlen(key);
if (key[n] == idx_encap)
{
n++;
make_string(&(data->encap), len + 1);
if (scan_field(&n, data->encap, len, FALSE, FALSE, FALSE))
break;
else
return (FALSE);
}
if (key[n] == idx_actual) {
n++;
make_string(&(data->af[i]), len + 1);
if (i == last)
{
if (!scan_field(&n, data->af[i], len, FALSE, TRUE, FALSE))
return (FALSE);
}
else
{
if (!scan_field(&n, data->af[i], len, TRUE, TRUE, FALSE))
return (FALSE);
}
} else {
/* Next nesting level */
if (second_round) {
i++;
n++;
}
make_string(&(data->sf[i]), len + 1);
if (i == last)
{
if (!scan_field(&n, data->sf[i], len, FALSE, TRUE, TRUE))
return (FALSE);
}
else
{
if (!scan_field(&n, data->sf[i], len, TRUE, TRUE, TRUE))
return (FALSE);
}
second_round = TRUE;
if (german_sort && strchr(data->sf[i], '"'))
{
make_string(&(data->af[i]),strlen(data->sf[i]) + 1);
search_quote(&(data->sf[i]), &(data->af[i]));
}
}
}
/* check for empty fields which shouldn't be empty */
if (*data->sf[0] == NUL) {
NULL_RTN;
}
for (i = 1; i < FIELD_MAX - 1; i++)
if ((*data->sf[i] == NUL) &&
((*data->af[i] != NUL) || (*data->sf[i + 1] != NUL))) {
NULL_RTN;
}
/* i == FIELD_MAX-1 */
if ((*data->sf[i] == NUL) && (*data->af[i] != NUL)) {
NULL_RTN;
}
return (TRUE);
}
static int
scan_field(int *n, char field[], int len_field, int ck_level, int ck_encap,
int ck_actual)
{
int i = 0;
int nbsh; /* backslash count */
if (compress_blanks && ((key[*n] == SPC) || (key[*n] == TAB)))
++* n;
while (TRUE) {
nbsh = 0;
while (key[*n] == idx_escape)
{
nbsh++;
field[i++] = key[*n];
CHECK_LENGTH();
++*n;
}
if (key[*n] == idx_quote)
{
if (nbsh % 2 == 0)
field[i++] = key[++*n];
else
field[i++] = key[*n];
CHECK_LENGTH();
}
else if ((ck_level && (key[*n] == idx_level)) ||
(ck_encap && (key[*n] == idx_encap)) ||
(ck_actual && (key[*n] == idx_actual)) ||
(key[*n] == NUL))
{
if ((i > 0) && compress_blanks && (field[i - 1] == SPC))
field[i - 1] = NUL;
else
field[i] = NUL;
return (TRUE);
} else {
field[i++] = key[*n];
CHECK_LENGTH();
if ((!ck_level) && (key[*n] == idx_level)) {
IDX_ERROR2("Extra `%c' at position %d of first argument.\n",
idx_level, *n + 1);
return (FALSE);
} else if ((!ck_encap) && (key[*n] == idx_encap)) {
IDX_ERROR2("Extra `%c' at position %d of first argument.\n",
idx_encap, *n + 1);
return (FALSE);
} else if ((!ck_actual) && (key[*n] == idx_actual)) {
IDX_ERROR2("Extra `%c' at position %d of first argument.\n",
idx_actual, *n + 1);
return (FALSE);
}
}
/* check if max field length is reached */
if (i > len_field)
{
FIELD_OVERFLOW:
if (!ck_encap) {
IDX_ERROR1("Encapsulator of page number too long (max. %d).\n",
len_field);
} else if (ck_actual) {
IDX_ERROR1("Index sort key too long (max. %d).\n", len_field);
} else {
IDX_ERROR1("Text of key entry too long (max. %d).\n", len_field);
}
return (FALSE);
}
++*n;
}
}
int
group_type(const char *str)
{
int i = 0;
while ((str[i] != NUL) && ISDIGIT(str[i]))
i++;
if (str[i] == NUL) {
sscanf(str, "%d", &i);
return (i);
} else if (ISSYMBOL(str[0]))
return (SYMBOL);
else
return (ALPHA);
}
static int
scan_no(char no[], int npg[], short *count, short *type)
{
int i = 1;
if (isdigit((unsigned char)no[0])) {
*type = ARAB;
if (!scan_arabic(no, npg, count))
return (FALSE);
/* simple heuristic to determine if a letter is Roman or Alpha */
} else if (IS_ROMAN_LOWER(no[0]) && (!IS_COMPOSITOR)) {
*type = ROML;
if (!scan_roman_lower(no, npg, count))
return (FALSE);
/* simple heuristic to determine if a letter is Roman or Alpha */
} else if (IS_ROMAN_UPPER(no[0]) &&
((no[0] == ROMAN_I) || (!IS_COMPOSITOR))) {
*type = ROMU;
if (!scan_roman_upper(no, npg, count))
return (FALSE);
} else if (IS_ALPHA_LOWER(no[0])) {
*type = ALPL;
if (!scan_alpha_lower(no, npg, count))
return (FALSE);
} else if (IS_ALPHA_UPPER(no[0])) {
*type = ALPU;
if (!scan_alpha_upper(no, npg, count))
return (FALSE);
} else {
IDX_ERROR1("Illegal page number %s.\n", no);
return (FALSE);
}
return (TRUE);
}
static int
scan_arabic(char no[], int npg[], short *count)
{
short i = 0;
char str[ARABIC_MAX+1]; /* space for trailing NUL */
while ((no[i] != NUL) && (i <= ARABIC_MAX) && (!IS_COMPOSITOR)) {
if (isdigit((unsigned char)no[i])) {
str[i] = no[i];
i++;
} else {
IDX_ERROR2("Illegal Arabic digit: position %d in %s.\n", i + 1, no);
return (FALSE);
}
}
if (i > ARABIC_MAX) {
IDX_ERROR2("Arabic page number %s too big (max %d digits).\n",
no, ARABIC_MAX);
return (FALSE);
}
str[i] = NUL;
ENTER(strtoint(str) + page_offset[ARAB]);
if (IS_COMPOSITOR)
return (scan_no(&no[i + comp_len], npg, count, &i));
else
return (TRUE);
}
static int
scan_roman_lower(char no[], int npg[], short *count)
{
short i = 0;
int inp = 0;
int prev = 0;
int the_new;
while ((no[i] != NUL) && (i < ROMAN_MAX) && (!IS_COMPOSITOR)) {
if ((IS_ROMAN_LOWER(no[i])) &&
((the_new = ROMAN_LOWER_VAL(no[i])) != 0)) {
if (prev == 0)
prev = the_new;
else {
if (prev < the_new) {
prev = the_new - prev;
the_new = 0;
}
inp += prev;
prev = the_new;
}
} else {
IDX_ERROR2("Illegal Roman number: position %d in %s.\n", i + 1, no);
return (FALSE);
}
i++;
}
if (i == ROMAN_MAX) {
IDX_ERROR2("Roman page number %s too big (max %d digits).\n",
no, ROMAN_MAX);
return (FALSE);
}
inp += prev;
ENTER(inp + page_offset[ROML]);
if (IS_COMPOSITOR)
return (scan_no(&no[i + comp_len], npg, count, &i));
else
return (TRUE);
}
static int
scan_roman_upper(char no[], int npg[], short *count)
{
short i = 0;
int inp = 0;
int prev = 0;
int the_new;
while ((no[i] != NUL) && (i < ROMAN_MAX) && (!IS_COMPOSITOR)) {
if ((IS_ROMAN_UPPER(no[i])) &&
((the_new = ROMAN_UPPER_VAL(no[i])) != 0)) {
if (prev == 0)
prev = the_new;
else {
if (prev < the_new) {
prev = the_new - prev;
the_new = 0;
}
inp += prev;
prev = the_new;
}
} else {
IDX_ERROR2("Illegal Roman number: position %d in %s.\n", i + 1, no);
return (FALSE);
}
i++;
}
if (i == ROMAN_MAX) {
IDX_ERROR2("Roman page number %s too big (max %d digits).\n",
no, ROMAN_MAX);
return (FALSE);
}
inp += prev;
ENTER(inp + page_offset[ROMU]);
if (IS_COMPOSITOR)
return (scan_no(&no[i + comp_len], npg, count, &i));
else
return (TRUE);
}
static int
scan_alpha_lower(char no[], int npg[], short *count)
{
short i;
ENTER(ALPHA_VAL(no[0]) + page_offset[ALPL]);
i = 1;
if (IS_COMPOSITOR)
return (scan_no(&no[comp_len + 1], npg, count, &i));
else
return (TRUE);
}
static int
scan_alpha_upper(char no[], int npg[], short *count)
{
short i;
ENTER(ALPHA_VAL(no[0]) + page_offset[ALPU]);
i = 1;
if (IS_COMPOSITOR)
return (scan_no(&no[comp_len + 1], npg, count, &i));
else
return (TRUE);
}
static int
scan_arg1(void)
{
int i = 0;
int n = 0; /* delimiter count */
int a;
if (compress_blanks)
while (((a = GET_CHAR(idx_fp)) == SPC) || (a == TAB));
else
a = GET_CHAR(idx_fp);
while ((i < ARGUMENT_MAX) && (a != EOF))
{
if ((a == idx_quote) || (a == idx_escape))
{ /* take next character literally */
key[i++] = (char) a; /* but preserve quote or escape */
a = GET_CHAR(idx_fp);
key[i++] = (char) a; /* save literal character */
}
else if (a == idx_aopen)
{ /* opening delimiters within the argument list */
key[i++] = (char) a;
n++;
}
else if (a == idx_aclose)
{
if (n == 0) /* end of argument */
{
if (compress_blanks && key[i - 1] == SPC)
key[i - 1] = NUL;
else
key[i] = NUL;
return (TRUE);
}
else /* nested delimiters */
{
key[i++] = (char) a;
n--;
}
}
else
{
switch (a)
{
case LFD:
idx_lc++;
IDX_ERROR("Incomplete first argument (premature LFD).\n");
return (FALSE);
case TAB:
case SPC:
/* compress successive SPC's to one SPC */
if (compress_blanks)
{
if ((i > 0) && (key[i - 1] != SPC) && (key[i - 1] != TAB))
key[i++] = SPC;
break;
}
default:
key[i++] = (char) a;
break;
}
}
a = GET_CHAR(idx_fp);
}
flush_to_eol(); /* Skip to end of line */
idx_lc++;
IDX_ERROR1("First argument too long (max %d).\n", ARGUMENT_MAX);
return (FALSE);
}
static int
scan_arg2(void)
{
int i = 0;
int a;
int hit_blank = FALSE;
while (((a = GET_CHAR(idx_fp)) == SPC) || (a == TAB));
while (i < NUMBER_MAX) {
if (a == idx_aclose) {
no[i] = NUL;
return (TRUE);
} else
switch (a) {
case LFD:
idx_lc++;
IDX_ERROR("Incomplete second argument (premature LFD).\n");
return (FALSE);
case TAB:
case SPC:
hit_blank = TRUE;
break;
default:
if (hit_blank) {
flush_to_eol(); /* Skip to end of line */
idx_lc++;
IDX_ERROR("Illegal space within numerals in second argument.\n");
return (FALSE);
}
no[i++] = (char) a;
break;
}
a = GET_CHAR(idx_fp);
}
flush_to_eol(); /* Skip to end of line */
idx_lc++;
IDX_ERROR1("Second argument too long (max %d).\n", NUMBER_MAX);
return (FALSE);
}
static void
search_quote(char **sort_key, char **actual_key)
{
char *ptr; /* pointer to sort_key */
const char *sort; /* contains sorting text */
int char_found = FALSE;
strcpy(*actual_key, *sort_key);
ptr = strchr(*sort_key, '"'); /* look for first double quote */
while (ptr != (char*)NULL)
{
sort = NULL;
switch (*(ptr + 1))
{ /* skip to umlaut or sharp S */
case 'a':
case 'A':
sort = isupper((unsigned char)*(ptr + 1)) ? "Ae" : "ae";
break;
case 'o':
case 'O':
sort = isupper((unsigned char)*(ptr + 1)) ? "Oe" : "oe";
break;
case 'u':
case 'U':
sort = isupper((unsigned char)*(ptr + 1)) ? "Ue" : "ue";
break;
case 's':
sort = "ss";
break;
default:
break;
}
if (sort != NULL)
{
char_found = TRUE;
*ptr = sort[0]; /* write to sort_key */
*(ptr + 1) = sort[1];
}
ptr = strchr(ptr + 1, '"'); /* next double quote */
}
if (!char_found) /* reset actual_key ? */
make_string(actual_key, 1);
return;
}
|