Mercurial > hg > Members > nobuyasu > test
view pyrect/pyrect/grepAAA.cbc.c @ 9:493c96d030c0
add pyrect
author | nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Jun 2011 17:24:03 +0900 |
parents | |
children |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> typedef unsigned char UCHAR; typedef unsigned char *UCHARP; typedef struct env_ { void (*ret)(); int count; } ENV; typedef ENV *ENVP; int matcher(UCHARP beg, UCHARP buf, UCHARP end); __code entry(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp); __code accept(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp); __code reject(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp); UCHARP get_line_beg(UCHARP p, UCHARP beg); __code quick_filter(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp) { static const UCHAR key[] = "AAA"; UCHARP tmp1, tmp2, end_ = end - 2; while (buf < end_) { if (*buf == 65 /* 'A' */) { tmp1 = buf, tmp2 = (UCHARP)key; while (*(++tmp1) == *(++tmp2)){ if (tmp2 == key+2) goto next; } } switch(buf[3]) { case 65: /* 'A' */ buf += 1; break; default: buf += 4; } } (*envp->ret)(); next: goto accept(beg, buf, end, envp); } UCHARP get_line_beg(UCHARP p, UCHARP beg) { while(p > beg) { if ((*--p) == '\n') return p+1; } return beg; } void print_line(UCHARP beg, UCHARP end) { fwrite(beg, sizeof(char), (end - beg + 1), stdout); } void grep(char *regexp, int fd, char *name) { caddr_t file_mmap; UCHARP buf, end, beg; off_t size; struct stat sb; if (fstat(fd, &sb)) { fprintf(stderr, "can't fstat %s\n", name); exit(0); } size = sb.st_size; file_mmap = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t)0); if (file_mmap == (caddr_t)-1) { fprintf(stderr, "can't mmap %s\n", name); exit(0); } beg = buf = (UCHARP) file_mmap; end = beg + size - 1; matcher(beg, beg, end); munmap(file_mmap, size); return; } int main(int argc, char* argv[]) { int i, fd; if (argc < 2) { fprintf(stderr, "usage: grep regexp [file ...]"); exit(0); } if (argc == 2) { return 0; } else { for (i = 2; i < argc; i++) { fd = open(argv[i], O_RDONLY, 0666); if (fd == 0) { fprintf(stderr, "can't open %s:", argv[i]); continue; } grep(argv[1], fd, argc > 3 ? argv[i] : NULL); close(fd); } } return 0; } int matcher(UCHARP beg, UCHARP buf, UCHARP end) { __label__ _return; void __return() { goto _return; } ENV env; env.ret = __return; env.count = 0; goto entry(beg, buf, end, &env); _return: return env.count; } __code entry(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp) { goto quick_filter(beg, buf, end, envp); } __code accept(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp) { UCHARP ret = (UCHARP)memchr(buf, '\n', (buf - end)); beg = get_line_beg(buf, beg); if (ret == NULL) { print_line(beg, end); (*envp->ret)(); } print_line(beg, ret); beg = buf = ret + 1; goto entry(beg, buf, end, envp); } __code reject(UCHARP beg, UCHARP buf, UCHARP end, ENVP envp) { if (buf >= end) (*envp->ret)(); beg = buf; goto entry(beg, buf, end, envp); }