File: eightbit.c

package info (click to toggle)
vile 9.8za-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,644 kB
  • sloc: ansic: 120,894; lex: 14,981; sh: 4,478; perl: 3,511; cpp: 3,180; makefile: 1,425; awk: 271
file content (924 lines) | stat: -rw-r--r-- 21,319 bytes parent folder | download
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*
 * $Id: eightbit.c,v 1.114 2025/01/26 17:09:56 tom Exp $
 *
 * Maintain "8bit" file-encoding mode by converting incoming UTF-8 to single
 * bytes, and providing a function that tells vile whether a given Unicode
 * value will map to the eight-bit value in the "narrow" locale.
 */

#include <estruct.h>
#include <chgdfunc.h>
#include <edef.h>
#include <nefsms.h>

#if OPT_LOCALE
#include <locale.h>
#endif

#if OPT_ICONV_FUNCS
#include <iconv.h>
#endif

#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif

#include <gnreight.h>

#define StrMalloc(s) ((s) ? strmalloc(s) : NULL)

#if DISP_TERMCAP || DISP_CURSES || DISP_BORLAND
#define USE_MBTERM 1
static int (*save_getch) (void);
static OUTC_DCL(*save_putch) (int c);
#else
#define USE_MBTERM 0
#endif

#define LEN_UTF8 6		/* maximum number of bytes in UTF-8 encoding */

/******************************************************************************/
static BLIST blist_encodings = init_blist(all_encodings);
static BLIST blist_locales = init_blist(all_locales);

/******************************************************************************/

typedef struct {
    UINT code;			/* Unicode value */
    char *text;			/* UTF-8 string */
} TABLE_8BIT;

static TABLE_8BIT table_8bit_utf8[N_chars];

typedef struct {
    UINT code;			/* Unicode value */
    int rinx;			/* actual index in table_8bit_utf8[] */
} RINDEX_8BIT;

static RINDEX_8BIT rindex_8bit[N_chars];

static int
cmp_rindex(const void *a, const void *b)
{
    const RINDEX_8BIT *p = (const RINDEX_8BIT *) a;
    const RINDEX_8BIT *q = (const RINDEX_8BIT *) b;
    return (int) p->code - (int) q->code;
}

static const GNREIGHT_ENC *from_encoding = NULL;
static const GNREIGHT_ENC *builtin_locale = NULL;

#if OPT_ICONV_FUNCS
#define MY_ICONV_TYPE iconv_t
#else
#define MY_ICONV_TYPE  const GNREIGHT_ENC *
#endif /* OPT_ICONV_FUNCS */

#define MY_ICONV  (MY_ICONV_TYPE)(-2)
#define NO_ICONV  (MY_ICONV_TYPE)(-1)

static MY_ICONV_TYPE mb_desc = NO_ICONV;

static void
close_encoding(void)
{
#if OPT_ICONV_FUNCS
    if (mb_desc == MY_ICONV) {
	;
    } else if (mb_desc != NO_ICONV) {
	iconv_close(mb_desc);
    }
#endif /* OPT_ICONV_FUNCS */
    mb_desc = NO_ICONV;
    from_encoding = NULL;
}

static int
try_encoding(const char *from, const char *to)
{
    int found;

    close_encoding();

#if OPT_ICONV_FUNCS
    mb_desc = iconv_open(to, from);
#endif

    if (mb_desc == NO_ICONV) {
	if (!vl_stricmp(to, "UTF-8")) {
	    found = blist_match(&blist_encodings, from);
	    if (found >= 0) {
		mb_desc = MY_ICONV;
		from_encoding = all_encodings[found].data;
		TRACE(("using built-in encoding\n"));
	    }
	}
    }

    TRACE(("try_encoding(from=%s, to=%s) %s\n",
	   from, to,
	   ((mb_desc != NO_ICONV)
	    ? "OK"
	    : "FAIL")));

    return (mb_desc != NO_ICONV);
}

static void
open_encoding(char *from, char *to)
{
    if (!try_encoding(from, to)) {
	fprintf(stderr, "Cannot setup translation from %s to %s\n", from, to);
	tidy_exit(BADEXIT);
    }
}

