ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/root-tail/root-tail.c
Revision: 1.14
Committed: Sat Mar 27 14:47:27 2004 UTC (22 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +19 -0 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.11 XClearArea (disp, root, win_x, win_y, width, height, False);
152     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.12 XClearArea (disp, root, win_x, win_y, width, height, False);
321     refresh (0, 32768);
322 pcg 1.1 }
323    
324     /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
325 pcg 1.11 void
326 pcg 1.12 refresh (int miny, int maxy)
327 pcg 1.1 {
328 pcg 1.11 int lin;
329     int offset = (listlen + 1) * font_height;
330     unsigned long black_color = GetColor ("black");
331 pcg 1.1
332 pcg 1.11 miny -= win_y + font_height;
333     maxy -= win_y - font_height;
334 pcg 1.1
335 pcg 1.12
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.12 XDrawRectangle (disp, root, WinGC, win_x - 2, win_y - 2, width + 4, height + 4);
359 pcg 1.1 }
360    
361     #if HAS_REGEX
362 pcg 1.11 void
363     transform_line (char *s)
364 pcg 1.1 {
365     #ifdef I_AM_Md
366 pcg 1.11 int i;
367     if (1)
368     {
369     for (i = 16; s[i]; i++)
370     s[i] = s[i + 11];
371 pcg 1.1 }
372 pcg 1.11 s[i + 1] = '\0';
373 pcg 1.1 #endif
374    
375 pcg 1.11 if (transformre)
376     {
377     int i;
378     regmatch_t matched[16];
379    
380     i = regexec (&transformre, string, 16, matched, 0);
381     if (i == 0)
382     { /* matched */
383     }
384 pcg 1.1 }
385     }
386     #endif
387    
388 pcg 1.12 char *
389     concat_line (const char *p1, const char *p2)
390     {
391     int l1 = p1 ? strlen (p1) : 0;
392     int l2 = strlen (p2);
393     char *r = xmalloc (l1 + l2 + 1);
394    
395     memcpy (r, p1, l1);
396     memcpy (r + l1, p2, l2);
397     r[l1 + l2] = 0;
398    
399     return r;
400     }
401 pcg 1.1
402     /*
403 pcg 1.12 * This routine should read a single line, no matter how long.
404 pcg 1.1 */
405 pcg 1.11 int
406     lineinput (struct logfile_entry *logfile)
407 pcg 1.1 {
408 pcg 1.12 char buff[1024], *p = buff;
409     int ch;
410     int ofs = logfile->buf ? strlen (logfile->buf) : 0;
411 pcg 1.1
412 pcg 1.11 do
413     {
414 pcg 1.12 ch = fgetc (logfile->fp);
415 pcg 1.1
416 pcg 1.12 if (ch == '\r')
417     continue;
418     else if (ch == EOF || ch == '\n')
419     break;
420     else if (ch == '\t')
421     {
422     do
423     {
424     *p++ = ' ';
425     ofs++;
426     }
427     while (ofs & 7);
428     }
429     else
430     {
431     *p++ = ch;
432     ofs++;
433     }
434 pcg 1.11 }
435 pcg 1.12 while (p < buff + (sizeof buff) - 8 - 1);
436 pcg 1.1
437 pcg 1.12 if (p == buff)
438     return 0;
439    
440     *p = 0;
441    
442     p = concat_line (logfile->buf, buff);
443     free (logfile->buf); logfile->buf = p;
444 chris_moore 1.9
445 pcg 1.12 logfile->lastpartial = logfile->partial;
446     logfile->partial = ch == EOF;
447    
448     if (logfile->partial && opt_whole)
449 pcg 1.11 return 0;
450 pcg 1.1
451     #if HAS_REGEX
452 pcg 1.12 transform_line (logfile->buf);
453 pcg 1.1 #endif
454 pcg 1.12 return 1;
455 pcg 1.1 }
456    
457     /* input: reads file->fname
458     * output: fills file->fp, file->inode
459     * returns file->fp
460     * in case of error, file->fp is NULL
461     */
462 pcg 1.11 FILE *
463     openlog (struct logfile_entry * file)
464     {
465     struct stat stats;
466    
467     if ((file->fp = fopen (file->fname, "r")) == NULL)
468     {
469     file->fp = NULL;
470     return NULL;
471     }
472    
473     fstat (fileno (file->fp), &stats);
474     if (S_ISFIFO (stats.st_mode))
475     {
476     if (fcntl (fileno (file->fp), F_SETFL, O_NONBLOCK) < 0)
477     perror ("fcntl"), exit (1);
478     file->inode = 0;
479     }
480     else
481     file->inode = stats.st_ino;
482    
483     if (opt_noinitial)
484     fseek (file->fp, 0, SEEK_END);
485     else if (stats.st_size > (listlen + 1) * width)
486     fseek (file->fp, -((listlen + 2) * width), SEEK_END);
487    
488     file->last_size = stats.st_size;
489     return file->fp;
490     }
491    
492     void
493     reopen (void)
494 pcg 1.1 {
495 pcg 1.11 struct logfile_entry *e;
496    
497     for (e = loglist; e; e = e->next)
498     {
499     if (!e->inode)
500     continue; /* skip stdin */
501 pcg 1.1
502 pcg 1.11 if (e->fp)
503     fclose (e->fp);
504     /* if fp is NULL we will try again later */
505     openlog (e);
506     }
507    
508     do_reopen = 0;
509     }
510 pcg 1.1
511 pcg 1.11 void
512     check_open_files (void)
513     {
514     struct logfile_entry *e;
515     struct stat stats;
516 pcg 1.1
517 pcg 1.11 for (e = loglist; e; e = e->next)
518     {
519     if (!e->inode)
520     continue; /* skip stdin */
521 pcg 1.1
522 pcg 1.11 if (stat (e->fname, &stats) < 0)
523     { /* file missing? */
524     sleep (1);
525     if (e->fp)
526     fclose (e->fp);
527     if (openlog (e) == NULL)
528     break;
529     }
530    
531     if (stats.st_ino != e->inode)
532     { /* file renamed? */
533     if (e->fp)
534     fclose (e->fp);
535     if (openlog (e) == NULL)
536     break;
537     }
538    
539     if (stats.st_size < e->last_size)
540     { /* file truncated? */
541     fseek (e->fp, 0, SEEK_SET);
542     e->last_size = stats.st_size;
543     }
544 pcg 1.1 }
545     }
546    
547 pcg 1.14 /*
548     * insert a single physical line (that must be short enough to fit)
549     * at position "idx" by pushing up lines above it. the caller
550     * MUST then fill in lines[idx] with valid data.
551     */
552 pcg 1.12 static void
553     insert_line (int idx)
554     {
555     int cur_line;
556     struct logfile_entry *current;
557    
558     free (lines[0].line);
559    
560     for (cur_line = 0; cur_line < idx; cur_line++)
561     lines[cur_line] = lines[cur_line + 1];
562    
563     for (current = loglist; current; current = current->next)
564     if (current->partial && current->index && current->index <= idx)
565     current->index--;
566 pcg 1.11 }
567    
568 pcg 1.14 /*
569     * remove a single physical line at position "idx" by moving the lines above it
570     * down and inserting a "~" line at the top.
571     */
572 pcg 1.12 static void
573     delete_line (int idx)
574     {
575     int cur_line;
576     struct logfile_entry *current;
577    
578     for (cur_line = idx; cur_line > 0; cur_line--)
579     lines[cur_line] = lines[cur_line - 1];
580    
581     lines[0].line = strdup ("~");
582    
583     for (current = loglist; current; current = current->next)
584     if (current->partial && current->index && current->index <= idx)
585     current->index++;
586     }
587    
588 pcg 1.14 /*
589     * takes a logical log file line and split it into multiple physical
590     * screen lines by splitting it whenever a part becomes too long.
591     * lal lines will be inserted at position "idx".
592     */
593 pcg 1.12 static void
594     split_line (int idx, const char *str, unsigned long color)
595     {
596     int l = strlen (str);
597     const char *p = str;
598    
599     do
600     {
601     const char *beg = p;
602     int w = 0;
603    
604     while (*p)
605     {
606     int len = mblen (p, l);
607     if (len <= 0)
608     len = 1; /* ignore (don't skip) ilegal character sequences */
609    
610     int cw = XmbTextEscapement (fontset, p, len);
611     if (cw + w >= width)
612     break;
613    
614     w += cw;
615     p += len;
616     l -= len;
617     }
618    
619     {
620     char *s = xmalloc (p - beg + 1);
621     memcpy (s, beg, p - beg);
622     s[p - beg] = 0;
623     insert_line (idx);
624     lines[idx].line = s;
625     lines[idx].len = p - beg;
626     lines[idx].color = color;
627     }
628     }
629     while (l);
630     }
631    
632 pcg 1.14 /*
633     * append something to an existing physical line. this is done
634     * by deleting the file on-screen, concatenating the new data to it
635     * and splitting it again.
636     */
637 pcg 1.12 static void
638     append_line (int idx, const char *str)
639     {
640     unsigned long color = lines[idx].color;
641     char *old = lines[idx].line;
642     char *new = concat_line (old, str);
643    
644     free (old);
645     free (str);
646    
647     delete_line (idx);
648     split_line (idx, new, color);
649     }
650    
651     static void
652 pcg 1.11 main_loop (void)
653     {
654 pcg 1.12 lines = xmalloc (sizeof (struct linematrix) * listlen);
655 pcg 1.11 int lin, miny, maxy;
656     time_t lastreload;
657     Region region = XCreateRegion ();
658     XEvent xev;
659     struct logfile_entry *lastprinted = NULL;
660     struct logfile_entry *current;
661    
662     maxy = 0;
663     miny = win_y + height;
664     lastreload = time (NULL);
665    
666     /* Initialize linematrix */
667     for (lin = 0; lin < listlen; lin++)
668     {
669 pcg 1.12 lines[lin].line = strdup ("~");
670     lines[lin].len = 1;
671 pcg 1.11 lines[lin].color = GetColor (def_color);
672     }
673    
674     /* show the display full of empty lines ("~") in case the first
675     * time around the loop doesn't produce any output, as it won't if
676     * either (a) -noinitial is set or (b) all the files are currently
677     * empty */
678     redraw ();
679    
680     for (;;)
681     {
682     int need_update = 0;
683    
684     /* read logs */
685     for (current = loglist; current; current = current->next)
686     {
687     if (!current->fp)
688     continue; /* skip missing files */
689    
690     clearerr (current->fp);
691    
692 pcg 1.12 while (lineinput (current))
693 pcg 1.11 {
694 pcg 1.12 need_update = 1;
695 pcg 1.11 /* if we're trying to update old partial lines in
696     * place, and the last time this file was updated the
697     * output was partial, and that partial line is not
698     * too close to the top of the screen, then update
699     * that partial line */
700 pcg 1.13 if (opt_update && current->lastpartial && current->index > 0)
701 pcg 1.11 {
702 pcg 1.12 append_line (current->index, current->buf);
703 pcg 1.13 free (current->buf), current->buf = 0;
704 pcg 1.12 continue;
705 pcg 1.11 }
706    
707     /* print filename if any, and if last line was from
708     * different file */
709 pcg 1.13 if (!opt_nofilename && lastprinted != current && current->desc[0])
710 pcg 1.11 {
711 pcg 1.12 char buf[1024]; /* HACK */
712     snprintf (buf, sizeof (buf), "[%s]", current->desc);
713     split_line (listlen - 1, buf, current->color);
714 pcg 1.11 }
715    
716     /* if this is the same file we showed last, and the
717     * last time we showed it, it wasn't finished, then
718     * append to the last line shown */
719     if (lastprinted == current && current->lastpartial)
720     {
721 pcg 1.12 append_line (listlen - 1, current->buf);
722 pcg 1.13 free (current->buf), current->buf = 0;
723 pcg 1.12 continue;
724 pcg 1.11 }
725     else
726     {
727 pcg 1.12 split_line (listlen - 1, current->buf, current->color);
728 pcg 1.13 free (current->buf), current->buf = 0;
729 pcg 1.11 }
730    
731     current->index = listlen - 1;
732     lastprinted = current;
733     }
734     }
735    
736     if (need_update)
737     redraw ();
738     else
739     {
740     XFlush (disp);
741    
742     if (!XPending (disp))
743     {
744     fd_set fdr;
745     struct timeval to = interval;
746    
747     FD_ZERO (&fdr);
748     FD_SET (ConnectionNumber (disp), &fdr);
749     select (ConnectionNumber (disp) + 1, &fdr, 0, 0, &to);
750     }
751     }
752    
753     check_open_files ();
754    
755     if (do_reopen)
756     reopen ();
757    
758     /* we ignore possible errors due to window resizing &c */
759     while (XPending (disp))
760     {
761     XNextEvent (disp, &xev);
762    
763     switch (xev.type)
764     {
765     case Expose:
766     {
767     XRectangle r;
768    
769     r.x = xev.xexpose.x;
770     r.y = xev.xexpose.y;
771     r.width = xev.xexpose.width;
772     r.height = xev.xexpose.height;
773     XUnionRectWithRegion (&r, region, region);
774     if (miny > r.y)
775     miny = r.y;
776     if (maxy < r.y + r.height)
777     maxy = r.y + r.height;
778     }
779     break;
780     default:
781     #ifdef DEBUGMODE
782     fprintf (stderr, "PANIC! Unknown event %d\n", xev.type);
783     #endif
784     break;
785     }
786     }
787    
788     /* reload if requested */
789     if (reload && lastreload + reload < time (NULL))
790     {
791     if (command && command[0])
792     system (command);
793    
794     reopen ();
795     lastreload = time (NULL);
796     }
797    
798     if (!XEmptyRegion (region))
799     {
800     XSetRegion (disp, WinGC, region);
801    
802 pcg 1.12 refresh (miny, maxy);
803 pcg 1.11 XDestroyRegion (region);
804     region = XCreateRegion ();
805     maxy = 0;
806     miny = win_y + height;
807     }
808     }
809     }
810 pcg 1.1
811 pcg 1.11
812     int
813     main (int argc, char *argv[])
814 pcg 1.1 {
815 pcg 1.11 int i;
816     int opt_daemonize = 0;
817     int opt_partial = 0, file_count = 0;
818 pcg 1.1 #if HAS_REGEX
819 pcg 1.11 char *transform = NULL;
820 pcg 1.1 #endif
821    
822 pcg 1.11 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
823    
824     /* window needs to be initialized before colorlookups can be done */
825     /* just a dummy to get the color lookups right */
826     geom_mask = NoValue;
827     InitWindow ();
828    
829     for (i = 1; i < argc; i++)
830     {
831     const char *arg = argv[i];
832 pcg 1.7
833 pcg 1.11 if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',')
834     {
835     if (arg[1] == '-')
836     arg++;
837    
838     if (!strcmp (arg, "-?") ||
839     !strcmp (arg, "-help") || !strcmp (arg, "-h"))
840     display_help (argv[0]);
841     else if (!strcmp (arg, "-V"))
842     display_version ();
843     else if (!strcmp (arg, "-g") || !strcmp (arg, "-geometry"))
844     geom_mask =
845     XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
846     else if (!strcmp (arg, "-display"))
847     dispname = argv[++i];
848     else if (!strcmp (arg, "-cont"))
849     continuation = argv[++i];
850     else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
851     fontname = argv[++i];
852 pcg 1.1 #if HAS_REGEX
853 pcg 1.11 else if (!strcmp (arg, "-t"))
854     transform = argv[++i];
855 pcg 1.1 #endif
856 pcg 1.11 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
857     opt_daemonize = 1;
858     else if (!strcmp (arg, "-reload"))
859     {
860     reload = atoi (argv[++i]);
861     command = argv[++i];
862     }
863     else if (!strcmp (arg, "-shade"))
864     opt_shade = 1;
865     else if (!strcmp (arg, "-frame"))
866     opt_frame = 1;
867     else if (!strcmp (arg, "-no-filename"))
868     opt_nofilename = 1;
869     else if (!strcmp (arg, "-reverse"))
870     opt_reverse = 1;
871     else if (!strcmp (arg, "-whole"))
872     opt_whole = 1;
873     else if (!strcmp (arg, "-partial"))
874     opt_partial = 1;
875     else if (!strcmp (arg, "-update"))
876     opt_update = opt_partial = 1;
877     else if (!strcmp (arg, "-color"))
878     def_color = argv[++i];
879     else if (!strcmp (arg, "-noinitial"))
880     opt_noinitial = 1;
881     else if (!strcmp (arg, "-id"))
882     root = atoi (argv[++i]);
883     else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i"))
884     {
885     double iv = atof (argv[++i]);
886    
887     interval.tv_sec = (int) iv;
888     interval.tv_usec = (iv - interval.tv_sec) * 1e6;
889     }
890     else
891     {
892     fprintf (stderr, "Unknown option '%s'.\n"
893     "Try --help for more information.\n", arg);
894     exit (1);
895     }
896     }
897     else
898     { /* it must be a filename */
899     struct logfile_entry *e;
900     const char *fname, *desc, *fcolor = def_color;
901     char *p;
902    
903     file_count++;
904    
905     /* this is not foolproof yet (',' in filenames are not allowed) */
906     fname = desc = arg;
907     if ((p = strchr (arg, ',')))
908     {
909     *p = '\0';
910     fcolor = p + 1;
911    
912     if ((p = strchr (fcolor, ',')))
913     {
914     *p = '\0';
915     desc = p + 1;
916     }
917     }
918    
919     e = xmalloc (sizeof (struct logfile_entry));
920 pcg 1.12 e->partial = 0;
921     e->buf = 0;
922    
923 pcg 1.11 if (arg[0] == '-' && arg[1] == '\0')
924     {
925     if ((e->fp = fdopen (0, "r")) == NULL)
926     perror ("fdopen"), exit (1);
927     if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
928     perror ("fcntl"), exit (1);
929     e->fname = NULL;
930     e->inode = 0;
931     e->desc = xstrdup ("stdin");
932     }
933     else
934     {
935     int l;
936    
937     e->fname = xstrdup (fname);
938     if (openlog (e) == NULL)
939     perror (fname), exit (1);
940    
941     l = strlen (desc);
942     if (l > width - 2) /* must account for [ ] */
943     l = width - 2;
944     e->desc = xmalloc (l + 1);
945     memcpy (e->desc, desc, l);
946     *(e->desc + l) = '\0';
947     }
948    
949     e->color = GetColor (fcolor);
950     e->partial = 0;
951     e->next = NULL;
952    
953     if (!loglist)
954     loglist = e;
955     if (loglist_tail)
956     loglist_tail->next = e;
957     loglist_tail = e;
958     }
959     }
960    
961     if (!loglist)
962     {
963     fprintf (stderr, "You did not specify any files to tail\n"
964     "use %s --help for help\n", argv[0]);
965     exit (1);
966     }
967    
968     if (opt_partial && opt_whole)
969     {
970     fprintf (stderr, "Specify at most one of -partial and -whole\n");
971     exit (1);
972 chris_moore 1.9 }
973    
974 pcg 1.11 if (opt_partial)
975 chris_moore 1.9 /* if we specifically requested to see partial lines then don't insist on whole lines */
976 pcg 1.11 opt_whole = 0;
977     else if (file_count > 1)
978 chris_moore 1.9 /* otherwise, if we've viewing multiple files, default to showing whole lines */
979 pcg 1.11 opt_whole = 1;
980 chris_moore 1.9
981 pcg 1.1 #if HAS_REGEX
982 pcg 1.11 if (transform)
983     {
984     int i;
985 pcg 1.1
986 pcg 1.11 transformre = xmalloc (sizeof (transformre));
987     i = regcomp (&transformre, transform, REG_EXTENDED);
988     if (i != 0)
989     {
990     char buf[512];
991    
992     regerror (i, &transformre, buf, sizeof (buf));
993     fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
994     }
995 pcg 1.1 }
996     #endif
997    
998 pcg 1.11 InitWindow ();
999 pcg 1.1
1000 pcg 1.11 install_signal (SIGINT, blank_window);
1001     install_signal (SIGQUIT, blank_window);
1002     install_signal (SIGTERM, blank_window);
1003     install_signal (SIGHUP, force_reopen);
1004     install_signal (SIGUSR1, list_files);
1005     install_signal (SIGUSR2, force_refresh);
1006 pcg 1.1
1007 pcg 1.11 if (opt_daemonize)
1008     daemonize ();
1009 pcg 1.1
1010 pcg 1.11 main_loop ();
1011 pcg 1.1
1012 pcg 1.11 exit (1); /* to make gcc -Wall stop complaining */
1013 pcg 1.1 }
1014    
1015 pcg 1.11 void
1016     install_signal (int sig, void (*handler) (int))
1017 pcg 1.1 {
1018 pcg 1.11 struct sigaction action;
1019    
1020     action.sa_handler = handler;
1021     sigemptyset (&action.sa_mask);
1022     action.sa_flags = SA_RESTART;
1023 pcg 1.1
1024 pcg 1.11 if (sigaction (sig, &action, NULL) < 0)
1025     fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1);
1026 pcg 1.1 }
1027    
1028 pcg 1.11 void *
1029     xstrdup (const char *string)
1030 pcg 1.1 {
1031 pcg 1.11 void *p;
1032 pcg 1.1
1033 pcg 1.11 while ((p = strdup (string)) == NULL)
1034     {
1035     fprintf (stderr, "Memory exausted.");
1036     sleep (10);
1037 pcg 1.1 }
1038 pcg 1.11
1039     return p;
1040 pcg 1.1 }
1041    
1042 pcg 1.11 void *
1043     xmalloc (size_t size)
1044 pcg 1.1 {
1045 pcg 1.11 void *p;
1046 pcg 1.1
1047 pcg 1.11 while ((p = malloc (size)) == NULL)
1048     {
1049     fprintf (stderr, "Memory exausted.");
1050     sleep (10);
1051 pcg 1.1 }
1052 pcg 1.12
1053 pcg 1.11 return p;
1054 pcg 1.1 }
1055    
1056 pcg 1.11 void
1057     display_help (char *myname)
1058     {
1059     printf ("Usage: %s [options] file1[,color[,desc]] "
1060     "[file2[,color[,desc]] ...]\n", myname);
1061     printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1062     " -color color use color $color as default\n"
1063     " -reload sec command reload after $sec and run command\n"
1064     " -id id window id to use instead of the root window\n"
1065     " -font FONTSPEC (-fn) font to use\n"
1066     " -f | -fork fork into background\n"
1067     " -reverse print new lines at the top\n"
1068     " -whole wait for \\n before showing a line\n"
1069     " -partial show lines even if they don't end with a \\n\n"
1070     " -update allow updates to old partial lines\n"
1071     " -cont string to prefix continued partial lines with\n"
1072     " defaults to \"[+]\"\n"
1073     " -shade add shading to font\n"
1074     " -noinitial don't display the last file lines on\n"
1075     " startup\n"
1076     " -i | -interval seconds interval between checks (fractional\n"
1077     " values o.k.). Default 3 seconds\n"
1078     " -V display version information and exit\n"
1079     "\n");
1080     printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green "
1081     "/var/log/secure,red,'ALERT'\n", myname);
1082     exit (0);
1083     }
1084    
1085     void
1086     display_version (void)
1087 pcg 1.1 {
1088 pcg 1.11 printf ("root-tail version " VERSION "\n");
1089     exit (0);
1090 pcg 1.1 }
1091    
1092 pcg 1.11 int
1093     daemonize (void)
1094     {
1095     switch (fork ())
1096     {
1097 pcg 1.1 case -1:
1098 pcg 1.11 return -1;
1099 pcg 1.1 case 0:
1100 pcg 1.11 break;
1101 pcg 1.1 default:
1102 pcg 1.11 _exit (0);
1103 pcg 1.1 }
1104    
1105 pcg 1.11 if (setsid () == -1)
1106     return -1;
1107 pcg 1.1
1108 pcg 1.11 return 0;
1109 pcg 1.1 }