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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines