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