Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtdocbook / lib / tptregexp / timer.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: timer.c /main/3 1996/06/19 17:13:50 drk $ */
24 /*
25  * Simple timing program for regcomp().
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: timer ncomp nexec nsub
45  *      or
46  *      timer ncomp nexec nsub regexp string [ answer [ sub ] ]
47  *
48  * The second form is for timing repetitions of a single test case.
49  * The first form's test data is a compiled-in copy of the "tests" file.
50  * Ncomp, nexec, nsub are how many times to do each regcomp, regexec,
51  * and regsub.  The way to time an operation individually is to do something
52  * like "timer 1 50 1".
53  */
54 #include <stdio.h>
55
56 struct try {
57         char *re, *str, *ans, *src, *dst;
58 } tests[] = {
59 #include "timer.t.h"
60 { NULL, NULL, NULL, NULL, NULL }
61 };
62
63 #include <tptregexp.h>
64
65 int errreport = 0;              /* Report errors via errseen? */
66 char *errseen = NULL;           /* Error message. */
67
68 char *progname;
69
70 /* ARGSUSED */
71 main(argc, argv)
72 int argc;
73 char *argv[];
74 {
75         int ncomp, nexec, nsub;
76         struct try one;
77         char dummy[512];
78
79         if (argc < 4) {
80                 ncomp = 1;
81                 nexec = 1;
82                 nsub = 1;
83         } else {
84                 ncomp = atoi(argv[1]);
85                 nexec = atoi(argv[2]);
86                 nsub = atoi(argv[3]);
87         }
88         
89         progname = argv[0];
90         if (argc > 5) {
91                 one.re = argv[4];
92                 one.str = argv[5];
93                 if (argc > 6)
94                         one.ans = argv[6];
95                 else
96                         one.ans = "y";
97                 if (argc > 7) { 
98                         one.src = argv[7];
99                         one.dst = "xxx";
100                 } else {
101                         one.src = "x";
102                         one.dst = "x";
103                 }
104                 errreport = 1;
105                 try(one, ncomp, nexec, nsub);
106         } else
107                 multiple(ncomp, nexec, nsub);
108         exit(0);
109 }
110
111 void
112 tpt_regerror(s)
113 char *s;
114 {
115         if (errreport)
116                 errseen = s;
117         else
118                 error(s, "");
119 }
120
121 #ifndef ERRAVAIL
122 error(s1, s2)
123 char *s1;
124 char *s2;
125 {
126         fprintf(stderr, "regexp: ");
127         fprintf(stderr, s1, s2);
128         fprintf(stderr, "\n");
129         exit(1);
130 }
131 #endif
132
133 int lineno = 0;
134
135 multiple(ncomp, nexec, nsub)
136 int ncomp, nexec, nsub;
137 {
138         register int i;
139         extern char *strchr();
140
141         errreport = 1;
142         for (i = 0; tests[i].re != NULL; i++) {
143                 lineno++;
144                 try(tests[i], ncomp, nexec, nsub);
145         }
146 }
147
148 try(fields, ncomp, nexec, nsub)
149 struct try fields;
150 int ncomp, nexec, nsub;
151 {
152         regexp *r;
153         char dbuf[BUFSIZ];
154         register int i;
155
156         errseen = NULL;
157         r = tpt_regcomp(fields.re);
158         if (r == NULL) {
159                 if (*fields.ans != 'c')
160                         complain("tpt_regcomp failure in `%s'", fields.re);
161                 return;
162         }
163         if (*fields.ans == 'c') {
164                 complain("unexpected tpt_regcomp success in `%s'", fields.re);
165                 free((char *)r);
166                 return;
167         }
168         for (i = ncomp-1; i > 0; i--) {
169                 free((char *)r);
170                 r = tpt_regcomp(fields.re);
171         }
172         if (!tpt_regexec(r, fields.str)) {
173                 if (*fields.ans != 'n')
174                         complain("tpt_regexec failure in `%s'", "");
175                 free((char *)r);
176                 return;
177         }
178         if (*fields.ans == 'n') {
179                 complain("unexpected tpt_regexec success", "");
180                 free((char *)r);
181                 return;
182         }
183         for (i = nexec-1; i > 0; i--)
184                 (void) tpt_regexec(r, fields.str);
185         errseen = NULL;
186         for (i = nsub; i > 0; i--)
187                 tpt_regsub(r, fields.src, dbuf);
188         if (errseen != NULL) {  
189                 complain("tpt_regsub complaint", "");
190                 free((char *)r);
191                 return;
192         }
193         if (strcmp(dbuf, fields.dst) != 0)
194                 complain("tpt_regsub result `%s' wrong", dbuf);
195         free((char *)r);
196 }
197
198 complain(s1, s2)
199 char *s1;
200 char *s2;
201 {
202         fprintf(stderr, "try: %d: ", lineno);
203         fprintf(stderr, s1, s2);
204         fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
205 }