📄 main.c
字号:
CONSTANT = ON; argv++; strcat(Pattern, argv[0]); if(argc > 1 && argv[1][0] == '-') { fprintf(stderr, "%s: -k should be the last option in the command\n", Progname); exit(2); } break; case 'l' : FILENAMEONLY = ON; break; case 'n' : LINENUM = ON; /* output prefixed by line no*/ break; case 'v' : INVERSE = ON; /* output no-matched lines */ break; case 't' : OUTTAIL = ON; /* output from tail of delimiter */ break; case 'B' : BESTMATCH = ON; break; case 'w' : WORDBOUND = ON;/* match to words */ if(WHOLELINE) { fprintf(stderr, "%s: illegal option combination\n", Progname); exit(2); } break; case 'y' : NOPROMPT = ON; break; case 'I' : I = atoi(argv[0]+2); /* Insertion Cost */ JUMP = ON; break; case 'S' : S = atoi(argv[0]+2); /* Substitution Cost */ JUMP = ON; break; case 'D' : DD = atoi(argv[0]+2); /* Deletion Cost */ JUMP = ON; break; case 'G' : FILEOUT = ON; COUNT = ON; break; default : if (isdigit(c)) { APPROX = ON; D = atoi(argv[0]+1); if (D > MaxError) { fprintf(stderr,"%s: the maximum number of errors is %d \n", Progname, MaxError); exit(2); } } else { fprintf(stderr, "%s: illegal option -%c\n",Progname, c); usage(); } } /* switch(c) */ } /* while (--argc > 0 && (*++argv)[0] == '-') */ if (FILENAMEONLY && NOFILENAME) { fprintf(stderr, "%s: -h and -l options are mutually exclusive\n",Progname); } if (COUNT && (FILENAMEONLY || NOFILENAME)) { FILENAMEONLY = OFF; if(!FILEOUT) NOFILENAME = OFF; } if (!(PAT_FILE) && Pattern[0] == '\0') { /* Pattern not set with -e option */ if (argc == 0) usage(); strcpy(Pattern, *argv); argc--; argv++; } Numfiles = 0; fd = 3; /* make sure it's not 0 */ if (argc == 0) /* check Pattern against stdin */ fd = 0; else { if (!(Textfiles = (CHAR **)malloc(argc * sizeof(CHAR *) ))) { fprintf(stderr, "%s: malloc failure (you probably don't have enough memory)\n", Progname); exit(2); } while (argc--) { /* one or more filenames on command line -- put the valid filenames in a array of strings */ /* if ((filetype = check_file(*argv)) != ISASCIIFILE) { if(filetype == NOSUCHFILE) fprintf(stderr,"%s: %s: no such file or directory\n",Progname,*argv); argv++;*/ if ((filetype = check_file(*argv)) == NOSUCHFILE) { if(filetype == NOSUCHFILE) fprintf(stderr,"%s: %s: no such file or directory\n",Progname,*argv); argv++; } else { /* file is ascii*/ if (!(Textfiles[Numfiles] = (CHAR *)malloc((strlen(*argv)+1)))) { fprintf(stderr, "%s: malloc failure (you probably don't have enough memory)\n", Progname); exit(2); } strcpy(Textfiles[Numfiles++], *argv++); } /* else */ } /* while (argc--) */ } /* else */ checksg(Pattern, D); /* check if the pattern is simple */ strcpy(OldPattern, Pattern); if (SGREP == 0) { preprocess(D_pattern, Pattern); strcpy(old_D_pat, D_pattern); M = maskgen(Pattern, D); } else M = strlen(OldPattern); if (PAT_FILE) prepf(fp); if (Numfiles > 1) FNAME = ON; if (NOFILENAME) FNAME = 0; num_of_matched = 0; compat(); /* check compatibility between options */ if (fd == 0) { if(FILENAMEONLY) { fprintf(stderr, "%s: -l option is not compatible with standard input\n", Progname); exit(2); } if(PAT_FILE) mgrep(fd); else { if(SGREP) sgrep(OldPattern, strlen(OldPattern), fd, D); else bitap(old_D_pat, Pattern, fd, M, D); } if (COUNT) { if(INVERSE && PAT_FILE) printf("%d\n", total_line-num_of_matched); else printf("%d\n", num_of_matched); } } else { for (i = 0; i < Numfiles; i++, close(fd), num_of_matched = 0) { strcpy(CurrentFileName, Textfiles[i]); if ((fd = open(Textfiles[i], 0)) <= 0) { fprintf(stderr, "%s: can't open file %s\n",Progname, Textfiles[i]); } else { if(PAT_FILE) mgrep(fd); else { if(SGREP) sgrep(OldPattern, strlen(OldPattern), fd, D); else bitap(old_D_pat, Pattern, fd, M, D); } if (num_of_matched) NOMATCH = OFF; if (COUNT && !FILEOUT) { if(INVERSE && PAT_FILE) { if(FNAME) printf("%s: %d\n", CurrentFileName, total_line - num_of_matched); else printf("%d\n", total_line - num_of_matched); } else { if(FNAME) printf("%s: %d\n", CurrentFileName, num_of_matched); else printf("%d\n", num_of_matched); } } /* if COUNT */ if(FILEOUT && num_of_matched) { file_out(CurrentFileName); } } /* else */ } /* for i < Numfiles */ if(NOMATCH && BESTMATCH) { if(WORDBOUND || WHOLELINE || LINENUM || INVERSE) { SGREP = 0; preprocess(D_pattern, Pattern); strcpy(old_D_pat, D_pattern); M = maskgen(Pattern, D); } COUNT=ON; D=1; while(D<M && D<=MaxError && num_of_matched == 0) { for (i = 0; i < Numfiles; i++, close(fd)) { strcpy(CurrentFileName, Textfiles[i]); if ((fd = open(Textfiles[i], 0)) > 0) { if(PAT_FILE) mgrep(fd); else { if(SGREP) sgrep(OldPattern,strlen(OldPattern),fd,D); else bitap(old_D_pat,Pattern,fd,M,D); } } } /* for i < Numfiles */ D++; } /* while */ if(num_of_matched > 0) { D--; COUNT = 0; if(NOPROMPT) goto GO_AHEAD; if(D==1) fprintf(stderr, "best match has 1 error, "); else fprintf(stderr, "best match has %d errors, ", D); fflush(stderr); if(num_of_matched == 1) fprintf(stderr,"there is 1 match, output it? (y/n)"); else fprintf(stderr,"there are %d matches, output them? (y/n)", num_of_matched); scanf("%c",&c); if(c != 'y') goto CONT;GO_AHEAD: for (i = 0; i < Numfiles; i++, close(fd)) { strcpy(CurrentFileName, Textfiles[i]); if ((fd = open(Textfiles[i], 0)) > 0) { if(PAT_FILE) mgrep(fd); else { if(SGREP) sgrep(OldPattern,strlen(OldPattern),fd,D); else bitap(old_D_pat,Pattern,fd,M,D); } } } /* for i < Numfiles */ NOMATCH = 0; } } }CONT: if(EATFIRST) { printf("\n"); EATFIRST = OFF; } if(num_of_matched) NOMATCH = OFF; if(NOMATCH) exit(1); exit(0);} /* end of main() */ file_out(fname)char *fname;{int num_read;int fd;int i, len;CHAR buf[4097]; if(FNAME) { len = strlen(fname); putchar('\n'); for(i=0; i< len; i++) putchar(':'); putchar('\n'); printf("%s\n", CurrentFileName); len = strlen(fname); for(i=0; i< len; i++) putchar(':'); putchar('\n'); fflush(stdout); } fd = open(fname, 0); while((num_read = read(fd, buf, 4096)) > 0) write(1, buf, num_read);}usage(){ fprintf(stderr, "usage: %s [-#cdehiklnpstvwxBDGIS] [-f patternfile] pattern [files]\n", Progname); printf("\n"); fprintf(stderr, "summary of frequently used options:\n"); fprintf(stderr, "-#: find matches with at most # errors\n"); fprintf(stderr, "-c: output the number of matched records\n"); fprintf(stderr, "-d: define record delimiter\n"); fprintf(stderr, "-h: do not output file names\n"); fprintf(stderr, "-i: case-insensitive search, e.g., 'a' = 'A'\n"); fprintf(stderr, "-l: output the names of files that contain a match\n"); fprintf(stderr, "-n: output record prefixed by record number\n"); fprintf(stderr, "-v: output those records containing no matches\n"); fprintf(stderr, "-w: pattern has to match as a word, e.g., 'win' will not match 'wind'\n"); fprintf(stderr, "-B: best match mode. find the closest matches to the pattern\n"); fprintf(stderr, "-G: output the files that contain a match\n"); printf("\n"); exit(2);}checksg(Pattern, D) CHAR *Pattern; int D;{ char c; int i, m; m = strlen(Pattern); if(!(PAT_FILE) && m <= D) { fprintf(stderr, "%s: size of pattern must be greater than number of errors\n", Progname); exit(2); } SIMPLEPATTERN = ON; for (i=0; i < m; i++) { switch(Pattern[i]) { case ';' : SIMPLEPATTERN = OFF; break; case ',' : SIMPLEPATTERN = OFF; break; case '.' : SIMPLEPATTERN = OFF; break; case '*' : SIMPLEPATTERN = OFF; break; case '-' : SIMPLEPATTERN = OFF; break; case '[' : SIMPLEPATTERN = OFF; break; case ']' : SIMPLEPATTERN = OFF; break; case '(' : SIMPLEPATTERN = OFF; break; case ')' : SIMPLEPATTERN = OFF; break; case '<' : SIMPLEPATTERN = OFF; break; case '>' : SIMPLEPATTERN = OFF; break; case '^' : if(D > 0) SIMPLEPATTERN = OFF; break; case '$' : if(D > 0) SIMPLEPATTERN = OFF; break; case '|' : SIMPLEPATTERN = OFF; break; case '#' : SIMPLEPATTERN = OFF; break; case '\\' : SIMPLEPATTERN = OFF; break; default : break; } } if (CONSTANT) SIMPLEPATTERN = ON; if (SIMPLEPATTERN == OFF) return; if (NOUPPER && D) return; if (JUMP == ON) return; if (I == 0) return; if (LINENUM) return; if (DELIMITER) return; if (INVERSE) return; if (WORDBOUND && D > 0) return; if (WHOLELINE && D > 0) return; if (SILENT) return; /* REMINDER: to be removed */ SGREP = ON; if(m >= 16) DNA = ON; for(i=0; i<m; i++) { c = Pattern[i]; if(c == 'a' || c == 'c' || c == 't' || c == 'g' ) ; else DNA = OFF; } return;}output (buffer, i1, i2, j) register CHAR *buffer; int i1, i2, j;{register CHAR *bp, *outend; if(i1 > i2) return; num_of_matched++; if(COUNT) return; if(SILENT) return; if(OUTTAIL) { i1 = i1 + D_length; i2 = i2 + D_length; } if(DELIMITER) j = j+1; if(FIRSTOUTPUT) { if (buffer[i1] == '\n') { i1++; EATFIRST = ON; } FIRSTOUTPUT = 0; } if(TRUNCATE) { fprintf(stderr, "WARNING!!! some lines have been truncated in output record #%d\n", num_of_matched-1); } while(buffer[i1] == '\n' && i1 <= i2) { printf("\n"); i1++; } if(FNAME == ON) printf("%s: ", CurrentFileName); if(LINENUM) printf("%d: ", j-1); bp = buffer + i1; outend = buffer + i2; while(bp <= outend) putchar(*bp++);}/* end of main.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -