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
|
/* Copyright (C) 1995 Free Software Foundation, Inc.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ctype.h>
#include <errno.h>
#include <libintl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "localedef.h"
#include "hash.h"
/* Data structure for representing charmap database. */
struct charmap charmap_data;
/* Line number in charmap file. */
static unsigned int line_no;
/* Prototypes for local functions. */
static void read_prolog (FILE *infile);
static unsigned long read_body (FILE *infile);
/* Read complete table of symbolic names for character set from file. If
this file does not exist or is not readable a default file is tried.
If this also is not readable no character map is defined. */
void
charmap_read (const char *filename)
{
unsigned long max_char;
long path_max = pathconf (".", _PC_PATH_MAX);
char buf[path_max];
FILE *infile = NULL;
/* Initialize charmap data. */
charmap_data.codeset_name = NULL;
charmap_data.mb_cur_max = -1;
charmap_data.mb_cur_min = -1;
charmap_data.escape_char = '\\';
charmap_data.comment_char = '#';
if (filename != NULL)
{
strcpy (buf, filename);
infile = fopen (filename, "r");
if (infile == NULL && filename[0] != '/')
{
snprintf (buf, path_max, "%s/%s", CHARMAP_PATH, filename);
infile = fopen (buf, "r");
}
}
if (infile == NULL)
{
if (filename != NULL)
error (0, errno, gettext ("input file `%s' not found"), filename);
snprintf (buf, path_max, "%s/%s", CHARMAP_PATH, DEFAULT_CHARMAP);
infile = fopen (buf, "r");
if (infile == NULL)
error (4, errno, gettext ("input file `%s' not found"), filename);
}
charmap_data.filename = buf;
init_hash (&charmap_data.table, 500);
line_no = 0;
/* Read the prolog of the charmap file. */
read_prolog (infile);
/* Last works on the charmap tables global data. */
if (charmap_data.mb_cur_max == -1)
charmap_data.mb_cur_max = 1;
if (charmap_data.mb_cur_min == -1)
charmap_data.mb_cur_min = charmap_data.mb_cur_max;
if ((size_t) charmap_data.mb_cur_max > sizeof (long))
{
error (2, 0, gettext ("program limitation: for now only upto %Zu "
"bytes per character are allowed"), sizeof (long));
}
/* Now process all entries. */
max_char = read_body (infile);
/* We don't need the file anymore. */
fclose (infile);
/* Determine the optimal table size when using the simple modulo hashing
function. */
if (max_char >= 256)
{
int size;
/* Current best values, initialized to some never reached high value. */
int best_count = 10000;
int best_size = 10000;
int best_product = best_count * best_size;
/* Give warning. */
error (-1, 0, gettext ("computing character table size: this may take "
"a while"));
for (size = 256; size <= best_product; ++size)
{
/* Array with slot counters. */
int cnt[size];
/* Current character. */
int ch;
/* Maximal number of characters in any slot. */
int maxcnt = 0;
/* Product of current size and maximal count. */
int product = 0;
/* Iteration pointer through hashing table. */
char *ptr = NULL;
/* Initializes counters to zero. */
memset(cnt, 0, size * sizeof (int));
/* Iterate through whole hashing table. */
while (product < best_product
&& iterate_table (&charmap_data.table, (void **) &ptr,
(void **) &ch))
{
/* Increment slot counter. */
++cnt[ch % size];
/* Test for current maximum. */
if (cnt[ch % size] > maxcnt)
{
maxcnt = cnt[ch % size];
product = maxcnt * size;
}
}
if (product < best_product)
{
best_count = maxcnt;
best_size = size;
best_product = best_count * best_size;
}
}
charmap_data.hash_size = best_size;
charmap_data.hash_layers = best_count;
}
else
{
charmap_data.hash_size = 256;
charmap_data.hash_layers = 1;
}
}
#define SYNTAX_ERROR \
do { error (0, 0, gettext ("%s:%u: syntax error in charmap file"), \
charmap_data.filename, line_no); \
goto end_of_loop; } while (0)
/* Read the prolog of the charmap file until the line containing `CHARMAP'.
All possible entries are processed. */
static void
read_prolog (FILE *infile)
{
size_t bufsize = sysconf (_SC_LINE_MAX);
char buf[bufsize];
while (1)
{
char *cp = buf;
char len;
/* Read the next line. */
fgets (buf, bufsize, infile);
len = strlen (buf);
/* On EOF simply return. */
if (len == 0 || buf[len - 1] != '\n')
error (4, 0, gettext ("%s: unexpected end of file in charmap"),
charmap_data.filename);
/* This is the next line. */
++line_no;
/* Comments and empty lines are ignored. */
if (len == 1 || buf[0] == charmap_data.comment_char)
continue;
buf[len - 1] = '\0';
/* Throw away leading white spaces. This is not defined in POSIX.2
so don't do it if conformance is requested. */
if (!posix_conformance)
while (isspace (*cp))
++cp;
/* If `CHARMAP' is read the prolog is over. */
if (strncmp (cp, "CHARMAP", 7) == 0
&& (!posix_conformance || cp[7] == '\0'))
return;
/* Now it can be only one of special symbols defining the charmap
parameters. All are beginning with '<'. */
if (*cp != '<')
SYNTAX_ERROR;
++cp;
if (strncmp (cp, "code_set_name>", 14) == 0)
{
char *startp;
#define cp_to_arg(no,pred) \
cp += no; \
while (isspace (*cp)) \
++cp; \
if (*cp == '\0' || !pred (*cp)) \
SYNTAX_ERROR;
cp_to_arg (14,isgraph)
if (charmap_data.codeset_name != NULL)
{
error (0, 0, gettext ("%s:%u: duplicate code set name "
"specification"),
charmap_data.filename, line_no);
free (charmap_data.codeset_name);
}
startp = cp;
while (*cp != '\0' && isgraph (*cp) && !isspace (*cp))
++cp;
charmap_data.codeset_name = (char *) xmalloc (cp - startp + 1);
strncpy (startp, startp, cp - startp);
}
else if (strncmp (cp, "mb_cur_max>", 11) == 0)
{
int new_val;
cp_to_arg (11,isdigit)
if (charmap_data.mb_cur_max != -1)
error (0, 0,
gettext ("%s:%u: duplicate definition of mb_cur_max"),
charmap_data.filename, line_no);
new_val = (int) strtol (cp, &cp, posix_conformance ? 10 : 0);
if (new_val < 1)
error (0, 0, gettext ("%s:%u: illegal value for mb_cur_max: %d"),
charmap_data.filename, line_no, new_val);
else
charmap_data.mb_cur_max = new_val;
}
else if (strncmp (cp, "mb_cur_min>", 11) == 0)
{
int new_val;
cp_to_arg (11,isdigit)
if (charmap_data.mb_cur_max != -1)
error (0, 0,
gettext ("%s:%u: duplicate definition of mb_cur_min"),
charmap_data.filename, line_no);
new_val = (int) strtol (cp, &cp, posix_conformance ? 10 : 0);
if (new_val < 1)
error (0, 0, gettext ("%s:%u: illegal value for mb_cur_min: %d"),
charmap_data.filename, line_no, new_val);
else
charmap_data.mb_cur_min = new_val;
}
else if (strncmp (cp, "escape_char>", 12) == 0)
{
cp_to_arg (12, isgraph)
charmap_data.escape_char = *cp;
}
else if (strncmp (cp, "comment_char>", 13) == 0)
{
cp_to_arg (13, isgraph)
charmap_data.comment_char = *cp;
}
else
SYNTAX_ERROR;
end_of_loop:
}
}
#undef cp_to_arg
static unsigned long
read_body (FILE *infile)
{
unsigned long max_char = 0;
size_t bufsize = sysconf (_SC_LINE_MAX);
char buf[bufsize];
char name_str[bufsize / 2];
char code_str[bufsize / 2];
while (1)
{
char *cp = buf;
size_t len;
/* Read the next line. */
fgets (buf, bufsize, infile);
len = strlen (buf);
/* On EOF simply return. */
if (len == 0)
error (0, 0, gettext ("%s: `END CHARMAP' is missing"),
charmap_data.filename);
/* This is the next line. */
++line_no;
if (len == bufsize - 1)
{
error (0, 0, gettext ("%s:%u: line too long; use `getconf "
"LINE_MAX' to get the current maximum line"
"length"), charmap_data.filename, line_no);
do
{
fgets (buf, bufsize, infile);
len = strlen (buf);
}
while (len == bufsize - 1);
continue;
}
/* Comments and empty lines are ignored. */
if (len == 1 || buf[0] == charmap_data.comment_char)
continue;
buf[len - 1] = '\0';
/* Throw away leading white spaces. This is not defined in POSIX.2
so don't do it if conformance is requested. */
if (!posix_conformance)
while (isspace (*cp))
++cp;
if (*cp == '<')
{
char *end1p, *end2p, *start2p;
size_t cnt = 0;
unsigned long char_value = 0;
if (sscanf (cp + 1, "%s %s", name_str, code_str) != 2)
SYNTAX_ERROR;
end1p = cp = name_str;
while (*cp != '\0' && *cp != '>')
{
if (*cp == charmap_data.escape_char)
if (*++cp == '\0')
SYNTAX_ERROR;
*end1p++ = *cp++;
}
if (*cp == '\0')
/* No final '>'. Make error condition. */
end1p = name_str;
else
++cp;
*end1p = '\0';
if (*cp == '.' && *++cp == '.' && *++cp == '.' && *++cp == '<')
{
/* This might be the alternate form. */
start2p = end2p = ++cp;
while (*cp != '\0' && *cp != '>')
{
if (*cp == charmap_data.escape_char)
if (*++cp == '\0')
SYNTAX_ERROR;
*end2p = *cp++;
}
if (*cp == '\0')
/* NO final '>'. Make error condition. */
end2p = start2p;
else
++cp;
}
else
start2p = end2p = NULL;
if (end1p == name_str || (start2p != NULL && start2p != end2p)
|| *cp != '\0'
|| *code_str != charmap_data.escape_char)
SYNTAX_ERROR;
cp = code_str;
do
{
char *begin;
long val;
switch (*++cp)
{
case 'd':
val = strtol ((begin = cp + 1), &cp, 10);
break;
case 'x':
val = strtol ((begin = cp + 1), &cp, 16);
break;
default:
val = strtol ((begin = cp), &cp, 8);
break;
}
if (begin == cp)
SYNTAX_ERROR;
if (posix_conformance && cp - begin < 2)
error (0, 0, gettext ("%s:%u: byte constant has less than "
"two digits"),
charmap_data.filename, line_no);
if (val < 0 || val > 255)
{
error (0, 0, gettext ("%s:%u: character encoding must be "
"given in 8-bit bytes"),
charmap_data.filename, line_no);
goto end_of_loop;
}
if (cnt < (size_t) charmap_data.mb_cur_max)
{
if (cnt < sizeof (long)) /* FIXME */
char_value = (char_value << 8) | val;
}
else
{
error (0, 0, gettext ("%s:%u: number of bytes in character "
"definition exceeds `mb_cur_max'"),
charmap_data.filename, line_no);
break;
}
++cnt;
}
while (*cp == charmap_data.escape_char);
/* Ignore the rest of the line (comment). */
if (end2p == NULL)
{
if (insert_entry (&charmap_data.table, name_str,
end1p - name_str, (void *) char_value))
error (0, 0, gettext ("%s:%u: duplicate entry"),
charmap_data.filename, line_no);
max_char = MAX (max_char, char_value);
}
else
{
char *en1, *en2, *start1p;
long n1, n2, n;
start1p = name_str;
while (*start1p == *start2p && !isdigit (*start1p)
&& start1p < end1p)
++start1p, ++start2p;
n1 = strtol (start1p, &en1, 10);
n2 = strtol (start2p, &en2, 10);
if (en1 - start1p != en2 - start2p || en1 != end1p
|| en2 != end2p)
SYNTAX_ERROR;
if (n1 > n2)
error (0, 0, gettext ("%s:%u: starting character is bigger "
"than last"),
charmap_data.filename, line_no);
n = n1;
while (n <= n2)
{
snprintf(start1p, en1 - start1p, "%0*d", en1 - start1p, n);
if (insert_entry (&charmap_data.table, name_str,
en1 - name_str,
(void *) (char_value + n - n1)))
error (0, 0, gettext ("%s:%u: duplicate entry"),
charmap_data.filename, line_no);
max_char = MAX (max_char, char_value + n - n1);
++n;
}
}
}
else
{
if (strncmp (cp, "END CHARMAP", 11) == 0)
return max_char;
SYNTAX_ERROR;
}
end_of_loop:
}
return max_char;
}
/*
* Local Variables:
* mode:c
* c-basic-offset:2
* End:
*/
|