⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcretest.c

📁 ncbi源码
💻 C
📖 第 1 页 / 共 3 页
字号:
int rc;if ((rc = pcre_fullinfo(re, study, option, ptr)) < 0)  fprintf(outfile, "Error %d from pcre_fullinfo(%d)\n", rc, option);}/* Read lines from named file or stdin and write to named file or stdout; linesconsist of a regular expression, in delimiters and optionally followed byoptions, followed by a set of test data, terminated by an empty line. */int main(int argc, char **argv){FILE *infile = stdin;int options = 0;int study_options = 0;int op = 1;int timeit = 0;int showinfo = 0;int showstore = 0;int size_offsets = 45;int size_offsets_max;int *offsets;#if !defined NOPOSIXint posix = 0;#endifint debug = 0;int done = 0;unsigned char buffer[30000];unsigned char dbuffer[1024];/* Static so that new_malloc can use it. */outfile = stdout;/* Scan options */while (argc > 1 && argv[op][0] == '-')  {  char *endptr;  if (strcmp(argv[op], "-s") == 0 || strcmp(argv[op], "-m") == 0)    showstore = 1;  else if (strcmp(argv[op], "-t") == 0) timeit = 1;  else if (strcmp(argv[op], "-i") == 0) showinfo = 1;  else if (strcmp(argv[op], "-d") == 0) showinfo = debug = 1;  else if (strcmp(argv[op], "-o") == 0 && argc > 2 &&      ((size_offsets = (int)strtoul(argv[op+1], &endptr, 10)), *endptr == 0))    {    op++;    argc--;    }#if !defined NOPOSIX  else if (strcmp(argv[op], "-p") == 0) posix = 1;#endif  else    {    printf("** Unknown or malformed option %s\n", argv[op]);    printf("Usage:   pcretest [-d] [-i] [-o <n>] [-p] [-s] [-t] [<input> [<output>]]\n");    printf("  -d     debug: show compiled code; implies -i\n"           "  -i     show information about compiled pattern\n"           "  -o <n> set size of offsets vector to <n>\n");#if !defined NOPOSIX    printf("  -p     use POSIX interface\n");#endif    printf("  -s     output store information\n"           "  -t     time compilation and execution\n");    return 1;    }  op++;  argc--;  }/* Get the store for the offsets vector, and remember what it was */size_offsets_max = size_offsets;offsets = malloc(size_offsets_max * sizeof(int));if (offsets == NULL)  {  printf("** Failed to get %d bytes of memory for offsets vector\n",    size_offsets_max * sizeof(int));  return 1;  }/* Sort out the input and output files */if (argc > 1)  {  infile = fopen(argv[op], "r");  if (infile == NULL)    {    printf("** Failed to open %s\n", argv[op]);    return 1;    }  }if (argc > 2)  {  outfile = fopen(argv[op+1], "wb");  if (outfile == NULL)    {    printf("** Failed to open %s\n", argv[op+1]);    return 1;    }  }/* Set alternative malloc function */pcre_malloc = new_malloc;/* Heading line, then prompt for first regex if stdin */fprintf(outfile, "PCRE version %s\n\n", pcre_version());/* Main loop */while (!done)  {  pcre *re = NULL;  pcre_extra *extra = NULL;#if !defined NOPOSIX  /* There are still compilers that require no indent */  regex_t preg;  int do_posix = 0;#endif  const char *error;  unsigned char *p, *pp, *ppp;  const unsigned char *tables = NULL;  int do_study = 0;  int do_debug = debug;  int do_G = 0;  int do_g = 0;  int do_showinfo = showinfo;  int do_showrest = 0;  int utf8 = 0;  int erroroffset, len, delimiter;  if (infile == stdin) printf("  re> ");  if (fgets((char *)buffer, sizeof(buffer), infile) == NULL) break;  if (infile != stdin) fprintf(outfile, "%s", (char *)buffer);  p = buffer;  while (isspace(*p)) p++;  if (*p == 0) continue;  /* Get the delimiter and seek the end of the pattern; if is isn't  complete, read more. */  delimiter = *p++;  if (isalnum(delimiter) || delimiter == '\\')    {    fprintf(outfile, "** Delimiter must not be alphameric or \\\n");    goto SKIP_DATA;    }  pp = p;  for(;;)    {    while (*pp != 0)      {      if (*pp == '\\' && pp[1] != 0) pp++;        else if (*pp == delimiter) break;      pp++;      }    if (*pp != 0) break;    len = sizeof(buffer) - (pp - buffer);    if (len < 256)      {      fprintf(outfile, "** Expression too long - missing delimiter?\n");      goto SKIP_DATA;      }    if (infile == stdin) printf("    > ");    if (fgets((char *)pp, len, infile) == NULL)      {      fprintf(outfile, "** Unexpected EOF\n");      done = 1;      goto CONTINUE;      }    if (infile != stdin) fprintf(outfile, "%s", (char *)pp);    }  /* If the first character after the delimiter is backslash, make  the pattern end with backslash. This is purely to provide a way  of testing for the error message when a pattern ends with backslash. */  if (pp[1] == '\\') *pp++ = '\\';  /* Terminate the pattern at the delimiter */  *pp++ = 0;  /* Look for options after final delimiter */  options = 0;  study_options = 0;  log_store = showstore;  /* default from command line */  while (*pp != 0)    {    switch (*pp++)      {      case 'g': do_g = 1; break;      case 'i': options |= PCRE_CASELESS; break;      case 'm': options |= PCRE_MULTILINE; break;      case 's': options |= PCRE_DOTALL; break;      case 'x': options |= PCRE_EXTENDED; break;      case '+': do_showrest = 1; break;      case 'A': options |= PCRE_ANCHORED; break;      case 'D': do_debug = do_showinfo = 1; break;      case 'E': options |= PCRE_DOLLAR_ENDONLY; break;      case 'G': do_G = 1; break;      case 'I': do_showinfo = 1; break;      case 'M': log_store = 1; break;#if !defined NOPOSIX      case 'P': do_posix = 1; break;#endif      case 'S': do_study = 1; break;      case 'U': options |= PCRE_UNGREEDY; break;      case 'X': options |= PCRE_EXTRA; break;      case '8': options |= PCRE_UTF8; utf8 = 1; break;      case 'L':      ppp = pp;      while (*ppp != '\n' && *ppp != ' ') ppp++;      *ppp = 0;      if (setlocale(LC_CTYPE, (const char *)pp) == NULL)        {        fprintf(outfile, "** Failed to set locale \"%s\"\n", pp);        goto SKIP_DATA;        }      tables = pcre_maketables();      pp = ppp;      break;      case '\n': case ' ': break;      default:      fprintf(outfile, "** Unknown option '%c'\n", pp[-1]);      goto SKIP_DATA;      }    }  /* Handle compiling via the POSIX interface, which doesn't support the  timing, showing, or debugging options, nor the ability to pass over  local character tables. */#if !defined NOPOSIX  if (posix || do_posix)    {    int rc;    int cflags = 0;    if ((options & PCRE_CASELESS) != 0) cflags |= REG_ICASE;    if ((options & PCRE_MULTILINE) != 0) cflags |= REG_NEWLINE;    rc = regcomp(&preg, (char *)p, cflags);    /* Compilation failed; go back for another re, skipping to blank line    if non-interactive. */    if (rc != 0)      {      (void)regerror(rc, &preg, (char *)buffer, sizeof(buffer));      fprintf(outfile, "Failed: POSIX code %d: %s\n", rc, buffer);      goto SKIP_DATA;      }    }  /* Handle compiling via the native interface */  else#endif  /* !defined NOPOSIX */    {    if (timeit)      {      register int i;      clock_t time_taken;      clock_t start_time = clock();      for (i = 0; i < LOOPREPEAT; i++)        {        re = pcre_compile((char *)p, options, &error, &erroroffset, tables);        if (re != NULL) free(re);        }      time_taken = clock() - start_time;      fprintf(outfile, "Compile time %.3f milliseconds\n",        ((double)time_taken * 1000.0) /        ((double)LOOPREPEAT * (double)CLOCKS_PER_SEC));      }    re = pcre_compile((char *)p, options, &error, &erroroffset, tables);    /* Compilation failed; go back for another re, skipping to blank line    if non-interactive. */    if (re == NULL)      {      fprintf(outfile, "Failed: %s at offset %d\n", error, erroroffset);      SKIP_DATA:      if (infile != stdin)        {        for (;;)          {          if (fgets((char *)buffer, sizeof(buffer), infile) == NULL)            {            done = 1;            goto CONTINUE;            }          len = (int)strlen((char *)buffer);          while (len > 0 && isspace(buffer[len-1])) len--;          if (len == 0) break;          }        fprintf(outfile, "\n");        }      goto CONTINUE;      }    /* Compilation succeeded; print data if required. There are now two    info-returning functions. The old one has a limited interface and    returns only limited data. Check that it agrees with the newer one. */    if (do_showinfo)      {      unsigned long int get_options;      int old_first_char, old_options, old_count;      int count, backrefmax, first_char, need_char;      size_t size;      if (do_debug) print_internals(re);      new_info(re, NULL, PCRE_INFO_OPTIONS, &get_options);      new_info(re, NULL, PCRE_INFO_SIZE, &size);      new_info(re, NULL, PCRE_INFO_CAPTURECOUNT, &count);      new_info(re, NULL, PCRE_INFO_BACKREFMAX, &backrefmax);      new_info(re, NULL, PCRE_INFO_FIRSTCHAR, &first_char);      new_info(re, NULL, PCRE_INFO_LASTLITERAL, &need_char);      old_count = pcre_info(re, &old_options, &old_first_char);      if (count < 0) fprintf(outfile,        "Error %d from pcre_info()\n", count);      else        {        if (old_count != count) fprintf(outfile,          "Count disagreement: pcre_fullinfo=%d pcre_info=%d\n", count,            old_count);        if (old_first_char != first_char) fprintf(outfile,          "First char disagreement: pcre_fullinfo=%d pcre_info=%d\n",            first_char, old_first_char);        if (old_options != (int)get_options) fprintf(outfile,          "Options disagreement: pcre_fullinfo=%ld pcre_info=%d\n",            get_options, old_options);        }      if (size != gotten_store) fprintf(outfile,        "Size disagreement: pcre_fullinfo=%d call to malloc for %d\n",        size, gotten_store);      fprintf(outfile, "Capturing subpattern count = %d\n", count);      if (backrefmax > 0)        fprintf(outfile, "Max back reference = %d\n", backrefmax);      if (get_options == 0) fprintf(outfile, "No options\n");        else fprintf(outfile, "Options:%s%s%s%s%s%s%s%s%s\n",          ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",          ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",          ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",          ((get_options & PCRE_MULTILINE) != 0)? " multiline" : "",          ((get_options & PCRE_DOTALL) != 0)? " dotall" : "",          ((get_options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",          ((get_options & PCRE_EXTRA) != 0)? " extra" : "",          ((get_options & PCRE_UNGREEDY) != 0)? " ungreedy" : "",          ((get_options & PCRE_UTF8) != 0)? " utf8" : "");      if (((((real_pcre *)re)->options) & PCRE_ICHANGED) != 0)        fprintf(outfile, "Case state changes\n");      if (first_char == -1)        {        fprintf(outfile, "First char at start or follows \\n\n");        }      else if (first_char < 0)        {        fprintf(outfile, "No first char\n");        }      else        {        if (isprint(first_char))          fprintf(outfile, "First char = \'%c\'\n", first_char);        else          fprintf(outfile, "First char = %d\n", first_char);        }      if (need_char < 0)        {        fprintf(outfile, "No need char\n");        }      else        {        if (isprint(need_char))          fprintf(outfile, "Need char = \'%c\'\n", need_char);        else          fprintf(outfile, "Need char = %d\n", need_char);        }      }    /* If /S was present, study the regexp to generate additional info to    help with the matching. */    if (do_study)      {      if (timeit)        {        register int i;        clock_t time_taken;        clock_t start_time = clock();        for (i = 0; i < LOOPREPEAT; i++)          extra = pcre_study(re, study_options, &error);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -