Skip to content

Commit

Permalink
main.c: Add [-hmd] [-e ENGINE] flags
Browse files Browse the repository at this point in the history
Remove the previous "match" argument. Instead, the added -m now
signifies "match" ("search" is the default).
Fix run-tests accordingly.

The -d flag exists only when compiling with -DDEBUG.
The debug messages don't appear unless -d is used. This is convenient
when debugging, and it also allows run-tests to use a binary compiled
with -DDEBUG (else the debug messages confuse it).

The intention of -e ENGINE is to ease the debug/development of a
specific engine. It is especially handy with the -t flag (trace VM
commands) that is to be introduced next.
  • Loading branch information
ampli authored and pfalcon committed Jul 7, 2015
1 parent b3f466d commit d275668
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
87 changes: 70 additions & 17 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ struct {
{"pike", re1_5_pikevm},
};

#ifdef DEBUG
int debug;
#endif
const char *re_engine;

void
usage(void)
{
fprintf(stderr, "usage: re search|match <regexp> <string>...\n");
fprintf(stderr, "Usage: re [-hmd] [-e ENGINE] <regexp> <string>...\n"
"-h: Print help message and exit\n"
"-m: String is anchored\n"
"-e ENGINE: Specify one of: recursive recursiveloop backtrack thompson pike\n");
#ifdef DEBUG
fprintf(stderr,
"-d: Print debug messages\n");
#endif
exit(2);
}

Expand All @@ -28,44 +40,81 @@ main(int argc, char **argv)
int i, j, k, l;
int is_anchored = 0;

if(argc < 3)
usage();
argv++;
argc--;
while (argc > 0 && argv[0][0] == '-') {
for (char *arg = &argv[0][1]; *arg; arg++) {
switch (*arg) {
case 'h':
usage();
break;
case 'm':
is_anchored = 1;
break;
#ifdef DEBUG
case 'd':
debug = 1;
break;
#endif
case 'e':
if (argv[1] == NULL)
re1_5_fatal("-e: Missing Regex engine argument");
if (re_engine)
re1_5_fatal("-e: Regex engine already specified");
re_engine = argv[1];
argv++;
argc--;
break;
default:
re1_5_fatal("Unknown flag");
}
}
argv++;
argc--;
}

if (*argv[1] == 'm')
is_anchored = 1;
if(argc < 2)
usage();

#ifdef ODEBUG
Regexp *re = parse(argv[2]);
#ifdef ODEBUG
// Old and unmaintained code
Regexp *re = parse(argv[0]);
printre(re);
printf("\n");

Prog *prog = compile(re);
printprog(prog);
printf("=============\n");
#endif
int sz = re1_5_sizecode(argv[2]);
#ifdef DEBUG
printf("Precalculated size: %d\n", sz);
#endif
#endif
int sz = re1_5_sizecode(argv[0]);
#ifdef DEBUG
if (debug) printf("Precalculated size: %d\n", sz);
#endif
if (sz == -1) {
re1_5_fatal("Error in regexp");
}

ByteProg *code = malloc(sizeof(ByteProg) + sz);
int ret = re1_5_compilecode(code, argv[2]);
int ret = re1_5_compilecode(code, argv[0]);
if (ret != 0) {
re1_5_fatal("Error in regexp");
}
#ifdef DEBUG
re1_5_dumpcode(code);
#endif

int sub_els = (code->sub + 1) * 2;
#ifdef DEBUG
if (debug) re1_5_dumpcode(code);
#endif
const char *sub[sub_els];
for(i=3; i<argc; i++) {
int engine_found = 0;
for(i=1; i<argc; i++) {
printf("#%d %s\n", i, argv[i]);
for(j=0; j<nelem(tab); j++) {
Subject subj = {argv[i], argv[i] + strlen(argv[i])};
if (re_engine) {
if (0 != strcmp(re_engine, tab[j].name))
continue;
engine_found = 1;
}
printf("%s ", tab[j].name);
memset(sub, 0, sub_els * sizeof sub[0]);
if(!tab[j].fn(code, &subj, sub, sub_els, is_anchored)) {
Expand All @@ -91,6 +140,10 @@ main(int argc, char **argv)
}
printf("\n");
}
if (re_engine && !engine_found)
re1_5_fatal("-e: Unknown engine name");
}

free(code);
return 0;
}
3 changes: 2 additions & 1 deletion run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def main():

# run our code
try:
re_res = subprocess.check_output([RE_EXEC, kind, regex, string], stderr=subprocess.STDOUT)
args = (["-m"] if kind == "match" else []) + [regex, string]
re_res = subprocess.check_output([RE_EXEC]+args, stderr=subprocess.STDOUT)
re_res = re_res.split(b'\n')[1:-1] # split lines, remove first and last
except subprocess.CalledProcessError as e:
if e.returncode == 2 and e.output == b"fatal error: Error in regexp\n":
Expand Down

0 comments on commit d275668

Please sign in to comment.