123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- #include <stdio.h>
- #include <string.h>
- #include <pcre.h>
- #define OVECCOUNT 30
- int main(int argc, char **argv)
- {
- pcre *re;
- const char *error;
- char *pattern;
- char *subject;
- unsigned char *name_table;
- unsigned int option_bits;
- int erroffset;
- int find_all;
- int crlf_is_newline;
- int namecount;
- int name_entry_size;
- int ovector[OVECCOUNT];
- int subject_length;
- int rc, i;
- int utf8;
- find_all = 0;
- for (i = 1; i < argc; i++)
- {
- if (strcmp(argv[i], "-g") == 0) find_all = 1;
- else break;
- }
- if (argc - i != 2)
- {
- printf("Two arguments required: a regex and a subject string\n");
- return 1;
- }
- pattern = argv[i];
- subject = argv[i+1];
- subject_length = (int)strlen(subject);
- re = pcre_compile(
- pattern,
- 0,
- &error,
- &erroffset,
- NULL);
- if (re == NULL)
- {
- printf("PCRE compilation failed at offset %d: %s\n", erroffset, error);
- return 1;
- }
- rc = pcre_exec(
- re,
- NULL,
- subject,
- subject_length,
- 0,
- 0,
- ovector,
- OVECCOUNT);
- if (rc < 0)
- {
- switch(rc)
- {
- case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
-
- default: printf("Matching error %d\n", rc); break;
- }
- pcre_free(re);
- return 1;
- }
- printf("\nMatch succeeded at offset %d\n", ovector[0]);
- if (rc == 0)
- {
- rc = OVECCOUNT/3;
- printf("ovector only has room for %d captured substrings\n", rc - 1);
- }
- for (i = 0; i < rc; i++)
- {
- char *substring_start = subject + ovector[2*i];
- int substring_length = ovector[2*i+1] - ovector[2*i];
- printf("%2d: %.*s\n", i, substring_length, substring_start);
- }
- (void)pcre_fullinfo(
- re,
- NULL,
- PCRE_INFO_NAMECOUNT,
- &namecount);
- if (namecount <= 0) printf("No named substrings\n"); else
- {
- unsigned char *tabptr;
- printf("Named substrings\n");
-
- (void)pcre_fullinfo(
- re,
- NULL,
- PCRE_INFO_NAMETABLE,
- &name_table);
- (void)pcre_fullinfo(
- re,
- NULL,
- PCRE_INFO_NAMEENTRYSIZE,
- &name_entry_size);
-
- tabptr = name_table;
- for (i = 0; i < namecount; i++)
- {
- int n = (tabptr[0] << 8) | tabptr[1];
- printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
- ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
- tabptr += name_entry_size;
- }
- }
- if (!find_all)
- {
- pcre_free(re);
- return 0;
- }
- (void)pcre_fullinfo(re, NULL, PCRE_INFO_OPTIONS, &option_bits);
- utf8 = option_bits & PCRE_UTF8;
- option_bits &= PCRE_NEWLINE_CR|PCRE_NEWLINE_LF|PCRE_NEWLINE_CRLF|
- PCRE_NEWLINE_ANY|PCRE_NEWLINE_ANYCRLF;
- if (option_bits == 0)
- {
- int d;
- (void)pcre_config(PCRE_CONFIG_NEWLINE, &d);
-
- option_bits = (d == 13)? PCRE_NEWLINE_CR :
- (d == 10)? PCRE_NEWLINE_LF :
- (d == (13<<8 | 10))? PCRE_NEWLINE_CRLF :
- (d == -2)? PCRE_NEWLINE_ANYCRLF :
- (d == -1)? PCRE_NEWLINE_ANY : 0;
- }
- crlf_is_newline =
- option_bits == PCRE_NEWLINE_ANY ||
- option_bits == PCRE_NEWLINE_CRLF ||
- option_bits == PCRE_NEWLINE_ANYCRLF;
- for (;;)
- {
- int options = 0;
- int start_offset = ovector[1];
-
- if (ovector[0] == ovector[1])
- {
- if (ovector[0] == subject_length) break;
- options = PCRE_NOTEMPTY_ATSTART | PCRE_ANCHORED;
- }
-
- rc = pcre_exec(
- re,
- NULL,
- subject,
- subject_length,
- start_offset,
- options,
- ovector,
- OVECCOUNT);
-
- if (rc == PCRE_ERROR_NOMATCH)
- {
- if (options == 0) break;
- ovector[1] = start_offset + 1;
- if (crlf_is_newline &&
- start_offset < subject_length - 1 &&
- subject[start_offset] == '\r' &&
- subject[start_offset + 1] == '\n')
- ovector[1] += 1;
- else if (utf8)
- {
- while (ovector[1] < subject_length)
- {
- if ((subject[ovector[1]] & 0xc0) != 0x80) break;
- ovector[1] += 1;
- }
- }
- continue;
- }
-
- if (rc < 0)
- {
- printf("Matching error %d\n", rc);
- pcre_free(re);
- return 1;
- }
-
- printf("\nMatch succeeded again at offset %d\n", ovector[0]);
-
- if (rc == 0)
- {
- rc = OVECCOUNT/3;
- printf("ovector only has room for %d captured substrings\n", rc - 1);
- }
-
- for (i = 0; i < rc; i++)
- {
- char *substring_start = subject + ovector[2*i];
- int substring_length = ovector[2*i+1] - ovector[2*i];
- printf("%2d: %.*s\n", i, substring_length, substring_start);
- }
- if (namecount <= 0) printf("No named substrings\n"); else
- {
- unsigned char *tabptr = name_table;
- printf("Named substrings\n");
- for (i = 0; i < namecount; i++)
- {
- int n = (tabptr[0] << 8) | tabptr[1];
- printf("(%d) %*s: %.*s\n", n, name_entry_size - 3, tabptr + 2,
- ovector[2*n+1] - ovector[2*n], subject + ovector[2*n]);
- tabptr += name_entry_size;
- }
- }
- }
- printf("\n");
- pcre_free(re);
- return 0;
- }
|