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
|
/*
* Parse /lib/keyboard to create latin1.h table for kernel.
* mklatinkbd -r prints an array of integers rather than a Rune string literal.
*/
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
int rflag;
int xflag;
enum {
MAXLD = 2, /* latin1.c assumes this is 2 */
};
char *head = ""
"/*\n"
" * This is automatically generated by %s from /lib/keyboard\n"
" * Edit /lib/keyboard instead.\n"
" */\n";
/*
* latin1.c assumes that strlen(ld) is at most 2.
* It also assumes that latintab[i].ld can be a prefix of latintab[j].ld
* only when j < i. We ensure this by sorting the output by prefix length.
* The so array is indexed by the character value.
*/
typedef struct Trie Trie;
struct Trie {
int n; /* of characters r */
char seq[MAXLD+1+1];
Rune r[256];
Trie *link[256];
};
Trie *root;
Trie*
mktrie(char *seq)
{
uchar *q;
Trie **tp;
if(root == nil) {
root = malloc(sizeof *root);
memset(root, 0, sizeof *root);
}
assert(seq[0] != '\0');
tp = &root;
for(q=(uchar*)seq; *(q+1) != '\0'; q++) {
tp = &(*tp)->link[*q];
if(*tp == nil) {
*tp = malloc(sizeof(**tp));
assert(*tp != nil);
memset(*tp, 0, sizeof(**tp));
strcpy((*tp)->seq, seq);
(*tp)->seq[q+1-(uchar*)seq] = '\0';
}
}
assert(*tp != nil);
return *tp;
}
/* add character sequence s meaning rune r */
void
insert(char *s, Rune r)
{
uchar lastc;
int len;
Trie *t;
len = strlen(s);
lastc = (uchar)s[len-1];
t = mktrie(s);
if(t->r[lastc]) {
fprint(2, "warning: table duplicate: %s is %C and %C\n", s, t->r[lastc], r);
return;
}
t->r[lastc] = r;
t->n++;
}
void
cprintchar(Biobuf *b, int c)
{
/* print a byte c safe for a C string. */
switch(c) {
case '\'':
case '\"':
case '\\':
Bprint(b, "\\%c", c);
break;
case '\t':
Bprint(b, "\\t");
break;
default:
if(isascii(c) && isprint(c))
Bprint(b, "%c", c);
else
Bprint(b, "\\x%.2x", c);
break;
}
}
void
cprints(Biobuf *b, char *p)
{
while(*p != '\0')
cprintchar(b, *p++);
}
void
xprint(Biobuf *b, int c)
{
}
void
printtrie(Biobuf *b, Trie *t)
{
int i;
char *p;
for(i=0; i<256; i++)
if(t->link[i])
printtrie(b, t->link[i]);
if(t->n == 0)
return;
if(xflag) {
for(i=0; i<256; i++) {
if(t->r[i] == 0)
continue;
Bprint(b, "<Multi_key>");
for(p=t->seq; *p; p++)
Bprint(b, " %k", *p);
Bprint(b, " %k : \"%C\" U%04X\n", i, t->r[i], t->r[i]);
}
return;
}
Bprint(b, "\t\"");
cprints(b, t->seq);
Bprint(b, "\", \"");
for(i=0; i<256; i++)
if(t->r[i])
cprintchar(b, i);
Bprint(b, "\",\t");
if(rflag) {
Bprint(b, "{");
for(i=0; i<256; i++)
if(t->r[i])
Bprint(b, " 0x%.4ux,", t->r[i]);
Bprint(b, " }");
} else {
Bprint(b, "L\"");
for(i=0; i<256; i++)
if(t->r[i])
Bprint(b, "%C", t->r[i]);
Bprint(b, "\"");
}
Bprint(b, ",\n");
}
void
readfile(char *fname)
{
Biobuf *b;
char *line, *p;
char *seq;
int inseq;
int lineno;
Rune r;
if((b = Bopen(fname, OREAD)) == 0) {
fprint(2, "cannot open \"%s\": %r\n", fname);
exits("open");
}
lineno = 0;
while((line = Brdline(b, '\n')) != 0) {
lineno++;
if(line[0] == '#')
continue;
r = strtol(line, nil, 16);
p = strchr(line, ' ');
if(r == 0 || (p != line+4 && p != line+5) || p[0] != ' ' || (p == line+4 && p[1] != ' ')) {
fprint(2, "%s:%d: cannot parse line\n", fname, lineno);
continue;
}
p = line+6;
/* 00AE Or rO ® registered trade mark sign */
for(inseq=1, seq=p; (uchar)*p < Runeself; p++) {
if(*p == '\0' || isspace(*p)) {
if(inseq && p-seq >= 2) {
*p = '\0';
inseq = 0;
insert(seq, r);
*p = ' ';
}
if(*p == '\0')
break;
} else {
if(!inseq) {
seq = p;
inseq = 1;
}
}
}
}
}
void
usage(void)
{
fprint(2, "usage: mklatinkbd [-r] [/lib/keyboard]\n");
exits("usage");
}
int kfmt(Fmt*);
void
main(int argc, char **argv)
{
int i;
Biobuf bout;
ARGBEGIN{
case 'r': /* print rune values */
rflag = 1;
break;
case 'x':
xflag = 1;
break;
default:
usage();
}ARGEND
if(argc > 1)
usage();
fmtinstall('k', kfmt);
readfile(argc == 1 ? argv[0] : "/dev/stdin");
Binit(&bout, 1, OWRITE);
if(xflag) {
Bprint(&bout, "# Generated by mklatinkbd -x; do not edit.\n");
for(i=0x20; i<0x10000; i++)
Bprint(&bout, "<Multi_key> <X> <%x> <%x> <%x> <%x> : \"%C\" U%04X\n",
(i>>12)&0xf, (i>>8)&0xf, (i>>4)&0xf, i&0xf, i, i);
}
if(root)
printtrie(&bout, root);
exits(0);
}
// X11 key names
struct {
int c;
char *s;
} xkey[] = {
' ', "space",
'!', "exclam",
'"', "quotedbl",
'#', "numbersign",
'$', "dollar",
'%', "percent",
'&', "ampersand",
'\'', "apostrophe",
'(', "parenleft",
')', "parenright",
'*', "asterisk",
'+', "plus",
',', "comma",
'-', "minus",
'.', "period",
'/', "slash",
':', "colon",
';', "semicolon",
'<', "less",
'=', "equal",
'>', "greater",
'?', "question",
'@', "at",
'[', "bracketleft",
'\\', "backslash",
',', "bracketright",
'^', "asciicircum",
'_', "underscore",
'`', "grave",
'{', "braceleft",
'|', "bar",
'}', "braceright",
'~', "asciitilde",
0, 0
};
int
kfmt(Fmt *f)
{
int i, c;
c = va_arg(f->args, int);
for(i=0; xkey[i].s; i++)
if(xkey[i].c == c)
return fmtprint(f, "<%s>", xkey[i].s);
return fmtprint(f, "<%c>", c);
}
|