static void
initialize_table_8bit_utf8(void)
{
    int n;

    for (n = 0; n < N_chars; ++n) {
	size_t converted;
	char output[80];
	size_t in_bytes = 1;
	size_t out_bytes = sizeof(output);

#if OPT_ICONV_FUNCS
	char input[80];
	input[0] = (char) n;
	input[1] = 0;
#endif
	(void) in_bytes;

	FreeIfNeeded(table_8bit_utf8[n].text);
	table_8bit_utf8[n].text = NULL;

#if OPT_ICONV_FUNCS
	if (mb_desc == MY_ICONV) {
	    UINT source = ((n >= 128)
			   ? (from_encoding->chmap[n - 128])
			   : (UINT) n);
	    converted = (size_t) vl_conv_to_utf8((UCHAR *) output, source,
						 sizeof(output));
	    out_bytes = sizeof(output) - converted;
	} else {
	    ICONV_CONST char *ip = input;
	    char *op = output;
	    converted = iconv(mb_desc, &ip, &in_bytes, &op, &out_bytes);
	}
#else
	{
	    UINT source = ((n >= 128)
			   ? (from_encoding->chmap[n - 128])
			   : (UINT) n);
	    converted = (size_t) vl_conv_to_utf8((UCHAR *) output, source,
						 sizeof(output));
	    out_bytes = sizeof(output) - converted;
	}
#endif /* OPT_ICONV_FUNCS */

	if (converted == (size_t) (-1)) {
	    TRACE(("err:%d\n", errno));
	    TRACE(("convert(%d) %d %d/%d\n", n,
		   (int) converted, (int) in_bytes, (int) out_bytes));
	} else if (n == 0) {
	    table_8bit_utf8[n].code = 0;
	    table_8bit_utf8[n].text = strmalloc("");
	} else if ((sizeof(output) - out_bytes) != 0) {
	    output[sizeof(output) - out_bytes] = 0;
	    table_8bit_utf8[n].text = strmalloc(output);
	    vl_conv_to_utf32(&(table_8bit_utf8[n].code),
			     table_8bit_utf8[n].text,
			     (B_COUNT) strlen(table_8bit_utf8[n].text));
#if OPT_TRACE > 1
	    if (n != (int) table_8bit_utf8[n].code)
		TRACE(("8bit_utf8 %d:%#4x\n", n, table_8bit_utf8[n].code));
#endif
	}
    }
}

/*
 * Fill-in character type information from built-in tables, i.e., when the
 * narrow locale is missing.
 */
void
vl_8bit_ctype_init(int wide, int ch)
{
    const GNREIGHT_ENC *encode;

    if (builtin_locale) {
	TRACE(("using previously found built-in encoding\n"));
	from_encoding = builtin_locale;
    }
#if !OPT_ICONV_FUNCS
    else {
	int found;

	from_encoding = 0;
	found = blist_match(&blist_encodings, wide ? "ISO-8859-1" : "POSIX");
	if (found >= 0) {
	    from_encoding = all_encodings[found].data;
	    TRACE(("using built-in encoding\n"));
	}
    }
#endif

    encode = (from_encoding
	      ? from_encoding
	      : (wide
		 ? &encode_ISO_8859_1
		 : &encode_ANSI_X3_4_1968));

    if ((ch < 128) || encode->chmap[ch - 128]) {
	CHARTYPE value = (CHARTYPE) ((ch < 128)
				     ? encode_POSIX.ctype[ch]
				     : encode->ctype[ch - 128]);

	/* xdigit is specific to ASCII */
	if (value & vl_digit)
	    value |= vl_xdigit;
	if ((value & vl_alpha) && (strchr("abcdefABCDEF", ch) != NULL))
	    value |= vl_xdigit;

	value &= ~vl_alpha;	/* unused in vile */

	setVlCTYPE(ch, value);

	vl_lowercase[ch + 1] = (char) ((ch < 128)
				       ? encode_POSIX.lower[ch]
				       : encode->lower[ch - 128]);

	vl_uppercase[ch + 1] = (char) ((ch < 128)
				       ? encode_POSIX.upper[ch]
				       : encode->upper[ch - 128]);
    }
}

