summaryrefslogtreecommitdiff
path: root/locale/locale-ctype.c
blob: e7a1e97960cea4c30cf9b16625526748c1b8e245 (plain)
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/* 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 <alloca.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <localeinfo.h>
#include <langinfo.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/uio.h>

#include "localedef.h"
#include "token.h"

/* Arrays representing ctype tables.  They must be initialized for the
   right size to hold the full charmap.  */
static u16 *ctype_b;
static i32 *names_b, *toupper_b, *tolower_b;

/* For accessing the element of the (possibly sparse) array we use this
   macro.  */
#define ELEM(arr, idx)							     \
  (arr)[({ int h = idx % charmap_data.hash_size; 			     \
           int n = 0;							     \
           while (n < charmap_data.hash_layers                               \
	          && names_b[n * charmap_data.hash_size + h] != idx)         \
           ++n;								     \
           if (n >= charmap_data.hash_layers)				     \
             error (6, 0, gettext ("internal error in %s, line %u"),         \
		    __FUNCTION__, __LINE__);                                 \
           n * charmap_data.hash_size + h; })]

/* The bit used for representing a special class.  */
#define BITPOS(class) ((class) - TOK_UPPER)
#define BIT(class) (1 << BITPOS (class))

/* Remember which class or conversion is already done.  */
static unsigned short class_done = 0;
static unsigned short toupper_done = 0;
static unsigned short tolower_done = 0;

#define SYNTAX_ERROR                                                         \
    error (0, 0, gettext ("%s:%Zd: syntax error in locale definition file"), \
			  locfile_data.filename, locfile_data.line_no);

 
/* Prototypes for local functions.  */
static void allocate_arrays (void);
static void set_class_defaults (void);
static int valid_char (int ch);


