ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/root-tail/root-tail.c
Revision: 1.60
Committed: Sun Apr 11 01:02:05 2004 UTC (22 years, 6 months ago) by chris_moore
Content type: text/plain
Branch: MAIN
Changes since 1.59: +17 -0 lines
Log Message:
Use code from XPenguins to find the root window under GNOME, KDE, etc.

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