annotate regen/src/grep/grep_jobs.cbc.c~ @ 12:a6f51c76dea8

add grep.c
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 28 Jun 2011 16:25:37 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <stdlib.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include <sys/mman.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 #include <sys/types.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include <sys/stat.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 #include <fcntl.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 #include <unistd.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 #include <string.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include <setjmp.h>
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 typedef unsigned char UCHAR;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 typedef unsigned char* UCHARP;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 typedef enum BOOL {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 FALSE = 0,
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 TRUE = 1
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 } BOOL;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 typedef struct ENV {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 BOOL print_count;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 BOOL print_filename;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 int count;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 char *filename;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 jmp_buf *ret;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 } *ENVP;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 __code filter(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 __code predict(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 __code reject(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 __code accept(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 void s0(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 __code _s0(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 #define __REGEX__ "h.c"
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 UCHARP get_line_beg(UCHARP p, UCHARP beg) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 while(p > beg) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 if ((*--p) == '\n') return p+1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return beg;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 void print_line(UCHARP beg, UCHARP end) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 fwrite(beg, sizeof(char), (end - beg + 1), stdout);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 void grep(int fd, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 caddr_t file_mmap;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 UCHARP buf, end, beg;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 off_t size;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 struct stat sb;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 if (fstat(fd, &sb)) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 fprintf(stderr, "can't fstat %s\n", env->filename);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 exit(0);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 size = sb.st_size;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 file_mmap = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t)0);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 if (file_mmap == (caddr_t)-1) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 fprintf(stderr, "can't mmap %s\n", env->filename);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 exit(0);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 beg = buf = (UCHARP) file_mmap;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 end = beg + size - 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 env->count = 0;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 s0(beg, beg, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 munmap(file_mmap, size);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 return;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 void stdingrep(ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 char buf_[BUFSIZ];
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 UCHARP buf, beg, end;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 buf = beg = (UCHARP) buf_;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 while (fgets(buf_, BUFSIZ, stdin) != NULL) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 env->count = 0;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 end = buf + strlen(buf_) - 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 s0(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 int main(int argc, char* argv[]) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 int i, fd, opt, with_filename = 0;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 ENVP env = (ENVP)calloc(sizeof(ENVP), 1);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 while ((opt=getopt(argc, argv, "chH")) != -1) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 switch (opt) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 case 'c':
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 env->print_count = 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 case 'h':
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 with_filename = -1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 case 'H':
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 with_filename = 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 default:
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 fprintf(stderr, "invalid option.");
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 exit(0);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 if (optind == argc) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 if (with_filename) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 /* handle -h/-H as help */
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 printf("grep pattern: %s\n", __REGEX__);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 exit(1);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 } else {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 stdingrep(env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 } else {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 switch (with_filename) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 case 0: //default
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 if (argc == optind + 1) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 /* just grep 1 file. */
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 env->print_filename = FALSE;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 } else {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 /* grep 1 more files. */
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 env->print_filename = TRUE;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 // option
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 case -1:
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 env->print_filename = FALSE;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 case 1:
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 env->print_filename = TRUE;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 default: break;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 for (i = optind; i < argc; i++) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 fd = open(argv[i], O_RDONLY, 0666);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 if (fd == 0) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 printf("%d, %s\n", i, argv[i]);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 fprintf(stderr, "can't open %s:", argv[i]);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 continue;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 env->filename = argv[i];
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 grep(fd, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 close(fd);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 return 0;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 __code s1(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 __code s2(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 __code s3(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 __code s4(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 __code s5(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 __code s6(UCHARP beg, UCHARP buf, UCHARP end, ENVP env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 void s0(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 env->ret = (jmp_buf *)malloc(sizeof(int)*100); // allocate dumy fields. for protect real ret addres.
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 env->ret = (jmp_buf *)malloc(sizeof(jmp_buf));
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 if (env == NULL) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 fprintf(stderr, "can't allocate jmp_buf");
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 exit(0);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 int ret;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 if ((ret = setjmp(env->ret)) == 0) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 goto _s0(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 //free(env->ret); danger!
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 return;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 __code _s0(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 goto filter(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 __code filter(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 static const UCHARP const key = (UCHARP)"h";
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 int i, len = 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 loop:
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 while ((buf = memchr(buf, key[0], end-buf))) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 for (i = 1; i < len; i++) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 if (buf[i] != key[i]) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 buf++;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 goto loop;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 goto next;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 buf = end;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 goto reject(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 next:
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 goto s1(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
195
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 __code s1(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 static __code (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s1, /* NL */ [10] = (void*)reject, /* h */ [104] = (void*)s2};
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 if (buf > end) goto tbl['\n'](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 goto tbl[*buf++](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 __code s2(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 static __code (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s3, /* NL */ [10] = (void*)reject, /* h */ [104] = (void*)s4};
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 if (buf > end) goto tbl['\n'](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 goto tbl[*buf++](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
207
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 __code s3(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 static __code (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s1, /* NL */ [10] = (void*)reject, /* c */ [99] = (void*)s5, /* h */ [104] = (void*)s2};
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 if (buf > end) goto tbl['\n'](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 goto tbl[*buf++](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
213
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 __code s4(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 static __code (*tbl[256])(UCHARP, UCHARP, UCHARP, ENVP) = {[0 ... 255] = (void*)s3, /* NL */ [10] = (void*)reject, /* c */ [99] = (void*)s6, /* h */ [104] = (void*)s4};
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 if (buf > end) goto tbl['\n'](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 goto tbl[*buf++](beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 __code s5(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 goto accept(beg, buf - 1, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
223
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 __code s6(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 goto accept(beg, buf - 1, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 __code
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 accept(UCHARP beg, UCHARP buf, UCHARP end, ENVP env)
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 UCHARP ret = (buf < end) ? (UCHARP) memchr(buf, '\n', (end - buf)) : NULL;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 if (env->print_count) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 env->count++;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 if (ret == NULL) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 if (env->print_filename) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 printf("%s:", env->filename);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 printf("%d\n", env->count);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 longjmp(env->ret, 1);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 beg = buf = ret + 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 } else {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 beg = get_line_beg(buf, beg);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 if (env->print_filename) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 printf("%s:", env->filename);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 if (ret == NULL) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 print_line(beg, end);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 longjmp(env->ret, 1);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 print_line(beg, ret);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 beg = buf = ret + 1;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 goto _s0(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
256
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 __code
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 reject(UCHARP beg, UCHARP buf, UCHARP end, ENVP env) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 if (buf >= end) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 if (env->print_count) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 if (env->print_filename) {
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 printf("%s:", env->filename);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 printf("%d\n", env->count);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 longjmp(env->ret, 1);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 }
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 beg = buf;
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 goto _s0(beg, buf, end, env);
a6f51c76dea8 add grep.c
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 }