/
main.c
385 lines (348 loc) · 8.71 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// main.c
// artemis
//
// Copyright 2013 Greg Berenfield
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#define MAX_LINE 128
struct match {
int index;
char *sloc;
};
char infile[128],outfile[128]; // input and output filenames
char current[MAX_LINE]; // The current text being typed for search
int current_length; // string-length of the current type text
int num_tags=0; // The file length of the tags file (# of tags)
int row,col,cursor_row,cursor_col,hit_row,hit_col;
bool showing; // Are we displaying the hunt_list ?
bool diags; // show diagnostics?
WINDOW *mywin; // curses window
char **tags; // set of tags to search
struct match *matches; // set of matched tags from current search
int sel_match; // currently selected match
int num_matches=0; // number of matches
char items[MAX_LINE*2]; // all choices, space-delimited
int exiting=0; // are we just escaping out?
int num_splits(char s[])
{
char *f;
int i=0;
f = strtok(s," ");
while (f != NULL) {
++i;
f = strtok(NULL," ");
}
return i;
}
int flen(char fname[])
{
FILE *inFile;
int lineCount,inputError;
inFile = fopen(fname, "r");
char word[MAX_LINE];
lineCount = 0;
while(inputError != EOF) {
inputError = fscanf(inFile, "%s\n", word);
lineCount++;
}
fclose(inFile);
return (lineCount-1);
}
void get_file()
{
FILE *inFile;
char str[MAX_LINE];
size_t i;
inFile = fopen(infile, "r");
tags = malloc(sizeof(*tags) * num_tags);
for(i = 0; i < num_tags; i++)
{
fscanf(inFile, "%s", str);
tags[i]=(char *)malloc(sizeof(char *) * (strlen(str)+1));
strcpy(tags[i],str);
}
fclose(inFile);
}
void put_file()
{
FILE *File;
File = fopen(outfile, "w");
fprintf(File, "%s\n", items);
fclose(File);
}
void setup_screen() /* print the message at the center of the screen */
{
char mesg[]="?: "; /* message to be appeared onn the screen */
mywin=initscr();
clear();
idcok(mywin, 1);
idlok(mywin, 1);
mvprintw(LINES - 2, 0, "!: %s", items);
getmaxyx(stdscr,row,col);/* get the number of rows and columns */
cursor_row = row/5;
cursor_col = (col-strlen(mesg))/4;
mvprintw(cursor_row,cursor_col,"%s",mesg);
}
void clear_matches()
{
size_t i;
sel_match = -1;
num_matches = 0;
for (i=0;i<num_matches;i++) {
matches[i].index=-1;
matches[i].sloc=NULL;
}
}
void hunt_current()
{
size_t i,j=0;
char str[MAX_LINE];
char *k;
clear_matches();
for (i=0;i<num_tags;i++) {
if ((k=strcasestr(tags[i],current)) != NULL) {
matches[j].index=i;
matches[j].sloc=k;
j++;
num_matches++;
}
}
}
void print_hit_highlight_kw(size_t i)
{
// i is the relative row position from the first (0) hit item
char front[128],back[128];
memset(front,'\0',128);
memset(back,'\0',128);
strncpy(front,tags[matches[i].index],matches[i].sloc-tags[matches[i].index]);
strcpy(back,&tags[matches[i].index][strlen(front)+strlen(current)]);
mvprintw((cursor_row+2+i),cursor_col,"%s",front);
attron(A_UNDERLINE);
printw("%s",current);
attroff(A_UNDERLINE);
printw("%s",back);
}
void show_hits()
{
size_t i=0;
clrtobot();
while (i<num_matches) {
print_hit_highlight_kw(i);
++i;
}
showing=TRUE;
mvprintw(LINES - 2, 0, "!: %s", items);
move(cursor_row,cursor_col);
refresh();
}
void backspace()
{
noraw();
move(cursor_row,cursor_col-1);
delch();
raw();
refresh();
getsyx(cursor_row,cursor_col);
}
void tab_hits_down()
{
if (sel_match < (num_matches-1)) {
sel_match++;
if (sel_match==0) {
hit_row=cursor_row+1;
hit_col=cursor_col;
}
else {
move (hit_row,hit_col);
print_hit_highlight_kw(sel_match-1);
}
move (++hit_row,hit_col);
clrtoeol();
attron(A_REVERSE);
printw(tags[matches[sel_match].index]);
attroff(A_REVERSE);
move(cursor_row,cursor_col);
}
}
void tab_hits_up()
{
if (sel_match > 0) {
sel_match--;
move (hit_row,hit_col);
print_hit_highlight_kw(sel_match+1);
move (--hit_row,hit_col);
clrtoeol();
attron(A_REVERSE);
printw(tags[matches[sel_match].index]);
attroff(A_REVERSE);
move(cursor_row,cursor_col);
}
}
void diag(int c)
{
getsyx(cursor_row,cursor_col);
mvprintw(LINES - 9, 0, "row:%d col:%d", cursor_row,cursor_col);
mvprintw(LINES - 8, 0, "# tags: %d", num_tags);
mvprintw(LINES - 7, 0, "# matches: %d", num_matches);
mvprintw(LINES - 6, 0, "sizeof matches: %d", sizeof(matches));
mvprintw(LINES - 5, 0, "current: %s", current);
mvprintw(LINES - 4, 0, "current_len: %d", current_length);
mvprintw(LINES - 3, 0, "c: %d", c);
move(cursor_row,cursor_col);
}
void get_current()
{
int c,lc,i=0;
raw();
noecho();
keypad(stdscr, TRUE);
while ((c=getch()))
{
if (c == '\t') // Tab
{
if (showing) tab_hits_down();
else {
clrtobot();
if (current_length > 1) {
hunt_current();
show_hits();
tab_hits_down();
}
}
}
else if (c == '\n' && lc == '\n') break; // two-returns to quit
else if (c == 27) { exiting=1;break;break; } // ESC quit
else if (c == 'J' || c == 258) // J | d-arrow down hit list
tab_hits_down();
else if (c == 'K' || c == 353 || c == 259) // K | <shift-TAB> | u-arrow up hit list
tab_hits_up();
else if (c == 32) // SPACE to reset search
{
memset(current,'\0',MAX_LINE);
current_length = 0;
clear_matches();
setup_screen();
mvprintw(LINES - 2, 0, "!: %s", items);
move(cursor_row,cursor_col+3);
refresh();
i=0;
}
else if (c == 127 || c == 8) // DELETE or BACKSPACE
{
if (current_length > 0 )
{
i--;
current_length--;
current[i] = '\0';
clrtobot();
mvprintw(LINES - 2, 0, "!: %s", items);
backspace();
move(cursor_row,cursor_col);
getsyx(cursor_row,cursor_col);
if (showing && current_length > 1) {
hunt_current();
show_hits();
tab_hits_down();
}
}
}
else if (c=='\n') // SELECT hightlighted hit or typed word
{
if (showing && sel_match>-1) strcat(items,tags[matches[sel_match].index]);
else strcat(items,current);
memset(current,'\0',MAX_LINE);
current_length=i=0;
setup_screen();
showing=FALSE;
strcat(items," ");
mvprintw(LINES - 2, 0, "!: %s", items);
move(cursor_row,cursor_col+3);
refresh();
}
else // regular key-typed
{
addch(c);
current[i]=c;
++i;
++current_length;
refresh();
getsyx(cursor_row,cursor_col);
if (showing && (current_length > 1)) {
noraw();
delch();
raw();
refresh();
hunt_current();
show_hits();
tab_hits_down();
}
else showing=FALSE;
}
lc = c;
if (diags) diag(c);
}
current[i] = '\0';
}
void show_basics()
{
printf("artemis - (C) 2013 Greg Berenfield\nReleased under the GNU GPL.\n\nUsage: artemis [-o <output>] [-f <word-file> | \"string of space-separated words\"]\n");
}
int main(int argc, const char * argv[])
{
size_t i;
if (argc==1) // no arguments
{
show_basics();
return 0;
}
for (i = 1; i < argc; i++) // parse arguments
{
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'f':strcpy(infile,argv[++i]); break;
case 'o':strcpy(outfile,argv[++i]); break;
case 'd':diags=TRUE; break;
}
}
}
if (strlen(infile)>0) // read file for tags
{
num_tags=flen(infile);
get_file();
}
else // read string from stdin for tags
{
i=0;
char *s,*t,*nt;
t = strdup(argv[argc-1]); // string of tags, space-delimted
nt = strdup(argv[argc-1]);
num_tags = num_splits(nt);
tags = malloc(sizeof(*tags) * num_tags);
s=strtok(t," "); // ashen
while (s != NULL)
{
tags[i] = strdup(s);
s = strtok (NULL, " ");
++i;
}
num_tags=i;
}
matches = malloc(num_tags * sizeof(struct match));
setup_screen();
get_current();
items[strlen(items)-1]='\0'; // remove trailing space-separation char
endwin();
if (!exiting) {
if (strlen(outfile)>0) put_file();
else printf("%s\n",items);
}
// be kind and free up allocations
for(i = 0; i < num_tags; i++) free(tags[i]);
// free tags
free(tags);
free(matches);
return 0;
}