ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.52
Committed: Thu Apr 8 00:37:03 2004 UTC (20 years, 1 month ago) by chris_moore
Content type: text/plain
Branch: MAIN
Changes since 1.51: +186 -131 lines
Log Message:
Allow each logfile to display in a different font.

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