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