Commit 79f8b002 authored by Jamie A. Jennings's avatar Jamie A. Jennings

Updated match.c and added a few rplx files (in data directory) for testing

parent fe9b6461
Pipeline #51139442 passed with stage
in 1 minute and 7 seconds
......@@ -2,23 +2,31 @@
/* */
/* match.c */
/* */
/* © Copyright Jamie A. Jennings 2018. */
/* © Copyright Jamie A. Jennings 2018, 2019. */
/* LICENSE: MIT License (https://opensource.org/licenses/mit-license.html) */
/* AUTHOR: Jamie A. Jennings */
/* THIS IS ONLY A TEST! IT IS A PROOF OF CONCEPT ILLUSTRATING HOW SMALL
* AN EXECUTABLE COULD BE IF IT CONTAINS ONLY THE ROSIE RUN-TIME.
*/
// gcc match.c -o match -I../include ../runtime/*.o
// ./match data/findall:net.ipv4.rplx data/log10.txt | json_pp
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/errno.h>
#include "../config.h"
#include "../rplx.h"
#include "../file.h"
#include "../buf.h"
#include "../vm.h"
#include "../json.h"
#include "../capture.h"
#include "config.h"
#include "rplx.h"
#include "file.h"
#include "buf.h"
#include "vm.h"
#include "json.h"
#include "capture.h"
void error(const char *message, const char *additional) {
fprintf(stderr, "Error: %s; %s\n", message, additional);
......@@ -59,9 +67,9 @@ int main(int argc, char **argv) {
int err;
Chunk c;
Encoder json_encoder = { json_Open, json_Fullcapture, json_Close };
Encoder noop_encoder = { noop_Open, noop_Fullcapture, noop_Close };
Encoder debug_encoder = { debug_Open, debug_Fullcapture, debug_Close };
Encoder json_encoder = { json_Open, json_Close };
Encoder noop_encoder = { noop_Open, noop_Close };
Encoder debug_encoder = { debug_Open, debug_Close };
Encoder *encoder = &json_encoder;
......@@ -76,20 +84,19 @@ int main(int argc, char **argv) {
char *line;
size_t len;
Match *match = match_new();
Buffer *input = buf_new(0);
Buffer input;
Stats stats = {0};
while ((line = fgetln(f, &len)) != NULL) {
if ((len > 0) && (*(line + len - 1) == '\n')) len -= 1;
/* input = buf_from(line, len); */
input->n = len;
input->data = line;
input.n = len;
input.data = line;
if (match->data) buf_reset(match->data);
err = vm_match(&c, input, 1, *encoder, match, (DEBUG ? &stats : NULL));
err = vm_match(&c, &input, 1, *encoder, match, (DEBUG ? &stats : NULL));
if (err) error(STRERROR(err, MATCH_MESSAGES), "expected successful match");
if (match->matched) {
if (encoder == &noop_encoder) {
fwrite(input->data, 1, input->n, stdout); /* '-o line' */
fwrite(input.data, 1, input.n, stdout); /* '-o data' */
} else {
fwrite(match->data->data, 1, match->data->n, stdout);
}
......@@ -100,8 +107,8 @@ int main(int argc, char **argv) {
stats.backtrack, stats.caplist, stats.capdepth);
#endif
}
/* free(input); */
}
match_free(match);
fclose(f);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment