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