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.25 by chris_moore, Sun Mar 28 03:53:48 2004 UTC vs.
Revision 1.26 by chris_moore, Sun Mar 28 21:53:09 2004 UTC

31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <sys/types.h> 32#include <sys/types.h>
33#include <locale.h> 33#include <locale.h>
34#include <ctype.h> 34#include <ctype.h>
35#include <stdarg.h> 35#include <stdarg.h>
36#include <X11/Xlib.h>
37#include <X11/Xatom.h>
38#include <X11/Xutil.h>
39
36#if HAS_REGEX 40#if HAS_REGEX
37#include <regex.h> 41#include <regex.h>
38#endif 42#endif
39#include <X11/Xlib.h>
40#include <X11/Xatom.h>
41#include <X11/Xutil.h>
42 43
43/* data structures */ 44/* data structures */
44struct logfile_entry 45struct logfile_entry
45{ 46{
46 struct logfile_entry *next; 47 struct logfile_entry *next;
93 regex_t from; 94 regex_t from;
94 const char *to; 95 const char *to;
95 struct re_list *next; 96 struct re_list *next;
96}; 97};
97struct re_list *re_head, *re_tail; 98struct re_list *re_head, *re_tail;
99char *transform_to = NULL;
100regex_t *transformre;
98#endif 101#endif
99 102
100 103
101/* prototypes */ 104/* prototypes */
102void list_files (int); 105void list_files (int);
396 if (transformre) 399 if (transformre)
397 { 400 {
398 int i; 401 int i;
399 regmatch_t matched[16]; 402 regmatch_t matched[16];
400 403
401 i = regexec (&transformre, string, 16, matched, 0); 404 i = regexec (transformre, s, 16, matched, 0);
402 if (i == 0) 405 if (i == 0)
403 { /* matched */ 406 { /* matched */
407 int match_start = matched[0].rm_so;
408 int match_end = matched[0].rm_eo;
409 int old_len = match_end - match_start;
410 int new_len = strlen(transform_to);
411 int old_whole_len = strlen(s);
412 printf("regexp was matched by '%s' - replace with '%s'\n", s, transform_to);
413 printf("match is from %d to %d\n",
414 match_start, match_end);
415 if (new_len > old_len) {
416 s = realloc(s, old_whole_len + new_len - old_len);
417 }
418 if (new_len != old_len) {
419 memcpy(s + match_end + new_len - old_len,
420 s + match_end,
421 old_whole_len - match_end);
422 s[old_whole_len + new_len - old_len] = '\0';
423 }
424 memcpy(s + match_start,
425 transform_to,
426 new_len);
427 printf("transformed to '%s'\n", s);
404 } 428 }
429 else
430 {
431 printf("regexp was not matched by '%s'\n", s);
432 }
405 } 433 }
406} 434}
407#endif 435#endif
408 436
409char * 437char *
426int 454int
427lineinput (struct logfile_entry *logfile) 455lineinput (struct logfile_entry *logfile)
428{ 456{
429 char buff[1024], *p = buff; 457 char buff[1024], *p = buff;
430 int ch; 458 int ch;
459 /* HACK this - to add on the length of any partial line which we will be appending to */
431 int ofs = logfile->buf ? strlen (logfile->buf) : 0; 460 int ofs = logfile->buf ? strlen (logfile->buf) : 0;
432 461
433 do 462 do
434 { 463 {
435 ch = fgetc (logfile->fp); 464 ch = fgetc (logfile->fp);
894 continuation = argv[++i]; 923 continuation = argv[++i];
895 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn")) 924 else if (!strcmp (arg, "-font") || !strcmp (arg, "-fn"))
896 fontname = argv[++i]; 925 fontname = argv[++i];
897#if HAS_REGEX 926#if HAS_REGEX
898 else if (!strcmp (arg, "-t")) 927 else if (!strcmp (arg, "-t"))
928 {
899 transform = argv[++i]; 929 transform = argv[++i];
930 transform_to = argv[++i];
931 printf("transform: '%s' to '%s'\n", transform, transform_to);
932 }
900#endif 933#endif
901 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f")) 934 else if (!strcmp (arg, "-fork") || !strcmp (arg, "-f"))
902 opt_daemonize = 1; 935 opt_daemonize = 1;
903 else if (!strcmp (arg, "-reload")) 936 else if (!strcmp (arg, "-reload"))
904 { 937 {
1033#if HAS_REGEX 1066#if HAS_REGEX
1034 if (transform) 1067 if (transform)
1035 { 1068 {
1036 int i; 1069 int i;
1037 1070
1071 printf("compiling regexp '%s'\n", transform);
1038 transformre = xmalloc (sizeof (transformre)); 1072 transformre = xmalloc (sizeof (regex_t));
1039 i = regcomp (&transformre, transform, REG_EXTENDED); 1073 i = regcomp (transformre, transform, REG_EXTENDED);
1040 if (i != 0) 1074 if (i != 0)
1041 { 1075 {
1042 char buf[512]; 1076 char buf[512];
1043 1077
1044 regerror (i, &transformre, buf, sizeof (buf)); 1078 regerror (i, transformre, buf, sizeof (buf));
1045 fprintf (stderr, "Cannot compile regular expression: %s\n", buf); 1079 fprintf (stderr, "Cannot compile regular expression: %s\n", buf);
1046 } 1080 }
1081 else
1082 {
1083 printf("compiled '%s' OK to %x\n", transform, (int)transformre);
1084 }
1047 } 1085 }
1048#endif 1086#endif
1049 1087
1050 InitWindow (); 1088 InitWindow ();
1051 1089
1143} 1181}
1144 1182
1145int 1183int
1146daemonize (void) 1184daemonize (void)
1147{ 1185{
1186 pid_t pid;
1187
1148 switch (fork ()) 1188 switch (pid = fork ())
1149 { 1189 {
1150 case -1: 1190 case -1:
1151 return -1; 1191 return -1;
1152 case 0: 1192 case 0:
1153 break; 1193 break;
1154 default: 1194 default:
1195 printf("%d\n", pid);
1155 _exit (0); 1196 exit (0);
1156 } 1197 }
1157 1198
1158 if (setsid () == -1) 1199 if (setsid () == -1)
1159 return -1; 1200 return -1;
1160 1201

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines