]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - tools/regression/geom_gpt/gctl_test_helper.c
MFC r319800,r319806:
[FreeBSD/stable/10.git] / tools / regression / geom_gpt / gctl_test_helper.c
1 /*-
2  * Copyright (c) 2005, 2006 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <assert.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <libgeom.h>
40
41 struct retval {
42         struct retval *retval;
43         const char *param;
44         char *value;
45 };
46
47 static struct retval *retval;
48 static int verbose;
49
50 static void
51 usage(void)
52 {
53         fprintf(stdout, "usage: %s [-v] param[:len][=value] ...\n",
54             getprogname());
55         exit(1);
56 }
57
58 static int
59 parse(char *arg, char **param, char **value, int *len)
60 {
61         char *e, *colon, *equal;
62
63         if (*arg == '\0')
64                 return (EINVAL);
65
66         colon = strchr(arg, ':');
67         equal = strchr(arg, '=');
68         if (colon == NULL && equal == NULL)
69                 return (EINVAL);
70         if (colon == arg || equal == arg)
71                 return (EINVAL);
72         if (colon != NULL && equal != NULL && equal < colon)
73                 return (EINVAL);
74
75         if (colon != NULL)
76                 *colon++ = '\0';
77         if (equal != NULL)
78                 *equal++ = '\0';
79
80         *param = arg;
81         if (colon != NULL) {
82                 /* Length specification. This parameter is RW. */
83                 if (*colon == '\0')
84                         return (EINVAL);
85                 *len = strtol(colon, &e, 0);
86                 if (*e != '\0')
87                         return (EINVAL);
88                 if (*len <= 0 || *len > PATH_MAX)
89                         return (EINVAL);
90                 *value = calloc(*len, sizeof(char));
91                 if (*value == NULL)
92                         return (ENOMEM);
93                 if (equal != NULL) {
94                         if (strlen(equal) >= PATH_MAX)
95                                 return (ENOMEM);
96                         strcpy(*value, equal);
97                 }
98         } else {
99                 /* This parameter is RO. */
100                 *len = -1;
101                 if (*equal == '\0')
102                         return (EINVAL);
103                 *value = equal;
104         }
105
106         return (0);
107 }
108
109 int
110 main(int argc, char *argv[])
111 {
112         struct retval *rv;
113         struct gctl_req *req;
114         char *param, *value;
115         const char *s;
116         int c, len, parse_retval;
117
118         req = gctl_get_handle();
119         assert(req != NULL);
120
121         while ((c = getopt(argc, argv, "v")) != -1) {
122                 switch (c) {
123                 case 'v':
124                         verbose = 1;
125                         break;
126                 case '?':
127                 default:
128                         usage();
129                         /* NOTREACHED */
130                         break;
131                 }
132         }
133
134         for (; optind < argc; optind++) {
135                 parse_retval = parse(argv[optind], &param, &value, &len);
136                 if (parse_retval == 0) {
137                         if (len > 0) {
138                                 rv = malloc(sizeof(struct retval));
139                                 assert(rv != NULL);
140                                 rv->param = param;
141                                 rv->value = value;
142                                 rv->retval = retval;
143                                 retval = rv;
144                                 gctl_rw_param(req, param, len, value);
145                         } else
146                                 gctl_ro_param(req, param, -1, value);
147                 } else
148                         warnc(parse_retval, "failed to parse argument (%s)",
149                             argv[optind]);
150         }
151
152         if (verbose)
153                 gctl_dump(req, stdout);
154
155         s = gctl_issue(req);
156         if (s == NULL) {
157                 printf("PASS");
158                 while (retval != NULL) {
159                         rv = retval->retval;
160                         printf(" %s=%s", retval->param, retval->value);
161                         free(retval->value);
162                         free(retval);
163                         retval = rv;
164                 }
165                 printf("\n");
166         } else
167                 printf("FAIL %s\n", s);
168
169         gctl_free(req);
170         return (0);
171 }