Regexp hardenning - error messages still not printed.

This commit is contained in:
Martin Dvorak 2014-05-04 08:11:01 +02:00
parent 96c476fe8c
commit f3ca5a46d5
4 changed files with 41 additions and 16 deletions

View file

@ -279,10 +279,11 @@ void print_help_label()
refresh();
}
void print_cmd_deleted_label(char *cmd, int occurences, Hstr *hstr)
void print_cmd_deleted_label(const char *cmd, int occurences, Hstr *hstr)
{
char screenLine[CMDLINE_LNG];
snprintf(screenLine, getmaxx(stdscr), "History item '%s' deleted (%d occurrence%s)", cmd, occurences, (occurences==1?"":"s"));
// TODO make this function
if(hstr->hicolor) {
color_attr_on(COLOR_PAIR(HH_COLOR_DELETE));
color_attr_on(A_BOLD);
@ -296,7 +297,24 @@ void print_cmd_deleted_label(char *cmd, int occurences, Hstr *hstr)
refresh();
}
void print_cmd_added_favorite_label(char *cmd, Hstr *hstr)
void print_regexp_error(const char *errorMessage)
{
char screenLine[CMDLINE_LNG];
snprintf(screenLine, getmaxx(stdscr), "%s", errorMessage);
if(hstr->hicolor) {
color_attr_on(COLOR_PAIR(HH_COLOR_DELETE));
color_attr_on(A_BOLD);
}
mvprintw(Y_OFFSET_HELP, 0, "%s", screenLine);
if(hstr->hicolor) {
color_attr_off(A_BOLD);
color_attr_on(COLOR_PAIR(1));
}
clrtoeol();
refresh();
}
void print_cmd_added_favorite_label(const char *cmd, Hstr *hstr)
{
char screenLine[CMDLINE_LNG];
snprintf(screenLine, getmaxx(stdscr), "Command '%s' added to favorites (C-/ to show favorites)", cmd);
@ -402,6 +420,8 @@ unsigned hstr_make_selection(char *prefix, HistoryItems *history, int maxSelecti
}
regmatch_t regexpMatch;
char regexpErrorMessage[CMDLINE_LNG];
bool regexpCompilationError=false;
for(i=0; i<count && selectionCount<maxSelectionCount; i++) {
if(source[i]) {
if(!prefix || !strlen(prefix)) {
@ -423,11 +443,17 @@ unsigned hstr_make_selection(char *prefix, HistoryItems *history, int maxSelecti
}
break;
case HH_MATCH_REGEXP:
if(hstr_regexp_match(&(hstr->regexp), prefix, source[i], &regexpMatch)) {
if(hstr_regexp_match(&(hstr->regexp), prefix, source[i], &regexpMatch, regexpErrorMessage, CMDLINE_LNG)) {
hstr->selection[selectionCount]=source[i];
hstr->selectionRegexpMatch[selectionCount].rm_so=regexpMatch.rm_so;
hstr->selectionRegexpMatch[selectionCount].rm_eo=regexpMatch.rm_eo;
selectionCount++;
} else {
if(!regexpCompilationError) {
// TODO fix broken messages - getting just escape sequences
// print_regexp_error(regexpErrorMessage);
regexpCompilationError=true;
}
}
break;
}
@ -685,6 +711,7 @@ void loop_to_select(Hstr *hstr)
char pattern[SELECTION_PREFIX_MAX_LNG];
pattern[0]=0;
// TODO this is too late! > don't render twice
// TODO overflow
strcpy(pattern, hstr->cmdline);
while (!done) {
maxHistoryItems=get_max_history_items(stdscr);

View file

@ -118,7 +118,6 @@ void favorites_save(FavoriteItems *favorites)
fclose(output_file);
}
}
free(fileName);
}

View file

@ -18,21 +18,26 @@ void hstr_regexp_init(HstrRegexp *hstrRegexp)
hashset_init(&hstrRegexp->cache);
}
bool hstr_regexp_match(HstrRegexp *hstrRegexp, char *regexp, char *text, regmatch_t *match)
bool hstr_regexp_match(
HstrRegexp *hstrRegexp,
const char *regexp,
const char *text,
regmatch_t *match,
char *errorMessage,
const size_t errorMessageSize)
{
regex_t* compiled=malloc(sizeof(regex_t));
regex_t* compiled;
if(hashset_contains(&hstrRegexp->cache,regexp)) {
compiled=hashset_get(&hstrRegexp->cache, regexp);
} else {
compiled=malloc(sizeof(regex_t));
int compilationFlags=(hstrRegexp->caseSensitive?0:REG_ICASE);
//printf("Regular expressions matching:\n '%s'\n '%s'",text,regexp);
int compilationStatus=regcomp(compiled, regexp, compilationFlags);
//printf("\nCompilation: %d",compilationStatus);
if(!compilationStatus) {
hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled);
} else {
regerror(compilationStatus, compiled, errorMessage, errorMessageSize);
free(compiled);
// TODO error handling: regerror() to turn error codes to messages
return false;
}
}
@ -41,12 +46,8 @@ bool hstr_regexp_match(HstrRegexp *hstrRegexp, char *regexp, char *text, regmatc
regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE];
int matchingFlags=0;
int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags);
//printf("\nMatching (status/matches): %s %d",(!matchingStatus?"match":"no-match"),matches);
if(!matchingStatus) {
//printf("\n %d %d",matchPtr[0].rm_so, matchPtr[0].rm_eo);
if(matchPtr[0].rm_so != -1) {
//printf("\n* %d %d",matchPtr[0].rm_so,matchPtr[0].rm_eo);
match->rm_so=matchPtr[0].rm_so;
match->rm_eo=matchPtr[0].rm_eo;
return true;
@ -59,4 +60,3 @@ void hstr_regexp_destroy(HstrRegexp *hstrRegexp)
{
// TODO hashset_destroy();
}

View file

@ -18,12 +18,11 @@
typedef struct {
bool caseSensitive;
HashSet cache;
} HstrRegexp;
void hstr_regexp_init(HstrRegexp *hstrRegexp);
bool hstr_regexp_match(HstrRegexp *hstrRegexp, char *regexp, char *text, regmatch_t *match);
bool hstr_regexp_match(HstrRegexp *hstrRegexp, const char *regexp, const char *text, regmatch_t *match, char *errorMessage, const size_t errorMessageSize);
void hstr_regexp_destroy(HstrRegexp *hstrRegexp);
#endif