ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.32
Committed: Tue Mar 30 23:33:59 2004 UTC (20 years, 1 month ago) by chris_moore
Content type: text/plain
Branch: MAIN
Changes since 1.31: +10 -8 lines
Log Message:
Commented the 7 places that need attention.

File Contents

# Content
1 /*
2 * Copyright 2001 by Marco d'Itri <md@linux.it>
3 * Copyright 2000,2001,2002,2003,2004
4 * Marc Lehmann <pcg@goof.com>,
5 * and many others, see README
6 *
7 * Original version by Mike Baker.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
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.
22 */
23
24 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <sys/time.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <locale.h>
37 #include <ctype.h>
38 #include <stdarg.h>
39 #include <X11/Xlib.h>
40 #include <X11/Xatom.h>
41 #include <X11/Xutil.h>
42
43 #if HAS_REGEX
44 #include <regex.h>
45 #endif
46
47 #define SHADE_X 2
48 #define SHADE_Y 2
49
50 /* data structures */
51 struct logfile_entry
52 {
53 struct logfile_entry *next;
54
55 char *fname; /* name of file */
56 char *desc; /* alternative description */
57 char *buf; /* text read but not yet displayed */
58 FILE *fp; /* FILE struct associated with file */
59 ino_t inode; /* inode of the file opened */
60 off_t last_size; /* file size at the last check */
61 unsigned long color; /* color to be used for printing */
62 int partial; /* true if the last line isn't complete */
63 int lastpartial; /* true if the previous output wasn't complete */
64 int index; /* index into linematrix of a partial line */
65 int modified; /* true if line is modified & needs displaying */
66 };
67
68 struct linematrix
69 {
70 char *line;
71 int len;
72 unsigned long color;
73 };
74
75 struct displaymatrix
76 {
77 char *line;
78 int len;
79 int buffer_size;
80 };
81
82 /* global variables */
83 struct linematrix *lines;
84 struct displaymatrix *display;
85 int width = STD_WIDTH, height = STD_HEIGHT, listlen;
86 int win_x = LOC_X, win_y = LOC_Y;
87 int font_ascent, font_height;
88 int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */
89 int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
90 int do_reopen;
91 struct timeval interval = { 2, 400000 };
92 XFontSet fontset;
93
94 /* command line options */
95 int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
96 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
97 geom_mask, reload = 0;
98 const char *command = NULL,
99 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
100 *continuation = "[+]";
101
102 struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
103
104 Display *disp;
105 Window root;
106 GC WinGC;
107
108 #if HAS_REGEX
109 struct re_list
110 {
111 regex_t from;
112 const char *to;
113 struct re_list *next;
114 };
115 struct re_list *re_head, *re_tail;
116 char *transform_to = NULL;
117 regex_t *transformre;
118 #endif
119
120
121 /* prototypes */
122 void list_files (int);
123 void force_reopen (int);
124 void force_refresh (int);
125 void blank_window (int);
126
127 void InitWindow (void);
128 unsigned long GetColor (const char *);
129 void redraw (int);
130 void refresh (int, int, int, int);
131
132 void transform_line (char *s);
133 int lineinput (struct logfile_entry *);
134 void reopen (void);
135 void check_open_files (void);
136 FILE *openlog (struct logfile_entry *);
137 static void main_loop (void);
138
139 void display_version (void);
140 void display_help (char *);
141 void install_signal (int, void (*)(int));
142 void *xstrdup (const char *);
143 void *xmalloc (size_t);
144 void *xrealloc (void *, size_t);
145 int daemonize (void);
146
147 /* signal handlers */
148 void
149 list_files (int dummy)
150 {
151 struct logfile_entry *e;
152
153 fprintf (stderr, "Files opened:\n");
154 for (e = loglist; e; e = e->next)
155 fprintf (stderr, "\t%s (%s)\n", e->fname, e->desc);
156 }
157
158 void
159 force_reopen (int dummy)
160 {
161 do_reopen = 1;
162 }
163
164 void
165 force_refresh (int dummy)
166 {
167 redraw (1);
168 }
169
170 void
171 blank_window (int dummy)
172 {
173 XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False);
174 XFlush (disp);
175 exit (0);
176 }
177
178 /* X related functions */
179 unsigned long
180 GetColor (const char *ColorName)
181 {
182 XColor Color;
183 XWindowAttributes Attributes;
184
185 XGetWindowAttributes (disp, root, &Attributes);
186 Color.pixel = 0;
187
188 if (!XParseColor (disp, Attributes.colormap, ColorName, &Color))
189 fprintf (stderr, "can't parse %s\n", ColorName);
190 else if (!XAllocColor (disp, Attributes.colormap, &Color))
191 fprintf (stderr, "can't allocate %s\n", ColorName);
192
193 return Color.pixel;
194 }
195
196 static Window
197 root_window (Display * display, int screen_number)
198 {
199 Atom SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False);
200 Window real_root_window = RootWindow (display, screen_number);
201
202 if (root) /* root window set via option */
203 return root;
204
205 if (SWM_VROOT != None)
206 {
207 Window unused, *windows;
208 unsigned int count;
209
210 if (XQueryTree (display, real_root_window, &unused, &unused, &windows,
211 &count))
212 {
213 int i;
214
215 for (i = 0; i < count; i++)
216 {
217 Atom type;
218 int format;
219 unsigned long nitems, bytes_after_return;
220 unsigned char *virtual_root_window;
221
222 if (XGetWindowProperty (display, windows[i], SWM_VROOT,
223 0, 1, False, XA_WINDOW, &type, &format,
224 &nitems, &bytes_after_return,
225 &virtual_root_window) == Success)
226 {
227 if (type != None)
228 {
229 if (type == XA_WINDOW)
230 {
231 XFree (windows);
232 return (Window) virtual_root_window;
233 }
234 else
235 fprintf (stderr,
236 "__SWM_VROOT property type mismatch");
237 }
238 }
239 else
240 fprintf (stderr,
241 "failed to get __SWM_VROOT property on window 0x%lx",
242 windows[i]);
243 }
244
245 if (count)
246 XFree (windows);
247 }
248 else
249 fprintf (stderr, "Can't query tree on root window 0x%lx",
250 real_root_window);
251 }
252 else
253 /* This shouldn't happen. The Xlib documentation is wrong BTW. */
254 fprintf (stderr, "Can't intern atom __SWM_VROOT");
255
256 return real_root_window;
257 }
258
259 void
260 InitWindow (void)
261 {
262 XGCValues gcv;
263 unsigned long gcm;
264 int screen, ScreenWidth, ScreenHeight;
265
266 if (!(disp = XOpenDisplay (dispname)))
267 {
268 fprintf (stderr, "Can't open display %s.\n", dispname);
269 exit (1);
270 }
271
272 screen = DefaultScreen (disp);
273 ScreenHeight = DisplayHeight (disp, screen);
274 ScreenWidth = DisplayWidth (disp, screen);
275
276 root = root_window (disp, screen);
277
278 gcm = GCBackground;
279 gcv.graphics_exposures = True;
280 WinGC = XCreateGC (disp, root, gcm, &gcv);
281 XMapWindow (disp, root);
282 XSetForeground (disp, WinGC, GetColor (DEF_COLOR));
283
284 {
285 char **missing_charset_list;
286 int missing_charset_count;
287 char *def_string;
288
289 fontset = XCreateFontSet (disp, fontname,
290 &missing_charset_list, &missing_charset_count,
291 &def_string);
292
293 if (missing_charset_count)
294 {
295 fprintf (stderr,
296 "Missing charsets in String to FontSet conversion (%s)\n",
297 missing_charset_list[0]);
298 XFreeStringList (missing_charset_list);
299 }
300 }
301
302 if (!fontset)
303 {
304 fprintf (stderr, "unable to create fontset, exiting.\n");
305 exit (1);
306 }
307
308 {
309 XFontSetExtents *xfe = XExtentsOfFontSet (fontset);
310
311 font_height = xfe->max_logical_extent.height;
312 font_ascent = -xfe->max_logical_extent.y;
313 }
314
315 if (geom_mask & XNegative)
316 win_x = win_x + ScreenWidth - width;
317 if (geom_mask & YNegative)
318 win_y = win_y + ScreenHeight - height;
319
320 if (opt_outline)
321 {
322 /* adding outline increases the total width and height by 2
323 pixels each, and offsets the text one pixel right and one
324 pixel down */
325 effect_x_space = effect_y_space = 2;
326 effect_x_offset = effect_y_offset = 1;
327 }
328 else if (opt_shade)
329 {
330 /* adding a shadow increases the space used */
331 effect_x_space = abs(SHADE_X);
332 effect_y_space = abs(SHADE_Y);
333 /* if the shadow is to the right and below then we don't need
334 * to move the text to make space for it, but shadows to the left
335 * and above need accomodating */
336 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
337 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
338 }
339 else
340 {
341 effect_x_space = effect_y_space = 0;
342 effect_x_offset = effect_y_offset = 0;
343 }
344
345 /* if we are using -shade or -outline, there will be less usable
346 * space for output */
347 listlen = (height - effect_y_space) / font_height;
348
349 if (!listlen)
350 {
351 fprintf (stderr, "height too small for a single line, setting to %d\n",
352 font_height);
353 listlen = 1;
354 }
355
356 /* leave the height how the user requested it. it might not all be
357 * used, but this will allow the geometry to be tuned more accurately
358 * (with the -frame option)
359 * the old code did this:
360 * height = listlen * font_height + effect_y_space; */
361
362 XSelectInput (disp, root, ExposureMask | FocusChangeMask);
363 }
364
365 /*
366 * if redraw() is passwd a non-zero argument, it does a complete
367 * redraw, rather than an update. if the argument is zero (and
368 * -noflicker is in effect) then only the lines which have changed
369 * since the last draw are redrawn.
370 *
371 * the rest is handled by regular refresh()'es
372 */
373 void
374 redraw (int redraw_all)
375 {
376 XSetClipMask (disp, WinGC, None);
377 refresh (0, 32768, 1, redraw_all);
378 }
379
380 /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
381 void
382 refresh (int miny, int maxy, int clear, int refresh_all)
383 {
384 int lin;
385 int offset = listlen * font_height + font_ascent + effect_y_offset;
386 unsigned long black_color = GetColor ("black");
387
388 miny -= win_y + font_height;
389 maxy -= win_y - font_height;
390
391 if (clear && !opt_noflicker)
392 XClearArea (disp, root, win_x, win_y, width, height, False);
393
394 for (lin = listlen; lin--;)
395 {
396 struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin);
397 struct displaymatrix *display_line = display + lin;
398
399 offset -= font_height;
400
401 if (offset < miny || offset > maxy)
402 continue;
403
404 /* if this line is a different than it was, then it
405 * needs displaying */
406 if (!opt_noflicker
407 || refresh_all
408 || display_line->len != line->len
409 || memcmp (display_line->line, line->line, line->len))
410 {
411 /* don't bother updating the record of what has been
412 * displayed if -noflicker isn't in effect, since we redraw
413 * the whole display every time anyway */
414 if (opt_noflicker)
415 {
416 /* update the record of what has been displayed;
417 * first make sure the buffer is big enough */
418 if (display_line->buffer_size <= line->len)
419 {
420 display_line->buffer_size = line->len + 1;
421 display_line->line = xrealloc (display_line->line, display_line->buffer_size);
422 }
423
424 display_line->len = line->len;
425 memcpy (display_line->line, line->line, line->len);
426
427 if (clear)
428 XClearArea (disp, root, win_x, win_y + offset - font_ascent,
429 width + effect_x_space, font_height + effect_y_space, False);
430 }
431
432 if (opt_outline)
433 {
434 int x, y;
435 XSetForeground (disp, WinGC, black_color);
436
437 for (x = -1; x < 2; x += 2)
438 for (y = -1; y < 2; y += 2)
439 XmbDrawString (disp, root, fontset, WinGC,
440 win_x + effect_x_offset + x,
441 win_y + y + offset,
442 line->line, line->len);
443 }
444 else if (opt_shade)
445 {
446 XSetForeground (disp, WinGC, black_color);
447 XmbDrawString (disp, root, fontset, WinGC,
448 win_x + effect_x_offset + SHADE_X,
449 win_y + offset + SHADE_Y,
450 line->line, line->len);
451 }
452
453 XSetForeground (disp, WinGC, line->color);
454 XmbDrawString (disp, root, fontset, WinGC,
455 win_x + effect_x_offset,
456 win_y + offset,
457 line->line, line->len);
458 }
459 }
460
461 if (opt_frame)
462 {
463 XSetForeground (disp, WinGC, GetColor (def_color));
464 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
465 }
466 }
467
468 #if HAS_REGEX
469 void
470 transform_line (char *s)
471 {
472 #ifdef I_AM_Md
473 int i;
474 if (1)
475 {
476 for (i = 16; s[i]; i++)
477 s[i] = s[i + 11];
478 }
479 s[i + 1] = '\0';
480 #endif
481
482 if (transformre)
483 {
484 int i;
485 regmatch_t matched[16];
486
487 i = regexec (transformre, s, 16, matched, 0);
488 if (i == 0)
489 { /* matched */
490 int match_start = matched[0].rm_so;
491 int match_end = matched[0].rm_eo;
492 int old_len = match_end - match_start;
493 int new_len = strlen(transform_to);
494 int old_whole_len = strlen(s);
495 printf("regexp was matched by '%s' - replace with '%s'\n", s, transform_to);
496 printf("match is from %d to %d\n",
497 match_start, match_end);
498 if (new_len > old_len) {
499 s = xrealloc(s, old_whole_len + new_len - old_len);
500 }
501 if (new_len != old_len) {
502 memcpy(s + match_end + new_len - old_len,
503 s + match_end,
504 old_whole_len - match_end);
505 s[old_whole_len + new_len - old_len] = '\0';
506 }
507 memcpy(s + match_start,
508 transform_to,
509 new_len);
510 printf("transformed to '%s'\n", s);
511 }
512 else
513 {
514 printf("regexp was not matched by '%s'\n", s);
515 }
516 }
517 }
518 #endif
519
520 char *
521 concat_line (const char *p1, const char *p2)
522 {
523 int l1 = p1 ? strlen (p1) : 0;
524 int l2 = strlen (p2);
525 char *r = xmalloc (l1 + l2 + 1);
526
527 memcpy (r, p1, l1);
528 memcpy (r + l1, p2, l2);
529 r[l1 + l2] = 0;
530
531 return r;
532 }
533
534 /*
535 * HACK-1: This routine should read a single line, no matter how long.
536 */
537 int
538 lineinput (struct logfile_entry *logfile)
539 {
540 char buff[1024], *p = buff;
541 int ch;
542 /* HACK-2: add on the length of any partial line which we will be appending to */
543 int ofs = logfile->buf ? strlen (logfile->buf) : 0;
544
545 do
546 {
547 ch = fgetc (logfile->fp);
548
549 if (ch == '\n' || ch == EOF)
550 break;
551 else if (ch == '\r')
552 continue; /* skip */
553 else if (ch == '\t')
554 {
555 do
556 {
557 *p++ = ' ';
558 ofs++;
559 }
560 while (ofs & 7);
561 }
562 else
563 {
564 *p++ = ch;
565 ofs++;
566 }
567 }
568 while (p < buff + (sizeof buff) - 8 - 1);
569
570 if (p == buff && ch == EOF)
571 return 0;
572
573 *p = 0;
574
575 p = concat_line (logfile->buf, buff);
576 free (logfile->buf); logfile->buf = p;
577
578 logfile->lastpartial = logfile->partial;
579 /* there are 3 ways we could have exited the loop: reading '\n',
580 * reaching EOF, or filling the buffer; the 2nd and 3rd of these
581 * both result in a partial line */
582 logfile->partial = ch != '\n';
583
584 if (logfile->partial && opt_whole)
585 return 0;
586
587 #if HAS_REGEX
588 transform_line (logfile->buf);
589 #endif
590 return 1;
591 }
592
593 /* input: reads file->fname
594 * output: fills file->fp, file->inode
595 * returns file->fp
596 * in case of error, file->fp is NULL
597 */
598 FILE *
599 openlog (struct logfile_entry * file)
600 {
601 struct stat stats;
602
603 if ((file->fp = fopen (file->fname, "r")) == NULL)
604 {
605 file->fp = NULL;
606 return NULL;
607 }
608
609 fstat (fileno (file->fp), &stats);
610 if (S_ISFIFO (stats.st_mode))
611 {
612 if (fcntl (fileno (file->fp), F_SETFL, O_NONBLOCK) < 0)
613 perror ("fcntl"), exit (1);
614 file->inode = 0;
615 }
616 else
617 file->inode = stats.st_ino;
618
619 if (opt_noinitial)
620 fseek (file->fp, 0, SEEK_END);
621 else if (stats.st_size > (listlen + 1) * width)
622 fseek (file->fp, -((listlen + 2) * width), SEEK_END);
623
624 file->last_size = stats.st_size;
625 return file->fp;
626 }
627
628 void
629 reopen (void)
630 {
631 struct logfile_entry *e;
632
633 for (e = loglist; e; e = e->next)
634 {
635 if (!e->inode)
636 continue; /* skip stdin */
637
638 if (e->fp)
639 fclose (e->fp);
640 /* if fp is NULL we will try again later */
641 openlog (e);
642 }
643
644 do_reopen = 0;
645 }
646
647 void
648 check_open_files (void)
649 {
650 struct logfile_entry *e;
651 struct stat stats;
652
653 for (e = loglist; e; e = e->next)
654 {
655 if (!e->inode)
656 continue; /* skip stdin */
657
658 if (stat (e->fname, &stats) < 0)
659 { /* file missing? */
660 sleep (1);
661 if (e->fp)
662 fclose (e->fp);
663 if (openlog (e) == NULL)
664 continue;
665 }
666
667 /* HACK-3: stats can be uninitialised here (if the file didn't
668 * exist when stat() was called, but was recreated during the
669 * sleep(1)) */
670 if (stats.st_ino != e->inode)
671 { /* file renamed? */
672 if (e->fp)
673 fclose (e->fp);
674 if (openlog (e) == NULL)
675 continue;
676 }
677
678 if (stats.st_size < e->last_size)
679 { /* file truncated? */
680 fseek (e->fp, 0, SEEK_SET);
681 e->last_size = stats.st_size;
682 }
683 }
684 }
685
686 /*
687 * insert a single physical line (that must be short enough to fit)
688 * at position "idx" by pushing up lines above it. the caller
689 * MUST then fill in lines[idx] with valid data.
690 */
691 static void
692 insert_line (int idx)
693 {
694 int cur_line;
695 struct logfile_entry *current;
696
697 free (lines[0].line);
698
699 for (cur_line = 0; cur_line < idx; cur_line++)
700 lines[cur_line] = lines[cur_line + 1];
701
702 for (current = loglist; current; current = current->next)
703 if (current->index <= idx)
704 current->index--;
705 }
706
707 /*
708 * remove a single physical line at position "idx" by moving the lines above it
709 * down and inserting a "~" line at the top.
710 */
711 static void
712 delete_line (int idx)
713 {
714 int cur_line;
715 struct logfile_entry *current;
716
717 for (cur_line = idx; cur_line > 0; cur_line--)
718 lines[cur_line] = lines[cur_line - 1];
719
720 lines[0].line = xstrdup ("~");
721
722 for (current = loglist; current; current = current->next)
723 if (current->index >= 0 && current->index <= idx)
724 current->index++;
725 }
726
727 /*
728 * takes a logical log file line and splits it into multiple physical
729 * screen lines by splitting it whenever a part becomes too long.
730 * lal lines will be inserted at position "idx".
731 */
732 static void
733 split_line (int idx, const char *str, unsigned long color)
734 {
735 int l = strlen (str);
736 const char *p = str;
737
738 do
739 {
740 const char *beg = p;
741 int w = 0, wrapped = 0;
742 const char *break_p = NULL;
743
744 while (*p)
745 {
746 /* find the length in bytes of the next multibyte character */
747 int len = mblen (p, l);
748 if (len <= 0)
749 len = 1; /* ignore (don't skip) illegal character sequences */
750
751 /* find the width in pixels of the next character */
752 int cw = XmbTextEscapement (fontset, p, len);
753 if (cw + w > width - effect_x_space)
754 {
755 wrapped = 1;
756 break;
757 }
758
759 if (opt_wordwrap && len == 1 && p[0] == ' ')
760 break_p = p;
761
762 w += cw;
763 p += len;
764 l -= len;
765 }
766
767 /* if we're wrapping at spaces, and the line is long enough to
768 * wrap, and we've seen a space already, and the space wasn't
769 * the first character on the line, then wrap at the space */
770 if (opt_wordwrap && wrapped && break_p && break_p != beg)
771 {
772 l += p - break_p;
773 p = break_p;
774 }
775
776 {
777 /* HACK-4 - consider inserting the 'continuation string'
778 * before the rest of the wrapped line */
779 char *s = xmalloc (p - beg + 1);
780 memcpy (s, beg, p - beg);
781 s[p - beg] = 0;
782 insert_line (idx);
783 lines[idx].line = s;
784 lines[idx].len = p - beg;
785 lines[idx].color = color;
786 }
787
788 /* if we wrapped at a space, don't display the space */
789 if (opt_wordwrap && wrapped && break_p && break_p != beg)
790 {
791 l--;
792 p++;
793 }
794 }
795 while (l);
796 }
797
798 /*
799 * append something to an existing physical line. this is done
800 * by deleting the file on-screen, concatenating the new data to it
801 * and splitting it again.
802 */
803 static void
804 append_line (int idx, const char *str)
805 {
806 unsigned long color = lines[idx].color;
807 char *old = lines[idx].line;
808 char *new = concat_line (old, str);
809
810 free (old);
811
812 delete_line (idx);
813 split_line (idx, new, color);
814 }
815
816 static void
817 main_loop (void)
818 {
819 lines = xmalloc (sizeof (struct linematrix) * listlen);
820 display = xmalloc (sizeof (struct displaymatrix) * listlen);
821 int lin;
822 time_t lastreload;
823 Region region = XCreateRegion ();
824 XEvent xev;
825 struct logfile_entry *lastprinted = NULL;
826 struct logfile_entry *current;
827 int need_update = 1;
828
829 lastreload = time (NULL);
830
831 /* Initialize linematrix */
832 for (lin = 0; lin < listlen; lin++)
833 {
834 lines[lin].line = xstrdup ("~");
835 lines[lin].len = 1;
836 display[lin].line = xstrdup("");
837 display[lin].len = 0;
838 display[lin].buffer_size = 1;
839 lines[lin].color = GetColor (def_color);
840 }
841
842 for (;;)
843 {
844 /* read logs */
845 for (current = loglist; current; current = current->next)
846 {
847 if (!current->fp)
848 continue; /* skip missing files */
849
850 clearerr (current->fp);
851
852 while (lineinput (current))
853 {
854 need_update = 1;
855 /* if we're trying to update old partial lines in
856 * place, and the last time this file was updated the
857 * output was partial, and that partial line is not
858 * too close to the top of the screen, then update
859 * that partial line */
860 if (opt_update && current->lastpartial && current->index >= 0)
861 {
862 int idx = current->index;
863 append_line (idx, current->buf);
864 current->index = idx;
865 free (current->buf), current->buf = 0;
866 continue;
867 }
868
869 /* print filename if any, and if last line was from
870 * different file */
871 if (!opt_nofilename && lastprinted != current && current->desc[0])
872 {
873 char buf[1024]; /* HACK-5 */
874 snprintf (buf, sizeof (buf), "[%s]", current->desc);
875 split_line (listlen - 1, buf, current->color);
876 }
877
878 /* if we're dealing with partial lines, and the last
879 * time we showed the line it wasn't finished ... */
880 if (!opt_whole && current->lastpartial)
881 {
882 /* if this is the same file we showed last then
883 append to the last line shown */
884 if (lastprinted == current)
885 append_line (listlen - 1, current->buf);
886 else
887 {
888 /* but if a different file has been shown in the
889 * mean time, make a new line, starting with the
890 * continuation string */
891 split_line (listlen - 1, continuation, current->color);
892 append_line (listlen - 1, current->buf);
893 }
894 }
895 else
896 /* otherwise just make a plain and simple new line */
897 split_line (listlen - 1, current->buf, current->color);
898
899 free (current->buf), current->buf = 0;
900 current->index = listlen - 1;
901 lastprinted = current;
902 }
903 }
904
905 if (need_update)
906 {
907 redraw (0);
908 need_update = 0;
909 }
910 else
911 {
912 XFlush (disp);
913
914 if (!XPending (disp))
915 {
916 fd_set fdr;
917 struct timeval to = interval;
918
919 FD_ZERO (&fdr);
920 FD_SET (ConnectionNumber (disp), &fdr);
921 select (ConnectionNumber (disp) + 1, &fdr, 0, 0, &to);
922 }
923 }
924
925 check_open_files ();
926
927 if (do_reopen)
928 reopen ();
929
930 /* we ignore possible errors due to window resizing &c */
931 while (XPending (disp))
932 {
933 XNextEvent (disp, &xev);
934
935 switch (xev.type)
936 {
937 case Expose:
938 {
939 XRectangle r;
940
941 r.x = xev.xexpose.x;
942 r.y = xev.xexpose.y;
943 r.width = xev.xexpose.width;
944 r.height = xev.xexpose.height;
945
946 XUnionRectWithRegion (&r, region, region);
947 }
948 break;
949 default:
950 #ifdef DEBUGMODE
951 fprintf (stderr, "PANIC! Unknown event %d\n", xev.type);
952 #endif
953 break;
954 }
955 }
956
957 /* reload if requested */
958 if (reload && lastreload + reload < time (NULL))
959 {
960 if (command && command[0])
961 system (command);
962
963 reopen ();
964 lastreload = time (NULL);
965 }
966
967 if (!XEmptyRegion (region))
968 {
969 XRectangle r;
970
971 XSetRegion (disp, WinGC, region);
972 XClipBox (region, &r);
973
974 refresh (r.y, r.y + r.height, 0, 1);
975
976 XDestroyRegion (region);
977 region = XCreateRegion ();
978 }
979 }
980 }
981
982
983 int
984 main (int argc, char *argv[])
985 {
986 int i;
987 int opt_daemonize = 0;
988 int opt_partial = 0, file_count = 0;
989 #if HAS_REGEX
990 char *transform = NULL;
991 #endif
992
993 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
994
995 /* window needs to be initialized before colorlookups can be done */
996 /* just a dummy to get the color lookups right */
997 geom_mask = NoValue;
998 InitWindow ();
999
1000 for (i = 1; i < argc; i++)
1001 {
1002 const char *arg = argv[i];
1003
1004 if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',')
1005 {
1006 if (arg[1] == '-')
1007 arg++;
1008
1009 if (!strcmp (arg, "-?") ||
1010 !strcmp (arg, "-help") || !strcmp (arg, "-h"))
1011 display_help (argv[0]);
1012 else if (!strcmp (arg, "-V"))
1013 display_version ();
1014 else if (!strcmp (arg, "-g") || !strcmp (arg, "-geometry"))
1015 geom_mask =
1016 XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
1017 else if (!strcmp (arg, "-display"))
1018 dispname = argv[++i];
1019 else if (!strcmp (arg, "-cont"))
1020 continuation = argv[++i];
1021 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
1022 fontname = argv[++i];
1023 #if HAS_REGEX
1024 else if (!strcmp (arg, "-t"))
1025 {
1026 transform = argv[++i];
1027 transform_to = argv[++i];
1028 printf("transform: '%s' to '%s'\n", transform, transform_to);
1029 }
1030 #endif
1031 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
1032 opt_daemonize = 1;
1033 else if (!strcmp (arg, "-reload"))
1034 {
1035 reload = atoi (argv[++i]);
1036 command = argv[++i];
1037 }
1038 else if (!strcmp (arg, "-shade"))
1039 opt_shade = 1;
1040 else if (!strcmp (arg, "-outline"))
1041 opt_outline = 1;
1042 else if (!strcmp (arg, "-noflicker"))
1043 opt_noflicker = 1;
1044 else if (!strcmp (arg, "-frame"))
1045 opt_frame = 1;
1046 else if (!strcmp (arg, "-no-filename"))
1047 opt_nofilename = 1;
1048 else if (!strcmp (arg, "-reverse"))
1049 opt_reverse = 1;
1050 else if (!strcmp (arg, "-whole"))
1051 opt_whole = 1;
1052 else if (!strcmp (arg, "-partial"))
1053 opt_partial = 1;
1054 else if (!strcmp (arg, "-update"))
1055 opt_update = opt_partial = 1;
1056 else if (!strcmp (arg, "-wordwrap"))
1057 opt_wordwrap = 1;
1058 else if (!strcmp (arg, "-color"))
1059 def_color = argv[++i];
1060 else if (!strcmp (arg, "-noinitial"))
1061 opt_noinitial = 1;
1062 else if (!strcmp (arg, "-id"))
1063 root = atoi (argv[++i]);
1064 else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i"))
1065 {
1066 double iv = atof (argv[++i]);
1067
1068 interval.tv_sec = (int) iv;
1069 interval.tv_usec = (iv - interval.tv_sec) * 1e6;
1070 }
1071 else
1072 {
1073 fprintf (stderr, "Unknown option '%s'.\n"
1074 "Try --help for more information.\n", arg);
1075 exit (1);
1076 }
1077 }
1078 else
1079 { /* it must be a filename */
1080 struct logfile_entry *e;
1081 const char *fname, *desc, *fcolor = def_color;
1082 char *p;
1083
1084 file_count++;
1085
1086 /* this is not foolproof yet (',' in filenames are not allowed) */
1087 fname = desc = arg;
1088 if ((p = strchr (arg, ',')))
1089 {
1090 *p = '\0';
1091 fcolor = p + 1;
1092
1093 if ((p = strchr (fcolor, ',')))
1094 {
1095 *p = '\0';
1096 desc = p + 1;
1097 }
1098 }
1099
1100 e = xmalloc (sizeof (struct logfile_entry));
1101 e->partial = 0;
1102 e->buf = 0;
1103 e->index = -1;
1104
1105 if (arg[0] == '-' && arg[1] == '\0')
1106 {
1107 if ((e->fp = fdopen (0, "r")) == NULL)
1108 perror ("fdopen"), exit (1);
1109 if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
1110 perror ("fcntl"), exit (1);
1111 e->fname = NULL;
1112 e->inode = 0;
1113 e->desc = xstrdup ("stdin");
1114 }
1115 else
1116 {
1117 int l;
1118
1119 e->fname = xstrdup (fname);
1120 if (openlog (e) == NULL)
1121 perror (fname), exit (1);
1122
1123 l = strlen (desc);
1124 /* HACK-6: width is in pixels now */
1125 if (l > width - 2) /* must account for [ ] */
1126 l = width - 2;
1127 e->desc = xmalloc (l + 1);
1128 memcpy (e->desc, desc, l);
1129 *(e->desc + l) = '\0';
1130 }
1131
1132 e->color = GetColor (fcolor);
1133 e->partial = 0;
1134 e->next = NULL;
1135
1136 if (!loglist)
1137 loglist = e;
1138 if (loglist_tail)
1139 loglist_tail->next = e;
1140 loglist_tail = e;
1141 }
1142 }
1143
1144 if (!loglist)
1145 {
1146 fprintf (stderr, "You did not specify any files to tail\n"
1147 "use %s --help for help\n", argv[0]);
1148 exit (1);
1149 }
1150
1151 if (opt_partial && opt_whole)
1152 {
1153 fprintf (stderr, "Specify at most one of -partial and -whole\n");
1154 exit (1);
1155 }
1156
1157 /* HACK-7: do we want to allow both -shade and -outline? */
1158 if (opt_shade && opt_outline)
1159 {
1160 fprintf (stderr, "Specify at most one of -shade and -outline\n");
1161 exit (1);
1162 }
1163
1164 if (opt_partial)
1165 /* if we specifically requested to see partial lines then don't insist on whole lines */
1166 opt_whole = 0;
1167 else if (file_count > 1)
1168 /* otherwise, if we're viewing multiple files, default to showing whole lines */
1169 opt_whole = 1;
1170
1171 #if HAS_REGEX
1172 if (transform)
1173 {
1174 int i;
1175
1176 printf("compiling regexp '%s'\n", transform);
1177 transformre = xmalloc (sizeof (regex_t));
1178 i = regcomp (transformre, transform, REG_EXTENDED);
1179 if (i != 0)
1180 {
1181 char buf[512];
1182
1183 regerror (i, transformre, buf, sizeof (buf));
1184 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1185 }
1186 else
1187 {
1188 printf("compiled '%s' OK to %x\n", transform, (int)transformre);
1189 }
1190 }
1191 #endif
1192
1193 InitWindow ();
1194
1195 install_signal (SIGINT, blank_window);
1196 install_signal (SIGQUIT, blank_window);
1197 install_signal (SIGTERM, blank_window);
1198 install_signal (SIGHUP, force_reopen);
1199 install_signal (SIGUSR1, list_files);
1200 install_signal (SIGUSR2, force_refresh);
1201
1202 if (opt_daemonize)
1203 daemonize ();
1204
1205 main_loop ();
1206
1207 exit (1); /* to make gcc -Wall stop complaining */
1208 }
1209
1210 void
1211 install_signal (int sig, void (*handler) (int))
1212 {
1213 struct sigaction action;
1214
1215 action.sa_handler = handler;
1216 sigemptyset (&action.sa_mask);
1217 action.sa_flags = SA_RESTART;
1218
1219 if (sigaction (sig, &action, NULL) < 0)
1220 fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1);
1221 }
1222
1223 void *
1224 xstrdup (const char *string)
1225 {
1226 void *p;
1227
1228 while ((p = strdup (string)) == NULL)
1229 {
1230 fprintf (stderr, "Memory exausted.");
1231 sleep (10);
1232 }
1233
1234 return p;
1235 }
1236
1237 void *
1238 xmalloc (size_t size)
1239 {
1240 void *p;
1241
1242 while ((p = malloc (size)) == NULL)
1243 {
1244 fprintf (stderr, "Memory exausted.");
1245 sleep (10);
1246 }
1247
1248 return p;
1249 }
1250
1251 void *
1252 xrealloc (void *ptr, size_t size)
1253 {
1254 void *p;
1255
1256 while ((p = realloc (ptr, size)) == NULL)
1257 {
1258 fprintf (stderr, "Memory exausted.");
1259 sleep (10);
1260 }
1261
1262 return p;
1263 }
1264
1265 void
1266 display_help (char *myname)
1267 {
1268 printf ("Usage: %s [options] file1[,color[,desc]] "
1269 "[file2[,color[,desc]] ...]\n", myname);
1270 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1271 " -color color use color $color as default\n"
1272 " -reload sec command reload after $sec and run command\n"
1273 " -id id window id to use instead of the root window\n"
1274 " -font FONTSPEC (-fn) font to use\n"
1275 " -f | -fork fork into background\n"
1276 " -reverse print new lines at the top\n"
1277 " -whole wait for \\n before showing a line\n"
1278 " -partial show lines even if they don't end with a \\n\n"
1279 " -update allow updates to old partial lines\n"
1280 " -cont string to prefix continued partial lines with\n"
1281 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1282 " defaults to \"[+]\"\n"
1283 " -shade add shading to font\n"
1284 " -noinitial don't display the last file lines on\n"
1285 " startup\n"
1286 " -i | -interval seconds interval between checks (fractional\n"
1287 " values o.k.). Default 2.4 seconds\n"
1288 " -V display version information and exit\n"
1289 "\n");
1290 printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green "
1291 "/var/log/secure,red,'ALERT'\n", myname);
1292 exit (0);
1293 }
1294
1295 void
1296 display_version (void)
1297 {
1298 printf ("root-tail version " VERSION "\n");
1299 exit (0);
1300 }
1301
1302 int
1303 daemonize (void)
1304 {
1305 pid_t pid;
1306
1307 switch (pid = fork ())
1308 {
1309 case -1:
1310 return -1;
1311 case 0:
1312 break;
1313 default:
1314 /*printf("%d\n", pid);*/
1315 exit (0);
1316 }
1317
1318 if (setsid () == -1)
1319 return -1;
1320
1321 return 0;
1322 }