re_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * regexec test program (good to find re lib. bugs)
  22. * uses the same flags as ser/textops for re-matching
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <ctype.h>
  32. #include <unistd.h>
  33. #include <regex.h>
  34. static char *id="$Id$";
  35. static char *version="re_test 0.1";
  36. static char* help_msg="\
  37. Usage: re_test [-f file] regular_expression \n\
  38. Options:\n\
  39. -f file file with the content of a sip packet (max 65k)\n\
  40. -h this help message\n\
  41. -n non matching list ([^...]) will match newlines\n\
  42. -s case sensitive\n\
  43. -v increase verbosity\n\
  44. ";
  45. #define BUF_SIZE 65535
  46. int main (int argc, char** argv)
  47. {
  48. int fd;
  49. char c;
  50. int n;
  51. char* re_str;
  52. char buffer[BUF_SIZE+1]; /* space for null-term. */
  53. char* buf;
  54. regex_t re;
  55. int flags;
  56. regmatch_t pmatch;
  57. int match;
  58. int eflags;
  59. int verbose;
  60. char *fname;
  61. /* init */
  62. verbose=0;
  63. fname=0;
  64. re_str=0;
  65. flags=REG_EXTENDED|REG_ICASE|REG_NEWLINE;
  66. match=0;
  67. buf=buffer;
  68. eflags=0;
  69. opterr=0;
  70. while ((c=getopt(argc,argv, "f:nsvhV"))!=-1){
  71. switch(c){
  72. case 'f':
  73. fname=optarg;
  74. break;
  75. case 'v':
  76. verbose++;
  77. break;
  78. case 'V':
  79. printf("version: %s\n", version);
  80. printf("%s\n",id);
  81. exit(0);
  82. break;
  83. case 'h':
  84. printf("version: %s\n", version);
  85. printf("%s", help_msg);
  86. exit(0);
  87. break;
  88. case 's':
  89. flags&=~REG_ICASE;
  90. break;
  91. case 'n':
  92. flags&=~REG_NEWLINE;
  93. break;
  94. case '?':
  95. if (isprint(optopt))
  96. fprintf(stderr, "Unknown option `-%c´\n", optopt);
  97. else
  98. fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
  99. goto error;
  100. case ':':
  101. fprintf(stderr, "Option `-%c´ requires an argument.\n",
  102. optopt);
  103. goto error;
  104. break;
  105. default:
  106. abort();
  107. }
  108. }
  109. /* check if we have non-options */
  110. if (optind < argc){
  111. re_str=argv[optind];
  112. }
  113. /* check if all the required params are present */
  114. if (re_str==0){
  115. fprintf(stderr, "ERROR: no regular expression specified\n");
  116. goto error;
  117. }else{
  118. if (regcomp(&re, re_str, flags)){
  119. fprintf(stderr, "ERROR: bad regular expression <%s>\n", re_str);
  120. goto error;
  121. }
  122. }
  123. if ((fname!=0 ) &&(strcmp(fname, "-")!=0)){
  124. /* open packet file */
  125. fd=open(fname, O_RDONLY);
  126. if (fd<0){
  127. fprintf(stderr, "ERROR: loading packet-file(%s): %s\n", fname,
  128. strerror(errno));
  129. goto error;
  130. }
  131. }else fd=0;
  132. n=read(fd, buf, BUF_SIZE);
  133. if (n<0){
  134. fprintf(stderr, "ERROR: reading file(%s): %s\n", fname,
  135. strerror(errno));
  136. goto error;
  137. }
  138. buf[n]=0; /* null terminate it */
  139. if (verbose) printf("read %d bytes from file %s\n", n, fname);
  140. if (fd!=0) close(fd); /* we don't want to close stdin */
  141. while (regexec(&re, buf, 1, &pmatch, eflags)==0){
  142. eflags|=REG_NOTBOL;
  143. match++;
  144. if (pmatch.rm_so==-1){
  145. fprintf(stderr, "ERROR: unknown match offset\n");
  146. goto error;
  147. }else{
  148. if (verbose){
  149. printf("%4d -%4d: ", (int)pmatch.rm_so+buf-buffer,
  150. (int)pmatch.rm_eo+buf-buffer);
  151. }
  152. printf("%.*s\n", (int)(pmatch.rm_eo-pmatch.rm_so),
  153. buf+pmatch.rm_so);
  154. }
  155. buf+=pmatch.rm_eo;
  156. }
  157. if (verbose) printf("\n%d matches\n", match);
  158. if (match) exit(0);
  159. else exit(1);
  160. error:
  161. exit(-1);
  162. }