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