ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
(Generate patch)

Comparing root-tail/root-tail.c (file contents):
Revision 1.12 by pcg, Sat Mar 27 00:52:52 2004 UTC vs.
Revision 1.41 by chris_moore, Wed Mar 31 16:42:22 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines