xref: /onnv-gate/usr/src/cmd/cfgadm/cfgadm.c (revision 10923:df470fd79c3c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57438SRameshkumar.Ramasamy@Sun.COM  * Common Development and Distribution License (the "License").
67438SRameshkumar.Ramasamy@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
217438SRameshkumar.Ramasamy@Sun.COM 
220Sstevel@tonic-gate /*
23*10923SEvan.Yan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * This is the main program file for the configuration administration
290Sstevel@tonic-gate  * command as set out in manual page cfgadm(1M).  It uses the configuration
300Sstevel@tonic-gate  * administration library interface, libcfgadm, as set out in manual
310Sstevel@tonic-gate  * page config_admin(3X).
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <locale.h>
380Sstevel@tonic-gate #include <langinfo.h>
390Sstevel@tonic-gate #include <time.h>
400Sstevel@tonic-gate #include <assert.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <sys/param.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/openpromio.h>
460Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
470Sstevel@tonic-gate #include <sys/systeminfo.h>
480Sstevel@tonic-gate #include <ctype.h>
497441SRameshkumar.Ramasamy@Sun.COM #include <zone.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <config_admin.h>
520Sstevel@tonic-gate #include "cfgadm.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define	S_FREE(x)	(((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
550Sstevel@tonic-gate #define	GET_DYN(a)	(strstr((a), CFGA_DYN_SEP))
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * forward declarations
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate static char *basename(char *);
600Sstevel@tonic-gate static void cfgadm_error(int, char *);
610Sstevel@tonic-gate static int confirm_interactive(void *, const char *);
620Sstevel@tonic-gate static int confirm_no(void *, const char *);
630Sstevel@tonic-gate static int confirm_yes(void *, const char *);
640Sstevel@tonic-gate static void usage(void);
650Sstevel@tonic-gate static void usage_field(void);
660Sstevel@tonic-gate static int extract_list_suboptions(char *, char **, char **, char **,
670Sstevel@tonic-gate     int *, char **, char **, char **);
680Sstevel@tonic-gate static int message_output(void *appdata_ptr, const char *message);
690Sstevel@tonic-gate static void *config_calloc_check(size_t, size_t);
700Sstevel@tonic-gate static cfga_ap_types_t find_arg_type(const char *);
710Sstevel@tonic-gate static int yesno(char *, char *);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static int compare_ap_id(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
750Sstevel@tonic-gate static int compare_r_state(cfga_list_data_t *, cfga_list_data_t *,
760Sstevel@tonic-gate     match_type_t);
770Sstevel@tonic-gate static int compare_o_state(cfga_list_data_t *, cfga_list_data_t *,
780Sstevel@tonic-gate     match_type_t);
790Sstevel@tonic-gate static int compare_cond(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
800Sstevel@tonic-gate static int compare_time(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
810Sstevel@tonic-gate static int compare_info(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
820Sstevel@tonic-gate static int compare_type(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
830Sstevel@tonic-gate static int compare_busy(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
840Sstevel@tonic-gate static int compare_class(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
850Sstevel@tonic-gate static int compare_null(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
860Sstevel@tonic-gate static void print_log_id(cfga_list_data_t *, int, char *);
870Sstevel@tonic-gate static void print_r_state(cfga_list_data_t *, int, char *);
880Sstevel@tonic-gate static void print_o_state(cfga_list_data_t *, int, char *);
890Sstevel@tonic-gate static void print_cond(cfga_list_data_t *, int, char *);
900Sstevel@tonic-gate static void print_time(cfga_list_data_t *, int, char *);
910Sstevel@tonic-gate static void print_time_p(cfga_list_data_t *, int, char *);
920Sstevel@tonic-gate static void print_info(cfga_list_data_t *, int, char *);
930Sstevel@tonic-gate static void print_type(cfga_list_data_t *, int, char *);
940Sstevel@tonic-gate static void print_busy(cfga_list_data_t *, int, char *);
950Sstevel@tonic-gate static void print_phys_id(cfga_list_data_t *, int, char *);
960Sstevel@tonic-gate static void print_class(cfga_list_data_t *, int, char *);
970Sstevel@tonic-gate static void print_null(cfga_list_data_t *, int, char *);
980Sstevel@tonic-gate static int count_fields(char *, char);
990Sstevel@tonic-gate static int process_sort_fields(int, struct sort_el *, char *);
1000Sstevel@tonic-gate static int process_fields(int, struct print_col *, int, char *);
1010Sstevel@tonic-gate static cfga_err_t print_fields(int, struct print_col *, int, int, char *,
1020Sstevel@tonic-gate     cfga_list_data_t *, FILE *);
1030Sstevel@tonic-gate static int ldata_compare(const void *, const void *);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static void arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts,
1060Sstevel@tonic-gate     int dyn_exp);
1070Sstevel@tonic-gate static void out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs,
1080Sstevel@tonic-gate     int no_dyn);
1090Sstevel@tonic-gate static void report_no_response(ap_arg_t *arg_array, int napids_to_list);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static cfga_err_t set_log_flt(cfga_list_data_t *p, const char *val);
1120Sstevel@tonic-gate static cfga_err_t set_type_flt(cfga_list_data_t *p, const char *val);
1130Sstevel@tonic-gate static cfga_err_t set_class_flt(cfga_list_data_t *p, const char *val);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static char *get_dyn(const char *ap_id);
1160Sstevel@tonic-gate static void remove_dyn(char *ap_id);
117399Svikram static char *s_strdup(char *str);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * global data
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate /* command name for messages */
1230Sstevel@tonic-gate static char *cmdname;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * control for comparing, printing and filtering cfga_list_data
1270Sstevel@tonic-gate  * NOTE:Field names (i.e. member 0 of field_info struct) may not contain '('.
1280Sstevel@tonic-gate  *	The post filtering code depends on it.
1290Sstevel@tonic-gate  * NOTE:A NULL value for the set_filter member indicates that filtering based
1300Sstevel@tonic-gate  *	on that field is currently not supported.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate static struct field_info all_fields[] = {
1330Sstevel@tonic-gate {"ap_id", "Ap_Id", SZ_EL(ap_log_id), compare_ap_id, print_log_id, set_log_flt},
1340Sstevel@tonic-gate {"r_state", "Receptacle", STATE_WIDTH, compare_r_state, print_r_state, NULL},
1350Sstevel@tonic-gate {"o_state", "Occupant", STATE_WIDTH, compare_o_state, print_o_state, NULL},
1360Sstevel@tonic-gate {"condition", "Condition", COND_WIDTH, compare_cond, print_cond, NULL},
1370Sstevel@tonic-gate {"status_time", "When", TIME_WIDTH, compare_time, print_time, NULL},
1380Sstevel@tonic-gate {"status_time_p", "When", TIME_P_WIDTH, compare_time, print_time_p, NULL},
1390Sstevel@tonic-gate {"info", "Information", SZ_EL(ap_info), compare_info, print_info, NULL},
1400Sstevel@tonic-gate {"type", "Type", SZ_EL(ap_type), compare_type, print_type, set_type_flt},
1410Sstevel@tonic-gate {"busy", "Busy", 8, compare_busy, print_busy, NULL},
1420Sstevel@tonic-gate {"physid", "Phys_Id", SZ_EL(ap_phys_id), compare_ap_id, print_phys_id, NULL},
1430Sstevel@tonic-gate {"class", "Class", SZ_EL(ap_class), compare_class, print_class, set_class_flt}
1440Sstevel@tonic-gate };
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #define	PREFILT_CLASS_STR	"class="
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate typedef struct {
1490Sstevel@tonic-gate 	cfga_list_data_t ldata;			/* Selection criteria */
1500Sstevel@tonic-gate 	match_type_t match_type_p[N_FIELDS];	/* Type of match */
1510Sstevel@tonic-gate } post_filter_t;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static struct field_info null_field =
1540Sstevel@tonic-gate 	{"null", "", 0, compare_null, print_null, NULL};
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct sort_el *sort_list;	/* Used in ldata_compare() */
1570Sstevel@tonic-gate static int nsort_list;
1580Sstevel@tonic-gate static char unk_field[] = "%s: field \"%s\" unknown\n";
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static char aptype_no_dyn[] = "%s: Invalid ap_id: %s\n";
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /* strings that make up the usage message */
1630Sstevel@tonic-gate static char *usage_tab[] = {
1640Sstevel@tonic-gate " %s [-f] [-y|-n] [-v] [-o hardware_opts ] -c function ap_id [ap_id...]\n",
1650Sstevel@tonic-gate " %s [-f] [-y|-n] [-v] [-o hardware_opts ] -x function ap_id [ap_id...]\n",
1660Sstevel@tonic-gate " %s [-v] [-s listing_options ] [-o hardware_opts ] [-a]\n"
1670Sstevel@tonic-gate "\t[-l [ap_id|ap_type...]]\n",
1680Sstevel@tonic-gate " %s [-v] [-o hardware_opts ] -t ap_id [ap_id...]\n",
1690Sstevel@tonic-gate " %s [-v] [-o hardware_opts ] -h [ap_id|ap_type...]\n",
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /* Type of matches currently supported by the select sub-option */
1730Sstevel@tonic-gate static match_cvt_t match_type_array[] = {
1740Sstevel@tonic-gate 	{"partial", CFGA_MATCH_PARTIAL},
1750Sstevel@tonic-gate 	{"exact", CFGA_MATCH_EXACT}
1760Sstevel@tonic-gate };
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #define	N_MATCH_TYPES	(sizeof (match_type_array)/sizeof (match_type_array[0]))
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static cfga_err_t setup_filter(const char *selectp, const char *matchp,
1810Sstevel@tonic-gate     post_filter_t *post_filtp, char **prefilt_optpp);
1820Sstevel@tonic-gate static cfga_err_t parse_select_opt(const char *selectp,
1830Sstevel@tonic-gate     post_filter_t *post_filtp, match_type_t match_type);
1840Sstevel@tonic-gate static int do_config_list(int, char *[], cfga_list_data_t *, int, char *,
1850Sstevel@tonic-gate     char *, char *, int, char *, post_filter_t *, int);
1860Sstevel@tonic-gate static void do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *jp);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * main - the main routine of cfgadm, processes the command line
1910Sstevel@tonic-gate  * and dispatches functions off to libraries.
1920Sstevel@tonic-gate  */
193399Svikram int
main(int argc,char * argv[])1940Sstevel@tonic-gate main(
1950Sstevel@tonic-gate 	int argc,
1960Sstevel@tonic-gate 	char *argv[])
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	extern char *optarg;
1990Sstevel@tonic-gate 	extern int optind;
2000Sstevel@tonic-gate 	int c;
2010Sstevel@tonic-gate 	char *subopts;
2020Sstevel@tonic-gate 	char *subvalue;
2030Sstevel@tonic-gate 	char *const *ap_args = NULL;
2040Sstevel@tonic-gate 	cfga_cmd_t sc_opt = NULL;
2050Sstevel@tonic-gate 	struct cfga_confirm confirm;
2060Sstevel@tonic-gate 	struct cfga_msg message;
2070Sstevel@tonic-gate 	int ret = CFGA_ERROR;
2080Sstevel@tonic-gate 	int i;
2090Sstevel@tonic-gate 	char *estrp = NULL;
2100Sstevel@tonic-gate 	cfga_op_t action = CFGA_OP_NONE;
2110Sstevel@tonic-gate 	char *plat_opts = NULL;
2120Sstevel@tonic-gate 	char *act_arg = NULL;
2130Sstevel@tonic-gate 	enum confirm confarg = CONFIRM_DEFAULT;
2140Sstevel@tonic-gate 	char *list_opts = NULL;
2150Sstevel@tonic-gate 	cfga_flags_t flags = 0;
2160Sstevel@tonic-gate 	int arg_error = 0;
2170Sstevel@tonic-gate 	int dyn_exp = 0;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	estrp = NULL;
2200Sstevel@tonic-gate 	if (argc > 0)
2210Sstevel@tonic-gate 		cmdname = basename(argv[0]);
2220Sstevel@tonic-gate 	else
2230Sstevel@tonic-gate 		cmdname = "cfgadm";
2240Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2250Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2260Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2270Sstevel@tonic-gate #endif
2280Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	while ((c = getopt(argc, argv, OPTIONS)) != EOF) {
2310Sstevel@tonic-gate 		static char dup_action[] =
2320Sstevel@tonic-gate "%s: more than one action specified (-c,-l,-t,-x)\n";
2330Sstevel@tonic-gate 		static char dup_option[] =
2340Sstevel@tonic-gate "%s: more than one -%c option specified\n";
2350Sstevel@tonic-gate 		switch (c) {
2360Sstevel@tonic-gate 		case 'a':
2370Sstevel@tonic-gate 			if (dyn_exp) {
2380Sstevel@tonic-gate 				arg_error = 1;
2390Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
2400Sstevel@tonic-gate 				    cmdname, c);
2410Sstevel@tonic-gate 			}
2420Sstevel@tonic-gate 			dyn_exp = 1;
2430Sstevel@tonic-gate 			break;
2440Sstevel@tonic-gate 		case 'c':
2450Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
2460Sstevel@tonic-gate 				arg_error = 1;
2470Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
2480Sstevel@tonic-gate 				    cmdname);
2490Sstevel@tonic-gate 			}
2500Sstevel@tonic-gate 			action = CFGA_OP_CHANGE_STATE;
2510Sstevel@tonic-gate 			subopts = optarg;
2520Sstevel@tonic-gate 			subvalue = NULL;
2530Sstevel@tonic-gate 			/*
2540Sstevel@tonic-gate 			 * Reject -c suboption if they are unrecognized
2550Sstevel@tonic-gate 			 * or more than one or have a associated value.
2560Sstevel@tonic-gate 			 */
2570Sstevel@tonic-gate 			if ((sc_opt = getsubopt(&subopts, state_opts,
2580Sstevel@tonic-gate 			    &subvalue)) == -1 || *subopts != '\0' ||
2590Sstevel@tonic-gate 			    subvalue != NULL) {
2600Sstevel@tonic-gate 				arg_error = 1;
2610Sstevel@tonic-gate 				break;
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 			break;
2640Sstevel@tonic-gate 		case 'f':
2650Sstevel@tonic-gate 			if ((flags & CFGA_FLAG_FORCE) != 0) {
2660Sstevel@tonic-gate 				arg_error = 1;
2670Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
2680Sstevel@tonic-gate 				    cmdname, c);
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 			flags |= CFGA_FLAG_FORCE;
2710Sstevel@tonic-gate 			break;
2720Sstevel@tonic-gate 		case 'h':
2730Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
2740Sstevel@tonic-gate 				arg_error = 1;
2750Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
2760Sstevel@tonic-gate 				    cmdname);
2770Sstevel@tonic-gate 			}
2780Sstevel@tonic-gate 			action = CFGA_OP_HELP;
2790Sstevel@tonic-gate 			break;
2800Sstevel@tonic-gate 		case 'l':
2810Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
2820Sstevel@tonic-gate 				arg_error = 1;
2830Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
2840Sstevel@tonic-gate 				    cmdname);
2850Sstevel@tonic-gate 			}
2860Sstevel@tonic-gate 			action = CFGA_OP_LIST;
2870Sstevel@tonic-gate 			break;
2880Sstevel@tonic-gate 		case 'n':
2890Sstevel@tonic-gate 			if (confarg != CONFIRM_DEFAULT) {
2900Sstevel@tonic-gate 				arg_error = 1;
2910Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
2920Sstevel@tonic-gate 				    cmdname, c);
2930Sstevel@tonic-gate 			}
2940Sstevel@tonic-gate 			confarg = CONFIRM_NO;
2950Sstevel@tonic-gate 			break;
2960Sstevel@tonic-gate 		case 'o':
2970Sstevel@tonic-gate 			if (plat_opts != NULL) {
2980Sstevel@tonic-gate 				arg_error = 1;
2990Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3000Sstevel@tonic-gate 				    cmdname, c);
3010Sstevel@tonic-gate 			}
3020Sstevel@tonic-gate 			plat_opts = optarg;
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 		case 's':
3050Sstevel@tonic-gate 			if (list_opts != NULL) {
3060Sstevel@tonic-gate 				arg_error = 1;
3070Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3080Sstevel@tonic-gate 				    cmdname, c);
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 			list_opts = optarg;
3110Sstevel@tonic-gate 			break;
3120Sstevel@tonic-gate 		case 't':
3130Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
3140Sstevel@tonic-gate 				arg_error = 1;
3150Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
3160Sstevel@tonic-gate 				    cmdname);
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 			action = CFGA_OP_TEST;
3190Sstevel@tonic-gate 			break;
3200Sstevel@tonic-gate 		case 'x':
3210Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
3220Sstevel@tonic-gate 				arg_error = 1;
3230Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
3240Sstevel@tonic-gate 				    cmdname);
3250Sstevel@tonic-gate 			}
3260Sstevel@tonic-gate 			action = CFGA_OP_PRIVATE;
3270Sstevel@tonic-gate 			act_arg = optarg;
3280Sstevel@tonic-gate 			break;
3290Sstevel@tonic-gate 		case 'v':
3300Sstevel@tonic-gate 			if ((flags & CFGA_FLAG_VERBOSE) != 0) {
3310Sstevel@tonic-gate 				arg_error = 1;
3320Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3330Sstevel@tonic-gate 				    cmdname, c);
3340Sstevel@tonic-gate 			}
3350Sstevel@tonic-gate 			flags |= CFGA_FLAG_VERBOSE;
3360Sstevel@tonic-gate 			break;
3370Sstevel@tonic-gate 		case 'y':
3380Sstevel@tonic-gate 			if (confarg != CONFIRM_DEFAULT) {
3390Sstevel@tonic-gate 				arg_error = 1;
3400Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3410Sstevel@tonic-gate 				    cmdname, c);
3420Sstevel@tonic-gate 			}
3430Sstevel@tonic-gate 			confarg = CONFIRM_YES;
3440Sstevel@tonic-gate 			break;
3450Sstevel@tonic-gate 		case '?':	/* getopts issues message is this case */
3460Sstevel@tonic-gate 		default:	/* catch programming errors */
3470Sstevel@tonic-gate 			arg_error = 1;
3480Sstevel@tonic-gate 			break;
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/* default action is list */
3530Sstevel@tonic-gate 	if (action == CFGA_OP_NONE)
3540Sstevel@tonic-gate 		action = CFGA_OP_LIST;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* -s and -a option only for list */
3570Sstevel@tonic-gate 	if (action != CFGA_OP_LIST && (list_opts != NULL || dyn_exp)) {
3580Sstevel@tonic-gate 		arg_error = 1;
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (arg_error) {
3620Sstevel@tonic-gate 		usage();
3630Sstevel@tonic-gate 		exit(EXIT_ARGERROR);
3640Sstevel@tonic-gate 		/*NOTREACHED*/
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3677438SRameshkumar.Ramasamy@Sun.COM 	if (getzoneid() != GLOBAL_ZONEID) {
3687438SRameshkumar.Ramasamy@Sun.COM 		cfgadm_error(CFGA_NOTSUPP,
3697438SRameshkumar.Ramasamy@Sun.COM 		    gettext("cfgadm can only be run from the global zone"));
3707438SRameshkumar.Ramasamy@Sun.COM 		exit(EXIT_NOTSUPP);
3717438SRameshkumar.Ramasamy@Sun.COM 	}
3727438SRameshkumar.Ramasamy@Sun.COM 
3730Sstevel@tonic-gate 	ap_args = &argv[optind];
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	/*
3760Sstevel@tonic-gate 	 * If neither -n of -y was specified, interactive confirmation
3770Sstevel@tonic-gate 	 * is used.  Check if the program has terminal I/O and
3780Sstevel@tonic-gate 	 * enforce -n if not.
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	(void) memset(&confirm, 0, sizeof (confirm));
3810Sstevel@tonic-gate 	if (action == CFGA_OP_CHANGE_STATE || action == CFGA_OP_PRIVATE) {
3820Sstevel@tonic-gate 		if (confarg == CONFIRM_DEFAULT &&
3830Sstevel@tonic-gate 		    !(isatty(fileno(stdin)) && isatty(fileno(stderr))))
3840Sstevel@tonic-gate 			confarg = CONFIRM_NO;
3850Sstevel@tonic-gate 		switch (confarg) {
3860Sstevel@tonic-gate 		case CONFIRM_DEFAULT:
3870Sstevel@tonic-gate 			confirm.confirm = confirm_interactive;
3880Sstevel@tonic-gate 			break;
3890Sstevel@tonic-gate 		case CONFIRM_NO:
3900Sstevel@tonic-gate 			confirm.confirm = confirm_no;
3910Sstevel@tonic-gate 			break;
3920Sstevel@tonic-gate 		case CONFIRM_YES:
3930Sstevel@tonic-gate 			confirm.confirm = confirm_yes;
3940Sstevel@tonic-gate 			break;
3950Sstevel@tonic-gate 		default:	/* paranoia */
3960Sstevel@tonic-gate 			abort();
3970Sstevel@tonic-gate 			/*NOTREACHED*/
3980Sstevel@tonic-gate 		}
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	/*
4020Sstevel@tonic-gate 	 * set up message output routine
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	message.message_routine = message_output;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	switch (action) {
4070Sstevel@tonic-gate 	case CFGA_OP_CHANGE_STATE:
408*10923SEvan.Yan@Sun.COM 		/* Sanity check - requires an argument */
409*10923SEvan.Yan@Sun.COM 		if ((argc - optind) <= 0) {
410*10923SEvan.Yan@Sun.COM 			usage();
411*10923SEvan.Yan@Sun.COM 			break;
412*10923SEvan.Yan@Sun.COM 		}
413*10923SEvan.Yan@Sun.COM 		/* Sanity check - args cannot be ap_types */
414*10923SEvan.Yan@Sun.COM 		for (i = 0; i < (argc - optind); i++) {
415*10923SEvan.Yan@Sun.COM 			if (find_arg_type(ap_args[i]) == AP_TYPE) {
416*10923SEvan.Yan@Sun.COM 				usage();
417*10923SEvan.Yan@Sun.COM 				exit(EXIT_ARGERROR);
418*10923SEvan.Yan@Sun.COM 				/*NOTREACHED*/
419*10923SEvan.Yan@Sun.COM 			}
420*10923SEvan.Yan@Sun.COM 		}
421*10923SEvan.Yan@Sun.COM 		ret = config_change_state(sc_opt, argc - optind, ap_args,
422*10923SEvan.Yan@Sun.COM 		    plat_opts, &confirm, &message, &estrp, flags);
423*10923SEvan.Yan@Sun.COM 		if (ret != CFGA_OK)
424*10923SEvan.Yan@Sun.COM 			cfgadm_error(ret, estrp);
4250Sstevel@tonic-gate 		break;
426*10923SEvan.Yan@Sun.COM 	case CFGA_OP_PRIVATE:
427*10923SEvan.Yan@Sun.COM 		/* Sanity check - requires an argument */
428*10923SEvan.Yan@Sun.COM 		if ((argc - optind) <= 0) {
4290Sstevel@tonic-gate 			usage();
430*10923SEvan.Yan@Sun.COM 			break;
431*10923SEvan.Yan@Sun.COM 		}
432*10923SEvan.Yan@Sun.COM 		/* Sanity check - args cannot be ap_types */
433*10923SEvan.Yan@Sun.COM 		for (i = 0; i < (argc - optind); i++) {
434*10923SEvan.Yan@Sun.COM 			if (find_arg_type(ap_args[i]) == AP_TYPE) {
435*10923SEvan.Yan@Sun.COM 				usage();
436*10923SEvan.Yan@Sun.COM 				exit(EXIT_ARGERROR);
437*10923SEvan.Yan@Sun.COM 				/*NOTREACHED*/
438*10923SEvan.Yan@Sun.COM 			}
439*10923SEvan.Yan@Sun.COM 		}
440*10923SEvan.Yan@Sun.COM 
441*10923SEvan.Yan@Sun.COM 		ret = config_private_func(act_arg, argc - optind, ap_args,
442*10923SEvan.Yan@Sun.COM 		    plat_opts, &confirm, &message, &estrp, flags);
443*10923SEvan.Yan@Sun.COM 
444*10923SEvan.Yan@Sun.COM 		if (ret != CFGA_OK)
445*10923SEvan.Yan@Sun.COM 			cfgadm_error(ret, estrp);
4460Sstevel@tonic-gate 		break;
447*10923SEvan.Yan@Sun.COM 	case CFGA_OP_TEST:
448*10923SEvan.Yan@Sun.COM 		/* Sanity check - requires an argument */
449*10923SEvan.Yan@Sun.COM 		if ((argc - optind) <= 0) {
450*10923SEvan.Yan@Sun.COM 			usage();
451*10923SEvan.Yan@Sun.COM 			break;
452*10923SEvan.Yan@Sun.COM 		}
453*10923SEvan.Yan@Sun.COM 
454*10923SEvan.Yan@Sun.COM 		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
4550Sstevel@tonic-gate 			usage();
4560Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
4570Sstevel@tonic-gate 			/*NOTREACHED*/
458*10923SEvan.Yan@Sun.COM 		}
4590Sstevel@tonic-gate 
460*10923SEvan.Yan@Sun.COM 		/* Sanity check - args cannot be ap_types */
461*10923SEvan.Yan@Sun.COM 		for (i = 0; i < (argc - optind); i++) {
462*10923SEvan.Yan@Sun.COM 			if (find_arg_type(ap_args[i]) == AP_TYPE) {
463*10923SEvan.Yan@Sun.COM 				usage();
464*10923SEvan.Yan@Sun.COM 				exit(EXIT_ARGERROR);
465*10923SEvan.Yan@Sun.COM 				/*NOTREACHED*/
466*10923SEvan.Yan@Sun.COM 			}
467*10923SEvan.Yan@Sun.COM 		}
468*10923SEvan.Yan@Sun.COM 		ret = config_test(argc - optind, ap_args, plat_opts, &message,
469*10923SEvan.Yan@Sun.COM 		    &estrp, flags);
470*10923SEvan.Yan@Sun.COM 		if (ret != CFGA_OK)
471*10923SEvan.Yan@Sun.COM 			cfgadm_error(ret, estrp);
4720Sstevel@tonic-gate 		break;
473*10923SEvan.Yan@Sun.COM 	case CFGA_OP_HELP:
4740Sstevel@tonic-gate 
475*10923SEvan.Yan@Sun.COM 		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
4760Sstevel@tonic-gate 			usage();
4770Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
4780Sstevel@tonic-gate 			/*NOTREACHED*/
479*10923SEvan.Yan@Sun.COM 		}
4800Sstevel@tonic-gate 
481*10923SEvan.Yan@Sun.COM 		/* always do usage? */
4820Sstevel@tonic-gate 		usage();
483*10923SEvan.Yan@Sun.COM 		ret = config_help(argc - optind, ap_args, &message, plat_opts,
484*10923SEvan.Yan@Sun.COM 		    flags);
485*10923SEvan.Yan@Sun.COM 		if (ret != CFGA_OK)
486*10923SEvan.Yan@Sun.COM 			cfgadm_error(ret, estrp);
487*10923SEvan.Yan@Sun.COM 		break;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	case CFGA_OP_LIST: {
490399Svikram 		/*
491399Svikram 		 * Note that we leak the strdup strings below (we never free
492399Svikram 		 * them). This is ok in this context since cfgadm is
493399Svikram 		 * a short lived process that will exit shortly freeing
494399Svikram 		 * the memory.
495399Svikram 		 */
4960Sstevel@tonic-gate 		cfga_list_data_t *list_array = NULL;
4970Sstevel@tonic-gate 		int nlist = 0;
498399Svikram 		char *sort_fields = s_strdup(DEF_SORT_FIELDS);
499399Svikram 		char *cols = s_strdup(DEF_COLS);
500399Svikram 		char *cols2 = s_strdup(DEF_COLS2);
5010Sstevel@tonic-gate 		int noheadings = 0;
502399Svikram 		char *delim = s_strdup(DEF_DELIM);
5030Sstevel@tonic-gate 		int exitcode = EXIT_OK;
5040Sstevel@tonic-gate 		int i;
5050Sstevel@tonic-gate 		int type = 0;
5060Sstevel@tonic-gate 		char *selectp = NULL, *matchp = NULL, *prefilt_optp = NULL;
5070Sstevel@tonic-gate 		post_filter_t *post_filtp = NULL;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
5100Sstevel@tonic-gate 			usage();
5110Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
5120Sstevel@tonic-gate 			/*NOTREACHED*/
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		if (flags & CFGA_FLAG_VERBOSE) {
516399Svikram 			cols = s_strdup(DEF_COLS_VERBOSE);
517399Svikram 			cols2 = s_strdup(DEF_COLS2_VERBOSE);
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		if (list_opts != NULL && !extract_list_suboptions(list_opts,
5210Sstevel@tonic-gate 		    &sort_fields, &cols, &cols2, &noheadings, &delim,
5220Sstevel@tonic-gate 		    &selectp, &matchp)) {
5230Sstevel@tonic-gate 			usage_field();
5240Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
5250Sstevel@tonic-gate 			/*NOTREACHED*/
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 		/*
5290Sstevel@tonic-gate 		 * Scan any args and see if there are any ap_types.
5300Sstevel@tonic-gate 		 * If there are we get all attachment point stats and
5310Sstevel@tonic-gate 		 * then filter what gets printed.
5320Sstevel@tonic-gate 		 */
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		type = 0;
5350Sstevel@tonic-gate 		for (i = 0; i < (argc - optind); i++) {
5360Sstevel@tonic-gate 			if (find_arg_type(ap_args[i]) == AP_TYPE) {
5370Sstevel@tonic-gate 				type = 1;
5380Sstevel@tonic-gate 				/* ap_types cannot have dynamic components */
5390Sstevel@tonic-gate 				if (get_dyn(ap_args[i]) != NULL) {
5400Sstevel@tonic-gate 					(void) fprintf(stderr,
5410Sstevel@tonic-gate 					    gettext(aptype_no_dyn),
5420Sstevel@tonic-gate 					    cmdname, ap_args[i]);
5430Sstevel@tonic-gate 					exit(EXIT_ARGERROR);
5440Sstevel@tonic-gate 					/*NOTREACHED*/
5450Sstevel@tonic-gate 				}
5460Sstevel@tonic-gate 				break;
5470Sstevel@tonic-gate 			}
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		/* Setup filter */
5510Sstevel@tonic-gate 		post_filtp = config_calloc_check(1, sizeof (*post_filtp));
5520Sstevel@tonic-gate 		if (post_filtp == NULL) {
5530Sstevel@tonic-gate 			exit(EXIT_OPFAILED);
5540Sstevel@tonic-gate 			/*NOTREACHED*/
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 		if (setup_filter(selectp, matchp, post_filtp, &prefilt_optp)
5570Sstevel@tonic-gate 		    != CFGA_OK) {
5580Sstevel@tonic-gate 			S_FREE(post_filtp);
5590Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
5600Sstevel@tonic-gate 			/*NOTREACHED*/
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 		list_array = NULL;
5640Sstevel@tonic-gate 		exitcode = EXIT_OK;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 		/*
5670Sstevel@tonic-gate 		 * Check for args. No args means find all libs
5680Sstevel@tonic-gate 		 * and call the cfga_list_ext routine with no ap_ids specified.
5690Sstevel@tonic-gate 		 * With args, if any one of the args are ap_types we
5700Sstevel@tonic-gate 		 * again find all attachment points as in the
5710Sstevel@tonic-gate 		 * no-args case above and then select which attachment points
5720Sstevel@tonic-gate 		 * are actually displayed.
5730Sstevel@tonic-gate 		 */
5740Sstevel@tonic-gate 		if (((argc - optind) == 0) || (type == 1)) {
5750Sstevel@tonic-gate 			/*
5760Sstevel@tonic-gate 			 * No args, or atleast 1 ap_type arg
5770Sstevel@tonic-gate 			 */
5780Sstevel@tonic-gate 			ret = config_list_ext(0, NULL, &list_array,
5790Sstevel@tonic-gate 			    &nlist, plat_opts, prefilt_optp, &estrp,
5800Sstevel@tonic-gate 			    dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
5810Sstevel@tonic-gate 		} else {
5820Sstevel@tonic-gate 			/*
5830Sstevel@tonic-gate 			 * If the args are all ap_ids (no ap_types) we call the
5840Sstevel@tonic-gate 			 * cfga_list_ext routine with those specific ap_ids.
5850Sstevel@tonic-gate 			 */
5860Sstevel@tonic-gate 			ret = config_list_ext(argc - optind, ap_args,
5870Sstevel@tonic-gate 			    &list_array, &nlist, plat_opts, prefilt_optp,
5880Sstevel@tonic-gate 			    &estrp, dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		S_FREE(prefilt_optp);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		if (ret == CFGA_OK) {
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 			if (do_config_list(
5960Sstevel@tonic-gate 			    (argc - optind), &argv[optind], list_array, nlist,
5970Sstevel@tonic-gate 			    sort_fields, cols, cols2, noheadings, delim,
5980Sstevel@tonic-gate 			    post_filtp, dyn_exp) != CFGA_OK) {
5990Sstevel@tonic-gate 				exitcode = EXIT_ARGERROR;
6000Sstevel@tonic-gate 			} else {
6010Sstevel@tonic-gate 				exitcode = EXIT_OK;
6020Sstevel@tonic-gate 			}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 			S_FREE(list_array);
6050Sstevel@tonic-gate 			S_FREE(post_filtp);
6060Sstevel@tonic-gate 
607*10923SEvan.Yan@Sun.COM 			if (estrp != NULL && *estrp != '\0')
608*10923SEvan.Yan@Sun.COM 				cfgadm_error(CFGA_NOTSUPP, estrp);
6090Sstevel@tonic-gate 			if (exitcode != EXIT_OK) {
6100Sstevel@tonic-gate 				exit(exitcode);
6110Sstevel@tonic-gate 				/*NOTREACHED*/
6120Sstevel@tonic-gate 			}
6130Sstevel@tonic-gate 		} else {
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 			S_FREE(post_filtp);
6160Sstevel@tonic-gate 			cfgadm_error(ret, estrp);
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate 		break;
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 	default:	/* paranoia */
6210Sstevel@tonic-gate 		abort();
6220Sstevel@tonic-gate 		/*NOTREACHED*/
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if (ret == CFGA_NOTSUPP) {
626399Svikram 		return (EXIT_NOTSUPP);
6270Sstevel@tonic-gate 	} else if (ret != CFGA_OK) {
628399Svikram 		return (EXIT_OPFAILED);
6290Sstevel@tonic-gate 	} else {
630399Svikram 		return (EXIT_OK);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	/*NOTREACHED*/
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate /*
6360Sstevel@tonic-gate  * usage - outputs the usage help message.
6370Sstevel@tonic-gate  */
6380Sstevel@tonic-gate static void
usage(void)639*10923SEvan.Yan@Sun.COM usage(void)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	int i;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n", gettext("Usage:"));
6440Sstevel@tonic-gate 	for (i = 0; i < sizeof (usage_tab)/sizeof (usage_tab[0]); i++) {
6450Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(usage_tab[i]), cmdname);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * Emit an error message.
6510Sstevel@tonic-gate  * As a side-effect the hardware specific error message is deallocated
6520Sstevel@tonic-gate  * as described in config_admin(3X).
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate static void
cfgadm_error(int errnum,char * estrp)6550Sstevel@tonic-gate cfgadm_error(int errnum, char *estrp)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	const char *ep;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	ep = config_strerror(errnum);
6600Sstevel@tonic-gate 	if (ep == NULL)
6610Sstevel@tonic-gate 		ep = gettext("configuration administration unknown error");
6620Sstevel@tonic-gate 	if (estrp != NULL && *estrp != '\0') {
6630Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", cmdname, ep, estrp);
6640Sstevel@tonic-gate 	} else {
6650Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n", cmdname, ep);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 	if (estrp != NULL)
6680Sstevel@tonic-gate 		free((void *)estrp);
6690Sstevel@tonic-gate 	if (errnum == CFGA_INVAL)
6700Sstevel@tonic-gate 		usage();
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate  * confirm_interactive - prompt user for confirmation
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate static int
confirm_interactive(void * appdata_ptr,const char * message)6770Sstevel@tonic-gate confirm_interactive(
6780Sstevel@tonic-gate 	void *appdata_ptr,
6790Sstevel@tonic-gate 	const char *message)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	static char yeschr[YESNO_STR_MAX + 2];
6820Sstevel@tonic-gate 	static char nochr[YESNO_STR_MAX + 2];
6830Sstevel@tonic-gate 	static int inited = 0;
6840Sstevel@tonic-gate 	int isyes;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate #ifdef lint
6870Sstevel@tonic-gate 	appdata_ptr = appdata_ptr;
6880Sstevel@tonic-gate #endif /* lint */
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * First time through initialisation.  In the original
6910Sstevel@tonic-gate 	 * version of this command this function is only called once,
6920Sstevel@tonic-gate 	 * but this function is generalized for the future.
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if (!inited) {
6950Sstevel@tonic-gate 		(void) strncpy(yeschr, nl_langinfo(YESSTR), YESNO_STR_MAX + 1);
6960Sstevel@tonic-gate 		(void) strncpy(nochr, nl_langinfo(NOSTR), YESNO_STR_MAX + 1);
6970Sstevel@tonic-gate 		inited = 1;
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	do {
7010Sstevel@tonic-gate 		(void) fprintf(stderr, "%s (%s/%s)? ", message, yeschr, nochr);
7020Sstevel@tonic-gate 		isyes = yesno(yeschr, nochr);
7030Sstevel@tonic-gate 	} while (isyes == -1);
7040Sstevel@tonic-gate 	return (isyes);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate  * If any text is input it must sub-string match either yes or no.
7090Sstevel@tonic-gate  * Failure of this match is indicated by return of -1.
7100Sstevel@tonic-gate  * If an empty line is input, this is taken as no.
7110Sstevel@tonic-gate  */
7120Sstevel@tonic-gate static int
yesno(char * yesp,char * nop)7130Sstevel@tonic-gate yesno(
7140Sstevel@tonic-gate 	char *yesp,
7150Sstevel@tonic-gate 	char *nop)
7160Sstevel@tonic-gate {
7170Sstevel@tonic-gate 	int	i, b;
7180Sstevel@tonic-gate 	char	ans[YESNO_STR_MAX + 1];
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	i = 0;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*CONSTCOND*/
7230Sstevel@tonic-gate 	while (1) {
7240Sstevel@tonic-gate 		b = getc(stdin);	/* more explicit that rm.c version */
7250Sstevel@tonic-gate 		if (b == '\n' || b == '\0' || b == EOF) {
7260Sstevel@tonic-gate 			if (i < YESNO_STR_MAX)	/* bug fix to rm.c version */
7270Sstevel@tonic-gate 				ans[i] = 0;
7280Sstevel@tonic-gate 			break;
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 		if (i < YESNO_STR_MAX)
7310Sstevel@tonic-gate 			ans[i] = b;
7320Sstevel@tonic-gate 		i++;
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate 	if (i >= YESNO_STR_MAX) {
7350Sstevel@tonic-gate 		i = YESNO_STR_MAX;
7360Sstevel@tonic-gate 		ans[YESNO_STR_MAX] = 0;
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 	/* changes to rm.c version follow */
7390Sstevel@tonic-gate 	if (i == 0)
7400Sstevel@tonic-gate 		return (0);
7410Sstevel@tonic-gate 	if (strncmp(nop, ans, i) == 0)
7420Sstevel@tonic-gate 		return (0);
7430Sstevel@tonic-gate 	if (strncmp(yesp, ans, i) == 0)
7440Sstevel@tonic-gate 		return (1);
7450Sstevel@tonic-gate 	return (-1);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate /*ARGSUSED*/
7490Sstevel@tonic-gate static int
confirm_no(void * appdata_ptr,const char * message)7500Sstevel@tonic-gate confirm_no(
7510Sstevel@tonic-gate 	void *appdata_ptr,
7520Sstevel@tonic-gate 	const char *message)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	return (0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /*ARGSUSED*/
7580Sstevel@tonic-gate static int
confirm_yes(void * appdata_ptr,const char * message)7590Sstevel@tonic-gate confirm_yes(
7600Sstevel@tonic-gate 	void *appdata_ptr,
7610Sstevel@tonic-gate 	const char *message)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	return (1);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate /*
7670Sstevel@tonic-gate  * Find base name of filename.
7680Sstevel@tonic-gate  */
7690Sstevel@tonic-gate static char *
basename(char * cp)7700Sstevel@tonic-gate basename(
7710Sstevel@tonic-gate 	char *cp)
7720Sstevel@tonic-gate {
7730Sstevel@tonic-gate 	char *sp;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if ((sp = strrchr(cp, '/')) != NULL)
7760Sstevel@tonic-gate 		return (sp + 1);
7770Sstevel@tonic-gate 	return (cp);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate /*ARGSUSED*/
7810Sstevel@tonic-gate static int
message_output(void * appdata_ptr,const char * message)7820Sstevel@tonic-gate message_output(
7830Sstevel@tonic-gate 	void *appdata_ptr,
7840Sstevel@tonic-gate 	const char *message)
7850Sstevel@tonic-gate {
7860Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", message);
7870Sstevel@tonic-gate 	return (CFGA_OK);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate  * extract_list_suboptions - process list option string
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate static int
extract_list_suboptions(char * arg,char ** sortpp,char ** colspp,char ** cols2pp,int * noheadingsp,char ** delimpp,char ** selectpp,char ** matchpp)7950Sstevel@tonic-gate extract_list_suboptions(
7960Sstevel@tonic-gate 	char *arg,
7970Sstevel@tonic-gate 	char **sortpp,
7980Sstevel@tonic-gate 	char **colspp,
7990Sstevel@tonic-gate 	char **cols2pp,
8000Sstevel@tonic-gate 	int *noheadingsp,
8010Sstevel@tonic-gate 	char **delimpp,
8020Sstevel@tonic-gate 	char **selectpp,
8030Sstevel@tonic-gate 	char **matchpp)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate 	char *value = NULL;
8060Sstevel@tonic-gate 	int subopt = 0;
8070Sstevel@tonic-gate 	int err = 0;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	while (*arg != '\0') {
8100Sstevel@tonic-gate 		static char need_value[] =
8110Sstevel@tonic-gate "%s: sub-option \"%s\" requires a value\n";
8120Sstevel@tonic-gate 		static char no_value[] =
8130Sstevel@tonic-gate "%s: sub-option \"%s\" does not take a value\n";
8140Sstevel@tonic-gate 		static char unk_subopt[] =
8150Sstevel@tonic-gate "%s: sub-option \"%s\" unknown\n";
8160Sstevel@tonic-gate 		char **pptr;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		subopt = getsubopt(&arg, list_options, &value);
8190Sstevel@tonic-gate 		switch (subopt) {
8200Sstevel@tonic-gate 		case LIST_SORT:
8210Sstevel@tonic-gate 			pptr = sortpp;
8220Sstevel@tonic-gate 			goto valcom;
8230Sstevel@tonic-gate 		case LIST_COLS:
8240Sstevel@tonic-gate 			pptr = colspp;
8250Sstevel@tonic-gate 			goto valcom;
8260Sstevel@tonic-gate 		case LIST_COLS2:
8270Sstevel@tonic-gate 			pptr = cols2pp;
8280Sstevel@tonic-gate 			goto valcom;
8290Sstevel@tonic-gate 		case LIST_SELECT:
8300Sstevel@tonic-gate 			pptr = selectpp;
8310Sstevel@tonic-gate 			goto valcom;
8320Sstevel@tonic-gate 		case LIST_MATCH:
8330Sstevel@tonic-gate 			pptr = matchpp;
8340Sstevel@tonic-gate 			goto valcom;
8350Sstevel@tonic-gate 		case LIST_DELIM:
8360Sstevel@tonic-gate 			pptr = delimpp;
8370Sstevel@tonic-gate 		valcom:
8380Sstevel@tonic-gate 			if (value == NULL) {
8390Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(need_value),
8400Sstevel@tonic-gate 				    cmdname, list_options[subopt]);
8410Sstevel@tonic-gate 				err = 1;
8420Sstevel@tonic-gate 			} else
8430Sstevel@tonic-gate 				*pptr = value;
8440Sstevel@tonic-gate 			break;
8450Sstevel@tonic-gate 		case LIST_NOHEADINGS:
8460Sstevel@tonic-gate 			if (value != NULL) {
8470Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(no_value),
8480Sstevel@tonic-gate 				    cmdname, list_options[subopt]);
8490Sstevel@tonic-gate 				err = 1;
8500Sstevel@tonic-gate 			} else
8510Sstevel@tonic-gate 				*noheadingsp = 1;
8520Sstevel@tonic-gate 			break;
8530Sstevel@tonic-gate 		default:
8540Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(unk_subopt),
8550Sstevel@tonic-gate 			    cmdname, value);
8560Sstevel@tonic-gate 			err = 1;
8570Sstevel@tonic-gate 			break;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	return (err == 0);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate static cfga_err_t
setup_prefilter(post_filter_t * post_filtp,char ** prefilt_optpp)8640Sstevel@tonic-gate setup_prefilter(post_filter_t *post_filtp, char **prefilt_optpp)
8650Sstevel@tonic-gate {
8660Sstevel@tonic-gate 	size_t len;
8670Sstevel@tonic-gate 	const char *clopt = PREFILT_CLASS_STR;
8680Sstevel@tonic-gate 	int idx;
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	*prefilt_optpp = NULL;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	/* Get the index for the "class" field */
8740Sstevel@tonic-gate 	for (idx = 0; idx < N_FIELDS; idx++) {
8750Sstevel@tonic-gate 		if (strcmp(all_fields[idx].name, PREFILT_CLASS_STR) == 0)
8760Sstevel@tonic-gate 			break;
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	/*
8800Sstevel@tonic-gate 	 * Currently pre-filter available only for class fld w/ EXACT match
8810Sstevel@tonic-gate 	 */
8820Sstevel@tonic-gate 	if (idx >= N_FIELDS ||
8830Sstevel@tonic-gate 	    post_filtp->match_type_p[idx] != CFGA_MATCH_EXACT) {
8840Sstevel@tonic-gate 		return (CFGA_OK);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	len = strlen(clopt) + strlen(post_filtp->ldata.ap_class) + 1;
8880Sstevel@tonic-gate 	if ((*prefilt_optpp = config_calloc_check(1, len)) == NULL) {
8890Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	(void) strcpy(*prefilt_optpp, clopt);
8930Sstevel@tonic-gate 	(void) strcat(*prefilt_optpp, post_filtp->ldata.ap_class);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * Since it is being pre-filtered, this attribute does not need
8970Sstevel@tonic-gate 	 * post-filtering.
8980Sstevel@tonic-gate 	 */
8990Sstevel@tonic-gate 	post_filtp->match_type_p[idx] = CFGA_MATCH_NOFILTER;
9000Sstevel@tonic-gate 	if (all_fields[idx].set_filter != NULL) {
9010Sstevel@tonic-gate 		(void) all_fields[idx].set_filter(&post_filtp->ldata, "");
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	return (CFGA_OK);
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate static cfga_err_t
set_attrval(const char * attr,const char * val,post_filter_t * post_filtp,match_type_t match_type)9080Sstevel@tonic-gate set_attrval(
9090Sstevel@tonic-gate 	const char *attr,
9100Sstevel@tonic-gate 	const char *val,
9110Sstevel@tonic-gate 	post_filter_t *post_filtp,
9120Sstevel@tonic-gate 	match_type_t match_type)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate 	int fld = 0;
9150Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	for (fld = 0; fld < N_FIELDS; fld++) {
9180Sstevel@tonic-gate 		if (strcmp(attr, all_fields[fld].name) == 0)
9190Sstevel@tonic-gate 			break;
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/* Valid field or is the select option supported for this field */
9230Sstevel@tonic-gate 	if (fld >= N_FIELDS || all_fields[fld].set_filter == NULL) {
9240Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	if ((ret = all_fields[fld].set_filter(&post_filtp->ldata, val))
9280Sstevel@tonic-gate 	    == CFGA_OK) {
9290Sstevel@tonic-gate 		post_filtp->match_type_p[fld] = match_type;
9300Sstevel@tonic-gate 	}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	return (ret);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate static char inval_optarg[] =
9370Sstevel@tonic-gate 	"%s: invalid value \"%s\" for %s suboption.\n";
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * Parses the "select" string and fills in the post_filter structure
9410Sstevel@tonic-gate  */
9420Sstevel@tonic-gate static cfga_err_t
parse_select_opt(const char * selectp,post_filter_t * post_filtp,match_type_t match_type)9430Sstevel@tonic-gate parse_select_opt(
9440Sstevel@tonic-gate 	const char *selectp,
9450Sstevel@tonic-gate 	post_filter_t *post_filtp,
9460Sstevel@tonic-gate 	match_type_t match_type)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	parse_state_t state = CFGA_PSTATE_INIT;
9490Sstevel@tonic-gate 	char *cp = NULL, *optstr = NULL, *attr = NULL, *val = NULL;
9500Sstevel@tonic-gate 	int bal = 0;	/* Tracks balancing */
9510Sstevel@tonic-gate 	char chr;
9520Sstevel@tonic-gate 	cfga_err_t ret;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	if (selectp == NULL || post_filtp == NULL) {
9560Sstevel@tonic-gate 		return (CFGA_ERROR);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	optstr = config_calloc_check(1, strlen(selectp) + 1);
9600Sstevel@tonic-gate 	if (optstr == NULL) {
9610Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	(void) strcpy(optstr, selectp);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/* Init */
9670Sstevel@tonic-gate 	ret = CFGA_ATTR_INVAL;
9680Sstevel@tonic-gate 	bal = 0;
9690Sstevel@tonic-gate 	cp = attr = optstr;
9700Sstevel@tonic-gate 	state = CFGA_PSTATE_INIT;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	for (; *cp != '\0'; cp++) {
9730Sstevel@tonic-gate 		switch (state) {
9740Sstevel@tonic-gate 		case CFGA_PSTATE_INIT:
9750Sstevel@tonic-gate 			if (*cp != LEFT_PAREN)
9760Sstevel@tonic-gate 				break;
9770Sstevel@tonic-gate 			*cp = '\0';
9780Sstevel@tonic-gate 			val = cp + 1;
9790Sstevel@tonic-gate 			bal = 1;
9800Sstevel@tonic-gate 			state = CFGA_PSTATE_ATTR_DONE;
9810Sstevel@tonic-gate 			break;
9820Sstevel@tonic-gate 		case CFGA_PSTATE_ATTR_DONE:
9830Sstevel@tonic-gate 			chr = *cp;
9840Sstevel@tonic-gate 			switch (chr) {
9850Sstevel@tonic-gate 			case LEFT_PAREN:
9860Sstevel@tonic-gate 				bal++;
9870Sstevel@tonic-gate 				break;
9880Sstevel@tonic-gate 			case RIGHT_PAREN:
9890Sstevel@tonic-gate 				bal--;
9900Sstevel@tonic-gate 				if (bal == 0) {
9910Sstevel@tonic-gate 					*cp = '\0';
9920Sstevel@tonic-gate 					state = CFGA_PSTATE_VAL_DONE;
9930Sstevel@tonic-gate 				}
9940Sstevel@tonic-gate 				break;
9950Sstevel@tonic-gate 			}
9960Sstevel@tonic-gate 			break;
9970Sstevel@tonic-gate 		case CFGA_PSTATE_VAL_DONE:
9980Sstevel@tonic-gate 			if (*cp != ':') {
9990Sstevel@tonic-gate 				state = CFGA_PSTATE_ERR;
10000Sstevel@tonic-gate 				goto out;
10010Sstevel@tonic-gate 			}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 			*cp = '\0';
10040Sstevel@tonic-gate 			if (set_attrval(attr, val, post_filtp,
10050Sstevel@tonic-gate 			    match_type) != CFGA_OK) {
10060Sstevel@tonic-gate 				state = CFGA_PSTATE_ERR;
10070Sstevel@tonic-gate 				goto out;
10080Sstevel@tonic-gate 			}
10090Sstevel@tonic-gate 			state = CFGA_PSTATE_INIT;
10100Sstevel@tonic-gate 			attr = cp + 1;
10110Sstevel@tonic-gate 			break;
10120Sstevel@tonic-gate 		default:
10130Sstevel@tonic-gate 			state = CFGA_PSTATE_ERR;
10140Sstevel@tonic-gate 			/* FALLTHROUGH */
10150Sstevel@tonic-gate 		case CFGA_PSTATE_ERR:
10160Sstevel@tonic-gate 			goto out;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*FALLTHRU*/
10210Sstevel@tonic-gate out:
10220Sstevel@tonic-gate 	if (state == CFGA_PSTATE_VAL_DONE) {
10230Sstevel@tonic-gate 		ret = set_attrval(attr, val, post_filtp, match_type);
10240Sstevel@tonic-gate 	} else {
10250Sstevel@tonic-gate 		ret = CFGA_ATTR_INVAL;
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (ret != CFGA_OK) {
10290Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(inval_optarg), cmdname,
10300Sstevel@tonic-gate 		    selectp, list_options[LIST_SELECT]);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	S_FREE(optstr);
10340Sstevel@tonic-gate 	return (ret);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate static cfga_err_t
setup_filter(const char * selectp,const char * matchp,post_filter_t * post_filtp,char ** prefilt_optpp)10400Sstevel@tonic-gate setup_filter(
10410Sstevel@tonic-gate 	const char *selectp,
10420Sstevel@tonic-gate 	const char *matchp,
10430Sstevel@tonic-gate 	post_filter_t *post_filtp,
10440Sstevel@tonic-gate 	char **prefilt_optpp)
10450Sstevel@tonic-gate {
10460Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
10470Sstevel@tonic-gate 	match_type_t match_type = CFGA_MATCH_NOFILTER;
10480Sstevel@tonic-gate 	int i;
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	static char match_needs_select[] =
10510Sstevel@tonic-gate 	    "%s: %s suboption can only be used with %s suboption.\n";
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	*prefilt_optpp = NULL;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/*
10570Sstevel@tonic-gate 	 * Initial: no filtering.
10580Sstevel@tonic-gate 	 * CFGA_MATCH_NOFILTER is NOT a valid user input
10590Sstevel@tonic-gate 	 */
10600Sstevel@tonic-gate 	for (i = 0; i < N_FIELDS; i++) {
10610Sstevel@tonic-gate 		post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	/* Determine type of match */
10650Sstevel@tonic-gate 	if (matchp == NULL && selectp == NULL) {
10660Sstevel@tonic-gate 		/* No filtering */
10670Sstevel@tonic-gate 		return (CFGA_OK);
10680Sstevel@tonic-gate 	} else if (matchp == NULL && selectp != NULL) {
10690Sstevel@tonic-gate 		match_type = CFGA_DEFAULT_MATCH;
10700Sstevel@tonic-gate 	} else if (matchp != NULL && selectp == NULL) {
10710Sstevel@tonic-gate 		/* If only match specified, select criteria also needed */
10720Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(match_needs_select),
10730Sstevel@tonic-gate 		    cmdname, list_options[LIST_MATCH],
10740Sstevel@tonic-gate 		    list_options[LIST_SELECT]);
10750Sstevel@tonic-gate 		return (CFGA_ERROR);
10760Sstevel@tonic-gate 	} else {
10770Sstevel@tonic-gate 		for (i = 0; i < N_MATCH_TYPES; i++) {
10780Sstevel@tonic-gate 			if (strcmp(matchp, match_type_array[i].str) == 0) {
10790Sstevel@tonic-gate 				match_type = match_type_array[i].type;
10800Sstevel@tonic-gate 				break;
10810Sstevel@tonic-gate 			}
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 		if (i >= N_MATCH_TYPES) {
10840Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(inval_optarg), cmdname,
10850Sstevel@tonic-gate 			    matchp, list_options[LIST_MATCH]);
10860Sstevel@tonic-gate 			return (CFGA_ERROR);
10870Sstevel@tonic-gate 		}
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if ((ret = parse_select_opt(selectp, post_filtp, match_type))
10910Sstevel@tonic-gate 	    != CFGA_OK) {
10920Sstevel@tonic-gate 		return (ret);
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	/* Handle pre-filtering. */
10960Sstevel@tonic-gate 	if ((ret = setup_prefilter(post_filtp, prefilt_optpp)) != CFGA_OK) {
10970Sstevel@tonic-gate 		/* Cleanup */
10980Sstevel@tonic-gate 		for (i = 0; i < N_FIELDS; i++) {
10990Sstevel@tonic-gate 			post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 		return (ret);
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	return (CFGA_OK);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate  * compare_ap_id - compare two ap_id's
11100Sstevel@tonic-gate  *
11110Sstevel@tonic-gate  * For partial matches, argument order is significant. The filtering criterion
11120Sstevel@tonic-gate  * should be the first argument.
11130Sstevel@tonic-gate  */
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate static int
compare_ap_id(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)11160Sstevel@tonic-gate compare_ap_id(
11170Sstevel@tonic-gate 	cfga_list_data_t *p1,
11180Sstevel@tonic-gate 	cfga_list_data_t *p2,
11190Sstevel@tonic-gate 	match_type_t match_type)
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	switch (match_type) {
11230Sstevel@tonic-gate 		case CFGA_MATCH_NOFILTER:
11240Sstevel@tonic-gate 			return (0);	/* No filtering. all pass */
11250Sstevel@tonic-gate 		case CFGA_MATCH_PARTIAL:
11260Sstevel@tonic-gate 			return (strncmp(p1->ap_log_id, p2->ap_log_id,
11270Sstevel@tonic-gate 			    strlen(p1->ap_log_id)));
11280Sstevel@tonic-gate 		case CFGA_MATCH_EXACT:
11290Sstevel@tonic-gate 			return (strcmp(p1->ap_log_id, p2->ap_log_id));
11300Sstevel@tonic-gate 		case CFGA_MATCH_ORDER:
11310Sstevel@tonic-gate 		default:
11320Sstevel@tonic-gate 			return (config_ap_id_cmp(p1->ap_log_id, p2->ap_log_id));
11330Sstevel@tonic-gate 	}
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate /*
11370Sstevel@tonic-gate  * print_log_id - print logical ap_id
11380Sstevel@tonic-gate  */
11390Sstevel@tonic-gate static void
print_log_id(cfga_list_data_t * p,int width,char * lp)11400Sstevel@tonic-gate print_log_id(
11410Sstevel@tonic-gate 	cfga_list_data_t *p,
11420Sstevel@tonic-gate 	int width,
11430Sstevel@tonic-gate 	char *lp)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_log_id),
11460Sstevel@tonic-gate 	    p->ap_log_id);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * set_log_flt - Setup filter for logical ap_id
11510Sstevel@tonic-gate  */
11520Sstevel@tonic-gate static cfga_err_t
set_log_flt(cfga_list_data_t * p,const char * val)11530Sstevel@tonic-gate set_log_flt(
11540Sstevel@tonic-gate 	cfga_list_data_t *p,
11550Sstevel@tonic-gate 	const char *val)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	if (strlen(val) > sizeof (p->ap_log_id) - 1)
11580Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	(void) strcpy(p->ap_log_id, val);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	return (CFGA_OK);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate  * set_type_flt - Setup filter for type field
11670Sstevel@tonic-gate  */
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate static cfga_err_t
set_type_flt(cfga_list_data_t * p,const char * val)11700Sstevel@tonic-gate set_type_flt(
11710Sstevel@tonic-gate 	cfga_list_data_t *p,
11720Sstevel@tonic-gate 	const char *val)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	if (strlen(val) > sizeof (p->ap_type) - 1)
11750Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	(void) strcpy(p->ap_type, val);
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	return (CFGA_OK);
11800Sstevel@tonic-gate }
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate /*
11830Sstevel@tonic-gate  * set_class_flt - Setup filter for class field
11840Sstevel@tonic-gate  */
11850Sstevel@tonic-gate static cfga_err_t
set_class_flt(cfga_list_data_t * p,const char * val)11860Sstevel@tonic-gate set_class_flt(
11870Sstevel@tonic-gate 	cfga_list_data_t *p,
11880Sstevel@tonic-gate 	const char *val)
11890Sstevel@tonic-gate {
11900Sstevel@tonic-gate 	if (strlen(val) > sizeof (p->ap_class) - 1)
11910Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	(void) strcpy(p->ap_class, val);
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	return (CFGA_OK);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate /*
12000Sstevel@tonic-gate  * compare_r_state - compare receptacle state of two ap_id's
12010Sstevel@tonic-gate  */
12020Sstevel@tonic-gate static int
compare_r_state(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)12030Sstevel@tonic-gate compare_r_state(
12040Sstevel@tonic-gate 	cfga_list_data_t *p1,
12050Sstevel@tonic-gate 	cfga_list_data_t *p2,
12060Sstevel@tonic-gate 	match_type_t match_type)
12070Sstevel@tonic-gate {
12080Sstevel@tonic-gate 	switch (match_type) {
12090Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:  /* no filtering. pass all */
12100Sstevel@tonic-gate 		return (0);
12110Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
12120Sstevel@tonic-gate 	default:
12130Sstevel@tonic-gate 		return (p1->ap_r_state - p2->ap_r_state);
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate }
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate /*
12180Sstevel@tonic-gate  * compare_o_state - compare occupant state of two ap_id's
12190Sstevel@tonic-gate  */
12200Sstevel@tonic-gate static int
compare_o_state(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)12210Sstevel@tonic-gate compare_o_state(
12220Sstevel@tonic-gate 	cfga_list_data_t *p1,
12230Sstevel@tonic-gate 	cfga_list_data_t *p2,
12240Sstevel@tonic-gate 	match_type_t match_type)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	switch (match_type) {
12270Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:	/* no filtering. all pass */
12280Sstevel@tonic-gate 		return (0);
12290Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
12300Sstevel@tonic-gate 	default:
12310Sstevel@tonic-gate 		return (p1->ap_o_state - p2->ap_o_state);
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate  * compare_busy - compare busy field of two ap_id's
12370Sstevel@tonic-gate  */
12380Sstevel@tonic-gate static int
compare_busy(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)12390Sstevel@tonic-gate compare_busy(
12400Sstevel@tonic-gate 	cfga_list_data_t *p1,
12410Sstevel@tonic-gate 	cfga_list_data_t *p2,
12420Sstevel@tonic-gate 	match_type_t match_type)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	switch (match_type) {
12460Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:	/* no filtering. all pass */
12470Sstevel@tonic-gate 		return (0);
12480Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
12490Sstevel@tonic-gate 	default:
12500Sstevel@tonic-gate 		return (p1->ap_busy - p2->ap_busy);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate /*
12550Sstevel@tonic-gate  * print_r_state - print receptacle state
12560Sstevel@tonic-gate  */
12570Sstevel@tonic-gate static void
print_r_state(cfga_list_data_t * p,int width,char * lp)12580Sstevel@tonic-gate print_r_state(
12590Sstevel@tonic-gate 	cfga_list_data_t *p,
12600Sstevel@tonic-gate 	int width,
12610Sstevel@tonic-gate 	char *lp)
12620Sstevel@tonic-gate {
12630Sstevel@tonic-gate 	char *cp;
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	switch (p->ap_r_state) {
12660Sstevel@tonic-gate 	case CFGA_STAT_EMPTY:
12670Sstevel@tonic-gate 		cp = "empty";
12680Sstevel@tonic-gate 		break;
12690Sstevel@tonic-gate 	case CFGA_STAT_CONNECTED:
12700Sstevel@tonic-gate 		cp = "connected";
12710Sstevel@tonic-gate 		break;
12720Sstevel@tonic-gate 	case CFGA_STAT_DISCONNECTED:
12730Sstevel@tonic-gate 		cp = "disconnected";
12740Sstevel@tonic-gate 		break;
12750Sstevel@tonic-gate 	default:
12760Sstevel@tonic-gate 		cp = "???";
12770Sstevel@tonic-gate 		break;
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, cp);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate  * print_o_state - print occupant state
12840Sstevel@tonic-gate  */
12850Sstevel@tonic-gate static void
print_o_state(cfga_list_data_t * p,int width,char * lp)12860Sstevel@tonic-gate print_o_state(
12870Sstevel@tonic-gate 	cfga_list_data_t *p,
12880Sstevel@tonic-gate 	int width,
12890Sstevel@tonic-gate 	char *lp)
12900Sstevel@tonic-gate {
12910Sstevel@tonic-gate 	char *cp;
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	switch (p->ap_o_state) {
12940Sstevel@tonic-gate 	case CFGA_STAT_UNCONFIGURED:
12950Sstevel@tonic-gate 		cp = "unconfigured";
12960Sstevel@tonic-gate 		break;
12970Sstevel@tonic-gate 	case CFGA_STAT_CONFIGURED:
12980Sstevel@tonic-gate 		cp = "configured";
12990Sstevel@tonic-gate 		break;
13000Sstevel@tonic-gate 	default:
13010Sstevel@tonic-gate 		cp = "???";
13020Sstevel@tonic-gate 		break;
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, cp);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate /*
13080Sstevel@tonic-gate  * compare_cond - compare condition field of two ap_id's
13090Sstevel@tonic-gate  */
13100Sstevel@tonic-gate static int
compare_cond(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)13110Sstevel@tonic-gate compare_cond(
13120Sstevel@tonic-gate 	cfga_list_data_t *p1,
13130Sstevel@tonic-gate 	cfga_list_data_t *p2,
13140Sstevel@tonic-gate 	match_type_t match_type)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	switch (match_type) {
13180Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
13190Sstevel@tonic-gate 		return (0);
13200Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
13210Sstevel@tonic-gate 	default:
13220Sstevel@tonic-gate 		return (p1->ap_cond - p2->ap_cond);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate  * print_cond - print attachment point condition
13280Sstevel@tonic-gate  */
13290Sstevel@tonic-gate static void
print_cond(cfga_list_data_t * p,int width,char * lp)13300Sstevel@tonic-gate print_cond(
13310Sstevel@tonic-gate 	cfga_list_data_t *p,
13320Sstevel@tonic-gate 	int width,
13330Sstevel@tonic-gate 	char *lp)
13340Sstevel@tonic-gate {
13350Sstevel@tonic-gate 	char *cp;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	switch (p->ap_cond) {
13380Sstevel@tonic-gate 	case CFGA_COND_UNKNOWN:
13390Sstevel@tonic-gate 		cp = "unknown";
13400Sstevel@tonic-gate 		break;
13410Sstevel@tonic-gate 	case CFGA_COND_UNUSABLE:
13420Sstevel@tonic-gate 		cp = "unusable";
13430Sstevel@tonic-gate 		break;
13440Sstevel@tonic-gate 	case CFGA_COND_FAILING:
13450Sstevel@tonic-gate 		cp = "failing";
13460Sstevel@tonic-gate 		break;
13470Sstevel@tonic-gate 	case CFGA_COND_FAILED:
13480Sstevel@tonic-gate 		cp = "failed";
13490Sstevel@tonic-gate 		break;
13500Sstevel@tonic-gate 	case CFGA_COND_OK:
13510Sstevel@tonic-gate 		cp = "ok";
13520Sstevel@tonic-gate 		break;
13530Sstevel@tonic-gate 	default:
13540Sstevel@tonic-gate 		cp = "???";
13550Sstevel@tonic-gate 		break;
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, cp);
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate /*
13610Sstevel@tonic-gate  * compare_time - compare time field of two ap_id's
13620Sstevel@tonic-gate  */
13630Sstevel@tonic-gate static int
compare_time(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)13640Sstevel@tonic-gate compare_time(
13650Sstevel@tonic-gate 	cfga_list_data_t *p1,
13660Sstevel@tonic-gate 	cfga_list_data_t *p2,
13670Sstevel@tonic-gate 	match_type_t match_type)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	switch (match_type) {
13700Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
13710Sstevel@tonic-gate 		return (0);
13720Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
13730Sstevel@tonic-gate 	default:
13740Sstevel@tonic-gate 		return (p1->ap_status_time - p2->ap_status_time);
13750Sstevel@tonic-gate 	}
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate /*
13800Sstevel@tonic-gate  * print_time - print time from cfga_list_data.
13810Sstevel@tonic-gate  * Time print based on ls(1).
13820Sstevel@tonic-gate  */
13830Sstevel@tonic-gate static void
print_time(cfga_list_data_t * p,int width,char * lp)13840Sstevel@tonic-gate print_time(
13850Sstevel@tonic-gate 	cfga_list_data_t *p,
13860Sstevel@tonic-gate 	int width,
13870Sstevel@tonic-gate 	char *lp)
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	static time_t   year, now;
13900Sstevel@tonic-gate 	time_t stime;
13910Sstevel@tonic-gate 	char	time_buf[50];	/* array to hold day and time */
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	if (year == 0) {
13940Sstevel@tonic-gate 		now = time((long *)NULL);
13950Sstevel@tonic-gate 		year = now - 6L*30L*24L*60L*60L; /* 6 months ago */
13960Sstevel@tonic-gate 		now = now + 60;
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 	stime = p->ap_status_time;
13990Sstevel@tonic-gate 	if (stime == (time_t)-1) {
14000Sstevel@tonic-gate 		(void) sprintf(lp, "%-*s", width, gettext("unavailable"));
14010Sstevel@tonic-gate 		return;
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	if ((stime < year) || (stime > now)) {
14050Sstevel@tonic-gate 		(void) strftime(time_buf, sizeof (time_buf),
14060Sstevel@tonic-gate 		    dcgettext(NULL, FORMAT1, LC_TIME), localtime(&stime));
14070Sstevel@tonic-gate 	} else {
14080Sstevel@tonic-gate 		(void) strftime(time_buf, sizeof (time_buf),
14090Sstevel@tonic-gate 		    dcgettext(NULL, FORMAT2, LC_TIME), localtime(&stime));
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, time_buf);
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate /*
14150Sstevel@tonic-gate  * print_time_p - print time from cfga_list_data.
14160Sstevel@tonic-gate  */
14170Sstevel@tonic-gate static void
print_time_p(cfga_list_data_t * p,int width,char * lp)14180Sstevel@tonic-gate print_time_p(
14190Sstevel@tonic-gate 	cfga_list_data_t *p,
14200Sstevel@tonic-gate 	int width,
14210Sstevel@tonic-gate 	char *lp)
14220Sstevel@tonic-gate {
14230Sstevel@tonic-gate 	struct tm *tp;
14240Sstevel@tonic-gate 	char tstr[TIME_P_WIDTH+1];
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	tp = localtime(&p->ap_status_time);
14270Sstevel@tonic-gate 	(void) sprintf(tstr, "%04d%02d%02d%02d%02d%02d", tp->tm_year + 1900,
14280Sstevel@tonic-gate 	    tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
14290Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, tstr);
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate /*
14330Sstevel@tonic-gate  * compare_info - compare info from two cfga_list_data structs
14340Sstevel@tonic-gate  */
14350Sstevel@tonic-gate static int
compare_info(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)14360Sstevel@tonic-gate compare_info(
14370Sstevel@tonic-gate 	cfga_list_data_t *p1,
14380Sstevel@tonic-gate 	cfga_list_data_t *p2,
14390Sstevel@tonic-gate 	match_type_t match_type)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate 	switch (match_type) {
14420Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
14430Sstevel@tonic-gate 		return (0);
14440Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
14450Sstevel@tonic-gate 	default:
14460Sstevel@tonic-gate 		return (strncmp(p1->ap_info, p2->ap_info,
14470Sstevel@tonic-gate 		    sizeof (p2->ap_info)));
14480Sstevel@tonic-gate 	}
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate /*
14520Sstevel@tonic-gate  * print_info - print info from cfga_list_data struct
14530Sstevel@tonic-gate  */
14540Sstevel@tonic-gate static void
print_info(cfga_list_data_t * p,int width,char * lp)14550Sstevel@tonic-gate print_info(
14560Sstevel@tonic-gate 	cfga_list_data_t *p,
14570Sstevel@tonic-gate 	int width,
14580Sstevel@tonic-gate 	char *lp)
14590Sstevel@tonic-gate {
14600Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_info), p->ap_info);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate /*
14640Sstevel@tonic-gate  * compare_type - compare type from two cfga_list_data structs
14650Sstevel@tonic-gate  *
14660Sstevel@tonic-gate  * For partial matches, argument order is significant. The filtering criterion
14670Sstevel@tonic-gate  * should be the first argument.
14680Sstevel@tonic-gate  */
14690Sstevel@tonic-gate static int
compare_type(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)14700Sstevel@tonic-gate compare_type(
14710Sstevel@tonic-gate 	cfga_list_data_t *p1,
14720Sstevel@tonic-gate 	cfga_list_data_t *p2,
14730Sstevel@tonic-gate 	match_type_t match_type)
14740Sstevel@tonic-gate {
14750Sstevel@tonic-gate 	switch (match_type) {
14760Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
14770Sstevel@tonic-gate 		return (0);
14780Sstevel@tonic-gate 	case CFGA_MATCH_PARTIAL:
14790Sstevel@tonic-gate 		return (strncmp(p1->ap_type, p2->ap_type, strlen(p1->ap_type)));
14800Sstevel@tonic-gate 	case CFGA_MATCH_EXACT:
14810Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
14820Sstevel@tonic-gate 	default:
14830Sstevel@tonic-gate 		return (strncmp(p1->ap_type, p2->ap_type,
14840Sstevel@tonic-gate 		    sizeof (p2->ap_type)));
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate /*
14890Sstevel@tonic-gate  * print_type - print type from cfga_list_data struct
14900Sstevel@tonic-gate  */
14910Sstevel@tonic-gate static void
print_type(cfga_list_data_t * p,int width,char * lp)14920Sstevel@tonic-gate print_type(
14930Sstevel@tonic-gate 	cfga_list_data_t *p,
14940Sstevel@tonic-gate 	int width,
14950Sstevel@tonic-gate 	char *lp)
14960Sstevel@tonic-gate {
14970Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_type), p->ap_type);
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate /*
15020Sstevel@tonic-gate  * compare_class - compare class from two cfga_list_data structs
15030Sstevel@tonic-gate  *
15040Sstevel@tonic-gate  * For partial matches, argument order is significant. The filtering criterion
15050Sstevel@tonic-gate  * should be the first argument.
15060Sstevel@tonic-gate  */
15070Sstevel@tonic-gate static int
compare_class(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)15080Sstevel@tonic-gate compare_class(
15090Sstevel@tonic-gate 	cfga_list_data_t *p1,
15100Sstevel@tonic-gate 	cfga_list_data_t *p2,
15110Sstevel@tonic-gate 	match_type_t match_type)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	switch (match_type) {
15150Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
15160Sstevel@tonic-gate 		return (0);
15170Sstevel@tonic-gate 	case CFGA_MATCH_PARTIAL:
15180Sstevel@tonic-gate 		return (strncmp(p1->ap_class, p2->ap_class,
15190Sstevel@tonic-gate 		    strlen(p1->ap_class)));
15200Sstevel@tonic-gate 	case CFGA_MATCH_EXACT:
15210Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
15220Sstevel@tonic-gate 	default:
15230Sstevel@tonic-gate 		return (strncmp(p1->ap_class, p2->ap_class,
15240Sstevel@tonic-gate 		    sizeof (p2->ap_class)));
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate }
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate /*
15290Sstevel@tonic-gate  * print_class - print class from cfga_list_data struct
15300Sstevel@tonic-gate  */
15310Sstevel@tonic-gate static void
print_class(cfga_list_data_t * p,int width,char * lp)15320Sstevel@tonic-gate print_class(
15330Sstevel@tonic-gate 	cfga_list_data_t *p,
15340Sstevel@tonic-gate 	int width,
15350Sstevel@tonic-gate 	char *lp)
15360Sstevel@tonic-gate {
15370Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_class), p->ap_class);
15380Sstevel@tonic-gate }
15390Sstevel@tonic-gate /*
15400Sstevel@tonic-gate  * print_busy - print busy from cfga_list_data struct
15410Sstevel@tonic-gate  */
15420Sstevel@tonic-gate /* ARGSUSED */
15430Sstevel@tonic-gate static void
print_busy(cfga_list_data_t * p,int width,char * lp)15440Sstevel@tonic-gate print_busy(
15450Sstevel@tonic-gate 	cfga_list_data_t *p,
15460Sstevel@tonic-gate 	int width,
15470Sstevel@tonic-gate 	char *lp)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate 	if (p->ap_busy)
15500Sstevel@tonic-gate 		(void) sprintf(lp, "%-*.*s", width, width, "y");
15510Sstevel@tonic-gate 	else
15520Sstevel@tonic-gate 		(void) sprintf(lp, "%-*.*s", width, width, "n");
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate /*
15560Sstevel@tonic-gate  * print_phys_id - print physical ap_id
15570Sstevel@tonic-gate  */
15580Sstevel@tonic-gate static void
print_phys_id(cfga_list_data_t * p,int width,char * lp)15590Sstevel@tonic-gate print_phys_id(
15600Sstevel@tonic-gate 	cfga_list_data_t *p,
15610Sstevel@tonic-gate 	int width,
15620Sstevel@tonic-gate 	char *lp)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_phys_id),
15650Sstevel@tonic-gate 	    p->ap_phys_id);
15660Sstevel@tonic-gate }
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate /*
15700Sstevel@tonic-gate  * find_field - find the named field
15710Sstevel@tonic-gate  */
15720Sstevel@tonic-gate static struct field_info *
find_field(char * fname)15730Sstevel@tonic-gate find_field(char *fname)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate 	struct field_info *fldp;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++)
15780Sstevel@tonic-gate 		if (strcmp(fname, fldp->name) == 0)
15790Sstevel@tonic-gate 			return (fldp);
15800Sstevel@tonic-gate 	return (NULL);
15810Sstevel@tonic-gate }
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * usage_field - print field usage
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate static void
usage_field()15870Sstevel@tonic-gate usage_field()
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate 	struct field_info *fldp = NULL;
15900Sstevel@tonic-gate 	const char *sep;
15910Sstevel@tonic-gate 	static char field_list[] = "%s: print or sort fields must be one of:";
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(field_list), cmdname);
15940Sstevel@tonic-gate 	sep = "";
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++) {
15970Sstevel@tonic-gate 		(void) fprintf(stderr, "%s %s", sep, fldp->name);
15980Sstevel@tonic-gate 		sep = ",";
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate /*
16040Sstevel@tonic-gate  * compare_null - null comparison routine
16050Sstevel@tonic-gate  */
16060Sstevel@tonic-gate /*ARGSUSED*/
16070Sstevel@tonic-gate static int
compare_null(cfga_list_data_t * p1,cfga_list_data_t * p2,match_type_t match_type)16080Sstevel@tonic-gate compare_null(
16090Sstevel@tonic-gate 	cfga_list_data_t *p1,
16100Sstevel@tonic-gate 	cfga_list_data_t *p2,
16110Sstevel@tonic-gate 	match_type_t match_type)
16120Sstevel@tonic-gate {
16130Sstevel@tonic-gate 	return (0);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate /*
16170Sstevel@tonic-gate  * print_null - print out a field of spaces
16180Sstevel@tonic-gate  */
16190Sstevel@tonic-gate /*ARGSUSED*/
16200Sstevel@tonic-gate static void
print_null(cfga_list_data_t * p,int width,char * lp)16210Sstevel@tonic-gate print_null(
16220Sstevel@tonic-gate 	cfga_list_data_t *p,
16230Sstevel@tonic-gate 	int width,
16240Sstevel@tonic-gate 	char *lp)
16250Sstevel@tonic-gate {
16260Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, "");
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate /*
16300Sstevel@tonic-gate  * do_config_list - directs the output of the listing functions
16310Sstevel@tonic-gate  */
16320Sstevel@tonic-gate static int
do_config_list(int l_argc,char * l_argv[],cfga_list_data_t * statlist,int nlist,char * sortp,char * colsp,char * cols2p,int noheadings,char * delimp,post_filter_t * post_filtp,int dyn_exp)16330Sstevel@tonic-gate do_config_list(
16340Sstevel@tonic-gate 	int l_argc,
16350Sstevel@tonic-gate 	char *l_argv[],
16360Sstevel@tonic-gate 	cfga_list_data_t *statlist,
16370Sstevel@tonic-gate 	int nlist,
16380Sstevel@tonic-gate 	char *sortp,
16390Sstevel@tonic-gate 	char *colsp,
16400Sstevel@tonic-gate 	char *cols2p,
16410Sstevel@tonic-gate 	int noheadings,
16420Sstevel@tonic-gate 	char *delimp,
16430Sstevel@tonic-gate 	post_filter_t *post_filtp,
16440Sstevel@tonic-gate 	int dyn_exp)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	int nprcols = 0, ncols2 = 0;
16470Sstevel@tonic-gate 	struct print_col *prnt_list = NULL;
16480Sstevel@tonic-gate 	int napids_to_list = 0;
16490Sstevel@tonic-gate 	FILE *fp = NULL;
16500Sstevel@tonic-gate 	int f_err;
16510Sstevel@tonic-gate 	cfga_list_data_t **sel_boards = NULL;
16520Sstevel@tonic-gate 	int nsel = 0;
16530Sstevel@tonic-gate 	int i, j;
16540Sstevel@tonic-gate 	cfga_err_t ret;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	ap_arg_t *arg_array = NULL;
16570Sstevel@tonic-gate 	ap_out_t *out_array = NULL;
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	sort_list = NULL;
16610Sstevel@tonic-gate 	f_err = 0;
16620Sstevel@tonic-gate 	fp = stdout;
16630Sstevel@tonic-gate 	nsort_list = count_fields(sortp, FDELIM);
16640Sstevel@tonic-gate 	if (nsort_list != 0) {
16650Sstevel@tonic-gate 		sort_list = config_calloc_check(nsort_list,
16660Sstevel@tonic-gate 		    sizeof (*sort_list));
16670Sstevel@tonic-gate 		if (sort_list == NULL) {
16680Sstevel@tonic-gate 			ret = CFGA_LIB_ERROR;
16690Sstevel@tonic-gate 			goto out;
16700Sstevel@tonic-gate 		}
16710Sstevel@tonic-gate 		f_err |= process_sort_fields(nsort_list, sort_list, sortp);
16720Sstevel@tonic-gate 	} else
16730Sstevel@tonic-gate 		sort_list = NULL;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	nprcols = count_fields(colsp, FDELIM);
16760Sstevel@tonic-gate 	if ((ncols2 = count_fields(cols2p, FDELIM)) > nprcols)
16770Sstevel@tonic-gate 		nprcols = ncols2;
16780Sstevel@tonic-gate 	if (nprcols != 0) {
16790Sstevel@tonic-gate 		prnt_list = config_calloc_check(nprcols, sizeof (*prnt_list));
16800Sstevel@tonic-gate 		if (prnt_list == NULL) {
16810Sstevel@tonic-gate 			ret = CFGA_LIB_ERROR;
16820Sstevel@tonic-gate 			goto out;
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 		f_err |= process_fields(nprcols, prnt_list, 0, colsp);
16850Sstevel@tonic-gate 		if (ncols2 != 0)
16860Sstevel@tonic-gate 			f_err |= process_fields(nprcols, prnt_list, 1, cols2p);
16870Sstevel@tonic-gate 	} else
16880Sstevel@tonic-gate 		prnt_list = NULL;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	if (f_err) {
16910Sstevel@tonic-gate 		usage_field();
16920Sstevel@tonic-gate 		ret = CFGA_ERROR;
16930Sstevel@tonic-gate 		goto out;
16940Sstevel@tonic-gate 	}
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	/* Create an array of all user args (if any) */
16970Sstevel@tonic-gate 	if (l_argc != 0) {
16980Sstevel@tonic-gate 		int i, j;
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 		napids_to_list = 0;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 		for (i = 0; i < l_argc; i++) {
17030Sstevel@tonic-gate 			napids_to_list += count_fields(l_argv[i], ARG_DELIM);
17040Sstevel@tonic-gate 		}
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 		arg_array = config_calloc_check(napids_to_list,
17070Sstevel@tonic-gate 		    sizeof (*arg_array));
17080Sstevel@tonic-gate 		if (arg_array == NULL) {
17090Sstevel@tonic-gate 			ret = CFGA_LIB_ERROR;
17100Sstevel@tonic-gate 			goto out;
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 		for (i = 0, j = 0; i < l_argc; i++) {
17140Sstevel@tonic-gate 			int n;
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 			n = count_fields(l_argv[i], ARG_DELIM);
17170Sstevel@tonic-gate 			if (n == 0) {
17180Sstevel@tonic-gate 				continue;
17190Sstevel@tonic-gate 			} else if (n == 1) {
17200Sstevel@tonic-gate 				arg_array[j].arg = l_argv[i];
17210Sstevel@tonic-gate 				arg_array[j].resp = 0;
17220Sstevel@tonic-gate 				j++;
17230Sstevel@tonic-gate 			} else {
17240Sstevel@tonic-gate 				char *cp, *ncp;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 				cp = l_argv[i];
17270Sstevel@tonic-gate 				for (;;) {
17280Sstevel@tonic-gate 					arg_array[j].arg = cp;
17290Sstevel@tonic-gate 					arg_array[j].resp = 0;
17300Sstevel@tonic-gate 					j++;
17310Sstevel@tonic-gate 					ncp = strchr(cp, ARG_DELIM);
17320Sstevel@tonic-gate 					if (ncp == NULL)
17330Sstevel@tonic-gate 						break;
17340Sstevel@tonic-gate 					*ncp = '\0';
17350Sstevel@tonic-gate 					cp = ncp + 1;
17360Sstevel@tonic-gate 				}
17370Sstevel@tonic-gate 			}
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 		assert(j == napids_to_list);
17400Sstevel@tonic-gate 	} else {
17410Sstevel@tonic-gate 		napids_to_list = 0;
17420Sstevel@tonic-gate 		arg_array = NULL;
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	assert(nlist != 0);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	out_array = config_calloc_check(nlist, sizeof (*out_array));
17480Sstevel@tonic-gate 	if (out_array == NULL) {
17490Sstevel@tonic-gate 		ret = CFGA_LIB_ERROR;
17500Sstevel@tonic-gate 		goto out;
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/* create a list of output stat data */
17550Sstevel@tonic-gate 	for (i = 0; i < nlist; i++) {
17560Sstevel@tonic-gate 		out_array[i].ldatap = &statlist[i];
17570Sstevel@tonic-gate 		out_array[i].req = 0;
17580Sstevel@tonic-gate 	}
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	/*
17610Sstevel@tonic-gate 	 * Mark all user input which got atleast 1 stat data in response
17620Sstevel@tonic-gate 	 */
17630Sstevel@tonic-gate 	for (i = 0; i < napids_to_list; i++) {
17640Sstevel@tonic-gate 		arg_got_resp(&arg_array[i], out_array, nlist, dyn_exp);
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	/*
17680Sstevel@tonic-gate 	 * Process output data
17690Sstevel@tonic-gate 	 */
17700Sstevel@tonic-gate 	nsel = 0;
17710Sstevel@tonic-gate 	for (i = 0; i < nlist; i++) {
17720Sstevel@tonic-gate 		/*
17730Sstevel@tonic-gate 		 * Mark all the stats which were actually requested by user
17740Sstevel@tonic-gate 		 */
17750Sstevel@tonic-gate 		out_was_req(&out_array[i], arg_array, napids_to_list, 0);
17760Sstevel@tonic-gate 		if (out_array[i].req == 0 && dyn_exp) {
17770Sstevel@tonic-gate 			/*
17780Sstevel@tonic-gate 			 * Try again without the dynamic component for the
17790Sstevel@tonic-gate 			 * if dynamic expansion was requested.
17800Sstevel@tonic-gate 			 */
17810Sstevel@tonic-gate 			out_was_req(&out_array[i], arg_array,
17820Sstevel@tonic-gate 			    napids_to_list, 1);
17830Sstevel@tonic-gate 		}
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 		/*
17860Sstevel@tonic-gate 		 * post filter data which was actually requested
17870Sstevel@tonic-gate 		 */
17880Sstevel@tonic-gate 		if (out_array[i].req == 1) {
17890Sstevel@tonic-gate 			do_post_filter(&out_array[i], post_filtp, &nsel);
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	sel_boards = config_calloc_check(nsel, sizeof (*sel_boards));
17940Sstevel@tonic-gate 	if (sel_boards == NULL) {
17950Sstevel@tonic-gate 		ret = CFGA_LIB_ERROR;
17960Sstevel@tonic-gate 		goto out;
17970Sstevel@tonic-gate 	}
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	for (i = 0, j = 0; i < nlist; i++) {
18000Sstevel@tonic-gate 		if (out_array[i].req == 1) {
18010Sstevel@tonic-gate 			sel_boards[j] = out_array[i].ldatap;
18020Sstevel@tonic-gate 			j++;
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	assert(j == nsel);
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	/*
18090Sstevel@tonic-gate 	 * Print headings even if no list entries - Bug or feature ?
18100Sstevel@tonic-gate 	 */
18110Sstevel@tonic-gate 	if (!noheadings && prnt_list != NULL) {
18120Sstevel@tonic-gate 		if ((ret = print_fields(nprcols, prnt_list, 1, 0,
18130Sstevel@tonic-gate 		    delimp, NULL, fp)) != CFGA_OK) {
18140Sstevel@tonic-gate 			goto out;
18150Sstevel@tonic-gate 		}
18160Sstevel@tonic-gate 		if (ncols2 != 0) {
18170Sstevel@tonic-gate 			if ((ret = print_fields(nprcols, prnt_list, 1,
18180Sstevel@tonic-gate 			    1, delimp, NULL, fp)) != CFGA_OK) {
18190Sstevel@tonic-gate 				goto out;
18200Sstevel@tonic-gate 			}
18210Sstevel@tonic-gate 		}
18220Sstevel@tonic-gate 	}
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 	if (nsel != 0) {
18250Sstevel@tonic-gate 		if (sort_list != NULL && nsel > 1) {
18260Sstevel@tonic-gate 			qsort(sel_boards, nsel, sizeof (sel_boards[0]),
18270Sstevel@tonic-gate 			    ldata_compare);
18280Sstevel@tonic-gate 		}
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 		if (prnt_list != NULL) {
18310Sstevel@tonic-gate 			for (i = 0; i < nsel; i++) {
18320Sstevel@tonic-gate 				if ((ret = print_fields(nprcols,
18330Sstevel@tonic-gate 				    prnt_list, 0, 0, delimp, sel_boards[i], fp))
18340Sstevel@tonic-gate 				    != CFGA_OK)
18350Sstevel@tonic-gate 					goto out;
18360Sstevel@tonic-gate 				if (ncols2 != 0) {
18370Sstevel@tonic-gate 					if ((ret = print_fields(
18380Sstevel@tonic-gate 					    nprcols, prnt_list, 0, 1, delimp,
18390Sstevel@tonic-gate 					    sel_boards[i], fp)) != CFGA_OK)
18400Sstevel@tonic-gate 						goto out;
18410Sstevel@tonic-gate 				}
18420Sstevel@tonic-gate 			}
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 	/*
18460Sstevel@tonic-gate 	 * Go thru the argument list and notify user about args
18470Sstevel@tonic-gate 	 * which did not have a match
18480Sstevel@tonic-gate 	 */
18490Sstevel@tonic-gate 	report_no_response(arg_array, napids_to_list);
18500Sstevel@tonic-gate 	ret = CFGA_OK;
18510Sstevel@tonic-gate 	/*FALLTHRU*/
18520Sstevel@tonic-gate out:
18530Sstevel@tonic-gate 	S_FREE(sel_boards);
18540Sstevel@tonic-gate 	S_FREE(arg_array);
18550Sstevel@tonic-gate 	S_FREE(out_array);
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	S_FREE(sort_list);
18580Sstevel@tonic-gate 	S_FREE(prnt_list);
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	return (ret);
18610Sstevel@tonic-gate }
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate /*
18650Sstevel@tonic-gate  * Mark all user inputs which got a response
18660Sstevel@tonic-gate  */
18670Sstevel@tonic-gate static void
arg_got_resp(ap_arg_t * inp,ap_out_t * out_array,int nouts,int dyn_exp)18680Sstevel@tonic-gate arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts, int dyn_exp)
18690Sstevel@tonic-gate {
18700Sstevel@tonic-gate 	int i;
18710Sstevel@tonic-gate 	cfga_ap_types_t type;
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	if (nouts == 0) {
18750Sstevel@tonic-gate 		return;
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	type = find_arg_type(inp->arg);
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	/*
18810Sstevel@tonic-gate 	 * Go through list of output stats and check if argument
18820Sstevel@tonic-gate 	 * produced that output
18830Sstevel@tonic-gate 	 */
18840Sstevel@tonic-gate 	for (i = 0; i < nouts; i++) {
18850Sstevel@tonic-gate 		if (type == PHYSICAL_AP_ID) {
18860Sstevel@tonic-gate 			if (config_ap_id_cmp(out_array[i].ldatap->ap_phys_id,
18870Sstevel@tonic-gate 			    inp->arg) == 0) {
18880Sstevel@tonic-gate 				break;
18890Sstevel@tonic-gate 			}
18900Sstevel@tonic-gate 		} else if (type == LOGICAL_AP_ID) {
18910Sstevel@tonic-gate 			if (config_ap_id_cmp(out_array[i].ldatap->ap_log_id,
18920Sstevel@tonic-gate 			    inp->arg) == 0) {
18930Sstevel@tonic-gate 				break;
18940Sstevel@tonic-gate 			}
18950Sstevel@tonic-gate 		} else if (type == AP_TYPE) {
18960Sstevel@tonic-gate 			/*
18970Sstevel@tonic-gate 			 * An AP_TYPE argument cannot generate dynamic
18980Sstevel@tonic-gate 			 * attachment point stats unless dynamic expansion was
18990Sstevel@tonic-gate 			 * requested by user.
19000Sstevel@tonic-gate 			 */
19010Sstevel@tonic-gate 			if (!dyn_exp && get_dyn(out_array[i].ldatap->ap_log_id)
19020Sstevel@tonic-gate 			    != NULL) {
19030Sstevel@tonic-gate 				continue;
19040Sstevel@tonic-gate 			}
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 			if (strncmp(out_array[i].ldatap->ap_log_id, inp->arg,
19070Sstevel@tonic-gate 			    strlen(inp->arg)) == 0) {
19080Sstevel@tonic-gate 				break;
19090Sstevel@tonic-gate 			}
19100Sstevel@tonic-gate 		} else {
19110Sstevel@tonic-gate 			return;
19120Sstevel@tonic-gate 		}
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	if (i < nouts) {
19160Sstevel@tonic-gate 		inp->resp = 1;
19170Sstevel@tonic-gate 	}
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate /* Mark all stat data which were requested by user */
19210Sstevel@tonic-gate static void
out_was_req(ap_out_t * outp,ap_arg_t * in_array,int nargs,int no_dyn)19220Sstevel@tonic-gate out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs, int no_dyn)
19230Sstevel@tonic-gate {
19240Sstevel@tonic-gate 	int i;
19250Sstevel@tonic-gate 	cfga_ap_types_t type = UNKNOWN_AP;
19260Sstevel@tonic-gate 	char physid[MAXPATHLEN], logid[MAXPATHLEN];
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	/* If no user args, all output is acceptable */
19300Sstevel@tonic-gate 	if (nargs == 0) {
19310Sstevel@tonic-gate 		outp->req = 1;
19320Sstevel@tonic-gate 		return;
19330Sstevel@tonic-gate 	}
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	(void) snprintf(physid, sizeof (physid), "%s",
19370Sstevel@tonic-gate 	    outp->ldatap->ap_phys_id);
19380Sstevel@tonic-gate 	(void) snprintf(logid, sizeof (logid), "%s", outp->ldatap->ap_log_id);
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	/*
19410Sstevel@tonic-gate 	 * Do comparison with or without dynamic component as requested by
19420Sstevel@tonic-gate 	 * user.
19430Sstevel@tonic-gate 	 */
19440Sstevel@tonic-gate 	if (no_dyn) {
19450Sstevel@tonic-gate 		/* Remove the dynamic component */
19460Sstevel@tonic-gate 		remove_dyn(physid);
19470Sstevel@tonic-gate 		remove_dyn(logid);
19480Sstevel@tonic-gate 	}
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
19510Sstevel@tonic-gate 		type = find_arg_type(in_array[i].arg);
19520Sstevel@tonic-gate 		if (type == PHYSICAL_AP_ID) {
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 			if (config_ap_id_cmp(in_array[i].arg, physid) == 0) {
19550Sstevel@tonic-gate 				break;
19560Sstevel@tonic-gate 			}
19570Sstevel@tonic-gate 		} else if (type == LOGICAL_AP_ID) {
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 			if (config_ap_id_cmp(in_array[i].arg, logid) == 0) {
19600Sstevel@tonic-gate 				break;
19610Sstevel@tonic-gate 			}
19620Sstevel@tonic-gate 		} else if (type == AP_TYPE) {
19630Sstevel@tonic-gate 			/*
19640Sstevel@tonic-gate 			 * Aptypes cannot generate dynamic attachment
19650Sstevel@tonic-gate 			 * points unless dynamic expansion is specified.
19660Sstevel@tonic-gate 			 * in which case this routine would be called a
19670Sstevel@tonic-gate 			 * 2nd time with the no_dyn flag set and there
19680Sstevel@tonic-gate 			 * would be no dynamic ap_ids.
19690Sstevel@tonic-gate 			 */
19700Sstevel@tonic-gate 			if (get_dyn(logid) != NULL) {
19710Sstevel@tonic-gate 				continue;
19720Sstevel@tonic-gate 			}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 			if (strncmp(in_array[i].arg, logid,
19750Sstevel@tonic-gate 			    strlen(in_array[i].arg)) == 0) {
19760Sstevel@tonic-gate 				break;
19770Sstevel@tonic-gate 			}
19780Sstevel@tonic-gate 		} else {
19790Sstevel@tonic-gate 			continue;
19800Sstevel@tonic-gate 		}
19810Sstevel@tonic-gate 	}
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	if (i < nargs) {
19840Sstevel@tonic-gate 		/* Ok, this output was requested */
19850Sstevel@tonic-gate 		outp->req = 1;
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate }
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate static void
do_post_filter(ap_out_t * outp,post_filter_t * post_filtp,int * nselp)19910Sstevel@tonic-gate do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *nselp)
19920Sstevel@tonic-gate {
19930Sstevel@tonic-gate 	int i;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	if (outp->req != 1) {
19960Sstevel@tonic-gate 		return;
19970Sstevel@tonic-gate 	}
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	/*
20000Sstevel@tonic-gate 	 * For fields without filtering (CFGA_MATCH_NOFILTER),
20010Sstevel@tonic-gate 	 * compare always returns 0 (success)
20020Sstevel@tonic-gate 	 */
20030Sstevel@tonic-gate 	for (i = 0; i < N_FIELDS; i++) {
20040Sstevel@tonic-gate 		/*
20050Sstevel@tonic-gate 		 * Note: Order is important for partial match (via strncmp).
20060Sstevel@tonic-gate 		 * The first argument for compare must be the filter.
20070Sstevel@tonic-gate 		 */
20080Sstevel@tonic-gate 		if (all_fields[i].compare(&post_filtp->ldata, outp->ldatap,
20090Sstevel@tonic-gate 		    post_filtp->match_type_p[i])) {
20100Sstevel@tonic-gate 			outp->req = 0;	/* Blocked by filter */
20110Sstevel@tonic-gate 			return;
20120Sstevel@tonic-gate 		}
20130Sstevel@tonic-gate 	}
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	/*
20160Sstevel@tonic-gate 	 * Passed through filter
20170Sstevel@tonic-gate 	 */
20180Sstevel@tonic-gate 	(*nselp)++;
20190Sstevel@tonic-gate }
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate static void
report_no_response(ap_arg_t * arg_array,int nargs)20220Sstevel@tonic-gate report_no_response(ap_arg_t *arg_array, int nargs)
20230Sstevel@tonic-gate {
20240Sstevel@tonic-gate 	int i;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	if (nargs == 0) {
20270Sstevel@tonic-gate 		return;
20280Sstevel@tonic-gate 	}
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	/*
20320Sstevel@tonic-gate 	 * nop if no user arguments
20330Sstevel@tonic-gate 	 */
20340Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
20350Sstevel@tonic-gate 		if (arg_array[i].resp == 0) {
20360Sstevel@tonic-gate 			(void) fprintf(stderr,
20370Sstevel@tonic-gate 			    gettext("%s: No matching library found\n"),
20380Sstevel@tonic-gate 			    arg_array[i].arg);
20390Sstevel@tonic-gate 		}
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate }
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate /*
20440Sstevel@tonic-gate  * ldata_compare - compare two attachment point list data structures.
20450Sstevel@tonic-gate  */
20460Sstevel@tonic-gate static int
ldata_compare(const void * vb1,const void * vb2)20470Sstevel@tonic-gate ldata_compare(
20480Sstevel@tonic-gate 	const void *vb1,
20490Sstevel@tonic-gate 	const void *vb2)
20500Sstevel@tonic-gate {
20510Sstevel@tonic-gate 	int i;
20520Sstevel@tonic-gate 	int res = -1;
20530Sstevel@tonic-gate 	cfga_list_data_t *b1, *b2;
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 	b1 = *(cfga_list_data_t **)vb1;
20570Sstevel@tonic-gate 	b2 = *(cfga_list_data_t **)vb2;
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	for (i = 0; i < nsort_list; i++) {
20600Sstevel@tonic-gate 		res = (*(sort_list[i].fld->compare))(b1, b2, CFGA_MATCH_ORDER);
20610Sstevel@tonic-gate 		if (res != 0) {
20620Sstevel@tonic-gate 			if (sort_list[i].reverse)
20630Sstevel@tonic-gate 				res = -res;
20640Sstevel@tonic-gate 			break;
20650Sstevel@tonic-gate 		}
20660Sstevel@tonic-gate 	}
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	return (res);
20690Sstevel@tonic-gate }
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate /*
20720Sstevel@tonic-gate  * count_fields - Count the number of fields, using supplied delimiter.
20730Sstevel@tonic-gate  */
20740Sstevel@tonic-gate static int
count_fields(char * fspec,char delim)20750Sstevel@tonic-gate count_fields(char *fspec, char delim)
20760Sstevel@tonic-gate {
20770Sstevel@tonic-gate 	char *cp = NULL;
20780Sstevel@tonic-gate 	int n;
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	if (fspec == 0 || *fspec == '\0')
20810Sstevel@tonic-gate 		return (0);
20820Sstevel@tonic-gate 	n = 1;
20830Sstevel@tonic-gate 	for (cp = fspec; *cp != '\0'; cp++)
20840Sstevel@tonic-gate 		if (*cp == delim)
20850Sstevel@tonic-gate 			n++;
20860Sstevel@tonic-gate 	return (n);
20870Sstevel@tonic-gate }
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate /*
20900Sstevel@tonic-gate  * get_field
20910Sstevel@tonic-gate  * This function is not a re-implementation of strtok().
20920Sstevel@tonic-gate  * There can be null fields - strtok() eats spans of delimiters.
20930Sstevel@tonic-gate  */
20940Sstevel@tonic-gate static char *
get_field(char ** fspp)20950Sstevel@tonic-gate get_field(char **fspp)
20960Sstevel@tonic-gate {
20970Sstevel@tonic-gate 	char *cp = NULL, *fld;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	fld = *fspp;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if (fld != NULL && *fld == '\0')
21020Sstevel@tonic-gate 		fld = NULL;
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	if (fld != NULL) {
21050Sstevel@tonic-gate 		cp = strchr(*fspp, FDELIM);
21060Sstevel@tonic-gate 		if (cp == NULL) {
21070Sstevel@tonic-gate 			*fspp = NULL;
21080Sstevel@tonic-gate 		} else {
21090Sstevel@tonic-gate 			*cp = '\0';
21100Sstevel@tonic-gate 			*fspp = cp + 1;
21110Sstevel@tonic-gate 			if (*fld == '\0')
21120Sstevel@tonic-gate 				fld = NULL;
21130Sstevel@tonic-gate 		}
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 	return (fld);
21160Sstevel@tonic-gate }
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate /*
21190Sstevel@tonic-gate  * process_fields -
21200Sstevel@tonic-gate  */
21210Sstevel@tonic-gate static int
process_fields(int ncol,struct print_col * list,int line2,char * fmt)21220Sstevel@tonic-gate process_fields(
21230Sstevel@tonic-gate 	int ncol,
21240Sstevel@tonic-gate 	struct print_col *list,
21250Sstevel@tonic-gate 	int line2,
21260Sstevel@tonic-gate 	char *fmt)
21270Sstevel@tonic-gate {
21280Sstevel@tonic-gate 	struct print_col *pp = NULL;
21290Sstevel@tonic-gate 	struct field_info *fldp = NULL;
21300Sstevel@tonic-gate 	char *fmtx;
21310Sstevel@tonic-gate 	char *fldn;
21320Sstevel@tonic-gate 	int err;
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	err = 0;
21350Sstevel@tonic-gate 	fmtx = fmt;
21360Sstevel@tonic-gate 	for (pp = list; pp < &list[ncol]; pp++) {
21370Sstevel@tonic-gate 		fldn = get_field(&fmtx);
21380Sstevel@tonic-gate 		fldp = &null_field;
21390Sstevel@tonic-gate 		if (fldn != NULL) {
21400Sstevel@tonic-gate 			struct field_info *tfldp;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 			tfldp = find_field(fldn);
21430Sstevel@tonic-gate 			if (tfldp != NULL) {
21440Sstevel@tonic-gate 				fldp = tfldp;
21450Sstevel@tonic-gate 			} else {
21460Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(unk_field),
21470Sstevel@tonic-gate 				    cmdname, fldn);
21480Sstevel@tonic-gate 				err = 1;
21490Sstevel@tonic-gate 			}
21500Sstevel@tonic-gate 		}
21510Sstevel@tonic-gate 		if (line2) {
21520Sstevel@tonic-gate 			pp->line2 = fldp;
21530Sstevel@tonic-gate 			if (fldp->width > pp->width)
21540Sstevel@tonic-gate 				pp->width = fldp->width;
21550Sstevel@tonic-gate 		} else {
21560Sstevel@tonic-gate 			pp->line1 = fldp;
21570Sstevel@tonic-gate 			pp->width = fldp->width;
21580Sstevel@tonic-gate 		}
21590Sstevel@tonic-gate 	}
21600Sstevel@tonic-gate 	return (err);
21610Sstevel@tonic-gate }
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate /*
21640Sstevel@tonic-gate  * process_sort_fields -
21650Sstevel@tonic-gate  */
21660Sstevel@tonic-gate static int
process_sort_fields(int nsort,struct sort_el * list,char * fmt)21670Sstevel@tonic-gate process_sort_fields(
21680Sstevel@tonic-gate 	int nsort,
21690Sstevel@tonic-gate 	struct sort_el *list,
21700Sstevel@tonic-gate 	char *fmt)
21710Sstevel@tonic-gate {
21720Sstevel@tonic-gate 	int i;
21730Sstevel@tonic-gate 	int rev;
21740Sstevel@tonic-gate 	struct field_info *fldp = NULL;
21750Sstevel@tonic-gate 	char *fmtx;
21760Sstevel@tonic-gate 	char *fldn;
21770Sstevel@tonic-gate 	int err;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	err = 0;
21800Sstevel@tonic-gate 	fmtx = fmt;
21810Sstevel@tonic-gate 	for (i = 0; i < nsort; i++) {
21820Sstevel@tonic-gate 		fldn = get_field(&fmtx);
21830Sstevel@tonic-gate 		fldp = &null_field;
21840Sstevel@tonic-gate 		rev = 0;
21850Sstevel@tonic-gate 		if (fldn != NULL) {
21860Sstevel@tonic-gate 			struct field_info *tfldp = NULL;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 			if (*fldn == '-') {
21890Sstevel@tonic-gate 				rev = 1;
21900Sstevel@tonic-gate 				fldn++;
21910Sstevel@tonic-gate 			}
21920Sstevel@tonic-gate 			tfldp = find_field(fldn);
21930Sstevel@tonic-gate 			if (tfldp != NULL) {
21940Sstevel@tonic-gate 				fldp = tfldp;
21950Sstevel@tonic-gate 			} else {
21960Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(unk_field),
21970Sstevel@tonic-gate 				    cmdname, fldn);
21980Sstevel@tonic-gate 				err = 1;
21990Sstevel@tonic-gate 			}
22000Sstevel@tonic-gate 		}
22010Sstevel@tonic-gate 		list[i].reverse = rev;
22020Sstevel@tonic-gate 		list[i].fld = fldp;
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 	return (err);
22050Sstevel@tonic-gate }
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate /*
22080Sstevel@tonic-gate  * print_fields -
22090Sstevel@tonic-gate  */
22100Sstevel@tonic-gate static cfga_err_t
print_fields(int ncol,struct print_col * list,int heading,int line2,char * delim,cfga_list_data_t * bdp,FILE * fp)22110Sstevel@tonic-gate print_fields(
22120Sstevel@tonic-gate 	int ncol,
22130Sstevel@tonic-gate 	struct print_col *list,
22140Sstevel@tonic-gate 	int heading,
22150Sstevel@tonic-gate 	int line2,
22160Sstevel@tonic-gate 	char *delim,
22170Sstevel@tonic-gate 	cfga_list_data_t *bdp,
22180Sstevel@tonic-gate 	FILE *fp)
22190Sstevel@tonic-gate {
22200Sstevel@tonic-gate 	char *del = NULL;
22210Sstevel@tonic-gate 	struct print_col *pp = NULL;
22220Sstevel@tonic-gate 	struct field_info *fldp = NULL;
22230Sstevel@tonic-gate 	static char *outline, *end;
22240Sstevel@tonic-gate 	char *lp;
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	if (outline == NULL) {
22270Sstevel@tonic-gate 		int out_len, delim_len;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 		delim_len = strlen(delim);
22300Sstevel@tonic-gate 		out_len = 0;
22310Sstevel@tonic-gate 		for (pp = list; pp < &list[ncol]; pp++) {
22320Sstevel@tonic-gate 			out_len += pp->width;
22330Sstevel@tonic-gate 			out_len += delim_len;
22340Sstevel@tonic-gate 		}
22350Sstevel@tonic-gate 		out_len -= delim_len;
22360Sstevel@tonic-gate 		outline = config_calloc_check(out_len + 1, 1);
22370Sstevel@tonic-gate 		if (outline == NULL) {
22380Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
22390Sstevel@tonic-gate 		}
22400Sstevel@tonic-gate 		end = &outline[out_len + 1];
22410Sstevel@tonic-gate 	}
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	lp = outline;
22440Sstevel@tonic-gate 	del = "";
22450Sstevel@tonic-gate 	for (pp = list; pp < &list[ncol]; pp++) {
22460Sstevel@tonic-gate 		fldp = line2 ? pp->line2 : pp->line1;
22470Sstevel@tonic-gate 		(void) snprintf(lp, end - lp, "%s", del);
22480Sstevel@tonic-gate 		lp += strlen(lp);
22490Sstevel@tonic-gate 		if (heading) {
22500Sstevel@tonic-gate 			(void) snprintf(lp, end - lp, "%-*s",
22510Sstevel@tonic-gate 			    fldp->width, fldp->heading);
22520Sstevel@tonic-gate 		} else {
22530Sstevel@tonic-gate 			(*fldp->printfn)(bdp, fldp->width, lp);
22540Sstevel@tonic-gate 		}
22550Sstevel@tonic-gate 		lp += strlen(lp);
22560Sstevel@tonic-gate 		del = delim;
22570Sstevel@tonic-gate 	}
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * Trim trailing spaces
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 	while (--lp >= outline && *lp == ' ')
22630Sstevel@tonic-gate 		*lp = '\0';
22640Sstevel@tonic-gate 	(void) fprintf(fp, "%s\n", outline);
22650Sstevel@tonic-gate 	return (CFGA_OK);
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate /*
22690Sstevel@tonic-gate  * config_calloc_check - perform allocation, check result and
22700Sstevel@tonic-gate  * set error indicator
22710Sstevel@tonic-gate  */
22720Sstevel@tonic-gate static void *
config_calloc_check(size_t nelem,size_t elsize)22730Sstevel@tonic-gate config_calloc_check(
22740Sstevel@tonic-gate 	size_t nelem,
22750Sstevel@tonic-gate 	size_t elsize)
22760Sstevel@tonic-gate {
22770Sstevel@tonic-gate 	void *p;
22780Sstevel@tonic-gate 	static char alloc_fail[] =
22790Sstevel@tonic-gate "%s: memory allocation failed (%d*%d bytes)\n";
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	p = calloc(nelem, elsize);
22830Sstevel@tonic-gate 	if (p == NULL) {
22840Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(alloc_fail), cmdname,
22850Sstevel@tonic-gate 		    nelem, elsize);
22860Sstevel@tonic-gate 	}
22870Sstevel@tonic-gate 	return (p);
22880Sstevel@tonic-gate }
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate /*
22910Sstevel@tonic-gate  * find_arg_type - determine if an argument is an ap_id or an ap_type.
22920Sstevel@tonic-gate  */
22930Sstevel@tonic-gate static cfga_ap_types_t
find_arg_type(const char * ap_id)22940Sstevel@tonic-gate find_arg_type(const char *ap_id)
22950Sstevel@tonic-gate {
22960Sstevel@tonic-gate 	struct stat sbuf;
22970Sstevel@tonic-gate 	cfga_ap_types_t type;
22980Sstevel@tonic-gate 	char *mkr = NULL, *cp;
22990Sstevel@tonic-gate 	int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
23000Sstevel@tonic-gate 	char path[MAXPATHLEN];
23010Sstevel@tonic-gate 	char apbuf[MAXPATHLEN];
23020Sstevel@tonic-gate 	size_t len;
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	/*
23060Sstevel@tonic-gate 	 * sanity checks
23070Sstevel@tonic-gate 	 */
23080Sstevel@tonic-gate 	if (ap_id == NULL || *ap_id == '\0') {
23090Sstevel@tonic-gate 		return (UNKNOWN_AP);
23100Sstevel@tonic-gate 	}
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 	/*
23130Sstevel@tonic-gate 	 * Mask the dynamic component if any
23140Sstevel@tonic-gate 	 */
23150Sstevel@tonic-gate 	if ((cp = GET_DYN(ap_id)) != NULL) {
23160Sstevel@tonic-gate 		len = cp - ap_id;
23170Sstevel@tonic-gate 	} else {
23180Sstevel@tonic-gate 		len = strlen(ap_id);
23190Sstevel@tonic-gate 	}
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	if (len >= sizeof (apbuf)) {
23220Sstevel@tonic-gate 		return (UNKNOWN_AP);
23230Sstevel@tonic-gate 	}
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	(void) strncpy(apbuf, ap_id, len);
23260Sstevel@tonic-gate 	apbuf[len] = '\0';
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	/*
23290Sstevel@tonic-gate 	 * If it starts with a slash and is stat-able
23300Sstevel@tonic-gate 	 * its a physical.
23310Sstevel@tonic-gate 	 */
23320Sstevel@tonic-gate 	if (*apbuf == '/' && stat(apbuf, &sbuf) == 0) {
23330Sstevel@tonic-gate 		return (PHYSICAL_AP_ID);
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	/*
23370Sstevel@tonic-gate 	 * Is this a symlink in CFGA_DEV_DIR ?
23380Sstevel@tonic-gate 	 */
23390Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s/%s", CFGA_DEV_DIR, apbuf);
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
23420Sstevel@tonic-gate 	    stat(path, &sbuf) == 0) {
23430Sstevel@tonic-gate 		return (LOGICAL_AP_ID);
23440Sstevel@tonic-gate 	}
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	/*
23470Sstevel@tonic-gate 	 * Check for ":" which is always present in an ap_id but not maybe
23480Sstevel@tonic-gate 	 * present or absent in an ap_type.
23490Sstevel@tonic-gate 	 * We need to check that the characters right before the : are digits
23500Sstevel@tonic-gate 	 * since an ap_id is of the form <name><instance>:<specific ap name>
23510Sstevel@tonic-gate 	 */
23520Sstevel@tonic-gate 	if ((mkr = strchr(apbuf, ':')) == NULL)  {
23530Sstevel@tonic-gate 		type = AP_TYPE;
23540Sstevel@tonic-gate 	} else {
23550Sstevel@tonic-gate 		size_ap = strlen(apbuf);
23560Sstevel@tonic-gate 		size_mkr = strlen(mkr);
23570Sstevel@tonic-gate 		mkr = apbuf;
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 		digit = 0;
23600Sstevel@tonic-gate 		for (i = size_ap - size_mkr - 1;  i > 0; i--) {
23610Sstevel@tonic-gate 			if ((int)isdigit(mkr[i])) {
23620Sstevel@tonic-gate 				digit++;
23630Sstevel@tonic-gate 				break;
23640Sstevel@tonic-gate 			}
23650Sstevel@tonic-gate 		}
23660Sstevel@tonic-gate 		if (digit == 0) {
23670Sstevel@tonic-gate 			type = AP_TYPE;
23680Sstevel@tonic-gate 		} else {
23690Sstevel@tonic-gate 			type = LOGICAL_AP_ID;
23700Sstevel@tonic-gate 		}
23710Sstevel@tonic-gate 	}
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	return (type);
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate static char *
get_dyn(const char * ap_id)23780Sstevel@tonic-gate get_dyn(const char *ap_id)
23790Sstevel@tonic-gate {
23800Sstevel@tonic-gate 	if (ap_id == NULL) {
23810Sstevel@tonic-gate 		return (NULL);
23820Sstevel@tonic-gate 	}
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	return (strstr(ap_id, CFGA_DYN_SEP));
23850Sstevel@tonic-gate }
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate /*
23880Sstevel@tonic-gate  * removes the dynamic component
23890Sstevel@tonic-gate  */
23900Sstevel@tonic-gate static void
remove_dyn(char * ap_id)23910Sstevel@tonic-gate remove_dyn(char *ap_id)
23920Sstevel@tonic-gate {
23930Sstevel@tonic-gate 	char *cp;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	if (ap_id == NULL) {
23960Sstevel@tonic-gate 		return;
23970Sstevel@tonic-gate 	}
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	cp = strstr(ap_id, CFGA_DYN_SEP);
24000Sstevel@tonic-gate 	if (cp != NULL) {
24010Sstevel@tonic-gate 		*cp = '\0';
24020Sstevel@tonic-gate 	}
24030Sstevel@tonic-gate }
2404399Svikram 
2405399Svikram 
2406399Svikram static char *
s_strdup(char * str)2407399Svikram s_strdup(char *str)
2408399Svikram {
2409399Svikram 	char *dup;
2410399Svikram 
2411399Svikram 	/*
2412399Svikram 	 * sometimes NULL strings may be passed in (see DEF_COLS2). This
2413399Svikram 	 * is not an error.
2414399Svikram 	 */
2415399Svikram 	if (str == NULL) {
2416399Svikram 		return (NULL);
2417399Svikram 	}
2418399Svikram 
2419399Svikram 	dup = strdup(str);
2420399Svikram 	if (dup == NULL) {
2421399Svikram 		(void) fprintf(stderr,
2422399Svikram 		    "%s \"%s\"\n", gettext("Cannot copy string"), str);
2423399Svikram 		return (NULL);
2424399Svikram 	}
2425399Svikram 
2426399Svikram 	return (dup);
2427399Svikram }
2428