util.c revision 237740
12061Sjkh/* 250479Speter * Written By Julian ELischer 32061Sjkh * Copyright julian Elischer 1993. 438666Sjb * Permission is granted to use or redistribute this file in any way as long 532427Sjb * as this notice remains. Julian Elischer does not guarantee that this file 6111131Sru * is totally correct for any given task and users of this file must 7111131Sru * accept responsibility for any damage that occurs from the application of this 8217733Sbz * file. 9217733Sbz * 1038666Sjb * (julian@tfs.com julian@dialix.oz.au) 1138666Sjb * 1238666Sjb * User SCSI hooks added by Peter Dufault: 13159363Strhodes * 1464049Salex * Copyright (c) 1994 HD Associates 1564049Salex * (contact: dufault@hda.com) 16116679Ssimokawa * All rights reserved. 1766071Smarkm * 18116679Ssimokawa * Redistribution and use in source and binary forms, with or without 1973504Sobrien * modification, are permitted provided that the following conditions 20204661Simp * are met: 21232907Sjmallett * 1. Redistributions of source code must retain the above copyright 22158962Snetchild * notice, this list of conditions and the following disclaimer. 23223148Sru * 2. Redistributions in binary form must reproduce the above copyright 24169597Sdes * notice, this list of conditions and the following disclaimer in the 25169597Sdes * documentation and/or other materials provided with the distribution. 26169597Sdes * 3. The name of HD Associates 27169597Sdes * may not be used to endorse or promote products derived from this software 28231821Spluknet * without specific prior written permission. 29169597Sdes * 30169597Sdes * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND 31169597Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32217815Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33217815Sbz * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES BE LIABLE 34218524Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3532427Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3638666Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37108451Sschweikh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3838666Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3938666Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 4038666Sjb * SUCH DAMAGE. 4138666Sjb */ 4217308Speter/* 43217273Simp * Taken from the original scsi(8) program. 44217294Simp * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $"; 4519175Sbde */ 4696205Sjwd#include <sys/cdefs.h> 47217297Simp__FBSDID("$FreeBSD: stable/9/sbin/camcontrol/util.c 237740 2012-06-29 03:37:23Z scottl $"); 48217297Simp 4938042Sbde#include <sys/stdint.h> 5096205Sjwd#include <sys/types.h> 5196205Sjwd 5238042Sbde#include <stdlib.h> 5396205Sjwd#include <stdio.h> 54159363Strhodes#include <string.h> 55159363Strhodes 5617308Speter#include <camlib.h> 5796205Sjwd#include "camcontrol.h" 5896205Sjwd 5917308Speterint verbose; 60148330Snetchild 61148330Snetchild/* iget: Integer argument callback 62148330Snetchild */ 63148330Snetchildint 64159831Sobrieniget(void *hook, char *name) 65148330Snetchild{ 66148330Snetchild struct get_hook *h = (struct get_hook *)hook; 67148330Snetchild int arg; 68148330Snetchild 69220512Sdougb if (h->got >= h->argc) 70148330Snetchild { 71148330Snetchild fprintf(stderr, "Expecting an integer argument.\n"); 7296205Sjwd usage(0); 7396205Sjwd exit(1); 7496205Sjwd } 75162147Sru arg = strtol(h->argv[h->got], 0, 0); 76162147Sru h->got++; 7798723Sdillon 7898723Sdillon if (verbose && name && *name) 7998723Sdillon printf("%s: %d\n", name, arg); 8038666Sjb 8138666Sjb return arg; 8217308Speter} 83123311Speter 84123311Speter/* cget: char * argument callback 85123311Speter */ 86123311Speterchar * 87175833Sjhbcget(void *hook, char *name) 88175833Sjhb{ 89169597Sdes struct get_hook *h = (struct get_hook *)hook; 90169597Sdes char *arg; 91169597Sdes 92169597Sdes if (h->got >= h->argc) 93219177Snwhitehorn { 94219177Snwhitehorn fprintf(stderr, "Expecting a character pointer argument.\n"); 95238051Sobrien usage(0); 96219177Snwhitehorn exit(1); 97219177Snwhitehorn } 98158962Snetchild arg = h->argv[h->got]; 99156840Sru h->got++; 100123311Speter 101137288Speter if (verbose && name) 102209128Sraj printf("cget: %s: %s", name, arg); 103209128Sraj 104156740Sru return arg; 1052061Sjkh} 10697769Sru 10797252Sru/* arg_put: "put argument" callback 108119579Sru */ 10997252Sruvoid 11095730Sruarg_put(void *hook __unused, int letter, void *arg, int count, char *name) 11195793Sru{ 112111617Sru if (verbose && name && *name) 11395730Sru printf("%s: ", name); 114116679Ssimokawa 11595730Sru switch(letter) 116116679Ssimokawa { 11795730Sru case 'i': 118110035Sru case 'b': 119107516Sru printf("%jd ", (intmax_t)(intptr_t)arg); 120138921Sru break; 121156145Syar 122138921Sru case 'c': 123133942Sru case 'z': 124133942Sru { 125156145Syar char *p; 126133942Sru 127110035Sru p = malloc(count + 1); 128117234Sru if (p == NULL) { 129110035Sru fprintf(stderr, "can't malloc memory for p\n"); 130117229Sru exit(1); 131218206Simp } 13254324Smarcel 133218130Simp bzero(p, count +1); 134218130Simp strncpy(p, (char *)arg, count); 135233644Sjmallett if (letter == 'z') 136218130Simp { 137218130Simp int i; 138239272Sgonzo for (i = count - 1; i >= 0; i--) 139218130Simp if (p[i] == ' ') 140233644Sjmallett p[i] = 0; 141218130Simp else 142233644Sjmallett break; 143233644Sjmallett } 144233644Sjmallett printf("%s ", p); 145218130Simp 146233644Sjmallett free(p); 147233644Sjmallett } 148218130Simp 149218130Simp break; 150218130Simp 151218130Simp default: 152218130Simp printf("Unknown format letter: '%c'\n", letter); 153218130Simp } 154218130Simp if (verbose) 155218130Simp putchar('\n'); 156218130Simp} 157218130Simp 158218130Simp/* 159218130Simp * Get confirmation from user 160218130Simp * Return values: 161218130Simp * 1: confirmed 162218130Simp * 0: unconfirmed 163218130Simp */ 16417308Speterint 165119519Smarcelget_confirmation(void) 166119519Smarcel{ 167119519Smarcel char str[1024]; 168119519Smarcel int response = -1; 169119519Smarcel 170119519Smarcel do { 171119579Sru fprintf(stdout, "Are you SURE you want to do this? (yes/no) "); 172119519Smarcel if (fgets(str, sizeof(str), stdin) != NULL) { 173119519Smarcel if (strncasecmp(str, "yes", 3) == 0) 174119519Smarcel response = 1; 175119519Smarcel else if (strncasecmp(str, "no", 2) == 0) 176119519Smarcel response = 0; 177126031Sgad else 178126024Sgad fprintf(stdout, 179126024Sgad "Please answer \"yes\" or \"no\"\n"); 180126024Sgad } else 181126024Sgad response = 0; 182126024Sgad } while (response == -1); 183126024Sgad return (response); 184126024Sgad} 185227771Sgjb