xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
Commit f3ca5a46d5796113061f25981b8de3a73605c3df

Regexp hardenning - error messages still not printed.
Author: Martin Dvorak
Author date (UTC): 2014-05-04 06:11
Committer name: Martin Dvorak
Committer date (UTC): 2014-05-04 06:11
Parent(s): 96c476fe8ca5ac425c03709e013c193f9b413fec
Signing key:
Tree: 8f81ed36112f6446654d35f1d6e85ae41509f689
File Lines added Lines deleted
src/hstr.c 30 3
src/hstr_favorites.c 0 1
src/hstr_regexp.c 10 10
src/include/hstr_regexp.h 1 2
File src/hstr.c changed (mode: 100644) (index 0d24c20..dfa2928)
... ... void print_help_label()
279 279 refresh(); refresh();
280 280 } }
281 281
282 void print_cmd_deleted_label(char *cmd, int occurences, Hstr *hstr)
282 void print_cmd_deleted_label(const char *cmd, int occurences, Hstr *hstr)
283 283 { {
284 284 char screenLine[CMDLINE_LNG]; char screenLine[CMDLINE_LNG];
285 285 snprintf(screenLine, getmaxx(stdscr), "History item '%s' deleted (%d occurrence%s)", cmd, occurences, (occurences==1?"":"s")); snprintf(screenLine, getmaxx(stdscr), "History item '%s' deleted (%d occurrence%s)", cmd, occurences, (occurences==1?"":"s"));
286 // TODO make this function
286 287 if(hstr->hicolor) { if(hstr->hicolor) {
287 288 color_attr_on(COLOR_PAIR(HH_COLOR_DELETE)); color_attr_on(COLOR_PAIR(HH_COLOR_DELETE));
288 289 color_attr_on(A_BOLD); color_attr_on(A_BOLD);
 
... ... void print_cmd_deleted_label(char *cmd, int occurences, Hstr *hstr)
296 297 refresh(); refresh();
297 298 } }
298 299
299 void print_cmd_added_favorite_label(char *cmd, Hstr *hstr)
300 void print_regexp_error(const char *errorMessage)
301 {
302 char screenLine[CMDLINE_LNG];
303 snprintf(screenLine, getmaxx(stdscr), "%s", errorMessage);
304 if(hstr->hicolor) {
305 color_attr_on(COLOR_PAIR(HH_COLOR_DELETE));
306 color_attr_on(A_BOLD);
307 }
308 mvprintw(Y_OFFSET_HELP, 0, "%s", screenLine);
309 if(hstr->hicolor) {
310 color_attr_off(A_BOLD);
311 color_attr_on(COLOR_PAIR(1));
312 }
313 clrtoeol();
314 refresh();
315 }
316
317 void print_cmd_added_favorite_label(const char *cmd, Hstr *hstr)
300 318 { {
301 319 char screenLine[CMDLINE_LNG]; char screenLine[CMDLINE_LNG];
302 320 snprintf(screenLine, getmaxx(stdscr), "Command '%s' added to favorites (C-/ to show favorites)", cmd); snprintf(screenLine, getmaxx(stdscr), "Command '%s' added to favorites (C-/ to show favorites)", cmd);
 
... ... unsigned hstr_make_selection(char *prefix, HistoryItems *history, int maxSelecti
402 420 } }
403 421
404 422 regmatch_t regexpMatch; regmatch_t regexpMatch;
423 char regexpErrorMessage[CMDLINE_LNG];
424 bool regexpCompilationError=false;
405 425 for(i=0; i<count && selectionCount<maxSelectionCount; i++) { for(i=0; i<count && selectionCount<maxSelectionCount; i++) {
406 426 if(source[i]) { if(source[i]) {
407 427 if(!prefix || !strlen(prefix)) { if(!prefix || !strlen(prefix)) {
 
... ... unsigned hstr_make_selection(char *prefix, HistoryItems *history, int maxSelecti
423 443 } }
424 444 break; break;
425 445 case HH_MATCH_REGEXP: case HH_MATCH_REGEXP:
426 if(hstr_regexp_match(&(hstr->regexp), prefix, source[i], &regexpMatch)) {
446 if(hstr_regexp_match(&(hstr->regexp), prefix, source[i], &regexpMatch, regexpErrorMessage, CMDLINE_LNG)) {
427 447 hstr->selection[selectionCount]=source[i]; hstr->selection[selectionCount]=source[i];
428 448 hstr->selectionRegexpMatch[selectionCount].rm_so=regexpMatch.rm_so; hstr->selectionRegexpMatch[selectionCount].rm_so=regexpMatch.rm_so;
429 449 hstr->selectionRegexpMatch[selectionCount].rm_eo=regexpMatch.rm_eo; hstr->selectionRegexpMatch[selectionCount].rm_eo=regexpMatch.rm_eo;
430 450 selectionCount++; selectionCount++;
451 } else {
452 if(!regexpCompilationError) {
453 // TODO fix broken messages - getting just escape sequences
454 // print_regexp_error(regexpErrorMessage);
455 regexpCompilationError=true;
456 }
431 457 } }
432 458 break; break;
433 459 } }
 
... ... void loop_to_select(Hstr *hstr)
685 711 char pattern[SELECTION_PREFIX_MAX_LNG]; char pattern[SELECTION_PREFIX_MAX_LNG];
686 712 pattern[0]=0; pattern[0]=0;
687 713 // TODO this is too late! > don't render twice // TODO this is too late! > don't render twice
714 // TODO overflow
688 715 strcpy(pattern, hstr->cmdline); strcpy(pattern, hstr->cmdline);
689 716 while (!done) { while (!done) {
690 717 maxHistoryItems=get_max_history_items(stdscr); maxHistoryItems=get_max_history_items(stdscr);
File src/hstr_favorites.c changed (mode: 100644) (index 922de1d..b53451f)
... ... void favorites_save(FavoriteItems *favorites)
118 118 fclose(output_file); fclose(output_file);
119 119 } }
120 120 } }
121
122 121 free(fileName); free(fileName);
123 122 } }
124 123
File src/hstr_regexp.c changed (mode: 100644) (index 64690ed..5ac2bec)
... ... void hstr_regexp_init(HstrRegexp *hstrRegexp)
18 18 hashset_init(&hstrRegexp->cache); hashset_init(&hstrRegexp->cache);
19 19 } }
20 20
21 bool hstr_regexp_match(HstrRegexp *hstrRegexp, char *regexp, char *text, regmatch_t *match)
21 bool hstr_regexp_match(
22 HstrRegexp *hstrRegexp,
23 const char *regexp,
24 const char *text,
25 regmatch_t *match,
26 char *errorMessage,
27 const size_t errorMessageSize)
22 28 { {
23 regex_t* compiled=malloc(sizeof(regex_t));
29 regex_t* compiled;
24 30 if(hashset_contains(&hstrRegexp->cache,regexp)) { if(hashset_contains(&hstrRegexp->cache,regexp)) {
25 31 compiled=hashset_get(&hstrRegexp->cache, regexp); compiled=hashset_get(&hstrRegexp->cache, regexp);
26 32 } else { } else {
33 compiled=malloc(sizeof(regex_t));
27 34 int compilationFlags=(hstrRegexp->caseSensitive?0:REG_ICASE); int compilationFlags=(hstrRegexp->caseSensitive?0:REG_ICASE);
28 //printf("Regular expressions matching:\n '%s'\n '%s'",text,regexp);
29 35 int compilationStatus=regcomp(compiled, regexp, compilationFlags); int compilationStatus=regcomp(compiled, regexp, compilationFlags);
30 //printf("\nCompilation: %d",compilationStatus);
31 36 if(!compilationStatus) { if(!compilationStatus) {
32 37 hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled); hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled);
33 38 } else { } else {
39 regerror(compilationStatus, compiled, errorMessage, errorMessageSize);
34 40 free(compiled); free(compiled);
35 // TODO error handling: regerror() to turn error codes to messages
36 41 return false; return false;
37 42 } }
38 43 } }
 
