| 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 |
| 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 |
| 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 |
|
|
| 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; |
| 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"); |
| 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 |
|
|
| 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 |
|
|
| 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; |
| 1329 |
{ |
{ |
| 1330 |
for (;;) |
for (;;) |
| 1331 |
{ |
{ |
| 1332 |
if (extend_inputline(infile, buffer) == NULL) |
if (extend_inputline(infile, buffer, NULL) == NULL) |
| 1333 |
{ |
{ |
| 1334 |
done = 1; |
done = 1; |
| 1335 |
goto CONTINUE; |
goto CONTINUE; |
| 1724 |
len = 0; |
len = 0; |
| 1725 |
for (;;) |
for (;;) |
| 1726 |
{ |
{ |
| 1727 |
if (infile == stdin) printf("data> "); |
if (extend_inputline(infile, buffer + len, "data> ") == NULL) |
|
if (extend_inputline(infile, buffer + len) == NULL) |
|
| 1728 |
{ |
{ |
| 1729 |
if (len > 0) break; |
if (len > 0) break; |
| 1730 |
done = 1; |
done = 1; |