1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * diagcode library unit test
29*7c478bd9Sstevel@tonic-gate *
30*7c478bd9Sstevel@tonic-gate * usually run from "make test" target. takes a single argument
31*7c478bd9Sstevel@tonic-gate * which is the directory where the test dictionaries are found.
32*7c478bd9Sstevel@tonic-gate * this test driver scans the dictionaries for comments of the form:
33*7c478bd9Sstevel@tonic-gate * #TEST:<routine>:<errno>:<input>:<output>
34*7c478bd9Sstevel@tonic-gate * and executes that test.
35*7c478bd9Sstevel@tonic-gate *
36*7c478bd9Sstevel@tonic-gate * exit 0 and an "All tests passed" message means no failures. otherwise
37*7c478bd9Sstevel@tonic-gate * error messages are spewed as appropriate and exit value is non-zero.
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <string.h>
43*7c478bd9Sstevel@tonic-gate #include <ctype.h>
44*7c478bd9Sstevel@tonic-gate #include <alloca.h>
45*7c478bd9Sstevel@tonic-gate #include <errno.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
47*7c478bd9Sstevel@tonic-gate #include <dirent.h>
48*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate #include <fm/diagcode.h>
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #define MAXLINE 10240
53*7c478bd9Sstevel@tonic-gate #define MAXARG 10
54*7c478bd9Sstevel@tonic-gate #define MAXKEY 100
55*7c478bd9Sstevel@tonic-gate #define MAXCODE 100
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate static char *Myname;
58*7c478bd9Sstevel@tonic-gate static char *Dict;
59*7c478bd9Sstevel@tonic-gate static int Line;
60*7c478bd9Sstevel@tonic-gate static int Errcount;
61*7c478bd9Sstevel@tonic-gate static fm_dc_handle_t *Dhp;
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
64*7c478bd9Sstevel@tonic-gate static void
err(const char * fmt,...)65*7c478bd9Sstevel@tonic-gate err(const char *fmt, ...)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate va_list ap;
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate va_start(ap, fmt);
70*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s:%d ", Myname, Dict, Line);
71*7c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
72*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
73*7c478bd9Sstevel@tonic-gate Errcount++;
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate /* parse an expected errno value from test line (numeric or some symbolic) */
77*7c478bd9Sstevel@tonic-gate static int
geterrno(const char * s)78*7c478bd9Sstevel@tonic-gate geterrno(const char *s)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate if (*s == '\0' || isspace(*s))
81*7c478bd9Sstevel@tonic-gate return (0);
82*7c478bd9Sstevel@tonic-gate else if (isdigit(*s))
83*7c478bd9Sstevel@tonic-gate return (atoi(s));
84*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "EPERM") == 0)
85*7c478bd9Sstevel@tonic-gate return (EPERM);
86*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "ENOENT") == 0)
87*7c478bd9Sstevel@tonic-gate return (ENOENT);
88*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "ESRCH") == 0)
89*7c478bd9Sstevel@tonic-gate return (ESRCH);
90*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "ENOMEM") == 0)
91*7c478bd9Sstevel@tonic-gate return (ENOMEM);
92*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "EACCES") == 0)
93*7c478bd9Sstevel@tonic-gate return (EACCES);
94*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "EINVAL") == 0)
95*7c478bd9Sstevel@tonic-gate return (EINVAL);
96*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "ERANGE") == 0)
97*7c478bd9Sstevel@tonic-gate return (ERANGE);
98*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "ENOMSG") == 0)
99*7c478bd9Sstevel@tonic-gate return (ENOMSG);
100*7c478bd9Sstevel@tonic-gate else if (strcmp(s, "ENOTSUP") == 0)
101*7c478bd9Sstevel@tonic-gate return (ENOTSUP);
102*7c478bd9Sstevel@tonic-gate else {
103*7c478bd9Sstevel@tonic-gate err("geterrno: don't know errno \"%s\"", s);
104*7c478bd9Sstevel@tonic-gate Errcount++;
105*7c478bd9Sstevel@tonic-gate return (0);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /* call fm_dc_opendict() as part of a test */
110*7c478bd9Sstevel@tonic-gate static void
do_open(const char * dirpath,const char * dictname,char * argv[],int argc)111*7c478bd9Sstevel@tonic-gate do_open(const char *dirpath, const char *dictname, char *argv[], int argc)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate int reterrno;
114*7c478bd9Sstevel@tonic-gate int experrno;
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate if (argc != 2) {
117*7c478bd9Sstevel@tonic-gate err("argc != 2");
118*7c478bd9Sstevel@tonic-gate return;
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate experrno = geterrno(argv[1]);
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate if ((Dhp = fm_dc_opendict(FM_DC_VERSION, dirpath, dictname)) == NULL)
123*7c478bd9Sstevel@tonic-gate reterrno = errno;
124*7c478bd9Sstevel@tonic-gate else
125*7c478bd9Sstevel@tonic-gate reterrno = 0;
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate if (reterrno != experrno)
128*7c478bd9Sstevel@tonic-gate err("opendict errno %d, expected %d", reterrno, experrno);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gate /* call fm_dc_closedict() as part of a test */
132*7c478bd9Sstevel@tonic-gate static void
do_close(const char * dirpath,const char * dictname,char * argv[],int argc)133*7c478bd9Sstevel@tonic-gate do_close(const char *dirpath, const char *dictname, char *argv[], int argc)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate if (Dhp) {
136*7c478bd9Sstevel@tonic-gate fm_dc_closedict(Dhp);
137*7c478bd9Sstevel@tonic-gate Dhp = NULL;
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate /* call fm_dc_codelen() as part of a test */
142*7c478bd9Sstevel@tonic-gate static void
do_codelen(const char * dirpath,const char * dictname,char * argv[],int argc)143*7c478bd9Sstevel@tonic-gate do_codelen(const char *dirpath, const char *dictname, char *argv[], int argc)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate int retcodelen;
146*7c478bd9Sstevel@tonic-gate int expcodelen;
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate if (argc != 3) {
149*7c478bd9Sstevel@tonic-gate err("argc != 3");
150*7c478bd9Sstevel@tonic-gate return;
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate expcodelen = geterrno(argv[2]);
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate if (Dhp == NULL) {
155*7c478bd9Sstevel@tonic-gate err("codelen NULL handle");
156*7c478bd9Sstevel@tonic-gate return;
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate retcodelen = fm_dc_codelen(Dhp);
160*7c478bd9Sstevel@tonic-gate
161*7c478bd9Sstevel@tonic-gate if (retcodelen != expcodelen)
162*7c478bd9Sstevel@tonic-gate err("codelen %d, expected %d", retcodelen, expcodelen);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate /* call fm_dc_maxkey() as part of a test */
166*7c478bd9Sstevel@tonic-gate static void
do_maxkey(const char * dirpath,const char * dictname,char * argv[],int argc)167*7c478bd9Sstevel@tonic-gate do_maxkey(const char *dirpath, const char *dictname, char *argv[], int argc)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate int retmaxkey;
170*7c478bd9Sstevel@tonic-gate int expmaxkey;
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate if (argc != 3) {
173*7c478bd9Sstevel@tonic-gate err("argc != 3");
174*7c478bd9Sstevel@tonic-gate return;
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate expmaxkey = geterrno(argv[2]);
177*7c478bd9Sstevel@tonic-gate
178*7c478bd9Sstevel@tonic-gate if (Dhp == NULL) {
179*7c478bd9Sstevel@tonic-gate err("maxkey NULL handle");
180*7c478bd9Sstevel@tonic-gate return;
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate retmaxkey = fm_dc_maxkey(Dhp);
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate if (retmaxkey != expmaxkey)
186*7c478bd9Sstevel@tonic-gate err("maxkey %d, expected %d", retmaxkey, expmaxkey);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate /* call fm_dc_key2code() as part of a test */
190*7c478bd9Sstevel@tonic-gate static void
do_key2code(const char * dirpath,const char * dictname,char * argv[],int argc)191*7c478bd9Sstevel@tonic-gate do_key2code(const char *dirpath, const char *dictname, char *argv[], int argc)
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate int reterrno;
194*7c478bd9Sstevel@tonic-gate int experrno;
195*7c478bd9Sstevel@tonic-gate const char *key[MAXKEY];
196*7c478bd9Sstevel@tonic-gate char code[MAXCODE];
197*7c478bd9Sstevel@tonic-gate int nel;
198*7c478bd9Sstevel@tonic-gate char *beginp;
199*7c478bd9Sstevel@tonic-gate char *endp;
200*7c478bd9Sstevel@tonic-gate
201*7c478bd9Sstevel@tonic-gate if (argc < 3) {
202*7c478bd9Sstevel@tonic-gate err("argc < 3");
203*7c478bd9Sstevel@tonic-gate return;
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate if (argc > 4) {
206*7c478bd9Sstevel@tonic-gate err("argc > 4");
207*7c478bd9Sstevel@tonic-gate return;
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate experrno = geterrno(argv[1]);
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate /* convert key into array */
212*7c478bd9Sstevel@tonic-gate nel = 0;
213*7c478bd9Sstevel@tonic-gate beginp = argv[2];
214*7c478bd9Sstevel@tonic-gate while (nel < MAXKEY - 1) {
215*7c478bd9Sstevel@tonic-gate key[nel++] = beginp;
216*7c478bd9Sstevel@tonic-gate if ((endp = strchr(beginp, ' ')) != NULL) {
217*7c478bd9Sstevel@tonic-gate *endp++ = '\0';
218*7c478bd9Sstevel@tonic-gate beginp = endp;
219*7c478bd9Sstevel@tonic-gate } else
220*7c478bd9Sstevel@tonic-gate break;
221*7c478bd9Sstevel@tonic-gate }
222*7c478bd9Sstevel@tonic-gate key[nel] = NULL;
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate if (Dhp == NULL) {
225*7c478bd9Sstevel@tonic-gate err("key2code NULL handle");
226*7c478bd9Sstevel@tonic-gate return;
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate
229*7c478bd9Sstevel@tonic-gate if (fm_dc_key2code(Dhp, key, code, MAXCODE) < 0)
230*7c478bd9Sstevel@tonic-gate reterrno = errno;
231*7c478bd9Sstevel@tonic-gate else
232*7c478bd9Sstevel@tonic-gate reterrno = 0;
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate if (reterrno != experrno) {
235*7c478bd9Sstevel@tonic-gate err("key2code errno %d, expected %d", reterrno, experrno);
236*7c478bd9Sstevel@tonic-gate return;
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate if (reterrno == 0 && argc > 3 && strcmp(code, argv[3]))
240*7c478bd9Sstevel@tonic-gate err("code \"%s\", expected \"%s\"", code, argv[3]);
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate /* call fm_dc_code2key() as part of a test */
244*7c478bd9Sstevel@tonic-gate static void
do_code2key(const char * dirpath,const char * dictname,char * argv[],int argc)245*7c478bd9Sstevel@tonic-gate do_code2key(const char *dirpath, const char *dictname, char *argv[], int argc)
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate int reterrno;
248*7c478bd9Sstevel@tonic-gate int experrno;
249*7c478bd9Sstevel@tonic-gate char keystr[MAXLINE];
250*7c478bd9Sstevel@tonic-gate char *key[MAXKEY];
251*7c478bd9Sstevel@tonic-gate int nel;
252*7c478bd9Sstevel@tonic-gate
253*7c478bd9Sstevel@tonic-gate if (argc < 3) {
254*7c478bd9Sstevel@tonic-gate err("argc < 3");
255*7c478bd9Sstevel@tonic-gate return;
256*7c478bd9Sstevel@tonic-gate }
257*7c478bd9Sstevel@tonic-gate if (argc > 4) {
258*7c478bd9Sstevel@tonic-gate err("argc > 4");
259*7c478bd9Sstevel@tonic-gate return;
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate experrno = geterrno(argv[1]);
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate if (Dhp == NULL) {
264*7c478bd9Sstevel@tonic-gate err("code2key NULL handle");
265*7c478bd9Sstevel@tonic-gate return;
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate
268*7c478bd9Sstevel@tonic-gate if (fm_dc_code2key(Dhp, argv[2], key, fm_dc_maxkey(Dhp)) < 0)
269*7c478bd9Sstevel@tonic-gate reterrno = errno;
270*7c478bd9Sstevel@tonic-gate else
271*7c478bd9Sstevel@tonic-gate reterrno = 0;
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate if (reterrno != experrno) {
274*7c478bd9Sstevel@tonic-gate err("errno %d, expected %d", reterrno, experrno);
275*7c478bd9Sstevel@tonic-gate return;
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate if (reterrno)
279*7c478bd9Sstevel@tonic-gate return;
280*7c478bd9Sstevel@tonic-gate
281*7c478bd9Sstevel@tonic-gate if (argc > 3) {
282*7c478bd9Sstevel@tonic-gate /* convert key into string */
283*7c478bd9Sstevel@tonic-gate keystr[0] = '\0';
284*7c478bd9Sstevel@tonic-gate for (nel = 0; key[nel]; nel++) {
285*7c478bd9Sstevel@tonic-gate if (nel)
286*7c478bd9Sstevel@tonic-gate (void) strcat(keystr, " ");
287*7c478bd9Sstevel@tonic-gate (void) strcat(keystr, key[nel]);
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate
290*7c478bd9Sstevel@tonic-gate if (strcmp(keystr, argv[3]))
291*7c478bd9Sstevel@tonic-gate err("key \"%s\", expected \"%s\"", keystr, argv[3]);
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate for (nel = 0; key[nel]; nel++)
294*7c478bd9Sstevel@tonic-gate free(key[nel]);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate /* call fm_dc_getprop() as part of a test */
298*7c478bd9Sstevel@tonic-gate static void
do_getprop(const char * dirpath,const char * dictname,char * argv[],int argc)299*7c478bd9Sstevel@tonic-gate do_getprop(const char *dirpath, const char *dictname, char *argv[], int argc)
300*7c478bd9Sstevel@tonic-gate {
301*7c478bd9Sstevel@tonic-gate int reterrno;
302*7c478bd9Sstevel@tonic-gate int experrno;
303*7c478bd9Sstevel@tonic-gate const char *val;
304*7c478bd9Sstevel@tonic-gate
305*7c478bd9Sstevel@tonic-gate if (argc != 4) {
306*7c478bd9Sstevel@tonic-gate err("argc != 4");
307*7c478bd9Sstevel@tonic-gate return;
308*7c478bd9Sstevel@tonic-gate }
309*7c478bd9Sstevel@tonic-gate experrno = geterrno(argv[1]);
310*7c478bd9Sstevel@tonic-gate
311*7c478bd9Sstevel@tonic-gate if (Dhp == NULL) {
312*7c478bd9Sstevel@tonic-gate err("getprop NULL handle");
313*7c478bd9Sstevel@tonic-gate return;
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate
316*7c478bd9Sstevel@tonic-gate if ((val = fm_dc_getprop(Dhp, argv[2])) == NULL)
317*7c478bd9Sstevel@tonic-gate reterrno = errno;
318*7c478bd9Sstevel@tonic-gate else
319*7c478bd9Sstevel@tonic-gate reterrno = 0;
320*7c478bd9Sstevel@tonic-gate
321*7c478bd9Sstevel@tonic-gate if (reterrno != experrno) {
322*7c478bd9Sstevel@tonic-gate err("getprop errno %d, expected %d", reterrno, experrno);
323*7c478bd9Sstevel@tonic-gate return;
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate
326*7c478bd9Sstevel@tonic-gate if (reterrno == 0 && strcmp(val, argv[3]))
327*7c478bd9Sstevel@tonic-gate err("val \"%s\", expected \"%s\"", val, argv[3]);
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate
330*7c478bd9Sstevel@tonic-gate /* scan a dictionary, looking for test directives embedded in the comments */
331*7c478bd9Sstevel@tonic-gate static void
testdict(const char * dirpath,const char * dictname)332*7c478bd9Sstevel@tonic-gate testdict(const char *dirpath, const char *dictname)
333*7c478bd9Sstevel@tonic-gate {
334*7c478bd9Sstevel@tonic-gate char linebuf[MAXLINE];
335*7c478bd9Sstevel@tonic-gate char fname[MAXLINE];
336*7c478bd9Sstevel@tonic-gate FILE *fp;
337*7c478bd9Sstevel@tonic-gate
338*7c478bd9Sstevel@tonic-gate (void) snprintf(fname, MAXLINE, "%s/%s.dict", dirpath, dictname);
339*7c478bd9Sstevel@tonic-gate
340*7c478bd9Sstevel@tonic-gate if ((fp = fopen(fname, "r")) == NULL) {
341*7c478bd9Sstevel@tonic-gate perror(fname);
342*7c478bd9Sstevel@tonic-gate Errcount++;
343*7c478bd9Sstevel@tonic-gate return;
344*7c478bd9Sstevel@tonic-gate }
345*7c478bd9Sstevel@tonic-gate
346*7c478bd9Sstevel@tonic-gate Line = 0;
347*7c478bd9Sstevel@tonic-gate Dict = fname;
348*7c478bd9Sstevel@tonic-gate
349*7c478bd9Sstevel@tonic-gate while (fgets(linebuf, MAXLINE, fp) != NULL) {
350*7c478bd9Sstevel@tonic-gate char *argv[MAXARG];
351*7c478bd9Sstevel@tonic-gate int argc;
352*7c478bd9Sstevel@tonic-gate char *beginp;
353*7c478bd9Sstevel@tonic-gate char *endp;
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate Line++;
356*7c478bd9Sstevel@tonic-gate if (strncmp(linebuf, "#TEST:", 6))
357*7c478bd9Sstevel@tonic-gate continue;
358*7c478bd9Sstevel@tonic-gate
359*7c478bd9Sstevel@tonic-gate if ((endp = strchr(linebuf, '\n')) != NULL)
360*7c478bd9Sstevel@tonic-gate *endp = '\0';
361*7c478bd9Sstevel@tonic-gate argc = 0;
362*7c478bd9Sstevel@tonic-gate beginp = &linebuf[6];
363*7c478bd9Sstevel@tonic-gate while (argc < MAXARG - 1) {
364*7c478bd9Sstevel@tonic-gate argv[argc++] = beginp;
365*7c478bd9Sstevel@tonic-gate if ((endp = strchr(beginp, ':')) != NULL) {
366*7c478bd9Sstevel@tonic-gate *endp++ = '\0';
367*7c478bd9Sstevel@tonic-gate beginp = endp;
368*7c478bd9Sstevel@tonic-gate } else
369*7c478bd9Sstevel@tonic-gate break;
370*7c478bd9Sstevel@tonic-gate }
371*7c478bd9Sstevel@tonic-gate argv[argc] = NULL;
372*7c478bd9Sstevel@tonic-gate
373*7c478bd9Sstevel@tonic-gate if (strcmp(argv[0], "open") == 0)
374*7c478bd9Sstevel@tonic-gate do_open(dirpath, dictname, argv, argc);
375*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[0], "close") == 0)
376*7c478bd9Sstevel@tonic-gate do_close(dirpath, dictname, argv, argc);
377*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[0], "codelen") == 0)
378*7c478bd9Sstevel@tonic-gate do_codelen(dirpath, dictname, argv, argc);
379*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[0], "maxkey") == 0)
380*7c478bd9Sstevel@tonic-gate do_maxkey(dirpath, dictname, argv, argc);
381*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[0], "key2code") == 0)
382*7c478bd9Sstevel@tonic-gate do_key2code(dirpath, dictname, argv, argc);
383*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[0], "code2key") == 0)
384*7c478bd9Sstevel@tonic-gate do_code2key(dirpath, dictname, argv, argc);
385*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[0], "getprop") == 0)
386*7c478bd9Sstevel@tonic-gate do_getprop(dirpath, dictname, argv, argc);
387*7c478bd9Sstevel@tonic-gate else {
388*7c478bd9Sstevel@tonic-gate err("unknown TEST command: \"%s\"", argv[0]);
389*7c478bd9Sstevel@tonic-gate Errcount++;
390*7c478bd9Sstevel@tonic-gate }
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate
393*7c478bd9Sstevel@tonic-gate (void) fclose(fp);
394*7c478bd9Sstevel@tonic-gate
395*7c478bd9Sstevel@tonic-gate if (Dhp) {
396*7c478bd9Sstevel@tonic-gate fm_dc_closedict(Dhp);
397*7c478bd9Sstevel@tonic-gate Dhp = NULL;
398*7c478bd9Sstevel@tonic-gate }
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate
401*7c478bd9Sstevel@tonic-gate /* scan a directory, looking for dictionaries to test against */
402*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])403*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
404*7c478bd9Sstevel@tonic-gate {
405*7c478bd9Sstevel@tonic-gate DIR *dirp;
406*7c478bd9Sstevel@tonic-gate struct dirent *dp;
407*7c478bd9Sstevel@tonic-gate
408*7c478bd9Sstevel@tonic-gate if ((Myname = strrchr(argv[0], '/')) == NULL)
409*7c478bd9Sstevel@tonic-gate Myname = argv[0];
410*7c478bd9Sstevel@tonic-gate else
411*7c478bd9Sstevel@tonic-gate Myname++;
412*7c478bd9Sstevel@tonic-gate
413*7c478bd9Sstevel@tonic-gate if (argc != 2) {
414*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s test-directory\n", argv[0]);
415*7c478bd9Sstevel@tonic-gate exit(1);
416*7c478bd9Sstevel@tonic-gate }
417*7c478bd9Sstevel@tonic-gate
418*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(argv[1])) == NULL) {
419*7c478bd9Sstevel@tonic-gate perror(argv[1]);
420*7c478bd9Sstevel@tonic-gate exit(1);
421*7c478bd9Sstevel@tonic-gate }
422*7c478bd9Sstevel@tonic-gate
423*7c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
424*7c478bd9Sstevel@tonic-gate char *ptr;
425*7c478bd9Sstevel@tonic-gate
426*7c478bd9Sstevel@tonic-gate if (dp->d_name[0] == '.')
427*7c478bd9Sstevel@tonic-gate continue;
428*7c478bd9Sstevel@tonic-gate
429*7c478bd9Sstevel@tonic-gate if ((ptr = strrchr(dp->d_name, '.')) == NULL ||
430*7c478bd9Sstevel@tonic-gate strcmp(ptr, ".dict"))
431*7c478bd9Sstevel@tonic-gate continue;
432*7c478bd9Sstevel@tonic-gate
433*7c478bd9Sstevel@tonic-gate *ptr = '\0'; /* remove the extension */
434*7c478bd9Sstevel@tonic-gate testdict(argv[1], dp->d_name);
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate (void) closedir(dirp);
437*7c478bd9Sstevel@tonic-gate
438*7c478bd9Sstevel@tonic-gate if (Errcount == 0)
439*7c478bd9Sstevel@tonic-gate (void) printf("%s: All tests passed.\n", Myname);
440*7c478bd9Sstevel@tonic-gate
441*7c478bd9Sstevel@tonic-gate return (Errcount);
442*7c478bd9Sstevel@tonic-gate }
443