... ... bool hstr_regexp_match(HstrRegexp *hstrRegexp, char *regexp, char *text, regmatc
41 46 regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE]; regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE];
42 47 int matchingFlags=0; int matchingFlags=0;
43 48 int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags); int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags);
44 //printf("\nMatching (status/matches): %s %d",(!matchingStatus?"match":"no-match"),matches);
45
46 49 if(!matchingStatus) { if(!matchingStatus) {
47 //printf("\n %d %d",matchPtr[0].rm_so, matchPtr[0].rm_eo);
48 50 if(matchPtr[0].rm_so != -1) { if(matchPtr[0].rm_so != -1) {
49 //printf("\n* %d %d",matchPtr[0].rm_so,matchPtr[0].rm_eo);
50 51 match->rm_so=matchPtr[0].rm_so; match->rm_so=matchPtr[0].rm_so;
51 52 match->rm_eo=matchPtr[0].rm_eo; match->rm_eo=matchPtr[0].rm_eo;
52 53 return true; return true;
 
... ... void hstr_regexp_destroy(HstrRegexp *hstrRegexp)
59 60 { {
60 61 // TODO hashset_destroy(); // TODO hashset_destroy();
61 62 } }
62
File src/include/hstr_regexp.h changed (mode: 100644) (index fc7643f..a1fcacd)
18 18
19 19 typedef struct { typedef struct {
20 20 bool caseSensitive; bool caseSensitive;
21
22 21 HashSet cache; HashSet cache;
23 22 } HstrRegexp; } HstrRegexp;
24 23
25 24 void hstr_regexp_init(HstrRegexp *hstrRegexp); void hstr_regexp_init(HstrRegexp *hstrRegexp);
26 bool hstr_regexp_match(HstrRegexp *hstrRegexp, char *regexp, char *text, regmatch_t *match);
25 bool hstr_regexp_match(HstrRegexp *hstrRegexp, const char *regexp, const char *text, regmatch_t *match, char *errorMessage, const size_t errorMessageSize);
27 26 void hstr_regexp_destroy(HstrRegexp *hstrRegexp); void hstr_regexp_destroy(HstrRegexp *hstrRegexp);
28 27
29 28 #endif #endif
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master