1 | /* |
1 | /* |
2 | * Copyright 2001 by Marco d'Itri <md@linux.it> |
2 | * Copyright 2001 by Marco d'Itri <md@linux.it> |
3 | * |
3 | * Copyright 2000,2001,2002,2003,2004 |
4 | * Original version by Mike Baker, then maintained by pcg@goof.com. |
4 | * Marc Lehmann <pcg@goof.com>, |
|
|
5 | * and many others, see README |
|
|
6 | * |
|
|
7 | * Original version by Mike Baker. |
5 | * |
8 | * |
6 | * This program is free software; you can redistribute it and/or modify |
9 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
10 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
11 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
12 | * (at your option) any later version. |
… | |
… | |
31 | #include <sys/stat.h> |
34 | #include <sys/stat.h> |
32 | #include <sys/types.h> |
35 | #include <sys/types.h> |
33 | #include <locale.h> |
36 | #include <locale.h> |
34 | #include <ctype.h> |
37 | #include <ctype.h> |
35 | #include <stdarg.h> |
38 | #include <stdarg.h> |
|
|
39 | #include <X11/Xlib.h> |
|
|
40 | #include <X11/Xatom.h> |
|
|
41 | #include <X11/Xutil.h> |
|
|
42 | |
36 | #if HAS_REGEX |
43 | #if HAS_REGEX |
37 | #include <regex.h> |
44 | #include <regex.h> |
38 | #endif |
45 | #endif |
39 | #include <X11/Xlib.h> |
46 | |
40 | #include <X11/Xatom.h> |
47 | #define SHADE_X 2 |
41 | #include <X11/Xutil.h> |
48 | #define SHADE_Y 2 |
42 | |
49 | |
43 | /* data structures */ |
50 | /* data structures */ |
44 | struct logfile_entry |
51 | struct logfile_entry |
45 | { |
52 | { |
46 | struct logfile_entry *next; |
53 | struct logfile_entry *next; |
… | |
… | |
53 | off_t last_size; /* file size at the last check */ |
60 | off_t last_size; /* file size at the last check */ |
54 | unsigned long color; /* color to be used for printing */ |
61 | unsigned long color; /* color to be used for printing */ |
55 | int partial; /* true if the last line isn't complete */ |
62 | int partial; /* true if the last line isn't complete */ |
56 | int lastpartial; /* true if the previous output wasn't complete */ |
63 | int lastpartial; /* true if the previous output wasn't complete */ |
57 | int index; /* index into linematrix of a partial line */ |
64 | int index; /* index into linematrix of a partial line */ |
|
|
65 | int modified; /* true if line is modified & needs displaying */ |
58 | }; |
66 | }; |
59 | |
67 | |
60 | struct linematrix |
68 | struct linematrix |
61 | { |
69 | { |
62 | char *line; |
70 | char *line; |
63 | int len; |
71 | int len; |
64 | unsigned long color; |
72 | unsigned long color; |
65 | }; |
73 | }; |
66 | |
74 | |
|
|
75 | struct displaymatrix |
|
|
76 | { |
|
|
77 | char *line; |
|
|
78 | int len; |
|
|
79 | int buffer_size; |
|
|
80 | }; |
|
|
81 | |
67 | /* global variables */ |
82 | /* global variables */ |
68 | struct linematrix *lines; |
83 | struct linematrix *lines; |
|
|
84 | struct displaymatrix *display; |
69 | int width = STD_WIDTH, height = STD_HEIGHT, listlen; |
85 | int width = STD_WIDTH, height = STD_HEIGHT, listlen; |
70 | int win_x = LOC_X, win_y = LOC_Y; |
86 | int win_x = LOC_X, win_y = LOC_Y; |
71 | int font_ascent, font_height; |
87 | int font_ascent, font_height; |
|
|
88 | int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */ |
|
|
89 | int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */ |
72 | int do_reopen; |
90 | int do_reopen; |
73 | struct timeval interval = { 2, 400000 }; |
91 | struct timeval interval = { 2, 400000 }; |
74 | XFontSet fontset; |
92 | XFontSet fontset; |
75 | |
93 | |
76 | /* command line options */ |
94 | /* command line options */ |
… | |
… | |
93 | regex_t from; |
111 | regex_t from; |
94 | const char *to; |
112 | const char *to; |
95 | struct re_list *next; |
113 | struct re_list *next; |
96 | }; |
114 | }; |
97 | struct re_list *re_head, *re_tail; |
115 | struct re_list *re_head, *re_tail; |
|
|
116 | char *transform_to = NULL; |
|
|
117 | regex_t *transformre; |
98 | #endif |
118 | #endif |
99 | |
119 | |
100 | |
120 | |
101 | /* prototypes */ |
121 | /* prototypes */ |
102 | void list_files (int); |
122 | void list_files (int); |
… | |
… | |
104 | void force_refresh (int); |
124 | void force_refresh (int); |
105 | void blank_window (int); |
125 | void blank_window (int); |
106 | |
126 | |
107 | void InitWindow (void); |
127 | void InitWindow (void); |
108 | unsigned long GetColor (const char *); |
128 | unsigned long GetColor (const char *); |
109 | void redraw (void); |
129 | void redraw (int); |
110 | void refresh (int, int, int); |
130 | void refresh (int, int, int, int); |
111 | |
131 | |
112 | void transform_line (char *s); |
132 | void transform_line (char *s); |
113 | int lineinput (struct logfile_entry *); |
133 | int lineinput (struct logfile_entry *); |
114 | void reopen (void); |
134 | void reopen (void); |
115 | void check_open_files (void); |
135 | void check_open_files (void); |
… | |
… | |
119 | void display_version (void); |
139 | void display_version (void); |
120 | void display_help (char *); |
140 | void display_help (char *); |
121 | void install_signal (int, void (*)(int)); |
141 | void install_signal (int, void (*)(int)); |
122 | void *xstrdup (const char *); |
142 | void *xstrdup (const char *); |
123 | void *xmalloc (size_t); |
143 | void *xmalloc (size_t); |
|
|
144 | void *xrealloc (void *, size_t); |
124 | int daemonize (void); |
145 | int daemonize (void); |
125 | |
146 | |
126 | /* signal handlers */ |
147 | /* signal handlers */ |
127 | void |
148 | void |
128 | list_files (int dummy) |
149 | list_files (int dummy) |
… | |
… | |
141 | } |
162 | } |
142 | |
163 | |
143 | void |
164 | void |
144 | force_refresh (int dummy) |
165 | force_refresh (int dummy) |
145 | { |
166 | { |
146 | redraw (); |
167 | redraw (1); |
147 | } |
168 | } |
148 | |
169 | |
149 | void |
170 | void |
150 | blank_window (int dummy) |
171 | blank_window (int dummy) |
151 | { |
172 | { |
… | |
… | |
294 | if (geom_mask & XNegative) |
315 | if (geom_mask & XNegative) |
295 | win_x = win_x + ScreenWidth - width; |
316 | win_x = win_x + ScreenWidth - width; |
296 | if (geom_mask & YNegative) |
317 | if (geom_mask & YNegative) |
297 | win_y = win_y + ScreenHeight - height; |
318 | win_y = win_y + ScreenHeight - height; |
298 | |
319 | |
|
|
320 | if (opt_outline) |
|
|
321 | { |
|
|
322 | /* adding outline increases the total width and height by 2 |
|
|
323 | pixels each, and offsets the text one pixel right and one |
|
|
324 | pixel down */ |
|
|
325 | effect_x_space = effect_y_space = 2; |
|
|
326 | effect_x_offset = effect_y_offset = 1; |
|
|
327 | } |
|
|
328 | else if (opt_shade) |
|
|
329 | { |
|
|
330 | /* adding a shadow increases the space used */ |
|
|
331 | effect_x_space = abs(SHADE_X); |
|
|
332 | effect_y_space = abs(SHADE_Y); |
|
|
333 | /* if the shadow is to the right and below then we don't need |
|
|
334 | * to move the text to make space for it, but shadows to the left |
|
|
335 | * and above need accomodating */ |
|
|
336 | effect_x_offset = SHADE_X > 0 ? 0 : -SHADE_X; |
|
|
337 | effect_y_offset = SHADE_Y > 0 ? 0 : -SHADE_Y; |
|
|
338 | } |
|
|
339 | else |
|
|
340 | { |
|
|
341 | effect_x_space = effect_y_space = 0; |
|
|
342 | effect_x_offset = effect_y_offset = 0; |
|
|
343 | } |
|
|
344 | |
|
|
345 | /* if we are using -shade or -outline, there will be less usable |
|
|
346 | * space for output */ |
299 | listlen = height / font_height; |
347 | listlen = (height - effect_y_space) / font_height; |
300 | |
348 | |
301 | if (!listlen) |
349 | if (!listlen) |
302 | { |
350 | { |
303 | fprintf (stderr, "height too small for a single line, setting to %d\n", |
351 | fprintf (stderr, "height too small for a single line, setting to %d\n", |
304 | font_height); |
352 | font_height); |
305 | listlen = 1; |
353 | listlen = 1; |
306 | } |
354 | } |
307 | |
355 | |
308 | height = listlen * font_height; |
356 | /* leave the height how the user requested it. it might not all be |
309 | |
357 | * used, but this will allow the geometry to be tuned more accurately |
|
|
358 | * (with the -frame option) |
|
|
359 | * the old code did this: |
|
|
360 | * height = listlen * font_height + effect_y_space; */ |
|
|
361 | |
310 | XSelectInput (disp, root, ExposureMask | FocusChangeMask); |
362 | XSelectInput (disp, root, ExposureMask | FocusChangeMask); |
311 | } |
363 | } |
312 | |
364 | |
313 | /* |
365 | /* |
314 | * redraw does a complete redraw, rather than an update (i.e. the area |
366 | * if redraw() is passwd a non-zero argument, it does a complete |
315 | * gets cleared first) |
367 | * redraw, rather than an update. if the argument is zero (and |
|
|
368 | * -noflicker is in effect) then only the lines which have changed |
|
|
369 | * since the last draw are redrawn. |
|
|
370 | * |
316 | * the rest is handled by regular refresh()'es |
371 | * the rest is handled by regular refresh()'es |
317 | */ |
372 | */ |
318 | void |
373 | void |
319 | redraw (void) |
374 | redraw (int redraw_all) |
320 | { |
375 | { |
321 | XSetClipMask (disp, WinGC, None); |
376 | XSetClipMask (disp, WinGC, None); |
322 | refresh (0, 32768, 1); |
377 | refresh (0, 32768, 1, redraw_all); |
323 | } |
378 | } |
324 | |
379 | |
325 | /* Just redraw everything without clearing (i.e. after an EXPOSE event) */ |
380 | /* Just redraw everything without clearing (i.e. after an EXPOSE event) */ |
326 | void |
381 | void |
327 | refresh (int miny, int maxy, int clear) |
382 | refresh (int miny, int maxy, int clear, int refresh_all) |
328 | { |
383 | { |
329 | int lin; |
384 | int lin; |
330 | int offset = (listlen + 1) * font_height; |
385 | int offset = listlen * font_height + font_ascent + effect_y_offset; |
331 | unsigned long black_color = GetColor ("black"); |
386 | unsigned long black_color = GetColor ("black"); |
332 | |
387 | |
333 | miny -= win_y + font_height; |
388 | miny -= win_y + font_height; |
334 | maxy -= win_y - font_height; |
389 | maxy -= win_y - font_height; |
335 | |
390 | |
… | |
… | |
337 | XClearArea (disp, root, win_x, win_y, width, height, False); |
392 | XClearArea (disp, root, win_x, win_y, width, height, False); |
338 | |
393 | |
339 | for (lin = listlen; lin--;) |
394 | for (lin = listlen; lin--;) |
340 | { |
395 | { |
341 | struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin); |
396 | struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin); |
|
|
397 | struct displaymatrix *display_line = display + lin; |
342 | |
398 | |
343 | offset -= font_height; |
399 | offset -= font_height; |
344 | |
400 | |
345 | if (offset < miny || offset > maxy) |
401 | if (offset < miny || offset > maxy) |
346 | continue; |
402 | continue; |
347 | |
403 | |
|
|
404 | /* if this line is a different than it was, then it |
|
|
405 | * needs displaying */ |
348 | if (clear && opt_noflicker) |
406 | if (!opt_noflicker |
349 | XClearArea (disp, root, win_x, win_y + offset - font_ascent, width, font_height, False); |
407 | || refresh_all |
350 | |
408 | || display_line->len != line->len |
351 | if (opt_outline) |
409 | || memcmp (display_line->line, line->line, line->len)) |
352 | { |
410 | { |
|
|
411 | /* don't bother updating the record of what has been |
|
|
412 | * displayed if -noflicker isn't in effect, since we redraw |
|
|
413 | * the whole display every time anyway */ |
|
|
414 | if (opt_noflicker) |
|
|
415 | { |
|
|
416 | /* update the record of what has been displayed; |
|
|
417 | * first make sure the buffer is big enough */ |
|
|
418 | if (display_line->buffer_size <= line->len) |
|
|
419 | { |
|
|
420 | display_line->buffer_size = line->len + 1; |
|
|
421 | display_line->line = xrealloc (display_line->line, display_line->buffer_size); |
|
|
422 | } |
|
|
423 | |
|
|
424 | display_line->len = line->len; |
|
|
425 | memcpy (display_line->line, line->line, line->len); |
|
|
426 | |
|
|
427 | if (clear) |
|
|
428 | XClearArea (disp, root, win_x, win_y + offset - font_ascent, |
|
|
429 | width + effect_x_space, font_height + effect_y_space, False); |
|
|
430 | } |
|
|
431 | |
|
|
432 | if (opt_outline) |
|
|
433 | { |
|
|
434 | int x, y; |
353 | XSetForeground (disp, WinGC, black_color); |
435 | XSetForeground (disp, WinGC, black_color); |
|
|
436 | |
|
|
437 | for (x = -1; x < 2; x += 2) |
|
|
438 | for (y = -1; y < 2; y += 2) |
354 | XmbDrawString (disp, root, fontset, WinGC, win_x - 1, |
439 | XmbDrawString (disp, root, fontset, WinGC, |
|
|
440 | win_x + effect_x_offset + x, |
|
|
441 | win_y + y + offset, |
355 | win_y + offset + 1, line->line, line->len); |
442 | line->line, line->len); |
356 | XmbDrawString (disp, root, fontset, WinGC, win_x + 1, |
|
|
357 | win_y + offset + 1, line->line, line->len); |
|
|
358 | XmbDrawString (disp, root, fontset, WinGC, win_x - 1, |
|
|
359 | win_y + offset - 1, line->line, line->len); |
|
|
360 | XmbDrawString (disp, root, fontset, WinGC, win_x + 1, |
|
|
361 | win_y + offset - 1, line->line, line->len); |
|
|
362 | } |
443 | } |
363 | else if (opt_shade) |
444 | else if (opt_shade) |
364 | { |
445 | { |
365 | XSetForeground (disp, WinGC, black_color); |
446 | XSetForeground (disp, WinGC, black_color); |
366 | XmbDrawString (disp, root, fontset, WinGC, win_x + 2, |
447 | XmbDrawString (disp, root, fontset, WinGC, |
|
|
448 | win_x + effect_x_offset + SHADE_X, |
|
|
449 | win_y + offset + SHADE_Y, |
367 | win_y + offset + 2, line->line, line->len); |
450 | line->line, line->len); |
368 | } |
451 | } |
369 | |
452 | |
370 | XSetForeground (disp, WinGC, line->color); |
453 | XSetForeground (disp, WinGC, line->color); |
371 | XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset, |
454 | XmbDrawString (disp, root, fontset, WinGC, |
|
|
455 | win_x + effect_x_offset, |
|
|
456 | win_y + offset, |
372 | line->line, line->len); |
457 | line->line, line->len); |
|
|
458 | } |
373 | } |
459 | } |
374 | |
460 | |
375 | if (opt_frame) |
461 | if (opt_frame) |
376 | { |
462 | { |
377 | XSetForeground (disp, WinGC, GetColor (def_color)); |
463 | XSetForeground (disp, WinGC, GetColor (def_color)); |
378 | XDrawRectangle (disp, root, WinGC, win_x - 2, win_y - 2, width + 4, height + 4); |
464 | XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1); |
379 | } |
465 | } |
380 | } |
466 | } |
381 | |
467 | |
382 | #if HAS_REGEX |
468 | #if HAS_REGEX |
383 | void |
469 | void |
… | |
… | |
396 | if (transformre) |
482 | if (transformre) |
397 | { |
483 | { |
398 | int i; |
484 | int i; |
399 | regmatch_t matched[16]; |
485 | regmatch_t matched[16]; |
400 | |
486 | |
401 | i = regexec (&transformre, string, 16, matched, 0); |
487 | i = regexec (transformre, s, 16, matched, 0); |
402 | if (i == 0) |
488 | if (i == 0) |
403 | { /* matched */ |
489 | { /* matched */ |
|
|
490 | int match_start = matched[0].rm_so; |
|
|
491 | int match_end = matched[0].rm_eo; |
|
|
492 | int old_len = match_end - match_start; |
|
|
493 | int new_len = strlen(transform_to); |
|
|
494 | int old_whole_len = strlen(s); |
|
|
495 | printf("regexp was matched by '%s' - replace with '%s'\n", s, transform_to); |
|
|
496 | printf("match is from %d to %d\n", |
|
|
497 | match_start, match_end); |
|
|
498 | if (new_len > old_len) { |
|
|
499 | s = xrealloc(s, old_whole_len + new_len - old_len); |
|
|
500 | } |
|
|
501 | if (new_len != old_len) { |
|
|
502 | memcpy(s + match_end + new_len - old_len, |
|
|
503 | s + match_end, |
|
|
504 | old_whole_len - match_end); |
|
|
505 | s[old_whole_len + new_len - old_len] = '\0'; |
|
|
506 | } |
|
|
507 | memcpy(s + match_start, |
|
|
508 | transform_to, |
|
|
509 | new_len); |
|
|
510 | printf("transformed to '%s'\n", s); |
404 | } |
511 | } |
|
|
512 | else |
|
|
513 | { |
|
|
514 | printf("regexp was not matched by '%s'\n", s); |
|
|
515 | } |
405 | } |
516 | } |
406 | } |
517 | } |
407 | #endif |
518 | #endif |
408 | |
519 | |
409 | char * |
520 | char * |
… | |
… | |
419 | |
530 | |
420 | return r; |
531 | return r; |
421 | } |
532 | } |
422 | |
533 | |
423 | /* |
534 | /* |
424 | * This routine should read a single line, no matter how long. |
535 | * HACK-1: This routine should read a single line, no matter how long. |
425 | */ |
536 | */ |
426 | int |
537 | int |
427 | lineinput (struct logfile_entry *logfile) |
538 | lineinput (struct logfile_entry *logfile) |
428 | { |
539 | { |
429 | char buff[1024], *p = buff; |
540 | char buff[1024], *p = buff; |
430 | int ch; |
541 | int ch; |
|
|
542 | /* HACK-2: add on the length of any partial line which we will be appending to */ |
431 | int ofs = logfile->buf ? strlen (logfile->buf) : 0; |
543 | int ofs = logfile->buf ? strlen (logfile->buf) : 0; |
432 | |
544 | |
433 | do |
545 | do |
434 | { |
546 | { |
435 | ch = fgetc (logfile->fp); |
547 | ch = fgetc (logfile->fp); |
… | |
… | |
548 | sleep (1); |
660 | sleep (1); |
549 | if (e->fp) |
661 | if (e->fp) |
550 | fclose (e->fp); |
662 | fclose (e->fp); |
551 | if (openlog (e) == NULL) |
663 | if (openlog (e) == NULL) |
552 | continue; |
664 | continue; |
|
|
665 | if (fstat (fileno (e->fp), &stats) < 0) |
|
|
666 | continue; |
553 | } |
667 | } |
554 | |
668 | |
555 | /* HACK this - stats can be uninitialised here (if the file |
|
|
556 | * didn't exist when stat() was called, but was recreated during |
|
|
557 | * the sleep(1)) */ |
|
|
558 | if (stats.st_ino != e->inode) |
669 | if (stats.st_ino != e->inode) |
559 | { /* file renamed? */ |
670 | { /* file renamed? */ |
560 | if (e->fp) |
671 | if (e->fp) |
561 | fclose (e->fp); |
672 | fclose (e->fp); |
562 | if (openlog (e) == NULL) |
673 | if (openlog (e) == NULL) |
… | |
… | |
603 | struct logfile_entry *current; |
714 | struct logfile_entry *current; |
604 | |
715 | |
605 | for (cur_line = idx; cur_line > 0; cur_line--) |
716 | for (cur_line = idx; cur_line > 0; cur_line--) |
606 | lines[cur_line] = lines[cur_line - 1]; |
717 | lines[cur_line] = lines[cur_line - 1]; |
607 | |
718 | |
608 | lines[0].line = strdup ("~"); |
719 | lines[0].line = xstrdup ("~"); |
609 | |
720 | |
610 | for (current = loglist; current; current = current->next) |
721 | for (current = loglist; current; current = current->next) |
611 | if (current->index >= 0 && current->index <= idx) |
722 | if (current->index >= 0 && current->index <= idx) |
612 | current->index++; |
723 | current->index++; |
613 | } |
724 | } |
… | |
… | |
619 | */ |
730 | */ |
620 | static void |
731 | static void |
621 | split_line (int idx, const char *str, unsigned long color) |
732 | split_line (int idx, const char *str, unsigned long color) |
622 | { |
733 | { |
623 | int l = strlen (str); |
734 | int l = strlen (str); |
|
|
735 | int last_wrapped = 0; |
624 | const char *p = str; |
736 | const char *p = str; |
|
|
737 | static int continuation_width = -1; |
|
|
738 | static int continuation_length; |
|
|
739 | |
|
|
740 | /* only calculate the continuation's width once */ |
|
|
741 | if (continuation_width == -1) |
|
|
742 | { |
|
|
743 | continuation_length = strlen(continuation); |
|
|
744 | continuation_width = XmbTextEscapement (fontset, continuation, continuation_length); |
|
|
745 | } |
625 | |
746 | |
626 | do |
747 | do |
627 | { |
748 | { |
628 | const char *beg = p; |
749 | const char *beg = p; |
|
|
750 | int w = last_wrapped ? continuation_width : 0; |
629 | int w = 0, wrapped = 0; |
751 | int wrapped = 0; |
630 | const char *break_p = NULL; |
752 | const char *break_p = NULL; |
631 | |
753 | |
632 | while (*p) |
754 | while (*p) |
633 | { |
755 | { |
634 | /* find the length in bytes of the next multibyte character */ |
756 | /* find the length in bytes of the next multibyte character */ |
… | |
… | |
636 | if (len <= 0) |
758 | if (len <= 0) |
637 | len = 1; /* ignore (don't skip) illegal character sequences */ |
759 | len = 1; /* ignore (don't skip) illegal character sequences */ |
638 | |
760 | |
639 | /* find the width in pixels of the next character */ |
761 | /* find the width in pixels of the next character */ |
640 | int cw = XmbTextEscapement (fontset, p, len); |
762 | int cw = XmbTextEscapement (fontset, p, len); |
641 | if (cw + w >= width) |
763 | if (cw + w > width - effect_x_space) |
642 | { |
764 | { |
|
|
765 | if (p == beg) |
|
|
766 | { |
|
|
767 | fprintf(stderr, "we can't even fit a single character onto the line\n"); |
|
|
768 | if (len == 1) fprintf(stderr, "(the character we couldn't fit was '%c')\n", *p); |
|
|
769 | exit(1); |
|
|
770 | } |
|
|
771 | |
643 | wrapped = 1; |
772 | wrapped = 1; |
644 | break; |
773 | break; |
645 | } |
774 | } |
646 | |
775 | |
647 | if (opt_wordwrap && len == 1 && p[0] == ' ') |
776 | if (opt_wordwrap && len == 1 && p[0] == ' ') |
… | |
… | |
660 | l += p - break_p; |
789 | l += p - break_p; |
661 | p = break_p; |
790 | p = break_p; |
662 | } |
791 | } |
663 | |
792 | |
664 | { |
793 | { |
|
|
794 | /* HACK-4 - consider inserting the 'continuation string' |
|
|
795 | * before the rest of the wrapped line */ |
|
|
796 | int len = p - beg + (last_wrapped ? continuation_length : 0); |
665 | char *s = xmalloc (p - beg + 1); |
797 | char *s = xmalloc (len + 1); |
|
|
798 | if (last_wrapped) |
|
|
799 | { |
|
|
800 | memcpy (s, continuation, continuation_length); |
|
|
801 | memcpy (s + continuation_length, beg, p - beg); |
|
|
802 | } |
|
|
803 | else |
666 | memcpy (s, beg, p - beg); |
804 | memcpy (s, beg, len); |
|
|
805 | |
667 | s[p - beg] = 0; |
806 | s[len] = 0; |
668 | insert_line (idx); |
807 | insert_line (idx); |
669 | lines[idx].line = s; |
808 | lines[idx].line = s; |
670 | lines[idx].len = p - beg; |
809 | lines[idx].len = len; |
671 | lines[idx].color = color; |
810 | lines[idx].color = color; |
672 | } |
811 | } |
673 | |
812 | |
674 | /* if we wrapped at a space, don't display the space */ |
813 | /* if we wrapped at a space, don't display the space */ |
675 | if (opt_wordwrap && wrapped && break_p && break_p != beg) |
814 | if (opt_wordwrap && wrapped && break_p && break_p != beg) |
676 | { |
815 | { |
677 | l--; |
816 | l--; |
678 | p++; |
817 | p++; |
679 | } |
818 | } |
|
|
819 | |
|
|
820 | last_wrapped = wrapped; |
680 | } |
821 | } |
681 | while (l); |
822 | while (l); |
682 | } |
823 | } |
683 | |
824 | |
684 | /* |
825 | /* |
… | |
… | |
701 | |
842 | |
702 | static void |
843 | static void |
703 | main_loop (void) |
844 | main_loop (void) |
704 | { |
845 | { |
705 | lines = xmalloc (sizeof (struct linematrix) * listlen); |
846 | lines = xmalloc (sizeof (struct linematrix) * listlen); |
|
|
847 | display = xmalloc (sizeof (struct displaymatrix) * listlen); |
706 | int lin; |
848 | int lin; |
707 | time_t lastreload; |
849 | time_t lastreload; |
708 | Region region = XCreateRegion (); |
850 | Region region = XCreateRegion (); |
709 | XEvent xev; |
851 | XEvent xev; |
710 | struct logfile_entry *lastprinted = NULL; |
852 | struct logfile_entry *lastprinted = NULL; |
… | |
… | |
714 | lastreload = time (NULL); |
856 | lastreload = time (NULL); |
715 | |
857 | |
716 | /* Initialize linematrix */ |
858 | /* Initialize linematrix */ |
717 | for (lin = 0; lin < listlen; lin++) |
859 | for (lin = 0; lin < listlen; lin++) |
718 | { |
860 | { |
719 | lines[lin].line = strdup ("~"); |
861 | lines[lin].line = xstrdup ("~"); |
720 | lines[lin].len = 1; |
862 | lines[lin].len = 1; |
|
|
863 | display[lin].line = xstrdup(""); |
|
|
864 | display[lin].len = 0; |
|
|
865 | display[lin].buffer_size = 1; |
721 | lines[lin].color = GetColor (def_color); |
866 | lines[lin].color = GetColor (def_color); |
722 | } |
867 | } |
723 | |
868 | |
724 | for (;;) |
869 | for (;;) |
725 | { |
870 | { |
… | |
… | |
750 | |
895 | |
751 | /* print filename if any, and if last line was from |
896 | /* print filename if any, and if last line was from |
752 | * different file */ |
897 | * different file */ |
753 | if (!opt_nofilename && lastprinted != current && current->desc[0]) |
898 | if (!opt_nofilename && lastprinted != current && current->desc[0]) |
754 | { |
899 | { |
755 | char buf[1024]; /* HACK */ |
900 | char buf[1024]; /* HACK-5 */ |
756 | snprintf (buf, sizeof (buf), "[%s]", current->desc); |
901 | snprintf (buf, sizeof (buf), "[%s]", current->desc); |
757 | split_line (listlen - 1, buf, current->color); |
902 | split_line (listlen - 1, buf, current->color); |
758 | } |
903 | } |
759 | |
904 | |
760 | /* if this is the same file we showed last, and the |
905 | /* if we're dealing with partial lines, and the last |
761 | * last time we showed it, it wasn't finished, then |
906 | * time we showed the line it wasn't finished ... */ |
762 | * append to the last line shown */ |
|
|
763 | if (lastprinted == current && current->lastpartial) |
|
|
764 | append_line (listlen - 1, current->buf); |
|
|
765 | else if (current->lastpartial) |
907 | if (!opt_whole && current->lastpartial) |
766 | { |
908 | { |
|
|
909 | /* if this is the same file we showed last then |
|
|
910 | append to the last line shown */ |
|
|
911 | if (lastprinted == current) |
|
|
912 | append_line (listlen - 1, current->buf); |
|
|
913 | else |
|
|
914 | { |
|
|
915 | /* but if a different file has been shown in the |
|
|
916 | * mean time, make a new line, starting with the |
|
|
917 | * continuation string */ |
767 | split_line (listlen - 1, continuation, current->color); |
918 | split_line (listlen - 1, continuation, current->color); |
768 | append_line (listlen - 1, current->buf); |
919 | append_line (listlen - 1, current->buf); |
|
|
920 | } |
769 | } |
921 | } |
770 | else |
922 | else |
|
|
923 | /* otherwise just make a plain and simple new line */ |
771 | split_line (listlen - 1, current->buf, current->color); |
924 | split_line (listlen - 1, current->buf, current->color); |
772 | |
925 | |
773 | free (current->buf), current->buf = 0; |
926 | free (current->buf), current->buf = 0; |
774 | current->index = listlen - 1; |
927 | current->index = listlen - 1; |
775 | lastprinted = current; |
928 | lastprinted = current; |
776 | } |
929 | } |
777 | } |
930 | } |
778 | |
931 | |
779 | if (need_update) |
932 | if (need_update) |
780 | { |
933 | { |
781 | redraw (); |
934 | redraw (0); |
782 | need_update = 0; |
935 | need_update = 0; |
783 | } |
936 | } |
784 | else |
937 | else |
785 | { |
938 | { |
786 | XFlush (disp); |
939 | XFlush (disp); |
… | |
… | |
843 | XRectangle r; |
996 | XRectangle r; |
844 | |
997 | |
845 | XSetRegion (disp, WinGC, region); |
998 | XSetRegion (disp, WinGC, region); |
846 | XClipBox (region, &r); |
999 | XClipBox (region, &r); |
847 | |
1000 | |
848 | refresh (r.y, r.y + r.height, 0); |
1001 | refresh (r.y, r.y + r.height, 0, 1); |
849 | |
1002 | |
850 | XDestroyRegion (region); |
1003 | XDestroyRegion (region); |
851 | region = XCreateRegion (); |
1004 | region = XCreateRegion (); |
852 | } |
1005 | } |
853 | } |
1006 | } |
… | |
… | |
894 | continuation = argv[++i]; |
1047 | continuation = argv[++i]; |
895 | else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) |
1048 | else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) |
896 | fontname = argv[++i]; |
1049 | fontname = argv[++i]; |
897 | #if HAS_REGEX |
1050 | #if HAS_REGEX |
898 | else if (!strcmp (arg, "-t")) |
1051 | else if (!strcmp (arg, "-t")) |
|
|
1052 | { |
899 | transform = argv[++i]; |
1053 | transform = argv[++i]; |
|
|
1054 | transform_to = argv[++i]; |
|
|
1055 | printf("transform: '%s' to '%s'\n", transform, transform_to); |
|
|
1056 | } |
900 | #endif |
1057 | #endif |
901 | else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) |
1058 | else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) |
902 | opt_daemonize = 1; |
1059 | opt_daemonize = 1; |
903 | else if (!strcmp (arg, "-reload")) |
1060 | else if (!strcmp (arg, "-reload")) |
904 | { |
1061 | { |
… | |
… | |
988 | |
1145 | |
989 | e->fname = xstrdup (fname); |
1146 | e->fname = xstrdup (fname); |
990 | if (openlog (e) == NULL) |
1147 | if (openlog (e) == NULL) |
991 | perror (fname), exit (1); |
1148 | perror (fname), exit (1); |
992 | |
1149 | |
993 | l = strlen (desc); |
1150 | e->desc = xstrdup (desc); |
994 | if (l > width - 2) /* must account for [ ] */ |
|
|
995 | l = width - 2; |
|
|
996 | e->desc = xmalloc (l + 1); |
|
|
997 | memcpy (e->desc, desc, l); |
|
|
998 | *(e->desc + l) = '\0'; |
|
|
999 | } |
1151 | } |
1000 | |
1152 | |
1001 | e->color = GetColor (fcolor); |
1153 | e->color = GetColor (fcolor); |
1002 | e->partial = 0; |
1154 | e->partial = 0; |
1003 | e->next = NULL; |
1155 | e->next = NULL; |
… | |
… | |
1021 | { |
1173 | { |
1022 | fprintf (stderr, "Specify at most one of -partial and -whole\n"); |
1174 | fprintf (stderr, "Specify at most one of -partial and -whole\n"); |
1023 | exit (1); |
1175 | exit (1); |
1024 | } |
1176 | } |
1025 | |
1177 | |
|
|
1178 | /* HACK-7: do we want to allow both -shade and -outline? */ |
|
|
1179 | if (opt_shade && opt_outline) |
|
|
1180 | { |
|
|
1181 | fprintf (stderr, "Specify at most one of -shade and -outline\n"); |
|
|
1182 | exit (1); |
|
|
1183 | } |
|
|
1184 | |
1026 | if (opt_partial) |
1185 | if (opt_partial) |
1027 | /* if we specifically requested to see partial lines then don't insist on whole lines */ |
1186 | /* if we specifically requested to see partial lines then don't insist on whole lines */ |
1028 | opt_whole = 0; |
1187 | opt_whole = 0; |
1029 | else if (file_count > 1) |
1188 | else if (file_count > 1) |
1030 | /* otherwise, if we've viewing multiple files, default to showing whole lines */ |
1189 | /* otherwise, if we're viewing multiple files, default to showing whole lines */ |
1031 | opt_whole = 1; |
1190 | opt_whole = 1; |
1032 | |
1191 | |
1033 | #if HAS_REGEX |
1192 | #if HAS_REGEX |
1034 | if (transform) |
1193 | if (transform) |
1035 | { |
1194 | { |
1036 | int i; |
1195 | int i; |
1037 | |
1196 | |
|
|
1197 | printf("compiling regexp '%s'\n", transform); |
1038 | transformre = xmalloc (sizeof (transformre)); |
1198 | transformre = xmalloc (sizeof (regex_t)); |
1039 | i = regcomp (&transformre, transform, REG_EXTENDED); |
1199 | i = regcomp (transformre, transform, REG_EXTENDED); |
1040 | if (i != 0) |
1200 | if (i != 0) |
1041 | { |
1201 | { |
1042 | char buf[512]; |
1202 | char buf[512]; |
1043 | |
1203 | |
1044 | regerror (i, &transformre, buf, sizeof (buf)); |
1204 | regerror (i, transformre, buf, sizeof (buf)); |
1045 | fprintf (stderr, "Cannot compile regular expression: %s\n", buf); |
1205 | fprintf (stderr, "Cannot compile regular expression: %s\n", buf); |
1046 | } |
1206 | } |
|
|
1207 | else |
|
|
1208 | { |
|
|
1209 | printf("compiled '%s' OK to %x\n", transform, (int)transformre); |
|
|
1210 | } |
1047 | } |
1211 | } |
1048 | #endif |
1212 | #endif |
1049 | |
1213 | |
1050 | InitWindow (); |
1214 | InitWindow (); |
1051 | |
1215 | |
… | |
… | |
1095 | xmalloc (size_t size) |
1259 | xmalloc (size_t size) |
1096 | { |
1260 | { |
1097 | void *p; |
1261 | void *p; |
1098 | |
1262 | |
1099 | while ((p = malloc (size)) == NULL) |
1263 | while ((p = malloc (size)) == NULL) |
|
|
1264 | { |
|
|
1265 | fprintf (stderr, "Memory exausted."); |
|
|
1266 | sleep (10); |
|
|
1267 | } |
|
|
1268 | |
|
|
1269 | return p; |
|
|
1270 | } |
|
|
1271 | |
|
|
1272 | void * |
|
|
1273 | xrealloc (void *ptr, size_t size) |
|
|
1274 | { |
|
|
1275 | void *p; |
|
|
1276 | |
|
|
1277 | while ((p = realloc (ptr, size)) == NULL) |
1100 | { |
1278 | { |
1101 | fprintf (stderr, "Memory exausted."); |
1279 | fprintf (stderr, "Memory exausted."); |
1102 | sleep (10); |
1280 | sleep (10); |
1103 | } |
1281 | } |
1104 | |
1282 | |
… | |
… | |
1143 | } |
1321 | } |
1144 | |
1322 | |
1145 | int |
1323 | int |
1146 | daemonize (void) |
1324 | daemonize (void) |
1147 | { |
1325 | { |
|
|
1326 | pid_t pid; |
|
|
1327 | |
1148 | switch (fork ()) |
1328 | switch (pid = fork ()) |
1149 | { |
1329 | { |
1150 | case -1: |
1330 | case -1: |
1151 | return -1; |
1331 | return -1; |
1152 | case 0: |
1332 | case 0: |
1153 | break; |
1333 | break; |
1154 | default: |
1334 | default: |
|
|
1335 | /*printf("%d\n", pid);*/ |
1155 | _exit (0); |
1336 | exit (0); |
1156 | } |
1337 | } |
1157 | |
1338 | |
1158 | if (setsid () == -1) |
1339 | if (setsid () == -1) |
1159 | return -1; |
1340 | return -1; |
1160 | |
1341 | |