/* Read CTYPE category.  The initial token is given as a parameter.  */
void
ctype_input (int token)
{
  char *ptr;
  int len;

  /* If necessary allocate arrays.  */
  allocate_arrays ();

  while (token != TOK_END)
    {
      switch (token)
	{
	case TOK_UPPER:  case TOK_LOWER: case TOK_ALPHA: case TOK_DIGIT:
	case TOK_XDIGIT: case TOK_SPACE: case TOK_PRINT: case TOK_GRAPH:
	case TOK_BLANK:  case TOK_CNTRL: case TOK_PUNCT:
	  {
	    /* TAKE CARE: the order of the tokens in "token.h" determines
	       the bit used to indicate the membership in the class.  This
	       also has to correspond to the values used in <ctype.h>.  */
	    int bit = BIT (token);
	    int was_ell = 0;
	    int last = -1;

	    if ((class_done & bit) != 0)
	      {
		char tmp[len + 1];
		memcpy (tmp, ptr, len);
		tmp[len] = '\0';

		error (0, 0, gettext ("%s:%Zd: duplicate definiton of item "
				      "`%s' in category `LC_CTYPE'"),
		       locfile_data.filename, locfile_data.line_no, tmp);
	      }
	    class_done |= bit;

	    do
	      {
		token = xlocfile_lex (&ptr, &len);

		if (token == TOK_ENDOFLINE)
		  {
		    SYNTAX_ERROR;
		    break;
		  }

		if (token == TOK_ELLIPSIS)
		  {
		    if (was_ell != 0 || last < 0)
		      {
			error (0, 0, gettext ("%s:%Zd: illegal use of `...'"),
			       locfile_data.filename, locfile_data.line_no);
			break;
		      }
		    was_ell = 1;
		    continue;
		  }

		if (token != TOK_CHAR)
		  {
		    if (token != TOK_ILL_CHAR)
		      SYNTAX_ERROR;
		    was_ell = 0;
		    last = -1;
		    continue;
		  }

		if (len < 0 || !valid_char (len))
		  {
		    was_ell = 0;
		    last = -1;
		    continue;
		  }

		/* We have found a valid character.  Include it to
		   the class' bit set.  */
		if (was_ell == 0)
		  {
		    ELEM (ctype_b, len) |= bit;
		    last = len;
		  }
		else
		  {
		    int i;

		    if (last > len)
		      {
			error (0, 0, gettext ("%s:%Zd: lower bound of "
					      "ellipsis not smaller"),
			       locfile_data.filename, locfile_data.line_no);
			was_ell = 0;
			last = -1;
			continue;
		      }

		    for (i = last + 1; i <= len; ++i)
		      ELEM (ctype_b, i) |= bit;

		    last = -1;
		  }
		was_ell = 0;
	      }
	    while ((token = locfile_lex (&ptr, &len)) == TOK_CHAR
		   && len == ';');

	    /* Rest of the line should be empty.  */
	    ignore_to_eol (token, 0);
	  }
	  break;
	case TOK_TOUPPER: case TOK_TOLOWER:
	  {
	    int from;
	    int to = -1;
	    int is_upper = token == TOK_TOUPPER;

	    if (((is_upper ? toupper_done : tolower_done) & BIT (token)) != 0)
	      error (0, 0, gettext ("%s:%Zd: duplicate definition of item "
				    "`%s' in category `LC_CTYPE'"),
		     locfile_data.filename, locfile_data.line_no,
		     is_upper ? "toupper" : "tolower");
	    (is_upper ? toupper_done : tolower_done) |= BIT (token); 

	    do
	      {
		int ignore;

		token = xlocfile_lex (&ptr, &len);
		if (token != TOK_CHAR || len != '(')
		  {
		    SYNTAX_ERROR;
		    break;
		  }

		token = xlocfile_lex (&ptr, &len);
		if (token != TOK_CHAR && token != TOK_ILL_CHAR)
		  {
		    SYNTAX_ERROR;
		    break;
		  }
		from = len;
		ignore = token == TOK_ILL_CHAR;

		token = xlocfile_lex (&ptr, &len);
		if (token != TOK_CHAR || len != ',')
		  {
		    SYNTAX_ERROR;
		    break;
		  }

		token = xlocfile_lex (&ptr, &len);
		if (token != TOK_CHAR && token != TOK_ILL_CHAR)
		  {
		    SYNTAX_ERROR;
		    break;
		  }
		to = len;
		ignore |= token == TOK_ILL_CHAR;
	      
		token = xlocfile_lex (&ptr, &len);
		if (token != TOK_CHAR || len != ')')
		  {
		    SYNTAX_ERROR;
		    break;
		  }

		if (!ignore && valid_char (from) && valid_char (to))
		  /* Have a valid pair.  */
		  ELEM (is_upper ? toupper_b : tolower_b, from) = to;
	      }
	    while ((token = locfile_lex (&ptr, &len)) == TOK_CHAR
		   && len == ';');

	    /* Rest of the line should be empty.  */
	    ignore_to_eol (token, 1);
	  }
	  break;
	default:
	  SYNTAX_ERROR;
	  ignore_to_eol (0, 0);
	  break;
	}

      /* Read next token.  */
      token = xlocfile_lex (&ptr, &len);
    }

  token = xlocfile_lex (&ptr, &len);

  if (token != _NL_NUM_LC_CTYPE)
    {
      error (0, 0, gettext ("%s:%Zd: category `%s' does not end with "
			    "`END %s'"), locfile_data.filename,
	     locfile_data.line_no, "LC_CTYPE", "LC_CTYPE");
      ignore_to_eol (0, 0);
    }
  else
    ignore_to_eol (0, posix_conformance);
}


void
ctype_check(void)
{
  /* Here are a lot of things to check.  See POSIX.2, table 2-6.  */
  #define NCLASS 11
  static const struct
    {
      const char *name;
      const char allow[NCLASS];
    }
  valid_table[NCLASS] =
    {
      /* The order is important.  See token.h for more information.
         M = Always, D = Default, - = Permitted, X = Mutually exclusive  */
      [BITPOS (TOK_UPPER)]  = { "upper",  "--MX-XDDXXX" },
      [BITPOS (TOK_LOWER)]  = { "lower",  "--MX-XDDXXX" },
      [BITPOS (TOK_ALPHA)]  = { "alpha",  "---X-XDDXXX" },
      [BITPOS (TOK_DIGIT)]  = { "digit",  "XXX--XDDXXX" },
      [BITPOS (TOK_XDIGIT)] = { "xdigit", "-----XDDXXX" },
      [BITPOS (TOK_SPACE)]  = { "space",  "XXXXX------" },
      [BITPOS (TOK_PRINT)]  = { "print",  "---------X-" },
      [BITPOS (TOK_GRAPH)]  = { "graph",  "---------X-" },
      [BITPOS (TOK_BLANK)]  = { "blank",  "XXXXXM-----" },
      [BITPOS (TOK_CNTRL)]  = { "cntrl",  "XXXXX-XX--X" },
      [BITPOS (TOK_PUNCT)]  = { "punct",  "XXXXX-DD-X-" }
    };
  int ch, cls1, cls2, eq, space_char;
  u16 tmp;

  /* Set default value for classes not specified.  */
  set_class_defaults ();

  /* Check according to table.  */
  for (ch = 0; ch < charmap_data.hash_size * charmap_data.hash_layers; ++ch)
    {
      if (ch != 0 && names_b[ch] == 0)
	continue;
      tmp = ELEM (ctype_b, names_b[ch]);
      for (cls1 = 0; cls1 < NCLASS; ++cls1)
        if ((tmp & (1 << cls1)) != 0)
          for (cls2 = 0; cls2 < NCLASS; ++cls2)
            if (cls2 != cls1 && valid_table[cls1].allow[cls2] != '-')
              {
                eq = (tmp & (1 << cls2)) != 0;
                switch (valid_table[cls1].allow[cls2])
                  {
                  case 'M':
                    if (!eq)
                      error (0, 0, gettext ("character '\\%o' in class `%s' "
					    "must be in class `%s'"), ch,
			     valid_table[cls1].name, valid_table[cls2].name);
                    break;
                  case 'X':
                    if (eq)
                      error (0, 0, gettext ("character '\\%o' inc class `%s' "
					    "must not be in class `%s'"), ch,
			     valid_table[cls1].name, valid_table[cls2].name);
                    break;
                  case 'D':
                    ELEM (ctype_b, names_b[ch]) |= 1 << cls2;
                    break;
                  default:
                    error (5, 0, gettext ("internal error in %s, line %u"),
			   __FUNCTION__, __LINE__);
                  }
              }
    }

  /* ... and now test <SP>  as a special case.  */
  if (find_entry (&charmap_data.table, "SP", 2, (void **) &space_char) == 0)
    error (0, 0, gettext ("character <SP> not defined in character map"));
  else if ((tmp = BITPOS (TOK_SPACE),
            (ELEM (ctype_b, space_char) & BIT (TOK_SPACE)) == 0)
           || (tmp = BITPOS (TOK_BLANK),
               (ELEM (ctype_b, space_char) & BIT (TOK_BLANK)) == 0))
    error (0, 0, gettext ("<SP> character not in class `%s'"),
	   valid_table[tmp].name);
  else if ((tmp = BITPOS (TOK_PUNCT),
            (ELEM (ctype_b, space_char) & BIT (TOK_PUNCT)) != 0)
           || (tmp = BITPOS (TOK_GRAPH),
               (ELEM (ctype_b, space_char) & BIT (TOK_GRAPH)) != 0))
    error (0, 0, gettext ("<SP> character must not be in class `%s'"),
	   valid_table[tmp].name);
  else
    ELEM (ctype_b, space_char) |= BIT (TOK_PRINT);
}


/* These macros can change little to big endian and vice versa.  */
#define SWAP16(v)							     \
      ((u16) (((((unsigned short) (v)) & 0x00ff) << 8)			     \
	      | ((((unsigned short) (v)) & 0xff00) >> 8)))
