2 * Parse /lib/keyboard to create latin1.h table for kernel.
3 * mklatinkbd -r prints an array of integers rather than a Rune string literal.
14 MAXLD = 2, /* latin1.c assumes this is 2 */
19 " * This is automatically generated by %s from /lib/keyboard\n"
20 " * Edit /lib/keyboard instead.\n"
24 * latin1.c assumes that strlen(ld) is at most 2.
25 * It also assumes that latintab[i].ld can be a prefix of latintab[j].ld
26 * only when j < i. We ensure this by sorting the output by prefix length.
27 * The so array is indexed by the character value.
30 typedef struct Trie Trie;
32 int n; /* of characters r */
47 root = malloc(sizeof *root);
48 memset(root, 0, sizeof *root);
51 assert(seq[0] != '\0');
54 for(q=(uchar*)seq; *(q+1) != '\0'; q++) {
55 tp = &(*tp)->link[*q];
57 *tp = malloc(sizeof(**tp));
59 memset(*tp, 0, sizeof(**tp));
60 strcpy((*tp)->seq, seq);
61 (*tp)->seq[q+1-(uchar*)seq] = '\0';
69 /* add character sequence s meaning rune r */
71 insert(char *s, Rune r)
78 lastc = (uchar)s[len-1];
82 fprint(2, "warning: table duplicate: %s is %C and %C\n", s, t->r[lastc], r);
90 cprintchar(Biobuf *b, int c)
92 /* print a byte c safe for a C string. */
103 if(isascii(c) && isprint(c))
106 Bprint(b, "\\x%.2x", c);
112 cprints(Biobuf *b, char *p)
120 printtrie(Biobuf *b, Trie *t)
126 printtrie(b, t->link[i]);
140 Bprint(b, " 0x%.4ux,", t->r[i]);
146 Bprint(b, "%C", t->r[i]);
154 readfile(char *fname)
163 if((b = Bopen(fname, OREAD)) == 0) {
164 fprint(2, "cannot open \"%s\": %r\n", fname);
169 while((line = Brdline(b, '\n')) != 0) {
174 r = strtol(line, nil, 16);
175 p = strchr(line, ' ');
176 if(r == 0 || p != line+4 || p[0] != ' ' || p[1] != ' ') {
177 fprint(2, "%s:%d: cannot parse line\n", fname, lineno);
182 /* 00AE Or rO ® registered trade mark sign */
183 for(inseq=1, seq=p; (uchar)*p < Runeself; p++) {
184 if(*p == '\0' || isspace(*p)) {
185 if(inseq && p-seq >= 2) {
206 fprint(2, "usage: mklatinkbd [-r] [/lib/keyboard]\n");
211 main(int argc, char **argv)
216 case 'r': /* print rune values */
226 readfile(argc == 1 ? argv[0] : "/fd/0");
228 Binit(&bout, 1, OWRITE);
230 printtrie(&bout, root);