1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
34 #include "linereader.h"
38 /* Uncomment following line for production version. */
43 /* Define the lookup function. */
44 #include "charmap-kw.h"
47 void *xmalloc (size_t __n
);
49 /* Prototypes for local functions. */
50 static struct charset_t
*parse_charmap (const char *filename
);
51 static void new_width (struct linereader
*cmfile
, struct charset_t
*result
,
52 const char *from
, const char *to
,
53 unsigned long int width
);
57 charmap_read (const char *filename
)
59 const char *pathnfile
;
60 struct charset_t
*result
= NULL
;
64 if (euidaccess (filename
, R_OK
) >= 0)
68 char *cp
= xmalloc (strlen (filename
) + sizeof CHARMAP_PATH
+ 1);
69 stpcpy (stpcpy (stpcpy (cp
, CHARMAP_PATH
), "/"), filename
);
71 pathnfile
= (const char *) cp
;
74 result
= parse_charmap (pathnfile
);
76 if (result
== NULL
&& !be_quiet
)
77 error (0, errno
, _("character map file `%s' not found"), filename
);
82 /* OK, one more try. We also accept the names given to the
83 character sets in the files. Sometimes they differ from the
86 struct dirent
*dirent
;
88 dir
= opendir (CHARMAP_PATH
);
91 while ((dirent
= readdir (dir
)) != NULL
)
92 if (strcmp (dirent
->d_name
, ".") != 0
93 && strcmp (dirent
->d_name
, "..") != 0)
95 char buf
[sizeof (CHARMAP_PATH
)
96 + strlen (dirent
->d_name
) + 1];
98 #ifdef _DIRENT_HAVE_D_TYPE
99 if (dirent
->d_type
!= DT_UNKNOWN
&& dirent
->d_type
!= DT_REG
)
102 stpcpy (stpcpy (stpcpy (buf
, CHARMAP_PATH
), "/"),
105 fp
= fopen (buf
, "r");
114 if (fscanf (fp
, " <code_set_name> %as", &name
) == 1)
117 while (fgets (junk
, sizeof junk
, fp
) != NULL
118 && strchr (junk
, '\n') == NULL
)
126 if (strcmp (name
, filename
) == 0)
128 result
= parse_charmap (buf
);
149 pathnfile
= CHARMAP_PATH
"/" DEFAULT_CHARMAP
;
151 result
= parse_charmap (pathnfile
);
154 error (4, errno
, _("default character map file `%s' not found"),
162 static struct charset_t
*
163 parse_charmap (const char *filename
)
165 struct linereader
*cmfile
;
166 struct charset_t
*result
;
168 enum token_t expected_tok
= tok_error
;
169 const char *expected_str
= NULL
;
170 char *from_name
= NULL
;
171 char *to_name
= NULL
;
173 /* Determine path. */
174 cmfile
= lr_open (filename
, charmap_hash
);
177 if (strchr (filename
, '/') == NULL
)
179 /* Look in the systems charmap directory. */
180 char *buf
= xmalloc (strlen (filename
) + 1 + sizeof (CHARMAP_PATH
));
182 stpcpy (stpcpy (stpcpy (buf
, CHARMAP_PATH
), "/"), filename
);
183 cmfile
= lr_open (buf
, charmap_hash
);
193 /* Allocate room for result. */
194 result
= (struct charset_t
*) xmalloc (sizeof (struct charset_t
));
195 memset (result
, '\0', sizeof (struct charset_t
));
196 /* The default DEFAULT_WIDTH is 1. */
197 result
->width_default
= 1;
199 #define obstack_chunk_alloc malloc
200 #define obstack_chunk_free free
201 obstack_init (&result
->mem_pool
);
203 if (init_hash (&result
->char_table
, 256))
209 /* We use a state machine to describe the charmap description file
215 struct token
*now
= lr_token (cmfile
, NULL
);
216 enum token_t nowtok
= now
->tok
;
219 if (nowtok
== tok_eof
)
225 /* The beginning. We expect the special declarations, EOL or
227 if (nowtok
== tok_eol
)
228 /* Ignore empty lines. */
231 if (nowtok
== tok_charmap
)
236 /* We have to set up the real work. Fill in some
238 if (result
->mb_cur_max
== 0)
239 result
->mb_cur_max
= 1;
240 if (result
->mb_cur_min
== 0)
241 result
->mb_cur_min
= result
->mb_cur_max
;
242 if (result
->mb_cur_min
> result
->mb_cur_max
&& !be_quiet
)
245 %s: <mb_cur_max> must be greater than <mb_cur_min>\n"),
248 result
->mb_cur_min
= result
->mb_cur_max
;
251 lr_ignore_rest (cmfile
, 1);
257 if (nowtok
!= tok_code_set_name
&& nowtok
!= tok_mb_cur_max
258 && nowtok
!= tok_mb_cur_min
&& nowtok
!= tok_escape_char
259 && nowtok
!= tok_comment_char
&& nowtok
!= tok_g0esc
260 && nowtok
!= tok_g1esc
&& nowtok
!= tok_g2esc
261 && nowtok
!= tok_g3esc
)
263 lr_error (cmfile
, _("syntax error in prolog: %s"),
264 _("illegal definition"));
266 lr_ignore_rest (cmfile
, 0);
270 /* We know that we need an argument. */
271 arg
= lr_token (cmfile
, NULL
);
275 case tok_code_set_name
:
276 if (arg
->tok
!= tok_ident
)
279 lr_error (cmfile
, _("syntax error in prolog: %s"),
282 lr_ignore_rest (cmfile
, 0);
286 result
->code_set_name
= obstack_copy0 (&result
->mem_pool
,
290 lr_ignore_rest (cmfile
, 1);
295 if (arg
->tok
!= tok_number
)
298 if (arg
->val
.num
< 1 || arg
->val
.num
> 4)
301 _("value for <%s> must lie between 1 and 4"),
302 nowtok
== tok_mb_cur_min
? "mb_cur_min"
305 lr_ignore_rest (cmfile
, 0);
308 if ((nowtok
== tok_mb_cur_max
&& result
->mb_cur_min
!= 0
309 && (int) arg
->val
.num
< result
->mb_cur_min
)
310 || (nowtok
== tok_mb_cur_min
&& result
->mb_cur_max
!= 0
311 && (int) arg
->val
.num
> result
->mb_cur_max
))
313 lr_error (cmfile
, _("\
314 value of <mb_cur_max> must be greater than the value of <mb_cur_min>"));
316 lr_ignore_rest (cmfile
, 0);
320 if (nowtok
== tok_mb_cur_max
)
321 result
->mb_cur_max
= arg
->val
.num
;
323 result
->mb_cur_min
= arg
->val
.num
;
325 lr_ignore_rest (cmfile
, 1);
328 case tok_escape_char
:
329 case tok_comment_char
:
330 if (arg
->tok
!= tok_ident
)
333 if (arg
->val
.str
.len
!= 1)
335 lr_error (cmfile
, _("\
336 argument to <%s> must be a single character"),
337 nowtok
== tok_escape_char
? "escape_char"
340 lr_ignore_rest (cmfile
, 0);
344 if (nowtok
== tok_escape_char
)
345 cmfile
->escape_char
= *arg
->val
.str
.start
;
347 cmfile
->comment_char
= *arg
->val
.str
.start
;
349 lr_ignore_rest (cmfile
, 1);
356 lr_ignore_rest (cmfile
, 0); /* XXX */
361 assert (! "Should not happen");
366 /* We have seen `CHARMAP' and now are in the body. Each line
367 must have the format "%s %s %s\n" or "%s...%s %s %s\n". */
368 if (nowtok
== tok_eol
)
369 /* Ignore empty lines. */
372 if (nowtok
== tok_end
)
374 expected_tok
= tok_charmap
;
375 expected_str
= "CHARMAP";
380 if (nowtok
!= tok_bsymbol
)
382 lr_error (cmfile
, _("syntax error in %s definition: %s"),
383 "CHARMAP", _("no symbolic name given"));
385 lr_ignore_rest (cmfile
, 0);
389 /* If the previous line was not completely correct free the
391 if (from_name
!= NULL
)
392 obstack_free (&result
->mem_pool
, from_name
);
394 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
403 /* We have two possibilities: We can see an ellipsis or an
405 if (nowtok
== tok_ellipsis
)
413 if (nowtok
!= tok_charcode
&& nowtok
!= tok_ucs2
414 && nowtok
!= tok_ucs4
)
416 lr_error (cmfile
, _("syntax error in %s definition: %s"),
417 "CHARMAP", _("illegal encoding given"));
419 lr_ignore_rest (cmfile
, 0);
425 if (nowtok
== tok_charcode
)
426 /* Write char value in table. */
427 charset_new_char (cmfile
, result
, now
->val
.charcode
.nbytes
,
428 now
->val
.charcode
.val
, from_name
, to_name
);
430 /* Determine ISO 10646 value and write into table. */
431 charset_new_unicode (cmfile
, result
, now
->val
.charcode
.nbytes
,
432 now
->val
.charcode
.val
, from_name
, to_name
);
434 /* Ignore trailing comment silently. */
435 lr_ignore_rest (cmfile
, 0);
444 if (nowtok
!= tok_bsymbol
)
446 lr_error (cmfile
, _("syntax error in %s definition: %s"),
448 _("no symbolic name given for end of range"));
450 lr_ignore_rest (cmfile
, 0);
454 /* If the previous line was not completely correct free the
456 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
457 cmfile
->token
.val
.str
.start
,
458 cmfile
->token
.val
.str
.len
);
464 if (nowtok
!= expected_tok
)
465 lr_error (cmfile
, _("\
466 `%1$s' definition does not end with `END %1$s'"), expected_str
);
468 lr_ignore_rest (cmfile
, nowtok
== expected_tok
);
473 /* Waiting for WIDTH... */
474 if (nowtok
== tok_eol
)
475 /* Ignore empty lines. */
478 if (nowtok
== tok_width_default
)
484 if (nowtok
== tok_width
)
486 lr_ignore_rest (cmfile
, 1);
491 if (nowtok
== tok_width_variable
)
493 lr_ignore_rest (cmfile
, 1);
498 lr_error (cmfile
, _("\
499 only WIDTH definitions are allowed to follow the CHARMAP definition"));
501 lr_ignore_rest (cmfile
, 0);
505 if (nowtok
!= tok_number
)
506 lr_error (cmfile
, _("value for %s must be an integer"),
509 result
->width_default
= now
->val
.num
;
511 lr_ignore_rest (cmfile
, nowtok
== tok_number
);
517 /* We now expect `END WIDTH' or lines of the format "%s %d\n" or
519 if (nowtok
== tok_eol
)
520 /* ignore empty lines. */
523 if (nowtok
== tok_end
)
525 expected_tok
= tok_width
;
526 expected_str
= "WIDTH";
531 if (nowtok
!= tok_bsymbol
)
533 lr_error (cmfile
, _("syntax error in %s definition: %s"),
534 "WIDTH", _("no symbolic name given"));
536 lr_ignore_rest (cmfile
, 0);
540 if (from_name
!= NULL
)
541 obstack_free (&result
->mem_pool
, from_name
);
543 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
552 if (nowtok
== tok_ellipsis
)
559 if (nowtok
!= tok_number
)
560 lr_error (cmfile
, _("value for %s must be an integer"),
564 /* Store width for chars. */
565 new_width (cmfile
, result
, from_name
, to_name
, now
->val
.num
);
571 lr_ignore_rest (cmfile
, nowtok
== tok_number
);
577 if (nowtok
!= tok_bsymbol
)
579 lr_error (cmfile
, _("syntax error in %s definition: %s"),
580 "WIDTH", _("no symbolic name given for end of range"));
582 lr_ignore_rest (cmfile
, 0);
588 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
596 /* We now expect `END WIDTH_VARIABLE' or lines of the format
597 "%s\n" or "%s...%s\n". */
598 if (nowtok
== tok_eol
)
599 /* ignore empty lines. */
602 if (nowtok
== tok_end
)
604 expected_tok
= tok_width_variable
;
605 expected_str
= "WIDTH_VARIABLE";
610 if (nowtok
!= tok_bsymbol
)
612 lr_error (cmfile
, _("syntax error in %s definition: %s"),
613 "WIDTH_VARIABLE", _("no symbolic name given"));
615 lr_ignore_rest (cmfile
, 0);
620 if (from_name
!= NULL
)
621 obstack_free (&result
->mem_pool
, from_name
);
623 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
632 if (nowtok
== tok_ellipsis
)
643 if (nowtok
!= tok_bsymbol
)
644 lr_error (cmfile
, _("syntax error in %s definition: %s"),
646 _("no symbolic name given for end of range"));
649 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
652 /* XXX Enter value into table. */
655 lr_ignore_rest (cmfile
, nowtok
== tok_bsymbol
);
661 error (5, 0, _("%s: error in state machine"), __FILE__
);
667 if (state
!= 91 && !be_quiet
)
668 error (0, 0, _("%s: premature end of file"), cmfile
->fname
);
677 new_width (struct linereader
*cmfile
, struct charset_t
*result
,
678 const char *from
, const char *to
, unsigned long int width
)
680 unsigned int from_val
, to_val
;
682 from_val
= charset_find_value (result
, from
, strlen (from
));
683 if ((wchar_t) from_val
== ILLEGAL_CHAR_VALUE
)
685 lr_error (cmfile
, _("unknown character `%s'"), from
);
693 to_val
= charset_find_value (result
, to
, strlen (to
));
694 if ((wchar_t) to_val
== ILLEGAL_CHAR_VALUE
)
696 lr_error (cmfile
, _("unknown character `%s'"), to
);
701 if (result
->nwidth_rules
>= result
->nwidth_rules_max
)
703 size_t new_size
= result
->nwidth_rules
+ 32;
704 struct width_rule
*new_rules
=
705 (struct width_rule
*) obstack_alloc (&result
->mem_pool
,
707 * sizeof (struct width_rule
)));
709 memcpy (new_rules
, result
->width_rules
,
710 result
->nwidth_rules_max
* sizeof (struct width_rule
));
712 result
->width_rules
= new_rules
;
713 result
->nwidth_rules_max
= new_size
;
716 result
->width_rules
[result
->nwidth_rules
].from
= from_val
;
717 result
->width_rules
[result
->nwidth_rules
].to
= to_val
;
718 result
->width_rules
[result
->nwidth_rules
].width
= (unsigned int) width
;
719 ++result
->nwidth_rules
;