2013-11-15 16:25:10 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <locale.h>
|
2013-11-15 16:25:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wchar.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-11-15 16:25:10 +00:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-07-14 22:49:42 +00:00
|
|
|
eprintf("usage: %s [-d] [-c] set1 [set2]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dflag, cflag;
|
|
|
|
static wchar_t mappings[0x110000];
|
|
|
|
|
|
|
|
struct wset_state {
|
|
|
|
char *s; /* current character */
|
|
|
|
wchar_t rfirst, rlast; /* first and last in range */
|
|
|
|
wchar_t prev; /* previous returned character */
|
|
|
|
int prev_was_range; /* was the previous character part of a c-c range? */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct set_state {
|
|
|
|
char *s, rfirst, rlast, prev;
|
|
|
|
int prev_was_octal; /* was the previous returned character written in octal? */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_state_defaults(struct set_state *s)
|
|
|
|
{
|
|
|
|
s->rfirst = 1;
|
|
|
|
s->rlast = 0;
|
|
|
|
s->prev_was_octal = 1;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 22:07:13 +00:00
|
|
|
static void
|
2014-07-14 22:49:42 +00:00
|
|
|
wset_state_defaults(struct wset_state *s)
|
2013-11-15 16:25:10 +00:00
|
|
|
{
|
2014-07-14 22:49:42 +00:00
|
|
|
s->rfirst = 1;
|
|
|
|
s->rlast = 0;
|
|
|
|
s->prev_was_range = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sets *s to the char that was intended to be written.
|
|
|
|
* returns how many bytes the s pointer has to advance to skip the
|
|
|
|
* escape sequence if it was an octal, always zero otherwise. */
|
|
|
|
static int
|
|
|
|
resolve_escape(char *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char c;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (*s) {
|
2013-11-15 16:25:10 +00:00
|
|
|
case 'n':
|
|
|
|
*s = '\n';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
case 't':
|
|
|
|
*s = '\t';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
case 'r':
|
|
|
|
*s = '\r';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
case 'f':
|
|
|
|
*s = '\f';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
case 'a':
|
|
|
|
*s = '\a';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
case 'b':
|
|
|
|
*s = '\b';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
case 'v':
|
|
|
|
*s = '\v';
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
|
|
|
case '\\':
|
|
|
|
*s = '\\';
|
|
|
|
return 0;
|
|
|
|
case '\0':
|
|
|
|
eprintf("stray '\\' at end of input:");
|
|
|
|
default: ;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
2014-07-14 22:49:42 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if(*s < '0' || *s > '7')
|
2014-07-14 22:49:42 +00:00
|
|
|
eprintf("invalid character after '\\':");
|
2014-11-13 17:29:30 +00:00
|
|
|
for(i = 0, c = 0; s[i] >= '0' && s[i] <= '7' && i < 3; i++) {
|
2014-07-14 22:49:42 +00:00
|
|
|
c <<= 3;
|
|
|
|
c += s[i]-'0';
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if(*s > '3' && i == 3)
|
2014-07-14 22:49:42 +00:00
|
|
|
eprintf("octal byte cannot be bigger than 377:");
|
|
|
|
*s = c;
|
|
|
|
return i;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 22:49:42 +00:00
|
|
|
#define embtowc(a, b) mbtowc(a, b, 4)
|
|
|
|
|
2014-04-12 18:50:51 +00:00
|
|
|
static int
|
|
|
|
xmbtowc(wchar_t *unicodep, const char *s)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
2014-07-14 22:49:42 +00:00
|
|
|
rv = embtowc(unicodep, s);
|
2014-04-12 18:50:51 +00:00
|
|
|
if (rv < 0)
|
2014-07-14 22:49:42 +00:00
|
|
|
eprintf("mbtowc: invalid input sequence:");
|
2014-04-12 18:50:51 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:49:42 +00:00
|
|
|
static int
|
|
|
|
has_octal_escapes(const char *s)
|
2013-11-15 16:25:10 +00:00
|
|
|
{
|
2014-11-13 17:29:30 +00:00
|
|
|
while (*s)
|
|
|
|
if (*s++ == '\\' && *s >= '0' && *s <= '7')
|
2014-07-14 22:49:42 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char
|
|
|
|
get_next_char(struct set_state *s)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int nchars;
|
|
|
|
|
|
|
|
start:
|
2014-11-13 17:29:30 +00:00
|
|
|
if (s->rfirst <= s->rlast) {
|
2014-07-14 22:49:42 +00:00
|
|
|
c = s->rfirst;
|
|
|
|
s->rfirst++;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s->s == '-' && !s->prev_was_octal) {
|
2014-07-14 22:49:42 +00:00
|
|
|
s->s++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!*s->s)
|
2014-07-14 22:49:42 +00:00
|
|
|
return '-';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
|
2014-07-14 22:49:42 +00:00
|
|
|
goto char_is_octal;
|
|
|
|
s->rlast = *(s->s)++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!s->rlast)
|
2014-07-14 22:49:42 +00:00
|
|
|
return '\0';
|
|
|
|
s->prev_was_octal = 1;
|
|
|
|
s->rfirst = ++(s->prev);
|
|
|
|
goto start;
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s->s == '\\' && (nchars = resolve_escape(++(s->s))))
|
2014-07-14 22:49:42 +00:00
|
|
|
goto char_is_octal;
|
|
|
|
|
|
|
|
s->prev_was_octal = 0;
|
|
|
|
c = *(s->s)++;
|
|
|
|
s->prev = c;
|
|
|
|
return c;
|
|
|
|
|
|
|
|
char_is_octal:
|
|
|
|
s->prev_was_octal = 1;
|
|
|
|
c = *s->s;
|
|
|
|
s->s += nchars;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wchar_t
|
|
|
|
get_next_wchar(struct wset_state *s)
|
|
|
|
{
|
|
|
|
start:
|
2014-11-13 17:29:30 +00:00
|
|
|
if (s->rfirst <= s->rlast) {
|
2014-07-14 22:49:42 +00:00
|
|
|
s->prev = s->rfirst;
|
|
|
|
s->rfirst++;
|
|
|
|
return s->prev;
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s->s == '-' && !s->prev_was_range) {
|
2014-07-14 22:49:42 +00:00
|
|
|
s->s++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!*s->s)
|
2014-07-14 22:49:42 +00:00
|
|
|
return '-';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s->s == '\\')
|
2014-07-14 22:49:42 +00:00
|
|
|
resolve_escape(++(s->s));
|
|
|
|
s->s += xmbtowc(&s->rlast, s->s);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!s->rlast)
|
2014-07-14 22:49:42 +00:00
|
|
|
return '\0';
|
|
|
|
s->rfirst = ++(s->prev);
|
|
|
|
s->prev_was_range = 1;
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (*s->s == '\\')
|
2014-07-14 22:49:42 +00:00
|
|
|
resolve_escape(++(s->s));
|
|
|
|
s->s += xmbtowc(&s->prev, s->s);
|
|
|
|
s->prev_was_range = 0;
|
|
|
|
return s->prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_mapping_wide(const char *set1, const char *set2)
|
|
|
|
{
|
|
|
|
struct set_state ss1, ss2;
|
|
|
|
struct wset_state wss1, wss2;
|
|
|
|
wchar_t wc1, wc2, last_wc2;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (has_octal_escapes(set1)) {
|
2014-07-14 22:49:42 +00:00
|
|
|
set_state_defaults(&ss1);
|
|
|
|
ss1.s = (char *) set1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (set2) {
|
2014-07-14 22:49:42 +00:00
|
|
|
set_state_defaults(&ss2);
|
|
|
|
ss2.s = (char *) set2;
|
|
|
|
/* if the character returned is from an octal triplet, it might be null
|
2014-07-16 19:42:46 +00:00
|
|
|
* and still need to continue */
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal ) {
|
|
|
|
if (!(wc2 = (unsigned char) get_next_char(&ss2)))
|
2014-07-14 22:49:42 +00:00
|
|
|
wc2 = last_wc2;
|
|
|
|
mappings[wc1] = wc2;
|
|
|
|
last_wc2 = wc2;
|
|
|
|
}
|
|
|
|
} else {
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((wc1 = (unsigned char) get_next_char(&ss1)) || ss1.prev_was_octal)
|
2014-07-14 22:49:42 +00:00
|
|
|
mappings[wc1] = 1;
|
2014-04-09 12:03:02 +00:00
|
|
|
}
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
wset_state_defaults(&wss1);
|
|
|
|
wss1.s = (char *) set1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (set2) {
|
2014-07-14 22:49:42 +00:00
|
|
|
wset_state_defaults(&wss2);
|
|
|
|
wss2.s = (char *) set2;
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((wc1 = get_next_wchar(&wss1))) {
|
|
|
|
if (!(wc2 = get_next_wchar(&wss2)))
|
2014-07-14 22:49:42 +00:00
|
|
|
wc2 = last_wc2;
|
|
|
|
mappings[wc1] = wc2;
|
|
|
|
last_wc2 = wc2;
|
|
|
|
}
|
|
|
|
} else {
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((wc1 = get_next_wchar(&wss1)))
|
2014-07-14 22:49:42 +00:00
|
|
|
mappings[wc1] = 1;
|
|
|
|
}
|
|
|
|
return 1;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
2014-07-14 22:49:42 +00:00
|
|
|
return 0; /* unreachable */
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 22:07:13 +00:00
|
|
|
static void
|
2014-07-14 22:49:42 +00:00
|
|
|
wmap_null(char *in, ssize_t nbytes)
|
2013-11-15 16:25:10 +00:00
|
|
|
{
|
2014-07-14 22:49:42 +00:00
|
|
|
char *s;
|
|
|
|
wchar_t rune;
|
|
|
|
int parsed_bytes = 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
|
|
|
|
s = in;
|
2014-11-13 17:29:30 +00:00
|
|
|
while (nbytes) {
|
2014-07-14 22:49:42 +00:00
|
|
|
parsed_bytes = embtowc(&rune, s);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (parsed_bytes < 0) {
|
2014-07-14 22:49:42 +00:00
|
|
|
rune = *s;
|
|
|
|
parsed_bytes = 1;
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (((!mappings[rune])&1) ^ cflag)
|
2014-07-14 22:49:42 +00:00
|
|
|
putwchar(rune);
|
|
|
|
s += parsed_bytes;
|
|
|
|
nbytes -= parsed_bytes;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-25 22:07:13 +00:00
|
|
|
static void
|
2014-07-14 22:49:42 +00:00
|
|
|
wmap_set(char *in, ssize_t nbytes)
|
2013-11-15 16:25:10 +00:00
|
|
|
{
|
2014-07-14 22:49:42 +00:00
|
|
|
char *s;
|
|
|
|
wchar_t rune;
|
|
|
|
int parsed_bytes = 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
|
|
|
|
s = in;
|
2014-11-13 17:29:30 +00:00
|
|
|
while (nbytes) {
|
2014-07-14 22:49:42 +00:00
|
|
|
parsed_bytes = embtowc(&rune, s);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (parsed_bytes < 0) {
|
2014-07-14 22:49:42 +00:00
|
|
|
rune = *s;
|
|
|
|
parsed_bytes = 1;
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!mappings[rune])
|
2014-07-14 22:49:42 +00:00
|
|
|
putwchar(rune);
|
2013-11-15 16:25:10 +00:00
|
|
|
else
|
2014-07-14 22:49:42 +00:00
|
|
|
putwchar(mappings[rune]);
|
|
|
|
nbytes -= parsed_bytes;
|
|
|
|
s += parsed_bytes;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:49:42 +00:00
|
|
|
static void
|
|
|
|
map_null(char *in, ssize_t nbytes)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (s = in; nbytes; s++, nbytes--)
|
|
|
|
if (((!mappings[(unsigned char)*s])&1) ^ cflag)
|
2014-07-14 22:49:42 +00:00
|
|
|
putchar(*s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
map_set(char *in, ssize_t nbytes)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (s = in; nbytes; s++, nbytes--)
|
|
|
|
if (!mappings[(unsigned char)*s])
|
2014-07-14 22:49:42 +00:00
|
|
|
putchar(*s);
|
|
|
|
else
|
|
|
|
putchar(mappings[(unsigned char)*s]);
|
|
|
|
}
|
|
|
|
|
2013-11-15 16:25:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t size = 0;
|
2014-07-14 22:49:42 +00:00
|
|
|
ssize_t nbytes;
|
|
|
|
void (*mapfunc)(char*, ssize_t);
|
2013-11-15 16:25:10 +00:00
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
2014-07-14 22:49:42 +00:00
|
|
|
dflag = cflag = 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2014-04-09 12:12:34 +00:00
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
2014-07-14 22:49:42 +00:00
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
2013-11-15 16:25:10 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0)
|
2013-11-15 16:25:10 +00:00
|
|
|
usage();
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (dflag) {
|
|
|
|
if (argc != 1)
|
2014-04-09 12:12:34 +00:00
|
|
|
usage();
|
2014-11-13 17:29:30 +00:00
|
|
|
if (is_mapping_wide(argv[0], NULL))
|
2014-07-14 22:49:42 +00:00
|
|
|
mapfunc = wmap_null;
|
|
|
|
else
|
|
|
|
mapfunc = map_null;
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (cflag) {
|
2014-07-14 22:49:42 +00:00
|
|
|
usage();
|
2014-11-13 17:29:30 +00:00
|
|
|
} else if (argc == 2) {
|
|
|
|
if (is_mapping_wide(argv[0], argv[1]))
|
2014-07-14 22:49:42 +00:00
|
|
|
mapfunc = wmap_set;
|
|
|
|
else
|
|
|
|
mapfunc = map_set;
|
2014-04-09 12:12:34 +00:00
|
|
|
} else {
|
2014-07-14 22:49:42 +00:00
|
|
|
usage();
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((nbytes = agetline(&buf, &size, stdin)) != -1)
|
2014-07-14 22:49:42 +00:00
|
|
|
mapfunc(buf, nbytes);
|
2013-11-15 16:25:10 +00:00
|
|
|
free(buf);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (ferror(stdin))
|
2013-11-15 16:25:10 +00:00
|
|
|
eprintf("<stdin>: read error:");
|
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-11-15 16:25:10 +00:00
|
|
|
}
|