-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlatin2js.c
78 lines (67 loc) · 1.49 KB
/
latin2js.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
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <bio.h>
/* See also /sys/src/cmd/aux/mklatinkbd.c */
/* Definition from /sys/src/9/port/latin1.c */
/*
* The code makes two assumptions: strlen(ld) is 1 or 2; latintab[i].ld can be a
* prefix of latintab[j].ld only when j<i.
*/
struct cvlist
{
char *ld; /* must be seen before using this conversion */
char *si; /* options for last input characters */
Rune *so; /* the corresponding Rune for each si entry */
} latintab[] = {
#include "/sys/src/9/port/latin1.h"
0, 0, 0
};
/* Do Javascript strings have the same escapes as C? */
void
cprint(Biobuf *out, char *s)
{
for(; *s; ++s){
switch(*s){
case '\'':
case '\"':
case '\\':
Bprint(out, "\\%c", *s);
break;
case '\t':
Bprint(out, "\\t");
break;
default:
Bprint(out, "%c", *s);
break;
}
}
}
/* Assumes no dangerous characters in rune string. */
/* XXX Large characters will break with Javascript's use of UTF-16! */
void
main(int argc, char **argv)
{
struct cvlist *l;
Biobuf out;
char *name;
if(Binit(&out, 1, OWRITE) == Beof)
exits("Binit");
if(argc >= 1)
name = argv[0];
else
name = "latin2js";
Bprint(&out, "/* Generated by %s. */\n", name);
Bprint(&out, "var Composetab = {\n");
for(l = latintab; l->ld; ++l){
Bprint(&out, "\t\"");
cprint(&out, l->ld);
Bprint(&out, "\": {from:\"");
cprint(&out, l->si);
Bprint(&out, "\", to:\"");
Bprint(&out, "%S", l->so);
Bprint(&out, "\"},\n");
}
Bprint(&out, "};\n");
exits("");
}