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.22 by pcg, Sat Mar 27 21:12:22 2004 UTC vs.
Revision 1.47 by pcg, Thu Apr 1 13:37:30 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_ascent, 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 = { 2, 400000 }; 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_outline, opt_noflicker, 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, 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{
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 XSetClipMask (disp, WinGC, None); 377 XSetClipMask (disp, WinGC, None);
321 refresh (0, 32768, 1); 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, int clear) 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
336 XClearArea (disp, root, win_x, win_y, width, height, False); 393 XClearArea (disp, root, win_x, win_y, width, height, False);
337 394
338 for (lin = listlen; lin--;) 395 for (lin = listlen; lin--;)
339 { 396 {
340 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;
341 399
342 offset -= font_height; 400 offset -= font_height;
343 401
344 if (offset < miny || offset > maxy) 402 if (offset < miny || offset > maxy)
345 continue; 403 continue;
346 404
405 /* if this line is a different than it was, then it
406 * needs displaying */
347 if (clear && opt_noflicker) 407 if (!opt_noflicker
348 XClearArea (disp, root, win_x, win_y + offset - font_ascent, width, font_height, False); 408 || refresh_all
349 409 || display_line->len != line->len
350 if (opt_outline) 410 || display_line->color != line->color
411 || memcmp (display_line->line, line->line, line->len))
351 { 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;
352 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)
353 XmbDrawString (disp, root, fontset, WinGC, win_x - 1, 442 XmbDrawString (disp, root, fontset, WinGC,
443 win_x + effect_x_offset + x,
444 win_y + y + offset,
354 win_y + offset + 1, line->line, line->len); 445 line->line, line->len);
355 XmbDrawString (disp, root, fontset, WinGC, win_x + 1,
356 win_y + offset + 1, line->line, line->len);
357 XmbDrawString (disp, root, fontset, WinGC, win_x - 1,
358 win_y + offset - 1, line->line, line->len);
359 XmbDrawString (disp, root, fontset, WinGC, win_x + 1,
360 win_y + offset - 1, line->line, line->len);
361 } 446 }
362 else if (opt_shade) 447 else if (opt_shade)
363 { 448 {
364 XSetForeground (disp, WinGC, black_color); 449 XSetForeground (disp, WinGC, black_color);
365 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,
366 win_y + offset + 2, line->line, line->len); 453 line->line, line->len);
367 } 454 }
368 455
369 XSetForeground (disp, WinGC, line->color); 456 XSetForeground (disp, WinGC, line->color);
370 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,
371 line->line, line->len); 460 line->line, line->len);
461 }
372 } 462 }
373 463
374 if (opt_frame) 464 if (opt_frame)
375 { 465 {
376 XSetForeground (disp, WinGC, GetColor (def_color)); 466 XSetForeground (disp, WinGC, GetColor (def_color));
377 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);
378 } 468 }
379} 469}
380 470
381#if HAS_REGEX 471#if HAS_REGEX
382void 472void
395 if (transformre) 485 if (transformre)
396 { 486 {
397 int i; 487 int i;
398 regmatch_t matched[16]; 488 regmatch_t matched[16];
399 489
400 i = regexec (&transformre, string, 16, matched, 0); 490 i = regexec (transformre, s, 16, matched, 0);
401 if (i == 0) 491 if (i == 0)
402 { /* 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';
403 } 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 }
404 } 521 }
405} 522}
406#endif 523#endif
407 524
408char * 525char *
418 535
419 return r; 536 return r;
420} 537}
421 538
422/* 539/*
423 * 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.
424 */ 541 */
425int 542int
426lineinput (struct logfile_entry *logfile) 543lineinput (struct logfile_entry *logfile)
427{ 544{
428 char buff[1024], *p = buff; 545 char buff[1024], *p;
429 int ch; 546 int ch;
547 /* HACK-2: add on the length of any partial line which we will be appending to */
430 int ofs = logfile->buf ? strlen (logfile->buf) : 0; 548 int ofs = logfile->buf ? strlen (logfile->buf) : 0;
431 549
550 /* this loop ensures that the whole line is read, even if it's
551 * longer than the buffer. we need to do this because when --whole
552 * is in effect we don't know whether to display the line or not
553 * until we've seen how (ie. whether) it ends */
432 do 554 do
433 { 555 {
556 p = buff;
557 do
558 {
434 ch = fgetc (logfile->fp); 559 ch = fgetc (logfile->fp);
435 560
436 if (ch == '\n' || ch == EOF) 561 if (ch == '\n' || ch == EOF)
437 break; 562 break;
438 else if (ch == '\r') 563 else if (ch == '\r')
439 continue; /* skip */ 564 continue; /* skip */
440 else if (ch == '\t') 565 else if (ch == '\t')
441 { 566 {
442 do 567 do
443 { 568 {
444 *p++ = ' '; 569 *p++ = ' ';
445 ofs++; 570 ofs++;
446 } 571 }
447 while (ofs & 7); 572 while (ofs & 7);
448 }
449 else
450 {
451 *p++ = ch;
452 ofs++;
453 }
454 } 573 }
574 else
575 {
576 *p++ = ch;
577 ofs++;
578 }
579 }
455 while (p < buff + (sizeof buff) - 8 - 1); 580 while (p < buff + (sizeof buff) - 8 - 1);
456 581
457 if (p == buff && ch == EOF) 582 if (p == buff && ch == EOF)
458 return 0; 583 return 0;
459 584
460 *p = 0; 585 *p = 0;
461 586
462 p = concat_line (logfile->buf, buff); 587 p = concat_line (logfile->buf, buff);
463 free (logfile->buf); logfile->buf = p; 588 free (logfile->buf); logfile->buf = p;
589 }
590 while (ch != '\n' && ch != EOF);
464 591
465 logfile->lastpartial = logfile->partial; 592 logfile->lastpartial = logfile->partial;
593 /* there are 3 ways we could have exited the loop: reading '\n',
594 * reaching EOF, or filling the buffer; the 2nd and 3rd of these
595 * both result in a partial line */
466 logfile->partial = ch == EOF; 596 logfile->partial = ch != '\n';
467 597
468 if (logfile->partial && opt_whole) 598 if (logfile->partial && opt_whole)
469 return 0; 599 return 0;
470 600
471#if HAS_REGEX 601#if HAS_REGEX
543 { /* file missing? */ 673 { /* file missing? */
544 sleep (1); 674 sleep (1);
545 if (e->fp) 675 if (e->fp)
546 fclose (e->fp); 676 fclose (e->fp);
547 if (openlog (e) == NULL) 677 if (openlog (e) == NULL)
548 break; 678 continue;
679 if (fstat (fileno (e->fp), &stats) < 0)
680 continue;
549 } 681 }
550 682
551 if (stats.st_ino != e->inode) 683 if (stats.st_ino != e->inode)
552 { /* file renamed? */ 684 { /* file renamed? */
553 if (e->fp) 685 if (e->fp)
554 fclose (e->fp); 686 fclose (e->fp);
555 if (openlog (e) == NULL) 687 if (openlog (e) == NULL)
556 break; 688 continue;
689 if (fstat (fileno (e->fp), &stats) < 0)
690 continue;
557 } 691 }
558 692
559 if (stats.st_size < e->last_size) 693 if (stats.st_size < e->last_size)
560 { /* file truncated? */ 694 { /* file truncated? */
561 fseek (e->fp, 0, SEEK_SET); 695 fseek (e->fp, 0, SEEK_SET);
596 struct logfile_entry *current; 730 struct logfile_entry *current;
597 731
598 for (cur_line = idx; cur_line > 0; cur_line--) 732 for (cur_line = idx; cur_line > 0; cur_line--)
599 lines[cur_line] = lines[cur_line - 1]; 733 lines[cur_line] = lines[cur_line - 1];
600 734
601 lines[0].line = strdup ("~"); 735 lines[0].line = xstrdup ("~");
602 736
603 for (current = loglist; current; current = current->next) 737 for (current = loglist; current; current = current->next)
604 if (current->index >= 0 && current->index <= idx) 738 if (current->index >= 0 && current->index <= idx)
605 current->index++; 739 current->index++;
606} 740}
607 741
608/* 742/*
609 * takes a logical log file line and split it into multiple physical 743 * takes a logical log file line and splits it into multiple physical
610 * screen lines by splitting it whenever a part becomes too long. 744 * screen lines by splitting it whenever a part becomes too long.
611 * lal lines will be inserted at position "idx". 745 * lal lines will be inserted at position "idx".
612 */ 746 */
613static void 747static void
614split_line (int idx, const char *str, unsigned long color) 748split_line (int idx, const char *str, unsigned long color)
615{ 749{
616 int l = strlen (str); 750 int l = strlen (str);
751 int last_wrapped = 0;
617 const char *p = str; 752 const char *p = str;
753 static int continuation_width = -1;
754 static int continuation_length;
755
756 /* only calculate the continuation's width once */
757 if (continuation_width == -1)
758 {
759 continuation_length = strlen (continuation);
760 continuation_width = XmbTextEscapement (fontset, continuation, continuation_length);
761 }
618 762
619 do 763 do
620 { 764 {
621 const char *beg = p; 765 const char *beg = p;
766 int w = last_wrapped ? continuation_width : 0;
622 int w = 0; 767 int wrapped = 0;
768 const char *break_p = NULL;
623 769
624 while (*p) 770 while (*p)
625 { 771 {
772 int cw, len;
773
774 /* find the length in bytes of the next multibyte character */
626 int len = mblen (p, l); 775 len = mblen (p, l);
627 if (len <= 0) 776 if (len <= 0)
628 len = 1; /* ignore (don't skip) ilegal character sequences */ 777 len = 1; /* ignore (don't skip) illegal character sequences */
629 778
779 /* find the width in pixels of the next character */
630 int cw = XmbTextEscapement (fontset, p, len); 780 cw = XmbTextEscapement (fontset, p, len);
631 if (cw + w >= width) 781 if (cw + w > width - effect_x_space)
782 {
783 if (p == beg)
784 {
785 fprintf (stderr, "we can't even fit a single character onto the line\n");
786 if (len == 1) fprintf (stderr, "(the character we couldn't fit was '%c')\n", *p);
787 exit (1);
788 }
789
790 wrapped = 1;
632 break; 791 break;
792 }
793
794 if (opt_wordwrap && len == 1 && p[0] == ' ')
795 break_p = p;
633 796
634 w += cw; 797 w += cw;
635 p += len; 798 p += len;
636 l -= len; 799 l -= len;
637 } 800 }
638 801
802 /* if we're wrapping at spaces, and the line is long enough to
803 * wrap, and we've seen a space already, and the space wasn't
804 * the first character on the line, then wrap at the space */
805 if (opt_wordwrap && wrapped && break_p && break_p != beg)
806 {
807 l += p - break_p;
808 p = break_p;
809 }
810
639 { 811 {
812 int len = p - beg + (last_wrapped ? continuation_length : 0);
640 char *s = xmalloc (p - beg + 1); 813 char *s = xmalloc (len + 1);
641 memcpy (s, beg, p - beg); 814 if (last_wrapped)
815 {
816 memcpy (s, continuation, continuation_length);
817 memcpy (s + continuation_length, beg, p - beg);
818 }
819 else
820 memcpy (s, beg, len);
821
642 s[p - beg] = 0; 822 s[len] = 0;
643 insert_line (idx); 823 insert_line (idx);
644 lines[idx].line = s; 824 lines[idx].line = s;
645 lines[idx].len = p - beg; 825 lines[idx].len = len;
646 lines[idx].color = color; 826 lines[idx].color = color;
647 } 827 }
828
829 /* if we wrapped at a space, don't display the space */
830 if (opt_wordwrap && wrapped && break_p && break_p != beg)
831 {
832 l--;
833 p++;
834 }
835
836 last_wrapped = wrapped;
648 } 837 }
649 while (l); 838 while (l);
650} 839}
651 840
652/* 841/*
668} 857}
669 858
670static void 859static void
671main_loop (void) 860main_loop (void)
672{ 861{
673 lines = xmalloc (sizeof (struct linematrix) * listlen);
674 int lin; 862 int lin;
675 time_t lastreload; 863 time_t lastreload;
676 Region region = XCreateRegion (); 864 Region region = XCreateRegion ();
677 XEvent xev; 865 XEvent xev;
678 struct logfile_entry *lastprinted = NULL; 866 struct logfile_entry *lastprinted = NULL;
679 struct logfile_entry *current; 867 struct logfile_entry *current;
680 int need_update = 1; 868 int need_update = 1;
681 869
870 lines = xmalloc (sizeof (struct linematrix) * listlen);
871 display = xmalloc (sizeof (struct displaymatrix) * listlen);
872
682 lastreload = time (NULL); 873 lastreload = time (NULL);
683 874
684 /* Initialize linematrix */ 875 /* Initialize linematrix */
685 for (lin = 0; lin < listlen; lin++) 876 for (lin = 0; lin < listlen; lin++)
686 { 877 {
687 lines[lin].line = strdup ("~"); 878 lines[lin].line = xstrdup ("~");
688 lines[lin].len = 1; 879 lines[lin].len = 1;
880 display[lin].line = xstrdup("");
881 display[lin].len = 0;
882 display[lin].buffer_size = 0;
689 lines[lin].color = GetColor (def_color); 883 lines[lin].color = GetColor (def_color);
690 } 884 }
691 885
692 for (;;) 886 for (;;)
693 { 887 {
718 912
719 /* print filename if any, and if last line was from 913 /* print filename if any, and if last line was from
720 * different file */ 914 * different file */
721 if (!opt_nofilename && lastprinted != current && current->desc[0]) 915 if (!opt_nofilename && lastprinted != current && current->desc[0])
722 { 916 {
723 char buf[1024]; /* HACK */
724 snprintf (buf, sizeof (buf), "[%s]", current->desc);
725 split_line (listlen - 1, buf, current->color); 917 split_line (listlen - 1, "[", current->color);
918 append_line (listlen - 1, current->desc);
919 append_line (listlen - 1, "]");
726 } 920 }
727 921
922 /* if we're dealing with partial lines, and the last
923 * time we showed the line it wasn't finished ... */
924 if (!opt_whole && current->lastpartial)
925 {
728 /* if this is the same file we showed last, and the 926 /* if this is the same file we showed last then
729 * last time we showed it, it wasn't finished, then
730 * append to the last line shown */ 927 append to the last line shown */
731 if (lastprinted == current && current->lastpartial) 928 if (lastprinted == current)
732 { 929 append_line (listlen - 1, current->buf);
930 else
931 {
932 /* but if a different file has been shown in the
933 * mean time, make a new line, starting with the
934 * continuation string */
935 split_line (listlen - 1, continuation, current->color);
733 append_line (listlen - 1, current->buf); 936 append_line (listlen - 1, current->buf);
734 free (current->buf), current->buf = 0; 937 }
735 continue; 938 }
736 }
737 else 939 else
738 { 940 /* otherwise just make a plain and simple new line */
739 split_line (listlen - 1, current->buf, current->color); 941 split_line (listlen - 1, current->buf, current->color);
942
740 free (current->buf), current->buf = 0; 943 free (current->buf), current->buf = 0;
741 }
742
743 current->index = listlen - 1; 944 current->index = listlen - 1;
744 lastprinted = current; 945 lastprinted = current;
745 } 946 }
746 } 947 }
747 948
748 if (need_update) 949 if (need_update)
749 { 950 {
750 redraw (); 951 redraw (0);
751 need_update = 0; 952 need_update = 0;
752 } 953 }
753 else 954 else
754 { 955 {
755 XFlush (disp); 956 XFlush (disp);
812 XRectangle r; 1013 XRectangle r;
813 1014
814 XSetRegion (disp, WinGC, region); 1015 XSetRegion (disp, WinGC, region);
815 XClipBox (region, &r); 1016 XClipBox (region, &r);
816 1017
817 refresh (r.y, r.y + r.height, 0); 1018 refresh (r.y, r.y + r.height, 0, 1);
818 1019
819 XDestroyRegion (region); 1020 XDestroyRegion (region);
820 region = XCreateRegion (); 1021 region = XCreateRegion ();
821 } 1022 }
822 } 1023 }
863 continuation = argv[++i]; 1064 continuation = argv[++i];
864 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) 1065 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
865 fontname = argv[++i]; 1066 fontname = argv[++i];
866#if HAS_REGEX 1067#if HAS_REGEX
867 else if (!strcmp (arg, "-t")) 1068 else if (!strcmp (arg, "-t"))
1069 {
868 transform = argv[++i]; 1070 transform = argv[++i];
1071 transform_to = argv[++i];
1072 printf("transform: '%s' to '%s'\n", transform, transform_to);
1073 }
869#endif 1074#endif
870 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) 1075 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
871 opt_daemonize = 1; 1076 opt_daemonize = 1;
872 else if (!strcmp (arg, "-reload")) 1077 else if (!strcmp (arg, "-reload"))
873 { 1078 {
890 opt_whole = 1; 1095 opt_whole = 1;
891 else if (!strcmp (arg, "-partial")) 1096 else if (!strcmp (arg, "-partial"))
892 opt_partial = 1; 1097 opt_partial = 1;
893 else if (!strcmp (arg, "-update")) 1098 else if (!strcmp (arg, "-update"))
894 opt_update = opt_partial = 1; 1099 opt_update = opt_partial = 1;
1100 else if (!strcmp (arg, "-wordwrap"))
1101 opt_wordwrap = 1;
895 else if (!strcmp (arg, "-color")) 1102 else if (!strcmp (arg, "-color"))
896 def_color = argv[++i]; 1103 def_color = argv[++i];
897 else if (!strcmp (arg, "-noinitial")) 1104 else if (!strcmp (arg, "-noinitial"))
898 opt_noinitial = 1; 1105 opt_noinitial = 1;
899 else if (!strcmp (arg, "-id")) 1106 else if (!strcmp (arg, "-id"))
943 { 1150 {
944 if ((e->fp = fdopen (0, "r")) == NULL) 1151 if ((e->fp = fdopen (0, "r")) == NULL)
945 perror ("fdopen"), exit (1); 1152 perror ("fdopen"), exit (1);
946 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0) 1153 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
947 perror ("fcntl"), exit (1); 1154 perror ("fcntl"), exit (1);
1155
948 e->fname = NULL; 1156 e->fname = NULL;
949 e->inode = 0; 1157 e->inode = 0;
950 e->desc = xstrdup ("stdin"); 1158 e->desc = xstrdup ("stdin");
951 } 1159 }
952 else 1160 else
953 { 1161 {
954 int l;
955
956 e->fname = xstrdup (fname); 1162 e->fname = xstrdup (fname);
1163
957 if (openlog (e) == NULL) 1164 if (openlog (e) == NULL)
958 perror (fname), exit (1); 1165 perror (fname), exit (1);
959 1166
960 l = strlen (desc); 1167 e->desc = xstrdup (desc);
961 if (l > width - 2) /* must account for [ ] */
962 l = width - 2;
963 e->desc = xmalloc (l + 1);
964 memcpy (e->desc, desc, l);
965 *(e->desc + l) = '\0';
966 } 1168 }
967 1169
968 e->color = GetColor (fcolor); 1170 e->color = GetColor (fcolor);
969 e->partial = 0; 1171 e->partial = 0;
970 e->next = NULL; 1172 e->next = NULL;
971 1173
972 if (!loglist) 1174 if (!loglist)
973 loglist = e; 1175 loglist = e;
974 if (loglist_tail) 1176 if (loglist_tail)
975 loglist_tail->next = e; 1177 loglist_tail->next = e;
1178
976 loglist_tail = e; 1179 loglist_tail = e;
977 } 1180 }
978 } 1181 }
979 1182
980 if (!loglist) 1183 if (!loglist)
982 fprintf (stderr, "You did not specify any files to tail\n" 1185 fprintf (stderr, "You did not specify any files to tail\n"
983 "use %s --help for help\n", argv[0]); 1186 "use %s --help for help\n", argv[0]);
984 exit (1); 1187 exit (1);
985 } 1188 }
986 1189
1190 if (opt_update && opt_whole)
1191 {
1192 fprintf (stderr, "Specify at most one of -update and -whole\n");
1193 exit (1);
1194 }
987 if (opt_partial && opt_whole) 1195 else if (opt_partial && opt_whole)
988 { 1196 {
989 fprintf (stderr, "Specify at most one of -partial and -whole\n"); 1197 fprintf (stderr, "Specify at most one of -partial and -whole\n");
1198 exit (1);
1199 }
1200
1201 /* HACK-7: do we want to allow both -shade and -outline? */
1202 if (opt_shade && opt_outline)
1203 {
1204 fprintf (stderr, "Specify at most one of -shade and -outline\n");
990 exit (1); 1205 exit (1);
991 } 1206 }
992 1207
993 if (opt_partial) 1208 if (opt_partial)
994 /* if we specifically requested to see partial lines then don't insist on whole lines */ 1209 /* if we specifically requested to see partial lines then don't insist on whole lines */
995 opt_whole = 0; 1210 opt_whole = 0;
996 else if (file_count > 1) 1211 else if (file_count > 1)
997 /* otherwise, if we've viewing multiple files, default to showing whole lines */ 1212 /* otherwise, if we're viewing multiple files, default to showing whole lines */
998 opt_whole = 1; 1213 opt_whole = 1;
999 1214
1000#if HAS_REGEX 1215#if HAS_REGEX
1001 if (transform) 1216 if (transform)
1002 { 1217 {
1003 int i; 1218 int i;
1004 1219
1220 printf("compiling regexp '%s'\n", transform);
1005 transformre = xmalloc (sizeof (transformre)); 1221 transformre = xmalloc (sizeof (regex_t));
1006 i = regcomp (&transformre, transform, REG_EXTENDED); 1222 i = regcomp (transformre, transform, REG_EXTENDED);
1007 if (i != 0) 1223 if (i != 0)
1008 { 1224 {
1009 char buf[512]; 1225 char buf[512];
1010 1226
1011 regerror (i, &transformre, buf, sizeof (buf)); 1227 regerror (i, transformre, buf, sizeof (buf));
1012 fprintf (stderr, "Cannot compile regular expression: %s\n", buf); 1228 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1013 } 1229 }
1230 else
1231 {
1232 printf("compiled '%s' OK to %x\n", transform, (int)transformre);
1233 }
1014 } 1234 }
1015#endif 1235#endif
1016 1236
1017 InitWindow (); 1237 InitWindow ();
1018 1238
1049{ 1269{
1050 void *p; 1270 void *p;
1051 1271
1052 while ((p = strdup (string)) == NULL) 1272 while ((p = strdup (string)) == NULL)
1053 { 1273 {
1054 fprintf (stderr, "Memory exausted."); 1274 fprintf (stderr, "Memory exhausted in xstrdup().\n");
1055 sleep (10); 1275 sleep (10);
1056 } 1276 }
1057 1277
1058 return p; 1278 return p;
1059} 1279}
1063{ 1283{
1064 void *p; 1284 void *p;
1065 1285
1066 while ((p = malloc (size)) == NULL) 1286 while ((p = malloc (size)) == NULL)
1067 { 1287 {
1068 fprintf (stderr, "Memory exausted."); 1288 fprintf (stderr, "Memory exhausted in xmalloc().\n");
1289 sleep (10);
1290 }
1291
1292 return p;
1293}
1294
1295void *
1296xrealloc (void *ptr, size_t size)
1297{
1298 void *p;
1299
1300 while ((p = realloc (ptr, size)) == NULL)
1301 {
1302 fprintf (stderr, "Memory exhausted in xrealloc().\n");
1069 sleep (10); 1303 sleep (10);
1070 } 1304 }
1071 1305
1072 return p; 1306 return p;
1073} 1307}
1086 " -reverse print new lines at the top\n" 1320 " -reverse print new lines at the top\n"
1087 " -whole wait for \\n before showing a line\n" 1321 " -whole wait for \\n before showing a line\n"
1088 " -partial show lines even if they don't end with a \\n\n" 1322 " -partial show lines even if they don't end with a \\n\n"
1089 " -update allow updates to old partial lines\n" 1323 " -update allow updates to old partial lines\n"
1090 " -cont string to prefix continued partial lines with\n" 1324 " -cont string to prefix continued partial lines with\n"
1325 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1091 " defaults to \"[+]\"\n" 1326 " defaults to \"[+]\"\n"
1092 " -shade add shading to font\n" 1327 " -shade add shading to font\n"
1093 " -noinitial don't display the last file lines on\n" 1328 " -noinitial don't display the last file lines on\n"
1094 " startup\n" 1329 " startup\n"
1095 " -i | -interval seconds interval between checks (fractional\n" 1330 " -i | -interval seconds interval between checks (fractional\n"
1096 " values o.k.). Default 2.4 seconds\n" 1331 " values o.k.). Default 2.4 seconds\n"
1097 " -V display version information and exit\n" 1332 " -V display version information and exit\n"
1098 "\n"); 1333 "\n");
1099 printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green " 1334 printf ("Example:\n%s -g 800x250+100+50 -font fixed /var/log/messages,green "
1100 "/var/log/secure,red,'ALERT'\n", myname); 1335 "/var/log/secure,red,'ALERT'\n", myname);
1101 exit (0); 1336 exit (0);
1102} 1337}
1103 1338
1104void 1339void
1109} 1344}
1110 1345
1111int 1346int
1112daemonize (void) 1347daemonize (void)
1113{ 1348{
1349 pid_t pid;
1350
1114 switch (fork ()) 1351 switch (pid = fork ())
1115 { 1352 {
1116 case -1: 1353 case -1:
1117 return -1; 1354 return -1;
1118 case 0: 1355 case 0:
1119 break; 1356 break;
1120 default: 1357 default:
1358 /*printf("%d\n", pid);*/
1121 _exit (0); 1359 exit (0);
1122 } 1360 }
1123 1361
1124 if (setsid () == -1) 1362 if (setsid () == -1)
1125 return -1; 1363 return -1;
1126 1364

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines