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