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.48 by chris_moore, Fri Apr 2 00:27:30 2004 UTC vs.
Revision 1.49 by chris_moore, Wed Apr 7 03:54:30 2004 UTC

20 * along with this program; if not, write to the Free Software 20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */ 22 */
23 23
24#include "config.h" 24#include "config.h"
25#include <assert.h>
25#include <stdlib.h> 26#include <stdlib.h>
26#include <stdio.h> 27#include <stdio.h>
27#include <unistd.h> 28#include <unistd.h>
28#include <string.h> 29#include <string.h>
29#include <signal.h> 30#include <signal.h>
50/* data structures */ 51/* data structures */
51struct logfile_entry 52struct logfile_entry
52{ 53{
53 struct logfile_entry *next; 54 struct logfile_entry *next;
54 55
55 char *fname; /* name of file */ 56 char *fname; /* name of file */
56 char *desc; /* alternative description */ 57 char *desc; /* alternative description */
57 char *buf; /* text read but not yet displayed */ 58 char *buf; /* text read but not yet displayed */
58 FILE *fp; /* FILE struct associated with file */ 59 FILE *fp; /* FILE struct associated with file */
59 ino_t inode; /* inode of the file opened */ 60 ino_t inode; /* inode of the file opened */
60 off_t last_size; /* file size at the last check */ 61 off_t last_size; /* file size at the last check */
61 unsigned long color; /* color to be used for printing */ 62 unsigned long color; /* color to be used for printing */
62 int partial; /* true if the last line isn't complete */ 63 int partial; /* true if the last line isn't complete */
63 int lastpartial; /* true if the previous output wasn't complete */ 64 int lastpartial; /* true if the previous output wasn't complete */
64 int index; /* index into linematrix of a partial line */ 65 struct line_node *last; /* last line we output */
65 int modified; /* true if line is modified & needs displaying */ 66 int modified; /* true if line is modified & needs displaying */
66}; 67};
67 68
68struct linematrix 69struct line_node
69{ 70{
70 char *line; 71 struct line_node *next;
71 int len; 72 struct line_node *prev;
72 unsigned long color; 73 struct logfile_entry *logfile;
74
75 char *line; /* the text of the line (so far) */
76 int len; /* the length of the line (in bytes) so far */
77 int wrapped_left; /* true if wrapped from the previous line */
78 int wrapped_right; /* true if wrapped to the next line */
79 struct breakinfo *breaks; /* array of indicies to spaces if wrapped_right */
80 int num_words; /* the number of words in the line */
81 int free_pixels; /* the number of free pixels to spread out */
82};
83
84struct breakinfo
85{
86 int index; /* index into string of start of substring */
87 int width; /* width in pixels of start of substring */
88 int len; /* length of substring */
73}; 89};
74 90
75struct displaymatrix 91struct displaymatrix
76{ 92{
77 char *line; 93 char *line;
79 int buffer_size; 95 int buffer_size;
80 unsigned long color; 96 unsigned long color;
81}; 97};
82 98
83/* global variables */ 99/* global variables */
84struct linematrix *lines; 100int debug = 1;
101
102struct line_node *linelist = NULL, *linelist_tail = NULL;
85struct displaymatrix *display; 103struct displaymatrix *display;
104int continuation_width = -1;
105int continuation_color;
106int continuation_length;
107
86int width = STD_WIDTH, height = STD_HEIGHT, listlen; 108int width = STD_WIDTH, height = STD_HEIGHT, listlen;
87int win_x = LOC_X, win_y = LOC_Y; 109int win_x = LOC_X, win_y = LOC_Y;
88int font_ascent, font_height; 110int font_ascent, font_height;
89int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */ 111int 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 */ 112int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
93XFontSet fontset; 115XFontSet fontset;
94 116
95/* command line options */ 117/* command line options */
96int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, 118int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
97 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap, 119 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
98 geom_mask, reload = 0; 120 opt_justify, geom_mask, reload = 0;
99const char *command = NULL, 121const char *command = NULL,
100 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, 122 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
101 *continuation = "[+]"; 123 *continuation = "|| ", *cont_color = DEF_CONT_COLOR;
102 124
103struct logfile_entry *loglist = NULL, *loglist_tail = NULL; 125struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
104 126
105Display *disp; 127Display *disp;
106Window root; 128Window root;
319 win_y = win_y + ScreenHeight - height; 341 win_y = win_y + ScreenHeight - height;
320 342
321 if (opt_outline) 343 if (opt_outline)
322 { 344 {
323 /* adding outline increases the total width and height by 2 345 /* adding outline increases the total width and height by 2
324 pixels each, and offsets the text one pixel right and one 346 pixels each, and offsets the text one pixel right and one
325 pixel down */ 347 pixel down */
326 effect_x_space = effect_y_space = 2; 348 effect_x_space = effect_y_space = 2;
327 effect_x_offset = effect_y_offset = 1; 349 effect_x_offset = effect_y_offset = 1;
328 } 350 }
329 else if (opt_shade) 351 else if (opt_shade)
330 { 352 {
331 /* adding a shadow increases the space used */ 353 /* adding a shadow increases the space used */
332 effect_x_space = abs(SHADE_X); 354 effect_x_space = abs (SHADE_X);
333 effect_y_space = abs(SHADE_Y); 355 effect_y_space = abs (SHADE_Y);
334 /* if the shadow is to the right and below then we don't need 356 /* 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 357 * to move the text to make space for it, but shadows to the left
336 * and above need accomodating */ 358 * and above need accomodating */
337 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X; 359 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
338 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y; 360 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
352 fprintf (stderr, "height too small for a single line, setting to %d\n", 374 fprintf (stderr, "height too small for a single line, setting to %d\n",
353 font_height); 375 font_height);
354 listlen = 1; 376 listlen = 1;
355 } 377 }
356 378
357 /* leave the height how the user requested it. it might not all be
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
363 XSelectInput (disp, root, ExposureMask | FocusChangeMask); 379 XSelectInput (disp, root, ExposureMask | FocusChangeMask);
364} 380}
365 381
366/* 382/*
367 * if redraw() is passwd a non-zero argument, it does a complete 383 * if redraw () is passwd a non-zero argument, it does a complete
368 * redraw, rather than an update. if the argument is zero (and 384 * redraw, rather than an update. if the argument is zero (and
369 * -noflicker is in effect) then only the lines which have changed 385 * -noflicker is in effect) then only the lines which have changed
370 * since the last draw are redrawn. 386 * since the last draw are redrawn.
371 * 387 *
372 * the rest is handled by regular refresh()'es 388 * the rest is handled by regular refresh ()'es
373 */ 389 */
374void 390void
375redraw (int redraw_all) 391redraw (int redraw_all)
376{ 392{
377 XSetClipMask (disp, WinGC, None); 393 XSetClipMask (disp, WinGC, None);
378 refresh (0, 32768, 1, redraw_all); 394 refresh (0, 32768, 1, redraw_all);
379} 395}
380 396
397void draw_text (Display *disp, Window root, XFontSet fontset, GC WinGC, int x, int y, struct line_node *line, int foreground)
398{
399 if (line->wrapped_right && opt_justify && line->breaks)
400 {
401 int i;
402 for (i = 0; i < line->num_words; i++) {
403 XmbDrawString (disp, root, fontset, WinGC,
404 x + line->breaks[i].width + ((i * line->free_pixels) / (line->num_words - 1)) + continuation_width * line->wrapped_left, y,
405 line->line + line->breaks[i].index,
406 line->breaks[i].len);
407 }
408 if (line->wrapped_left)
409 {
410 if (foreground) XSetForeground (disp, WinGC, continuation_color);
411 XmbDrawString (disp, root, fontset, WinGC, x, y, continuation, continuation_length);
412 }
413 }
414 else
415 {
416 XmbDrawString (disp, root, fontset, WinGC, x + continuation_width * line->wrapped_left, y, line->line, line->len);
417 if (line->wrapped_left)
418 {
419 if (foreground) XSetForeground (disp, WinGC, continuation_color);
420 XmbDrawString (disp, root, fontset, WinGC, x, y, continuation, continuation_length);
421 }
422 }
423}
424
381/* Just redraw everything without clearing (i.e. after an EXPOSE event) */ 425/* Just redraw everything without clearing (i.e. after an EXPOSE event) */
382void 426void
383refresh (int miny, int maxy, int clear, int refresh_all) 427refresh (int miny, int maxy, int clear, int refresh_all)
384{ 428{
385 int lin; 429 int lin = 0;
386 int offset = listlen * font_height + font_ascent + effect_y_offset; 430 int offset;
387 unsigned long black_color = GetColor ("black"); 431 unsigned long black_color = GetColor ("black");
432 struct line_node *line;
433 int step_per_line;
434 int foreground = 0;
435
436 if (opt_reverse)
437 {
438 step_per_line = font_height;
439 offset = font_ascent + effect_y_offset;
440 }
441 else
442 {
443 step_per_line = -font_height;
444 offset = (listlen - 1) * font_height + font_ascent + effect_y_offset;
445 }
388 446
389 miny -= win_y + font_height; 447 miny -= win_y + font_height;
390 maxy -= win_y - font_height; 448 maxy -= win_y - font_height;
391 449
392 if (clear && !opt_noflicker) 450 if (clear && !opt_noflicker)
393 XClearArea (disp, root, win_x, win_y, width, height, False); 451 XClearArea (disp, root, win_x, win_y, width, height, False);
394 452
395 for (lin = listlen; lin--;) 453 for (line = linelist; line && lin < listlen; line = line->next, lin++, offset += step_per_line)
396 { 454 {
397 struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin);
398 struct displaymatrix *display_line = display + lin; 455 struct displaymatrix *display_line = display + lin;
399
400 offset -= font_height;
401 456
402 if (offset < miny || offset > maxy) 457 if (offset < miny || offset > maxy)
403 continue; 458 continue;
404 459
405 /* if this line is a different than it was, then it 460 /* if this line is a different than it was, then it
406 * needs displaying */ 461 * needs displaying */
407 if (!opt_noflicker 462 if (!opt_noflicker
408 || refresh_all 463 || refresh_all
409 || display_line->len != line->len 464 || display_line->len != line->len
410 || display_line->color != line->color 465 || display_line->color != line->logfile->color
411 || memcmp (display_line->line, line->line, line->len)) 466 || memcmp (display_line->line, line->line, line->len))
412 { 467 {
413 /* don't bother updating the record of what has been 468 /* don't bother updating the record of what has been
414 * displayed if -noflicker isn't in effect, since we redraw 469 * displayed if -noflicker isn't in effect, since we redraw
415 * the whole display every time anyway */ 470 * the whole display every time anyway */
416 if (opt_noflicker) 471 if (opt_noflicker)
417 { 472 {
418 /* update the record of what has been displayed; 473 /* update the record of what has been displayed;
419 * first make sure the buffer is big enough */ 474 * first make sure the buffer is big enough */
420 if (display_line->buffer_size < line->len) 475 if (display_line->buffer_size < line->len)
421 { 476 {
422 display_line->buffer_size = line->len; 477 display_line->buffer_size = line->len;
423 display_line->line = xrealloc (display_line->line, display_line->buffer_size); 478 display_line->line = xrealloc (display_line->line, display_line->buffer_size);
424 } 479 }
425 480
426 display_line->len = line->len; 481 display_line->len = line->len;
427 display_line->color = line->color; 482 display_line->color = line->logfile->color;
428 memcpy (display_line->line, line->line, line->len); 483 memcpy (display_line->line, line->line, line->len);
429 484
430 if (clear) 485 if (clear)
431 XClearArea (disp, root, win_x, win_y + offset - font_ascent, 486 XClearArea (disp, root, win_x, win_y + offset - font_ascent,
432 width + effect_x_space, font_height + effect_y_space, False); 487 width + effect_x_space, font_height + effect_y_space, False);
433 } 488 }
434 489
435 if (opt_outline) 490 if (opt_outline)
436 { 491 {
437 int x, y; 492 int x, y;
438 XSetForeground (disp, WinGC, black_color); 493 XSetForeground (disp, WinGC, black_color);
439 494
440 for (x = -1; x <= 1; x += 2) 495 for (x = -1; x <= 1; x += 2)
441 for (y = -1; y <= 1; y += 2) 496 for (y = -1; y <= 1; y += 2)
442 XmbDrawString (disp, root, fontset, WinGC, 497 draw_text (disp, root, fontset, WinGC,
443 win_x + effect_x_offset + x, 498 win_x + effect_x_offset + x,
444 win_y + y + offset, 499 win_y + y + offset, line, foreground = 0);
445 line->line, line->len);
446 } 500 }
447 else if (opt_shade) 501 else if (opt_shade)
448 { 502 {
449 XSetForeground (disp, WinGC, black_color); 503 XSetForeground (disp, WinGC, black_color);
450 XmbDrawString (disp, root, fontset, WinGC, 504 draw_text (disp, root, fontset, WinGC,
451 win_x + effect_x_offset + SHADE_X, 505 win_x + effect_x_offset + SHADE_X,
452 win_y + offset + SHADE_Y, 506 win_y + offset + SHADE_Y, line, foreground = 0);
453 line->line, line->len);
454 } 507 }
455 508
456 XSetForeground (disp, WinGC, line->color); 509 XSetForeground (disp, WinGC, line->logfile->color);
457 XmbDrawString (disp, root, fontset, WinGC, 510 draw_text (disp, root, fontset, WinGC,
458 win_x + effect_x_offset, 511 win_x + effect_x_offset,
459 win_y + offset, 512 win_y + offset, line, foreground = 1);
460 line->line, line->len); 513 }
461 } 514 }
515
516 /* any lines that didn't just get looked at are never going to be, so break the chain */
517 if (line) line->prev->next = 0;
518
519 /* and throw them all away */
520 while (line)
521 {
522 struct line_node *this = line;
523 line = line->next;
524 if (this->logfile && this->logfile->last == this)
525 this->logfile->last = NULL;
526 free (this->line);
527 free (this->breaks);
528 free (this);
462 } 529 }
463 530
464 if (opt_frame) 531 if (opt_frame)
465 { 532 {
466 XSetForeground (disp, WinGC, GetColor (def_color)); 533 XSetForeground (disp, WinGC, GetColor (def_color));
488 regmatch_t matched[16]; 555 regmatch_t matched[16];
489 556
490 i = regexec (transformre, s, 16, matched, 0); 557 i = regexec (transformre, s, 16, matched, 0);
491 if (i == 0) 558 if (i == 0)
492 { /* matched */ 559 { /* matched */
493 int match_start = matched[0].rm_so; 560 int match_start = matched[0].rm_so;
494 int match_end = matched[0].rm_eo; 561 int match_end = matched[0].rm_eo;
495 int old_len = match_end - match_start; 562 int old_len = match_end - match_start;
496 int new_len = strlen (transform_to); 563 int new_len = strlen (transform_to);
497 int old_whole_len = strlen (s); 564 int old_whole_len = strlen (s);
498 565
499 printf ("regexp was matched by '%s' - replace with '%s'\n", s, transform_to); 566 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); 567 printf ("match is from %d to %d\n", match_start, match_end);
501 if (new_len > old_len) 568 if (new_len > old_len)
502 s = xrealloc(s, old_whole_len + new_len - old_len); 569 s = xrealloc (s, old_whole_len + new_len - old_len);
503
504 if (new_len != old_len)
505 { 570
571 if (new_len != old_len)
572 {
506 memcpy(s + match_end + new_len - old_len, 573 memcpy (s + match_end + new_len - old_len,
507 s + match_end, 574 s + match_end,
508 old_whole_len - match_end); 575 old_whole_len - match_end);
509 s[old_whole_len + new_len - old_len] = '\0'; 576 s[old_whole_len + new_len - old_len] = '\0';
510 } 577 }
511 578
512 memcpy (s + match_start, 579 memcpy (s + match_start,
513 transform_to, 580 transform_to,
514 new_len); 581 new_len);
515 printf ("transformed to '%s'\n", s); 582 printf ("transformed to '%s'\n", s);
516 } 583 }
517 else 584 else
518 {
519 printf ("regexp was not matched by '%s'\n", s); 585 printf ("regexp was not matched by '%s'\n", s);
520 }
521 } 586 }
522} 587}
523#endif 588#endif
524 589
590/*
591 * appends p2 to the end of p1, if p1 is not null
592 * otherwise allocates a new string and copies p2 to it
593 */
525char * 594char *
526concat_line (const char *p1, const char *p2) 595concat_line (char *p1, const char *p2)
527{ 596{
597 assert(p2);
598
528 int l1 = p1 ? strlen (p1) : 0; 599 int l1 = p1 ? strlen (p1) : 0;
529 int l2 = strlen (p2); 600 int l2 = strlen (p2);
530 char *r = xmalloc (l1 + l2 + 1); 601 char *r;
531 602
532 memcpy (r, p1, l1); 603 if (p1)
604 r = xrealloc(p1, l1 + l2 + 1);
605 else
606 r = xmalloc (l2 + 1);
607
533 memcpy (r + l1, p2, l2); 608 memcpy (r + l1, p2, l2);
534 r[l1 + l2] = 0; 609 r[l1 + l2] = 0;
535 610
536 return r; 611 return r;
537} 612}
553 * until we've seen how (ie. whether) it ends */ 628 * until we've seen how (ie. whether) it ends */
554 do 629 do
555 { 630 {
556 p = buff; 631 p = buff;
557 do 632 do
558 { 633 {
559 ch = fgetc (logfile->fp); 634 ch = fgetc (logfile->fp);
560 635
561 if (ch == '\n' || ch == EOF) 636 if (ch == '\n' || ch == EOF)
562 break; 637 break;
563 else if (ch == '\r') 638 else if (ch == '\r')
564 continue; /* skip */ 639 continue; /* skip */
565 else if (ch == '\t') 640 else if (ch == '\t')
566 { 641 {
567 do 642 do
568 { 643 {
569 *p++ = ' '; 644 *p++ = ' ';
570 ofs++; 645 ofs++;
571 } 646 }
572 while (ofs & 7); 647 while (ofs & 7);
573 } 648 }
574 else 649 else
575 { 650 {
576 *p++ = ch; 651 *p++ = ch;
577 ofs++; 652 ofs++;
578 } 653 }
579 } 654 }
580 while (p < buff + (sizeof buff) - 8 - 1); 655 while (p < buff + (sizeof buff) - 8 - 1);
581 656
582 if (p == buff && ch == EOF) 657 if (p == buff && ch == EOF)
583 return 0; 658 return 0;
584 659
585 *p = 0; 660 *p = 0;
586 661
587 p = concat_line (logfile->buf, buff); 662 p = logfile->buf = concat_line (logfile->buf, buff);
588 free (logfile->buf); logfile->buf = p;
589 } 663 }
590 while (ch != '\n' && ch != EOF); 664 while (ch != '\n' && ch != EOF);
591 665
592 logfile->lastpartial = logfile->partial; 666 logfile->lastpartial = logfile->partial;
593 /* there are 3 ways we could have exited the loop: reading '\n', 667 /* there are 3 ways we could have exited the loop: reading '\n',
631 file->inode = stats.st_ino; 705 file->inode = stats.st_ino;
632 706
633 if (opt_noinitial) 707 if (opt_noinitial)
634 fseek (file->fp, 0, SEEK_END); 708 fseek (file->fp, 0, SEEK_END);
635 else if (stats.st_size > (listlen + 1) * width) 709 else if (stats.st_size > (listlen + 1) * width)
710 /* HACK - 'width' is in pixels - how are we to know how much text will fit? */
636 fseek (file->fp, -((listlen + 2) * width), SEEK_END); 711 fseek (file->fp, -((listlen + 2) * width/10), SEEK_END);
637 712
638 file->last_size = stats.st_size; 713 file->last_size = stats.st_size;
639 return file->fp; 714 return file->fp;
640} 715}
641 716
697 } 772 }
698 } 773 }
699} 774}
700 775
701/* 776/*
702 * insert a single physical line (that must be short enough to fit) 777 * insert a single node in the list of screen lines and return a
703 * at position "idx" by pushing up lines above it. the caller 778 * pointer to the new node.
704 * MUST then fill in lines[idx] with valid data. 779 * the caller MUST then fill in ret->line and ret->len with valid
780 * data.
781 */
782static struct line_node *
783new_line_node (struct logfile_entry *log)
784{
785 struct line_node *new = xmalloc (sizeof (struct line_node));
786
787 new->logfile = log;
788 new->wrapped_left = 0;
789 new->wrapped_right = 0;
790 new->breaks = 0;
791
792 assert(log);
793
794 if (!log || !log->last)
795 {
796 new->next = linelist;
797 new->next->prev = new;
798
799 new->prev = NULL;
800 linelist = new;
801 }
802 else
803 {
804 /* 2 pointers from the new node */
805 new->next = log->last;
806 new->prev = log->last->prev;
807
808 /* 2 pointers back to the new node */
809 if (new->next) new->next->prev = new;
810 if (new->prev) new->prev->next = new;
811
812 /* if this is a new first entry in the list then update
813 * 'linelist' */
814 if (log->last == linelist)
815 linelist = new;
816 }
817
818 /* update the logfile record */
819 if (log)
820 log->last = new;
821
822 return new;
823}
824
825/*
826 * this is called after either adding a new line or appending to an
827 * old one. in both cases it's possible that the line no longer fits,
828 * and needs wrapping. this function checks the last line associated
829 * with the supplied logfile.
705 */ 830 */
706static void 831static void
707insert_line (int idx) 832possibly_split_long_line (struct logfile_entry *log)
708{ 833{
709 int cur_line; 834 char *str = log->last->line;
710 struct logfile_entry *current;
711
712 free (lines[0].line);
713
714 for (cur_line = 0; cur_line < idx; cur_line++)
715 lines[cur_line] = lines[cur_line + 1];
716
717 for (current = loglist; current; current = current->next)
718 if (current->index <= idx)
719 current->index--;
720}
721
722/*
723 * remove a single physical line at position "idx" by moving the lines above it
724 * down and inserting a "~" line at the top.
725 */
726static void
727delete_line (int idx)
728{
729 int cur_line;
730 struct logfile_entry *current;
731
732 free (lines[idx].line);
733
734 for (cur_line = idx; cur_line > 0; cur_line--)
735 lines[cur_line] = lines[cur_line - 1];
736
737 lines[0].line = xstrdup ("~");
738
739 for (current = loglist; current; current = current->next)
740 if (current->index >= 0 && current->index <= idx)
741 current->index++;
742}
743
744/*
745 * takes a logical log file line and splits it into multiple physical
746 * screen lines by splitting it whenever a part becomes too long.
747 * lal lines will be inserted at position "idx".
748 */
749static void
750split_line (int idx, const char *str, unsigned long color)
751{
752 int l = strlen (str); 835 int l = strlen (str);
753 int last_wrapped = 0;
754 const char *p = str; 836 char *p = str;
755 static int continuation_width = -1; 837 struct line_node *line;
756 static int continuation_length; 838 int spaces;
839 static struct breakinfo *breaks;
840 static int break_buffer_size;
757 841
758 /* only calculate the continuation's width once */ 842 /* only calculate the continuation's width once */
759 if (continuation_width == -1) 843 if (continuation_width == -1)
760 { 844 {
761 continuation_length = strlen (continuation); 845 continuation_length = strlen (continuation);
762 continuation_width = XmbTextEscapement (fontset, continuation, continuation_length); 846 continuation_width = XmbTextEscapement (fontset, continuation, continuation_length);
847 continuation_color = GetColor (cont_color);
848
849 /* make an array to store information about the location of
850 * spaces in the line */
851 if (opt_justify)
852 {
853 break_buffer_size = 32;
854 breaks = xmalloc (break_buffer_size * sizeof (struct breakinfo));
855 }
763 } 856 }
764 857
765 do 858 do
766 { 859 {
767 const char *beg = p; 860 const char *beg = p;
768 int w = last_wrapped ? continuation_width : 0; 861 int start_w = log->last->wrapped_left ? continuation_width : 0;
862 int w = start_w;
769 int wrapped = 0; 863 int wrapped = 0;
770 const char *break_p = NULL; 864 char *break_p = NULL;
865 int width_at_break_p;
866 spaces = 0;
867
868 if (opt_justify)
869 breaks[spaces].index = breaks[spaces].width = 0;
771 870
772 while (*p) 871 while (*p)
773 { 872 {
774 int cw, len; 873 int cw, len;
775 874
776 /* find the length in bytes of the next multibyte character */ 875 /* find the length in bytes of the next multibyte character */
777 len = mblen (p, l); 876 len = mblen (p, l);
778 if (len <= 0) 877 if (len <= 0)
779 len = 1; /* ignore (don't skip) illegal character sequences */ 878 len = 1; /* ignore (don't skip) illegal character sequences */
780 879
781 /* find the width in pixels of the next character */ 880 /* find the width in pixels of the next character */
782 cw = XmbTextEscapement (fontset, p, len); 881 cw = XmbTextEscapement (fontset, p, len);
882
883 if (opt_wordwrap && len == 1 && p[0] == ' ' && p != break_p + 1)
884 {
885 break_p = p;
886 width_at_break_p = w;
887 spaces++;
888
889 if (opt_justify)
890 {
891 /* increase the size of the 'breaks' array when
892 * necessary */
893 if (spaces >= break_buffer_size)
894 {
895 break_buffer_size *= 1.5;
896 breaks = xrealloc (breaks, break_buffer_size * sizeof (struct breakinfo));
897 }
898
899 /* store information about (a) the location of each
900 * space */
901 breaks[spaces].index = p + 1 - beg;
902 /* (b) the width (in pixels) of the string up to
903 * this space */
904 breaks[spaces].width = cw + w - start_w;
905 /* (c) the length of each 'word' */
906 breaks[spaces-1].len = breaks[spaces].index - breaks[spaces-1].index;
907 }
908 }
909
783 if (cw + w > width - effect_x_space) 910 if (cw + w > width - effect_x_space)
784 { 911 {
785 if (p == beg) 912 if (p == beg)
786 { 913 {
787 fprintf (stderr, "we can't even fit a single character onto the line\n"); 914 fprintf (stderr, "we can't even fit a single character onto the line\n");
788 if (len == 1) fprintf (stderr, "(the character we couldn't fit was '%c')\n", *p); 915 if (len == 1) fprintf (stderr, "(the character we couldn't fit was '%c')\n", *p);
789 exit (1); 916 exit (1);
790 } 917 }
791 918
792 wrapped = 1; 919 wrapped = 1;
793 break; 920 break;
794 } 921 }
795
796 if (opt_wordwrap && len == 1 && p[0] == ' ')
797 break_p = p;
798 922
799 w += cw; 923 w += cw;
800 p += len; 924 p += len;
801 l -= len; 925 l -= len;
802 } 926 }
803 927
804 /* if we're wrapping at spaces, and the line is long enough to 928 /* if we're wrapping at spaces, and the line is long enough to
805 * wrap, and we've seen a space already, and the space wasn't 929 * wrap, and we've seen a space already, and the space wasn't
806 * the first character on the line, then wrap at the space */ 930 * the first character on the line, then wrap at the space */
807 if (opt_wordwrap && wrapped && break_p && break_p != beg) 931 if (!wrapped)
808 { 932 break;
809 l += p - break_p;
810 p = break_p;
811 }
812
813 { 933
814 int len = p - beg + (last_wrapped ? continuation_length : 0); 934 int prefix_len;
815 char *s = xmalloc (len + 1);
816 if (last_wrapped)
817 {
818 memcpy (s, continuation, continuation_length);
819 memcpy (s + continuation_length, beg, p - beg);
820 }
821 else
822 memcpy (s, beg, len);
823 935
824 s[len] = 0; 936 /* choose where to break the line */
825 insert_line (idx);
826 lines[idx].line = s;
827 lines[idx].len = len;
828 lines[idx].color = color;
829 }
830
831 /* if we wrapped at a space, don't display the space */
832 if (opt_wordwrap && wrapped && break_p && break_p != beg) 937 if (opt_wordwrap && break_p && break_p != beg)
833 { 938 {
834 l--; 939 prefix_len = break_p - beg;
835 p++; 940 p = break_p;
836 } 941 w = width_at_break_p;
837 942
838 last_wrapped = wrapped; 943 /* if breaking at a space, skip all adjacent spaces */
944 while (*p == ' ')
945 {
946 int len = mblen (p, l);
947 if (len != 1) break;
948 p++;
949 }
950
951 if (opt_justify)
952 {
953 spaces--;
954 breaks[spaces].len--;
955 }
956 }
957 else
958 prefix_len = p - beg;
959
960 /* make a copy of the tail end of the string */
961 p = xstrdup (p);
962
963 /* and reduce the size of the head of the string */
964 log->last->line = xrealloc (log->last->line, prefix_len + 1);
965 log->last->len = prefix_len;
966 log->last->line[prefix_len] = '\0';
967
968 /* note that the head was wrapped on it's right */
969 log->last->wrapped_right = 1;
970
971 /* 'spaces' includes any space we broke on; we can only justify
972 * if there's at least one other space */
973 if (opt_justify && spaces &&
974 width - effect_x_space - width_at_break_p < spaces * font_height)
975 {
976 int i;
977 log->last->free_pixels = width - w;
978 log->last->num_words = spaces + 1;
979 log->last->breaks = malloc (log->last->num_words * sizeof (struct breakinfo));
980 for (i = 0; i < log->last->num_words; i++)
981 log->last->breaks[i] = breaks[i];
982 }
983
984 line = new_line_node (log);
985 line->line = p;
986 l = line->len = strlen (p);
987
988 /* note that the tail end of the string is wrapped at its left */
989 line->wrapped_left = 1;
839 } 990 }
840 while (l); 991 while (l);
992}
993
994static void
995insert_new_line (char *str, struct logfile_entry *log)
996{
997 struct line_node *new;
998 new = new_line_node (log);
999 new->line = str;
1000 new->len = strlen (str);
1001
1002 possibly_split_long_line (log);
841} 1003}
842 1004
843/* 1005/*
844 * append something to an existing physical line. this is done 1006 * append something to an existing physical line. this is done
845 * by deleting the file on-screen, concatenating the new data to it 1007 * by deleting the file on-screen, concatenating the new data to it
846 * and splitting it again. 1008 * and splitting it again.
847 */ 1009 */
848static void 1010static void
849append_line (int idx, const char *str) 1011append_to_existing_line (char *str, struct logfile_entry *log)
850{ 1012{
851 unsigned long color = lines[idx].color; 1013 char *old, *new;
852 char *old = lines[idx].line; 1014
1015 assert(log);
1016 assert(log->last);
1017
1018 old = log->last->line;
1019 assert(old);
1020
853 char *new = concat_line (old, str); 1021 new = concat_line (old, str);
854
855 delete_line (idx);
856 split_line (idx, new, color);
857 free (new); 1022 free (str);
1023 log->last->line = new;
1024 log->last->len = strlen (new);
1025 possibly_split_long_line (log);
858} 1026}
859 1027
860static void 1028static void
861main_loop (void) 1029main_loop (void)
862{ 1030{
866 XEvent xev; 1034 XEvent xev;
867 struct logfile_entry *lastprinted = NULL; 1035 struct logfile_entry *lastprinted = NULL;
868 struct logfile_entry *current; 1036 struct logfile_entry *current;
869 int need_update = 1; 1037 int need_update = 1;
870 1038
871 lines = xmalloc (sizeof (struct linematrix) * listlen);
872 display = xmalloc (sizeof (struct displaymatrix) * listlen); 1039 display = xmalloc (sizeof (struct displaymatrix) * listlen);
873 1040
874 lastreload = time (NULL); 1041 lastreload = time (NULL);
875 1042
876 /* Initialize linematrix */ 1043 /* Initialize line_node */
877 for (lin = 0; lin < listlen; lin++) 1044 for (lin = 0; lin < listlen; lin++)
878 { 1045 {
1046 struct line_node *e = xmalloc (sizeof (struct line_node));
879 lines[lin].line = xstrdup ("~"); 1047 e->line = xstrdup ("~");
880 lines[lin].len = 1; 1048 e->len = 1;
1049 e->logfile = loglist; /* this is only needed to get a color for the '~' */
1050 e->wrapped_left = 0;
1051 e->wrapped_right = 0;
1052 e->breaks = 0;
1053 e->next = NULL;
1054 e->prev = linelist_tail;
1055
1056 if (!linelist)
1057 linelist = e;
1058 if (linelist_tail)
1059 linelist_tail->next = e;
1060 linelist_tail = e;
1061
881 display[lin].line = xstrdup(""); 1062 display[lin].line = xstrdup ("");
882 display[lin].len = 0; 1063 display[lin].len = 0;
883 display[lin].buffer_size = 0; 1064 display[lin].buffer_size = 0;
884 lines[lin].color = GetColor (def_color);
885 } 1065 }
886 1066
887 for (;;) 1067 for (;;)
888 { 1068 {
889 /* read logs */ 1069 /* read logs */
900 /* if we're trying to update old partial lines in 1080 /* if we're trying to update old partial lines in
901 * place, and the last time this file was updated the 1081 * place, and the last time this file was updated the
902 * output was partial, and that partial line is not 1082 * output was partial, and that partial line is not
903 * too close to the top of the screen, then update 1083 * too close to the top of the screen, then update
904 * that partial line */ 1084 * that partial line */
905 if (opt_update && current->lastpartial && current->index >= 0) 1085 if (opt_update && current->lastpartial && current->last)
906 { 1086 {
907 int idx = current->index; 1087 // int idx = current->index;
908 append_line (idx, current->buf); 1088 append_to_existing_line (current->buf, current);
909 current->index = idx; 1089 // current->index = idx;
910 free (current->buf), current->buf = 0; 1090 current->buf = 0;
1091 continue;
1092 }
1093
1094 /* if all we just read was a newline ending a line that we've already displayed, skip it */
1095 if (current->buf[0] == '\0' && current->lastpartial)
1096 {
1097 current->buf = 0;
911 continue; 1098 continue;
912 } 1099 }
913 1100
914 /* print filename if any, and if last line was from 1101 /* print filename if any, and if last line was from
915 * different file */ 1102 * different file */
916 if (!opt_nofilename && lastprinted != current && current->desc[0]) 1103 if (!opt_nofilename && lastprinted != current && current->desc[0])
917 { 1104 {
918 split_line (listlen - 1, "[", current->color); 1105 current->last = 0;
919 append_line (listlen - 1, current->desc); 1106 insert_new_line (xstrdup ("["), current);
920 append_line (listlen - 1, "]"); 1107 append_to_existing_line (xstrdup (current->desc), current);
1108 append_to_existing_line (xstrdup ("]"), current);
921 } 1109 }
922 1110
923 /* if we're dealing with partial lines, and the last 1111 /* if we're dealing with partial lines, and the last
924 * time we showed the line it wasn't finished ... */ 1112 * time we showed the line it wasn't finished ... */
925 if (!opt_whole && current->lastpartial) 1113 if (!opt_whole && current->lastpartial)
926 { 1114 {
927 /* if this is the same file we showed last then 1115 /* if this is the same file we showed last then
928 append to the last line shown */ 1116 append to the last line shown */
929 if (lastprinted == current) 1117 if (lastprinted == current)
930 append_line (listlen - 1, current->buf); 1118 append_to_existing_line (current->buf, current);
931 else 1119 else
932 { 1120 {
933 /* but if a different file has been shown in the 1121 /* but if a different file has been shown in the
934 * mean time, make a new line, starting with the 1122 * mean time, make a new line, starting with the
935 * continuation string */ 1123 * continuation string */
936 split_line (listlen - 1, continuation, current->color); 1124 insert_new_line (current->buf, current);
937 append_line (listlen - 1, current->buf); 1125 current->last->wrapped_left = 1;
938 } 1126 }
939 } 1127 }
940 else 1128 else
941 /* otherwise just make a plain and simple new line */ 1129 /* otherwise just make a plain and simple new line */
942 split_line (listlen - 1, current->buf, current->color); 1130 insert_new_line (current->buf, current);
943 1131
944 free (current->buf), current->buf = 0; 1132 current->buf = 0;
945 current->index = listlen - 1; 1133 // current->index = listlen - 1;
946 lastprinted = current; 1134 lastprinted = current;
947 } 1135 }
948 } 1136 }
949 1137
950 if (need_update) 1138 if (need_update)
1061 XParseGeometry (argv[++i], &win_x, &win_y, &width, &height); 1249 XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
1062 else if (!strcmp (arg, "-display")) 1250 else if (!strcmp (arg, "-display"))
1063 dispname = argv[++i]; 1251 dispname = argv[++i];
1064 else if (!strcmp (arg, "-cont")) 1252 else if (!strcmp (arg, "-cont"))
1065 continuation = argv[++i]; 1253 continuation = argv[++i];
1254 else if (!strcmp (arg, "-cont-color"))
1255 cont_color = argv[++i];
1066 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) 1256 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
1067 fontname = argv[++i]; 1257 fontname = argv[++i];
1068#if HAS_REGEX 1258#if HAS_REGEX
1069 else if (!strcmp (arg, "-t")) 1259 else if (!strcmp (arg, "-t"))
1070 { 1260 {
1071 transform = argv[++i]; 1261 transform = argv[++i];
1072 transform_to = argv[++i]; 1262 transform_to = argv[++i];
1073 printf("transform: '%s' to '%s'\n", transform, transform_to); 1263 printf ("transform: '%s' to '%s'\n", transform, transform_to);
1074 } 1264 }
1075#endif 1265#endif
1076 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) 1266 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
1077 opt_daemonize = 1; 1267 opt_daemonize = 1;
1078 else if (!strcmp (arg, "-reload")) 1268 else if (!strcmp (arg, "-reload"))
1079 { 1269 {
1098 opt_partial = 1; 1288 opt_partial = 1;
1099 else if (!strcmp (arg, "-update")) 1289 else if (!strcmp (arg, "-update"))
1100 opt_update = opt_partial = 1; 1290 opt_update = opt_partial = 1;
1101 else if (!strcmp (arg, "-wordwrap")) 1291 else if (!strcmp (arg, "-wordwrap"))
1102 opt_wordwrap = 1; 1292 opt_wordwrap = 1;
1293 else if (!strcmp (arg, "-justify"))
1294 opt_justify = 1;
1103 else if (!strcmp (arg, "-color")) 1295 else if (!strcmp (arg, "-color"))
1104 def_color = argv[++i]; 1296 def_color = argv[++i];
1105 else if (!strcmp (arg, "-noinitial")) 1297 else if (!strcmp (arg, "-noinitial"))
1106 opt_noinitial = 1; 1298 opt_noinitial = 1;
1107 else if (!strcmp (arg, "-id")) 1299 else if (!strcmp (arg, "-id"))
1143 } 1335 }
1144 1336
1145 e = xmalloc (sizeof (struct logfile_entry)); 1337 e = xmalloc (sizeof (struct logfile_entry));
1146 e->partial = 0; 1338 e->partial = 0;
1147 e->buf = 0; 1339 e->buf = 0;
1148 e->index = -1; 1340 // e->index = -1;
1149 1341
1150 if (arg[0] == '-' && arg[1] == '\0') 1342 if (arg[0] == '-' && arg[1] == '\0')
1151 { 1343 {
1152 if ((e->fp = fdopen (0, "r")) == NULL) 1344 if ((e->fp = fdopen (0, "r")) == NULL)
1153 perror ("fdopen"), exit (1); 1345 perror ("fdopen"), exit (1);
1168 e->desc = xstrdup (desc); 1360 e->desc = xstrdup (desc);
1169 } 1361 }
1170 1362
1171 e->color = GetColor (fcolor); 1363 e->color = GetColor (fcolor);
1172 e->partial = 0; 1364 e->partial = 0;
1365 e->last = NULL;
1173 e->next = NULL; 1366 e->next = NULL;
1174 1367
1175 if (!loglist) 1368 if (!loglist)
1176 loglist = e; 1369 loglist = e;
1177 if (loglist_tail) 1370 if (loglist_tail)
1196 else if (opt_partial && opt_whole) 1389 else if (opt_partial && opt_whole)
1197 { 1390 {
1198 fprintf (stderr, "Specify at most one of -partial and -whole\n"); 1391 fprintf (stderr, "Specify at most one of -partial and -whole\n");
1199 exit (1); 1392 exit (1);
1200 } 1393 }
1394
1395 /* it doesn't make sense to justify if word wrap isn't on */
1396 if (opt_justify)
1397 opt_wordwrap = 1;
1201 1398
1202 /* HACK-7: do we want to allow both -shade and -outline? */ 1399 /* HACK-7: do we want to allow both -shade and -outline? */
1203 if (opt_shade && opt_outline) 1400 if (opt_shade && opt_outline)
1204 { 1401 {
1205 fprintf (stderr, "Specify at most one of -shade and -outline\n"); 1402 fprintf (stderr, "Specify at most one of -shade and -outline\n");
1216#if HAS_REGEX 1413#if HAS_REGEX
1217 if (transform) 1414 if (transform)
1218 { 1415 {
1219 int i; 1416 int i;
1220 1417
1221 printf("compiling regexp '%s'\n", transform); 1418 printf ("compiling regexp '%s'\n", transform);
1222 transformre = xmalloc (sizeof (regex_t)); 1419 transformre = xmalloc (sizeof (regex_t));
1223 i = regcomp (transformre, transform, REG_EXTENDED); 1420 i = regcomp (transformre, transform, REG_EXTENDED);
1224 if (i != 0) 1421 if (i != 0)
1225 { 1422 {
1226 char buf[512]; 1423 char buf[512];
1227 1424
1228 regerror (i, transformre, buf, sizeof (buf)); 1425 regerror (i, transformre, buf, sizeof (buf));
1229 fprintf (stderr, "Cannot compile regular expression: %s\n", buf); 1426 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1230 } 1427 }
1231 else 1428 else
1232 {
1233 printf("compiled '%s' OK to %x\n", transform, (int)transformre); 1429 printf ("compiled '%s' OK to %x\n", transform, (int)transformre);
1234 }
1235 } 1430 }
1236#endif 1431#endif
1237 1432
1238 InitWindow (); 1433 InitWindow ();
1239 1434
1260 action.sa_handler = handler; 1455 action.sa_handler = handler;
1261 sigemptyset (&action.sa_mask); 1456 sigemptyset (&action.sa_mask);
1262 action.sa_flags = SA_RESTART; 1457 action.sa_flags = SA_RESTART;
1263 1458
1264 if (sigaction (sig, &action, NULL) < 0) 1459 if (sigaction (sig, &action, NULL) < 0)
1265 fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1); 1460 fprintf (stderr, "sigaction (%d): %s\n", sig, strerror (errno)), exit (1);
1266} 1461}
1267 1462
1268void * 1463void *
1269xstrdup (const char *string) 1464xstrdup (const char *string)
1270{ 1465{
1271 void *p; 1466 void *p;
1272 1467
1273 while ((p = strdup (string)) == NULL) 1468 while ((p = strdup (string)) == NULL)
1274 { 1469 {
1275 fprintf (stderr, "Memory exhausted in xstrdup().\n"); 1470 fprintf (stderr, "Memory exhausted in xstrdup ().\n");
1276 sleep (10); 1471 sleep (10);
1277 } 1472 }
1278 1473
1279 return p; 1474 return p;
1280} 1475}
1284{ 1479{
1285 void *p; 1480 void *p;
1286 1481
1287 while ((p = malloc (size)) == NULL) 1482 while ((p = malloc (size)) == NULL)
1288 { 1483 {
1289 fprintf (stderr, "Memory exhausted in xmalloc().\n"); 1484 fprintf (stderr, "Memory exhausted in xmalloc ().\n");
1290 sleep (10); 1485 sleep (10);
1291 } 1486 }
1292 1487
1293 return p; 1488 return p;
1294} 1489}
1298{ 1493{
1299 void *p; 1494 void *p;
1300 1495
1301 while ((p = realloc (ptr, size)) == NULL) 1496 while ((p = realloc (ptr, size)) == NULL)
1302 { 1497 {
1303 fprintf (stderr, "Memory exhausted in xrealloc().\n"); 1498 fprintf (stderr, "Memory exhausted in xrealloc ().\n");
1304 sleep (10); 1499 sleep (10);
1305 } 1500 }
1306 1501
1307 return p; 1502 return p;
1308} 1503}
1354 case -1: 1549 case -1:
1355 return -1; 1550 return -1;
1356 case 0: 1551 case 0:
1357 break; 1552 break;
1358 default: 1553 default:
1359 /*printf("%d\n", pid);*/ 1554 /*printf ("%d\n", pid);*/
1360 exit (0); 1555 exit (0);
1361 } 1556 }
1362 1557
1363 if (setsid () == -1) 1558 if (setsid () == -1)
1364 return -1; 1559 return -1;
1365 1560
1366 return 0; 1561 return 0;
1367} 1562}
1563
1564/* todo - get reverse display working again */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines