ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/root-tail/root-tail.c
Revision: 1.20
Committed: Sat Mar 27 20:42:40 2004 UTC (22 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.19: +8 -11 lines
Log Message:
*** empty log message ***

File Contents

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