1*838f5788Ssimonb /* $NetBSD: charset.c,v 1.5 2023/10/06 05:49:49 simonb Exp $ */
220006a0bStron
320006a0bStron /*
4*838f5788Ssimonb * Copyright (C) 1984-2023 Mark Nudelman
520006a0bStron *
620006a0bStron * You may distribute under the terms of either the GNU General Public
720006a0bStron * License or the Less License, as specified in the README file.
820006a0bStron *
9ec18bca0Stron * For more information, see the README file.
1020006a0bStron */
1120006a0bStron
1220006a0bStron
1320006a0bStron /*
1420006a0bStron * Functions to define the character set
1520006a0bStron * and do things specific to the character set.
1620006a0bStron */
1720006a0bStron
1820006a0bStron #include "less.h"
1920006a0bStron #if HAVE_LOCALE
2020006a0bStron #include <locale.h>
2120006a0bStron #include <ctype.h>
2220006a0bStron #include <langinfo.h>
2320006a0bStron #endif
2420006a0bStron
2520006a0bStron #include "charset.h"
26*838f5788Ssimonb #include "xbuf.h"
27*838f5788Ssimonb
28*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
29*838f5788Ssimonb #define WIN32_LEAN_AND_MEAN
30*838f5788Ssimonb #include <windows.h>
31*838f5788Ssimonb #endif
32*838f5788Ssimonb
33*838f5788Ssimonb extern int bs_mode;
3420006a0bStron
3520006a0bStron public int utf_mode = 0;
3620006a0bStron
3720006a0bStron /*
3820006a0bStron * Predefined character sets,
3920006a0bStron * selected by the LESSCHARSET environment variable.
4020006a0bStron */
4120006a0bStron struct charset {
4220006a0bStron char *name;
4320006a0bStron int *p_flag;
4420006a0bStron char *desc;
4520006a0bStron } charsets[] = {
4620006a0bStron { "ascii", NULL, "8bcccbcc18b95.b" },
4720006a0bStron { "utf-8", &utf_mode, "8bcccbcc18b95.b126.bb" },
4820006a0bStron { "iso8859", NULL, "8bcccbcc18b95.33b." },
4920006a0bStron { "latin3", NULL, "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
5020006a0bStron { "arabic", NULL, "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
5120006a0bStron { "greek", NULL, "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
5220006a0bStron { "greek2005", NULL, "8bcccbcc18b95.33b14.b35.b44.b" },
5320006a0bStron { "hebrew", NULL, "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
5420006a0bStron { "koi8-r", NULL, "8bcccbcc18b95.b." },
5520006a0bStron { "KOI8-T", NULL, "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
5620006a0bStron { "georgianps", NULL, "8bcccbcc18b95.3b11.4b12.2b." },
5720006a0bStron { "tcvn", NULL, "b..b...bcccbccbbb7.8b95.b48.5b." },
5820006a0bStron { "TIS-620", NULL, "8bcccbcc18b95.b.4b.11b7.8b." },
5920006a0bStron { "next", NULL, "8bcccbcc18b95.bb125.bb" },
6020006a0bStron { "dos", NULL, "8bcccbcc12bc5b95.b." },
6120006a0bStron { "windows-1251", NULL, "8bcccbcc12bc5b95.b24.b." },
6220006a0bStron { "windows-1252", NULL, "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
6320006a0bStron { "windows-1255", NULL, "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
6420006a0bStron { "ebcdic", NULL, "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
6520006a0bStron { "IBM-1047", NULL, "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
6620006a0bStron { NULL, NULL, NULL }
6720006a0bStron };
6820006a0bStron
6920006a0bStron /*
7020006a0bStron * Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
7120006a0bStron */
7220006a0bStron struct cs_alias {
7320006a0bStron char *name;
7420006a0bStron char *oname;
7520006a0bStron } cs_aliases[] = {
7620006a0bStron { "UTF-8", "utf-8" },
77*838f5788Ssimonb { "utf8", "utf-8" },
78*838f5788Ssimonb { "UTF8", "utf-8" },
7920006a0bStron { "ANSI_X3.4-1968", "ascii" },
8020006a0bStron { "US-ASCII", "ascii" },
8120006a0bStron { "latin1", "iso8859" },
8220006a0bStron { "ISO-8859-1", "iso8859" },
8320006a0bStron { "latin9", "iso8859" },
8420006a0bStron { "ISO-8859-15", "iso8859" },
8520006a0bStron { "latin2", "iso8859" },
8620006a0bStron { "ISO-8859-2", "iso8859" },
8720006a0bStron { "ISO-8859-3", "latin3" },
8820006a0bStron { "latin4", "iso8859" },
8920006a0bStron { "ISO-8859-4", "iso8859" },
9020006a0bStron { "cyrillic", "iso8859" },
9120006a0bStron { "ISO-8859-5", "iso8859" },
9220006a0bStron { "ISO-8859-6", "arabic" },
9320006a0bStron { "ISO-8859-7", "greek" },
9420006a0bStron { "IBM9005", "greek2005" },
9520006a0bStron { "ISO-8859-8", "hebrew" },
9620006a0bStron { "latin5", "iso8859" },
9720006a0bStron { "ISO-8859-9", "iso8859" },
9820006a0bStron { "latin6", "iso8859" },
9920006a0bStron { "ISO-8859-10", "iso8859" },
10020006a0bStron { "latin7", "iso8859" },
10120006a0bStron { "ISO-8859-13", "iso8859" },
10220006a0bStron { "latin8", "iso8859" },
10320006a0bStron { "ISO-8859-14", "iso8859" },
10420006a0bStron { "latin10", "iso8859" },
10520006a0bStron { "ISO-8859-16", "iso8859" },
10620006a0bStron { "IBM437", "dos" },
10720006a0bStron { "EBCDIC-US", "ebcdic" },
10820006a0bStron { "IBM1047", "IBM-1047" },
10920006a0bStron { "KOI8-R", "koi8-r" },
11020006a0bStron { "KOI8-U", "koi8-r" },
11120006a0bStron { "GEORGIAN-PS", "georgianps" },
11220006a0bStron { "TCVN5712-1", "tcvn" },
11320006a0bStron { "NEXTSTEP", "next" },
11420006a0bStron { "windows", "windows-1252" }, /* backward compatibility */
11520006a0bStron { "CP1251", "windows-1251" },
11620006a0bStron { "CP1252", "windows-1252" },
11720006a0bStron { "CP1255", "windows-1255" },
11820006a0bStron { NULL, NULL }
11920006a0bStron };
12020006a0bStron
12120006a0bStron #define IS_BINARY_CHAR 01
12220006a0bStron #define IS_CONTROL_CHAR 02
12320006a0bStron
12420006a0bStron static char chardef[256];
12520006a0bStron static char *binfmt = NULL;
12620006a0bStron static char *utfbinfmt = NULL;
127*838f5788Ssimonb public int binattr = AT_STANDOUT|AT_COLOR_BIN;
12820006a0bStron
129*838f5788Ssimonb static struct xbuffer user_wide_array;
130*838f5788Ssimonb static struct xbuffer user_ubin_array;
131*838f5788Ssimonb static struct xbuffer user_compose_array;
132*838f5788Ssimonb static struct xbuffer user_prt_array;
133*838f5788Ssimonb static struct wchar_range_table user_wide_table;
134*838f5788Ssimonb static struct wchar_range_table user_ubin_table;
135*838f5788Ssimonb static struct wchar_range_table user_compose_table;
136*838f5788Ssimonb static struct wchar_range_table user_prt_table;
137*838f5788Ssimonb
138*838f5788Ssimonb /*
139*838f5788Ssimonb * Set a wchar_range_table to the table in an xbuffer.
140*838f5788Ssimonb */
wchar_range_table_set(struct wchar_range_table * tbl,struct xbuffer * arr)141*838f5788Ssimonb static void wchar_range_table_set(struct wchar_range_table *tbl, struct xbuffer *arr)
142*838f5788Ssimonb {
143*838f5788Ssimonb tbl->table = (struct wchar_range *) arr->data;
144*838f5788Ssimonb tbl->count = arr->end / sizeof(struct wchar_range);
145*838f5788Ssimonb }
146*838f5788Ssimonb
147*838f5788Ssimonb /*
148*838f5788Ssimonb * Skip over a "U" or "U+" prefix before a hex codepoint.
149*838f5788Ssimonb */
skip_uprefix(char * s)150*838f5788Ssimonb static char * skip_uprefix(char *s)
151*838f5788Ssimonb {
152*838f5788Ssimonb if (*s == 'U' || *s == 'u')
153*838f5788Ssimonb if (*++s == '+') ++s;
154*838f5788Ssimonb return s;
155*838f5788Ssimonb }
156*838f5788Ssimonb
157*838f5788Ssimonb /*
158*838f5788Ssimonb * Parse a dash-separated range of hex values.
159*838f5788Ssimonb */
wchar_range_get(char ** ss,struct wchar_range * range)160*838f5788Ssimonb static void wchar_range_get(char **ss, struct wchar_range *range)
161*838f5788Ssimonb {
162*838f5788Ssimonb char *s = skip_uprefix(*ss);
163*838f5788Ssimonb range->first = lstrtoul(s, &s, 16);
164*838f5788Ssimonb if (s[0] == '-')
165*838f5788Ssimonb {
166*838f5788Ssimonb s = skip_uprefix(&s[1]);
167*838f5788Ssimonb range->last = lstrtoul(s, &s, 16);
168*838f5788Ssimonb } else
169*838f5788Ssimonb {
170*838f5788Ssimonb range->last = range->first;
171*838f5788Ssimonb }
172*838f5788Ssimonb *ss = s;
173*838f5788Ssimonb }
174*838f5788Ssimonb
175*838f5788Ssimonb /*
176*838f5788Ssimonb * Parse the LESSUTFCHARDEF variable.
177*838f5788Ssimonb */
ichardef_utf(char * s)178*838f5788Ssimonb static void ichardef_utf(char *s)
179*838f5788Ssimonb {
180*838f5788Ssimonb xbuf_init(&user_wide_array);
181*838f5788Ssimonb xbuf_init(&user_ubin_array);
182*838f5788Ssimonb xbuf_init(&user_compose_array);
183*838f5788Ssimonb xbuf_init(&user_prt_array);
184*838f5788Ssimonb
185*838f5788Ssimonb if (s != NULL)
186*838f5788Ssimonb {
187*838f5788Ssimonb while (s[0] != '\0')
188*838f5788Ssimonb {
189*838f5788Ssimonb struct wchar_range range;
190*838f5788Ssimonb wchar_range_get(&s, &range);
191*838f5788Ssimonb if (range.last == 0)
192*838f5788Ssimonb {
193*838f5788Ssimonb error("invalid hex number(s) in LESSUTFCHARDEF", NULL_PARG);
194*838f5788Ssimonb quit(QUIT_ERROR);
195*838f5788Ssimonb }
196*838f5788Ssimonb if (*s++ != ':')
197*838f5788Ssimonb {
198*838f5788Ssimonb error("missing colon in LESSUTFCHARDEF", NULL_PARG);
199*838f5788Ssimonb quit(QUIT_ERROR);
200*838f5788Ssimonb }
201*838f5788Ssimonb switch (*s++)
202*838f5788Ssimonb {
203*838f5788Ssimonb case 'b':
204*838f5788Ssimonb xbuf_add_data(&user_ubin_array, (unsigned char *) &range, sizeof(range));
205*838f5788Ssimonb break;
206*838f5788Ssimonb case 'c':
207*838f5788Ssimonb xbuf_add_data(&user_compose_array, (unsigned char *) &range, sizeof(range));
208*838f5788Ssimonb break;
209*838f5788Ssimonb case 'w':
210*838f5788Ssimonb xbuf_add_data(&user_wide_array, (unsigned char *) &range, sizeof(range));
211*838f5788Ssimonb xbuf_add_data(&user_prt_array, (unsigned char *) &range, sizeof(range));
212*838f5788Ssimonb break;
213*838f5788Ssimonb case 'p': case '.':
214*838f5788Ssimonb xbuf_add_data(&user_prt_array, (unsigned char *) &range, sizeof(range));
215*838f5788Ssimonb break;
216*838f5788Ssimonb case '\0':
217*838f5788Ssimonb s--;
218*838f5788Ssimonb break;
219*838f5788Ssimonb default:
220*838f5788Ssimonb /* Ignore unknown character attribute. */
221*838f5788Ssimonb break;
222*838f5788Ssimonb }
223*838f5788Ssimonb if (s[0] == ',') ++s;
224*838f5788Ssimonb }
225*838f5788Ssimonb }
226*838f5788Ssimonb wchar_range_table_set(&user_wide_table, &user_wide_array);
227*838f5788Ssimonb wchar_range_table_set(&user_ubin_table, &user_ubin_array);
228*838f5788Ssimonb wchar_range_table_set(&user_compose_table, &user_compose_array);
229*838f5788Ssimonb wchar_range_table_set(&user_prt_table, &user_prt_array);
230*838f5788Ssimonb }
23120006a0bStron
23220006a0bStron /*
23320006a0bStron * Define a charset, given a description string.
23420006a0bStron * The string consists of 256 letters,
23520006a0bStron * one for each character in the charset.
23620006a0bStron * If the string is shorter than 256 letters, missing letters
23720006a0bStron * are taken to be identical to the last one.
23820006a0bStron * A decimal number followed by a letter is taken to be a
23920006a0bStron * repetition of the letter.
24020006a0bStron *
24120006a0bStron * Each letter is one of:
24220006a0bStron * . normal character
24320006a0bStron * b binary character
24420006a0bStron * c control character
24520006a0bStron */
ichardef(char * s)246*838f5788Ssimonb static void ichardef(char *s)
24720006a0bStron {
248*838f5788Ssimonb char *cp;
249*838f5788Ssimonb int n;
250*838f5788Ssimonb char v;
25120006a0bStron
25220006a0bStron n = 0;
25320006a0bStron v = 0;
25420006a0bStron cp = chardef;
25520006a0bStron while (*s != '\0')
25620006a0bStron {
25720006a0bStron switch (*s++)
25820006a0bStron {
25920006a0bStron case '.':
26020006a0bStron v = 0;
26120006a0bStron break;
26220006a0bStron case 'c':
26320006a0bStron v = IS_CONTROL_CHAR;
26420006a0bStron break;
26520006a0bStron case 'b':
26620006a0bStron v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
26720006a0bStron break;
26820006a0bStron
26920006a0bStron case '0': case '1': case '2': case '3': case '4':
27020006a0bStron case '5': case '6': case '7': case '8': case '9':
271*838f5788Ssimonb if (ckd_mul(&n, n, 10) || ckd_add(&n, n, s[-1] - '0'))
272*838f5788Ssimonb goto invalid_chardef;
27320006a0bStron continue;
27420006a0bStron
27520006a0bStron default:
276*838f5788Ssimonb invalid_chardef:
27720006a0bStron error("invalid chardef", NULL_PARG);
27820006a0bStron quit(QUIT_ERROR);
27920006a0bStron /*NOTREACHED*/
28020006a0bStron }
28120006a0bStron
28220006a0bStron do
28320006a0bStron {
28420006a0bStron if (cp >= chardef + sizeof(chardef))
28520006a0bStron {
28620006a0bStron error("chardef longer than 256", NULL_PARG);
28720006a0bStron quit(QUIT_ERROR);
28820006a0bStron /*NOTREACHED*/
28920006a0bStron }
29020006a0bStron *cp++ = v;
29120006a0bStron } while (--n > 0);
29220006a0bStron n = 0;
29320006a0bStron }
29420006a0bStron
29520006a0bStron while (cp < chardef + sizeof(chardef))
29620006a0bStron *cp++ = v;
29720006a0bStron }
29820006a0bStron
29920006a0bStron /*
30020006a0bStron * Define a charset, given a charset name.
30120006a0bStron * The valid charset names are listed in the "charsets" array.
30220006a0bStron */
icharset(char * name,int no_error)303*838f5788Ssimonb static int icharset(char *name, int no_error)
30420006a0bStron {
305*838f5788Ssimonb struct charset *p;
306*838f5788Ssimonb struct cs_alias *a;
30720006a0bStron
30820006a0bStron if (name == NULL || *name == '\0')
30920006a0bStron return (0);
31020006a0bStron
31120006a0bStron /* First see if the name is an alias. */
31220006a0bStron for (a = cs_aliases; a->name != NULL; a++)
31320006a0bStron {
31420006a0bStron if (strcmp(name, a->name) == 0)
31520006a0bStron {
31620006a0bStron name = a->oname;
31720006a0bStron break;
31820006a0bStron }
31920006a0bStron }
32020006a0bStron
32120006a0bStron for (p = charsets; p->name != NULL; p++)
32220006a0bStron {
32320006a0bStron if (strcmp(name, p->name) == 0)
32420006a0bStron {
32520006a0bStron ichardef(p->desc);
32620006a0bStron if (p->p_flag != NULL)
327*838f5788Ssimonb {
328*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
329*838f5788Ssimonb *(p->p_flag) = 1 + (GetConsoleOutputCP() != CP_UTF8);
330*838f5788Ssimonb #else
33120006a0bStron *(p->p_flag) = 1;
332*838f5788Ssimonb #endif
333*838f5788Ssimonb }
33420006a0bStron return (1);
33520006a0bStron }
33620006a0bStron }
33720006a0bStron
33820006a0bStron if (!no_error) {
33920006a0bStron error("invalid charset name", NULL_PARG);
34020006a0bStron quit(QUIT_ERROR);
34120006a0bStron }
34220006a0bStron return (0);
34320006a0bStron }
34420006a0bStron
34520006a0bStron #if HAVE_LOCALE
34620006a0bStron /*
34720006a0bStron * Define a charset, given a locale name.
34820006a0bStron */
ilocale(void)349*838f5788Ssimonb static void ilocale(void)
35020006a0bStron {
351*838f5788Ssimonb int c;
35220006a0bStron
35320006a0bStron for (c = 0; c < (int) sizeof(chardef); c++)
35420006a0bStron {
35520006a0bStron if (isprint(c))
35620006a0bStron chardef[c] = 0;
35720006a0bStron else if (iscntrl(c))
35820006a0bStron chardef[c] = IS_CONTROL_CHAR;
35920006a0bStron else
36020006a0bStron chardef[c] = IS_BINARY_CHAR|IS_CONTROL_CHAR;
36120006a0bStron }
36220006a0bStron }
36320006a0bStron #endif
36420006a0bStron
36520006a0bStron /*
36620006a0bStron * Define the printing format for control (or binary utf) chars.
36720006a0bStron */
setfmt(char * s,char ** fmtvarptr,int * attrptr,char * default_fmt,int for_printf)368*838f5788Ssimonb public void setfmt(char *s, char **fmtvarptr, int *attrptr, char *default_fmt, int for_printf)
36920006a0bStron {
37020006a0bStron if (s && utf_mode)
37120006a0bStron {
37220006a0bStron /* It would be too hard to account for width otherwise. */
373*838f5788Ssimonb char constant *t = s;
37420006a0bStron while (*t)
37520006a0bStron {
37620006a0bStron if (*t < ' ' || *t > '~')
37720006a0bStron {
37820006a0bStron s = default_fmt;
37920006a0bStron goto attr;
38020006a0bStron }
38120006a0bStron t++;
38220006a0bStron }
38320006a0bStron }
38420006a0bStron
385*838f5788Ssimonb if (s == NULL || *s == '\0')
386*838f5788Ssimonb s = default_fmt;
387*838f5788Ssimonb else if (for_printf &&
388*838f5788Ssimonb ((*s == '*' && (s[1] == '\0' || s[2] == '\0' || strchr(s + 2, 'n'))) ||
389*838f5788Ssimonb (*s != '*' && strchr(s, 'n'))))
39020006a0bStron /* %n is evil */
39120006a0bStron s = default_fmt;
39220006a0bStron
39320006a0bStron /*
39420006a0bStron * Select the attributes if it starts with "*".
39520006a0bStron */
39620006a0bStron attr:
397*838f5788Ssimonb if (*s == '*' && s[1] != '\0')
39820006a0bStron {
39920006a0bStron switch (s[1])
40020006a0bStron {
401*838f5788Ssimonb case 'd': *attrptr = AT_BOLD; break;
402*838f5788Ssimonb case 'k': *attrptr = AT_BLINK; break;
403*838f5788Ssimonb case 's': *attrptr = AT_STANDOUT; break;
404*838f5788Ssimonb case 'u': *attrptr = AT_UNDERLINE; break;
405*838f5788Ssimonb default: *attrptr = AT_NORMAL; break;
40620006a0bStron }
40720006a0bStron s += 2;
40820006a0bStron }
40920006a0bStron *fmtvarptr = s;
41020006a0bStron }
41120006a0bStron
41220006a0bStron /*
41320006a0bStron *
41420006a0bStron */
set_charset(void)415*838f5788Ssimonb static void set_charset(void)
41620006a0bStron {
41720006a0bStron char *s;
41820006a0bStron
419*838f5788Ssimonb #if MSDOS_COMPILER==WIN32C
420*838f5788Ssimonb /*
421*838f5788Ssimonb * If the Windows console is using UTF-8, we'll use it too.
422*838f5788Ssimonb */
423*838f5788Ssimonb if (GetConsoleOutputCP() == CP_UTF8)
424*838f5788Ssimonb if (icharset("utf-8", 1))
425*838f5788Ssimonb return;
426*838f5788Ssimonb #endif
427*838f5788Ssimonb
428*838f5788Ssimonb ichardef_utf(lgetenv("LESSUTFCHARDEF"));
429*838f5788Ssimonb
43020006a0bStron /*
43120006a0bStron * See if environment variable LESSCHARSET is defined.
43220006a0bStron */
43320006a0bStron s = lgetenv("LESSCHARSET");
43420006a0bStron if (icharset(s, 0))
43520006a0bStron return;
43620006a0bStron
43720006a0bStron /*
43820006a0bStron * LESSCHARSET is not defined: try LESSCHARDEF.
43920006a0bStron */
44020006a0bStron s = lgetenv("LESSCHARDEF");
441*838f5788Ssimonb if (!isnullenv(s))
44220006a0bStron {
44320006a0bStron ichardef(s);
44420006a0bStron return;
44520006a0bStron }
44620006a0bStron
44720006a0bStron #if HAVE_LOCALE
44820006a0bStron #ifdef CODESET
44920006a0bStron /*
45020006a0bStron * Try using the codeset name as the charset name.
45120006a0bStron */
45220006a0bStron s = nl_langinfo(CODESET);
45320006a0bStron if (icharset(s, 1))
45420006a0bStron return;
45520006a0bStron #endif
45620006a0bStron #endif
45720006a0bStron
45820006a0bStron #if HAVE_STRSTR
45920006a0bStron /*
46020006a0bStron * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
46120006a0bStron */
46220006a0bStron if ((s = lgetenv("LC_ALL")) != NULL ||
46320006a0bStron (s = lgetenv("LC_CTYPE")) != NULL ||
46420006a0bStron (s = lgetenv("LANG")) != NULL)
46520006a0bStron {
46620006a0bStron if ( strstr(s, "UTF-8") != NULL || strstr(s, "utf-8") != NULL
46720006a0bStron || strstr(s, "UTF8") != NULL || strstr(s, "utf8") != NULL)
46820006a0bStron if (icharset("utf-8", 1))
46920006a0bStron return;
47020006a0bStron }
47120006a0bStron #endif
47220006a0bStron
47320006a0bStron #if HAVE_LOCALE
47420006a0bStron /*
47520006a0bStron * Get character definitions from locale functions,
47620006a0bStron * rather than from predefined charset entry.
47720006a0bStron */
47820006a0bStron ilocale();
479*838f5788Ssimonb #else
48020006a0bStron #if MSDOS_COMPILER
48120006a0bStron /*
48220006a0bStron * Default to "dos".
48320006a0bStron */
48420006a0bStron (void) icharset("dos", 1);
48520006a0bStron #else
48620006a0bStron /*
48720006a0bStron * Default to "latin1".
48820006a0bStron */
48920006a0bStron (void) icharset("latin1", 1);
49020006a0bStron #endif
49120006a0bStron #endif
49220006a0bStron }
49320006a0bStron
49420006a0bStron /*
49520006a0bStron * Initialize charset data structures.
49620006a0bStron */
init_charset(void)497*838f5788Ssimonb public void init_charset(void)
49820006a0bStron {
49920006a0bStron char *s;
50020006a0bStron
50120006a0bStron #if HAVE_LOCALE
50220006a0bStron setlocale(LC_ALL, "");
50320006a0bStron #endif
50420006a0bStron
50520006a0bStron set_charset();
50620006a0bStron
50720006a0bStron s = lgetenv("LESSBINFMT");
508*838f5788Ssimonb setfmt(s, &binfmt, &binattr, "*s<%02X>", TRUE);
50920006a0bStron
51020006a0bStron s = lgetenv("LESSUTFBINFMT");
511*838f5788Ssimonb setfmt(s, &utfbinfmt, &binattr, "<U+%04lX>", TRUE);
51220006a0bStron }
51320006a0bStron
51420006a0bStron /*
51520006a0bStron * Is a given character a "binary" character?
51620006a0bStron */
binary_char(LWCHAR c)517*838f5788Ssimonb public int binary_char(LWCHAR c)
51820006a0bStron {
51920006a0bStron if (utf_mode)
52020006a0bStron return (is_ubin_char(c));
52120006a0bStron c &= 0377;
52220006a0bStron return (chardef[c] & IS_BINARY_CHAR);
52320006a0bStron }
52420006a0bStron
52520006a0bStron /*
52620006a0bStron * Is a given character a "control" character?
52720006a0bStron */
control_char(LWCHAR c)528*838f5788Ssimonb public int control_char(LWCHAR c)
52920006a0bStron {
53020006a0bStron c &= 0377;
53120006a0bStron return (chardef[c] & IS_CONTROL_CHAR);
53220006a0bStron }
53320006a0bStron
53420006a0bStron /*
53520006a0bStron * Return the printable form of a character.
53620006a0bStron * For example, in the "ascii" charset '\3' is printed as "^C".
53720006a0bStron */
prchar(LWCHAR c)538*838f5788Ssimonb public char * prchar(LWCHAR c)
53920006a0bStron {
54020006a0bStron /* {{ This buffer can be overrun if LESSBINFMT is a long string. }} */
541*838f5788Ssimonb static char buf[MAX_PRCHAR_LEN+1];
54220006a0bStron
54320006a0bStron c &= 0377;
54420006a0bStron if ((c < 128 || !utf_mode) && !control_char(c))
54520006a0bStron SNPRINTF1(buf, sizeof(buf), "%c", (int) c);
54620006a0bStron else if (c == ESC)
54720006a0bStron strcpy(buf, "ESC");
54820006a0bStron #if IS_EBCDIC_HOST
54920006a0bStron else if (!binary_char(c) && c < 64)
55020006a0bStron SNPRINTF1(buf, sizeof(buf), "^%c",
55120006a0bStron /*
55220006a0bStron * This array roughly inverts CONTROL() #defined in less.h,
55320006a0bStron * and should be kept in sync with CONTROL() and IBM-1047.
55420006a0bStron */
55520006a0bStron "@ABC.I.?...KLMNO"
55620006a0bStron "PQRS.JH.XY.."
55720006a0bStron "\\]^_"
55820006a0bStron "......W[.....EFG"
55920006a0bStron "..V....D....TU.Z"[c]);
56020006a0bStron #else
56120006a0bStron else if (c < 128 && !control_char(c ^ 0100))
56220006a0bStron SNPRINTF1(buf, sizeof(buf), "^%c", (int) (c ^ 0100));
56320006a0bStron #endif
56420006a0bStron else
56520006a0bStron SNPRINTF1(buf, sizeof(buf), binfmt, c);
56620006a0bStron return (buf);
56720006a0bStron }
56820006a0bStron
56920006a0bStron /*
57020006a0bStron * Return the printable form of a UTF-8 character.
57120006a0bStron */
prutfchar(LWCHAR ch)572*838f5788Ssimonb public char * prutfchar(LWCHAR ch)
57320006a0bStron {
574*838f5788Ssimonb static char buf[MAX_PRCHAR_LEN+1];
57520006a0bStron
57620006a0bStron if (ch == ESC)
57720006a0bStron strcpy(buf, "ESC");
57820006a0bStron else if (ch < 128 && control_char(ch))
57920006a0bStron {
58020006a0bStron if (!control_char(ch ^ 0100))
58120006a0bStron SNPRINTF1(buf, sizeof(buf), "^%c", ((char) ch) ^ 0100);
58220006a0bStron else
58320006a0bStron SNPRINTF1(buf, sizeof(buf), binfmt, (char) ch);
58420006a0bStron } else if (is_ubin_char(ch))
585*838f5788Ssimonb {
58620006a0bStron SNPRINTF1(buf, sizeof(buf), utfbinfmt, ch);
58720006a0bStron } else
58820006a0bStron {
589*838f5788Ssimonb char *p = buf;
590*838f5788Ssimonb if (ch >= 0x80000000)
591*838f5788Ssimonb ch = 0xFFFD; /* REPLACEMENT CHARACTER */
592*838f5788Ssimonb put_wchar(&p, ch);
593*838f5788Ssimonb *p = '\0';
59420006a0bStron }
59520006a0bStron return (buf);
59620006a0bStron }
59720006a0bStron
59820006a0bStron /*
59920006a0bStron * Get the length of a UTF-8 character in bytes.
60020006a0bStron */
utf_len(int ch)601*838f5788Ssimonb public int utf_len(int ch)
60220006a0bStron {
60320006a0bStron if ((ch & 0x80) == 0)
60420006a0bStron return 1;
60520006a0bStron if ((ch & 0xE0) == 0xC0)
60620006a0bStron return 2;
60720006a0bStron if ((ch & 0xF0) == 0xE0)
60820006a0bStron return 3;
60920006a0bStron if ((ch & 0xF8) == 0xF0)
61020006a0bStron return 4;
61120006a0bStron if ((ch & 0xFC) == 0xF8)
61220006a0bStron return 5;
61320006a0bStron if ((ch & 0xFE) == 0xFC)
61420006a0bStron return 6;
61520006a0bStron /* Invalid UTF-8 encoding. */
61620006a0bStron return 1;
61720006a0bStron }
61820006a0bStron
61920006a0bStron /*
620*838f5788Ssimonb * Does the parameter point to the lead byte of a well-formed UTF-8 character?
62120006a0bStron */
is_utf8_well_formed(char * ss,int slen)622*838f5788Ssimonb public int is_utf8_well_formed(char *ss, int slen)
62320006a0bStron {
62420006a0bStron int i;
62520006a0bStron int len;
626*838f5788Ssimonb unsigned char *s = (unsigned char *) ss;
62720006a0bStron
62820006a0bStron if (IS_UTF8_INVALID(s[0]))
62920006a0bStron return (0);
63020006a0bStron
631*838f5788Ssimonb len = utf_len(s[0]);
632*838f5788Ssimonb if (len > slen)
633*838f5788Ssimonb return (0);
63420006a0bStron if (len == 1)
63520006a0bStron return (1);
63620006a0bStron if (len == 2)
63720006a0bStron {
63820006a0bStron if (s[0] < 0xC2)
63920006a0bStron return (0);
64020006a0bStron } else
64120006a0bStron {
64220006a0bStron unsigned char mask;
64320006a0bStron mask = (~((1 << (8-len)) - 1)) & 0xFF;
64420006a0bStron if (s[0] == mask && (s[1] & mask) == 0x80)
64520006a0bStron return (0);
64620006a0bStron }
64720006a0bStron
64820006a0bStron for (i = 1; i < len; i++)
64920006a0bStron if (!IS_UTF8_TRAIL(s[i]))
65020006a0bStron return (0);
65120006a0bStron return (1);
65220006a0bStron }
65320006a0bStron
65420006a0bStron /*
655*838f5788Ssimonb * Skip bytes until a UTF-8 lead byte (11xxxxxx) or ASCII byte (0xxxxxxx) is found.
656*838f5788Ssimonb */
utf_skip_to_lead(char ** pp,char * limit)657*838f5788Ssimonb public void utf_skip_to_lead(char **pp, char *limit)
658*838f5788Ssimonb {
659*838f5788Ssimonb do {
660*838f5788Ssimonb ++(*pp);
661*838f5788Ssimonb } while (*pp < limit && !IS_UTF8_LEAD((*pp)[0] & 0377) && !IS_ASCII_OCTET((*pp)[0]));
662*838f5788Ssimonb }
663*838f5788Ssimonb
664*838f5788Ssimonb
665*838f5788Ssimonb /*
66620006a0bStron * Get the value of a UTF-8 character.
66720006a0bStron */
get_wchar(constant char * p)668*838f5788Ssimonb public LWCHAR get_wchar(constant char *p)
66920006a0bStron {
67020006a0bStron switch (utf_len(p[0]))
67120006a0bStron {
67220006a0bStron case 1:
67320006a0bStron default:
67420006a0bStron /* 0xxxxxxx */
67520006a0bStron return (LWCHAR)
67620006a0bStron (p[0] & 0xFF);
67720006a0bStron case 2:
67820006a0bStron /* 110xxxxx 10xxxxxx */
67920006a0bStron return (LWCHAR) (
68020006a0bStron ((p[0] & 0x1F) << 6) |
68120006a0bStron (p[1] & 0x3F));
68220006a0bStron case 3:
68320006a0bStron /* 1110xxxx 10xxxxxx 10xxxxxx */
68420006a0bStron return (LWCHAR) (
68520006a0bStron ((p[0] & 0x0F) << 12) |
68620006a0bStron ((p[1] & 0x3F) << 6) |
68720006a0bStron (p[2] & 0x3F));
68820006a0bStron case 4:
68920006a0bStron /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
69020006a0bStron return (LWCHAR) (
69120006a0bStron ((p[0] & 0x07) << 18) |
69220006a0bStron ((p[1] & 0x3F) << 12) |
69320006a0bStron ((p[2] & 0x3F) << 6) |
69420006a0bStron (p[3] & 0x3F));
69520006a0bStron case 5:
69620006a0bStron /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
69720006a0bStron return (LWCHAR) (
69820006a0bStron ((p[0] & 0x03) << 24) |
69920006a0bStron ((p[1] & 0x3F) << 18) |
70020006a0bStron ((p[2] & 0x3F) << 12) |
70120006a0bStron ((p[3] & 0x3F) << 6) |
70220006a0bStron (p[4] & 0x3F));
70320006a0bStron case 6:
70420006a0bStron /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
70520006a0bStron return (LWCHAR) (
70620006a0bStron ((p[0] & 0x01) << 30) |
70720006a0bStron ((p[1] & 0x3F) << 24) |
70820006a0bStron ((p[2] & 0x3F) << 18) |
70920006a0bStron ((p[3] & 0x3F) << 12) |
71020006a0bStron ((p[4] & 0x3F) << 6) |
71120006a0bStron (p[5] & 0x3F));
71220006a0bStron }
71320006a0bStron }
71420006a0bStron
71520006a0bStron /*
71620006a0bStron * Store a character into a UTF-8 string.
71720006a0bStron */
put_wchar(char ** pp,LWCHAR ch)718*838f5788Ssimonb public void put_wchar(char **pp, LWCHAR ch)
71920006a0bStron {
72020006a0bStron if (!utf_mode || ch < 0x80)
72120006a0bStron {
72220006a0bStron /* 0xxxxxxx */
72320006a0bStron *(*pp)++ = (char) ch;
72420006a0bStron } else if (ch < 0x800)
72520006a0bStron {
72620006a0bStron /* 110xxxxx 10xxxxxx */
72720006a0bStron *(*pp)++ = (char) (0xC0 | ((ch >> 6) & 0x1F));
72820006a0bStron *(*pp)++ = (char) (0x80 | (ch & 0x3F));
72920006a0bStron } else if (ch < 0x10000)
73020006a0bStron {
73120006a0bStron /* 1110xxxx 10xxxxxx 10xxxxxx */
73220006a0bStron *(*pp)++ = (char) (0xE0 | ((ch >> 12) & 0x0F));
73320006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
73420006a0bStron *(*pp)++ = (char) (0x80 | (ch & 0x3F));
73520006a0bStron } else if (ch < 0x200000)
73620006a0bStron {
73720006a0bStron /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
73820006a0bStron *(*pp)++ = (char) (0xF0 | ((ch >> 18) & 0x07));
73920006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
74020006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
74120006a0bStron *(*pp)++ = (char) (0x80 | (ch & 0x3F));
74220006a0bStron } else if (ch < 0x4000000)
74320006a0bStron {
74420006a0bStron /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
74520006a0bStron *(*pp)++ = (char) (0xF0 | ((ch >> 24) & 0x03));
74620006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
74720006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
74820006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
74920006a0bStron *(*pp)++ = (char) (0x80 | (ch & 0x3F));
75020006a0bStron } else
75120006a0bStron {
75220006a0bStron /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
75320006a0bStron *(*pp)++ = (char) (0xF0 | ((ch >> 30) & 0x01));
75420006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 24) & 0x3F));
75520006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 18) & 0x3F));
75620006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 12) & 0x3F));
75720006a0bStron *(*pp)++ = (char) (0x80 | ((ch >> 6) & 0x3F));
75820006a0bStron *(*pp)++ = (char) (0x80 | (ch & 0x3F));
75920006a0bStron }
76020006a0bStron }
76120006a0bStron
76220006a0bStron /*
76320006a0bStron * Step forward or backward one character in a string.
76420006a0bStron */
step_char(char ** pp,signed int dir,constant char * limit)765*838f5788Ssimonb public LWCHAR step_char(char **pp, signed int dir, constant char *limit)
76620006a0bStron {
76720006a0bStron LWCHAR ch;
76820006a0bStron int len;
76920006a0bStron char *p = *pp;
77020006a0bStron
77120006a0bStron if (!utf_mode)
77220006a0bStron {
77320006a0bStron /* It's easy if chars are one byte. */
77420006a0bStron if (dir > 0)
775*838f5788Ssimonb ch = (LWCHAR) (unsigned char) ((p < limit) ? *p++ : 0);
77620006a0bStron else
777*838f5788Ssimonb ch = (LWCHAR) (unsigned char) ((p > limit) ? *--p : 0);
77820006a0bStron } else if (dir > 0)
77920006a0bStron {
78020006a0bStron len = utf_len(*p);
78120006a0bStron if (p + len > limit)
78220006a0bStron {
78320006a0bStron ch = 0;
784*838f5788Ssimonb p = (char *) limit;
78520006a0bStron } else
78620006a0bStron {
78720006a0bStron ch = get_wchar(p);
78820006a0bStron p += len;
78920006a0bStron }
79020006a0bStron } else
79120006a0bStron {
79220006a0bStron while (p > limit && IS_UTF8_TRAIL(p[-1]))
79320006a0bStron p--;
79420006a0bStron if (p > limit)
79520006a0bStron ch = get_wchar(--p);
79620006a0bStron else
79720006a0bStron ch = 0;
79820006a0bStron }
79920006a0bStron *pp = p;
80020006a0bStron return ch;
80120006a0bStron }
80220006a0bStron
80320006a0bStron /*
80420006a0bStron * Unicode characters data
805*838f5788Ssimonb * Actual data is in the generated *.uni files.
80620006a0bStron */
80720006a0bStron
808*838f5788Ssimonb #define DECLARE_RANGE_TABLE_START(name) \
809*838f5788Ssimonb static struct wchar_range name##_array[] = {
810*838f5788Ssimonb #define DECLARE_RANGE_TABLE_END(name) \
811*838f5788Ssimonb }; struct wchar_range_table name##_table = { name##_array, sizeof(name##_array)/sizeof(*name##_array) };
81220006a0bStron
813*838f5788Ssimonb DECLARE_RANGE_TABLE_START(compose)
814*838f5788Ssimonb #include "compose.uni"
815*838f5788Ssimonb DECLARE_RANGE_TABLE_END(compose)
816*838f5788Ssimonb
817*838f5788Ssimonb DECLARE_RANGE_TABLE_START(ubin)
818*838f5788Ssimonb #include "ubin.uni"
819*838f5788Ssimonb DECLARE_RANGE_TABLE_END(ubin)
820*838f5788Ssimonb
821*838f5788Ssimonb DECLARE_RANGE_TABLE_START(wide)
822*838f5788Ssimonb #include "wide.uni"
823*838f5788Ssimonb DECLARE_RANGE_TABLE_END(wide)
824*838f5788Ssimonb
825*838f5788Ssimonb DECLARE_RANGE_TABLE_START(fmt)
826*838f5788Ssimonb #include "fmt.uni"
827*838f5788Ssimonb DECLARE_RANGE_TABLE_END(fmt)
828*838f5788Ssimonb
829*838f5788Ssimonb /* comb_table is special pairs, not ranges. */
83020006a0bStron static struct wchar_range comb_table[] = {
83120006a0bStron {0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
83220006a0bStron };
83320006a0bStron
83420006a0bStron
is_in_table(LWCHAR ch,struct wchar_range_table * table)835*838f5788Ssimonb static int is_in_table(LWCHAR ch, struct wchar_range_table *table)
83620006a0bStron {
83720006a0bStron int hi;
83820006a0bStron int lo;
83920006a0bStron
84020006a0bStron /* Binary search in the table. */
841*838f5788Ssimonb if (table->table == NULL || table->count == 0 || ch < table->table[0].first)
84220006a0bStron return 0;
84320006a0bStron lo = 0;
844*838f5788Ssimonb hi = table->count - 1;
84520006a0bStron while (lo <= hi)
84620006a0bStron {
84720006a0bStron int mid = (lo + hi) / 2;
848*838f5788Ssimonb if (ch > table->table[mid].last)
84920006a0bStron lo = mid + 1;
850*838f5788Ssimonb else if (ch < table->table[mid].first)
85120006a0bStron hi = mid - 1;
85220006a0bStron else
85320006a0bStron return 1;
85420006a0bStron }
85520006a0bStron return 0;
85620006a0bStron }
85720006a0bStron
85820006a0bStron /*
85920006a0bStron * Is a character a UTF-8 composing character?
86020006a0bStron * If a composing character follows any char, the two combine into one glyph.
86120006a0bStron */
is_composing_char(LWCHAR ch)862*838f5788Ssimonb public int is_composing_char(LWCHAR ch)
86320006a0bStron {
864*838f5788Ssimonb if (is_in_table(ch, &user_prt_table)) return 0;
865*838f5788Ssimonb return is_in_table(ch, &user_compose_table) ||
866*838f5788Ssimonb is_in_table(ch, &compose_table) ||
867*838f5788Ssimonb (bs_mode != BS_CONTROL && is_in_table(ch, &fmt_table));
86820006a0bStron }
86920006a0bStron
87020006a0bStron /*
87120006a0bStron * Should this UTF-8 character be treated as binary?
87220006a0bStron */
is_ubin_char(LWCHAR ch)873*838f5788Ssimonb public int is_ubin_char(LWCHAR ch)
87420006a0bStron {
875*838f5788Ssimonb if (is_in_table(ch, &user_prt_table)) return 0;
876*838f5788Ssimonb return is_in_table(ch, &user_ubin_table) ||
877*838f5788Ssimonb is_in_table(ch, &ubin_table) ||
878*838f5788Ssimonb (bs_mode == BS_CONTROL && is_in_table(ch, &fmt_table));
87920006a0bStron }
88020006a0bStron
88120006a0bStron /*
88220006a0bStron * Is this a double width UTF-8 character?
88320006a0bStron */
is_wide_char(LWCHAR ch)884*838f5788Ssimonb public int is_wide_char(LWCHAR ch)
88520006a0bStron {
886*838f5788Ssimonb return is_in_table(ch, &user_wide_table) ||
887*838f5788Ssimonb is_in_table(ch, &wide_table);
88820006a0bStron }
88920006a0bStron
89020006a0bStron /*
89120006a0bStron * Is a character a UTF-8 combining character?
89220006a0bStron * A combining char acts like an ordinary char, but if it follows
89320006a0bStron * a specific char (not any char), the two combine into one glyph.
89420006a0bStron */
is_combining_char(LWCHAR ch1,LWCHAR ch2)895*838f5788Ssimonb public int is_combining_char(LWCHAR ch1, LWCHAR ch2)
89620006a0bStron {
89720006a0bStron /* The table is small; use linear search. */
89820006a0bStron int i;
89920006a0bStron for (i = 0; i < sizeof(comb_table)/sizeof(*comb_table); i++)
90020006a0bStron {
90120006a0bStron if (ch1 == comb_table[i].first &&
90220006a0bStron ch2 == comb_table[i].last)
90320006a0bStron return 1;
90420006a0bStron }
90520006a0bStron return 0;
90620006a0bStron }
90720006a0bStron
908