added re9_subst() and sample for it
[libre9.git] / src / test.c
blobeb600f3b91daf86d384b6f9006afca49db8e485a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "libre9/re9.h"
8 struct x {
9 const char *re;
10 const char *s;
11 re9_prog_t *p;
15 static struct x t[] = {
16 { "^[^!@]+$", "/bin/upas/aliasmail '\\0'", 0 },
17 { "^local!(.*)$", "/mail/box/\\1/mbox", 0 },
18 { "^plan9!(.*)$", "\\1", 0 },
19 { "^helix!(.*)$", "\\1", 0 },
20 { "^([^!]+)@([^!@]+)$", "\\2!\\1", 0 },
21 { "^(uk\\.[^!]*)(!.*)$", "/bin/upas/uk2uk '\\1' '\\2'", 0 },
22 { "^[^!]*\\.[^!]*!.*$", "inet!\\0", 0 },
23 { "^\xE2\x98\xBA$", "smiley", 0 },
24 { "^(coma|research|pipe|pyxis|inet|hunny|gauss)!(.*)$", "/mail/lib/qmail '\\s' 'net!\\1' '\\2'", 0 },
25 { "^.*$", "/mail/lib/qmail '\\s' 'net!research' '\\{0}'", 0 },
26 { 0, 0, 0 },
30 int main (int argc, char *argv[]) {
31 re9_sub_t rs[10];
32 static char dst[8192];
33 struct x *tp;
34 /* */
35 for (tp = t; tp->re; ++tp) tp->p = re9_compile(tp->re, RE9_FLAG_NONUTF8, NULL);
36 /* */
37 for (int f = 1; f < argc; ++f) {
38 if (f > 1) printf("===============================\n");
39 for (tp = t; tp->re; ++tp) {
40 printf("<%s> VIA <%s>", argv[f], tp->re);
41 if (re9_execute(tp->p, RE9_FLAG_NONUTF8, argv[f], rs, 10)) {
42 int sz = re9_sub(dst, sizeof(dst), tp->s, rs, 10);
43 printf(" sub %s -> <%s> (%d)", tp->s, dst, sz);
45 printf("\n");
48 /* */
49 for (tp = t; tp->re; ++tp) re9_free(tp->p);
50 /* */
51 return 0;