ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.49
Committed: Wed Apr 7 03:54:30 2004 UTC (22 years, 5 months ago) by chris_moore
Content type: text/plain
Branch: MAIN
Changes since 1.48: +460 -263 lines
Log Message:
new options -justify and -cont-color
linked list instead of array

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