ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.46
Committed: Thu Apr 1 02:40:47 2004 UTC (22 years, 5 months ago) by chris_moore
Content type: text/plain
Branch: MAIN
CVS Tags: last_stable
Changes since 1.45: +1 -1 lines
Log Message:
Fixed the example geometry in the --help text.  It's in pixels now.

File Contents

# User Rev Content
1 pcg 1.1 /*
2     * Copyright 2001 by Marco d'Itri <md@linux.it>
3 pcg 1.29 * Copyright 2000,2001,2002,2003,2004
4     * Marc Lehmann <pcg@goof.com>,
5     * and many others, see README
6     *
7     * Original version by Mike Baker.
8 pcg 1.1 *
9     * This program is free software; you can redistribute it and/or modify
10     * it under the terms of the GNU General Public License as published by
11     * the Free Software Foundation; either version 2 of the License, or
12     * (at your option) any later version.
13     *
14     * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18     *
19     * You should have received a copy of the GNU General Public License
20     * along with this program; if not, write to the Free Software
21     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22     */
23    
24     #include "config.h"
25     #include <stdlib.h>
26     #include <stdio.h>
27     #include <unistd.h>
28     #include <string.h>
29     #include <signal.h>
30     #include <time.h>
31     #include <fcntl.h>
32     #include <errno.h>
33     #include <sys/time.h>
34     #include <sys/stat.h>
35     #include <sys/types.h>
36 pcg 1.7 #include <locale.h>
37 pcg 1.11 #include <ctype.h>
38     #include <stdarg.h>
39 chris_moore 1.26 #include <X11/Xlib.h>
40     #include <X11/Xatom.h>
41     #include <X11/Xutil.h>
42    
43 pcg 1.1 #if HAS_REGEX
44     #include <regex.h>
45     #endif
46    
47 chris_moore 1.27 #define SHADE_X 2
48     #define SHADE_Y 2
49    
50 pcg 1.1 /* data structures */
51 pcg 1.11 struct logfile_entry
52     {
53     struct logfile_entry *next;
54    
55     char *fname; /* name of file */
56     char *desc; /* alternative description */
57     char *buf; /* text read but not yet displayed */
58     FILE *fp; /* FILE struct associated with file */
59     ino_t inode; /* inode of the file opened */
60     off_t last_size; /* file size at the last check */
61     unsigned long color; /* color to be used for printing */
62     int partial; /* true if the last line isn't complete */
63     int lastpartial; /* true if the previous output wasn't complete */
64     int index; /* index into linematrix of a partial line */
65 chris_moore 1.28 int modified; /* true if line is modified & needs displaying */
66 pcg 1.1 };
67    
68 pcg 1.11 struct linematrix
69     {
70     char *line;
71 pcg 1.12 int len;
72 pcg 1.11 unsigned long color;
73 pcg 1.1 };
74    
75 chris_moore 1.28 struct displaymatrix
76     {
77     char *line;
78     int len;
79     int buffer_size;
80 chris_moore 1.40 unsigned long color;
81 chris_moore 1.28 };
82    
83 pcg 1.1 /* global variables */
84 pcg 1.12 struct linematrix *lines;
85 chris_moore 1.28 struct displaymatrix *display;
86 pcg 1.11 int width = STD_WIDTH, height = STD_HEIGHT, listlen;
87 pcg 1.1 int win_x = LOC_X, win_y = LOC_Y;
88 pcg 1.22 int font_ascent, font_height;
89 chris_moore 1.27 int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */
90     int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
91 pcg 1.1 int do_reopen;
92 pcg 1.21 struct timeval interval = { 2, 400000 };
93 pcg 1.7 XFontSet fontset;
94 pcg 1.1
95     /* command line options */
96 pcg 1.5 int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
97 chris_moore 1.24 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
98     geom_mask, reload = 0;
99 pcg 1.1 const char *command = NULL,
100 pcg 1.11 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
101     *continuation = "[+]";
102 pcg 1.1
103     struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
104    
105     Display *disp;
106     Window root;
107     GC WinGC;
108    
109     #if HAS_REGEX
110 pcg 1.11 struct re_list
111     {
112     regex_t from;
113     const char *to;
114     struct re_list *next;
115 pcg 1.1 };
116     struct re_list *re_head, *re_tail;
117 chris_moore 1.26 char *transform_to = NULL;
118     regex_t *transformre;
119 pcg 1.1 #endif
120    
121    
122     /* prototypes */
123 pcg 1.11 void list_files (int);
124     void force_reopen (int);
125     void force_refresh (int);
126     void blank_window (int);
127    
128     void InitWindow (void);
129     unsigned long GetColor (const char *);
130 chris_moore 1.28 void redraw (int);
131     void refresh (int, int, int, int);
132 pcg 1.11
133     void transform_line (char *s);
134     int lineinput (struct logfile_entry *);
135     void reopen (void);
136     void check_open_files (void);
137     FILE *openlog (struct logfile_entry *);
138 pcg 1.12 static void main_loop (void);
139 pcg 1.11
140     void display_version (void);
141     void display_help (char *);
142     void install_signal (int, void (*)(int));
143     void *xstrdup (const char *);
144     void *xmalloc (size_t);
145 chris_moore 1.28 void *xrealloc (void *, size_t);
146 pcg 1.11 int daemonize (void);
147 pcg 1.1
148     /* signal handlers */
149 pcg 1.11 void
150     list_files (int dummy)
151 pcg 1.1 {
152 pcg 1.11 struct logfile_entry *e;
153 pcg 1.1
154 pcg 1.11 fprintf (stderr, "Files opened:\n");
155     for (e = loglist; e; e = e->next)
156     fprintf (stderr, "\t%s (%s)\n", e->fname, e->desc);
157 pcg 1.1 }
158    
159 pcg 1.11 void
160     force_reopen (int dummy)
161 pcg 1.1 {
162 pcg 1.11 do_reopen = 1;
163 pcg 1.1 }
164    
165 pcg 1.11 void
166     force_refresh (int dummy)
167 pcg 1.1 {
168 chris_moore 1.28 redraw (1);
169 pcg 1.1 }
170    
171 pcg 1.11 void
172     blank_window (int dummy)
173 pcg 1.1 {
174 pcg 1.19 XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False);
175 pcg 1.11 XFlush (disp);
176     exit (0);
177 pcg 1.1 }
178    
179     /* X related functions */
180 pcg 1.11 unsigned long
181     GetColor (const char *ColorName)
182 pcg 1.1 {
183 pcg 1.11 XColor Color;
184     XWindowAttributes Attributes;
185 pcg 1.1
186 pcg 1.11 XGetWindowAttributes (disp, root, &Attributes);
187     Color.pixel = 0;
188 pcg 1.8
189 pcg 1.11 if (!XParseColor (disp, Attributes.colormap, ColorName, &Color))
190     fprintf (stderr, "can't parse %s\n", ColorName);
191     else if (!XAllocColor (disp, Attributes.colormap, &Color))
192     fprintf (stderr, "can't allocate %s\n", ColorName);
193 pcg 1.8
194 pcg 1.11 return Color.pixel;
195 pcg 1.1 }
196    
197 pcg 1.11 static Window
198     root_window (Display * display, int screen_number)
199 pcg 1.7 {
200 pcg 1.11 Atom SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False);
201 pcg 1.7 Window real_root_window = RootWindow (display, screen_number);
202    
203 pcg 1.11 if (root) /* root window set via option */
204 pcg 1.8 return root;
205    
206 pcg 1.11 if (SWM_VROOT != None)
207 pcg 1.7 {
208     Window unused, *windows;
209     unsigned int count;
210    
211     if (XQueryTree (display, real_root_window, &unused, &unused, &windows,
212 pcg 1.11 &count))
213     {
214     int i;
215    
216     for (i = 0; i < count; i++)
217     {
218     Atom type;
219     int format;
220     unsigned long nitems, bytes_after_return;
221     unsigned char *virtual_root_window;
222    
223     if (XGetWindowProperty (display, windows[i], SWM_VROOT,
224     0, 1, False, XA_WINDOW, &type, &format,
225     &nitems, &bytes_after_return,
226     &virtual_root_window) == Success)
227     {
228     if (type != None)
229     {
230     if (type == XA_WINDOW)
231     {
232     XFree (windows);
233     return (Window) virtual_root_window;
234     }
235     else
236     fprintf (stderr,
237     "__SWM_VROOT property type mismatch");
238     }
239     }
240     else
241     fprintf (stderr,
242     "failed to get __SWM_VROOT property on window 0x%lx",
243     windows[i]);
244     }
245    
246     if (count)
247     XFree (windows);
248     }
249 pcg 1.7 else
250 pcg 1.11 fprintf (stderr, "Can't query tree on root window 0x%lx",
251     real_root_window);
252 pcg 1.7 }
253     else
254     /* This shouldn't happen. The Xlib documentation is wrong BTW. */
255     fprintf (stderr, "Can't intern atom __SWM_VROOT");
256    
257     return real_root_window;
258     }
259    
260 pcg 1.11 void
261     InitWindow (void)
262 pcg 1.1 {
263 pcg 1.11 XGCValues gcv;
264     unsigned long gcm;
265     int screen, ScreenWidth, ScreenHeight;
266    
267     if (!(disp = XOpenDisplay (dispname)))
268     {
269     fprintf (stderr, "Can't open display %s.\n", dispname);
270     exit (1);
271     }
272    
273     screen = DefaultScreen (disp);
274     ScreenHeight = DisplayHeight (disp, screen);
275     ScreenWidth = DisplayWidth (disp, screen);
276    
277     root = root_window (disp, screen);
278    
279     gcm = GCBackground;
280     gcv.graphics_exposures = True;
281     WinGC = XCreateGC (disp, root, gcm, &gcv);
282     XMapWindow (disp, root);
283     XSetForeground (disp, WinGC, GetColor (DEF_COLOR));
284    
285     {
286     char **missing_charset_list;
287     int missing_charset_count;
288     char *def_string;
289    
290     fontset = XCreateFontSet (disp, fontname,
291     &missing_charset_list, &missing_charset_count,
292     &def_string);
293    
294     if (missing_charset_count)
295     {
296     fprintf (stderr,
297     "Missing charsets in String to FontSet conversion (%s)\n",
298     missing_charset_list[0]);
299     XFreeStringList (missing_charset_list);
300     }
301     }
302    
303     if (!fontset)
304     {
305     fprintf (stderr, "unable to create fontset, exiting.\n");
306     exit (1);
307     }
308    
309     {
310     XFontSetExtents *xfe = XExtentsOfFontSet (fontset);
311    
312     font_height = xfe->max_logical_extent.height;
313 pcg 1.22 font_ascent = -xfe->max_logical_extent.y;
314 pcg 1.11 }
315    
316     if (geom_mask & XNegative)
317     win_x = win_x + ScreenWidth - width;
318     if (geom_mask & YNegative)
319     win_y = win_y + ScreenHeight - height;
320    
321 chris_moore 1.27 if (opt_outline)
322     {
323     /* adding outline increases the total width and height by 2
324     pixels each, and offsets the text one pixel right and one
325     pixel down */
326     effect_x_space = effect_y_space = 2;
327     effect_x_offset = effect_y_offset = 1;
328     }
329     else if (opt_shade)
330     {
331     /* adding a shadow increases the space used */
332     effect_x_space = abs(SHADE_X);
333     effect_y_space = abs(SHADE_Y);
334     /* if the shadow is to the right and below then we don't need
335     * to move the text to make space for it, but shadows to the left
336     * and above need accomodating */
337     effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
338     effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
339     }
340     else
341     {
342     effect_x_space = effect_y_space = 0;
343     effect_x_offset = effect_y_offset = 0;
344     }
345    
346     /* if we are using -shade or -outline, there will be less usable
347     * space for output */
348     listlen = (height - effect_y_space) / font_height;
349 pcg 1.1
350 pcg 1.11 if (!listlen)
351     {
352     fprintf (stderr, "height too small for a single line, setting to %d\n",
353     font_height);
354     listlen = 1;
355     }
356    
357 chris_moore 1.27 /* leave the height how the user requested it. it might not all be
358     * used, but this will allow the geometry to be tuned more accurately
359     * (with the -frame option)
360     * the old code did this:
361     * height = listlen * font_height + effect_y_space; */
362    
363 pcg 1.11 XSelectInput (disp, root, ExposureMask | FocusChangeMask);
364 pcg 1.1 }
365    
366     /*
367 chris_moore 1.28 * if redraw() is passwd a non-zero argument, it does a complete
368     * redraw, rather than an update. if the argument is zero (and
369     * -noflicker is in effect) then only the lines which have changed
370     * since the last draw are redrawn.
371     *
372 pcg 1.1 * the rest is handled by regular refresh()'es
373     */
374 pcg 1.11 void
375 chris_moore 1.28 redraw (int redraw_all)
376 pcg 1.1 {
377 pcg 1.20 XSetClipMask (disp, WinGC, None);
378 chris_moore 1.28 refresh (0, 32768, 1, redraw_all);
379 pcg 1.1 }
380    
381     /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
382 pcg 1.11 void
383 chris_moore 1.28 refresh (int miny, int maxy, int clear, int refresh_all)
384 pcg 1.1 {
385 pcg 1.11 int lin;
386 pcg 1.29 int offset = listlen * font_height + font_ascent + effect_y_offset;
387 pcg 1.11 unsigned long black_color = GetColor ("black");
388 pcg 1.1
389 pcg 1.11 miny -= win_y + font_height;
390     maxy -= win_y - font_height;
391 pcg 1.1
392 pcg 1.22 if (clear && !opt_noflicker)
393     XClearArea (disp, root, win_x, win_y, width, height, False);
394    
395 pcg 1.11 for (lin = listlen; lin--;)
396     {
397 pcg 1.12 struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin);
398 chris_moore 1.28 struct displaymatrix *display_line = display + lin;
399 pcg 1.7
400 pcg 1.11 offset -= font_height;
401 pcg 1.7
402 pcg 1.11 if (offset < miny || offset > maxy)
403     continue;
404 jebusbfb 1.4
405 pcg 1.29 /* if this line is a different than it was, then it
406     * needs displaying */
407 chris_moore 1.30 if (!opt_noflicker
408     || refresh_all
409 pcg 1.29 || display_line->len != line->len
410 chris_moore 1.40 || display_line->color != line->color
411 pcg 1.29 || memcmp (display_line->line, line->line, line->len))
412     {
413 chris_moore 1.30 /* don't bother updating the record of what has been
414     * displayed if -noflicker isn't in effect, since we redraw
415     * the whole display every time anyway */
416     if (opt_noflicker)
417     {
418     /* update the record of what has been displayed;
419     * first make sure the buffer is big enough */
420 chris_moore 1.44 if (display_line->buffer_size < line->len)
421 chris_moore 1.30 {
422 pcg 1.39 display_line->buffer_size = line->len;
423 chris_moore 1.30 display_line->line = xrealloc (display_line->line, display_line->buffer_size);
424     }
425 pcg 1.29
426 chris_moore 1.30 display_line->len = line->len;
427 chris_moore 1.40 display_line->color = line->color;
428 chris_moore 1.30 memcpy (display_line->line, line->line, line->len);
429 chris_moore 1.28
430 chris_moore 1.30 if (clear)
431     XClearArea (disp, root, win_x, win_y + offset - font_ascent,
432     width + effect_x_space, font_height + effect_y_space, False);
433     }
434 pcg 1.22
435 pcg 1.29 if (opt_outline)
436     {
437     int x, y;
438     XSetForeground (disp, WinGC, black_color);
439 chris_moore 1.28
440 pcg 1.39 for (x = -1; x <= 1; x += 2)
441     for (y = -1; y <= 1; y += 2)
442 pcg 1.29 XmbDrawString (disp, root, fontset, WinGC,
443     win_x + effect_x_offset + x,
444     win_y + y + offset,
445     line->line, line->len);
446     }
447     else if (opt_shade)
448     {
449     XSetForeground (disp, WinGC, black_color);
450     XmbDrawString (disp, root, fontset, WinGC,
451     win_x + effect_x_offset + SHADE_X,
452     win_y + offset + SHADE_Y,
453     line->line, line->len);
454     }
455 chris_moore 1.27
456 pcg 1.29 XSetForeground (disp, WinGC, line->color);
457     XmbDrawString (disp, root, fontset, WinGC,
458     win_x + effect_x_offset,
459     win_y + offset,
460     line->line, line->len);
461 chris_moore 1.28 }
462 pcg 1.1 }
463    
464 pcg 1.11 if (opt_frame)
465 pcg 1.15 {
466     XSetForeground (disp, WinGC, GetColor (def_color));
467 chris_moore 1.27 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
468 pcg 1.15 }
469 pcg 1.1 }
470    
471     #if HAS_REGEX
472 pcg 1.11 void
473     transform_line (char *s)
474 pcg 1.1 {
475     #ifdef I_AM_Md
476 pcg 1.11 int i;
477     if (1)
478     {
479     for (i = 16; s[i]; i++)
480     s[i] = s[i + 11];
481 pcg 1.1 }
482 pcg 1.11 s[i + 1] = '\0';
483 pcg 1.1 #endif
484    
485 pcg 1.11 if (transformre)
486     {
487     int i;
488     regmatch_t matched[16];
489    
490 chris_moore 1.26 i = regexec (transformre, s, 16, matched, 0);
491 pcg 1.11 if (i == 0)
492     { /* matched */
493 chris_moore 1.26 int match_start = matched[0].rm_so;
494     int match_end = matched[0].rm_eo;
495     int old_len = match_end - match_start;
496 pcg 1.39 int new_len = strlen (transform_to);
497     int old_whole_len = strlen (s);
498    
499     printf ("regexp was matched by '%s' - replace with '%s'\n", s, transform_to);
500     printf ("match is from %d to %d\n", match_start, match_end);
501     if (new_len > old_len)
502 chris_moore 1.28 s = xrealloc(s, old_whole_len + new_len - old_len);
503 pcg 1.39
504     if (new_len != old_len)
505     {
506     memcpy(s + match_end + new_len - old_len,
507     s + match_end,
508     old_whole_len - match_end);
509     s[old_whole_len + new_len - old_len] = '\0';
510     }
511    
512     memcpy (s + match_start,
513     transform_to,
514     new_len);
515     printf ("transformed to '%s'\n", s);
516 pcg 1.11 }
517 chris_moore 1.26 else
518     {
519 pcg 1.39 printf ("regexp was not matched by '%s'\n", s);
520 chris_moore 1.26 }
521 pcg 1.1 }
522     }
523     #endif
524    
525 pcg 1.12 char *
526     concat_line (const char *p1, const char *p2)
527     {
528     int l1 = p1 ? strlen (p1) : 0;
529     int l2 = strlen (p2);
530     char *r = xmalloc (l1 + l2 + 1);
531    
532     memcpy (r, p1, l1);
533     memcpy (r + l1, p2, l2);
534     r[l1 + l2] = 0;
535    
536     return r;
537     }
538 pcg 1.1
539     /*
540 chris_moore 1.41 * This routine can read a line of any length if it is called enough times.
541 pcg 1.1 */
542 pcg 1.11 int
543     lineinput (struct logfile_entry *logfile)
544 pcg 1.1 {
545 chris_moore 1.45 char buff[1024], *p;
546 pcg 1.12 int ch;
547 chris_moore 1.32 /* HACK-2: add on the length of any partial line which we will be appending to */
548 pcg 1.12 int ofs = logfile->buf ? strlen (logfile->buf) : 0;
549 pcg 1.1
550 chris_moore 1.45 /* this loop ensures that the whole line is read, even if it's
551     * longer than the buffer. we need to do this because when --whole
552     * is in effect we don't know whether to display the line or not
553     * until we've seen how (ie. whether) it ends */
554 pcg 1.11 do
555     {
556 chris_moore 1.45 p = buff;
557     do
558     {
559     ch = fgetc (logfile->fp);
560 pcg 1.1
561 chris_moore 1.45 if (ch == '\n' || ch == EOF)
562     break;
563     else if (ch == '\r')
564     continue; /* skip */
565     else if (ch == '\t')
566     {
567     do
568     {
569     *p++ = ' ';
570     ofs++;
571     }
572     while (ofs & 7);
573     }
574     else
575     {
576     *p++ = ch;
577     ofs++;
578     }
579     }
580     while (p < buff + (sizeof buff) - 8 - 1);
581 pcg 1.1
582 chris_moore 1.45 if (p == buff && ch == EOF)
583     return 0;
584 pcg 1.12
585 chris_moore 1.45 *p = 0;
586 pcg 1.12
587 chris_moore 1.45 p = concat_line (logfile->buf, buff);
588     free (logfile->buf); logfile->buf = p;
589     }
590     while (ch != '\n' && ch != EOF);
591 chris_moore 1.9
592 pcg 1.12 logfile->lastpartial = logfile->partial;
593 chris_moore 1.25 /* there are 3 ways we could have exited the loop: reading '\n',
594     * reaching EOF, or filling the buffer; the 2nd and 3rd of these
595     * both result in a partial line */
596     logfile->partial = ch != '\n';
597 pcg 1.12
598     if (logfile->partial && opt_whole)
599 pcg 1.11 return 0;
600 pcg 1.1
601     #if HAS_REGEX
602 pcg 1.12 transform_line (logfile->buf);
603 pcg 1.1 #endif
604 pcg 1.12 return 1;
605 pcg 1.1 }
606    
607     /* input: reads file->fname
608     * output: fills file->fp, file->inode
609     * returns file->fp
610     * in case of error, file->fp is NULL
611     */
612 pcg 1.11 FILE *
613     openlog (struct logfile_entry * file)
614     {
615     struct stat stats;
616    
617     if ((file->fp = fopen (file->fname, "r")) == NULL)
618     {
619     file->fp = NULL;
620     return NULL;
621     }
622    
623     fstat (fileno (file->fp), &stats);
624     if (S_ISFIFO (stats.st_mode))
625     {
626     if (fcntl (fileno (file->fp), F_SETFL, O_NONBLOCK) < 0)
627     perror ("fcntl"), exit (1);
628     file->inode = 0;
629     }
630     else
631     file->inode = stats.st_ino;
632    
633     if (opt_noinitial)
634     fseek (file->fp, 0, SEEK_END);
635     else if (stats.st_size > (listlen + 1) * width)
636     fseek (file->fp, -((listlen + 2) * width), SEEK_END);
637    
638     file->last_size = stats.st_size;
639     return file->fp;
640     }
641    
642     void
643     reopen (void)
644 pcg 1.1 {
645 pcg 1.11 struct logfile_entry *e;
646    
647     for (e = loglist; e; e = e->next)
648     {
649     if (!e->inode)
650     continue; /* skip stdin */
651 pcg 1.1
652 pcg 1.11 if (e->fp)
653     fclose (e->fp);
654     /* if fp is NULL we will try again later */
655     openlog (e);
656     }
657    
658     do_reopen = 0;
659     }
660 pcg 1.1
661 pcg 1.11 void
662     check_open_files (void)
663     {
664     struct logfile_entry *e;
665     struct stat stats;
666 pcg 1.1
667 pcg 1.11 for (e = loglist; e; e = e->next)
668     {
669     if (!e->inode)
670     continue; /* skip stdin */
671 pcg 1.1
672 pcg 1.11 if (stat (e->fname, &stats) < 0)
673     { /* file missing? */
674     sleep (1);
675     if (e->fp)
676     fclose (e->fp);
677 pcg 1.37 if (openlog (e) == NULL)
678     continue;
679     if (fstat (fileno (e->fp), &stats) < 0)
680     continue;
681 pcg 1.11 }
682    
683     if (stats.st_ino != e->inode)
684     { /* file renamed? */
685     if (e->fp)
686     fclose (e->fp);
687     if (openlog (e) == NULL)
688 chris_moore 1.23 continue;
689 pcg 1.39 if (fstat (fileno (e->fp), &stats) < 0)
690     continue;
691 pcg 1.11 }
692    
693     if (stats.st_size < e->last_size)
694     { /* file truncated? */
695     fseek (e->fp, 0, SEEK_SET);
696     e->last_size = stats.st_size;
697     }
698 pcg 1.1 }
699     }
700    
701 pcg 1.14 /*
702     * insert a single physical line (that must be short enough to fit)
703     * at position "idx" by pushing up lines above it. the caller
704     * MUST then fill in lines[idx] with valid data.
705     */
706 pcg 1.12 static void
707     insert_line (int idx)
708     {
709     int cur_line;
710     struct logfile_entry *current;
711    
712     free (lines[0].line);
713    
714     for (cur_line = 0; cur_line < idx; cur_line++)
715     lines[cur_line] = lines[cur_line + 1];
716    
717     for (current = loglist; current; current = current->next)
718 pcg 1.16 if (current->index <= idx)
719 pcg 1.12 current->index--;
720 pcg 1.11 }
721    
722 pcg 1.14 /*
723     * remove a single physical line at position "idx" by moving the lines above it
724     * down and inserting a "~" line at the top.
725     */
726 pcg 1.12 static void
727     delete_line (int idx)
728     {
729     int cur_line;
730     struct logfile_entry *current;
731    
732     for (cur_line = idx; cur_line > 0; cur_line--)
733     lines[cur_line] = lines[cur_line - 1];
734    
735 chris_moore 1.28 lines[0].line = xstrdup ("~");
736 pcg 1.12
737     for (current = loglist; current; current = current->next)
738 pcg 1.16 if (current->index >= 0 && current->index <= idx)
739 pcg 1.12 current->index++;
740     }
741    
742 pcg 1.14 /*
743 chris_moore 1.24 * takes a logical log file line and splits it into multiple physical
744 pcg 1.14 * screen lines by splitting it whenever a part becomes too long.
745     * lal lines will be inserted at position "idx".
746     */
747 pcg 1.12 static void
748     split_line (int idx, const char *str, unsigned long color)
749     {
750     int l = strlen (str);
751 chris_moore 1.33 int last_wrapped = 0;
752 pcg 1.12 const char *p = str;
753 chris_moore 1.33 static int continuation_width = -1;
754     static int continuation_length;
755    
756     /* only calculate the continuation's width once */
757     if (continuation_width == -1)
758     {
759 pcg 1.39 continuation_length = strlen (continuation);
760 chris_moore 1.33 continuation_width = XmbTextEscapement (fontset, continuation, continuation_length);
761     }
762 pcg 1.12
763     do
764     {
765     const char *beg = p;
766 chris_moore 1.33 int w = last_wrapped ? continuation_width : 0;
767     int wrapped = 0;
768 chris_moore 1.24 const char *break_p = NULL;
769 pcg 1.12
770     while (*p)
771     {
772 chris_moore 1.24 /* find the length in bytes of the next multibyte character */
773 pcg 1.12 int len = mblen (p, l);
774     if (len <= 0)
775 chris_moore 1.24 len = 1; /* ignore (don't skip) illegal character sequences */
776 pcg 1.12
777 chris_moore 1.24 /* find the width in pixels of the next character */
778 pcg 1.12 int cw = XmbTextEscapement (fontset, p, len);
779 chris_moore 1.27 if (cw + w > width - effect_x_space)
780 chris_moore 1.24 {
781 chris_moore 1.34 if (p == beg)
782     {
783 pcg 1.39 fprintf (stderr, "we can't even fit a single character onto the line\n");
784     if (len == 1) fprintf (stderr, "(the character we couldn't fit was '%c')\n", *p);
785     exit (1);
786 chris_moore 1.34 }
787    
788 chris_moore 1.24 wrapped = 1;
789     break;
790     }
791    
792     if (opt_wordwrap && len == 1 && p[0] == ' ')
793     break_p = p;
794 pcg 1.12
795     w += cw;
796     p += len;
797     l -= len;
798     }
799    
800 chris_moore 1.24 /* if we're wrapping at spaces, and the line is long enough to
801     * wrap, and we've seen a space already, and the space wasn't
802     * the first character on the line, then wrap at the space */
803     if (opt_wordwrap && wrapped && break_p && break_p != beg)
804     {
805     l += p - break_p;
806     p = break_p;
807     }
808    
809 pcg 1.12 {
810 chris_moore 1.33 int len = p - beg + (last_wrapped ? continuation_length : 0);
811     char *s = xmalloc (len + 1);
812     if (last_wrapped)
813     {
814     memcpy (s, continuation, continuation_length);
815     memcpy (s + continuation_length, beg, p - beg);
816     }
817     else
818 pcg 1.39 memcpy (s, beg, len);
819 chris_moore 1.33
820     s[len] = 0;
821 pcg 1.12 insert_line (idx);
822     lines[idx].line = s;
823 chris_moore 1.33 lines[idx].len = len;
824 pcg 1.12 lines[idx].color = color;
825     }
826 chris_moore 1.24
827     /* if we wrapped at a space, don't display the space */
828     if (opt_wordwrap && wrapped && break_p && break_p != beg)
829     {
830     l--;
831     p++;
832     }
833 chris_moore 1.33
834     last_wrapped = wrapped;
835 pcg 1.12 }
836     while (l);
837     }
838    
839 pcg 1.14 /*
840     * append something to an existing physical line. this is done
841     * by deleting the file on-screen, concatenating the new data to it
842     * and splitting it again.
843     */
844 pcg 1.12 static void
845     append_line (int idx, const char *str)
846     {
847     unsigned long color = lines[idx].color;
848     char *old = lines[idx].line;
849     char *new = concat_line (old, str);
850    
851     free (old);
852    
853     delete_line (idx);
854     split_line (idx, new, color);
855     }
856    
857     static void
858 pcg 1.11 main_loop (void)
859     {
860 pcg 1.12 lines = xmalloc (sizeof (struct linematrix) * listlen);
861 chris_moore 1.28 display = xmalloc (sizeof (struct displaymatrix) * listlen);
862 pcg 1.20 int lin;
863 pcg 1.11 time_t lastreload;
864     Region region = XCreateRegion ();
865     XEvent xev;
866     struct logfile_entry *lastprinted = NULL;
867     struct logfile_entry *current;
868 pcg 1.18 int need_update = 1;
869 pcg 1.11
870     lastreload = time (NULL);
871    
872     /* Initialize linematrix */
873     for (lin = 0; lin < listlen; lin++)
874     {
875 chris_moore 1.28 lines[lin].line = xstrdup ("~");
876 pcg 1.12 lines[lin].len = 1;
877 chris_moore 1.28 display[lin].line = xstrdup("");
878     display[lin].len = 0;
879 pcg 1.39 display[lin].buffer_size = 0;
880 pcg 1.11 lines[lin].color = GetColor (def_color);
881     }
882    
883     for (;;)
884     {
885     /* read logs */
886     for (current = loglist; current; current = current->next)
887     {
888     if (!current->fp)
889     continue; /* skip missing files */
890    
891     clearerr (current->fp);
892    
893 pcg 1.12 while (lineinput (current))
894 pcg 1.11 {
895 pcg 1.12 need_update = 1;
896 pcg 1.11 /* if we're trying to update old partial lines in
897     * place, and the last time this file was updated the
898     * output was partial, and that partial line is not
899     * too close to the top of the screen, then update
900     * that partial line */
901 pcg 1.16 if (opt_update && current->lastpartial && current->index >= 0)
902 pcg 1.11 {
903 pcg 1.16 int idx = current->index;
904     append_line (idx, current->buf);
905     current->index = idx;
906 pcg 1.13 free (current->buf), current->buf = 0;
907 pcg 1.12 continue;
908 pcg 1.11 }
909    
910     /* print filename if any, and if last line was from
911     * different file */
912 pcg 1.13 if (!opt_nofilename && lastprinted != current && current->desc[0])
913 pcg 1.11 {
914 chris_moore 1.42 split_line (listlen - 1, "[", current->color);
915     append_line (listlen - 1, current->desc);
916     append_line (listlen - 1, "]");
917 pcg 1.11 }
918    
919 chris_moore 1.31 /* if we're dealing with partial lines, and the last
920     * time we showed the line it wasn't finished ... */
921     if (!opt_whole && current->lastpartial)
922 chris_moore 1.23 {
923 chris_moore 1.31 /* if this is the same file we showed last then
924     append to the last line shown */
925     if (lastprinted == current)
926     append_line (listlen - 1, current->buf);
927     else
928     {
929     /* but if a different file has been shown in the
930     * mean time, make a new line, starting with the
931     * continuation string */
932     split_line (listlen - 1, continuation, current->color);
933     append_line (listlen - 1, current->buf);
934     }
935 chris_moore 1.23 }
936 pcg 1.11 else
937 chris_moore 1.31 /* otherwise just make a plain and simple new line */
938 chris_moore 1.23 split_line (listlen - 1, current->buf, current->color);
939 pcg 1.11
940 chris_moore 1.23 free (current->buf), current->buf = 0;
941 pcg 1.11 current->index = listlen - 1;
942     lastprinted = current;
943     }
944     }
945    
946     if (need_update)
947 pcg 1.18 {
948 chris_moore 1.28 redraw (0);
949 pcg 1.18 need_update = 0;
950     }
951 pcg 1.11 else
952     {
953     XFlush (disp);
954    
955     if (!XPending (disp))
956     {
957     fd_set fdr;
958     struct timeval to = interval;
959    
960     FD_ZERO (&fdr);
961     FD_SET (ConnectionNumber (disp), &fdr);
962     select (ConnectionNumber (disp) + 1, &fdr, 0, 0, &to);
963     }
964     }
965    
966     check_open_files ();
967    
968     if (do_reopen)
969     reopen ();
970    
971     /* we ignore possible errors due to window resizing &c */
972     while (XPending (disp))
973     {
974     XNextEvent (disp, &xev);
975    
976     switch (xev.type)
977     {
978     case Expose:
979     {
980     XRectangle r;
981    
982     r.x = xev.xexpose.x;
983     r.y = xev.xexpose.y;
984     r.width = xev.xexpose.width;
985     r.height = xev.xexpose.height;
986 pcg 1.20
987 pcg 1.11 XUnionRectWithRegion (&r, region, region);
988     }
989     break;
990     default:
991     #ifdef DEBUGMODE
992     fprintf (stderr, "PANIC! Unknown event %d\n", xev.type);
993     #endif
994     break;
995     }
996     }
997    
998     /* reload if requested */
999     if (reload && lastreload + reload < time (NULL))
1000     {
1001     if (command && command[0])
1002     system (command);
1003    
1004     reopen ();
1005     lastreload = time (NULL);
1006     }
1007    
1008     if (!XEmptyRegion (region))
1009     {
1010 pcg 1.20 XRectangle r;
1011    
1012 pcg 1.11 XSetRegion (disp, WinGC, region);
1013 pcg 1.20 XClipBox (region, &r);
1014    
1015 chris_moore 1.28 refresh (r.y, r.y + r.height, 0, 1);
1016 pcg 1.11
1017     XDestroyRegion (region);
1018     region = XCreateRegion ();
1019     }
1020     }
1021     }
1022 pcg 1.1
1023 pcg 1.11
1024     int
1025     main (int argc, char *argv[])
1026 pcg 1.1 {
1027 pcg 1.11 int i;
1028     int opt_daemonize = 0;
1029     int opt_partial = 0, file_count = 0;
1030 pcg 1.1 #if HAS_REGEX
1031 pcg 1.11 char *transform = NULL;
1032 pcg 1.1 #endif
1033    
1034 pcg 1.11 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
1035    
1036     /* window needs to be initialized before colorlookups can be done */
1037     /* just a dummy to get the color lookups right */
1038     geom_mask = NoValue;
1039     InitWindow ();
1040    
1041     for (i = 1; i < argc; i++)
1042     {
1043     const char *arg = argv[i];
1044 pcg 1.7
1045 pcg 1.11 if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',')
1046     {
1047     if (arg[1] == '-')
1048     arg++;
1049    
1050     if (!strcmp (arg, "-?") ||
1051     !strcmp (arg, "-help") || !strcmp (arg, "-h"))
1052     display_help (argv[0]);
1053     else if (!strcmp (arg, "-V"))
1054     display_version ();
1055     else if (!strcmp (arg, "-g") || !strcmp (arg, "-geometry"))
1056     geom_mask =
1057     XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
1058     else if (!strcmp (arg, "-display"))
1059     dispname = argv[++i];
1060     else if (!strcmp (arg, "-cont"))
1061     continuation = argv[++i];
1062     else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
1063     fontname = argv[++i];
1064 pcg 1.1 #if HAS_REGEX
1065 pcg 1.11 else if (!strcmp (arg, "-t"))
1066 chris_moore 1.26 {
1067     transform = argv[++i];
1068     transform_to = argv[++i];
1069     printf("transform: '%s' to '%s'\n", transform, transform_to);
1070     }
1071 pcg 1.1 #endif
1072 pcg 1.11 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
1073     opt_daemonize = 1;
1074     else if (!strcmp (arg, "-reload"))
1075     {
1076     reload = atoi (argv[++i]);
1077     command = argv[++i];
1078     }
1079     else if (!strcmp (arg, "-shade"))
1080     opt_shade = 1;
1081 pcg 1.21 else if (!strcmp (arg, "-outline"))
1082     opt_outline = 1;
1083 pcg 1.22 else if (!strcmp (arg, "-noflicker"))
1084     opt_noflicker = 1;
1085 pcg 1.11 else if (!strcmp (arg, "-frame"))
1086     opt_frame = 1;
1087     else if (!strcmp (arg, "-no-filename"))
1088     opt_nofilename = 1;
1089     else if (!strcmp (arg, "-reverse"))
1090     opt_reverse = 1;
1091     else if (!strcmp (arg, "-whole"))
1092     opt_whole = 1;
1093     else if (!strcmp (arg, "-partial"))
1094     opt_partial = 1;
1095     else if (!strcmp (arg, "-update"))
1096     opt_update = opt_partial = 1;
1097 chris_moore 1.24 else if (!strcmp (arg, "-wordwrap"))
1098     opt_wordwrap = 1;
1099 pcg 1.11 else if (!strcmp (arg, "-color"))
1100     def_color = argv[++i];
1101     else if (!strcmp (arg, "-noinitial"))
1102     opt_noinitial = 1;
1103     else if (!strcmp (arg, "-id"))
1104     root = atoi (argv[++i]);
1105     else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i"))
1106     {
1107     double iv = atof (argv[++i]);
1108    
1109     interval.tv_sec = (int) iv;
1110     interval.tv_usec = (iv - interval.tv_sec) * 1e6;
1111     }
1112     else
1113     {
1114     fprintf (stderr, "Unknown option '%s'.\n"
1115     "Try --help for more information.\n", arg);
1116     exit (1);
1117     }
1118     }
1119     else
1120     { /* it must be a filename */
1121     struct logfile_entry *e;
1122     const char *fname, *desc, *fcolor = def_color;
1123     char *p;
1124    
1125     file_count++;
1126    
1127     /* this is not foolproof yet (',' in filenames are not allowed) */
1128     fname = desc = arg;
1129     if ((p = strchr (arg, ',')))
1130     {
1131     *p = '\0';
1132     fcolor = p + 1;
1133    
1134     if ((p = strchr (fcolor, ',')))
1135     {
1136     *p = '\0';
1137     desc = p + 1;
1138     }
1139     }
1140    
1141     e = xmalloc (sizeof (struct logfile_entry));
1142 pcg 1.12 e->partial = 0;
1143     e->buf = 0;
1144 pcg 1.16 e->index = -1;
1145 pcg 1.12
1146 pcg 1.11 if (arg[0] == '-' && arg[1] == '\0')
1147     {
1148     if ((e->fp = fdopen (0, "r")) == NULL)
1149     perror ("fdopen"), exit (1);
1150     if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
1151     perror ("fcntl"), exit (1);
1152 pcg 1.39
1153 pcg 1.11 e->fname = NULL;
1154     e->inode = 0;
1155     e->desc = xstrdup ("stdin");
1156     }
1157     else
1158     {
1159 pcg 1.39 e->fname = xstrdup (fname);
1160 pcg 1.11
1161     if (openlog (e) == NULL)
1162     perror (fname), exit (1);
1163    
1164 pcg 1.38 e->desc = xstrdup (desc);
1165 pcg 1.11 }
1166    
1167     e->color = GetColor (fcolor);
1168     e->partial = 0;
1169     e->next = NULL;
1170    
1171     if (!loglist)
1172     loglist = e;
1173     if (loglist_tail)
1174     loglist_tail->next = e;
1175 pcg 1.39
1176 pcg 1.11 loglist_tail = e;
1177     }
1178     }
1179    
1180     if (!loglist)
1181     {
1182     fprintf (stderr, "You did not specify any files to tail\n"
1183     "use %s --help for help\n", argv[0]);
1184     exit (1);
1185     }
1186    
1187 chris_moore 1.43 if (opt_update && opt_whole)
1188     {
1189     fprintf (stderr, "Specify at most one of -update and -whole\n");
1190     exit (1);
1191     }
1192     else if (opt_partial && opt_whole)
1193 pcg 1.11 {
1194     fprintf (stderr, "Specify at most one of -partial and -whole\n");
1195     exit (1);
1196 chris_moore 1.9 }
1197    
1198 chris_moore 1.32 /* HACK-7: do we want to allow both -shade and -outline? */
1199 chris_moore 1.27 if (opt_shade && opt_outline)
1200     {
1201     fprintf (stderr, "Specify at most one of -shade and -outline\n");
1202     exit (1);
1203     }
1204    
1205 pcg 1.11 if (opt_partial)
1206 chris_moore 1.9 /* if we specifically requested to see partial lines then don't insist on whole lines */
1207 pcg 1.11 opt_whole = 0;
1208     else if (file_count > 1)
1209 chris_moore 1.31 /* otherwise, if we're viewing multiple files, default to showing whole lines */
1210 pcg 1.11 opt_whole = 1;
1211 chris_moore 1.9
1212 pcg 1.1 #if HAS_REGEX
1213 pcg 1.11 if (transform)
1214     {
1215     int i;
1216 pcg 1.1
1217 chris_moore 1.26 printf("compiling regexp '%s'\n", transform);
1218     transformre = xmalloc (sizeof (regex_t));
1219     i = regcomp (transformre, transform, REG_EXTENDED);
1220 pcg 1.11 if (i != 0)
1221     {
1222     char buf[512];
1223    
1224 chris_moore 1.26 regerror (i, transformre, buf, sizeof (buf));
1225 pcg 1.11 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1226     }
1227 chris_moore 1.26 else
1228     {
1229     printf("compiled '%s' OK to %x\n", transform, (int)transformre);
1230     }
1231 pcg 1.1 }
1232     #endif
1233    
1234 pcg 1.11 InitWindow ();
1235 pcg 1.1
1236 pcg 1.11 install_signal (SIGINT, blank_window);
1237     install_signal (SIGQUIT, blank_window);
1238     install_signal (SIGTERM, blank_window);
1239     install_signal (SIGHUP, force_reopen);
1240     install_signal (SIGUSR1, list_files);
1241     install_signal (SIGUSR2, force_refresh);
1242 pcg 1.1
1243 pcg 1.11 if (opt_daemonize)
1244     daemonize ();
1245 pcg 1.1
1246 pcg 1.11 main_loop ();
1247 pcg 1.1
1248 pcg 1.11 exit (1); /* to make gcc -Wall stop complaining */
1249 pcg 1.1 }
1250    
1251 pcg 1.11 void
1252     install_signal (int sig, void (*handler) (int))
1253 pcg 1.1 {
1254 pcg 1.11 struct sigaction action;
1255    
1256     action.sa_handler = handler;
1257     sigemptyset (&action.sa_mask);
1258     action.sa_flags = SA_RESTART;
1259 pcg 1.1
1260 pcg 1.11 if (sigaction (sig, &action, NULL) < 0)
1261     fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1);
1262 pcg 1.1 }
1263    
1264 pcg 1.11 void *
1265     xstrdup (const char *string)
1266 pcg 1.1 {
1267 pcg 1.11 void *p;
1268 pcg 1.1
1269 pcg 1.11 while ((p = strdup (string)) == NULL)
1270     {
1271 chris_moore 1.44 fprintf (stderr, "Memory exhausted in xstrdup().\n");
1272 pcg 1.11 sleep (10);
1273 pcg 1.1 }
1274 pcg 1.11
1275     return p;
1276 pcg 1.1 }
1277    
1278 pcg 1.11 void *
1279     xmalloc (size_t size)
1280 pcg 1.1 {
1281 pcg 1.11 void *p;
1282 pcg 1.1
1283 pcg 1.11 while ((p = malloc (size)) == NULL)
1284     {
1285 chris_moore 1.44 fprintf (stderr, "Memory exhausted in xmalloc().\n");
1286 pcg 1.11 sleep (10);
1287 pcg 1.1 }
1288 pcg 1.12
1289 pcg 1.11 return p;
1290 pcg 1.1 }
1291    
1292 chris_moore 1.28 void *
1293     xrealloc (void *ptr, size_t size)
1294     {
1295     void *p;
1296    
1297     while ((p = realloc (ptr, size)) == NULL)
1298     {
1299 chris_moore 1.44 fprintf (stderr, "Memory exhausted in xrealloc().\n");
1300 chris_moore 1.28 sleep (10);
1301     }
1302    
1303     return p;
1304     }
1305    
1306 pcg 1.11 void
1307     display_help (char *myname)
1308     {
1309     printf ("Usage: %s [options] file1[,color[,desc]] "
1310     "[file2[,color[,desc]] ...]\n", myname);
1311     printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1312     " -color color use color $color as default\n"
1313     " -reload sec command reload after $sec and run command\n"
1314     " -id id window id to use instead of the root window\n"
1315     " -font FONTSPEC (-fn) font to use\n"
1316     " -f | -fork fork into background\n"
1317     " -reverse print new lines at the top\n"
1318     " -whole wait for \\n before showing a line\n"
1319     " -partial show lines even if they don't end with a \\n\n"
1320     " -update allow updates to old partial lines\n"
1321     " -cont string to prefix continued partial lines with\n"
1322 chris_moore 1.24 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1323 pcg 1.11 " defaults to \"[+]\"\n"
1324     " -shade add shading to font\n"
1325     " -noinitial don't display the last file lines on\n"
1326     " startup\n"
1327     " -i | -interval seconds interval between checks (fractional\n"
1328 pcg 1.21 " values o.k.). Default 2.4 seconds\n"
1329 pcg 1.11 " -V display version information and exit\n"
1330     "\n");
1331 chris_moore 1.46 printf ("Example:\n%s -g 800x250+100+50 -font fixed /var/log/messages,green "
1332 pcg 1.11 "/var/log/secure,red,'ALERT'\n", myname);
1333     exit (0);
1334     }
1335    
1336     void
1337     display_version (void)
1338 pcg 1.1 {
1339 pcg 1.11 printf ("root-tail version " VERSION "\n");
1340     exit (0);
1341 pcg 1.1 }
1342    
1343 pcg 1.11 int
1344     daemonize (void)
1345     {
1346 chris_moore 1.26 pid_t pid;
1347    
1348     switch (pid = fork ())
1349 pcg 1.11 {
1350 pcg 1.1 case -1:
1351 pcg 1.11 return -1;
1352 pcg 1.1 case 0:
1353 pcg 1.11 break;
1354 pcg 1.1 default:
1355 pcg 1.29 /*printf("%d\n", pid);*/
1356 chris_moore 1.26 exit (0);
1357 pcg 1.1 }
1358    
1359 pcg 1.11 if (setsid () == -1)
1360     return -1;
1361 pcg 1.1
1362 pcg 1.11 return 0;
1363 pcg 1.1 }