2 * Copyright (C) 1984-2019 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
12 * Functions to define the character set
13 * and do things specific to the character set.
25 #if MSDOS_COMPILER==WIN32C
26 #define WIN32_LEAN_AND_MEAN
32 public int utf_mode
= 0;
35 * Predefined character sets,
36 * selected by the LESSCHARSET environment variable.
43 { "ascii", NULL
, "8bcccbcc18b95.b" },
44 { "utf-8", &utf_mode
, "8bcccbcc18b95.b126.bb" },
45 { "iso8859", NULL
, "8bcccbcc18b95.33b." },
46 { "latin3", NULL
, "8bcccbcc18b95.33b5.b8.b15.b4.b12.b18.b12.b." },
47 { "arabic", NULL
, "8bcccbcc18b95.33b.3b.7b2.13b.3b.b26.5b19.b" },
48 { "greek", NULL
, "8bcccbcc18b95.33b4.2b4.b3.b35.b44.b" },
49 { "greek2005", NULL
, "8bcccbcc18b95.33b14.b35.b44.b" },
50 { "hebrew", NULL
, "8bcccbcc18b95.33b.b29.32b28.2b2.b" },
51 { "koi8-r", NULL
, "8bcccbcc18b95.b." },
52 { "KOI8-T", NULL
, "8bcccbcc18b95.b8.b6.b8.b.b.5b7.3b4.b4.b3.b.b.3b." },
53 { "georgianps", NULL
, "8bcccbcc18b95.3b11.4b12.2b." },
54 { "tcvn", NULL
, "b..b...bcccbccbbb7.8b95.b48.5b." },
55 { "TIS-620", NULL
, "8bcccbcc18b95.b.4b.11b7.8b." },
56 { "next", NULL
, "8bcccbcc18b95.bb125.bb" },
57 { "dos", NULL
, "8bcccbcc12bc5b95.b." },
58 { "windows-1251", NULL
, "8bcccbcc12bc5b95.b24.b." },
59 { "windows-1252", NULL
, "8bcccbcc12bc5b95.b.b11.b.2b12.b." },
60 { "windows-1255", NULL
, "8bcccbcc12bc5b95.b.b8.b.5b9.b.4b." },
61 { "ebcdic", NULL
, "5bc6bcc7bcc41b.9b7.9b5.b..8b6.10b6.b9.7b9.8b8.17b3.3b9.7b9.8b8.6b10.b.b.b." },
62 { "IBM-1047", NULL
, "4cbcbc3b9cbccbccbb4c6bcc5b3cbbc4bc4bccbc191.b" },
67 * Support "locale charmap"/nl_langinfo(CODESET) values, as well as others.
76 { "ANSI_X3.4-1968", "ascii" },
77 { "US-ASCII", "ascii" },
78 { "latin1", "iso8859" },
79 { "ISO-8859-1", "iso8859" },
80 { "latin9", "iso8859" },
81 { "ISO-8859-15", "iso8859" },
82 { "latin2", "iso8859" },
83 { "ISO-8859-2", "iso8859" },
84 { "ISO-8859-3", "latin3" },
85 { "latin4", "iso8859" },
86 { "ISO-8859-4", "iso8859" },
87 { "cyrillic", "iso8859" },
88 { "ISO-8859-5", "iso8859" },
89 { "ISO-8859-6", "arabic" },
90 { "ISO-8859-7", "greek" },
91 { "IBM9005", "greek2005" },
92 { "ISO-8859-8", "hebrew" },
93 { "latin5", "iso8859" },
94 { "ISO-8859-9", "iso8859" },
95 { "latin6", "iso8859" },
96 { "ISO-8859-10", "iso8859" },
97 { "latin7", "iso8859" },
98 { "ISO-8859-13", "iso8859" },
99 { "latin8", "iso8859" },
100 { "ISO-8859-14", "iso8859" },
101 { "latin10", "iso8859" },
102 { "ISO-8859-16", "iso8859" },
104 { "EBCDIC-US", "ebcdic" },
105 { "IBM1047", "IBM-1047" },
106 { "KOI8-R", "koi8-r" },
107 { "KOI8-U", "koi8-r" },
108 { "GEORGIAN-PS", "georgianps" },
109 { "TCVN5712-1", "tcvn" },
110 { "NEXTSTEP", "next" },
111 { "windows", "windows-1252" }, /* backward compatibility */
112 { "CP1251", "windows-1251" },
113 { "CP1252", "windows-1252" },
114 { "CP1255", "windows-1255" },
118 #define IS_BINARY_CHAR 01
119 #define IS_CONTROL_CHAR 02
121 static char chardef
[256];
122 static char *binfmt
= NULL
;
123 static char *utfbinfmt
= NULL
;
124 public int binattr
= AT_STANDOUT
;
128 * Define a charset, given a description string.
129 * The string consists of 256 letters,
130 * one for each character in the charset.
131 * If the string is shorter than 256 letters, missing letters
132 * are taken to be identical to the last one.
133 * A decimal number followed by a letter is taken to be a
134 * repetition of the letter.
136 * Each letter is one of:
139 * c control character
163 v
= IS_BINARY_CHAR
|IS_CONTROL_CHAR
;
166 case '0': case '1': case '2': case '3': case '4':
167 case '5': case '6': case '7': case '8': case '9':
168 n
= (10 * n
) + (s
[-1] - '0');
172 error("invalid chardef", NULL_PARG
);
179 if (cp
>= chardef
+ sizeof(chardef
))
181 error("chardef longer than 256", NULL_PARG
);
190 while (cp
< chardef
+ sizeof(chardef
))
195 * Define a charset, given a charset name.
196 * The valid charset names are listed in the "charsets" array.
199 icharset(name
, no_error
)
206 if (name
== NULL
|| *name
== '\0')
209 /* First see if the name is an alias. */
210 for (a
= cs_aliases
; a
->name
!= NULL
; a
++)
212 if (strcmp(name
, a
->name
) == 0)
219 for (p
= charsets
; p
->name
!= NULL
; p
++)
221 if (strcmp(name
, p
->name
) == 0)
224 if (p
->p_flag
!= NULL
)
226 #if MSDOS_COMPILER==WIN32C
227 *(p
->p_flag
) = 1 + (GetConsoleOutputCP() != CP_UTF8
);
237 error("invalid charset name", NULL_PARG
);
245 * Define a charset, given a locale name.
252 for (c
= 0; c
< (int) sizeof(chardef
); c
++)
257 chardef
[c
] = IS_CONTROL_CHAR
;
259 chardef
[c
] = IS_BINARY_CHAR
|IS_CONTROL_CHAR
;
265 * Define the printing format for control (or binary utf) chars.
268 setfmt(s
, fmtvarptr
, attrptr
, default_fmt
)
276 /* It would be too hard to account for width otherwise. */
277 char constant
*t
= s
;
280 if (*t
< ' ' || *t
> '~')
290 if (s
== NULL
|| *s
== '\0' ||
291 (*s
== '*' && (s
[1] == '\0' || s
[2] == '\0' || strchr(s
+ 2, 'n'))) ||
292 (*s
!= '*' && strchr(s
, 'n')))
296 * Select the attributes if it starts with "*".
299 if (*s
== '*' && s
[1] != '\0')
303 case 'd': *attrptr
= AT_BOLD
; break;
304 case 'k': *attrptr
= AT_BLINK
; break;
305 case 's': *attrptr
= AT_STANDOUT
; break;
306 case 'u': *attrptr
= AT_UNDERLINE
; break;
307 default: *attrptr
= AT_NORMAL
; break;
318 set_charset(VOID_PARAM
)
322 #if MSDOS_COMPILER==WIN32C
324 * If the Windows console is using UTF-8, we'll use it too.
326 if (GetConsoleOutputCP() == CP_UTF8
)
327 if (icharset("utf-8", 1))
331 * See if environment variable LESSCHARSET is defined.
333 s
= lgetenv("LESSCHARSET");
338 * LESSCHARSET is not defined: try LESSCHARDEF.
340 s
= lgetenv("LESSCHARDEF");
350 * Try using the codeset name as the charset name.
352 s
= nl_langinfo(CODESET
);
360 * Check whether LC_ALL, LC_CTYPE or LANG look like UTF-8 is used.
362 if ((s
= lgetenv("LC_ALL")) != NULL
||
363 (s
= lgetenv("LC_CTYPE")) != NULL
||
364 (s
= lgetenv("LANG")) != NULL
)
366 if ( strstr(s
, "UTF-8") != NULL
|| strstr(s
, "utf-8") != NULL
367 || strstr(s
, "UTF8") != NULL
|| strstr(s
, "utf8") != NULL
)
368 if (icharset("utf-8", 1))
375 * Get character definitions from locale functions,
376 * rather than from predefined charset entry.
384 (void) icharset("dos", 1);
387 * Default to "latin1".
389 (void) icharset("latin1", 1);
395 * Initialize charset data structures.
398 init_charset(VOID_PARAM
)
403 setlocale(LC_ALL
, "");
408 s
= lgetenv("LESSBINFMT");
409 setfmt(s
, &binfmt
, &binattr
, "*s<%02X>");
411 s
= lgetenv("LESSUTFBINFMT");
412 setfmt(s
, &utfbinfmt
, &binattr
, "<U+%04lX>");
416 * Is a given character a "binary" character?
423 return (is_ubin_char(c
));
425 return (chardef
[c
] & IS_BINARY_CHAR
);
429 * Is a given character a "control" character?
436 return (chardef
[c
] & IS_CONTROL_CHAR
);
440 * Return the printable form of a character.
441 * For example, in the "ascii" charset '\3' is printed as "^C".
447 /* {{ This buffer can be overrun if LESSBINFMT is a long string. }} */
451 if ((c
< 128 || !utf_mode
) && !control_char(c
))
452 SNPRINTF1(buf
, sizeof(buf
), "%c", (int) c
);
456 else if (!binary_char(c
) && c
< 64)
457 SNPRINTF1(buf
, sizeof(buf
), "^%c",
459 * This array roughly inverts CONTROL() #defined in less.h,
460 * and should be kept in sync with CONTROL() and IBM-1047.
466 "..V....D....TU.Z"[c
]);
468 else if (c
< 128 && !control_char(c
^ 0100))
469 SNPRINTF1(buf
, sizeof(buf
), "^%c", (int) (c
^ 0100));
472 SNPRINTF1(buf
, sizeof(buf
), binfmt
, c
);
477 * Return the printable form of a UTF-8 character.
487 else if (ch
< 128 && control_char(ch
))
489 if (!control_char(ch
^ 0100))
490 SNPRINTF1(buf
, sizeof(buf
), "^%c", ((char) ch
) ^ 0100);
492 SNPRINTF1(buf
, sizeof(buf
), binfmt
, (char) ch
);
493 } else if (is_ubin_char(ch
))
495 SNPRINTF1(buf
, sizeof(buf
), utfbinfmt
, ch
);
499 if (ch
>= 0x80000000)
500 ch
= 0xFFFD; /* REPLACEMENT CHARACTER */
508 * Get the length of a UTF-8 character in bytes.
514 if ((ch
& 0x80) == 0)
516 if ((ch
& 0xE0) == 0xC0)
518 if ((ch
& 0xF0) == 0xE0)
520 if ((ch
& 0xF8) == 0xF0)
522 if ((ch
& 0xFC) == 0xF8)
524 if ((ch
& 0xFE) == 0xFC)
526 /* Invalid UTF-8 encoding. */
531 * Does the parameter point to the lead byte of a well-formed UTF-8 character?
534 is_utf8_well_formed(ss
, slen
)
540 unsigned char *s
= (unsigned char *) ss
;
542 if (IS_UTF8_INVALID(s
[0]))
557 mask
= (~((1 << (8-len
)) - 1)) & 0xFF;
558 if (s
[0] == mask
&& (s
[1] & mask
) == 0x80)
562 for (i
= 1; i
< len
; i
++)
563 if (!IS_UTF8_TRAIL(s
[i
]))
569 * Skip bytes until a UTF-8 lead byte (11xxxxxx) or ASCII byte (0xxxxxxx) is found.
572 utf_skip_to_lead(pp
, limit
)
578 } while (*pp
< limit
&& !IS_UTF8_LEAD((*pp
)[0] & 0377) && !IS_ASCII_OCTET((*pp
)[0]));
583 * Get the value of a UTF-8 character.
589 switch (utf_len(p
[0]))
597 /* 110xxxxx 10xxxxxx */
599 ((p
[0] & 0x1F) << 6) |
602 /* 1110xxxx 10xxxxxx 10xxxxxx */
604 ((p
[0] & 0x0F) << 12) |
605 ((p
[1] & 0x3F) << 6) |
608 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
610 ((p
[0] & 0x07) << 18) |
611 ((p
[1] & 0x3F) << 12) |
612 ((p
[2] & 0x3F) << 6) |
615 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
617 ((p
[0] & 0x03) << 24) |
618 ((p
[1] & 0x3F) << 18) |
619 ((p
[2] & 0x3F) << 12) |
620 ((p
[3] & 0x3F) << 6) |
623 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
625 ((p
[0] & 0x01) << 30) |
626 ((p
[1] & 0x3F) << 24) |
627 ((p
[2] & 0x3F) << 18) |
628 ((p
[3] & 0x3F) << 12) |
629 ((p
[4] & 0x3F) << 6) |
635 * Store a character into a UTF-8 string.
642 if (!utf_mode
|| ch
< 0x80)
645 *(*pp
)++ = (char) ch
;
646 } else if (ch
< 0x800)
648 /* 110xxxxx 10xxxxxx */
649 *(*pp
)++ = (char) (0xC0 | ((ch
>> 6) & 0x1F));
650 *(*pp
)++ = (char) (0x80 | (ch
& 0x3F));
651 } else if (ch
< 0x10000)
653 /* 1110xxxx 10xxxxxx 10xxxxxx */
654 *(*pp
)++ = (char) (0xE0 | ((ch
>> 12) & 0x0F));
655 *(*pp
)++ = (char) (0x80 | ((ch
>> 6) & 0x3F));
656 *(*pp
)++ = (char) (0x80 | (ch
& 0x3F));
657 } else if (ch
< 0x200000)
659 /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
660 *(*pp
)++ = (char) (0xF0 | ((ch
>> 18) & 0x07));
661 *(*pp
)++ = (char) (0x80 | ((ch
>> 12) & 0x3F));
662 *(*pp
)++ = (char) (0x80 | ((ch
>> 6) & 0x3F));
663 *(*pp
)++ = (char) (0x80 | (ch
& 0x3F));
664 } else if (ch
< 0x4000000)
666 /* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
667 *(*pp
)++ = (char) (0xF0 | ((ch
>> 24) & 0x03));
668 *(*pp
)++ = (char) (0x80 | ((ch
>> 18) & 0x3F));
669 *(*pp
)++ = (char) (0x80 | ((ch
>> 12) & 0x3F));
670 *(*pp
)++ = (char) (0x80 | ((ch
>> 6) & 0x3F));
671 *(*pp
)++ = (char) (0x80 | (ch
& 0x3F));
674 /* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
675 *(*pp
)++ = (char) (0xF0 | ((ch
>> 30) & 0x01));
676 *(*pp
)++ = (char) (0x80 | ((ch
>> 24) & 0x3F));
677 *(*pp
)++ = (char) (0x80 | ((ch
>> 18) & 0x3F));
678 *(*pp
)++ = (char) (0x80 | ((ch
>> 12) & 0x3F));
679 *(*pp
)++ = (char) (0x80 | ((ch
>> 6) & 0x3F));
680 *(*pp
)++ = (char) (0x80 | (ch
& 0x3F));
685 * Step forward or backward one character in a string.
688 step_char(pp
, dir
, limit
)
691 constant
char *limit
;
699 /* It's easy if chars are one byte. */
701 ch
= (LWCHAR
) (unsigned char) ((p
< limit
) ? *p
++ : 0);
703 ch
= (LWCHAR
) (unsigned char) ((p
> limit
) ? *--p
: 0);
718 while (p
> limit
&& IS_UTF8_TRAIL(p
[-1]))
730 * Unicode characters data
731 * Actual data is in the generated *.uni files.
734 #define DECLARE_RANGE_TABLE_START(name) \
735 static struct wchar_range name##_array[] = {
736 #define DECLARE_RANGE_TABLE_END(name) \
737 }; struct wchar_range_table name##_table = { name##_array, sizeof(name##_array)/sizeof(*name##_array) };
739 DECLARE_RANGE_TABLE_START(compose
)
740 #include "compose.uni"
741 DECLARE_RANGE_TABLE_END(compose
)
743 DECLARE_RANGE_TABLE_START(ubin
)
745 DECLARE_RANGE_TABLE_END(ubin
)
747 DECLARE_RANGE_TABLE_START(wide
)
749 DECLARE_RANGE_TABLE_END(wide
)
751 DECLARE_RANGE_TABLE_START(fmt
)
753 DECLARE_RANGE_TABLE_END(fmt
)
755 /* comb_table is special pairs, not ranges. */
756 static struct wchar_range comb_table
[] = {
757 {0x0644,0x0622}, {0x0644,0x0623}, {0x0644,0x0625}, {0x0644,0x0627},
762 is_in_table(ch
, table
)
764 struct wchar_range_table
*table
;
769 /* Binary search in the table. */
770 if (ch
< table
->table
[0].first
)
773 hi
= table
->count
- 1;
776 int mid
= (lo
+ hi
) / 2;
777 if (ch
> table
->table
[mid
].last
)
779 else if (ch
< table
->table
[mid
].first
)
788 * Is a character a UTF-8 composing character?
789 * If a composing character follows any char, the two combine into one glyph.
792 is_composing_char(ch
)
795 return is_in_table(ch
, &compose_table
) ||
796 (bs_mode
!= BS_CONTROL
&& is_in_table(ch
, &fmt_table
));
800 * Should this UTF-8 character be treated as binary?
806 int ubin
= is_in_table(ch
, &ubin_table
) ||
807 (bs_mode
== BS_CONTROL
&& is_in_table(ch
, &fmt_table
));
808 #if MSDOS_COMPILER==WIN32C
809 if (!ubin
&& utf_mode
== 2 && ch
< 0x10000)
812 * Consider it binary if it can't be converted.
814 BOOL used_default
= TRUE
;
815 WideCharToMultiByte(GetConsoleOutputCP(), WC_NO_BEST_FIT_CHARS
, (LPCWSTR
) &ch
, 1, NULL
, 0, NULL
, &used_default
);
824 * Is this a double width UTF-8 character?
830 return is_in_table(ch
, &wide_table
);
834 * Is a character a UTF-8 combining character?
835 * A combining char acts like an ordinary char, but if it follows
836 * a specific char (not any char), the two combine into one glyph.
839 is_combining_char(ch1
, ch2
)
843 /* The table is small; use linear search. */
845 for (i
= 0; i
< sizeof(comb_table
)/sizeof(*comb_table
); i
++)
847 if (ch1
== comb_table
[i
].first
&&
848 ch2
== comb_table
[i
].last
)