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