ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
(Generate patch)

Comparing root-tail/root-tail.c (file contents):
Revision 1.64 by root, Tue Nov 23 15:54:41 2004 UTC vs.
Revision 1.65 by root, Sun Jun 16 01:24:29 2019 UTC

39#include <ctype.h> 39#include <ctype.h>
40#include <stdarg.h> 40#include <stdarg.h>
41#include <X11/Xlib.h> 41#include <X11/Xlib.h>
42#include <X11/Xatom.h> 42#include <X11/Xatom.h>
43#include <X11/Xutil.h> 43#include <X11/Xutil.h>
44#include <X11/extensions/shape.h>
45#include <X11/extensions/Xfixes.h>
44 46
45#if HAS_REGEX 47#if HAS_REGEX
46#include <regex.h> 48#include <regex.h>
47#endif 49#endif
48 50
105 int buffer_size; 107 int buffer_size;
106 unsigned long color; 108 unsigned long color;
107}; 109};
108 110
109/* global variables */ 111/* global variables */
110struct line_node *linelist = NULL, *linelist_tail = NULL; 112static struct line_node *linelist = NULL, *linelist_tail = NULL;
111struct displaymatrix *display; 113static struct displaymatrix *display;
112int continuation_width = -1; 114static int continuation_width = -1;
113int continuation_color; 115static int continuation_color;
114int continuation_length; 116static int continuation_length;
115 117
116/* HACK - ideally listlen will start at however many '~'s will fit on 118/* HACK - ideally listlen will start at however many '~'s will fit on
117 * the screen */ 119 * the screen */
118int width = STD_WIDTH, height = STD_HEIGHT, listlen = 50; 120static unsigned int width = STD_WIDTH, height = STD_HEIGHT;
121static int listlen = 50;
119int win_x = LOC_X, win_y = LOC_Y; 122static int win_x = LOC_X, win_y = LOC_Y;
120int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */ 123static int effect_x_space, effect_y_space; /* how much space does shading / outlining take up */
121int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */ 124static int effect_x_offset, effect_y_offset; /* and how does it offset the usable space */
122int do_reopen; 125static int do_reopen;
123struct timeval interval = { 2, 400000 }; 126static struct timeval interval = { 2, 400000 };
124 127
125/* command line options */ 128/* command line options */
126int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename, 129static int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
127 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap, 130 opt_outline, opt_noflicker, opt_whole, opt_update, opt_wordwrap,
128 opt_justify, geom_mask, opt_minspace, reload; 131 opt_justify, geom_mask, opt_minspace, opt_windowed, reload;
129const char *command = NULL, 132static const char *command = NULL,
130 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR, 133 *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR,
131 *continuation = "|| ", *cont_color = DEF_CONT_COLOR; 134 *continuation = "|| ", *cont_color = DEF_CONT_COLOR;
132 135
133struct logfile_entry *loglist = NULL, *loglist_tail = NULL; 136struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
134 137
135Display *disp; 138static Display *disp;
136Window root; 139static Window root;
137GC WinGC; 140static GC WinGC;
138 141
139#if HAS_REGEX 142#if HAS_REGEX
140struct re_list 143struct re_list
141{ 144{
142 regex_t from; 145 regex_t from;
143 const char *to; 146 const char *to;
144 struct re_list *next; 147 struct re_list *next;
145}; 148};
146struct re_list *re_head, *re_tail; 149static struct re_list *re_head, *re_tail;
147char *transform_to = NULL; 150static char *transform_to = NULL;
148regex_t *transformre; 151static regex_t *transformre;
149#endif 152#endif
150 153
151 154
152/* prototypes */ 155/* prototypes */
153void list_files (int); 156static void list_files (int);
154void force_reopen (int); 157static void force_reopen (int);
155void force_refresh (int); 158static void force_refresh (int);
156void blank_window (int); 159static void blank_window (int);
157#ifdef USE_TOON_GET_ROOT_WINDOW 160#ifdef USE_TOON_GET_ROOT_WINDOW
158Window ToonGetRootWindow(Display *, int, Window *); 161static Window ToonGetRootWindow(Display *, int, Window *);
159#endif /* USE_TOON_GET_ROOT_WINDOW */ 162#endif /* USE_TOON_GET_ROOT_WINDOW */
160 163
161void InitWindow (void); 164static void InitWindow (void);
162unsigned long GetColor (const char *); 165static unsigned long GetColor (const char *);
163void redraw (int); 166static void redraw (int);
164void refresh (int, int, int, int); 167static void refresh (int, int, int, int);
165 168
166void transform_line (char *s);
167int lineinput (struct logfile_entry *); 169static int lineinput (struct logfile_entry *);
168void reopen (void); 170static void reopen (void);
169void check_open_files (void); 171static void check_open_files (void);
170FILE *openlog (struct logfile_entry *); 172static FILE *openlog (struct logfile_entry *);
171static void main_loop (void); 173static void main_loop (void);
172 174
173void display_version (void); 175static void display_version (void);
174void display_help (char *); 176static void display_help (char *);
175void install_signal (int, void (*)(int)); 177static void install_signal (int, void (*)(int));
176void *xstrdup (const char *); 178static void *xstrdup (const char *);
177void *xmalloc (size_t); 179static void *xmalloc (size_t);
178void *xrealloc (void *, size_t); 180static void *xrealloc (void *, size_t);
179int daemonize (void); 181static int daemonize (void);
180 182
181/* signal handlers */ 183/* signal handlers */
182void 184static void
183list_files (int dummy) 185list_files (int dummy)
184{ 186{
185 struct logfile_entry *e; 187 struct logfile_entry *e;
186 188
187 fprintf (stderr, "Files opened:\n"); 189 fprintf (stderr, "Files opened:\n");
188 for (e = loglist; e; e = e->next) 190 for (e = loglist; e; e = e->next)
189 fprintf (stderr, "\t%s (%s)\n", e->fname, e->desc); 191 fprintf (stderr, "\t%s (%s)\n", e->fname, e->desc);
190} 192}
191 193
192void 194static void
193force_reopen (int dummy) 195force_reopen (int dummy)
194{ 196{
195 do_reopen = 1; 197 do_reopen = 1;
196} 198}
197 199
198void 200static void
199force_refresh (int dummy) 201force_refresh (int dummy)
200{ 202{
201 redraw (1); 203 redraw (1);
202} 204}
203 205
204void 206static void
205blank_window (int dummy) 207blank_window (int dummy)
206{ 208{
207 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False); 209 XClearArea (disp, root, win_x, win_y, width + MARGIN_OF_ERROR, height, False);
208 XFlush (disp); 210 XFlush (disp);
209 exit (0); 211 exit (0);
210} 212}
211 213
212/* X related functions */ 214/* X related functions */
213unsigned long 215static unsigned long
214GetColor (const char *ColorName) 216GetColor (const char *ColorName)
215{ 217{
216 XColor Color; 218 XColor Color;
217 XWindowAttributes Attributes; 219 XWindowAttributes Attributes;
218 220
281 fprintf (stderr, "Can't query tree on root window 0x%lx", root); 283 fprintf (stderr, "Can't query tree on root window 0x%lx", root);
282 } 284 }
283} 285}
284#endif /* USE_TOON_GET_ROOT_WINDOW */ 286#endif /* USE_TOON_GET_ROOT_WINDOW */
285 287
286void 288static void
287InitWindow (void) 289InitWindow (void)
288{ 290{
289 XGCValues gcv; 291 XGCValues gcv;
290 unsigned long gcm; 292 unsigned long gcm;
291 int screen, ScreenWidth, ScreenHeight; 293 int screen, ScreenWidth, ScreenHeight;
299 301
300 screen = DefaultScreen (disp); 302 screen = DefaultScreen (disp);
301 ScreenHeight = DisplayHeight (disp, screen); 303 ScreenHeight = DisplayHeight (disp, screen);
302 ScreenWidth = DisplayWidth (disp, screen); 304 ScreenWidth = DisplayWidth (disp, screen);
303 305
306 if (opt_windowed)
307 {
308 XRectangle rect = { };
309 XSetWindowAttributes attr;
310
311 attr.background_pixmap = ParentRelative;
312 attr.override_redirect = True;
313
314 root = XCreateWindow (
315 disp, DefaultRootWindow (disp), 0, 0, DisplayWidth (disp, screen), DisplayHeight (disp, screen),
316 0, CopyFromParent, InputOutput, CopyFromParent,
317 CWOverrideRedirect | CWBackPixmap, &attr);
318
319 XMapWindow (disp, root);
320 XLowerWindow (disp, root);
321
322 XserverRegion region = XFixesCreateRegion (disp, &rect, 1);
323 XFixesSetWindowShapeRegion (disp, root, ShapeInput, 0, 0, region);
324 XFixesDestroyRegion (disp, region);
325 }
326 else
304 find_root_window (disp, screen); 327 find_root_window (disp, screen);
305 328
306 gcm = GCBackground; 329 gcm = GCBackground;
307 gcv.graphics_exposures = True; 330 gcv.graphics_exposures = True;
308 WinGC = XCreateGC (disp, root, gcm, &gcv); 331 WinGC = XCreateGC (disp, root, gcm, &gcv);
309 XMapWindow (disp, root); 332 XMapWindow (disp, root);
317 char *def_string; 340 char *def_string;
318 341
319 e->fontset = XCreateFontSet (disp, e->fontname, 342 e->fontset = XCreateFontSet (disp, e->fontname,
320 &missing_charset_list, &missing_charset_count, 343 &missing_charset_list, &missing_charset_count,
321 &def_string); 344 &def_string);
322
323 if (missing_charset_count) 345 if (missing_charset_count)
324 { 346 {
347#if 0
325 fprintf (stderr, 348 fprintf (stderr,
326 "Missing charsets in String to FontSet conversion (%s)\n", 349 "Missing charsets in String to FontSet conversion (%s)\n",
327 missing_charset_list[0]); 350 missing_charset_list[0]);
351#endif
328 XFreeStringList (missing_charset_list); 352 XFreeStringList (missing_charset_list);
329 } 353 }
330 354
331 if (!e->fontset) 355 if (!e->fontset)
332 { 356 {
376 * -noflicker is in effect) then only the lines which have changed 400 * -noflicker is in effect) then only the lines which have changed
377 * since the last draw are redrawn. 401 * since the last draw are redrawn.
378 * 402 *
379 * the rest is handled by regular refresh ()'es 403 * the rest is handled by regular refresh ()'es
380 */ 404 */
381void 405static void
382redraw (int redraw_all) 406redraw (int redraw_all)
383{ 407{
384 XSetClipMask (disp, WinGC, None); 408 XSetClipMask (disp, WinGC, None);
385 refresh (0, 32768, 1, redraw_all); 409 refresh (0, 32768, 1, redraw_all);
386} 410}
387 411
412static void
388void draw_text (Display *disp, Window root, GC WinGC, int x, int y, struct line_node *line, int foreground) 413draw_text (Display *disp, Window root, GC WinGC, int x, int y, struct line_node *line, int foreground)
389{ 414{
390 if (line->wrapped_right && opt_justify && line->breaks) 415 if (line->wrapped_right && opt_justify && line->breaks)
391 { 416 {
392 int i; 417 int i;
393 for (i = 0; i < line->num_words; i++) 418 for (i = 0; i < line->num_words; i++)
415 } 440 }
416 } 441 }
417} 442}
418 443
419/* Just redraw everything without clearing (i.e. after an EXPOSE event) */ 444/* Just redraw everything without clearing (i.e. after an EXPOSE event) */
420void 445static void
421refresh (int miny, int maxy, int clear, int refresh_all) 446refresh (int miny, int maxy, int clear, int refresh_all)
422{ 447{
423 int lin = 0; 448 int lin = 0;
424 int space = height; 449 int space = height;
425 int offset; 450 int offset;
593 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1); 618 XDrawRectangle (disp, root, WinGC, win_x - 0, win_y - 0, width - 1, height - 1);
594 } 619 }
595} 620}
596 621
597#if HAS_REGEX 622#if HAS_REGEX
598void 623void void
599transform_line (char *s) 624transform_line (char *s)
600{ 625{
601#ifdef I_AM_Md 626#ifdef I_AM_Md
602 int i; 627 int i;
603 if (1) 628 if (1)
648 673
649/* 674/*
650 * appends p2 to the end of p1, if p1 is not null 675 * appends p2 to the end of p1, if p1 is not null
651 * otherwise allocates a new string and copies p2 to it 676 * otherwise allocates a new string and copies p2 to it
652 */ 677 */
653char * 678static char *
654concat_line (char *p1, const char *p2) 679concat_line (char *p1, const char *p2)
655{ 680{
656 int l1 = p1 ? strlen (p1) : 0; 681 int l1 = p1 ? strlen (p1) : 0;
657 int l2 = strlen (p2); 682 int l2 = strlen (p2);
658 char *r; 683 char *r;
671} 696}
672 697
673/* 698/*
674 * This routine can read a line of any length if it is called enough times. 699 * This routine can read a line of any length if it is called enough times.
675 */ 700 */
676int 701static int
677lineinput (struct logfile_entry *logfile) 702lineinput (struct logfile_entry *logfile)
678{ 703{
679 char buff[1024], *p; 704 char buff[1024], *p;
680 int ch; 705 int ch;
681 /* HACK-2: add on the length of any partial line which we will be appending to */ 706 /* HACK-2: add on the length of any partial line which we will be appending to */
740/* input: reads file->fname 765/* input: reads file->fname
741 * output: fills file->fp, file->inode 766 * output: fills file->fp, file->inode
742 * returns file->fp 767 * returns file->fp
743 * in case of error, file->fp is NULL 768 * in case of error, file->fp is NULL
744 */ 769 */
745FILE * 770static FILE *
746openlog (struct logfile_entry * file) 771openlog (struct logfile_entry *file)
747{ 772{
748 struct stat stats; 773 struct stat stats;
749 774
750 if ((file->fp = fopen (file->fname, "r")) == NULL) 775 if ((file->fp = fopen (file->fname, "r")) == NULL)
751 { 776 {
772 797
773 file->last_size = stats.st_size; 798 file->last_size = stats.st_size;
774 return file->fp; 799 return file->fp;
775} 800}
776 801
777void 802static void
778reopen (void) 803reopen (void)
779{ 804{
780 struct logfile_entry *e; 805 struct logfile_entry *e;
781 806
782 for (e = loglist; e; e = e->next) 807 for (e = loglist; e; e = e->next)
791 } 816 }
792 817
793 do_reopen = 0; 818 do_reopen = 0;
794} 819}
795 820
796void 821static void
797check_open_files (void) 822check_open_files (void)
798{ 823{
799 struct logfile_entry *e; 824 struct logfile_entry *e;
800 struct stat stats; 825 struct stat stats;
801 826
1271 region = XCreateRegion (); 1296 region = XCreateRegion ();
1272 } 1297 }
1273 } 1298 }
1274} 1299}
1275 1300
1276
1277int 1301int
1278main (int argc, char *argv[]) 1302main (int argc, char *argv[])
1279{ 1303{
1280 int i; 1304 int i;
1281 int opt_daemonize = 0; 1305 int opt_daemonize = 0;
1324 else if (!strcmp (arg, "-reload")) 1348 else if (!strcmp (arg, "-reload"))
1325 { 1349 {
1326 reload = atoi (argv[++i]); 1350 reload = atoi (argv[++i]);
1327 command = argv[++i]; 1351 command = argv[++i];
1328 } 1352 }
1353 else if (!strcmp (arg, "-windowed"))
1354 opt_windowed = 1;
1329 else if (!strcmp (arg, "-shade")) 1355 else if (!strcmp (arg, "-shade"))
1330 opt_shade = 1; 1356 opt_shade = 1;
1331 else if (!strcmp (arg, "-outline")) 1357 else if (!strcmp (arg, "-outline"))
1332 opt_outline = 1; 1358 opt_outline = 1;
1333 else if (!strcmp (arg, "-minspace")) 1359 else if (!strcmp (arg, "-minspace"))
1537 main_loop (); 1563 main_loop ();
1538 1564
1539 exit (1); /* to make gcc -Wall stop complaining */ 1565 exit (1); /* to make gcc -Wall stop complaining */
1540} 1566}
1541 1567
1542void 1568static void
1543install_signal (int sig, void (*handler) (int)) 1569install_signal (int sig, void (*handler) (int))
1544{ 1570{
1545 struct sigaction action; 1571 struct sigaction action;
1546 1572
1547 action.sa_handler = handler; 1573 action.sa_handler = handler;
1550 1576
1551 if (sigaction (sig, &action, NULL) < 0) 1577 if (sigaction (sig, &action, NULL) < 0)
1552 fprintf (stderr, "sigaction (%d): %s\n", sig, strerror (errno)), exit (1); 1578 fprintf (stderr, "sigaction (%d): %s\n", sig, strerror (errno)), exit (1);
1553} 1579}
1554 1580
1555void * 1581static void *
1556xstrdup (const char *string) 1582xstrdup (const char *string)
1557{ 1583{
1558 void *p; 1584 void *p;
1559 1585
1560 while ((p = strdup (string)) == NULL) 1586 while ((p = strdup (string)) == NULL)
1564 } 1590 }
1565 1591
1566 return p; 1592 return p;
1567} 1593}
1568 1594
1569void * 1595static void *
1570xmalloc (size_t size) 1596xmalloc (size_t size)
1571{ 1597{
1572 void *p; 1598 void *p;
1573 1599
1574 while ((p = malloc (size)) == NULL) 1600 while ((p = malloc (size)) == NULL)
1578 } 1604 }
1579 1605
1580 return p; 1606 return p;
1581} 1607}
1582 1608
1583void * 1609static void *
1584xrealloc (void *ptr, size_t size) 1610xrealloc (void *ptr, size_t size)
1585{ 1611{
1586 void *p; 1612 void *p;
1587 1613
1588 while ((p = realloc (ptr, size)) == NULL) 1614 while ((p = realloc (ptr, size)) == NULL)
1592 } 1618 }
1593 1619
1594 return p; 1620 return p;
1595} 1621}
1596 1622
1597void 1623static void
1598display_help (char *myname) 1624display_help (char *myname)
1599{ 1625{
1600 printf ("Usage: %s [options] file1[,color[,desc]]" 1626 printf ("Usage: %s [options] file1[,color[,desc]]"
1601 "[options] [file2[,color[,desc]] ...]\n", myname); 1627 "[options] [file2[,color[,desc]] ...]\n", myname);
1602 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n" 1628 printf (" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
1603 " -color color use color $color as default\n" 1629 " -color color use color $color as default\n"
1604 " -reload sec command reload after $sec and run command\n" 1630 " -reload sec command reload after $sec and run command\n"
1605 " -id id window id to use instead of the root window\n" 1631 " -id id window id to use instead of the root window\n"
1632 " -windowed create a window instead of writing to the root\n"
1606 " -font FONTSPEC (-fn) font to use\n" 1633 " -font FONTSPEC (-fn) font to use\n"
1607 " -f | -fork fork into background\n" 1634 " -f | -fork fork into background\n"
1608 " -reverse print new lines at the top\n" 1635 " -reverse print new lines at the top\n"
1609 " -whole wait for \\n before showing a line\n" 1636 " -whole wait for \\n before showing a line\n"
1610 " -partial show lines even if they don't end with a \\n\n" 1637 " -partial show lines even if they don't end with a \\n\n"
1624 printf ("Example:\n%s -g 800x250+100+50 -font fixed /var/log/messages,green " 1651 printf ("Example:\n%s -g 800x250+100+50 -font fixed /var/log/messages,green "
1625 "/var/log/secure,red,'ALERT'\n", myname); 1652 "/var/log/secure,red,'ALERT'\n", myname);
1626 exit (0); 1653 exit (0);
1627} 1654}
1628 1655
1629void 1656static void
1630display_version (void) 1657display_version (void)
1631{ 1658{
1632 printf ("root-tail version " VERSION "\n"); 1659 printf ("root-tail version " VERSION "\n");
1633 exit (0); 1660 exit (0);
1634} 1661}
1635 1662
1636int 1663static int
1637daemonize (void) 1664daemonize (void)
1638{ 1665{
1639 pid_t pid; 1666 pid_t pid;
1640 1667
1641 switch (pid = fork ()) 1668 switch (pid = fork ())
1652 if (setsid () == -1) 1679 if (setsid () == -1)
1653 return -1; 1680 return -1;
1654 1681
1655 return 0; 1682 return 0;
1656} 1683}
1684

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines