/* * Reads trace output, printing only output for which cwp has a * particular value. * * Usage: $0 [flags] value * * value is passed to atoi; the result is the cwp value this program * looks for. */ #include #include #include #include //#include extern const char *__progname; static int cwp; static int wantcwp; #define Cisdigit(x) isdigit((unsigned char)(x)) static void setup(int ac, char **av) { int i; int errs; errs = 0; while ((ac >= 2) && (av[1][0] == '-')) { i = 1; fprintf(stderr,"%s: unrecognized flag `%s'\n",__progname,av[1]); errs = 1; ac -= i; av += i; } if (ac != 2) { fprintf(stderr,"Usage: %s [flags] value\n",__progname); exit(1); } cwp = -1; wantcwp = atoi(av[1]); } static int want(const char *s0) { const char *s; int w; s = s0; while (*s && Cisdigit(*s)) s ++; if (*s++ != ':') return(wantcwp==cwp); if (*s++ != ' ') return(wantcwp==cwp); while (*s && Cisdigit(*s)) s ++; if (*s++ != ':') return(wantcwp==cwp); if (*s++ != ' ') return(wantcwp==cwp); if ((s[0] != 'c') || (s[1] != 'h') || (s[2] != 'g')) return(wantcwp==cwp); s += 3; w = (wantcwp == cwp); while (1) { while (*s == ' ') s ++; if (! *s) break; if ((s[0] == 'c') && (s[1] == 'w') && (s[2] == 'p') && (s[3] == '=')) { cwp = atoi(s+4); break; } while (*s && (*s != ' ')) s ++; } return(w||(wantcwp==cwp)); } static void process(void) { char *b; int l; int a; int c; b = 0; a = 0; l = 0; while (1) { c = getchar(); if (c == EOF) { if (l > 0) fprintf(stderr,"%s: partial line ignored at end of input\n",__progname); break; } if (l >= a) b = realloc(b,a=l+8); b[l++] = c; if (c == '\n') { b[--l] = '\0'; if (want(b)) { printf("%s\n",b); } l = 0; } } free(b); } int main(int, char **); int main(int ac, char **av) { setup(ac,av); process(); return(0); }