int
vl_8bit_builtin(void)
{
    return (builtin_locale != NULL);
}

char *
vl_narrowed(const char *wide)
{
    char *result = NULL;

    TRACE((T_CALLED "vl_narrowed(%s)\n", NonNull(wide)));
    if (wide != NULL) {
	const char *mapping = sys_getenv("VILE_LOCALE_MAPPING");
	const char *parsed;
	char *on_left;
	char *on_right;
	regexp *exp;
	int n;

	/*
	 * The default mapping strips ".UTF-8", ".utf-8", ".UTF8" or ".utf8",
	 * and assumes that the default locale alias corresponds to the 8-bit
	 * encoding.  That is true on many systems.
	 */
	if (mapping == NULL)
	    mapping = "/\\.\\(UTF\\|utf\\)[-]\\?8$//";

	parsed = mapping;
	on_left = regparser(&parsed);
	on_right = regparser(&parsed);
	if (on_left != NULL
	    && on_right != NULL
	    && parsed[1] == '\0'
	    && (exp = regcomp(on_left, strlen(on_left), TRUE)) != NULL) {
	    int len = (int) strlen(wide);
	    int found = 0;

	    if ((result = malloc(strlen(wide) + 2 + strlen(on_right))) != NULL) {
		strcpy(result, wide);
		for (n = 0; n < len; ++n) {
		    found = regexec(exp, result, result + len, n, len, FALSE);
		    if (found)
			break;
		}

		/*
		 * Substitute the result (back-substitutions not supported).
		 */
		if (found) {
		    char *save = strmalloc(exp->endp[0]);

		    TRACE(("vl_narrowed match \"%.*s\", replace with \"%s\"\n",
			   (int) (exp->endp[0] - exp->startp[0]),
			   exp->startp[0],
			   on_right));

		    strcpy(exp->startp[0], on_right);
		    strcat(exp->startp[0], save);
		    free(save);
		} else {
		    free(result);
		    result = NULL;
		}
	    }
	    regfree(exp);
	} else {
	    fprintf(stderr, "VILE_LOCAL_MAPPING error:%s\n", mapping);
	}
	FreeIfNeeded(on_left);
	FreeIfNeeded(on_right);
    }
    returnString(result);
}

/*
 * Do the rest of the work for setting up the wide- and narrow-locales.
 *
 * Set vl_encoding to the internal code used for resolving encoding of
 * buffers whose file-encoding is "locale" (enc_LOCALE).
 */
