… | |
… | |
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_descent, font_height; |
60 | int do_reopen; |
72 | int do_reopen; |
61 | struct timeval interval = { 2, 400000 }; /* see Knuth */ |
73 | struct timeval interval = { 3, 0 }; |
|
|
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_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); |
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, win_y, width, height, 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_descent = 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 | XClearArea (disp, root, win_x, win_y, width, height, False); |
|
|
321 | refresh (0, 32768); |
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) |
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 | |
234 | for (lin = listlen; lin--;) |
336 | for (lin = listlen; lin--;) |
|
|
337 | { |
|
|
338 | struct linematrix *line = lines + (opt_reverse ? listlen - lin - 1 : lin); |
|
|
339 | |
|
|
340 | offset -= font_height; |
|
|
341 | |
|
|
342 | if (offset < miny || offset > maxy) |
|
|
343 | continue; |
|
|
344 | |
|
|
345 | if (opt_shade) |
235 | { |
346 | { |
236 | char *temp; |
347 | XSetForeground (disp, WinGC, black_color); |
|
|
348 | XmbDrawString (disp, root, fontset, WinGC, win_x + 2, |
|
|
349 | win_y + offset + 2, line->line, line->len); |
|
|
350 | } |
|
|
351 | |
|
|
352 | XSetForeground (disp, WinGC, line->color); |
|
|
353 | XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset, |
|
|
354 | line->line, line->len); |
|
|
355 | } |
|
|
356 | |
|
|
357 | if (opt_frame) |
|
|
358 | XDrawRectangle (disp, root, WinGC, win_x - 2, win_y - 2, width + 4, height + 4); |
|
|
359 | } |
|
|
360 | |
|
|
361 | #if HAS_REGEX |
|
|
362 | void |
|
|
363 | transform_line (char *s) |
|
|
364 | { |
|
|
365 | #ifdef I_AM_Md |
|
|
366 | int i; |
|
|
367 | if (1) |
|
|
368 | { |
|
|
369 | for (i = 16; s[i]; i++) |
|
|
370 | s[i] = s[i + 11]; |
|
|
371 | } |
|
|
372 | s[i + 1] = '\0'; |
|
|
373 | #endif |
|
|
374 | |
|
|
375 | if (transformre) |
|
|
376 | { |
|
|
377 | int i; |
|
|
378 | regmatch_t matched[16]; |
|
|
379 | |
|
|
380 | i = regexec (&transformre, string, 16, matched, 0); |
|
|
381 | if (i == 0) |
|
|
382 | { /* matched */ |
|
|
383 | } |
|
|
384 | } |
|
|
385 | } |
|
|
386 | #endif |
|
|
387 | |
|
|
388 | char * |
|
|
389 | concat_line (const char *p1, const char *p2) |
|
|
390 | { |
|
|
391 | int l1 = p1 ? strlen (p1) : 0; |
|
|
392 | int l2 = strlen (p2); |
|
|
393 | char *r = xmalloc (l1 + l2 + 1); |
|
|
394 | |
|
|
395 | memcpy (r, p1, l1); |
|
|
396 | memcpy (r + l1, p2, l2); |
|
|
397 | r[l1 + l2] = 0; |
|
|
398 | |
|
|
399 | return r; |
|
|
400 | } |
|
|
401 | |
|
|
402 | /* |
|
|
403 | * This routine should read a single line, no matter how long. |
|
|
404 | */ |
|
|
405 | int |
|
|
406 | lineinput (struct logfile_entry *logfile) |
|
|
407 | { |
|
|
408 | char buff[1024], *p = buff; |
|
|
409 | int ch; |
|
|
410 | int ofs = logfile->buf ? strlen (logfile->buf) : 0; |
|
|
411 | |
|
|
412 | do |
|
|
413 | { |
|
|
414 | ch = fgetc (logfile->fp); |
|
|
415 | |
|
|
416 | if (ch == '\r') |
|
|
417 | continue; |
|
|
418 | else if (ch == EOF || ch == '\n') |
|
|
419 | break; |
|
|
420 | else if (ch == '\t') |
|
|
421 | { |
|
|
422 | do |
|
|
423 | { |
|
|
424 | *p++ = ' '; |
|
|
425 | ofs++; |
|
|
426 | } |
|
|
427 | while (ofs & 7); |
|
|
428 | } |
|
|
429 | else |
|
|
430 | { |
|
|
431 | *p++ = ch; |
|
|
432 | ofs++; |
|
|
433 | } |
|
|
434 | } |
|
|
435 | while (p < buff + (sizeof buff) - 8 - 1); |
|
|
436 | |
|
|
437 | if (p == buff) |
|
|
438 | return 0; |
|
|
439 | |
|
|
440 | *p = 0; |
|
|
441 | |
|
|
442 | p = concat_line (logfile->buf, buff); |
|
|
443 | free (logfile->buf); logfile->buf = p; |
|
|
444 | |
|
|
445 | logfile->lastpartial = logfile->partial; |
|
|
446 | logfile->partial = ch == EOF; |
237 | |
447 | |
238 | offset -= font_height; |
448 | if (logfile->partial && opt_whole) |
239 | |
449 | 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 | |
450 | |
272 | #if HAS_REGEX |
451 | #if HAS_REGEX |
273 | void transform_line(char *s) |
452 | 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 |
453 | #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; |
454 | 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 | } |
455 | } |
326 | |
456 | |
327 | /* input: reads file->fname |
457 | /* input: reads file->fname |
328 | * output: fills file->fp, file->inode |
458 | * output: fills file->fp, file->inode |
329 | * returns file->fp |
459 | * returns file->fp |
330 | * in case of error, file->fp is NULL |
460 | * in case of error, file->fp is NULL |
331 | */ |
461 | */ |
|
|
462 | FILE * |
332 | FILE *openlog(struct logfile_entry *file) |
463 | openlog (struct logfile_entry * file) |
333 | { |
464 | { |
334 | struct stat stats; |
465 | struct stat stats; |
335 | |
466 | |
336 | if ((file->fp = fopen(file->fname, "r")) == NULL) { |
467 | if ((file->fp = fopen (file->fname, "r")) == NULL) |
|
|
468 | { |
337 | file->fp = NULL; |
469 | file->fp = NULL; |
338 | return NULL; |
470 | return NULL; |
339 | } |
471 | } |
340 | |
472 | |
341 | fstat(fileno(file->fp), &stats); |
473 | fstat (fileno (file->fp), &stats); |
342 | if (S_ISFIFO(stats.st_mode)) { |
474 | if (S_ISFIFO (stats.st_mode)) |
|
|
475 | { |
343 | if (fcntl(fileno(file->fp), F_SETFL, O_NONBLOCK) < 0) |
476 | if (fcntl (fileno (file->fp), F_SETFL, O_NONBLOCK) < 0) |
344 | perror("fcntl"), exit(1); |
477 | perror ("fcntl"), exit (1); |
345 | file->inode = 0; |
478 | file->inode = 0; |
|
|
479 | } |
346 | } else |
480 | else |
347 | file->inode = stats.st_ino; |
481 | file->inode = stats.st_ino; |
348 | |
482 | |
349 | if (opt_noinitial) |
483 | if (opt_noinitial) |
350 | fseek (file->fp, 0, SEEK_END); |
484 | fseek (file->fp, 0, SEEK_END); |
351 | else if (stats.st_size > (listlen + 1) * width) |
485 | else if (stats.st_size > (listlen + 1) * width) |
|
|
486 | fseek (file->fp, -((listlen + 2) * width), SEEK_END); |
|
|
487 | |
|
|
488 | file->last_size = stats.st_size; |
|
|
489 | return file->fp; |
|
|
490 | } |
|
|
491 | |
|
|
492 | void |
|
|
493 | reopen (void) |
|
|
494 | { |
|
|
495 | struct logfile_entry *e; |
|
|
496 | |
|
|
497 | for (e = loglist; e; e = e->next) |
|
|
498 | { |
|
|
499 | if (!e->inode) |
|
|
500 | continue; /* skip stdin */ |
|
|
501 | |
|
|
502 | if (e->fp) |
|
|
503 | fclose (e->fp); |
|
|
504 | /* if fp is NULL we will try again later */ |
|
|
505 | openlog (e); |
|
|
506 | } |
|
|
507 | |
|
|
508 | do_reopen = 0; |
|
|
509 | } |
|
|
510 | |
|
|
511 | void |
|
|
512 | check_open_files (void) |
|
|
513 | { |
|
|
514 | struct logfile_entry *e; |
|
|
515 | struct stat stats; |
|
|
516 | |
|
|
517 | for (e = loglist; e; e = e->next) |
|
|
518 | { |
|
|
519 | if (!e->inode) |
|
|
520 | continue; /* skip stdin */ |
|
|
521 | |
|
|
522 | if (stat (e->fname, &stats) < 0) |
|
|
523 | { /* file missing? */ |
|
|
524 | sleep (1); |
|
|
525 | if (e->fp) |
|
|
526 | fclose (e->fp); |
|
|
527 | if (openlog (e) == NULL) |
|
|
528 | break; |
|
|
529 | } |
|
|
530 | |
|
|
531 | if (stats.st_ino != e->inode) |
|
|
532 | { /* file renamed? */ |
|
|
533 | if (e->fp) |
|
|
534 | fclose (e->fp); |
|
|
535 | if (openlog (e) == NULL) |
|
|
536 | break; |
|
|
537 | } |
|
|
538 | |
|
|
539 | if (stats.st_size < e->last_size) |
|
|
540 | { /* file truncated? */ |
|
|
541 | fseek (e->fp, 0, SEEK_SET); |
|
|
542 | e->last_size = stats.st_size; |
|
|
543 | } |
|
|
544 | } |
|
|
545 | } |
|
|
546 | |
|
|
547 | static void |
|
|
548 | insert_line (int idx) |
|
|
549 | { |
|
|
550 | int cur_line; |
|
|
551 | struct logfile_entry *current; |
|
|
552 | |
|
|
553 | free (lines[0].line); |
|
|
554 | |
|
|
555 | for (cur_line = 0; cur_line < idx; cur_line++) |
|
|
556 | lines[cur_line] = lines[cur_line + 1]; |
|
|
557 | |
|
|
558 | for (current = loglist; current; current = current->next) |
|
|
559 | if (current->partial && current->index && current->index <= idx) |
|
|
560 | current->index--; |
|
|
561 | } |
|
|
562 | |
|
|
563 | static void |
|
|
564 | delete_line (int idx) |
|
|
565 | { |
|
|
566 | int cur_line; |
|
|
567 | struct logfile_entry *current; |
|
|
568 | |
|
|
569 | for (cur_line = idx; cur_line > 0; cur_line--) |
|
|
570 | lines[cur_line] = lines[cur_line - 1]; |
|
|
571 | |
|
|
572 | lines[0].line = strdup ("~"); |
|
|
573 | |
|
|
574 | for (current = loglist; current; current = current->next) |
|
|
575 | if (current->partial && current->index && current->index <= idx) |
|
|
576 | current->index++; |
|
|
577 | } |
|
|
578 | |
|
|
579 | static void |
|
|
580 | split_line (int idx, const char *str, unsigned long color) |
|
|
581 | { |
|
|
582 | int l = strlen (str); |
|
|
583 | const char *p = str; |
|
|
584 | |
|
|
585 | do |
|
|
586 | { |
|
|
587 | const char *beg = p; |
|
|
588 | int w = 0; |
|
|
589 | |
|
|
590 | while (*p) |
|
|
591 | { |
|
|
592 | int len = mblen (p, l); |
|
|
593 | if (len <= 0) |
|
|
594 | len = 1; /* ignore (don't skip) ilegal character sequences */ |
|
|
595 | |
|
|
596 | int cw = XmbTextEscapement (fontset, p, len); |
|
|
597 | if (cw + w >= width) |
|
|
598 | break; |
|
|
599 | |
|
|
600 | w += cw; |
|
|
601 | p += len; |
|
|
602 | l -= len; |
|
|
603 | } |
|
|
604 | |
352 | { |
605 | { |
353 | char dummy[255]; |
606 | char *s = xmalloc (p - beg + 1); |
354 | |
607 | memcpy (s, beg, p - beg); |
355 | fseek(file->fp, -((listlen + 2) * width), SEEK_END); |
608 | s[p - beg] = 0; |
356 | /* the pointer might point halfway some line. Let's |
609 | insert_line (idx); |
357 | be nice and skip this damaged line */ |
610 | lines[idx].line = s; |
358 | lineinput(dummy, sizeof(dummy), file->fp); |
611 | lines[idx].len = p - beg; |
|
|
612 | lines[idx].color = color; |
359 | } |
613 | } |
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 | } |
614 | } |
378 | |
615 | while (l); |
379 | do_reopen = 0; |
|
|
380 | } |
616 | } |
381 | |
617 | |
382 | void check_open_files(void) |
618 | static void |
|
|
619 | append_line (int idx, const char *str) |
383 | { |
620 | { |
384 | struct logfile_entry *e; |
621 | unsigned long color = lines[idx].color; |
385 | struct stat stats; |
622 | char *old = lines[idx].line; |
|
|
623 | char *new = concat_line (old, str); |
386 | |
624 | |
387 | for (e = loglist; e; e = e->next) { |
625 | free (old); |
388 | if (!e->inode) |
626 | free (str); |
389 | continue; /* skip stdin */ |
|
|
390 | |
627 | |
391 | if (stat(e->fname, &stats) < 0) { /* file missing? */ |
628 | delete_line (idx); |
392 | sleep(1); |
629 | 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 | } |
630 | } |
412 | |
631 | |
413 | #define SCROLL_UP(lines, listlen) \ |
632 | 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) |
633 | main_loop (void) |
423 | { |
634 | { |
424 | struct linematrix *lines = xmalloc(sizeof(struct linematrix) * listlen); |
635 | lines = xmalloc (sizeof (struct linematrix) * listlen); |
425 | int lin, miny, maxy, buflen; |
636 | int lin, miny, maxy; |
426 | char *buf; |
|
|
427 | time_t lastreload; |
637 | time_t lastreload; |
428 | Region region = XCreateRegion(); |
638 | Region region = XCreateRegion (); |
429 | XEvent xev; |
639 | XEvent xev; |
|
|
640 | struct logfile_entry *lastprinted = NULL; |
|
|
641 | struct logfile_entry *current; |
430 | |
642 | |
431 | maxy = 0; |
643 | maxy = 0; |
432 | miny = win_y + h; |
644 | miny = win_y + height; |
433 | buflen = width + 2; |
|
|
434 | buf = xmalloc(buflen); |
|
|
435 | lastreload = time(NULL); |
645 | lastreload = time (NULL); |
436 | |
646 | |
437 | /* Initialize linematrix */ |
647 | /* Initialize linematrix */ |
438 | for (lin = 0; lin < listlen; lin++) { |
648 | for (lin = 0; lin < listlen; lin++) |
439 | lines[lin].line = xmalloc(buflen); |
649 | { |
440 | strcpy(lines[lin].line, "~"); |
650 | lines[lin].line = strdup ("~"); |
|
|
651 | lines[lin].len = 1; |
441 | lines[lin].color = GetColor(def_color); |
652 | lines[lin].color = GetColor (def_color); |
442 | } |
653 | } |
443 | |
654 | |
444 | if (!opt_noinitial) |
655 | /* show the display full of empty lines ("~") in case the first |
445 | while (lineinput(buf, buflen, loglist->fp) != 0) { |
656 | * time around the loop doesn't produce any output, as it won't if |
446 | SCROLL_UP(lines, listlen); |
657 | * either (a) -noinitial is set or (b) all the files are currently |
447 | /* print the next line */ |
658 | * empty */ |
448 | strcpy(lines[listlen - 1].line, buf); |
659 | redraw (); |
449 | } |
|
|
450 | |
660 | |
451 | for (;;) { |
661 | for (;;) |
|
|
662 | { |
452 | int need_update = 0; |
663 | int need_update = 0; |
453 | struct logfile_entry *current; |
|
|
454 | static struct logfile_entry *lastprinted = NULL; |
|
|
455 | |
664 | |
456 | /* read logs */ |
665 | /* read logs */ |
457 | for (current = loglist; current; current = current->next) { |
666 | for (current = loglist; current; current = current->next) |
|
|
667 | { |
458 | if (!current->fp) |
668 | if (!current->fp) |
459 | continue; /* skip missing files */ |
669 | continue; /* skip missing files */ |
460 | |
670 | |
461 | clearerr(current->fp); |
671 | clearerr (current->fp); |
462 | |
672 | |
463 | while (lineinput(buf, buflen, current->fp) != 0) { |
673 | while (lineinput (current)) |
|
|
674 | { |
|
|
675 | need_update = 1; |
|
|
676 | /* if we're trying to update old partial lines in |
|
|
677 | * place, and the last time this file was updated the |
|
|
678 | * output was partial, and that partial line is not |
|
|
679 | * too close to the top of the screen, then update |
|
|
680 | * that partial line */ |
|
|
681 | if (opt_update && current->lastpartial && current->index > 0) |
|
|
682 | { |
|
|
683 | append_line (current->index, current->buf); |
|
|
684 | free (current->buf), current->buf = 0; |
|
|
685 | continue; |
|
|
686 | } |
|
|
687 | |
464 | /* print filename if any, and if last line was from |
688 | /* print filename if any, and if last line was from |
465 | different file */ |
689 | * different file */ |
466 | if (!opt_nofilename && |
690 | if (!opt_nofilename && lastprinted != current && current->desc[0]) |
467 | !(lastprinted && lastprinted == current) && |
691 | { |
468 | current->desc[0]) { |
692 | char buf[1024]; /* HACK */ |
469 | SCROLL_UP(lines, listlen); |
693 | snprintf (buf, sizeof (buf), "[%s]", current->desc); |
470 | sprintf(lines[listlen - 1].line, "[%s]", current->desc); |
694 | split_line (listlen - 1, buf, current->color); |
471 | lines[listlen - 1].color = current->color; |
695 | } |
472 | } |
|
|
473 | |
696 | |
474 | SCROLL_UP(lines, listlen); |
697 | /* if this is the same file we showed last, and the |
475 | strcpy(lines[listlen - 1].line, buf); |
698 | * last time we showed it, it wasn't finished, then |
476 | lines[listlen - 1].color = current->color; |
699 | * append to the last line shown */ |
|
|
700 | if (lastprinted == current && current->lastpartial) |
|
|
701 | { |
|
|
702 | append_line (listlen - 1, current->buf); |
|
|
703 | free (current->buf), current->buf = 0; |
|
|
704 | continue; |
|
|
705 | } |
|
|
706 | else |
|
|
707 | { |
|
|
708 | split_line (listlen - 1, current->buf, current->color); |
|
|
709 | free (current->buf), current->buf = 0; |
|
|
710 | } |
477 | |
711 | |
478 | lastprinted = current; |
712 | current->index = listlen - 1; |
479 | need_update = 1; |
713 | lastprinted = current; |
480 | } |
714 | } |
481 | } |
715 | } |
482 | |
716 | |
483 | if (need_update) |
717 | if (need_update) |
484 | redraw(); |
718 | redraw (); |
485 | else { |
719 | else |
|
|
720 | { |
486 | XFlush(disp); |
721 | XFlush (disp); |
|
|
722 | |
487 | if (!XPending(disp)) { |
723 | if (!XPending (disp)) |
488 | fd_set fdr; |
724 | { |
|
|
725 | fd_set fdr; |
489 | struct timeval to = interval; |
726 | struct timeval to = interval; |
490 | |
727 | |
491 | FD_ZERO(&fdr); |
728 | FD_ZERO (&fdr); |
492 | FD_SET(ConnectionNumber(disp), &fdr); |
729 | FD_SET (ConnectionNumber (disp), &fdr); |
493 | select(ConnectionNumber(disp) + 1, &fdr, 0, 0, &to); |
730 | select (ConnectionNumber (disp) + 1, &fdr, 0, 0, &to); |
494 | } |
731 | } |
495 | } |
732 | } |
496 | |
733 | |
497 | check_open_files(); |
734 | check_open_files (); |
498 | |
735 | |
499 | if (do_reopen) |
736 | if (do_reopen) |
500 | reopen(); |
737 | reopen (); |
501 | |
738 | |
502 | /* we ignore possible errors due to window resizing &c */ |
739 | /* we ignore possible errors due to window resizing &c */ |
503 | while (XPending(disp)) { |
740 | while (XPending (disp)) |
|
|
741 | { |
504 | XNextEvent(disp, &xev); |
742 | XNextEvent (disp, &xev); |
|
|
743 | |
505 | switch (xev.type) { |
744 | switch (xev.type) |
|
|
745 | { |
506 | case Expose: |
746 | case Expose: |
507 | { |
747 | { |
508 | XRectangle r; |
748 | XRectangle r; |
509 | |
749 | |
510 | r.x = xev.xexpose.x; |
750 | r.x = xev.xexpose.x; |
511 | r.y = xev.xexpose.y; |
751 | r.y = xev.xexpose.y; |
512 | r.width = xev.xexpose.width; |
752 | r.width = xev.xexpose.width; |
513 | r.height = xev.xexpose.height; |
753 | r.height = xev.xexpose.height; |
514 | XUnionRectWithRegion(&r, region, region); |
754 | XUnionRectWithRegion (&r, region, region); |
515 | if (miny > r.y) |
755 | if (miny > r.y) |
516 | miny = r.y; |
756 | miny = r.y; |
517 | if (maxy < r.y + r.height) |
757 | if (maxy < r.y + r.height) |
518 | maxy = r.y + r.height; |
758 | maxy = r.y + r.height; |
519 | } |
759 | } |
520 | break; |
760 | break; |
521 | default: |
761 | default: |
522 | #ifdef DEBUGMODE |
762 | #ifdef DEBUGMODE |
523 | fprintf(stderr, "PANIC! Unknown event %d\n", xev.type); |
763 | fprintf (stderr, "PANIC! Unknown event %d\n", xev.type); |
524 | #endif |
764 | #endif |
525 | break; |
765 | break; |
|
|
766 | } |
|
|
767 | } |
|
|
768 | |
|
|
769 | /* reload if requested */ |
|
|
770 | if (reload && lastreload + reload < time (NULL)) |
|
|
771 | { |
|
|
772 | if (command && command[0]) |
|
|
773 | system (command); |
|
|
774 | |
|
|
775 | reopen (); |
|
|
776 | lastreload = time (NULL); |
|
|
777 | } |
|
|
778 | |
|
|
779 | if (!XEmptyRegion (region)) |
|
|
780 | { |
|
|
781 | XSetRegion (disp, WinGC, region); |
|
|
782 | |
|
|
783 | refresh (miny, maxy); |
|
|
784 | XDestroyRegion (region); |
|
|
785 | region = XCreateRegion (); |
|
|
786 | maxy = 0; |
|
|
787 | miny = win_y + height; |
|
|
788 | } |
526 | } |
789 | } |
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 | } |
790 | } |
548 | |
791 | |
549 | |
792 | |
|
|
793 | int |
550 | int main(int argc, char *argv[]) |
794 | main (int argc, char *argv[]) |
551 | { |
795 | { |
552 | int i; |
796 | int i; |
553 | int opt_daemonize = 0; |
797 | int opt_daemonize = 0; |
|
|
798 | int opt_partial = 0, file_count = 0; |
554 | #if HAS_REGEX |
799 | #if HAS_REGEX |
555 | char *transform = NULL; |
800 | char *transform = NULL; |
556 | #endif |
801 | #endif |
557 | |
802 | |
|
|
803 | setlocale (LC_CTYPE, ""); /* try to initialize the locale. */ |
|
|
804 | |
558 | /* window needs to be initialized before colorlookups can be done */ |
805 | /* window needs to be initialized before colorlookups can be done */ |
559 | /* just a dummy to get the color lookups right */ |
806 | /* just a dummy to get the color lookups right */ |
560 | geom_mask = NoValue; |
807 | geom_mask = NoValue; |
561 | InitWindow(); |
808 | InitWindow (); |
562 | |
809 | |
563 | for (i = 1; i < argc; i++) { |
810 | for (i = 1; i < argc; i++) |
|
|
811 | { |
|
|
812 | const char *arg = argv[i]; |
|
|
813 | |
564 | if (argv[i][0] == '-' && argv[i][1] != '\0' && argv[i][1] != ',') { |
814 | if (arg[0] == '-' && arg[1] != '\0' && arg[1] != ',') |
|
|
815 | { |
|
|
816 | if (arg[1] == '-') |
|
|
817 | arg++; |
|
|
818 | |
565 | if (!strcmp(argv[i], "--?") || |
819 | if (!strcmp (arg, "-?") || |
566 | !strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) |
820 | !strcmp (arg, "-help") || !strcmp (arg, "-h")) |
567 | display_help(argv[0]); |
821 | display_help (argv[0]); |
568 | else if (!strcmp(argv[i], "-V")) |
822 | else if (!strcmp (arg, "-V")) |
569 | display_version(); |
823 | display_version (); |
570 | else if (!strcmp(argv[i], "-g") || !strcmp(argv[i], "-geometry")) |
824 | else if (!strcmp (arg, "-g") || !strcmp (arg, "-geometry")) |
571 | geom_mask = XParseGeometry(argv[++i], |
825 | geom_mask = |
572 | &win_x, &win_y, &width, &listlen); |
826 | XParseGeometry (argv[++i], &win_x, &win_y, &width, &height); |
573 | else if (!strcmp(argv[i], "-display")) |
827 | else if (!strcmp (arg, "-display")) |
574 | dispname = argv[++i]; |
828 | dispname = argv[++i]; |
|
|
829 | else if (!strcmp (arg, "-cont")) |
|
|
830 | continuation = argv[++i]; |
575 | else if (!strcmp(argv[i], "-font") || !strcmp(argv[i], "-fn")) |
831 | else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) |
576 | fontname = argv[++i]; |
832 | fontname = argv[++i]; |
577 | #if HAS_REGEX |
833 | #if HAS_REGEX |
578 | else if (!strcmp(argv[i], "-t")) |
834 | else if (!strcmp (arg, "-t")) |
579 | transform = argv[++i]; |
835 | transform = argv[++i]; |
580 | #endif |
836 | #endif |
581 | else if (!strcmp(argv[i], "-fork") || !strcmp(argv[i], "-f")) |
837 | else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) |
582 | opt_daemonize = 1; |
838 | opt_daemonize = 1; |
583 | else if (!strcmp(argv[i], "-reload")) { |
839 | else if (!strcmp (arg, "-reload")) |
584 | reload = atoi(argv[++i]); |
840 | { |
585 | command = argv[++i]; |
841 | reload = atoi (argv[++i]); |
586 | } |
842 | command = argv[++i]; |
|
|
843 | } |
587 | else if (!strcmp(argv[i], "-shade")) |
844 | else if (!strcmp (arg, "-shade")) |
588 | opt_shade = 1; |
845 | opt_shade = 1; |
589 | else if (!strcmp(argv[i], "-frame")) |
846 | else if (!strcmp (arg, "-frame")) |
590 | opt_frame = 1; |
847 | opt_frame = 1; |
591 | else if (!strcmp(argv[i], "-no-filename")) |
848 | else if (!strcmp (arg, "-no-filename")) |
592 | opt_nofilename = 1; |
849 | opt_nofilename = 1; |
593 | else if (!strcmp(argv[i], "-reverse")) |
850 | else if (!strcmp (arg, "-reverse")) |
594 | opt_reverse = 1; |
851 | opt_reverse = 1; |
|
|
852 | else if (!strcmp (arg, "-whole")) |
|
|
853 | opt_whole = 1; |
|
|
854 | else if (!strcmp (arg, "-partial")) |
|
|
855 | opt_partial = 1; |
|
|
856 | else if (!strcmp (arg, "-update")) |
|
|
857 | opt_update = opt_partial = 1; |
595 | else if (!strcmp(argv[i], "-color")) |
858 | else if (!strcmp (arg, "-color")) |
596 | def_color = argv[++i]; |
859 | def_color = argv[++i]; |
597 | else if (!strcmp(argv[i], "-noinitial")) |
860 | else if (!strcmp (arg, "-noinitial")) |
598 | opt_noinitial = 1; |
861 | opt_noinitial = 1; |
|
|
862 | else if (!strcmp (arg, "-id")) |
|
|
863 | root = atoi (argv[++i]); |
599 | else if (!strcmp(argv[i], "-interval") || !strcmp(argv[i], "-i")) { |
864 | else if (!strcmp (arg, "-interval") || !strcmp (arg, "-i")) |
|
|
865 | { |
600 | double iv = atof(argv[++i]); |
866 | double iv = atof (argv[++i]); |
601 | |
867 | |
602 | interval.tv_sec = (int) iv; |
868 | interval.tv_sec = (int) iv; |
603 | interval.tv_usec = (iv - interval.tv_sec) * 1e6; |
869 | interval.tv_usec = (iv - interval.tv_sec) * 1e6; |
604 | } else { |
870 | } |
|
|
871 | else |
|
|
872 | { |
605 | fprintf(stderr, "Unknown option '%s'.\n" |
873 | fprintf (stderr, "Unknown option '%s'.\n" |
606 | "Try --help for more information.\n", argv[i]); |
874 | "Try --help for more information.\n", arg); |
607 | exit(1); |
875 | exit (1); |
608 | } |
876 | } |
609 | } else { /* it must be a filename */ |
877 | } |
|
|
878 | else |
|
|
879 | { /* it must be a filename */ |
610 | struct logfile_entry *e; |
880 | struct logfile_entry *e; |
611 | const char *fname, *desc, *fcolor = def_color; |
881 | const char *fname, *desc, *fcolor = def_color; |
612 | char *p; |
882 | char *p; |
613 | |
883 | |
|
|
884 | file_count++; |
|
|
885 | |
614 | /* this is not foolproof yet (',' in filenames are not allowed) */ |
886 | /* this is not foolproof yet (',' in filenames are not allowed) */ |
615 | fname = desc = argv[i]; |
887 | fname = desc = arg; |
616 | if ((p = strchr(argv[i], ','))) { |
888 | if ((p = strchr (arg, ','))) |
617 | *p = '\0'; |
889 | { |
618 | fcolor = p + 1; |
890 | *p = '\0'; |
|
|
891 | fcolor = p + 1; |
619 | |
892 | |
620 | if ((p = strchr(fcolor, ','))) { |
893 | if ((p = strchr (fcolor, ','))) |
621 | *p = '\0'; |
894 | { |
622 | desc = p + 1; |
895 | *p = '\0'; |
623 | } |
896 | desc = p + 1; |
624 | } |
897 | } |
|
|
898 | } |
625 | |
899 | |
626 | e = xmalloc(sizeof(struct logfile_entry)); |
900 | e = xmalloc (sizeof (struct logfile_entry)); |
|
|
901 | e->partial = 0; |
|
|
902 | e->buf = 0; |
|
|
903 | |
627 | if (argv[i][0] == '-' && argv[i][1] == '\0') { |
904 | if (arg[0] == '-' && arg[1] == '\0') |
|
|
905 | { |
628 | if ((e->fp = fdopen(0, "r")) == NULL) |
906 | if ((e->fp = fdopen (0, "r")) == NULL) |
629 | perror("fdopen"), exit(1); |
907 | perror ("fdopen"), exit (1); |
630 | if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) |
908 | if (fcntl (0, F_SETFL, O_NONBLOCK) < 0) |
631 | perror("fcntl"), exit(1); |
909 | perror ("fcntl"), exit (1); |
632 | e->fname = NULL; |
910 | e->fname = NULL; |
633 | e->inode = 0; |
911 | e->inode = 0; |
634 | e->desc = xstrdup("stdin"); |
912 | e->desc = xstrdup ("stdin"); |
635 | } else { |
913 | } |
636 | int l; |
914 | else |
|
|
915 | { |
|
|
916 | int l; |
637 | |
917 | |
638 | e->fname = xstrdup(fname); |
918 | e->fname = xstrdup (fname); |
639 | if (openlog(e) == NULL) |
919 | if (openlog (e) == NULL) |
640 | perror(fname), exit(1); |
920 | perror (fname), exit (1); |
641 | |
921 | |
642 | l = strlen(desc); |
922 | l = strlen (desc); |
643 | if (l > width - 2) /* must account for [ ] */ |
923 | if (l > width - 2) /* must account for [ ] */ |
644 | l = width - 2; |
924 | l = width - 2; |
645 | e->desc = xmalloc(l + 1); |
925 | e->desc = xmalloc (l + 1); |
646 | memcpy(e->desc, desc, l); |
926 | memcpy (e->desc, desc, l); |
647 | *(e->desc + l) = '\0'; |
927 | *(e->desc + l) = '\0'; |
|
|
928 | } |
|
|
929 | |
|
|
930 | e->color = GetColor (fcolor); |
|
|
931 | e->partial = 0; |
|
|
932 | e->next = NULL; |
|
|
933 | |
|
|
934 | if (!loglist) |
|
|
935 | loglist = e; |
|
|
936 | if (loglist_tail) |
|
|
937 | loglist_tail->next = e; |
|
|
938 | loglist_tail = e; |
|
|
939 | } |
648 | } |
940 | } |
649 | |
941 | |
650 | e->color = GetColor(fcolor); |
|
|
651 | e->next = NULL; |
|
|
652 | |
|
|
653 | if (!loglist) |
942 | if (!loglist) |
654 | loglist = e; |
|
|
655 | if (loglist_tail) |
|
|
656 | loglist_tail->next = e; |
|
|
657 | loglist_tail = e; |
|
|
658 | } |
|
|
659 | } |
943 | { |
660 | |
|
|
661 | if (!loglist) { |
|
|
662 | fprintf(stderr, "You did not specify any files to tail\n" |
944 | fprintf (stderr, "You did not specify any files to tail\n" |
663 | "use %s --help for help\n", argv[0]); |
945 | "use %s --help for help\n", argv[0]); |
664 | exit(1); |
946 | exit (1); |
|
|
947 | } |
|
|
948 | |
|
|
949 | if (opt_partial && opt_whole) |
665 | } |
950 | { |
|
|
951 | fprintf (stderr, "Specify at most one of -partial and -whole\n"); |
|
|
952 | exit (1); |
|
|
953 | } |
|
|
954 | |
|
|
955 | if (opt_partial) |
|
|
956 | /* if we specifically requested to see partial lines then don't insist on whole lines */ |
|
|
957 | opt_whole = 0; |
|
|
958 | else if (file_count > 1) |
|
|
959 | /* otherwise, if we've viewing multiple files, default to showing whole lines */ |
|
|
960 | opt_whole = 1; |
666 | |
961 | |
667 | #if HAS_REGEX |
962 | #if HAS_REGEX |
668 | if (transform) { |
963 | if (transform) |
669 | int i; |
964 | { |
|
|
965 | int i; |
670 | |
966 | |
671 | transformre = xmalloc(sizeof(transformre)); |
967 | transformre = xmalloc (sizeof (transformre)); |
672 | i = regcomp(&transformre, transform, REG_EXTENDED); |
968 | i = regcomp (&transformre, transform, REG_EXTENDED); |
673 | if (i != 0) { |
969 | if (i != 0) |
|
|
970 | { |
674 | char buf[512]; |
971 | char buf[512]; |
675 | |
972 | |
676 | regerror(i, &transformre, buf, sizeof(buf)); |
973 | regerror (i, &transformre, buf, sizeof (buf)); |
677 | fprintf(stderr, "Cannot compile regular expression: %s\n", buf); |
974 | fprintf (stderr, "Cannot compile regular expression: %s\n", buf); |
678 | } |
975 | } |
679 | } |
976 | } |
680 | #endif |
977 | #endif |
681 | |
978 | |
682 | InitWindow(); |
979 | InitWindow (); |
683 | |
980 | |
684 | install_signal(SIGINT, blank_window); |
981 | install_signal (SIGINT, blank_window); |
685 | install_signal(SIGQUIT, blank_window); |
982 | install_signal (SIGQUIT, blank_window); |
686 | install_signal(SIGTERM, blank_window); |
983 | install_signal (SIGTERM, blank_window); |
687 | install_signal(SIGHUP, force_reopen); |
984 | install_signal (SIGHUP, force_reopen); |
688 | install_signal(SIGUSR1, list_files); |
985 | install_signal (SIGUSR1, list_files); |
689 | install_signal(SIGUSR2, force_refresh); |
986 | install_signal (SIGUSR2, force_refresh); |
690 | |
987 | |
691 | if (opt_daemonize) |
988 | if (opt_daemonize) |
692 | daemonize(); |
989 | daemonize (); |
693 | |
990 | |
694 | main_loop(); |
991 | main_loop (); |
695 | |
992 | |
696 | exit(1); /* to make gcc -Wall stop complaining */ |
993 | exit (1); /* to make gcc -Wall stop complaining */ |
697 | } |
994 | } |
698 | |
995 | |
|
|
996 | void |
699 | void install_signal(int sig, void (*handler)(int)) |
997 | install_signal (int sig, void (*handler) (int)) |
700 | { |
998 | { |
701 | struct sigaction action; |
999 | struct sigaction action; |
702 | |
1000 | |
703 | action.sa_handler = handler; |
1001 | action.sa_handler = handler; |
704 | sigemptyset(&action.sa_mask); |
1002 | sigemptyset (&action.sa_mask); |
705 | action.sa_flags = SA_RESTART; |
1003 | action.sa_flags = SA_RESTART; |
|
|
1004 | |
706 | if (sigaction(sig, &action, NULL) < 0) |
1005 | if (sigaction (sig, &action, NULL) < 0) |
707 | fprintf(stderr, "sigaction(%d): %s\n", sig, strerror(errno)), exit(1); |
1006 | fprintf (stderr, "sigaction(%d): %s\n", sig, strerror (errno)), exit (1); |
708 | } |
1007 | } |
709 | |
1008 | |
|
|
1009 | void * |
710 | void *xstrdup(const char *string) |
1010 | xstrdup (const char *string) |
711 | { |
1011 | { |
712 | void *p; |
1012 | void *p; |
713 | |
1013 | |
714 | while ((p = strdup(string)) == NULL) { |
1014 | while ((p = strdup (string)) == NULL) |
|
|
1015 | { |
715 | fprintf(stderr, "Memory exausted."); |
1016 | fprintf (stderr, "Memory exausted."); |
716 | sleep(10); |
1017 | sleep (10); |
717 | } |
1018 | } |
|
|
1019 | |
718 | return p; |
1020 | return p; |
719 | } |
1021 | } |
720 | |
1022 | |
|
|
1023 | void * |
721 | void *xmalloc(size_t size) |
1024 | xmalloc (size_t size) |
722 | { |
1025 | { |
723 | void *p; |
1026 | void *p; |
724 | |
1027 | |
725 | while ((p = malloc(size)) == NULL) { |
1028 | while ((p = malloc (size)) == NULL) |
|
|
1029 | { |
726 | fprintf(stderr, "Memory exausted."); |
1030 | fprintf (stderr, "Memory exausted."); |
727 | sleep(10); |
1031 | sleep (10); |
728 | } |
1032 | } |
|
|
1033 | |
729 | return p; |
1034 | return p; |
730 | } |
1035 | } |
731 | |
1036 | |
|
|
1037 | void |
732 | void display_help(char *myname) |
1038 | display_help (char *myname) |
733 | { |
1039 | { |
734 | printf("Usage: %s [options] file1[,color[,desc]] " |
1040 | printf ("Usage: %s [options] file1[,color[,desc]] " |
735 | "[file2[,color[,desc]] ...]\n", myname); |
1041 | "[file2[,color[,desc]] ...]\n", myname); |
736 | printf(" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n" |
1042 | printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n" |
737 | " -color color use color $color as default\n" |
1043 | " -color color use color $color as default\n" |
738 | " -reload sec command reload after $sec and run command\n" |
1044 | " -reload sec command reload after $sec and run command\n" |
739 | " by default -- 3 mins\n" |
1045 | " -id id window id to use instead of the root window\n" |
740 | " -font FONTSPEC (-fn) font to use\n" |
1046 | " -font FONTSPEC (-fn) font to use\n" |
741 | " -f | -fork fork into background\n" |
1047 | " -f | -fork fork into background\n" |
742 | " -reverse print new lines at the top\n" |
1048 | " -reverse print new lines at the top\n" |
|
|
1049 | " -whole wait for \\n before showing a line\n" |
|
|
1050 | " -partial show lines even if they don't end with a \\n\n" |
|
|
1051 | " -update allow updates to old partial lines\n" |
|
|
1052 | " -cont string to prefix continued partial lines with\n" |
|
|
1053 | " defaults to \"[+]\"\n" |
743 | " -shade add shading to font\n" |
1054 | " -shade add shading to font\n" |
744 | " -noinitial don't display the last file lines on\n" |
1055 | " -noinitial don't display the last file lines on\n" |
745 | " startup\n" |
1056 | " startup\n" |
746 | " -i | -interval seconds interval between checks (fractional\n" |
1057 | " -i | -interval seconds interval between checks (fractional\n" |
747 | " values o.k.). Default 3\n" |
1058 | " values o.k.). Default 3 seconds\n" |
748 | " -V display version information and exit\n" |
1059 | " -V display version information and exit\n" |
749 | "\n"); |
1060 | "\n"); |
750 | printf("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green " |
1061 | printf ("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green " |
751 | "/var/log/secure,red,'ALERT'\n", myname); |
1062 | "/var/log/secure,red,'ALERT'\n", myname); |
752 | exit(0); |
1063 | exit (0); |
753 | } |
1064 | } |
754 | |
1065 | |
|
|
1066 | void |
755 | void display_version(void) { |
1067 | display_version (void) |
|
|
1068 | { |
756 | printf("root-tail version " VERSION "\n"); |
1069 | printf ("root-tail version " VERSION "\n"); |
757 | exit(0); |
1070 | exit (0); |
758 | } |
1071 | } |
759 | |
1072 | |
|
|
1073 | int |
760 | int daemonize(void) { |
1074 | daemonize (void) |
|
|
1075 | { |
761 | switch (fork()) { |
1076 | switch (fork ()) |
|
|
1077 | { |
762 | case -1: |
1078 | case -1: |
763 | return -1; |
1079 | return -1; |
764 | case 0: |
1080 | case 0: |
765 | break; |
1081 | break; |
766 | default: |
1082 | default: |
767 | _exit(0); |
1083 | _exit (0); |
768 | } |
1084 | } |
769 | |
1085 | |
770 | if (setsid() == -1) |
1086 | if (setsid () == -1) |
771 | return -1; |
1087 | return -1; |
772 | |
1088 | |
773 | return 0; |
1089 | return 0; |
774 | } |
1090 | } |
775 | |
|
|