#define SWAP32(v)							     \
	((u32) (((((u32) (v)) & 0x000000ff) << 24)			     \
		| ((((u32) (v)) & 0x0000ff00) << 8)			     \
		| ((((u32) (v)) & 0x00ff0000) >> 8)			     \
		| ((((u32) (v)) & 0xff000000) >> 24)))


int
ctype_output (void)
{
  char *path, *t;
  int ch;
  /* File descriptor for output file.  */
  int fd;
  /* Magic number.  */
  i32 magic = LIMAGIC (LC_CTYPE);
  /* Number of table.  */
  int tables = 6;
  /* Number ints in leading information table.  */
#if 0
  i32 n = 2 + 2 * tables;
#else
  i32 n = 5;
#endif
  /* Values describing the character set.  */
  char mb_cur_min = (char) charmap_data.mb_cur_min;
  char mb_cur_max = (char) charmap_data.mb_cur_max;
  /* Optimal size of hashing table.  */
  i32 hash_size = charmap_data.hash_size;
  i32 hash_layers = charmap_data.hash_layers;
  /* Number of elements in the tables.  */
  int size = hash_size * charmap_data.hash_layers;
  /* Positions of the tables.  */
  i32 pos[14] =
    {
      /* No, no.  We don't play towers of Hanoi.  This is a more or less
	 readable table of the offsets of the different strings in the
	 produced file.  It is seperated in three columns which represent
	 the number of values with 1, 2, and 4 bytes.  */

#if 0
                                   4 *  (2 + n),
      1 +                          4 *  (2 + n),
      2 +                          4 *  (2 + n),
      2 +                          4 *  (3 + n),
      2 +                          4 *  (4 + n),
      2 + 2 *      (128 + size)  + 4 *  (4 + n),
      2 + 2 *      (128 + size)  + 4 * ((4 + n) +     (size + 128)),
      2 + 2 *      (128 + size)  + 4 * ((4 + n) + 2 * (size + 128)),
      2 + 2 *      (128 + size)  + 4 * ((4 + n) + 2 * (size + 128) + 1 * size),
      2 + 2 *      (128 + size)  + 4 * ((5 + n) + 2 * (size + 128) + 1 * size),
      2 + 2 *      (128 + size)  + 4 * ((6 + n) + 2 * (size + 128) + 1 * size),
      2 + 2 * (2 * (128 + size)) + 4 * ((6 + n) + 2 * (size + 128) + 1 * size),
      2 + 2 * (2 * (128 + size)) + 4 * ((6 + n) + 3 * (size + 128) + 1 * size),
      2 + 2 * (2 * (128 + size)) + 4 * ((6 + n) + 4 * (size + 128) + 1 * size),
#else
                                   4 *  (2 + n),
          2 *      (128 + size)  + 4 *  (2 + n),
          2 *      (128 + size)  + 4 * ((2 + n) +     (size + 128)),
          2 *      (128 + size)  + 4 * ((2 + n) + 2 * (size + 128)),
	  2 *      (128 + size)  + 4 * ((2 + n) + 3 * (size + 128)),
#endif
    };
  /* Parameter to writev.  */
  struct iovec iov[11] = 
    { 
      { &magic, sizeof (i32) },
      { &n, sizeof (i32) },
#if 0
      { pos, sizeof (pos) },
      { &mb_cur_min, 1 },
      { &mb_cur_max, 1 },
      { &hash_size, sizeof (i32) },
      { &hash_layers, sizeof (i32) },
#else
      { pos, 5 * 4 },
#endif
      { ctype_b   - 128, (size + 128) * sizeof (u16) },
      { toupper_b - 128, (size + 128) * sizeof (i32) },
      { tolower_b - 128, (size + 128) * sizeof (i32) },
      { names_b, size * sizeof (i32) }
    };
  int result = 0;
  
  /* Now we can bring the representations into the right form.  */
  for (ch = -128; ch < -1; ++ch)
    {
      ctype_b[ch] = ctype_b[256 + ch];
      toupper_b[ch] = toupper_b[256 + ch];
      tolower_b[ch] = tolower_b[256 + ch];
    }
  /* Set value for EOF.  */
  ctype_b[-1] = 0;
  toupper_b[-1] = -1;
  tolower_b[-1] = -1;

  for (ch = -128; ch < size; ++ch)
    ctype_b[ch] = htons (ctype_b[ch]);

  /* Construct the output filename from the argument given to
     localedef on the command line.  */
  path = (char *) alloca (strlen (output_path) +
			  strlen (category[LC_CTYPE].name) + 1);
  t = stpcpy (path, output_path);
  strcpy (t, category[LC_CTYPE].name);

  fd = creat (path, 0666);
  if (fd == -1)
    {
      error (0, 0, gettext ("cannot open output file `%s': %m"), path);
      result = 1;
    }
  else
    {
      int idx;

#if 0
      if (writev (fd, iov, 10) == -1)
#else
      if (writev (fd, iov, 6) == -1)
#endif
	{
	  error (0, 0, gettext ("cannot write output file `%s': %m"), path);
	  result = 1;
	  goto close_and_return;
	}

      /* Now we have to write the three tables with different endianess.  */
      hash_size = SWAP32 (hash_size);
      for (idx = -128; idx < size; ++idx)
	{
	  ctype_b[idx] = SWAP16 (ctype_b[idx]);
	  toupper_b[idx] = SWAP32 (toupper_b[idx]);
	  tolower_b[idx] = SWAP32 (tolower_b[idx]);
	  if (idx >= 0)
	    names_b[idx] = SWAP32 (names_b[idx]);
	}

#if 0
      if (writev (fd, iov + 5, 6) == -1)
#else
      if (writev (fd, iov + 3, 2) == -1)
#endif
	{
	  error (0, 0, gettext ("cannot write output file `%s': %m"), path);
	  result = 1;
	}

      close_and_return:
      close (fd);
    }

  return result;
}


