ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.53
Committed: Thu Apr 8 01:19:57 2004 UTC (22 years, 5 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.52: +7 -4 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     * Copyright 2001 by Marco d'Itri <md@linux.it>
3 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 pcg 1.53 for (i = 0; i < line->num_words; i++)
401 chris_moore 1.52 XmbDrawString (disp, root, line->logfile->fontset, WinGC,
402 pcg 1.53 x + line->breaks[i].width + ((i * line->free_pixels) / (line->num_words - 1))
403     + continuation_width * line->wrapped_left, y,
404 chris_moore 1.49 line->line + line->breaks[i].index,
405     line->breaks[i].len);
406 pcg 1.53
407 chris_moore 1.49 if (line->wrapped_left)
408     {
409     if (foreground) XSetForeground (disp, WinGC, continuation_color);
410 chris_moore 1.52 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x, y, continuation, continuation_length);
411 chris_moore 1.49 }
412     }
413     else
414     {
415 pcg 1.53 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x + continuation_width * line->wrapped_left,
416     y, line->line, line->len);
417    
418 chris_moore 1.49 if (line->wrapped_left)
419     {
420     if (foreground) XSetForeground (disp, WinGC, continuation_color);
421 chris_moore 1.52 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x, y, continuation, continuation_length);
422 chris_moore 1.49 }
423     }
424     }
425    
426 pcg 1.1 /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
427 pcg 1.11 void
428 chris_moore 1.28 refresh (int miny, int maxy, int clear, int refresh_all)
429 pcg 1.1 {
430 chris_moore 1.49 int lin = 0;
431 chris_moore 1.52 int space = height;
432 chris_moore 1.49 int offset;
433 pcg 1.11 unsigned long black_color = GetColor ("black");
434 chris_moore 1.49 struct line_node *line;
435     int step_per_line;
436     int foreground = 0;
437    
438     if (opt_reverse)
439 chris_moore 1.52 offset = effect_y_offset;
440 chris_moore 1.49 else
441 chris_moore 1.52 offset = height + effect_y_offset;
442 pcg 1.1
443 chris_moore 1.52 miny -= win_y;
444     maxy -= win_y;
445 pcg 1.1
446 pcg 1.22 if (clear && !opt_noflicker)
447 chris_moore 1.52 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False);
448 pcg 1.22
449 chris_moore 1.52 for (line = linelist; line; line = line->next, lin++)
450 pcg 1.11 {
451 chris_moore 1.52 struct displaymatrix *display_line;
452    
453     if (opt_noflicker && lin >= listlen)
454     {
455     int i = listlen;
456     listlen *= 1.5;
457     display = xrealloc(display, listlen * sizeof(struct displaymatrix));
458     for (; i < listlen; i++)
459     {
460     display[i].line = xstrdup ("");
461     display[i].len = 0;
462     display[i].offset = 0;
463     display[i].buffer_size = 0;
464     }
465     }
466    
467     display_line = display + lin;
468    
469     step_per_line = line->logfile->font_height + effect_y_space;
470     if (step_per_line > space)
471     break;
472    
473     if (!opt_reverse)
474     offset -= step_per_line;
475    
476     offset += line->logfile->font_ascent;
477 pcg 1.7
478 chris_moore 1.52 miny -= line->logfile->font_height;
479     maxy += line->logfile->font_height;
480 jebusbfb 1.4
481 chris_moore 1.52 if (offset >= miny && offset <= maxy)
482 pcg 1.29 {
483 chris_moore 1.52 /* if this line is a different than it was, then it
484     * needs displaying */
485     if (!opt_noflicker
486     || refresh_all
487     || display_line->len != line->len
488     || display_line->color != line->logfile->color
489     || display_line->offset != offset
490     || memcmp (display_line->line, line->line, line->len))
491     {
492     /* don't bother updating the record of what has been
493     * displayed if -noflicker isn't in effect, since we redraw
494     * the whole display every time anyway */
495     if (opt_noflicker)
496     {
497     /* update the record of what has been displayed;
498     * first make sure the buffer is big enough */
499     if (display_line->buffer_size < line->len)
500     {
501     display_line->buffer_size = line->len;
502     display_line->line = xrealloc (display_line->line, display_line->buffer_size);
503     }
504    
505     display_line->len = line->len;
506     display_line->color = line->logfile->color;
507     display_line->offset = offset;
508     memcpy (display_line->line, line->line, line->len);
509    
510     if (clear)
511     {
512     #ifdef DEBUG
513     static int toggle;
514     toggle = 1 - toggle;
515     XSetForeground (disp, WinGC, toggle ? GetColor ("cyan") : GetColor ("yellow"));
516     XFillRectangle (disp, root, WinGC, win_x, win_y + offset - line->logfile->font_ascent,
517     width, step_per_line);
518     #else /* DEBUG */
519     XClearArea (disp, root, win_x, win_y + offset - line->logfile->font_ascent,
520     width + MARGIN_OF_ERROR, step_per_line, False);
521     #endif /* DEBUG */
522     }
523     }
524    
525     if (opt_outline)
526     {
527     int x, y;
528     XSetForeground (disp, WinGC, black_color);
529    
530     for (x = -1; x <= 1; x += 2)
531     for (y = -1; y <= 1; y += 2)
532     draw_text (disp, root, WinGC,
533     win_x + effect_x_offset + x,
534     win_y + y + offset, line, foreground = 0);
535     }
536     else if (opt_shade)
537 chris_moore 1.49 {
538 chris_moore 1.52 XSetForeground (disp, WinGC, black_color);
539     draw_text (disp, root, WinGC,
540     win_x + effect_x_offset + SHADE_X,
541     win_y + offset + SHADE_Y, line, foreground = 0);
542 chris_moore 1.49 }
543    
544 chris_moore 1.52 XSetForeground (disp, WinGC, line->logfile->color);
545     draw_text (disp, root, WinGC,
546     win_x + effect_x_offset,
547     win_y + offset, line, foreground = 1);
548 chris_moore 1.49 }
549 chris_moore 1.52 }
550 pcg 1.22
551 chris_moore 1.52 if (opt_reverse)
552     offset += step_per_line;
553     offset -= line->logfile->font_ascent;
554    
555     miny += line->logfile->font_height;
556     maxy -= line->logfile->font_height;
557 chris_moore 1.28
558 chris_moore 1.52 space -= step_per_line;
559     }
560 chris_moore 1.27
561 chris_moore 1.52 if (space > 0 && clear)
562     {
563     #ifdef DEBUG
564     XSetForeground (disp, WinGC, GetColor ("orange"));
565     XFillRectangle (disp, root, WinGC, win_x, win_y + offset - (opt_reverse ? 0 : space),
566     width, space);
567     #else /* DEBUG */
568     XClearArea (disp, root, win_x, win_y + offset - (opt_reverse ? 0 : space),
569     width + MARGIN_OF_ERROR, space, False);
570     #endif
571 chris_moore 1.49 }
572    
573     /* any lines that didn't just get looked at are never going to be, so break the chain */
574     if (line) line->prev->next = 0;
575    
576     /* and throw them all away */
577     while (line)
578     {
579     struct line_node *this = line;
580     line = line->next;
581     if (this->logfile && this->logfile->last == this)
582     this->logfile->last = NULL;
583     free (this->line);
584     free (this->breaks);
585     free (this);
586 pcg 1.1 }
587    
588 pcg 1.11 if (opt_frame)
589 pcg 1.15 {
590     XSetForeground (disp, WinGC, GetColor (def_color));
591 chris_moore 1.52 /* note that XDrawRectangle() draws a rectangle one pixel bigger
592     * in both dimensions than you ask for, hence the subtractions.
593     * XFillRectangle() doesn't suffer from this problem */
594 chris_moore 1.27 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
595 pcg 1.15 }
596 pcg 1.1 }
597    
598     #if HAS_REGEX
599 pcg 1.11 void
600     transform_line (char *s)
601 pcg 1.1 {
602     #ifdef I_AM_Md
603 pcg 1.11 int i;
604     if (1)
605     {
606     for (i = 16; s[i]; i++)
607     s[i] = s[i + 11];
608 pcg 1.1 }
609 pcg 1.11 s[i + 1] = '\0';
610 pcg 1.1 #endif
611    
612 pcg 1.11 if (transformre)
613     {
614     int i;
615     regmatch_t matched[16];
616    
617 chris_moore 1.26 i = regexec (transformre, s, 16, matched, 0);
618 pcg 1.11 if (i == 0)
619     { /* matched */
620 chris_moore 1.49 int match_start = matched[0].rm_so;
621     int match_end = matched[0].rm_eo;
622     int old_len = match_end - match_start;
623     int new_len = strlen (transform_to);
624     int old_whole_len = strlen (s);
625    
626     printf ("regexp was matched by '%s' - replace with '%s'\n", s, transform_to);
627     printf ("match is from %d to %d\n", match_start, match_end);
628     if (new_len > old_len)
629     s = xrealloc (s, old_whole_len + new_len - old_len);
630 chris_moore 1.52
631 chris_moore 1.49 if (new_len != old_len)
632     {
633     memcpy (s + match_end + new_len - old_len,
634     s + match_end,
635     old_whole_len - match_end);
636 pcg 1.39 s[old_whole_len + new_len - old_len] = '\0';
637     }
638    
639 chris_moore 1.49 memcpy (s + match_start,
640     transform_to,
641     new_len);
642     printf ("transformed to '%s'\n", s);
643 pcg 1.11 }
644 chris_moore 1.26 else
645 chris_moore 1.49 printf ("regexp was not matched by '%s'\n", s);
646 pcg 1.1 }
647     }
648     #endif
649    
650 chris_moore 1.49 /*
651     * appends p2 to the end of p1, if p1 is not null
652     * otherwise allocates a new string and copies p2 to it
653     */
654 pcg 1.12 char *
655 chris_moore 1.49 concat_line (char *p1, const char *p2)
656 pcg 1.12 {
657 chris_moore 1.49 assert(p2);
658    
659 pcg 1.12 int l1 = p1 ? strlen (p1) : 0;
660     int l2 = strlen (p2);
661 chris_moore 1.49 char *r;
662    
663     if (p1)
664     r = xrealloc(p1, l1 + l2 + 1);
665     else
666     r = xmalloc (l2 + 1);
667 pcg 1.12
668     memcpy (r + l1, p2, l2);
669     r[l1 + l2] = 0;
670    
671     return r;
672     }
673 pcg 1.1
674     /*
675 chris_moore 1.41 * This routine can read a line of any length if it is called enough times.
676 pcg 1.1 */
677 pcg 1.11 int
678     lineinput (struct logfile_entry *logfile)
679 pcg 1.1 {
680 chris_moore 1.45 char buff[1024], *p;
681 pcg 1.12 int ch;
682 chris_moore 1.32 /* HACK-2: add on the length of any partial line which we will be appending to */
683 pcg 1.12 int ofs = logfile->buf ? strlen (logfile->buf) : 0;
684 pcg 1.1
685 chris_moore 1.45 /* this loop ensures that the whole line is read, even if it's
686     * longer than the buffer. we need to do this because when --whole
687     * is in effect we don't know whether to display the line or not
688     * until we've seen how (ie. whether) it ends */
689 pcg 1.11 do
690     {
691 chris_moore 1.45 p = buff;
692     do
693 chris_moore 1.49 {
694     ch = fgetc (logfile->fp);
695 pcg 1.1
696 chris_moore 1.49 if (ch == '\n' || ch == EOF)
697     break;
698     else if (ch == '\r')
699     continue; /* skip */
700     else if (ch == '\t')
701     {
702     do
703     {
704     *p++ = ' ';
705     ofs++;
706     }
707     while (ofs & 7);
708     }
709     else
710     {
711     *p++ = ch;
712     ofs++;
713     }
714     }
715 chris_moore 1.45 while (p < buff + (sizeof buff) - 8 - 1);
716 pcg 1.1
717 chris_moore 1.45 if (p == buff && ch == EOF)
718 chris_moore 1.49 return 0;
719 pcg 1.12
720 chris_moore 1.45 *p = 0;
721 pcg 1.12
722 chris_moore 1.49 p = logfile->buf = concat_line (logfile->buf, buff);
723 chris_moore 1.45 }
724     while (ch != '\n' && ch != EOF);
725 chris_moore 1.9
726 pcg 1.12 logfile->lastpartial = logfile->partial;
727 chris_moore 1.25 /* there are 3 ways we could have exited the loop: reading '\n',
728     * reaching EOF, or filling the buffer; the 2nd and 3rd of these
729     * both result in a partial line */
730     logfile->partial = ch != '\n';
731 chris_moore 1.52
732 pcg 1.12 if (logfile->partial && opt_whole)
733 pcg 1.11 return 0;
734 pcg 1.1
735     #if HAS_REGEX
736 pcg 1.12 transform_line (logfile->buf);
737 pcg 1.1 #endif
738 pcg 1.12 return 1;
739 pcg 1.1 }
740    
741     /* input: reads file->fname
742     * output: fills file->fp, file->inode
743     * returns file->fp
744     * in case of error, file->fp is NULL
745     */
746 pcg 1.11 FILE *
747     openlog (struct logfile_entry * file)
748     {
749     struct stat stats;
750    
751     if ((file->fp = fopen (file->fname, "r")) == NULL)
752     {
753     file->fp = NULL;
754     return NULL;
755     }
756    
757     fstat (fileno (file->fp), &stats);
758     if (S_ISFIFO (stats.st_mode))
759     {
760     if (fcntl (fileno (file->fp), F_SETFL, O_NONBLOCK) < 0)
761     perror ("fcntl"), exit (1);
762     file->inode = 0;
763     }
764     else
765     file->inode = stats.st_ino;
766    
767     if (opt_noinitial)
768     fseek (file->fp, 0, SEEK_END);
769 chris_moore 1.52 else /* if (stats.st_size > (listlen + 1) * width)
770     * HACK - 'width' is in pixels - how are we to know how much text will fit?
771     * fseek (file->fp, -((listlen + 2) * width/10), SEEK_END); */
772     fseek (file->fp, -5000, SEEK_END);
773 pcg 1.11
774     file->last_size = stats.st_size;
775     return file->fp;
776     }
777    
778     void
779     reopen (void)
780 pcg 1.1 {
781 pcg 1.11 struct logfile_entry *e;
782    
783     for (e = loglist; e; e = e->next)
784     {
785     if (!e->inode)
786     continue; /* skip stdin */
787 pcg 1.1
788 pcg 1.11 if (e->fp)
789     fclose (e->fp);
790     /* if fp is NULL we will try again later */
791     openlog (e);
792     }
793    
794     do_reopen = 0;
795     }
796 pcg 1.1
797 pcg 1.11 void
798     check_open_files (void)
799     {
800     struct logfile_entry *e;
801     struct stat stats;
802 pcg 1.1
803 pcg 1.11 for (e = loglist; e; e = e->next)
804     {
805     if (!e->inode)
806     continue; /* skip stdin */
807 pcg 1.1
808 pcg 1.11 if (stat (e->fname, &stats) < 0)
809     { /* file missing? */
810     sleep (1);
811     if (e->fp)
812     fclose (e->fp);
813 pcg 1.37 if (openlog (e) == NULL)
814     continue;
815     if (fstat (fileno (e->fp), &stats) < 0)
816     continue;
817 pcg 1.11 }
818    
819     if (stats.st_ino != e->inode)
820     { /* file renamed? */
821     if (e->fp)
822     fclose (e->fp);
823     if (openlog (e) == NULL)
824 chris_moore 1.23 continue;
825 pcg 1.39 if (fstat (fileno (e->fp), &stats) < 0)
826     continue;
827 pcg 1.11 }
828    
829     if (stats.st_size < e->last_size)
830     { /* file truncated? */
831     fseek (e->fp, 0, SEEK_SET);
832     e->last_size = stats.st_size;
833     }
834 pcg 1.1 }
835     }
836    
837 pcg 1.14 /*
838 chris_moore 1.49 * insert a single node in the list of screen lines and return a
839     * pointer to the new node.
840     * the caller MUST then fill in ret->line and ret->len with valid
841     * data.
842 pcg 1.14 */
843 chris_moore 1.49 static struct line_node *
844     new_line_node (struct logfile_entry *log)
845 pcg 1.12 {
846 chris_moore 1.49 struct line_node *new = xmalloc (sizeof (struct line_node));
847 pcg 1.12
848 chris_moore 1.49 new->logfile = log;
849     new->wrapped_left = 0;
850     new->wrapped_right = 0;
851     new->breaks = 0;
852 pcg 1.12
853 chris_moore 1.49 assert(log);
854 pcg 1.12
855 chris_moore 1.49 if (!log || !log->last)
856     {
857     new->next = linelist;
858     new->next->prev = new;
859 pcg 1.11
860 chris_moore 1.49 new->prev = NULL;
861     linelist = new;
862     }
863     else
864     {
865     /* 2 pointers from the new node */
866     new->next = log->last;
867     new->prev = log->last->prev;
868 pcg 1.12
869 chris_moore 1.49 /* 2 pointers back to the new node */
870     if (new->next) new->next->prev = new;
871     if (new->prev) new->prev->next = new;
872 chris_moore 1.48
873 chris_moore 1.49 /* if this is a new first entry in the list then update
874     * 'linelist' */
875     if (log->last == linelist)
876     linelist = new;
877     }
878 pcg 1.12
879 chris_moore 1.49 /* update the logfile record */
880     if (log)
881     log->last = new;
882 pcg 1.12
883 chris_moore 1.49 return new;
884 pcg 1.12 }
885    
886 pcg 1.14 /*
887 chris_moore 1.49 * this is called after either adding a new line or appending to an
888     * old one. in both cases it's possible that the line no longer fits,
889     * and needs wrapping. this function checks the last line associated
890     * with the supplied logfile.
891 pcg 1.14 */
892 pcg 1.12 static void
893 chris_moore 1.49 possibly_split_long_line (struct logfile_entry *log)
894 pcg 1.12 {
895 chris_moore 1.49 char *str = log->last->line;
896 pcg 1.12 int l = strlen (str);
897 chris_moore 1.49 char *p = str;
898     struct line_node *line;
899     int spaces;
900     static struct breakinfo *breaks;
901     static int break_buffer_size;
902 chris_moore 1.33
903     /* only calculate the continuation's width once */
904     if (continuation_width == -1)
905     {
906 pcg 1.39 continuation_length = strlen (continuation);
907 chris_moore 1.52 continuation_width = XmbTextEscapement (log->fontset, continuation, continuation_length);
908 chris_moore 1.49 continuation_color = GetColor (cont_color);
909    
910     /* make an array to store information about the location of
911     * spaces in the line */
912     if (opt_justify)
913     {
914     break_buffer_size = 32;
915     breaks = xmalloc (break_buffer_size * sizeof (struct breakinfo));
916     }
917 chris_moore 1.33 }
918 pcg 1.12
919     do
920     {
921     const char *beg = p;
922 chris_moore 1.49 int start_w = log->last->wrapped_left ? continuation_width : 0;
923     int w = start_w;
924 chris_moore 1.33 int wrapped = 0;
925 chris_moore 1.49 char *break_p = NULL;
926 chris_moore 1.50 int width_at_break_p = 0;
927 chris_moore 1.49 spaces = 0;
928    
929     if (opt_justify)
930     breaks[spaces].index = breaks[spaces].width = 0;
931 pcg 1.12
932     while (*p)
933     {
934 pcg 1.47 int cw, len;
935    
936 chris_moore 1.49 /* find the length in bytes of the next multibyte character */
937 pcg 1.47 len = mblen (p, l);
938 pcg 1.12 if (len <= 0)
939 chris_moore 1.24 len = 1; /* ignore (don't skip) illegal character sequences */
940 pcg 1.12
941 chris_moore 1.49 /* find the width in pixels of the next character */
942 chris_moore 1.52 cw = XmbTextEscapement (log->fontset, p, len);
943 chris_moore 1.49 if (opt_wordwrap && len == 1 && p[0] == ' ' && p != break_p + 1)
944     {
945     break_p = p;
946     width_at_break_p = w;
947     spaces++;
948    
949     if (opt_justify)
950     {
951     /* increase the size of the 'breaks' array when
952     * necessary */
953     if (spaces >= break_buffer_size)
954     {
955     break_buffer_size *= 1.5;
956     breaks = xrealloc (breaks, break_buffer_size * sizeof (struct breakinfo));
957     }
958    
959     /* store information about (a) the location of each
960     * space */
961     breaks[spaces].index = p + 1 - beg;
962     /* (b) the width (in pixels) of the string up to
963     * this space */
964     breaks[spaces].width = cw + w - start_w;
965     /* (c) the length of each 'word' */
966     breaks[spaces-1].len = breaks[spaces].index - breaks[spaces-1].index;
967     }
968     }
969    
970 chris_moore 1.27 if (cw + w > width - effect_x_space)
971 chris_moore 1.49 {
972     if (p == beg)
973     {
974     fprintf (stderr, "we can't even fit a single character onto the line\n");
975     if (len == 1) fprintf (stderr, "(the character we couldn't fit was '%c')\n", *p);
976     exit (1);
977     }
978 chris_moore 1.24
979 chris_moore 1.49 wrapped = 1;
980     break;
981     }
982 pcg 1.12
983     w += cw;
984     p += len;
985     l -= len;
986     }
987    
988 chris_moore 1.24 /* if we're wrapping at spaces, and the line is long enough to
989     * wrap, and we've seen a space already, and the space wasn't
990     * the first character on the line, then wrap at the space */
991 chris_moore 1.49 if (!wrapped)
992     break;
993 chris_moore 1.52
994 chris_moore 1.49 int prefix_len;
995 chris_moore 1.24
996 chris_moore 1.49 /* choose where to break the line */
997     if (opt_wordwrap && break_p && break_p != beg)
998     {
999     prefix_len = break_p - beg;
1000     p = break_p;
1001     w = width_at_break_p;
1002    
1003     /* if breaking at a space, skip all adjacent spaces */
1004     while (*p == ' ')
1005     {
1006     int len = mblen (p, l);
1007     if (len != 1) break;
1008     p++;
1009     }
1010 chris_moore 1.24
1011 chris_moore 1.49 if (opt_justify)
1012     {
1013     spaces--;
1014     breaks[spaces].len--;
1015     }
1016     }
1017     else
1018     prefix_len = p - beg;
1019 chris_moore 1.52
1020 chris_moore 1.49 /* make a copy of the tail end of the string */
1021     p = xstrdup (p);
1022    
1023     /* and reduce the size of the head of the string */
1024     log->last->line = xrealloc (log->last->line, prefix_len + 1);
1025     log->last->len = prefix_len;
1026     log->last->line[prefix_len] = '\0';
1027    
1028     /* note that the head was wrapped on it's right */
1029     log->last->wrapped_right = 1;
1030    
1031     /* 'spaces' includes any space we broke on; we can only justify
1032     * if there's at least one other space */
1033     if (opt_justify && spaces &&
1034 chris_moore 1.52 width - effect_x_space - width_at_break_p < spaces * log->font_height)
1035 chris_moore 1.49 {
1036     int i;
1037 chris_moore 1.52 log->last->free_pixels = width - effect_x_space - w;
1038 chris_moore 1.49 log->last->num_words = spaces + 1;
1039     log->last->breaks = malloc (log->last->num_words * sizeof (struct breakinfo));
1040     for (i = 0; i < log->last->num_words; i++)
1041     log->last->breaks[i] = breaks[i];
1042     }
1043 chris_moore 1.52
1044 chris_moore 1.49 line = new_line_node (log);
1045     line->line = p;
1046     l = line->len = strlen (p);
1047 chris_moore 1.33
1048 chris_moore 1.49 /* note that the tail end of the string is wrapped at its left */
1049     line->wrapped_left = 1;
1050 pcg 1.12 }
1051     while (l);
1052     }
1053    
1054 chris_moore 1.49 static void
1055     insert_new_line (char *str, struct logfile_entry *log)
1056     {
1057     struct line_node *new;
1058     new = new_line_node (log);
1059     new->line = str;
1060     new->len = strlen (str);
1061    
1062     possibly_split_long_line (log);
1063     }
1064    
1065 pcg 1.14 /*
1066     * append something to an existing physical line. this is done
1067     * by deleting the file on-screen, concatenating the new data to it
1068     * and splitting it again.
1069     */
1070 pcg 1.12 static void
1071 chris_moore 1.49 append_to_existing_line (char *str, struct logfile_entry *log)
1072 pcg 1.12 {
1073 chris_moore 1.49 char *old, *new;
1074    
1075     assert(log);
1076     assert(log->last);
1077    
1078     old = log->last->line;
1079     assert(old);
1080    
1081     new = concat_line (old, str);
1082     free (str);
1083     log->last->line = new;
1084     log->last->len = strlen (new);
1085 chris_moore 1.52 possibly_split_long_line (log);
1086 pcg 1.12 }
1087    
1088     static void
1089 pcg 1.11 main_loop (void)
1090     {
1091 pcg 1.20 int lin;
1092 pcg 1.11 time_t lastreload;
1093     Region region = XCreateRegion ();
1094     XEvent xev;
1095     struct logfile_entry *lastprinted = NULL;
1096     struct logfile_entry *current;
1097 pcg 1.18 int need_update = 1;
1098 pcg 1.11
1099 pcg 1.47 display = xmalloc (sizeof (struct displaymatrix) * listlen);
1100    
1101 pcg 1.11 lastreload = time (NULL);
1102    
1103 chris_moore 1.49 /* Initialize line_node */
1104 pcg 1.11 for (lin = 0; lin < listlen; lin++)
1105     {
1106 chris_moore 1.49 struct line_node *e = xmalloc (sizeof (struct line_node));
1107     e->line = xstrdup ("~");
1108     e->len = 1;
1109     e->logfile = loglist; /* this is only needed to get a color for the '~' */
1110     e->wrapped_left = 0;
1111     e->wrapped_right = 0;
1112     e->breaks = 0;
1113     e->next = NULL;
1114     e->prev = linelist_tail;
1115    
1116     if (!linelist)
1117     linelist = e;
1118     if (linelist_tail)
1119     linelist_tail->next = e;
1120     linelist_tail = e;
1121    
1122     display[lin].line = xstrdup ("");
1123 chris_moore 1.28 display[lin].len = 0;
1124 chris_moore 1.52 display[lin].offset = 0;
1125 pcg 1.39 display[lin].buffer_size = 0;
1126 pcg 1.11 }
1127    
1128     for (;;)
1129     {
1130     /* read logs */
1131     for (current = loglist; current; current = current->next)
1132     {
1133     if (!current->fp)
1134     continue; /* skip missing files */
1135    
1136     clearerr (current->fp);
1137    
1138 pcg 1.12 while (lineinput (current))
1139 pcg 1.11 {
1140 pcg 1.12 need_update = 1;
1141 pcg 1.11 /* if we're trying to update old partial lines in
1142     * place, and the last time this file was updated the
1143     * output was partial, and that partial line is not
1144     * too close to the top of the screen, then update
1145     * that partial line */
1146 chris_moore 1.49 if (opt_update && current->lastpartial && current->last)
1147     {
1148     append_to_existing_line (current->buf, current);
1149     current->buf = 0;
1150     continue;
1151     }
1152    
1153     /* if all we just read was a newline ending a line that we've already displayed, skip it */
1154     if (current->buf[0] == '\0' && current->lastpartial)
1155 pcg 1.11 {
1156 chris_moore 1.52 free(current->buf);
1157 chris_moore 1.49 current->buf = 0;
1158 pcg 1.12 continue;
1159 pcg 1.11 }
1160    
1161     /* print filename if any, and if last line was from
1162     * different file */
1163 pcg 1.13 if (!opt_nofilename && lastprinted != current && current->desc[0])
1164 pcg 1.11 {
1165 chris_moore 1.49 current->last = 0;
1166     insert_new_line (xstrdup ("["), current);
1167     append_to_existing_line (xstrdup (current->desc), current);
1168     append_to_existing_line (xstrdup ("]"), current);
1169 pcg 1.11 }
1170    
1171 chris_moore 1.31 /* if we're dealing with partial lines, and the last
1172     * time we showed the line it wasn't finished ... */
1173     if (!opt_whole && current->lastpartial)
1174 chris_moore 1.49 {
1175     /* if this is the same file we showed last then
1176     append to the last line shown */
1177     if (lastprinted == current)
1178     append_to_existing_line (current->buf, current);
1179     else
1180     {
1181     /* but if a different file has been shown in the
1182     * mean time, make a new line, starting with the
1183     * continuation string */
1184     insert_new_line (current->buf, current);
1185     current->last->wrapped_left = 1;
1186     }
1187     }
1188 pcg 1.11 else
1189 chris_moore 1.49 /* otherwise just make a plain and simple new line */
1190     insert_new_line (current->buf, current);
1191 pcg 1.11
1192 chris_moore 1.49 current->buf = 0;
1193 pcg 1.11 lastprinted = current;
1194     }
1195     }
1196    
1197     if (need_update)
1198 pcg 1.18 {
1199 chris_moore 1.28 redraw (0);
1200 pcg 1.18 need_update = 0;
1201     }
1202 pcg 1.11 else
1203     {
1204     XFlush (disp);
1205    
1206     if (!XPending (disp))
1207     {
1208     fd_set fdr;
1209     struct timeval to = interval;
1210    
1211     FD_ZERO (&fdr);
1212     FD_SET (ConnectionNumber (disp), &fdr);
1213     select (ConnectionNumber (disp) + 1, &fdr, 0, 0, &to);
1214     }
1215     }
1216    
1217     check_open_files ();
1218    
1219     if (do_reopen)
1220     reopen ();
1221    
1222     /* we ignore possible errors due to window resizing &c */
1223     while (XPending (disp))
1224     {
1225     XNextEvent (disp, &xev);
1226    
1227     switch (xev.type)
1228     {
1229     case Expose:
1230     {
1231     XRectangle r;
1232    
1233     r.x = xev.xexpose.x;
1234     r.y = xev.xexpose.y;
1235     r.width = xev.xexpose.width;
1236     r.height = xev.xexpose.height;
1237 pcg 1.20
1238 pcg 1.11 XUnionRectWithRegion (&r, region, region);
1239     }
1240     break;
1241     default:
1242     #ifdef DEBUGMODE
1243     fprintf (stderr, "PANIC! Unknown event %d\n", xev.type);
1244     #endif
1245     break;
1246     }
1247     }
1248    
1249     /* reload if requested */
1250     if (reload && lastreload + reload < time (NULL))
1251     {
1252     if (command && command[0])
1253     system (command);
1254    
1255     reopen ();
1256     lastreload = time (NULL);
1257     }
1258    
1259     if (!XEmptyRegion (region))
1260     {
1261 pcg 1.20 XRectangle r;
1262    
1263 pcg 1.11 XSetRegion (disp, WinGC, region);
1264 pcg 1.20 XClipBox (region, &r);
1265    
1266 chris_moore 1.28 refresh (r.y, r.y + r.height, 0, 1);
1267 pcg 1.11
1268     XDestroyRegion (region);
1269     region = XCreateRegion ();
1270     }
1271     }
1272     }
1273 pcg 1.1
1274 pcg 1.11
1275     int
1276     main (int argc, char *argv[])
1277 pcg 1.1 {
1278 pcg 1.11 int i;
1279     int opt_daemonize = 0;
1280     int opt_partial = 0, file_count = 0;
1281 pcg 1.1 #if HAS_REGEX
1282 pcg 1.11 char *transform = NULL;
1283 pcg 1.1 #endif
1284    
1285 pcg 1.11 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
1286    
1287     /* window needs to be initialized before colorlookups can be done */
1288     /* just a dummy to get the color lookups right */
1289     geom_mask = NoValue;
1290     InitWindow ();
1291    
1292     for (i = 1; i < argc; i++)
1293     {
1294     const char *arg = argv[i];
1295 pcg 1.7
1296 pcg 1.11 if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',')
1297     {
1298     if (arg[1] == '-')
1299     arg++;
1300    
1301     if (!strcmp (arg, "-?") ||
1302     !strcmp (arg, "-help") || !strcmp (arg, "-h"))
1303     display_help (argv[0]);
1304     else if (!strcmp (arg, "-V"))
1305     display_version ();
1306     else if (!strcmp (arg, "-g") || !strcmp (arg, "-geometry"))
1307     geom_mask =
1308     XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
1309     else if (!strcmp (arg, "-display"))
1310     dispname = argv[++i];
1311     else if (!strcmp (arg, "-cont"))
1312     continuation = argv[++i];
1313 chris_moore 1.49 else if (!strcmp (arg, "-cont-color"))
1314     cont_color = argv[++i];
1315 pcg 1.11 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
1316     fontname = argv[++i];
1317 pcg 1.1 #if HAS_REGEX
1318 pcg 1.11 else if (!strcmp (arg, "-t"))
1319 chris_moore 1.49 {
1320     transform = argv[++i];
1321     transform_to = argv[++i];
1322     printf ("transform: '%s' to '%s'\n", transform, transform_to);
1323     }
1324 pcg 1.1 #endif
1325 pcg 1.11 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
1326     opt_daemonize = 1;
1327     else if (!strcmp (arg, "-reload"))
1328     {
1329     reload = atoi (argv[++i]);
1330     command = argv[++i];
1331     }
1332     else if (!strcmp (arg, "-shade"))
1333     opt_shade = 1;
1334 pcg 1.21 else if (!strcmp (arg, "-outline"))
1335     opt_outline = 1;
1336 pcg 1.22 else if (!strcmp (arg, "-noflicker"))
1337     opt_noflicker = 1;
1338 pcg 1.11 else if (!strcmp (arg, "-frame"))
1339     opt_frame = 1;
1340     else if (!strcmp (arg, "-no-filename"))
1341     opt_nofilename = 1;
1342     else if (!strcmp (arg, "-reverse"))
1343     opt_reverse = 1;
1344     else if (!strcmp (arg, "-whole"))
1345     opt_whole = 1;
1346     else if (!strcmp (arg, "-partial"))
1347     opt_partial = 1;
1348     else if (!strcmp (arg, "-update"))
1349     opt_update = opt_partial = 1;
1350 chris_moore 1.24 else if (!strcmp (arg, "-wordwrap"))
1351     opt_wordwrap = 1;
1352 chris_moore 1.49 else if (!strcmp (arg, "-justify"))
1353     opt_justify = 1;
1354 pcg 1.11 else if (!strcmp (arg, "-color"))
1355     def_color = argv[++i];
1356     else if (!strcmp (arg, "-noinitial"))
1357     opt_noinitial = 1;
1358     else if (!strcmp (arg, "-id"))
1359     root = atoi (argv[++i]);
1360     else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i"))
1361     {
1362     double iv = atof (argv[++i]);
1363    
1364     interval.tv_sec = (int) iv;
1365     interval.tv_usec = (iv - interval.tv_sec) * 1e6;
1366     }
1367     else
1368     {
1369     fprintf (stderr, "Unknown option '%s'.\n"
1370     "Try --help for more information.\n", arg);
1371     exit (1);
1372     }
1373     }
1374     else
1375     { /* it must be a filename */
1376     struct logfile_entry *e;
1377     const char *fname, *desc, *fcolor = def_color;
1378     char *p;
1379    
1380     file_count++;
1381    
1382     /* this is not foolproof yet (',' in filenames are not allowed) */
1383     fname = desc = arg;
1384     if ((p = strchr (arg, ',')))
1385     {
1386     *p = '\0';
1387     fcolor = p + 1;
1388    
1389     if ((p = strchr (fcolor, ',')))
1390     {
1391     *p = '\0';
1392     desc = p + 1;
1393     }
1394     }
1395    
1396     e = xmalloc (sizeof (struct logfile_entry));
1397 pcg 1.12 e->partial = 0;
1398     e->buf = 0;
1399    
1400 pcg 1.11 if (arg[0] == '-' && arg[1] == '\0')
1401     {
1402     if ((e->fp = fdopen (0, "r")) == NULL)
1403     perror ("fdopen"), exit (1);
1404     if (fcntl (0, F_SETFL, O_NONBLOCK) < 0)
1405     perror ("fcntl"), exit (1);
1406 pcg 1.39
1407 pcg 1.11 e->fname = NULL;
1408     e->inode = 0;
1409     e->desc = xstrdup ("stdin");
1410     }
1411     else
1412     {
1413 pcg 1.39 e->fname = xstrdup (fname);
1414 pcg 1.11
1415     if (openlog (e) == NULL)
1416     perror (fname), exit (1);
1417    
1418 pcg 1.38 e->desc = xstrdup (desc);
1419 pcg 1.11 }
1420    
1421     e->color = GetColor (fcolor);
1422     e->partial = 0;
1423 chris_moore 1.52 e->fontname = fontname;
1424 chris_moore 1.49 e->last = NULL;
1425 pcg 1.11 e->next = NULL;
1426    
1427     if (!loglist)
1428     loglist = e;
1429     if (loglist_tail)
1430     loglist_tail->next = e;
1431 pcg 1.39
1432 pcg 1.11 loglist_tail = e;
1433     }
1434     }
1435    
1436     if (!loglist)
1437     {
1438     fprintf (stderr, "You did not specify any files to tail\n"
1439     "use %s --help for help\n", argv[0]);
1440     exit (1);
1441     }
1442    
1443 chris_moore 1.43 if (opt_update && opt_whole)
1444     {
1445     fprintf (stderr, "Specify at most one of -update and -whole\n");
1446     exit (1);
1447     }
1448     else if (opt_partial && opt_whole)
1449 pcg 1.11 {
1450     fprintf (stderr, "Specify at most one of -partial and -whole\n");
1451     exit (1);
1452 chris_moore 1.9 }
1453    
1454 chris_moore 1.49 /* it doesn't make sense to justify if word wrap isn't on */
1455     if (opt_justify)
1456     opt_wordwrap = 1;
1457    
1458 chris_moore 1.32 /* HACK-7: do we want to allow both -shade and -outline? */
1459 chris_moore 1.27 if (opt_shade && opt_outline)
1460     {
1461     fprintf (stderr, "Specify at most one of -shade and -outline\n");
1462     exit (1);
1463     }
1464    
1465 pcg 1.11 if (opt_partial)
1466 chris_moore 1.9 /* if we specifically requested to see partial lines then don't insist on whole lines */
1467 pcg 1.11 opt_whole = 0;
1468     else if (file_count > 1)
1469 chris_moore 1.31 /* otherwise, if we're viewing multiple files, default to showing whole lines */
1470 pcg 1.11 opt_whole = 1;
1471 chris_moore 1.9
1472 pcg 1.1 #if HAS_REGEX
1473 pcg 1.11 if (transform)
1474     {
1475     int i;
1476 pcg 1.1
1477 chris_moore 1.49 printf ("compiling regexp '%s'\n", transform);
1478 chris_moore 1.26 transformre = xmalloc (sizeof (regex_t));
1479     i = regcomp (transformre, transform, REG_EXTENDED);
1480 pcg 1.11 if (i != 0)
1481     {
1482     char buf[512];
1483    
1484 chris_moore 1.26 regerror (i, transformre, buf, sizeof (buf));
1485 pcg 1.11 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1486     }
1487 chris_moore 1.26 else
1488 chris_moore 1.49 printf ("compiled '%s' OK to %x\n", transform, (int)transformre);
1489 pcg 1.1 }
1490     #endif
1491    
1492 pcg 1.11 InitWindow ();
1493 pcg 1.1
1494 pcg 1.11 install_signal (SIGINT, blank_window);
1495     install_signal (SIGQUIT, blank_window);
1496     install_signal (SIGTERM, blank_window);
1497     install_signal (SIGHUP, force_reopen);
1498     install_signal (SIGUSR1, list_files);
1499     install_signal (SIGUSR2, force_refresh);
1500 pcg 1.1
1501 pcg 1.11 if (opt_daemonize)
1502     daemonize ();
1503 pcg 1.1
1504 pcg 1.11 main_loop ();
1505 pcg 1.1
1506 pcg 1.11 exit (1); /* to make gcc -Wall stop complaining */
1507 pcg 1.1 }
1508    
1509 pcg 1.11 void
1510     install_signal (int sig, void (*handler) (int))
1511 pcg 1.1 {
1512 pcg 1.11 struct sigaction action;
1513    
1514     action.sa_handler = handler;
1515     sigemptyset (&action.sa_mask);
1516     action.sa_flags = SA_RESTART;
1517 pcg 1.1
1518 pcg 1.11 if (sigaction (sig, &action, NULL) < 0)
1519 chris_moore 1.49 fprintf (stderr, "sigaction (%d): %s\n", sig, strerror (errno)), exit (1);
1520 pcg 1.1 }
1521    
1522 pcg 1.11 void *
1523     xstrdup (const char *string)
1524 pcg 1.1 {
1525 pcg 1.11 void *p;
1526 pcg 1.1
1527 pcg 1.11 while ((p = strdup (string)) == NULL)
1528     {
1529 chris_moore 1.49 fprintf (stderr, "Memory exhausted in xstrdup ().\n");
1530 pcg 1.11 sleep (10);
1531 pcg 1.1 }
1532 pcg 1.11
1533     return p;
1534 pcg 1.1 }
1535    
1536 pcg 1.11 void *
1537     xmalloc (size_t size)
1538 pcg 1.1 {
1539 pcg 1.11 void *p;
1540 pcg 1.1
1541 pcg 1.11 while ((p = malloc (size)) == NULL)
1542     {
1543 chris_moore 1.49 fprintf (stderr, "Memory exhausted in xmalloc ().\n");
1544 pcg 1.11 sleep (10);
1545 pcg 1.1 }
1546 pcg 1.12
1547 pcg 1.11 return p;
1548 pcg 1.1 }
1549    
1550 chris_moore 1.28 void *
1551     xrealloc (void *ptr, size_t size)
1552     {
1553     void *p;
1554    
1555     while ((p = realloc (ptr, size)) == NULL)
1556     {
1557 chris_moore 1.49 fprintf (stderr, "Memory exhausted in xrealloc ().\n");
1558 chris_moore 1.28 sleep (10);
1559     }
1560    
1561     return p;
1562     }
1563    
1564 pcg 1.11 void
1565     display_help (char *myname)
1566     {
1567 chris_moore 1.52 printf ("Usage: %s [options] file1[,color[,desc]]"
1568     "[options] [file2[,color[,desc]] ...]\n", myname);
1569 pcg 1.11 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1570     " -color color use color $color as default\n"
1571     " -reload sec command reload after $sec and run command\n"
1572     " -id id window id to use instead of the root window\n"
1573     " -font FONTSPEC (-fn) font to use\n"
1574     " -f | -fork fork into background\n"
1575     " -reverse print new lines at the top\n"
1576     " -whole wait for \\n before showing a line\n"
1577     " -partial show lines even if they don't end with a \\n\n"
1578     " -update allow updates to old partial lines\n"
1579     " -cont string to prefix continued partial lines with\n"
1580 chris_moore 1.24 " -wordwrap wrap long lines at spaces to avoid breaking words\n"
1581 pcg 1.11 " defaults to \"[+]\"\n"
1582     " -shade add shading to font\n"
1583     " -noinitial don't display the last file lines on\n"
1584     " startup\n"
1585     " -i | -interval seconds interval between checks (fractional\n"
1586 pcg 1.21 " values o.k.). Default 2.4 seconds\n"
1587 pcg 1.11 " -V display version information and exit\n"
1588     "\n");
1589 chris_moore 1.46 printf ("Example:\n%s -g 800x250+100+50 -font fixed /var/log/messages,green "
1590 pcg 1.11 "/var/log/secure,red,'ALERT'\n", myname);
1591     exit (0);
1592     }
1593    
1594     void
1595     display_version (void)
1596 pcg 1.1 {
1597 pcg 1.11 printf ("root-tail version " VERSION "\n");
1598     exit (0);
1599 pcg 1.1 }
1600    
1601 pcg 1.11 int
1602     daemonize (void)
1603     {
1604 chris_moore 1.26 pid_t pid;
1605    
1606     switch (pid = fork ())
1607 pcg 1.11 {
1608 pcg 1.1 case -1:
1609 pcg 1.11 return -1;
1610 pcg 1.1 case 0:
1611 pcg 1.11 break;
1612 pcg 1.1 default:
1613 chris_moore 1.49 /*printf ("%d\n", pid);*/
1614 chris_moore 1.26 exit (0);
1615 pcg 1.1 }
1616    
1617 pcg 1.11 if (setsid () == -1)
1618     return -1;
1619 pcg 1.1
1620 pcg 1.11 return 0;
1621 pcg 1.1 }
1622 chris_moore 1.49
1623     /* todo - get reverse display working again */