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