/* If necessary allocate the memory for the arrays according to the
   current character map.  */
static void
allocate_arrays (void)
{
  /* Init ctype data structures.  */
  if (ctype_b == NULL)
    /* All data structures are not initialized yet.  */
    {
      /* You wonder about this amount of memory?  This is only because
	 some users do not manage to address the array with unsigned
	 values or data types with range >= 256.  '\200' would result
	 in the array index -128.  To help these poor people we
	 duplicate the entries for 128 upto 255 below the entry for \0.  */
      int ch, h, n;
      char *ptr;
      int size = charmap_data.hash_size * charmap_data.hash_layers;

      ctype_b = xmalloc ((size - (-128)) * sizeof (u16));
      bzero (ctype_b, (size - (-128)) * sizeof (u16));
      ctype_b += 128;


      names_b = xmalloc (size * sizeof (i32));
      bzero (names_b, size * sizeof (i32));

      toupper_b = xmalloc ((size - (-128)) * sizeof (i32));
      bzero (toupper_b, (size - (-128)) * sizeof (i32));
      toupper_b += 128;

      tolower_b = xmalloc ((size - (-128)) * sizeof (i32));
      bzero (tolower_b, (size - (-128)) * sizeof (i32));
      tolower_b += 128;

      ptr = NULL;
      /* Mark the place of the NUL character as occupied.  */
      names_b[0] = 1;

      while (iterate_table (&charmap_data.table, (void **) &ptr,
			    (void **) &ch))
	{
	  /* We already handled the NUL character.  */
	  if (ch == 0)
	    continue;

	  h = ch % charmap_data.hash_size;
	  n = 0;
	  while (names_b[h + n * charmap_data.hash_size] != 0)
	    ++n;

	  names_b[h + n * charmap_data.hash_size] = ch;
	  toupper_b[h + n * charmap_data.hash_size] = ch;
	  tolower_b[h + n * charmap_data.hash_size] = ch;
	}
      /* Correct the value for NUL character.  */
      names_b[0] = 0;
    }
}

