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