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.11 by pcg, Fri Mar 26 13:14:54 2004 UTC vs.
Revision 1.27 by chris_moore, Mon Mar 29 02:20:55 2004 UTC

31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <sys/types.h> 32#include <sys/types.h>
33#include <locale.h> 33#include <locale.h>
34#include <ctype.h> 34#include <ctype.h>
35#include <stdarg.h> 35#include <stdarg.h>
36#include <X11/Xlib.h>
37#include <X11/Xatom.h>
38#include <X11/Xutil.h>
39
36#if HAS_REGEX 40#if HAS_REGEX
37#include <regex.h> 41#include <regex.h>
38#endif 42#endif
39#include <X11/Xlib.h> 43
40#include <X11/Xatom.h> 44#define SHADE_X 2
41#include <X11/Xutil.h> 45#define SHADE_Y 2
42 46
43/* data structures */ 47/* data structures */
44struct logfile_entry 48struct logfile_entry
45{ 49{
46 struct logfile_entry *next; 50 struct logfile_entry *next;
58}; 62};
59 63
60struct linematrix 64struct linematrix
61{ 65{
62 char *line; 66 char *line;
67 int len;
63 unsigned long color; 68 unsigned long color;
64}; 69};
65 70
66/* global variables */ 71/* global variables */
72struct linematrix *lines;
67int width = STD_WIDTH, height = STD_HEIGHT, listlen; 73int width = STD_WIDTH, height = STD_HEIGHT, listlen;
68int win_x = LOC_X, win_y = LOC_Y; 74int win_x = LOC_X, win_y = LOC_Y;
69int font_descent, font_height; 75int font_ascent, font_height;
76int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */
77int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
70int do_reopen; 78int do_reopen;
71struct timeval interval = { 3, 0 }; 79struct timeval interval = { 2, 400000 };
72XFontSet fontset; 80XFontSet fontset;
73 81
74/* command line options */ 82/* command line options */
75int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, 83int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
76 opt_whole, opt_update, geom_mask, reload = 0; 84 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
85 geom_mask, reload = 0;
77const char *command = NULL, 86const char *command = NULL,
78 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, 87 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
79 *continuation = "[+]"; 88 *continuation = "[+]";
80 89
81struct logfile_entry *loglist = NULL, *loglist_tail = NULL; 90struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
90 regex_t from; 99 regex_t from;
91 const char *to; 100 const char *to;
92 struct re_list *next; 101 struct re_list *next;
93}; 102};
94struct re_list *re_head, *re_tail; 103struct re_list *re_head, *re_tail;
104char *transform_to = NULL;
105regex_t *transformre;
95#endif 106#endif
96 107
97 108
98/* prototypes */ 109/* prototypes */
99void list_files (int); 110void list_files (int);
102void blank_window (int); 113void blank_window (int);
103 114
104void InitWindow (void); 115void InitWindow (void);
105unsigned long GetColor (const char *); 116unsigned long GetColor (const char *);
106void redraw (void); 117void redraw (void);
107void refresh (struct linematrix *, int, int); 118void refresh (int, int, int);
108 119
109void transform_line (char *s); 120void transform_line (char *s);
110int lineinput (struct logfile_entry *); 121int lineinput (struct logfile_entry *);
111void reopen (void); 122void reopen (void);
112void check_open_files (void); 123void check_open_files (void);
113FILE *openlog (struct logfile_entry *); 124FILE *openlog (struct logfile_entry *);
114void main_loop (void); 125static void main_loop (void);
115 126
116void display_version (void); 127void display_version (void);
117void display_help (char *); 128void display_help (char *);
118void install_signal (int, void (*)(int)); 129void install_signal (int, void (*)(int));
119void *xstrdup (const char *); 130void *xstrdup (const char *);
138} 149}
139 150
140void 151void
141force_refresh (int dummy) 152force_refresh (int dummy)
142{ 153{
143 XClearArea (disp, root, win_x, win_y, width, height, False);
144 redraw (); 154 redraw ();
145} 155}
146 156
147void 157void
148blank_window (int dummy) 158blank_window (int dummy)
149{ 159{
150 XClearArea (disp, root, win_x, win_y, width, height, False); 160 XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False);
151 XFlush (disp); 161 XFlush (disp);
152 exit (0); 162 exit (0);
153} 163}
154 164
155/* X related functions */ 165/* X related functions */
284 294
285 { 295 {
286 XFontSetExtents *xfe = XExtentsOfFontSet (fontset); 296 XFontSetExtents *xfe = XExtentsOfFontSet (fontset);
287 297
288 font_height = xfe->max_logical_extent.height; 298 font_height = xfe->max_logical_extent.height;
289 font_descent = xfe->max_logical_extent.y; 299 font_ascent = -xfe->max_logical_extent.y;
290 } 300 }
291 301
292 if (geom_mask & XNegative) 302 if (geom_mask & XNegative)
293 win_x = win_x + ScreenWidth - width; 303 win_x = win_x + ScreenWidth - width;
294 if (geom_mask & YNegative) 304 if (geom_mask & YNegative)
295 win_y = win_y + ScreenHeight - height; 305 win_y = win_y + ScreenHeight - height;
296 306
307 if (opt_outline)
308 {
309 /* adding outline increases the total width and height by 2
310 pixels each, and offsets the text one pixel right and one
311 pixel down */
312 effect_x_space = effect_y_space = 2;
313 effect_x_offset = effect_y_offset = 1;
314 }
315 else if (opt_shade)
316 {
317 /* adding a shadow increases the space used */
318 effect_x_space = abs(SHADE_X);
319 effect_y_space = abs(SHADE_Y);
320 /* if the shadow is to the right and below then we don't need
321 * to move the text to make space for it, but shadows to the left
322 * and above need accomodating */
323 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
324 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
325 }
326 else
327 {
328 effect_x_space = effect_y_space = 0;
329 effect_x_offset = effect_y_offset = 0;
330 }
331
332 /* if we are using -shade or -outline, there will be less usable
333 * space for output */
297 listlen = height / font_height; 334 listlen = (height - effect_y_space) / font_height;
298 335
299 if (!listlen) 336 if (!listlen)
300 { 337 {
301 fprintf (stderr, "height too small for a single line, setting to %d\n", 338 fprintf (stderr, "height too small for a single line, setting to %d\n",
302 font_height); 339 font_height);
303 listlen = 1; 340 listlen = 1;
304 } 341 }
305 342
306 height = listlen * font_height; 343 /* leave the height how the user requested it. it might not all be
307 344 * used, but this will allow the geometry to be tuned more accurately
345 * (with the -frame option)
346 * the old code did this:
347 * height = listlen * font_height + effect_y_space; */
348
308 XSelectInput (disp, root, ExposureMask | FocusChangeMask); 349 XSelectInput (disp, root, ExposureMask | FocusChangeMask);
309}
310
311char *
312detabificate (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} 350}
332 351
333/* 352/*
334 * redraw does a complete redraw, rather than an update (i.e. the area 353 * redraw does a complete redraw, rather than an update (i.e. the area
335 * gets cleared first) 354 * gets cleared first)
336 * the rest is handled by regular refresh()'es 355 * the rest is handled by regular refresh()'es
337 */ 356 */
338void 357void
339redraw (void) 358redraw (void)
340{ 359{
341 XClearArea (disp, root, win_x, win_y, width, height, True); 360 XSetClipMask (disp, WinGC, None);
361 refresh (0, 32768, 1);
342} 362}
343 363
344/* Just redraw everything without clearing (i.e. after an EXPOSE event) */ 364/* Just redraw everything without clearing (i.e. after an EXPOSE event) */
345void 365void
346refresh (struct linematrix *lines, int miny, int maxy) 366refresh (int miny, int maxy, int clear)
347{ 367{
348 int lin; 368 int lin;
349 int offset = (listlen + 1) * font_height; 369 int offset = (listlen + 1) * font_height;
350 unsigned long black_color = GetColor ("black"); 370 unsigned long black_color = GetColor ("black");
351 371
352 miny -= win_y + font_height; 372 miny -= win_y + font_height;
353 maxy -= win_y - font_height; 373 maxy -= win_y - font_height;
354 374
375 if (clear && !opt_noflicker)
376 XClearArea (disp, root, win_x, win_y, width, height, False);
377
355 for (lin = listlen; lin--;) 378 for (lin = listlen; lin--;)
356 { 379 {
357 struct linematrix *line =
358 lines + (opt_reverse ? listlen - lin - 1 : lin); 380 struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin);
359 381
360 offset -= font_height; 382 offset -= font_height;
361 383
362 if (offset < miny || offset > maxy) 384 if (offset < miny || offset > maxy)
363 continue; 385 continue;
364 386
387 if (clear && opt_noflicker)
388 XClearArea (disp, root, win_x, win_y + offset - font_height,
389 width + effect_x_space, font_height + effect_y_space, False);
390
365 if (opt_shade) 391 if (opt_outline)
366 { 392 {
393 int x, y;
367 XSetForeground (disp, WinGC, black_color); 394 XSetForeground (disp, WinGC, black_color);
395
396 for (x = -1; x < 2; x += 2)
397 for (y = -1; y < 2; y += 2)
398 XmbDrawString (disp, root, fontset, WinGC,
399 win_x + effect_x_offset + x,
400 win_y + effect_y_offset + y + offset - font_height + font_ascent,
401 line->line, line->len);
402 }
403 else if (opt_shade)
404 {
405 XSetForeground (disp, WinGC, black_color);
368 XmbDrawString (disp, root, fontset, WinGC, win_x + 2, 406 XmbDrawString (disp, root, fontset, WinGC,
369 win_y + offset + 2, line->line, strlen (line->line)); 407 win_x + effect_x_offset + SHADE_X,
408 win_y + effect_y_offset + offset + SHADE_Y - font_height + font_ascent,
409 line->line, line->len);
370 } 410 }
371 411
372 XSetForeground (disp, WinGC, line->color); 412 XSetForeground (disp, WinGC, line->color);
373 XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset, 413 XmbDrawString (disp, root, fontset, WinGC,
414 win_x + effect_x_offset,
415 win_y + effect_y_offset + offset - font_height + font_ascent,
374 line->line, strlen (line->line)); 416 line->line, line->len);
375 } 417 }
376 418
377 if (opt_frame) 419 if (opt_frame)
420 {
421 XSetForeground (disp, WinGC, GetColor (def_color));
378 XDrawRectangle (disp, root, WinGC, win_x, win_y, width, height); 422 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
423 }
379} 424}
380 425
381#if HAS_REGEX 426#if HAS_REGEX
382void 427void
383transform_line (char *s) 428transform_line (char *s)
395 if (transformre) 440 if (transformre)
396 { 441 {
397 int i; 442 int i;
398 regmatch_t matched[16]; 443 regmatch_t matched[16];
399 444
400 i = regexec (&transformre, string, 16, matched, 0); 445 i = regexec (transformre, s, 16, matched, 0);
401 if (i == 0) 446 if (i == 0)
402 { /* matched */ 447 { /* matched */
448 int match_start = matched[0].rm_so;
449 int match_end = matched[0].rm_eo;
450 int old_len = match_end - match_start;
451 int new_len = strlen(transform_to);
452 int old_whole_len = strlen(s);
453 printf("regexp was matched by '%s' - replace with '%s'\n", s, transform_to);
454 printf("match is from %d to %d\n",
455 match_start, match_end);
456 if (new_len > old_len) {
457 s = realloc(s, old_whole_len + new_len - old_len);
458 }
459 if (new_len != old_len) {
460 memcpy(s + match_end + new_len - old_len,
461 s + match_end,
462 old_whole_len - match_end);
463 s[old_whole_len + new_len - old_len] = '\0';
464 }
465 memcpy(s + match_start,
466 transform_to,
467 new_len);
468 printf("transformed to '%s'\n", s);
403 } 469 }
470 else
471 {
472 printf("regexp was not matched by '%s'\n", s);
473 }
404 } 474 }
405} 475}
406#endif 476#endif
407 477
478char *
479concat_line (const char *p1, const char *p2)
480{
481 int l1 = p1 ? strlen (p1) : 0;
482 int l2 = strlen (p2);
483 char *r = xmalloc (l1 + l2 + 1);
484
485 memcpy (r, p1, l1);
486 memcpy (r + l1, p2, l2);
487 r[l1 + l2] = 0;
488
489 return r;
490}
408 491
409/* 492/*
410 * This routine should read 'width' characters and not more. However, 493 * 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 */ 494 */
416int 495int
417lineinput (struct logfile_entry *logfile) 496lineinput (struct logfile_entry *logfile)
418{ 497{
419 char *string = logfile->buf; 498 char buff[1024], *p = buff;
420 int slen = width + 2; 499 int ch;
421 FILE *f = logfile->fp; 500 /* HACK this - to add on the length of any partial line which we will be appending to */
422 501 int ofs = logfile->buf ? strlen (logfile->buf) : 0;
423 int len = strlen (string);
424 int partial = logfile->partial;
425 502
426 do 503 do
427 { 504 {
428 if (fgets (string + len, slen - len, f) == NULL) /* EOF or Error */ 505 ch = fgetc (logfile->fp);
429 return 0;
430 506
431 len = strlen (string); 507 if (ch == '\n' || ch == EOF)
432 } 508 break;
433 while (len == 0); 509 else if (ch == '\r')
434 510 continue; /* skip */
435 logfile->partial = 0; 511 else if (ch == '\t')
436 512 {
437 if (string[len - 1] == '\n') 513 do
438 /* if the string ends in a newline, delete the newline */ 514 {
439 string[len - 1] = '\0'; /* erase newline */ 515 *p++ = ' ';
440 else if (len >= slen - 1) 516 ofs++;
517 }
518 while (ofs & 7);
519 }
520 else
521 {
522 *p++ = ch;
523 ofs++;
524 }
441 { 525 }
442 /* otherwise if we've read one too many characters, un-read the last one and delete it */ 526 while (p < buff + (sizeof buff) - 8 - 1);
443 ungetc (string[len - 1], f); 527
444 string[len - 1] = '\0'; 528 if (p == buff && ch == EOF)
445 }
446 else if (opt_whole)
447 return 0; 529 return 0;
448 else 530
531 *p = 0;
532
533 p = concat_line (logfile->buf, buff);
534 free (logfile->buf); logfile->buf = p;
535
536 logfile->lastpartial = logfile->partial;
537 /* there are 3 ways we could have exited the loop: reading '\n',
538 * reaching EOF, or filling the buffer; the 2nd and 3rd of these
539 * both result in a partial line */
449 logfile->partial = 1; 540 logfile->partial = ch != '\n';
541
542 if (logfile->partial && opt_whole)
543 return 0;
450 544
451#if HAS_REGEX 545#if HAS_REGEX
452 transform_line (string); 546 transform_line (logfile->buf);
453#endif 547#endif
454 logfile->lastpartial = partial;
455 return len; 548 return 1;
456} 549}
457 550
458/* input: reads file->fname 551/* input: reads file->fname
459 * output: fills file->fp, file->inode 552 * output: fills file->fp, file->inode
460 * returns file->fp 553 * returns file->fp
524 { /* file missing? */ 617 { /* file missing? */
525 sleep (1); 618 sleep (1);
526 if (e->fp) 619 if (e->fp)
527 fclose (e->fp); 620 fclose (e->fp);
528 if (openlog (e) == NULL) 621 if (openlog (e) == NULL)
529 break; 622 continue;
530 } 623 }
531 624
625 /* HACK this - stats can be uninitialised here (if the file
626 * didn't exist when stat() was called, but was recreated during
627 * the sleep(1)) */
532 if (stats.st_ino != e->inode) 628 if (stats.st_ino != e->inode)
533 { /* file renamed? */ 629 { /* file renamed? */
534 if (e->fp) 630 if (e->fp)
535 fclose (e->fp); 631 fclose (e->fp);
536 if (openlog (e) == NULL) 632 if (openlog (e) == NULL)
537 break; 633 continue;
538 } 634 }
539 635
540 if (stats.st_size < e->last_size) 636 if (stats.st_size < e->last_size)
541 { /* file truncated? */ 637 { /* file truncated? */
542 fseek (e->fp, 0, SEEK_SET); 638 fseek (e->fp, 0, SEEK_SET);
543 e->last_size = stats.st_size; 639 e->last_size = stats.st_size;
544 } 640 }
545 } 641 }
546} 642}
547 643
548#define SCROLL_UP(lines, listlen) \ 644/*
549{ \ 645 * insert a single physical line (that must be short enough to fit)
550 int cur_line; \ 646 * at position "idx" by pushing up lines above it. the caller
551 struct logfile_entry *current; \ 647 * MUST then fill in lines[idx] with valid data.
648 */
649static void
650insert_line (int idx)
651{
652 int cur_line;
653 struct logfile_entry *current;
654
655 free (lines[0].line);
656
552 for (cur_line = 0; cur_line < (listlen - 1); cur_line++) { \ 657 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; \ 658 lines[cur_line] = lines[cur_line + 1];
555 } \ 659
556 for (current = loglist; current; current = current->next) \ 660 for (current = loglist; current; current = current->next)
557 if (current->partial && current->index) \ 661 if (current->index <= idx)
558 current->index--; \ 662 current->index--;
559} 663}
560 664
561void 665/*
666 * remove a single physical line at position "idx" by moving the lines above it
667 * down and inserting a "~" line at the top.
668 */
669static void
670delete_line (int idx)
671{
672 int cur_line;
673 struct logfile_entry *current;
674
675 for (cur_line = idx; cur_line > 0; cur_line--)
676 lines[cur_line] = lines[cur_line - 1];
677
678 lines[0].line = strdup ("~");
679
680 for (current = loglist; current; current = current->next)
681 if (current->index >= 0 && current->index <= idx)
682 current->index++;
683}
684
685/*
686 * takes a logical log file line and splits it into multiple physical
687 * screen lines by splitting it whenever a part becomes too long.
688 * lal lines will be inserted at position "idx".
689 */
690static void
691split_line (int idx, const char *str, unsigned long color)
692{
693 int l = strlen (str);
694 const char *p = str;
695
696 do
697 {
698 const char *beg = p;
699 int w = 0, wrapped = 0;
700 const char *break_p = NULL;
701
702 while (*p)
703 {
704 /* find the length in bytes of the next multibyte character */
705 int len = mblen (p, l);
706 if (len <= 0)
707 len = 1; /* ignore (don't skip) illegal character sequences */
708
709 /* find the width in pixels of the next character */
710 int cw = XmbTextEscapement (fontset, p, len);
711 if (cw + w > width - effect_x_space)
712 {
713 wrapped = 1;
714 break;
715 }
716
717 if (opt_wordwrap && len == 1 && p[0] == ' ')
718 break_p = p;
719
720 w += cw;
721 p += len;
722 l -= len;
723 }
724
725 /* if we're wrapping at spaces, and the line is long enough to
726 * wrap, and we've seen a space already, and the space wasn't
727 * the first character on the line, then wrap at the space */
728 if (opt_wordwrap && wrapped && break_p && break_p != beg)
729 {
730 l += p - break_p;
731 p = break_p;
732 }
733
734 {
735 char *s = xmalloc (p - beg + 1);
736 memcpy (s, beg, p - beg);
737 s[p - beg] = 0;
738 insert_line (idx);
739 lines[idx].line = s;
740 lines[idx].len = p - beg;
741 lines[idx].color = color;
742 }
743
744 /* if we wrapped at a space, don't display the space */
745 if (opt_wordwrap && wrapped && break_p && break_p != beg)
746 {
747 l--;
748 p++;
749 }
750 }
751 while (l);
752}
753
754/*
755 * append something to an existing physical line. this is done
756 * by deleting the file on-screen, concatenating the new data to it
757 * and splitting it again.
758 */
759static void
760append_line (int idx, const char *str)
761{
762 unsigned long color = lines[idx].color;
763 char *old = lines[idx].line;
764 char *new = concat_line (old, str);
765
766 free (old);
767
768 delete_line (idx);
769 split_line (idx, new, color);
770}
771
772static void
562main_loop (void) 773main_loop (void)
563{ 774{
564 struct linematrix *lines = xmalloc (sizeof (struct linematrix) * listlen); 775 lines = xmalloc (sizeof (struct linematrix) * listlen);
565 int lin, miny, maxy; 776 int lin;
566 time_t lastreload; 777 time_t lastreload;
567 Region region = XCreateRegion (); 778 Region region = XCreateRegion ();
568 XEvent xev; 779 XEvent xev;
569 struct logfile_entry *lastprinted = NULL; 780 struct logfile_entry *lastprinted = NULL;
570 struct logfile_entry *current; 781 struct logfile_entry *current;
782 int need_update = 1;
571 783
572 maxy = 0;
573 miny = win_y + height;
574 lastreload = time (NULL); 784 lastreload = time (NULL);
575 785
576 /* Initialize linematrix */ 786 /* Initialize linematrix */
577 for (lin = 0; lin < listlen; lin++) 787 for (lin = 0; lin < listlen; lin++)
578 { 788 {
579 lines[lin].line = xmalloc (width + 2); 789 lines[lin].line = strdup ("~");
580 strcpy (lines[lin].line, "~"); 790 lines[lin].len = 1;
581 lines[lin].color = GetColor (def_color); 791 lines[lin].color = GetColor (def_color);
582 } 792 }
583 793
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 (;;) 794 for (;;)
591 { 795 {
592 int need_update = 0;
593
594 /* read logs */ 796 /* read logs */
595 for (current = loglist; current; current = current->next) 797 for (current = loglist; current; current = current->next)
596 { 798 {
597 if (!current->fp) 799 if (!current->fp)
598 continue; /* skip missing files */ 800 continue; /* skip missing files */
599 801
600 clearerr (current->fp); 802 clearerr (current->fp);
601 803
602 while (lineinput (current) != 0) 804 while (lineinput (current))
603 { 805 {
806 need_update = 1;
604 /* if we're trying to update old partial lines in 807 /* if we're trying to update old partial lines in
605 * place, and the last time this file was updated the 808 * place, and the last time this file was updated the
606 * output was partial, and that partial line is not 809 * output was partial, and that partial line is not
607 * too close to the top of the screen, then update 810 * too close to the top of the screen, then update
608 * that partial line */ 811 * that partial line */
609 if (opt_update && current->lastpartial && current->index >= 3) 812 if (opt_update && current->lastpartial && current->index >= 0)
610 { 813 {
611 int old_len = strlen (lines[current->index].line); 814 int idx = current->index;
612 int new_len = strlen (current->buf); 815 append_line (idx, current->buf);
613 int space_on_old_line = width / font_width - old_len; //D 816 current->index = idx;
614 817 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; 818 continue;
634 }
635 } 819 }
636 820
637 /* print filename if any, and if last line was from 821 /* print filename if any, and if last line was from
638 * different file */ 822 * different file */
639 if (!opt_nofilename &&
640 lastprinted != current && current->desc[0]) 823 if (!opt_nofilename && lastprinted != current && current->desc[0])
641 { 824 {
642 SCROLL_UP (lines, listlen); 825 char buf[1024]; /* HACK */
643 sprintf (lines[listlen - 1].line, "[%s]", current->desc); 826 snprintf (buf, sizeof (buf), "[%s]", current->desc);
644 lines[listlen - 1].color = current->color; 827 split_line (listlen - 1, buf, current->color);
645 } 828 }
646 829
647 /* if this is the same file we showed last, and the 830 /* if this is the same file we showed last, and the
648 * last time we showed it, it wasn't finished, then 831 * last time we showed it, it wasn't finished, then
649 * append to the last line shown */ 832 * append to the last line shown */
650 if (lastprinted == current && current->lastpartial) 833 if (lastprinted == current && current->lastpartial)
651 { 834 append_line (listlen - 1, current->buf);
652 int old_len = strlen (lines[listlen - 1].line);
653 int new_len = strlen (current->buf);
654 strncat (lines[listlen - 1].line, current->buf,
655 width - old_len);
656 /* if it doesn't all fit, then put the part that
657 * doesn't fit on a new line */
658 if (new_len > width - old_len)
659 {
660 SCROLL_UP (lines, listlen);
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) 835 else if (current->lastpartial)
669 { 836 {
670 int old_len = strlen (continuation); 837 split_line (listlen - 1, continuation, current->color);
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, 838 append_line (listlen - 1, current->buf);
675 width - old_len); 839 }
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 840 else
686 { 841 split_line (listlen - 1, current->buf, current->color);
687 SCROLL_UP (lines, listlen);
688 strcpy (lines[listlen - 1].line, current->buf);
689 }
690 842
691 /* we've shown the line now. clear the buffer for the next line */ 843 free (current->buf), current->buf = 0;
692 strcpy (current->buf, "");
693 current->index = listlen - 1; 844 current->index = listlen - 1;
694 lines[listlen - 1].color = current->color;
695
696 lastprinted = current; 845 lastprinted = current;
697 need_update = 1;
698 } 846 }
699 } 847 }
700 848
701 if (need_update) 849 if (need_update)
850 {
702 redraw (); 851 redraw ();
852 need_update = 0;
853 }
703 else 854 else
704 { 855 {
705 XFlush (disp); 856 XFlush (disp);
706 857
707 if (!XPending (disp)) 858 if (!XPending (disp))
733 884
734 r.x = xev.xexpose.x; 885 r.x = xev.xexpose.x;
735 r.y = xev.xexpose.y; 886 r.y = xev.xexpose.y;
736 r.width = xev.xexpose.width; 887 r.width = xev.xexpose.width;
737 r.height = xev.xexpose.height; 888 r.height = xev.xexpose.height;
889
738 XUnionRectWithRegion (&r, region, region); 890 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 } 891 }
744 break; 892 break;
745 default: 893 default:
746#ifdef DEBUGMODE 894#ifdef DEBUGMODE
747 fprintf (stderr, "PANIC! Unknown event %d\n", xev.type); 895 fprintf (stderr, "PANIC! Unknown event %d\n", xev.type);
760 lastreload = time (NULL); 908 lastreload = time (NULL);
761 } 909 }
762 910
763 if (!XEmptyRegion (region)) 911 if (!XEmptyRegion (region))
764 { 912 {
913 XRectangle r;
914
765 XSetRegion (disp, WinGC, region); 915 XSetRegion (disp, WinGC, region);
916 XClipBox (region, &r);
766 917
767 refresh (lines, miny, maxy); 918 refresh (r.y, r.y + r.height, 0);
919
768 XDestroyRegion (region); 920 XDestroyRegion (region);
769 region = XCreateRegion (); 921 region = XCreateRegion ();
770 maxy = 0;
771 miny = win_y + height;
772 } 922 }
773 } 923 }
774} 924}
775 925
776 926
814 continuation = argv[++i]; 964 continuation = argv[++i];
815 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) 965 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
816 fontname = argv[++i]; 966 fontname = argv[++i];
817#if HAS_REGEX 967#if HAS_REGEX
818 else if (!strcmp (arg, "-t")) 968 else if (!strcmp (arg, "-t"))
969 {
819 transform = argv[++i]; 970 transform = argv[++i];
971 transform_to = argv[++i];
972 printf("transform: '%s' to '%s'\n", transform, transform_to);
973 }
820#endif 974#endif
821 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) 975 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
822 opt_daemonize = 1; 976 opt_daemonize = 1;
823 else if (!strcmp (arg, "-reload")) 977 else if (!strcmp (arg, "-reload"))
824 { 978 {
825 reload = atoi (argv[++i]); 979 reload = atoi (argv[++i]);
826 command = argv[++i]; 980 command = argv[++i];
827 } 981 }
828 else if (!strcmp (arg, "-shade")) 982 else if (!strcmp (arg, "-shade"))
829 opt_shade = 1; 983 opt_shade = 1;
984 else if (!strcmp (arg, "-outline"))
985 opt_outline = 1;
986 else if (!strcmp (arg, "-noflicker"))
987 opt_noflicker = 1;
830 else if (!strcmp (arg, "-frame")) 988 else if (!strcmp (arg, "-frame"))
831 opt_frame = 1; 989 opt_frame = 1;
832 else if (!strcmp (arg, "-no-filename")) 990 else if (!strcmp (arg, "-no-filename"))
833 opt_nofilename = 1; 991 opt_nofilename = 1;
834 else if (!strcmp (arg, "-reverse")) 992 else if (!strcmp (arg, "-reverse"))
837 opt_whole = 1; 995 opt_whole = 1;
838 else if (!strcmp (arg, "-partial")) 996 else if (!strcmp (arg, "-partial"))
839 opt_partial = 1; 997 opt_partial = 1;
840 else if (!strcmp (arg, "-update")) 998 else if (!strcmp (arg, "-update"))
841 opt_update = opt_partial = 1; 999 opt_update = opt_partial = 1;
1000 else if (!strcmp (arg, "-wordwrap"))
1001 opt_wordwrap = 1;
842 else if (!strcmp (arg, "-color")) 1002 else if (!strcmp (arg, "-color"))
843 def_color = argv[++i]; 1003 def_color = argv[++i];
844 else if (!strcmp (arg, "-noinitial")) 1004 else if (!strcmp (arg, "-noinitial"))
845 opt_noinitial = 1; 1005 opt_noinitial = 1;
846 else if (!strcmp (arg, "-id")) 1006 else if (!strcmp (arg, "-id"))
880 desc = p + 1; 1040 desc = p + 1;
881 } 1041 }
882 } 1042 }
883 1043
884 e = xmalloc (sizeof (struct logfile_entry)); 1044 e = xmalloc (sizeof (struct logfile_entry));
1045 e->partial = 0;
1046 e->buf = 0;
1047 e->index = -1;
1048
885 if (arg[0] == '-' && arg[1] == '\0') 1049 if (arg[0] == '-' && arg[1] == '\0')
886 { 1050 {
887 if ((e->fp = fdopen (0, "r")) == NULL) 1051 if ((e->fp = fdopen (0, "r")) == NULL)
888 perror ("fdopen"), exit (1); 1052 perror ("fdopen"), exit (1);
889 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0) 1053 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
899 e->fname = xstrdup (fname); 1063 e->fname = xstrdup (fname);
900 if (openlog (e) == NULL) 1064 if (openlog (e) == NULL)
901 perror (fname), exit (1); 1065 perror (fname), exit (1);
902 1066
903 l = strlen (desc); 1067 l = strlen (desc);
1068 /* HACK on this - width is in pixels now */
904 if (l > width - 2) /* must account for [ ] */ 1069 if (l > width - 2) /* must account for [ ] */
905 l = width - 2; 1070 l = width - 2;
906 e->desc = xmalloc (l + 1); 1071 e->desc = xmalloc (l + 1);
907 memcpy (e->desc, desc, l); 1072 memcpy (e->desc, desc, l);
908 *(e->desc + l) = '\0'; 1073 *(e->desc + l) = '\0';
909 } 1074 }
910 1075
911 e->color = GetColor (fcolor); 1076 e->color = GetColor (fcolor);
912 e->buf = xmalloc (width + 2);
913 e->partial = 0; 1077 e->partial = 0;
914 e->buf[0] = '\0';
915 e->next = NULL; 1078 e->next = NULL;
916 1079
917 if (!loglist) 1080 if (!loglist)
918 loglist = e; 1081 loglist = e;
919 if (loglist_tail) 1082 if (loglist_tail)
930 } 1093 }
931 1094
932 if (opt_partial && opt_whole) 1095 if (opt_partial && opt_whole)
933 { 1096 {
934 fprintf (stderr, "Specify at most one of -partial and -whole\n"); 1097 fprintf (stderr, "Specify at most one of -partial and -whole\n");
1098 exit (1);
1099 }
1100
1101 /* HACK this - do we want to allow both -shade and -outline? */
1102 if (opt_shade && opt_outline)
1103 {
1104 fprintf (stderr, "Specify at most one of -shade and -outline\n");
935 exit (1); 1105 exit (1);
936 } 1106 }
937 1107
938 if (opt_partial) 1108 if (opt_partial)
939 /* if we specifically requested to see partial lines then don't insist on whole lines */ 1109 /* if we specifically requested to see partial lines then don't insist on whole lines */
945#if HAS_REGEX 1115#if HAS_REGEX
946 if (transform) 1116 if (transform)
947 { 1117 {
948 int i; 1118 int i;
949 1119
1120 printf("compiling regexp '%s'\n", transform);
950 transformre = xmalloc (sizeof (transformre)); 1121 transformre = xmalloc (sizeof (regex_t));
951 i = regcomp (&transformre, transform, REG_EXTENDED); 1122 i = regcomp (transformre, transform, REG_EXTENDED);
952 if (i != 0) 1123 if (i != 0)
953 { 1124 {
954 char buf[512]; 1125 char buf[512];
955 1126
956 regerror (i, &transformre, buf, sizeof (buf)); 1127 regerror (i, transformre, buf, sizeof (buf));
957 fprintf (stderr, "Cannot compile regular expression: %s\n", buf); 1128 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
958 } 1129 }
1130 else
1131 {
1132 printf("compiled '%s' OK to %x\n", transform, (int)transformre);
1133 }
959 } 1134 }
960#endif 1135#endif
961 1136
962 InitWindow (); 1137 InitWindow ();
963 1138
1011 while ((p = malloc (size)) == NULL) 1186 while ((p = malloc (size)) == NULL)
1012 { 1187 {
1013 fprintf (stderr, "Memory exausted."); 1188 fprintf (stderr, "Memory exausted.");
1014 sleep (10); 1189 sleep (10);
1015 } 1190 }
1191
1016 return p; 1192 return p;
1017} 1193}
1018 1194
1019void 1195void
1020display_help (char *myname) 1196display_help (char *myname)
1030 " -reverse print new lines at the top\n" 1206 " -reverse print new lines at the top\n"
1031 " -whole wait for \\n before showing a line\n" 1207 " -whole wait for \\n before showing a line\n"
1032 " -partial show lines even if they don't end with a \\n\n" 1208 " -partial show lines even if they don't end with a \\n\n"
1033 " -update allow updates to old partial lines\n" 1209 " -update allow updates to old partial lines\n"
1034 " -cont string to prefix continued partial lines with\n" 1210 " -cont string to prefix continued partial lines with\n"
1211 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1035 " defaults to \"[+]\"\n" 1212 " defaults to \"[+]\"\n"
1036 " -shade add shading to font\n" 1213 " -shade add shading to font\n"
1037 " -noinitial don't display the last file lines on\n" 1214 " -noinitial don't display the last file lines on\n"
1038 " startup\n" 1215 " startup\n"
1039 " -i | -interval seconds interval between checks (fractional\n" 1216 " -i | -interval seconds interval between checks (fractional\n"
1040 " values o.k.). Default 3 seconds\n" 1217 " values o.k.). Default 2.4 seconds\n"
1041 " -V display version information and exit\n" 1218 " -V display version information and exit\n"
1042 "\n"); 1219 "\n");
1043 printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green " 1220 printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green "
1044 "/var/log/secure,red,'ALERT'\n", myname); 1221 "/var/log/secure,red,'ALERT'\n", myname);
1045 exit (0); 1222 exit (0);
1053} 1230}
1054 1231
1055int 1232int
1056daemonize (void) 1233daemonize (void)
1057{ 1234{
1235 pid_t pid;
1236
1058 switch (fork ()) 1237 switch (pid = fork ())
1059 { 1238 {
1060 case -1: 1239 case -1:
1061 return -1; 1240 return -1;
1062 case 0: 1241 case 0:
1063 break; 1242 break;
1064 default: 1243 default:
1244 printf("%d\n", pid);
1065 _exit (0); 1245 exit (0);
1066 } 1246 }
1067 1247
1068 if (setsid () == -1) 1248 if (setsid () == -1)
1069 return -1; 1249 return -1;
1070 1250

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines