/[pcre]/code/trunk/pcretest.c
ViewVC logotype

Diff of /code/trunk/pcretest.c

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

revision 65 by nigel, Sat Feb 24 21:40:08 2007 UTC revision 71 by nigel, Sat Feb 24 21:40:24 2007 UTC
# Line 37  Makefile. */ Line 37  Makefile. */
37    
38  #define LOOPREPEAT 50000  #define LOOPREPEAT 50000
39    
40    #define BUFFER_SIZE 30000
41    #define DBUFFER_SIZE 1024
42    
43    
44  static FILE *outfile;  static FILE *outfile;
45  static int log_store = 0;  static int log_store = 0;
# Line 45  static int callout_extra; Line 48  static int callout_extra;
48  static int callout_fail_count;  static int callout_fail_count;
49  static int callout_fail_id;  static int callout_fail_id;
50  static int first_callout;  static int first_callout;
51  static int utf8;  static int use_utf8;
52  static size_t gotten_store;  static size_t gotten_store;
53    
54    
55    static const int utf8_table1[] = {
 static int utf8_table1[] = {  
56    0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff};    0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff};
57    
58  static int utf8_table2[] = {  static const int utf8_table2[] = {
59    0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc};    0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
60    
61  static int utf8_table3[] = {  static const int utf8_table3[] = {
62    0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};    0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};
63    
64    
# Line 155  Returns:   >  0 => the number of bytes c Line 157  Returns:   >  0 => the number of bytes c
157             -6 to 0 => malformed UTF-8 character at offset = (-return)             -6 to 0 => malformed UTF-8 character at offset = (-return)
158  */  */
159    
160  int  static int
161  utf82ord(unsigned char *buffer, int *vptr)  utf82ord(unsigned char *buffer, int *vptr)
162  {  {
163  int c = *buffer++;  int c = *buffer++;
# Line 213  int yield = 0; Line 215  int yield = 0;
215    
216  while (length-- > 0)  while (length-- > 0)
217    {    {
218    if (utf8)    if (use_utf8)
219      {      {
220      int rc = utf82ord(p, &c);      int rc = utf82ord(p, &c);
221    
# Line 270  int i, pre_start, post_start; Line 272  int i, pre_start, post_start;
272    
273  if (callout_extra)  if (callout_extra)
274    {    {
   int i;  
275    fprintf(f, "Callout %d: last capture = %d\n",    fprintf(f, "Callout %d: last capture = %d\n",
276      cb->callout_number, cb->capture_last);      cb->callout_number, cb->capture_last);
277    
# Line 319  if (post_start > 0) Line 320  if (post_start > 0)
320    }    }
321    
322  fprintf(outfile, "\n");  fprintf(outfile, "\n");
   
323  first_callout = 0;  first_callout = 0;
324    
325  if ((int)(cb->callout_data) != 0)  if (cb->callout_data != NULL)
326    {    {
327    fprintf(outfile, "Callout data = %d\n", (int)(cb->callout_data));    int callout_data = *((int *)(cb->callout_data));
328    return (int)(cb->callout_data);    if (callout_data != 0)
329        {
330        fprintf(outfile, "Callout data = %d\n", callout_data);
331        return callout_data;
332        }
333    }    }
334    
335  return (cb->callout_number != callout_fail_id)? 0 :  return (cb->callout_number != callout_fail_id)? 0 :
# Line 388  int posix = 0; Line 392  int posix = 0;
392  #endif  #endif
393  int debug = 0;  int debug = 0;
394  int done = 0;  int done = 0;
395  unsigned char buffer[30000];  
396  unsigned char dbuffer[1024];  unsigned char *buffer;
397    unsigned char *dbuffer;
398    
399    /* Get buffers from malloc() so that Electric Fence will check their misuse
400    when I am debugging. */
401    
402    buffer = (unsigned char *)malloc(BUFFER_SIZE);
403    dbuffer = (unsigned char *)malloc(DBUFFER_SIZE);
404    
405  /* Static so that new_malloc can use it. */  /* Static so that new_malloc can use it. */
406    
# Line 455  while (argc > 1 && argv[op][0] == '-') Line 466  while (argc > 1 && argv[op][0] == '-')
466  /* Get the store for the offsets vector, and remember what it was */  /* Get the store for the offsets vector, and remember what it was */
467    
468  size_offsets_max = size_offsets;  size_offsets_max = size_offsets;
469  offsets = malloc(size_offsets_max * sizeof(int));  offsets = (int *)malloc(size_offsets_max * sizeof(int));
470  if (offsets == NULL)  if (offsets == NULL)
471    {    {
472    printf("** Failed to get %d bytes of memory for offsets vector\n",    printf("** Failed to get %d bytes of memory for offsets vector\n",
# Line 516  while (!done) Line 527  while (!done)
527    int do_showrest = 0;    int do_showrest = 0;
528    int erroroffset, len, delimiter;    int erroroffset, len, delimiter;
529    
530    utf8 = 0;    use_utf8 = 0;
531    
532    if (infile == stdin) printf("  re> ");    if (infile == stdin) printf("  re> ");
533    if (fgets((char *)buffer, sizeof(buffer), infile) == NULL) break;    if (fgets((char *)buffer, BUFFER_SIZE, infile) == NULL) break;
534    if (infile != stdin) fprintf(outfile, "%s", (char *)buffer);    if (infile != stdin) fprintf(outfile, "%s", (char *)buffer);
535    fflush(outfile);    fflush(outfile);
536    
# Line 550  while (!done) Line 561  while (!done)
561        }        }
562      if (*pp != 0) break;      if (*pp != 0) break;
563    
564      len = sizeof(buffer) - (pp - buffer);      len = BUFFER_SIZE - (pp - buffer);
565      if (len < 256)      if (len < 256)
566        {        {
567        fprintf(outfile, "** Expression too long - missing delimiter?\n");        fprintf(outfile, "** Expression too long - missing delimiter?\n");
# Line 609  while (!done) Line 620  while (!done)
620        case 'S': do_study = 1; break;        case 'S': do_study = 1; break;
621        case 'U': options |= PCRE_UNGREEDY; break;        case 'U': options |= PCRE_UNGREEDY; break;
622        case 'X': options |= PCRE_EXTRA; break;        case 'X': options |= PCRE_EXTRA; break;
623        case '8': options |= PCRE_UTF8; utf8 = 1; break;        case '8': options |= PCRE_UTF8; use_utf8 = 1; break;
624          case '?': options |= PCRE_NO_UTF8_CHECK; break;
625    
626        case 'L':        case 'L':
627        ppp = pp;        ppp = pp;
# Line 649  while (!done) Line 661  while (!done)
661    
662      if (rc != 0)      if (rc != 0)
663        {        {
664        (void)regerror(rc, &preg, (char *)buffer, sizeof(buffer));        (void)regerror(rc, &preg, (char *)buffer, BUFFER_SIZE);
665        fprintf(outfile, "Failed: POSIX code %d: %s\n", rc, buffer);        fprintf(outfile, "Failed: POSIX code %d: %s\n", rc, buffer);
666        goto SKIP_DATA;        goto SKIP_DATA;
667        }        }
# Line 690  while (!done) Line 702  while (!done)
702          {          {
703          for (;;)          for (;;)
704            {            {
705            if (fgets((char *)buffer, sizeof(buffer), infile) == NULL)            if (fgets((char *)buffer, BUFFER_SIZE, infile) == NULL)
706              {              {
707              done = 1;              done = 1;
708              goto CONTINUE;              goto CONTINUE;
# Line 737  while (!done) Line 749  while (!done)
749        new_info(re, NULL, PCRE_INFO_LASTLITERAL, &need_char);        new_info(re, NULL, PCRE_INFO_LASTLITERAL, &need_char);
750        new_info(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize);        new_info(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize);
751        new_info(re, NULL, PCRE_INFO_NAMECOUNT, &namecount);        new_info(re, NULL, PCRE_INFO_NAMECOUNT, &namecount);
752        new_info(re, NULL, PCRE_INFO_NAMETABLE, &nametable);        new_info(re, NULL, PCRE_INFO_NAMETABLE, (void *)&nametable);
753    
754        old_count = pcre_info(re, &old_options, &old_first_char);        old_count = pcre_info(re, &old_options, &old_first_char);
755        if (count < 0) fprintf(outfile,        if (count < 0) fprintf(outfile,
# Line 778  while (!done) Line 790  while (!done)
790          }          }
791    
792        if (get_options == 0) fprintf(outfile, "No options\n");        if (get_options == 0) fprintf(outfile, "No options\n");
793          else fprintf(outfile, "Options:%s%s%s%s%s%s%s%s%s\n",          else fprintf(outfile, "Options:%s%s%s%s%s%s%s%s%s%s\n",
794            ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",            ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",
795            ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",            ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",
796            ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",            ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",
# Line 787  while (!done) Line 799  while (!done)
799            ((get_options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",            ((get_options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",
800            ((get_options & PCRE_EXTRA) != 0)? " extra" : "",            ((get_options & PCRE_EXTRA) != 0)? " extra" : "",
801            ((get_options & PCRE_UNGREEDY) != 0)? " ungreedy" : "",            ((get_options & PCRE_UNGREEDY) != 0)? " ungreedy" : "",
802            ((get_options & PCRE_UTF8) != 0)? " utf8" : "");            ((get_options & PCRE_UTF8) != 0)? " utf8" : "",
803              ((get_options & PCRE_NO_UTF8_CHECK) != 0)? " no_utf8_check" : "");
804    
805        if (((((real_pcre *)re)->options) & PCRE_ICHANGED) != 0)        if (((((real_pcre *)re)->options) & PCRE_ICHANGED) != 0)
806          fprintf(outfile, "Case state changes\n");          fprintf(outfile, "Case state changes\n");
# Line 803  while (!done) Line 816  while (!done)
816        else        else
817          {          {
818          int ch = first_char & 255;          int ch = first_char & 255;
819          char *caseless = ((first_char & REQ_CASELESS) == 0)?          const char *caseless = ((first_char & REQ_CASELESS) == 0)?
820            "" : " (caseless)";            "" : " (caseless)";
821          if (isprint(ch))          if (isprint(ch))
822            fprintf(outfile, "First char = \'%c\'%s\n", ch, caseless);            fprintf(outfile, "First char = \'%c\'%s\n", ch, caseless);
# Line 818  while (!done) Line 831  while (!done)
831        else        else
832          {          {
833          int ch = need_char & 255;          int ch = need_char & 255;
834          char *caseless = ((need_char & REQ_CASELESS) == 0)?          const char *caseless = ((need_char & REQ_CASELESS) == 0)?
835            "" : " (caseless)";            "" : " (caseless)";
836          if (isprint(ch))          if (isprint(ch))
837            fprintf(outfile, "Need char = \'%c\'%s\n", ch, caseless);            fprintf(outfile, "Need char = \'%c\'%s\n", ch, caseless);
# Line 852  while (!done) Line 865  while (!done)
865        else if (extra == NULL)        else if (extra == NULL)
866          fprintf(outfile, "Study returned NULL\n");          fprintf(outfile, "Study returned NULL\n");
867    
868          /* Don't output study size; at present it is in any case a fixed
869          value, but it varies, depending on the computer architecture, and
870          so messes up the test suite. */
871    
872        else if (do_showinfo)        else if (do_showinfo)
873          {          {
874          size_t size;          size_t size;
875          uschar *start_bits = NULL;          uschar *start_bits = NULL;
876          new_info(re, extra, PCRE_INFO_STUDYSIZE, &size);          new_info(re, extra, PCRE_INFO_STUDYSIZE, &size);
877          new_info(re, extra, PCRE_INFO_FIRSTTABLE, &start_bits);          new_info(re, extra, PCRE_INFO_FIRSTTABLE, &start_bits);
878          fprintf(outfile, "Study size = %d\n", size);          /* fprintf(outfile, "Study size = %d\n", size); */
879          if (start_bits == NULL)          if (start_bits == NULL)
880            fprintf(outfile, "No starting character set\n");            fprintf(outfile, "No starting character set\n");
881          else          else
# Line 922  while (!done) Line 939  while (!done)
939      callout_fail_id = -1;      callout_fail_id = -1;
940    
941      if (infile == stdin) printf("data> ");      if (infile == stdin) printf("data> ");
942      if (fgets((char *)buffer, sizeof(buffer), infile) == NULL)      if (fgets((char *)buffer, BUFFER_SIZE, infile) == NULL)
943        {        {
944        done = 1;        done = 1;
945        goto CONTINUE;        goto CONTINUE;
# Line 973  while (!done) Line 990  while (!done)
990              c = c * 16 + tolower(*pt) - ((isdigit(*pt))? '0' : 'W');              c = c * 16 + tolower(*pt) - ((isdigit(*pt))? '0' : 'W');
991            if (*pt == '}')            if (*pt == '}')
992              {              {
993              unsigned char buffer[8];              unsigned char buff8[8];
994              int ii, utn;              int ii, utn;
995              utn = ord2utf8(c, buffer);              utn = ord2utf8(c, buff8);
996              for (ii = 0; ii < utn - 1; ii++) *q++ = buffer[ii];              for (ii = 0; ii < utn - 1; ii++) *q++ = buff8[ii];
997              c = buffer[ii];   /* Last byte */              c = buff8[ii];   /* Last byte */
998              p = pt + 1;              p = pt + 1;
999              break;              break;
1000              }              }
# Line 1015  while (!done) Line 1032  while (!done)
1032          else if (isalnum(*p))          else if (isalnum(*p))
1033            {            {
1034            uschar name[256];            uschar name[256];
1035            uschar *pp = name;            uschar *npp = name;
1036            while (isalnum(*p)) *pp++ = *p++;            while (isalnum(*p)) *npp++ = *p++;
1037            *pp = 0;            *npp = 0;
1038            n = pcre_get_stringnumber(re, (char *)name);            n = pcre_get_stringnumber(re, (char *)name);
1039            if (n < 0)            if (n < 0)
1040              fprintf(outfile, "no parentheses with name \"%s\"\n", name);              fprintf(outfile, "no parentheses with name \"%s\"\n", name);
# Line 1068  while (!done) Line 1085  while (!done)
1085          else if (isalnum(*p))          else if (isalnum(*p))
1086            {            {
1087            uschar name[256];            uschar name[256];
1088            uschar *pp = name;            uschar *npp = name;
1089            while (isalnum(*p)) *pp++ = *p++;            while (isalnum(*p)) *npp++ = *p++;
1090            *pp = 0;            *npp = 0;
1091            n = pcre_get_stringnumber(re, (char *)name);            n = pcre_get_stringnumber(re, (char *)name);
1092            if (n < 0)            if (n < 0)
1093              fprintf(outfile, "no parentheses with name \"%s\"\n", name);              fprintf(outfile, "no parentheses with name \"%s\"\n", name);
# Line 1096  while (!done) Line 1113  while (!done)
1113            {            {
1114            size_offsets_max = n;            size_offsets_max = n;
1115            free(offsets);            free(offsets);
1116            use_offsets = offsets = malloc(size_offsets_max * sizeof(int));            use_offsets = offsets = (int *)malloc(size_offsets_max * sizeof(int));
1117            if (offsets == NULL)            if (offsets == NULL)
1118              {              {
1119              printf("** Failed to get %d bytes of memory for offsets vector\n",              printf("** Failed to get %d bytes of memory for offsets vector\n",
# Line 1111  while (!done) Line 1128  while (!done)
1128          case 'Z':          case 'Z':
1129          options |= PCRE_NOTEOL;          options |= PCRE_NOTEOL;
1130          continue;          continue;
1131    
1132            case '?':
1133            options |= PCRE_NO_UTF8_CHECK;
1134            continue;
1135          }          }
1136        *q++ = c;        *q++ = c;
1137        }        }
# Line 1127  while (!done) Line 1148  while (!done)
1148        int eflags = 0;        int eflags = 0;
1149        regmatch_t *pmatch = NULL;        regmatch_t *pmatch = NULL;
1150        if (use_size_offsets > 0)        if (use_size_offsets > 0)
1151          pmatch = malloc(sizeof(regmatch_t) * use_size_offsets);          pmatch = (regmatch_t *)malloc(sizeof(regmatch_t) * use_size_offsets);
1152        if ((options & PCRE_NOTBOL) != 0) eflags |= REG_NOTBOL;        if ((options & PCRE_NOTBOL) != 0) eflags |= REG_NOTBOL;
1153        if ((options & PCRE_NOTEOL) != 0) eflags |= REG_NOTEOL;        if ((options & PCRE_NOTEOL) != 0) eflags |= REG_NOTEOL;
1154    
# Line 1135  while (!done) Line 1156  while (!done)
1156    
1157        if (rc != 0)        if (rc != 0)
1158          {          {
1159          (void)regerror(rc, &preg, (char *)buffer, sizeof(buffer));          (void)regerror(rc, &preg, (char *)buffer, BUFFER_SIZE);
1160          fprintf(outfile, "No match: POSIX code %d: %s\n", rc, buffer);          fprintf(outfile, "No match: POSIX code %d: %s\n", rc, buffer);
1161          }          }
1162        else        else
# Line 1194  while (!done) Line 1215  while (!done)
1215    
1216          if (extra == NULL)          if (extra == NULL)
1217            {            {
1218            extra = malloc(sizeof(pcre_extra));            extra = (pcre_extra *)malloc(sizeof(pcre_extra));
1219            extra->flags = 0;            extra->flags = 0;
1220            }            }
1221          extra->flags |= PCRE_EXTRA_MATCH_LIMIT;          extra->flags |= PCRE_EXTRA_MATCH_LIMIT;
# Line 1233  while (!done) Line 1254  while (!done)
1254          {          {
1255          if (extra == NULL)          if (extra == NULL)
1256            {            {
1257            extra = malloc(sizeof(pcre_extra));            extra = (pcre_extra *)malloc(sizeof(pcre_extra));
1258            extra->flags = 0;            extra->flags = 0;
1259            }            }
1260          extra->flags |= PCRE_EXTRA_CALLOUT_DATA;          extra->flags |= PCRE_EXTRA_CALLOUT_DATA;
1261          extra->callout_data = (void *)callout_data;          extra->callout_data = &callout_data;
1262          count = pcre_exec(re, extra, (char *)bptr, len, start_offset,          count = pcre_exec(re, extra, (char *)bptr, len, start_offset,
1263            options | g_notempty, use_offsets, use_size_offsets);            options | g_notempty, use_offsets, use_size_offsets);
1264          extra->flags &= ~PCRE_EXTRA_CALLOUT_DATA;          extra->flags &= ~PCRE_EXTRA_CALLOUT_DATA;

Legend:
Removed from v.65  
changed lines
  Added in v.71

  ViewVC Help
Powered by ViewVC 1.1.5