#include #include #include #include #include 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