forked from enferex/treetop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
967 lines (826 loc) · 26.3 KB
/
main.c
File metadata and controls
967 lines (826 loc) · 26.3 KB
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
/******************************************************************************
* main.c
*
* treetop - A 'top' like text/log file monitor.
*
* Copyright (C) 2013, Matt Davis (enferex)
*
* This file is part of treetop.
* treetop 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 2 of the License, or
* (at your option) any later version.
*
* treetop 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 treetop. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#define _WITH_GETLINE
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <curses.h>
#include <errno.h>
#include <menu.h>
#include <panel.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_EVENT_H
#include <sys/event.h>
#elif defined(HAVE_EPOLL_CREATE)
#include <sys/epoll.h>
#endif /* !HAVE_SYS_EVENT_H */
/* Output routines */
#define _PR(_tag, _outfd, ...) \
do { \
fprintf(_outfd, "[logtop]"_tag __VA_ARGS__); \
fputc('\n', _outfd); \
} while(0)
#define PR(...) _PR(" ", stdout, __VA_ARGS__)
#define DBG(...) _PR("[debug] ", stdout, __VA_ARGS__)
#define WR(...) _PR("[warning] ", stderr, __VA_ARGS__)
#define ER(...) \
do { \
_PR("[error] ", stderr, __VA_ARGS__); \
exit(-1); \
} while (0)
/* Comment character for config file (anything after this char is ignored) */
#define COMMENT_CHAR '#'
/* If the file has the 'UPDATED' state */
#define UPDATED_CHAR "*"
/* Default delay (seconds) */
#define DEFAULT_TIMEOUT_SECS 10
/* Max */
#define MAX(_a, _b) (((_a)>(_b)) ? (_a) : (_b))
/* Window dimensions */
#define INNER_WIN_LINES (LINES-3)
#define INNER_WIN_COLS (COLS-2)
#define SHOW_DETAILS 0x1
#define HIDE_DETAILS 0x2
#define TITLE "}-= TreeTop =-{"
/* File state */
typedef enum _state_e
{
UNCHANGED,
UPDATED
} state_e;
/* Stores the last X window columns number */
static int columns = 0;
/* Mutex to protect post_menu calls */
static pthread_mutex_t mtx_post_menu;
/* Mutex to protect doupdate calls */
static pthread_mutex_t mtx_update_panels;
/* Pipe to send "commands" from the getch loop to the reading thread */
static int fildes[2];
/* I always forget these */
#define PIPE_READ 0
#define PIPE_WRITE 1
/* File information */
typedef struct _data_t
{
int fd;
FILE *fp;
const char *full_path;
const char *base_name;
char *line; /* pointer to the last line in buff */
char *buff; /* file content */
struct _data_t *next;
state_e state;
ITEM *item; /* Curses menu item for this file */
time_t last_mod;
} data_t;
/* Screen (ncurses state and content) */
typedef struct _screen_t
{
WINDOW *master; /* Nothing here it just needs a border to look pretty */
WINDOW *content; /* Menu goes here */
WINDOW *details; /* Display details about selected item */
PANEL *master_panel;
PANEL *content_panel;
PANEL *details_panel;
MENU *menu;
ITEM **items;
data_t *datas;
} screen_t;
/* Param to a thread */
typedef struct _thread_param_t
{
int opened_files;
screen_t *master_screen;
} thread_param_t;
static void get_last_line(data_t *d);
const data_t *show_details;
static void usage(const char *execname, const char *msg)
{
if (msg)
PR("%s", msg);
printf("Usage: %s <config> [-d secs] [-h]\n"
" -h: Display this help screen\n"
" -d secs: Auto-update display every 'secs' seconds\n", execname);
exit(0);
}
/* Returns the starting x-coordinate such that when displaying a value of
* 'length' characters long, it will be centered in the given window.
*/
static int find_center_start(const WINDOW *win, size_t length)
{
int max_x = getmaxx(win);
int half = length / 2;
return max_x / 2 - half;
}
/* Writes the program title into the window */
static void write_title_window(WINDOW *master)
{
int x;
box(master, 0, 0);
x = find_center_start(master, strlen(TITLE));
mvwprintw(master, 0, x, TITLE);
}
static void read_files(int bytes, int opened_files, data_t *data) {
char c;
int j;
char *last, *tmp;
data_t *d;
for (d = data; d; d = d->next)
{
if (d->state == UPDATED) {
if (d->buff == NULL) {
if ((d->buff = malloc(sizeof(char) * bytes)) == NULL) {
ER("Can't allocate memory for file buffer");
}
}
else if (bytes * sizeof(char) != sizeof(d->buff)) {
if ((tmp = realloc(d->buff, sizeof(char) * bytes)) == NULL) {
ER("Can't reallocate memory for file buffer");
}
d->buff = tmp;
}
memset(d->buff, '\0', sizeof(char) * bytes);
/* Set the file-read pointer */
if (fseek(d->fp, -bytes, SEEK_END) == -1)
fseek(d->fp, 0, SEEK_SET);
j=0;
last = d->buff;
while ((c = fgetc(d->fp)) != EOF)
{
d->buff[j] = c;
if (j > 0 && d->buff[j-1] == '\n') {
last = d->buff + j;
}
j++;
}
d->buff[j] = '\0';
d->line = last;
}
}
}
/* returns the writable bytes of the s WINDOW */
static int getMaxBytes(WINDOW *s, int *maxx, int *maxy) {
getmaxyx(s, *maxy, *maxx);
*maxy -= 2; /* Ignore border */
*maxx -= 2; /* Ignore border */
return *maxx * *maxy;
}
static void refresh_menus(screen_t *screen)
{
pthread_mutex_lock(&mtx_post_menu);
unpost_menu(screen->menu);
post_menu(screen->menu);
pthread_mutex_unlock(&mtx_post_menu);
}
static void update_panels_safe()
{
pthread_mutex_lock(&mtx_update_panels);
update_panels();
doupdate();
pthread_mutex_unlock(&mtx_update_panels);
}
static void menu_driver_update(screen_t *screen, int c)
{
data_t *d;
if (c >= 0)
{
menu_driver(screen->menu, c);
}
for (d=screen->datas; d; d=d->next)
{
if (d->state == UPDATED)
{
d->item->description.str = d->line;
}
}
refresh_menus(screen);
for (d=screen->datas; d; d=d->next)
{
if (d->state == UPDATED)
{
if (d != ((data_t *)(item_userptr(current_item(screen->menu)))))
{
mvwprintw(screen->content, item_index(d->item), 3, UPDATED_CHAR);
}
else {
d->state = UNCHANGED;
}
}
}
if (c > 0)
{
update_panels_safe();
}
}
static void *thread_read_files(void *args)
{
char c, cmd;
int i, nfds, opened_files, maxx, maxy;
ssize_t r;
#ifdef HAVE_KQUEUE
int kq;
struct kevent *ev;
#elif defined(HAVE_EPOLL_CREATE)
int epollfd;
struct epoll_event *ev, event;
#endif /* !HAVE_KQUEUE */
#ifndef HAVE_KQUEUE
struct stat stats;
#endif
screen_t *screen;
data_t *data, *d;//, *show_details = NULL;
opened_files = ((thread_param_t *)args)->opened_files;
screen = ((thread_param_t *)args)->master_screen;
data = screen->datas;
free(args);
#ifdef HAVE_KQUEUE
kq = kqueue();
if (kq < 0)
{
ER("Can't initialize kqueue");
}
#elif defined(HAVE_EPOLL_CREATE)
epollfd = epoll_create1(0);
if (epoll_create1 < 0)
{
ER("Can't initialize epoll: %s", strerror(errno));
}
event.events = EPOLLIN;
#endif /* !HAVE_KQUEUE */
#ifdef HAVE_KQUEUE
ev = (struct kevent *) malloc(sizeof(struct kevent) * opened_files + 2);
if (ev == NULL)
{
ER("Can't allocate memory for kevents");
}
#elif defined(HAVE_EPOLL_CREATE)
//ev = (struct epoll_event *) malloc(sizeof(struct epoll_event) * opened_files + 1); /* allocate two extra events to get the notifications from the event loop */
ev = (struct epoll_event *) malloc(sizeof(struct epoll_event) * 1); /* allocate two extra events to get the notifications from the event loop */
if (ev == NULL)
{
ER("Can't allocate memory for epoll_events");
}
#endif /* !HAVE_KQUEUE */
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL_CREATE)
#ifdef HAVE_KQUEUE
i = 0;
for (d=screen->datas; d; d=d->next)
{
EV_SET(&ev[i], d->fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_DELETE | NOTE_RENAME, 0, 0); /* Detect removal and renamming of the file */
EV_SET(&ev[i], d->fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); /* Detect new data */
i++;
}
EV_SET(&ev[i++], SIGWINCH, EVFILT_SIGNAL, EV_ADD | EV_ENABLE, 0, 0, 0);
EV_SET(&ev[i++], fildes[PIPE_READ], EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
if (kevent(kq, ev, opened_files + 2, NULL, 0, NULL) < 0) {
ER("Can't set kevent");
}
#elif defined(HAVE_EPOLL_CREATE)
event.data.fd = fildes[PIPE_READ];
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fildes[PIPE_READ], &event) == -1) {
ER("Can't add file descriptor in epoll instance: %s", strerror(errno));
}
#endif
#endif /* defined(HAVE_KQUEUE) || defined(HAVE_EPOLL_CREATE) */
for (;;) {
read_files (getMaxBytes(screen->details, &maxx, &maxy), opened_files, data);
if (show_details == NULL) {
menu_driver_update(screen, -1);
hide_panel(screen->details_panel);
}
else {
wmove(screen->details, 1, 1);
i = 0;
while ((c = show_details->buff[i++]) != '\0') {
if (getcurx(screen->details) == maxx)
{
waddch(screen->details, ' ');
waddch(screen->details, ' ');
waddch(screen->details, ' ');
}
else if (getcurx(screen->details) == 0)
waddch(screen->details, ' ');
waddch(screen->details, c);
}
/* Display file name and draw border */
box(screen->details, 0, 0);
mvwprintw(screen->details, 0, 1, "[%s]", show_details->base_name);
show_panel(screen->details_panel);
}
update_panels_safe();
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL_CREATE)
#ifdef HAVE_KQUEUE
nfds = kevent(kq, NULL, 0, ev, 1, NULL);
#elif defined(HAVE_EPOLL_CREATE)
nfds = epoll_wait(epollfd, ev, 1, 0);
#endif
if (nfds < 0)
{
#ifdef HAVE_KQUEUE
DBG("Error retrieving kevent, we might have been interrupted");
#elif defined(HAVE_EPOLL_CREATE)
DBG("Error retrieving epoll events, we might have been interrupted");
#endif
}
for (i = 0; i < nfds; i++)
{
#ifdef HAVE_KQUEUE
if (ev[i].filter == EVFILT_SIGNAL &&
ev[i].ident == SIGWINCH) {
/* This ensure that LINES and COLS are correctly updated */
doupdate();
mvwprintw(screen->master, 1, columns-1, " ");
columns = COLS;
if (wresize(screen->master, LINES, COLS) == ERR)
WR("Error resizing master windows");
if (wresize(screen->content,
INNER_WIN_LINES, INNER_WIN_COLS) == ERR)
WR("Error resizing content windows");
if (wresize(screen->details,
INNER_WIN_LINES, INNER_WIN_COLS) == ERR)
WR("Error resizing details windows");
/* Redraw the title and clean up the border */
write_title_window(screen->master);
refresh_menus(screen);
update_panels_safe();
}
else if (ev[i].filter == EVFILT_SIGNAL &&
ev[i].ident == SIGUSR1) {
/* handle displaying details here */
show_details = item_userptr(current_item(screen->menu));
}
else if (ev[i].filter == EVFILT_SIGNAL &&
ev[i].ident == SIGUSR2) {
/* handle displaying details here */
show_details = NULL;
}
else if (ev[i].filter == EVFILT_READ) {
if (fildes[PIPE_READ] == ev[i].ident)
#elif defined(HAVE_EPOLL_CREATE)
if (fildes[PIPE_READ] == ev[i].data.fd)
#endif
{
if ((r = read(fildes[PIPE_READ], &cmd, sizeof(cmd))) == -1)
{
WR("reading command from pipe returned an error: %s", strerror(errno));
}
else if (r == 0)
{
WR("end-of-file detected from the command pipe, this is abnormal !");
}
else
{
switch(cmd)
{
case SHOW_DETAILS:
show_details = item_userptr(current_item(screen->menu));
break;
case HIDE_DETAILS:
show_details = NULL;
break;
default:
/* Dunno what to do here ? */
break;
}
}
}
#ifdef HAVE_KQUEUE
}
else
{
for (d=screen->datas; d; d=d->next) {
if(d->fd == ev[i].ident) {
d->state = UPDATED;
break;
}
}
}
#endif
}
#endif
#ifndef HAVE_KQUEUE
/* If the files have been updated, grab the last line from the file */
for (d=data; d; d=d->next)
{
if (stat(d->full_path, &stats) == -1)
ER("Could not obtain file stats for: '%s'", d->base_name);
/* If the file has been modified since last check, update */
if (stats.st_mtime != d->last_mod)
{
d->last_mod = stats.st_mtime;
d->state = UPDATED;
}
}
usleep(25000);
#endif /* !HAVE_KQUEUE */
}
#ifdef HAVE_KQUEUE
free(ev);
#elif defined(HAVE_EPOLL_CREATE)
free(ev);
#endif
return (void *) NULL;
}
/* Update display */
static void screen_create_menu(screen_t *screen)
{
int i = 0;
data_t *d;
char line[COLS];
const char *def = "Updating...";
/* Count number of data items */
for (d=screen->datas; d; d=d->next)
++i;
/* Allocate a long line for the description (make it all spaces) */
memset(line, ' ', sizeof(line) - 1);
line[sizeof(line)] = '\0';
memcpy(line, def, strlen(def));
/* Allocate and create menu items (one per data item */
screen->items = (ITEM **)calloc(i+1, sizeof(ITEM *));
for (i=0, d=screen->datas; d; d=d->next, ++i)
{
screen->items[i] = new_item(d->base_name, line);
screen->items[i]->description.length = sizeof(line);
set_item_userptr(screen->items[i], (void *)d);
d->item = screen->items[i];
}
screen->menu = new_menu(screen->items);
set_menu_mark(screen->menu, "--> ");
set_menu_win(screen->menu, screen->content);
post_menu(screen->menu);
}
/* Initialize curses */
static screen_t *screen_create(data_t *datas, int timeout_ms)
{
screen_t *screen;
initscr();
cbreak();
noecho();
curs_set(0); /* Turn cursor off */
timeout(-1);
keypad(stdscr, TRUE);
screen = calloc(1, sizeof(screen_t));
screen->datas = datas;
/* Create the windows */
screen->master = newwin(LINES, COLS, 0, 0);
screen->content = newwin(INNER_WIN_LINES, INNER_WIN_COLS, 2, 1);
screen->details = newwin(INNER_WIN_LINES, INNER_WIN_COLS, 2, 1);
scrollok(screen->details, TRUE);
/* Decorate the master window */
write_title_window(screen->master);
/* Put the windows in panels (easier to refresh things) */
screen->master_panel = new_panel(screen->master);
screen->details_panel = new_panel(screen->details);
screen->content_panel = new_panel(screen->content);
screen_create_menu(screen);
return screen;
}
/* Cleanup from curses */
static void screen_destroy(screen_t *screen)
{
nocbreak();
echo();
endwin();
free(screen);
}
/* wrapper function for thread creation */
static pthread_t *threads_init(int opened_files, screen_t *screen) {
thread_param_t *params;
pthread_t *thread;
/* Init the mutex which protects "post_menu" calls */
if (pthread_mutex_init(&mtx_post_menu, NULL) < 0)
{
ER("Can't initiate post_menu mutex");
}
if (pthread_mutex_init(&mtx_update_panels, NULL) < 0)
{
ER("Can't initiate post_menu mutex");
}
thread = (pthread_t *) malloc(sizeof(pthread_t));
if (thread == NULL) {
ER("Can't allocate memory for reading threads");
}
params = (thread_param_t *) malloc(sizeof(thread_param_t));
if (params == NULL) {
ER("Can't allocate memory for thread params");
}
params->opened_files = opened_files;
params->master_screen = screen;
pthread_create(thread, NULL, thread_read_files, (void *)params);
return (thread);
}
/* Create our file information */
static data_t *data_init(const char *fname, int *nb_of_opened_files)
{
FILE *fp, *entry_fp;
data_t *head, *tmp;
char *c, *line;
size_t sz;
ssize_t ret;
#define CONTINUE {free(line); line=NULL; continue;}
if (!(fp = fopen(fname, "r")))
ER("Could not open config file '%s'", fname);
/* For each line in config */
head = NULL;
line = NULL;
while ((ret = getline(&line, &sz, fp)) != -1)
{
/* Skip whitespace */
c = line;
while (*c && isspace(*c))
++c;
if (*c == COMMENT_CHAR)
CONTINUE;
/* Trim line */
if (strchr(c, COMMENT_CHAR))
*(strchr(c, COMMENT_CHAR)) = '\0';
if (strchr(c, '\n'))
*(strchr(c, '\n')) = '\0';
if (strchr(c, ' '))
*(strchr(c, ' ')) = '\0';
if (strlen(c) == 0)
CONTINUE;
if (!(entry_fp = fopen(c, "r")))
{
WR("Could not open file: '%s'", c);
CONTINUE;
}
/* Add to list */
DBG("Monitoring file: '%s'...", c);
tmp = head;
head = calloc(1, sizeof(data_t));
head->fp = entry_fp;
head->fd = fileno(entry_fp);
head->full_path = strdup(c);
head->base_name = strdup(basename((char *)head->full_path));
head->state = UPDATED; /* Force first update to process this */
head->buff = NULL;
head->next = tmp;
free(line);
line = NULL;
(*nb_of_opened_files)++;
if (fcntl(head->fd, F_SETFL, fcntl(head->fd, F_GETFL) & ~O_NONBLOCK ) == -1)
ER("Can't set blocking to file %s", head->base_name);
}
return head;
}
static void threads_destroy(pthread_t *thread) {
pthread_cancel(*thread);
pthread_join(*thread, (void **) NULL);
free(thread);
}
/* Cleanup */
static void data_destroy(data_t *datas)
{
data_t *curr, *d = datas;
while (d)
{
curr = d;
fclose(d->fp);
free(d->buff);
d = curr->next;
free(curr);
}
}
/* Set the last line for this file */
static void get_last_line(data_t *d)
{
ssize_t idx, n_bytes;
char line[1024] = {0};
/* Read in last 1024 bytes */
if (fseek(d->fp, -sizeof(line), SEEK_END) == -1)
fseek(d->fp, 0, SEEK_SET);
n_bytes = fread(line, 1, sizeof(line)-1, d->fp);
idx = n_bytes - 1;
/* If file ends with newline */
while (idx > 0 && (line[idx] == '\n' || line[idx] == '\r'))
--idx;
/* Now find the next newline */
while (idx > 0 && (line[idx] != '\n' && line[idx] != '\r'))
--idx;
if (line[idx] == '\n' && idx+1 < n_bytes)
++idx;
/* Copy starting from the last line */
if (idx >= 0)
strncpy(d->line, line+idx, sizeof(d->line) - 1);
}
/* Update data */
static void data_update(data_t *datas)
{
data_t *d;
struct stat stats;
/* If the files have been updated, grab the last line from the file */
for (d=datas; d; d=d->next)
{
if (stat(d->full_path, &stats) == -1)
ER("Could not obtain file stats for: '%s'", d->base_name);
/* If the file has been modified since last check, update */
if (stats.st_mtime != d->last_mod)
{
get_last_line(d);
d->last_mod = stats.st_mtime;
d->state = UPDATED;
}
}
}
/* Update the details screen to display info about the selected item */
static void update_details(screen_t *screen, const data_t *selected)
{
int c, maxx, maxy, bytes;
/* Clear and get the size of the details window */
wclear(screen->details);
getmaxyx(screen->details, maxy, maxx);
/* Draw last n bytes of file: (figure out how much room we have) */
maxy -= 2; /* Ignore border */
maxx -= 2; /* Ignore border */
bytes = maxx * maxy;
/* Set the file-read pointer */
if (fseek(selected->fp, -bytes, SEEK_END) == -1)
fseek(selected->fp, 0, SEEK_SET);
/* Read in file contents */
wmove(screen->details, 1, 1);
while ((c = fgetc(selected->fp)) != EOF)
{
/* Add whitespace if the cursor is on a border */
if (getcurx(screen->details) == maxx)
{
waddch(screen->details, ' ');
waddch(screen->details, ' ');
waddch(screen->details, ' ');
}
else if (getcurx(screen->details) == 0)
waddch(screen->details, ' ');
waddch(screen->details, c);
}
/* Display file name and draw border */
box(screen->details, 0, 0);
mvwprintw(screen->details, 0, 1, "[%s]", selected->base_name);
}
/* Update display
* 'show_details' is the selected item, if no item is slected this val is NULL
*/
static void screen_update(screen_t *screen, const data_t *show_details)
{
data_t *d;
/* Check for updated data */
for (d=screen->datas; d; d=d->next)
if (d->state == UPDATED)
d->item->description.str = d->line;
/* Refresh menu */
unpost_menu(screen->menu);
post_menu(screen->menu);
/* Now draw a character signifying which file just changed */
for (d=screen->datas; d; d=d->next)
{
if (d->state == UPDATED)
{
mvwprintw(screen->content, item_index(d->item), 3, UPDATED_CHAR);
d->state = UNCHANGED;
}
}
/* Update display */
if (show_details)
{
update_details(screen, show_details);
show_panel(screen->details_panel);
}
else
hide_panel(screen->details_panel);
update_panels_safe();
}
/* Capture user input (keys) and timeout to periodically referesh */
static void process(screen_t *screen)
{
char cmd;
int c;
/* Force initial drawing */
show_details = NULL;
while ((c = getch()) != 'Q' && c != 'q')
{
cmd = 0;
switch (c)
{
case KEY_UP:
case 'k':
menu_driver_update(screen, REQ_UP_ITEM);
break;
case KEY_DOWN:
case 'j':
menu_driver_update(screen, REQ_DOWN_ITEM);
break;
case KEY_ENTER:
case '\n':
case 'l':
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL_CREATE)
cmd = SHOW_DETAILS;
#else /* !HAVE_KQUEUE */
show_details = item_userptr(current_item(screen->menu));
#endif /* HAVE_KQUEUE */
break;
/* If no key was registered, or on some wacky
* input we don't care about don't modify the screen state.
*/
case ERR:
break;
/* Someother key was pressed, exit details window */
default:
#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL_CREATE)
cmd = HIDE_DETAILS;
#else /* !HAVE_KQUEUE */
show_details = NULL;
#endif
}
if (cmd != 0)
{
/* wakes up the kevent/epoll routine */
write(fildes[PIPE_WRITE], &cmd, sizeof(cmd));
}
}
}
int main(int argc, char **argv)
{
int i, timeout_secs, opened_files = 0;
screen_t *screen;
data_t *datas;
pthread_t *thread;
const char *fname;
struct sigaction action;
/* Args */
fname = 0;
timeout_secs = DEFAULT_TIMEOUT_SECS;
for (i=1; i<argc; ++i)
{
if (strncmp(argv[i], "-d", strlen("-d")) == 0)
{
if (i+1 < argc)
timeout_secs = atoi(argv[++i]);
else
usage(argv[0], "Incorrect timeout value specified");
}
else if (strncmp(argv[i], "-h", strlen("-h")) == 0)
usage(argv[0], NULL);
else if (argv[i][0] != '-')
fname = argv[i];
else
usage(argv[0], "Invalid argument specified");
}
/* Sanity check args */
if (!fname)
usage(argv[0], "Please provide a configuration file");
if (timeout_secs < 0)
usage(argv[0], "Incorrect timeout value specified");
DBG("Using config: %s", fname);
DBG("Using timeout: %d seconds", timeout_secs);
/* Initializing pipe */
if (pipe(fildes) == -1) {
ER("Can't create pipe: %s", strerror(errno));
}
/* Disabling SIGUSR1 */
action.sa_handler = SIG_IGN;
sigaction(SIGUSR1, &action, NULL);
sigaction(SIGUSR2, &action, NULL);
/* Load data */
datas = data_init(fname, &opened_files);
/* Initialize display */
screen = screen_create(datas, timeout_secs * 1000);
/* Initialize columns variable */
columns = COLS;
/* Create reading threads */
thread = threads_init(opened_files, screen);
/* Do the work */
process(screen);
/* Cleanup */
threads_destroy(thread);
data_destroy(screen->datas);
screen_destroy(screen);
return 0;
}