Skip to content

Commit

Permalink
Add compil and exec, both PCRE versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal MALAISE committed May 9, 2016
1 parent 2dfd30a commit 19da632
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 11 deletions.
5 changes: 3 additions & 2 deletions pcre/makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
include $(HOME)/Makefiles/common.mk

EXES := tpcre
EXES := tpcre tpcre2

LARGS_tpcre := -lpcre
LARGS_tpcre := -lpcreposix -lpcre
LARGS_tpcre2 := -lpcre2-posix -lpcre2-8

include $(TEMPLATES)/c.mk

84 changes: 75 additions & 9 deletions pcre/tpcre.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,86 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pcreposix.h>
#include <pcre.h>

int main (void) {
static void usage (char *name) {
fprintf (stderr, "Usage: %s <regex> { [ <string> ] }\n", name);
}

static void print_match (regmatch_t match) {
printf ("Match at %d-%d\n", match.rm_so, match.rm_eo - 1);
}

#define MAX_MATCHES 100
int main (int argc, char *argv[]) {

int i, j, n, res;
regex_t regex;
char buffer[1024];
regmatch_t matches[MAX_MATCHES];

/* At least one argument, -h | --help | <regex> */
if (argc < 2) {
usage (argv[0]);
exit (1);
}

/* Help */
if ( (strcmp (argv[1], "-h") == 0) || (strcmp (argv[1], "--help") == 0) ) {
usage (argv[0]);
exit (0);
}
/* Show version */
if ( (strcmp (argv[1], "-v") == 0) || (strcmp (argv[1], "--version") == 0) ) {
printf ("Pcre version is %s\n", pcre_version());
exit (0);
}

int res;
int set;
/* Compile regex */
res = regcomp (&regex, argv[1], 0);
if (res != 0) {
(void) regerror (res, &regex, buffer, sizeof(buffer));
fprintf (stderr, "ERROR: Compilation has failed with error: %s\n",
buffer);
exit (1);
}

/* Check strings */
for (i = 3; i <= argc; i++) {
/* Compile */
res = regexec (&regex, argv[i-1], MAX_MATCHES, matches, 0);
if (res == REG_NOMATCH) {
printf ("No match\n");
continue;
}
if (res != 0) {
(void) regerror (res, &regex, buffer, sizeof(buffer));
fprintf (stderr, "ERROR: Execution has failed with error: %s\n",
buffer);
exit (1);
}
/* Print result */
print_match (matches[0]);

printf ("Pcre version is %s\n", pcre_version());
/* Count matching substrings */
n = 0;
for (j = MAX_MATCHES - 1; j >= 0; j--) {
if (matches[j].rm_so != -1) {
n = j;
break;
}
}

printf ("Calling pcre_config\n");
res = pcre_config (PCRE_CONFIG_UTF8, &set);
printf ("Result is %d\n", res);
printf ("Set is %d\n", set);
/* Print matching substrings */
for (j = 1; j <= n; j++) {
print_match (matches[j]);
}
}

/* Cleanup */
regfree (&regex);

/* Done */
exit (0);
}

89 changes: 89 additions & 0 deletions pcre/tpcre2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <pcre2posix.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

static void usage (char *name) {
fprintf (stderr, "Usage: %s <regex> { [ <string> ] }\n", name);
}

static void print_match (regmatch_t match) {
printf ("Match at %d-%d\n", match.rm_so, match.rm_eo - 1);
}

#define MAX_MATCHES 100
int main (int argc, char *argv[]) {

int i, j, n, res;
regex_t regex;
char buffer[1024];
regmatch_t matches[MAX_MATCHES];

/* At least one argument, -h | --help | <regex> */
if (argc < 2) {
usage (argv[0]);
exit (1);
}

/* Help */
if ( (strcmp (argv[1], "-h") == 0) || (strcmp (argv[1], "--help") == 0) ) {
usage (argv[0]);
exit (0);
}
/* Show version */
if ( (strcmp (argv[1], "-v") == 0) || (strcmp (argv[1], "--version") == 0) ) {
res = pcre2_config (PCRE2_CONFIG_VERSION, buffer);
printf ("Pcre version is %s\n", buffer);
exit (0);
}

/* Compile regex */
res = regcomp (&regex, argv[1], 0);
if (res != 0) {
(void) regerror (res, &regex, buffer, sizeof(buffer));
fprintf (stderr, "ERROR: Compilation has failed with error: %s\n",
buffer);
exit (1);
}

/* Check strings */
for (i = 3; i <= argc; i++) {
/* Compile */
res = regexec (&regex, argv[i-1], MAX_MATCHES, matches, 0);
if (res == REG_NOMATCH) {
printf ("No match\n");
continue;
}
if (res != 0) {
(void) regerror (res, &regex, buffer, sizeof(buffer));
fprintf (stderr, "ERROR: Execution has failed with error: %s\n",
buffer);
exit (1);
}
/* Print result */
print_match (matches[0]);

/* Count matching substrings */
n = 0;
for (j = MAX_MATCHES - 1; j >= 0; j--) {
if (matches[j].rm_so != -1) {
n = j;
break;
}
}

/* Print matching substrings */
for (j = 1; j <= n; j++) {
print_match (matches[j]);
}
}

/* Cleanup */
regfree (&regex);

/* Done */
exit (0);
}

0 comments on commit 19da632

Please sign in to comment.