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