Browse code

something of a working version

John Hawthorn authored on 11/07/2014 09:45:17
Showing 2 changed files

... ...
@@ -1,5 +1,5 @@
1 1
 LIBS=
2
-CFLAGS+=-Wall -g
2
+CFLAGS+=-Wall -Wextra -g
3 3
 TARGET=hawthfuzz
4 4
 OBJECTS=hawthfuzz.o
5 5
 
... ...
@@ -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
 }