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