Kannel: Open Source WAP and SMS gateway  svn-r5335
test_regex.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * test_regex.c - test regex module
59  *
60  * Stipe Tolj <stolj@wapme.de>
61  */
62 
63 #include <string.h>
64 #include <unistd.h>
65 #include <signal.h>
66 
67 #include "gwlib/gwlib.h"
68 #include "gwlib/gw-regex.h"
69 
70 #if defined(HAVE_REGEX) || defined(HAVE_PCRE)
71 
72 int main(int argc, char **argv)
73 {
74  Octstr *re, *os, *sub;
75  Octstr *tmp;
76  regex_t *regexp;
77  regmatch_t pmatch[REGEX_MAX_SUB_MATCH];
78  int rc;
79 
80  gwlib_init();
81 
82  get_and_set_debugs(argc, argv, NULL);
83 
84  if (argc < 4)
85  panic(0, "Syntax: %s <os> <re> <sub>\n", argv[0]);
86 
87  os = octstr_create(argv[1]);
88  re = octstr_create(argv[2]);
89  sub = octstr_create(argv[3]);
90 
91  info(0, "step 1: generic functions");
92 
93  /* compile */
94  if ((regexp = gw_regex_comp(re, REG_EXTENDED)) == NULL)
95  panic(0, "regex compilation failed!");
96 
97  debug("regex",0,"RE: regex <%s> has %ld subexpressions.",
98  octstr_get_cstr(re), (long)regexp->re_nsub);
99 
100  /* execute */
101  rc = gw_regex_exec(regexp, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0);
102  if (rc == REG_NOMATCH) {
103  info(0, "RE: regex <%s> did not match on string <%s>.",
105  } else if (rc != 0) {
106  Octstr *err = gw_regex_error(rc, regexp);
107  error(0, "RE: regex <%s> execution failed: %s",
108  octstr_get_cstr(re), octstr_get_cstr(err));
109  octstr_destroy(err);
110  } else {
111  int i;
112  char *rsub;
113  debug("regex",0,"RE: regex <%s> matches.", octstr_get_cstr(re));
114  debug("regex",0,"RE: substring matches are:");
115  for (i = 0; i <= regexp->re_nsub; i++) {
116  if (pmatch[i].rm_so != -1 && pmatch[i].rm_eo != -1) {
117  Octstr *s = octstr_copy(os, pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
118  debug("regex",0,"RE: %d: <%s>", i, octstr_get_cstr(s));
119  octstr_destroy(s);
120  }
121  }
122  rsub = gw_regex_sub(octstr_get_cstr(sub), octstr_get_cstr(os),
123  REGEX_MAX_SUB_MATCH, &pmatch[0]);
124  debug("regex",0,"RE: substituted string is <%s>.", rsub);
125  gw_free(rsub);
126  }
127 
128  info(0, "step 2: wrapper functions");
129 
130  debug("regex",0,"RE: regex_match <%s> on <%s> did: %s",
132  gw_regex_match(re, os) ? "match" : "NOT match");
133 
134  debug("regex",0,"RE: regex_match_pre on <%s> did: %s",
135  octstr_get_cstr(os),
136  gw_regex_match_pre(regexp, os) ? "match" : "NOT match");
137 
138  tmp = gw_regex_subst(re, os, sub);
139  debug("regex",0,"RE: regex_subst <%s> on <%s> rule <%s>: %s",
141  octstr_get_cstr(tmp));
142  octstr_destroy(tmp);
143 
144  tmp = gw_regex_subst_pre(regexp, os, sub);
145  debug("regex",0,"RE: regex_subst_pre on <%s> rule <%s>: %s",
147 
148  gw_regex_destroy(regexp);
149  octstr_destroy(tmp);
150  octstr_destroy(re);
151  octstr_destroy(os);
152  gwlib_shutdown();
153  return 0;
154 }
155 
156 #endif
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
#define octstr_copy(ostr, from, len)
Definition: octstr.h:178
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
int main(int argc, char **argv)
Definition: opensmppbox.c:2620
#define octstr_create(cstr)
Definition: octstr.h:125
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
#define panic
Definition: log.h:87
void gwlib_shutdown(void)
Definition: gwlib.c:94
void gwlib_init(void)
Definition: gwlib.c:78
int get_and_set_debugs(int argc, char **argv, int(*find_own)(int index, int argc, char **argv))
Definition: utils.c:626
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.