1330449Seadler/*-
2330449Seadler * SPDX-License-Identifier: BSD-3-Clause
3330449Seadler *
439214Sgibbs * Written By Julian ELischer
539214Sgibbs * Copyright julian Elischer 1993.
639214Sgibbs * Permission is granted to use or redistribute this file in any way as long
739214Sgibbs * as this notice remains. Julian Elischer does not guarantee that this file
839214Sgibbs * is totally correct for any given task and users of this file must
939214Sgibbs * accept responsibility for any damage that occurs from the application of this
1039214Sgibbs * file.
1139214Sgibbs *
1239214Sgibbs * (julian@tfs.com julian@dialix.oz.au)
1339214Sgibbs *
1439214Sgibbs * User SCSI hooks added by Peter Dufault:
1539214Sgibbs *
1639214Sgibbs * Copyright (c) 1994 HD Associates
1739214Sgibbs * (contact: dufault@hda.com)
1839214Sgibbs * All rights reserved.
1939214Sgibbs *
2039214Sgibbs * Redistribution and use in source and binary forms, with or without
2139214Sgibbs * modification, are permitted provided that the following conditions
2239214Sgibbs * are met:
2339214Sgibbs * 1. Redistributions of source code must retain the above copyright
2439214Sgibbs *    notice, this list of conditions and the following disclaimer.
2539214Sgibbs * 2. Redistributions in binary form must reproduce the above copyright
2639214Sgibbs *    notice, this list of conditions and the following disclaimer in the
2739214Sgibbs *    documentation and/or other materials provided with the distribution.
2839214Sgibbs * 3. The name of HD Associates
2939214Sgibbs *    may not be used to endorse or promote products derived from this software
3039214Sgibbs *    without specific prior written permission.
3139214Sgibbs *
3239214Sgibbs * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
3339214Sgibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3439214Sgibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3539214Sgibbs * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
3639214Sgibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3739214Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3839214Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3939214Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4039214Sgibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4139214Sgibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4239214Sgibbs * SUCH DAMAGE.
4339214Sgibbs */
4439214Sgibbs/*
4539214Sgibbs * Taken from the original scsi(8) program.
4639214Sgibbs * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";
4739214Sgibbs */
48114513Sobrien#include <sys/cdefs.h>
49114513Sobrien__FBSDID("$FreeBSD: stable/11/sbin/camcontrol/util.c 330449 2018-03-05 07:26:05Z eadler $");
5039214Sgibbs
51111195Sjohan#include <sys/stdint.h>
52111195Sjohan#include <sys/types.h>
53111195Sjohan
5439214Sgibbs#include <stdlib.h>
5539214Sgibbs#include <stdio.h>
5664382Skbyanc#include <string.h>
5739214Sgibbs
5839214Sgibbs#include <camlib.h>
5939214Sgibbs#include "camcontrol.h"
6039214Sgibbs
6164382Skbyancint verbose;
6239214Sgibbs
6339214Sgibbs/* iget: Integer argument callback
6439214Sgibbs */
6539214Sgibbsint
6639214Sgibbsiget(void *hook, char *name)
6739214Sgibbs{
6839214Sgibbs	struct get_hook *h = (struct get_hook *)hook;
6939214Sgibbs	int arg;
7039214Sgibbs
7139214Sgibbs	if (h->got >= h->argc)
7239214Sgibbs	{
7339214Sgibbs		fprintf(stderr, "Expecting an integer argument.\n");
7446938Sken		usage(0);
7539214Sgibbs		exit(1);
7639214Sgibbs	}
7739214Sgibbs	arg = strtol(h->argv[h->got], 0, 0);
7839214Sgibbs	h->got++;
7939214Sgibbs
8039214Sgibbs	if (verbose && name && *name)
8139214Sgibbs		printf("%s: %d\n", name, arg);
8239214Sgibbs
8339214Sgibbs	return arg;
8439214Sgibbs}
8539214Sgibbs
8639214Sgibbs/* cget: char * argument callback
8739214Sgibbs */
8839214Sgibbschar *
8939214Sgibbscget(void *hook, char *name)
9039214Sgibbs{
9139214Sgibbs	struct get_hook *h = (struct get_hook *)hook;
9239214Sgibbs	char *arg;
9339214Sgibbs
9439214Sgibbs	if (h->got >= h->argc)
9539214Sgibbs	{
9639214Sgibbs		fprintf(stderr, "Expecting a character pointer argument.\n");
9746938Sken		usage(0);
9839214Sgibbs		exit(1);
9939214Sgibbs	}
10039214Sgibbs	arg = h->argv[h->got];
10139214Sgibbs	h->got++;
10239214Sgibbs
10339214Sgibbs	if (verbose && name)
10439214Sgibbs		printf("cget: %s: %s", name, arg);
10539214Sgibbs
10639214Sgibbs	return arg;
10739214Sgibbs}
10839214Sgibbs
10939214Sgibbs/* arg_put: "put argument" callback
11039214Sgibbs */
11139214Sgibbsvoid
112118478Sjohanarg_put(void *hook __unused, int letter, void *arg, int count, char *name)
11339214Sgibbs{
11439214Sgibbs	if (verbose && name && *name)
11539214Sgibbs		printf("%s:  ", name);
11639214Sgibbs
11739214Sgibbs	switch(letter)
11839214Sgibbs	{
11939214Sgibbs		case 'i':
12039214Sgibbs		case 'b':
121111195Sjohan		printf("%jd ", (intmax_t)(intptr_t)arg);
12239214Sgibbs		break;
12339214Sgibbs
12439214Sgibbs		case 'c':
12539214Sgibbs		case 'z':
12639214Sgibbs		{
12739214Sgibbs			char *p;
12839214Sgibbs
12939214Sgibbs			p = malloc(count + 1);
13069471Sjedgar			if (p == NULL) {
13169471Sjedgar				fprintf(stderr, "can't malloc memory for p\n");
13269471Sjedgar				exit(1);
13369471Sjedgar			}
13439214Sgibbs
13539214Sgibbs			bzero(p, count +1);
13639214Sgibbs			strncpy(p, (char *)arg, count);
13739214Sgibbs			if (letter == 'z')
13839214Sgibbs			{
13939214Sgibbs				int i;
14039214Sgibbs				for (i = count - 1; i >= 0; i--)
14139214Sgibbs					if (p[i] == ' ')
14239214Sgibbs						p[i] = 0;
14339214Sgibbs					else
14439214Sgibbs						break;
14539214Sgibbs			}
14639214Sgibbs			printf("%s ", p);
14739214Sgibbs
14839214Sgibbs			free(p);
14939214Sgibbs		}
15039214Sgibbs
15139214Sgibbs		break;
15239214Sgibbs
15339214Sgibbs		default:
15439214Sgibbs		printf("Unknown format letter: '%c'\n", letter);
15539214Sgibbs	}
15639214Sgibbs	if (verbose)
15739214Sgibbs		putchar('\n');
15839214Sgibbs}
159227961Semaste
160227961Semaste/*
161227961Semaste * Get confirmation from user
162227961Semaste * Return values:
163227961Semaste *    1: confirmed
164227961Semaste *    0: unconfirmed
165227961Semaste */
166227961Semasteint
167227963Sdelphijget_confirmation(void)
168227961Semaste{
169227961Semaste	char str[1024];
170227961Semaste	int response = -1;
171227961Semaste
172227961Semaste	do {
173227961Semaste		fprintf(stdout, "Are you SURE you want to do this? (yes/no) ");
174227961Semaste		if (fgets(str, sizeof(str), stdin) != NULL) {
175227961Semaste			if (strncasecmp(str, "yes", 3) == 0)
176227961Semaste				response = 1;
177227961Semaste			else if (strncasecmp(str, "no", 2) == 0)
178227961Semaste				response = 0;
179227961Semaste			else
180227961Semaste				fprintf(stdout,
181227961Semaste				    "Please answer \"yes\" or \"no\"\n");
182227961Semaste		} else
183227961Semaste			response = 0;
184227961Semaste	} while (response == -1);
185227961Semaste	return (response);
186227961Semaste}
187