-
Notifications
You must be signed in to change notification settings - Fork 1
/
ucsnames.c
463 lines (346 loc) · 9.05 KB
/
ucsnames.c
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
#include "def.h"
/*
* Mg3a: Routines to deal with and show Unicode character names.
*/
#ifdef UCSNAMES
/* Name of Unicode database file */
static char unicode_data[NFILEN] = "";
/*
* Test if we have a Unicode database
*/
int
unicode_database_exists()
{
return unicode_data[0] && !access(unicode_data, R_OK);
}
/*
* Set name of Unicode database file. Also allow clearing it.
*/
INT
set_unicode_data(INT f, INT n)
{
char buf[NFILEN], *adjf;
INT s;
s = eread("Unicode Data File: (default none) ", buf, sizeof(buf), EFFILE);
if (s == ABORT) {
return s;
} else if (s == FALSE) {
unicode_data[0] = 0;
eerase();
return TRUE;
}
if ((adjf = adjustname(buf)) == NULL) return FALSE;
stringcopy(unicode_data, adjf, NFILEN);
return TRUE;
}
/*
* Put a null after the second database field (the character name) and
* return a pointer to that field. Return NULL if error.
*/
static char *
ucs_name(char *s)
{
char *cp1, *cp2;
if ((cp1 = strchr(s, ';')) == NULL) return NULL;
if ((cp2 = strchr(cp1 + 1, ';')) == NULL) return NULL;
*cp2 = 0;
return cp1 + 1;
}
/*
* Get next Unicode character name for completion.
*/
char *
getnext_unicode(INT first)
{
static FILE *f;
static char line[UNIDATALEN];
char *cp;
if (unicode_data[0] == 0) return NULL;
if (first) {
if ((f = fopen(unicode_data, "r")) == NULL) return NULL;
}
while (1) {
if (!fgets(line, sizeof(line), f) || ((cp = ucs_name(line)) == NULL)) {
fclose(f);
return NULL;
}
if (ISALPHA(*cp)) break;
}
return cp;
}
/*
* Given a (full, case-insensitive) Unicode character name, return its
* number. Return -1 on error.
*/
INT
ucs_nametonumber(char *s)
{
FILE *f;
char line[UNIDATALEN], *cp;
int val = -1;
if (unicode_data[0] == 0 || !ISALPHA(*s)) return -1;
if ((f = fopen(unicode_data, "r")) == NULL) return -1;
while (fgets(line, sizeof(line), f)) {
if ((cp = ucs_name(line))) {
if (ascii_strcasecmp(cp, s) == 0) {
if (sscanf(line, "%x", &val) != 1) val = -1;
break;
}
} else {
break;
}
}
fclose(f);
return val;
}
/*
* Return the character name for a Unicode codepoint.
* Return the block name (if possible) if the character is not found.
* Return NULL on error.
*/
char *
ucs_numbertoname(INT val)
{
FILE *f;
static char line[UNIDATALEN];
char *cp = NULL, *endstr, *cp2;
long i;
if (unicode_data[0] == 0) return NULL;
if ((f = fopen(unicode_data, "r")) == NULL) return NULL;
while (fgets(line, sizeof(line), f)) {
i = strtol(line, &endstr, 16); // Faster than sscanf
if (endstr != line && i >= val) {
cp = ucs_name(line);
if (i != val) {
if (cp && *cp == '<' && (cp2 = strstr(cp, ", Last>"))) {
strcpy(cp2, ">");
} else {
cp = NULL;
}
}
break;
}
}
fclose(f);
return cp;
}
/*
* List matching Unicode codepoints, with character names. You can
* match either codepoint (with "U+(hex)") or a substring of the two
* first database fields (code and name).
*
* If given an argument, match and show the whole database line.
*/
INT
list_unicode(INT f, INT n)
{
char line[UNIDATALEN], match[UNIDATALEN], matchupper[UNIDATALEN];
char *cp = NULL, outline[UNIDATALEN];
char charnum[30];
FILE *uf;
INT matched, all;
BUFFER *bp;
int val, matchval;
bp = emptyhelpbuffer();
if (bp == NULL) return FALSE;
all = (f & FFARG);
if (unicode_data[0] == 0) {
ewprintf("No Unicode data");
return FALSE;
}
if (eread("Match%s (U+(hex) or string): ", match, sizeof(match), EFUNICODE,
all ? " all" : "") == ABORT)
{
return ABORT;
}
if ((uf = fopen(unicode_data, "r")) == NULL) {
ewprintf("Error opening Unicode Data file: %s", strerror(errno));
return FALSE;
}
stringcopy(matchupper, match, sizeof(matchupper));
upcase_ascii(matchupper);
if (sscanf(matchupper, " U+%x", &matchval) != 1) matchval = -1;
while (fgets(line, sizeof(line), uf)) {
chomp(line);
if ((!all && (cp = ucs_name(line)) == NULL) ||
sscanf(line, "%x;", &val) != 1)
{
ewprintf("Wrong format: %s", line);
fclose(uf);
return FALSE;
}
if (matchval >= 0) {
matched = (matchval == val);
} else {
matched = (strstr(line, match) || strstr(line, matchupper));
}
if (matched) {
snprintf(charnum, sizeof(charnum), "%04X", val);
if (all) {
stringcopy(outline, line, sizeof(outline));
} else {
snprintf(outline, sizeof(outline), "U+%-6s %s", charnum, cp);
}
if (addline_extra(bp, outline, LCUNICODE, charnum) == FALSE) {
fclose(uf);
return FALSE;
}
}
}
fclose(uf);
return popbuftop(bp);
}
/*
* Add a codepoint in a line to a buffer for advanced display of the
* current character.
*/
#define ADVFORMAT "%-6s %-20s %-9s %s"
static INT
add_advanced_display_line(BUFFER *bp, INT f, INT n, int c,
char *charptr, INT charlen, INT screenwidth)
{
char line[UNIDATALEN + 256];
char width[10];
char bytes[30], bytetemp[10];
char unicode[15], hexnum[10];
char *name;
INT decimal = 0, hex = 1;
INT i;
if (((f & FFARG) == FFUNIV && n == 16) || n == 2) {decimal = 1; hex = 0;}
else if (((f & FFARG) == FFUNIV && n == 64) || n == 3) hex = 0;
snprintf(width, sizeof(width), "%d [%d]", (int)ucs_width(c), (int)screenwidth);
bytes[0] = 0;
for (i = 0; i < charlen; i++) {
if (hex) {
snprintf(bytetemp, sizeof(bytetemp), "0x%02x", CHARMASK(charptr[i]));
} else if (decimal) {
snprintf(bytetemp, sizeof(bytetemp), "%d", CHARMASK(charptr[i]));
} else {
snprintf(bytetemp, sizeof(bytetemp), "0%03o", CHARMASK(charptr[i]));
}
stringconcat2(bytes, (i == 0) ? "" : " ", bytetemp, sizeof(bytes));
}
if (hex) {
snprintf(unicode, sizeof(unicode), "U+%04X", c);
} else if (decimal) {
snprintf(unicode, sizeof(unicode), "%d", c);
} else {
snprintf(unicode, sizeof(unicode), "0%03o", c);
}
if ((name = ucs_numbertoname(c)) == NULL) name = "(undefined)";
snprintf(line, sizeof(line), ADVFORMAT, width, bytes, unicode, name);
snprintf(hexnum, sizeof(hexnum), "%x", c);
return addline_extra(bp, line, LCUNICODE, hexnum);
}
#ifdef UCSNAMES2
/*
* Add a database line for "ch" to "bp"
*/
static int
showdatabaseline(BUFFER *bp, int ch)
{
FILE *f;
char line[UNIDATALEN];
char *endstr, hexnum[10];
long i;
if (unicode_data[0] == 0) return 0;
if ((f = fopen(unicode_data, "r")) == NULL) return 0;
while (fgets(line, sizeof(line), f)) {
i = strtol(line, &endstr, 16);
if (endstr != line && i >= ch) {
fclose(f);
if (i != ch) {
snprintf(line, sizeof(line), "(U+%04X not found)", ch);
} else {
chomp(line);
}
snprintf(hexnum, sizeof(hexnum), "%x", ch);
return addline_extra(bp, line, LCUNICODE, hexnum) == TRUE;
}
}
fclose(f);
return 0;
}
/*
* Do a raw database display for the current character.
*/
static INT
showdatabase(BUFFER *bp, LINE *dotp, INT doto, charset_t charset, INT unixlf)
{
INT ret = FALSE, ch, startdoto = doto, nextdoto;
if (doto == llength(dotp)) {
if (!unixlf && !showdatabaseline(bp, 13)) goto end;
if (!showdatabaseline(bp, 10)) goto end;
} else {
while (doto < llength(dotp)) {
ch = ucs_char(charset, dotp, doto, &nextdoto);
if (doto > startdoto && !ucs_nonspacing(ch)) break;
if (!showdatabaseline(bp, ch)) goto end;
doto = nextdoto;
}
}
ret = popbuftop(bp);
end:
free(dotp); // Not lfree
return ret;
}
#endif
/*
* Advanced display of the current character.
*/
INT
advanced_display(INT f, INT n)
{
INT ch, screenwidth;
BUFFER *bp;
INT nextdoto, doto = curwp->w_doto, startdoto = doto;
LINE *dotp;
char line[256];
INT ret = FALSE, bom, unixlf;
charset_t charset;
charset = curbp->charset;
bom = (curbp->b_flag & BFBOM);
unixlf = (curbp->b_flag & BFUNIXLF);
// Must make a copy of the current line if dot is in the help buffer
if ((dotp = lalloc(llength(curwp->w_dotp))) == NULL) return FALSE;
memcpy(ltext(dotp), ltext(curwp->w_dotp), llength(dotp));
bp = emptyhelpbuffer();
if (bp == NULL) goto end;
#ifdef UCSNAMES2
if (((f & FFNUM) && n == 4) || ((f & FFARG) == FFUNIV && n == 256)) {
return showdatabase(bp, dotp, doto, charset, unixlf);
}
#endif
snprintf(line, sizeof(line), "Charset: %s", charsettoname(charset));
if (bom) stringconcat(line, " (with BOM)", sizeof(line));
if (addline(bp, line) == FALSE) goto end;
if (addline(bp, "") == FALSE) goto end;
snprintf(line, sizeof(line), ADVFORMAT, "Width", "Byte(s)", "Unicode", "Name");
if (addline(bp, line) == FALSE) goto end;
make_underline(line);
if (addline(bp, line) == FALSE) goto end;
if (doto == llength(dotp)) {
if (!unixlf) {
if (add_advanced_display_line(bp, f, n, 13, "\xd", 1, 0) == FALSE) goto end;
}
if (add_advanced_display_line(bp, f, n, 10, "\xa", 1, 0) == FALSE) goto end;
} else {
while (doto < llength(dotp)) {
ch = ucs_char(charset, dotp, doto, &nextdoto);
if (doto > startdoto && !ucs_nonspacing(ch)) break;
if (ch == 9) {
screenwidth = getcolumn(curwp, dotp, nextdoto) - getcolumn(curwp, dotp, doto);
} else {
screenwidth = ucs_termwidth(ch);
}
if (add_advanced_display_line(bp, f, n, ch,
<ext(dotp)[doto], nextdoto - doto, screenwidth) == FALSE) goto end;
doto = nextdoto;
}
}
ret = popbuftop(bp);
end:
free(dotp); // Not lfree
return ret;
}
#endif /* UCSNAMES */