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
|
/* file: utf7.l */
%{
#ifdef __MSDOS__
# include <dir.h>
# include <fcntl.h>
# include <io.h>
#else
# include <unistd.h>
#endif
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
int
printcode = 0;
unsigned int
outcode [2],
instep,
outstep;
char
*programname;
void
get_programname (char const *argv0),
syntax (void),
errit (char const *format, ...),
utf (void),
nextout (void),
outchar (long unsigned);
#ifdef __MSDOS__
#define strcasecmp(s1, s2) (stricmp(s1, s2))
#endif
#define YY_NO_UNPUT
#define YY_SKIP_YYWRAP
#ifdef yywrap
# undef yywrap
#endif
int yywrap()
{
return 1;
}
%}
%Start _utf
%%
<INITIAL>"+-" { fputc ('+', stdout); }
<INITIAL>"+" { instep = outstep = 0;
BEGIN _utf; }
<_utf>[A-Za-z0-9+/] { utf (); }
<_utf>"-" { BEGIN INITIAL; }
<_utf>.|\n { fputc (yytext [0], stdout);
BEGIN INITIAL; }
%%
void utf ()
{
unsigned
i,
c;
i = yytext [0];
if (i >= 'A' && i <= 'Z')
c = i - 'A';
else if (i >= 'a' && i <= 'z')
c = i + 26 - 'a';
else if (i >= '0' && i <= '9')
c = i + 52 - '0';
else if (i == '+')
c = 62;
else if (i == '/')
c = 63;
switch (instep) {
case 0:
outcode [outstep] = (c << 2);
break;
case 1:
outcode [outstep] |= (c >> 4);
nextout ();
outcode [outstep] = (c << 4);
break;
case 2:
outcode [outstep] |= (c >> 2);
nextout ();
outcode [outstep] = (c << 6);
break;
case 3:
outcode [outstep] |= c;
nextout ();
break;
}
if (++instep == 4)
instep = 0;
}
void nextout ()
{
unsigned
c;
if (outstep == 0) {
outstep = 1;
} else {
outstep = 0;
c = ((outcode [0] & 0xFF) << 8) | (outcode [1] & 0xFF);
outchar (c);
}
}
void outchar (long unsigned c)
{
int
i;
char
*s;
/*
* iso-8859-1
*/
if (c < 256) {
fputc (c, stdout);
return;
}
/*
* iso-8859-15
*/
i = 0;
switch (c) {
case 0x20Ac: i = 0xA4; break; /* euro */
case 0x0160: i = 0xA6; break; /* S caron */
case 0x0161: i = 0xA8; break; /* s caron */
case 0x017D: i = 0xB4; break; /* Z caron */
case 0x017E: i = 0xB8; break; /* z caron */
case 0x0152: i = 0xBC; break; /* OE ligature */
case 0x0153: i = 0xBD; break; /* oe ligature */
case 0x0178: i = 0xBE; break; /* Y diaeresis */
}
if (i) {
fputc (i, stdout);
return;
}
/*
* substitutions
*/
s = NULL;
switch (c) {
case 0x0132: s = "IJ"; break;
case 0x0133: s = "ij"; break;
}
if (s) {
fputs (s, stdout);
return;
}
if (printcode) {
if (c < 0x10000)
printf ("U+%04X", (unsigned) c);
else
printf ("U+%08lX", c);
} else
fputc (191, stdout);
}
int main (int argc, char *argv [])
{
get_programname (argv [0]);
while (argc > 1)
if (! strcmp (argv [1], "-c")) {
printcode = 1;
argv++;
argc--;
} else
break;
switch (argc) {
case 1:
if (isatty (fileno (stdin)))
syntax ();
yyin = stdin;
break;
case 2:
yyin = fopen (argv [1], "r");
if (! yyin)
errit ("Opening file \"%s\": %s", argv [1], strerror (errno));
break;
default:
syntax ();
}
yylex ();
if (yyin != stdin)
fclose (yyin);
return 0;
}
void get_programname (char const *argv0)
{
#ifdef __MSDOS__
char
name [MAXFILE];
fnsplit (argv0, NULL, NULL, name, NULL);
programname = strdup (name);
#else /* unix */
char
*p;
p = strrchr (argv0, '/');
if (p)
programname = strdup (p + 1);
else
programname = strdup (argv0);
#endif
}
void errit (char const *format, ...)
{
va_list
list;
fprintf (stderr, "\nError %s: ", programname);
va_start (list, format);
vfprintf (stderr, format, list);
fprintf (stderr, "\n\n");
exit (1);
}
void syntax ()
{
fprintf (
stderr,
"\n"
"Syntax: %s [-c] [utf-7 encoded file]\n"
"\n"
"The file will be translated to iso-8859-1 *and* iso-8859-15\n"
"\n"
" -c : print U+code for characters not in iso-8859-1/15\n"
"\n",
programname
);
exit (1);
}
|