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