diff options
Diffstat (limited to 'src/tr.c')
-rw-r--r-- | src/tr.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/tr.c b/src/tr.c new file mode 100644 index 0000000..5dad823 --- /dev/null +++ b/src/tr.c @@ -0,0 +1,118 @@ + +#include <stdio.h> +#include <wchar.h> +#include <wctype.h> +#include <unistd.h> +#include <assert.h> + + +int index_of(wchar_t c, char *s) +{ + mbstate_t mbst, mbst_init = { }; + int i=0; + char *z; + + while (*s) { + if (s[0] == '[' && s[1] == ':') { + wctype_t type; + char typename[32]; + z = strchr(s, ':'); + if (!z || z[1] != ']' || z-s >= sizeof typename) { + ERROR; + } + memcpy(typename, s, z-s); + typename[z-s] = 0; + type = wctype(typename); + if (!type) + ERROR; + if (iswctype(c, type)) + return i; + } + + if (mbrtowc(&wc, s, MB_LEN_MAX, &mbst) >= (size_t)-2) + return ERROR; + } +} + + +char *parse_repl(char *s1, char *s2) +{ + struct map *head, *tail; + int i; + for (i=j=0; s1[i]; i++) { + tail->next = malloc(sizeof(struct map)); + tail = tail->next; + + if (!strncmp(s1, "[:upper:]", 9) + && !strncmp(s2, "[:lower:]", 9)) { + tail->from = tail->to = 0; + s1 += 9; s2 += 9; + } + if (!strncmp(s1, "[:lower:]", 9) + && !strncmp(s2, "[:upper:]", 9)) { + tail->from = tail->to = 1; + s1 += 9; s2 += 9; + } + } +} + + +int main(int argc, char *argv[]) +{ + int o; + int cmpl = 0, del = 0, sqez = 0, repl = 1; + wchar_t ch, pv = WEOF; + char b[MB_LEN_MAX]; + int i, j; + mbstate_t mbst, mbst_init = { }; + + setlocale(LC_CTYPE, ""); + + while ((o = getopt(argc, argv, "Ccds")) != EOF) + { + switch (o) + { + case 'C': + case 'c': + cmpl = 1; + break; + case 'd': + del = 1; + break; + case 's': + sqez = 1; + break; + default: + return 1; + } + } + string1 = argc-optind >= 1 ? argv[optind] : ""; + string2 = argc-optind >= 2 ? argv[optind+1] : ""; + if (!string2 || del) repl = 0; + + i=0; + while (b[i] = getchar() != EOF) { +retry: + switch (mbrtowc(&wc, b+i, 1, &mbst)) { + case -1: + if (!i) { + putchar(b[0]); + mbst = mbst_init; + continue; + } + for (j=0; j<i; j++) + putchar(b[j]); + b[i=0] = b[j]; + mbst = mbst_init; + goto retry; + case -2: + i++; + continue; + } + b[++i] = 0; + } + + + + return 0; +} |