void
vl_init_8bit(const char *wide, const char *narrow)
{
    int fixup, fixed_up;
    int n;

    TRACE((T_CALLED "vl_init_8bit(%s, %s)\n", NonNull(wide), NonNull(narrow)));
    if (wide == NULL || narrow == NULL) {
	TRACE(("setup POSIX-locale\n"));
	vl_encoding = enc_POSIX;
	vl_wide_enc.locale = NULL;
	vl_narrow_enc.locale = NULL;
	vl_get_encoding(&vl_narrow_enc.encoding, narrow);
    } else if (vl_stricmp(wide, narrow)) {
	TRACE(("setup mixed-locale(%s, %s)\n", wide, narrow));
	vl_wide_enc.locale = StrMalloc(wide);
	vl_narrow_enc.locale = StrMalloc(narrow);
	vl_get_encoding(&vl_wide_enc.encoding, wide);
	vl_get_encoding(&vl_narrow_enc.encoding, narrow);
	TRACE(("...locale encodings(%s, %s)\n",
	       NONNULL(vl_wide_enc.encoding),
	       NONNULL(vl_narrow_enc.encoding)));

	if (vl_is_utf8_encoding(vl_wide_enc.encoding)) {
	    vl_encoding = enc_UTF8;
	} else {
	    vl_encoding = enc_8BIT;
	}

	/*
	 * If the wide/narrow encodings do not differ, that is probably because
	 * the narrow encoding is really a wide-encoding.
	 */
	if (vl_narrow_enc.encoding != NULL
	    && vl_wide_enc.encoding != NULL
	    && vl_stricmp(vl_narrow_enc.encoding, vl_wide_enc.encoding)) {
	    open_encoding(vl_narrow_enc.encoding, vl_wide_enc.encoding);
	    initialize_table_8bit_utf8();
	    close_encoding();
	}
    } else {
	TRACE(("setup narrow-locale(%s)\n", narrow));
	vl_encoding = enc_8BIT;
	vl_wide_enc.locale = NULL;
	vl_narrow_enc.locale = StrMalloc(narrow);
	vl_get_encoding(&vl_narrow_enc.encoding, narrow);
	if (try_encoding(vl_narrow_enc.encoding, "UTF-8")) {
	    initialize_table_8bit_utf8();
	    close_encoding();
	}
    }

    /*
     * Even if we do not have iconv, we can still convert between the narrow
     * encoding (if it happens to be ISO-8859-1) and UTF-8.
     */
    if (vl_is_latin1_encoding(vl_narrow_enc.encoding)) {
	fixup = N_chars;
    } else {
	/* if nothing else, still accept POSIX characters */
	fixup = 128;
    }
    for (n = 0, latin1_codes = 128, fixed_up = 0; n < fixup; ++n) {
	if (table_8bit_utf8[n].text == NULL) {
	    char temp[10];
	    int len = vl_conv_to_utf8((UCHAR *) temp, (UINT) n, sizeof(temp));

	    temp[len] = EOS;
	    table_8bit_utf8[n].code = (UINT) n;
	    FreeIfNeeded(table_8bit_utf8[n].text);
	    table_8bit_utf8[n].text = strmalloc(temp);
	    if (len)
		++fixed_up;
	}
    }
    TRACE(("fixed-up %d of %d 8bit/utf8 mappings\n", fixed_up, fixup));

    /*
     * Build reverse-index.
     */
    TRACE2(("build rindex_8bit\n"));
    for (n = 0; n < N_chars; ++n) {
	int code = (int) table_8bit_utf8[n].code;

	if (n == 0 || code > 0) {
	    rindex_8bit[n].code = (UINT) code;
	    rindex_8bit[n].rinx = n;
	} else {
	    table_8bit_utf8[n].code = (UINT) - 1;
	    rindex_8bit[n].code = (UINT) - 1;
	    rindex_8bit[n].rinx = -1;
	}

	TRACE2(("code %d (0x%04X) is \\u%04X:%s\n", n, n,
		table_8bit_utf8[n].code,
		NonNull(table_8bit_utf8[n].text)));
    }
    qsort(rindex_8bit, (size_t) N_chars, sizeof(RINDEX_8BIT), cmp_rindex);

    /*
     * As long as the narrow/wide encodings match, we can treat those as
     * Latin-1.
     */
    for (n = 0, latin1_codes = 0; n < N_chars; ++n) {
	int check;
	if (vl_ucs_to_8bit(&check, n)) {
	    latin1_codes = n + 1;
	} else {
	    break;
	}
    }
    TRACE(("assume %d Latin1 codes\n", latin1_codes));

    returnVoid();
}

int
vl_is_8bit_encoding(const char *value)
{
    int rc = vl_is_latin1_encoding(value);
    if (!rc) {
	rc = (isEmpty(value)
	      || strstr(value, "ASCII") != NULL
	      || strstr(value, "ANSI") != NULL
	      || strncmp(value, "KOI8-R", (size_t) 6) == 0);
    }
    return rc;
}

int
vl_is_latin1_encoding(const char *value)
{
    int rc = FALSE;

    if (!isEmpty(value)) {
	if (strncmp(value, "ISO-8859", (size_t) 8) == 0
	    || strncmp(value, "ISO 8859", (size_t) 8) == 0
	    || strncmp(value, "ISO_8859", (size_t) 8) == 0
	    || strncmp(value, "ISO8859", (size_t) 7) == 0
	    || strncmp(value, "8859", (size_t) 4) == 0) {
	    rc = TRUE;
	}
    }
    return rc;
}

