live code from FOSDEM: add print \xDB and \u20AC, including regression test

agreed bsiegert@
good idea and manpage diff ok'd by some netbsd person sitting next to me
This commit is contained in:
tg
2008-02-24 15:48:43 +00:00
parent 1a5ea7052c
commit d16fc19335
4 changed files with 72 additions and 10 deletions

53
funcs.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.67 2007/10/25 15:23:09 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.68 2008/02/24 15:48:42 tg Exp $");
/* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin;
@ -449,6 +449,57 @@ c_print(const char **wp)
break;
}
break;
case 'x':
/* Look for a hexadecimal number of
* up to 2 digits, write raw octet.
*/
c = 0;
for (i = 0; i < 2; i++) {
c <<= 4;
if (*s >= '0' && *s <= '9')
c += *s++ - '0';
else if (*s >= 'A' && *s <= 'F')
c += *s++ - 'A' + 10;
else if (*s >= 'a' && *s <= 'f')
c += *s++ - 'a' + 10;
else {
c >>= 4;
break;
}
}
break;
case 'u':
/* Look for a hexadecimal number of
* up to 4 digits, write Unicode.
*/
c = 0;
for (i = 0; i < 4; i++) {
c <<= 4;
if (*s >= '0' && *s <= '9')
c += *s++ - '0';
else if (*s >= 'A' && *s <= 'F')
c += *s++ - 'A' + 10;
else if (*s >= 'a' && *s <= 'f')
c += *s++ - 'a' + 10;
else {
c >>= 4;
break;
}
}
if (c < 0x80)
/* Xput below writes ASCII */;
else if (c < 0x0800) {
Xput(xs, xp, (c >> 6) | 0xC0);
c = 0x80 | (c & 0x3F);
/* leave 2nd octet to below */
} else {
Xput(xs, xp, (c >> 12) | 0xE0);
Xput(xs, xp,
((c >> 6) & 0x3F) | 0x80);
c = 0x80 | (c & 0x3F);
/* leave 3rd octet to below */
}
break;
case '\0': s--; c = '\\'; break;
case '\\': break;
default: