Skip to content

Commit

Permalink
Unify mbtowc() invocation and error handling
Browse files Browse the repository at this point in the history
An abstraction worth performing since we can unify the internal state
reset and there's currently no need to distinguish between the two
scenarios where mbtowc() returns -1 or 0.
  • Loading branch information
mptre committed Sep 11, 2017
1 parent 7a7ab4a commit 1a4888f
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions pick.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static int tty_putc(int);
static void tty_restore(void);
static void tty_size(void);
static __dead void usage(void);
static int xmbtowc(wchar_t *, const char *);

static struct termios original_attributes;
static struct {
Expand Down Expand Up @@ -625,13 +626,8 @@ strcasechr(const char *s1, const char *s2)
size_t i;
int in_esc_seq, nbytes;

switch (mbtowc(&wc2, s2, MB_CUR_MAX)) {
case -1:
mbtowc(NULL, NULL, MB_CUR_MAX);
/* FALLTHROUGH */
case 0:
if (xmbtowc(&wc2, s2) == 0)
return NULL;
}

in_esc_seq = 0;
for (i = 0; s1[i] != '\0';) {
Expand All @@ -642,16 +638,13 @@ strcasechr(const char *s1, const char *s2)
in_esc_seq = 0;
} else if (i > 0 && s1[i - 1] == '\033' && s1[i] == '[') {
in_esc_seq = 1;
} else if ((nbytes = mbtowc(&wc1, &s1[i], MB_CUR_MAX)) == -1) {
mbtowc(NULL, NULL, MB_CUR_MAX);
} else if ((nbytes = xmbtowc(&wc1, &s1[i])) == 0) {
nbytes = 1;
} else if (wcsncasecmp(&wc1, &wc2, 1) == 0) {
return &s1[i];
}

if (nbytes > 0)
i += nbytes;
else
i++;
i += nbytes;
}

return NULL;
Expand Down Expand Up @@ -816,12 +809,7 @@ print_line(const char *str, size_t len, int standout,
continue;
}

/*
* Due to the explicit NUL-check above, the case where
* mbtowc(3) returns 0 is not handled here.
*/
if ((nbytes = mbtowc(&wc, &str[i], MB_CUR_MAX)) == -1) {
mbtowc(NULL, NULL, MB_CUR_MAX);
if ((nbytes = xmbtowc(&wc, &str[i])) == 0) {
i++;
continue;
}
Expand Down Expand Up @@ -1072,3 +1060,17 @@ isword(const char *s)

return iswalnum(wc) || wc == L'_';
}

int
xmbtowc(wchar_t *wc, const char *s)
{
int n;

n = mbtowc(wc, s, MB_CUR_MAX);
if (n == -1) {
mbtowc(NULL, NULL, MB_CUR_MAX);
return 0;
}

return n;
}

0 comments on commit 1a4888f

Please sign in to comment.