static void
set_class_defaults (void)
{
  /* These function defines the default values for the classes and conversions
     according to POSIX.2 2.5.2.1.
     It may seem that the order of these if-blocks is arbitrary but it is NOT.
     Don't move them unless you know what you do!  */

  void set_default (int bit, int from, int to)
    {
      char tmp[4];
      int ch;
      /* Define string.  */
      strcpy (tmp, "<?>");

      for (ch = from; ch <= to; ++ch)
	{
	  int code;
	  tmp[1] = ch;

	  code = find_char (tmp + 1, 1);
	  if (code == -1)
	    error (5, 0, gettext ("character `%s' not defined while needed "
				  "as default value"), tmp);
	  ELEM (ctype_b, code) |= bit;
	}
    }

  /* If necessary allocate arrays.  */
  allocate_arrays ();

  /* Set default values if keyword was not present.  */
  if ((class_done & BIT (TOK_UPPER)) == 0)
    /* "If this keyword [lower] is not specified, the lowercase letters
        `A' through `Z', ..., shall automatically belong to this class,
	with implementation defined character values."  */
    set_default (BIT (TOK_UPPER), 'A', 'Z');

  if ((class_done & BIT (TOK_LOWER)) == 0)
    /* "If this keyword [lower] is not specified, the lowercase letters
        `a' through `z', ..., shall automatically belong to this class,
	with implementation defined character values."  */
    set_default (BIT (TOK_LOWER), 'a', 'z');

  if ((class_done & BIT (TOK_DIGIT)) == 0)
    /* "If this keyword [digit] is not specified, the digits `0' through
        `9', ..., shall automatically belong to this class, with
	implementation-defined character values."  */        
    set_default (BIT (TOK_DIGIT), '0', '9');

  if ((class_done & BIT (TOK_SPACE)) == 0)
    /* "If this keyword [space] is not specified, the characters <space>,
        <form-feed>, <newline>, <carriage-return>, <tab>, and
	<vertical-tab>, ..., shall automatically belong to this class,
	with implementtation-defined character values."  */
    {
      int code;

      code = find_char ("space", 5);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<space>");
      ELEM (ctype_b, code) |= BIT (TOK_SPACE);

      code = find_char ("form-feed", 9);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<form-feed>");
      ELEM (ctype_b, code) |= BIT (TOK_SPACE);

      code = find_char ("newline", 7);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<newline>");
      ELEM (ctype_b, code) |= BIT (TOK_SPACE);

      code = find_char ("carriage-return", 15);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<carriage-return>");
      ELEM (ctype_b, code) |= BIT (TOK_SPACE);

      code = find_char ("tab", 3);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<tab>");
      ELEM (ctype_b, code) |= BIT (TOK_SPACE);

      code = find_char ("vertical-tab", 11);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<vertical-tab>");
      ELEM (ctype_b, code) |= BIT (TOK_SPACE);
    }
  
  if ((class_done & BIT (TOK_XDIGIT)) == 0)
    /* "If this keyword is not specified, the digits `0' to `9', the
        uppercase letters `A' through `F', and the lowercase letters `a'
	through `f', ..., shell automatically belong to this class, with
	implementation defined character values."  */
    {
      if ((class_done & BIT (TOK_XDIGIT)) == 0)
	set_default (BIT (TOK_XDIGIT), '0', '9');

      if ((class_done & BIT (TOK_XDIGIT)) == 0)
	set_default (BIT (TOK_XDIGIT), 'A', 'F');

      if ((class_done & BIT (TOK_XDIGIT)) == 0)
	set_default (BIT (TOK_XDIGIT), 'a', 'f');
    }

  if ((class_done & BIT (TOK_BLANK)) == 0)
    /* "If this keyword [blank] is unspecified, the characters <space> and
       <tab> shall belong to this character class."  */
   {
      int code;

      code = find_char ("space", 5);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<space>");
      ELEM (ctype_b, code) |= BIT (TOK_BLANK);

      code = find_char ("tab", 3);
      if (code == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<tab>");
      ELEM (ctype_b, code) |= BIT (TOK_BLANK);
    }

  if ((class_done & BIT (TOK_GRAPH)) == 0)
    /* "If this keyword [graph] is not specified, characters specified for
        the keywords `upper', `lower', `alpha', `digit', `xdigit' and `punct',
	shall belong to this character class."  */
    {
      int ch;
      unsigned short int mask = BIT (TOK_UPPER) | BIT (TOK_LOWER) |
	BIT (TOK_ALPHA) | BIT (TOK_DIGIT) | BIT (TOK_XDIGIT) | BIT (TOK_PUNCT);

      for (ch = 0; ch < charmap_data.hash_size * charmap_data.hash_layers;
		   ++ch)
	{
	  if (ch != 0 && names_b[ch] == 0)
	    continue;
	  if ((ELEM (ctype_b, names_b[ch]) & mask) != 0)
	    ELEM (ctype_b, names_b[ch]) |= BIT (TOK_GRAPH);
	}
    }

  if ((class_done & BIT (TOK_PRINT)) == 0)
    /* "If this keyword [print] is not provided, characters specified for
        the keywords `upper', `lower', `alpha', `digit', `xdigit', `punct',
	and the <space> character shall belong to this character class."  */
    {
      int ch;
      int space = find_char ("space", 5);
      unsigned short int mask = BIT (TOK_UPPER) | BIT (TOK_LOWER) |
	BIT (TOK_ALPHA) | BIT (TOK_DIGIT) | BIT (TOK_XDIGIT) | BIT (TOK_PUNCT);

      if (space == -1)
	error (5, 0, gettext ("character `%s' not defined while needed as "
			      "default value"), "<space>");

      for (ch = 0; ch < charmap_data.hash_size * charmap_data.hash_layers;
		   ++ch)
	{
	  if (ch != 0 && names_b[ch] == 0)
	    continue;
	  if ((ELEM (ctype_b, names_b[ch]) & mask) != 0)
	    ELEM (ctype_b, names_b[ch]) |= BIT (TOK_PRINT);
	}
      ELEM (ctype_b, space) |= BIT (TOK_PRINT);
    }

  if (toupper_done == 0)
    /* "If this keyword [toupper] is not spcified, the lowercase letters
        `a' through `z', and their corresponding uppercase letters `A' to
	`Z', ..., shall automatically be included, with implementation-
	defined character values."  */
    {
      char tmp[4];
      int ch;

      strcpy (tmp, "<?>");

      for (ch = 'a'; ch <= 'z'; ++ch)
	{
	  int code_to, code_from;

	  tmp[1] = ch;
	  code_from = find_char (tmp + 1, 1);
	  if (code_from == -1)
	    error (5, 0, gettext ("character `%s' not defined while needed "
				  "as default value"), tmp);

	  /* This conversion is implementation defined.  */
	  tmp[1] = ch + ('A' - 'a');
	  code_to = find_char (tmp + 1, 1);
	  if (code_to == -1)
	    error (5, 0, gettext ("character `%s' not defined while needed "
				  "as default value"), tmp);

	  ELEM (toupper_b, code_from) = code_to;
	}
    }

  if (tolower_done == 0)
    /* "If this keyword [tolower] is not specified, the mapping shall be
        the reverse mapping of the one specified to `toupper'."  */
    {
      int ch;

      for (ch = 0; ch < charmap_data.hash_size * charmap_data.hash_layers;
		   ++ch)
	{
	  if (ch != 0 && names_b[ch] == 0)
	    continue;

	  if (toupper_b[ch] != names_b[ch])
	    ELEM (tolower_b, toupper_b[ch]) = names_b[ch];
	}
    }
}


/* Test whether the given character is valid for the current charmap.  */
static int
valid_char (int ch)
{
  /* FIXME: this assumes 32-bit integers.  */
  int ok = ch >= 0
    && (charmap_data.mb_cur_max < 4
	? ch < 1 << (8 * charmap_data.mb_cur_max) : 1);

  return ok;
}


/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:2
 * End:
 */