gctl_test_helper.c revision 319170
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 319170 2017-05-30 02:56:37Z 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 = 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 109int 110main(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 gctl_ro_param(req, "class", -1, "PART"); 121 122 while ((c = getopt(argc, argv, "v")) != -1) { 123 switch (c) { 124 case 'v': 125 verbose = 1; 126 break; 127 case '?': 128 default: 129 usage(); 130 /* NOTREACHED */ 131 break; 132 } 133 } 134 135 for (; optind < argc; optind++) { 136 parse_retval = parse(argv[optind], ¶m, &value, &len); 137 if (parse_retval == 0) { 138 if (len > 0) { 139 rv = malloc(sizeof(struct retval)); 140 assert(rv != NULL); 141 rv->param = param; 142 rv->value = value; 143 rv->retval = retval; 144 retval = rv; 145 gctl_rw_param(req, param, len, value); 146 } else 147 gctl_ro_param(req, param, -1, value); 148 } else 149 warnc(parse_retval, "failed to parse argument (%s)", 150 argv[optind]); 151 } 152 153 if (verbose) 154 gctl_dump(req, stdout); 155 156 s = gctl_issue(req); 157 if (s == NULL) { 158 printf("PASS"); 159 while (retval != NULL) { 160 rv = retval->retval; 161 printf(" %s=%s", retval->param, retval->value); 162 free(retval->value); 163 free(retval); 164 retval = rv; 165 } 166 printf("\n"); 167 } else 168 printf("FAIL %s\n", s); 169 170 gctl_free(req); 171 return (0); 172} 173