diff options
Diffstat (limited to 'gnu/usr.bin/grep/search.c')
-rw-r--r-- | gnu/usr.bin/grep/search.c | 80 |
1 files changed, 62 insertions, 18 deletions
diff --git a/gnu/usr.bin/grep/search.c b/gnu/usr.bin/grep/search.c index 4336caf..b8a1dcd 100644 --- a/gnu/usr.bin/grep/search.c +++ b/gnu/usr.bin/grep/search.c @@ -151,15 +151,16 @@ kwsmusts (void) static char* check_multibyte_string(char const *buf, size_t size) { - char *mb_properties = malloc(size); + char *mb_properties = xmalloc(size); mbstate_t cur_state; + wchar_t wc; int i; memset(&cur_state, 0, sizeof(mbstate_t)); memset(mb_properties, 0, sizeof(char)*size); for (i = 0; i < size ;) { size_t mbclen; - mbclen = mbrlen(buf + i, size - i, &cur_state); + mbclen = mbrtowc(&wc, buf + i, size - i, &cur_state); if (mbclen == (size_t) -1 || mbclen == (size_t) -2 || mbclen == 0) { @@ -167,6 +168,14 @@ check_multibyte_string(char const *buf, size_t size) We treat it as a singlebyte character. */ mbclen = 1; } + else if (match_icase) + { + if (iswupper((wint_t)wc)) + { + wc = towlower((wint_t)wc); + wcrtomb(buf + i, wc, &cur_state); + } + } mb_properties[i] = mbclen; i += mbclen; } @@ -235,7 +244,7 @@ Gcompile (char const *pattern, size_t size) static char const line_end[] = "\\)$"; static char const word_beg[] = "\\(^\\|[^[:alnum:]_]\\)\\("; static char const word_end[] = "\\)\\([^[:alnum:]_]\\|$\\)"; - char *n = malloc (sizeof word_beg - 1 + size + sizeof word_end); + char *n = xmalloc (sizeof word_beg - 1 + size + sizeof word_end); size_t i; strcpy (n, match_lines ? line_beg : word_beg); i = strlen (n); @@ -318,7 +327,7 @@ Ecompile (char const *pattern, size_t size) static char const line_end[] = ")$"; static char const word_beg[] = "(^|[^[:alnum:]_])("; static char const word_end[] = ")([^[:alnum:]_]|$)"; - char *n = malloc (sizeof word_beg - 1 + size + sizeof word_end); + char *n = xmalloc (sizeof word_beg - 1 + size + sizeof word_end); size_t i; strcpy (n, match_lines ? line_beg : word_beg); i = strlen(n); @@ -341,14 +350,20 @@ EGexecute (char const *buf, size_t size, size_t *match_size, int exact) char eol = eolbyte; int backref, start, len; struct kwsmatch kwsm; - size_t i; + size_t i, ret_val; #ifdef MBS_SUPPORT char *mb_properties = NULL; -#endif /* MBS_SUPPORT */ - -#ifdef MBS_SUPPORT - if (MB_CUR_MAX > 1 && kwset) - mb_properties = check_multibyte_string(buf, size); + if (MB_CUR_MAX > 1) + { + if (match_icase) + { + char *case_buf = xmalloc(size); + memcpy(case_buf, buf, size); + buf = case_buf; + } + if (kwset) + mb_properties = check_multibyte_string(buf, size); + } #endif /* MBS_SUPPORT */ buflim = buf + size; @@ -457,8 +472,13 @@ EGexecute (char const *buf, size_t size, size_t *match_size, int exact) failure: #ifdef MBS_SUPPORT - if (MB_CUR_MAX > 1 && mb_properties) - free (mb_properties); + if (MB_CUR_MAX > 1) + { + if (mb_properties) + free (mb_properties); + if (match_icase) + free ((char *) buf); + } #endif /* MBS_SUPPORT */ return (size_t) -1; @@ -469,8 +489,13 @@ EGexecute (char const *buf, size_t size, size_t *match_size, int exact) success_in_start_and_len: #ifdef MBS_SUPPORT - if (MB_CUR_MAX > 1 && mb_properties) - free (mb_properties); + if (MB_CUR_MAX > 1) + { + if (mb_properties) + free (mb_properties); + if (match_icase) + free ((char *) buf); + } #endif /* MBS_SUPPORT */ *match_size = len; return start; @@ -506,10 +531,19 @@ Fexecute (char const *buf, size_t size, size_t *match_size, int exact) register size_t len; char eol = eolbyte; struct kwsmatch kwsmatch; + size_t ret_val; #ifdef MBS_SUPPORT - char *mb_properties; + char *mb_properties = NULL; if (MB_CUR_MAX > 1) - mb_properties = check_multibyte_string (buf, size); + { + if (match_icase) + { + char *case_buf = xmalloc(size); + memcpy(case_buf, buf, size); + buf = case_buf; + } + mb_properties = check_multibyte_string(buf, size); + } #endif /* MBS_SUPPORT */ for (beg = buf; beg <= buf + size; ++beg) @@ -567,7 +601,12 @@ Fexecute (char const *buf, size_t size, size_t *match_size, int exact) failure: #ifdef MBS_SUPPORT if (MB_CUR_MAX > 1) - free (mb_properties); + { + if (match_icase) + free((char *) buf); + if (mb_properties) + free(mb_properties); + } #endif /* MBS_SUPPORT */ return -1; @@ -583,7 +622,12 @@ Fexecute (char const *buf, size_t size, size_t *match_size, int exact) *match_size = len; #ifdef MBS_SUPPORT if (MB_CUR_MAX > 1) - free (mb_properties); + { + if (mb_properties) + free (mb_properties); + if (match_icase) + free ((char *) buf); + } #endif /* MBS_SUPPORT */ return beg - buf; } |