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 #include <sys/param.h> 29 #include <assert.h> 30 #include <err.h> 31 #include <errno.h> 32 #include <limits.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <libgeom.h> 38 39 struct retval { 40 struct retval *retval; 41 const char *param; 42 char *value; 43 }; 44 45 static struct retval *retval; 46 static int verbose; 47 48 static void 49 usage(void) 50 { 51 fprintf(stdout, "usage: %s [-v] param[:len][=value] ...\n", 52 getprogname()); 53 exit(1); 54 } 55 56 static int 57 parse(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 = calloc(*len, sizeof(char)); 89 if (*value == NULL) 90 return (ENOMEM); 91 if (equal != NULL) { 92 if (strlen(equal) >= PATH_MAX) 93 return (ENOMEM); 94 strcpy(*value, equal); 95 } 96 } else { 97 /* This parameter is RO. */ 98 *len = -1; 99 if (*equal == '\0') 100 return (EINVAL); 101 *value = equal; 102 } 103 104 return (0); 105 } 106 107 int 108 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, parse_retval; 115 116 req = gctl_get_handle(); 117 assert(req != NULL); 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 for (; optind < argc; optind++) { 133 parse_retval = parse(argv[optind], ¶m, &value, &len); 134 if (parse_retval == 0) { 135 if (len > 0) { 136 rv = malloc(sizeof(struct retval)); 137 assert(rv != NULL); 138 rv->param = param; 139 rv->value = value; 140 rv->retval = retval; 141 retval = rv; 142 gctl_rw_param(req, param, len, value); 143 } else 144 gctl_ro_param(req, param, -1, value); 145 } else 146 warnc(parse_retval, "failed to parse argument (%s)", 147 argv[optind]); 148 } 149 150 if (verbose) 151 gctl_dump(req, stdout); 152 153 s = gctl_issue(req); 154 if (s == NULL) { 155 printf("PASS"); 156 while (retval != NULL) { 157 rv = retval->retval; 158 printf(" %s=%s", retval->param, retval->value); 159 free(retval->value); 160 free(retval); 161 retval = rv; 162 } 163 printf("\n"); 164 } else 165 printf("FAIL %s\n", s); 166 167 gctl_free(req); 168 return (0); 169 } 170