/[pcre]/code/branches/pcre16/pcretest.c
ViewVC logotype

Diff of /code/branches/pcre16/pcretest.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 545 by ph10, Wed Jun 16 10:51:15 2010 UTC revision 626 by ph10, Wed Jul 20 17:51:54 2011 UTC
# Line 79  input mode under Windows. */ Line 79  input mode under Windows. */
79  #define fileno _fileno  #define fileno _fileno
80  #endif  #endif
81    
82    /* A user sent this fix for Borland Builder 5 under Windows. */
83    
84    #ifdef __BORLANDC__
85    #define _setmode(handle, mode) setmode(handle, mode)
86    #endif
87    
88    /* Not Windows */
89    
90  #else  #else
91  #include <sys/time.h>          /* These two includes are needed */  #include <sys/time.h>          /* These two includes are needed */
92  #include <sys/resource.h>      /* for setrlimit(). */  #include <sys/resource.h>      /* for setrlimit(). */
# Line 189  static uschar *buffer = NULL; Line 197  static uschar *buffer = NULL;
197  static uschar *dbuffer = NULL;  static uschar *dbuffer = NULL;
198  static uschar *pbuffer = NULL;  static uschar *pbuffer = NULL;
199    
200    /* Textual explanations for runtime error codes */
201    
202    static const char *errtexts[] = {
203      NULL,  /* 0 is no error */
204      NULL,  /* NOMATCH is handled specially */
205      "NULL argument passed",
206      "bad option value",
207      "magic number missing",
208      "unknown opcode - pattern overwritten?",
209      "no more memory",
210      NULL,  /* never returned by pcre_exec() or pcre_dfa_exec() */
211      "match limit exceeded",
212      "callout error code",
213      NULL,  /* BADUTF8 is handled specially */
214      "bad UTF-8 offset",
215      NULL,  /* PARTIAL is handled specially */
216      "not used - internal error",
217      "internal error - pattern overwritten?",
218      "bad count value",
219      "item unsupported for DFA matching",
220      "backreference condition or recursion test not supported for DFA matching",
221      "match limit not supported for DFA matching",
222      "workspace size exceeded in DFA matching",
223      "too much recursion for DFA matching",
224      "recursion limit exceeded",
225      "not used - internal error",
226      "invalid combination of newline options",
227      "bad offset value",
228      NULL  /* SHORTUTF8 is handled specially */
229    };
230    
231    
232  /*************************************************  /*************************************************
233  *         Alternate character tables             *  *         Alternate character tables             *
# Line 516  static const unsigned char tables1[] = { Line 555  static const unsigned char tables1[] = {
555    
556    
557    
558    
559    #ifndef HAVE_STRERROR
560    /*************************************************
561    *     Provide strerror() for non-ANSI libraries  *
562    *************************************************/
563    
564    /* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror()
565    in their libraries, but can provide the same facility by this simple
566    alternative function. */
567    
568    extern int   sys_nerr;
569    extern char *sys_errlist[];
570    
571    char *
572    strerror(int n)
573    {
574    if (n < 0 || n >= sys_nerr) return "unknown error number";
575    return sys_errlist[n];
576    }
577    #endif /* HAVE_STRERROR */
578    
579    
580    
581    
582  /*************************************************  /*************************************************
583  *        Read or extend an input line            *  *        Read or extend an input line            *
584  *************************************************/  *************************************************/
# Line 1123  printf("  -p       use POSIX interface\n Line 1186  printf("  -p       use POSIX interface\n
1186  #endif  #endif
1187  printf("  -q       quiet: do not output PCRE version number at start\n");  printf("  -q       quiet: do not output PCRE version number at start\n");
1188  printf("  -S <n>   set stack size to <n> megabytes\n");  printf("  -S <n>   set stack size to <n> megabytes\n");
1189  printf("  -s       output store (memory) used information\n"  printf("  -s       force each pattern to be studied\n"
1190         "  -t       time compilation and execution\n");         "  -t       time compilation and execution\n");
1191  printf("  -t <n>   time compilation and execution, repeating <n> times\n");  printf("  -t <n>   time compilation and execution, repeating <n> times\n");
1192  printf("  -tm      time execution (matching) only\n");  printf("  -tm      time execution (matching) only\n");
# Line 1151  int timeit = 0; Line 1214  int timeit = 0;
1214  int timeitm = 0;  int timeitm = 0;
1215  int showinfo = 0;  int showinfo = 0;
1216  int showstore = 0;  int showstore = 0;
1217    int force_study = 0;
1218  int quiet = 0;  int quiet = 0;
1219  int size_offsets = 45;  int size_offsets = 45;
1220  int size_offsets_max;  int size_offsets_max;
# Line 1199  while (argc > 1 && argv[op][0] == '-') Line 1263  while (argc > 1 && argv[op][0] == '-')
1263    {    {
1264    unsigned char *endptr;    unsigned char *endptr;
1265    
1266    if (strcmp(argv[op], "-s") == 0 || strcmp(argv[op], "-m") == 0)    if (strcmp(argv[op], "-m") == 0) showstore = 1;
1267      showstore = 1;    else if (strcmp(argv[op], "-s") == 0) force_study = 1;
1268    else if (strcmp(argv[op], "-q") == 0) quiet = 1;    else if (strcmp(argv[op], "-q") == 0) quiet = 1;
1269    else if (strcmp(argv[op], "-b") == 0) debug = 1;    else if (strcmp(argv[op], "-b") == 0) debug = 1;
1270    else if (strcmp(argv[op], "-i") == 0) showinfo = 1;    else if (strcmp(argv[op], "-i") == 0) showinfo = 1;
# Line 1370  while (!done) Line 1434  while (!done)
1434    const unsigned char *tables = NULL;    const unsigned char *tables = NULL;
1435    unsigned long int true_size, true_study_size = 0;    unsigned long int true_size, true_study_size = 0;
1436    size_t size, regex_gotten_store;    size_t size, regex_gotten_store;
1437      int do_allcaps = 0;
1438    int do_mark = 0;    int do_mark = 0;
1439    int do_study = 0;    int do_study = 0;
1440      int no_force_study = 0;
1441    int do_debug = debug;    int do_debug = debug;
1442    int do_G = 0;    int do_G = 0;
1443    int do_g = 0;    int do_g = 0;
1444    int do_showinfo = showinfo;    int do_showinfo = showinfo;
1445    int do_showrest = 0;    int do_showrest = 0;
1446      int do_showcaprest = 0;
1447    int do_flip = 0;    int do_flip = 0;
1448    int erroroffset, len, delimiter, poffset;    int erroroffset, len, delimiter, poffset;
1449    
# Line 1438  while (!done) Line 1505  while (!done)
1505          }          }
1506        }        }
1507    
1508      fprintf(outfile, "Compiled regex%s loaded from %s\n",      fprintf(outfile, "Compiled pattern%s loaded from %s\n",
1509        do_flip? " (byte-inverted)" : "", p);        do_flip? " (byte-inverted)" : "", p);
1510    
1511      /* Need to know if UTF-8 for printing data strings */      /* Need to know if UTF-8 for printing data strings */
# Line 1446  while (!done) Line 1513  while (!done)
1513      new_info(re, NULL, PCRE_INFO_OPTIONS, &get_options);      new_info(re, NULL, PCRE_INFO_OPTIONS, &get_options);
1514      use_utf8 = (get_options & PCRE_UTF8) != 0;      use_utf8 = (get_options & PCRE_UTF8) != 0;
1515    
1516      /* Now see if there is any following study data */      /* Now see if there is any following study data. */
1517    
1518      if (true_study_size != 0)      if (true_study_size != 0)
1519        {        {
# Line 1542  while (!done) Line 1609  while (!done)
1609        case 's': options |= PCRE_DOTALL; break;        case 's': options |= PCRE_DOTALL; break;
1610        case 'x': options |= PCRE_EXTENDED; break;        case 'x': options |= PCRE_EXTENDED; break;
1611    
1612        case '+': do_showrest = 1; break;        case '+':
1613          if (do_showrest) do_showcaprest = 1; else do_showrest = 1;
1614          break;
1615    
1616          case '=': do_allcaps = 1; break;
1617        case 'A': options |= PCRE_ANCHORED; break;        case 'A': options |= PCRE_ANCHORED; break;
1618        case 'B': do_debug = 1; break;        case 'B': do_debug = 1; break;
1619        case 'C': options |= PCRE_AUTO_CALLOUT; break;        case 'C': options |= PCRE_AUTO_CALLOUT; break;
# Line 1560  while (!done) Line 1631  while (!done)
1631        case 'P': do_posix = 1; break;        case 'P': do_posix = 1; break;
1632  #endif  #endif
1633    
1634        case 'S': do_study = 1; break;        case 'S':
1635          if (do_study == 0) do_study = 1; else
1636            {
1637            do_study = 0;
1638            no_force_study = 1;
1639            }
1640          break;
1641    
1642        case 'U': options |= PCRE_UNGREEDY; break;        case 'U': options |= PCRE_UNGREEDY; break;
1643        case 'W': options |= PCRE_UCP; break;        case 'W': options |= PCRE_UCP; break;
1644        case 'X': options |= PCRE_EXTRA; break;        case 'X': options |= PCRE_EXTRA; break;
1645          case 'Y': options |= PCRE_NO_START_OPTIMISE; break;
1646        case 'Z': debug_lengths = 0; break;        case 'Z': debug_lengths = 0; break;
1647        case '8': options |= PCRE_UTF8; use_utf8 = 1; break;        case '8': options |= PCRE_UTF8; use_utf8 = 1; break;
1648        case '?': options |= PCRE_NO_UTF8_CHECK; break;        case '?': options |= PCRE_NO_UTF8_CHECK; break;
# Line 1743  while (!done) Line 1822  while (!done)
1822      true_size = ((real_pcre *)re)->size;      true_size = ((real_pcre *)re)->size;
1823      regex_gotten_store = gotten_store;      regex_gotten_store = gotten_store;
1824    
1825      /* If /S was present, study the regexp to generate additional info to      /* If -s or /S was present, study the regex to generate additional info to
1826      help with the matching. */      help with the matching, unless the pattern has the SS option, which
1827        suppresses the effect of /S (used for a few test patterns where studying is
1828        never sensible). */
1829    
1830      if (do_study)      if (do_study || (force_study && !no_force_study))
1831        {        {
1832        if (timeit > 0)        if (timeit > 0)
1833          {          {
# Line 1900  while (!done) Line 1981  while (!done)
1981        if (do_flip) all_options = byteflip(all_options, sizeof(all_options));        if (do_flip) all_options = byteflip(all_options, sizeof(all_options));
1982    
1983        if (get_options == 0) fprintf(outfile, "No options\n");        if (get_options == 0) fprintf(outfile, "No options\n");
1984          else fprintf(outfile, "Options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",          else fprintf(outfile, "Options:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1985            ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",            ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",
1986            ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",            ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",
1987            ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",            ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",
# Line 1916  while (!done) Line 1997  while (!done)
1997            ((get_options & PCRE_UTF8) != 0)? " utf8" : "",            ((get_options & PCRE_UTF8) != 0)? " utf8" : "",
1998            ((get_options & PCRE_UCP) != 0)? " ucp" : "",            ((get_options & PCRE_UCP) != 0)? " ucp" : "",
1999            ((get_options & PCRE_NO_UTF8_CHECK) != 0)? " no_utf8_check" : "",            ((get_options & PCRE_NO_UTF8_CHECK) != 0)? " no_utf8_check" : "",
2000              ((get_options & PCRE_NO_START_OPTIMIZE) != 0)? " no_start_optimize" : "",
2001            ((get_options & PCRE_DUPNAMES) != 0)? " dupnames" : "");            ((get_options & PCRE_DUPNAMES) != 0)? " dupnames" : "");
2002    
2003        if (jchanged) fprintf(outfile, "Duplicate name status changes\n");        if (jchanged) fprintf(outfile, "Duplicate name status changes\n");
# Line 1983  while (!done) Line 2065  while (!done)
2065        /* Don't output study size; at present it is in any case a fixed        /* Don't output study size; at present it is in any case a fixed
2066        value, but it varies, depending on the computer architecture, and        value, but it varies, depending on the computer architecture, and
2067        so messes up the test suite. (And with the /F option, it might be        so messes up the test suite. (And with the /F option, it might be
2068        flipped.) */        flipped.) If study was forced by an external -s, don't show this
2069          information unless -i or -d was also present. This means that, except
2070          when auto-callouts are involved, the output from runs with and without
2071          -s should be identical. */
2072    
2073        if (do_study)        if (do_study || (force_study && showinfo && !no_force_study))
2074          {          {
2075          if (extra == NULL)          if (extra == NULL)
2076            fprintf(outfile, "Study returned NULL\n");            fprintf(outfile, "Study returned NULL\n");
# Line 2063  while (!done) Line 2148  while (!done)
2148            }            }
2149          else          else
2150            {            {
2151            fprintf(outfile, "Compiled regex written to %s\n", to_file);            fprintf(outfile, "Compiled pattern written to %s\n", to_file);
2152    
2153              /* If there is study data, write it, but verify the writing only
2154              if the studying was requested by /S, not just by -s. */
2155    
2156            if (extra != NULL)            if (extra != NULL)
2157              {              {
2158              if (fwrite(extra->study_data, 1, true_study_size, f) <              if (fwrite(extra->study_data, 1, true_study_size, f) <
# Line 2073  while (!done) Line 2162  while (!done)
2162                  strerror(errno));                  strerror(errno));
2163                }                }
2164              else fprintf(outfile, "Study data written to %s\n", to_file);              else fprintf(outfile, "Study data written to %s\n", to_file);
   
2165              }              }
2166            }            }
2167          fclose(f);          fclose(f);
# Line 2108  while (!done) Line 2196  while (!done)
2196      int getlist = 0;      int getlist = 0;
2197      int gmatched = 0;      int gmatched = 0;
2198      int start_offset = 0;      int start_offset = 0;
2199        int start_offset_sign = 1;
2200      int g_notempty = 0;      int g_notempty = 0;
2201      int use_dfa = 0;      int use_dfa = 0;
2202    
# Line 2240  while (!done) Line 2329  while (!done)
2329          continue;          continue;
2330    
2331          case '>':          case '>':
2332            if (*p == '-')
2333              {
2334              start_offset_sign = -1;
2335              p++;
2336              }
2337          while(isdigit(*p)) start_offset = start_offset * 10 + *p++ - '0';          while(isdigit(*p)) start_offset = start_offset * 10 + *p++ - '0';
2338            start_offset *= start_offset_sign;
2339          continue;          continue;
2340    
2341          case 'A':  /* Option setting */          case 'A':  /* Option setting */
# Line 2313  while (!done) Line 2408  while (!done)
2408  #endif  #endif
2409            use_dfa = 1;            use_dfa = 1;
2410          continue;          continue;
2411    #endif
2412    
2413    #if !defined NODFA
2414          case 'F':          case 'F':
2415          options |= PCRE_DFA_SHORTEST;          options |= PCRE_DFA_SHORTEST;
2416          continue;          continue;
# Line 2496  while (!done) Line 2593  while (!done)
2593              (void)pchars(dbuffer + pmatch[i].rm_so,              (void)pchars(dbuffer + pmatch[i].rm_so,
2594                pmatch[i].rm_eo - pmatch[i].rm_so, outfile);                pmatch[i].rm_eo - pmatch[i].rm_so, outfile);
2595              fprintf(outfile, "\n");              fprintf(outfile, "\n");
2596              if (i == 0 && do_showrest)              if (do_showcaprest || (i == 0 && do_showrest))
2597                {                {
2598                fprintf(outfile, " 0+ ");                fprintf(outfile, "%2d+ ", (int)i);
2599                (void)pchars(dbuffer + pmatch[i].rm_eo, len - pmatch[i].rm_eo,                (void)pchars(dbuffer + pmatch[i].rm_eo, len - pmatch[i].rm_eo,
2600                  outfile);                  outfile);
2601                fprintf(outfile, "\n");                fprintf(outfile, "\n");
# Line 2639  while (!done) Line 2736  while (!done)
2736              do_g = do_G = FALSE;        /* Break g/G loop */              do_g = do_G = FALSE;        /* Break g/G loop */
2737              }              }
2738            }            }
2739    
2740            /* do_allcaps requests showing of all captures in the pattern, to check
2741            unset ones at the end. */
2742    
2743            if (do_allcaps)
2744              {
2745              new_info(re, NULL, PCRE_INFO_CAPTURECOUNT, &count);
2746              count++;   /* Allow for full match */
2747              if (count * 2 > use_size_offsets) count = use_size_offsets/2;
2748              }
2749    
2750            /* Output the captured substrings */
2751    
2752          for (i = 0; i < count * 2; i += 2)          for (i = 0; i < count * 2; i += 2)
2753            {            {
2754            if (use_offsets[i] < 0)            if (use_offsets[i] < 0)
2755                {
2756                if (use_offsets[i] != -1)
2757                  fprintf(outfile, "ERROR: bad negative value %d for offset %d\n",
2758                    use_offsets[i], i);
2759                if (use_offsets[i+1] != -1)
2760                  fprintf(outfile, "ERROR: bad negative value %d for offset %d\n",
2761                    use_offsets[i+1], i+1);
2762              fprintf(outfile, "%2d: <unset>\n", i/2);              fprintf(outfile, "%2d: <unset>\n", i/2);
2763                }
2764            else            else
2765              {              {
2766              fprintf(outfile, "%2d: ", i/2);              fprintf(outfile, "%2d: ", i/2);
2767              (void)pchars(bptr + use_offsets[i],              (void)pchars(bptr + use_offsets[i],
2768                use_offsets[i+1] - use_offsets[i], outfile);                use_offsets[i+1] - use_offsets[i], outfile);
2769              fprintf(outfile, "\n");              fprintf(outfile, "\n");
2770              if (i == 0)              if (do_showcaprest || (i == 0 && do_showrest))
2771                {                {
2772                if (do_showrest)                fprintf(outfile, "%2d+ ", i/2);
2773                  {                (void)pchars(bptr + use_offsets[i+1], len - use_offsets[i+1],
2774                  fprintf(outfile, " 0+ ");                  outfile);
2775                  (void)pchars(bptr + use_offsets[i+1], len - use_offsets[i+1],                fprintf(outfile, "\n");
                   outfile);  
                 fprintf(outfile, "\n");  
                 }  
2776                }                }
2777              }              }
2778            }            }
# Line 2765  while (!done) Line 2879  while (!done)
2879        to advance the start offset, and continue. We won't be at the end of the        to advance the start offset, and continue. We won't be at the end of the
2880        string - that was checked before setting g_notempty.        string - that was checked before setting g_notempty.
2881    
2882        Complication arises in the case when the newline option is "any" or        Complication arises in the case when the newline convention is "any",
2883        "anycrlf". If the previous match was at the end of a line terminated by        "crlf", or "anycrlf". If the previous match was at the end of a line
2884        CRLF, an advance of one character just passes the \r, whereas we should        terminated by CRLF, an advance of one character just passes the \r,
2885        prefer the longer newline sequence, as does the code in pcre_exec().        whereas we should prefer the longer newline sequence, as does the code in
2886        Fudge the offset value to achieve this.        pcre_exec(). Fudge the offset value to achieve this. We check for a
2887          newline setting in the pattern; if none was set, use pcre_config() to
2888          find the default.
2889    
2890        Otherwise, in the case of UTF-8 matching, the advance must be one        Otherwise, in the case of UTF-8 matching, the advance must be one
2891        character, not one byte. */        character, not one byte. */
# Line 2794  while (!done) Line 2910  while (!done)
2910                      (d == -1)? PCRE_NEWLINE_ANY : 0;                      (d == -1)? PCRE_NEWLINE_ANY : 0;
2911              }              }
2912            if (((obits & PCRE_NEWLINE_BITS) == PCRE_NEWLINE_ANY ||            if (((obits & PCRE_NEWLINE_BITS) == PCRE_NEWLINE_ANY ||
2913                   (obits & PCRE_NEWLINE_BITS) == PCRE_NEWLINE_CRLF ||
2914                 (obits & PCRE_NEWLINE_BITS) == PCRE_NEWLINE_ANYCRLF)                 (obits & PCRE_NEWLINE_BITS) == PCRE_NEWLINE_ANYCRLF)
2915                &&                &&
2916                start_offset < len - 1 &&                start_offset < len - 1 &&
# Line 2804  while (!done) Line 2921  while (!done)
2921              {              {
2922              while (start_offset + onechar < len)              while (start_offset + onechar < len)
2923                {                {
2924                int tb = bptr[start_offset+onechar];                if ((bptr[start_offset+onechar] & 0xc0) != 0x80) break;
2925                if (tb <= 127) break;                onechar++;
               tb &= 0xc0;  
               if (tb != 0 && tb != 0xc0) onechar++;  
2926                }                }
2927              }              }
2928            use_offsets[1] = start_offset + onechar;            use_offsets[1] = start_offset + onechar;
2929            }            }
2930          else          else
2931            {            {
2932            if (count == PCRE_ERROR_NOMATCH)            switch(count)
2933              {              {
2934                case PCRE_ERROR_NOMATCH:
2935              if (gmatched == 0)              if (gmatched == 0)
2936                {                {
2937                if (markptr == NULL) fprintf(outfile, "No match\n");                if (markptr == NULL) fprintf(outfile, "No match\n");
2938                  else fprintf(outfile, "No match, mark = %s\n", markptr);                  else fprintf(outfile, "No match, mark = %s\n", markptr);
2939                }                }
2940                break;
2941    
2942                case PCRE_ERROR_BADUTF8:
2943                case PCRE_ERROR_SHORTUTF8:
2944                fprintf(outfile, "Error %d (%s UTF-8 string)", count,
2945                  (count == PCRE_ERROR_BADUTF8)? "bad" : "short");
2946                if (use_size_offsets >= 2)
2947                  fprintf(outfile, " offset=%d reason=%d", use_offsets[0],
2948                    use_offsets[1]);
2949                fprintf(outfile, "\n");
2950                break;
2951    
2952                default:
2953                if (count < 0 && (-count) < sizeof(errtexts)/sizeof(const char *))
2954                  fprintf(outfile, "Error %d (%s)\n", count, errtexts[-count]);
2955                else
2956                  fprintf(outfile, "Error %d (Unexpected value)\n", count);
2957                break;
2958              }              }
2959            else fprintf(outfile, "Error %d\n", count);  
2960            break;  /* Out of the /g loop */            break;  /* Out of the /g loop */
2961            }            }
2962          }          }

Legend:
Removed from v.545  
changed lines
  Added in v.626

  ViewVC Help
Powered by ViewVC 1.1.5