int
vl_is_utf8_encoding(const char *value)
{
    int rc = FALSE;

    if (!isEmpty(value)) {
	char *suffix = strchr(value, '.');
	if (suffix != NULL) {
#ifdef WIN32
	    if (!strcmp(suffix, ".65000")) {
		rc = TRUE;
	    } else
#endif
		if (strchr(value + 1, '.') == NULL
		    && vl_is_utf8_encoding(value + 1)) {
		rc = TRUE;
	    }
	} else if (!vl_stricmp(value, "UTF8")
		   || !vl_stricmp(value, "UTF-8")) {
	    rc = TRUE;
	}
    }
    return rc;
}

/*
 * Check if the given Unicode value can be mapped to an "8bit" value.
 */
int
vl_mb_is_8bit(int code)
{
    int result = 0;

    RINDEX_8BIT *p;
    RINDEX_8BIT key;

    key.code = (UINT) code;
    p = (RINDEX_8BIT *) bsearch(&key,
				rindex_8bit,
				(size_t) N_chars,
				sizeof(RINDEX_8BIT),
				cmp_rindex);

    if (p != NULL)
	result = ((int) p->code >= 0 && p->code < 256);

    return result;
}

/*
 * Returns a string representing the current locale.
 * If the target is nonnull, allocate a copy of it.
 */
char *
vl_get_locale(char **target)
{
    char *result = setlocale(LC_CTYPE, NULL);

    if (target != NULL) {
	FreeIfNeeded(*target);
	if (result != NULL)
	    result = strmalloc(result);
	*target = result;
    }
    return result;
}

/*
 * Returns a string representing the character encoding.
 * If the target is nonnull, allocate a copy of it.
 */
char *
vl_get_encoding(char **target, const char *locale)
{
    static char iso_latin1[] = "ISO-8859-1";
    static char utf_eight[] = "UTF-8";
    static char us_ascii[] = "ASCII";

#ifdef WIN32
    static char cp_value[80];
#endif
    char *result = NULL;
    char *actual = setlocale(LC_CTYPE, locale);
    int can_free = 0;

    TRACE((T_CALLED "vl_get_encoding(%s)%s\n",
	   NONNULL(locale),
	   isEmpty(actual) ? " illegal" : ""));

    if (isEmpty(actual)) {	/* nonempty means legal locale */
	char *mylocale;

	/*
	 * If it failed, this may be because we asked for a narrow locale
	 * that corresponds to the given (wide) locale.
	 */
	beginDisplay();
	if (vl_is_utf8_encoding(locale)) {
	    result = utf_eight;
	} else if (!isEmpty(locale) && (mylocale = strmalloc(locale)) != NULL) {
	    regexp *exp;

	    exp = regcomp(tb_values(latin1_expr),
			  (size_t) tb_length0(latin1_expr), TRUE);
	    if (exp != NULL) {
		if (nregexec(exp, mylocale, (char *) 0, 0, -1, FALSE)) {
		    TRACE(("... found match in $latin1-expr\n"));
		    result = iso_latin1;
		}
		free(exp);
	    }
	    free(mylocale);

	    if (result == NULL) {
		int found = blist_match(&blist_locales, locale);
		if (found >= 0) {
		    int find;
		    TRACE(("... found built-in locale data\n"));
		    for (find = 0; all_encodings[find].name; ++find) {
			if (all_encodings[find].data == all_locales[found].data) {
			    TRACE(("... found built-in encoding data\n"));
			    result = strmalloc(all_encodings[find].name);
			    can_free = 1;
			    builtin_locale = all_encodings[find].data;
			    break;
			}
		    }
		}
	    }
	}
	endofDisplay();
    } else {
#ifdef HAVE_LANGINFO_CODESET
	result = nl_langinfo(CODESET);
#endif
	if (isEmpty(result)) {
	    if (vl_is_utf8_encoding(locale)) {
		result = utf_eight;
	    } else if (isEmpty(locale)
		       || !vl_stricmp(locale, "C")
		       || !vl_stricmp(locale, "POSIX")) {
		result = us_ascii;
	    } else if (!vl_stricmp(locale, "en_US")) {
		result = iso_latin1;
	    } else {
#ifdef WIN32
		char *suffix = strrchr(locale, '.');
		char *next = 0;

		if (suffix != 0
		    && strtol(++suffix, &next, 10) != 0
		    && (next == 0 || *next == 0)) {
		    result = cp_value;
		    sprintf(result, "CP%s", suffix);
		} else
#endif
		    result = iso_latin1;
	    }
	}
    }
    if (target != NULL) {
	FreeIfNeeded(*target);
	if (result != NULL && !can_free)
	    result = strmalloc(result);
	*target = result;
    }
    returnString(result);
}

