-
Notifications
You must be signed in to change notification settings - Fork 63
/
mt.c
3657 lines (3056 loc) · 82 KB
/
mt.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
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _LARGEFILE64_SOURCE /* required for GLIBC to enable stat64 and friends */
#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
#if !defined(__APPLE__) && !defined(__CYGWIN__)
#include <search.h>
#endif
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <time.h>
#include <glob.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#ifndef AIX
#include <sys/termios.h> /* needed on Solaris 8 */
#endif
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#if defined(__GLIBC__) && ( __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 2))
#include <sys/sysinfo.h>
#endif
/* syslog receive */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef UTF8_SUPPORT
#include <wchar.h>
#include <wctype.h>
#endif
#include "mt.h"
#include "globals.h"
#include "error.h"
#include "mem.h"
#include "utils.h"
#include "scrollback.h"
#include "help.h"
#include "term.h"
#include "history.h"
#include "cv.h"
#include "selbox.h"
#include "stripstring.h"
#include "color.h"
#include "misc.h"
#include "ui.h"
#include "exec.h"
#include "diff.h"
#include "config.h"
#include "cmdline.h"
/* #define KEYB_DEBUG */
void LOG(char *s, ...)
{
#ifdef _DEBUG
va_list ap;
FILE *fh = fopen("log.log", "a+");
if (!fh)
{
endwin();
error_exit(TRUE, TRUE, "error logging\n");
}
va_start(ap, s);
vfprintf(fh, s, ap);
va_end(ap);
fclose(fh);
#endif
}
typedef void (*sh_t)(int);
void set_signal(int sig, sh_t func, char *signame)
{
if (SIG_ERR == signal(sig, func)) error_exit(TRUE, FALSE, "Setting of handler for signal %s failed.\n", signame);
}
void free_subentry(proginfo *entry)
{
int loop;
/* free up filters */
for(loop=0; loop<entry -> n_strip; loop++)
myfree((entry -> pstrip)[loop].del);
myfree(entry -> pstrip);
/* free all those allocated memory blocks */
myfree(entry -> filename);
myfree(entry -> cdef.field_del);
if (entry -> status)
delete_popup(entry -> status);
if (entry -> data)
delete_popup(entry -> data);
/* free buffers for diff (if any) */
if (entry -> restart.diff.bcur)
delete_array(entry -> restart.diff.bcur, entry -> restart.diff.ncur);
if (entry -> restart.diff.bprev)
delete_array(entry -> restart.diff.bprev, entry -> restart.diff.nprev);
/* delete regular expressions */
for(loop=0; loop<entry -> n_re; loop++)
free_re(&(entry -> pre)[loop]);
myfree(entry -> pre);
/* stop process */
stop_process(entry -> pid);
/* close pipe to (tail) process */
myclose(entry -> fd);
if (entry -> wfd != entry -> fd) myclose(entry -> wfd);
}
void buffer_replace_pi_pointers(int f_index, proginfo *org, proginfo *new)
{
int loop;
for(loop=0; loop<lb[f_index].curpos; loop++)
{
if ((lb[f_index].be)[loop].pi == org)
{
if (!new)
{
myfree((lb[f_index].be)[loop].Bline);
(lb[f_index].be)[loop].Bline = NULL;
(lb[f_index].be)[loop].pi = NULL;
}
else
{
(lb[f_index].be)[loop].pi = new;
}
}
}
}
void delete_be_in_buffer(buffer *pb)
{
int loop;
for(loop=0; loop<pb -> curpos; loop++)
myfree((pb -> be)[loop].Bline);
myfree(pb -> be);
pb -> be = NULL;
pb -> curpos = 0;
}
char delete_entry(int f_index, proginfo *sub)
{
char delete_all = 0;
/* no children? then sub must be pointing to current */
if (pi[f_index].next == NULL)
{
sub = NULL;
}
/* stop the process(es) we're watching ('tail' most of the time) */
if (sub == NULL) /* delete all? */
{
proginfo *cur = &pi[f_index];
do
{
free_subentry(cur);
cur = cur -> next;
}
while(cur);
/* free the subwindows (if any) */
cur = pi[f_index].next;
while(cur)
{
proginfo *dummy = cur -> next;
myfree(cur);
cur = dummy;
}
delete_all = 1;
}
else
{
free_subentry(sub);
}
/* remove entry from array */
if (sub == NULL) /* delete entry in the main array */
{
int n_to_move = (nfd - f_index) - 1;
/* free buffers */
delete_be_in_buffer(&lb[f_index]);
if (n_to_move > 0)
{
int loop;
/* update buffer proginfo-pointers */
for(loop=f_index + 1; loop<nfd; loop++)
{
/* replace lb[f_index].pi -> pi[loop] met pi[loop-1] */
buffer_replace_pi_pointers(loop, &pi[loop], &pi[loop - 1]);
}
/* prog info */
memmove(&pi[f_index], &pi[f_index+1], sizeof(proginfo) * n_to_move);
/* buffers */
memmove(&lb[f_index], &lb[f_index+1], sizeof(buffer) * n_to_move);
}
nfd--;
/* shrink array */
if (nfd == 0) /* empty? */
{
myfree(pi);
pi = NULL;
myfree(lb);
lb = NULL;
}
else /* not empty, just shrink */
{
pi = (proginfo *)myrealloc(pi, nfd * sizeof(proginfo));
lb = (buffer *) myrealloc(lb, nfd * sizeof(buffer));
}
}
else /* delete sub */
{
if (sub != &pi[f_index]) /* not in main array? */
{
proginfo *parent = &pi[f_index];
/* find parent of 'sub' */
while (parent -> next != sub)
parent = parent -> next;
parent -> next = sub -> next;
buffer_replace_pi_pointers(f_index, sub, NULL);
myfree(sub);
}
else /* main array, find next */
{
proginfo *oldnext = pi[f_index].next;
/* first, delete the entries of that entry (which is in the array) */
buffer_replace_pi_pointers(f_index, &pi[f_index], NULL);
/* then, 'rename' the pointers (original address -> address in array)... */
buffer_replace_pi_pointers(f_index, oldnext, &pi[f_index]);
/* and move the object to the array */
memmove(&pi[f_index], oldnext, sizeof(proginfo));
/* free the obsolete entry */
myfree(oldnext);
}
}
return delete_all;
}
/** do_exit
* - in: int sig
* - returns: nothing (doesn't return!)
* this function is called when MultiTail receives the TERM-signal. it is also
* called by, for example, wait_for_keypress when ^c is pressed. it stops all
* child-processes, ends the curses-library and exits with SUCCESS status
*/
void do_exit(void)
{
proginfo *cur;
int loop;
set_signal(SIGCHLD, SIG_IGN, "do_exit");
/* kill tail processes */
for(loop=0; loop<nfd; loop++)
{
cur = &pi[loop];
do
{
int r_i;
for(r_i = 0; r_i < cur -> n_redirect; r_i++)
{
if ((cur -> predir)[r_i].type != 0)
{
myclose((cur -> predir)[r_i].fd);
if ((cur -> predir)[r_i].type == REDIRECTTO_PIPE_FILTERED || (cur -> predir)[r_i].type == REDIRECTTO_PIPE)
stop_process((cur -> predir)[r_i].pid);
}
}
myclose(cur -> fd);
if (cur -> wfd != -1 && cur -> wfd != cur -> fd)
myclose(cur -> wfd);
if (cur -> pid != -1)
stop_process(cur -> pid);
cur = cur -> next;
}
while(cur);
}
/* kill convesion scripts */
for(loop=0; loop<n_conversions; loop++)
{
int loop2;
for(loop2=0; loop2<conversions[loop].n; loop2++)
{
if (conversions[loop].pcs[loop2].pid != 0)
{
stop_process(conversions[loop].pcs[loop2].pid);
}
}
}
/* kill colorscripts */
for(loop=0; loop<n_cschemes; loop++)
{
if (cschemes[loop].color_script.pid)
stop_process(cschemes[loop].color_script.pid);
}
if (set_title)
gui_window_header("");
#ifdef _DEBUG
/* free up as much as possible */
clean_memory();
/* to be able to check for memory leaks, (try to :-]) free up the whole
* array of windows and such. normally, don't do this: libc & kernel will
* take care of that
*/
dump_mem(SIGHUP);
#endif
endwin();
exit(EXIT_SUCCESS);
}
void signal_handler(int sig)
{
set_signal(sig, SIG_IGN, "? from signal_handler (start)");
switch(sig)
{
case SIGCHLD:
need_died_procs_check = 1;
break;
case SIGINT:
case SIGHUP:
case SIGTERM:
do_exit();
break;
case SIGUSR1:
got_sigusr1 = 1;
break;
case SIGWINCH:
terminal_changed = 1;
break;
default:
error_popup("Signal handler", -1, "Unexpected signal %d received.", sig);
}
set_signal(sig, signal_handler, "? from signal_handler (exit)");
}
void select_display_start_and_end(char *string, char mode, int wrap_offset, int win_width, int *prt_start, int *prt_end, int *disp_end)
{
int nbytes = strlen(string);
*prt_start = 0;
*prt_end = nbytes;
*disp_end = nbytes;
/* figure out what to display (because of linewraps and such) */
if (mode == 'a')
{
/* default is everything */
*disp_end = -1;
}
else if (mode == 'l')
{
*prt_start= min(wrap_offset, nbytes);
*prt_end = min(nbytes, win_width + wrap_offset);
*disp_end = min(win_width, nbytes - *prt_start);
}
else if (mode == 'r')
{
*prt_start = max(0, nbytes - win_width);
*disp_end = win_width;
}
else if (mode == 'S')
{
*prt_start = find_char_offset(string, ':');
if (*prt_start == -1)
*prt_start = 0;
*disp_end = nbytes - *prt_start;
}
else if (mode == 's')
{
if (nbytes > 16)
*prt_start = find_char_offset(&string[16], ' ');
else
*prt_start = -1;
if (*prt_start == -1)
*prt_start = 0;
*disp_end = nbytes - *prt_start;
}
else if (mode == 'o')
{
*prt_start = min(wrap_offset, nbytes);
*disp_end = nbytes - *prt_start;
}
}
int draw_tab(NEWWIN *win)
{
if (tab_width)
{
int curx = getcurx(win -> win), loop;
int move = (((curx / tab_width) + 1) * tab_width) - curx;
for(loop=0; loop<move; loop++)
waddch(win -> win, ' ');
return move;
}
return 0;
}
char find_highlight_matches(regmatch_t *matches, char use_regex, int offset)
{
int match_offset;
for(match_offset=0; match_offset<MAX_N_RE_MATCHES; match_offset++)
{
char matching;
if (matches[match_offset].rm_so == -1)
{
return 0;
}
matching = offset >= matches[match_offset].rm_so && offset < matches[match_offset].rm_eo;
if ((use_regex == 'c' && matching) || (use_regex == 'C' && !matching))
{
return 1;
}
}
return 0;
}
myattr_t * find_cmatches_index(color_offset_in_line *cmatches, int n_cmatches, mybool_t has_merge_colors, int offset)
{
static myattr_t final_color;
int cmatches_index = 0;
int fg_composed = -1, bg_composed = -1;
int attrs = -1;
char first_set = 1;
for(cmatches_index=0; cmatches_index<n_cmatches; cmatches_index++)
{
if (offset >= cmatches[cmatches_index].start && offset < cmatches[cmatches_index].end)
{
if (has_merge_colors == MY_FALSE)
{
return &cmatches[cmatches_index].attrs;
}
if (cmatches[cmatches_index].merge_color == MY_TRUE)
{
/* merge these colors */
if (cmatches[cmatches_index].attrs.colorpair_index != -1)
{
int fg_fc, bg_fc;
fg_fc = cp.fg_color[cmatches[cmatches_index].attrs.colorpair_index];
bg_fc = cp.bg_color[cmatches[cmatches_index].attrs.colorpair_index];
if (fg_fc != -1 && fg_composed == -1)
fg_composed = fg_fc;
if (bg_fc != -1 && bg_composed == -1)
bg_composed = bg_fc;
}
if (cmatches[cmatches_index].attrs.attrs != -1)
{
if (attrs == -1)
attrs = cmatches[cmatches_index].attrs.attrs;
else
attrs |= cmatches[cmatches_index].attrs.attrs;
}
}
else if (first_set)
{
first_set = 0;
fg_composed = cp.fg_color[cmatches[cmatches_index].attrs.colorpair_index];
bg_composed = cp.bg_color[cmatches[cmatches_index].attrs.colorpair_index];
attrs = cmatches[cmatches_index].attrs.attrs;
}
}
}
if (fg_composed != -1 || bg_composed != -1 || final_color.attrs != -1)
{
final_color.attrs = attrs;
final_color.colorpair_index = find_or_init_colorpair(fg_composed, bg_composed, 1);
return &final_color;
}
return NULL;
}
void gen_wordwrap_offsets(char *string, int start_offset, int end_offset, int win_width, int **offsets)
{
int check_x = start_offset;
int n_ww = 0;
for(;;)
{
int max_len = wordwrapmaxlength < win_width ? wordwrapmaxlength : win_width - 2;
check_x += win_width - 1;
if (check_x >= end_offset)
break;
while(max_len >= 0 && (!isspace(string[check_x])) && check_x > 0)
{
check_x--;
max_len--;
}
if (max_len >= 0)
{
*offsets = (int *)myrealloc(*offsets, (n_ww + 1) * sizeof(int));
(*offsets)[n_ww++] = check_x;
}
else
{
check_x += wordwrapmaxlength;
}
}
*offsets = (int *)myrealloc(*offsets, (n_ww + 1) * sizeof(int));
(*offsets)[n_ww] = -1;
}
int count_utf_bytes(int c)
{
if ((c & 0xe0) == 0xc0)
return 2;
else if ((c & 0xf0) == 0xe0)
return 3;
else if ((c & 0xf8) == 0xf0)
return 4;
return 1;
}
void do_color_print(proginfo *cur, char *use_string, int prt_start, int prt_end, int disp_end, color_offset_in_line *cmatches, int n_cmatches, mybool_t has_merge_colors, char start_reverse, regmatch_t *matches, int matching_regex, char use_regex, NEWWIN *win)
{
int offset;
myattr_t cdev = { -1, -1 };
char default_reverse_state = 0;
int disp_offset = 0;
char highlight_part = toupper(use_regex) == 'C';
char use_colorschemes = cur -> cdef.colorize == 'S' || cur -> cdef.colorize == 'T' || highlight_part;
int *ww = NULL;
int ww_offset = 0;
if (global_highlight_str)
{
if (regexec(&global_highlight_re, use_string, 0, NULL, 0) == 0)
default_reverse_state = 1;
}
if (cur -> line_wrap == 'w')
{
gen_wordwrap_offsets(use_string, prt_start, prt_end, getmaxx(win -> win), &ww);
}
/* print text */
for(offset=prt_start; offset<prt_end;)
{
char re_inv = default_reverse_state;
char is_control_or_extended_ascii = 0;
myattr_t new_cdev = { -1, -1 };
#ifdef UTF8_SUPPORT
const char *dummy = &use_string[offset];
wchar_t wcur = 0;
mbsrtowcs(&wcur, &dummy, 1, NULL);
#else
char wcur = use_string[offset];
#endif
if (ww != NULL && offset == ww[ww_offset])
{
wprintw(win -> win, "\n");
ww_offset++;
#ifdef UTF8_SUPPORT
if (iswspace(wcur))
#else
if (isspace(wcur))
#endif
{
offset++;
continue;
}
}
/* find things to colorize */
if (use_colorschemes)
{
if (highlight_part && find_highlight_matches(matches, use_regex, offset))
{
re_inv = 1;
}
else
{
/* if there's a list of offsets where colors should be displayed, check if the
* current offset in the string (the one we're going to display) is somewhere
* in that list off offsets
*/
myattr_t *pa = find_cmatches_index(cmatches, n_cmatches, has_merge_colors, offset);
if (pa != NULL)
new_cdev = *pa;
}
}
/* control-characters will be displayed as an inverse '.' */
#ifdef UTF8_SUPPORT
if (iswcntrl(wcur))
#else
if (iscntrl(wcur))
#endif
{
is_control_or_extended_ascii = 1;
#ifdef UTF8_SUPPORT
if (!iswspace(wcur))
#else
if (!isspace(wcur))
#endif
re_inv = 1;
}
if (re_inv)
new_cdev.attrs = inverse_attrs;
/* new color selected? then switch off previous color */
if (cdev.colorpair_index != new_cdev.colorpair_index || cdev.attrs != new_cdev.attrs)
{
myattr_off(win, cdev);
myattr_on(win, new_cdev);
cdev = new_cdev;
}
if (!is_control_or_extended_ascii)
{
#ifdef UTF8_SUPPORT
waddnwstr(win -> win, &wcur, 1);
#else
wprintw(win -> win, "%c", wcur);
#endif
disp_offset++;
}
else
{
/* error_exit("> 126 %d: %02x %02x %02x", wcur, use_string[offset+0], use_string[offset+1], use_string[offset+2]); */
if (wcur == 9 && tab_width > 0) /* TAB? */
{
disp_offset += draw_tab(win);
}
/* 13 (CR) is just silently ignored */
else if (wcur != 13)
{
if (caret_notation)
{
wprintw(win -> win, "^%c", 'a' + wcur - 1);
disp_offset++;
}
else
waddch(win -> win, '.');
disp_offset++;
}
}
if (disp_offset >= disp_end && disp_end != -1)
break;
offset += count_utf_bytes(use_string[offset]);
}
if (prt_start == prt_end) /* scrolled out line */
wprintw(win -> win, "\n");
if (cdev.attrs != -1)
myattr_off(win, cdev);
myfree(ww);
}
void color_print(int f_index, NEWWIN *win, proginfo *cur, char *string, regmatch_t *matches, int matching_regex, mybool_t force_to_winwidth, int start_at_offset, int end_at_offset, double ts, char show_window_nr)
{
char reverse = 0;
myattr_t cdev = { -1, -1 };
int prt_start = 0, prt_end = 0, disp_end = 0;
int mx = -1;
char use_regex = 0;
color_offset_in_line *cmatches = NULL;
int n_cmatches = 0;
mybool_t has_merge_colors = MY_FALSE;
char *use_string = NULL;
int x;
/* stop if there's no window tou output too */
if (!win)
return;
/* check if the cursor is not at the beginning of the line
* if it is not and it is also not at the most right position, move it
* to the left (when it is already at the right most position, it'll move
* to the left itself when emitting text)
*/
mx = getmaxx(win -> win);
x = getcurx(win -> win);
if ((x != 0 && x != mx) || !suppress_empty_lines)
wprintw(win -> win, "\n");
/* cur == NULL? Markerline! */
if (IS_MARKERLINE(cur))
{
draw_marker_line(win, string, cur);
return;
}
if (f_index >= 0)
{
/* add a window-id [xx] in front of each line */
if (show_window_nr)
{
mx -= wprintw(win -> win, window_number, f_index);
}
/* add a subwindow-ID: [xx] in front of each line */
if (show_subwindow_id || (show_window_nr && pi[f_index].next != NULL))
{
proginfo *ppi = &pi[f_index];
int subwin_nr = 0;
while(ppi != cur && ppi -> next != NULL)
{
subwin_nr++;
ppi = ppi -> next;
}
mx -= wprintw(win -> win, subwindow_number, subwin_nr);
}
}
/* add a timestamp in front of each line */
if (cur -> add_timestamp)
{
char timestamp[1024];
double_ts_to_str(ts, line_ts_format, timestamp, sizeof(timestamp));
mx -= wprintw(win -> win, "%s ", timestamp);
}
/* add a label */
LOG("Label\n");
if (cur -> label != NULL && (cur -> label)[0])
{
mx -= wprintw(win -> win, "%s", cur -> label);
}
if (mx <= 0) mx = 4;
/* had a matching regexp? */
if (matching_regex != -1)
use_regex = (cur -> pre)[matching_regex].use_regex;
/* select color or generate list of offset for colors */
if (cur -> cdef.colorize) /* no need to do this for b/w terminals */
{
/* choose_color not only generates a list of colors but if there are terminal-
* codes in that string which set a color those are stripped as well
* the stripping part should be moved to a seperate function
*/
cdev = choose_color(string, cur, &cmatches, &n_cmatches, &has_merge_colors, &use_string);
/* if not using colorschemes (which can have more then one color
* per line), set the color
*/
if (cur -> cdef.colorize != 'S' && cur -> cdef.colorize != 'T')
{
myattr_on(win, cdev);
if (cdev.attrs != -1 && cdev.attrs & A_REVERSE)
reverse = 1;
}
}
/* select start and end of string to display */
LOG("lwo: %d, mx: %d, ps: %d, pe: %d, de: %d\n", cur -> line_wrap_offset, mx, prt_start, prt_end, disp_end);
select_display_start_and_end(USE_IF_SET(use_string, string), force_to_winwidth == MY_TRUE?'l':cur -> line_wrap, cur -> line_wrap_offset, mx, &prt_start, &prt_end, &disp_end);
if (prt_start == 0 && start_at_offset > 0)
{
prt_start = start_at_offset;
prt_end = end_at_offset;
}
/* and display on terminal */
LOG("ps: %d, pe: %d, de: %d\n", prt_start, prt_end, disp_end);
LOG("%s\n", USE_IF_SET(use_string, string));
do_color_print(cur, USE_IF_SET(use_string, string), prt_start, prt_end, disp_end, cmatches, n_cmatches, has_merge_colors, reverse, matches, matching_regex, use_regex, win);
myattr_off(win, cdev);
myfree(use_string);
myfree(cmatches);
}
void check_filter_exec(char *cmd, char *matching_string)
{
int str_index, par_len = strlen(matching_string);
int cmd_len = strlen(cmd);
char *command = mymalloc(cmd_len + 1/* cmd + space */
+ 1 + (par_len * 2) + 1 + 1); /* "string"\0 */
int loop;
memcpy(command, cmd, cmd_len);
str_index = cmd_len;
command[str_index++] = ' ';
command[str_index++] = '\"';
for(loop=0; loop<par_len; loop++)
{
if (matching_string[loop] == '"' || matching_string[loop] == '`')
{
command[str_index++] = '\\';
}
command[str_index++] = matching_string[loop];
}
command[str_index++] = '\"';
command[str_index] = 0x00;
if (execute_program(command, 1) == -1)
error_popup("Execute triggered by filter", -1, "Failed to execute command '%s'.", command);
myfree(command);
}
/* check if the current line matches with a regular expression
returns 0 if a regexp failed to execute
*/
/* this code can be optimized quit a bit but I wanted to get it to work quickly so I expanded everything (2006/03/28) */
char check_filter(proginfo *cur, char *string, regmatch_t **pmatch, char **error, int *matching_regex, char do_re, char *display)
{
regmatch_t local_matches[MAX_N_RE_MATCHES];
int loop;
/* if no regexps, then always matches so it's the default */
char re_exec_ok = 1;
*error = NULL;
if (pmatch)
*pmatch = NULL;
*matching_regex = -1;
*display = 1;
local_matches[1].rm_so = local_matches[1].rm_eo = -1; /* let the compiler stop complaining */
/* do this regular expression? */
for(loop=0; loop<cur -> n_re; loop++)
{
int rc;
char cmd = (cur -> pre)[loop].use_regex;
char invert = (cur -> pre)[loop].invert_regex;
/* SKIP DISABLED REGEXPS */
if (!cmd)
continue;
/* EXECUTE THE REGEXPS */
if (pmatch)
{
if (*pmatch == NULL)
*pmatch = (regmatch_t *)mymalloc(sizeof(regmatch_t) * MAX_N_RE_MATCHES);
rc = regexec(&(cur -> pre)[loop].regex, string, MAX_N_RE_MATCHES, *pmatch, 0);
}
else
{
rc = regexec(&(cur -> pre)[loop].regex, string, MAX_N_RE_MATCHES, local_matches, 0);
}
/* IF ERROR, ABORT EVERYTHING */
if (rc != 0 && rc != REG_NOMATCH)
{
*error = convert_regexp_error(rc, &(cur -> pre)[loop].regex);
re_exec_ok = 0;
break;
}
/* MATCHED? */
if (rc == 0)
{
if (!invert)
{
*matching_regex = loop;
if (cmd == 'm')
{
*display = 1;
(cur -> pre)[loop].match_count++;
return 1;
}
else if (cmd == 'v')
{
*display = 0;
(cur -> pre)[loop].match_count++;
return 1;
}
else if (cmd == 'x' && do_re)
{
check_filter_exec((cur -> pre)[loop].cmd, string);
(cur -> pre)[loop].match_count++;
}
else if (cmd == 'X' && do_re)
{
regmatch_t *usep = USE_IF_SET(*pmatch, local_matches);
int len = usep[1].rm_eo - usep[1].rm_so;
char *dummy = (char *)mymalloc(len + 1);
memcpy(dummy, &string[usep[1].rm_so], len);
dummy[len] = 0x00;
check_filter_exec((cur -> pre)[loop].cmd, dummy);
(cur -> pre)[loop].match_count++;
myfree(dummy);
}
else if (toupper(cmd) == 'B' && do_re)
{
beep();
(cur -> pre)[loop].match_count++;
}
}
else
{
if (cmd == 'm')
{
*display = 0;
}
}
}
else
{
myfree(*pmatch);