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.3 by pcg, Sun May 5 19:10:39 2002 UTC vs.
Revision 1.27 by chris_moore, Mon Mar 29 02:20:55 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines