xref: /titanic_41/usr/src/cmd/saf/util.c (revision 34e485807cef99a975f8962a04f4b7d1aa3529fe)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
25*34e48580Sdp /*
26*34e48580Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*34e48580Sdp  * Use is subject to license terms.
28*34e48580Sdp  */
297c478bd9Sstevel@tonic-gate 
30*34e48580Sdp #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
34*34e48580Sdp #include <stdlib.h>
35*34e48580Sdp #include <strings.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include "extern.h"
407c478bd9Sstevel@tonic-gate #include "misc.h"
417c478bd9Sstevel@tonic-gate #include <sac.h>
427c478bd9Sstevel@tonic-gate #include "structs.h"
437c478bd9Sstevel@tonic-gate #ifdef SAC
447c478bd9Sstevel@tonic-gate #include "msgs.h"
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate char	Comment[SIZE];	/* place holder for comments */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * nexttok - return next token, essentially a strtok, but it can
527c478bd9Sstevel@tonic-gate  *	deal with null fields and strtok can not
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *	args:	str - the string to be examined, NULL if we should
557c478bd9Sstevel@tonic-gate  *		      examine the remembered string
567c478bd9Sstevel@tonic-gate  *		delim - the list of valid delimiters
577c478bd9Sstevel@tonic-gate  *		ros - rest of string flag (1 for rest of string, 0 for
587c478bd9Sstevel@tonic-gate  *		      normal processing)
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate char *
nexttok(str,delim,ros)637c478bd9Sstevel@tonic-gate nexttok(str, delim, ros)
647c478bd9Sstevel@tonic-gate char *str;
657c478bd9Sstevel@tonic-gate register char *delim;
667c478bd9Sstevel@tonic-gate int ros;
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	static char *savep;	/* the remembered string */
697c478bd9Sstevel@tonic-gate 	register char *p;	/* pointer to start of token */
707c478bd9Sstevel@tonic-gate 	register char *ep;	/* pointer to end of token */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	p = (str == NULL) ? savep : str ;
737c478bd9Sstevel@tonic-gate 	if (ros)
747c478bd9Sstevel@tonic-gate 		return(p);
757c478bd9Sstevel@tonic-gate 	if (p == NULL)
767c478bd9Sstevel@tonic-gate 		return(NULL);
777c478bd9Sstevel@tonic-gate 	ep = strpbrk(p, delim);
787c478bd9Sstevel@tonic-gate 	if (ep == NULL) {
797c478bd9Sstevel@tonic-gate 		savep = NULL;
807c478bd9Sstevel@tonic-gate 		return(p);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	savep = ep + 1;
837c478bd9Sstevel@tonic-gate 	*ep = '\0';
847c478bd9Sstevel@tonic-gate 	return(p);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * parse - parse a line from _sactab.  This routine will return if the parse
907c478bd9Sstevel@tonic-gate  *		was successful, otherwise it will output an error and exit.
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  *	args:	p - pointer to the data read from the file
937c478bd9Sstevel@tonic-gate  *		sp - pointer to a structure in which the separated fields
947c478bd9Sstevel@tonic-gate  *		     are placed
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *	A line in the file has the following format:
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  *	tag:type:flags:restart_count:command_string	#comment
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate void
parse(p,sp)1037c478bd9Sstevel@tonic-gate parse(p, sp)
1047c478bd9Sstevel@tonic-gate register char *p;
1057c478bd9Sstevel@tonic-gate register struct sactab *sp;
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	char scratch[SIZE];	/* a scratch buffer */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * get the PM tag
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	p = nexttok(p, DELIM, FALSE);
1147c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1157c478bd9Sstevel@tonic-gate # ifdef SAC
1167c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1177c478bd9Sstevel@tonic-gate # else
1187c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1197c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1207c478bd9Sstevel@tonic-gate # endif
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (strlen(p) > PMTAGSIZE) {
1237c478bd9Sstevel@tonic-gate 		p[PMTAGSIZE] = '\0';
1247c478bd9Sstevel@tonic-gate # ifdef SAC
1257c478bd9Sstevel@tonic-gate 		(void) sprintf(scratch, "tag too long, truncated to <%s>", p);
1267c478bd9Sstevel@tonic-gate 		log(scratch);
1277c478bd9Sstevel@tonic-gate # else
1287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "tag too long, truncated to <%s>", p);
1297c478bd9Sstevel@tonic-gate # endif
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_tag, p);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * get the PM type
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1387c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1397c478bd9Sstevel@tonic-gate # ifdef SAC
1407c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1417c478bd9Sstevel@tonic-gate # else
1427c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1437c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1447c478bd9Sstevel@tonic-gate # endif
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	if (strlen(p) > PMTYPESIZE) {
1477c478bd9Sstevel@tonic-gate 		p[PMTYPESIZE] = '\0';
1487c478bd9Sstevel@tonic-gate # ifdef SAC
1497c478bd9Sstevel@tonic-gate 		(void) sprintf(scratch, "type too long, truncated to <%s>", p);
1507c478bd9Sstevel@tonic-gate 		log(scratch);
1517c478bd9Sstevel@tonic-gate # else
1527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "type too long, truncated to <%s>", p);
1537c478bd9Sstevel@tonic-gate # endif
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_type, p);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * get the flags
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1627c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1637c478bd9Sstevel@tonic-gate # ifdef SAC
1647c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1657c478bd9Sstevel@tonic-gate # else
1667c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1677c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1687c478bd9Sstevel@tonic-gate # endif
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	sp->sc_flags = 0;
1717c478bd9Sstevel@tonic-gate 	while (*p) {
1727c478bd9Sstevel@tonic-gate 		switch (*p++) {
1737c478bd9Sstevel@tonic-gate 		case 'd':
1747c478bd9Sstevel@tonic-gate 			sp->sc_flags |= D_FLAG;
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		case 'x':
1777c478bd9Sstevel@tonic-gate 			sp->sc_flags |= X_FLAG;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 		default:
1807c478bd9Sstevel@tonic-gate 			(void) sprintf(scratch, "Unrecognized flag <%c>", *(p - 1));
1817c478bd9Sstevel@tonic-gate # ifdef SAC
1827c478bd9Sstevel@tonic-gate 			log(scratch);
1837c478bd9Sstevel@tonic-gate # else
1847c478bd9Sstevel@tonic-gate 			Saferrno = E_SAFERR;
1857c478bd9Sstevel@tonic-gate 			error(scratch);
1867c478bd9Sstevel@tonic-gate # endif
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * get the restart count
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1967c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1977c478bd9Sstevel@tonic-gate # ifdef SAC
1987c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1997c478bd9Sstevel@tonic-gate # else
2007c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2017c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
2027c478bd9Sstevel@tonic-gate # endif
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	sp->sc_rsmax = atoi(p);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * get the command string
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
2117c478bd9Sstevel@tonic-gate 	if (p == NULL) {
2127c478bd9Sstevel@tonic-gate # ifdef SAC
2137c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
2147c478bd9Sstevel@tonic-gate # else
2157c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2167c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
2177c478bd9Sstevel@tonic-gate # endif
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if ((sp->sc_cmd = malloc((unsigned) (strlen(p) + 1))) == NULL) {
2207c478bd9Sstevel@tonic-gate # ifdef SAC
2217c478bd9Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
2227c478bd9Sstevel@tonic-gate # else
2237c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2247c478bd9Sstevel@tonic-gate 		error("malloc failed");
2257c478bd9Sstevel@tonic-gate # endif
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_cmd, p);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * remember the comment string
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if ((sp->sc_comment = malloc((unsigned) (strlen(Comment) + 1))) == NULL) {
2347c478bd9Sstevel@tonic-gate # ifdef SAC
2357c478bd9Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
2367c478bd9Sstevel@tonic-gate # else
2377c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2387c478bd9Sstevel@tonic-gate 		error("malloc failed");
2397c478bd9Sstevel@tonic-gate # endif
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_comment, Comment);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * trim - remove comments, trim off trailing white space, done in place
2477c478bd9Sstevel@tonic-gate  *	args:	p - string to be acted upon
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate char *
trim(p)2517c478bd9Sstevel@tonic-gate trim(p)
2527c478bd9Sstevel@tonic-gate register char *p;
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	register char *tp;	/* temp pointer */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * remove comments, if any, but remember them for later
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	tp = strchr(p, COMMENT);
2617c478bd9Sstevel@tonic-gate 	Comment[0] = '\0';
2627c478bd9Sstevel@tonic-gate 	if (tp) {
2637c478bd9Sstevel@tonic-gate 		(void) strcpy(Comment, tp + 1);	/* skip the '#' */
2647c478bd9Sstevel@tonic-gate 		*tp = '\0';
2657c478bd9Sstevel@tonic-gate 		tp = strchr(Comment, '\n');
2667c478bd9Sstevel@tonic-gate 		if (tp)
2677c478bd9Sstevel@tonic-gate 			*tp ='\0';
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * remove trailing whitespace, if any
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
2757c478bd9Sstevel@tonic-gate 		*tp = '\0';
2767c478bd9Sstevel@tonic-gate 	return(p);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * pstate - put port monitor state into intelligible form for output
2827c478bd9Sstevel@tonic-gate  *	SSTATE is only used by sacadm
2837c478bd9Sstevel@tonic-gate  *
2847c478bd9Sstevel@tonic-gate  *	args:	state - binary representation of state
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate char *
pstate(unchar state)288*34e48580Sdp pstate(unchar state)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	switch (state) {
2917c478bd9Sstevel@tonic-gate 	case NOTRUNNING:
2927c478bd9Sstevel@tonic-gate 		return("NOTRUNNING");
2937c478bd9Sstevel@tonic-gate 	case STARTING:
2947c478bd9Sstevel@tonic-gate 		return("STARTING");
2957c478bd9Sstevel@tonic-gate 	case ENABLED:
2967c478bd9Sstevel@tonic-gate 		return("ENABLED");
2977c478bd9Sstevel@tonic-gate 	case DISABLED:
2987c478bd9Sstevel@tonic-gate 		return("DISABLED");
2997c478bd9Sstevel@tonic-gate 	case STOPPING:
3007c478bd9Sstevel@tonic-gate 		return("STOPPING");
3017c478bd9Sstevel@tonic-gate 	case FAILED:
3027c478bd9Sstevel@tonic-gate 		return("FAILED");
3037c478bd9Sstevel@tonic-gate 	case UNKNOWN:
3047c478bd9Sstevel@tonic-gate 		return("UNKNOWN");
3057c478bd9Sstevel@tonic-gate # ifndef SAC
3067c478bd9Sstevel@tonic-gate 	case SSTATE:
3077c478bd9Sstevel@tonic-gate 		return("NO_SAC");
3087c478bd9Sstevel@tonic-gate # endif
3097c478bd9Sstevel@tonic-gate 	default:
3107c478bd9Sstevel@tonic-gate # ifdef SAC
3117c478bd9Sstevel@tonic-gate 		error(E_BADSTATE, EXIT);
3127c478bd9Sstevel@tonic-gate # else
3137c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
3147c478bd9Sstevel@tonic-gate 		error("Improper message from SAC\n");
3157c478bd9Sstevel@tonic-gate # endif
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
318*34e48580Sdp 	return (NULL);
3197c478bd9Sstevel@tonic-gate }
320