2 bb7ff349 2009-07-08 mt * Parse /lib/keyboard to create latin1.h table for kernel.
3 bb7ff349 2009-07-08 mt * mklatinkbd -r prints an array of integers rather than a Rune string literal.
7 bb7ff349 2009-07-08 mt #include <libc.h>
8 bb7ff349 2009-07-08 mt #include <bio.h>
9 bb7ff349 2009-07-08 mt #include <ctype.h>
15 bb7ff349 2009-07-08 mt MAXLD = 2, /* latin1.c assumes this is 2 */
18 bb7ff349 2009-07-08 mt char *head = ""
20 bb7ff349 2009-07-08 mt " * This is automatically generated by %s from /lib/keyboard\n"
21 bb7ff349 2009-07-08 mt " * Edit /lib/keyboard instead.\n"
25 bb7ff349 2009-07-08 mt * latin1.c assumes that strlen(ld) is at most 2.
26 bb7ff349 2009-07-08 mt * It also assumes that latintab[i].ld can be a prefix of latintab[j].ld
27 bb7ff349 2009-07-08 mt * only when j < i. We ensure this by sorting the output by prefix length.
28 bb7ff349 2009-07-08 mt * The so array is indexed by the character value.
31 bb7ff349 2009-07-08 mt typedef struct Trie Trie;
33 bb7ff349 2009-07-08 mt int n; /* of characters r */
34 42ef984c 2011-04-04 rsc char seq[MAXLD+1+1];
36 bb7ff349 2009-07-08 mt Trie *link[256];
42 bb7ff349 2009-07-08 mt mktrie(char *seq)
47 bb7ff349 2009-07-08 mt if(root == nil) {
48 bb7ff349 2009-07-08 mt root = malloc(sizeof *root);
49 bb7ff349 2009-07-08 mt memset(root, 0, sizeof *root);
52 bb7ff349 2009-07-08 mt assert(seq[0] != '\0');
55 bb7ff349 2009-07-08 mt for(q=(uchar*)seq; *(q+1) != '\0'; q++) {
56 bb7ff349 2009-07-08 mt tp = &(*tp)->link[*q];
57 bb7ff349 2009-07-08 mt if(*tp == nil) {
58 bb7ff349 2009-07-08 mt *tp = malloc(sizeof(**tp));
59 bb7ff349 2009-07-08 mt assert(*tp != nil);
60 bb7ff349 2009-07-08 mt memset(*tp, 0, sizeof(**tp));
61 bb7ff349 2009-07-08 mt strcpy((*tp)->seq, seq);
62 bb7ff349 2009-07-08 mt (*tp)->seq[q+1-(uchar*)seq] = '\0';
66 bb7ff349 2009-07-08 mt assert(*tp != nil);
70 bb7ff349 2009-07-08 mt /* add character sequence s meaning rune r */
72 bb7ff349 2009-07-08 mt insert(char *s, Rune r)
78 bb7ff349 2009-07-08 mt len = strlen(s);
79 bb7ff349 2009-07-08 mt lastc = (uchar)s[len-1];
81 bb7ff349 2009-07-08 mt t = mktrie(s);
82 bb7ff349 2009-07-08 mt if(t->r[lastc]) {
83 bb7ff349 2009-07-08 mt fprint(2, "warning: table duplicate: %s is %C and %C\n", s, t->r[lastc], r);
86 bb7ff349 2009-07-08 mt t->r[lastc] = r;
91 bb7ff349 2009-07-08 mt cprintchar(Biobuf *b, int c)
93 bb7ff349 2009-07-08 mt /* print a byte c safe for a C string. */
98 bb7ff349 2009-07-08 mt Bprint(b, "\\%c", c);
101 bb7ff349 2009-07-08 mt Bprint(b, "\\t");
104 bb7ff349 2009-07-08 mt if(isascii(c) && isprint(c))
105 bb7ff349 2009-07-08 mt Bprint(b, "%c", c);
107 bb7ff349 2009-07-08 mt Bprint(b, "\\x%.2x", c);
113 bb7ff349 2009-07-08 mt cprints(Biobuf *b, char *p)
115 bb7ff349 2009-07-08 mt while(*p != '\0')
116 bb7ff349 2009-07-08 mt cprintchar(b, *p++);
120 b567422f 2011-01-02 rsc xprint(Biobuf *b, int c)
125 bb7ff349 2009-07-08 mt printtrie(Biobuf *b, Trie *t)
130 bb7ff349 2009-07-08 mt for(i=0; i<256; i++)
131 bb7ff349 2009-07-08 mt if(t->link[i])
132 bb7ff349 2009-07-08 mt printtrie(b, t->link[i]);
133 b567422f 2011-01-02 rsc if(t->n == 0)
137 b567422f 2011-01-02 rsc for(i=0; i<256; i++) {
138 b567422f 2011-01-02 rsc if(t->r[i] == 0)
140 b567422f 2011-01-02 rsc Bprint(b, "<Multi_key>");
141 b567422f 2011-01-02 rsc for(p=t->seq; *p; p++)
142 b567422f 2011-01-02 rsc Bprint(b, " %k", *p);
143 b567422f 2011-01-02 rsc Bprint(b, " %k : \"%C\" U%04X\n", i, t->r[i], t->r[i]);
148 b567422f 2011-01-02 rsc Bprint(b, "\t\"");
149 b567422f 2011-01-02 rsc cprints(b, t->seq);
150 b567422f 2011-01-02 rsc Bprint(b, "\", \"");
151 b567422f 2011-01-02 rsc for(i=0; i<256; i++)
153 b567422f 2011-01-02 rsc cprintchar(b, i);
154 b567422f 2011-01-02 rsc Bprint(b, "\",\t");
156 b567422f 2011-01-02 rsc Bprint(b, "{");
157 bb7ff349 2009-07-08 mt for(i=0; i<256; i++)
159 b567422f 2011-01-02 rsc Bprint(b, " 0x%.4ux,", t->r[i]);
160 b567422f 2011-01-02 rsc Bprint(b, " }");
162 b567422f 2011-01-02 rsc Bprint(b, "L\"");
163 b567422f 2011-01-02 rsc for(i=0; i<256; i++)
165 b567422f 2011-01-02 rsc Bprint(b, "%C", t->r[i]);
166 b567422f 2011-01-02 rsc Bprint(b, "\"");
168 b567422f 2011-01-02 rsc Bprint(b, ",\n");
172 bb7ff349 2009-07-08 mt readfile(char *fname)
175 bb7ff349 2009-07-08 mt char *line, *p;
181 bb7ff349 2009-07-08 mt if((b = Bopen(fname, OREAD)) == 0) {
182 bb7ff349 2009-07-08 mt fprint(2, "cannot open \"%s\": %r\n", fname);
183 bb7ff349 2009-07-08 mt exits("open");
187 bb7ff349 2009-07-08 mt while((line = Brdline(b, '\n')) != 0) {
189 bb7ff349 2009-07-08 mt if(line[0] == '#')
192 bb7ff349 2009-07-08 mt r = strtol(line, nil, 16);
193 bb7ff349 2009-07-08 mt p = strchr(line, ' ');
194 d25d0ca1 2020-05-19 rsc if(r == 0 || (p != line+4 && p != line+5) || p[0] != ' ' || (p == line+4 && p[1] != ' ')) {
195 bb7ff349 2009-07-08 mt fprint(2, "%s:%d: cannot parse line\n", fname, lineno);
200 bb7ff349 2009-07-08 mt /* 00AE Or rO ® registered trade mark sign */
201 bb7ff349 2009-07-08 mt for(inseq=1, seq=p; (uchar)*p < Runeself; p++) {
202 bb7ff349 2009-07-08 mt if(*p == '\0' || isspace(*p)) {
203 bb7ff349 2009-07-08 mt if(inseq && p-seq >= 2) {
206 bb7ff349 2009-07-08 mt insert(seq, r);
209 bb7ff349 2009-07-08 mt if(*p == '\0')
224 bb7ff349 2009-07-08 mt fprint(2, "usage: mklatinkbd [-r] [/lib/keyboard]\n");
225 bb7ff349 2009-07-08 mt exits("usage");
228 b567422f 2011-01-02 rsc int kfmt(Fmt*);
231 bb7ff349 2009-07-08 mt main(int argc, char **argv)
237 bb7ff349 2009-07-08 mt case 'r': /* print rune values */
250 b567422f 2011-01-02 rsc fmtinstall('k', kfmt);
251 b567422f 2011-01-02 rsc readfile(argc == 1 ? argv[0] : "/dev/stdin");
253 bb7ff349 2009-07-08 mt Binit(&bout, 1, OWRITE);
255 b567422f 2011-01-02 rsc Bprint(&bout, "# Generated by mklatinkbd -x; do not edit.\n");
256 b567422f 2011-01-02 rsc for(i=0x20; i<0x10000; i++)
257 b567422f 2011-01-02 rsc Bprint(&bout, "<Multi_key> <X> <%x> <%x> <%x> <%x> : \"%C\" U%04X\n",
258 b567422f 2011-01-02 rsc (i>>12)&0xf, (i>>8)&0xf, (i>>4)&0xf, i&0xf, i, i);
261 bb7ff349 2009-07-08 mt printtrie(&bout, root);
265 b567422f 2011-01-02 rsc // X11 key names
270 b567422f 2011-01-02 rsc } xkey[] = {
271 b567422f 2011-01-02 rsc ' ', "space",
272 b567422f 2011-01-02 rsc '!', "exclam",
273 b567422f 2011-01-02 rsc '"', "quotedbl",
274 b567422f 2011-01-02 rsc '#', "numbersign",
275 b567422f 2011-01-02 rsc '$', "dollar",
276 b567422f 2011-01-02 rsc '%', "percent",
277 b567422f 2011-01-02 rsc '&', "ampersand",
278 b567422f 2011-01-02 rsc '\'', "apostrophe",
279 b567422f 2011-01-02 rsc '(', "parenleft",
280 b567422f 2011-01-02 rsc ')', "parenright",
281 b567422f 2011-01-02 rsc '*', "asterisk",
282 b567422f 2011-01-02 rsc '+', "plus",
283 b567422f 2011-01-02 rsc ',', "comma",
284 b567422f 2011-01-02 rsc '-', "minus",
285 b567422f 2011-01-02 rsc '.', "period",
286 b567422f 2011-01-02 rsc '/', "slash",
287 b567422f 2011-01-02 rsc ':', "colon",
288 b567422f 2011-01-02 rsc ';', "semicolon",
289 b567422f 2011-01-02 rsc '<', "less",
290 b567422f 2011-01-02 rsc '=', "equal",
291 b567422f 2011-01-02 rsc '>', "greater",
292 b567422f 2011-01-02 rsc '?', "question",
294 b567422f 2011-01-02 rsc '[', "bracketleft",
295 b567422f 2011-01-02 rsc '\\', "backslash",
296 b567422f 2011-01-02 rsc ',', "bracketright",
297 b567422f 2011-01-02 rsc '^', "asciicircum",
298 b567422f 2011-01-02 rsc '_', "underscore",
299 b567422f 2011-01-02 rsc '`', "grave",
300 b567422f 2011-01-02 rsc '{', "braceleft",
302 b567422f 2011-01-02 rsc '}', "braceright",
303 b567422f 2011-01-02 rsc '~', "asciitilde",
308 b567422f 2011-01-02 rsc kfmt(Fmt *f)
312 b567422f 2011-01-02 rsc c = va_arg(f->args, int);
313 b567422f 2011-01-02 rsc for(i=0; xkey[i].s; i++)
314 b567422f 2011-01-02 rsc if(xkey[i].c == c)
315 b567422f 2011-01-02 rsc return fmtprint(f, "<%s>", xkey[i].s);
316 b567422f 2011-01-02 rsc return fmtprint(f, "<%c>", c);