1 | /* |
1 | /* |
2 | * Copyright 2001 by Marco d'Itri <md@linux.it> |
2 | * Copyright 2001 by Marco d'Itri <md@linux.it> |
3 | * |
3 | * Copyright 2000,2001,2002,2003,2004 |
4 | * Original version by Mike Baker, then maintained by pcg@goof.com. |
4 | * Marc Lehmann <pcg@goof.com>, |
|
|
5 | * and many others, see README |
|
|
6 | * |
|
|
7 | * Original version by Mike Baker. |
5 | * |
8 | * |
6 | * This program is free software; you can redistribute it and/or modify |
9 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
10 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
11 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
12 | * (at your option) any later version. |
… | |
… | |
31 | #include <sys/stat.h> |
34 | #include <sys/stat.h> |
32 | #include <sys/types.h> |
35 | #include <sys/types.h> |
33 | #include <locale.h> |
36 | #include <locale.h> |
34 | #include <ctype.h> |
37 | #include <ctype.h> |
35 | #include <stdarg.h> |
38 | #include <stdarg.h> |
|
|
39 | #include <X11/Xlib.h> |
|
|
40 | #include <X11/Xatom.h> |
|
|
41 | #include <X11/Xutil.h> |
|
|
42 | |
36 | #if HAS_REGEX |
43 | #if HAS_REGEX |
37 | #include <regex.h> |
44 | #include <regex.h> |
38 | #endif |
45 | #endif |
39 | #include <X11/Xlib.h> |
46 | |
40 | #include <X11/Xatom.h> |
47 | #define SHADE_X 2 |
41 | #include <X11/Xutil.h> |
48 | #define SHADE_Y 2 |
42 | |
49 | |
43 | /* data structures */ |
50 | /* data structures */ |
44 | struct logfile_entry |
51 | struct logfile_entry |
45 | { |
52 | { |
46 | struct logfile_entry *next; |
53 | struct logfile_entry *next; |
… | |
… | |
53 | off_t last_size; /* file size at the last check */ |
60 | off_t last_size; /* file size at the last check */ |
54 | unsigned long color; /* color to be used for printing */ |
61 | unsigned long color; /* color to be used for printing */ |
55 | int partial; /* true if the last line isn't complete */ |
62 | int partial; /* true if the last line isn't complete */ |
56 | int lastpartial; /* true if the previous output wasn't complete */ |
63 | int lastpartial; /* true if the previous output wasn't complete */ |
57 | int index; /* index into linematrix of a partial line */ |
64 | int index; /* index into linematrix of a partial line */ |
|
|
65 | int modified; /* true if line is modified & needs displaying */ |
58 | }; |
66 | }; |
59 | |
67 | |
60 | struct linematrix |
68 | struct linematrix |
61 | { |
69 | { |
62 | char *line; |
70 | char *line; |
|
|
71 | int len; |
63 | unsigned long color; |
72 | unsigned long color; |
64 | }; |
73 | }; |
65 | |
74 | |
|
|
75 | struct displaymatrix |
|
|
76 | { |
|
|
77 | char *line; |
|
|
78 | int len; |
|
|
79 | int buffer_size; |
|
|
80 | }; |
|
|
81 | |
66 | /* global variables */ |
82 | /* global variables */ |
|
|
83 | struct linematrix *lines; |
|
|
84 | struct displaymatrix *display; |
67 | int width = STD_WIDTH, height = STD_HEIGHT, listlen; |
85 | int width = STD_WIDTH, height = STD_HEIGHT, listlen; |
68 | int win_x = LOC_X, win_y = LOC_Y; |
86 | int win_x = LOC_X, win_y = LOC_Y; |
69 | int font_descent, font_height; |
87 | int font_ascent, font_height; |
|
|
88 | int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */ |
|
|
89 | int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */ |
70 | int do_reopen; |
90 | int do_reopen; |
71 | struct timeval interval = { 3, 0 }; |
91 | struct timeval interval = { 2, 400000 }; |
72 | XFontSet fontset; |
92 | XFontSet fontset; |
73 | |
93 | |
74 | /* command line options */ |
94 | /* command line options */ |
75 | int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, |
95 | int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, |
76 | opt_whole, opt_update, geom_mask, reload = 0; |
96 | opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap, |
|
|
97 | geom_mask, reload = 0; |
77 | const char *command = NULL, |
98 | const char *command = NULL, |
78 | *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, |
99 | *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, |
79 | *continuation = "[+]"; |
100 | *continuation = "[+]"; |
80 | |
101 | |
81 | struct logfile_entry *loglist = NULL, *loglist_tail = NULL; |
102 | struct logfile_entry *loglist = NULL, *loglist_tail = NULL; |
… | |
… | |
90 | regex_t from; |
111 | regex_t from; |
91 | const char *to; |
112 | const char *to; |
92 | struct re_list *next; |
113 | struct re_list *next; |
93 | }; |
114 | }; |
94 | struct re_list *re_head, *re_tail; |
115 | struct re_list *re_head, *re_tail; |
|
|
116 | char *transform_to = NULL; |
|
|
117 | regex_t *transformre; |
95 | #endif |
118 | #endif |
96 | |
119 | |
97 | |
120 | |
98 | /* prototypes */ |
121 | /* prototypes */ |
99 | void list_files (int); |
122 | void list_files (int); |
… | |
… | |
101 | void force_refresh (int); |
124 | void force_refresh (int); |
102 | void blank_window (int); |
125 | void blank_window (int); |
103 | |
126 | |
104 | void InitWindow (void); |
127 | void InitWindow (void); |
105 | unsigned long GetColor (const char *); |
128 | unsigned long GetColor (const char *); |
106 | void redraw (void); |
129 | void redraw (int); |
107 | void refresh (struct linematrix *, int, int); |
130 | void refresh (int, int, int, int); |
108 | |
131 | |
109 | void transform_line (char *s); |
132 | void transform_line (char *s); |
110 | int lineinput (struct logfile_entry *); |
133 | int lineinput (struct logfile_entry *); |
111 | void reopen (void); |
134 | void reopen (void); |
112 | void check_open_files (void); |
135 | void check_open_files (void); |
113 | FILE *openlog (struct logfile_entry *); |
136 | FILE *openlog (struct logfile_entry *); |
114 | void main_loop (void); |
137 | static void main_loop (void); |
115 | |
138 | |
116 | void display_version (void); |
139 | void display_version (void); |
117 | void display_help (char *); |
140 | void display_help (char *); |
118 | void install_signal (int, void (*)(int)); |
141 | void install_signal (int, void (*)(int)); |
119 | void *xstrdup (const char *); |
142 | void *xstrdup (const char *); |
120 | void *xmalloc (size_t); |
143 | void *xmalloc (size_t); |
|
|
144 | void *xrealloc (void *, size_t); |
121 | int daemonize (void); |
145 | int daemonize (void); |
122 | |
146 | |
123 | /* signal handlers */ |
147 | /* signal handlers */ |
124 | void |
148 | void |
125 | list_files (int dummy) |
149 | list_files (int dummy) |
… | |
… | |
138 | } |
162 | } |
139 | |
163 | |
140 | void |
164 | void |
141 | force_refresh (int dummy) |
165 | force_refresh (int dummy) |
142 | { |
166 | { |
143 | XClearArea (disp, root, win_x, win_y, width, height, False); |
|
|
144 | redraw (); |
167 | redraw (1); |
145 | } |
168 | } |
146 | |
169 | |
147 | void |
170 | void |
148 | blank_window (int dummy) |
171 | blank_window (int dummy) |
149 | { |
172 | { |
150 | XClearArea (disp, root, win_x, win_y, width, height, False); |
173 | XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False); |
151 | XFlush (disp); |
174 | XFlush (disp); |
152 | exit (0); |
175 | exit (0); |
153 | } |
176 | } |
154 | |
177 | |
155 | /* X related functions */ |
178 | /* X related functions */ |
… | |
… | |
284 | |
307 | |
285 | { |
308 | { |
286 | XFontSetExtents *xfe = XExtentsOfFontSet (fontset); |
309 | XFontSetExtents *xfe = XExtentsOfFontSet (fontset); |
287 | |
310 | |
288 | font_height = xfe->max_logical_extent.height; |
311 | font_height = xfe->max_logical_extent.height; |
289 | font_descent = xfe->max_logical_extent.y; |
312 | font_ascent = -xfe->max_logical_extent.y; |
290 | } |
313 | } |
291 | |
314 | |
292 | if (geom_mask & XNegative) |
315 | if (geom_mask & XNegative) |
293 | win_x = win_x + ScreenWidth - width; |
316 | win_x = win_x + ScreenWidth - width; |
294 | if (geom_mask & YNegative) |
317 | if (geom_mask & YNegative) |
295 | win_y = win_y + ScreenHeight - height; |
318 | win_y = win_y + ScreenHeight - height; |
296 | |
319 | |
|
|
320 | if (opt_outline) |
|
|
321 | { |
|
|
322 | /* adding outline increases the total width and height by 2 |
|
|
323 | pixels each, and offsets the text one pixel right and one |
|
|
324 | pixel down */ |
|
|
325 | effect_x_space = effect_y_space = 2; |
|
|
326 | effect_x_offset = effect_y_offset = 1; |
|
|
327 | } |
|
|
328 | else if (opt_shade) |
|
|
329 | { |
|
|
330 | /* adding a shadow increases the space used */ |
|
|
331 | effect_x_space = abs(SHADE_X); |
|
|
332 | effect_y_space = abs(SHADE_Y); |
|
|
333 | /* if the shadow is to the right and below then we don't need |
|
|
334 | * to move the text to make space for it, but shadows to the left |
|
|
335 | * and above need accomodating */ |
|
|
336 | effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X; |
|
|
337 | effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y; |
|
|
338 | } |
|
|
339 | else |
|
|
340 | { |
|
|
341 | effect_x_space = effect_y_space = 0; |
|
|
342 | effect_x_offset = effect_y_offset = 0; |
|
|
343 | } |
|
|
344 | |
|
|
345 | /* if we are using -shade or -outline, there will be less usable |
|
|
346 | * space for output */ |
297 | listlen = height / font_height; |
347 | listlen = (height - effect_y_space) / font_height; |
298 | |
348 | |
299 | if (!listlen) |
349 | if (!listlen) |
300 | { |
350 | { |
301 | fprintf (stderr, "height too small for a single line, setting to %d\n", |
351 | fprintf (stderr, "height too small for a single line, setting to %d\n", |
302 | font_height); |
352 | font_height); |
303 | listlen = 1; |
353 | listlen = 1; |
304 | } |
354 | } |
305 | |
355 | |
306 | height = listlen * font_height; |
356 | /* leave the height how the user requested it. it might not all be |
307 | |
357 | * used, but this will allow the geometry to be tuned more accurately |
|
|
358 | * (with the -frame option) |
|
|
359 | * the old code did this: |
|
|
360 | * height = listlen * font_height + effect_y_space; */ |
|
|
361 | |
308 | XSelectInput (disp, root, ExposureMask | FocusChangeMask); |
362 | XSelectInput (disp, root, ExposureMask | FocusChangeMask); |
309 | } |
363 | } |
310 | |
364 | |
311 | char * |
|
|
312 | detabificate (char *s) |
|
|
313 | { |
|
|
314 | char *out; |
|
|
315 | int i, j; |
|
|
316 | |
|
|
317 | out = malloc (8 * strlen (s) + 1); |
|
|
318 | |
|
|
319 | for (i = 0, j = 0; s[i]; i++) |
|
|
320 | { |
|
|
321 | if (s[i] == '\t') |
|
|
322 | do |
|
|
323 | out[j++] = ' '; |
|
|
324 | while (j % 8); |
|
|
325 | else |
|
|
326 | out[j++] = s[i]; |
|
|
327 | } |
|
|
328 | |
|
|
329 | out[j] = '\0'; |
|
|
330 | return out; |
|
|
331 | } |
|
|
332 | |
|
|
333 | /* |
365 | /* |
334 | * redraw does a complete redraw, rather than an update (i.e. the area |
366 | * if redraw() is passwd a non-zero argument, it does a complete |
335 | * gets cleared first) |
367 | * redraw, rather than an update. if the argument is zero (and |
|
|
368 | * -noflicker is in effect) then only the lines which have changed |
|
|
369 | * since the last draw are redrawn. |
|
|
370 | * |
336 | * the rest is handled by regular refresh()'es |
371 | * the rest is handled by regular refresh()'es |
337 | */ |
372 | */ |
338 | void |
373 | void |
339 | redraw (void) |
374 | redraw (int redraw_all) |
340 | { |
375 | { |
341 | XClearArea (disp, root, win_x, win_y, width, height, True); |
376 | XSetClipMask (disp, WinGC, None); |
|
|
377 | refresh (0, 32768, 1, redraw_all); |
342 | } |
378 | } |
343 | |
379 | |
344 | /* Just redraw everything without clearing (i.e. after an EXPOSE event) */ |
380 | /* Just redraw everything without clearing (i.e. after an EXPOSE event) */ |
345 | void |
381 | void |
346 | refresh (struct linematrix *lines, int miny, int maxy) |
382 | refresh (int miny, int maxy, int clear, int refresh_all) |
347 | { |
383 | { |
348 | int lin; |
384 | int lin; |
349 | int offset = (listlen + 1) * font_height; |
385 | int offset = listlen * font_height + font_ascent + effect_y_offset; |
350 | unsigned long black_color = GetColor ("black"); |
386 | unsigned long black_color = GetColor ("black"); |
351 | |
387 | |
352 | miny -= win_y + font_height; |
388 | miny -= win_y + font_height; |
353 | maxy -= win_y - font_height; |
389 | maxy -= win_y - font_height; |
354 | |
390 | |
|
|
391 | if (clear && !opt_noflicker) |
|
|
392 | XClearArea (disp, root, win_x, win_y, width, height, False); |
|
|
393 | |
355 | for (lin = listlen; lin--;) |
394 | for (lin = listlen; lin--;) |
356 | { |
395 | { |
357 | struct linematrix *line = |
|
|
358 | lines + (opt_reverse ? listlen - lin - 1 : lin); |
396 | struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin); |
|
|
397 | struct displaymatrix *display_line = display + lin; |
359 | |
398 | |
360 | offset -= font_height; |
399 | offset -= font_height; |
361 | |
400 | |
362 | if (offset < miny || offset > maxy) |
401 | if (offset < miny || offset > maxy) |
363 | continue; |
402 | continue; |
364 | |
403 | |
365 | if (opt_shade) |
404 | /* if this line is a different than it was, then it |
|
|
405 | * needs displaying */ |
|
|
406 | if (!opt_noflicker |
|
|
407 | || refresh_all |
|
|
408 | || display_line->len != line->len |
|
|
409 | || memcmp (display_line->line, line->line, line->len)) |
366 | { |
410 | { |
|
|
411 | /* don't bother updating the record of what has been |
|
|
412 | * displayed if -noflicker isn't in effect, since we redraw |
|
|
413 | * the whole display every time anyway */ |
|
|
414 | if (opt_noflicker) |
|
|
415 | { |
|
|
416 | /* update the record of what has been displayed; |
|
|
417 | * first make sure the buffer is big enough */ |
|
|
418 | if (display_line->buffer_size <= line->len) |
|
|
419 | { |
|
|
420 | display_line->buffer_size = line->len + 1; |
|
|
421 | display_line->line = xrealloc (display_line->line, display_line->buffer_size); |
|
|
422 | } |
|
|
423 | |
|
|
424 | display_line->len = line->len; |
|
|
425 | memcpy (display_line->line, line->line, line->len); |
|
|
426 | |
|
|
427 | if (clear) |
|
|
428 | XClearArea (disp, root, win_x, win_y + offset - font_ascent, |
|
|
429 | width + effect_x_space, font_height + effect_y_space, False); |
|
|
430 | } |
|
|
431 | |
|
|
432 | if (opt_outline) |
|
|
433 | { |
|
|
434 | int x, y; |
367 | XSetForeground (disp, WinGC, black_color); |
435 | XSetForeground (disp, WinGC, black_color); |
|
|
436 | |
|
|
437 | for (x = -1; x < 2; x += 2) |
|
|
438 | for (y = -1; y < 2; y += 2) |
|
|
439 | XmbDrawString (disp, root, fontset, WinGC, |
|
|
440 | win_x + effect_x_offset + x, |
|
|
441 | win_y + y + offset, |
|
|
442 | line->line, line->len); |
|
|
443 | } |
|
|
444 | else if (opt_shade) |
|
|
445 | { |
|
|
446 | XSetForeground (disp, WinGC, black_color); |
368 | XmbDrawString (disp, root, fontset, WinGC, win_x + 2, |
447 | XmbDrawString (disp, root, fontset, WinGC, |
369 | win_y + offset + 2, line->line, strlen (line->line)); |
448 | win_x + effect_x_offset + SHADE_X, |
|
|
449 | win_y + offset + SHADE_Y, |
|
|
450 | line->line, line->len); |
370 | } |
451 | } |
371 | |
452 | |
372 | XSetForeground (disp, WinGC, line->color); |
453 | XSetForeground (disp, WinGC, line->color); |
373 | XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset, |
454 | XmbDrawString (disp, root, fontset, WinGC, |
|
|
455 | win_x + effect_x_offset, |
|
|
456 | win_y + offset, |
374 | line->line, strlen (line->line)); |
457 | line->line, line->len); |
|
|
458 | } |
375 | } |
459 | } |
376 | |
460 | |
377 | if (opt_frame) |
461 | if (opt_frame) |
|
|
462 | { |
|
|
463 | XSetForeground (disp, WinGC, GetColor (def_color)); |
378 | XDrawRectangle (disp, root, WinGC, win_x, win_y, width, height); |
464 | XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1); |
|
|
465 | } |
379 | } |
466 | } |
380 | |
467 | |
381 | #if HAS_REGEX |
468 | #if HAS_REGEX |
382 | void |
469 | void |
383 | transform_line (char *s) |
470 | transform_line (char *s) |
… | |
… | |
395 | if (transformre) |
482 | if (transformre) |
396 | { |
483 | { |
397 | int i; |
484 | int i; |
398 | regmatch_t matched[16]; |
485 | regmatch_t matched[16]; |
399 | |
486 | |
400 | i = regexec (&transformre, string, 16, matched, 0); |
487 | i = regexec (transformre, s, 16, matched, 0); |
401 | if (i == 0) |
488 | if (i == 0) |
402 | { /* matched */ |
489 | { /* matched */ |
|
|
490 | int match_start = matched[0].rm_so; |
|
|
491 | int match_end = matched[0].rm_eo; |
|
|
492 | int old_len = match_end - match_start; |
|
|
493 | int new_len = strlen(transform_to); |
|
|
494 | int old_whole_len = strlen(s); |
|
|
495 | printf("regexp was matched by '%s' - replace with '%s'\n", s, transform_to); |
|
|
496 | printf("match is from %d to %d\n", |
|
|
497 | match_start, match_end); |
|
|
498 | if (new_len > old_len) { |
|
|
499 | s = xrealloc(s, old_whole_len + new_len - old_len); |
|
|
500 | } |
|
|
501 | if (new_len != old_len) { |
|
|
502 | memcpy(s + match_end + new_len - old_len, |
|
|
503 | s + match_end, |
|
|
504 | old_whole_len - match_end); |
|
|
505 | s[old_whole_len + new_len - old_len] = '\0'; |
|
|
506 | } |
|
|
507 | memcpy(s + match_start, |
|
|
508 | transform_to, |
|
|
509 | new_len); |
|
|
510 | printf("transformed to '%s'\n", s); |
403 | } |
511 | } |
|
|
512 | else |
|
|
513 | { |
|
|
514 | printf("regexp was not matched by '%s'\n", s); |
|
|
515 | } |
404 | } |
516 | } |
405 | } |
517 | } |
406 | #endif |
518 | #endif |
407 | |
519 | |
|
|
520 | char * |
|
|
521 | concat_line (const char *p1, const char *p2) |
|
|
522 | { |
|
|
523 | int l1 = p1 ? strlen (p1) : 0; |
|
|
524 | int l2 = strlen (p2); |
|
|
525 | char *r = xmalloc (l1 + l2 + 1); |
|
|
526 | |
|
|
527 | memcpy (r, p1, l1); |
|
|
528 | memcpy (r + l1, p2, l2); |
|
|
529 | r[l1 + l2] = 0; |
|
|
530 | |
|
|
531 | return r; |
|
|
532 | } |
408 | |
533 | |
409 | /* |
534 | /* |
410 | * This routine should read 'width' characters and not more. However, |
535 | * HACK-1: This routine should read a single line, no matter how long. |
411 | * we really want to read width + 1 characters if the last char is a '\n', |
|
|
412 | * which we should remove afterwards. So, read width+1 chars and ungetc |
|
|
413 | * the last character if it's not a newline. This means 'string' must be |
|
|
414 | * width + 2 wide! |
|
|
415 | */ |
536 | */ |
416 | int |
537 | int |
417 | lineinput (struct logfile_entry *logfile) |
538 | lineinput (struct logfile_entry *logfile) |
418 | { |
539 | { |
419 | char *string = logfile->buf; |
540 | char buff[1024], *p = buff; |
420 | int slen = width + 2; |
541 | int ch; |
421 | FILE *f = logfile->fp; |
542 | /* HACK-2: add on the length of any partial line which we will be appending to */ |
422 | |
543 | int ofs = logfile->buf ? strlen (logfile->buf) : 0; |
423 | int len = strlen (string); |
|
|
424 | int partial = logfile->partial; |
|
|
425 | |
544 | |
426 | do |
545 | do |
427 | { |
546 | { |
428 | if (fgets (string + len, slen - len, f) == NULL) /* EOF or Error */ |
547 | ch = fgetc (logfile->fp); |
429 | return 0; |
|
|
430 | |
548 | |
431 | len = strlen (string); |
549 | if (ch == '\n' || ch == EOF) |
432 | } |
550 | break; |
433 | while (len == 0); |
551 | else if (ch == '\r') |
434 | |
552 | continue; /* skip */ |
435 | logfile->partial = 0; |
553 | else if (ch == '\t') |
436 | |
554 | { |
437 | if (string[len - 1] == '\n') |
555 | do |
438 | /* if the string ends in a newline, delete the newline */ |
556 | { |
439 | string[len - 1] = '\0'; /* erase newline */ |
557 | *p++ = ' '; |
440 | else if (len >= slen - 1) |
558 | ofs++; |
|
|
559 | } |
|
|
560 | while (ofs & 7); |
|
|
561 | } |
|
|
562 | else |
|
|
563 | { |
|
|
564 | *p++ = ch; |
|
|
565 | ofs++; |
|
|
566 | } |
441 | { |
567 | } |
442 | /* otherwise if we've read one too many characters, un-read the last one and delete it */ |
568 | while (p < buff + (sizeof buff) - 8 - 1); |
443 | ungetc (string[len - 1], f); |
569 | |
444 | string[len - 1] = '\0'; |
570 | if (p == buff && ch == EOF) |
445 | } |
|
|
446 | else if (opt_whole) |
|
|
447 | return 0; |
571 | return 0; |
448 | else |
572 | |
|
|
573 | *p = 0; |
|
|
574 | |
|
|
575 | p = concat_line (logfile->buf, buff); |
|
|
576 | free (logfile->buf); logfile->buf = p; |
|
|
577 | |
|
|
578 | logfile->lastpartial = logfile->partial; |
|
|
579 | /* there are 3 ways we could have exited the loop: reading '\n', |
|
|
580 | * reaching EOF, or filling the buffer; the 2nd and 3rd of these |
|
|
581 | * both result in a partial line */ |
449 | logfile->partial = 1; |
582 | logfile->partial = ch != '\n'; |
|
|
583 | |
|
|
584 | if (logfile->partial && opt_whole) |
|
|
585 | return 0; |
450 | |
586 | |
451 | #if HAS_REGEX |
587 | #if HAS_REGEX |
452 | transform_line (string); |
588 | transform_line (logfile->buf); |
453 | #endif |
589 | #endif |
454 | logfile->lastpartial = partial; |
|
|
455 | return len; |
590 | return 1; |
456 | } |
591 | } |
457 | |
592 | |
458 | /* input: reads file->fname |
593 | /* input: reads file->fname |
459 | * output: fills file->fp, file->inode |
594 | * output: fills file->fp, file->inode |
460 | * returns file->fp |
595 | * returns file->fp |
… | |
… | |
524 | { /* file missing? */ |
659 | { /* file missing? */ |
525 | sleep (1); |
660 | sleep (1); |
526 | if (e->fp) |
661 | if (e->fp) |
527 | fclose (e->fp); |
662 | fclose (e->fp); |
528 | if (openlog (e) == NULL) |
663 | if (openlog (e) == NULL) |
529 | break; |
664 | continue; |
530 | } |
665 | } |
531 | |
666 | |
|
|
667 | /* HACK-3: stats can be uninitialised here (if the file didn't |
|
|
668 | * exist when stat() was called, but was recreated during the |
|
|
669 | * sleep(1)) */ |
532 | if (stats.st_ino != e->inode) |
670 | if (stats.st_ino != e->inode) |
533 | { /* file renamed? */ |
671 | { /* file renamed? */ |
534 | if (e->fp) |
672 | if (e->fp) |
535 | fclose (e->fp); |
673 | fclose (e->fp); |
536 | if (openlog (e) == NULL) |
674 | if (openlog (e) == NULL) |
537 | break; |
675 | continue; |
538 | } |
676 | } |
539 | |
677 | |
540 | if (stats.st_size < e->last_size) |
678 | if (stats.st_size < e->last_size) |
541 | { /* file truncated? */ |
679 | { /* file truncated? */ |
542 | fseek (e->fp, 0, SEEK_SET); |
680 | fseek (e->fp, 0, SEEK_SET); |
543 | e->last_size = stats.st_size; |
681 | e->last_size = stats.st_size; |
544 | } |
682 | } |
545 | } |
683 | } |
546 | } |
684 | } |
547 | |
685 | |
548 | #define SCROLL_UP(lines, listlen) \ |
686 | /* |
549 | { \ |
687 | * insert a single physical line (that must be short enough to fit) |
550 | int cur_line; \ |
688 | * at position "idx" by pushing up lines above it. the caller |
551 | struct logfile_entry *current; \ |
689 | * MUST then fill in lines[idx] with valid data. |
|
|
690 | */ |
|
|
691 | static void |
|
|
692 | insert_line (int idx) |
|
|
693 | { |
|
|
694 | int cur_line; |
|
|
695 | struct logfile_entry *current; |
|
|
696 | |
|
|
697 | free (lines[0].line); |
|
|
698 | |
552 | for (cur_line = 0; cur_line < (listlen - 1); cur_line++) { \ |
699 | for (cur_line = 0; cur_line < idx; cur_line++) |
553 | strcpy(lines[cur_line].line, lines[cur_line + 1].line); \ |
|
|
554 | lines[cur_line].color = lines[cur_line + 1].color; \ |
700 | lines[cur_line] = lines[cur_line + 1]; |
555 | } \ |
701 | |
556 | for (current = loglist; current; current = current->next) \ |
702 | for (current = loglist; current; current = current->next) |
557 | if (current->partial && current->index) \ |
703 | if (current->index <= idx) |
558 | current->index--; \ |
704 | current->index--; |
559 | } |
705 | } |
560 | |
706 | |
561 | void |
707 | /* |
|
|
708 | * remove a single physical line at position "idx" by moving the lines above it |
|
|
709 | * down and inserting a "~" line at the top. |
|
|
710 | */ |
|
|
711 | static void |
|
|
712 | delete_line (int idx) |
|
|
713 | { |
|
|
714 | int cur_line; |
|
|
715 | struct logfile_entry *current; |
|
|
716 | |
|
|
717 | for (cur_line = idx; cur_line > 0; cur_line--) |
|
|
718 | lines[cur_line] = lines[cur_line - 1]; |
|
|
719 | |
|
|
720 | lines[0].line = xstrdup ("~"); |
|
|
721 | |
|
|
722 | for (current = loglist; current; current = current->next) |
|
|
723 | if (current->index >= 0 && current->index <= idx) |
|
|
724 | current->index++; |
|
|
725 | } |
|
|
726 | |
|
|
727 | /* |
|
|
728 | * takes a logical log file line and splits it into multiple physical |
|
|
729 | * screen lines by splitting it whenever a part becomes too long. |
|
|
730 | * lal lines will be inserted at position "idx". |
|
|
731 | */ |
|
|
732 | static void |
|
|
733 | split_line (int idx, const char *str, unsigned long color) |
|
|
734 | { |
|
|
735 | int l = strlen (str); |
|
|
736 | int last_wrapped = 0; |
|
|
737 | const char *p = str; |
|
|
738 | static int continuation_width = -1; |
|
|
739 | static int continuation_length; |
|
|
740 | |
|
|
741 | /* only calculate the continuation's width once */ |
|
|
742 | if (continuation_width == -1) |
|
|
743 | { |
|
|
744 | continuation_length = strlen(continuation); |
|
|
745 | continuation_width = XmbTextEscapement (fontset, continuation, continuation_length); |
|
|
746 | printf("continuation width is %d and length is %d\n", continuation_width, continuation_length); |
|
|
747 | } |
|
|
748 | |
|
|
749 | do |
|
|
750 | { |
|
|
751 | const char *beg = p; |
|
|
752 | int w = last_wrapped ? continuation_width : 0; |
|
|
753 | int wrapped = 0; |
|
|
754 | const char *break_p = NULL; |
|
|
755 | |
|
|
756 | while (*p) |
|
|
757 | { |
|
|
758 | /* find the length in bytes of the next multibyte character */ |
|
|
759 | int len = mblen (p, l); |
|
|
760 | if (len <= 0) |
|
|
761 | len = 1; /* ignore (don't skip) illegal character sequences */ |
|
|
762 | |
|
|
763 | /* find the width in pixels of the next character */ |
|
|
764 | int cw = XmbTextEscapement (fontset, p, len); |
|
|
765 | if (cw + w > width - effect_x_space) |
|
|
766 | { |
|
|
767 | if (p == beg) |
|
|
768 | { |
|
|
769 | fprintf(stderr, "we can't even fit a single character onto the line\n"); |
|
|
770 | if (len == 1) fprintf(stderr, "(the character we couldn't fit was '%c')\n", *p); |
|
|
771 | exit(1); |
|
|
772 | } |
|
|
773 | |
|
|
774 | wrapped = 1; |
|
|
775 | break; |
|
|
776 | } |
|
|
777 | |
|
|
778 | if (opt_wordwrap && len == 1 && p[0] == ' ') |
|
|
779 | break_p = p; |
|
|
780 | |
|
|
781 | w += cw; |
|
|
782 | p += len; |
|
|
783 | l -= len; |
|
|
784 | } |
|
|
785 | |
|
|
786 | /* if we're wrapping at spaces, and the line is long enough to |
|
|
787 | * wrap, and we've seen a space already, and the space wasn't |
|
|
788 | * the first character on the line, then wrap at the space */ |
|
|
789 | if (opt_wordwrap && wrapped && break_p && break_p != beg) |
|
|
790 | { |
|
|
791 | l += p - break_p; |
|
|
792 | p = break_p; |
|
|
793 | } |
|
|
794 | |
|
|
795 | { |
|
|
796 | /* HACK-4 - consider inserting the 'continuation string' |
|
|
797 | * before the rest of the wrapped line */ |
|
|
798 | int len = p - beg + (last_wrapped ? continuation_length : 0); |
|
|
799 | char *s = xmalloc (len + 1); |
|
|
800 | if (last_wrapped) |
|
|
801 | { |
|
|
802 | memcpy (s, continuation, continuation_length); |
|
|
803 | memcpy (s + continuation_length, beg, p - beg); |
|
|
804 | } |
|
|
805 | else |
|
|
806 | memcpy (s, beg, len); |
|
|
807 | |
|
|
808 | s[len] = 0; |
|
|
809 | insert_line (idx); |
|
|
810 | lines[idx].line = s; |
|
|
811 | lines[idx].len = len; |
|
|
812 | lines[idx].color = color; |
|
|
813 | } |
|
|
814 | |
|
|
815 | /* if we wrapped at a space, don't display the space */ |
|
|
816 | if (opt_wordwrap && wrapped && break_p && break_p != beg) |
|
|
817 | { |
|
|
818 | l--; |
|
|
819 | p++; |
|
|
820 | } |
|
|
821 | |
|
|
822 | last_wrapped = wrapped; |
|
|
823 | } |
|
|
824 | while (l); |
|
|
825 | } |
|
|
826 | |
|
|
827 | /* |
|
|
828 | * append something to an existing physical line. this is done |
|
|
829 | * by deleting the file on-screen, concatenating the new data to it |
|
|
830 | * and splitting it again. |
|
|
831 | */ |
|
|
832 | static void |
|
|
833 | append_line (int idx, const char *str) |
|
|
834 | { |
|
|
835 | unsigned long color = lines[idx].color; |
|
|
836 | char *old = lines[idx].line; |
|
|
837 | char *new = concat_line (old, str); |
|
|
838 | |
|
|
839 | free (old); |
|
|
840 | |
|
|
841 | delete_line (idx); |
|
|
842 | split_line (idx, new, color); |
|
|
843 | } |
|
|
844 | |
|
|
845 | static void |
562 | main_loop (void) |
846 | main_loop (void) |
563 | { |
847 | { |
564 | struct linematrix *lines = xmalloc (sizeof (struct linematrix) * listlen); |
848 | lines = xmalloc (sizeof (struct linematrix) * listlen); |
565 | int lin, miny, maxy; |
849 | display = xmalloc (sizeof (struct displaymatrix) * listlen); |
|
|
850 | int lin; |
566 | time_t lastreload; |
851 | time_t lastreload; |
567 | Region region = XCreateRegion (); |
852 | Region region = XCreateRegion (); |
568 | XEvent xev; |
853 | XEvent xev; |
569 | struct logfile_entry *lastprinted = NULL; |
854 | struct logfile_entry *lastprinted = NULL; |
570 | struct logfile_entry *current; |
855 | struct logfile_entry *current; |
|
|
856 | int need_update = 1; |
571 | |
857 | |
572 | maxy = 0; |
|
|
573 | miny = win_y + height; |
|
|
574 | lastreload = time (NULL); |
858 | lastreload = time (NULL); |
575 | |
859 | |
576 | /* Initialize linematrix */ |
860 | /* Initialize linematrix */ |
577 | for (lin = 0; lin < listlen; lin++) |
861 | for (lin = 0; lin < listlen; lin++) |
578 | { |
862 | { |
579 | lines[lin].line = xmalloc (width + 2); |
863 | lines[lin].line = xstrdup ("~"); |
580 | strcpy (lines[lin].line, "~"); |
864 | lines[lin].len = 1; |
|
|
865 | display[lin].line = xstrdup(""); |
|
|
866 | display[lin].len = 0; |
|
|
867 | display[lin].buffer_size = 1; |
581 | lines[lin].color = GetColor (def_color); |
868 | lines[lin].color = GetColor (def_color); |
582 | } |
869 | } |
583 | |
870 | |
584 | /* show the display full of empty lines ("~") in case the first |
|
|
585 | * time around the loop doesn't produce any output, as it won't if |
|
|
586 | * either (a) -noinitial is set or (b) all the files are currently |
|
|
587 | * empty */ |
|
|
588 | redraw (); |
|
|
589 | |
|
|
590 | for (;;) |
871 | for (;;) |
591 | { |
872 | { |
592 | int need_update = 0; |
|
|
593 | |
|
|
594 | /* read logs */ |
873 | /* read logs */ |
595 | for (current = loglist; current; current = current->next) |
874 | for (current = loglist; current; current = current->next) |
596 | { |
875 | { |
597 | if (!current->fp) |
876 | if (!current->fp) |
598 | continue; /* skip missing files */ |
877 | continue; /* skip missing files */ |
599 | |
878 | |
600 | clearerr (current->fp); |
879 | clearerr (current->fp); |
601 | |
880 | |
602 | while (lineinput (current) != 0) |
881 | while (lineinput (current)) |
603 | { |
882 | { |
|
|
883 | need_update = 1; |
604 | /* if we're trying to update old partial lines in |
884 | /* if we're trying to update old partial lines in |
605 | * place, and the last time this file was updated the |
885 | * place, and the last time this file was updated the |
606 | * output was partial, and that partial line is not |
886 | * output was partial, and that partial line is not |
607 | * too close to the top of the screen, then update |
887 | * too close to the top of the screen, then update |
608 | * that partial line */ |
888 | * that partial line */ |
609 | if (opt_update && current->lastpartial && current->index >= 3) |
889 | if (opt_update && current->lastpartial && current->index >= 0) |
610 | { |
890 | { |
611 | int old_len = strlen (lines[current->index].line); |
891 | int idx = current->index; |
612 | int new_len = strlen (current->buf); |
892 | append_line (idx, current->buf); |
613 | int space_on_old_line = width / font_width - old_len; //D |
893 | current->index = idx; |
614 | |
894 | free (current->buf), current->buf = 0; |
615 | strncat (lines[current->index].line, current->buf, |
|
|
616 | width - old_len); |
|
|
617 | /* if we can't fit the whole update into the old |
|
|
618 | * partial line then we're going to have to print |
|
|
619 | * the rest of it at the bottom on the screen */ |
|
|
620 | if (new_len > space_on_old_line) |
|
|
621 | { |
|
|
622 | /* strcpy() doesn't like the strings to |
|
|
623 | * overlap in memory, but memmove() doesn't |
|
|
624 | * care */ |
|
|
625 | memmove (current->buf, |
|
|
626 | current->buf + space_on_old_line, |
|
|
627 | new_len - space_on_old_line + 1); |
|
|
628 | } |
|
|
629 | else |
|
|
630 | { |
|
|
631 | need_update = 1; |
|
|
632 | strcpy (current->buf, ""); |
|
|
633 | continue; |
895 | continue; |
634 | } |
|
|
635 | } |
896 | } |
636 | |
897 | |
637 | /* print filename if any, and if last line was from |
898 | /* print filename if any, and if last line was from |
638 | * different file */ |
899 | * different file */ |
639 | if (!opt_nofilename && |
|
|
640 | lastprinted != current && current->desc[0]) |
900 | if (!opt_nofilename && lastprinted != current && current->desc[0]) |
641 | { |
901 | { |
642 | SCROLL_UP (lines, listlen); |
902 | char buf[1024]; /* HACK-5 */ |
643 | sprintf (lines[listlen - 1].line, "[%s]", current->desc); |
903 | snprintf (buf, sizeof (buf), "[%s]", current->desc); |
644 | lines[listlen - 1].color = current->color; |
904 | split_line (listlen - 1, buf, current->color); |
645 | } |
905 | } |
646 | |
906 | |
|
|
907 | /* if we're dealing with partial lines, and the last |
|
|
908 | * time we showed the line it wasn't finished ... */ |
|
|
909 | if (!opt_whole && current->lastpartial) |
|
|
910 | { |
647 | /* if this is the same file we showed last, and the |
911 | /* if this is the same file we showed last then |
648 | * last time we showed it, it wasn't finished, then |
|
|
649 | * append to the last line shown */ |
912 | append to the last line shown */ |
650 | if (lastprinted == current && current->lastpartial) |
913 | if (lastprinted == current) |
651 | { |
914 | append_line (listlen - 1, current->buf); |
652 | int old_len = strlen (lines[listlen - 1].line); |
915 | else |
653 | int new_len = strlen (current->buf); |
916 | { |
654 | strncat (lines[listlen - 1].line, current->buf, |
917 | /* but if a different file has been shown in the |
655 | width - old_len); |
918 | * mean time, make a new line, starting with the |
656 | /* if it doesn't all fit, then put the part that |
919 | * continuation string */ |
657 | * doesn't fit on a new line */ |
920 | split_line (listlen - 1, continuation, current->color); |
658 | if (new_len > width - old_len) |
921 | append_line (listlen - 1, current->buf); |
659 | { |
922 | } |
660 | SCROLL_UP (lines, listlen); |
923 | } |
661 | strcpy (lines[listlen - 1].line, |
|
|
662 | current->buf + width - old_len); |
|
|
663 | } |
|
|
664 | /* show the 'continuation' string because we've got a |
|
|
665 | * continued partial line, but we weren't able to |
|
|
666 | * append it to the old displayed partial line */ |
|
|
667 | } |
|
|
668 | else if (current->lastpartial) |
|
|
669 | { |
|
|
670 | int old_len = strlen (continuation); |
|
|
671 | int new_len = strlen (current->buf); |
|
|
672 | SCROLL_UP (lines, listlen); |
|
|
673 | strcpy (lines[listlen - 1].line, continuation); |
|
|
674 | strncat (lines[listlen - 1].line, current->buf, |
|
|
675 | width - old_len); |
|
|
676 | /* it might not fit, now that we've displayed the |
|
|
677 | * continuation string, so we may need to 'wrap' it */ |
|
|
678 | if (new_len > width - old_len) |
|
|
679 | { |
|
|
680 | SCROLL_UP (lines, listlen); |
|
|
681 | strcpy (lines[listlen - 1].line, |
|
|
682 | current->buf + width - old_len); |
|
|
683 | } |
|
|
684 | } |
|
|
685 | else |
924 | else |
686 | { |
925 | /* otherwise just make a plain and simple new line */ |
687 | SCROLL_UP (lines, listlen); |
926 | split_line (listlen - 1, current->buf, current->color); |
688 | strcpy (lines[listlen - 1].line, current->buf); |
|
|
689 | } |
|
|
690 | |
927 | |
691 | /* we've shown the line now. clear the buffer for the next line */ |
928 | free (current->buf), current->buf = 0; |
692 | strcpy (current->buf, ""); |
|
|
693 | current->index = listlen - 1; |
929 | current->index = listlen - 1; |
694 | lines[listlen - 1].color = current->color; |
|
|
695 | |
|
|
696 | lastprinted = current; |
930 | lastprinted = current; |
697 | need_update = 1; |
|
|
698 | } |
931 | } |
699 | } |
932 | } |
700 | |
933 | |
701 | if (need_update) |
934 | if (need_update) |
|
|
935 | { |
702 | redraw (); |
936 | redraw (0); |
|
|
937 | need_update = 0; |
|
|
938 | } |
703 | else |
939 | else |
704 | { |
940 | { |
705 | XFlush (disp); |
941 | XFlush (disp); |
706 | |
942 | |
707 | if (!XPending (disp)) |
943 | if (!XPending (disp)) |
… | |
… | |
733 | |
969 | |
734 | r.x = xev.xexpose.x; |
970 | r.x = xev.xexpose.x; |
735 | r.y = xev.xexpose.y; |
971 | r.y = xev.xexpose.y; |
736 | r.width = xev.xexpose.width; |
972 | r.width = xev.xexpose.width; |
737 | r.height = xev.xexpose.height; |
973 | r.height = xev.xexpose.height; |
|
|
974 | |
738 | XUnionRectWithRegion (&r, region, region); |
975 | XUnionRectWithRegion (&r, region, region); |
739 | if (miny > r.y) |
|
|
740 | miny = r.y; |
|
|
741 | if (maxy < r.y + r.height) |
|
|
742 | maxy = r.y + r.height; |
|
|
743 | } |
976 | } |
744 | break; |
977 | break; |
745 | default: |
978 | default: |
746 | #ifdef DEBUGMODE |
979 | #ifdef DEBUGMODE |
747 | fprintf (stderr, "PANIC! Unknown event %d\n", xev.type); |
980 | fprintf (stderr, "PANIC! Unknown event %d\n", xev.type); |
… | |
… | |
760 | lastreload = time (NULL); |
993 | lastreload = time (NULL); |
761 | } |
994 | } |
762 | |
995 | |
763 | if (!XEmptyRegion (region)) |
996 | if (!XEmptyRegion (region)) |
764 | { |
997 | { |
|
|
998 | XRectangle r; |
|
|
999 | |
765 | XSetRegion (disp, WinGC, region); |
1000 | XSetRegion (disp, WinGC, region); |
|
|
1001 | XClipBox (region, &r); |
766 | |
1002 | |
767 | refresh (lines, miny, maxy); |
1003 | refresh (r.y, r.y + r.height, 0, 1); |
|
|
1004 | |
768 | XDestroyRegion (region); |
1005 | XDestroyRegion (region); |
769 | region = XCreateRegion (); |
1006 | region = XCreateRegion (); |
770 | maxy = 0; |
|
|
771 | miny = win_y + height; |
|
|
772 | } |
1007 | } |
773 | } |
1008 | } |
774 | } |
1009 | } |
775 | |
1010 | |
776 | |
1011 | |
… | |
… | |
814 | continuation = argv[++i]; |
1049 | continuation = argv[++i]; |
815 | else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) |
1050 | else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) |
816 | fontname = argv[++i]; |
1051 | fontname = argv[++i]; |
817 | #if HAS_REGEX |
1052 | #if HAS_REGEX |
818 | else if (!strcmp (arg, "-t")) |
1053 | else if (!strcmp (arg, "-t")) |
|
|
1054 | { |
819 | transform = argv[++i]; |
1055 | transform = argv[++i]; |
|
|
1056 | transform_to = argv[++i]; |
|
|
1057 | printf("transform: '%s' to '%s'\n", transform, transform_to); |
|
|
1058 | } |
820 | #endif |
1059 | #endif |
821 | else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) |
1060 | else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) |
822 | opt_daemonize = 1; |
1061 | opt_daemonize = 1; |
823 | else if (!strcmp (arg, "-reload")) |
1062 | else if (!strcmp (arg, "-reload")) |
824 | { |
1063 | { |
825 | reload = atoi (argv[++i]); |
1064 | reload = atoi (argv[++i]); |
826 | command = argv[++i]; |
1065 | command = argv[++i]; |
827 | } |
1066 | } |
828 | else if (!strcmp (arg, "-shade")) |
1067 | else if (!strcmp (arg, "-shade")) |
829 | opt_shade = 1; |
1068 | opt_shade = 1; |
|
|
1069 | else if (!strcmp (arg, "-outline")) |
|
|
1070 | opt_outline = 1; |
|
|
1071 | else if (!strcmp (arg, "-noflicker")) |
|
|
1072 | opt_noflicker = 1; |
830 | else if (!strcmp (arg, "-frame")) |
1073 | else if (!strcmp (arg, "-frame")) |
831 | opt_frame = 1; |
1074 | opt_frame = 1; |
832 | else if (!strcmp (arg, "-no-filename")) |
1075 | else if (!strcmp (arg, "-no-filename")) |
833 | opt_nofilename = 1; |
1076 | opt_nofilename = 1; |
834 | else if (!strcmp (arg, "-reverse")) |
1077 | else if (!strcmp (arg, "-reverse")) |
… | |
… | |
837 | opt_whole = 1; |
1080 | opt_whole = 1; |
838 | else if (!strcmp (arg, "-partial")) |
1081 | else if (!strcmp (arg, "-partial")) |
839 | opt_partial = 1; |
1082 | opt_partial = 1; |
840 | else if (!strcmp (arg, "-update")) |
1083 | else if (!strcmp (arg, "-update")) |
841 | opt_update = opt_partial = 1; |
1084 | opt_update = opt_partial = 1; |
|
|
1085 | else if (!strcmp (arg, "-wordwrap")) |
|
|
1086 | opt_wordwrap = 1; |
842 | else if (!strcmp (arg, "-color")) |
1087 | else if (!strcmp (arg, "-color")) |
843 | def_color = argv[++i]; |
1088 | def_color = argv[++i]; |
844 | else if (!strcmp (arg, "-noinitial")) |
1089 | else if (!strcmp (arg, "-noinitial")) |
845 | opt_noinitial = 1; |
1090 | opt_noinitial = 1; |
846 | else if (!strcmp (arg, "-id")) |
1091 | else if (!strcmp (arg, "-id")) |
… | |
… | |
880 | desc = p + 1; |
1125 | desc = p + 1; |
881 | } |
1126 | } |
882 | } |
1127 | } |
883 | |
1128 | |
884 | e = xmalloc (sizeof (struct logfile_entry)); |
1129 | e = xmalloc (sizeof (struct logfile_entry)); |
|
|
1130 | e->partial = 0; |
|
|
1131 | e->buf = 0; |
|
|
1132 | e->index = -1; |
|
|
1133 | |
885 | if (arg[0] == '-' && arg[1] == '\0') |
1134 | if (arg[0] == '-' && arg[1] == '\0') |
886 | { |
1135 | { |
887 | if ((e->fp = fdopen (0, "r")) == NULL) |
1136 | if ((e->fp = fdopen (0, "r")) == NULL) |
888 | perror ("fdopen"), exit (1); |
1137 | perror ("fdopen"), exit (1); |
889 | if (fcntl (0, F_SETFL, O_NONBLOCK) < 0) |
1138 | if (fcntl (0, F_SETFL, O_NONBLOCK) < 0) |
… | |
… | |
899 | e->fname = xstrdup (fname); |
1148 | e->fname = xstrdup (fname); |
900 | if (openlog (e) == NULL) |
1149 | if (openlog (e) == NULL) |
901 | perror (fname), exit (1); |
1150 | perror (fname), exit (1); |
902 | |
1151 | |
903 | l = strlen (desc); |
1152 | l = strlen (desc); |
|
|
1153 | /* HACK-6: width is in pixels now */ |
904 | if (l > width - 2) /* must account for [ ] */ |
1154 | if (l > width - 2) /* must account for [ ] */ |
905 | l = width - 2; |
1155 | l = width - 2; |
906 | e->desc = xmalloc (l + 1); |
1156 | e->desc = xmalloc (l + 1); |
907 | memcpy (e->desc, desc, l); |
1157 | memcpy (e->desc, desc, l); |
908 | *(e->desc + l) = '\0'; |
1158 | *(e->desc + l) = '\0'; |
909 | } |
1159 | } |
910 | |
1160 | |
911 | e->color = GetColor (fcolor); |
1161 | e->color = GetColor (fcolor); |
912 | e->buf = xmalloc (width + 2); |
|
|
913 | e->partial = 0; |
1162 | e->partial = 0; |
914 | e->buf[0] = '\0'; |
|
|
915 | e->next = NULL; |
1163 | e->next = NULL; |
916 | |
1164 | |
917 | if (!loglist) |
1165 | if (!loglist) |
918 | loglist = e; |
1166 | loglist = e; |
919 | if (loglist_tail) |
1167 | if (loglist_tail) |
… | |
… | |
933 | { |
1181 | { |
934 | fprintf (stderr, "Specify at most one of -partial and -whole\n"); |
1182 | fprintf (stderr, "Specify at most one of -partial and -whole\n"); |
935 | exit (1); |
1183 | exit (1); |
936 | } |
1184 | } |
937 | |
1185 | |
|
|
1186 | /* HACK-7: do we want to allow both -shade and -outline? */ |
|
|
1187 | if (opt_shade && opt_outline) |
|
|
1188 | { |
|
|
1189 | fprintf (stderr, "Specify at most one of -shade and -outline\n"); |
|
|
1190 | exit (1); |
|
|
1191 | } |
|
|
1192 | |
938 | if (opt_partial) |
1193 | if (opt_partial) |
939 | /* if we specifically requested to see partial lines then don't insist on whole lines */ |
1194 | /* if we specifically requested to see partial lines then don't insist on whole lines */ |
940 | opt_whole = 0; |
1195 | opt_whole = 0; |
941 | else if (file_count > 1) |
1196 | else if (file_count > 1) |
942 | /* otherwise, if we've viewing multiple files, default to showing whole lines */ |
1197 | /* otherwise, if we're viewing multiple files, default to showing whole lines */ |
943 | opt_whole = 1; |
1198 | opt_whole = 1; |
944 | |
1199 | |
945 | #if HAS_REGEX |
1200 | #if HAS_REGEX |
946 | if (transform) |
1201 | if (transform) |
947 | { |
1202 | { |
948 | int i; |
1203 | int i; |
949 | |
1204 | |
|
|
1205 | printf("compiling regexp '%s'\n", transform); |
950 | transformre = xmalloc (sizeof (transformre)); |
1206 | transformre = xmalloc (sizeof (regex_t)); |
951 | i = regcomp (&transformre, transform, REG_EXTENDED); |
1207 | i = regcomp (transformre, transform, REG_EXTENDED); |
952 | if (i != 0) |
1208 | if (i != 0) |
953 | { |
1209 | { |
954 | char buf[512]; |
1210 | char buf[512]; |
955 | |
1211 | |
956 | regerror (i, &transformre, buf, sizeof (buf)); |
1212 | regerror (i, transformre, buf, sizeof (buf)); |
957 | fprintf (stderr, "Cannot compile regular expression: %s\n", buf); |
1213 | fprintf (stderr, "Cannot compile regular expression: %s\n", buf); |
958 | } |
1214 | } |
|
|
1215 | else |
|
|
1216 | { |
|
|
1217 | printf("compiled '%s' OK to %x\n", transform, (int)transformre); |
|
|
1218 | } |
959 | } |
1219 | } |
960 | #endif |
1220 | #endif |
961 | |
1221 | |
962 | InitWindow (); |
1222 | InitWindow (); |
963 | |
1223 | |
… | |
… | |
1011 | while ((p = malloc (size)) == NULL) |
1271 | while ((p = malloc (size)) == NULL) |
1012 | { |
1272 | { |
1013 | fprintf (stderr, "Memory exausted."); |
1273 | fprintf (stderr, "Memory exausted."); |
1014 | sleep (10); |
1274 | sleep (10); |
1015 | } |
1275 | } |
|
|
1276 | |
|
|
1277 | return p; |
|
|
1278 | } |
|
|
1279 | |
|
|
1280 | void * |
|
|
1281 | xrealloc (void *ptr, size_t size) |
|
|
1282 | { |
|
|
1283 | void *p; |
|
|
1284 | |
|
|
1285 | while ((p = realloc (ptr, size)) == NULL) |
|
|
1286 | { |
|
|
1287 | fprintf (stderr, "Memory exausted."); |
|
|
1288 | sleep (10); |
|
|
1289 | } |
|
|
1290 | |
1016 | return p; |
1291 | return p; |
1017 | } |
1292 | } |
1018 | |
1293 | |
1019 | void |
1294 | void |
1020 | display_help (char *myname) |
1295 | display_help (char *myname) |
… | |
… | |
1030 | " -reverse print new lines at the top\n" |
1305 | " -reverse print new lines at the top\n" |
1031 | " -whole wait for \\n before showing a line\n" |
1306 | " -whole wait for \\n before showing a line\n" |
1032 | " -partial show lines even if they don't end with a \\n\n" |
1307 | " -partial show lines even if they don't end with a \\n\n" |
1033 | " -update allow updates to old partial lines\n" |
1308 | " -update allow updates to old partial lines\n" |
1034 | " -cont string to prefix continued partial lines with\n" |
1309 | " -cont string to prefix continued partial lines with\n" |
|
|
1310 | " -wordwrap wrap long lines at spaces to avoid breaking words\n" |
1035 | " defaults to \"[+]\"\n" |
1311 | " defaults to \"[+]\"\n" |
1036 | " -shade add shading to font\n" |
1312 | " -shade add shading to font\n" |
1037 | " -noinitial don't display the last file lines on\n" |
1313 | " -noinitial don't display the last file lines on\n" |
1038 | " startup\n" |
1314 | " startup\n" |
1039 | " -i | -interval seconds interval between checks (fractional\n" |
1315 | " -i | -interval seconds interval between checks (fractional\n" |
1040 | " values o.k.). Default 3 seconds\n" |
1316 | " values o.k.). Default 2.4 seconds\n" |
1041 | " -V display version information and exit\n" |
1317 | " -V display version information and exit\n" |
1042 | "\n"); |
1318 | "\n"); |
1043 | printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green " |
1319 | printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green " |
1044 | "/var/log/secure,red,'ALERT'\n", myname); |
1320 | "/var/log/secure,red,'ALERT'\n", myname); |
1045 | exit (0); |
1321 | exit (0); |
… | |
… | |
1053 | } |
1329 | } |
1054 | |
1330 | |
1055 | int |
1331 | int |
1056 | daemonize (void) |
1332 | daemonize (void) |
1057 | { |
1333 | { |
|
|
1334 | pid_t pid; |
|
|
1335 | |
1058 | switch (fork ()) |
1336 | switch (pid = fork ()) |
1059 | { |
1337 | { |
1060 | case -1: |
1338 | case -1: |
1061 | return -1; |
1339 | return -1; |
1062 | case 0: |
1340 | case 0: |
1063 | break; |
1341 | break; |
1064 | default: |
1342 | default: |
|
|
1343 | /*printf("%d\n", pid);*/ |
1065 | _exit (0); |
1344 | exit (0); |
1066 | } |
1345 | } |
1067 | |
1346 | |
1068 | if (setsid () == -1) |
1347 | if (setsid () == -1) |
1069 | return -1; |
1348 | return -1; |
1070 | |
1349 | |