Debian Codegraph @

src/names.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1993-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1993.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22
23 #include <locale.h>
24
25 #include "hash.h"
26
27 /*-------------------------------------------------------------------------.
28 | Return the corresponding UCS-2 value in a CHARSET for a given CODE, or a |
29 | negative number if this symbol is not defined. |
30 `-------------------------------------------------------------------------*/
31
32 _GL_ATTRIBUTE_PURE int
33 recode_code_to_ucs2 (RECODE_CONST_SYMBOL charset, unsigned code)
34 {
35 /* FIXME: if no specific UCS-2 translation, assume an identity map. */
36 if (charset->data_type != RECODE_STRIP_DATA)
37 return code;
38
39 const struct strip_data *data = (const struct strip_data *) charset->data;
40 const recode_ucs2 *pool = data->pool;
41 unsigned offset = data->offset[code / STRIP_SIZE];
42 unsigned value = pool[offset + code % STRIP_SIZE];
43
44 return value == BIT_MASK (16) ? -1 : (int) value;
45 }
46
47 /*---------------------------------------------------------.
48 | Return true if BEFORE to AFTER is currently restricted. |
49 `---------------------------------------------------------*/
50
51 static _GL_ATTRIBUTE_PURE bool
52 check_restricted (RECODE_CONST_OUTER outer,
53 RECODE_CONST_SYMBOL before,
54 RECODE_CONST_SYMBOL after)
55 {
56 struct recode_known_pair *pair;
57 int left;
58 int right;
59
60 for (pair = outer->pair_restriction;
61 pair < outer->pair_restriction + outer->pair_restrictions;
62 pair++)
63 {
64 /* Reject the charset if the characters in the pair do not exist of
65 if their respective definition do not match. */
66
67 left = recode_code_to_ucs2 (before, pair->left);
68 if (left < 0)
69 return true;
70 right = recode_code_to_ucs2 (after, pair->right);
71 if (right < 0)
72 return true;
73 if (left != right)
74 return true;
75 }
76
77 /* No restriction found. */
78
79 return false;
80 }
81
82 /* Charset names. */
83
84 /*-------------------------------------.
85 | Prepare aliases for initialisation. |
86 `-------------------------------------*/
87
88 static size_t
89 alias_hasher (const void *void_alias, size_t limit)
90 {
91 RECODE_CONST_ALIAS alias = (RECODE_CONST_ALIAS) void_alias;
92
93 return hash_string (alias->name, limit);
94 }
95
96 static bool
97 alias_comparator (const void *void_first, const void *void_second)
98 {
99 RECODE_CONST_ALIAS first = (RECODE_CONST_ALIAS) void_first;
100 RECODE_CONST_ALIAS second = (RECODE_CONST_ALIAS) void_second;
101
102 return strcmp (first->name, second->name) == 0;
103 }
104
105 static void
106 alias_free (void *void_alias)
107 {
108 RECODE_ALIAS alias = (RECODE_ALIAS) void_alias;
109 struct recode_surface_list *list, *next;
110
111 for (list = alias->implied_surfaces; list; list = next)
112 {
113 next = list->next;
114 free (list);
115 }
116 free (alias);
117 }
118
119 bool
120 recode_prepare_for_aliases (RECODE_OUTER outer)
121 {
122 outer->symbol_list = NULL;
123 outer->number_of_symbols = 0;
124
125 outer->alias_table
126 = hash_initialize (800, NULL, alias_hasher, alias_comparator, alias_free);
127 if (!outer->alias_table)
128 return false;
129
130 return true;
131 }
132
133 /*---------------------------------------------------------------------------.
134 | Return a newly allocated copy of symbol NAME, with upper case letters |
135 | turned into lower case, and all non alphanumeric discarded, or NULL if any |
136 | problem. |
137 `---------------------------------------------------------------------------*/
138
139 static char *
140 name_for_argmatch (RECODE_OUTER outer, const char *name)
141 {
142 char *result;
143 char *out;
144 const char *in;
145 int character;
146
147 if (!ALLOC (result, strlen (name) + 1, char))
148 return NULL;
149 out = result;
150 for (in = name; *in; in++)
151 {
152 character = *(const unsigned char *) in;
153 if ((character >= 'a' && character <= 'z')
154 || (character >= '0' && character <= '9'))
155 *out++ = character;
156 else if (character >= 'A' && character <= 'Z')
157 *out++ = character - 'A' + 'a';
158 }
159 *out = NUL;
160
161 return result;
162 }
163
164 /*---------------------------------------------------------------------------.
165 | Given an abbreviated NAME of a charset or surface, return its full name, |
166 | properly capitalized and punctuated, or NULL if this cannot be done |
167 | successfully. FIND_TYPE may restrict the interpretation. A NULL or empty |
168 | string means the default charset, if this default charset is defined. |
169 `---------------------------------------------------------------------------*/
170
171 static const char *
172 disambiguate_name (RECODE_OUTER outer,
173 const char *name, enum alias_find_type find_type)
174 {
175 char *hashname;
176 int ordinal;
177 const char *result;
178
179 result = NULL; /* for lint */
180
181 /* Look for a match. */
182
183 if (!name || !*name)
184 switch (find_type)
185 {
186 case ALIAS_FIND_AS_CHARSET:
187 case ALIAS_FIND_AS_EITHER:
188 name = getenv ("DEFAULT_CHARSET");
189 if (!name || !*name)
190 name = locale_charset();
191 if (!name || !*name)
192 return NULL;
193 break;
194
195 default:
196 return NULL;
197 }
198
199 hashname = name_for_argmatch (outer, name);
200 if (!hashname)
201 return NULL;
202
203 switch (find_type)
204 {
205 case SYMBOL_CREATE_CHARSET:
206 case SYMBOL_CREATE_DATA_SURFACE:
207 abort ();
208
209 case ALIAS_FIND_AS_CHARSET:
210 ordinal = argmatch (hashname, outer->argmatch_charset_array, NULL, 0);
211 result = ordinal < 0 ? NULL : outer->realname_charset_array[ordinal];
212 break;
213
214 case ALIAS_FIND_AS_SURFACE:
215 ordinal = argmatch (hashname, outer->argmatch_surface_array, NULL, 0);
216 result = ordinal < 0 ? NULL : outer->realname_surface_array[ordinal];
217 break;
218
219 case ALIAS_FIND_AS_EITHER:
220 ordinal = argmatch (hashname, outer->argmatch_charset_array, NULL, 0);
221 if (ordinal >= 0)
222 result = outer->realname_charset_array[ordinal];
223 else
224 {
225 ordinal = argmatch (hashname, outer->argmatch_surface_array, NULL, 0);
226 result = ordinal < 0 ? NULL : outer->realname_surface_array[ordinal];
227 }
228 break;
229
230 default:
231 break;
232 }
233
234 free (hashname);
235 return result;
236 }
237
238 /*----------------------------------------------------------------------------.
239 | Delete the given alias. |
240 `----------------------------------------------------------------------------*/
241
242 void
243 recode_delete_alias (RECODE_ALIAS alias)
244 {
245 free (alias->symbol);
246 free (alias);
247 }
248
249 /*----------------------------------------------------------------------------.
250 | Return the alias from its given NAME, possibly abbreviated. If FIND_TYPE |
251 | is any of SYMBOL_CREATE_*, NAME is not abbreviated, create a new symbol if |
252 | it does not exist, or return NULL if any problem happens at creation time. |
253 | For other TYPE values, never create a new charset, merely return NULL if |
254 | NAME cannot be correctly disambiguated. |
255 `----------------------------------------------------------------------------*/
256
257 RECODE_ALIAS
258 recode_find_alias (RECODE_OUTER outer, const char *name,
259 enum alias_find_type find_type)
260 {
261 struct recode_alias lookup;
262 RECODE_ALIAS alias;
263 RECODE_SYMBOL symbol;
264 enum recode_symbol_type type = RECODE_NO_SYMBOL_TYPE;
265
266 switch (find_type)
267 {
268 case SYMBOL_CREATE_CHARSET:
269 type = RECODE_CHARSET;
270 break;
271
272 case SYMBOL_CREATE_DATA_SURFACE:
273 type = RECODE_DATA_SURFACE;
274 break;
275
276 default:
277 /* Clean and disambiguate first as requested. */
278
279 name = disambiguate_name (outer, name, find_type);
280 if (!name)
281 return NULL;
282 }
283
284 /* Search the whole hash bucket and return any match. */
285
286 if (!name)
287 return NULL;
288 lookup.name = name;
289 if (alias = (RECODE_ALIAS) hash_lookup
290 ((const Hash_table *) outer->alias_table, &lookup), alias)
291 return alias;
292
293 /* If we reach this point, find_type is necessarily one of SYMBOL_CREATE_*.
294 For any other value of find_type, the alias would have already been found
295 and returned. So, here, a new symbol does have to be created. */
296
297 if (!ALLOC (symbol, 1, struct recode_symbol))
298 return NULL;
299 symbol->ordinal = outer->number_of_symbols++;
300 symbol->name = name;
301 symbol->iconv_name = NULL;
302 symbol->type = type;
303 symbol->ignore = false;
304 symbol->data_type = RECODE_NO_CHARSET_DATA;
305 symbol->data = NULL;
306 symbol->resurfacer = NULL;
307 symbol->unsurfacer = NULL;
308
309 if (!ALLOC (alias, 1, struct recode_alias))
310 {
311 free (symbol);
312 return NULL;
313 }
314 alias->name = name;
315 alias->symbol = symbol;
316 alias->implied_surfaces = NULL;
317 if (!hash_insert ((Hash_table *) outer->alias_table, alias))
318 {
319 recode_delete_alias (alias);
320 return NULL;
321 }
322
323 symbol->next = outer->symbol_list;
324 outer->symbol_list = symbol;
325
326 return alias;
327 }
328
329 /*-----------------------------------------------------------------------.
330 | Have NAME to be an alternate name for OLD_NAME. Create OLD_NAME if it |
331 | does not exist already. |
332 `-----------------------------------------------------------------------*/
333
334 RECODE_ALIAS
335 recode_declare_alias (RECODE_OUTER outer, const char *name, const char *old_name)
336 {
337 struct recode_alias lookup;
338 RECODE_ALIAS alias;
339 RECODE_SYMBOL symbol;
340
341 /* Find the symbol. */
342
343 if (alias = recode_find_alias (outer, old_name, SYMBOL_CREATE_CHARSET), !alias)
344 return NULL;
345 symbol = alias->symbol;
346
347 lookup.name = name;
348 if (alias = (RECODE_ALIAS) hash_lookup
349 ((Hash_table *) outer->alias_table, &lookup), alias)
350 {
351 if (alias->symbol == symbol)
352 return alias;
353 recode_error (outer, _("Charset %s already exists and is not %s"),
354 name, old_name);
355 return NULL;
356 }
357
358 /* Make the alias. */
359
360 if (!ALLOC (alias, 1, struct recode_alias))
361 return NULL;
362 alias->name = name;
363 alias->symbol = symbol;
364 alias->implied_surfaces = NULL;
365 if (!hash_insert ((Hash_table *) outer->alias_table, alias))
366 {
367 free (alias);
368 return NULL;
369 }
370
371 return alias;
372 }
373
374 /*-------------------------------------------------------------------------.
375 | To the end of implied surfaces for CHARSET_NAME, add the one represented |
376 | by SURFACE_NAME. Both names are created as necessary. |
377 `-------------------------------------------------------------------------*/
378
379 bool
380 recode_declare_implied_surface (RECODE_OUTER outer, RECODE_ALIAS alias,
381 RECODE_CONST_SYMBOL surface)
382 {
383 struct recode_surface_list *list;
384 struct recode_surface_list *hook;
385
386 if (!ALLOC (hook, 1, struct recode_surface_list))
387 return false;
388
389 hook->surface = surface;
390 hook->next = NULL;
391
392 if (alias->implied_surfaces)
393 {
394 list = alias->implied_surfaces;
395 while (list->next)
396 list = list->next;
397 list->next = hook;
398 }
399 else
400 alias->implied_surfaces = hook;
401
402 return true;
403 }
404
405 /*-------------------------------------------.
406 | Construct the string arrays for argmatch. |
407 `-------------------------------------------*/
408
409 struct make_argmatch_walk
410 {
411 RECODE_OUTER outer;
412 unsigned charset_counter; /* number of acceptable charset names */
413 unsigned surface_counter; /* number of acceptable surface names */
414 };
415
416 static bool
417 make_argmatch_walker_1 (void *void_alias, void *void_walk)
418 {
419 RECODE_ALIAS alias = (RECODE_ALIAS) void_alias;
420 struct make_argmatch_walk *walk = (struct make_argmatch_walk *) void_walk;
421
422 if (alias->symbol->type == RECODE_CHARSET)
423 walk->charset_counter++;
424 else
425 walk->surface_counter++;
426
427 return true;
428 }
429
430 static bool
431 make_argmatch_walker_2 (void *void_alias, void *void_walk)
432 {
433 RECODE_ALIAS alias = (RECODE_ALIAS) void_alias;
434 struct make_argmatch_walk *walk = (struct make_argmatch_walk *) void_walk;
435 RECODE_OUTER outer = walk->outer;
436
437 if (alias->symbol->type == RECODE_CHARSET)
438 {
439 char *string = name_for_argmatch (outer, alias->name);
440
441 if (!string)
442 abort ();
443 outer->argmatch_charset_array[walk->charset_counter] = string;
444 outer->realname_charset_array[walk->charset_counter] = alias->name;
445 walk->charset_counter++;
446 }
447 else
448 {
449 char *string = name_for_argmatch (outer, alias->name);
450
451 if (!string)
452 abort ();
453 outer->argmatch_surface_array[walk->surface_counter] = string;
454 outer->realname_surface_array[walk->surface_counter] = alias->name;
455 walk->surface_counter++;
456 }
457
458 return true;
459 }
460
461 bool
462 recode_make_argmatch_arrays (RECODE_OUTER outer)
463 {
464 struct make_argmatch_walk walk; /* wanderer's data */
465
466 /* It may happen that new modules are added only once all initialisation
467 completed. To handle that case, free previous arrays if any. */
468
469 if (outer->argmatch_charset_array)
470 {
471 const char **cursor;
472
473 for (cursor = outer->argmatch_charset_array; *cursor; cursor++)
474 free ((char *) *cursor);
475 for (cursor = outer->argmatch_surface_array; *cursor; cursor++)
476 free ((char *) *cursor);
477 free (outer->argmatch_charset_array);
478 }
479
480 /* Count how many strings we need. */
481
482 walk.outer = outer;
483 walk.charset_counter = 0;
484 walk.surface_counter = 0;
485 hash_do_for_each ((Hash_table *) outer->alias_table,
486 make_argmatch_walker_1, &walk);
487
488 /* Allocate the argmatch and realname arrays, each with a NULL sentinel. */
489
490 {
491 const char **cursor;
492
493 if (!ALLOC (cursor, 2*walk.charset_counter + 2*walk.surface_counter + 4,
494 const char *))
495 return false;
496
497 outer->argmatch_charset_array = cursor;
498 cursor += walk.charset_counter;
499 *cursor++ = NULL;
500
501 outer->argmatch_surface_array = cursor;
502 cursor += walk.surface_counter;
503 *cursor++ = NULL;
504
505 outer->realname_charset_array = (const char **) cursor;
506 cursor += walk.charset_counter;
507 *cursor++ = NULL;
508
509 outer->realname_surface_array = (const char **) cursor;
510 cursor += walk.surface_counter;
511 *cursor = NULL;
512 }
513
514 /* Fill in the arrays. */
515
516 walk.charset_counter = 0;
517 walk.surface_counter = 0;
518 hash_do_for_each ((Hash_table *) outer->alias_table,
519 make_argmatch_walker_2, &walk);
520
521 return true;
522 }
523
524 /*-------------------------------------------------------------------------.
525 | Order two strings lexicographically, ignoring case and comparing numeric |
526 | values for run of decimal digits at first. |
527 `-------------------------------------------------------------------------*/
528
529 static _GL_ATTRIBUTE_PURE int
530 compare_strings (const char *stringA, const char *stringB)
531 {
532 int delayed = 0;
533
534 while (*stringA && *stringB)
535 if (*stringA >= '0' && *stringA <= '9')
536 if (*stringB >= '0' && *stringB <= '9')
537 {
538 unsigned valueA = 0;
539 unsigned valueB = 0;
540
541 while (*stringA >= '0' && *stringA <= '9'
542 && *stringB >= '0' && *stringB <= '9')
543 {
544 valueA = 10 * valueA + *stringA - '0';
545 valueB = 10 * valueB + *stringB - '0';
546 if (delayed == 0)
547 delayed = *stringA - *stringB;
548 stringA++;
549 stringB++;
550 }
551 while (*stringA >= '0' && *stringA <= '9')
552 {
553 valueA = 10 * valueA + *stringA - '0';
554 if (delayed == 0)
555 delayed = 1;
556 stringA++;
557 }
558 while (*stringB >= '0' && *stringB <= '9')
559 {
560 valueB = 10 * valueB + *stringB - '0';
561 if (delayed == 0)
562 delayed = -1;
563 stringB++;
564 }
565 if (valueA - valueB != 0)
566 return valueA - valueB;
567 }
568 else
569 return -1;
570 else
571 if (*stringB >= '0' && *stringB <= '9')
572 return 1;
573 else
574 {
575 char charA = *stringA;
576 char charB = *stringB;
577
578 if (charA >= 'a' && charA <= 'z')
579 charA += 'A' - 'a';
580 if (charB >= 'a' && charB <= 'z')
581 charB += 'A' - 'a';
582 if (charA - charB != 0)
583 return charA - charB;
584 if (delayed == 0)
585 delayed = *stringA - *stringB;
586 stringA++;
587 stringB++;
588 }
589
590 return *stringA ? 1 : *stringB ? -1 : delayed;
591 }
592
593 /*--------------------------------------------------------------------------.
594 | Order two struct recode_alias, using the first key to group all surfaces |
595 | together, the second key to group charsets or surfaces having the same |
596 | unaliased name, the third key to list unaliases names first, and the last |
597 | key to order aliased names. |
598 `--------------------------------------------------------------------------*/
599
600 static int
601 compare_struct_alias (const void *void_first, const void *void_second)
602 {
603 RECODE_CONST_ALIAS first = (RECODE_CONST_ALIAS) void_first;
604 RECODE_CONST_ALIAS second = (RECODE_CONST_ALIAS) void_second;
605 int value;
606
607 if (first->symbol->type == RECODE_CHARSET
608 && second->symbol->type != RECODE_CHARSET)
609 return 1;
610 if (first->symbol->type != RECODE_CHARSET
611 && second->symbol->type == RECODE_CHARSET)
612 return -1;
613
614 if (value = compare_strings (first->symbol->name, second->symbol->name),
615 value != 0)
616 return value;
617
618 if (first->name == first->symbol->name
619 && second->name != second->symbol->name)
620 return -1;
621 if (second->name == second->symbol->name
622 && first->name != first->symbol->name)
623 return 1;
624
625 return compare_strings (first->name, second->name);
626 }
627
628 /*-------------------------------------------------------------------------.
629 | List all available symbols, obeying restrictions for an AFTER charset if |
630 | any. |
631 `-------------------------------------------------------------------------*/
632
633 struct list_symbols_walk
634 {
635 RECODE_ALIAS array;
636 unsigned number;
637 };
638
639 static bool
640 list_symbols_walker_1 (void *void_alias, void *void_walk)
641 {
642 RECODE_ALIAS alias = (RECODE_ALIAS) void_alias;
643 struct list_symbols_walk *walk = (struct list_symbols_walk *) void_walk;
644
645 if (!alias->symbol->ignore)
646 walk->number++;
647
648 return true;
649 }
650
651 static bool
652 list_symbols_walker_2 (void *void_alias, void *void_walk)
653 {
654 RECODE_ALIAS alias = (RECODE_ALIAS) void_alias;
655 struct list_symbols_walk *walk = (struct list_symbols_walk *) void_walk;
656
657 if (!alias->symbol->ignore)
658 walk->array[walk->number++] = *alias;
659
660 return true;
661 }
662
663 bool
664 recode_list_all_symbols (RECODE_OUTER outer, RECODE_CONST_SYMBOL after)
665 {
666 struct list_symbols_walk walk; /* wanderer's data */
667 RECODE_ALIAS alias; /* cursor into sorted array */
668 bool list_flag; /* if the current alias should be listed */
669
670 /* Count how many symbols we have. */
671
672 walk.number = 0;
673 hash_do_for_each ((Hash_table *) outer->alias_table,
674 list_symbols_walker_1, &walk);
675
676 /* Allocate a structure to hold them. */
677
678 if (!ALLOC (walk.array, walk.number, struct recode_alias))
679 return false;
680
681 /* Copy all symbols in it. */
682
683 walk.number = 0;
684 hash_do_for_each ((Hash_table *) outer->alias_table,
685 list_symbols_walker_2, &walk);
686
687 /* Sort it. */
688
689 qsort (walk.array, (size_t) walk.number, sizeof (struct recode_alias),
690 compare_struct_alias);
691
692 /* Print it, one line per symbol, giving the true symbol name first,
693 followed by all its alias in lexicographic order. */
694
695 list_flag = false;
696 for (alias = walk.array; alias < walk.array + walk.number; alias++)
697 {
698 /* Begin a new line with the true symbol name when it changes. */
699
700 if (alias == walk.array
701 || alias->symbol->name != (alias - 1)->symbol->name)
702 {
703 if (list_flag && alias != walk.array)
704 putchar ('\n');
705
706 list_flag
707 = !after || !check_restricted (outer, alias->symbol, after);
708
709 if (list_flag && alias->symbol->type != RECODE_CHARSET)
710 putchar ('/');
711 }
712 else if (list_flag)
713 putchar (' ');
714
715 /* Print a name and its usual surfaces. */
716
717 if (list_flag)
718 {
719 struct recode_surface_list *cursor;
720
721 fputs (alias->name, stdout);
722 for (cursor = alias->implied_surfaces; cursor; cursor = cursor->next)
723 {
724 putchar ('/');
725 fputs (cursor->surface->name, stdout);
726 }
727 }
728 }
729 if (list_flag)
730 putchar ('\n');
731
732 /* Release the work array. */
733
734 free (walk.array);
735 return true;
736 }
737
738 /* Charset contents. */
739
740 /*-----------------------------------------------------------------.
741 | Decode a known PAIRS argument, given in STRING, constructing the |
742 | pair_restriction array out of it. |
743 `-----------------------------------------------------------------*/
744
745 bool
746 recode_decode_known_pairs (RECODE_OUTER outer, const char *string)
747 {
748 const char *cursor;
749 char *after;
750 int left_value;
751 int right_value;
752 int *pointer;
753
754 if (!ALLOC (outer->pair_restriction, 16, struct recode_known_pair))
755 return false;
756
757 left_value = -1;
758 right_value = -1;
759 pointer = &left_value;
760
761 cursor = string;
762 while (*cursor)
763 switch (*cursor)
764 {
765 default:
766 return false;
767
768 case '0':
769 case '1':
770 case '2':
771 case '3':
772 case '4':
773 case '5':
774 case '6':
775 case '7':
776 case '8':
777 case '9':
778 *pointer = strtoul (cursor, &after, 0);
779 cursor = after;
780 if (*pointer > 255)
781 return false;
782 break;
783
784 case ':':
785 cursor++;
786 if (left_value < 0 || pointer != &left_value)
787 return false;
788 pointer = &right_value;
789 break;
790
791 case ',':
792 cursor++;
793 if (left_value < 0 || right_value < 0)
794 return false;
795
796 outer->pair_restriction[outer->pair_restrictions].left
797 = (unsigned char) left_value;
798 outer->pair_restriction[outer->pair_restrictions].right
799 = (unsigned char) right_value;
800 outer->pair_restrictions++;
801
802 if (outer->pair_restrictions % 16 == 0)
803 if (!REALLOC (outer->pair_restriction,
804 outer->pair_restrictions + 16,
805 struct recode_known_pair))
806 return false;
807
808 left_value = -1;
809 right_value = -1;
810 pointer = &left_value;
811 break;
812 }
813
814 if (left_value < 0 || right_value < 0)
815 return false;
816
817 outer->pair_restriction[outer->pair_restrictions].left
818 = (unsigned char) left_value;
819 outer->pair_restriction[outer->pair_restrictions].right
820 = (unsigned char) right_value;
821 outer->pair_restrictions++;
822
823 return true;
824 }
825
826 /*-----------------------------------------------------------------.
827 | Print a concise, tabular CHARSET description on standard output, |
828 | presented according to LIST_FORMAT. |
829 `-----------------------------------------------------------------*/
830
831 bool
832 recode_list_concise_charset (RECODE_OUTER outer,
833 RECODE_CONST_SYMBOL charset,
834 const enum recode_list_format list_format)
835 {
836 unsigned half; /* half 0, half 1 of the table */
837 const char *format; /* format string */
838 const char *blanks; /* white space to replace format string */
839 unsigned counter; /* code counter */
840 unsigned counter2; /* code counter */
841 unsigned code; /* code value */
842
843 /* Ensure we have a strip table to play with. */
844
845 if (charset->data_type != RECODE_STRIP_DATA)
846 {
847 recode_error (outer,
848 _("Cannot list `%s', no names available for this charset"),
849 charset->name);
850 return false;
851 }
852
853 printf ("%s\n", charset->name);
854
855 /* Select format for numbers. */
856
857 switch (list_format)
858 {
859 default:
860 return false; /* cannot happen */
861
862 case RECODE_NO_FORMAT:
863 case RECODE_DECIMAL_FORMAT:
864 format = "%3d";
865 blanks = " ";
866 break;
867
868 case RECODE_OCTAL_FORMAT:
869 format = "%0.3o";
870 blanks = " ";
871 break;
872
873 case RECODE_HEXADECIMAL_FORMAT:
874 format = "%0.2x";
875 blanks = " ";
876 break;
877 }
878
879 /* Print both halves of the table. */
880
881 for (half = 0; half < 2; half++)
882 {
883 /* Skip printing this half if it is empty. */
884
885 for (code = 128 * half; code < 128 * (half + 1); code++)
886 if (recode_code_to_ucs2 (charset, code) >= 0)
887 break;
888 if (code == 128 * (half + 1))
889 continue;
890
891 /* Print this half. */
892
893 printf ("\n");
894 for (counter = 128 * half; counter < 128 * half + 16; counter++)
895 for (counter2 = 0; counter2 < 128; counter2 += 16)
896 {
897 int ucs2;
898 const char *mnemonic;
899
900 if (counter2 > 0)
901 printf (" ");
902
903 code = counter + counter2;
904 ucs2 = recode_code_to_ucs2 (charset, code);
905 mnemonic = ucs2 >= 0 ? recode_ucs2_to_rfc1345 (ucs2) : NULL;
906
907 /* FIXME: Trailing space elimination is not always effective. */
908
909 #pragma GCC diagnostic push
910 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
911 if (ucs2 >= 0)
912 printf (format, code);
913 else if (mnemonic || counter2 != 112)
914 fputs (blanks, stdout);
915 #pragma GCC diagnostic pop
916
917 if (mnemonic)
918 printf (counter2 == 112 ? " %s\n" : " %-3s", mnemonic);
919 else
920 printf (counter2 == 112 ? "\n" : " ");
921 }
922 }
923
924 return true;
925 }
926
927 /*------------------------------------------------------.
928 | Print a full CHARSET description on standard output. |
929 `------------------------------------------------------*/
930
931 static void
932 list_full_charset_line (int code, recode_ucs2 ucs2, bool french)
933 {
934 const char *mnemonic = recode_ucs2_to_rfc1345 (ucs2);
935 const char *charname;
936
937 if (code >= 0)
938 printf ("%3d %.3o %.2x", code, (unsigned)code, (unsigned)code);
939 else
940 fputs (" + + + ", stdout);
941
942 printf (" %.4X", ucs2);
943
944 if (mnemonic)
945 printf (" %-3s", mnemonic);
946 else
947 fputs (" ", stdout);
948
949 if (french)
950 {
951 charname = recode_ucs2_to_french_charname (ucs2);
952 if (!charname)
953 charname = recode_ucs2_to_charname (ucs2);
954 }
955 else
956 {
957 charname = recode_ucs2_to_charname (ucs2);
958 if (!charname)
959 charname = recode_ucs2_to_french_charname (ucs2);
960 }
961
962 if (charname)
963 {
964 fputs (" ", stdout);
965 fputs (charname, stdout);
966 }
967 putchar ('\n');
968 }
969
970 /*-----------------------------------------------.
971 | Decide if we prefer French or English output. |
972 `-----------------------------------------------*/
973
974 bool
975 recode_should_prefer_french (void)
976 {
977 const char *string = setlocale(LC_MESSAGES, NULL);
978
979 return string && string[0] == 'f' && string[1] == 'r';
980 }
981
982 bool
983 recode_list_full_charset (RECODE_OUTER outer, RECODE_CONST_SYMBOL charset)
984 {
985 bool french = recode_should_prefer_french();
986
987 /* See which data is available. */
988
989 switch (charset->data_type)
990 {
991 case RECODE_EXPLODE_DATA:
992 {
993 const unsigned short *data = (const unsigned short *) charset->data;
994 unsigned code; /* code counter */
995 unsigned expected; /* expected value for code counter */
996 bool insert_white; /* insert a while line before printing */
997
998 /* Print the long table according to explode data. */
999
1000 printf (_("Dec Oct Hex UCS2 Mne %s\n"), charset->name);
1001 insert_white = true;
1002 expected = 0;
1003
1004 while (*data != DONE)
1005 {
1006 code = *data++;
1007 while (expected < code)
1008 {
1009 if (insert_white)
1010 {
1011 putchar ('\n');
1012 insert_white = false;
1013 }
1014 list_full_charset_line (expected, expected, french);
1015 expected++;
1016 }
1017 if (*data == ELSE || *data == DONE)
1018 insert_white = true;
1019 else
1020 {
1021 if (insert_white)
1022 {
1023 putchar ('\n');
1024 insert_white = false;
1025 }
1026 list_full_charset_line (code, *data++, french);
1027 while (*data != ELSE && *data != DONE)
1028 list_full_charset_line (-1, *data++, french);
1029 }
1030 while (*data != DONE)
1031 data++;
1032 expected = code + 1;
1033 data++;
1034 }
1035 }
1036 return true;
1037
1038 case RECODE_STRIP_DATA:
1039 {
1040 unsigned code; /* code counter */
1041 int ucs2; /* UCS-2 translation */
1042 bool insert_white; /* insert a while line before printing */
1043
1044 /* Print the long table according to strip data. */
1045
1046 printf (_("Dec Oct Hex UCS2 Mne %s\n"), charset->name);
1047 insert_white = true;
1048
1049 for (code = 0; code < 256; code++)
1050 if ((ucs2 = recode_code_to_ucs2 (charset, code)), ucs2 >= 0)
1051 {
1052 if (insert_white)
1053 {
1054 putchar ('\n');
1055 insert_white = false;
1056 }
1057 list_full_charset_line (code, ucs2, french);
1058 }
1059 else
1060 insert_white = true;
1061 }
1062 return true;
1063
1064 default:
1065 recode_error (outer, _("Sorry, no names available for `%s'"),
1066 charset->name);
1067 return false;
1068 }
1069 }
1070
1071 /*----------------------------------------------------------------------------.
1072 | This is a diagnostic tool. Report all charsets which are a subset of |
1073 | another, or are identical. Return true only if there are no such subsets. |
1074 `----------------------------------------------------------------------------*/
1075
1076 bool
1077 recode_find_and_report_subsets (RECODE_OUTER outer)
1078 {
1079 bool success = true;
1080 RECODE_SYMBOL charset1;
1081
1082 for (charset1 = outer->symbol_list;
1083 charset1;
1084 charset1 = charset1->next)
1085 {
1086 const struct strip_data *table1
1087 = (const struct strip_data *) charset1->data;
1088 RECODE_SYMBOL charset2;
1089
1090 if (charset1->ignore || charset1->data_type != RECODE_STRIP_DATA)
1091 continue;
1092
1093 for (charset2 = outer->symbol_list;
1094 charset2;
1095 charset2 = charset2->next)
1096 {
1097 const struct strip_data *table2
1098 = (const struct strip_data *) charset2->data;
1099
1100 if (charset2->ignore || charset2->data_type != RECODE_STRIP_DATA
1101 || charset2 == charset1)
1102 continue;
1103
1104 {
1105 bool subset = true;
1106 unsigned distance = 0;
1107 unsigned counter;
1108 unsigned slider;
1109
1110 for (counter = 0; counter < 256/STRIP_SIZE; counter++)
1111 {
1112 const recode_ucs2 *pool1 = table1->pool;
1113 const recode_ucs2 *pool2 = table2->pool;
1114 const short offset1 = table1->offset[counter];
1115 const short offset2 = table2->offset[counter];
1116
1117 if (pool1 != pool2 || offset1 != offset2)
1118 for (slider = 0; slider < STRIP_SIZE; slider++)
1119 {
1120 recode_ucs2 value1 = pool1[offset1 + slider];
1121 recode_ucs2 value2 = pool2[offset2 + slider];
1122
1123 if (value1 != value2)
1124 {
1125 if (value1 == BIT_MASK (16))
1126 distance++;
1127 else
1128 {
1129 subset = false;
1130 break;
1131 }
1132 }
1133 }
1134 if (!subset)
1135 break;
1136 }
1137
1138 if (subset)
1139 {
1140 if (distance == 0)
1141 printf ("[ 0] %s == %s\n",
1142 charset1->name, charset2->name);
1143 else
1144 printf ("[%3u] %s < %s\n", distance,
1145 charset1->name, charset2->name);
1146
1147 success = false;
1148 }
1149 }
1150 }
1151 }
1152
1153 return success;
1154 }