Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / lib / tptregexp / try.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: try.c /main/3 1996/06/19 17:13:58 drk $ */
24 /*
25  * Simple test program for regexp(3) stuff.  Knows about debugging hooks.
26  *
27  *      Copyright (c) 1986 by University of Toronto.
28  *      Written by Henry Spencer.  Not derived from licensed software.
29  *
30  *      Permission is granted to anyone to use this software for any
31  *      purpose on any computer system, and to redistribute it freely,
32  *      subject to the following restrictions:
33  *
34  *      1. The author is not responsible for the consequences of use of
35  *              this software, no matter how awful, even if they arise
36  *              from defects in it.
37  *
38  *      2. The origin of this software must not be misrepresented, either
39  *              by explicit claim or by omission.
40  *
41  *      3. Altered versions must be plainly marked as such, and must not
42  *              be misrepresented as being the original software.
43  *
44  * Usage: try re [string [output [-]]]
45  * The re is compiled and dumped, regexeced against the string, the result
46  * is applied to output using regsub().  The - triggers a running narrative
47  * from regexec().  Dumping and narrative don't happen unless DEBUG.
48  *
49  * If there are no arguments, stdin is assumed to be a stream of lines with
50  * five fields:  a r.e., a string to match it against, a result code, a
51  * source string for regsub, and the proper result.  Result codes are 'c'
52  * for compile failure, 'y' for match success, 'n' for match failure.
53  * Field separator is tab.
54  */
55 #include <stdio.h>
56 #include <tptregexp.h>
57
58 #ifdef ERRAVAIL
59 char *progname;
60 extern char *mkprogname();
61 #endif
62
63 #ifdef DEBUG
64 extern int regnarrate;
65 #endif
66
67 char buf[BUFSIZ];
68
69 int errreport = 0;              /* Report errors via errseen? */
70 char *errseen = NULL;           /* Error message. */
71 int status = 0;                 /* Exit status. */
72
73 /* ARGSUSED */
74 main(argc, argv)
75 int argc;
76 char *argv[];
77 {
78         regexp *r;
79         int i;
80
81 #ifdef ERRAVAIL
82         progname = mkprogname(argv[0]);
83 #endif
84
85         if (argc == 1) {
86                 multiple();
87                 exit(status);
88         }
89
90         r = tpt_regcomp(argv[1]);
91         if (r == NULL)
92                 error("tpt_regcomp failure", "");
93 #ifdef DEBUG
94         regdump(r);
95         if (argc > 4)
96                 regnarrate++;
97 #endif
98         if (argc > 2) {
99                 i = tpt_regexec(r, argv[2]);
100                 printf("%d", i);
101                 for (i = 1; i < NSUBEXP; i++)
102                         if (r->startp[i] != NULL && r->endp[i] != NULL)
103                                 printf(" \\%d", i);
104                 printf("\n");
105         }
106         if (argc > 3) {
107                 tpt_regsub(r, argv[3], buf);
108                 printf("%s\n", buf);
109         }
110         exit(status);
111 }
112
113 void
114 tpt_regerror(s)
115 char *s;
116 {
117         if (errreport)
118                 errseen = s;
119         else
120                 error(s, "");
121 }
122
123 #ifndef ERRAVAIL
124 error(s1, s2)
125 char *s1;
126 char *s2;
127 {
128         fprintf(stderr, "regexp: ");
129         fprintf(stderr, s1, s2);
130         fprintf(stderr, "\n");
131         exit(1);
132 }
133 #endif
134
135 int lineno;
136
137 regexp badregexp;               /* Implicit init to 0. */
138
139 multiple()
140 {
141         char rbuf[BUFSIZ];
142         char *field[5];
143         char *scan;
144         int i;
145         regexp *r;
146         extern char *strchr();
147
148         errreport = 1;
149         lineno = 0;
150         while (fgets(rbuf, sizeof(rbuf), stdin) != NULL) {
151                 rbuf[strlen(rbuf)-1] = '\0';    /* Dispense with \n. */
152                 lineno++;
153                 scan = rbuf;
154                 for (i = 0; i < 5; i++) {
155                         field[i] = scan;
156                         if (field[i] == NULL) {
157                                 complain("bad testfile format", "");
158                                 exit(1);
159                         }
160                         scan = strchr(scan, '\t');
161                         if (scan != NULL)
162                                 *scan++ = '\0';
163                 }
164                 try(field);
165         }
166
167         /* And finish up with some internal testing... */
168         lineno = 9990;
169         errseen = NULL;
170         if (tpt_regcomp((char *)NULL) != NULL || errseen == NULL)
171                 complain("tpt_regcomp(NULL) doesn't complain", "");
172         lineno = 9991;
173         errseen = NULL;
174         if (tpt_regexec((regexp *)NULL, "foo") || errseen == NULL)
175                 complain("tpt_regexec(NULL, ...) doesn't complain", "");
176         lineno = 9992;
177         r = tpt_regcomp("foo");
178         if (r == NULL) {
179                 complain("tpt_regcomp(\"foo\") fails", "");
180                 return;
181         }
182         lineno = 9993;
183         errseen = NULL;
184         if (tpt_regexec(r, (char *)NULL) || errseen == NULL)
185                 complain("tpt_regexec(..., NULL) doesn't complain", "");
186         lineno = 9994;
187         errseen = NULL;
188         tpt_regsub((regexp *)NULL, "foo", rbuf);
189         if (errseen == NULL)
190                 complain("tpt_regsub(NULL, ..., ...) doesn't complain", "");
191         lineno = 9995;
192         errseen = NULL;
193         tpt_regsub(r, (char *)NULL, rbuf);
194         if (errseen == NULL)
195                 complain("tpt_regsub(..., NULL, ...) doesn't complain", "");
196         lineno = 9996;
197         errseen = NULL;
198         tpt_regsub(r, "foo", (char *)NULL);
199         if (errseen == NULL)
200                 complain("tpt_regsub(..., ..., NULL) doesn't complain", "");
201         lineno = 9997;
202         errseen = NULL;
203         if (tpt_regexec(&badregexp, "foo") || errseen == NULL)
204                 complain("tpt_regexec(nonsense, ...) doesn't complain", "");
205         lineno = 9998;
206         errseen = NULL;
207         tpt_regsub(&badregexp, "foo", rbuf);
208         if (errseen == NULL)
209                 complain("tpt_regsub(nonsense, ..., ...) doesn't complain", "");
210 }
211
212 try(fields)
213 char **fields;
214 {
215         regexp *r;
216         char dbuf[BUFSIZ];
217
218         errseen = NULL;
219         r = tpt_regcomp(fields[0]);
220         if (r == NULL) {
221                 if (*fields[2] != 'c')
222                         complain("tpt_regcomp failure in `%s'", fields[0]);
223                 return;
224         }
225         if (*fields[2] == 'c') {
226                 complain("unexpected tpt_regcomp success in `%s'", fields[0]);
227                 free((char *)r);
228                 return;
229         }
230         if (!tpt_regexec(r, fields[1])) {
231                 if (*fields[2] != 'n')
232                         complain("tpt_regexec failure in `%s'", "");
233                 free((char *)r);
234                 return;
235         }
236         if (*fields[2] == 'n') {
237                 complain("unexpected tpt_regexec success", "");
238                 free((char *)r);
239                 return;
240         }
241         errseen = NULL;
242         tpt_regsub(r, fields[3], dbuf);
243         if (errseen != NULL) {
244                 complain("tpt_regsub complaint", "");
245                 free((char *)r);
246                 return;
247         }
248         if (strcmp(dbuf, fields[4]) != 0)
249                 complain("tpt_regsub result `%s' wrong", dbuf);
250         free((char *)r);
251 }
252
253 complain(s1, s2)
254 char *s1;
255 char *s2;
256 {
257         fprintf(stderr, "try: %d: ", lineno);
258         fprintf(stderr, s1, s2);
259         fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
260         status = 1;
261 }