/*
 * Use the lookup table created in vl_init_8bit() to convert an "8bit"
 * value to the corresponding UTF-8 string.  If the current locale
 * encoding is ISO-8859-1 (the default), this is a 1-1 mapping.
 */
const char *
vl_mb_to_utf8(int code)
{
    const char *result = NULL;
    RINDEX_8BIT *p;
    RINDEX_8BIT key;

    key.code = (UINT) code;
    p = (RINDEX_8BIT *) bsearch(&key,
				rindex_8bit,
				(size_t) N_chars,
				sizeof(RINDEX_8BIT),
				cmp_rindex);
    if (p != NULL && p->rinx >= 0)
	result = table_8bit_utf8[p->rinx].text;

    return result;
}

/*
 * Use the lookup table created in vl_init_8bit() to convert an "8bit"
 * value to the corresponding Unicode value.  If the current locale
 * encoding is ISO-8859-1 (the default), this is a 1-1 mapping.
 */
int
vl_8bit_to_ucs(int *result, int code)
{
    int status = FALSE;

    if (code >= 0 && code < 256 && (int) table_8bit_utf8[code].code >= 0) {
	*result = (int) table_8bit_utf8[code].code;
	status = TRUE;
    }
    return status;
}

/*
 * Use the lookup table to find if a Unicode value exists in the 8bit/utf-8
 * mapping, and if so, set the 8bit result, returning true.
 */
int
vl_ucs_to_8bit(int *result, int code)
{
    int status = FALSE;
    RINDEX_8BIT *p;
    RINDEX_8BIT key;

    key.code = (UINT) code;
    p = (RINDEX_8BIT *) bsearch(&key,
				rindex_8bit,
				(size_t) N_chars,
				sizeof(RINDEX_8BIT),
				cmp_rindex);
    if (p != NULL && p->rinx >= 0) {
	*result = p->rinx;
	status = TRUE;
    }
    return status;
}

#if USE_MBTERM
/*
 * Decode a buffer as UTF-8, returning the character value if successful.
 * If unsuccessful, return -1.
 */
static int
decode_utf8(char *input, int used)
{
    UINT check = 0;
    int rc;
    int ch;

    rc = vl_conv_to_utf32(&check, input, (B_COUNT) used);
    if ((rc == used) && (check != 0) && !isSpecial(check))
	ch = (int) check;
    else
	ch = -1;
    return ch;
}

/*
 * Read a whole character, according to the locale.  If an error is detected,
 * return -1.
 */
