xref: /titanic_52/usr/src/cmd/format/misc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This file contains miscellaneous routines.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate #include "global.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <signal.h>
36*7c478bd9Sstevel@tonic-gate #include <malloc.h>
37*7c478bd9Sstevel@tonic-gate #include <unistd.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
44*7c478bd9Sstevel@tonic-gate #include <ctype.h>
45*7c478bd9Sstevel@tonic-gate #include <termio.h>
46*7c478bd9Sstevel@tonic-gate #include "misc.h"
47*7c478bd9Sstevel@tonic-gate #include "analyze.h"
48*7c478bd9Sstevel@tonic-gate #include "label.h"
49*7c478bd9Sstevel@tonic-gate #include "startup.h"
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
54*7c478bd9Sstevel@tonic-gate static void	cleanup(int sig);
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate /* Function prototypes for non-ANSI C Compilers */
59*7c478bd9Sstevel@tonic-gate static void	cleanup();
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate struct	env *current_env = NULL;	/* ptr to current environment */
64*7c478bd9Sstevel@tonic-gate static int	stop_pending = 0;	/* ctrl-Z is pending */
65*7c478bd9Sstevel@tonic-gate struct	ttystate ttystate;		/* tty info */
66*7c478bd9Sstevel@tonic-gate static int	aborting = 0;		/* in process of aborting */
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * For 4.x, limit the choices of valid disk names to this set.
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate static char		*disk_4x_identifiers[] = { "sd", "id"};
72*7c478bd9Sstevel@tonic-gate #define	N_DISK_4X_IDS	(sizeof (disk_4x_identifiers)/sizeof (char *))
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /*
76*7c478bd9Sstevel@tonic-gate  * This is the list of legal inputs for all yes/no questions.
77*7c478bd9Sstevel@tonic-gate  */
78*7c478bd9Sstevel@tonic-gate char	*confirm_list[] = {
79*7c478bd9Sstevel@tonic-gate 	"yes",
80*7c478bd9Sstevel@tonic-gate 	"no",
81*7c478bd9Sstevel@tonic-gate 	NULL,
82*7c478bd9Sstevel@tonic-gate };
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate  * This routine is a wrapper for malloc.  It allocates pre-zeroed space,
86*7c478bd9Sstevel@tonic-gate  * and checks the return value so the caller doesn't have to.
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate void *
89*7c478bd9Sstevel@tonic-gate zalloc(count)
90*7c478bd9Sstevel@tonic-gate 	int	count;
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	void	*ptr;
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if ((ptr = (void *) calloc(1, (unsigned)count)) == NULL) {
95*7c478bd9Sstevel@tonic-gate 		err_print("Error: unable to calloc more space.\n");
96*7c478bd9Sstevel@tonic-gate 		fullabort();
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	return (ptr);
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * This routine is a wrapper for realloc.  It reallocates the given
103*7c478bd9Sstevel@tonic-gate  * space, and checks the return value so the caller doesn't have to.
104*7c478bd9Sstevel@tonic-gate  * Note that the any space added by this call is NOT necessarily
105*7c478bd9Sstevel@tonic-gate  * zeroed.
106*7c478bd9Sstevel@tonic-gate  */
107*7c478bd9Sstevel@tonic-gate void *
108*7c478bd9Sstevel@tonic-gate rezalloc(ptr, count)
109*7c478bd9Sstevel@tonic-gate 	void	*ptr;
110*7c478bd9Sstevel@tonic-gate 	int	count;
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	void	*new_ptr;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if ((new_ptr = (void *) realloc((char *)ptr,
116*7c478bd9Sstevel@tonic-gate 				(unsigned)count)) == NULL) {
117*7c478bd9Sstevel@tonic-gate 		err_print("Error: unable to realloc more space.\n");
118*7c478bd9Sstevel@tonic-gate 		fullabort();
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 	return (new_ptr);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*
124*7c478bd9Sstevel@tonic-gate  * This routine is a wrapper for free.
125*7c478bd9Sstevel@tonic-gate  */
126*7c478bd9Sstevel@tonic-gate void
127*7c478bd9Sstevel@tonic-gate destroy_data(data)
128*7c478bd9Sstevel@tonic-gate 	char	*data;
129*7c478bd9Sstevel@tonic-gate {
130*7c478bd9Sstevel@tonic-gate 	free((char *)data);
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate #ifdef	not
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * This routine takes the space number returned by an ioctl call and
136*7c478bd9Sstevel@tonic-gate  * returns a mnemonic name for that space.
137*7c478bd9Sstevel@tonic-gate  */
138*7c478bd9Sstevel@tonic-gate char *
139*7c478bd9Sstevel@tonic-gate space2str(space)
140*7c478bd9Sstevel@tonic-gate 	uint_t	space;
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	char	*name;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	switch (space&SP_BUSMASK) {
145*7c478bd9Sstevel@tonic-gate 	    case SP_VIRTUAL:
146*7c478bd9Sstevel@tonic-gate 		name = "virtual";
147*7c478bd9Sstevel@tonic-gate 		break;
148*7c478bd9Sstevel@tonic-gate 	    case SP_OBMEM:
149*7c478bd9Sstevel@tonic-gate 		name = "obmem";
150*7c478bd9Sstevel@tonic-gate 		break;
151*7c478bd9Sstevel@tonic-gate 	    case SP_OBIO:
152*7c478bd9Sstevel@tonic-gate 		name = "obio";
153*7c478bd9Sstevel@tonic-gate 		break;
154*7c478bd9Sstevel@tonic-gate 	    case SP_MBMEM:
155*7c478bd9Sstevel@tonic-gate 		name = "mbmem";
156*7c478bd9Sstevel@tonic-gate 		break;
157*7c478bd9Sstevel@tonic-gate 	    case SP_MBIO:
158*7c478bd9Sstevel@tonic-gate 		name = "mbio";
159*7c478bd9Sstevel@tonic-gate 		break;
160*7c478bd9Sstevel@tonic-gate 	    default:
161*7c478bd9Sstevel@tonic-gate 		err_print("Error: unknown address space type encountered.\n");
162*7c478bd9Sstevel@tonic-gate 		fullabort();
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	return (name);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate #endif	/* not */
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * This routine asks the user the given yes/no question and returns
170*7c478bd9Sstevel@tonic-gate  * the response.
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate int
173*7c478bd9Sstevel@tonic-gate check(question)
174*7c478bd9Sstevel@tonic-gate 	char	*question;
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	int		answer;
177*7c478bd9Sstevel@tonic-gate 	u_ioparam_t	ioparam;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	/*
180*7c478bd9Sstevel@tonic-gate 	 * If we are running out of a command file, assume a yes answer.
181*7c478bd9Sstevel@tonic-gate 	 */
182*7c478bd9Sstevel@tonic-gate 	if (option_f)
183*7c478bd9Sstevel@tonic-gate 		return (0);
184*7c478bd9Sstevel@tonic-gate 	/*
185*7c478bd9Sstevel@tonic-gate 	 * Ask the user.
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	ioparam.io_charlist = confirm_list;
188*7c478bd9Sstevel@tonic-gate 	answer = input(FIO_MSTR, question, '?', &ioparam,
189*7c478bd9Sstevel@tonic-gate 	    (int *)NULL, DATA_INPUT);
190*7c478bd9Sstevel@tonic-gate 	return (answer);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate /*
194*7c478bd9Sstevel@tonic-gate  * This routine aborts the current command.  It is called by a ctrl-C
195*7c478bd9Sstevel@tonic-gate  * interrupt and also under certain error conditions.
196*7c478bd9Sstevel@tonic-gate  */
197*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
198*7c478bd9Sstevel@tonic-gate void
199*7c478bd9Sstevel@tonic-gate cmdabort(sig)
200*7c478bd9Sstevel@tonic-gate 	int	sig;
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	/*
204*7c478bd9Sstevel@tonic-gate 	 * If there is no usable saved environment, gracefully exit.  This
205*7c478bd9Sstevel@tonic-gate 	 * allows the user to interrupt the program even when input is from
206*7c478bd9Sstevel@tonic-gate 	 * a file, or if there is no current menu, like at the "Select disk:"
207*7c478bd9Sstevel@tonic-gate 	 * prompt.
208*7c478bd9Sstevel@tonic-gate 	 */
209*7c478bd9Sstevel@tonic-gate 	if (current_env == NULL || !(current_env->flags & ENV_USE))
210*7c478bd9Sstevel@tonic-gate 		fullabort();
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	/*
213*7c478bd9Sstevel@tonic-gate 	 * If we are in a critical zone, note the attempt and return.
214*7c478bd9Sstevel@tonic-gate 	 */
215*7c478bd9Sstevel@tonic-gate 	if (current_env->flags & ENV_CRITICAL) {
216*7c478bd9Sstevel@tonic-gate 		current_env->flags |= ENV_ABORT;
217*7c478bd9Sstevel@tonic-gate 		return;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	/*
220*7c478bd9Sstevel@tonic-gate 	 * All interruptions when we are running out of a command file
221*7c478bd9Sstevel@tonic-gate 	 * cause the program to gracefully exit.
222*7c478bd9Sstevel@tonic-gate 	 */
223*7c478bd9Sstevel@tonic-gate 	if (option_f)
224*7c478bd9Sstevel@tonic-gate 		fullabort();
225*7c478bd9Sstevel@tonic-gate 	fmt_print("\n");
226*7c478bd9Sstevel@tonic-gate 	/*
227*7c478bd9Sstevel@tonic-gate 	 * Clean up any state left by the interrupted command.
228*7c478bd9Sstevel@tonic-gate 	 */
229*7c478bd9Sstevel@tonic-gate 	cleanup(sig);
230*7c478bd9Sstevel@tonic-gate 	/*
231*7c478bd9Sstevel@tonic-gate 	 * Jump to the saved environment.
232*7c478bd9Sstevel@tonic-gate 	 */
233*7c478bd9Sstevel@tonic-gate 	longjmp(current_env->env, 0);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * This routine implements the ctrl-Z suspend mechanism.  It is called
238*7c478bd9Sstevel@tonic-gate  * when a suspend signal is received.
239*7c478bd9Sstevel@tonic-gate  */
240*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
241*7c478bd9Sstevel@tonic-gate void
242*7c478bd9Sstevel@tonic-gate onsusp(sig)
243*7c478bd9Sstevel@tonic-gate 	int	sig;
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	int		fix_term;
246*7c478bd9Sstevel@tonic-gate #ifdef	NOT_DEF
247*7c478bd9Sstevel@tonic-gate 	sigset_t	sigmask;
248*7c478bd9Sstevel@tonic-gate #endif	/* NOT_DEF */
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	/*
251*7c478bd9Sstevel@tonic-gate 	 * If we are in a critical zone, note the attempt and return.
252*7c478bd9Sstevel@tonic-gate 	 */
253*7c478bd9Sstevel@tonic-gate 	if (current_env != NULL && current_env->flags & ENV_CRITICAL) {
254*7c478bd9Sstevel@tonic-gate 		stop_pending = 1;
255*7c478bd9Sstevel@tonic-gate 		return;
256*7c478bd9Sstevel@tonic-gate 	}
257*7c478bd9Sstevel@tonic-gate 	/*
258*7c478bd9Sstevel@tonic-gate 	 * If the terminal is mucked up, note that we will need to
259*7c478bd9Sstevel@tonic-gate 	 * re-muck it when we start up again.
260*7c478bd9Sstevel@tonic-gate 	 */
261*7c478bd9Sstevel@tonic-gate 	fix_term = ttystate.ttyflags;
262*7c478bd9Sstevel@tonic-gate 	fmt_print("\n");
263*7c478bd9Sstevel@tonic-gate 	/*
264*7c478bd9Sstevel@tonic-gate 	 * Clean up any state left by the interrupted command.
265*7c478bd9Sstevel@tonic-gate 	 */
266*7c478bd9Sstevel@tonic-gate 	cleanup(sig);
267*7c478bd9Sstevel@tonic-gate #ifdef	NOT_DEF
268*7c478bd9Sstevel@tonic-gate 	/* Investigate whether all this is necessary */
269*7c478bd9Sstevel@tonic-gate 	/*
270*7c478bd9Sstevel@tonic-gate 	 * Stop intercepting the suspend signal, then send ourselves one
271*7c478bd9Sstevel@tonic-gate 	 * to cause us to stop.
272*7c478bd9Sstevel@tonic-gate 	 */
273*7c478bd9Sstevel@tonic-gate 	sigmask.sigbits[0] = (ulong_t)0xffffffff;
274*7c478bd9Sstevel@tonic-gate 	if (sigprocmask(SIG_SETMASK, &sigmask, (sigset_t *)NULL) == -1)
275*7c478bd9Sstevel@tonic-gate 		err_print("sigprocmask failed %d\n", errno);
276*7c478bd9Sstevel@tonic-gate #endif	/* NOT_DEF */
277*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_DFL);
278*7c478bd9Sstevel@tonic-gate 	(void) kill(0, SIGTSTP);
279*7c478bd9Sstevel@tonic-gate 	/*
280*7c478bd9Sstevel@tonic-gate 	 * PC stops here
281*7c478bd9Sstevel@tonic-gate 	 */
282*7c478bd9Sstevel@tonic-gate 	/*
283*7c478bd9Sstevel@tonic-gate 	 * We are started again.  Set us up to intercept the suspend
284*7c478bd9Sstevel@tonic-gate 	 * signal once again.
285*7c478bd9Sstevel@tonic-gate 	 */
286*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, onsusp);
287*7c478bd9Sstevel@tonic-gate 	/*
288*7c478bd9Sstevel@tonic-gate 	 * Re-muck the terminal if necessary.
289*7c478bd9Sstevel@tonic-gate 	 */
290*7c478bd9Sstevel@tonic-gate 	if (fix_term & TTY_ECHO_OFF)
291*7c478bd9Sstevel@tonic-gate 		echo_off();
292*7c478bd9Sstevel@tonic-gate 	if (fix_term & TTY_CBREAK_ON)
293*7c478bd9Sstevel@tonic-gate 		charmode_on();
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate /*
297*7c478bd9Sstevel@tonic-gate  * This routine implements the timing function used during long-term
298*7c478bd9Sstevel@tonic-gate  * disk operations (e.g. formatting).  It is called when an alarm signal
299*7c478bd9Sstevel@tonic-gate  * is received.
300*7c478bd9Sstevel@tonic-gate  */
301*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
302*7c478bd9Sstevel@tonic-gate void
303*7c478bd9Sstevel@tonic-gate onalarm(sig)
304*7c478bd9Sstevel@tonic-gate 	int	sig;
305*7c478bd9Sstevel@tonic-gate {
306*7c478bd9Sstevel@tonic-gate }
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate  * This routine gracefully exits the program.
311*7c478bd9Sstevel@tonic-gate  */
312*7c478bd9Sstevel@tonic-gate void
313*7c478bd9Sstevel@tonic-gate fullabort()
314*7c478bd9Sstevel@tonic-gate {
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	fmt_print("\n");
317*7c478bd9Sstevel@tonic-gate 	/*
318*7c478bd9Sstevel@tonic-gate 	 * Clean up any state left by an interrupted command.
319*7c478bd9Sstevel@tonic-gate 	 * Avoid infinite loops caused by a clean-up
320*7c478bd9Sstevel@tonic-gate 	 * routine failing again...
321*7c478bd9Sstevel@tonic-gate 	 */
322*7c478bd9Sstevel@tonic-gate 	if (!aborting) {
323*7c478bd9Sstevel@tonic-gate 		aborting = 1;
324*7c478bd9Sstevel@tonic-gate 		cleanup(SIGKILL);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 	exit(1);
327*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate /*
331*7c478bd9Sstevel@tonic-gate  * This routine cleans up the state of the world.  It is a hodge-podge
332*7c478bd9Sstevel@tonic-gate  * of kludges to allow us to interrupt commands whenever possible.
333*7c478bd9Sstevel@tonic-gate  *
334*7c478bd9Sstevel@tonic-gate  * Some cleanup actions may depend on the type of signal.
335*7c478bd9Sstevel@tonic-gate  */
336*7c478bd9Sstevel@tonic-gate static void
337*7c478bd9Sstevel@tonic-gate cleanup(int sig)
338*7c478bd9Sstevel@tonic-gate {
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	/*
341*7c478bd9Sstevel@tonic-gate 	 * Lock out interrupts to avoid recursion.
342*7c478bd9Sstevel@tonic-gate 	 */
343*7c478bd9Sstevel@tonic-gate 	enter_critical();
344*7c478bd9Sstevel@tonic-gate 	/*
345*7c478bd9Sstevel@tonic-gate 	 * Fix up the tty if necessary.
346*7c478bd9Sstevel@tonic-gate 	 */
347*7c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags & TTY_CBREAK_ON) {
348*7c478bd9Sstevel@tonic-gate 		charmode_off();
349*7c478bd9Sstevel@tonic-gate 	}
350*7c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags & TTY_ECHO_OFF) {
351*7c478bd9Sstevel@tonic-gate 		echo_on();
352*7c478bd9Sstevel@tonic-gate 	}
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	/*
355*7c478bd9Sstevel@tonic-gate 	 * If the defect list is dirty, write it out.
356*7c478bd9Sstevel@tonic-gate 	 */
357*7c478bd9Sstevel@tonic-gate 	if (cur_list.flags & LIST_DIRTY) {
358*7c478bd9Sstevel@tonic-gate 		cur_list.flags = 0;
359*7c478bd9Sstevel@tonic-gate 		if (!EMBEDDED_SCSI)
360*7c478bd9Sstevel@tonic-gate 			write_deflist(&cur_list);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 	/*
363*7c478bd9Sstevel@tonic-gate 	 * If the label is dirty, write it out.
364*7c478bd9Sstevel@tonic-gate 	 */
365*7c478bd9Sstevel@tonic-gate 	if (cur_flags & LABEL_DIRTY) {
366*7c478bd9Sstevel@tonic-gate 		cur_flags &= ~LABEL_DIRTY;
367*7c478bd9Sstevel@tonic-gate 		(void) write_label();
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 	/*
370*7c478bd9Sstevel@tonic-gate 	 * If we are logging and just interrupted a scan, print out
371*7c478bd9Sstevel@tonic-gate 	 * some summary info to the log file.
372*7c478bd9Sstevel@tonic-gate 	 */
373*7c478bd9Sstevel@tonic-gate 	if (log_file && scan_cur_block >= 0) {
374*7c478bd9Sstevel@tonic-gate 		pr_dblock(log_print, scan_cur_block);
375*7c478bd9Sstevel@tonic-gate 		log_print("\n");
376*7c478bd9Sstevel@tonic-gate 	}
377*7c478bd9Sstevel@tonic-gate 	if (scan_blocks_fixed >= 0)
378*7c478bd9Sstevel@tonic-gate 		fmt_print("Total of %lld defective blocks repaired.\n",
379*7c478bd9Sstevel@tonic-gate 		    scan_blocks_fixed);
380*7c478bd9Sstevel@tonic-gate 	if (sig != SIGSTOP) { /* Don't reset on suspend (converted to stop) */
381*7c478bd9Sstevel@tonic-gate 		scan_cur_block = scan_blocks_fixed = -1;
382*7c478bd9Sstevel@tonic-gate 	}
383*7c478bd9Sstevel@tonic-gate 	exit_critical();
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate /*
387*7c478bd9Sstevel@tonic-gate  * This routine causes the program to enter a critical zone.  Within the
388*7c478bd9Sstevel@tonic-gate  * critical zone, no interrupts are allowed.  Note that calls to this
389*7c478bd9Sstevel@tonic-gate  * routine for the same environment do NOT nest, so there is not
390*7c478bd9Sstevel@tonic-gate  * necessarily pairing between calls to enter_critical() and exit_critical().
391*7c478bd9Sstevel@tonic-gate  */
392*7c478bd9Sstevel@tonic-gate void
393*7c478bd9Sstevel@tonic-gate enter_critical()
394*7c478bd9Sstevel@tonic-gate {
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	/*
397*7c478bd9Sstevel@tonic-gate 	 * If there is no saved environment, interrupts will be ignored.
398*7c478bd9Sstevel@tonic-gate 	 */
399*7c478bd9Sstevel@tonic-gate 	if (current_env == NULL)
400*7c478bd9Sstevel@tonic-gate 		return;
401*7c478bd9Sstevel@tonic-gate 	/*
402*7c478bd9Sstevel@tonic-gate 	 * Mark the environment to be in a critical zone.
403*7c478bd9Sstevel@tonic-gate 	 */
404*7c478bd9Sstevel@tonic-gate 	current_env->flags |= ENV_CRITICAL;
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate /*
408*7c478bd9Sstevel@tonic-gate  * This routine causes the program to exit a critical zone.  Note that
409*7c478bd9Sstevel@tonic-gate  * calls to enter_critical() for the same environment do NOT nest, so
410*7c478bd9Sstevel@tonic-gate  * one call to exit_critical() will erase any number of such calls.
411*7c478bd9Sstevel@tonic-gate  */
412*7c478bd9Sstevel@tonic-gate void
413*7c478bd9Sstevel@tonic-gate exit_critical()
414*7c478bd9Sstevel@tonic-gate {
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	/*
417*7c478bd9Sstevel@tonic-gate 	 * If there is a saved environment, mark it to be non-critical.
418*7c478bd9Sstevel@tonic-gate 	 */
419*7c478bd9Sstevel@tonic-gate 	if (current_env != NULL)
420*7c478bd9Sstevel@tonic-gate 		current_env->flags &= ~ENV_CRITICAL;
421*7c478bd9Sstevel@tonic-gate 	/*
422*7c478bd9Sstevel@tonic-gate 	 * If there is a stop pending, execute the stop.
423*7c478bd9Sstevel@tonic-gate 	 */
424*7c478bd9Sstevel@tonic-gate 	if (stop_pending) {
425*7c478bd9Sstevel@tonic-gate 		stop_pending = 0;
426*7c478bd9Sstevel@tonic-gate 		onsusp(SIGSTOP);
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate 	/*
429*7c478bd9Sstevel@tonic-gate 	 * If there is an abort pending, execute the abort.
430*7c478bd9Sstevel@tonic-gate 	 */
431*7c478bd9Sstevel@tonic-gate 	if (current_env == NULL)
432*7c478bd9Sstevel@tonic-gate 		return;
433*7c478bd9Sstevel@tonic-gate 	if (current_env->flags & ENV_ABORT) {
434*7c478bd9Sstevel@tonic-gate 		current_env->flags &= ~ENV_ABORT;
435*7c478bd9Sstevel@tonic-gate 		cmdabort(SIGINT);
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate }
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate /*
440*7c478bd9Sstevel@tonic-gate  * This routine turns off echoing on the controlling tty for the program.
441*7c478bd9Sstevel@tonic-gate  */
442*7c478bd9Sstevel@tonic-gate void
443*7c478bd9Sstevel@tonic-gate echo_off()
444*7c478bd9Sstevel@tonic-gate {
445*7c478bd9Sstevel@tonic-gate 	/*
446*7c478bd9Sstevel@tonic-gate 	 * Open the tty and store the file pointer for later.
447*7c478bd9Sstevel@tonic-gate 	 */
448*7c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
449*7c478bd9Sstevel@tonic-gate 		if ((ttystate.ttyfile = open("/dev/tty",
450*7c478bd9Sstevel@tonic-gate 					O_RDWR | O_NDELAY)) < 0) {
451*7c478bd9Sstevel@tonic-gate 			err_print("Unable to open /dev/tty.\n");
452*7c478bd9Sstevel@tonic-gate 			fullabort();
453*7c478bd9Sstevel@tonic-gate 		}
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 	/*
456*7c478bd9Sstevel@tonic-gate 	 * Get the parameters for the tty, turn off echoing and set them.
457*7c478bd9Sstevel@tonic-gate 	 */
458*7c478bd9Sstevel@tonic-gate 	if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
459*7c478bd9Sstevel@tonic-gate 		err_print("Unable to get tty parameters.\n");
460*7c478bd9Sstevel@tonic-gate 		fullabort();
461*7c478bd9Sstevel@tonic-gate 	}
462*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag &= ~ECHO;
463*7c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
464*7c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to echo off state.\n");
465*7c478bd9Sstevel@tonic-gate 		fullabort();
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	/*
469*7c478bd9Sstevel@tonic-gate 	 * Remember that we've successfully turned
470*7c478bd9Sstevel@tonic-gate 	 * ECHO mode off, so we know to fix it later.
471*7c478bd9Sstevel@tonic-gate 	 */
472*7c478bd9Sstevel@tonic-gate 	ttystate.ttyflags |= TTY_ECHO_OFF;
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate /*
476*7c478bd9Sstevel@tonic-gate  * This routine turns on echoing on the controlling tty for the program.
477*7c478bd9Sstevel@tonic-gate  */
478*7c478bd9Sstevel@tonic-gate void
479*7c478bd9Sstevel@tonic-gate echo_on()
480*7c478bd9Sstevel@tonic-gate {
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	/*
483*7c478bd9Sstevel@tonic-gate 	 * Using the saved parameters, turn echoing on and set them.
484*7c478bd9Sstevel@tonic-gate 	 */
485*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag |= ECHO;
486*7c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
487*7c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to echo on state.\n");
488*7c478bd9Sstevel@tonic-gate 		fullabort();
489*7c478bd9Sstevel@tonic-gate 	}
490*7c478bd9Sstevel@tonic-gate 	/*
491*7c478bd9Sstevel@tonic-gate 	 * Close the tty and mark it ok again.
492*7c478bd9Sstevel@tonic-gate 	 */
493*7c478bd9Sstevel@tonic-gate 	ttystate.ttyflags &= ~TTY_ECHO_OFF;
494*7c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
495*7c478bd9Sstevel@tonic-gate 		(void) close(ttystate.ttyfile);
496*7c478bd9Sstevel@tonic-gate 	}
497*7c478bd9Sstevel@tonic-gate }
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate /*
500*7c478bd9Sstevel@tonic-gate  * This routine turns off single character entry mode for tty.
501*7c478bd9Sstevel@tonic-gate  */
502*7c478bd9Sstevel@tonic-gate void
503*7c478bd9Sstevel@tonic-gate charmode_on()
504*7c478bd9Sstevel@tonic-gate {
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate 	/*
507*7c478bd9Sstevel@tonic-gate 	 * If tty unopened, open the tty and store the file pointer for later.
508*7c478bd9Sstevel@tonic-gate 	 */
509*7c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
510*7c478bd9Sstevel@tonic-gate 		if ((ttystate.ttyfile = open("/dev/tty",
511*7c478bd9Sstevel@tonic-gate 					O_RDWR | O_NDELAY)) < 0) {
512*7c478bd9Sstevel@tonic-gate 			err_print("Unable to open /dev/tty.\n");
513*7c478bd9Sstevel@tonic-gate 			fullabort();
514*7c478bd9Sstevel@tonic-gate 		}
515*7c478bd9Sstevel@tonic-gate 	}
516*7c478bd9Sstevel@tonic-gate 	/*
517*7c478bd9Sstevel@tonic-gate 	 * Get the parameters for the tty, turn on char mode.
518*7c478bd9Sstevel@tonic-gate 	 */
519*7c478bd9Sstevel@tonic-gate 	if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
520*7c478bd9Sstevel@tonic-gate 		err_print("Unable to get tty parameters.\n");
521*7c478bd9Sstevel@tonic-gate 		fullabort();
522*7c478bd9Sstevel@tonic-gate 	}
523*7c478bd9Sstevel@tonic-gate 	ttystate.vmin = ttystate.ttystate.c_cc[VMIN];
524*7c478bd9Sstevel@tonic-gate 	ttystate.vtime = ttystate.ttystate.c_cc[VTIME];
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag &= ~ICANON;
527*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VMIN] = 1;
528*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VTIME] = 0;
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
531*7c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to cbreak on state.\n");
532*7c478bd9Sstevel@tonic-gate 		fullabort();
533*7c478bd9Sstevel@tonic-gate 	}
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	/*
536*7c478bd9Sstevel@tonic-gate 	 * Remember that we've successfully turned
537*7c478bd9Sstevel@tonic-gate 	 * CBREAK mode on, so we know to fix it later.
538*7c478bd9Sstevel@tonic-gate 	 */
539*7c478bd9Sstevel@tonic-gate 	ttystate.ttyflags |= TTY_CBREAK_ON;
540*7c478bd9Sstevel@tonic-gate }
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate /*
543*7c478bd9Sstevel@tonic-gate  * This routine turns on single character entry mode for tty.
544*7c478bd9Sstevel@tonic-gate  * Note, this routine must be called before echo_on.
545*7c478bd9Sstevel@tonic-gate  */
546*7c478bd9Sstevel@tonic-gate void
547*7c478bd9Sstevel@tonic-gate charmode_off()
548*7c478bd9Sstevel@tonic-gate {
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 	/*
551*7c478bd9Sstevel@tonic-gate 	 * Using the saved parameters, turn char mode on.
552*7c478bd9Sstevel@tonic-gate 	 */
553*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_lflag |= ICANON;
554*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VMIN] = ttystate.vmin;
555*7c478bd9Sstevel@tonic-gate 	ttystate.ttystate.c_cc[VTIME] = ttystate.vtime;
556*7c478bd9Sstevel@tonic-gate 	if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
557*7c478bd9Sstevel@tonic-gate 		err_print("Unable to set tty to cbreak off state.\n");
558*7c478bd9Sstevel@tonic-gate 		fullabort();
559*7c478bd9Sstevel@tonic-gate 	}
560*7c478bd9Sstevel@tonic-gate 	/*
561*7c478bd9Sstevel@tonic-gate 	 * Close the tty and mark it ok again.
562*7c478bd9Sstevel@tonic-gate 	 */
563*7c478bd9Sstevel@tonic-gate 	ttystate.ttyflags &= ~TTY_CBREAK_ON;
564*7c478bd9Sstevel@tonic-gate 	if (ttystate.ttyflags == 0) {
565*7c478bd9Sstevel@tonic-gate 		(void) close(ttystate.ttyfile);
566*7c478bd9Sstevel@tonic-gate 	}
567*7c478bd9Sstevel@tonic-gate }
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate /*
571*7c478bd9Sstevel@tonic-gate  * Allocate space for and return a pointer to a string
572*7c478bd9Sstevel@tonic-gate  * on the stack.  If the string is null, create
573*7c478bd9Sstevel@tonic-gate  * an empty string.
574*7c478bd9Sstevel@tonic-gate  * Use destroy_data() to free when no longer used.
575*7c478bd9Sstevel@tonic-gate  */
576*7c478bd9Sstevel@tonic-gate char *
577*7c478bd9Sstevel@tonic-gate alloc_string(s)
578*7c478bd9Sstevel@tonic-gate 	char	*s;
579*7c478bd9Sstevel@tonic-gate {
580*7c478bd9Sstevel@tonic-gate 	char	*ns;
581*7c478bd9Sstevel@tonic-gate 
582*7c478bd9Sstevel@tonic-gate 	if (s == (char *)NULL) {
583*7c478bd9Sstevel@tonic-gate 		ns = (char *)zalloc(1);
584*7c478bd9Sstevel@tonic-gate 	} else {
585*7c478bd9Sstevel@tonic-gate 		ns = (char *)zalloc(strlen(s) + 1);
586*7c478bd9Sstevel@tonic-gate 		(void) strcpy(ns, s);
587*7c478bd9Sstevel@tonic-gate 	}
588*7c478bd9Sstevel@tonic-gate 	return (ns);
589*7c478bd9Sstevel@tonic-gate }
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate /*
594*7c478bd9Sstevel@tonic-gate  * This function can be used to build up an array of strings
595*7c478bd9Sstevel@tonic-gate  * dynamically, with a trailing NULL to terminate the list.
596*7c478bd9Sstevel@tonic-gate  *
597*7c478bd9Sstevel@tonic-gate  * Parameters:
598*7c478bd9Sstevel@tonic-gate  *	argvlist:  a pointer to the base of the current list.
599*7c478bd9Sstevel@tonic-gate  *		   does not have to be initialized.
600*7c478bd9Sstevel@tonic-gate  *	size:	   pointer to an integer, indicating the number
601*7c478bd9Sstevel@tonic-gate  *		   of string installed in the list.  Must be
602*7c478bd9Sstevel@tonic-gate  *		   initialized to zero.
603*7c478bd9Sstevel@tonic-gate  *	alloc:	   pointer to an integer, indicating the amount
604*7c478bd9Sstevel@tonic-gate  *		   of space allocated.  Must be initialized to
605*7c478bd9Sstevel@tonic-gate  *		   zero.  For efficiency, we allocate the list
606*7c478bd9Sstevel@tonic-gate  *		   in chunks and use it piece-by-piece.
607*7c478bd9Sstevel@tonic-gate  *	str:	   the string to be inserted in the list.
608*7c478bd9Sstevel@tonic-gate  *		   A copy of the string is malloc'ed, and
609*7c478bd9Sstevel@tonic-gate  *		   appended at the end of the list.
610*7c478bd9Sstevel@tonic-gate  * Returns:
611*7c478bd9Sstevel@tonic-gate  *	a pointer to the possibly-moved argvlist.
612*7c478bd9Sstevel@tonic-gate  *
613*7c478bd9Sstevel@tonic-gate  * No attempt to made to free unused memory when the list is
614*7c478bd9Sstevel@tonic-gate  * completed, although this would not be hard to do.  For
615*7c478bd9Sstevel@tonic-gate  * reasonably small lists, this should suffice.
616*7c478bd9Sstevel@tonic-gate  */
617*7c478bd9Sstevel@tonic-gate #define	INITIAL_LISTSIZE	32
618*7c478bd9Sstevel@tonic-gate #define	INCR_LISTSIZE		32
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate char **
621*7c478bd9Sstevel@tonic-gate build_argvlist(argvlist, size, alloc, str)
622*7c478bd9Sstevel@tonic-gate 	char	**argvlist;
623*7c478bd9Sstevel@tonic-gate 	int	*size;
624*7c478bd9Sstevel@tonic-gate 	int	*alloc;
625*7c478bd9Sstevel@tonic-gate 	char	*str;
626*7c478bd9Sstevel@tonic-gate {
627*7c478bd9Sstevel@tonic-gate 	if (*size + 2 > *alloc) {
628*7c478bd9Sstevel@tonic-gate 		if (*alloc == 0) {
629*7c478bd9Sstevel@tonic-gate 			*alloc = INITIAL_LISTSIZE;
630*7c478bd9Sstevel@tonic-gate 			argvlist = (char **)
631*7c478bd9Sstevel@tonic-gate 				zalloc(sizeof (char *) * (*alloc));
632*7c478bd9Sstevel@tonic-gate 		} else {
633*7c478bd9Sstevel@tonic-gate 			*alloc += INCR_LISTSIZE;
634*7c478bd9Sstevel@tonic-gate 			argvlist = (char **)
635*7c478bd9Sstevel@tonic-gate 				rezalloc((void *) argvlist,
636*7c478bd9Sstevel@tonic-gate 				sizeof (char *) * (*alloc));
637*7c478bd9Sstevel@tonic-gate 		}
638*7c478bd9Sstevel@tonic-gate 	}
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	argvlist[*size] = alloc_string(str);
641*7c478bd9Sstevel@tonic-gate 	*size += 1;
642*7c478bd9Sstevel@tonic-gate 	argvlist[*size] = NULL;
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate 	return (argvlist);
645*7c478bd9Sstevel@tonic-gate }
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate /*
649*7c478bd9Sstevel@tonic-gate  * Useful parsing macros
650*7c478bd9Sstevel@tonic-gate  */
651*7c478bd9Sstevel@tonic-gate #define	must_be(s, c)		if (*s++ != c) return (0)
652*7c478bd9Sstevel@tonic-gate #define	skip_digits(s)		while (isdigit(*s)) s++
653*7c478bd9Sstevel@tonic-gate /* Parsing macro below is created to handle fabric devices which contains */
654*7c478bd9Sstevel@tonic-gate /* upper hex digits like c2t210000203708B8CEd0s0.			  */
655*7c478bd9Sstevel@tonic-gate /* To get the target id(tid) the digit and hex upper digit need to	  */
656*7c478bd9Sstevel@tonic-gate /* be processed.							  */
657*7c478bd9Sstevel@tonic-gate #define	skip_digit_or_hexupper(s)	while (isdigit(*s) || \
658*7c478bd9Sstevel@tonic-gate 					(isxdigit(*s) && isupper(*s))) s++
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate /*
661*7c478bd9Sstevel@tonic-gate  * Return true if a device name matches the conventions
662*7c478bd9Sstevel@tonic-gate  * for the particular system.
663*7c478bd9Sstevel@tonic-gate  */
664*7c478bd9Sstevel@tonic-gate int
665*7c478bd9Sstevel@tonic-gate conventional_name(char *name)
666*7c478bd9Sstevel@tonic-gate {
667*7c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
668*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
669*7c478bd9Sstevel@tonic-gate 	if (*name == 't') {
670*7c478bd9Sstevel@tonic-gate 		name++;
671*7c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
672*7c478bd9Sstevel@tonic-gate 	}
673*7c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
674*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
675*7c478bd9Sstevel@tonic-gate 	must_be(name, 's');
676*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
677*7c478bd9Sstevel@tonic-gate 	return (*name == 0);
678*7c478bd9Sstevel@tonic-gate }
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate /*
681*7c478bd9Sstevel@tonic-gate  * Return true if a device name matches the intel physical name conventions
682*7c478bd9Sstevel@tonic-gate  * for the particular system.
683*7c478bd9Sstevel@tonic-gate  */
684*7c478bd9Sstevel@tonic-gate int
685*7c478bd9Sstevel@tonic-gate fdisk_physical_name(char *name)
686*7c478bd9Sstevel@tonic-gate {
687*7c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
688*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
689*7c478bd9Sstevel@tonic-gate 	if (*name == 't') {
690*7c478bd9Sstevel@tonic-gate 		name++;
691*7c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
692*7c478bd9Sstevel@tonic-gate 	}
693*7c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
694*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
695*7c478bd9Sstevel@tonic-gate 	must_be(name, 'p');
696*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
697*7c478bd9Sstevel@tonic-gate 	return (*name == 0);
698*7c478bd9Sstevel@tonic-gate }
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate /*
701*7c478bd9Sstevel@tonic-gate  * Return true if a device name matches the conventions
702*7c478bd9Sstevel@tonic-gate  * for a "whole disk" name for the particular system.
703*7c478bd9Sstevel@tonic-gate  * The name in this case must match exactly that which
704*7c478bd9Sstevel@tonic-gate  * would appear in the device directory itself.
705*7c478bd9Sstevel@tonic-gate  */
706*7c478bd9Sstevel@tonic-gate int
707*7c478bd9Sstevel@tonic-gate whole_disk_name(name)
708*7c478bd9Sstevel@tonic-gate 	char	*name;
709*7c478bd9Sstevel@tonic-gate {
710*7c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
711*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
712*7c478bd9Sstevel@tonic-gate 	if (*name == 't') {
713*7c478bd9Sstevel@tonic-gate 		name++;
714*7c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
715*7c478bd9Sstevel@tonic-gate 	}
716*7c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
717*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
718*7c478bd9Sstevel@tonic-gate 	must_be(name, 's');
719*7c478bd9Sstevel@tonic-gate 	must_be(name, '2');
720*7c478bd9Sstevel@tonic-gate 	return (*name == 0);
721*7c478bd9Sstevel@tonic-gate }
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate /*
725*7c478bd9Sstevel@tonic-gate  * Return true if a name is in the internal canonical form
726*7c478bd9Sstevel@tonic-gate  */
727*7c478bd9Sstevel@tonic-gate int
728*7c478bd9Sstevel@tonic-gate canonical_name(name)
729*7c478bd9Sstevel@tonic-gate 	char	*name;
730*7c478bd9Sstevel@tonic-gate {
731*7c478bd9Sstevel@tonic-gate 	must_be(name, 'c');
732*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
733*7c478bd9Sstevel@tonic-gate 	if (*name == 't') {
734*7c478bd9Sstevel@tonic-gate 		name++;
735*7c478bd9Sstevel@tonic-gate 		skip_digit_or_hexupper(name);
736*7c478bd9Sstevel@tonic-gate 	}
737*7c478bd9Sstevel@tonic-gate 	must_be(name, 'd');
738*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
739*7c478bd9Sstevel@tonic-gate 	return (*name == 0);
740*7c478bd9Sstevel@tonic-gate }
741*7c478bd9Sstevel@tonic-gate 
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate /*
744*7c478bd9Sstevel@tonic-gate  * Return true if a name is in the internal canonical form for 4.x
745*7c478bd9Sstevel@tonic-gate  * Used to support 4.x naming conventions under 5.0.
746*7c478bd9Sstevel@tonic-gate  */
747*7c478bd9Sstevel@tonic-gate int
748*7c478bd9Sstevel@tonic-gate canonical4x_name(name)
749*7c478bd9Sstevel@tonic-gate 	char	*name;
750*7c478bd9Sstevel@tonic-gate {
751*7c478bd9Sstevel@tonic-gate 	char    **p;
752*7c478bd9Sstevel@tonic-gate 	int	i;
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	p = disk_4x_identifiers;
755*7c478bd9Sstevel@tonic-gate 	for (i = N_DISK_4X_IDS; i > 0; i--, p++) {
756*7c478bd9Sstevel@tonic-gate 		if (match_substr(name, *p)) {
757*7c478bd9Sstevel@tonic-gate 			name += strlen(*p);
758*7c478bd9Sstevel@tonic-gate 			break;
759*7c478bd9Sstevel@tonic-gate 		}
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 	if (i == 0)
762*7c478bd9Sstevel@tonic-gate 		return (0);
763*7c478bd9Sstevel@tonic-gate 	skip_digits(name);
764*7c478bd9Sstevel@tonic-gate 	return (*name == 0);
765*7c478bd9Sstevel@tonic-gate }
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate /*
769*7c478bd9Sstevel@tonic-gate  * Map a conventional name into the internal canonical form:
770*7c478bd9Sstevel@tonic-gate  *
771*7c478bd9Sstevel@tonic-gate  *	/dev/rdsk/c0t0d0s0 -> c0t0d0
772*7c478bd9Sstevel@tonic-gate  */
773*7c478bd9Sstevel@tonic-gate void
774*7c478bd9Sstevel@tonic-gate canonicalize_name(dst, src)
775*7c478bd9Sstevel@tonic-gate 	char	*dst;
776*7c478bd9Sstevel@tonic-gate 	char	*src;
777*7c478bd9Sstevel@tonic-gate {
778*7c478bd9Sstevel@tonic-gate 	char	*s;
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 	/*
781*7c478bd9Sstevel@tonic-gate 	 * Copy from the 'c' to the end to the destination string...
782*7c478bd9Sstevel@tonic-gate 	 */
783*7c478bd9Sstevel@tonic-gate 	s = strchr(src, 'c');
784*7c478bd9Sstevel@tonic-gate 	if (s != NULL) {
785*7c478bd9Sstevel@tonic-gate 		(void) strcpy(dst, s);
786*7c478bd9Sstevel@tonic-gate 		/*
787*7c478bd9Sstevel@tonic-gate 		 * Remove the trailing slice (partition) reference
788*7c478bd9Sstevel@tonic-gate 		 */
789*7c478bd9Sstevel@tonic-gate 		s = dst + strlen(dst) - 2;
790*7c478bd9Sstevel@tonic-gate 		if (*s == 's') {
791*7c478bd9Sstevel@tonic-gate 			*s = 0;
792*7c478bd9Sstevel@tonic-gate 		}
793*7c478bd9Sstevel@tonic-gate 	} else {
794*7c478bd9Sstevel@tonic-gate 		*dst = 0;	/* be tolerant of garbage input */
795*7c478bd9Sstevel@tonic-gate 	}
796*7c478bd9Sstevel@tonic-gate }
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate /*
800*7c478bd9Sstevel@tonic-gate  * Return true if we find an occurance of s2 at the
801*7c478bd9Sstevel@tonic-gate  * beginning of s1.  We don't have to match all of
802*7c478bd9Sstevel@tonic-gate  * s1, but we do have to match all of s2
803*7c478bd9Sstevel@tonic-gate  */
804*7c478bd9Sstevel@tonic-gate int
805*7c478bd9Sstevel@tonic-gate match_substr(s1, s2)
806*7c478bd9Sstevel@tonic-gate 	char    *s1;
807*7c478bd9Sstevel@tonic-gate 	char    *s2;
808*7c478bd9Sstevel@tonic-gate {
809*7c478bd9Sstevel@tonic-gate 	while (*s2 != 0) {
810*7c478bd9Sstevel@tonic-gate 		if (*s1++ != *s2++)
811*7c478bd9Sstevel@tonic-gate 		return (0);
812*7c478bd9Sstevel@tonic-gate 	}
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	return (1);
815*7c478bd9Sstevel@tonic-gate }
816*7c478bd9Sstevel@tonic-gate 
817*7c478bd9Sstevel@tonic-gate 
818*7c478bd9Sstevel@tonic-gate /*
819*7c478bd9Sstevel@tonic-gate  * Dump a structure in hexadecimal, for diagnostic purposes
820*7c478bd9Sstevel@tonic-gate  */
821*7c478bd9Sstevel@tonic-gate #define	BYTES_PER_LINE		16
822*7c478bd9Sstevel@tonic-gate 
823*7c478bd9Sstevel@tonic-gate void
824*7c478bd9Sstevel@tonic-gate dump(hdr, src, nbytes, format)
825*7c478bd9Sstevel@tonic-gate 	char	*hdr;
826*7c478bd9Sstevel@tonic-gate 	caddr_t	src;
827*7c478bd9Sstevel@tonic-gate 	int	nbytes;
828*7c478bd9Sstevel@tonic-gate 	int	format;
829*7c478bd9Sstevel@tonic-gate {
830*7c478bd9Sstevel@tonic-gate 	int	i;
831*7c478bd9Sstevel@tonic-gate 	int	n;
832*7c478bd9Sstevel@tonic-gate 	char	*p;
833*7c478bd9Sstevel@tonic-gate 	char	s[256];
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 	assert(format == HEX_ONLY || format == HEX_ASCII);
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate 	(void) strcpy(s, hdr);
838*7c478bd9Sstevel@tonic-gate 	for (p = s; *p; p++) {
839*7c478bd9Sstevel@tonic-gate 		*p = ' ';
840*7c478bd9Sstevel@tonic-gate 	}
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 	p = hdr;
843*7c478bd9Sstevel@tonic-gate 	while (nbytes > 0) {
844*7c478bd9Sstevel@tonic-gate 		err_print("%s", p);
845*7c478bd9Sstevel@tonic-gate 		p = s;
846*7c478bd9Sstevel@tonic-gate 		n = min(nbytes, BYTES_PER_LINE);
847*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
848*7c478bd9Sstevel@tonic-gate 			err_print("%02x ", src[i] & 0xff);
849*7c478bd9Sstevel@tonic-gate 		}
850*7c478bd9Sstevel@tonic-gate 		if (format == HEX_ASCII) {
851*7c478bd9Sstevel@tonic-gate 			for (i = BYTES_PER_LINE-n; i > 0; i--) {
852*7c478bd9Sstevel@tonic-gate 				err_print("   ");
853*7c478bd9Sstevel@tonic-gate 			}
854*7c478bd9Sstevel@tonic-gate 			err_print("    ");
855*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < n; i++) {
856*7c478bd9Sstevel@tonic-gate 				err_print("%c",
857*7c478bd9Sstevel@tonic-gate 					isprint(src[i]) ? src[i] : '.');
858*7c478bd9Sstevel@tonic-gate 			}
859*7c478bd9Sstevel@tonic-gate 		}
860*7c478bd9Sstevel@tonic-gate 		err_print("\n");
861*7c478bd9Sstevel@tonic-gate 		nbytes -= n;
862*7c478bd9Sstevel@tonic-gate 		src += n;
863*7c478bd9Sstevel@tonic-gate 	}
864*7c478bd9Sstevel@tonic-gate }
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate float
868*7c478bd9Sstevel@tonic-gate bn2mb(uint64_t nblks)
869*7c478bd9Sstevel@tonic-gate {
870*7c478bd9Sstevel@tonic-gate 	float	n;
871*7c478bd9Sstevel@tonic-gate 
872*7c478bd9Sstevel@tonic-gate 	n = (float)nblks / 1024.0;
873*7c478bd9Sstevel@tonic-gate 	return ((n / 1024.0) * DEV_BSIZE);
874*7c478bd9Sstevel@tonic-gate }
875*7c478bd9Sstevel@tonic-gate 
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate uint_t
878*7c478bd9Sstevel@tonic-gate mb2bn(float mb)
879*7c478bd9Sstevel@tonic-gate {
880*7c478bd9Sstevel@tonic-gate 	uint_t	n;
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate 	n = (uint_t)(mb * 1024.0 * (1024.0 / DEV_BSIZE));
883*7c478bd9Sstevel@tonic-gate 	return (n);
884*7c478bd9Sstevel@tonic-gate }
885*7c478bd9Sstevel@tonic-gate 
886*7c478bd9Sstevel@tonic-gate float
887*7c478bd9Sstevel@tonic-gate bn2gb(uint64_t nblks)
888*7c478bd9Sstevel@tonic-gate {
889*7c478bd9Sstevel@tonic-gate 	float	n;
890*7c478bd9Sstevel@tonic-gate 
891*7c478bd9Sstevel@tonic-gate 	n = (float)nblks / (1024.0 * 1024.0);
892*7c478bd9Sstevel@tonic-gate 	return ((n/1024.0) * DEV_BSIZE);
893*7c478bd9Sstevel@tonic-gate 
894*7c478bd9Sstevel@tonic-gate }
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate float
897*7c478bd9Sstevel@tonic-gate bn2tb(uint64_t nblks)
898*7c478bd9Sstevel@tonic-gate {
899*7c478bd9Sstevel@tonic-gate 	float	n;
900*7c478bd9Sstevel@tonic-gate 
901*7c478bd9Sstevel@tonic-gate 	n = (float)nblks / (1024.0 * 1024.0 * 1024.0);
902*7c478bd9Sstevel@tonic-gate 	return ((n/1024.0) * DEV_BSIZE);
903*7c478bd9Sstevel@tonic-gate }
904*7c478bd9Sstevel@tonic-gate 
905*7c478bd9Sstevel@tonic-gate uint_t
906*7c478bd9Sstevel@tonic-gate gb2bn(float gb)
907*7c478bd9Sstevel@tonic-gate {
908*7c478bd9Sstevel@tonic-gate 	uint_t	n;
909*7c478bd9Sstevel@tonic-gate 
910*7c478bd9Sstevel@tonic-gate 	n = (uint_t)(gb * 1024.0 * 1024.0 * (1024.0 / DEV_BSIZE));
911*7c478bd9Sstevel@tonic-gate 	return (n);
912*7c478bd9Sstevel@tonic-gate }
913*7c478bd9Sstevel@tonic-gate 
914*7c478bd9Sstevel@tonic-gate /*
915*7c478bd9Sstevel@tonic-gate  * This routine finds out the number of lines (rows) in a terminal
916*7c478bd9Sstevel@tonic-gate  * window. The default value of TTY_LINES is returned on error.
917*7c478bd9Sstevel@tonic-gate  */
918*7c478bd9Sstevel@tonic-gate int
919*7c478bd9Sstevel@tonic-gate get_tty_lines()
920*7c478bd9Sstevel@tonic-gate {
921*7c478bd9Sstevel@tonic-gate 	int	tty_lines = TTY_LINES;
922*7c478bd9Sstevel@tonic-gate 	struct	winsize	winsize;
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate 	if ((option_f == (char *)NULL) && isatty(0) == 1 && isatty(1) == 1) {
925*7c478bd9Sstevel@tonic-gate 		/*
926*7c478bd9Sstevel@tonic-gate 		 * We have a real terminal for std input and output
927*7c478bd9Sstevel@tonic-gate 		 */
928*7c478bd9Sstevel@tonic-gate 		winsize.ws_row = 0;
929*7c478bd9Sstevel@tonic-gate 		if (ioctl(1, TIOCGWINSZ, &winsize) == 0) {
930*7c478bd9Sstevel@tonic-gate 			if (winsize.ws_row > 2) {
931*7c478bd9Sstevel@tonic-gate 				/*
932*7c478bd9Sstevel@tonic-gate 				 * Should be atleast 2 lines, for division
933*7c478bd9Sstevel@tonic-gate 				 * by (tty_lines - 1, tty_lines - 2) to work.
934*7c478bd9Sstevel@tonic-gate 				 */
935*7c478bd9Sstevel@tonic-gate 				tty_lines = winsize.ws_row;
936*7c478bd9Sstevel@tonic-gate 			}
937*7c478bd9Sstevel@tonic-gate 		}
938*7c478bd9Sstevel@tonic-gate 	}
939*7c478bd9Sstevel@tonic-gate 	return (tty_lines);
940*7c478bd9Sstevel@tonic-gate }
941