00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <errno.h>
00041 #include <string.h>
00042 #include <sys/types.h>
00043 #include <sys/stat.h>
00044 #include <fcntl.h>
00045 #include <ctype.h>
00046 #include <unistd.h>
00047 #include <regex.h>
00048
00049
00050 static char *id="$Id$";
00051 static char *version="re_test 0.1";
00052 static char* help_msg="\
00053 Usage: re_test [-f file] regular_expression \n\
00054 Options:\n\
00055 -f file file with the content of a sip packet (max 65k)\n\
00056 -h this help message\n\
00057 -n non matching list ([^...]) will match newlines\n\
00058 -s case sensitive\n\
00059 -v increase verbosity\n\
00060 ";
00061
00062 #define BUF_SIZE 65535
00063
00064
00065 int main (int argc, char** argv)
00066 {
00067 int fd;
00068 char c;
00069 int n;
00070 char* re_str;
00071 char buffer[BUF_SIZE+1];
00072 char* buf;
00073 regex_t re;
00074 int flags;
00075 regmatch_t pmatch;
00076 int match;
00077 int eflags;
00078
00079 int verbose;
00080 char *fname;
00081
00082
00083 verbose=0;
00084 fname=0;
00085 re_str=0;
00086 flags=REG_EXTENDED|REG_ICASE|REG_NEWLINE;
00087 match=0;
00088 buf=buffer;
00089 eflags=0;
00090
00091 opterr=0;
00092 while ((c=getopt(argc,argv, "f:nsvhV"))!=-1){
00093 switch(c){
00094 case 'f':
00095 fname=optarg;
00096 break;
00097 case 'v':
00098 verbose++;
00099 break;
00100 case 'V':
00101 printf("version: %s\n", version);
00102 printf("%s\n",id);
00103 exit(0);
00104 break;
00105 case 'h':
00106 printf("version: %s\n", version);
00107 printf("%s", help_msg);
00108 exit(0);
00109 break;
00110 case 's':
00111 flags&=~REG_ICASE;
00112 break;
00113 case 'n':
00114 flags&=~REG_NEWLINE;
00115 break;
00116 case '?':
00117 if (isprint(optopt))
00118 fprintf(stderr, "Unknown option `-%c´\n", optopt);
00119 else
00120 fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
00121 goto error;
00122 case ':':
00123 fprintf(stderr, "Option `-%c´ requires an argument.\n",
00124 optopt);
00125 goto error;
00126 break;
00127 default:
00128 abort();
00129 }
00130 }
00131
00132 if (optind < argc){
00133 re_str=argv[optind];
00134 }
00135
00136
00137 if (re_str==0){
00138 fprintf(stderr, "ERROR: no regular expression specified\n");
00139 goto error;
00140 }else{
00141 if (regcomp(&re, re_str, flags)){
00142 fprintf(stderr, "ERROR: bad regular expression <%s>\n", re_str);
00143 goto error;
00144 }
00145 }
00146
00147 if ((fname!=0 ) &&(strcmp(fname, "-")!=0)){
00148
00149 fd=open(fname, O_RDONLY);
00150 if (fd<0){
00151 fprintf(stderr, "ERROR: loading packet-file(%s): %s\n", fname,
00152 strerror(errno));
00153 goto error;
00154 }
00155 }else fd=0;
00156 n=read(fd, buf, BUF_SIZE);
00157 if (n<0){
00158 fprintf(stderr, "ERROR: reading file(%s): %s\n", fname,
00159 strerror(errno));
00160 goto error;
00161 }
00162 buf[n]=0;
00163 if (verbose) printf("read %d bytes from file %s\n", n, fname);
00164 if (fd!=0) close(fd);
00165
00166 while (regexec(&re, buf, 1, &pmatch, eflags)==0){
00167 eflags|=REG_NOTBOL;
00168 match++;
00169 if (pmatch.rm_so==-1){
00170 fprintf(stderr, "ERROR: unknown match offset\n");
00171 goto error;
00172 }else{
00173 if (verbose){
00174 printf("%4d -%4d: ", (int)pmatch.rm_so+buf-buffer,
00175 (int)pmatch.rm_eo+buf-buffer);
00176 }
00177 printf("%.*s\n", (int)(pmatch.rm_eo-pmatch.rm_so),
00178 buf+pmatch.rm_so);
00179 }
00180 buf+=pmatch.rm_eo;
00181 }
00182 if (verbose) printf("\n%d matches\n", match);
00183 if (match) exit(0);
00184 else exit(1);
00185
00186 error:
00187 exit(-1);
00188 }