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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines