xref: /titanic_53/usr/src/cmd/saf/util.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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"       /* SVr4.0 1.8*/
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate # include <stdio.h>
30*7c478bd9Sstevel@tonic-gate # include <ctype.h>
31*7c478bd9Sstevel@tonic-gate # include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate # include <unistd.h>
33*7c478bd9Sstevel@tonic-gate # include "extern.h"
34*7c478bd9Sstevel@tonic-gate # include "misc.h"
35*7c478bd9Sstevel@tonic-gate # include <sac.h>
36*7c478bd9Sstevel@tonic-gate # include "structs.h"
37*7c478bd9Sstevel@tonic-gate # ifdef SAC
38*7c478bd9Sstevel@tonic-gate # include "msgs.h"
39*7c478bd9Sstevel@tonic-gate # endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate char	Comment[SIZE];	/* place holder for comments */
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * nexttok - return next token, essentially a strtok, but it can
46*7c478bd9Sstevel@tonic-gate  *	deal with null fields and strtok can not
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  *	args:	str - the string to be examined, NULL if we should
49*7c478bd9Sstevel@tonic-gate  *		      examine the remembered string
50*7c478bd9Sstevel@tonic-gate  *		delim - the list of valid delimiters
51*7c478bd9Sstevel@tonic-gate  *		ros - rest of string flag (1 for rest of string, 0 for
52*7c478bd9Sstevel@tonic-gate  *		      normal processing)
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate char *
57*7c478bd9Sstevel@tonic-gate nexttok(str, delim, ros)
58*7c478bd9Sstevel@tonic-gate char *str;
59*7c478bd9Sstevel@tonic-gate register char *delim;
60*7c478bd9Sstevel@tonic-gate int ros;
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	static char *savep;	/* the remembered string */
63*7c478bd9Sstevel@tonic-gate 	register char *p;	/* pointer to start of token */
64*7c478bd9Sstevel@tonic-gate 	register char *ep;	/* pointer to end of token */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	p = (str == NULL) ? savep : str ;
67*7c478bd9Sstevel@tonic-gate 	if (ros)
68*7c478bd9Sstevel@tonic-gate 		return(p);
69*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
70*7c478bd9Sstevel@tonic-gate 		return(NULL);
71*7c478bd9Sstevel@tonic-gate 	ep = strpbrk(p, delim);
72*7c478bd9Sstevel@tonic-gate 	if (ep == NULL) {
73*7c478bd9Sstevel@tonic-gate 		savep = NULL;
74*7c478bd9Sstevel@tonic-gate 		return(p);
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 	savep = ep + 1;
77*7c478bd9Sstevel@tonic-gate 	*ep = '\0';
78*7c478bd9Sstevel@tonic-gate 	return(p);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * parse - parse a line from _sactab.  This routine will return if the parse
84*7c478bd9Sstevel@tonic-gate  *		was successful, otherwise it will output an error and exit.
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  *	args:	p - pointer to the data read from the file
87*7c478bd9Sstevel@tonic-gate  *		sp - pointer to a structure in which the separated fields
88*7c478bd9Sstevel@tonic-gate  *		     are placed
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  *	A line in the file has the following format:
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  *	tag:type:flags:restart_count:command_string	#comment
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate void
97*7c478bd9Sstevel@tonic-gate parse(p, sp)
98*7c478bd9Sstevel@tonic-gate register char *p;
99*7c478bd9Sstevel@tonic-gate register struct sactab *sp;
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	char scratch[SIZE];	/* a scratch buffer */
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /*
104*7c478bd9Sstevel@tonic-gate  * get the PM tag
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	p = nexttok(p, DELIM, FALSE);
108*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
109*7c478bd9Sstevel@tonic-gate # ifdef SAC
110*7c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
111*7c478bd9Sstevel@tonic-gate # else
112*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
113*7c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
114*7c478bd9Sstevel@tonic-gate # endif
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	if (strlen(p) > PMTAGSIZE) {
117*7c478bd9Sstevel@tonic-gate 		p[PMTAGSIZE] = '\0';
118*7c478bd9Sstevel@tonic-gate # ifdef SAC
119*7c478bd9Sstevel@tonic-gate 		(void) sprintf(scratch, "tag too long, truncated to <%s>", p);
120*7c478bd9Sstevel@tonic-gate 		log(scratch);
121*7c478bd9Sstevel@tonic-gate # else
122*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "tag too long, truncated to <%s>", p);
123*7c478bd9Sstevel@tonic-gate # endif
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_tag, p);
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * get the PM type
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
132*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
133*7c478bd9Sstevel@tonic-gate # ifdef SAC
134*7c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
135*7c478bd9Sstevel@tonic-gate # else
136*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
137*7c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
138*7c478bd9Sstevel@tonic-gate # endif
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	if (strlen(p) > PMTYPESIZE) {
141*7c478bd9Sstevel@tonic-gate 		p[PMTYPESIZE] = '\0';
142*7c478bd9Sstevel@tonic-gate # ifdef SAC
143*7c478bd9Sstevel@tonic-gate 		(void) sprintf(scratch, "type too long, truncated to <%s>", p);
144*7c478bd9Sstevel@tonic-gate 		log(scratch);
145*7c478bd9Sstevel@tonic-gate # else
146*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "type too long, truncated to <%s>", p);
147*7c478bd9Sstevel@tonic-gate # endif
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_type, p);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate /*
152*7c478bd9Sstevel@tonic-gate  * get the flags
153*7c478bd9Sstevel@tonic-gate  */
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
156*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
157*7c478bd9Sstevel@tonic-gate # ifdef SAC
158*7c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
159*7c478bd9Sstevel@tonic-gate # else
160*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
161*7c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
162*7c478bd9Sstevel@tonic-gate # endif
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	sp->sc_flags = 0;
165*7c478bd9Sstevel@tonic-gate 	while (*p) {
166*7c478bd9Sstevel@tonic-gate 		switch (*p++) {
167*7c478bd9Sstevel@tonic-gate 		case 'd':
168*7c478bd9Sstevel@tonic-gate 			sp->sc_flags |= D_FLAG;
169*7c478bd9Sstevel@tonic-gate 			break;
170*7c478bd9Sstevel@tonic-gate 		case 'x':
171*7c478bd9Sstevel@tonic-gate 			sp->sc_flags |= X_FLAG;
172*7c478bd9Sstevel@tonic-gate 			break;
173*7c478bd9Sstevel@tonic-gate 		default:
174*7c478bd9Sstevel@tonic-gate 			(void) sprintf(scratch, "Unrecognized flag <%c>", *(p - 1));
175*7c478bd9Sstevel@tonic-gate # ifdef SAC
176*7c478bd9Sstevel@tonic-gate 			log(scratch);
177*7c478bd9Sstevel@tonic-gate # else
178*7c478bd9Sstevel@tonic-gate 			Saferrno = E_SAFERR;
179*7c478bd9Sstevel@tonic-gate 			error(scratch);
180*7c478bd9Sstevel@tonic-gate # endif
181*7c478bd9Sstevel@tonic-gate 			break;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate /*
186*7c478bd9Sstevel@tonic-gate  * get the restart count
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
190*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
191*7c478bd9Sstevel@tonic-gate # ifdef SAC
192*7c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
193*7c478bd9Sstevel@tonic-gate # else
194*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
195*7c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
196*7c478bd9Sstevel@tonic-gate # endif
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	sp->sc_rsmax = atoi(p);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate /*
201*7c478bd9Sstevel@tonic-gate  * get the command string
202*7c478bd9Sstevel@tonic-gate  */
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
205*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
206*7c478bd9Sstevel@tonic-gate # ifdef SAC
207*7c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
208*7c478bd9Sstevel@tonic-gate # else
209*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
210*7c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
211*7c478bd9Sstevel@tonic-gate # endif
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	if ((sp->sc_cmd = malloc((unsigned) (strlen(p) + 1))) == NULL) {
214*7c478bd9Sstevel@tonic-gate # ifdef SAC
215*7c478bd9Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
216*7c478bd9Sstevel@tonic-gate # else
217*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
218*7c478bd9Sstevel@tonic-gate 		error("malloc failed");
219*7c478bd9Sstevel@tonic-gate # endif
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_cmd, p);
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate /*
224*7c478bd9Sstevel@tonic-gate  * remember the comment string
225*7c478bd9Sstevel@tonic-gate  */
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if ((sp->sc_comment = malloc((unsigned) (strlen(Comment) + 1))) == NULL) {
228*7c478bd9Sstevel@tonic-gate # ifdef SAC
229*7c478bd9Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
230*7c478bd9Sstevel@tonic-gate # else
231*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
232*7c478bd9Sstevel@tonic-gate 		error("malloc failed");
233*7c478bd9Sstevel@tonic-gate # endif
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_comment, Comment);
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate /*
240*7c478bd9Sstevel@tonic-gate  * trim - remove comments, trim off trailing white space, done in place
241*7c478bd9Sstevel@tonic-gate  *	args:	p - string to be acted upon
242*7c478bd9Sstevel@tonic-gate  */
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate char *
245*7c478bd9Sstevel@tonic-gate trim(p)
246*7c478bd9Sstevel@tonic-gate register char *p;
247*7c478bd9Sstevel@tonic-gate {
248*7c478bd9Sstevel@tonic-gate 	register char *tp;	/* temp pointer */
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /*
251*7c478bd9Sstevel@tonic-gate  * remove comments, if any, but remember them for later
252*7c478bd9Sstevel@tonic-gate  */
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	tp = strchr(p, COMMENT);
255*7c478bd9Sstevel@tonic-gate 	Comment[0] = '\0';
256*7c478bd9Sstevel@tonic-gate 	if (tp) {
257*7c478bd9Sstevel@tonic-gate 		(void) strcpy(Comment, tp + 1);	/* skip the '#' */
258*7c478bd9Sstevel@tonic-gate 		*tp = '\0';
259*7c478bd9Sstevel@tonic-gate 		tp = strchr(Comment, '\n');
260*7c478bd9Sstevel@tonic-gate 		if (tp)
261*7c478bd9Sstevel@tonic-gate 			*tp ='\0';
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate /*
265*7c478bd9Sstevel@tonic-gate  * remove trailing whitespace, if any
266*7c478bd9Sstevel@tonic-gate  */
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
269*7c478bd9Sstevel@tonic-gate 		*tp = '\0';
270*7c478bd9Sstevel@tonic-gate 	return(p);
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate /*
275*7c478bd9Sstevel@tonic-gate  * pstate - put port monitor state into intelligible form for output
276*7c478bd9Sstevel@tonic-gate  *	SSTATE is only used by sacadm
277*7c478bd9Sstevel@tonic-gate  *
278*7c478bd9Sstevel@tonic-gate  *	args:	state - binary representation of state
279*7c478bd9Sstevel@tonic-gate  */
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate char *
282*7c478bd9Sstevel@tonic-gate pstate(state)
283*7c478bd9Sstevel@tonic-gate unchar state;
284*7c478bd9Sstevel@tonic-gate {
285*7c478bd9Sstevel@tonic-gate 	switch (state) {
286*7c478bd9Sstevel@tonic-gate 	case NOTRUNNING:
287*7c478bd9Sstevel@tonic-gate 		return("NOTRUNNING");
288*7c478bd9Sstevel@tonic-gate 	case STARTING:
289*7c478bd9Sstevel@tonic-gate 		return("STARTING");
290*7c478bd9Sstevel@tonic-gate 	case ENABLED:
291*7c478bd9Sstevel@tonic-gate 		return("ENABLED");
292*7c478bd9Sstevel@tonic-gate 	case DISABLED:
293*7c478bd9Sstevel@tonic-gate 		return("DISABLED");
294*7c478bd9Sstevel@tonic-gate 	case STOPPING:
295*7c478bd9Sstevel@tonic-gate 		return("STOPPING");
296*7c478bd9Sstevel@tonic-gate 	case FAILED:
297*7c478bd9Sstevel@tonic-gate 		return("FAILED");
298*7c478bd9Sstevel@tonic-gate 	case UNKNOWN:
299*7c478bd9Sstevel@tonic-gate 		return("UNKNOWN");
300*7c478bd9Sstevel@tonic-gate # ifndef SAC
301*7c478bd9Sstevel@tonic-gate 	case SSTATE:
302*7c478bd9Sstevel@tonic-gate 		return("NO_SAC");
303*7c478bd9Sstevel@tonic-gate # endif
304*7c478bd9Sstevel@tonic-gate 	default:
305*7c478bd9Sstevel@tonic-gate # ifdef SAC
306*7c478bd9Sstevel@tonic-gate 		error(E_BADSTATE, EXIT);
307*7c478bd9Sstevel@tonic-gate # else
308*7c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
309*7c478bd9Sstevel@tonic-gate 		error("Improper message from SAC\n");
310*7c478bd9Sstevel@tonic-gate # endif
311*7c478bd9Sstevel@tonic-gate 	}
312*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
313*7c478bd9Sstevel@tonic-gate }
314