diff --git a/src/hashset.c b/src/hashset.c index b73f8fd8..6e06ee06 100644 --- a/src/hashset.c +++ b/src/hashset.c @@ -46,7 +46,7 @@ void *hashset_get(const HashSet * hs, const char *key) int hashset_contains(const HashSet * hs, const char *key) { - return (hashset_get(hs, key) != NULL); + return (hashset_get(hs, key) != NULL); } int hashset_put(HashSet *hs, const char *key, void *value) @@ -74,7 +74,7 @@ int hashset_put(HashSet *hs, const char *key, void *value) int hashset_add(HashSet * hs, const char *key) { - return hashset_put(hs, key, "nil"); + return hashset_put(hs, key, "nil"); } int hashset_size(const HashSet *hs) @@ -95,48 +95,48 @@ void hashset_stat(const HashSet *hs) char** hashset_keys(const HashSet *hs) { - if(hs->currentSize) { - char **result=malloc(sizeof(char*) * hs->currentSize); - int i=0, j=0; - struct HashSetNode *p; - for(i=0; ilists[i]; - while(p && p->next) { - result[j++]=hstr_strdup(p->key); - p=p->next; - } - if(p) { - result[j++]=hstr_strdup(p->key); - } - } - return result; - } else { - return NULL; - } + if(hs->currentSize) { + char **result=malloc(sizeof(char*) * hs->currentSize); + int i=0, j=0; + struct HashSetNode *p; + for(i=0; ilists[i]; + while(p && p->next) { + result[j++]=hstr_strdup(p->key); + p=p->next; + } + if(p) { + result[j++]=hstr_strdup(p->key); + } + } + return result; + } else { + return NULL; + } } void hashset_destroy(HashSet *hs, const bool freeValues) { - // only hashset keys (and possibly values) are freed - caller must free hashset itself - if(hs && hs->currentSize) { - int i=0; - struct HashSetNode *p, *pp; - for(i=0; ilists[i]; - while(p && p->next) { - if(p->key) { - free(p->key); - if(freeValues && p->value) free(p->value); - } - pp=p; - p=p->next; - free(pp); - } - if(p && p->key) { - free(p->key); - if(freeValues && p->value) free(p->value); - free(p); - } - } - } + // only hashset keys (and possibly values) are freed - caller must free hashset itself + if(hs && hs->currentSize) { + int i=0; + struct HashSetNode *p, *pp; + for(i=0; ilists[i]; + while(p && p->next) { + if(p->key) { + free(p->key); + if(freeValues && p->value) free(p->value); + } + pp=p; + p=p->next; + free(pp); + } + if(p && p->key) { + free(p->key); + if(freeValues && p->value) free(p->value); + free(p); + } + } + } } diff --git a/src/hstr_curses.c b/src/hstr_curses.c index 48404065..55908d34 100644 --- a/src/hstr_curses.c +++ b/src/hstr_curses.c @@ -14,18 +14,18 @@ static bool terminalHasColors=FALSE; void hstr_curses_start() { - initscr(); - keypad(stdscr, TRUE); - noecho(); - terminalHasColors=has_colors(); - if(terminalHasColors) { - start_color(); - use_default_colors(); - } + initscr(); + keypad(stdscr, TRUE); + noecho(); + terminalHasColors=has_colors(); + if(terminalHasColors) { + start_color(); + use_default_colors(); + } } bool terminal_has_colors() { - return terminalHasColors; + return terminalHasColors; } void hstr_curses_stop() { diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index 77055c77..be966cfc 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -18,180 +18,180 @@ void favorites_init(FavoriteItems *favorites) { - favorites->items=NULL; - favorites->count=0; - favorites->loaded=false; - favorites->set=malloc(sizeof(HashSet)); + favorites->items=NULL; + favorites->count=0; + favorites->loaded=false; + favorites->set=malloc(sizeof(HashSet)); } void favorites_show(FavoriteItems *favorites) { - printf("\n\nFavorites (%d):", favorites->count); - if(favorites->count) { - int i; - for(i=0;icount;i++) { - printf("\n%s",favorites->items[i]); - } - } - printf("\n"); + printf("\n\nFavorites (%d):", favorites->count); + if(favorites->count) { + int i; + for(i=0;icount;i++) { + printf("\n%s",favorites->items[i]); + } + } + printf("\n"); } char* favorites_get_filename() { - char *home = getenv(ENV_VAR_HOME); - char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_RC) + 1); - strcpy(fileName, home); - strcat(fileName, "/"); - strcat(fileName, FILE_HH_RC); - return fileName; + char *home = getenv(ENV_VAR_HOME); + char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_RC) + 1); + strcpy(fileName, home); + strcat(fileName, "/"); + strcat(fileName, FILE_HH_RC); + return fileName; } void favorites_get(FavoriteItems *favorites) { - if(!favorites->loaded) { - char* fileName = favorites_get_filename(); - char *file_contents=NULL; - if(access(fileName, F_OK) != -1) { - long input_file_size; - - FILE *input_file = fopen(fileName, "rb"); - fseek(input_file, 0, SEEK_END); - input_file_size = ftell(input_file); - rewind(input_file); - file_contents = malloc((input_file_size + 1) * (sizeof(char))); - if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { - exit(EXIT_FAILURE); - } - fclose(input_file); - file_contents[input_file_size] = 0; - - if(file_contents && strlen(file_contents)) { - favorites->count = 0; - char *p=strchr(file_contents,'\n'); - while (p!=NULL) { - favorites->count++; - p=strchr(p+1,'\n'); - } - - favorites->items = malloc(sizeof(char*) * favorites->count); - favorites->count=0; - char *pb=file_contents, *pe, *s; - pe=strchr(file_contents, '\n'); - HashSet set; - hashset_init(&set); - while(pe!=NULL) { - *pe=0; - if(!hashset_contains(&set,pb)) { - s=hstr_strdup(pb); - favorites->items[favorites->count++]=s; - hashset_add(&set,s); - } - pb=pe+1; - pe=strchr(pb, '\n'); - } - free(file_contents); - } - } else { - // favorites file not found > favorites don't exist yet - favorites->loaded=true; - return; - } - free(fileName); - } + if(!favorites->loaded) { + char* fileName = favorites_get_filename(); + char *file_contents=NULL; + if(access(fileName, F_OK) != -1) { + long input_file_size; + + FILE *input_file = fopen(fileName, "rb"); + fseek(input_file, 0, SEEK_END); + input_file_size = ftell(input_file); + rewind(input_file); + file_contents = malloc((input_file_size + 1) * (sizeof(char))); + if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { + exit(EXIT_FAILURE); + } + fclose(input_file); + file_contents[input_file_size] = 0; + + if(file_contents && strlen(file_contents)) { + favorites->count = 0; + char *p=strchr(file_contents,'\n'); + while (p!=NULL) { + favorites->count++; + p=strchr(p+1,'\n'); + } + + favorites->items = malloc(sizeof(char*) * favorites->count); + favorites->count=0; + char *pb=file_contents, *pe, *s; + pe=strchr(file_contents, '\n'); + HashSet set; + hashset_init(&set); + while(pe!=NULL) { + *pe=0; + if(!hashset_contains(&set,pb)) { + s=hstr_strdup(pb); + favorites->items[favorites->count++]=s; + hashset_add(&set,s); + } + pb=pe+1; + pe=strchr(pb, '\n'); + } + free(file_contents); + } + } else { + // favorites file not found > favorites don't exist yet + favorites->loaded=true; + return; + } + free(fileName); + } } void favorites_save(FavoriteItems *favorites) { - char *fileName=favorites_get_filename(); - - if(favorites->count) { - FILE *output_file = fopen(fileName, "wb"); - rewind(output_file); - int i; - for(i=0; icount; i++) { - if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) { - exit(EXIT_FAILURE); - } - if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) { - exit(EXIT_FAILURE); - } - } - fclose(output_file); - } else { - if(access(fileName, F_OK) != -1) { - FILE *output_file = fopen(fileName, "wb"); - fclose(output_file); - } - } - free(fileName); + char *fileName=favorites_get_filename(); + + if(favorites->count) { + FILE *output_file = fopen(fileName, "wb"); + rewind(output_file); + int i; + for(i=0; icount; i++) { + if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) { + exit(EXIT_FAILURE); + } + if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) { + exit(EXIT_FAILURE); + } + } + fclose(output_file); + } else { + if(access(fileName, F_OK) != -1) { + FILE *output_file = fopen(fileName, "wb"); + fclose(output_file); + } + } + free(fileName); } void favorites_add(FavoriteItems *favorites, char *newFavorite) { - if(favorites->count) { - favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); - favorites->items[favorites->count-1]=hstr_strdup(newFavorite); - favorites_choose(favorites, newFavorite); - } else { - favorites->items=malloc(sizeof(char*)); - favorites->items[0]=hstr_strdup(newFavorite); - favorites->count=1; - } - - favorites_save(favorites); + if(favorites->count) { + favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); + favorites->items[favorites->count-1]=hstr_strdup(newFavorite); + favorites_choose(favorites, newFavorite); + } else { + favorites->items=malloc(sizeof(char*)); + favorites->items[0]=hstr_strdup(newFavorite); + favorites->count=1; + } + + favorites_save(favorites); } void favorites_choose(FavoriteItems *favorites, char *choice) { - if(favorites->count && choice) { - int r; - char *b=NULL, *next; - for(r=0; rcount; r++) { - if(!strcmp(favorites->items[r],choice)) { - favorites->items[0]=favorites->items[r]; - if(b) { - favorites->items[r]=b; - } - favorites_save(favorites); - return; - } - next=favorites->items[r]; - favorites->items[r]=b; - b=next; - } - } + if(favorites->count && choice) { + int r; + char *b=NULL, *next; + for(r=0; rcount; r++) { + if(!strcmp(favorites->items[r],choice)) { + favorites->items[0]=favorites->items[r]; + if(b) { + favorites->items[r]=b; + } + favorites_save(favorites); + return; + } + next=favorites->items[r]; + favorites->items[r]=b; + b=next; + } + } } bool favorites_remove(FavoriteItems *favorites, char *almostDead) { - if(favorites->count) { - int r, w, count; - count=favorites->count; - for(r=0, w=0; ritems[r], almostDead)) { - favorites->count--; - } else { - if(witems[w]=favorites->items[r]; - } - w++; - } - } - favorites_save(favorites); - return true; - } else { - return false; - } + if(favorites->count) { + int r, w, count; + count=favorites->count; + for(r=0, w=0; ritems[r], almostDead)) { + favorites->count--; + } else { + if(witems[w]=favorites->items[r]; + } + w++; + } + } + favorites_save(favorites); + return true; + } else { + return false; + } } void favorites_destroy(FavoriteItems *favorites) { - if(favorites) { - int i; - for(i=0; icount; i++) { - free(favorites->items[i]); - } - hashset_destroy(favorites->set, false); - free(favorites); - } + if(favorites) { + int i; + for(i=0; icount; i++) { + free(favorites->items[i]); + } + hashset_destroy(favorites->set, false); + free(favorites); + } } diff --git a/src/hstr_history.c b/src/hstr_history.c index 35655db8..722625fc 100644 --- a/src/hstr_history.c +++ b/src/hstr_history.c @@ -16,16 +16,16 @@ #include typedef struct { - char *item; - unsigned rank; + char *item; + unsigned rank; } RankedHistoryItem; static HistoryItems *prioritizedHistory; static bool dirty; static const char *commandBlacklist[] = { - "ls", "pwd", "cd", "cd ..", "hh", "mc", - "ls ", "pwd ", "cd ", "cd .. ", "hh ", "mc " + "ls", "pwd", "cd", "cd ..", "hh", "mc", + "ls ", "pwd ", "cd ", "cd .. ", "hh ", "mc " }; #ifdef DEBUG_RADIX @@ -35,167 +35,167 @@ static const char *commandBlacklist[] = { #endif unsigned history_ranking_function(unsigned rank, int newOccurenceOrder, size_t length) { - long metrics=rank+(log(newOccurenceOrder)*10.0)+length; - // alternative metrics: - // rank+newOccurenceOrder/10+length - assert(metricscount; i++) { - if(ph->items[i]!=NULL) { - printf("\n%s",ph->items[i]); fflush(stdout); - } else { - printf("\n %d NULL",i); fflush(stdout); - } - } - printf("\n"); fflush(stdout); + printf("\n\nPrioritized history:"); + int i; + for(i=0; icount; i++) { + if(ph->items[i]!=NULL) { + printf("\n%s",ph->items[i]); fflush(stdout); + } else { + printf("\n %d NULL",i); fflush(stdout); + } + } + printf("\n"); fflush(stdout); } HistoryItems *get_prioritized_history() { - using_history(); - - char *historyFile = get_history_file_name(); - if(read_history(historyFile)!=0) { - fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile); - exit(EXIT_FAILURE); - } - HISTORY_STATE *historyState=history_get_history_state(); - - if(historyState->length > 0) { - HashSet rankmap; - hashset_init(&rankmap); - - HashSet blacklist; - int i; - hashset_init(&blacklist); - int length=sizeof(commandBlacklist)/sizeof(commandBlacklist[0]); - for(i=0; isize*1000; - radixsort_init(&rs, (radixMaxKeyEstimate<100000?100000:radixMaxKeyEstimate)); - rs.optFloorAndInsertBigKeys=true; - - RankedHistoryItem *r; - RadixItem *radixItem; + using_history(); + + char *historyFile = get_history_file_name(); + if(read_history(historyFile)!=0) { + fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile); + exit(EXIT_FAILURE); + } + HISTORY_STATE *historyState=history_get_history_state(); + + if(historyState->length > 0) { + HashSet rankmap; + hashset_init(&rankmap); + + HashSet blacklist; + int i; + hashset_init(&blacklist); + int length=sizeof(commandBlacklist)/sizeof(commandBlacklist[0]); + for(i=0; isize*1000; + radixsort_init(&rs, (radixMaxKeyEstimate<100000?100000:radixMaxKeyEstimate)); + rs.optFloorAndInsertBigKeys=true; + + RankedHistoryItem *r; + RadixItem *radixItem; HIST_ENTRY **historyList=history_list(); char **rawHistory=malloc(sizeof(char*) * historyState->length); int rawOffset=historyState->length-1; - char *line; - for(i=0; ilength; i++, rawOffset--) { - line=historyList[i]->line; - rawHistory[rawOffset]=line; - if(hashset_contains(&blacklist, line)) { - continue; - } - if((r=hashset_get(&rankmap, line))==NULL) { - r=malloc(sizeof(RankedHistoryItem)); - r->rank=history_ranking_function(0, i, strlen(line)); - r->item=historyList[i]->line; - - hashset_put(&rankmap, line, r); - - radixItem=malloc(sizeof(RadixItem)); - radixItem->key=r->rank; - radixItem->data=r; - radixItem->next=NULL; - radixsort_add(&rs, radixItem); - } else { - radixItem=radix_cut(&rs, r->rank, r); - - assert(radixItem); - - if(radixItem) { - r->rank=history_ranking_function(r->rank, i, strlen(line)); - radixItem->key=r->rank; - radixsort_add(&rs, radixItem); - } - } - } - - DEBUG_RADIXSORT(); - - RadixItem **prioritizedRadix=radixsort_dump(&rs); - prioritizedHistory=malloc(sizeof(HistoryItems)); - prioritizedHistory->count=rs.size; - prioritizedHistory->rawCount=historyState->length; - prioritizedHistory->items=malloc(rs.size * sizeof(char*)); - prioritizedHistory->raw=rawHistory; - for(i=0; idata) { - prioritizedHistory->items[i]=((RankedHistoryItem *)(prioritizedRadix[i]->data))->item; - } - free(prioritizedRadix[i]->data); - free(prioritizedRadix[i]); - } - - radixsort_destroy(&rs); - // TODO rankmap (?) and blacklist (?) to be destroyed - - return prioritizedHistory; - } else { - return NULL; - } + char *line; + for(i=0; ilength; i++, rawOffset--) { + line=historyList[i]->line; + rawHistory[rawOffset]=line; + if(hashset_contains(&blacklist, line)) { + continue; + } + if((r=hashset_get(&rankmap, line))==NULL) { + r=malloc(sizeof(RankedHistoryItem)); + r->rank=history_ranking_function(0, i, strlen(line)); + r->item=historyList[i]->line; + + hashset_put(&rankmap, line, r); + + radixItem=malloc(sizeof(RadixItem)); + radixItem->key=r->rank; + radixItem->data=r; + radixItem->next=NULL; + radixsort_add(&rs, radixItem); + } else { + radixItem=radix_cut(&rs, r->rank, r); + + assert(radixItem); + + if(radixItem) { + r->rank=history_ranking_function(r->rank, i, strlen(line)); + radixItem->key=r->rank; + radixsort_add(&rs, radixItem); + } + } + } + + DEBUG_RADIXSORT(); + + RadixItem **prioritizedRadix=radixsort_dump(&rs); + prioritizedHistory=malloc(sizeof(HistoryItems)); + prioritizedHistory->count=rs.size; + prioritizedHistory->rawCount=historyState->length; + prioritizedHistory->items=malloc(rs.size * sizeof(char*)); + prioritizedHistory->raw=rawHistory; + for(i=0; idata) { + prioritizedHistory->items[i]=((RankedHistoryItem *)(prioritizedRadix[i]->data))->item; + } + free(prioritizedRadix[i]->data); + free(prioritizedRadix[i]); + } + + radixsort_destroy(&rs); + // TODO rankmap (?) and blacklist (?) to be destroyed + + return prioritizedHistory; + } else { + return NULL; + } } void free_prioritized_history() { - free(prioritizedHistory->items); - free(prioritizedHistory); + free(prioritizedHistory->items); + free(prioritizedHistory); } void history_mgmt_open() { - dirty=false; + dirty=false; } void history_clear_dirty() { - dirty=false; + dirty=false; } int history_mgmt_remove(char *cmd) { - int offset=history_search_pos(cmd, 0, 0), occurences=0; - char *l; - HISTORY_STATE *historyState=history_get_history_state(); - while(offset>=0) { - l=historyState->entries[offset]->line; - if(offsetlength && !strcmp(cmd, l)) { - occurences++; - free_history_entry(remove_history(offset)); - } - offset=history_search_pos(cmd, 0, ++offset); - } - if(occurences) { - write_history(get_history_file_name()); - dirty=true; - } - return occurences; + int offset=history_search_pos(cmd, 0, 0), occurences=0; + char *l; + HISTORY_STATE *historyState=history_get_history_state(); + while(offset>=0) { + l=historyState->entries[offset]->line; + if(offsetlength && !strcmp(cmd, l)) { + occurences++; + free_history_entry(remove_history(offset)); + } + offset=history_search_pos(cmd, 0, ++offset); + } + if(occurences) { + write_history(get_history_file_name()); + dirty=true; + } + return occurences; } void history_mgmt_flush() { - if(dirty) { - fill_terminal_input("history -r\n", false); - } + if(dirty) { + fill_terminal_input("history -r\n", false); + } } diff --git a/src/hstr_regexp.c b/src/hstr_regexp.c index 06e90293..dc6c59d1 100644 --- a/src/hstr_regexp.c +++ b/src/hstr_regexp.c @@ -15,48 +15,48 @@ void hstr_regexp_init(HstrRegexp *hstrRegexp) { - hashset_init(&hstrRegexp->cache); + hashset_init(&hstrRegexp->cache); } bool hstr_regexp_match( - HstrRegexp *hstrRegexp, - const char *regexp, - const char *text, - regmatch_t *match, - char *errorMessage, - const size_t errorMessageSize) + HstrRegexp *hstrRegexp, + const char *regexp, + const char *text, + regmatch_t *match, + char *errorMessage, + const size_t errorMessageSize) { - 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); - int compilationStatus=regcomp(compiled, regexp, compilationFlags); - if(!compilationStatus) { - hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled); - } else { - regerror(compilationStatus, compiled, errorMessage, errorMessageSize); - free(compiled); - return false; - } - } + 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); + int compilationStatus=regcomp(compiled, regexp, compilationFlags); + if(!compilationStatus) { + hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled); + } else { + regerror(compilationStatus, compiled, errorMessage, errorMessageSize); + free(compiled); + return false; + } + } - int matches=REGEXP_MATCH_BUFFER_SIZE; - regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE]; - int matchingFlags=0; - int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags); - if(!matchingStatus) { - if(matchPtr[0].rm_so != -1) { - match->rm_so=matchPtr[0].rm_so; - match->rm_eo=matchPtr[0].rm_eo; - return true; - } - } - return false; + int matches=REGEXP_MATCH_BUFFER_SIZE; + regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE]; + int matchingFlags=0; + int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags); + if(!matchingStatus) { + if(matchPtr[0].rm_so != -1) { + match->rm_so=matchPtr[0].rm_so; + match->rm_eo=matchPtr[0].rm_eo; + return true; + } + } + return false; } void hstr_regexp_destroy(HstrRegexp *hstrRegexp) { - hashset_destroy(&hstrRegexp->cache, true); + hashset_destroy(&hstrRegexp->cache, true); } diff --git a/src/hstr_utils.c b/src/hstr_utils.c index 720663b4..57b7621f 100644 --- a/src/hstr_utils.c +++ b/src/hstr_utils.c @@ -25,72 +25,72 @@ char *hstr_strdup(const char * s) // wide char aware strlen() int hstr_strlen(const char *s) { - if(s) { - int result=0; - bool isHighBitSet=false; - int i=0; - while(s[i]) { - if(1<<7 & s[i]) { - if(isHighBitSet) { - isHighBitSet=false; - result++; - } else { - isHighBitSet=true; - } - } else { - result++; - } - i++; - } - return result; - } else { - return 0; - } + if(s) { + int result=0; + bool isHighBitSet=false; + int i=0; + while(s[i]) { + if(1<<7 & s[i]) { + if(isHighBitSet) { + isHighBitSet=false; + result++; + } else { + isHighBitSet=true; + } + } else { + result++; + } + i++; + } + return result; + } else { + return 0; + } } void hstr_chop(char *s) { - if(s) { - int i=strlen(s); - if(i) { - if(1<<7 & s[i-1]) { - s[i-2]=0; - } else { - s[i-1]=0; - } - } - } + if(s) { + int i=strlen(s); + if(i) { + if(1<<7 & s[i-1]) { + s[i-2]=0; + } else { + s[i-1]=0; + } + } + } } void tiocsti() { - char buf[] = DEFAULT_COMMAND; - int i; - for (i = 0; i < sizeof buf - 1; i++) { - ioctl(0, TIOCSTI, &buf[i]); - } + char buf[] = DEFAULT_COMMAND; + int i; + for (i = 0; i < sizeof buf - 1; i++) { + ioctl(0, TIOCSTI, &buf[i]); + } } void fill_terminal_input(char *cmd, bool padding) { - if(cmd && strlen(cmd)>0) { - size_t size = strlen(cmd); - unsigned i; - char *c; - for (i = 0; i < size; i++) { - // terminal I/O control, simulate terminal input - c=(cmd+i); - ioctl(0, TIOCSTI, c); - } - // echo, but don't flush to terminal - if(padding) printf("\n"); - } + if(cmd && strlen(cmd)>0) { + size_t size = strlen(cmd); + unsigned i; + char *c; + for (i = 0; i < size; i++) { + // terminal I/O control, simulate terminal input + c=(cmd+i); + ioctl(0, TIOCSTI, c); + } + // echo, but don't flush to terminal + if(padding) printf("\n"); + } } void reverse_char_pointer_array(char **array, unsigned length) { - char *temp; - unsigned i; + char *temp; + unsigned i; for (i=0; i0) { - int i; - for(i = 0; str[i]; i++){ - if(lowercase) { - str[i] = tolower(str[i]); - } else { - str[i] = toupper(str[i]); - } - } - } + if(str && strlen(str)>0) { + int i; + for(i = 0; str[i]; i++){ + if(lowercase) { + str[i] = tolower(str[i]); + } else { + str[i] = toupper(str[i]); + } + } + } } diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index 0ff47ba5..f3573a94 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -20,10 +20,10 @@ #define FILE_HH_RC ".hh_favorites" typedef struct { - char **items; - unsigned count; - bool loaded; - HashSet *set; + char **items; + unsigned count; + bool loaded; + HashSet *set; } FavoriteItems; void favorites_init(FavoriteItems *favorites); diff --git a/src/include/hstr_history.h b/src/include/hstr_history.h index 82ddebcf..73d2916d 100644 --- a/src/include/hstr_history.h +++ b/src/include/hstr_history.h @@ -28,10 +28,10 @@ #define FILE_DEFAULT_HISTORY ".bash_history" typedef struct { - char **items; - unsigned count; - char **raw; - unsigned rawCount; + char **items; + unsigned count; + char **raw; + unsigned rawCount; } HistoryItems; HistoryItems *get_prioritized_history(); diff --git a/src/include/hstr_regexp.h b/src/include/hstr_regexp.h index a1fcacd2..70e1a891 100644 --- a/src/include/hstr_regexp.h +++ b/src/include/hstr_regexp.h @@ -17,8 +17,8 @@ #include "hashset.h" typedef struct { - bool caseSensitive; - HashSet cache; + bool caseSensitive; + HashSet cache; } HstrRegexp; void hstr_regexp_init(HstrRegexp *hstrRegexp); diff --git a/src/include/radixsort.h b/src/include/radixsort.h index fa5d8647..cbb5cfc4 100644 --- a/src/include/radixsort.h +++ b/src/include/radixsort.h @@ -24,30 +24,30 @@ #define RADIX_DEBUG_LEVEL_DEBUG 2 typedef struct radixitem { - unsigned key; - void *data; - struct radixitem *next; + unsigned key; + void *data; + struct radixitem *next; } RadixItem; typedef struct radixslot { - unsigned min; - unsigned max; - unsigned size; + unsigned min; + unsigned max; + unsigned size; } RadixSlot; typedef struct { - unsigned size; - unsigned maxKey; - unsigned keyLimit; - RadixItem ***topDigits; - - bool optFloorAndInsertBigKeys; - bool optIgnoreBigKeys; - - RadixSlot **_slotDescriptors; - unsigned _slotsCount; - unsigned _topIndexLimit; - unsigned _debug; + unsigned size; + unsigned maxKey; + unsigned keyLimit; + RadixItem ***topDigits; + + bool optFloorAndInsertBigKeys; + bool optIgnoreBigKeys; + + RadixSlot **_slotDescriptors; + unsigned _slotsCount; + unsigned _topIndexLimit; + unsigned _debug; } RadixSorter; void radixsort_init(RadixSorter *rs, unsigned keyLimit); diff --git a/src/radixsort.c b/src/radixsort.c index 6c4cbc57..6ab0d4ec 100644 --- a/src/radixsort.c +++ b/src/radixsort.c @@ -14,217 +14,217 @@ void radixsort_init(RadixSorter *rs, unsigned keyLimit) { - rs->optFloorAndInsertBigKeys=false; - rs->optIgnoreBigKeys=false; - - rs->_topIndexLimit=GET_TOP_INDEX(keyLimit); - rs->size=0; - rs->topDigits=malloc(rs->_topIndexLimit * sizeof(RadixItem ***)); - memset(rs->topDigits, 0, rs->_topIndexLimit * sizeof(RadixItem ***)); - rs->maxKey=0; - rs->keyLimit=keyLimit; - - rs->_slotDescriptors=malloc(rs->_topIndexLimit * sizeof(RadixSlot **)); - rs->_slotsCount=0; + rs->optFloorAndInsertBigKeys=false; + rs->optIgnoreBigKeys=false; + + rs->_topIndexLimit=GET_TOP_INDEX(keyLimit); + rs->size=0; + rs->topDigits=malloc(rs->_topIndexLimit * sizeof(RadixItem ***)); + memset(rs->topDigits, 0, rs->_topIndexLimit * sizeof(RadixItem ***)); + rs->maxKey=0; + rs->keyLimit=keyLimit; + + rs->_slotDescriptors=malloc(rs->_topIndexLimit * sizeof(RadixSlot **)); + rs->_slotsCount=0; } void radixsort_set_debug_level(RadixSorter *rs, unsigned debugLevel) { - rs->_debug=debugLevel; + rs->_debug=debugLevel; } RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex) { - RadixItem **slot=malloc(RADIX_SLOT_SIZE * sizeof(RadixItem *)); - memset(slot, 0, RADIX_SLOT_SIZE * sizeof(RadixItem *)); + RadixItem **slot=malloc(RADIX_SLOT_SIZE * sizeof(RadixItem *)); + memset(slot, 0, RADIX_SLOT_SIZE * sizeof(RadixItem *)); - RadixSlot *descriptor=malloc(sizeof(RadixSlot)); - descriptor->min=rs->keyLimit; - descriptor->max=0; - descriptor->size=0; + RadixSlot *descriptor=malloc(sizeof(RadixSlot)); + descriptor->min=rs->keyLimit; + descriptor->max=0; + descriptor->size=0; - rs->_slotDescriptors[topIndex]=descriptor; - rs->_slotsCount++; - return slot; + rs->_slotDescriptors[topIndex]=descriptor; + rs->_slotsCount++; + return slot; } void radixsort_add(RadixSorter *rs, RadixItem *item) { - if(item->key > rs->keyLimit) { - if(rs->_debug > RADIX_DEBUG_LEVEL_NONE) { - fprintf(stderr, "WARNING: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key); - } - if(rs->optFloorAndInsertBigKeys) { - item->key = rs->keyLimit-1; - } else { - if(rs->optIgnoreBigKeys) { - return; - } else { - exit(0); - } - } - } - - unsigned topIndex = GET_TOP_INDEX(item->key); - unsigned lowIndex = GET_LOW_INDEX(item->key); - - if(!rs->topDigits[topIndex]) { - rs->topDigits[topIndex]=radixsort_get_slot(rs, topIndex); - } - - RadixItem *chain=rs->topDigits[topIndex][lowIndex]; - rs->topDigits[topIndex][lowIndex]=item; - if(chain==NULL) { - item->next=NULL; - } else { - item->next=chain; - } - - rs->size++; - rs->maxKey=MAX(rs->maxKey,item->key); - rs->_slotDescriptors[topIndex]->min=MIN(rs->_slotDescriptors[topIndex]->min,item->key); - rs->_slotDescriptors[topIndex]->max=MAX(rs->_slotDescriptors[topIndex]->max,item->key); - rs->_slotDescriptors[topIndex]->size++; + if(item->key > rs->keyLimit) { + if(rs->_debug > RADIX_DEBUG_LEVEL_NONE) { + fprintf(stderr, "WARNING: Radix sort overflow - inserted key is bigger than limit (%u): %u\n", rs->keyLimit, item->key); + } + if(rs->optFloorAndInsertBigKeys) { + item->key = rs->keyLimit-1; + } else { + if(rs->optIgnoreBigKeys) { + return; + } else { + exit(0); + } + } + } + + unsigned topIndex = GET_TOP_INDEX(item->key); + unsigned lowIndex = GET_LOW_INDEX(item->key); + + if(!rs->topDigits[topIndex]) { + rs->topDigits[topIndex]=radixsort_get_slot(rs, topIndex); + } + + RadixItem *chain=rs->topDigits[topIndex][lowIndex]; + rs->topDigits[topIndex][lowIndex]=item; + if(chain==NULL) { + item->next=NULL; + } else { + item->next=chain; + } + + rs->size++; + rs->maxKey=MAX(rs->maxKey,item->key); + rs->_slotDescriptors[topIndex]->min=MIN(rs->_slotDescriptors[topIndex]->min,item->key); + rs->_slotDescriptors[topIndex]->max=MAX(rs->_slotDescriptors[topIndex]->max,item->key); + rs->_slotDescriptors[topIndex]->size++; } void radix_dec_slot_descriptor_size(RadixSorter *rs, RadixSlot *descriptor, unsigned key, unsigned topIndex) { - descriptor->size--; - if(!descriptor->size) { - descriptor->min=rs->keyLimit; - descriptor->max=0; - } else { - if(descriptor->size==1) { - if(rs->topDigits[topIndex][GET_LOW_INDEX(descriptor->max)]) { - descriptor->min=descriptor->max; - } else { - if(rs->topDigits[topIndex][GET_LOW_INDEX(descriptor->min)]) { - descriptor->max=descriptor->min; - } - } - } - } + descriptor->size--; + if(!descriptor->size) { + descriptor->min=rs->keyLimit; + descriptor->max=0; + } else { + if(descriptor->size==1) { + if(rs->topDigits[topIndex][GET_LOW_INDEX(descriptor->max)]) { + descriptor->min=descriptor->max; + } else { + if(rs->topDigits[topIndex][GET_LOW_INDEX(descriptor->min)]) { + descriptor->max=descriptor->min; + } + } + } + } } RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data) { - // TODO optimization: fix min/max on cut of a value - if(key <= rs->maxKey) { - unsigned topIndex = GET_TOP_INDEX(key); - unsigned lowIndex = GET_LOW_INDEX(key); - - if(rs->topDigits[topIndex]) { - RadixItem *ri=rs->topDigits[topIndex][lowIndex]; - RadixItem *lastRi=NULL; - while(ri->data!=data) { - if(ri->next) { - lastRi=ri; - ri=ri->next; - } else { - break; - } - } - if(ri->data==data) { - if(lastRi) { - lastRi->next=ri->next; - } else { - rs->topDigits[topIndex][lowIndex]=ri->next; - } - ri->next=NULL; - rs->size--; - radix_dec_slot_descriptor_size(rs, rs->_slotDescriptors[topIndex], key, topIndex); - return ri; - } - } - } - return NULL; + // TODO optimization: fix min/max on cut of a value + if(key <= rs->maxKey) { + unsigned topIndex = GET_TOP_INDEX(key); + unsigned lowIndex = GET_LOW_INDEX(key); + + if(rs->topDigits[topIndex]) { + RadixItem *ri=rs->topDigits[topIndex][lowIndex]; + RadixItem *lastRi=NULL; + while(ri->data!=data) { + if(ri->next) { + lastRi=ri; + ri=ri->next; + } else { + break; + } + } + if(ri->data==data) { + if(lastRi) { + lastRi->next=ri->next; + } else { + rs->topDigits[topIndex][lowIndex]=ri->next; + } + ri->next=NULL; + rs->size--; + radix_dec_slot_descriptor_size(rs, rs->_slotDescriptors[topIndex], key, topIndex); + return ri; + } + } + } + return NULL; } RadixItem **radixsort_dump(RadixSorter *rs) { - if(rs->size>0) { - RadixItem **result=malloc(rs->size * sizeof(RadixItem *)); - int t = GET_TOP_INDEX(rs->maxKey); - int slotMin, slotSize, slotCount, l; - unsigned items=0; - do { - if(rs->topDigits[t]) { - if(rs->_slotDescriptors[t]->size>0) { - l=GET_LOW_INDEX(rs->_slotDescriptors[t]->max); - slotMin=GET_LOW_INDEX(rs->_slotDescriptors[t]->min); - slotSize=rs->_slotDescriptors[t]->size; - slotCount=0; - do { - if(rs->topDigits[t][l]) { - result[items++]=rs->topDigits[t][l]; - slotCount++; - RadixItem *ri=rs->topDigits[t][l]->next; - while(ri) { - result[items++]=ri; - slotCount++; - ri=ri->next; - } - } - } while(--l>=slotMin && slotCount=0); - return result; - } - return NULL; + if(rs->size>0) { + RadixItem **result=malloc(rs->size * sizeof(RadixItem *)); + int t = GET_TOP_INDEX(rs->maxKey); + int slotMin, slotSize, slotCount, l; + unsigned items=0; + do { + if(rs->topDigits[t]) { + if(rs->_slotDescriptors[t]->size>0) { + l=GET_LOW_INDEX(rs->_slotDescriptors[t]->max); + slotMin=GET_LOW_INDEX(rs->_slotDescriptors[t]->min); + slotSize=rs->_slotDescriptors[t]->size; + slotCount=0; + do { + if(rs->topDigits[t][l]) { + result[items++]=rs->topDigits[t][l]; + slotCount++; + RadixItem *ri=rs->topDigits[t][l]->next; + while(ri) { + result[items++]=ri; + slotCount++; + ri=ri->next; + } + } + } while(--l>=slotMin && slotCount=0); + return result; + } + return NULL; } void radixsort_stat(RadixSorter *rs, bool listing) { - printf("\n Radixsort (size/max/limit/slot count): %u %u %u %u", rs->size, rs->maxKey, rs->keyLimit, rs->_slotsCount); - unsigned memory=rs->_topIndexLimit * sizeof(RadixItem ***); - memory+=memory; - memory+=rs->_slotsCount*(RADIX_SLOT_SIZE * sizeof(RadixItem *)); - printf("\n Memory: %u\n", memory); - if(listing && rs->size>0) { - int t = GET_TOP_INDEX(rs->maxKey); - int slotMin, slotSize, slotCount, l; - unsigned items=0; - do { - if(rs->topDigits[t]) { - printf("\n Slot %u (size/min/max): %u %u %u",t, rs->_slotDescriptors[t]->size, rs->_slotDescriptors[t]->min, rs->_slotDescriptors[t]->max); - if(rs->_slotDescriptors[t]->size>0) { - l=GET_LOW_INDEX(rs->_slotDescriptors[t]->max); - slotMin=GET_LOW_INDEX(rs->_slotDescriptors[t]->min); - slotSize=rs->_slotDescriptors[t]->size; - slotCount=0; - do { - if(rs->topDigits[t][l]) { - printf("\n > %d #%u",l, ++items); - ++slotCount; - RadixItem *ri=rs->topDigits[t][l]->next; - while(ri) { - printf(" #%u",++items); - ++slotCount; - ri=ri->next; - } - } - } while(--l>=slotMin && slotCount SKIPPED"); - } - } - } while(--t>=0); - } - fflush(stdout); + printf("\n Radixsort (size/max/limit/slot count): %u %u %u %u", rs->size, rs->maxKey, rs->keyLimit, rs->_slotsCount); + unsigned memory=rs->_topIndexLimit * sizeof(RadixItem ***); + memory+=memory; + memory+=rs->_slotsCount*(RADIX_SLOT_SIZE * sizeof(RadixItem *)); + printf("\n Memory: %u\n", memory); + if(listing && rs->size>0) { + int t = GET_TOP_INDEX(rs->maxKey); + int slotMin, slotSize, slotCount, l; + unsigned items=0; + do { + if(rs->topDigits[t]) { + printf("\n Slot %u (size/min/max): %u %u %u",t, rs->_slotDescriptors[t]->size, rs->_slotDescriptors[t]->min, rs->_slotDescriptors[t]->max); + if(rs->_slotDescriptors[t]->size>0) { + l=GET_LOW_INDEX(rs->_slotDescriptors[t]->max); + slotMin=GET_LOW_INDEX(rs->_slotDescriptors[t]->min); + slotSize=rs->_slotDescriptors[t]->size; + slotCount=0; + do { + if(rs->topDigits[t][l]) { + printf("\n > %d #%u",l, ++items); + ++slotCount; + RadixItem *ri=rs->topDigits[t][l]->next; + while(ri) { + printf(" #%u",++items); + ++slotCount; + ri=ri->next; + } + } + } while(--l>=slotMin && slotCount SKIPPED"); + } + } + } while(--t>=0); + } + fflush(stdout); } void radixsort_destroy(RadixSorter *rs) { - // radix items: DONE (passed on dump() by reference) - // rs: DONE (created and destroyed by caller) - // slots: - int topIndex = GET_TOP_INDEX(rs->maxKey); - do { - if(rs->topDigits[topIndex]) { - free(rs->topDigits[topIndex]); - free(rs->_slotDescriptors[topIndex]); - } - } while(--topIndex>=0); + // radix items: DONE (passed on dump() by reference) + // rs: DONE (created and destroyed by caller) + // slots: + int topIndex = GET_TOP_INDEX(rs->maxKey); + do { + if(rs->topDigits[topIndex]) { + free(rs->topDigits[topIndex]); + free(rs->_slotDescriptors[topIndex]); + } + } while(--topIndex>=0); } diff --git a/tests/src/test_args.c b/tests/src/test_args.c index 69896206..eb422d63 100644 --- a/tests/src/test_args.c +++ b/tests/src/test_args.c @@ -14,25 +14,25 @@ int main(int argc, char *argv[]) { - printf("%d",strchr("a\nb",10)); + printf("%d",strchr("a\nb",10)); - if(argc>0) { - int i; - char line[LINELNG]; - line[0]=0; - for(i=0; iLINELNG) break; - printf("%d %s\n", i, argv[i]); - if(strstr(argv[i], " ")) { - strcat(line, "\""); - } - strcat(line, argv[i]); - if(strstr(argv[i], " ")) { - strcat(line, "\""); - } - strcat(line, " "); - } + if(argc>0) { + int i; + char line[LINELNG]; + line[0]=0; + for(i=0; iLINELNG) break; + printf("%d %s\n", i, argv[i]); + if(strstr(argv[i], " ")) { + strcat(line, "\""); + } + strcat(line, argv[i]); + if(strstr(argv[i], " ")) { + strcat(line, "\""); + } + strcat(line, " "); + } - printf("#%s#", line); - } + printf("#%s#", line); + } } diff --git a/tests/src/test_curses_keyb.c b/tests/src/test_curses_keyb.c index 39d126a1..ca81aabe 100644 --- a/tests/src/test_curses_keyb.c +++ b/tests/src/test_curses_keyb.c @@ -12,43 +12,43 @@ int main(int argc, char *argv[]) { - initscr(); - noecho(); - keypad(stdscr, TRUE); - - int c; - while(TRUE) { - c = getch(); - - mvprintw(1, 0, "Key number: '%3d' / Char: '%c'", c, c); - - switch(c) { - // ctrl-r, ctrl-h, ctrl-i - case KEY_BACKSPACE: - case KEY_LEFT: - case KEY_RIGHT: - case KEY_UP: - case KEY_DOWN: - mvprintw(5, 0, "CATCHED! %3d",c); - break; - - case KEY_STAB: - case KEY_BTAB: - case KEY_CTAB: - mvprintw(5, 0, "TAB! %3d",c); - break; - - case KEY_RESIZE: - mvprintw(5, 0, "RESIZE! %3d",c); - break; - - case KEY_ENTER: - endwin(); - exit(0); - default: - break; - } - } - - endwin(); + initscr(); + noecho(); + keypad(stdscr, TRUE); + + int c; + while(TRUE) { + c = getch(); + + mvprintw(1, 0, "Key number: '%3d' / Char: '%c'", c, c); + + switch(c) { + // ctrl-r, ctrl-h, ctrl-i + case KEY_BACKSPACE: + case KEY_LEFT: + case KEY_RIGHT: + case KEY_UP: + case KEY_DOWN: + mvprintw(5, 0, "CATCHED! %3d",c); + break; + + case KEY_STAB: + case KEY_BTAB: + case KEY_CTAB: + mvprintw(5, 0, "TAB! %3d",c); + break; + + case KEY_RESIZE: + mvprintw(5, 0, "RESIZE! %3d",c); + break; + + case KEY_ENTER: + endwin(); + exit(0); + default: + break; + } + } + + endwin(); } diff --git a/tests/src/test_escape_chars.c b/tests/src/test_escape_chars.c index e8bdee54..4508348b 100644 --- a/tests/src/test_escape_chars.c +++ b/tests/src/test_escape_chars.c @@ -8,20 +8,20 @@ */ void generate_bash_history_with_special_chars() { - /* - // crash - %s + /* + // crash + %s - // substituation > should not happen: - date -d yesterday +%A + // substituation > should not happen: + date -d yesterday +%A date -d yesterday +0X1.4C41E000008P-895 // crash mocp -Q "<%state> '%file'\n" - */ + */ } void main() { - generate_bash_history_with_special_chars(); + generate_bash_history_with_special_chars(); } diff --git a/tests/src/test_favorites.c b/tests/src/test_favorites.c index 56657036..e67433a9 100644 --- a/tests/src/test_favorites.c +++ b/tests/src/test_favorites.c @@ -11,11 +11,11 @@ int main(int argc, char *argv[]) { - FavoriteItems favoriteItems; + FavoriteItems favoriteItems; - favorites_init(&favoriteItems); - favorites_get(&favoriteItems); - favorites_remove(&favoriteItems, "c"); + favorites_init(&favoriteItems); + favorites_get(&favoriteItems); + favorites_remove(&favoriteItems, "c"); - favorites_choose(&favoriteItems, "ccc"); + favorites_choose(&favoriteItems, "ccc"); } diff --git a/tests/src/test_getopt.c b/tests/src/test_getopt.c index 4b4a32e8..c421017d 100644 --- a/tests/src/test_getopt.c +++ b/tests/src/test_getopt.c @@ -13,75 +13,75 @@ int main(int argc, char **argv) { - int c; - int digit_optind = 0; - - while (1) { - int this_option_optind = optind ? optind : 1; - int option_index = 0; - static struct option long_options[] = { - {"add", required_argument, 0, 0 }, - {"append", no_argument, 0, 0 }, - {"delete", required_argument, 0, 0 }, - {"verbose", no_argument, 0, 0 }, - {"create", required_argument, 0, 'c'}, - {"file", required_argument, 0, 0 }, - {0, 0, 0, 0 } - }; - - c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); - if (c == -1) - break; - - - switch (c) { - case 0: - printf("option %s", long_options[option_index].name); - if (optarg) - printf(" with arg %s", optarg); - printf("\n"); - break; - - case '0': - case '1': - case '2': - if (digit_optind != 0 && digit_optind != this_option_optind) - printf("digits occur in two different argv-elements.\n"); - digit_optind = this_option_optind; - printf("option %c\n", c); - break; - - case 'a': - printf("option a\n"); - break; - - case 'b': - printf("option b\n"); - break; - - case 'c': - printf("option c with value '%s'\n", optarg); - break; - - case 'd': - printf("option d with value '%s'\n", optarg); - break; - - case '?': - - break; - - default: - printf("?? getopt returned character code 0%o ??\n", c); - } - } - - if (optind < argc) { - printf("non-option ARGV-elements: "); - while (optind < argc) - printf("%s ", argv[optind++]); - printf("\n"); - } - - exit(EXIT_SUCCESS); + int c; + int digit_optind = 0; + + while (1) { + int this_option_optind = optind ? optind : 1; + int option_index = 0; + static struct option long_options[] = { + {"add", required_argument, 0, 0 }, + {"append", no_argument, 0, 0 }, + {"delete", required_argument, 0, 0 }, + {"verbose", no_argument, 0, 0 }, + {"create", required_argument, 0, 'c'}, + {"file", required_argument, 0, 0 }, + {0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "abc:d:012", long_options, &option_index); + if (c == -1) + break; + + + switch (c) { + case 0: + printf("option %s", long_options[option_index].name); + if (optarg) + printf(" with arg %s", optarg); + printf("\n"); + break; + + case '0': + case '1': + case '2': + if (digit_optind != 0 && digit_optind != this_option_optind) + printf("digits occur in two different argv-elements.\n"); + digit_optind = this_option_optind; + printf("option %c\n", c); + break; + + case 'a': + printf("option a\n"); + break; + + case 'b': + printf("option b\n"); + break; + + case 'c': + printf("option c with value '%s'\n", optarg); + break; + + case 'd': + printf("option d with value '%s'\n", optarg); + break; + + case '?': + + break; + + default: + printf("?? getopt returned character code 0%o ??\n", c); + } + } + + if (optind < argc) { + printf("non-option ARGV-elements: "); + while (optind < argc) + printf("%s ", argv[optind++]); + printf("\n"); + } + + exit(EXIT_SUCCESS); } diff --git a/tests/src/test_hashset.c b/tests/src/test_hashset.c index ec42edd3..7291acce 100644 --- a/tests/src/test_hashset.c +++ b/tests/src/test_hashset.c @@ -11,36 +11,36 @@ #include "../../src/include/hstr_utils.h" void testBlacklist() { - const char* commandBlacklist[] = { "a","b","c","d","e" }; - HashSet blacklist; - int i; - hashset_init(&blacklist); - for (i = 0; i < 5; i++) { - hashset_add(&blacklist, commandBlacklist[i]); - } - for (i = 0; i < 5; i++) { - printf("match %d\n", hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i]))); - } + const char* commandBlacklist[] = { "a","b","c","d","e" }; + HashSet blacklist; + int i; + hashset_init(&blacklist); + for (i = 0; i < 5; i++) { + hashset_add(&blacklist, commandBlacklist[i]); + } + for (i = 0; i < 5; i++) { + printf("match %d\n", hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i]))); + } } void testGetKeys() { - const char* commandBlacklist[] = { "a","b","c","d","e" }; - HashSet blacklist; - int i; - hashset_init(&blacklist); - for (i = 0; i < 5; i++) { - hashset_add(&blacklist, commandBlacklist[i]); - } + const char* commandBlacklist[] = { "a","b","c","d","e" }; + HashSet blacklist; + int i; + hashset_init(&blacklist); + for (i = 0; i < 5; i++) { + hashset_add(&blacklist, commandBlacklist[i]); + } - char **keys=hashset_keys(&blacklist); - if(keys) { - for(i=0; i void testLog() { - const int HISTORY_SIZE=2000; - int i; - for(i=0; i>i) & 0x01]; - } - mvprintw(10, 0, "bits: %s", binary); - free(binary); - - mvprintw(11, 0, "high bit: %d %d ", 1<<7, 1<<7 & c); - - char cc=pattern[0]; - i=0; - int myStrlen=0; - char isHighBitSet=0; - while(cc) { - print_char_bits(12, 9*i-8, pattern[i++]); - cc=pattern[i]; - - if(1<<7 & pattern[i]) { - if(isHighBitSet) { - isHighBitSet=0; - myStrlen++; - } else { - isHighBitSet=1; - } - } else { - myStrlen++; - } - } - - mvprintw(14, 0, "mystrlen(): %d ", myStrlen); - } + // TODO implement getch with counter; getch result analysis (trip); append analysis; ... + + initscr(); + keypad(stdscr, TRUE); + noecho(); + start_color(); + use_default_colors(); + + char pattern[512]; + int c; + + pattern[0]=0; + while (1) { + c = wgetch(stdscr); + strcat(pattern, (char*)(&c)); + mvprintw(2, 0, "Pattern '%s'", pattern); + mvprintw(3, 0, "Char '%d'", c); + + mvprintw(6, 0, "strlen() '%d'", strlen(pattern)); + mvprintw(7, 0, "mbstowcs() '%d'", mbstowcs(NULL,pattern,0)); + + int i; + int intSizeInBits = sizeof(int) * 8; + char symbol[2] = {'0','1'}; + char * binary = (char *)malloc(intSizeInBits + 1); + memset(binary, 0, intSizeInBits + 1); + for (i=0; i< intSizeInBits; i++) { + binary[intSizeInBits-i-1] = symbol[(c>>i) & 0x01]; + } + mvprintw(10, 0, "bits: %s", binary); + free(binary); + + mvprintw(11, 0, "high bit: %d %d ", 1<<7, 1<<7 & c); + + char cc=pattern[0]; + i=0; + int myStrlen=0; + char isHighBitSet=0; + while(cc) { + print_char_bits(12, 9*i-8, pattern[i++]); + cc=pattern[i]; + + if(1<<7 & pattern[i]) { + if(isHighBitSet) { + isHighBitSet=0; + myStrlen++; + } else { + isHighBitSet=1; + } + } else { + myStrlen++; + } + } + + mvprintw(14, 0, "mystrlen(): %d ", myStrlen); + } clear(); refresh(); @@ -190,7 +190,7 @@ void getch_with_counter_curses() { } void done() { - printf("\n\n"); + printf("\n\n"); } int main(int argc, char *argv[]) @@ -203,6 +203,6 @@ int main(int argc, char *argv[]) //get_string_length(); //loop_string(); - getch_with_counter_curses(); - done(); + getch_with_counter_curses(); + done(); }