2 * Functions to define the character set
3 * and do things specific to the character set.
9 * Predefined character sets,
10 * selected by the LESSCHARSET environment variable.
16 { "ascii", "8bcccbcc18b95.b" },
17 { "latin1", "8bcccbcc18b95.33b." },
21 #define IS_BINARY_CHAR 01
22 #define IS_CONTROL_CHAR 02
24 static char chardef[256];
25 static char *binfmt = "\\%o";
26 public int binattr = BLINK;
28 extern char *getenv();
31 * Define a charset, given a description string.
32 * The string consists of 256 letters,
33 * one for each character in the charset.
34 * If the string is shorter than 256 letters, missing letters
35 * are taken to be identical to the last one.
36 * A decimal number followed by a letter is taken to be a
37 * repetition of the letter.
39 * Each letter is one of:
65 v = IS_BINARY_CHAR|IS_CONTROL_CHAR;
68 case '0': case '1': case '2': case '3': case '4':
69 case '5': case '6': case '7': case '8': case '9':
70 n = (10 * n) + (s[-1] - '0');
74 error("invalid chardef", NULL_PARG);
81 if (cp >= chardef + sizeof(chardef))
83 error("chardef longer than 256", NULL_PARG);
92 while (cp < chardef + sizeof(chardef))
97 * Define a charset, given a charset name.
98 * The valid charset names are listed in the "charsets" array.
104 register struct charset *p;
106 if (name == NULL || *name == '\0')
109 for (p = charsets; p->name != NULL; p++)
111 if (strcmp(name, p->name) == 0)
118 error("invalid charset name", NULL_PARG);
124 * Initialize charset data structures.
132 * Try environment variable LESSCHARSET.
133 * If LESSCHARSET is not set, try LESSCHARDEF.
134 * If LESSCHARDEF is not set, default to "ascii" charset.
136 s = getenv("LESSCHARSET");
140 s = getenv("LESSCHARDEF");
141 if (s != NULL && *s != '\0')
147 (void) icharset("ascii");
149 s = getenv("LESSBINFMT");
150 if (s != NULL && *s != '\0')
156 case 'd': binattr = BOLD; break;
157 case 'k': binattr = BLINK; break;
158 case 'u': binattr = UNDERLINE; break;
159 default: binattr = NORMAL; break;
169 * Is a given character a "binary" character?
175 return (chardef[c] & IS_BINARY_CHAR);
179 * Is a given character a "control" character?
185 return (chardef[c] & IS_CONTROL_CHAR);
189 * Return the printable form of a character.
190 * For example, in the "ascii" charset '\3' is printed as "^C".
198 if (!control_char(c))
199 sprintf(buf, "%c", c);
200 else if (!control_char(c ^ 0100))
201 sprintf(buf, "^%c", c ^ 0100);
203 sprintf(buf, binfmt, c);