ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.25
Committed: Sun Mar 28 03:53:48 2004 UTC (22 years, 5 months ago) by chris_moore
Content type: text/plain
Branch: MAIN
Changes since 1.24: +4 -1 lines
Log Message:
Fixed bug with very long (> 1024 character) lines being broken up.

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.22 int font_ascent, font_height;
72 pcg 1.1 int do_reopen;
73 pcg 1.21 struct timeval interval = { 2, 400000 };
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 chris_moore 1.24 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
79     geom_mask, reload = 0;
80 pcg 1.1 const char *command = NULL,
81 pcg 1.11 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
82     *continuation = "[+]";
83 pcg 1.1
84     struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
85    
86     Display *disp;
87     Window root;
88     GC WinGC;
89    
90     #if HAS_REGEX
91 pcg 1.11 struct re_list
92     {
93     regex_t from;
94     const char *to;
95     struct re_list *next;
96 pcg 1.1 };
97     struct re_list *re_head, *re_tail;
98     #endif
99    
100    
101     /* prototypes */
102 pcg 1.11 void list_files (int);
103     void force_reopen (int);
104     void force_refresh (int);
105     void blank_window (int);
106    
107     void InitWindow (void);
108     unsigned long GetColor (const char *);
109     void redraw (void);
110 pcg 1.22 void refresh (int, int, int);
111 pcg 1.11
112     void transform_line (char *s);
113     int lineinput (struct logfile_entry *);
114     void reopen (void);
115     void check_open_files (void);
116     FILE *openlog (struct logfile_entry *);
117 pcg 1.12 static void main_loop (void);
118 pcg 1.11
119     void display_version (void);
120     void display_help (char *);
121     void install_signal (int, void (*)(int));
122     void *xstrdup (const char *);
123     void *xmalloc (size_t);
124     int daemonize (void);
125 pcg 1.1
126     /* signal handlers */
127 pcg 1.11 void
128     list_files (int dummy)
129 pcg 1.1 {
130 pcg 1.11 struct logfile_entry *e;
131 pcg 1.1
132 pcg 1.11 fprintf (stderr, "Files opened:\n");
133     for (e = loglist; e; e = e->next)
134     fprintf (stderr, "\t%s (%s)\n", e->fname, e->desc);
135 pcg 1.1 }
136    
137 pcg 1.11 void
138     force_reopen (int dummy)
139 pcg 1.1 {
140 pcg 1.11 do_reopen = 1;
141 pcg 1.1 }
142    
143 pcg 1.11 void
144     force_refresh (int dummy)
145 pcg 1.1 {
146 pcg 1.11 redraw ();
147 pcg 1.1 }
148    
149 pcg 1.11 void
150     blank_window (int dummy)
151 pcg 1.1 {
152 pcg 1.19 XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False);
153 pcg 1.11 XFlush (disp);
154     exit (0);
155 pcg 1.1 }
156    
157     /* X related functions */
158 pcg 1.11 unsigned long
159     GetColor (const char *ColorName)
160 pcg 1.1 {
161 pcg 1.11 XColor Color;
162     XWindowAttributes Attributes;
163 pcg 1.1
164 pcg 1.11 XGetWindowAttributes (disp, root, &Attributes);
165     Color.pixel = 0;
166 pcg 1.8
167 pcg 1.11 if (!XParseColor (disp, Attributes.colormap, ColorName, &Color))
168     fprintf (stderr, "can't parse %s\n", ColorName);
169     else if (!XAllocColor (disp, Attributes.colormap, &Color))
170     fprintf (stderr, "can't allocate %s\n", ColorName);
171 pcg 1.8
172 pcg 1.11 return Color.pixel;
173 pcg 1.1 }
174    
175 pcg 1.11 static Window
176     root_window (Display * display, int screen_number)
177 pcg 1.7 {
178 pcg 1.11 Atom SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False);
179 pcg 1.7 Window real_root_window = RootWindow (display, screen_number);
180    
181 pcg 1.11 if (root) /* root window set via option */
182 pcg 1.8 return root;
183    
184 pcg 1.11 if (SWM_VROOT != None)
185 pcg 1.7 {
186     Window unused, *windows;
187     unsigned int count;
188    
189     if (XQueryTree (display, real_root_window, &unused, &unused, &windows,
190 pcg 1.11 &count))
191     {
192     int i;
193    
194     for (i = 0; i < count; i++)
195     {
196     Atom type;
197     int format;
198     unsigned long nitems, bytes_after_return;
199     unsigned char *virtual_root_window;
200    
201     if (XGetWindowProperty (display, windows[i], SWM_VROOT,
202     0, 1, False, XA_WINDOW, &type, &format,
203     &nitems, &bytes_after_return,
204     &virtual_root_window) == Success)
205     {
206     if (type != None)
207     {
208     if (type == XA_WINDOW)
209     {
210     XFree (windows);
211     return (Window) virtual_root_window;
212     }
213     else
214     fprintf (stderr,
215     "__SWM_VROOT property type mismatch");
216     }
217     }
218     else
219     fprintf (stderr,
220     "failed to get __SWM_VROOT property on window 0x%lx",
221     windows[i]);
222     }
223    
224     if (count)
225     XFree (windows);
226     }
227 pcg 1.7 else
228 pcg 1.11 fprintf (stderr, "Can't query tree on root window 0x%lx",
229     real_root_window);
230 pcg 1.7 }
231     else
232     /* This shouldn't happen. The Xlib documentation is wrong BTW. */
233     fprintf (stderr, "Can't intern atom __SWM_VROOT");
234    
235     return real_root_window;
236     }
237    
238 pcg 1.11 void
239     InitWindow (void)
240 pcg 1.1 {
241 pcg 1.11 XGCValues gcv;
242     unsigned long gcm;
243     int screen, ScreenWidth, ScreenHeight;
244    
245     if (!(disp = XOpenDisplay (dispname)))
246     {
247     fprintf (stderr, "Can't open display %s.\n", dispname);
248     exit (1);
249     }
250    
251     screen = DefaultScreen (disp);
252     ScreenHeight = DisplayHeight (disp, screen);
253     ScreenWidth = DisplayWidth (disp, screen);
254    
255     root = root_window (disp, screen);
256    
257     gcm = GCBackground;
258     gcv.graphics_exposures = True;
259     WinGC = XCreateGC (disp, root, gcm, &gcv);
260     XMapWindow (disp, root);
261     XSetForeground (disp, WinGC, GetColor (DEF_COLOR));
262    
263     {
264     char **missing_charset_list;
265     int missing_charset_count;
266     char *def_string;
267    
268     fontset = XCreateFontSet (disp, fontname,
269     &missing_charset_list, &missing_charset_count,
270     &def_string);
271    
272     if (missing_charset_count)
273     {
274     fprintf (stderr,
275     "Missing charsets in String to FontSet conversion (%s)\n",
276     missing_charset_list[0]);
277     XFreeStringList (missing_charset_list);
278     }
279     }
280    
281     if (!fontset)
282     {
283     fprintf (stderr, "unable to create fontset, exiting.\n");
284     exit (1);
285     }
286    
287     {
288     XFontSetExtents *xfe = XExtentsOfFontSet (fontset);
289    
290     font_height = xfe->max_logical_extent.height;
291 pcg 1.22 font_ascent = -xfe->max_logical_extent.y;
292 pcg 1.11 }
293    
294     if (geom_mask & XNegative)
295     win_x = win_x + ScreenWidth - width;
296     if (geom_mask & YNegative)
297     win_y = win_y + ScreenHeight - height;
298    
299     listlen = height / font_height;
300 pcg 1.1
301 pcg 1.11 if (!listlen)
302     {
303     fprintf (stderr, "height too small for a single line, setting to %d\n",
304     font_height);
305     listlen = 1;
306     }
307    
308     height = listlen * font_height;
309    
310     XSelectInput (disp, root, ExposureMask | FocusChangeMask);
311 pcg 1.1 }
312    
313     /*
314     * redraw does a complete redraw, rather than an update (i.e. the area
315     * gets cleared first)
316     * the rest is handled by regular refresh()'es
317     */
318 pcg 1.11 void
319     redraw (void)
320 pcg 1.1 {
321 pcg 1.20 XSetClipMask (disp, WinGC, None);
322 pcg 1.22 refresh (0, 32768, 1);
323 pcg 1.1 }
324    
325     /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
326 pcg 1.11 void
327 pcg 1.22 refresh (int miny, int maxy, int clear)
328 pcg 1.1 {
329 pcg 1.11 int lin;
330     int offset = (listlen + 1) * font_height;
331     unsigned long black_color = GetColor ("black");
332 pcg 1.1
333 pcg 1.11 miny -= win_y + font_height;
334     maxy -= win_y - font_height;
335 pcg 1.1
336 pcg 1.22 if (clear && !opt_noflicker)
337     XClearArea (disp, root, win_x, win_y, width, height, False);
338    
339 pcg 1.11 for (lin = listlen; lin--;)
340     {
341 pcg 1.12 struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin);
342 pcg 1.7
343 pcg 1.11 offset -= font_height;
344 pcg 1.7
345 pcg 1.11 if (offset < miny || offset > maxy)
346     continue;
347 jebusbfb 1.4
348 pcg 1.22 if (clear && opt_noflicker)
349     XClearArea (disp, root, win_x, win_y + offset - font_ascent, width, font_height, False);
350    
351 pcg 1.21 if (opt_outline)
352     {
353     XSetForeground (disp, WinGC, black_color);
354     XmbDrawString (disp, root, fontset, WinGC, win_x - 1,
355     win_y + offset + 1, line->line, line->len);
356     XmbDrawString (disp, root, fontset, WinGC, win_x + 1,
357     win_y + offset + 1, line->line, line->len);
358     XmbDrawString (disp, root, fontset, WinGC, win_x - 1,
359     win_y + offset - 1, line->line, line->len);
360     XmbDrawString (disp, root, fontset, WinGC, win_x + 1,
361     win_y + offset - 1, line->line, line->len);
362     }
363     else if (opt_shade)
364 pcg 1.11 {
365     XSetForeground (disp, WinGC, black_color);
366     XmbDrawString (disp, root, fontset, WinGC, win_x + 2,
367 pcg 1.12 win_y + offset + 2, line->line, line->len);
368 pcg 1.11 }
369    
370     XSetForeground (disp, WinGC, line->color);
371     XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset,
372 pcg 1.12 line->line, line->len);
373 pcg 1.1 }
374    
375 pcg 1.11 if (opt_frame)
376 pcg 1.15 {
377     XSetForeground (disp, WinGC, GetColor (def_color));
378     XDrawRectangle (disp, root, WinGC, win_x - 2, win_y - 2, width + 4, height + 4);
379     }
380 pcg 1.1 }
381    
382     #if HAS_REGEX
383 pcg 1.11 void
384     transform_line (char *s)
385 pcg 1.1 {
386     #ifdef I_AM_Md
387 pcg 1.11 int i;
388     if (1)
389     {
390     for (i = 16; s[i]; i++)
391     s[i] = s[i + 11];
392 pcg 1.1 }
393 pcg 1.11 s[i + 1] = '\0';
394 pcg 1.1 #endif
395    
396 pcg 1.11 if (transformre)
397     {
398     int i;
399     regmatch_t matched[16];
400    
401     i = regexec (&transformre, string, 16, matched, 0);
402     if (i == 0)
403     { /* matched */
404     }
405 pcg 1.1 }
406     }
407     #endif
408    
409 pcg 1.12 char *
410     concat_line (const char *p1, const char *p2)
411     {
412     int l1 = p1 ? strlen (p1) : 0;
413     int l2 = strlen (p2);
414     char *r = xmalloc (l1 + l2 + 1);
415    
416     memcpy (r, p1, l1);
417     memcpy (r + l1, p2, l2);
418     r[l1 + l2] = 0;
419    
420     return r;
421     }
422 pcg 1.1
423     /*
424 pcg 1.12 * This routine should read a single line, no matter how long.
425 pcg 1.1 */
426 pcg 1.11 int
427     lineinput (struct logfile_entry *logfile)
428 pcg 1.1 {
429 pcg 1.12 char buff[1024], *p = buff;
430     int ch;
431     int ofs = logfile->buf ? strlen (logfile->buf) : 0;
432 pcg 1.1
433 pcg 1.11 do
434     {
435 pcg 1.12 ch = fgetc (logfile->fp);
436 pcg 1.1
437 pcg 1.18 if (ch == '\n' || ch == EOF)
438     break;
439 pcg 1.17 else if (ch == '\r')
440 pcg 1.18 continue; /* skip */
441 pcg 1.12 else if (ch == '\t')
442     {
443     do
444     {
445     *p++ = ' ';
446     ofs++;
447     }
448     while (ofs & 7);
449     }
450     else
451     {
452     *p++ = ch;
453     ofs++;
454     }
455 pcg 1.11 }
456 pcg 1.12 while (p < buff + (sizeof buff) - 8 - 1);
457 pcg 1.1
458 pcg 1.19 if (p == buff && ch == EOF)
459 pcg 1.12 return 0;
460    
461     *p = 0;
462    
463     p = concat_line (logfile->buf, buff);
464     free (logfile->buf); logfile->buf = p;
465 chris_moore 1.9
466 pcg 1.12 logfile->lastpartial = logfile->partial;
467 chris_moore 1.25 /* there are 3 ways we could have exited the loop: reading '\n',
468     * reaching EOF, or filling the buffer; the 2nd and 3rd of these
469     * both result in a partial line */
470     logfile->partial = ch != '\n';
471 pcg 1.12
472     if (logfile->partial && opt_whole)
473 pcg 1.11 return 0;
474 pcg 1.1
475     #if HAS_REGEX
476 pcg 1.12 transform_line (logfile->buf);
477 pcg 1.1 #endif
478 pcg 1.12 return 1;
479 pcg 1.1 }
480    
481     /* input: reads file->fname
482     * output: fills file->fp, file->inode
483     * returns file->fp
484     * in case of error, file->fp is NULL
485     */
486 pcg 1.11 FILE *
487     openlog (struct logfile_entry * file)
488     {
489     struct stat stats;
490    
491     if ((file->fp = fopen (file->fname, "r")) == NULL)
492     {
493     file->fp = NULL;
494     return NULL;
495     }
496    
497     fstat (fileno (file->fp), &stats);
498     if (S_ISFIFO (stats.st_mode))
499     {
500     if (fcntl (fileno (file->fp), F_SETFL, O_NONBLOCK) < 0)
501     perror ("fcntl"), exit (1);
502     file->inode = 0;
503     }
504     else
505     file->inode = stats.st_ino;
506    
507     if (opt_noinitial)
508     fseek (file->fp, 0, SEEK_END);
509     else if (stats.st_size > (listlen + 1) * width)
510     fseek (file->fp, -((listlen + 2) * width), SEEK_END);
511    
512     file->last_size = stats.st_size;
513     return file->fp;
514     }
515    
516     void
517     reopen (void)
518 pcg 1.1 {
519 pcg 1.11 struct logfile_entry *e;
520    
521     for (e = loglist; e; e = e->next)
522     {
523     if (!e->inode)
524     continue; /* skip stdin */
525 pcg 1.1
526 pcg 1.11 if (e->fp)
527     fclose (e->fp);
528     /* if fp is NULL we will try again later */
529     openlog (e);
530     }
531    
532     do_reopen = 0;
533     }
534 pcg 1.1
535 pcg 1.11 void
536     check_open_files (void)
537     {
538     struct logfile_entry *e;
539     struct stat stats;
540 pcg 1.1
541 pcg 1.11 for (e = loglist; e; e = e->next)
542     {
543     if (!e->inode)
544     continue; /* skip stdin */
545 pcg 1.1
546 pcg 1.11 if (stat (e->fname, &stats) < 0)
547     { /* file missing? */
548     sleep (1);
549     if (e->fp)
550     fclose (e->fp);
551     if (openlog (e) == NULL)
552 chris_moore 1.23 continue;
553 pcg 1.11 }
554    
555 chris_moore 1.23 /* HACK this - stats can be uninitialised here (if the file
556     * didn't exist when stat() was called, but was recreated during
557     * the sleep(1)) */
558 pcg 1.11 if (stats.st_ino != e->inode)
559     { /* file renamed? */
560     if (e->fp)
561     fclose (e->fp);
562     if (openlog (e) == NULL)
563 chris_moore 1.23 continue;
564 pcg 1.11 }
565    
566     if (stats.st_size < e->last_size)
567     { /* file truncated? */
568     fseek (e->fp, 0, SEEK_SET);
569     e->last_size = stats.st_size;
570     }
571 pcg 1.1 }
572     }
573    
574 pcg 1.14 /*
575     * insert a single physical line (that must be short enough to fit)
576     * at position "idx" by pushing up lines above it. the caller
577     * MUST then fill in lines[idx] with valid data.
578     */
579 pcg 1.12 static void
580     insert_line (int idx)
581     {
582     int cur_line;
583     struct logfile_entry *current;
584    
585     free (lines[0].line);
586    
587     for (cur_line = 0; cur_line < idx; cur_line++)
588     lines[cur_line] = lines[cur_line + 1];
589    
590     for (current = loglist; current; current = current->next)
591 pcg 1.16 if (current->index <= idx)
592 pcg 1.12 current->index--;
593 pcg 1.11 }
594    
595 pcg 1.14 /*
596     * remove a single physical line at position "idx" by moving the lines above it
597     * down and inserting a "~" line at the top.
598     */
599 pcg 1.12 static void
600     delete_line (int idx)
601     {
602     int cur_line;
603     struct logfile_entry *current;
604    
605     for (cur_line = idx; cur_line > 0; cur_line--)
606     lines[cur_line] = lines[cur_line - 1];
607    
608     lines[0].line = strdup ("~");
609    
610     for (current = loglist; current; current = current->next)
611 pcg 1.16 if (current->index >= 0 && current->index <= idx)
612 pcg 1.12 current->index++;
613     }
614    
615 pcg 1.14 /*
616 chris_moore 1.24 * takes a logical log file line and splits it into multiple physical
617 pcg 1.14 * screen lines by splitting it whenever a part becomes too long.
618     * lal lines will be inserted at position "idx".
619     */
620 pcg 1.12 static void
621     split_line (int idx, const char *str, unsigned long color)
622     {
623     int l = strlen (str);
624     const char *p = str;
625    
626     do
627     {
628     const char *beg = p;
629 chris_moore 1.24 int w = 0, wrapped = 0;
630     const char *break_p = NULL;
631 pcg 1.12
632     while (*p)
633     {
634 chris_moore 1.24 /* find the length in bytes of the next multibyte character */
635 pcg 1.12 int len = mblen (p, l);
636     if (len <= 0)
637 chris_moore 1.24 len = 1; /* ignore (don't skip) illegal character sequences */
638 pcg 1.12
639 chris_moore 1.24 /* find the width in pixels of the next character */
640 pcg 1.12 int cw = XmbTextEscapement (fontset, p, len);
641     if (cw + w >= width)
642 chris_moore 1.24 {
643     wrapped = 1;
644     break;
645     }
646    
647     if (opt_wordwrap && len == 1 && p[0] == ' ')
648     break_p = p;
649 pcg 1.12
650     w += cw;
651     p += len;
652     l -= len;
653     }
654    
655 chris_moore 1.24 /* if we're wrapping at spaces, and the line is long enough to
656     * wrap, and we've seen a space already, and the space wasn't
657     * the first character on the line, then wrap at the space */
658     if (opt_wordwrap && wrapped && break_p && break_p != beg)
659     {
660     l += p - break_p;
661     p = break_p;
662     }
663    
664 pcg 1.12 {
665     char *s = xmalloc (p - beg + 1);
666     memcpy (s, beg, p - beg);
667     s[p - beg] = 0;
668     insert_line (idx);
669     lines[idx].line = s;
670     lines[idx].len = p - beg;
671     lines[idx].color = color;
672     }
673 chris_moore 1.24
674     /* if we wrapped at a space, don't display the space */
675     if (opt_wordwrap && wrapped && break_p && break_p != beg)
676     {
677     l--;
678     p++;
679     }
680 pcg 1.12 }
681     while (l);
682     }
683    
684 pcg 1.14 /*
685     * append something to an existing physical line. this is done
686     * by deleting the file on-screen, concatenating the new data to it
687     * and splitting it again.
688     */
689 pcg 1.12 static void
690     append_line (int idx, const char *str)
691     {
692     unsigned long color = lines[idx].color;
693     char *old = lines[idx].line;
694     char *new = concat_line (old, str);
695    
696     free (old);
697    
698     delete_line (idx);
699     split_line (idx, new, color);
700     }
701    
702     static void
703 pcg 1.11 main_loop (void)
704     {
705 pcg 1.12 lines = xmalloc (sizeof (struct linematrix) * listlen);
706 pcg 1.20 int lin;
707 pcg 1.11 time_t lastreload;
708     Region region = XCreateRegion ();
709     XEvent xev;
710     struct logfile_entry *lastprinted = NULL;
711     struct logfile_entry *current;
712 pcg 1.18 int need_update = 1;
713 pcg 1.11
714     lastreload = time (NULL);
715    
716     /* Initialize linematrix */
717     for (lin = 0; lin < listlen; lin++)
718     {
719 pcg 1.12 lines[lin].line = strdup ("~");
720     lines[lin].len = 1;
721 pcg 1.11 lines[lin].color = GetColor (def_color);
722     }
723    
724     for (;;)
725     {
726     /* read logs */
727     for (current = loglist; current; current = current->next)
728     {
729     if (!current->fp)
730     continue; /* skip missing files */
731    
732     clearerr (current->fp);
733    
734 pcg 1.12 while (lineinput (current))
735 pcg 1.11 {
736 pcg 1.12 need_update = 1;
737 pcg 1.11 /* if we're trying to update old partial lines in
738     * place, and the last time this file was updated the
739     * output was partial, and that partial line is not
740     * too close to the top of the screen, then update
741     * that partial line */
742 pcg 1.16 if (opt_update && current->lastpartial && current->index >= 0)
743 pcg 1.11 {
744 pcg 1.16 int idx = current->index;
745     append_line (idx, current->buf);
746     current->index = idx;
747 pcg 1.13 free (current->buf), current->buf = 0;
748 pcg 1.12 continue;
749 pcg 1.11 }
750    
751     /* print filename if any, and if last line was from
752     * different file */
753 pcg 1.13 if (!opt_nofilename && lastprinted != current && current->desc[0])
754 pcg 1.11 {
755 pcg 1.12 char buf[1024]; /* HACK */
756     snprintf (buf, sizeof (buf), "[%s]", current->desc);
757     split_line (listlen - 1, buf, current->color);
758 pcg 1.11 }
759    
760     /* if this is the same file we showed last, and the
761     * last time we showed it, it wasn't finished, then
762     * append to the last line shown */
763     if (lastprinted == current && current->lastpartial)
764 chris_moore 1.23 append_line (listlen - 1, current->buf);
765     else if (current->lastpartial)
766     {
767     split_line (listlen - 1, continuation, current->color);
768 pcg 1.12 append_line (listlen - 1, current->buf);
769 chris_moore 1.23 }
770 pcg 1.11 else
771 chris_moore 1.23 split_line (listlen - 1, current->buf, current->color);
772 pcg 1.11
773 chris_moore 1.23 free (current->buf), current->buf = 0;
774 pcg 1.11 current->index = listlen - 1;
775     lastprinted = current;
776     }
777     }
778    
779     if (need_update)
780 pcg 1.18 {
781     redraw ();
782     need_update = 0;
783     }
784 pcg 1.11 else
785     {
786     XFlush (disp);
787    
788     if (!XPending (disp))
789     {
790     fd_set fdr;
791     struct timeval to = interval;
792    
793     FD_ZERO (&fdr);
794     FD_SET (ConnectionNumber (disp), &fdr);
795     select (ConnectionNumber (disp) + 1, &fdr, 0, 0, &to);
796     }
797     }
798    
799     check_open_files ();
800    
801     if (do_reopen)
802     reopen ();
803    
804     /* we ignore possible errors due to window resizing &c */
805     while (XPending (disp))
806     {
807     XNextEvent (disp, &xev);
808    
809     switch (xev.type)
810     {
811     case Expose:
812     {
813     XRectangle r;
814    
815     r.x = xev.xexpose.x;
816     r.y = xev.xexpose.y;
817     r.width = xev.xexpose.width;
818     r.height = xev.xexpose.height;
819 pcg 1.20
820 pcg 1.11 XUnionRectWithRegion (&r, region, region);
821     }
822     break;
823     default:
824     #ifdef DEBUGMODE
825     fprintf (stderr, "PANIC! Unknown event %d\n", xev.type);
826     #endif
827     break;
828     }
829     }
830    
831     /* reload if requested */
832     if (reload && lastreload + reload < time (NULL))
833     {
834     if (command && command[0])
835     system (command);
836    
837     reopen ();
838     lastreload = time (NULL);
839     }
840    
841     if (!XEmptyRegion (region))
842     {
843 pcg 1.20 XRectangle r;
844    
845 pcg 1.11 XSetRegion (disp, WinGC, region);
846 pcg 1.20 XClipBox (region, &r);
847    
848 pcg 1.22 refresh (r.y, r.y + r.height, 0);
849 pcg 1.11
850     XDestroyRegion (region);
851     region = XCreateRegion ();
852     }
853     }
854     }
855 pcg 1.1
856 pcg 1.11
857     int
858     main (int argc, char *argv[])
859 pcg 1.1 {
860 pcg 1.11 int i;
861     int opt_daemonize = 0;
862     int opt_partial = 0, file_count = 0;
863 pcg 1.1 #if HAS_REGEX
864 pcg 1.11 char *transform = NULL;
865 pcg 1.1 #endif
866    
867 pcg 1.11 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
868    
869     /* window needs to be initialized before colorlookups can be done */
870     /* just a dummy to get the color lookups right */
871     geom_mask = NoValue;
872     InitWindow ();
873    
874     for (i = 1; i < argc; i++)
875     {
876     const char *arg = argv[i];
877 pcg 1.7
878 pcg 1.11 if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',')
879     {
880     if (arg[1] == '-')
881     arg++;
882    
883     if (!strcmp (arg, "-?") ||
884     !strcmp (arg, "-help") || !strcmp (arg, "-h"))
885     display_help (argv[0]);
886     else if (!strcmp (arg, "-V"))
887     display_version ();
888     else if (!strcmp (arg, "-g") || !strcmp (arg, "-geometry"))
889     geom_mask =
890     XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
891     else if (!strcmp (arg, "-display"))
892     dispname = argv[++i];
893     else if (!strcmp (arg, "-cont"))
894     continuation = argv[++i];
895     else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
896     fontname = argv[++i];
897 pcg 1.1 #if HAS_REGEX
898 pcg 1.11 else if (!strcmp (arg, "-t"))
899     transform = argv[++i];
900 pcg 1.1 #endif
901 pcg 1.11 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
902     opt_daemonize = 1;
903     else if (!strcmp (arg, "-reload"))
904     {
905     reload = atoi (argv[++i]);
906     command = argv[++i];
907     }
908     else if (!strcmp (arg, "-shade"))
909     opt_shade = 1;
910 pcg 1.21 else if (!strcmp (arg, "-outline"))
911     opt_outline = 1;
912 pcg 1.22 else if (!strcmp (arg, "-noflicker"))
913     opt_noflicker = 1;
914 pcg 1.11 else if (!strcmp (arg, "-frame"))
915     opt_frame = 1;
916     else if (!strcmp (arg, "-no-filename"))
917     opt_nofilename = 1;
918     else if (!strcmp (arg, "-reverse"))
919     opt_reverse = 1;
920     else if (!strcmp (arg, "-whole"))
921     opt_whole = 1;
922     else if (!strcmp (arg, "-partial"))
923     opt_partial = 1;
924     else if (!strcmp (arg, "-update"))
925     opt_update = opt_partial = 1;
926 chris_moore 1.24 else if (!strcmp (arg, "-wordwrap"))
927     opt_wordwrap = 1;
928 pcg 1.11 else if (!strcmp (arg, "-color"))
929     def_color = argv[++i];
930     else if (!strcmp (arg, "-noinitial"))
931     opt_noinitial = 1;
932     else if (!strcmp (arg, "-id"))
933     root = atoi (argv[++i]);
934     else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i"))
935     {
936     double iv = atof (argv[++i]);
937    
938     interval.tv_sec = (int) iv;
939     interval.tv_usec = (iv - interval.tv_sec) * 1e6;
940     }
941     else
942     {
943     fprintf (stderr, "Unknown option '%s'.\n"
944     "Try --help for more information.\n", arg);
945     exit (1);
946     }
947     }
948     else
949     { /* it must be a filename */
950     struct logfile_entry *e;
951     const char *fname, *desc, *fcolor = def_color;
952     char *p;
953    
954     file_count++;
955    
956     /* this is not foolproof yet (',' in filenames are not allowed) */
957     fname = desc = arg;
958     if ((p = strchr (arg, ',')))
959     {
960     *p = '\0';
961     fcolor = p + 1;
962    
963     if ((p = strchr (fcolor, ',')))
964     {
965     *p = '\0';
966     desc = p + 1;
967     }
968     }
969    
970     e = xmalloc (sizeof (struct logfile_entry));
971 pcg 1.12 e->partial = 0;
972     e->buf = 0;
973 pcg 1.16 e->index = -1;
974 pcg 1.12
975 pcg 1.11 if (arg[0] == '-' && arg[1] == '\0')
976     {
977     if ((e->fp = fdopen (0, "r")) == NULL)
978     perror ("fdopen"), exit (1);
979     if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
980     perror ("fcntl"), exit (1);
981     e->fname = NULL;
982     e->inode = 0;
983     e->desc = xstrdup ("stdin");
984     }
985     else
986     {
987     int l;
988    
989     e->fname = xstrdup (fname);
990     if (openlog (e) == NULL)
991     perror (fname), exit (1);
992    
993     l = strlen (desc);
994     if (l > width - 2) /* must account for [ ] */
995     l = width - 2;
996     e->desc = xmalloc (l + 1);
997     memcpy (e->desc, desc, l);
998     *(e->desc + l) = '\0';
999     }
1000    
1001     e->color = GetColor (fcolor);
1002     e->partial = 0;
1003     e->next = NULL;
1004    
1005     if (!loglist)
1006     loglist = e;
1007     if (loglist_tail)
1008     loglist_tail->next = e;
1009     loglist_tail = e;
1010     }
1011     }
1012    
1013     if (!loglist)
1014     {
1015     fprintf (stderr, "You did not specify any files to tail\n"
1016     "use %s --help for help\n", argv[0]);
1017     exit (1);
1018     }
1019    
1020     if (opt_partial && opt_whole)
1021     {
1022     fprintf (stderr, "Specify at most one of -partial and -whole\n");
1023     exit (1);
1024 chris_moore 1.9 }
1025    
1026 pcg 1.11 if (opt_partial)
1027 chris_moore 1.9 /* if we specifically requested to see partial lines then don't insist on whole lines */
1028 pcg 1.11 opt_whole = 0;
1029     else if (file_count > 1)
1030 chris_moore 1.9 /* otherwise, if we've viewing multiple files, default to showing whole lines */
1031 pcg 1.11 opt_whole = 1;
1032 chris_moore 1.9
1033 pcg 1.1 #if HAS_REGEX
1034 pcg 1.11 if (transform)
1035     {
1036     int i;
1037 pcg 1.1
1038 pcg 1.11 transformre = xmalloc (sizeof (transformre));
1039     i = regcomp (&transformre, transform, REG_EXTENDED);
1040     if (i != 0)
1041     {
1042     char buf[512];
1043    
1044     regerror (i, &transformre, buf, sizeof (buf));
1045     fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1046     }
1047 pcg 1.1 }
1048     #endif
1049    
1050 pcg 1.11 InitWindow ();
1051 pcg 1.1
1052 pcg 1.11 install_signal (SIGINT, blank_window);
1053     install_signal (SIGQUIT, blank_window);
1054     install_signal (SIGTERM, blank_window);
1055     install_signal (SIGHUP, force_reopen);
1056     install_signal (SIGUSR1, list_files);
1057     install_signal (SIGUSR2, force_refresh);
1058 pcg 1.1
1059 pcg 1.11 if (opt_daemonize)
1060     daemonize ();
1061 pcg 1.1
1062 pcg 1.11 main_loop ();
1063 pcg 1.1
1064 pcg 1.11 exit (1); /* to make gcc -Wall stop complaining */
1065 pcg 1.1 }
1066    
1067 pcg 1.11 void
1068     install_signal (int sig, void (*handler) (int))
1069 pcg 1.1 {
1070 pcg 1.11 struct sigaction action;
1071    
1072     action.sa_handler = handler;
1073     sigemptyset (&action.sa_mask);
1074     action.sa_flags = SA_RESTART;
1075 pcg 1.1
1076 pcg 1.11 if (sigaction (sig, &action, NULL) < 0)
1077     fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1);
1078 pcg 1.1 }
1079    
1080 pcg 1.11 void *
1081     xstrdup (const char *string)
1082 pcg 1.1 {
1083 pcg 1.11 void *p;
1084 pcg 1.1
1085 pcg 1.11 while ((p = strdup (string)) == NULL)
1086     {
1087     fprintf (stderr, "Memory exausted.");
1088     sleep (10);
1089 pcg 1.1 }
1090 pcg 1.11
1091     return p;
1092 pcg 1.1 }
1093    
1094 pcg 1.11 void *
1095     xmalloc (size_t size)
1096 pcg 1.1 {
1097 pcg 1.11 void *p;
1098 pcg 1.1
1099 pcg 1.11 while ((p = malloc (size)) == NULL)
1100     {
1101     fprintf (stderr, "Memory exausted.");
1102     sleep (10);
1103 pcg 1.1 }
1104 pcg 1.12
1105 pcg 1.11 return p;
1106 pcg 1.1 }
1107    
1108 pcg 1.11 void
1109     display_help (char *myname)
1110     {
1111     printf ("Usage: %s [options] file1[,color[,desc]] "
1112     "[file2[,color[,desc]] ...]\n", myname);
1113     printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1114     " -color color use color $color as default\n"
1115     " -reload sec command reload after $sec and run command\n"
1116     " -id id window id to use instead of the root window\n"
1117     " -font FONTSPEC (-fn) font to use\n"
1118     " -f | -fork fork into background\n"
1119     " -reverse print new lines at the top\n"
1120     " -whole wait for \\n before showing a line\n"
1121     " -partial show lines even if they don't end with a \\n\n"
1122     " -update allow updates to old partial lines\n"
1123     " -cont string to prefix continued partial lines with\n"
1124 chris_moore 1.24 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1125 pcg 1.11 " defaults to \"[+]\"\n"
1126     " -shade add shading to font\n"
1127     " -noinitial don't display the last file lines on\n"
1128     " startup\n"
1129     " -i | -interval seconds interval between checks (fractional\n"
1130 pcg 1.21 " values o.k.). Default 2.4 seconds\n"
1131 pcg 1.11 " -V display version information and exit\n"
1132     "\n");
1133     printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green "
1134     "/var/log/secure,red,'ALERT'\n", myname);
1135     exit (0);
1136     }
1137    
1138     void
1139     display_version (void)
1140 pcg 1.1 {
1141 pcg 1.11 printf ("root-tail version " VERSION "\n");
1142     exit (0);
1143 pcg 1.1 }
1144    
1145 pcg 1.11 int
1146     daemonize (void)
1147     {
1148     switch (fork ())
1149     {
1150 pcg 1.1 case -1:
1151 pcg 1.11 return -1;
1152 pcg 1.1 case 0:
1153 pcg 1.11 break;
1154 pcg 1.1 default:
1155 pcg 1.11 _exit (0);
1156 pcg 1.1 }
1157    
1158 pcg 1.11 if (setsid () == -1)
1159     return -1;
1160 pcg 1.1
1161 pcg 1.11 return 0;
1162 pcg 1.1 }