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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines