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