#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>
void getu(Biobuf* bin, Biobuf* bout){
char d[5];
int i;
int u;
for(i=0; i<4 ; i++){
d[i] = Bgetc(bin);
if (!isxdigit(d[i])){
goto loose;
}
}
d[i--] = 0;
u = strtoul(d, 0, 16);
if (u<=Runemax){
Bprint(bout, "%C", u);
return;
}
loose:
if (-1 == d[i]){
i--;
}
if (i>0){
Bprint(bout, "%.*s", i, d);
}
}
void convert(Biobuf * bin, Biobuf * bout)
{
int c;
for(;;){
c = Bgetrune(bin);
top: switch(c){
default:
Bputrune(bout, c);
break;
case '\\':
c = Bgetrune(bin);
if ('u' != c){
Bputrune(bout, '\\');
goto top;
}
getu(bin, bout);
break;
case -1:
return;
}
}
}
void main(int c, char **v)
{
int i;
Biobuf *b, bin, bout;
Binit(&bout, 1, OWRITE);
if (c == 1) {
Binit(&bin, 0, OREAD);
convert(&bin, &bout);
} else {
for (i = 1; i < c; i++) {
if ((b = Bopen(v[i], OREAD)) == nil)
sysfatal("open %s: %r", v[i]);
convert(b, &bout);
Bterm(b);
}
}
Bterm(&bout);
exits(nil);
}
|