xref: /freebsd/sbin/camcontrol/util.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Written By Julian ELischer
5  * Copyright julian Elischer 1993.
6  * Permission is granted to use or redistribute this file in any way as long
7  * as this notice remains. Julian Elischer does not guarantee that this file
8  * is totally correct for any given task and users of this file must
9  * accept responsibility for any damage that occurs from the application of this
10  * file.
11  *
12  * (julian@tfs.com julian@dialix.oz.au)
13  *
14  * User SCSI hooks added by Peter Dufault:
15  *
16  * Copyright (c) 1994 HD Associates
17  * (contact: dufault@hda.com)
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. The name of HD Associates
29  *    may not be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  */
44 /*
45  * Taken from the original scsi(8) program.
46  * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";
47  */
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include <sys/stdint.h>
52 #include <sys/types.h>
53 
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
57 
58 #include <camlib.h>
59 #include "camcontrol.h"
60 
61 int verbose;
62 
63 /* iget: Integer argument callback
64  */
65 int
66 iget(void *hook, char *name)
67 {
68 	struct get_hook *h = (struct get_hook *)hook;
69 	int arg;
70 
71 	if (h->got >= h->argc)
72 	{
73 		fprintf(stderr, "Expecting an integer argument.\n");
74 		usage(0);
75 		exit(1);
76 	}
77 	arg = strtol(h->argv[h->got], 0, 0);
78 	h->got++;
79 
80 	if (verbose && name && *name)
81 		printf("%s: %d\n", name, arg);
82 
83 	return arg;
84 }
85 
86 /* cget: char * argument callback
87  */
88 char *
89 cget(void *hook, char *name)
90 {
91 	struct get_hook *h = (struct get_hook *)hook;
92 	char *arg;
93 
94 	if (h->got >= h->argc)
95 	{
96 		fprintf(stderr, "Expecting a character pointer argument.\n");
97 		usage(0);
98 		exit(1);
99 	}
100 	arg = h->argv[h->got];
101 	h->got++;
102 
103 	if (verbose && name)
104 		printf("cget: %s: %s", name, arg);
105 
106 	return arg;
107 }
108 
109 /* arg_put: "put argument" callback
110  */
111 void
112 arg_put(void *hook __unused, int letter, void *arg, int count, char *name)
113 {
114 	if (verbose && name && *name)
115 		printf("%s:  ", name);
116 
117 	switch(letter)
118 	{
119 		case 'i':
120 		case 'b':
121 		printf("%jd ", (intmax_t)(intptr_t)arg);
122 		break;
123 
124 		case 'c':
125 		case 'z':
126 		{
127 			char *p;
128 
129 			p = calloc(1, count + 1);
130 			if (p == NULL) {
131 				fprintf(stderr, "can't malloc memory for p\n");
132 				exit(1);
133 			}
134 
135 			strlcpy(p, (char *)arg, count + 1);
136 			if (letter == 'z')
137 			{
138 				int i;
139 				for (i = count - 1; i >= 0; i--)
140 					if (p[i] == ' ')
141 						p[i] = 0;
142 					else
143 						break;
144 			}
145 			printf("%s ", p);
146 
147 			free(p);
148 		}
149 
150 		break;
151 
152 		default:
153 		printf("Unknown format letter: '%c'\n", letter);
154 	}
155 	if (verbose)
156 		putchar('\n');
157 }
158 
159 /*
160  * Get confirmation from user
161  * Return values:
162  *    1: confirmed
163  *    0: unconfirmed
164  */
165 int
166 get_confirmation(void)
167 {
168 	char str[1024];
169 	int response = -1;
170 
171 	do {
172 		fprintf(stdout, "Are you SURE you want to do this? (yes/no) ");
173 		if (fgets(str, sizeof(str), stdin) != NULL) {
174 			if (strncasecmp(str, "yes", 3) == 0)
175 				response = 1;
176 			else if (strncasecmp(str, "no", 2) == 0)
177 				response = 0;
178 			else
179 				fprintf(stdout,
180 				    "Please answer \"yes\" or \"no\"\n");
181 		} else
182 			response = 0;
183 	} while (response == -1);
184 	return (response);
185 }
186