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 <errno.h>
32#include <libgeom.h>
33#include <limits.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39struct retval {
40	struct retval *retval;
41	const char *param;
42	char *value;
43};
44
45struct retval *retval;
46int verbose;
47
48static void
49usage()
50{
51	fprintf(stdout, "usage: %s [-v] param[:len][=value] ...\n",
52	    getprogname());
53	exit(1);
54}
55
56static int
57parse(char *arg, char **param, char **value, int *len)
58{
59	char *e, *colon, *equal;
60
61	if (*arg == '\0')
62		return (EINVAL);
63
64	colon = strchr(arg, ':');
65	equal = strchr(arg, '=');
66	if (colon == NULL && equal == NULL)
67		return (EINVAL);
68	if (colon == arg || equal == arg)
69		return (EINVAL);
70	if (colon != NULL && equal != NULL && equal < colon)
71		return (EINVAL);
72
73	if (colon != NULL)
74		*colon++ = '\0';
75	if (equal != NULL)
76		*equal++ = '\0';
77
78	*param = arg;
79	if (colon != NULL) {
80		/* Length specification. This parameter is RW. */
81		if (*colon == '\0')
82			return (EINVAL);
83		*len = strtol(colon, &e, 0);
84		if (*e != '\0')
85			return (EINVAL);
86		if (*len <= 0 || *len > PATH_MAX)
87			return (EINVAL);
88		*value = malloc(*len);
89		if (*value == NULL)
90			return (ENOMEM);
91		memset(*value, 0, *len);
92		if (equal != NULL) {
93			if (strlen(equal) >= PATH_MAX)
94				return (ENOMEM);
95			strcpy(*value, equal);
96		}
97	} else {
98		/* This parameter is RO. */
99		*len = -1;
100		if (*equal == '\0')
101			return (EINVAL);
102		*value = equal;
103	}
104
105	return (0);
106}
107
108int main(int argc, char *argv[])
109{
110	struct retval *rv;
111	struct gctl_req *req;
112	char *param, *value;
113	const char *s;
114	int c, len;
115
116	req = gctl_get_handle();
117	gctl_ro_param(req, "class", -1, "GPT");
118
119	while ((c = getopt(argc, argv, "v")) != -1) {
120		switch (c) {
121		case 'v':
122			verbose = 1;
123			break;
124		case '?':
125		default:
126			usage();
127			/* NOTREACHED */
128			break;
129		}
130	}
131
132	while (optind < argc) {
133		if (!parse(argv[optind++], &param, &value, &len)) {
134			if (len > 0) {
135				rv = malloc(sizeof(struct retval));
136				rv->param = param;
137				rv->value = value;
138				rv->retval = retval;
139				retval = rv;
140				gctl_rw_param(req, param, len, value);
141			} else
142				gctl_ro_param(req, param, -1, value);
143		}
144	}
145
146	if (verbose)
147		gctl_dump(req, stdout);
148
149	s = gctl_issue(req);
150	if (s == NULL) {
151		printf("PASS");
152		while (retval != NULL) {
153			rv = retval->retval;
154			printf(" %s=%s", retval->param, retval->value);
155			free(retval->value);
156			free(retval);
157			retval = rv;
158		}
159		printf("\n");
160	} else
161		printf("FAIL %s\n", s);
162
163	gctl_free(req);
164	return (0);
165}
166