/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Written By Julian ELischer4* Copyright julian Elischer 1993.5* Permission is granted to use or redistribute this file in any way as long6* as this notice remains. Julian Elischer does not guarantee that this file7* is totally correct for any given task and users of this file must8* accept responsibility for any damage that occurs from the application of this9* file.10*11* ([email protected] [email protected])12*13* User SCSI hooks added by Peter Dufault:14*15* Copyright (c) 1994 HD Associates16* (contact: [email protected])17* All rights reserved.18*19* Redistribution and use in source and binary forms, with or without20* modification, are permitted provided that the following conditions21* are met:22* 1. Redistributions of source code must retain the above copyright23* notice, this list of conditions and the following disclaimer.24* 2. Redistributions in binary form must reproduce the above copyright25* notice, this list of conditions and the following disclaimer in the26* documentation and/or other materials provided with the distribution.27* 3. The name of HD Associates28* may not be used to endorse or promote products derived from this software29* without specific prior written permission.30*31* THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND32* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE33* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE34* ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES BE LIABLE35* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL36* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS37* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)38* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT39* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY40* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF41* SUCH DAMAGE.42*/43/*44* Taken from the original scsi(8) program.45* from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";46*/47#include <sys/cdefs.h>48#include <sys/stdint.h>49#include <sys/types.h>5051#include <stdlib.h>52#include <stdio.h>53#include <string.h>5455#include <camlib.h>56#include "camcontrol.h"5758int verbose;5960/* iget: Integer argument callback61*/62int63iget(void *hook, char *name)64{65struct get_hook *h = (struct get_hook *)hook;66int arg;6768if (h->got >= h->argc)69{70fprintf(stderr, "Expecting an integer argument.\n");71usage(0);72exit(1);73}74arg = strtol(h->argv[h->got], 0, 0);75h->got++;7677if (verbose && name && *name)78printf("%s: %d\n", name, arg);7980return arg;81}8283/* cget: char * argument callback84*/85char *86cget(void *hook, char *name)87{88struct get_hook *h = (struct get_hook *)hook;89char *arg;9091if (h->got >= h->argc)92{93fprintf(stderr, "Expecting a character pointer argument.\n");94usage(0);95exit(1);96}97arg = h->argv[h->got];98h->got++;99100if (verbose && name)101printf("cget: %s: %s", name, arg);102103return arg;104}105106/* arg_put: "put argument" callback107*/108void109arg_put(void *hook __unused, int letter, void *arg, int count, char *name)110{111if (verbose && name && *name)112printf("%s: ", name);113114switch(letter)115{116case 'i':117case 'b':118printf("%jd ", (intmax_t)(intptr_t)arg);119break;120121case 'c':122case 'z':123{124char *p;125126p = calloc(1, count + 1);127if (p == NULL) {128fprintf(stderr, "can't malloc memory for p\n");129exit(1);130}131132strlcpy(p, (char *)arg, count + 1);133if (letter == 'z')134{135int i;136for (i = count - 1; i >= 0; i--)137if (p[i] == ' ')138p[i] = 0;139else140break;141}142printf("%s ", p);143144free(p);145}146147break;148149default:150printf("Unknown format letter: '%c'\n", letter);151}152if (verbose)153putchar('\n');154}155156/*157* Get confirmation from user158* Return values:159* 1: confirmed160* 0: unconfirmed161*/162int163get_confirmation(void)164{165char str[1024];166int response = -1;167168do {169fprintf(stdout, "Are you SURE you want to do this? (yes/no) ");170if (fgets(str, sizeof(str), stdin) != NULL) {171if (strncasecmp(str, "yes", 3) == 0)172response = 1;173else if (strncasecmp(str, "no", 2) == 0)174response = 0;175else176fprintf(stdout,177"Please answer \"yes\" or \"no\"\n");178} else179response = 0;180} while (response == -1);181return (response);182}183184185