ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.10
Committed: Fri Mar 26 12:11:05 2004 UTC (22 years, 5 months ago) by chris_moore
Content type: text/plain
Branch: MAIN
Changes since 1.9: +7 -3 lines
Log Message:
	- call redraw() before entering the main loop to ensure that
          at least something is displayed on the root window
          immediately

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