17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
23113f423akaplan * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bdstevel@tonic-gate/*        All Rights Reserved   */
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate/*
347c478bdstevel@tonic-gate *  getdev.c
357c478bdstevel@tonic-gate *
367c478bdstevel@tonic-gate *  Contains
377c478bdstevel@tonic-gate *	getdev	Writes on the standard output stream a list of devices
387c478bdstevel@tonic-gate *		that match certain criteria.
397c478bdstevel@tonic-gate */
407c478bdstevel@tonic-gate#include	<sys/types.h>
417c478bdstevel@tonic-gate#include	<stdio.h>
427c478bdstevel@tonic-gate#include	<errno.h>
437c478bdstevel@tonic-gate#include	<stdlib.h>
447c478bdstevel@tonic-gate#include	<string.h>
457c478bdstevel@tonic-gate#include	<fmtmsg.h>
467c478bdstevel@tonic-gate#include	<devmgmt.h>
477c478bdstevel@tonic-gate#include	<devtab.h>
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate/*
517c478bdstevel@tonic-gate *  Local Definitions
527c478bdstevel@tonic-gate *	TRUE		Boolean TRUE value
537c478bdstevel@tonic-gate *	FALSE		Boolean FALSE value
547c478bdstevel@tonic-gate *	EX_OK		Exit Value if all went well
557c478bdstevel@tonic-gate *	EX_ERROR	Exit Value if an error occurred
567c478bdstevel@tonic-gate *	EX_DEVTAB	Exit Value if the device table couldn't be opened
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gate */
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate#ifndef	TRUE
617c478bdstevel@tonic-gate#define	TRUE		(1)
627c478bdstevel@tonic-gate#endif
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate#ifndef	FALSE
657c478bdstevel@tonic-gate#define FALSE		(0)
667c478bdstevel@tonic-gate#endif
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate#define	EX_OK		0
697c478bdstevel@tonic-gate#define	EX_ERROR	1
707c478bdstevel@tonic-gate#define	EX_DEVTAB	2
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate/*
747c478bdstevel@tonic-gate *  Messages:
757c478bdstevel@tonic-gate *	M_USAGE		Usage error
767c478bdstevel@tonic-gate *	M_DEVTAB	Can't open the device table
777c478bdstevel@tonic-gate *	M_NODEV		Device not found in the device table
787c478bdstevel@tonic-gate *	M_ERROR		Unexpected or internal error
797c478bdstevel@tonic-gate */
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate#define	M_USAGE		"usage: getdev [-ae] [criterion [...]] [device [...]]"
827c478bdstevel@tonic-gate#define	M_DEVTAB	"Cannot open the device table: %s"
837c478bdstevel@tonic-gate#define	M_NODEV		"Device not found in the device table: %s"
847c478bdstevel@tonic-gate#define	M_ERROR		"Internal error, errno=%d"
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate/*
887c478bdstevel@tonic-gate *  Local (Static) Definitions and macros
897c478bdstevel@tonic-gate *	buildcriterialist()	Builds the criteria list from the command-line
907c478bdstevel@tonic-gate *	builddevlist()		Builds the device list from the command-line
917c478bdstevel@tonic-gate *	lbl			Buffer for the standard message label
927c478bdstevel@tonic-gate *	txt			Buffer for the standard message text
937c478bdstevel@tonic-gate *	stdmsg(r,l,s,t)		Write a standard message:
947c478bdstevel@tonic-gate *				    r	Recoverability flag
957c478bdstevel@tonic-gate *				    l	Standard label
967c478bdstevel@tonic-gate *				    s	Severity
977c478bdstevel@tonic-gate *				    t	Text
987c478bdstevel@tonic-gate */
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gatestatic	char  **buildcriterialist();
1017c478bdstevel@tonic-gatestatic	char  **builddevlist();
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gatestatic	char	lbl[MM_MXLABELLN+1];
1047c478bdstevel@tonic-gatestatic	char	txt[MM_MXTXTLN+1];
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate#define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate/*
1097c478bdstevel@tonic-gate *  getdev [-ae] [criterion [...]] [device [...]]
1107c478bdstevel@tonic-gate *
1117c478bdstevel@tonic-gate *	This command generates a list of devices that match the
1127c478bdstevel@tonic-gate *	specified criteria.
1137c478bdstevel@tonic-gate *
1147c478bdstevel@tonic-gate *  Options:
1157c478bdstevel@tonic-gate *	-a		A device must meet all of the criteria to be
1167c478bdstevel@tonic-gate *			included in the generated list instead of just
1177c478bdstevel@tonic-gate *			one of the criteria (the "and" flag)
1187c478bdstevel@tonic-gate *	-e		Exclude the devices mentioned from the generated
1197c478bdstevel@tonic-gate *			list.  If this flag is omitted, the devices in the
1207c478bdstevel@tonic-gate *			list are selected from the devices mentioned.
1217c478bdstevel@tonic-gate *
1227c478bdstevel@tonic-gate *  Arguments:
1237c478bdstevel@tonic-gate *	criterion	An <attr><op><value> expression that describes
1247c478bdstevel@tonic-gate *			a device attribute.
1257c478bdstevel@tonic-gate *			<attr>	is a device attribute
1267c478bdstevel@tonic-gate *			<op> 	may be = != : !: indicating equals, does not
1277c478bdstevel@tonic-gate *				equal, is defined, and is not defined
1287c478bdstevel@tonic-gate *				respectively
1297c478bdstevel@tonic-gate *			<value>	is the attribute value.  Currently, the only
1307c478bdstevel@tonic-gate *				value supported for the : and !: operators
1317c478bdstevel@tonic-gate *				is *
1327c478bdstevel@tonic-gate *	device		A device to select for or exclude from the generated
1337c478bdstevel@tonic-gate *			list
1347c478bdstevel@tonic-gate *
1357c478bdstevel@tonic-gate *  Exit values:
1367c478bdstevel@tonic-gate *	EX_OK		All went well
1377c478bdstevel@tonic-gate *	EX_ERROR	An error (syntax, internal, or resource) occurred
1387c478bdstevel@tonic-gate *	EX_DEVTAB	The device-table could not be opened for reading
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gate
141113f423akaplanint
142113f423akaplanmain(int argc, char **argv)
1437c478bdstevel@tonic-gate{
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate	/*
1467c478bdstevel@tonic-gate	 *  Automatic data
1477c478bdstevel@tonic-gate	 */
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gate	char	      **arglist;	/* List of arguments */
1507c478bdstevel@tonic-gate	char	      **criterialist;	/* List of criteria */
1517c478bdstevel@tonic-gate	char	      **devicelist;	/* List of devices to search/ignore */
1527c478bdstevel@tonic-gate	char	      **fitdevlist;	/* List of devices that fit criteria */
1537c478bdstevel@tonic-gate	char	       *cmdname;	/* Simple command name */
1547c478bdstevel@tonic-gate	char	       *device;		/* Device name in the list */
1557c478bdstevel@tonic-gate	char	       *devtab;		/* The device table name */
1567c478bdstevel@tonic-gate	int		exitcode;	/* Value to return to the caller */
1577c478bdstevel@tonic-gate	int		sev;		/* Message severity */
1587c478bdstevel@tonic-gate	int		optchar;	/* Option character (from getopt()) */
1597c478bdstevel@tonic-gate	int		andflag;	/* TRUE if criteria are to be anded */
1607c478bdstevel@tonic-gate	int		excludeflag;	/* TRUE if exclude "devices" lists */
1617c478bdstevel@tonic-gate	int		options;	/* Options to pass to getdev() */
1627c478bdstevel@tonic-gate	int		usageerr;	/* TRUE if syntax error */
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	/* Build the message label from the (simple) command name */
1667c478bdstevel@tonic-gate	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
1677c478bdstevel@tonic-gate	else cmdname = argv[0];
1687c478bdstevel@tonic-gate	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate	/* Write text-component of messages only (goes away in SVR4.1) */
1717c478bdstevel@tonic-gate	(void) putenv("MSGVERB=text");
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	/*
1747c478bdstevel@tonic-gate	 *  Parse the command line:
1757c478bdstevel@tonic-gate	 *	- Options
1767c478bdstevel@tonic-gate	 *	- Selection criteria
1777c478bdstevel@tonic-gate	 *	- Devices to include or exclude
1787c478bdstevel@tonic-gate	 */
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gate	/*
1817c478bdstevel@tonic-gate	 *  Extract options from the command line
1827c478bdstevel@tonic-gate	 */
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate	/* Initializations */
1857c478bdstevel@tonic-gate	andflag = FALSE;	/* No -a -- Or criteria data */
1867c478bdstevel@tonic-gate	excludeflag = FALSE;	/* No -e -- Include only mentioned devices */
1877c478bdstevel@tonic-gate	usageerr = FALSE;	/* No errors on the command line (yet) */
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	/*
1907c478bdstevel@tonic-gate	 *  Loop until all of the command line options have been parced
1917c478bdstevel@tonic-gate	 */
1927c478bdstevel@tonic-gate	opterr = FALSE;			/* Don't let getopt() write messages */
1937c478bdstevel@tonic-gate	while ((optchar = getopt(argc, argv, "ae")) != EOF) switch (optchar) {
1947c478bdstevel@tonic-gate
1957c478bdstevel@tonic-gate	/* -a  List devices that fit all of the criteria listed */
1967c478bdstevel@tonic-gate	case 'a':
1977c478bdstevel@tonic-gate	    if (andflag) usageerr = TRUE;
1987c478bdstevel@tonic-gate	    else andflag = TRUE;
1997c478bdstevel@tonic-gate	    break;
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	/* -e  Exclude those devices mentioned on the command line */
2027c478bdstevel@tonic-gate	case 'e':
2037c478bdstevel@tonic-gate	    if (excludeflag) usageerr = TRUE;
2047c478bdstevel@tonic-gate	    else excludeflag = TRUE;
2057c478bdstevel@tonic-gate	    break;
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gate	/* Default case -- command usage error */
2087c478bdstevel@tonic-gate	case '?':
2097c478bdstevel@tonic-gate	default:
2107c478bdstevel@tonic-gate	    usageerr = TRUE;
2117c478bdstevel@tonic-gate	    break;
2127c478bdstevel@tonic-gate	}
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gate	/* If there is a usage error, write an appropriate message and exit */
2157c478bdstevel@tonic-gate	if (usageerr) {
2167c478bdstevel@tonic-gate	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
2177c478bdstevel@tonic-gate	    exit(EX_ERROR);
2187c478bdstevel@tonic-gate	}
2197c478bdstevel@tonic-gate
2207c478bdstevel@tonic-gate	/* Open the device file (if there's one to be opened) */
2217c478bdstevel@tonic-gate	if (!_opendevtab("r")) {
2227c478bdstevel@tonic-gate	    if (devtab = _devtabpath()) {
2237c478bdstevel@tonic-gate		(void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab);
2247c478bdstevel@tonic-gate		sev = MM_ERROR;
2257c478bdstevel@tonic-gate		exitcode = EX_DEVTAB;
2267c478bdstevel@tonic-gate	    } else {
2277c478bdstevel@tonic-gate		(void) sprintf(txt, M_ERROR, errno);
2287c478bdstevel@tonic-gate		sev = MM_HALT;
2297c478bdstevel@tonic-gate		exitcode = EX_ERROR;
2307c478bdstevel@tonic-gate	    }
2317c478bdstevel@tonic-gate	    stdmsg(MM_NRECOV, lbl, sev, txt);
2327c478bdstevel@tonic-gate	    exit(exitcode);
2337c478bdstevel@tonic-gate	}
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate	/* Build the list of criteria and devices */
2367c478bdstevel@tonic-gate	arglist = argv + optind;
2377c478bdstevel@tonic-gate	criterialist = buildcriterialist(arglist);
2387c478bdstevel@tonic-gate	devicelist = builddevlist(arglist);
2397c478bdstevel@tonic-gate	options = (excludeflag?DTAB_EXCLUDEFLAG:0)|(andflag?DTAB_ANDCRITERIA:0);
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate	/*
2427c478bdstevel@tonic-gate	 *  Get the list of devices that meets the criteria requested.  If we
2437c478bdstevel@tonic-gate	 *  got a list (that might be empty), write that list to the standard
2447c478bdstevel@tonic-gate	 *  output file (stdout).
2457c478bdstevel@tonic-gate	 */
2467c478bdstevel@tonic-gate
2477c478bdstevel@tonic-gate	exitcode = 0;
2487c478bdstevel@tonic-gate	if (!(fitdevlist = getdev(devicelist, criterialist, options))) {
2497c478bdstevel@tonic-gate	    exitcode = 1;
2507c478bdstevel@tonic-gate	}
2517c478bdstevel@tonic-gate	else for (device = *fitdevlist++ ; device ; device = *fitdevlist++)
2527c478bdstevel@tonic-gate		(void) puts(device);
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate	/* Finished */
2557c478bdstevel@tonic-gate	return(exitcode);
2567c478bdstevel@tonic-gate}
2577c478bdstevel@tonic-gate
2587c478bdstevel@tonic-gate/*
2597c478bdstevel@tonic-gate *  char **buildcriterialist(arglist)
2607c478bdstevel@tonic-gate *	char  **arglist
2617c478bdstevel@tonic-gate *
2627c478bdstevel@tonic-gate *	Build a list of pointers to the criterion on the command-line
2637c478bdstevel@tonic-gate *
2647c478bdstevel@tonic-gate *  Arguments:
2657c478bdstevel@tonic-gate *	arglist		The list of arguments on the command-line
2667c478bdstevel@tonic-gate *
2677c478bdstevel@tonic-gate *  Returns:  char **
2687c478bdstevel@tonic-gate *	The address of the first item of the list of criterion on the
2697c478bdstevel@tonic-gate *	command-line.  This is a pointer to malloc()ed space.
2707c478bdstevel@tonic-gate */
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gatestatic char  **
2737c478bdstevel@tonic-gatebuildcriterialist(arglist)
2747c478bdstevel@tonic-gate	char  **arglist;	/* Pointer to the list of argument pointers */
2757c478bdstevel@tonic-gate{
2767c478bdstevel@tonic-gate	/*
2777c478bdstevel@tonic-gate	 *  Automatic data
2787c478bdstevel@tonic-gate	 */
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate	char  **pp;			/* Pointer to a criteria */
2817c478bdstevel@tonic-gate	char  **allocbuf;		/* Pointer to the allocated data */
2827c478bdstevel@tonic-gate	int	ncriteria;		/* Number of criteria found */
2837c478bdstevel@tonic-gate
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate	/*
2867c478bdstevel@tonic-gate	 *  Search the argument list, looking for the end of the list or
2877c478bdstevel@tonic-gate	 *  the first thing that's not a criteria.  (A criteria is a
2887c478bdstevel@tonic-gate	 *  character-string that contains a colon (':') or an equal-sign ('=')
2897c478bdstevel@tonic-gate	 */
2907c478bdstevel@tonic-gate
2917c478bdstevel@tonic-gate	pp = arglist;
2927c478bdstevel@tonic-gate	ncriteria = 0;
2937c478bdstevel@tonic-gate	while (*pp && (strchr(*pp, '=') || strchr(*pp, ':'))) {
2947c478bdstevel@tonic-gate	    ncriteria++;
2957c478bdstevel@tonic-gate	    pp++;
2967c478bdstevel@tonic-gate	}
2977c478bdstevel@tonic-gate
2987c478bdstevel@tonic-gate	if (ncriteria > 0) {
2997c478bdstevel@tonic-gate
3007c478bdstevel@tonic-gate	    /* Allocate space for the list of criteria pointers */
3017c478bdstevel@tonic-gate	    allocbuf = (char **) malloc((ncriteria+1)*sizeof(char **));
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate	    /*
3047c478bdstevel@tonic-gate	     *  Build the list of criteria arguments
3057c478bdstevel@tonic-gate	     */
3067c478bdstevel@tonic-gate	    pp = allocbuf;	/* Beginning of the list */
3077c478bdstevel@tonic-gate	    while (*arglist &&			/* If there's more to do ... */
3087c478bdstevel@tonic-gate		   (strchr(*arglist, '=') ||	/* and it's a = criterion ... */
3097c478bdstevel@tonic-gate		    strchr(*arglist, ':')))	/* or it's a : criterion ... */
3107c478bdstevel@tonic-gate			*pp++ = *arglist++;	/* Include it in the list */
3117c478bdstevel@tonic-gate	    *pp = (char *) NULL;	/* Terminate the list */
3127c478bdstevel@tonic-gate
3137c478bdstevel@tonic-gate	} else allocbuf = (char **) NULL;	/* NO criteria */
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gate
3167c478bdstevel@tonic-gate	return (allocbuf);
3177c478bdstevel@tonic-gate}
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate/*
3207c478bdstevel@tonic-gate *  char **builddevlist(arglist)
3217c478bdstevel@tonic-gate *	char  **arglist
3227c478bdstevel@tonic-gate *
3237c478bdstevel@tonic-gate *	Builds a list of pointers to the devices mentioned on the command-
3247c478bdstevel@tonic-gate *	line and returns the address of that list.
3257c478bdstevel@tonic-gate *
3267c478bdstevel@tonic-gate *  Arguments:
3277c478bdstevel@tonic-gate *	arglist		The address of the list of arguments to the
3287c478bdstevel@tonic-gate *			getdev command.
3297c478bdstevel@tonic-gate *
3307c478bdstevel@tonic-gate *  Returns:  char **
3317c478bdstevel@tonic-gate *	A pointer to the first item in the list of pointers to devices
3327c478bdstevel@tonic-gate *	specified on the command-line
3337c478bdstevel@tonic-gate */
3347c478bdstevel@tonic-gate
3357c478bdstevel@tonic-gatestatic char  **
3367c478bdstevel@tonic-gatebuilddevlist(arglist)
3377c478bdstevel@tonic-gate	char  **arglist;	/* Pointer to the list of pointers to args */
3387c478bdstevel@tonic-gate{
3397c478bdstevel@tonic-gate	/*
3407c478bdstevel@tonic-gate	 *  Automatic data
3417c478bdstevel@tonic-gate	 */
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gate	/*
3447c478bdstevel@tonic-gate	 *  Search the argument list, looking for the end of the list or the
3457c478bdstevel@tonic-gate	 *  first thing that's not a criteria.  It is the first device in the
3467c478bdstevel@tonic-gate	 *  list of devices (if any).
3477c478bdstevel@tonic-gate	 */
3487c478bdstevel@tonic-gate
3497c478bdstevel@tonic-gate	while (*arglist && (strchr(*arglist, '=') || strchr(*arglist, ':'))) arglist++;
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	/* Return a pointer to the argument list. */
3527c478bdstevel@tonic-gate	return(*arglist?arglist:(char **) NULL);
3537c478bdstevel@tonic-gate}
354