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.52 by chris_moore, Thu Apr 8 00:37:03 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 * and many others, see README 5 * and many others, see README
6 * 6 *
7 * Original version by Mike Baker. 7 * Original version by Mike Baker.
8 * 8 *
9 * 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
10 * 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
11 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
20 * along with this program; if not, write to the Free Software 20 * along with this program; if not, write to the Free Software
21 * 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.
22 */ 22 */
23 23
24#include "config.h" 24#include "config.h"
25#include <assert.h>
25#include <stdlib.h> 26#include <stdlib.h>
26#include <stdio.h> 27#include <stdio.h>
27#include <unistd.h> 28#include <unistd.h>
28#include <string.h> 29#include <string.h>
29#include <signal.h> 30#include <signal.h>
45#endif 46#endif
46 47
47#define SHADE_X 2 48#define SHADE_X 2
48#define SHADE_Y 2 49#define SHADE_Y 2
49 50
51/* some italic fonts still go over the margin - this margin of error cleans up the mess */
52#define MARGIN_OF_ERROR 2
53
50/* data structures */ 54/* data structures */
51struct logfile_entry 55struct logfile_entry
52{ 56{
53 struct logfile_entry *next; 57 struct logfile_entry *next;
54 58
55 char *fname; /* name of file */ 59 char *fname; /* name of file */
56 char *desc; /* alternative description */ 60 char *desc; /* alternative description */
57 char *buf; /* text read but not yet displayed */ 61 char *buf; /* text read but not yet displayed */
62 const char *fontname;
63 XFontSet fontset;
64 int font_height;
65 int font_ascent;
58 FILE *fp; /* FILE struct associated with file */ 66 FILE *fp; /* FILE struct associated with file */
59 ino_t inode; /* inode of the file opened */ 67 ino_t inode; /* inode of the file opened */
60 off_t last_size; /* file size at the last check */ 68 off_t last_size; /* file size at the last check */
61 unsigned long color; /* color to be used for printing */ 69 unsigned long color; /* color to be used for printing */
62 int partial; /* true if the last line isn't complete */ 70 int partial; /* true if the last line isn't complete */
63 int lastpartial; /* true if the previous output wasn't complete */ 71 int lastpartial; /* true if the previous output wasn't complete */
64 int index; /* index into linematrix of a partial line */ 72 struct line_node *last; /* last line we output */
65 int modified; /* true if line is modified & needs displaying */ 73 int modified; /* true if line is modified & needs displaying */
66}; 74};
67 75
76struct line_node
77{
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 */
89};
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
68struct linematrix 98struct displaymatrix
69{ 99{
70 char *line; 100 char *line;
71 int len; 101 int len;
72 unsigned long color; 102 int offset;
73};
74
75struct displaymatrix
76{
77 char *line;
78 int len;
79 int buffer_size; 103 int buffer_size;
80 unsigned long color; 104 unsigned long color;
81}; 105};
82 106
83/* global variables */ 107/* global variables */
84struct linematrix *lines; 108struct line_node *linelist = NULL, *linelist_tail = NULL;
85struct displaymatrix *display; 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 */
86int width = STD_WIDTH, height = STD_HEIGHT, listlen; 116int width = STD_WIDTH, height = STD_HEIGHT, listlen = 50;
87int win_x = LOC_X, win_y = LOC_Y; 117int 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 */ 118int 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 */ 119int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
91int do_reopen; 120int do_reopen;
92struct timeval interval = { 2, 400000 }; 121struct timeval interval = { 2, 400000 };
93XFontSet fontset;
94 122
95/* command line options */ 123/* command line options */
96int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, 124int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
97 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap, 125 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
98 geom_mask, reload = 0; 126 opt_justify, geom_mask, reload = 0;
99const char *command = NULL, 127const char *command = NULL,
100 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, 128 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
101 *continuation = "[+]"; 129 *continuation = "|| ", *cont_color = DEF_CONT_COLOR;
102 130
103struct logfile_entry *loglist = NULL, *loglist_tail = NULL; 131struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
104 132
105Display *disp; 133Display *disp;
106Window root; 134Window root;
169} 197}
170 198
171void 199void
172blank_window (int dummy) 200blank_window (int dummy)
173{ 201{
174 XClearArea (disp, root, win_x - 2, win_y - 2, width + 5, height + 5, False); 202 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False);
175 XFlush (disp); 203 XFlush (disp);
176 exit (0); 204 exit (0);
177} 205}
178 206
179/* X related functions */ 207/* X related functions */
261InitWindow (void) 289InitWindow (void)
262{ 290{
263 XGCValues gcv; 291 XGCValues gcv;
264 unsigned long gcm; 292 unsigned long gcm;
265 int screen, ScreenWidth, ScreenHeight; 293 int screen, ScreenWidth, ScreenHeight;
294 struct logfile_entry *e;
266 295
267 if (!(disp = XOpenDisplay (dispname))) 296 if (!(disp = XOpenDisplay (dispname)))
268 { 297 {
269 fprintf (stderr, "Can't open display %s.\n", dispname); 298 fprintf (stderr, "Can't open display %s.\n", dispname);
270 exit (1); 299 exit (1);
280 gcv.graphics_exposures = True; 309 gcv.graphics_exposures = True;
281 WinGC = XCreateGC (disp, root, gcm, &gcv); 310 WinGC = XCreateGC (disp, root, gcm, &gcv);
282 XMapWindow (disp, root); 311 XMapWindow (disp, root);
283 XSetForeground (disp, WinGC, GetColor (DEF_COLOR)); 312 XSetForeground (disp, WinGC, GetColor (DEF_COLOR));
284 313
314 for (e = loglist; e; e = e->next)
285 { 315 {
286 char **missing_charset_list; 316 char **missing_charset_list;
287 int missing_charset_count; 317 int missing_charset_count;
288 char *def_string; 318 char *def_string;
289 319
290 fontset = XCreateFontSet (disp, fontname, 320 e->fontset = XCreateFontSet (disp, e->fontname,
291 &missing_charset_list, &missing_charset_count, 321 &missing_charset_list, &missing_charset_count,
292 &def_string); 322 &def_string);
293 323
294 if (missing_charset_count) 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
295 { 338 {
296 fprintf (stderr, 339 XFontSetExtents *xfe = XExtentsOfFontSet (e->fontset);
297 "Missing charsets in String to FontSet conversion (%s)\n", 340
298 missing_charset_list[0]); 341 e->font_height = xfe->max_logical_extent.height;
299 XFreeStringList (missing_charset_list); 342 e->font_ascent = -xfe->max_logical_extent.y;
300 } 343 }
301 }
302
303 if (!fontset)
304 { 344 }
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 345
316 if (geom_mask & XNegative) 346 if (geom_mask & XNegative)
317 win_x = win_x + ScreenWidth - width; 347 win_x = win_x + ScreenWidth - width;
318 if (geom_mask & YNegative) 348 if (geom_mask & YNegative)
319 win_y = win_y + ScreenHeight - height; 349 win_y = win_y + ScreenHeight - height;
320 350
321 if (opt_outline) 351 if (opt_outline)
322 { 352 {
323 /* adding outline increases the total width and height by 2 353 /* adding outline increases the total width and height by 2
324 pixels each, and offsets the text one pixel right and one 354 pixels each, and offsets the text one pixel right and one
325 pixel down */ 355 pixel down */
326 effect_x_space = effect_y_space = 2; 356 effect_x_space = effect_y_space = 2;
327 effect_x_offset = effect_y_offset = 1; 357 effect_x_offset = effect_y_offset = 1;
328 } 358 }
329 else if (opt_shade) 359 else if (opt_shade)
330 { 360 {
331 /* adding a shadow increases the space used */ 361 /* adding a shadow increases the space used */
332 effect_x_space = abs(SHADE_X); 362 effect_x_space = abs (SHADE_X);
333 effect_y_space = abs(SHADE_Y); 363 effect_y_space = abs (SHADE_Y);
364
334 /* if the shadow is to the right and below then we don't need 365 /* 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 366 * to move the text to make space for it, but shadows to the left
336 * and above need accomodating */ 367 * and above need accomodating */
337 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X; 368 effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X;
338 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y; 369 effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y;
341 { 372 {
342 effect_x_space = effect_y_space = 0; 373 effect_x_space = effect_y_space = 0;
343 effect_x_offset = effect_y_offset = 0; 374 effect_x_offset = effect_y_offset = 0;
344 } 375 }
345 376
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
363 XSelectInput (disp, root, ExposureMask | FocusChangeMask); 377 XSelectInput (disp, root, ExposureMask | FocusChangeMask);
364} 378}
365 379
366/* 380/*
367 * if redraw() is passwd a non-zero argument, it does a complete 381 * if redraw () is passwd a non-zero argument, it does a complete
368 * redraw, rather than an update. if the argument is zero (and 382 * redraw, rather than an update. if the argument is zero (and
369 * -noflicker is in effect) then only the lines which have changed 383 * -noflicker is in effect) then only the lines which have changed
370 * since the last draw are redrawn. 384 * since the last draw are redrawn.
371 * 385 *
372 * the rest is handled by regular refresh()'es 386 * the rest is handled by regular refresh ()'es
373 */ 387 */
374void 388void
375redraw (int redraw_all) 389redraw (int redraw_all)
376{ 390{
377 XSetClipMask (disp, WinGC, None); 391 XSetClipMask (disp, WinGC, None);
378 refresh (0, 32768, 1, redraw_all); 392 refresh (0, 32768, 1, redraw_all);
379} 393}
380 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)) + continuation_width * line->wrapped_left, y,
403 line->line + line->breaks[i].index,
404 line->breaks[i].len);
405 }
406 if (line->wrapped_left)
407 {
408 if (foreground) XSetForeground (disp, WinGC, continuation_color);
409 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x, y, continuation, continuation_length);
410 }
411 }
412 else
413 {
414 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x + continuation_width * line->wrapped_left, y, line->line, line->len);
415 if (line->wrapped_left)
416 {
417 if (foreground) XSetForeground (disp, WinGC, continuation_color);
418 XmbDrawString (disp, root, line->logfile->fontset, WinGC, x, y, continuation, continuation_length);
419 }
420 }
421}
422
381/* Just redraw everything without clearing (i.e. after an EXPOSE event) */ 423/* Just redraw everything without clearing (i.e. after an EXPOSE event) */
382void 424void
383refresh (int miny, int maxy, int clear, int refresh_all) 425refresh (int miny, int maxy, int clear, int refresh_all)
384{ 426{
385 int lin; 427 int lin = 0;
386 int offset = listlen * font_height + font_ascent + effect_y_offset; 428 int space = height;
429 int offset;
387 unsigned long black_color = GetColor ("black"); 430 unsigned long black_color = GetColor ("black");
431 struct line_node *line;
432 int step_per_line;
433 int foreground = 0;
388 434
389 miny -= win_y + font_height; 435 if (opt_reverse)
390 maxy -= win_y - font_height; 436 offset = effect_y_offset;
437 else
438 offset = height + effect_y_offset;
439
440 miny -= win_y;
441 maxy -= win_y;
391 442
392 if (clear && !opt_noflicker) 443 if (clear && !opt_noflicker)
393 XClearArea (disp, root, win_x, win_y, width, height, False); 444 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False);
394 445
395 for (lin = listlen; lin--;) 446 for (line = linelist; line; line = line->next, lin++)
396 { 447 {
397 struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin);
398 struct displaymatrix *display_line = display + lin; 448 struct displaymatrix *display_line;
399 449
400 offset -= font_height; 450 if (opt_noflicker && lin >= listlen)
451 {
452 int i = listlen;
453 listlen *= 1.5;
454 display = xrealloc(display, listlen * sizeof(struct displaymatrix));
455 for (; i < listlen; i++)
456 {
457 display[i].line = xstrdup ("");
458 display[i].len = 0;
459 display[i].offset = 0;
460 display[i].buffer_size = 0;
461 }
462 }
401 463
464 display_line = display + lin;
465
466 step_per_line = line->logfile->font_height + effect_y_space;
467 if (step_per_line > space)
468 break;
469
470 if (!opt_reverse)
471 offset -= step_per_line;
472
473 offset += line->logfile->font_ascent;
474
475 miny -= line->logfile->font_height;
476 maxy += line->logfile->font_height;
477
402 if (offset < miny || offset > maxy) 478 if (offset >= miny && offset <= maxy)
403 continue; 479 {
404
405 /* if this line is a different than it was, then it 480 /* if this line is a different than it was, then it
406 * needs displaying */ 481 * needs displaying */
407 if (!opt_noflicker 482 if (!opt_noflicker
408 || refresh_all 483 || refresh_all
409 || display_line->len != line->len 484 || display_line->len != line->len
410 || display_line->color != line->color 485 || display_line->color != line->logfile->color
486 || display_line->offset != offset
411 || memcmp (display_line->line, line->line, line->len)) 487 || memcmp (display_line->line, line->line, line->len))
412 { 488 {
413 /* don't bother updating the record of what has been 489 /* don't bother updating the record of what has been
414 * displayed if -noflicker isn't in effect, since we redraw 490 * displayed if -noflicker isn't in effect, since we redraw
415 * the whole display every time anyway */ 491 * the whole display every time anyway */
416 if (opt_noflicker) 492 if (opt_noflicker)
417 { 493 {
418 /* update the record of what has been displayed; 494 /* update the record of what has been displayed;
419 * first make sure the buffer is big enough */ 495 * first make sure the buffer is big enough */
420 if (display_line->buffer_size < line->len) 496 if (display_line->buffer_size < line->len)
421 { 497 {
422 display_line->buffer_size = line->len; 498 display_line->buffer_size = line->len;
423 display_line->line = xrealloc (display_line->line, display_line->buffer_size); 499 display_line->line = xrealloc (display_line->line, display_line->buffer_size);
424 } 500 }
425 501
426 display_line->len = line->len; 502 display_line->len = line->len;
427 display_line->color = line->color; 503 display_line->color = line->logfile->color;
504 display_line->offset = offset;
428 memcpy (display_line->line, line->line, line->len); 505 memcpy (display_line->line, line->line, line->len);
429 506
430 if (clear) 507 if (clear)
508 {
509#ifdef DEBUG
510 static int toggle;
511 toggle = 1 - toggle;
512 XSetForeground (disp, WinGC, toggle ? GetColor ("cyan") : GetColor ("yellow"));
513 XFillRectangle (disp, root, WinGC, win_x, win_y + offset - line->logfile->font_ascent,
514 width, step_per_line);
515#else /* DEBUG */
431 XClearArea (disp, root, win_x, win_y + offset - font_ascent, 516 XClearArea (disp, root, win_x, win_y + offset - line->logfile->font_ascent,
432 width + effect_x_space, font_height + effect_y_space, False); 517 width + MARGIN_OF_ERROR, step_per_line, False);
433 } 518#endif /* DEBUG */
519 }
520 }
434 521
435 if (opt_outline) 522 if (opt_outline)
436 { 523 {
437 int x, y; 524 int x, y;
438 XSetForeground (disp, WinGC, black_color); 525 XSetForeground (disp, WinGC, black_color);
439 526
440 for (x = -1; x <= 1; x += 2) 527 for (x = -1; x <= 1; x += 2)
441 for (y = -1; y <= 1; y += 2) 528 for (y = -1; y <= 1; y += 2)
442 XmbDrawString (disp, root, fontset, WinGC, 529 draw_text (disp, root, WinGC,
443 win_x + effect_x_offset + x, 530 win_x + effect_x_offset + x,
444 win_y + y + offset, 531 win_y + y + offset, line, foreground = 0);
445 line->line, line->len);
446 } 532 }
447 else if (opt_shade) 533 else if (opt_shade)
448 { 534 {
449 XSetForeground (disp, WinGC, black_color); 535 XSetForeground (disp, WinGC, black_color);
450 XmbDrawString (disp, root, fontset, WinGC, 536 draw_text (disp, root, WinGC,
451 win_x + effect_x_offset + SHADE_X, 537 win_x + effect_x_offset + SHADE_X,
452 win_y + offset + SHADE_Y, 538 win_y + offset + SHADE_Y, line, foreground = 0);
453 line->line, line->len);
454 } 539 }
455 540
456 XSetForeground (disp, WinGC, line->color); 541 XSetForeground (disp, WinGC, line->logfile->color);
457 XmbDrawString (disp, root, fontset, WinGC, 542 draw_text (disp, root, WinGC,
458 win_x + effect_x_offset, 543 win_x + effect_x_offset,
459 win_y + offset, 544 win_y + offset, line, foreground = 1);
460 line->line, line->len); 545 }
461 } 546 }
547
548 if (opt_reverse)
549 offset += step_per_line;
550 offset -= line->logfile->font_ascent;
551
552 miny += line->logfile->font_height;
553 maxy -= line->logfile->font_height;
554
555 space -= step_per_line;
556 }
557
558 if (space > 0 && clear)
559 {
560#ifdef DEBUG
561 XSetForeground (disp, WinGC, GetColor ("orange"));
562 XFillRectangle (disp, root, WinGC, win_x, win_y + offset - (opt_reverse ? 0 : space),
563 width, space);
564#else /* DEBUG */
565 XClearArea (disp, root, win_x, win_y + offset - (opt_reverse ? 0 : space),
566 width + MARGIN_OF_ERROR, space, False);
567#endif
568 }
569
570 /* any lines that didn't just get looked at are never going to be, so break the chain */
571 if (line) line->prev->next = 0;
572
573 /* and throw them all away */
574 while (line)
575 {
576 struct line_node *this = line;
577 line = line->next;
578 if (this->logfile && this->logfile->last == this)
579 this->logfile->last = NULL;
580 free (this->line);
581 free (this->breaks);
582 free (this);
462 } 583 }
463 584
464 if (opt_frame) 585 if (opt_frame)
465 { 586 {
466 XSetForeground (disp, WinGC, GetColor (def_color)); 587 XSetForeground (disp, WinGC, GetColor (def_color));
588 /* note that XDrawRectangle() draws a rectangle one pixel bigger
589 * in both dimensions than you ask for, hence the subtractions.
590 * XFillRectangle() doesn't suffer from this problem */
467 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1); 591 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
468 } 592 }
469} 593}
470 594
471#if HAS_REGEX 595#if HAS_REGEX
488 regmatch_t matched[16]; 612 regmatch_t matched[16];
489 613
490 i = regexec (transformre, s, 16, matched, 0); 614 i = regexec (transformre, s, 16, matched, 0);
491 if (i == 0) 615 if (i == 0)
492 { /* matched */ 616 { /* matched */
493 int match_start = matched[0].rm_so; 617 int match_start = matched[0].rm_so;
494 int match_end = matched[0].rm_eo; 618 int match_end = matched[0].rm_eo;
495 int old_len = match_end - match_start; 619 int old_len = match_end - match_start;
496 int new_len = strlen (transform_to); 620 int new_len = strlen (transform_to);
497 int old_whole_len = strlen (s); 621 int old_whole_len = strlen (s);
498 622
499 printf ("regexp was matched by '%s' - replace with '%s'\n", s, transform_to); 623 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); 624 printf ("match is from %d to %d\n", match_start, match_end);
501 if (new_len > old_len) 625 if (new_len > old_len)
502 s = xrealloc(s, old_whole_len + new_len - old_len); 626 s = xrealloc (s, old_whole_len + new_len - old_len);
503 627
504 if (new_len != old_len) 628 if (new_len != old_len)
505 { 629 {
506 memcpy(s + match_end + new_len - old_len, 630 memcpy (s + match_end + new_len - old_len,
507 s + match_end, 631 s + match_end,
508 old_whole_len - match_end); 632 old_whole_len - match_end);
509 s[old_whole_len + new_len - old_len] = '\0'; 633 s[old_whole_len + new_len - old_len] = '\0';
510 } 634 }
511 635
512 memcpy (s + match_start, 636 memcpy (s + match_start,
513 transform_to, 637 transform_to,
514 new_len); 638 new_len);
515 printf ("transformed to '%s'\n", s); 639 printf ("transformed to '%s'\n", s);
516 } 640 }
517 else 641 else
518 {
519 printf ("regexp was not matched by '%s'\n", s); 642 printf ("regexp was not matched by '%s'\n", s);
520 }
521 } 643 }
522} 644}
523#endif 645#endif
524 646
647/*
648 * appends p2 to the end of p1, if p1 is not null
649 * otherwise allocates a new string and copies p2 to it
650 */
525char * 651char *
526concat_line (const char *p1, const char *p2) 652concat_line (char *p1, const char *p2)
527{ 653{
654 assert(p2);
655
528 int l1 = p1 ? strlen (p1) : 0; 656 int l1 = p1 ? strlen (p1) : 0;
529 int l2 = strlen (p2); 657 int l2 = strlen (p2);
530 char *r = xmalloc (l1 + l2 + 1); 658 char *r;
531 659
532 memcpy (r, p1, l1); 660 if (p1)
661 r = xrealloc(p1, l1 + l2 + 1);
662 else
663 r = xmalloc (l2 + 1);
664
533 memcpy (r + l1, p2, l2); 665 memcpy (r + l1, p2, l2);
534 r[l1 + l2] = 0; 666 r[l1 + l2] = 0;
535 667
536 return r; 668 return r;
537} 669}
553 * until we've seen how (ie. whether) it ends */ 685 * until we've seen how (ie. whether) it ends */
554 do 686 do
555 { 687 {
556 p = buff; 688 p = buff;
557 do 689 do
558 { 690 {
559 ch = fgetc (logfile->fp); 691 ch = fgetc (logfile->fp);
560 692
561 if (ch == '\n' || ch == EOF) 693 if (ch == '\n' || ch == EOF)
562 break; 694 break;
563 else if (ch == '\r') 695 else if (ch == '\r')
564 continue; /* skip */ 696 continue; /* skip */
565 else if (ch == '\t') 697 else if (ch == '\t')
566 { 698 {
567 do 699 do
568 { 700 {
569 *p++ = ' '; 701 *p++ = ' ';
570 ofs++; 702 ofs++;
571 } 703 }
572 while (ofs & 7); 704 while (ofs & 7);
573 } 705 }
574 else 706 else
575 { 707 {
576 *p++ = ch; 708 *p++ = ch;
577 ofs++; 709 ofs++;
578 } 710 }
579 } 711 }
580 while (p < buff + (sizeof buff) - 8 - 1); 712 while (p < buff + (sizeof buff) - 8 - 1);
581 713
582 if (p == buff && ch == EOF) 714 if (p == buff && ch == EOF)
583 return 0; 715 return 0;
584 716
585 *p = 0; 717 *p = 0;
586 718
587 p = concat_line (logfile->buf, buff); 719 p = logfile->buf = concat_line (logfile->buf, buff);
588 free (logfile->buf); logfile->buf = p;
589 } 720 }
590 while (ch != '\n' && ch != EOF); 721 while (ch != '\n' && ch != EOF);
591 722
592 logfile->lastpartial = logfile->partial; 723 logfile->lastpartial = logfile->partial;
593 /* there are 3 ways we could have exited the loop: reading '\n', 724 /* 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 725 * reaching EOF, or filling the buffer; the 2nd and 3rd of these
595 * both result in a partial line */ 726 * both result in a partial line */
596 logfile->partial = ch != '\n'; 727 logfile->partial = ch != '\n';
597 728
598 if (logfile->partial && opt_whole) 729 if (logfile->partial && opt_whole)
599 return 0; 730 return 0;
600 731
601#if HAS_REGEX 732#if HAS_REGEX
602 transform_line (logfile->buf); 733 transform_line (logfile->buf);
630 else 761 else
631 file->inode = stats.st_ino; 762 file->inode = stats.st_ino;
632 763
633 if (opt_noinitial) 764 if (opt_noinitial)
634 fseek (file->fp, 0, SEEK_END); 765 fseek (file->fp, 0, SEEK_END);
635 else if (stats.st_size > (listlen + 1) * width) 766 else /* if (stats.st_size > (listlen + 1) * width)
767 * HACK - 'width' is in pixels - how are we to know how much text will fit?
636 fseek (file->fp, -((listlen + 2) * width), SEEK_END); 768 * fseek (file->fp, -((listlen + 2) * width/10), SEEK_END); */
769 fseek (file->fp, -5000, SEEK_END);
637 770
638 file->last_size = stats.st_size; 771 file->last_size = stats.st_size;
639 return file->fp; 772 return file->fp;
640} 773}
641 774
697 } 830 }
698 } 831 }
699} 832}
700 833
701/* 834/*
702 * insert a single physical line (that must be short enough to fit) 835 * 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 836 * pointer to the new node.
704 * MUST then fill in lines[idx] with valid data. 837 * the caller MUST then fill in ret->line and ret->len with valid
838 * data.
839 */
840static struct line_node *
841new_line_node (struct logfile_entry *log)
842{
843 struct line_node *new = xmalloc (sizeof (struct line_node));
844
845 new->logfile = log;
846 new->wrapped_left = 0;
847 new->wrapped_right = 0;
848 new->breaks = 0;
849
850 assert(log);
851
852 if (!log || !log->last)
853 {
854 new->next = linelist;
855 new->next->prev = new;
856
857 new->prev = NULL;
858 linelist = new;
859 }
860 else
861 {
862 /* 2 pointers from the new node */
863 new->next = log->last;
864 new->prev = log->last->prev;
865
866 /* 2 pointers back to the new node */
867 if (new->next) new->next->prev = new;
868 if (new->prev) new->prev->next = new;
869
870 /* if this is a new first entry in the list then update
871 * 'linelist' */
872 if (log->last == linelist)
873 linelist = new;
874 }
875
876 /* update the logfile record */
877 if (log)
878 log->last = new;
879
880 return new;
881}
882
883/*
884 * this is called after either adding a new line or appending to an
885 * old one. in both cases it's possible that the line no longer fits,
886 * and needs wrapping. this function checks the last line associated
887 * with the supplied logfile.
705 */ 888 */
706static void 889static void
707insert_line (int idx) 890possibly_split_long_line (struct logfile_entry *log)
708{ 891{
709 int cur_line; 892 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); 893 int l = strlen (str);
751 int last_wrapped = 0;
752 const char *p = str; 894 char *p = str;
753 static int continuation_width = -1; 895 struct line_node *line;
754 static int continuation_length; 896 int spaces;
897 static struct breakinfo *breaks;
898 static int break_buffer_size;
755 899
756 /* only calculate the continuation's width once */ 900 /* only calculate the continuation's width once */
757 if (continuation_width == -1) 901 if (continuation_width == -1)
758 { 902 {
759 continuation_length = strlen (continuation); 903 continuation_length = strlen (continuation);
760 continuation_width = XmbTextEscapement (fontset, continuation, continuation_length); 904 continuation_width = XmbTextEscapement (log->fontset, continuation, continuation_length);
905 continuation_color = GetColor (cont_color);
906
907 /* make an array to store information about the location of
908 * spaces in the line */
909 if (opt_justify)
910 {
911 break_buffer_size = 32;
912 breaks = xmalloc (break_buffer_size * sizeof (struct breakinfo));
913 }
761 } 914 }
762 915
763 do 916 do
764 { 917 {
765 const char *beg = p; 918 const char *beg = p;
766 int w = last_wrapped ? continuation_width : 0; 919 int start_w = log->last->wrapped_left ? continuation_width : 0;
920 int w = start_w;
767 int wrapped = 0; 921 int wrapped = 0;
768 const char *break_p = NULL; 922 char *break_p = NULL;
923 int width_at_break_p = 0;
924 spaces = 0;
925
926 if (opt_justify)
927 breaks[spaces].index = breaks[spaces].width = 0;
769 928
770 while (*p) 929 while (*p)
771 { 930 {
772 int cw, len; 931 int cw, len;
773 932
774 /* find the length in bytes of the next multibyte character */ 933 /* find the length in bytes of the next multibyte character */
775 len = mblen (p, l); 934 len = mblen (p, l);
776 if (len <= 0) 935 if (len <= 0)
777 len = 1; /* ignore (don't skip) illegal character sequences */ 936 len = 1; /* ignore (don't skip) illegal character sequences */
778 937
779 /* find the width in pixels of the next character */ 938 /* find the width in pixels of the next character */
780 cw = XmbTextEscapement (fontset, p, len); 939 cw = XmbTextEscapement (log->fontset, p, len);
940 if (opt_wordwrap && len == 1 && p[0] == ' ' && p != break_p + 1)
941 {
942 break_p = p;
943 width_at_break_p = w;
944 spaces++;
945
946 if (opt_justify)
947 {
948 /* increase the size of the 'breaks' array when
949 * necessary */
950 if (spaces >= break_buffer_size)
951 {
952 break_buffer_size *= 1.5;
953 breaks = xrealloc (breaks, break_buffer_size * sizeof (struct breakinfo));
954 }
955
956 /* store information about (a) the location of each
957 * space */
958 breaks[spaces].index = p + 1 - beg;
959 /* (b) the width (in pixels) of the string up to
960 * this space */
961 breaks[spaces].width = cw + w - start_w;
962 /* (c) the length of each 'word' */
963 breaks[spaces-1].len = breaks[spaces].index - breaks[spaces-1].index;
964 }
965 }
966
781 if (cw + w > width - effect_x_space) 967 if (cw + w > width - effect_x_space)
782 { 968 {
783 if (p == beg) 969 if (p == beg)
784 { 970 {
785 fprintf (stderr, "we can't even fit a single character onto the line\n"); 971 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); 972 if (len == 1) fprintf (stderr, "(the character we couldn't fit was '%c')\n", *p);
787 exit (1); 973 exit (1);
788 } 974 }
789 975
790 wrapped = 1; 976 wrapped = 1;
791 break; 977 break;
792 } 978 }
793
794 if (opt_wordwrap && len == 1 && p[0] == ' ')
795 break_p = p;
796 979
797 w += cw; 980 w += cw;
798 p += len; 981 p += len;
799 l -= len; 982 l -= len;
800 } 983 }
801 984
802 /* if we're wrapping at spaces, and the line is long enough to 985 /* 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 986 * 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 */ 987 * the first character on the line, then wrap at the space */
988 if (!wrapped)
989 break;
990
991 int prefix_len;
992
993 /* choose where to break the line */
805 if (opt_wordwrap && wrapped && break_p && break_p != beg) 994 if (opt_wordwrap && break_p && break_p != beg)
806 {
807 l += p - break_p;
808 p = break_p;
809 }
810
811 { 995 {
812 int len = p - beg + (last_wrapped ? continuation_length : 0); 996 prefix_len = break_p - beg;
813 char *s = xmalloc (len + 1); 997 p = break_p;
814 if (last_wrapped) 998 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 999
822 s[len] = 0; 1000 /* if breaking at a space, skip all adjacent spaces */
823 insert_line (idx); 1001 while (*p == ' ')
824 lines[idx].line = s; 1002 {
825 lines[idx].len = len; 1003 int len = mblen (p, l);
826 lines[idx].color = color; 1004 if (len != 1) break;
1005 p++;
1006 }
1007
1008 if (opt_justify)
1009 {
1010 spaces--;
1011 breaks[spaces].len--;
1012 }
827 } 1013 }
1014 else
1015 prefix_len = p - beg;
828 1016
829 /* if we wrapped at a space, don't display the space */ 1017 /* make a copy of the tail end of the string */
830 if (opt_wordwrap && wrapped && break_p && break_p != beg) 1018 p = xstrdup (p);
831 {
832 l--;
833 p++;
834 }
835 1019
836 last_wrapped = wrapped; 1020 /* and reduce the size of the head of the string */
1021 log->last->line = xrealloc (log->last->line, prefix_len + 1);
1022 log->last->len = prefix_len;
1023 log->last->line[prefix_len] = '\0';
1024
1025 /* note that the head was wrapped on it's right */
1026 log->last->wrapped_right = 1;
1027
1028 /* 'spaces' includes any space we broke on; we can only justify
1029 * if there's at least one other space */
1030 if (opt_justify && spaces &&
1031 width - effect_x_space - width_at_break_p < spaces * log->font_height)
1032 {
1033 int i;
1034 log->last->free_pixels = width - effect_x_space - w;
1035 log->last->num_words = spaces + 1;
1036 log->last->breaks = malloc (log->last->num_words * sizeof (struct breakinfo));
1037 for (i = 0; i < log->last->num_words; i++)
1038 log->last->breaks[i] = breaks[i];
1039 }
1040
1041 line = new_line_node (log);
1042 line->line = p;
1043 l = line->len = strlen (p);
1044
1045 /* note that the tail end of the string is wrapped at its left */
1046 line->wrapped_left = 1;
837 } 1047 }
838 while (l); 1048 while (l);
1049}
1050
1051static void
1052insert_new_line (char *str, struct logfile_entry *log)
1053{
1054 struct line_node *new;
1055 new = new_line_node (log);
1056 new->line = str;
1057 new->len = strlen (str);
1058
1059 possibly_split_long_line (log);
839} 1060}
840 1061
841/* 1062/*
842 * append something to an existing physical line. this is done 1063 * append something to an existing physical line. this is done
843 * by deleting the file on-screen, concatenating the new data to it 1064 * by deleting the file on-screen, concatenating the new data to it
844 * and splitting it again. 1065 * and splitting it again.
845 */ 1066 */
846static void 1067static void
847append_line (int idx, const char *str) 1068append_to_existing_line (char *str, struct logfile_entry *log)
848{ 1069{
849 unsigned long color = lines[idx].color; 1070 char *old, *new;
850 char *old = lines[idx].line; 1071
1072 assert(log);
1073 assert(log->last);
1074
1075 old = log->last->line;
1076 assert(old);
1077
851 char *new = concat_line (old, str); 1078 new = concat_line (old, str);
852
853 free (old); 1079 free (str);
854 1080 log->last->line = new;
855 delete_line (idx); 1081 log->last->len = strlen (new);
856 split_line (idx, new, color); 1082 possibly_split_long_line (log);
857} 1083}
858 1084
859static void 1085static void
860main_loop (void) 1086main_loop (void)
861{ 1087{
865 XEvent xev; 1091 XEvent xev;
866 struct logfile_entry *lastprinted = NULL; 1092 struct logfile_entry *lastprinted = NULL;
867 struct logfile_entry *current; 1093 struct logfile_entry *current;
868 int need_update = 1; 1094 int need_update = 1;
869 1095
870 lines = xmalloc (sizeof (struct linematrix) * listlen);
871 display = xmalloc (sizeof (struct displaymatrix) * listlen); 1096 display = xmalloc (sizeof (struct displaymatrix) * listlen);
872 1097
873 lastreload = time (NULL); 1098 lastreload = time (NULL);
874 1099
875 /* Initialize linematrix */ 1100 /* Initialize line_node */
876 for (lin = 0; lin < listlen; lin++) 1101 for (lin = 0; lin < listlen; lin++)
877 { 1102 {
1103 struct line_node *e = xmalloc (sizeof (struct line_node));
878 lines[lin].line = xstrdup ("~"); 1104 e->line = xstrdup ("~");
879 lines[lin].len = 1; 1105 e->len = 1;
1106 e->logfile = loglist; /* this is only needed to get a color for the '~' */
1107 e->wrapped_left = 0;
1108 e->wrapped_right = 0;
1109 e->breaks = 0;
1110 e->next = NULL;
1111 e->prev = linelist_tail;
1112
1113 if (!linelist)
1114 linelist = e;
1115 if (linelist_tail)
1116 linelist_tail->next = e;
1117 linelist_tail = e;
1118
880 display[lin].line = xstrdup(""); 1119 display[lin].line = xstrdup ("");
881 display[lin].len = 0; 1120 display[lin].len = 0;
1121 display[lin].offset = 0;
882 display[lin].buffer_size = 0; 1122 display[lin].buffer_size = 0;
883 lines[lin].color = GetColor (def_color);
884 } 1123 }
885 1124
886 for (;;) 1125 for (;;)
887 { 1126 {
888 /* read logs */ 1127 /* read logs */
899 /* if we're trying to update old partial lines in 1138 /* if we're trying to update old partial lines in
900 * place, and the last time this file was updated the 1139 * place, and the last time this file was updated the
901 * output was partial, and that partial line is not 1140 * output was partial, and that partial line is not
902 * too close to the top of the screen, then update 1141 * too close to the top of the screen, then update
903 * that partial line */ 1142 * that partial line */
904 if (opt_update && current->lastpartial && current->index >= 0) 1143 if (opt_update && current->lastpartial && current->last)
905 { 1144 {
906 int idx = current->index;
907 append_line (idx, current->buf); 1145 append_to_existing_line (current->buf, current);
908 current->index = idx; 1146 current->buf = 0;
1147 continue;
1148 }
1149
1150 /* if all we just read was a newline ending a line that we've already displayed, skip it */
1151 if (current->buf[0] == '\0' && current->lastpartial)
1152 {
909 free (current->buf), current->buf = 0; 1153 free(current->buf);
1154 current->buf = 0;
910 continue; 1155 continue;
911 } 1156 }
912 1157
913 /* print filename if any, and if last line was from 1158 /* print filename if any, and if last line was from
914 * different file */ 1159 * different file */
915 if (!opt_nofilename && lastprinted != current && current->desc[0]) 1160 if (!opt_nofilename && lastprinted != current && current->desc[0])
916 { 1161 {
917 split_line (listlen - 1, "[", current->color); 1162 current->last = 0;
918 append_line (listlen - 1, current->desc); 1163 insert_new_line (xstrdup ("["), current);
919 append_line (listlen - 1, "]"); 1164 append_to_existing_line (xstrdup (current->desc), current);
1165 append_to_existing_line (xstrdup ("]"), current);
920 } 1166 }
921 1167
922 /* if we're dealing with partial lines, and the last 1168 /* if we're dealing with partial lines, and the last
923 * time we showed the line it wasn't finished ... */ 1169 * time we showed the line it wasn't finished ... */
924 if (!opt_whole && current->lastpartial) 1170 if (!opt_whole && current->lastpartial)
925 { 1171 {
926 /* if this is the same file we showed last then 1172 /* if this is the same file we showed last then
927 append to the last line shown */ 1173 append to the last line shown */
928 if (lastprinted == current) 1174 if (lastprinted == current)
929 append_line (listlen - 1, current->buf); 1175 append_to_existing_line (current->buf, current);
930 else 1176 else
931 { 1177 {
932 /* but if a different file has been shown in the 1178 /* but if a different file has been shown in the
933 * mean time, make a new line, starting with the 1179 * mean time, make a new line, starting with the
934 * continuation string */ 1180 * continuation string */
935 split_line (listlen - 1, continuation, current->color); 1181 insert_new_line (current->buf, current);
936 append_line (listlen - 1, current->buf); 1182 current->last->wrapped_left = 1;
937 } 1183 }
938 } 1184 }
939 else 1185 else
940 /* otherwise just make a plain and simple new line */ 1186 /* otherwise just make a plain and simple new line */
941 split_line (listlen - 1, current->buf, current->color); 1187 insert_new_line (current->buf, current);
942 1188
943 free (current->buf), current->buf = 0; 1189 current->buf = 0;
944 current->index = listlen - 1;
945 lastprinted = current; 1190 lastprinted = current;
946 } 1191 }
947 } 1192 }
948 1193
949 if (need_update) 1194 if (need_update)
1060 XParseGeometry (argv[++i], &win_x, &win_y, &width, &height); 1305 XParseGeometry (argv[++i], &win_x, &win_y, &width, &height);
1061 else if (!strcmp (arg, "-display")) 1306 else if (!strcmp (arg, "-display"))
1062 dispname = argv[++i]; 1307 dispname = argv[++i];
1063 else if (!strcmp (arg, "-cont")) 1308 else if (!strcmp (arg, "-cont"))
1064 continuation = argv[++i]; 1309 continuation = argv[++i];
1310 else if (!strcmp (arg, "-cont-color"))
1311 cont_color = argv[++i];
1065 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) 1312 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
1066 fontname = argv[++i]; 1313 fontname = argv[++i];
1067#if HAS_REGEX 1314#if HAS_REGEX
1068 else if (!strcmp (arg, "-t")) 1315 else if (!strcmp (arg, "-t"))
1069 { 1316 {
1070 transform = argv[++i]; 1317 transform = argv[++i];
1071 transform_to = argv[++i]; 1318 transform_to = argv[++i];
1072 printf("transform: '%s' to '%s'\n", transform, transform_to); 1319 printf ("transform: '%s' to '%s'\n", transform, transform_to);
1073 } 1320 }
1074#endif 1321#endif
1075 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) 1322 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
1076 opt_daemonize = 1; 1323 opt_daemonize = 1;
1077 else if (!strcmp (arg, "-reload")) 1324 else if (!strcmp (arg, "-reload"))
1078 { 1325 {
1097 opt_partial = 1; 1344 opt_partial = 1;
1098 else if (!strcmp (arg, "-update")) 1345 else if (!strcmp (arg, "-update"))
1099 opt_update = opt_partial = 1; 1346 opt_update = opt_partial = 1;
1100 else if (!strcmp (arg, "-wordwrap")) 1347 else if (!strcmp (arg, "-wordwrap"))
1101 opt_wordwrap = 1; 1348 opt_wordwrap = 1;
1349 else if (!strcmp (arg, "-justify"))
1350 opt_justify = 1;
1102 else if (!strcmp (arg, "-color")) 1351 else if (!strcmp (arg, "-color"))
1103 def_color = argv[++i]; 1352 def_color = argv[++i];
1104 else if (!strcmp (arg, "-noinitial")) 1353 else if (!strcmp (arg, "-noinitial"))
1105 opt_noinitial = 1; 1354 opt_noinitial = 1;
1106 else if (!strcmp (arg, "-id")) 1355 else if (!strcmp (arg, "-id"))
1142 } 1391 }
1143 1392
1144 e = xmalloc (sizeof (struct logfile_entry)); 1393 e = xmalloc (sizeof (struct logfile_entry));
1145 e->partial = 0; 1394 e->partial = 0;
1146 e->buf = 0; 1395 e->buf = 0;
1147 e->index = -1;
1148 1396
1149 if (arg[0] == '-' && arg[1] == '\0') 1397 if (arg[0] == '-' && arg[1] == '\0')
1150 { 1398 {
1151 if ((e->fp = fdopen (0, "r")) == NULL) 1399 if ((e->fp = fdopen (0, "r")) == NULL)
1152 perror ("fdopen"), exit (1); 1400 perror ("fdopen"), exit (1);
1167 e->desc = xstrdup (desc); 1415 e->desc = xstrdup (desc);
1168 } 1416 }
1169 1417
1170 e->color = GetColor (fcolor); 1418 e->color = GetColor (fcolor);
1171 e->partial = 0; 1419 e->partial = 0;
1420 e->fontname = fontname;
1421 e->last = NULL;
1172 e->next = NULL; 1422 e->next = NULL;
1173 1423
1174 if (!loglist) 1424 if (!loglist)
1175 loglist = e; 1425 loglist = e;
1176 if (loglist_tail) 1426 if (loglist_tail)
1195 else if (opt_partial && opt_whole) 1445 else if (opt_partial && opt_whole)
1196 { 1446 {
1197 fprintf (stderr, "Specify at most one of -partial and -whole\n"); 1447 fprintf (stderr, "Specify at most one of -partial and -whole\n");
1198 exit (1); 1448 exit (1);
1199 } 1449 }
1450
1451 /* it doesn't make sense to justify if word wrap isn't on */
1452 if (opt_justify)
1453 opt_wordwrap = 1;
1200 1454
1201 /* HACK-7: do we want to allow both -shade and -outline? */ 1455 /* HACK-7: do we want to allow both -shade and -outline? */
1202 if (opt_shade && opt_outline) 1456 if (opt_shade && opt_outline)
1203 { 1457 {
1204 fprintf (stderr, "Specify at most one of -shade and -outline\n"); 1458 fprintf (stderr, "Specify at most one of -shade and -outline\n");
1215#if HAS_REGEX 1469#if HAS_REGEX
1216 if (transform) 1470 if (transform)
1217 { 1471 {
1218 int i; 1472 int i;
1219 1473
1220 printf("compiling regexp '%s'\n", transform); 1474 printf ("compiling regexp '%s'\n", transform);
1221 transformre = xmalloc (sizeof (regex_t)); 1475 transformre = xmalloc (sizeof (regex_t));
1222 i = regcomp (transformre, transform, REG_EXTENDED); 1476 i = regcomp (transformre, transform, REG_EXTENDED);
1223 if (i != 0) 1477 if (i != 0)
1224 { 1478 {
1225 char buf[512]; 1479 char buf[512];
1226 1480
1227 regerror (i, transformre, buf, sizeof (buf)); 1481 regerror (i, transformre, buf, sizeof (buf));
1228 fprintf (stderr, "Cannot compile regular expression: %s\n", buf); 1482 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1229 } 1483 }
1230 else 1484 else
1231 {
1232 printf("compiled '%s' OK to %x\n", transform, (int)transformre); 1485 printf ("compiled '%s' OK to %x\n", transform, (int)transformre);
1233 }
1234 } 1486 }
1235#endif 1487#endif
1236 1488
1237 InitWindow (); 1489 InitWindow ();
1238 1490
1259 action.sa_handler = handler; 1511 action.sa_handler = handler;
1260 sigemptyset (&action.sa_mask); 1512 sigemptyset (&action.sa_mask);
1261 action.sa_flags = SA_RESTART; 1513 action.sa_flags = SA_RESTART;
1262 1514
1263 if (sigaction (sig, &action, NULL) < 0) 1515 if (sigaction (sig, &action, NULL) < 0)
1264 fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1); 1516 fprintf (stderr, "sigaction (%d): %s\n", sig, strerror (errno)), exit (1);
1265} 1517}
1266 1518
1267void * 1519void *
1268xstrdup (const char *string) 1520xstrdup (const char *string)
1269{ 1521{
1270 void *p; 1522 void *p;
1271 1523
1272 while ((p = strdup (string)) == NULL) 1524 while ((p = strdup (string)) == NULL)
1273 { 1525 {
1274 fprintf (stderr, "Memory exhausted in xstrdup().\n"); 1526 fprintf (stderr, "Memory exhausted in xstrdup ().\n");
1275 sleep (10); 1527 sleep (10);
1276 } 1528 }
1277 1529
1278 return p; 1530 return p;
1279} 1531}
1283{ 1535{
1284 void *p; 1536 void *p;
1285 1537
1286 while ((p = malloc (size)) == NULL) 1538 while ((p = malloc (size)) == NULL)
1287 { 1539 {
1288 fprintf (stderr, "Memory exhausted in xmalloc().\n"); 1540 fprintf (stderr, "Memory exhausted in xmalloc ().\n");
1289 sleep (10); 1541 sleep (10);
1290 } 1542 }
1291 1543
1292 return p; 1544 return p;
1293} 1545}
1297{ 1549{
1298 void *p; 1550 void *p;
1299 1551
1300 while ((p = realloc (ptr, size)) == NULL) 1552 while ((p = realloc (ptr, size)) == NULL)
1301 { 1553 {
1302 fprintf (stderr, "Memory exhausted in xrealloc().\n"); 1554 fprintf (stderr, "Memory exhausted in xrealloc ().\n");
1303 sleep (10); 1555 sleep (10);
1304 } 1556 }
1305 1557
1306 return p; 1558 return p;
1307} 1559}
1308 1560
1309void 1561void
1310display_help (char *myname) 1562display_help (char *myname)
1311{ 1563{
1312 printf ("Usage: %s [options] file1[,color[,desc]] " 1564 printf ("Usage: %s [options] file1[,color[,desc]]"
1313 "[file2[,color[,desc]] ...]\n", myname); 1565 "[options] [file2[,color[,desc]] ...]\n", myname);
1314 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n" 1566 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1315 " -color color use color $color as default\n" 1567 " -color color use color $color as default\n"
1316 " -reload sec command reload after $sec and run command\n" 1568 " -reload sec command reload after $sec and run command\n"
1317 " -id id window id to use instead of the root window\n" 1569 " -id id window id to use instead of the root window\n"
1318 " -font FONTSPEC (-fn) font to use\n" 1570 " -font FONTSPEC (-fn) font to use\n"
1353 case -1: 1605 case -1:
1354 return -1; 1606 return -1;
1355 case 0: 1607 case 0:
1356 break; 1608 break;
1357 default: 1609 default:
1358 /*printf("%d\n", pid);*/ 1610 /*printf ("%d\n", pid);*/
1359 exit (0); 1611 exit (0);
1360 } 1612 }
1361 1613
1362 if (setsid () == -1) 1614 if (setsid () == -1)
1363 return -1; 1615 return -1;
1364 1616
1365 return 0; 1617 return 0;
1366} 1618}
1619
1620/* todo - get reverse display working again */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines