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