ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/root-tail/root-tail.c
Revision: 1.8
Committed: Thu Mar 25 21:46:56 2004 UTC (22 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +37 -21 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.1 #if HAS_REGEX
35     #include <regex.h>
36     #endif
37     #include <X11/Xlib.h>
38 pcg 1.7 #include <X11/Xatom.h>
39 pcg 1.1 #include <X11/Xutil.h>
40    
41     /* data structures */
42     struct logfile_entry {
43     char *fname; /* name of file */
44     char *desc; /* alternative description */
45     FILE *fp; /* FILE struct associated with file */
46     ino_t inode; /* inode of the file opened */
47     off_t last_size; /* file size at the last check */
48     unsigned long color; /* color to be used for printing */
49     struct logfile_entry *next;
50     };
51    
52     struct linematrix {
53     char *line;
54     unsigned long color;
55     };
56    
57    
58     /* global variables */
59 pcg 1.6 int width = STD_WIDTH, listlen = STD_HEIGHT;
60 pcg 1.1 int win_x = LOC_X, win_y = LOC_Y;
61     int w = -1, h = -1, font_width, font_height, font_descent;
62     int do_reopen;
63     struct timeval interval = { 2, 400000 }; /* see Knuth */
64 pcg 1.7 XFontSet fontset;
65 pcg 1.1
66     /* command line options */
67 pcg 1.5 int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
68 pcg 1.1 geom_mask, reload = 3600;
69     const char *command = NULL,
70     *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR;
71    
72     struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
73    
74     Display *disp;
75     Window root;
76     GC WinGC;
77    
78     #if HAS_REGEX
79     struct re_list {
80     regex_t from;
81     const char *to;
82     struct re_list *next;
83     };
84     struct re_list *re_head, *re_tail;
85     #endif
86    
87    
88     /* prototypes */
89     void list_files(int);
90     void force_reopen(int);
91     void force_refresh(int);
92     void blank_window(int);
93    
94     void InitWindow(void);
95     unsigned long GetColor(const char *);
96     void redraw(void);
97     void refresh(struct linematrix *, int, int);
98    
99     void transform_line(char *s);
100     int lineinput(char *, int, FILE *);
101     void reopen(void);
102     void check_open_files(void);
103     FILE *openlog(struct logfile_entry *);
104     void main_loop(void);
105    
106     void display_version(void);
107     void display_help(char *);
108     void install_signal(int, void (*)(int));
109     void *xstrdup(const char *);
110     void *xmalloc(size_t);
111     int daemonize(void);
112    
113     /* signal handlers */
114     void list_files(int dummy)
115     {
116     struct logfile_entry *e;
117    
118     fprintf(stderr, "Files opened:\n");
119     for (e = loglist; e; e = e->next)
120     fprintf(stderr, "\t%s (%s)\n", e->fname, e->desc);
121     }
122    
123     void force_reopen(int dummy)
124     {
125     do_reopen = 1;
126     }
127    
128     void force_refresh(int dummy)
129     {
130 pcg 1.7 XClearArea(disp, root, win_x, win_y, w, h + font_descent + 2, False);
131 pcg 1.1 redraw();
132     }
133    
134     void blank_window(int dummy)
135     {
136 pcg 1.7 XClearArea(disp, root, win_x, win_y, w, h + font_descent + 2, False);
137 pcg 1.1 XFlush(disp);
138     exit(0);
139     }
140    
141     /* X related functions */
142     unsigned long GetColor(const char *ColorName)
143     {
144     XColor Color;
145     XWindowAttributes Attributes;
146    
147     XGetWindowAttributes(disp, root, &Attributes);
148     Color.pixel = 0;
149 pcg 1.8
150 pcg 1.1 if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
151     fprintf(stderr, "can't parse %s\n", ColorName);
152     else if (!XAllocColor(disp, Attributes.colormap, &Color))
153     fprintf(stderr, "can't allocate %s\n", ColorName);
154 pcg 1.8
155 pcg 1.1 return Color.pixel;
156     }
157    
158 pcg 1.7 static Window root_window (Display *display, int screen_number)
159     {
160     Atom __SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False);
161     Window real_root_window = RootWindow (display, screen_number);
162    
163 pcg 1.8 if (root) /* root window set via option */
164     return root;
165    
166 pcg 1.7 if (__SWM_VROOT != None)
167     {
168     Window unused, *windows;
169     unsigned int count;
170    
171     if (XQueryTree (display, real_root_window, &unused, &unused, &windows,
172     &count))
173     {
174     int i;
175    
176     for (i = 0; i < count; i++)
177     {
178     Atom type;
179     int format;
180     unsigned long nitems, bytes_after_return;
181     unsigned char *virtual_root_window;
182    
183     if (XGetWindowProperty (display, windows[i], __SWM_VROOT,
184     0, 1, False, XA_WINDOW, &type, &format,
185     &nitems, &bytes_after_return,
186     &virtual_root_window)
187     == Success)
188     {
189     if (type != None)
190     {
191     if (type == XA_WINDOW)
192     {
193     XFree (windows);
194     return (Window)virtual_root_window;
195     }
196     else
197     fprintf (stderr, "__SWM_VROOT property type mismatch");
198     }
199     }
200     else
201     fprintf (stderr,
202     "failed to get __SWM_VROOT property on window 0x%lx",
203     windows[i]);
204     }
205    
206     if (count)
207     XFree (windows);
208     }
209     else
210     fprintf (stderr, "Can't query tree on root window 0x%lx",
211     real_root_window);
212     }
213     else
214     /* This shouldn't happen. The Xlib documentation is wrong BTW. */
215     fprintf (stderr, "Can't intern atom __SWM_VROOT");
216    
217     return real_root_window;
218     }
219    
220 pcg 1.1 void InitWindow(void)
221     {
222     XGCValues gcv;
223     Font font;
224     unsigned long gcm;
225     XFontStruct *info;
226     int screen, ScreenWidth, ScreenHeight;
227    
228     if (!(disp = XOpenDisplay(dispname))) {
229     fprintf(stderr, "Can't open display %s.\n", dispname);
230     exit(1);
231     }
232     screen = DefaultScreen(disp);
233     ScreenHeight = DisplayHeight(disp, screen);
234     ScreenWidth = DisplayWidth(disp, screen);
235 pcg 1.8
236 pcg 1.7 root = root_window (disp, screen);
237 pcg 1.8
238 pcg 1.1 gcm = GCBackground;
239     gcv.graphics_exposures = True;
240     WinGC = XCreateGC(disp, root, gcm, &gcv);
241     XMapWindow(disp, root);
242     XSetForeground(disp, WinGC, GetColor(DEF_COLOR));
243    
244     font = XLoadFont(disp, fontname);
245     XSetFont(disp, WinGC, font);
246     info = XQueryFont(disp, font);
247     font_width = info->max_bounds.width;
248     font_descent = info->max_bounds.descent;
249     font_height = info->max_bounds.ascent + font_descent;
250    
251     w = width * font_width;
252     h = listlen * font_height;
253 pcg 1.8
254 pcg 1.1 if (geom_mask & XNegative)
255     win_x = win_x + ScreenWidth - w;
256     if (geom_mask & YNegative)
257     win_y = win_y + ScreenHeight - h;
258    
259     XSelectInput(disp, root, ExposureMask|FocusChangeMask);
260     }
261    
262 pcg 1.7 char *
263 pcg 1.2 detabificate (char *s)
264     {
265     char * out;
266     int i, j;
267    
268     out = malloc (8 * strlen (s) + 1);
269    
270 pcg 1.7 for(i = 0, j = 0; s[i]; i++)
271 pcg 1.2 {
272 pcg 1.7 if (s[i] == '\t')
273     do
274 pcg 1.2 out[j++] = ' ';
275 pcg 1.7 while (j % 8);
276 pcg 1.2 else
277     out[j++] = s[i];
278     }
279 pcg 1.7
280 pcg 1.2 out[j] = '\0';
281     return out;
282     }
283    
284 pcg 1.1 /*
285     * redraw does a complete redraw, rather than an update (i.e. the area
286     * gets cleared first)
287     * the rest is handled by regular refresh()'es
288     */
289     void redraw(void)
290     {
291     XClearArea(disp, root, win_x, win_y, w, h + font_descent + 2, True);
292     }
293    
294     /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
295     void refresh(struct linematrix *lines, int miny, int maxy)
296     {
297     int lin;
298     int offset = (listlen + 1) * font_height;
299     unsigned long black_color = GetColor("black");
300    
301     miny -= win_y + font_height;
302     maxy -= win_y - font_height;
303    
304 pcg 1.2 for (lin = listlen; lin--;)
305     {
306     char *temp;
307 pcg 1.7
308 pcg 1.2 offset -= font_height;
309 pcg 1.7
310 pcg 1.2 if (offset < miny || offset > maxy)
311     continue;
312 jebusbfb 1.4
313     #define LIN ((opt_reverse)?(listlen-lin-1):(lin))
314     temp = detabificate (lines[LIN].line);
315 pcg 1.7
316 pcg 1.2 if (opt_shade)
317     {
318     XSetForeground (disp, WinGC, black_color);
319 pcg 1.7 #if 0
320     XmbDrawString (disp, root, fontset, WinGC, win_x + 2, win_y + offset + 2,
321     temp, strlen (temp));
322     #endif
323 pcg 1.2 XDrawString (disp, root, WinGC, win_x + 2, win_y + offset + 2,
324     temp, strlen (temp));
325     }
326 pcg 1.7
327 jebusbfb 1.4 XSetForeground (disp, WinGC, lines[LIN].color);
328 pcg 1.7 #if 0
329     XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset,
330     temp, strlen (temp));
331     #endif
332 pcg 1.2 XDrawString (disp, root, WinGC, win_x, win_y + offset,
333 pcg 1.7 temp, strlen (temp));
334 pcg 1.2 free (temp);
335 pcg 1.1 }
336    
337     if (opt_frame) {
338     int bot_y = win_y + h + font_descent + 2;
339    
340     XDrawLine(disp, root, WinGC, win_x, win_y, win_x + w, win_y);
341     XDrawLine(disp, root, WinGC, win_x + w, win_y, win_x + w, bot_y);
342     XDrawLine(disp, root, WinGC, win_x + w, bot_y, win_x, bot_y);
343     XDrawLine(disp, root, WinGC, win_x, bot_y, win_x, win_y);
344     }
345     }
346    
347     #if HAS_REGEX
348     void transform_line(char *s)
349     {
350     #ifdef I_AM_Md
351     int i;
352     if (1) {
353     for (i = 16; s[i]; i++)
354     s[i] = s[i + 11];
355     }
356     s[i + 1] = '\0';
357     #endif
358    
359     if (transformre) {
360     int i;
361     regmatch_t matched[16];
362    
363     i = regexec(&transformre, string, 16, matched, 0);
364     if (i == 0) { /* matched */
365     }
366     }
367     }
368     #endif
369    
370    
371     /*
372     * This routine should read 'width' characters and not more. However,
373 pcg 1.7 * we really want to read width + 1 characters if the last char is a '\n',
374 pcg 1.1 * which we should remove afterwards. So, read width+1 chars and ungetc
375     * the last character if it's not a newline. This means 'string' must be
376     * width + 2 wide!
377     */
378     int lineinput(char *string, int slen, FILE *f)
379     {
380     int len;
381    
382     do {
383     if (fgets(string, slen, f) == NULL) /* EOF or Error */
384     return 0;
385    
386     len = strlen(string);
387     } while (len == 0);
388    
389     if (string[len - 1] == '\n')
390     string[len - 1] = '\0'; /* erase newline */
391     else if (len >= slen - 1) {
392     ungetc(string[len - 1], f);
393     string[len - 1] = '\0';
394     }
395    
396     #if HAS_REGEX
397     transform_line(string);
398     #endif
399     return len;
400     }
401    
402     /* input: reads file->fname
403     * output: fills file->fp, file->inode
404     * returns file->fp
405     * in case of error, file->fp is NULL
406     */
407     FILE *openlog(struct logfile_entry *file)
408     {
409     struct stat stats;
410    
411     if ((file->fp = fopen(file->fname, "r")) == NULL) {
412     file->fp = NULL;
413     return NULL;
414     }
415    
416     fstat(fileno(file->fp), &stats);
417     if (S_ISFIFO(stats.st_mode)) {
418     if (fcntl(fileno(file->fp), F_SETFL, O_NONBLOCK) < 0)
419     perror("fcntl"), exit(1);
420     file->inode = 0;
421     } else
422     file->inode = stats.st_ino;
423    
424 pcg 1.2 if (opt_noinitial)
425     fseek (file->fp, 0, SEEK_END);
426     else if (stats.st_size > (listlen + 1) * width)
427     {
428     char dummy[255];
429    
430     fseek(file->fp, -((listlen + 2) * width), SEEK_END);
431     /* the pointer might point halfway some line. Let's
432     be nice and skip this damaged line */
433     lineinput(dummy, sizeof(dummy), file->fp);
434     }
435 pcg 1.1
436     file->last_size = stats.st_size;
437     return file->fp;
438     }
439    
440     void reopen(void)
441     {
442     struct logfile_entry *e;
443    
444     for (e = loglist; e; e = e->next) {
445     if (!e->inode)
446     continue; /* skip stdin */
447    
448     if (e->fp)
449     fclose(e->fp);
450     /* if fp is NULL we will try again later */
451     openlog(e);
452     }
453    
454     do_reopen = 0;
455     }
456    
457     void check_open_files(void)
458     {
459     struct logfile_entry *e;
460     struct stat stats;
461    
462     for (e = loglist; e; e = e->next) {
463     if (!e->inode)
464     continue; /* skip stdin */
465    
466     if (stat(e->fname, &stats) < 0) { /* file missing? */
467     sleep(1);
468     if (e->fp)
469     fclose(e->fp);
470     if (openlog(e) == NULL)
471     break;
472     }
473    
474     if (stats.st_ino != e->inode) { /* file renamed? */
475     if (e->fp)
476     fclose(e->fp);
477     if (openlog(e) == NULL)
478     break;
479     }
480    
481     if (stats.st_size < e->last_size) { /* file truncated? */
482     fseek(e->fp, 0, SEEK_SET);
483     e->last_size = stats.st_size;
484     }
485     }
486     }
487    
488     #define SCROLL_UP(lines, listlen) \
489     { \
490     int cur_line; \
491     for (cur_line = 0; cur_line < (listlen - 1); cur_line++) { \
492     strcpy(lines[cur_line].line, lines[cur_line + 1].line); \
493     lines[cur_line].color = lines[cur_line + 1].color; \
494     } \
495     }
496    
497     void main_loop(void)
498     {
499     struct linematrix *lines = xmalloc(sizeof(struct linematrix) * listlen);
500     int lin, miny, maxy, buflen;
501     char *buf;
502     time_t lastreload;
503     Region region = XCreateRegion();
504     XEvent xev;
505    
506     maxy = 0;
507     miny = win_y + h;
508     buflen = width + 2;
509     buf = xmalloc(buflen);
510     lastreload = time(NULL);
511    
512     /* Initialize linematrix */
513     for (lin = 0; lin < listlen; lin++) {
514     lines[lin].line = xmalloc(buflen);
515     strcpy(lines[lin].line, "~");
516     lines[lin].color = GetColor(def_color);
517     }
518    
519     if (!opt_noinitial)
520 pcg 1.7 {
521 pcg 1.1 while (lineinput(buf, buflen, loglist->fp) != 0) {
522     SCROLL_UP(lines, listlen);
523     /* print the next line */
524     strcpy(lines[listlen - 1].line, buf);
525     }
526    
527 pcg 1.7 redraw ();
528     }
529    
530 pcg 1.1 for (;;) {
531     int need_update = 0;
532     struct logfile_entry *current;
533     static struct logfile_entry *lastprinted = NULL;
534    
535     /* read logs */
536     for (current = loglist; current; current = current->next) {
537     if (!current->fp)
538     continue; /* skip missing files */
539    
540     clearerr(current->fp);
541    
542     while (lineinput(buf, buflen, current->fp) != 0) {
543     /* print filename if any, and if last line was from
544     different file */
545     if (!opt_nofilename &&
546     !(lastprinted && lastprinted == current) &&
547     current->desc[0]) {
548     SCROLL_UP(lines, listlen);
549     sprintf(lines[listlen - 1].line, "[%s]", current->desc);
550     lines[listlen - 1].color = current->color;
551     }
552    
553     SCROLL_UP(lines, listlen);
554     strcpy(lines[listlen - 1].line, buf);
555     lines[listlen - 1].color = current->color;
556    
557     lastprinted = current;
558     need_update = 1;
559     }
560     }
561    
562     if (need_update)
563     redraw();
564     else {
565     XFlush(disp);
566     if (!XPending(disp)) {
567     fd_set fdr;
568     struct timeval to = interval;
569    
570     FD_ZERO(&fdr);
571     FD_SET(ConnectionNumber(disp), &fdr);
572     select(ConnectionNumber(disp) + 1, &fdr, 0, 0, &to);
573     }
574     }
575    
576     check_open_files();
577    
578     if (do_reopen)
579     reopen();
580    
581     /* we ignore possible errors due to window resizing &c */
582     while (XPending(disp)) {
583     XNextEvent(disp, &xev);
584     switch (xev.type) {
585     case Expose:
586     {
587     XRectangle r;
588    
589     r.x = xev.xexpose.x;
590     r.y = xev.xexpose.y;
591     r.width = xev.xexpose.width;
592     r.height = xev.xexpose.height;
593     XUnionRectWithRegion(&r, region, region);
594     if (miny > r.y)
595     miny = r.y;
596     if (maxy < r.y + r.height)
597     maxy = r.y + r.height;
598     }
599     break;
600     default:
601     #ifdef DEBUGMODE
602     fprintf(stderr, "PANIC! Unknown event %d\n", xev.type);
603     #endif
604     break;
605     }
606     }
607    
608     /* reload if requested */
609     if (reload && lastreload + reload < time(NULL)) {
610     if (command)
611     system(command);
612    
613     reopen();
614     lastreload = time(NULL);
615     }
616    
617     if (!XEmptyRegion(region)) {
618     XSetRegion(disp, WinGC, region);
619     refresh(lines, miny, maxy);
620     XDestroyRegion(region);
621     region = XCreateRegion();
622     maxy = 0;
623     miny = win_y + h;
624     }
625     }
626     }
627    
628    
629     int main(int argc, char *argv[])
630     {
631     int i;
632     int opt_daemonize = 0;
633     #if HAS_REGEX
634     char *transform = NULL;
635     #endif
636    
637 pcg 1.7 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
638    
639 pcg 1.1 /* window needs to be initialized before colorlookups can be done */
640     /* just a dummy to get the color lookups right */
641     geom_mask = NoValue;
642     InitWindow();
643    
644     for (i = 1; i < argc; i++) {
645 pcg 1.8 const char *arg = argv[i];
646    
647     if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',') {
648     if (arg[1] == '-')
649     arg++;
650    
651     if (!strcmp(arg, "-?") ||
652     !strcmp(arg, "-help") || !strcmp(arg, "-h"))
653 pcg 1.1 display_help(argv[0]);
654 pcg 1.8 else if (!strcmp(arg, "-V"))
655 pcg 1.1 display_version();
656 pcg 1.8 else if (!strcmp(arg, "-g") || !strcmp(arg, "-geometry"))
657 pcg 1.1 geom_mask = XParseGeometry(argv[++i],
658     &win_x, &win_y, &width, &listlen);
659 pcg 1.8 else if (!strcmp(arg, "-display"))
660 pcg 1.1 dispname = argv[++i];
661 pcg 1.8 else if (!strcmp(arg, "-font") || !strcmp(arg, "-fn"))
662 pcg 1.1 fontname = argv[++i];
663     #if HAS_REGEX
664 pcg 1.8 else if (!strcmp(arg, "-t"))
665 pcg 1.1 transform = argv[++i];
666     #endif
667 pcg 1.8 else if (!strcmp(arg, "-fork") || !strcmp(arg, "-f"))
668 pcg 1.1 opt_daemonize = 1;
669 pcg 1.8 else if (!strcmp(arg, "-reload")) {
670 pcg 1.1 reload = atoi(argv[++i]);
671     command = argv[++i];
672     }
673 pcg 1.8 else if (!strcmp(arg, "-shade"))
674 pcg 1.1 opt_shade = 1;
675 pcg 1.8 else if (!strcmp(arg, "-frame"))
676 pcg 1.1 opt_frame = 1;
677 pcg 1.8 else if (!strcmp(arg, "-no-filename"))
678 pcg 1.1 opt_nofilename = 1;
679 pcg 1.8 else if (!strcmp(arg, "-reverse"))
680 pcg 1.1 opt_reverse = 1;
681 pcg 1.8 else if (!strcmp(arg, "-color"))
682 pcg 1.1 def_color = argv[++i];
683 pcg 1.8 else if (!strcmp(arg, "-noinitial"))
684 pcg 1.1 opt_noinitial = 1;
685 pcg 1.8 else if (!strcmp(arg, "-id"))
686     root = atoi (argv[++i]);
687     else if (!strcmp(arg, "-interval") || !strcmp(arg, "-i")) {
688 pcg 1.1 double iv = atof(argv[++i]);
689    
690     interval.tv_sec = (int) iv;
691     interval.tv_usec = (iv - interval.tv_sec) * 1e6;
692     } else {
693     fprintf(stderr, "Unknown option '%s'.\n"
694 pcg 1.8 "Try --help for more information.\n", arg);
695 pcg 1.1 exit(1);
696     }
697     } else { /* it must be a filename */
698     struct logfile_entry *e;
699     const char *fname, *desc, *fcolor = def_color;
700     char *p;
701    
702     /* this is not foolproof yet (',' in filenames are not allowed) */
703 pcg 1.8 fname = desc = arg;
704     if ((p = strchr(arg, ','))) {
705 pcg 1.1 *p = '\0';
706     fcolor = p + 1;
707    
708     if ((p = strchr(fcolor, ','))) {
709     *p = '\0';
710     desc = p + 1;
711     }
712     }
713    
714     e = xmalloc(sizeof(struct logfile_entry));
715 pcg 1.8 if (arg[0] == '-' && arg[1] == '\0') {
716 pcg 1.1 if ((e->fp = fdopen(0, "r")) == NULL)
717     perror("fdopen"), exit(1);
718     if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
719     perror("fcntl"), exit(1);
720     e->fname = NULL;
721     e->inode = 0;
722     e->desc = xstrdup("stdin");
723     } else {
724     int l;
725    
726     e->fname = xstrdup(fname);
727     if (openlog(e) == NULL)
728     perror(fname), exit(1);
729    
730     l = strlen(desc);
731     if (l > width - 2) /* must account for [ ] */
732     l = width - 2;
733     e->desc = xmalloc(l + 1);
734     memcpy(e->desc, desc, l);
735     *(e->desc + l) = '\0';
736     }
737    
738     e->color = GetColor(fcolor);
739     e->next = NULL;
740    
741     if (!loglist)
742     loglist = e;
743     if (loglist_tail)
744     loglist_tail->next = e;
745     loglist_tail = e;
746     }
747     }
748    
749     if (!loglist) {
750     fprintf(stderr, "You did not specify any files to tail\n"
751     "use %s --help for help\n", argv[0]);
752     exit(1);
753     }
754    
755     #if HAS_REGEX
756     if (transform) {
757     int i;
758    
759     transformre = xmalloc(sizeof(transformre));
760     i = regcomp(&transformre, transform, REG_EXTENDED);
761     if (i != 0) {
762     char buf[512];
763    
764     regerror(i, &transformre, buf, sizeof(buf));
765     fprintf(stderr, "Cannot compile regular expression: %s\n", buf);
766     }
767     }
768     #endif
769    
770     InitWindow();
771    
772     install_signal(SIGINT, blank_window);
773     install_signal(SIGQUIT, blank_window);
774     install_signal(SIGTERM, blank_window);
775     install_signal(SIGHUP, force_reopen);
776     install_signal(SIGUSR1, list_files);
777     install_signal(SIGUSR2, force_refresh);
778    
779     if (opt_daemonize)
780     daemonize();
781    
782     main_loop();
783    
784     exit(1); /* to make gcc -Wall stop complaining */
785     }
786    
787     void install_signal(int sig, void (*handler)(int))
788     {
789     struct sigaction action;
790    
791     action.sa_handler = handler;
792     sigemptyset(&action.sa_mask);
793     action.sa_flags = SA_RESTART;
794     if (sigaction(sig, &action, NULL) < 0)
795     fprintf(stderr, "sigaction(%d): %s\n", sig, strerror(errno)), exit(1);
796     }
797    
798     void *xstrdup(const char *string)
799     {
800     void *p;
801    
802     while ((p = strdup(string)) == NULL) {
803     fprintf(stderr, "Memory exausted.");
804     sleep(10);
805     }
806     return p;
807     }
808    
809     void *xmalloc(size_t size)
810     {
811     void *p;
812    
813     while ((p = malloc(size)) == NULL) {
814     fprintf(stderr, "Memory exausted.");
815     sleep(10);
816     }
817     return p;
818     }
819    
820     void display_help(char *myname)
821     {
822     printf("Usage: %s [options] file1[,color[,desc]] "
823     "[file2[,color[,desc]] ...]\n", myname);
824     printf(" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
825     " -color color use color $color as default\n"
826     " -reload sec command reload after $sec and run command\n"
827     " by default -- 3 mins\n"
828 pcg 1.8 " -id id window id to use instead of the root window\n"
829 pcg 1.1 " -font FONTSPEC (-fn) font to use\n"
830     " -f | -fork fork into background\n"
831     " -reverse print new lines at the top\n"
832     " -shade add shading to font\n"
833     " -noinitial don't display the last file lines on\n"
834     " startup\n"
835     " -i | -interval seconds interval between checks (fractional\n"
836     " values o.k.). Default 3\n"
837     " -V display version information and exit\n"
838     "\n");
839     printf("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green "
840     "/var/log/secure,red,'ALERT'\n", myname);
841     exit(0);
842     }
843    
844     void display_version(void) {
845     printf("root-tail version " VERSION "\n");
846     exit(0);
847     }
848    
849     int daemonize(void) {
850     switch (fork()) {
851     case -1:
852     return -1;
853     case 0:
854     break;
855     default:
856     _exit(0);
857     }
858    
859     if (setsid() == -1)
860     return -1;
861    
862     return 0;
863     }