gctl_test_helper.c revision 319014
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: stable/10/tools/regression/geom_gpt/gctl_test_helper.c 319014 2017-05-27 23:31:59Z ngie $"); 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 41struct retval { 42 struct retval *retval; 43 const char *param; 44 char *value; 45}; 46 47static struct retval *retval; 48static int verbose; 49 50static void 51usage(void) 52{ 53 fprintf(stdout, "usage: %s [-v] param[:len][=value] ...\n", 54 getprogname()); 55 exit(1); 56} 57 58static int 59parse(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 = malloc(*len); 91 if (*value == NULL) 92 return (ENOMEM); 93 memset(*value, 0, *len); 94 if (equal != NULL) { 95 if (strlen(equal) >= PATH_MAX) 96 return (ENOMEM); 97 strcpy(*value, equal); 98 } 99 } else { 100 /* This parameter is RO. */ 101 *len = -1; 102 if (*equal == '\0') 103 return (EINVAL); 104 *value = equal; 105 } 106 107 return (0); 108} 109 110int 111main(int argc, char *argv[]) 112{ 113 struct retval *rv; 114 struct gctl_req *req; 115 char *param, *value; 116 const char *s; 117 int c, len, parse_retval; 118 119 req = gctl_get_handle(); 120 assert(req != NULL); 121 gctl_ro_param(req, "class", -1, "PART"); 122 123 while ((c = getopt(argc, argv, "v")) != -1) { 124 switch (c) { 125 case 'v': 126 verbose = 1; 127 break; 128 case '?': 129 default: 130 usage(); 131 /* NOTREACHED */ 132 break; 133 } 134 } 135 136 for (; optind < argc; optind++) { 137 parse_retval = parse(argv[optind], ¶m, &value, &len); 138 if (parse_retval == 0) { 139 if (len > 0) { 140 rv = malloc(sizeof(struct retval)); 141 assert(rv != NULL); 142 rv->param = param; 143 rv->value = value; 144 rv->retval = retval; 145 retval = rv; 146 gctl_rw_param(req, param, len, value); 147 } else 148 gctl_ro_param(req, param, -1, value); 149 } else 150 warnc(parse_retval, "failed to parse argument (%s)", 151 argv[optind]); 152 } 153 154 if (verbose) 155 gctl_dump(req, stdout); 156 157 s = gctl_issue(req); 158 if (s == NULL) { 159 printf("PASS"); 160 while (retval != NULL) { 161 rv = retval->retval; 162 printf(" %s=%s", retval->param, retval->value); 163 free(retval->value); 164 free(retval); 165 retval = rv; 166 } 167 printf("\n"); 168 } else 169 printf("FAIL %s\n", s); 170 171 gctl_free(req); 172 return (0); 173} 174