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