re_test.c

00001 
00002 /* $Id$ */
00003 /*
00004  *
00005  * Copyright (C) 2001-2003 FhG Fokus
00006  *
00007  * This file is part of ser, a free SIP server.
00008  *
00009  * ser is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * For a license to use the ser software under conditions
00015  * other than those described here, or to purchase support for this
00016  * software, please contact iptel.org by e-mail at the following addresses:
00017  *    info@iptel.org
00018  *
00019  * ser is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU General Public License 
00025  * along with this program; if not, write to the Free Software 
00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  */
00028 /*
00029  * regexec test program (good to find re lib. bugs)
00030  * uses the same flags as ser/textops for re-matching
00031  * History:
00032  * --------
00033  *  created by andrei
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]; /* space for null-term. */
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         /* init */
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         /* check if we have non-options */
00132         if (optind < argc){
00133                 re_str=argv[optind];
00134         }
00135         
00136         /* check if all the required params are present */
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                 /* open packet file */
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; /* null terminate it */
00163         if (verbose) printf("read %d bytes from file %s\n", n, fname);
00164         if (fd!=0) close(fd); /* we don't want to close stdin */
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 }