-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwag.c
526 lines (480 loc) · 15.4 KB
/
wag.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
wag file tracker
Copyright 2016 nateohlson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "wag.h"
#include <ncurses.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
//TODO clean up and describe globals
WINDOW *top; /* top 'search' window */
WINDOW *bottom; /* bottom 'main' window */
bool dualPane; /* is the second window visible */
int parent_y, parent_x; /*size of the terminal window itself */
char searchBuffer[1000]; /* buffer for the users search query */
char searchQuery[1000]; /* user's search query */
char searchResult[1000];
FILE * readfd; /*file descriptor for log file */
char eventBuffer[EVENT_BUF_LEN]; /* event buffer for inotify */
int inFd; /* file descriptor for inotify */
char filename[100]; /* log filename */
int formatCode;
int wd; /* watch descriptor */
char formats[NUM_FORMAT_OPTIONS][50] = {"1. None", "2. {NAME}YYYY-MM-DD.{FILEEXT}", "3. MM-DD-YYY.{FILEEXT}"};
void track(void) {
char c;
int length;
int i;
int j;
char info[40];
struct stat sb;
while (1) {
memset(eventBuffer, 0, EVENT_BUF_LEN);
length = read( inFd, eventBuffer, EVENT_BUF_LEN );
j = 0;
while (j < length) {
struct inotify_event *event = (struct inotify_event *)&eventBuffer[j];
if( event->mask & IN_MODIFY ) {
if (stat(filename, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
i = 0;
char tempTrackBuf[500];
while ((c = fgetc(readfd)) != EOF) {
tempTrackBuf[i] = c;
i++;
}
tempTrackBuf[i] = '\0';
updateLogBuffer(tempTrackBuf);
} else if ( event->mask & IN_MOVE_SELF) {
sprintf(info, "File has moved. Attempting to rotate log...\n");
updateLogBuffer(info);
sleep(1); //wait for rotation to occur
if ((wd = swapFilename(inFd, wd, filename, filename)) < 0) {
perror("filename not found");
exit(1);
} else {
sprintf(info, "Success.\n");
updateLogBuffer(info);
}
}
if ( length < 0 ) {
perror( "read" );
}
j += EVENT_SIZE + event->len;
}
}
}
void print_last_lines(FILE * fd, int n) {
int eolcount = 0;
int printcount = 0;
char c;
fseek(fd, -2, SEEK_END); //go to last byte of file
char lastLineBuffer[LOG_BUFFER_SIZE];
memset(lastLineBuffer, 0, sizeof(lastLineBuffer));
int atStart = 0;
while (eolcount < n && !atStart) {
while ((c = fgetc(fd)) != '\n' && !atStart) {
if (fseek(fd, -2, SEEK_CUR) != 0) {
atStart = 1;
}
}
if (c == '\n' && !atStart) {
eolcount++;
fseek(fd, -2, SEEK_CUR);
}
}
if (fseek(fd, 1, SEEK_CUR) != 0 && !atStart) {
perror("seek error");
}
if (atStart) {
fseek(fd, 0, SEEK_SET);
} else {
fseek(fd, 1, SEEK_CUR); //skip the last newline character and start from next newline
}
int j = 0;
while (printcount < eolcount) {
while ((c = fgetc(fd)) != EOF) {
lastLineBuffer[j] = c;
j++;
if (c == '\n') {
updateLogBuffer(lastLineBuffer);
memset(lastLineBuffer,0,sizeof(lastLineBuffer));
j = 0;
printcount++;
break;
}
}
}
}
bool updateLogBuffer(char * string) {
int stringLen = strlen(string);
if (logBuffer.writePos + stringLen < logBuffer.size) {
logBuffer.writePos += sprintf(logBuffer.buffer + logBuffer.writePos, "%s", string);
logBuffer.writePos = MOD(logBuffer.writePos, logBuffer.size);
} else {
int i = 0;
int k = logBuffer.writePos;
while (i < stringLen) {
logBuffer.buffer[MOD(k, logBuffer.size)] = string[i];
i++;
k++;
}
logBuffer.writePos = MOD(k, logBuffer.size);
}
if (strstr(string, "\n") != NULL) { //if the new string has a newline, flush butter to screen
fullWinRefresh();
}
return true;
}
bool updateLogBufferC(char c) {
logBuffer.writePos += sprintf(logBuffer.buffer + logBuffer.writePos, "%c", c);
logBuffer.writePos = MOD(logBuffer.writePos, logBuffer.size);
if (c == '\n') {
fullWinRefresh();
}
return true;
}
int searchFile(char * fileName, char * search) {
char line[512];
}
void search(void) {
char cur = 0;
int breakOut = 0;
//maintain the search "state"
//TODO figure out ESC key to escape from searching
while (1) {
int i = 0;
while ((cur = wgetch(top)) != 10) {
if (cur == 127) { //delete char
i--;
searchBuffer[i] = '\0'; //delete the char from the buffer
wmove(top, (parent_y / 2) - 2, 8 + i);
wdelch(top);
wrefresh(top);
} else if (cur != -10 && cur != -102) { //special case for change in screen size during typing. needs to be rethought
searchBuffer[i] = cur;
wmove(top, (parent_y / 2)-2, 8 + i);
wprintw(top, "%c", cur);
wrefresh(top);
i++;
}
}
searchBuffer[i] = '\0';
sprintf(searchQuery, "%s", searchBuffer);
//search file
FILE *fpsearch =fopen(filename,"r");
char tmp[500]={0x0};
int lineNumber = 0;
int found = 0;
int stopSearch = 0;
while (fpsearch != NULL && fgets(tmp, sizeof(tmp), fpsearch) != NULL && !stopSearch) {
lineNumber++;
if (strstr(tmp, searchQuery)) {
found = 1;
sprintf(searchResult, "%s", tmp);
drawSearchWindow();
//printf("line number: %d %s", counter, tmp);
while ((cur = wgetch(top)) != 'n') {
if (cur == 's') {
breakOut = 1;
stopSearch = 1;
break;
}
}
}
}
if (fpsearch != NULL) {
fclose(fpsearch);
}
//print out if search came up empty
if (!found) {
sprintf(searchResult, "Query not found\n");
drawSearchWindow();
}
//after search state, wating for input
if (!breakOut) {
while (1) {
cur = wgetch(top);
if (cur == 127) {
memset(searchBuffer, 0, sizeof(searchBuffer));
memset(searchQuery, 0, sizeof(searchQuery));
drawSearchWindow();
break;
} else if (cur == 's') {
toggleSearchWindow();
breakOut = 1;
break;
}
}
}
if (breakOut) {
break;
}
}
}
void drawSearchWindow(void) {
int half = parent_y/2;
top = newwin(half, parent_x, 0, 0);
scrollok(top, TRUE);
wprintw(top, searchResult);
mvwhline(top, half-1, 0, ACS_HLINE, parent_x);
mvwhline(top, half-3, 0, ACS_HLINE, parent_x);
mvwprintw(top, half-2, 0, "Search: ");
wmove(top, half-2, 8);
wprintw(top, "%s", searchBuffer);
wrefresh(top);
}
void toggleSearchWindow(void){
if (dualPane) {
dualPane = false;
memset(searchResult, 0, sizeof(searchResult));
delwin(top);
refresh();
fullWinRefresh();
refillMain();
} else {
dualPane = true;
//clear search buffer
memset(searchBuffer,0,sizeof(searchBuffer));
drawSearchWindow();
search();
}
}
void drawMainWindow(void) {
wrefresh(bottom);
if (logBuffer.writePos != logBuffer.readPos) {
char drawBuf[logBuffer.size];
memset(drawBuf, 0, sizeof(drawBuf));
int k = 0; int i = logBuffer.readPos;
while ((i % logBuffer.size) != (MOD(logBuffer.writePos, logBuffer.size))) {
drawBuf[k] = logBuffer.buffer[MOD(i, logBuffer.size)];
k++;
i++;
}
wprintw(bottom, drawBuf);
logBuffer.readPos = logBuffer.writePos;
}
wrefresh(bottom);
}
void gracefulExit(void) {
delwin(bottom);
delwin(top);
endwin();
memset(searchBuffer,0,sizeof(searchBuffer));
memset(logBuffer.buffer,0,logBuffer.size);
exit(1);
}
void fullWinRefresh(void) {
drawMainWindow();
if (dualPane) {
drawSearchWindow();
}
}
void refillMain(void) {
int new_y, new_x;
getmaxyx(stdscr, new_y, new_x);
int j = logBuffer.readPos -1;
int newlineCount = 0;
while (newlineCount < new_y && j != logBuffer.readPos && logBuffer.buffer[j] != '\0') {
if (logBuffer.buffer[j] == '\n') {
newlineCount++;
// wprintw(bottom, "%i, %i\n", newlineCount, j);
// wrefresh(bottom);
}
j = MOD((j-1), LOG_BUFFER_SIZE);
}
logBuffer.readPos = MOD(j + 1, logBuffer.size);
werase(bottom);
fullWinRefresh();
}
void winchHandler(int nil) {
//updateLogBuffer("Screen Resize\n");
int new_y, new_x;
endwin();
refresh();
clear();
getmaxyx(stdscr, new_y, new_x);
parent_y = new_y;
parent_x = new_x;
mvwin(bottom, new_y, 0);
wclear(bottom);
refillMain();
}
void intHandler(int nil) {
gracefulExit();
}
void onboarding(void) {
// onboarding window
char onboardChar;
char prompt[] = "File: ";
char invalidFilenameMessage[] = "Invalid filename. Please re-enter...";
char item[50];
int k = 0;
//create onboarding window
WINDOW * onboard = newwin(parent_y, parent_x, 0, 0);
//move cursor to middle of screen to write prompt
//TODO: coordinate positions in the following section need to be captured in variables, too hard to understand
wmove(onboard, parent_y/2, parent_x/2 -6);
wprintw(onboard, prompt);
wrefresh(onboard);
//take input for filename
while (1) {
while ((onboardChar = wgetch(onboard)) != 10) {
if (onboardChar == 127) {
k--;
wmove(onboard, parent_y/2, parent_x/2 + strlen(prompt) + k - 6);
wdelch(onboard);
wrefresh(onboard);
} else if (onboardChar != -10 && onboardChar != -102) { //special case for change in screen size during typing. needs to be rethought
filename[k] = onboardChar;
wmove(onboard, parent_y/2, parent_x/2 + strlen(prompt) + k - 6);
wprintw(onboard, "%c", onboardChar);
wrefresh(onboard);
k++;
}
}
//validate input and filename
if (access(filename, F_OK) == -1) {
//file doesn't exist
memset(filename, 0, sizeof(filename));
k = 0;
wclear(onboard);
wmove(onboard, parent_y/2, parent_x/2 -17);
wprintw(onboard, invalidFilenameMessage);
wrefresh(onboard);
sleep(3);
wclear(onboard);
wmove(onboard, parent_y/2, parent_x/2 -6);
wprintw(onboard, prompt);
wrefresh(onboard);
} else {
filename[k] = '\0';
break;
}
}
wclear(onboard);
/*
keypad(onboard, TRUE);
k = 0;
mvwprintw(onboard, 0, 0, "Please select file name format:");
//find format type
for (int i = 0; i < NUM_FORMAT_OPTIONS; i++) {
if (i==0) {
wattron(onboard, A_STANDOUT);
} else {
wattroff(onboard, A_STANDOUT);
}
sprintf(item, "%-7s", formats[i]);
mvwprintw(onboard, i+1, 2, "%s", item);
}
wrefresh(onboard);
while ((onboardChar = wgetch(onboard)) != 10) {
sprintf(item, "%-7s", formats[k]);
mvwprintw(onboard, k+1, 2, "%s", item);
switch (onboardChar) {
case 3:
k--;
k = (k<0) ? NUM_FORMAT_OPTIONS-1 : k;
break;
case 2:
k++;
k = (k>=NUM_FORMAT_OPTIONS) ? 0: k;
break;
}
wattron(onboard, A_STANDOUT);
sprintf(item, "%-7s", formats[k]);
mvwprintw(onboard, k+1, 2, "%s", item);
wattroff(onboard, A_STANDOUT);
}
mvwprintw(onboard, 20, 20, "%i", k);
formatCode = k;
*/
delwin(onboard);
endwin();
}
int swapFilename(int fd, int wd, char * oldFilename, char * newFilename) {
if (*oldFilename != 0 && fcntl(wd, F_GETFD) != -1) {
if (inotify_rm_watch(fd, wd) == -1) {
perror("inotify rm watch error");
}
}
readfd = fopen(filename, "r");
print_last_lines(readfd, NUM_INIT_LINES);
return inotify_add_watch(fd, newFilename, IN_MODIFY|IN_MOVE_SELF);
}
int main(int argc, char * argv[]) {
dualPane = false; // starts off with one pane
char startupMessages[200];
char choice;
//initialize buffers
memset(searchBuffer, 0, sizeof(searchBuffer));
memset(logBuffer.buffer, 0, sizeof(logBuffer.buffer));
logBuffer.size = LOG_BUFFER_SIZE;
logBuffer.readPos = 0;
logBuffer.writePos = 0;
//ncurses init
initscr();
noecho();
timeout(0);
curs_set(FALSE);
getmaxyx(stdscr, parent_y, parent_x);
//onboard to set filename and get formatting
onboarding();
// new bottom 'main' window
bottom = newwin(parent_y, parent_x, 0, 0);
scrollok(bottom, TRUE);
//signal handlers
signal(SIGWINCH, winchHandler);
signal(SIGINT, intHandler);
/*creating the INOTIFY instance*/
if ((inFd = inotify_init()) < 0 ) {
perror("inotify_init error");
}
//add watch for the filename
wd = inotify_add_watch( inFd, filename, IN_MODIFY|IN_MOVE_SELF);
//open file for reading and print last lines
readfd = fopen(filename, "r");
print_last_lines(readfd, NUM_INIT_LINES);
// //create thread for tracking file
pthread_t trackThread;
pthread_create(&trackThread, NULL, (void * (*) (void *))track, NULL);
//mainloop
// sprintf(startupMessages, "Format code %i selected\n", formatCode);
updateLogBuffer(startupMessages);
// sprintf(startupMessages, "Began tracking file %s\n", filename);
updateLogBuffer(startupMessages);
while (1) {
choice = wgetch(bottom);
if (choice == 's') {
toggleSearchWindow();
} else if (choice == 'q') {
gracefulExit();
}
}
return 0;
}