| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,182 +0,0 @@ |
| 1 |
-#include <stdio.h> |
|
| 2 |
-#include <string.h> |
|
| 3 |
-#include <stdlib.h> |
|
| 4 |
-#include <termios.h> |
|
| 5 |
-#include <unistd.h> |
|
| 6 |
-#include <sys/stat.h> |
|
| 7 |
-#include <fcntl.h> |
|
| 8 |
-#include <ctype.h> |
|
| 9 |
- |
|
| 10 |
-double match(const char *needle, const char *haystack){
|
|
| 11 |
- while(*needle){
|
|
| 12 |
- if(!*haystack) |
|
| 13 |
- return 0.0; |
|
| 14 |
- while(tolower(*needle) == tolower(*haystack++)) |
|
| 15 |
- needle++; |
|
| 16 |
- } |
|
| 17 |
- return 1.0; |
|
| 18 |
-} |
|
| 19 |
- |
|
| 20 |
-#define INITIAL_CAPACITY 1 |
|
| 21 |
-int choices_capacity = 0; |
|
| 22 |
-int choices_n = 0; |
|
| 23 |
-const char **choices = NULL; |
|
| 24 |
- |
|
| 25 |
-void resize_choices(int new_capacity){
|
|
| 26 |
- choices = realloc(choices, new_capacity * sizeof(const char *)); |
|
| 27 |
- int i = choices_capacity; |
|
| 28 |
- for(; i < new_capacity; i++){
|
|
| 29 |
- choices[i] = NULL; |
|
| 30 |
- } |
|
| 31 |
- choices_capacity = new_capacity; |
|
| 32 |
-} |
|
| 33 |
- |
|
| 34 |
-void add_choice(const char *choice){
|
|
| 35 |
- if(choices_n == choices_capacity){
|
|
| 36 |
- resize_choices(choices_capacity * 2); |
|
| 37 |
- } |
|
| 38 |
- choices[choices_n++] = choice; |
|
| 39 |
-} |
|
| 40 |
- |
|
| 41 |
-void read_choices(){
|
|
| 42 |
- char *line = NULL; |
|
| 43 |
- size_t len = 0; |
|
| 44 |
- ssize_t read; |
|
| 45 |
- |
|
| 46 |
- while ((read = getline(&line, &len, stdin)) != -1) {
|
|
| 47 |
- char *nl; |
|
| 48 |
- if((nl = strchr(line, '\n'))) |
|
| 49 |
- *nl = '\0'; |
|
| 50 |
- |
|
| 51 |
- add_choice(line); |
|
| 52 |
- |
|
| 53 |
- line = NULL; |
|
| 54 |
- } |
|
| 55 |
- free(line); |
|
| 56 |
-} |
|
| 57 |
- |
|
| 58 |
-void run_search(char *needle){
|
|
| 59 |
- int i; |
|
| 60 |
- for(i = 0; i < choices_n; i++){
|
|
| 61 |
- double rank = match(needle, choices[i]); |
|
| 62 |
- if(rank > 0){
|
|
| 63 |
- printf("%s\n", choices[i]);
|
|
| 64 |
- } |
|
| 65 |
- } |
|
| 66 |
-} |
|
| 67 |
- |
|
| 68 |
-int ttyin; |
|
| 69 |
-FILE *ttyout; |
|
| 70 |
-struct termios original_termios; |
|
| 71 |
- |
|
| 72 |
-void reset_tty(){
|
|
| 73 |
- tcsetattr(ttyin, TCSANOW, &original_termios); |
|
| 74 |
-} |
|
| 75 |
- |
|
| 76 |
-void init_tty(){
|
|
| 77 |
- ttyin = open("/dev/tty", O_RDONLY);
|
|
| 78 |
- ttyout = fopen("/dev/tty", "w");
|
|
| 79 |
- |
|
| 80 |
- tcgetattr(ttyin, &original_termios); |
|
| 81 |
- |
|
| 82 |
- struct termios new_termios = original_termios; |
|
| 83 |
- |
|
| 84 |
- new_termios.c_lflag &= ~(ICANON | ECHO); |
|
| 85 |
- |
|
| 86 |
- tcsetattr(ttyin, TCSANOW, &new_termios); |
|
| 87 |
-} |
|
| 88 |
- |
|
| 89 |
-char ttygetchar(){
|
|
| 90 |
- char ch; |
|
| 91 |
- int size = read(ttyin, &ch, 1); |
|
| 92 |
- if(size < 0){
|
|
| 93 |
- perror("error reading from tty");
|
|
| 94 |
- exit(EXIT_FAILURE); |
|
| 95 |
- }else if(size == 0){
|
|
| 96 |
- /* EOF */ |
|
| 97 |
- exit(EXIT_FAILURE); |
|
| 98 |
- }else{
|
|
| 99 |
- return ch; |
|
| 100 |
- } |
|
| 101 |
-} |
|
| 102 |
- |
|
| 103 |
-int search_size; |
|
| 104 |
-char search[4096] = {0};
|
|
| 105 |
- |
|
| 106 |
-void clear(){
|
|
| 107 |
- fprintf(ttyout, "%c%c0G", 0x1b, '['); |
|
| 108 |
- int line = 0; |
|
| 109 |
- while(line++ < 10 + 1){
|
|
| 110 |
- fprintf(ttyout, "%c%cK\n", 0x1b, '['); |
|
| 111 |
- } |
|
| 112 |
- fprintf(ttyout, "%c%c%iA", 0x1b, '[', line-1); |
|
| 113 |
- fprintf(ttyout, "%c%c0G", 0x1b, '['); |
|
| 114 |
-} |
|
| 115 |
- |
|
| 116 |
-void draw(){
|
|
| 117 |
- int line = 0; |
|
| 118 |
- int i; |
|
| 119 |
- const char *prompt = "> "; |
|
| 120 |
- clear(); |
|
| 121 |
- fprintf(ttyout, "%s%s\n", prompt, search); |
|
| 122 |
- for(i = 0; line < 10 && i < choices_n; i++){
|
|
| 123 |
- double rank = match(search, choices[i]); |
|
| 124 |
- if(rank > 0){
|
|
| 125 |
- fprintf(ttyout, "%s\n", choices[i]); |
|
| 126 |
- line++; |
|
| 127 |
- } |
|
| 128 |
- } |
|
| 129 |
- fprintf(ttyout, "%c%c%iA", 0x1b, '[', line + 1); |
|
| 130 |
- fprintf(ttyout, "%c%c%iG", 0x1b, '[', strlen(prompt) + strlen(search) + 1); |
|
| 131 |
- fflush(ttyout); |
|
| 132 |
-} |
|
| 133 |
- |
|
| 134 |
-void run(){
|
|
| 135 |
- draw(); |
|
| 136 |
- char ch; |
|
| 137 |
- do {
|
|
| 138 |
- ch = ttygetchar(); |
|
| 139 |
- if(isprint(ch)){
|
|
| 140 |
- /* FIXME: overflow */ |
|
| 141 |
- search[search_size++] = ch; |
|
| 142 |
- search[search_size] = '\0'; |
|
| 143 |
- draw(); |
|
| 144 |
- }else if(ch == 127){ /* DEL */
|
|
| 145 |
- if(search_size) |
|
| 146 |
- search[--search_size] = '\0'; |
|
| 147 |
- draw(); |
|
| 148 |
- }else if(ch == 23){ /* C-W */
|
|
| 149 |
- if(search_size) |
|
| 150 |
- search[--search_size] = '\0'; |
|
| 151 |
- while(search_size && !isspace(search[--search_size])) |
|
| 152 |
- search[search_size] = '\0'; |
|
| 153 |
- draw(); |
|
| 154 |
- }else if(ch == 10){ /* Enter */
|
|
| 155 |
- clear(); |
|
| 156 |
- exit(0); |
|
| 157 |
- }else{
|
|
| 158 |
- printf("'%c' (%i)\n", ch, ch);
|
|
| 159 |
- } |
|
| 160 |
- }while(ch != 'q'); |
|
| 161 |
-} |
|
| 162 |
- |
|
| 163 |
-void usage(const char *argv0){
|
|
| 164 |
- fprintf(stderr, "USAGE: %s\n", argv0); |
|
| 165 |
- exit(EXIT_FAILURE); |
|
| 166 |
-} |
|
| 167 |
- |
|
| 168 |
-int main(int argc, char *argv[]){
|
|
| 169 |
- if(argc != 1){
|
|
| 170 |
- usage(argv[0]); |
|
| 171 |
- } |
|
| 172 |
- atexit(reset_tty); |
|
| 173 |
- init_tty(reset_tty); |
|
| 174 |
- |
|
| 175 |
- resize_choices(INITIAL_CAPACITY); |
|
| 176 |
- read_choices(); |
|
| 177 |
- |
|
| 178 |
- clear(); |
|
| 179 |
- run(); |
|
| 180 |
- |
|
| 181 |
- return 0; |
|
| 182 |
-} |
| ... | ... |
@@ -66,7 +66,7 @@ void run_search(char *needle){
|
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 | 68 |
int ttyin; |
| 69 |
-int ttyout; |
|
| 69 |
+FILE *ttyout; |
|
| 70 | 70 |
struct termios original_termios; |
| 71 | 71 |
|
| 72 | 72 |
void reset_tty(){
|
| ... | ... |
@@ -75,7 +75,7 @@ void reset_tty(){
|
| 75 | 75 |
|
| 76 | 76 |
void init_tty(){
|
| 77 | 77 |
ttyin = open("/dev/tty", O_RDONLY);
|
| 78 |
- ttyout = open("/dev/tty", O_WRONLY);
|
|
| 78 |
+ ttyout = fopen("/dev/tty", "w");
|
|
| 79 | 79 |
|
| 80 | 80 |
tcgetattr(ttyin, &original_termios); |
| 81 | 81 |
|
| ... | ... |
@@ -104,13 +104,13 @@ int search_size; |
| 104 | 104 |
char search[4096] = {0};
|
| 105 | 105 |
|
| 106 | 106 |
void clear(){
|
| 107 |
- fprintf(stdout, "%c%c0G", 0x1b, '['); |
|
| 107 |
+ fprintf(ttyout, "%c%c0G", 0x1b, '['); |
|
| 108 | 108 |
int line = 0; |
| 109 |
- while(line++ < 11 + 1){
|
|
| 110 |
- fprintf(stdout, "%c%cK\n", 0x1b, '['); |
|
| 109 |
+ while(line++ < 10 + 1){
|
|
| 110 |
+ fprintf(ttyout, "%c%cK\n", 0x1b, '['); |
|
| 111 | 111 |
} |
| 112 |
- fprintf(stdout, "%c%c%iA", 0x1b, '[', line-1); |
|
| 113 |
- fprintf(stdout, "%c%c0G", 0x1b, '['); |
|
| 112 |
+ fprintf(ttyout, "%c%c%iA", 0x1b, '[', line-1); |
|
| 113 |
+ fprintf(ttyout, "%c%c0G", 0x1b, '['); |
|
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 | 116 |
void draw(){
|
| ... | ... |
@@ -118,18 +118,17 @@ void draw(){
|
| 118 | 118 |
int i; |
| 119 | 119 |
const char *prompt = "> "; |
| 120 | 120 |
clear(); |
| 121 |
- printf("%s%s\n", prompt, search);
|
|
| 121 |
+ fprintf(ttyout, "%s%s\n", prompt, search); |
|
| 122 | 122 |
for(i = 0; line < 10 && i < choices_n; i++){
|
| 123 | 123 |
double rank = match(search, choices[i]); |
| 124 | 124 |
if(rank > 0){
|
| 125 |
- //fprintf(stdout, "%c%cK", 0x1b, '['); |
|
| 126 |
- printf("%s\n", choices[i]);
|
|
| 125 |
+ fprintf(ttyout, "%s\n", choices[i]); |
|
| 127 | 126 |
line++; |
| 128 | 127 |
} |
| 129 | 128 |
} |
| 130 |
- fprintf(stdout, "%c%c%iA", 0x1b, '[', line + 1); |
|
| 131 |
- fprintf(stdout, "%c%c%iG", 0x1b, '[', strlen(prompt) + strlen(search) + 1); |
|
| 132 |
- fflush(stdout); |
|
| 129 |
+ fprintf(ttyout, "%c%c%iA", 0x1b, '[', line + 1); |
|
| 130 |
+ fprintf(ttyout, "%c%c%iG", 0x1b, '[', strlen(prompt) + strlen(search) + 1); |
|
| 131 |
+ fflush(ttyout); |
|
| 133 | 132 |
} |
| 134 | 133 |
|
| 135 | 134 |
void run(){
|
| ... | ... |
@@ -103,17 +103,40 @@ char ttygetchar(){
|
| 103 | 103 |
int search_size; |
| 104 | 104 |
char search[4096] = {0};
|
| 105 | 105 |
|
| 106 |
+void clear(){
|
|
| 107 |
+ fprintf(stdout, "%c%c0G", 0x1b, '['); |
|
| 108 |
+ int line = 0; |
|
| 109 |
+ while(line++ < 11 + 1){
|
|
| 110 |
+ fprintf(stdout, "%c%cK\n", 0x1b, '['); |
|
| 111 |
+ } |
|
| 112 |
+ fprintf(stdout, "%c%c%iA", 0x1b, '[', line-1); |
|
| 113 |
+ fprintf(stdout, "%c%c0G", 0x1b, '['); |
|
| 114 |
+} |
|
| 115 |
+ |
|
| 106 | 116 |
void draw(){
|
| 107 |
- run_search(search); |
|
| 108 |
- printf("> %s", search);
|
|
| 117 |
+ int line = 0; |
|
| 118 |
+ int i; |
|
| 119 |
+ const char *prompt = "> "; |
|
| 120 |
+ clear(); |
|
| 121 |
+ printf("%s%s\n", prompt, search);
|
|
| 122 |
+ for(i = 0; line < 10 && i < choices_n; i++){
|
|
| 123 |
+ double rank = match(search, choices[i]); |
|
| 124 |
+ if(rank > 0){
|
|
| 125 |
+ //fprintf(stdout, "%c%cK", 0x1b, '['); |
|
| 126 |
+ printf("%s\n", choices[i]);
|
|
| 127 |
+ line++; |
|
| 128 |
+ } |
|
| 129 |
+ } |
|
| 130 |
+ fprintf(stdout, "%c%c%iA", 0x1b, '[', line + 1); |
|
| 131 |
+ fprintf(stdout, "%c%c%iG", 0x1b, '[', strlen(prompt) + strlen(search) + 1); |
|
| 109 | 132 |
fflush(stdout); |
| 110 | 133 |
} |
| 111 | 134 |
|
| 112 | 135 |
void run(){
|
| 136 |
+ draw(); |
|
| 113 | 137 |
char ch; |
| 114 | 138 |
do {
|
| 115 | 139 |
ch = ttygetchar(); |
| 116 |
- printf("\n");
|
|
| 117 | 140 |
if(isprint(ch)){
|
| 118 | 141 |
/* FIXME: overflow */ |
| 119 | 142 |
search[search_size++] = ch; |
| ... | ... |
@@ -129,6 +152,9 @@ void run(){
|
| 129 | 152 |
while(search_size && !isspace(search[--search_size])) |
| 130 | 153 |
search[search_size] = '\0'; |
| 131 | 154 |
draw(); |
| 155 |
+ }else if(ch == 10){ /* Enter */
|
|
| 156 |
+ clear(); |
|
| 157 |
+ exit(0); |
|
| 132 | 158 |
}else{
|
| 133 | 159 |
printf("'%c' (%i)\n", ch, ch);
|
| 134 | 160 |
} |
| ... | ... |
@@ -136,12 +162,12 @@ void run(){
|
| 136 | 162 |
} |
| 137 | 163 |
|
| 138 | 164 |
void usage(const char *argv0){
|
| 139 |
- fprintf(stderr, "USAGE: %s SEARCH\n", argv0); |
|
| 165 |
+ fprintf(stderr, "USAGE: %s\n", argv0); |
|
| 140 | 166 |
exit(EXIT_FAILURE); |
| 141 | 167 |
} |
| 142 | 168 |
|
| 143 | 169 |
int main(int argc, char *argv[]){
|
| 144 |
- if(argc != 2){
|
|
| 170 |
+ if(argc != 1){
|
|
| 145 | 171 |
usage(argv[0]); |
| 146 | 172 |
} |
| 147 | 173 |
atexit(reset_tty); |
| ... | ... |
@@ -149,8 +175,8 @@ int main(int argc, char *argv[]){
|
| 149 | 175 |
|
| 150 | 176 |
resize_choices(INITIAL_CAPACITY); |
| 151 | 177 |
read_choices(); |
| 152 |
- run_search(argv[1]); |
|
| 153 | 178 |
|
| 179 |
+ clear(); |
|
| 154 | 180 |
run(); |
| 155 | 181 |
|
| 156 | 182 |
return 0; |
| ... | ... |
@@ -1,15 +1,20 @@ |
| 1 | 1 |
#include <stdio.h> |
| 2 | 2 |
#include <string.h> |
| 3 | 3 |
#include <stdlib.h> |
| 4 |
+#include <termios.h> |
|
| 5 |
+#include <unistd.h> |
|
| 6 |
+#include <sys/stat.h> |
|
| 7 |
+#include <fcntl.h> |
|
| 8 |
+#include <ctype.h> |
|
| 4 | 9 |
|
| 5 | 10 |
double match(const char *needle, const char *haystack){
|
| 6 | 11 |
while(*needle){
|
| 7 |
- while(*needle == *haystack++) |
|
| 8 |
- needle++; |
|
| 9 | 12 |
if(!*haystack) |
| 10 |
- return 1; |
|
| 13 |
+ return 0.0; |
|
| 14 |
+ while(tolower(*needle) == tolower(*haystack++)) |
|
| 15 |
+ needle++; |
|
| 11 | 16 |
} |
| 12 |
- return 0; |
|
| 17 |
+ return 1.0; |
|
| 13 | 18 |
} |
| 14 | 19 |
|
| 15 | 20 |
#define INITIAL_CAPACITY 1 |
| ... | ... |
@@ -54,13 +59,99 @@ void run_search(char *needle){
|
| 54 | 59 |
int i; |
| 55 | 60 |
for(i = 0; i < choices_n; i++){
|
| 56 | 61 |
double rank = match(needle, choices[i]); |
| 57 |
- printf("'%s' =~ '%s'\t%f\n", needle, choices[i], rank);
|
|
| 62 |
+ if(rank > 0){
|
|
| 63 |
+ printf("%s\n", choices[i]);
|
|
| 64 |
+ } |
|
| 65 |
+ } |
|
| 66 |
+} |
|
| 67 |
+ |
|
| 68 |
+int ttyin; |
|
| 69 |
+int ttyout; |
|
| 70 |
+struct termios original_termios; |
|
| 71 |
+ |
|
| 72 |
+void reset_tty(){
|
|
| 73 |
+ tcsetattr(ttyin, TCSANOW, &original_termios); |
|
| 74 |
+} |
|
| 75 |
+ |
|
| 76 |
+void init_tty(){
|
|
| 77 |
+ ttyin = open("/dev/tty", O_RDONLY);
|
|
| 78 |
+ ttyout = open("/dev/tty", O_WRONLY);
|
|
| 79 |
+ |
|
| 80 |
+ tcgetattr(ttyin, &original_termios); |
|
| 81 |
+ |
|
| 82 |
+ struct termios new_termios = original_termios; |
|
| 83 |
+ |
|
| 84 |
+ new_termios.c_lflag &= ~(ICANON | ECHO); |
|
| 85 |
+ |
|
| 86 |
+ tcsetattr(ttyin, TCSANOW, &new_termios); |
|
| 87 |
+} |
|
| 88 |
+ |
|
| 89 |
+char ttygetchar(){
|
|
| 90 |
+ char ch; |
|
| 91 |
+ int size = read(ttyin, &ch, 1); |
|
| 92 |
+ if(size < 0){
|
|
| 93 |
+ perror("error reading from tty");
|
|
| 94 |
+ exit(EXIT_FAILURE); |
|
| 95 |
+ }else if(size == 0){
|
|
| 96 |
+ /* EOF */ |
|
| 97 |
+ exit(EXIT_FAILURE); |
|
| 98 |
+ }else{
|
|
| 99 |
+ return ch; |
|
| 58 | 100 |
} |
| 59 | 101 |
} |
| 60 | 102 |
|
| 103 |
+int search_size; |
|
| 104 |
+char search[4096] = {0};
|
|
| 105 |
+ |
|
| 106 |
+void draw(){
|
|
| 107 |
+ run_search(search); |
|
| 108 |
+ printf("> %s", search);
|
|
| 109 |
+ fflush(stdout); |
|
| 110 |
+} |
|
| 111 |
+ |
|
| 112 |
+void run(){
|
|
| 113 |
+ char ch; |
|
| 114 |
+ do {
|
|
| 115 |
+ ch = ttygetchar(); |
|
| 116 |
+ printf("\n");
|
|
| 117 |
+ if(isprint(ch)){
|
|
| 118 |
+ /* FIXME: overflow */ |
|
| 119 |
+ search[search_size++] = ch; |
|
| 120 |
+ search[search_size] = '\0'; |
|
| 121 |
+ draw(); |
|
| 122 |
+ }else if(ch == 127){ /* DEL */
|
|
| 123 |
+ if(search_size) |
|
| 124 |
+ search[--search_size] = '\0'; |
|
| 125 |
+ draw(); |
|
| 126 |
+ }else if(ch == 23){ /* C-W */
|
|
| 127 |
+ if(search_size) |
|
| 128 |
+ search[--search_size] = '\0'; |
|
| 129 |
+ while(search_size && !isspace(search[--search_size])) |
|
| 130 |
+ search[search_size] = '\0'; |
|
| 131 |
+ draw(); |
|
| 132 |
+ }else{
|
|
| 133 |
+ printf("'%c' (%i)\n", ch, ch);
|
|
| 134 |
+ } |
|
| 135 |
+ }while(ch != 'q'); |
|
| 136 |
+} |
|
| 137 |
+ |
|
| 138 |
+void usage(const char *argv0){
|
|
| 139 |
+ fprintf(stderr, "USAGE: %s SEARCH\n", argv0); |
|
| 140 |
+ exit(EXIT_FAILURE); |
|
| 141 |
+} |
|
| 142 |
+ |
|
| 61 | 143 |
int main(int argc, char *argv[]){
|
| 144 |
+ if(argc != 2){
|
|
| 145 |
+ usage(argv[0]); |
|
| 146 |
+ } |
|
| 147 |
+ atexit(reset_tty); |
|
| 148 |
+ init_tty(reset_tty); |
|
| 149 |
+ |
|
| 62 | 150 |
resize_choices(INITIAL_CAPACITY); |
| 63 | 151 |
read_choices(); |
| 64 | 152 |
run_search(argv[1]); |
| 153 |
+ |
|
| 154 |
+ run(); |
|
| 155 |
+ |
|
| 65 | 156 |
return 0; |
| 66 | 157 |
} |
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,66 @@ |
| 1 |
+#include <stdio.h> |
|
| 2 |
+#include <string.h> |
|
| 3 |
+#include <stdlib.h> |
|
| 4 |
+ |
|
| 5 |
+double match(const char *needle, const char *haystack){
|
|
| 6 |
+ while(*needle){
|
|
| 7 |
+ while(*needle == *haystack++) |
|
| 8 |
+ needle++; |
|
| 9 |
+ if(!*haystack) |
|
| 10 |
+ return 1; |
|
| 11 |
+ } |
|
| 12 |
+ return 0; |
|
| 13 |
+} |
|
| 14 |
+ |
|
| 15 |
+#define INITIAL_CAPACITY 1 |
|
| 16 |
+int choices_capacity = 0; |
|
| 17 |
+int choices_n = 0; |
|
| 18 |
+const char **choices = NULL; |
|
| 19 |
+ |
|
| 20 |
+void resize_choices(int new_capacity){
|
|
| 21 |
+ choices = realloc(choices, new_capacity * sizeof(const char *)); |
|
| 22 |
+ int i = choices_capacity; |
|
| 23 |
+ for(; i < new_capacity; i++){
|
|
| 24 |
+ choices[i] = NULL; |
|
| 25 |
+ } |
|
| 26 |
+ choices_capacity = new_capacity; |
|
| 27 |
+} |
|
| 28 |
+ |
|
| 29 |
+void add_choice(const char *choice){
|
|
| 30 |
+ if(choices_n == choices_capacity){
|
|
| 31 |
+ resize_choices(choices_capacity * 2); |
|
| 32 |
+ } |
|
| 33 |
+ choices[choices_n++] = choice; |
|
| 34 |
+} |
|
| 35 |
+ |
|
| 36 |
+void read_choices(){
|
|
| 37 |
+ char *line = NULL; |
|
| 38 |
+ size_t len = 0; |
|
| 39 |
+ ssize_t read; |
|
| 40 |
+ |
|
| 41 |
+ while ((read = getline(&line, &len, stdin)) != -1) {
|
|
| 42 |
+ char *nl; |
|
| 43 |
+ if((nl = strchr(line, '\n'))) |
|
| 44 |
+ *nl = '\0'; |
|
| 45 |
+ |
|
| 46 |
+ add_choice(line); |
|
| 47 |
+ |
|
| 48 |
+ line = NULL; |
|
| 49 |
+ } |
|
| 50 |
+ free(line); |
|
| 51 |
+} |
|
| 52 |
+ |
|
| 53 |
+void run_search(char *needle){
|
|
| 54 |
+ int i; |
|
| 55 |
+ for(i = 0; i < choices_n; i++){
|
|
| 56 |
+ double rank = match(needle, choices[i]); |
|
| 57 |
+ printf("'%s' =~ '%s'\t%f\n", needle, choices[i], rank);
|
|
| 58 |
+ } |
|
| 59 |
+} |
|
| 60 |
+ |
|
| 61 |
+int main(int argc, char *argv[]){
|
|
| 62 |
+ resize_choices(INITIAL_CAPACITY); |
|
| 63 |
+ read_choices(); |
|
| 64 |
+ run_search(argv[1]); |
|
| 65 |
+ return 0; |
|
| 66 |
+} |