Mercurial > hg > Members > shinya > pyrect
annotate src/template/grep.c @ 24:ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 06 Jul 2010 05:39:41 +0900 |
parents | |
children | 3db85244784b |
rev | line source |
---|---|
24
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 /* Excerpted from 'The Practice of Programming' */ |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 /* by Brian W. Kernighan and Rob Pike */ |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include <stdio.h> |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 #include <stdlib.h> |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 #include <string.h> |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 #define BUFSIZE 1024 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 int match(char *regexp, char *text) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 if (regexp[0] == '^') |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 return matchhere(regexp+1, text); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 do { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 if (matchhere(regexp+1, text)) |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 return 1; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 } while (*text++ != '\0'); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 return 0; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 int matchhere(char *regexp, char *text) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 if (regexp[0] == '\0') |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 return 1; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 if (regexp[1] == '*') |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 return matchstar(regexp[0], regexp+2, text); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 if (regexp[0] == '$' && regexp[1] == '\0') |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 return *text == '\0'; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text)) |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 return matchhere(regexp+1, text+1); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 return 0; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 int matchstar(int c, char *regexp, char *text) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 do { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 if (matchhere(regexp, text)) |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 return 1; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 } while (*text != '\0' && (*text++ == c || c == '.')); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 return 0; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 int grep(char * regexp, FILE *f, char *name) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 int n, nmatch; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 char buf[BUFSIZE]; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 nmatch = 0; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 while (fgets(buf, sizeof buf, f) != NULL) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 n = strlen(buf); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 if (n > 0 && buf[n-1] == '\n') |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 buf[n-1] = '\0'; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 if (match(regexp, buf)) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 nmatch++; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 if (name != NULL) |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 printf("%s:", name); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 printf("%s\n", buf); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 return nmatch; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 int main(int argc, char* argv[]) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 int i, nmatch; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 FILE *f; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 if (argc < 2) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 fprintf(stderr, "usage: grep regexp [file ...]"); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 exit(0); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 nmatch = 0; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 if (argc == 2) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 if (grep(argv[1], stdin, NULL)) |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 nmatch; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 } else { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 for (i = 2; i < argc; i++) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 f = fopen(argv[i], "r"); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 if (f == NULL) { |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 fprintf(stderr, "can't open %s:", argv[i]); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 continue; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 if (grep(argv[1], f, argc > 3 ? argv[i] : NULL) > 0) |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 nmatch++; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 fclose(f); |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 } |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 return nmatch; |
ceb0b8a73d68
add grep.c, it was introduced by Brian W. Kernighan and Rob Pike in "The Practice of Programming"
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 } |