static int
vl_mb_getch(void)
{
    static char input[80];
    static int used = 0;
    static int have = 0;

    UINT tempch;
    int ch;
    int need, test;
    int actual_encoding;

    if (used < have) {
	ch = CharOf(input[used++]);
    } else {
	used = have = 0;

	if ((actual_encoding = kbd_encoding) == enc_LOCALE) {
	    actual_encoding = (okCTYPE2(vl_wide_enc)
			       ? enc_UTF8
			       : enc_8BIT);
	}
	switch (actual_encoding) {
	case enc_AUTO:
	    input[0] = (char) (ch = save_getch());
	    if ((need = vl_check_utf8(input, (B_COUNT) 1)) > 1) {
		have = 1;
		for (;;) {
		    input[have++] = (char) save_getch();
		    test = vl_check_utf8(input, (B_COUNT) have);
		    if (test == 0) {
			kbd_encoding = enc_8BIT;
			ch = vl_mb_getch();
			break;
		    } else if (test == need) {
			kbd_encoding = enc_UTF8;
			vl_conv_to_utf32(&tempch, input, (B_COUNT) have);
			ch = (int) tempch;
			used = have = 0;
			break;
		    }
		}
	    }
	    break;
	case enc_POSIX:
	    do {
		ch = save_getch();
	    } while (ch >= 128);
	    break;
	default:
	case enc_8BIT:
	    ch = save_getch();
	    if (ch >= 128 && okCTYPE2(vl_wide_enc)) {
		if (ch >= 256) {
		    ch = -1;
		} else if ((ch = (int) table_8bit_utf8[ch].code) == 0) {
		    ch = -1;
		}
	    }
	    break;
	case enc_UTF8:
	    for (;;) {
		ch = save_getch();
		input[have++] = (char) ch;
		input[have] = 0;

		ch = decode_utf8(input, have);
		if (ch >= 0) {
		    break;
		} else if (have > LEN_UTF8) {
		    ch = -1;
		    break;
		}
	    }
	    have = used = 0;
	    if (ch >= 256 && !okCTYPE2(vl_wide_enc)) {
		ch = -1;
	    }
	    break;
	}
    }
    TRACE(("vl_mb_getch:%#x\n", ch));
    return ch;
}

static OUTC_DCL
vl_mb_putch(int c)
{
    if (c > 0) {
	int rc;
	UCHAR temp[LEN_UTF8 + 1];
	const char *s = vl_mb_to_utf8(c);

	/*
	 * If we got no result, then it was not in the cached mapping to 8bit.
	 * But we can still convert it to UTF-8.
	 */
	if (s == NULL) {
	    rc = vl_conv_to_utf8(temp, (UINT) c, sizeof(temp));
	    if (rc > 0) {
		temp[rc] = EOS;
		s = (const char *) temp;
	    }
	}
	if (s != NULL) {
	    while (*s != 0) {
		save_putch(*s++);
	    }
	}
    }
    OUTC_RET 0;
}

/*
 * If the display driver (termcap/terminfo/curses) is capable of
 * reading/writing UTF-8 (or other iconv-supported multibyte sequences),
 * save/intercept the put/get pointers from 'term'.
 */
void
vl_open_mbterm(void)
{
    TRACE(("vl_open_mbterm\n"));

    if (save_getch == NULL) {
	TRACE(("...using vl_mb_getch(%s)\n", encoding2s(kbd_encoding)));
	save_getch = term.getch;
	term.getch = vl_mb_getch;
    }

    if (okCTYPE2(vl_wide_enc)) {
	TRACE(("...using vl_mb_putch\n"));

	if (term.putch != vl_mb_putch) {
	    save_putch = term.putch;
	    term.putch = vl_mb_putch;
	}
	term.set_enc(enc_UTF8);
    }
}

void
vl_close_mbterm(void)
{
    if (save_getch != NULL) {
	term.getch = save_getch;
	save_getch = NULL;
    }

    if (okCTYPE2(vl_wide_enc)) {
	if (save_putch != NULL) {
	    TRACE(("vl_close_mbterm\n"));
	    term.putch = save_putch;

	    term.set_enc(enc_POSIX);

	    save_putch = NULL;
	}
    }
}
#endif /* USE_MBTERM */

#if NO_LEAKS
void
eightbit_leaks(void)
{
    int n;

#if OPT_ICONV_FUNCS
    close_encoding();
#endif

    for (n = 0; n < N_chars; ++n) {
	if (table_8bit_utf8[n].text != NULL) {
	    free(table_8bit_utf8[n].text);
	    table_8bit_utf8[n].text = NULL;
	}
    }
    FreeIfNeeded(vl_wide_enc.locale);
    FreeIfNeeded(vl_narrow_enc.locale);
}
#endif