ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
(Generate patch)

Comparing root-tail/root-tail.c (file contents):
Revision 1.2 by pcg, Sun May 5 15:51:24 2002 UTC vs.
Revision 1.47 by pcg, Thu Apr 1 13:37:30 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines