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