/[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 227 by ph10, Tue Aug 21 15:00:15 2007 UTC revision 336 by ph10, Sat Apr 12 15:59:03 2008 UTC
# Line 37  POSSIBILITY OF SUCH DAMAGE. Line 37  POSSIBILITY OF SUCH DAMAGE.
37    
38    
39  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
40  #include <config.h>  #include "config.h"
41  #endif  #endif
42    
43  #include <ctype.h>  #include <ctype.h>
# Line 48  POSSIBILITY OF SUCH DAMAGE. Line 48  POSSIBILITY OF SUCH DAMAGE.
48  #include <locale.h>  #include <locale.h>
49  #include <errno.h>  #include <errno.h>
50    
51    #ifdef SUPPORT_LIBREADLINE
52    #include <unistd.h>
53    #include <readline/readline.h>
54    #include <readline/history.h>
55    #endif
56    
57    
58  /* A number of things vary for Windows builds. Originally, pcretest opened its  /* A number of things vary for Windows builds. Originally, pcretest opened its
59  input and output without "b"; then I was told that "b" was needed in some  input and output without "b"; then I was told that "b" was needed in some
# Line 94  symbols to prevent clashes. */ Line 100  symbols to prevent clashes. */
100  #define _pcre_utf8_table4      utf8_table4  #define _pcre_utf8_table4      utf8_table4
101  #define _pcre_utt              utt  #define _pcre_utt              utt
102  #define _pcre_utt_size         utt_size  #define _pcre_utt_size         utt_size
103    #define _pcre_utt_names        utt_names
104  #define _pcre_OP_lengths       OP_lengths  #define _pcre_OP_lengths       OP_lengths
105    
106  #include "pcre_tables.c"  #include "pcre_tables.c"
# Line 188  optimal way of handling this, but hey, t Line 195  optimal way of handling this, but hey, t
195  Arguments:  Arguments:
196    f            the file to read    f            the file to read
197    start        where in buffer to start (this *must* be within buffer)    start        where in buffer to start (this *must* be within buffer)
198      prompt       for stdin or readline()
199    
200  Returns:       pointer to the start of new data  Returns:       pointer to the start of new data
201                 could be a copy of start, or could be moved                 could be a copy of start, or could be moved
# Line 195  Returns: pointer to the start of n Line 203  Returns: pointer to the start of n
203  */  */
204    
205  static uschar *  static uschar *
206  extend_inputline(FILE *f, uschar *start)  extend_inputline(FILE *f, uschar *start, const char *prompt)
207  {  {
208  uschar *here = start;  uschar *here = start;
209    
# Line 206  for (;;) Line 214  for (;;)
214    if (rlen > 1000)    if (rlen > 1000)
215      {      {
216      int dlen;      int dlen;
217      if (fgets((char *)here, rlen,  f) == NULL)  
218        return (here == start)? NULL : start;      /* If libreadline support is required, use readline() to read a line if the
219        input is a terminal. Note that readline() removes the trailing newline, so
220        we must put it back again, to be compatible with fgets(). */
221    
222    #ifdef SUPPORT_LIBREADLINE
223        if (isatty(fileno(f)))
224          {
225          size_t len;
226          char *s = readline(prompt);
227          if (s == NULL) return (here == start)? NULL : start;
228          len = strlen(s);
229          if (len > 0) add_history(s);
230          if (len > rlen - 1) len = rlen - 1;
231          memcpy(here, s, len);
232          here[len] = '\n';
233          here[len+1] = 0;
234          free(s);
235          }
236        else
237    #endif
238    
239        /* Read the next line by normal means, prompting if the file is stdin. */
240    
241          {
242          if (f == stdin) printf(prompt);
243          if (fgets((char *)here, rlen,  f) == NULL)
244            return (here == start)? NULL : start;
245          }
246    
247      dlen = (int)strlen((char *)here);      dlen = (int)strlen((char *)here);
248      if (dlen > 0 && here[dlen - 1] == '\n') return start;      if (dlen > 0 && here[dlen - 1] == '\n') return start;
249      here += dlen;      here += dlen;
# Line 712  if (strncmpic(p, (uschar *)"lf>", 3) == Line 748  if (strncmpic(p, (uschar *)"lf>", 3) ==
748  if (strncmpic(p, (uschar *)"crlf>", 5) == 0) return PCRE_NEWLINE_CRLF;  if (strncmpic(p, (uschar *)"crlf>", 5) == 0) return PCRE_NEWLINE_CRLF;
749  if (strncmpic(p, (uschar *)"anycrlf>", 8) == 0) return PCRE_NEWLINE_ANYCRLF;  if (strncmpic(p, (uschar *)"anycrlf>", 8) == 0) return PCRE_NEWLINE_ANYCRLF;
750  if (strncmpic(p, (uschar *)"any>", 4) == 0) return PCRE_NEWLINE_ANY;  if (strncmpic(p, (uschar *)"any>", 4) == 0) return PCRE_NEWLINE_ANY;
751    if (strncmpic(p, (uschar *)"bsr_anycrlf>", 12) == 0) return PCRE_BSR_ANYCRLF;
752    if (strncmpic(p, (uschar *)"bsr_unicode>", 12) == 0) return PCRE_BSR_UNICODE;
753  fprintf(f, "Unknown newline type at: <%s\n", p);  fprintf(f, "Unknown newline type at: <%s\n", p);
754  return 0;  return 0;
755  }  }
# Line 725  return 0; Line 763  return 0;
763  static void  static void
764  usage(void)  usage(void)
765  {  {
766  printf("Usage:     pcretest [options] [<input> [<output>]]\n");  printf("Usage:     pcretest [options] [<input file> [<output file>]]\n\n");
767    printf("Input and output default to stdin and stdout.\n");
768    #ifdef SUPPORT_LIBREADLINE
769    printf("If input is a terminal, readline() is used to read from it.\n");
770    #else
771    printf("This version of pcretest is not linked with readline().\n");
772    #endif
773    printf("\nOptions:\n");
774  printf("  -b       show compiled code (bytecode)\n");  printf("  -b       show compiled code (bytecode)\n");
775  printf("  -C       show PCRE compile-time options and exit\n");  printf("  -C       show PCRE compile-time options and exit\n");
776  printf("  -d       debug: show compiled code and information (-b and -i)\n");  printf("  -d       debug: show compiled code and information (-b and -i)\n");
# Line 885  while (argc > 1 && argv[op][0] == '-') Line 930  while (argc > 1 && argv[op][0] == '-')
930        (rc == '\n')? "LF" : (rc == ('\r'<<8 | '\n'))? "CRLF" :        (rc == '\n')? "LF" : (rc == ('\r'<<8 | '\n'))? "CRLF" :
931        (rc == -2)? "ANYCRLF" :        (rc == -2)? "ANYCRLF" :
932        (rc == -1)? "ANY" : "???");        (rc == -1)? "ANY" : "???");
933        (void)pcre_config(PCRE_CONFIG_BSR, &rc);
934        printf("  \\R matches %s\n", rc? "CR, LF, or CRLF only" :
935                                         "all Unicode newlines");
936      (void)pcre_config(PCRE_CONFIG_LINK_SIZE, &rc);      (void)pcre_config(PCRE_CONFIG_LINK_SIZE, &rc);
937      printf("  Internal link size = %d\n", rc);      printf("  Internal link size = %d\n", rc);
938      (void)pcre_config(PCRE_CONFIG_POSIX_MALLOC_THRESHOLD, &rc);      (void)pcre_config(PCRE_CONFIG_POSIX_MALLOC_THRESHOLD, &rc);
# Line 991  while (!done) Line 1039  while (!done)
1039    use_utf8 = 0;    use_utf8 = 0;
1040    debug_lengths = 1;    debug_lengths = 1;
1041    
1042    if (infile == stdin) printf("  re> ");    if (extend_inputline(infile, buffer, "  re> ") == NULL) break;
   if (extend_inputline(infile, buffer) == NULL) break;  
1043    if (infile != stdin) fprintf(outfile, "%s", (char *)buffer);    if (infile != stdin) fprintf(outfile, "%s", (char *)buffer);
1044    fflush(outfile);    fflush(outfile);
1045    
# Line 1092  while (!done) Line 1139  while (!done)
1139    
1140    if (isalnum(delimiter) || delimiter == '\\')    if (isalnum(delimiter) || delimiter == '\\')
1141      {      {
1142      fprintf(outfile, "** Delimiter must not be alphameric or \\\n");      fprintf(outfile, "** Delimiter must not be alphanumeric or \\\n");
1143      goto SKIP_DATA;      goto SKIP_DATA;
1144      }      }
1145    
# Line 1108  while (!done) Line 1155  while (!done)
1155        pp++;        pp++;
1156        }        }
1157      if (*pp != 0) break;      if (*pp != 0) break;
1158      if (infile == stdin) printf("    > ");      if ((pp = extend_inputline(infile, pp, "    > ")) == NULL)
     if ((pp = extend_inputline(infile, pp)) == NULL)  
1159        {        {
1160        fprintf(outfile, "** Unexpected EOF\n");        fprintf(outfile, "** Unexpected EOF\n");
1161        done = 1;        done = 1;
# Line 1201  while (!done) Line 1247  while (!done)
1247    
1248        case '<':        case '<':
1249          {          {
1250          int x = check_newline(pp, outfile);          if (strncmp((char *)pp, "JS>", 3) == 0)
1251          if (x == 0) goto SKIP_DATA;            {
1252          options |= x;            options |= PCRE_JAVASCRIPT_COMPAT;
1253          while (*pp++ != '>');            pp += 3;
1254              }
1255            else
1256              {
1257              int x = check_newline(pp, outfile);
1258              if (x == 0) goto SKIP_DATA;
1259              options |= x;
1260              while (*pp++ != '>');
1261              }
1262          }          }
1263        break;        break;
1264    
# Line 1283  while (!done) Line 1337  while (!done)
1337          {          {
1338          for (;;)          for (;;)
1339            {            {
1340            if (extend_inputline(infile, buffer) == NULL)            if (extend_inputline(infile, buffer, NULL) == NULL)
1341              {              {
1342              done = 1;              done = 1;
1343              goto CONTINUE;              goto CONTINUE;
# Line 1346  while (!done) Line 1400  while (!done)
1400      if (do_flip)      if (do_flip)
1401        {        {
1402        real_pcre *rre = (real_pcre *)re;        real_pcre *rre = (real_pcre *)re;
1403        rre->magic_number = byteflip(rre->magic_number, sizeof(rre->magic_number));        rre->magic_number =
1404            byteflip(rre->magic_number, sizeof(rre->magic_number));
1405        rre->size = byteflip(rre->size, sizeof(rre->size));        rre->size = byteflip(rre->size, sizeof(rre->size));
1406        rre->options = byteflip(rre->options, sizeof(rre->options));        rre->options = byteflip(rre->options, sizeof(rre->options));
1407        rre->top_bracket = byteflip(rre->top_bracket, sizeof(rre->top_bracket));        rre->flags = (pcre_uint16)byteflip(rre->flags, sizeof(rre->flags));
1408        rre->top_backref = byteflip(rre->top_backref, sizeof(rre->top_backref));        rre->top_bracket =
1409        rre->first_byte = byteflip(rre->first_byte, sizeof(rre->first_byte));          (pcre_uint16)byteflip(rre->top_bracket, sizeof(rre->top_bracket));
1410        rre->req_byte = byteflip(rre->req_byte, sizeof(rre->req_byte));        rre->top_backref =
1411        rre->name_table_offset = byteflip(rre->name_table_offset,          (pcre_uint16)byteflip(rre->top_backref, sizeof(rre->top_backref));
1412          rre->first_byte =
1413            (pcre_uint16)byteflip(rre->first_byte, sizeof(rre->first_byte));
1414          rre->req_byte =
1415            (pcre_uint16)byteflip(rre->req_byte, sizeof(rre->req_byte));
1416          rre->name_table_offset = (pcre_uint16)byteflip(rre->name_table_offset,
1417          sizeof(rre->name_table_offset));          sizeof(rre->name_table_offset));
1418        rre->name_entry_size = byteflip(rre->name_entry_size,        rre->name_entry_size = (pcre_uint16)byteflip(rre->name_entry_size,
1419          sizeof(rre->name_entry_size));          sizeof(rre->name_entry_size));
1420        rre->name_count = byteflip(rre->name_count, sizeof(rre->name_count));        rre->name_count = (pcre_uint16)byteflip(rre->name_count,
1421            sizeof(rre->name_count));
1422    
1423        if (extra != NULL)        if (extra != NULL)
1424          {          {
# Line 1448  while (!done) Line 1509  while (!done)
1509        if (do_flip) all_options = byteflip(all_options, sizeof(all_options));        if (do_flip) all_options = byteflip(all_options, sizeof(all_options));
1510    
1511        if (get_options == 0) fprintf(outfile, "No options\n");        if (get_options == 0) fprintf(outfile, "No options\n");
1512          else fprintf(outfile, "Options:%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\n",
1513            ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",            ((get_options & PCRE_ANCHORED) != 0)? " anchored" : "",
1514            ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",            ((get_options & PCRE_CASELESS) != 0)? " caseless" : "",
1515            ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",            ((get_options & PCRE_EXTENDED) != 0)? " extended" : "",
1516            ((get_options & PCRE_MULTILINE) != 0)? " multiline" : "",            ((get_options & PCRE_MULTILINE) != 0)? " multiline" : "",
1517            ((get_options & PCRE_FIRSTLINE) != 0)? " firstline" : "",            ((get_options & PCRE_FIRSTLINE) != 0)? " firstline" : "",
1518            ((get_options & PCRE_DOTALL) != 0)? " dotall" : "",            ((get_options & PCRE_DOTALL) != 0)? " dotall" : "",
1519              ((get_options & PCRE_BSR_ANYCRLF) != 0)? " bsr_anycrlf" : "",
1520              ((get_options & PCRE_BSR_UNICODE) != 0)? " bsr_unicode" : "",
1521            ((get_options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",            ((get_options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "",
1522            ((get_options & PCRE_EXTRA) != 0)? " extra" : "",            ((get_options & PCRE_EXTRA) != 0)? " extra" : "",
1523            ((get_options & PCRE_UNGREEDY) != 0)? " ungreedy" : "",            ((get_options & PCRE_UNGREEDY) != 0)? " ungreedy" : "",
# Line 1587  while (!done) Line 1650  while (!done)
1650        else        else
1651          {          {
1652          uschar sbuf[8];          uschar sbuf[8];
1653          sbuf[0] = (true_size >> 24)  & 255;          sbuf[0] = (uschar)((true_size >> 24) & 255);
1654          sbuf[1] = (true_size >> 16)  & 255;          sbuf[1] = (uschar)((true_size >> 16) & 255);
1655          sbuf[2] = (true_size >>  8)  & 255;          sbuf[2] = (uschar)((true_size >>  8) & 255);
1656          sbuf[3] = (true_size)  & 255;          sbuf[3] = (uschar)((true_size) & 255);
1657    
1658          sbuf[4] = (true_study_size >> 24)  & 255;          sbuf[4] = (uschar)((true_study_size >> 24) & 255);
1659          sbuf[5] = (true_study_size >> 16)  & 255;          sbuf[5] = (uschar)((true_study_size >> 16) & 255);
1660          sbuf[6] = (true_study_size >>  8)  & 255;          sbuf[6] = (uschar)((true_study_size >>  8) & 255);
1661          sbuf[7] = (true_study_size)  & 255;          sbuf[7] = (uschar)((true_study_size) & 255);
1662    
1663          if (fwrite(sbuf, 1, 8, f) < 8 ||          if (fwrite(sbuf, 1, 8, f) < 8 ||
1664              fwrite(re, 1, true_size, f) < true_size)              fwrite(re, 1, true_size, f) < true_size)
# Line 1669  while (!done) Line 1732  while (!done)
1732      len = 0;      len = 0;
1733      for (;;)      for (;;)
1734        {        {
1735        if (infile == stdin) printf("data> ");        if (extend_inputline(infile, buffer + len, "data> ") == NULL)
       if (extend_inputline(infile, buffer + len) == NULL)  
1736          {          {
1737          if (len > 0) break;          if (len > 0) break;
1738          done = 1;          done = 1;

Legend:
Removed from v.227  
changed lines
  Added in v.336

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12