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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines