-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compil and exec, both PCRE versions
- Loading branch information
Pascal MALAISE
committed
May 9, 2016
1 parent
2dfd30a
commit 19da632
Showing
3 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (®ex, argv[1], 0); | ||
if (res != 0) { | ||
(void) regerror (res, ®ex, 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 (®ex, argv[i-1], MAX_MATCHES, matches, 0); | ||
if (res == REG_NOMATCH) { | ||
printf ("No match\n"); | ||
continue; | ||
} | ||
if (res != 0) { | ||
(void) regerror (res, ®ex, 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 (®ex); | ||
|
||
/* Done */ | ||
exit (0); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (®ex, argv[1], 0); | ||
if (res != 0) { | ||
(void) regerror (res, ®ex, 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 (®ex, argv[i-1], MAX_MATCHES, matches, 0); | ||
if (res == REG_NOMATCH) { | ||
printf ("No match\n"); | ||
continue; | ||
} | ||
if (res != 0) { | ||
(void) regerror (res, ®ex, 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 (®ex); | ||
|
||
/* Done */ | ||
exit (0); | ||
} |