1150305Smarcel/*- 2157621Smarcel * Copyright (c) 2005, 2006 Marcel Moolenaar 3150305Smarcel * All rights reserved. 4150305Smarcel * 5150305Smarcel * Redistribution and use in source and binary forms, with or without 6150305Smarcel * modification, are permitted provided that the following conditions 7150305Smarcel * are met: 8150305Smarcel * 9150305Smarcel * 1. Redistributions of source code must retain the above copyright 10150305Smarcel * notice, this list of conditions and the following disclaimer. 11150305Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12150305Smarcel * notice, this list of conditions and the following disclaimer in the 13150305Smarcel * documentation and/or other materials provided with the distribution. 14150305Smarcel * 15150305Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16150305Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17150305Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18150305Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19150305Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20150305Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21150305Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22150305Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23150305Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24150305Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25150305Smarcel */ 26150305Smarcel 27150305Smarcel#include <sys/cdefs.h> 28150305Smarcel__FBSDID("$FreeBSD: stable/11/tools/regression/geom_gpt/gctl_test_helper.c 321136 2017-07-18 17:16:55Z ngie $"); 29150305Smarcel 30157621Smarcel#include <sys/param.h> 31319013Sngie#include <assert.h> 32319013Sngie#include <err.h> 33150305Smarcel#include <errno.h> 34150305Smarcel#include <limits.h> 35150305Smarcel#include <stdio.h> 36150305Smarcel#include <stdlib.h> 37150305Smarcel#include <string.h> 38150305Smarcel#include <unistd.h> 39317290Sngie#include <libgeom.h> 40150305Smarcel 41157621Smarcelstruct retval { 42157621Smarcel struct retval *retval; 43157621Smarcel const char *param; 44157621Smarcel char *value; 45157621Smarcel}; 46157621Smarcel 47319013Sngiestatic struct retval *retval; 48319013Sngiestatic int verbose; 49150305Smarcel 50150305Smarcelstatic void 51319013Sngieusage(void) 52150305Smarcel{ 53157621Smarcel fprintf(stdout, "usage: %s [-v] param[:len][=value] ...\n", 54157621Smarcel getprogname()); 55150305Smarcel exit(1); 56150305Smarcel} 57150305Smarcel 58150305Smarcelstatic int 59157621Smarcelparse(char *arg, char **param, char **value, int *len) 60150305Smarcel{ 61157621Smarcel char *e, *colon, *equal; 62150305Smarcel 63157621Smarcel if (*arg == '\0') 64157621Smarcel return (EINVAL); 65157621Smarcel 66157621Smarcel colon = strchr(arg, ':'); 67157621Smarcel equal = strchr(arg, '='); 68157621Smarcel if (colon == NULL && equal == NULL) 69157621Smarcel return (EINVAL); 70157621Smarcel if (colon == arg || equal == arg) 71157621Smarcel return (EINVAL); 72157621Smarcel if (colon != NULL && equal != NULL && equal < colon) 73157621Smarcel return (EINVAL); 74157621Smarcel 75157621Smarcel if (colon != NULL) 76157621Smarcel *colon++ = '\0'; 77157621Smarcel if (equal != NULL) 78157621Smarcel *equal++ = '\0'; 79157621Smarcel 80150305Smarcel *param = arg; 81157621Smarcel if (colon != NULL) { 82157621Smarcel /* Length specification. This parameter is RW. */ 83157621Smarcel if (*colon == '\0') 84157621Smarcel return (EINVAL); 85157621Smarcel *len = strtol(colon, &e, 0); 86157621Smarcel if (*e != '\0') 87157621Smarcel return (EINVAL); 88157621Smarcel if (*len <= 0 || *len > PATH_MAX) 89157621Smarcel return (EINVAL); 90319169Sngie *value = calloc(*len, sizeof(char)); 91157621Smarcel if (*value == NULL) 92157621Smarcel return (ENOMEM); 93157621Smarcel if (equal != NULL) { 94157621Smarcel if (strlen(equal) >= PATH_MAX) 95157621Smarcel return (ENOMEM); 96157621Smarcel strcpy(*value, equal); 97157621Smarcel } 98157621Smarcel } else { 99157621Smarcel /* This parameter is RO. */ 100157621Smarcel *len = -1; 101157621Smarcel if (*equal == '\0') 102157621Smarcel return (EINVAL); 103157621Smarcel *value = equal; 104157621Smarcel } 105157621Smarcel 106150305Smarcel return (0); 107150305Smarcel} 108150305Smarcel 109319013Sngieint 110319013Sngiemain(int argc, char *argv[]) 111150305Smarcel{ 112157621Smarcel struct retval *rv; 113150305Smarcel struct gctl_req *req; 114150308Smarcel char *param, *value; 115150305Smarcel const char *s; 116319013Sngie int c, len, parse_retval; 117150305Smarcel 118150305Smarcel req = gctl_get_handle(); 119319013Sngie assert(req != NULL); 120150305Smarcel 121150308Smarcel while ((c = getopt(argc, argv, "v")) != -1) { 122150305Smarcel switch (c) { 123150305Smarcel case 'v': 124150305Smarcel verbose = 1; 125150305Smarcel break; 126150305Smarcel case '?': 127150305Smarcel default: 128150305Smarcel usage(); 129150305Smarcel /* NOTREACHED */ 130150305Smarcel break; 131150305Smarcel } 132150305Smarcel } 133150305Smarcel 134319013Sngie for (; optind < argc; optind++) { 135319013Sngie parse_retval = parse(argv[optind], ¶m, &value, &len); 136319013Sngie if (parse_retval == 0) { 137157621Smarcel if (len > 0) { 138157621Smarcel rv = malloc(sizeof(struct retval)); 139319013Sngie assert(rv != NULL); 140157621Smarcel rv->param = param; 141157621Smarcel rv->value = value; 142157621Smarcel rv->retval = retval; 143157621Smarcel retval = rv; 144157621Smarcel gctl_rw_param(req, param, len, value); 145157621Smarcel } else 146157621Smarcel gctl_ro_param(req, param, -1, value); 147319013Sngie } else 148319013Sngie warnc(parse_retval, "failed to parse argument (%s)", 149319013Sngie argv[optind]); 150150308Smarcel } 151150308Smarcel 152150305Smarcel if (verbose) 153150305Smarcel gctl_dump(req, stdout); 154150305Smarcel 155150305Smarcel s = gctl_issue(req); 156157621Smarcel if (s == NULL) { 157157621Smarcel printf("PASS"); 158157621Smarcel while (retval != NULL) { 159157621Smarcel rv = retval->retval; 160157621Smarcel printf(" %s=%s", retval->param, retval->value); 161157621Smarcel free(retval->value); 162157621Smarcel free(retval); 163157621Smarcel retval = rv; 164157621Smarcel } 165157621Smarcel printf("\n"); 166157621Smarcel } else 167150305Smarcel printf("FAIL %s\n", s); 168157621Smarcel 169150305Smarcel gctl_free(req); 170150305Smarcel return (0); 171150305Smarcel} 172