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