1229997Sken/*
2229997Sken * Written By Julian ELischer
3229997Sken * Copyright julian Elischer 1993.
4229997Sken * Permission is granted to use or redistribute this file in any way as long
5229997Sken * as this notice remains. Julian Elischer does not guarantee that this file
6229997Sken * is totally correct for any given task and users of this file must
7229997Sken * accept responsibility for any damage that occurs from the application of this
8229997Sken * file.
9229997Sken *
10229997Sken * (julian@tfs.com julian@dialix.oz.au)
11229997Sken *
12229997Sken * User SCSI hooks added by Peter Dufault:
13229997Sken *
14229997Sken * Copyright (c) 1994 HD Associates
15229997Sken * (contact: dufault@hda.com)
16229997Sken * All rights reserved.
17229997Sken *
18229997Sken * Redistribution and use in source and binary forms, with or without
19229997Sken * modification, are permitted provided that the following conditions
20229997Sken * are met:
21229997Sken * 1. Redistributions of source code must retain the above copyright
22229997Sken *    notice, this list of conditions and the following disclaimer.
23229997Sken * 2. Redistributions in binary form must reproduce the above copyright
24229997Sken *    notice, this list of conditions and the following disclaimer in the
25229997Sken *    documentation and/or other materials provided with the distribution.
26229997Sken * 3. The name of HD Associates
27229997Sken *    may not be used to endorse or promote products derived from this software
28229997Sken *    without specific prior written permission.
29229997Sken *
30229997Sken * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
31229997Sken * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32229997Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33229997Sken * ARE DISCLAIMED.  IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
34229997Sken * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38229997Sken * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39229997Sken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40229997Sken * SUCH DAMAGE.
41229997Sken */
42229997Sken/*
43229997Sken * Taken from the original scsi(8) program.
44229997Sken * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";
45229997Sken */
46229997Sken#include <sys/cdefs.h>
47229997Sken__FBSDID("$FreeBSD: releng/10.3/usr.sbin/ctladm/util.c 241737 2012-10-19 14:49:42Z ed $");
48229997Sken
49229997Sken#include <sys/stdint.h>
50229997Sken#include <sys/types.h>
51229997Sken
52229997Sken#include <stdlib.h>
53229997Sken#include <stdio.h>
54229997Sken#include <string.h>
55229997Sken
56229997Sken#include <camlib.h>
57229997Sken#include "ctladm.h"
58229997Sken
59241737Sedstatic int verbose;
60229997Sken
61229997Sken/* iget: Integer argument callback
62229997Sken */
63229997Skenint
64229997Skeniget(void *hook, char *name)
65229997Sken{
66229997Sken	struct get_hook *h = (struct get_hook *)hook;
67229997Sken	int arg;
68229997Sken
69229997Sken	if (h->got >= h->argc)
70229997Sken	{
71229997Sken		fprintf(stderr, "Expecting an integer argument.\n");
72229997Sken		usage(0);
73229997Sken		exit(1);
74229997Sken	}
75229997Sken	arg = strtol(h->argv[h->got], 0, 0);
76229997Sken	h->got++;
77229997Sken
78229997Sken	if (verbose && name && *name)
79229997Sken		printf("%s: %d\n", name, arg);
80229997Sken
81229997Sken	return arg;
82229997Sken}
83229997Sken
84229997Sken/* cget: char * argument callback
85229997Sken */
86229997Skenchar *
87229997Skencget(void *hook, char *name)
88229997Sken{
89229997Sken	struct get_hook *h = (struct get_hook *)hook;
90229997Sken	char *arg;
91229997Sken
92229997Sken	if (h->got >= h->argc)
93229997Sken	{
94229997Sken		fprintf(stderr, "Expecting a character pointer argument.\n");
95229997Sken		usage(0);
96229997Sken		exit(1);
97229997Sken	}
98229997Sken	arg = h->argv[h->got];
99229997Sken	h->got++;
100229997Sken
101229997Sken	if (verbose && name)
102229997Sken		printf("cget: %s: %s", name, arg);
103229997Sken
104229997Sken	return arg;
105229997Sken}
106229997Sken
107229997Sken/* arg_put: "put argument" callback
108229997Sken */
109229997Skenvoid
110229997Skenarg_put(void *hook __unused, int letter, void *arg, int count, char *name)
111229997Sken{
112229997Sken	if (verbose && name && *name)
113229997Sken		printf("%s:  ", name);
114229997Sken
115229997Sken	switch(letter)
116229997Sken	{
117229997Sken		case 'i':
118229997Sken		case 'b':
119229997Sken		printf("%jd ", (intmax_t)(intptr_t)arg);
120229997Sken		break;
121229997Sken
122229997Sken		case 'c':
123229997Sken		case 'z':
124229997Sken		{
125229997Sken			char *p;
126229997Sken
127229997Sken			p = malloc(count + 1);
128229997Sken			if (p == NULL) {
129229997Sken				fprintf(stderr, "can't malloc memory for p\n");
130229997Sken				exit(1);
131229997Sken			}
132229997Sken
133229997Sken			bzero(p, count +1);
134229997Sken			strncpy(p, (char *)arg, count);
135229997Sken			if (letter == 'z')
136229997Sken			{
137229997Sken				int i;
138229997Sken				for (i = count - 1; i >= 0; i--)
139229997Sken					if (p[i] == ' ')
140229997Sken						p[i] = 0;
141229997Sken					else
142229997Sken						break;
143229997Sken			}
144229997Sken			printf("%s ", p);
145229997Sken
146229997Sken			free(p);
147229997Sken		}
148229997Sken
149229997Sken		break;
150229997Sken
151229997Sken		default:
152229997Sken		printf("Unknown format letter: '%c'\n", letter);
153229997Sken	}
154229997Sken	if (verbose)
155229997Sken		putchar('\n');
156229997Sken}
157