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
|
/*
*
* This file is part of
* MakeIndex - A formatter and format independent index processor
*
* Copyright (C) 1998-2012 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 "qsort.h"
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
static long idx_gc;
static int check_mixsym (const char *x, const char *y);
static int compare (const void *va, const void *vb);
static int compare_one (const char *x, const char *y);
static int compare_page (const FIELD_PTR *a, const FIELD_PTR *b);
static int compare_string (const unsigned char *a, const unsigned char *b);
static int new_strcmp (const unsigned char *a, const unsigned char *b,
int option);
void
sort_idx(void)
{
#ifdef HAVE_SETLOCALE
char *prev_locale;
#endif
MESSAGE("Sorting entries...");
#ifdef HAVE_SETLOCALE
prev_locale = setlocale(LC_COLLATE, NULL);
setlocale(LC_COLLATE, "");
#endif
idx_dc = 0;
idx_gc = 0L;
qqsort(idx_key, idx_gt, sizeof(FIELD_PTR), compare);
#ifdef HAVE_SETLOCALE
setlocale(LC_COLLATE, prev_locale);
#endif
MESSAGE1("done (%ld comparisons).\n", idx_gc);
}
static int
compare(const void *va, const void *vb)
{
const FIELD_PTR *a = va;
const FIELD_PTR *b = vb;
int i;
int dif;
idx_gc++;
IDX_DOT(CMP_MAX);
for (i = 0; i < FIELD_MAX; i++) {
/* compare the sort fields */
if ((dif = compare_one((*a)->sf[i], (*b)->sf[i])) != 0)
break;
/* compare the actual fields */
if ((dif = compare_one((*a)->af[i], (*b)->af[i])) != 0)
break;
}
/* both key aggregates are identical, compare page numbers */
if (i == FIELD_MAX)
dif = compare_page(a, b);
return (dif);
}
static int
compare_one(const char *x, const char *y)
{
int m;
int n;
if ((x[0] == NUL) && (y[0] == NUL))
return (0);
if (x[0] == NUL)
return (-1);
if (y[0] == NUL)
return (1);
m = group_type(x);
n = group_type(y);
/* both pure digits */
if ((m >= 0) && (n >= 0))
return (m - n);
/* x digit, y non-digit */
if (m >= 0) {
if (german_sort)
return (1);
else
return ((n == -1) ? 1 : -1);
}
/* x non-digit, y digit */
if (n >= 0) {
if (german_sort)
return (-1);
else
return ((m == -1) ? -1 : 1);
}
/* strings started with a symbol (including digit) */
if ((m == SYMBOL) && (n == SYMBOL))
return (check_mixsym(x, y));
/* x symbol, y non-symbol */
if (m == SYMBOL)
return (-1);
/* x non-symbol, y symbol */
if (n == SYMBOL)
return (1);
/* strings with a leading letter, the ALPHA type */
return (compare_string((const unsigned char*)x, (const unsigned char*)y));
}
static int
check_mixsym(const char *x, const char *y)
{
int m;
int n;
m = ISDIGIT(x[0]);
n = ISDIGIT(y[0]);
if (m && !n)
return (1);
if (!m && n)
return (-1);
return (locale_sort ? strcoll(x, y) : strcmp(x, y));
}
static int
compare_string(const unsigned char *a, const unsigned char *b)
{
int i = 0;
int j = 0;
int al;
int bl;
if (locale_sort) return strcoll((const char *)a, (const char *)b);
while ((a[i] != NUL) || (b[j] != NUL)) {
if (a[i] == NUL)
return (-1);
if (b[j] == NUL)
return (1);
if (letter_ordering) {
if (a[i] == SPC)
i++;
if (b[j] == SPC)
j++;
}
al = TOLOWER(a[i]);
bl = TOLOWER(b[j]);
if (al != bl)
return (al - bl);
i++;
j++;
}
if (german_sort)
return (new_strcmp(a, b, GERMAN));
else
return (strcmp((const char*)a, (const char*)b));
}
static int
compare_page(const FIELD_PTR *a, const FIELD_PTR *b)
{
int m = 0;
short i = 0;
while ((i < (*a)->count) && (i < (*b)->count) &&
((m = (*a)->npg[i] - (*b)->npg[i]) == 0))
{
i++;
}
if (m == 0)
{ /* common leading page numbers match */
if ((i == (*a)->count) && (i == (*b)->count))
{ /* all page numbers match */
/***********************************************************
We have identical entries, except possibly in encap fields.
The ordering is tricky here. Consider the following input
sequence of index names, encaps, and page numbers:
foo|( 2
foo|) 6
foo|( 6
foo|) 10
This might legimately occur when a page range ends, and
subsequently, a new range starts, on the same page. If we
just order by range_open and range_close (here, parens),
then we will produce
foo|( 2
foo|( 6
foo|) 6
foo|) 10
This will later generate the index entry
foo, 2--6, \({6}, 10
which is not only wrong, but has also introduced an illegal
LaTeX macro, \({6}, because the merging step treated this
like a \see{6} entry.
The solution is to preserve the original input order, which
we can do by treating range_open and range_close as equal,
and then ordering by input line number. This will then
generate the correct index entry
foo, 2--10
Ordering inconsistencies from missing range open or close
entries, or mixing roman and arabic page numbers, will be
detected later.
***********************************************************/
#define isrange(c) ( ((c) == idx_ropen) || ((c) == idx_rclose) )
/* Order two range values by input line number */
if (isrange(*(*a)->encap) && isrange(*(*b)->encap))
m = (*a)->lc - (*b)->lc;
/* Handle identical encap fields; neither is a range delimiter */
else if (STREQ((*a)->encap, (*b)->encap))
{
/* If neither are yet marked duplicate, mark the second
of them to be ignored. */
if (((*a)->type != DUPLICATE) &&
((*b)->type != DUPLICATE))
(*b)->type = DUPLICATE;
/* leave m == 0 to show equality */
}
/* Encap fields differ: only one may be a range delimiter, */
/* or else neither of them is. If either of them is a range */
/* delimiter, order by input line number; otherwise, order */
/* by name. */
else
{
if ( isrange(*(*a)->encap) || isrange(*(*b)->encap) )
m = (*a)->lc - (*b)->lc; /* order by input line number */
else /* order non-range items by */
/* their encap strings */
m = compare_string((const unsigned char*)((*a)->encap),
(const unsigned char*)((*b)->encap));
}
}
else if ((i == (*a)->count) && (i < (*b)->count))
m = -1;
else if ((i < (*a)->count) && (i == (*b)->count))
m = 1;
}
return (m);
}
static int
new_strcmp(const unsigned char *s1, const unsigned char *s2, int option)
{
int i;
i = 0;
while (s1[i] == s2[i])
if (s1[i++] == NUL)
return (0);
if (option) /* ASCII */
return (isupper(s1[i]) ? -1 : 1);
else /* GERMAN */
return (isupper(s1[i]) ? 1 : -1);
}
|