xref: /titanic_53/usr/src/cmd/cfgadm/cfgadm.c (revision 3977d6cda0256e9899aa743f72c50d9e7ecbd612)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*3977d6cdSvikram  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This is the main program file for the configuration administration
317c478bd9Sstevel@tonic-gate  * command as set out in manual page cfgadm(1M).  It uses the configuration
327c478bd9Sstevel@tonic-gate  * administration library interface, libcfgadm, as set out in manual
337c478bd9Sstevel@tonic-gate  * page config_admin(3X).
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <locale.h>
407c478bd9Sstevel@tonic-gate #include <langinfo.h>
417c478bd9Sstevel@tonic-gate #include <time.h>
427c478bd9Sstevel@tonic-gate #include <assert.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
487c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
497c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <config_admin.h>
537c478bd9Sstevel@tonic-gate #include "cfgadm.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	S_FREE(x)	(((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
567c478bd9Sstevel@tonic-gate #define	GET_DYN(a)	(strstr((a), CFGA_DYN_SEP))
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * forward declarations
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static char *basename(char *);
617c478bd9Sstevel@tonic-gate static void cfgadm_error(int, char *);
627c478bd9Sstevel@tonic-gate static int confirm_interactive(void *, const char *);
637c478bd9Sstevel@tonic-gate static int confirm_no(void *, const char *);
647c478bd9Sstevel@tonic-gate static int confirm_yes(void *, const char *);
657c478bd9Sstevel@tonic-gate static void usage(void);
667c478bd9Sstevel@tonic-gate static void usage_field(void);
677c478bd9Sstevel@tonic-gate static int extract_list_suboptions(char *, char **, char **, char **,
687c478bd9Sstevel@tonic-gate     int *, char **, char **, char **);
697c478bd9Sstevel@tonic-gate static int message_output(void *appdata_ptr, const char *message);
707c478bd9Sstevel@tonic-gate static void *config_calloc_check(size_t, size_t);
717c478bd9Sstevel@tonic-gate static cfga_ap_types_t find_arg_type(const char *);
727c478bd9Sstevel@tonic-gate static int yesno(char *, char *);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static int compare_ap_id(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
767c478bd9Sstevel@tonic-gate static int compare_r_state(cfga_list_data_t *, cfga_list_data_t *,
777c478bd9Sstevel@tonic-gate     match_type_t);
787c478bd9Sstevel@tonic-gate static int compare_o_state(cfga_list_data_t *, cfga_list_data_t *,
797c478bd9Sstevel@tonic-gate     match_type_t);
807c478bd9Sstevel@tonic-gate static int compare_cond(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
817c478bd9Sstevel@tonic-gate static int compare_time(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
827c478bd9Sstevel@tonic-gate static int compare_info(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
837c478bd9Sstevel@tonic-gate static int compare_type(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
847c478bd9Sstevel@tonic-gate static int compare_busy(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
857c478bd9Sstevel@tonic-gate static int compare_class(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
867c478bd9Sstevel@tonic-gate static int compare_null(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
877c478bd9Sstevel@tonic-gate static void print_log_id(cfga_list_data_t *, int, char *);
887c478bd9Sstevel@tonic-gate static void print_r_state(cfga_list_data_t *, int, char *);
897c478bd9Sstevel@tonic-gate static void print_o_state(cfga_list_data_t *, int, char *);
907c478bd9Sstevel@tonic-gate static void print_cond(cfga_list_data_t *, int, char *);
917c478bd9Sstevel@tonic-gate static void print_time(cfga_list_data_t *, int, char *);
927c478bd9Sstevel@tonic-gate static void print_time_p(cfga_list_data_t *, int, char *);
937c478bd9Sstevel@tonic-gate static void print_info(cfga_list_data_t *, int, char *);
947c478bd9Sstevel@tonic-gate static void print_type(cfga_list_data_t *, int, char *);
957c478bd9Sstevel@tonic-gate static void print_busy(cfga_list_data_t *, int, char *);
967c478bd9Sstevel@tonic-gate static void print_phys_id(cfga_list_data_t *, int, char *);
977c478bd9Sstevel@tonic-gate static void print_class(cfga_list_data_t *, int, char *);
987c478bd9Sstevel@tonic-gate static void print_null(cfga_list_data_t *, int, char *);
997c478bd9Sstevel@tonic-gate static int count_fields(char *, char);
1007c478bd9Sstevel@tonic-gate static int process_sort_fields(int, struct sort_el *, char *);
1017c478bd9Sstevel@tonic-gate static int process_fields(int, struct print_col *, int, char *);
1027c478bd9Sstevel@tonic-gate static cfga_err_t print_fields(int, struct print_col *, int, int, char *,
1037c478bd9Sstevel@tonic-gate     cfga_list_data_t *, FILE *);
1047c478bd9Sstevel@tonic-gate static int ldata_compare(const void *, const void *);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static void arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts,
1077c478bd9Sstevel@tonic-gate     int dyn_exp);
1087c478bd9Sstevel@tonic-gate static void out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs,
1097c478bd9Sstevel@tonic-gate     int no_dyn);
1107c478bd9Sstevel@tonic-gate static void report_no_response(ap_arg_t *arg_array, int napids_to_list);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static cfga_err_t set_log_flt(cfga_list_data_t *p, const char *val);
1137c478bd9Sstevel@tonic-gate static cfga_err_t set_type_flt(cfga_list_data_t *p, const char *val);
1147c478bd9Sstevel@tonic-gate static cfga_err_t set_class_flt(cfga_list_data_t *p, const char *val);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static char *get_dyn(const char *ap_id);
1177c478bd9Sstevel@tonic-gate static void remove_dyn(char *ap_id);
118*3977d6cdSvikram static char *s_strdup(char *str);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * global data
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate /* command name for messages */
1247c478bd9Sstevel@tonic-gate static char *cmdname;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * control for comparing, printing and filtering cfga_list_data
1287c478bd9Sstevel@tonic-gate  * NOTE:Field names (i.e. member 0 of field_info struct) may not contain '('.
1297c478bd9Sstevel@tonic-gate  *	The post filtering code depends on it.
1307c478bd9Sstevel@tonic-gate  * NOTE:A NULL value for the set_filter member indicates that filtering based
1317c478bd9Sstevel@tonic-gate  *	on that field is currently not supported.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate static struct field_info all_fields[] = {
1347c478bd9Sstevel@tonic-gate {"ap_id", "Ap_Id", SZ_EL(ap_log_id), compare_ap_id, print_log_id, set_log_flt},
1357c478bd9Sstevel@tonic-gate {"r_state", "Receptacle", STATE_WIDTH, compare_r_state, print_r_state, NULL},
1367c478bd9Sstevel@tonic-gate {"o_state", "Occupant", STATE_WIDTH, compare_o_state, print_o_state, NULL},
1377c478bd9Sstevel@tonic-gate {"condition", "Condition", COND_WIDTH, compare_cond, print_cond, NULL},
1387c478bd9Sstevel@tonic-gate {"status_time", "When", TIME_WIDTH, compare_time, print_time, NULL},
1397c478bd9Sstevel@tonic-gate {"status_time_p", "When", TIME_P_WIDTH, compare_time, print_time_p, NULL},
1407c478bd9Sstevel@tonic-gate {"info", "Information", SZ_EL(ap_info), compare_info, print_info, NULL},
1417c478bd9Sstevel@tonic-gate {"type", "Type", SZ_EL(ap_type), compare_type, print_type, set_type_flt},
1427c478bd9Sstevel@tonic-gate {"busy", "Busy", 8, compare_busy, print_busy, NULL},
1437c478bd9Sstevel@tonic-gate {"physid", "Phys_Id", SZ_EL(ap_phys_id), compare_ap_id, print_phys_id, NULL},
1447c478bd9Sstevel@tonic-gate {"class", "Class", SZ_EL(ap_class), compare_class, print_class, set_class_flt}
1457c478bd9Sstevel@tonic-gate };
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #define	PREFILT_CLASS_STR	"class="
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate typedef struct {
1507c478bd9Sstevel@tonic-gate 	cfga_list_data_t ldata;			/* Selection criteria */
1517c478bd9Sstevel@tonic-gate 	match_type_t match_type_p[N_FIELDS];	/* Type of match */
1527c478bd9Sstevel@tonic-gate } post_filter_t;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static struct field_info null_field =
1557c478bd9Sstevel@tonic-gate 	{"null", "", 0, compare_null, print_null, NULL};
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static struct sort_el *sort_list;	/* Used in ldata_compare() */
1587c478bd9Sstevel@tonic-gate static int nsort_list;
1597c478bd9Sstevel@tonic-gate static char unk_field[] = "%s: field \"%s\" unknown\n";
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static char aptype_no_dyn[] = "%s: Invalid ap_id: %s\n";
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /* strings that make up the usage message */
1647c478bd9Sstevel@tonic-gate static char *usage_tab[] = {
1657c478bd9Sstevel@tonic-gate " %s [-f] [-y|-n] [-v] [-o hardware_opts ] -c function ap_id [ap_id...]\n",
1667c478bd9Sstevel@tonic-gate " %s [-f] [-y|-n] [-v] [-o hardware_opts ] -x function ap_id [ap_id...]\n",
1677c478bd9Sstevel@tonic-gate " %s [-v] [-s listing_options ] [-o hardware_opts ] [-a]\n"
1687c478bd9Sstevel@tonic-gate "\t[-l [ap_id|ap_type...]]\n",
1697c478bd9Sstevel@tonic-gate " %s [-v] [-o hardware_opts ] -t ap_id [ap_id...]\n",
1707c478bd9Sstevel@tonic-gate " %s [-v] [-o hardware_opts ] -h [ap_id|ap_type...]\n",
1717c478bd9Sstevel@tonic-gate };
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /* Type of matches currently supported by the select sub-option */
1747c478bd9Sstevel@tonic-gate static match_cvt_t match_type_array[] = {
1757c478bd9Sstevel@tonic-gate 	{"partial", CFGA_MATCH_PARTIAL},
1767c478bd9Sstevel@tonic-gate 	{"exact", CFGA_MATCH_EXACT}
1777c478bd9Sstevel@tonic-gate };
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #define	N_MATCH_TYPES	(sizeof (match_type_array)/sizeof (match_type_array[0]))
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static cfga_err_t setup_filter(const char *selectp, const char *matchp,
1827c478bd9Sstevel@tonic-gate     post_filter_t *post_filtp, char **prefilt_optpp);
1837c478bd9Sstevel@tonic-gate static cfga_err_t parse_select_opt(const char *selectp,
1847c478bd9Sstevel@tonic-gate     post_filter_t *post_filtp, match_type_t match_type);
1857c478bd9Sstevel@tonic-gate static int do_config_list(int, char *[], cfga_list_data_t *, int, char *,
1867c478bd9Sstevel@tonic-gate     char *, char *, int, char *, post_filter_t *, int);
1877c478bd9Sstevel@tonic-gate static void do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *jp);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * main - the main routine of cfgadm, processes the command line
1927c478bd9Sstevel@tonic-gate  * and dispatches functions off to libraries.
1937c478bd9Sstevel@tonic-gate  */
194*3977d6cdSvikram int
1957c478bd9Sstevel@tonic-gate main(
1967c478bd9Sstevel@tonic-gate 	int argc,
1977c478bd9Sstevel@tonic-gate 	char *argv[])
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	extern char *optarg;
2007c478bd9Sstevel@tonic-gate 	extern int optind;
2017c478bd9Sstevel@tonic-gate 	int c;
2027c478bd9Sstevel@tonic-gate 	char *subopts;
2037c478bd9Sstevel@tonic-gate 	char *subvalue;
2047c478bd9Sstevel@tonic-gate 	char *const *ap_args = NULL;
2057c478bd9Sstevel@tonic-gate 	cfga_cmd_t sc_opt = NULL;
2067c478bd9Sstevel@tonic-gate 	struct cfga_confirm confirm;
2077c478bd9Sstevel@tonic-gate 	struct cfga_msg message;
2087c478bd9Sstevel@tonic-gate 	int ret = CFGA_ERROR;
2097c478bd9Sstevel@tonic-gate 	int i;
2107c478bd9Sstevel@tonic-gate 	char *estrp = NULL;
2117c478bd9Sstevel@tonic-gate 	cfga_op_t action = CFGA_OP_NONE;
2127c478bd9Sstevel@tonic-gate 	char *plat_opts = NULL;
2137c478bd9Sstevel@tonic-gate 	char *act_arg = NULL;
2147c478bd9Sstevel@tonic-gate 	enum confirm confarg = CONFIRM_DEFAULT;
2157c478bd9Sstevel@tonic-gate 	char *list_opts = NULL;
2167c478bd9Sstevel@tonic-gate 	cfga_flags_t flags = 0;
2177c478bd9Sstevel@tonic-gate 	int arg_error = 0;
2187c478bd9Sstevel@tonic-gate 	int dyn_exp = 0;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	estrp = NULL;
2217c478bd9Sstevel@tonic-gate 	if (argc > 0)
2227c478bd9Sstevel@tonic-gate 		cmdname = basename(argv[0]);
2237c478bd9Sstevel@tonic-gate 	else
2247c478bd9Sstevel@tonic-gate 		cmdname = "cfgadm";
2257c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2267c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2277c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2287c478bd9Sstevel@tonic-gate #endif
2297c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, OPTIONS)) != EOF) {
2327c478bd9Sstevel@tonic-gate 		static char dup_action[] =
2337c478bd9Sstevel@tonic-gate "%s: more than one action specified (-c,-l,-t,-x)\n";
2347c478bd9Sstevel@tonic-gate 		static char dup_option[] =
2357c478bd9Sstevel@tonic-gate "%s: more than one -%c option specified\n";
2367c478bd9Sstevel@tonic-gate 		switch (c) {
2377c478bd9Sstevel@tonic-gate 		case 'a':
2387c478bd9Sstevel@tonic-gate 			if (dyn_exp) {
2397c478bd9Sstevel@tonic-gate 				arg_error = 1;
2407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
2417c478bd9Sstevel@tonic-gate 				    cmdname, c);
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 			dyn_exp = 1;
2447c478bd9Sstevel@tonic-gate 			break;
2457c478bd9Sstevel@tonic-gate 		case 'c':
2467c478bd9Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
2477c478bd9Sstevel@tonic-gate 				arg_error = 1;
2487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
2497c478bd9Sstevel@tonic-gate 				    cmdname);
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 			action = CFGA_OP_CHANGE_STATE;
2527c478bd9Sstevel@tonic-gate 			subopts = optarg;
2537c478bd9Sstevel@tonic-gate 			subvalue = NULL;
2547c478bd9Sstevel@tonic-gate 			/*
2557c478bd9Sstevel@tonic-gate 			 * Reject -c suboption if they are unrecognized
2567c478bd9Sstevel@tonic-gate 			 * or more than one or have a associated value.
2577c478bd9Sstevel@tonic-gate 			 */
2587c478bd9Sstevel@tonic-gate 			if ((sc_opt = getsubopt(&subopts, state_opts,
2597c478bd9Sstevel@tonic-gate 			    &subvalue)) == -1 || *subopts != '\0' ||
2607c478bd9Sstevel@tonic-gate 			    subvalue != NULL) {
2617c478bd9Sstevel@tonic-gate 				arg_error = 1;
2627c478bd9Sstevel@tonic-gate 				break;
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 		case 'f':
2667c478bd9Sstevel@tonic-gate 			if ((flags & CFGA_FLAG_FORCE) != 0) {
2677c478bd9Sstevel@tonic-gate 				arg_error = 1;
2687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
2697c478bd9Sstevel@tonic-gate 				    cmdname, c);
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			flags |= CFGA_FLAG_FORCE;
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 		case 'h':
2747c478bd9Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
2757c478bd9Sstevel@tonic-gate 				arg_error = 1;
2767c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
2777c478bd9Sstevel@tonic-gate 				    cmdname);
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate 			action = CFGA_OP_HELP;
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 		case 'l':
2827c478bd9Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
2837c478bd9Sstevel@tonic-gate 				arg_error = 1;
2847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
2857c478bd9Sstevel@tonic-gate 				    cmdname);
2867c478bd9Sstevel@tonic-gate 			}
2877c478bd9Sstevel@tonic-gate 			action = CFGA_OP_LIST;
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 		case 'n':
2907c478bd9Sstevel@tonic-gate 			if (confarg != CONFIRM_DEFAULT) {
2917c478bd9Sstevel@tonic-gate 				arg_error = 1;
2927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
2937c478bd9Sstevel@tonic-gate 				    cmdname, c);
2947c478bd9Sstevel@tonic-gate 			}
2957c478bd9Sstevel@tonic-gate 			confarg = CONFIRM_NO;
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 		case 'o':
2987c478bd9Sstevel@tonic-gate 			if (plat_opts != NULL) {
2997c478bd9Sstevel@tonic-gate 				arg_error = 1;
3007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3017c478bd9Sstevel@tonic-gate 				    cmdname, c);
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			plat_opts = optarg;
3047c478bd9Sstevel@tonic-gate 			break;
3057c478bd9Sstevel@tonic-gate 		case 's':
3067c478bd9Sstevel@tonic-gate 			if (list_opts != NULL) {
3077c478bd9Sstevel@tonic-gate 				arg_error = 1;
3087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3097c478bd9Sstevel@tonic-gate 				    cmdname, c);
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 			list_opts = optarg;
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 		case 't':
3147c478bd9Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
3157c478bd9Sstevel@tonic-gate 				arg_error = 1;
3167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
3177c478bd9Sstevel@tonic-gate 				    cmdname);
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 			action = CFGA_OP_TEST;
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		case 'x':
3227c478bd9Sstevel@tonic-gate 			if (action != CFGA_OP_NONE) {
3237c478bd9Sstevel@tonic-gate 				arg_error = 1;
3247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_action),
3257c478bd9Sstevel@tonic-gate 				    cmdname);
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 			action = CFGA_OP_PRIVATE;
3287c478bd9Sstevel@tonic-gate 			act_arg = optarg;
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		case 'v':
3317c478bd9Sstevel@tonic-gate 			if ((flags & CFGA_FLAG_VERBOSE) != 0) {
3327c478bd9Sstevel@tonic-gate 				arg_error = 1;
3337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3347c478bd9Sstevel@tonic-gate 				    cmdname, c);
3357c478bd9Sstevel@tonic-gate 			}
3367c478bd9Sstevel@tonic-gate 			flags |= CFGA_FLAG_VERBOSE;
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		case 'y':
3397c478bd9Sstevel@tonic-gate 			if (confarg != CONFIRM_DEFAULT) {
3407c478bd9Sstevel@tonic-gate 				arg_error = 1;
3417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(dup_option),
3427c478bd9Sstevel@tonic-gate 				    cmdname, c);
3437c478bd9Sstevel@tonic-gate 			}
3447c478bd9Sstevel@tonic-gate 			confarg = CONFIRM_YES;
3457c478bd9Sstevel@tonic-gate 			break;
3467c478bd9Sstevel@tonic-gate 		case '?':	/* getopts issues message is this case */
3477c478bd9Sstevel@tonic-gate 		default:	/* catch programming errors */
3487c478bd9Sstevel@tonic-gate 			arg_error = 1;
3497c478bd9Sstevel@tonic-gate 			break;
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/* default action is list */
3547c478bd9Sstevel@tonic-gate 	if (action == CFGA_OP_NONE)
3557c478bd9Sstevel@tonic-gate 		action = CFGA_OP_LIST;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/* -s and -a option only for list */
3587c478bd9Sstevel@tonic-gate 	if (action != CFGA_OP_LIST && (list_opts != NULL || dyn_exp)) {
3597c478bd9Sstevel@tonic-gate 		arg_error = 1;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (arg_error) {
3637c478bd9Sstevel@tonic-gate 		usage();
3647c478bd9Sstevel@tonic-gate 		exit(EXIT_ARGERROR);
3657c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	ap_args = &argv[optind];
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * If neither -n of -y was specified, interactive confirmation
3727c478bd9Sstevel@tonic-gate 	 * is used.  Check if the program has terminal I/O and
3737c478bd9Sstevel@tonic-gate 	 * enforce -n if not.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	(void) memset(&confirm, 0, sizeof (confirm));
3767c478bd9Sstevel@tonic-gate 	if (action == CFGA_OP_CHANGE_STATE || action == CFGA_OP_PRIVATE) {
3777c478bd9Sstevel@tonic-gate 		if (confarg == CONFIRM_DEFAULT &&
3787c478bd9Sstevel@tonic-gate 		    !(isatty(fileno(stdin)) && isatty(fileno(stderr))))
3797c478bd9Sstevel@tonic-gate 			confarg = CONFIRM_NO;
3807c478bd9Sstevel@tonic-gate 		switch (confarg) {
3817c478bd9Sstevel@tonic-gate 		case CONFIRM_DEFAULT:
3827c478bd9Sstevel@tonic-gate 			confirm.confirm = confirm_interactive;
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		case CONFIRM_NO:
3857c478bd9Sstevel@tonic-gate 			confirm.confirm = confirm_no;
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 		case CONFIRM_YES:
3887c478bd9Sstevel@tonic-gate 			confirm.confirm = confirm_yes;
3897c478bd9Sstevel@tonic-gate 			break;
3907c478bd9Sstevel@tonic-gate 		default:	/* paranoia */
3917c478bd9Sstevel@tonic-gate 			abort();
3927c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * set up message output routine
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	message.message_routine = message_output;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	switch (action) {
4027c478bd9Sstevel@tonic-gate 	case CFGA_OP_CHANGE_STATE:
4037c478bd9Sstevel@tonic-gate 	    /* Sanity check - requires an argument */
4047c478bd9Sstevel@tonic-gate 	    if ((argc - optind) <= 0) {
4057c478bd9Sstevel@tonic-gate 		usage();
4067c478bd9Sstevel@tonic-gate 		break;
4077c478bd9Sstevel@tonic-gate 	    }
4087c478bd9Sstevel@tonic-gate 	    /* Sanity check - args cannot be ap_types */
4097c478bd9Sstevel@tonic-gate 	    for (i = 0; i < (argc - optind); i++) {
4107c478bd9Sstevel@tonic-gate 		    if (find_arg_type(ap_args[i]) == AP_TYPE) {
4117c478bd9Sstevel@tonic-gate 			usage();
4127c478bd9Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
4137c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4147c478bd9Sstevel@tonic-gate 		    }
4157c478bd9Sstevel@tonic-gate 	    }
4167c478bd9Sstevel@tonic-gate 	    ret = config_change_state(sc_opt, argc - optind, ap_args, plat_opts,
4177c478bd9Sstevel@tonic-gate 		&confirm, &message, &estrp, flags);
4187c478bd9Sstevel@tonic-gate 	    if (ret != CFGA_OK)
4197c478bd9Sstevel@tonic-gate 		    cfgadm_error(ret, estrp);
4207c478bd9Sstevel@tonic-gate 	    break;
4217c478bd9Sstevel@tonic-gate 	case CFGA_OP_PRIVATE:
4227c478bd9Sstevel@tonic-gate 	    /* Sanity check - requires an argument */
4237c478bd9Sstevel@tonic-gate 	    if ((argc - optind) <= 0) {
4247c478bd9Sstevel@tonic-gate 		usage();
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate 	    }
4277c478bd9Sstevel@tonic-gate 	    /* Sanity check - args cannot be ap_types */
4287c478bd9Sstevel@tonic-gate 	    for (i = 0; i < (argc - optind); i++) {
4297c478bd9Sstevel@tonic-gate 		    if (find_arg_type(ap_args[i]) == AP_TYPE) {
4307c478bd9Sstevel@tonic-gate 			usage();
4317c478bd9Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
4327c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4337c478bd9Sstevel@tonic-gate 		    }
4347c478bd9Sstevel@tonic-gate 	    }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	    ret = config_private_func(act_arg, argc - optind, ap_args,
4377c478bd9Sstevel@tonic-gate 		plat_opts, &confirm, &message, &estrp, flags);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	    if (ret != CFGA_OK)
4407c478bd9Sstevel@tonic-gate 		    cfgadm_error(ret, estrp);
4417c478bd9Sstevel@tonic-gate 	    break;
4427c478bd9Sstevel@tonic-gate 	case CFGA_OP_TEST:
4437c478bd9Sstevel@tonic-gate 	    /* Sanity check - requires an argument */
4447c478bd9Sstevel@tonic-gate 	    if ((argc - optind) <= 0) {
4457c478bd9Sstevel@tonic-gate 		usage();
4467c478bd9Sstevel@tonic-gate 		break;
4477c478bd9Sstevel@tonic-gate 	    }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	    if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
4507c478bd9Sstevel@tonic-gate 		usage();
4517c478bd9Sstevel@tonic-gate 		exit(EXIT_ARGERROR);
4527c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4537c478bd9Sstevel@tonic-gate 	    }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	    /* Sanity check - args cannot be ap_types */
4567c478bd9Sstevel@tonic-gate 	    for (i = 0; i < (argc - optind); i++) {
4577c478bd9Sstevel@tonic-gate 		    if (find_arg_type(ap_args[i]) == AP_TYPE) {
4587c478bd9Sstevel@tonic-gate 			usage();
4597c478bd9Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
4607c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
4617c478bd9Sstevel@tonic-gate 		    }
4627c478bd9Sstevel@tonic-gate 	    }
4637c478bd9Sstevel@tonic-gate 	    ret = config_test(argc - optind, ap_args, plat_opts, &message,
4647c478bd9Sstevel@tonic-gate 		&estrp, flags);
4657c478bd9Sstevel@tonic-gate 	    if (ret != CFGA_OK)
4667c478bd9Sstevel@tonic-gate 		    cfgadm_error(ret, estrp);
4677c478bd9Sstevel@tonic-gate 	    break;
4687c478bd9Sstevel@tonic-gate 	case CFGA_OP_HELP:
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	    if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
4717c478bd9Sstevel@tonic-gate 		usage();
4727c478bd9Sstevel@tonic-gate 		exit(EXIT_ARGERROR);
4737c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4747c478bd9Sstevel@tonic-gate 	    }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	    /* always do usage? */
4777c478bd9Sstevel@tonic-gate 	    usage();
4787c478bd9Sstevel@tonic-gate 	    ret = config_help(argc - optind, ap_args, &message, plat_opts,
4797c478bd9Sstevel@tonic-gate 		flags);
4807c478bd9Sstevel@tonic-gate 	    if (ret != CFGA_OK)
4817c478bd9Sstevel@tonic-gate 		    cfgadm_error(ret, estrp);
4827c478bd9Sstevel@tonic-gate 	    break;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	case CFGA_OP_LIST: {
485*3977d6cdSvikram 		/*
486*3977d6cdSvikram 		 * Note that we leak the strdup strings below (we never free
487*3977d6cdSvikram 		 * them). This is ok in this context since cfgadm is
488*3977d6cdSvikram 		 * a short lived process that will exit shortly freeing
489*3977d6cdSvikram 		 * the memory.
490*3977d6cdSvikram 		 */
4917c478bd9Sstevel@tonic-gate 		cfga_list_data_t *list_array = NULL;
4927c478bd9Sstevel@tonic-gate 		int nlist = 0;
493*3977d6cdSvikram 		char *sort_fields = s_strdup(DEF_SORT_FIELDS);
494*3977d6cdSvikram 		char *cols = s_strdup(DEF_COLS);
495*3977d6cdSvikram 		char *cols2 = s_strdup(DEF_COLS2);
4967c478bd9Sstevel@tonic-gate 		int noheadings = 0;
497*3977d6cdSvikram 		char *delim = s_strdup(DEF_DELIM);
4987c478bd9Sstevel@tonic-gate 		int exitcode = EXIT_OK;
4997c478bd9Sstevel@tonic-gate 		int i;
5007c478bd9Sstevel@tonic-gate 		int type = 0;
5017c478bd9Sstevel@tonic-gate 		char *selectp = NULL, *matchp = NULL, *prefilt_optp = NULL;
5027c478bd9Sstevel@tonic-gate 		post_filter_t *post_filtp = NULL;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
5057c478bd9Sstevel@tonic-gate 			usage();
5067c478bd9Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
5077c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5087c478bd9Sstevel@tonic-gate 		}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		if (flags & CFGA_FLAG_VERBOSE) {
511*3977d6cdSvikram 			cols = s_strdup(DEF_COLS_VERBOSE);
512*3977d6cdSvikram 			cols2 = s_strdup(DEF_COLS2_VERBOSE);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		if (list_opts != NULL && !extract_list_suboptions(list_opts,
5167c478bd9Sstevel@tonic-gate 		    &sort_fields, &cols, &cols2, &noheadings, &delim,
5177c478bd9Sstevel@tonic-gate 		    &selectp, &matchp)) {
5187c478bd9Sstevel@tonic-gate 			usage_field();
5197c478bd9Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
5207c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		/*
5247c478bd9Sstevel@tonic-gate 		 * Scan any args and see if there are any ap_types.
5257c478bd9Sstevel@tonic-gate 		 * If there are we get all attachment point stats and
5267c478bd9Sstevel@tonic-gate 		 * then filter what gets printed.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		type = 0;
5307c478bd9Sstevel@tonic-gate 		for (i = 0; i < (argc - optind); i++) {
5317c478bd9Sstevel@tonic-gate 			if (find_arg_type(ap_args[i]) == AP_TYPE) {
5327c478bd9Sstevel@tonic-gate 				type = 1;
5337c478bd9Sstevel@tonic-gate 				/* ap_types cannot have dynamic components */
5347c478bd9Sstevel@tonic-gate 				if (get_dyn(ap_args[i]) != NULL) {
5357c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
5367c478bd9Sstevel@tonic-gate 					    gettext(aptype_no_dyn),
5377c478bd9Sstevel@tonic-gate 					    cmdname, ap_args[i]);
5387c478bd9Sstevel@tonic-gate 					exit(EXIT_ARGERROR);
5397c478bd9Sstevel@tonic-gate 					/*NOTREACHED*/
5407c478bd9Sstevel@tonic-gate 				}
5417c478bd9Sstevel@tonic-gate 				break;
5427c478bd9Sstevel@tonic-gate 			}
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		/* Setup filter */
5467c478bd9Sstevel@tonic-gate 		post_filtp = config_calloc_check(1, sizeof (*post_filtp));
5477c478bd9Sstevel@tonic-gate 		if (post_filtp == NULL) {
5487c478bd9Sstevel@tonic-gate 			exit(EXIT_OPFAILED);
5497c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		if (setup_filter(selectp, matchp, post_filtp, &prefilt_optp)
5527c478bd9Sstevel@tonic-gate 		    != CFGA_OK) {
5537c478bd9Sstevel@tonic-gate 			S_FREE(post_filtp);
5547c478bd9Sstevel@tonic-gate 			exit(EXIT_ARGERROR);
5557c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		list_array = NULL;
5597c478bd9Sstevel@tonic-gate 		exitcode = EXIT_OK;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * Check for args. No args means find all libs
5637c478bd9Sstevel@tonic-gate 		 * and call the cfga_list_ext routine with no ap_ids specified.
5647c478bd9Sstevel@tonic-gate 		 * With args, if any one of the args are ap_types we
5657c478bd9Sstevel@tonic-gate 		 * again find all attachment points as in the
5667c478bd9Sstevel@tonic-gate 		 * no-args case above and then select which attachment points
5677c478bd9Sstevel@tonic-gate 		 * are actually displayed.
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		if (((argc - optind) == 0) || (type == 1)) {
5707c478bd9Sstevel@tonic-gate 			/*
5717c478bd9Sstevel@tonic-gate 			 * No args, or atleast 1 ap_type arg
5727c478bd9Sstevel@tonic-gate 			 */
5737c478bd9Sstevel@tonic-gate 			ret = config_list_ext(0, NULL, &list_array,
5747c478bd9Sstevel@tonic-gate 			    &nlist, plat_opts, prefilt_optp, &estrp,
5757c478bd9Sstevel@tonic-gate 			    dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
5767c478bd9Sstevel@tonic-gate 		} else {
5777c478bd9Sstevel@tonic-gate 			/*
5787c478bd9Sstevel@tonic-gate 			 * If the args are all ap_ids (no ap_types) we call the
5797c478bd9Sstevel@tonic-gate 			 * cfga_list_ext routine with those specific ap_ids.
5807c478bd9Sstevel@tonic-gate 			 */
5817c478bd9Sstevel@tonic-gate 			ret = config_list_ext(argc - optind, ap_args,
5827c478bd9Sstevel@tonic-gate 			    &list_array, &nlist, plat_opts, prefilt_optp,
5837c478bd9Sstevel@tonic-gate 			    &estrp, dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		S_FREE(prefilt_optp);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 		if (ret == CFGA_OK) {
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 			if (do_config_list(
5917c478bd9Sstevel@tonic-gate 			    (argc - optind), &argv[optind], list_array, nlist,
5927c478bd9Sstevel@tonic-gate 			    sort_fields, cols, cols2, noheadings, delim,
5937c478bd9Sstevel@tonic-gate 			    post_filtp, dyn_exp) != CFGA_OK) {
5947c478bd9Sstevel@tonic-gate 				exitcode = EXIT_ARGERROR;
5957c478bd9Sstevel@tonic-gate 			} else {
5967c478bd9Sstevel@tonic-gate 				exitcode = EXIT_OK;
5977c478bd9Sstevel@tonic-gate 			}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 			S_FREE(list_array);
6007c478bd9Sstevel@tonic-gate 			S_FREE(post_filtp);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 			if (exitcode != EXIT_OK) {
6037c478bd9Sstevel@tonic-gate 				exit(exitcode);
6047c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 		} else {
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 			S_FREE(post_filtp);
6097c478bd9Sstevel@tonic-gate 			cfgadm_error(ret, estrp);
6107c478bd9Sstevel@tonic-gate 		}
6117c478bd9Sstevel@tonic-gate 		break;
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 	default:	/* paranoia */
6147c478bd9Sstevel@tonic-gate 		abort();
6157c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (ret == CFGA_NOTSUPP) {
619*3977d6cdSvikram 		return (EXIT_NOTSUPP);
6207c478bd9Sstevel@tonic-gate 	} else if (ret != CFGA_OK) {
621*3977d6cdSvikram 		return (EXIT_OPFAILED);
6227c478bd9Sstevel@tonic-gate 	} else {
623*3977d6cdSvikram 		return (EXIT_OK);
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate  * usage - outputs the usage help message.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate static void
6327c478bd9Sstevel@tonic-gate usage(
6337c478bd9Sstevel@tonic-gate 	void)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	int i;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n", gettext("Usage:"));
6387c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (usage_tab)/sizeof (usage_tab[0]); i++) {
6397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(usage_tab[i]), cmdname);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * Emit an error message.
6457c478bd9Sstevel@tonic-gate  * As a side-effect the hardware specific error message is deallocated
6467c478bd9Sstevel@tonic-gate  * as described in config_admin(3X).
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate static void
6497c478bd9Sstevel@tonic-gate cfgadm_error(int errnum, char *estrp)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	const char *ep;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	ep = config_strerror(errnum);
6547c478bd9Sstevel@tonic-gate 	if (ep == NULL)
6557c478bd9Sstevel@tonic-gate 		ep = gettext("configuration administration unknown error");
6567c478bd9Sstevel@tonic-gate 	if (estrp != NULL && *estrp != '\0') {
6577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", cmdname, ep, estrp);
6587c478bd9Sstevel@tonic-gate 	} else {
6597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n", cmdname, ep);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 	if (estrp != NULL)
6627c478bd9Sstevel@tonic-gate 		free((void *)estrp);
6637c478bd9Sstevel@tonic-gate 	if (errnum == CFGA_INVAL)
6647c478bd9Sstevel@tonic-gate 		usage();
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate  * confirm_interactive - prompt user for confirmation
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate static int
6717c478bd9Sstevel@tonic-gate confirm_interactive(
6727c478bd9Sstevel@tonic-gate 	void *appdata_ptr,
6737c478bd9Sstevel@tonic-gate 	const char *message)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	static char yeschr[YESNO_STR_MAX + 2];
6767c478bd9Sstevel@tonic-gate 	static char nochr[YESNO_STR_MAX + 2];
6777c478bd9Sstevel@tonic-gate 	static int inited = 0;
6787c478bd9Sstevel@tonic-gate 	int isyes;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate #ifdef lint
6817c478bd9Sstevel@tonic-gate 	appdata_ptr = appdata_ptr;
6827c478bd9Sstevel@tonic-gate #endif /* lint */
6837c478bd9Sstevel@tonic-gate 	/*
6847c478bd9Sstevel@tonic-gate 	 * First time through initialisation.  In the original
6857c478bd9Sstevel@tonic-gate 	 * version of this command this function is only called once,
6867c478bd9Sstevel@tonic-gate 	 * but this function is generalized for the future.
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	if (!inited) {
6897c478bd9Sstevel@tonic-gate 		(void) strncpy(yeschr, nl_langinfo(YESSTR), YESNO_STR_MAX + 1);
6907c478bd9Sstevel@tonic-gate 		(void) strncpy(nochr, nl_langinfo(NOSTR), YESNO_STR_MAX + 1);
6917c478bd9Sstevel@tonic-gate 		inited = 1;
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	do {
6957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s (%s/%s)? ", message, yeschr, nochr);
6967c478bd9Sstevel@tonic-gate 		isyes = yesno(yeschr, nochr);
6977c478bd9Sstevel@tonic-gate 	} while (isyes == -1);
6987c478bd9Sstevel@tonic-gate 	return (isyes);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * If any text is input it must sub-string match either yes or no.
7037c478bd9Sstevel@tonic-gate  * Failure of this match is indicated by return of -1.
7047c478bd9Sstevel@tonic-gate  * If an empty line is input, this is taken as no.
7057c478bd9Sstevel@tonic-gate  */
7067c478bd9Sstevel@tonic-gate static int
7077c478bd9Sstevel@tonic-gate yesno(
7087c478bd9Sstevel@tonic-gate 	char *yesp,
7097c478bd9Sstevel@tonic-gate 	char *nop)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	int	i, b;
7127c478bd9Sstevel@tonic-gate 	char	ans[YESNO_STR_MAX + 1];
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	i = 0;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
7177c478bd9Sstevel@tonic-gate 	while (1) {
7187c478bd9Sstevel@tonic-gate 		b = getc(stdin);	/* more explicit that rm.c version */
7197c478bd9Sstevel@tonic-gate 		if (b == '\n' || b == '\0' || b == EOF) {
7207c478bd9Sstevel@tonic-gate 			if (i < YESNO_STR_MAX)	/* bug fix to rm.c version */
7217c478bd9Sstevel@tonic-gate 				ans[i] = 0;
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		if (i < YESNO_STR_MAX)
7257c478bd9Sstevel@tonic-gate 			ans[i] = b;
7267c478bd9Sstevel@tonic-gate 		i++;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	if (i >= YESNO_STR_MAX) {
7297c478bd9Sstevel@tonic-gate 		i = YESNO_STR_MAX;
7307c478bd9Sstevel@tonic-gate 		ans[YESNO_STR_MAX] = 0;
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	/* changes to rm.c version follow */
7337c478bd9Sstevel@tonic-gate 	if (i == 0)
7347c478bd9Sstevel@tonic-gate 		return (0);
7357c478bd9Sstevel@tonic-gate 	if (strncmp(nop, ans, i) == 0)
7367c478bd9Sstevel@tonic-gate 		return (0);
7377c478bd9Sstevel@tonic-gate 	if (strncmp(yesp, ans, i) == 0)
7387c478bd9Sstevel@tonic-gate 		return (1);
7397c478bd9Sstevel@tonic-gate 	return (-1);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7437c478bd9Sstevel@tonic-gate static int
7447c478bd9Sstevel@tonic-gate confirm_no(
7457c478bd9Sstevel@tonic-gate 	void *appdata_ptr,
7467c478bd9Sstevel@tonic-gate 	const char *message)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	return (0);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7527c478bd9Sstevel@tonic-gate static int
7537c478bd9Sstevel@tonic-gate confirm_yes(
7547c478bd9Sstevel@tonic-gate 	void *appdata_ptr,
7557c478bd9Sstevel@tonic-gate 	const char *message)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	return (1);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate  * Find base name of filename.
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate static char *
7647c478bd9Sstevel@tonic-gate basename(
7657c478bd9Sstevel@tonic-gate 	char *cp)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	char *sp;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if ((sp = strrchr(cp, '/')) != NULL)
7707c478bd9Sstevel@tonic-gate 		return (sp + 1);
7717c478bd9Sstevel@tonic-gate 	return (cp);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7757c478bd9Sstevel@tonic-gate static int
7767c478bd9Sstevel@tonic-gate message_output(
7777c478bd9Sstevel@tonic-gate 	void *appdata_ptr,
7787c478bd9Sstevel@tonic-gate 	const char *message)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", message);
7817c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /*
7867c478bd9Sstevel@tonic-gate  * extract_list_suboptions - process list option string
7877c478bd9Sstevel@tonic-gate  */
7887c478bd9Sstevel@tonic-gate static int
7897c478bd9Sstevel@tonic-gate extract_list_suboptions(
7907c478bd9Sstevel@tonic-gate 	char *arg,
7917c478bd9Sstevel@tonic-gate 	char **sortpp,
7927c478bd9Sstevel@tonic-gate 	char **colspp,
7937c478bd9Sstevel@tonic-gate 	char **cols2pp,
7947c478bd9Sstevel@tonic-gate 	int *noheadingsp,
7957c478bd9Sstevel@tonic-gate 	char **delimpp,
7967c478bd9Sstevel@tonic-gate 	char **selectpp,
7977c478bd9Sstevel@tonic-gate 	char **matchpp)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 	char *value = NULL;
8007c478bd9Sstevel@tonic-gate 	int subopt = 0;
8017c478bd9Sstevel@tonic-gate 	int err = 0;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	while (*arg != '\0') {
8047c478bd9Sstevel@tonic-gate 		static char need_value[] =
8057c478bd9Sstevel@tonic-gate "%s: sub-option \"%s\" requires a value\n";
8067c478bd9Sstevel@tonic-gate 		static char no_value[] =
8077c478bd9Sstevel@tonic-gate "%s: sub-option \"%s\" does not take a value\n";
8087c478bd9Sstevel@tonic-gate 		static char unk_subopt[] =
8097c478bd9Sstevel@tonic-gate "%s: sub-option \"%s\" unknown\n";
8107c478bd9Sstevel@tonic-gate 		char **pptr;
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 		subopt = getsubopt(&arg, list_options, &value);
8137c478bd9Sstevel@tonic-gate 		switch (subopt) {
8147c478bd9Sstevel@tonic-gate 		case LIST_SORT:
8157c478bd9Sstevel@tonic-gate 			pptr = sortpp;
8167c478bd9Sstevel@tonic-gate 			goto valcom;
8177c478bd9Sstevel@tonic-gate 		case LIST_COLS:
8187c478bd9Sstevel@tonic-gate 			pptr = colspp;
8197c478bd9Sstevel@tonic-gate 			goto valcom;
8207c478bd9Sstevel@tonic-gate 		case LIST_COLS2:
8217c478bd9Sstevel@tonic-gate 			pptr = cols2pp;
8227c478bd9Sstevel@tonic-gate 			goto valcom;
8237c478bd9Sstevel@tonic-gate 		case LIST_SELECT:
8247c478bd9Sstevel@tonic-gate 			pptr = selectpp;
8257c478bd9Sstevel@tonic-gate 			goto valcom;
8267c478bd9Sstevel@tonic-gate 		case LIST_MATCH:
8277c478bd9Sstevel@tonic-gate 			pptr = matchpp;
8287c478bd9Sstevel@tonic-gate 			goto valcom;
8297c478bd9Sstevel@tonic-gate 		case LIST_DELIM:
8307c478bd9Sstevel@tonic-gate 			pptr = delimpp;
8317c478bd9Sstevel@tonic-gate 		valcom:
8327c478bd9Sstevel@tonic-gate 			if (value == NULL) {
8337c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(need_value),
8347c478bd9Sstevel@tonic-gate 				    cmdname, list_options[subopt]);
8357c478bd9Sstevel@tonic-gate 				err = 1;
8367c478bd9Sstevel@tonic-gate 			} else
8377c478bd9Sstevel@tonic-gate 				*pptr = value;
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 		case LIST_NOHEADINGS:
8407c478bd9Sstevel@tonic-gate 			if (value != NULL) {
8417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(no_value),
8427c478bd9Sstevel@tonic-gate 				    cmdname, list_options[subopt]);
8437c478bd9Sstevel@tonic-gate 				err = 1;
8447c478bd9Sstevel@tonic-gate 			} else
8457c478bd9Sstevel@tonic-gate 				*noheadingsp = 1;
8467c478bd9Sstevel@tonic-gate 			break;
8477c478bd9Sstevel@tonic-gate 		default:
8487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(unk_subopt),
8497c478bd9Sstevel@tonic-gate 			    cmdname, value);
8507c478bd9Sstevel@tonic-gate 			err = 1;
8517c478bd9Sstevel@tonic-gate 			break;
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	return (err == 0);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate static cfga_err_t
8587c478bd9Sstevel@tonic-gate setup_prefilter(post_filter_t *post_filtp, char **prefilt_optpp)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	size_t len;
8617c478bd9Sstevel@tonic-gate 	const char *clopt = PREFILT_CLASS_STR;
8627c478bd9Sstevel@tonic-gate 	int idx;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	*prefilt_optpp = NULL;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/* Get the index for the "class" field */
8687c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < N_FIELDS; idx++) {
8697c478bd9Sstevel@tonic-gate 		if (strcmp(all_fields[idx].name, PREFILT_CLASS_STR) == 0)
8707c478bd9Sstevel@tonic-gate 			break;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * Currently pre-filter available only for class fld w/ EXACT match
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 	if (idx >= N_FIELDS ||
8777c478bd9Sstevel@tonic-gate 	    post_filtp->match_type_p[idx] != CFGA_MATCH_EXACT) {
8787c478bd9Sstevel@tonic-gate 		return (CFGA_OK);
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	len = strlen(clopt) + strlen(post_filtp->ldata.ap_class) + 1;
8827c478bd9Sstevel@tonic-gate 	if ((*prefilt_optpp = config_calloc_check(1, len)) == NULL) {
8837c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	(void) strcpy(*prefilt_optpp, clopt);
8877c478bd9Sstevel@tonic-gate 	(void) strcat(*prefilt_optpp, post_filtp->ldata.ap_class);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	/*
8907c478bd9Sstevel@tonic-gate 	 * Since it is being pre-filtered, this attribute does not need
8917c478bd9Sstevel@tonic-gate 	 * post-filtering.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	post_filtp->match_type_p[idx] = CFGA_MATCH_NOFILTER;
8947c478bd9Sstevel@tonic-gate 	if (all_fields[idx].set_filter != NULL) {
8957c478bd9Sstevel@tonic-gate 		(void) all_fields[idx].set_filter(&post_filtp->ldata, "");
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate static cfga_err_t
9027c478bd9Sstevel@tonic-gate set_attrval(
9037c478bd9Sstevel@tonic-gate 	const char *attr,
9047c478bd9Sstevel@tonic-gate 	const char *val,
9057c478bd9Sstevel@tonic-gate 	post_filter_t *post_filtp,
9067c478bd9Sstevel@tonic-gate 	match_type_t match_type)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	int fld = 0;
9097c478bd9Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	for (fld = 0; fld < N_FIELDS; fld++) {
9127c478bd9Sstevel@tonic-gate 		if (strcmp(attr, all_fields[fld].name) == 0)
9137c478bd9Sstevel@tonic-gate 			break;
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/* Valid field or is the select option supported for this field */
9177c478bd9Sstevel@tonic-gate 	if (fld >= N_FIELDS || all_fields[fld].set_filter == NULL) {
9187c478bd9Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if ((ret = all_fields[fld].set_filter(&post_filtp->ldata, val))
9227c478bd9Sstevel@tonic-gate 	    == CFGA_OK) {
9237c478bd9Sstevel@tonic-gate 		post_filtp->match_type_p[fld] = match_type;
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	return (ret);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate static char inval_optarg[] =
9317c478bd9Sstevel@tonic-gate 	"%s: invalid value \"%s\" for %s suboption.\n";
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Parses the "select" string and fills in the post_filter structure
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate static cfga_err_t
9377c478bd9Sstevel@tonic-gate parse_select_opt(
9387c478bd9Sstevel@tonic-gate 	const char *selectp,
9397c478bd9Sstevel@tonic-gate 	post_filter_t *post_filtp,
9407c478bd9Sstevel@tonic-gate 	match_type_t match_type)
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 	parse_state_t state = CFGA_PSTATE_INIT;
9437c478bd9Sstevel@tonic-gate 	char *cp = NULL, *optstr = NULL, *attr = NULL, *val = NULL;
9447c478bd9Sstevel@tonic-gate 	int bal = 0;	/* Tracks balancing */
9457c478bd9Sstevel@tonic-gate 	char chr;
9467c478bd9Sstevel@tonic-gate 	cfga_err_t ret;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (selectp == NULL || post_filtp == NULL) {
9507c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	optstr = config_calloc_check(1, strlen(selectp) + 1);
9547c478bd9Sstevel@tonic-gate 	if (optstr == NULL) {
9557c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	(void) strcpy(optstr, selectp);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/* Init */
9617c478bd9Sstevel@tonic-gate 	ret = CFGA_ATTR_INVAL;
9627c478bd9Sstevel@tonic-gate 	bal = 0;
9637c478bd9Sstevel@tonic-gate 	cp = attr = optstr;
9647c478bd9Sstevel@tonic-gate 	state = CFGA_PSTATE_INIT;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	for (; *cp != '\0'; cp++) {
9677c478bd9Sstevel@tonic-gate 		switch (state) {
9687c478bd9Sstevel@tonic-gate 		case CFGA_PSTATE_INIT:
9697c478bd9Sstevel@tonic-gate 			if (*cp != LEFT_PAREN)
9707c478bd9Sstevel@tonic-gate 				break;
9717c478bd9Sstevel@tonic-gate 			*cp = '\0';
9727c478bd9Sstevel@tonic-gate 			val = cp + 1;
9737c478bd9Sstevel@tonic-gate 			bal = 1;
9747c478bd9Sstevel@tonic-gate 			state = CFGA_PSTATE_ATTR_DONE;
9757c478bd9Sstevel@tonic-gate 			break;
9767c478bd9Sstevel@tonic-gate 		case CFGA_PSTATE_ATTR_DONE:
9777c478bd9Sstevel@tonic-gate 			chr = *cp;
9787c478bd9Sstevel@tonic-gate 			switch (chr) {
9797c478bd9Sstevel@tonic-gate 			case LEFT_PAREN:
9807c478bd9Sstevel@tonic-gate 				bal++;
9817c478bd9Sstevel@tonic-gate 				break;
9827c478bd9Sstevel@tonic-gate 			case RIGHT_PAREN:
9837c478bd9Sstevel@tonic-gate 				bal--;
9847c478bd9Sstevel@tonic-gate 				if (bal == 0) {
9857c478bd9Sstevel@tonic-gate 					*cp = '\0';
9867c478bd9Sstevel@tonic-gate 					state = CFGA_PSTATE_VAL_DONE;
9877c478bd9Sstevel@tonic-gate 				}
9887c478bd9Sstevel@tonic-gate 				break;
9897c478bd9Sstevel@tonic-gate 			}
9907c478bd9Sstevel@tonic-gate 			break;
9917c478bd9Sstevel@tonic-gate 		case CFGA_PSTATE_VAL_DONE:
9927c478bd9Sstevel@tonic-gate 			if (*cp != ':') {
9937c478bd9Sstevel@tonic-gate 				state = CFGA_PSTATE_ERR;
9947c478bd9Sstevel@tonic-gate 				goto out;
9957c478bd9Sstevel@tonic-gate 			}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 			*cp = '\0';
9987c478bd9Sstevel@tonic-gate 			if (set_attrval(attr, val, post_filtp,
9997c478bd9Sstevel@tonic-gate 			    match_type) != CFGA_OK) {
10007c478bd9Sstevel@tonic-gate 				state = CFGA_PSTATE_ERR;
10017c478bd9Sstevel@tonic-gate 				goto out;
10027c478bd9Sstevel@tonic-gate 			}
10037c478bd9Sstevel@tonic-gate 			state = CFGA_PSTATE_INIT;
10047c478bd9Sstevel@tonic-gate 			attr = cp + 1;
10057c478bd9Sstevel@tonic-gate 			break;
10067c478bd9Sstevel@tonic-gate 		default:
10077c478bd9Sstevel@tonic-gate 			state = CFGA_PSTATE_ERR;
10087c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
10097c478bd9Sstevel@tonic-gate 		case CFGA_PSTATE_ERR:
10107c478bd9Sstevel@tonic-gate 			goto out;
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/*FALLTHRU*/
10157c478bd9Sstevel@tonic-gate out:
10167c478bd9Sstevel@tonic-gate 	if (state == CFGA_PSTATE_VAL_DONE) {
10177c478bd9Sstevel@tonic-gate 		ret = set_attrval(attr, val, post_filtp, match_type);
10187c478bd9Sstevel@tonic-gate 	} else {
10197c478bd9Sstevel@tonic-gate 		ret = CFGA_ATTR_INVAL;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	if (ret != CFGA_OK) {
10237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(inval_optarg), cmdname,
10247c478bd9Sstevel@tonic-gate 		    selectp, list_options[LIST_SELECT]);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	S_FREE(optstr);
10287c478bd9Sstevel@tonic-gate 	return (ret);
10297c478bd9Sstevel@tonic-gate }
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate static cfga_err_t
10347c478bd9Sstevel@tonic-gate setup_filter(
10357c478bd9Sstevel@tonic-gate 	const char *selectp,
10367c478bd9Sstevel@tonic-gate 	const char *matchp,
10377c478bd9Sstevel@tonic-gate 	post_filter_t *post_filtp,
10387c478bd9Sstevel@tonic-gate 	char **prefilt_optpp)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
10417c478bd9Sstevel@tonic-gate 	match_type_t match_type = CFGA_MATCH_NOFILTER;
10427c478bd9Sstevel@tonic-gate 	int i;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	static char match_needs_select[] =
10457c478bd9Sstevel@tonic-gate 	    "%s: %s suboption can only be used with %s suboption.\n";
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	*prefilt_optpp = NULL;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	/*
10517c478bd9Sstevel@tonic-gate 	 * Initial: no filtering.
10527c478bd9Sstevel@tonic-gate 	 * CFGA_MATCH_NOFILTER is NOT a valid user input
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	for (i = 0; i < N_FIELDS; i++) {
10557c478bd9Sstevel@tonic-gate 		post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	/* Determine type of match */
10597c478bd9Sstevel@tonic-gate 	if (matchp == NULL && selectp == NULL) {
10607c478bd9Sstevel@tonic-gate 		/* No filtering */
10617c478bd9Sstevel@tonic-gate 		return (CFGA_OK);
10627c478bd9Sstevel@tonic-gate 	} else if (matchp == NULL && selectp != NULL) {
10637c478bd9Sstevel@tonic-gate 		match_type = CFGA_DEFAULT_MATCH;
10647c478bd9Sstevel@tonic-gate 	} else if (matchp != NULL && selectp == NULL) {
10657c478bd9Sstevel@tonic-gate 		/* If only match specified, select criteria also needed */
10667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(match_needs_select),
10677c478bd9Sstevel@tonic-gate 		    cmdname, list_options[LIST_MATCH],
10687c478bd9Sstevel@tonic-gate 		    list_options[LIST_SELECT]);
10697c478bd9Sstevel@tonic-gate 		return (CFGA_ERROR);
10707c478bd9Sstevel@tonic-gate 	} else {
10717c478bd9Sstevel@tonic-gate 		for (i = 0; i < N_MATCH_TYPES; i++) {
10727c478bd9Sstevel@tonic-gate 			if (strcmp(matchp, match_type_array[i].str) == 0) {
10737c478bd9Sstevel@tonic-gate 				match_type = match_type_array[i].type;
10747c478bd9Sstevel@tonic-gate 				break;
10757c478bd9Sstevel@tonic-gate 			}
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 		if (i >= N_MATCH_TYPES) {
10787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(inval_optarg), cmdname,
10797c478bd9Sstevel@tonic-gate 			    matchp, list_options[LIST_MATCH]);
10807c478bd9Sstevel@tonic-gate 			return (CFGA_ERROR);
10817c478bd9Sstevel@tonic-gate 		}
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if ((ret = parse_select_opt(selectp, post_filtp, match_type))
10857c478bd9Sstevel@tonic-gate 	    != CFGA_OK) {
10867c478bd9Sstevel@tonic-gate 		return (ret);
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/* Handle pre-filtering. */
10907c478bd9Sstevel@tonic-gate 	if ((ret = setup_prefilter(post_filtp, prefilt_optpp)) != CFGA_OK) {
10917c478bd9Sstevel@tonic-gate 		/* Cleanup */
10927c478bd9Sstevel@tonic-gate 		for (i = 0; i < N_FIELDS; i++) {
10937c478bd9Sstevel@tonic-gate 			post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 		return (ret);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate  * compare_ap_id - compare two ap_id's
11047c478bd9Sstevel@tonic-gate  *
11057c478bd9Sstevel@tonic-gate  * For partial matches, argument order is significant. The filtering criterion
11067c478bd9Sstevel@tonic-gate  * should be the first argument.
11077c478bd9Sstevel@tonic-gate  */
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate static int
11107c478bd9Sstevel@tonic-gate compare_ap_id(
11117c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
11127c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
11137c478bd9Sstevel@tonic-gate 	match_type_t match_type)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	switch (match_type) {
11177c478bd9Sstevel@tonic-gate 		case CFGA_MATCH_NOFILTER:
11187c478bd9Sstevel@tonic-gate 			return (0);	/* No filtering. all pass */
11197c478bd9Sstevel@tonic-gate 		case CFGA_MATCH_PARTIAL:
11207c478bd9Sstevel@tonic-gate 			return (strncmp(p1->ap_log_id, p2->ap_log_id,
11217c478bd9Sstevel@tonic-gate 			    strlen(p1->ap_log_id)));
11227c478bd9Sstevel@tonic-gate 		case CFGA_MATCH_EXACT:
11237c478bd9Sstevel@tonic-gate 			return (strcmp(p1->ap_log_id, p2->ap_log_id));
11247c478bd9Sstevel@tonic-gate 		case CFGA_MATCH_ORDER:
11257c478bd9Sstevel@tonic-gate 		default:
11267c478bd9Sstevel@tonic-gate 			return (config_ap_id_cmp(p1->ap_log_id, p2->ap_log_id));
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * print_log_id - print logical ap_id
11327c478bd9Sstevel@tonic-gate  */
11337c478bd9Sstevel@tonic-gate static void
11347c478bd9Sstevel@tonic-gate print_log_id(
11357c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
11367c478bd9Sstevel@tonic-gate 	int width,
11377c478bd9Sstevel@tonic-gate 	char *lp)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_log_id),
11407c478bd9Sstevel@tonic-gate 	    p->ap_log_id);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate  * set_log_flt - Setup filter for logical ap_id
11457c478bd9Sstevel@tonic-gate  */
11467c478bd9Sstevel@tonic-gate static cfga_err_t
11477c478bd9Sstevel@tonic-gate set_log_flt(
11487c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
11497c478bd9Sstevel@tonic-gate 	const char *val)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	if (strlen(val) > sizeof (p->ap_log_id) - 1)
11527c478bd9Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	(void) strcpy(p->ap_log_id, val);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /*
11607c478bd9Sstevel@tonic-gate  * set_type_flt - Setup filter for type field
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate static cfga_err_t
11647c478bd9Sstevel@tonic-gate set_type_flt(
11657c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
11667c478bd9Sstevel@tonic-gate 	const char *val)
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	if (strlen(val) > sizeof (p->ap_type) - 1)
11697c478bd9Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	(void) strcpy(p->ap_type, val);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate /*
11777c478bd9Sstevel@tonic-gate  * set_class_flt - Setup filter for class field
11787c478bd9Sstevel@tonic-gate  */
11797c478bd9Sstevel@tonic-gate static cfga_err_t
11807c478bd9Sstevel@tonic-gate set_class_flt(
11817c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
11827c478bd9Sstevel@tonic-gate 	const char *val)
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	if (strlen(val) > sizeof (p->ap_class) - 1)
11857c478bd9Sstevel@tonic-gate 		return (CFGA_ATTR_INVAL);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	(void) strcpy(p->ap_class, val);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate /*
11947c478bd9Sstevel@tonic-gate  * compare_r_state - compare receptacle state of two ap_id's
11957c478bd9Sstevel@tonic-gate  */
11967c478bd9Sstevel@tonic-gate static int
11977c478bd9Sstevel@tonic-gate compare_r_state(
11987c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
11997c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
12007c478bd9Sstevel@tonic-gate 	match_type_t match_type)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	switch (match_type) {
12037c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:  /* no filtering. pass all */
12047c478bd9Sstevel@tonic-gate 		return (0);
12057c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
12067c478bd9Sstevel@tonic-gate 	default:
12077c478bd9Sstevel@tonic-gate 		return (p1->ap_r_state - p2->ap_r_state);
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate  * compare_o_state - compare occupant state of two ap_id's
12137c478bd9Sstevel@tonic-gate  */
12147c478bd9Sstevel@tonic-gate static int
12157c478bd9Sstevel@tonic-gate compare_o_state(
12167c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
12177c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
12187c478bd9Sstevel@tonic-gate 	match_type_t match_type)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	switch (match_type) {
12217c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:	/* no filtering. all pass */
12227c478bd9Sstevel@tonic-gate 		return (0);
12237c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
12247c478bd9Sstevel@tonic-gate 	default:
12257c478bd9Sstevel@tonic-gate 		return (p1->ap_o_state - p2->ap_o_state);
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate }
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate /*
12307c478bd9Sstevel@tonic-gate  * compare_busy - compare busy field of two ap_id's
12317c478bd9Sstevel@tonic-gate  */
12327c478bd9Sstevel@tonic-gate static int
12337c478bd9Sstevel@tonic-gate compare_busy(
12347c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
12357c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
12367c478bd9Sstevel@tonic-gate 	match_type_t match_type)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	switch (match_type) {
12407c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:	/* no filtering. all pass */
12417c478bd9Sstevel@tonic-gate 		return (0);
12427c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
12437c478bd9Sstevel@tonic-gate 	default:
12447c478bd9Sstevel@tonic-gate 		return (p1->ap_busy - p2->ap_busy);
12457c478bd9Sstevel@tonic-gate 	}
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate /*
12497c478bd9Sstevel@tonic-gate  * print_r_state - print receptacle state
12507c478bd9Sstevel@tonic-gate  */
12517c478bd9Sstevel@tonic-gate static void
12527c478bd9Sstevel@tonic-gate print_r_state(
12537c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
12547c478bd9Sstevel@tonic-gate 	int width,
12557c478bd9Sstevel@tonic-gate 	char *lp)
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate 	char *cp;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	switch (p->ap_r_state) {
12607c478bd9Sstevel@tonic-gate 	case CFGA_STAT_EMPTY:
12617c478bd9Sstevel@tonic-gate 		cp = "empty";
12627c478bd9Sstevel@tonic-gate 		break;
12637c478bd9Sstevel@tonic-gate 	case CFGA_STAT_CONNECTED:
12647c478bd9Sstevel@tonic-gate 		cp = "connected";
12657c478bd9Sstevel@tonic-gate 		break;
12667c478bd9Sstevel@tonic-gate 	case CFGA_STAT_DISCONNECTED:
12677c478bd9Sstevel@tonic-gate 		cp = "disconnected";
12687c478bd9Sstevel@tonic-gate 		break;
12697c478bd9Sstevel@tonic-gate 	default:
12707c478bd9Sstevel@tonic-gate 		cp = "???";
12717c478bd9Sstevel@tonic-gate 		break;
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, cp);
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate /*
12777c478bd9Sstevel@tonic-gate  * print_o_state - print occupant state
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate static void
12807c478bd9Sstevel@tonic-gate print_o_state(
12817c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
12827c478bd9Sstevel@tonic-gate 	int width,
12837c478bd9Sstevel@tonic-gate 	char *lp)
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	char *cp;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	switch (p->ap_o_state) {
12887c478bd9Sstevel@tonic-gate 	case CFGA_STAT_UNCONFIGURED:
12897c478bd9Sstevel@tonic-gate 		cp = "unconfigured";
12907c478bd9Sstevel@tonic-gate 		break;
12917c478bd9Sstevel@tonic-gate 	case CFGA_STAT_CONFIGURED:
12927c478bd9Sstevel@tonic-gate 		cp = "configured";
12937c478bd9Sstevel@tonic-gate 		break;
12947c478bd9Sstevel@tonic-gate 	default:
12957c478bd9Sstevel@tonic-gate 		cp = "???";
12967c478bd9Sstevel@tonic-gate 		break;
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, cp);
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate  * compare_cond - compare condition field of two ap_id's
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate static int
13057c478bd9Sstevel@tonic-gate compare_cond(
13067c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
13077c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
13087c478bd9Sstevel@tonic-gate 	match_type_t match_type)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	switch (match_type) {
13127c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
13137c478bd9Sstevel@tonic-gate 		return (0);
13147c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
13157c478bd9Sstevel@tonic-gate 	default:
13167c478bd9Sstevel@tonic-gate 		return (p1->ap_cond - p2->ap_cond);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate }
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate /*
13217c478bd9Sstevel@tonic-gate  * print_cond - print attachment point condition
13227c478bd9Sstevel@tonic-gate  */
13237c478bd9Sstevel@tonic-gate static void
13247c478bd9Sstevel@tonic-gate print_cond(
13257c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
13267c478bd9Sstevel@tonic-gate 	int width,
13277c478bd9Sstevel@tonic-gate 	char *lp)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	char *cp;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	switch (p->ap_cond) {
13327c478bd9Sstevel@tonic-gate 	case CFGA_COND_UNKNOWN:
13337c478bd9Sstevel@tonic-gate 		cp = "unknown";
13347c478bd9Sstevel@tonic-gate 		break;
13357c478bd9Sstevel@tonic-gate 	case CFGA_COND_UNUSABLE:
13367c478bd9Sstevel@tonic-gate 		cp = "unusable";
13377c478bd9Sstevel@tonic-gate 		break;
13387c478bd9Sstevel@tonic-gate 	case CFGA_COND_FAILING:
13397c478bd9Sstevel@tonic-gate 		cp = "failing";
13407c478bd9Sstevel@tonic-gate 		break;
13417c478bd9Sstevel@tonic-gate 	case CFGA_COND_FAILED:
13427c478bd9Sstevel@tonic-gate 		cp = "failed";
13437c478bd9Sstevel@tonic-gate 		break;
13447c478bd9Sstevel@tonic-gate 	case CFGA_COND_OK:
13457c478bd9Sstevel@tonic-gate 		cp = "ok";
13467c478bd9Sstevel@tonic-gate 		break;
13477c478bd9Sstevel@tonic-gate 	default:
13487c478bd9Sstevel@tonic-gate 		cp = "???";
13497c478bd9Sstevel@tonic-gate 		break;
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, cp);
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate /*
13557c478bd9Sstevel@tonic-gate  * compare_time - compare time field of two ap_id's
13567c478bd9Sstevel@tonic-gate  */
13577c478bd9Sstevel@tonic-gate static int
13587c478bd9Sstevel@tonic-gate compare_time(
13597c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
13607c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
13617c478bd9Sstevel@tonic-gate 	match_type_t match_type)
13627c478bd9Sstevel@tonic-gate {
13637c478bd9Sstevel@tonic-gate 	switch (match_type) {
13647c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
13657c478bd9Sstevel@tonic-gate 		return (0);
13667c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
13677c478bd9Sstevel@tonic-gate 	default:
13687c478bd9Sstevel@tonic-gate 		return (p1->ap_status_time - p2->ap_status_time);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate /*
13747c478bd9Sstevel@tonic-gate  * print_time - print time from cfga_list_data.
13757c478bd9Sstevel@tonic-gate  * Time print based on ls(1).
13767c478bd9Sstevel@tonic-gate  */
13777c478bd9Sstevel@tonic-gate static void
13787c478bd9Sstevel@tonic-gate print_time(
13797c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
13807c478bd9Sstevel@tonic-gate 	int width,
13817c478bd9Sstevel@tonic-gate 	char *lp)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	static time_t   year, now;
13847c478bd9Sstevel@tonic-gate 	time_t stime;
13857c478bd9Sstevel@tonic-gate 	char	time_buf[50];	/* array to hold day and time */
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	if (year == 0) {
13887c478bd9Sstevel@tonic-gate 		now = time((long *)NULL);
13897c478bd9Sstevel@tonic-gate 		year = now - 6L*30L*24L*60L*60L; /* 6 months ago */
13907c478bd9Sstevel@tonic-gate 		now = now + 60;
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 	stime = p->ap_status_time;
13937c478bd9Sstevel@tonic-gate 	if (stime == (time_t)-1) {
13947c478bd9Sstevel@tonic-gate 		(void) sprintf(lp, "%-*s", width, gettext("unavailable"));
13957c478bd9Sstevel@tonic-gate 		return;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	if ((stime < year) || (stime > now)) {
13997c478bd9Sstevel@tonic-gate 		(void) strftime(time_buf, sizeof (time_buf),
14007c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, FORMAT1, LC_TIME), localtime(&stime));
14017c478bd9Sstevel@tonic-gate 	} else {
14027c478bd9Sstevel@tonic-gate 		(void) strftime(time_buf, sizeof (time_buf),
14037c478bd9Sstevel@tonic-gate 		    dcgettext(NULL, FORMAT2, LC_TIME), localtime(&stime));
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, time_buf);
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate /*
14097c478bd9Sstevel@tonic-gate  * print_time_p - print time from cfga_list_data.
14107c478bd9Sstevel@tonic-gate  */
14117c478bd9Sstevel@tonic-gate static void
14127c478bd9Sstevel@tonic-gate print_time_p(
14137c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
14147c478bd9Sstevel@tonic-gate 	int width,
14157c478bd9Sstevel@tonic-gate 	char *lp)
14167c478bd9Sstevel@tonic-gate {
14177c478bd9Sstevel@tonic-gate 	struct tm *tp;
14187c478bd9Sstevel@tonic-gate 	char tstr[TIME_P_WIDTH+1];
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	tp = localtime(&p->ap_status_time);
14217c478bd9Sstevel@tonic-gate 	(void) sprintf(tstr, "%04d%02d%02d%02d%02d%02d", tp->tm_year + 1900,
14227c478bd9Sstevel@tonic-gate 	    tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
14237c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, tstr);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate /*
14277c478bd9Sstevel@tonic-gate  * compare_info - compare info from two cfga_list_data structs
14287c478bd9Sstevel@tonic-gate  */
14297c478bd9Sstevel@tonic-gate static int
14307c478bd9Sstevel@tonic-gate compare_info(
14317c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
14327c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
14337c478bd9Sstevel@tonic-gate 	match_type_t match_type)
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	switch (match_type) {
14367c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
14377c478bd9Sstevel@tonic-gate 		return (0);
14387c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
14397c478bd9Sstevel@tonic-gate 	default:
14407c478bd9Sstevel@tonic-gate 		return (strncmp(p1->ap_info, p2->ap_info,
14417c478bd9Sstevel@tonic-gate 		    sizeof (p2->ap_info)));
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate /*
14467c478bd9Sstevel@tonic-gate  * print_info - print info from cfga_list_data struct
14477c478bd9Sstevel@tonic-gate  */
14487c478bd9Sstevel@tonic-gate static void
14497c478bd9Sstevel@tonic-gate print_info(
14507c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
14517c478bd9Sstevel@tonic-gate 	int width,
14527c478bd9Sstevel@tonic-gate 	char *lp)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_info), p->ap_info);
14557c478bd9Sstevel@tonic-gate }
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate /*
14587c478bd9Sstevel@tonic-gate  * compare_type - compare type from two cfga_list_data structs
14597c478bd9Sstevel@tonic-gate  *
14607c478bd9Sstevel@tonic-gate  * For partial matches, argument order is significant. The filtering criterion
14617c478bd9Sstevel@tonic-gate  * should be the first argument.
14627c478bd9Sstevel@tonic-gate  */
14637c478bd9Sstevel@tonic-gate static int
14647c478bd9Sstevel@tonic-gate compare_type(
14657c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
14667c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
14677c478bd9Sstevel@tonic-gate 	match_type_t match_type)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	switch (match_type) {
14707c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
14717c478bd9Sstevel@tonic-gate 		return (0);
14727c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_PARTIAL:
14737c478bd9Sstevel@tonic-gate 		return (strncmp(p1->ap_type, p2->ap_type, strlen(p1->ap_type)));
14747c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_EXACT:
14757c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
14767c478bd9Sstevel@tonic-gate 	default:
14777c478bd9Sstevel@tonic-gate 		return (strncmp(p1->ap_type, p2->ap_type,
14787c478bd9Sstevel@tonic-gate 		    sizeof (p2->ap_type)));
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate  * print_type - print type from cfga_list_data struct
14847c478bd9Sstevel@tonic-gate  */
14857c478bd9Sstevel@tonic-gate static void
14867c478bd9Sstevel@tonic-gate print_type(
14877c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
14887c478bd9Sstevel@tonic-gate 	int width,
14897c478bd9Sstevel@tonic-gate 	char *lp)
14907c478bd9Sstevel@tonic-gate {
14917c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_type), p->ap_type);
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate  * compare_class - compare class from two cfga_list_data structs
14977c478bd9Sstevel@tonic-gate  *
14987c478bd9Sstevel@tonic-gate  * For partial matches, argument order is significant. The filtering criterion
14997c478bd9Sstevel@tonic-gate  * should be the first argument.
15007c478bd9Sstevel@tonic-gate  */
15017c478bd9Sstevel@tonic-gate static int
15027c478bd9Sstevel@tonic-gate compare_class(
15037c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
15047c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
15057c478bd9Sstevel@tonic-gate 	match_type_t match_type)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	switch (match_type) {
15097c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_NOFILTER:
15107c478bd9Sstevel@tonic-gate 		return (0);
15117c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_PARTIAL:
15127c478bd9Sstevel@tonic-gate 		return (strncmp(p1->ap_class, p2->ap_class,
15137c478bd9Sstevel@tonic-gate 		    strlen(p1->ap_class)));
15147c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_EXACT:
15157c478bd9Sstevel@tonic-gate 	case CFGA_MATCH_ORDER:
15167c478bd9Sstevel@tonic-gate 	default:
15177c478bd9Sstevel@tonic-gate 		return (strncmp(p1->ap_class, p2->ap_class,
15187c478bd9Sstevel@tonic-gate 		    sizeof (p2->ap_class)));
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate }
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate /*
15237c478bd9Sstevel@tonic-gate  * print_class - print class from cfga_list_data struct
15247c478bd9Sstevel@tonic-gate  */
15257c478bd9Sstevel@tonic-gate static void
15267c478bd9Sstevel@tonic-gate print_class(
15277c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
15287c478bd9Sstevel@tonic-gate 	int width,
15297c478bd9Sstevel@tonic-gate 	char *lp)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_class), p->ap_class);
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate /*
15347c478bd9Sstevel@tonic-gate  * print_busy - print busy from cfga_list_data struct
15357c478bd9Sstevel@tonic-gate  */
15367c478bd9Sstevel@tonic-gate /* ARGSUSED */
15377c478bd9Sstevel@tonic-gate static void
15387c478bd9Sstevel@tonic-gate print_busy(
15397c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
15407c478bd9Sstevel@tonic-gate 	int width,
15417c478bd9Sstevel@tonic-gate 	char *lp)
15427c478bd9Sstevel@tonic-gate {
15437c478bd9Sstevel@tonic-gate 	if (p->ap_busy)
15447c478bd9Sstevel@tonic-gate 		(void) sprintf(lp, "%-*.*s", width, width, "y");
15457c478bd9Sstevel@tonic-gate 	else
15467c478bd9Sstevel@tonic-gate 		(void) sprintf(lp, "%-*.*s", width, width, "n");
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate  * print_phys_id - print physical ap_id
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate static void
15537c478bd9Sstevel@tonic-gate print_phys_id(
15547c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
15557c478bd9Sstevel@tonic-gate 	int width,
15567c478bd9Sstevel@tonic-gate 	char *lp)
15577c478bd9Sstevel@tonic-gate {
15587c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_phys_id),
15597c478bd9Sstevel@tonic-gate 	    p->ap_phys_id);
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate /*
15647c478bd9Sstevel@tonic-gate  * find_field - find the named field
15657c478bd9Sstevel@tonic-gate  */
15667c478bd9Sstevel@tonic-gate static struct field_info *
15677c478bd9Sstevel@tonic-gate find_field(char *fname)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	struct field_info *fldp;
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++)
15727c478bd9Sstevel@tonic-gate 		if (strcmp(fname, fldp->name) == 0)
15737c478bd9Sstevel@tonic-gate 			return (fldp);
15747c478bd9Sstevel@tonic-gate 	return (NULL);
15757c478bd9Sstevel@tonic-gate }
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate /*
15787c478bd9Sstevel@tonic-gate  * usage_field - print field usage
15797c478bd9Sstevel@tonic-gate  */
15807c478bd9Sstevel@tonic-gate static void
15817c478bd9Sstevel@tonic-gate usage_field()
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	struct field_info *fldp = NULL;
15847c478bd9Sstevel@tonic-gate 	const char *sep;
15857c478bd9Sstevel@tonic-gate 	static char field_list[] = "%s: print or sort fields must be one of:";
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(field_list), cmdname);
15887c478bd9Sstevel@tonic-gate 	sep = "";
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++) {
15917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s %s", sep, fldp->name);
15927c478bd9Sstevel@tonic-gate 		sep = ",";
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate /*
15987c478bd9Sstevel@tonic-gate  * compare_null - null comparison routine
15997c478bd9Sstevel@tonic-gate  */
16007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16017c478bd9Sstevel@tonic-gate static int
16027c478bd9Sstevel@tonic-gate compare_null(
16037c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p1,
16047c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p2,
16057c478bd9Sstevel@tonic-gate 	match_type_t match_type)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	return (0);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate /*
16117c478bd9Sstevel@tonic-gate  * print_null - print out a field of spaces
16127c478bd9Sstevel@tonic-gate  */
16137c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16147c478bd9Sstevel@tonic-gate static void
16157c478bd9Sstevel@tonic-gate print_null(
16167c478bd9Sstevel@tonic-gate 	cfga_list_data_t *p,
16177c478bd9Sstevel@tonic-gate 	int width,
16187c478bd9Sstevel@tonic-gate 	char *lp)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	(void) sprintf(lp, "%-*s", width, "");
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate /*
16247c478bd9Sstevel@tonic-gate  * do_config_list - directs the output of the listing functions
16257c478bd9Sstevel@tonic-gate  */
16267c478bd9Sstevel@tonic-gate static int
16277c478bd9Sstevel@tonic-gate do_config_list(
16287c478bd9Sstevel@tonic-gate 	int l_argc,
16297c478bd9Sstevel@tonic-gate 	char *l_argv[],
16307c478bd9Sstevel@tonic-gate 	cfga_list_data_t *statlist,
16317c478bd9Sstevel@tonic-gate 	int nlist,
16327c478bd9Sstevel@tonic-gate 	char *sortp,
16337c478bd9Sstevel@tonic-gate 	char *colsp,
16347c478bd9Sstevel@tonic-gate 	char *cols2p,
16357c478bd9Sstevel@tonic-gate 	int noheadings,
16367c478bd9Sstevel@tonic-gate 	char *delimp,
16377c478bd9Sstevel@tonic-gate 	post_filter_t *post_filtp,
16387c478bd9Sstevel@tonic-gate 	int dyn_exp)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	int nprcols = 0, ncols2 = 0;
16417c478bd9Sstevel@tonic-gate 	struct print_col *prnt_list = NULL;
16427c478bd9Sstevel@tonic-gate 	int napids_to_list = 0;
16437c478bd9Sstevel@tonic-gate 	FILE *fp = NULL;
16447c478bd9Sstevel@tonic-gate 	int f_err;
16457c478bd9Sstevel@tonic-gate 	cfga_list_data_t **sel_boards = NULL;
16467c478bd9Sstevel@tonic-gate 	int nsel = 0;
16477c478bd9Sstevel@tonic-gate 	int i, j;
16487c478bd9Sstevel@tonic-gate 	cfga_err_t ret;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	ap_arg_t *arg_array = NULL;
16517c478bd9Sstevel@tonic-gate 	ap_out_t *out_array = NULL;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	sort_list = NULL;
16557c478bd9Sstevel@tonic-gate 	f_err = 0;
16567c478bd9Sstevel@tonic-gate 	fp = stdout;
16577c478bd9Sstevel@tonic-gate 	nsort_list = count_fields(sortp, FDELIM);
16587c478bd9Sstevel@tonic-gate 	if (nsort_list != 0) {
16597c478bd9Sstevel@tonic-gate 		sort_list = config_calloc_check(nsort_list,
16607c478bd9Sstevel@tonic-gate 		    sizeof (*sort_list));
16617c478bd9Sstevel@tonic-gate 		if (sort_list == NULL) {
16627c478bd9Sstevel@tonic-gate 			ret = CFGA_LIB_ERROR;
16637c478bd9Sstevel@tonic-gate 			goto out;
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 		f_err |= process_sort_fields(nsort_list, sort_list, sortp);
16667c478bd9Sstevel@tonic-gate 	} else
16677c478bd9Sstevel@tonic-gate 		sort_list = NULL;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	nprcols = count_fields(colsp, FDELIM);
16707c478bd9Sstevel@tonic-gate 	if ((ncols2 = count_fields(cols2p, FDELIM)) > nprcols)
16717c478bd9Sstevel@tonic-gate 		nprcols = ncols2;
16727c478bd9Sstevel@tonic-gate 	if (nprcols != 0) {
16737c478bd9Sstevel@tonic-gate 		prnt_list = config_calloc_check(nprcols, sizeof (*prnt_list));
16747c478bd9Sstevel@tonic-gate 		if (prnt_list == NULL) {
16757c478bd9Sstevel@tonic-gate 			ret = CFGA_LIB_ERROR;
16767c478bd9Sstevel@tonic-gate 			goto out;
16777c478bd9Sstevel@tonic-gate 		}
16787c478bd9Sstevel@tonic-gate 		f_err |= process_fields(nprcols, prnt_list, 0, colsp);
16797c478bd9Sstevel@tonic-gate 		if (ncols2 != 0)
16807c478bd9Sstevel@tonic-gate 			f_err |= process_fields(nprcols, prnt_list, 1, cols2p);
16817c478bd9Sstevel@tonic-gate 	} else
16827c478bd9Sstevel@tonic-gate 		prnt_list = NULL;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if (f_err) {
16857c478bd9Sstevel@tonic-gate 		usage_field();
16867c478bd9Sstevel@tonic-gate 		ret = CFGA_ERROR;
16877c478bd9Sstevel@tonic-gate 		goto out;
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	/* Create an array of all user args (if any) */
16917c478bd9Sstevel@tonic-gate 	if (l_argc != 0) {
16927c478bd9Sstevel@tonic-gate 		int i, j;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 		napids_to_list = 0;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 		for (i = 0; i < l_argc; i++) {
16977c478bd9Sstevel@tonic-gate 			napids_to_list += count_fields(l_argv[i], ARG_DELIM);
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		arg_array = config_calloc_check(napids_to_list,
17017c478bd9Sstevel@tonic-gate 		    sizeof (*arg_array));
17027c478bd9Sstevel@tonic-gate 		if (arg_array == NULL) {
17037c478bd9Sstevel@tonic-gate 			ret = CFGA_LIB_ERROR;
17047c478bd9Sstevel@tonic-gate 			goto out;
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; i < l_argc; i++) {
17087c478bd9Sstevel@tonic-gate 			int n;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 			n = count_fields(l_argv[i], ARG_DELIM);
17117c478bd9Sstevel@tonic-gate 			if (n == 0) {
17127c478bd9Sstevel@tonic-gate 				continue;
17137c478bd9Sstevel@tonic-gate 			} else if (n == 1) {
17147c478bd9Sstevel@tonic-gate 				arg_array[j].arg = l_argv[i];
17157c478bd9Sstevel@tonic-gate 				arg_array[j].resp = 0;
17167c478bd9Sstevel@tonic-gate 				j++;
17177c478bd9Sstevel@tonic-gate 			} else {
17187c478bd9Sstevel@tonic-gate 				char *cp, *ncp;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 				cp = l_argv[i];
17217c478bd9Sstevel@tonic-gate 				for (;;) {
17227c478bd9Sstevel@tonic-gate 					arg_array[j].arg = cp;
17237c478bd9Sstevel@tonic-gate 					arg_array[j].resp = 0;
17247c478bd9Sstevel@tonic-gate 					j++;
17257c478bd9Sstevel@tonic-gate 					ncp = strchr(cp, ARG_DELIM);
17267c478bd9Sstevel@tonic-gate 					if (ncp == NULL)
17277c478bd9Sstevel@tonic-gate 						break;
17287c478bd9Sstevel@tonic-gate 					*ncp = '\0';
17297c478bd9Sstevel@tonic-gate 					cp = ncp + 1;
17307c478bd9Sstevel@tonic-gate 				}
17317c478bd9Sstevel@tonic-gate 			}
17327c478bd9Sstevel@tonic-gate 		}
17337c478bd9Sstevel@tonic-gate 		assert(j == napids_to_list);
17347c478bd9Sstevel@tonic-gate 	} else {
17357c478bd9Sstevel@tonic-gate 		napids_to_list = 0;
17367c478bd9Sstevel@tonic-gate 		arg_array = NULL;
17377c478bd9Sstevel@tonic-gate 	}
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	assert(nlist != 0);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	out_array = config_calloc_check(nlist, sizeof (*out_array));
17427c478bd9Sstevel@tonic-gate 	if (out_array == NULL) {
17437c478bd9Sstevel@tonic-gate 		ret = CFGA_LIB_ERROR;
17447c478bd9Sstevel@tonic-gate 		goto out;
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	/* create a list of output stat data */
17497c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlist; i++) {
17507c478bd9Sstevel@tonic-gate 		out_array[i].ldatap = &statlist[i];
17517c478bd9Sstevel@tonic-gate 		out_array[i].req = 0;
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	/*
17557c478bd9Sstevel@tonic-gate 	 * Mark all user input which got atleast 1 stat data in response
17567c478bd9Sstevel@tonic-gate 	 */
17577c478bd9Sstevel@tonic-gate 	for (i = 0; i < napids_to_list; i++) {
17587c478bd9Sstevel@tonic-gate 		arg_got_resp(&arg_array[i], out_array, nlist, dyn_exp);
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	/*
17627c478bd9Sstevel@tonic-gate 	 * Process output data
17637c478bd9Sstevel@tonic-gate 	 */
17647c478bd9Sstevel@tonic-gate 	nsel = 0;
17657c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlist; i++) {
17667c478bd9Sstevel@tonic-gate 		/*
17677c478bd9Sstevel@tonic-gate 		 * Mark all the stats which were actually requested by user
17687c478bd9Sstevel@tonic-gate 		 */
17697c478bd9Sstevel@tonic-gate 		out_was_req(&out_array[i], arg_array, napids_to_list, 0);
17707c478bd9Sstevel@tonic-gate 		if (out_array[i].req == 0 && dyn_exp) {
17717c478bd9Sstevel@tonic-gate 			/*
17727c478bd9Sstevel@tonic-gate 			 * Try again without the dynamic component for the
17737c478bd9Sstevel@tonic-gate 			 * if dynamic expansion was requested.
17747c478bd9Sstevel@tonic-gate 			 */
17757c478bd9Sstevel@tonic-gate 			out_was_req(&out_array[i], arg_array,
17767c478bd9Sstevel@tonic-gate 			    napids_to_list, 1);
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 		/*
17807c478bd9Sstevel@tonic-gate 		 * post filter data which was actually requested
17817c478bd9Sstevel@tonic-gate 		 */
17827c478bd9Sstevel@tonic-gate 		if (out_array[i].req == 1) {
17837c478bd9Sstevel@tonic-gate 			do_post_filter(&out_array[i], post_filtp, &nsel);
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	sel_boards = config_calloc_check(nsel, sizeof (*sel_boards));
17887c478bd9Sstevel@tonic-gate 	if (sel_boards == NULL) {
17897c478bd9Sstevel@tonic-gate 		ret = CFGA_LIB_ERROR;
17907c478bd9Sstevel@tonic-gate 		goto out;
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < nlist; i++) {
17947c478bd9Sstevel@tonic-gate 		if (out_array[i].req == 1) {
17957c478bd9Sstevel@tonic-gate 			sel_boards[j] = out_array[i].ldatap;
17967c478bd9Sstevel@tonic-gate 			j++;
17977c478bd9Sstevel@tonic-gate 		}
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	assert(j == nsel);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/*
18037c478bd9Sstevel@tonic-gate 	 * Print headings even if no list entries - Bug or feature ?
18047c478bd9Sstevel@tonic-gate 	 */
18057c478bd9Sstevel@tonic-gate 	if (!noheadings && prnt_list != NULL) {
18067c478bd9Sstevel@tonic-gate 		if ((ret = print_fields(nprcols, prnt_list, 1, 0,
18077c478bd9Sstevel@tonic-gate 		    delimp, NULL, fp)) != CFGA_OK) {
18087c478bd9Sstevel@tonic-gate 			goto out;
18097c478bd9Sstevel@tonic-gate 		}
18107c478bd9Sstevel@tonic-gate 		if (ncols2 != 0) {
18117c478bd9Sstevel@tonic-gate 			if ((ret = print_fields(nprcols, prnt_list, 1,
18127c478bd9Sstevel@tonic-gate 			    1, delimp, NULL, fp)) != CFGA_OK) {
18137c478bd9Sstevel@tonic-gate 				goto out;
18147c478bd9Sstevel@tonic-gate 			}
18157c478bd9Sstevel@tonic-gate 		}
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	if (nsel != 0) {
18197c478bd9Sstevel@tonic-gate 		if (sort_list != NULL && nsel > 1) {
18207c478bd9Sstevel@tonic-gate 			qsort(sel_boards, nsel, sizeof (sel_boards[0]),
18217c478bd9Sstevel@tonic-gate 			    ldata_compare);
18227c478bd9Sstevel@tonic-gate 		}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 		if (prnt_list != NULL) {
18257c478bd9Sstevel@tonic-gate 			for (i = 0; i < nsel; i++) {
18267c478bd9Sstevel@tonic-gate 				if ((ret = print_fields(nprcols,
18277c478bd9Sstevel@tonic-gate 				    prnt_list, 0, 0, delimp, sel_boards[i], fp))
18287c478bd9Sstevel@tonic-gate 				    != CFGA_OK)
18297c478bd9Sstevel@tonic-gate 					goto out;
18307c478bd9Sstevel@tonic-gate 				if (ncols2 != 0) {
18317c478bd9Sstevel@tonic-gate 					if ((ret = print_fields(
18327c478bd9Sstevel@tonic-gate 					    nprcols, prnt_list, 0, 1, delimp,
18337c478bd9Sstevel@tonic-gate 					    sel_boards[i], fp)) != CFGA_OK)
18347c478bd9Sstevel@tonic-gate 						goto out;
18357c478bd9Sstevel@tonic-gate 				}
18367c478bd9Sstevel@tonic-gate 			}
18377c478bd9Sstevel@tonic-gate 		}
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 	/*
18407c478bd9Sstevel@tonic-gate 	 * Go thru the argument list and notify user about args
18417c478bd9Sstevel@tonic-gate 	 * which did not have a match
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	report_no_response(arg_array, napids_to_list);
18447c478bd9Sstevel@tonic-gate 	ret = CFGA_OK;
18457c478bd9Sstevel@tonic-gate 	/*FALLTHRU*/
18467c478bd9Sstevel@tonic-gate out:
18477c478bd9Sstevel@tonic-gate 	S_FREE(sel_boards);
18487c478bd9Sstevel@tonic-gate 	S_FREE(arg_array);
18497c478bd9Sstevel@tonic-gate 	S_FREE(out_array);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	S_FREE(sort_list);
18527c478bd9Sstevel@tonic-gate 	S_FREE(prnt_list);
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	return (ret);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate /*
18597c478bd9Sstevel@tonic-gate  * Mark all user inputs which got a response
18607c478bd9Sstevel@tonic-gate  */
18617c478bd9Sstevel@tonic-gate static void
18627c478bd9Sstevel@tonic-gate arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts, int dyn_exp)
18637c478bd9Sstevel@tonic-gate {
18647c478bd9Sstevel@tonic-gate 	int i;
18657c478bd9Sstevel@tonic-gate 	cfga_ap_types_t type;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	if (nouts == 0) {
18697c478bd9Sstevel@tonic-gate 		return;
18707c478bd9Sstevel@tonic-gate 	}
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	type = find_arg_type(inp->arg);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	/*
18757c478bd9Sstevel@tonic-gate 	 * Go through list of output stats and check if argument
18767c478bd9Sstevel@tonic-gate 	 * produced that output
18777c478bd9Sstevel@tonic-gate 	 */
18787c478bd9Sstevel@tonic-gate 	for (i = 0; i < nouts; i++) {
18797c478bd9Sstevel@tonic-gate 		if (type == PHYSICAL_AP_ID) {
18807c478bd9Sstevel@tonic-gate 			if (config_ap_id_cmp(out_array[i].ldatap->ap_phys_id,
18817c478bd9Sstevel@tonic-gate 			    inp->arg) == 0) {
18827c478bd9Sstevel@tonic-gate 				break;
18837c478bd9Sstevel@tonic-gate 			}
18847c478bd9Sstevel@tonic-gate 		} else if (type == LOGICAL_AP_ID) {
18857c478bd9Sstevel@tonic-gate 			if (config_ap_id_cmp(out_array[i].ldatap->ap_log_id,
18867c478bd9Sstevel@tonic-gate 			    inp->arg) == 0) {
18877c478bd9Sstevel@tonic-gate 				break;
18887c478bd9Sstevel@tonic-gate 			}
18897c478bd9Sstevel@tonic-gate 		} else if (type == AP_TYPE) {
18907c478bd9Sstevel@tonic-gate 			/*
18917c478bd9Sstevel@tonic-gate 			 * An AP_TYPE argument cannot generate dynamic
18927c478bd9Sstevel@tonic-gate 			 * attachment point stats unless dynamic expansion was
18937c478bd9Sstevel@tonic-gate 			 * requested by user.
18947c478bd9Sstevel@tonic-gate 			 */
18957c478bd9Sstevel@tonic-gate 			if (!dyn_exp && get_dyn(out_array[i].ldatap->ap_log_id)
18967c478bd9Sstevel@tonic-gate 			    != NULL) {
18977c478bd9Sstevel@tonic-gate 				continue;
18987c478bd9Sstevel@tonic-gate 			}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 			if (strncmp(out_array[i].ldatap->ap_log_id, inp->arg,
19017c478bd9Sstevel@tonic-gate 			    strlen(inp->arg)) == 0) {
19027c478bd9Sstevel@tonic-gate 				break;
19037c478bd9Sstevel@tonic-gate 			}
19047c478bd9Sstevel@tonic-gate 		} else {
19057c478bd9Sstevel@tonic-gate 			return;
19067c478bd9Sstevel@tonic-gate 		}
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	if (i < nouts) {
19107c478bd9Sstevel@tonic-gate 		inp->resp = 1;
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /* Mark all stat data which were requested by user */
19157c478bd9Sstevel@tonic-gate static void
19167c478bd9Sstevel@tonic-gate out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs, int no_dyn)
19177c478bd9Sstevel@tonic-gate {
19187c478bd9Sstevel@tonic-gate 	int i;
19197c478bd9Sstevel@tonic-gate 	cfga_ap_types_t type = UNKNOWN_AP;
19207c478bd9Sstevel@tonic-gate 	char physid[MAXPATHLEN], logid[MAXPATHLEN];
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	/* If no user args, all output is acceptable */
19247c478bd9Sstevel@tonic-gate 	if (nargs == 0) {
19257c478bd9Sstevel@tonic-gate 		outp->req = 1;
19267c478bd9Sstevel@tonic-gate 		return;
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	(void) snprintf(physid, sizeof (physid), "%s",
19317c478bd9Sstevel@tonic-gate 	    outp->ldatap->ap_phys_id);
19327c478bd9Sstevel@tonic-gate 	(void) snprintf(logid, sizeof (logid), "%s", outp->ldatap->ap_log_id);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	/*
19357c478bd9Sstevel@tonic-gate 	 * Do comparison with or without dynamic component as requested by
19367c478bd9Sstevel@tonic-gate 	 * user.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	if (no_dyn) {
19397c478bd9Sstevel@tonic-gate 		/* Remove the dynamic component */
19407c478bd9Sstevel@tonic-gate 		remove_dyn(physid);
19417c478bd9Sstevel@tonic-gate 		remove_dyn(logid);
19427c478bd9Sstevel@tonic-gate 	}
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
19457c478bd9Sstevel@tonic-gate 		type = find_arg_type(in_array[i].arg);
19467c478bd9Sstevel@tonic-gate 		if (type == PHYSICAL_AP_ID) {
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 			if (config_ap_id_cmp(in_array[i].arg, physid) == 0) {
19497c478bd9Sstevel@tonic-gate 				break;
19507c478bd9Sstevel@tonic-gate 			}
19517c478bd9Sstevel@tonic-gate 		} else if (type == LOGICAL_AP_ID) {
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 			if (config_ap_id_cmp(in_array[i].arg, logid) == 0) {
19547c478bd9Sstevel@tonic-gate 				break;
19557c478bd9Sstevel@tonic-gate 			}
19567c478bd9Sstevel@tonic-gate 		} else if (type == AP_TYPE) {
19577c478bd9Sstevel@tonic-gate 			/*
19587c478bd9Sstevel@tonic-gate 			 * Aptypes cannot generate dynamic attachment
19597c478bd9Sstevel@tonic-gate 			 * points unless dynamic expansion is specified.
19607c478bd9Sstevel@tonic-gate 			 * in which case this routine would be called a
19617c478bd9Sstevel@tonic-gate 			 * 2nd time with the no_dyn flag set and there
19627c478bd9Sstevel@tonic-gate 			 * would be no dynamic ap_ids.
19637c478bd9Sstevel@tonic-gate 			 */
19647c478bd9Sstevel@tonic-gate 			if (get_dyn(logid) != NULL) {
19657c478bd9Sstevel@tonic-gate 				continue;
19667c478bd9Sstevel@tonic-gate 			}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 			if (strncmp(in_array[i].arg, logid,
19697c478bd9Sstevel@tonic-gate 			    strlen(in_array[i].arg)) == 0) {
19707c478bd9Sstevel@tonic-gate 				break;
19717c478bd9Sstevel@tonic-gate 			}
19727c478bd9Sstevel@tonic-gate 		} else {
19737c478bd9Sstevel@tonic-gate 			continue;
19747c478bd9Sstevel@tonic-gate 		}
19757c478bd9Sstevel@tonic-gate 	}
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	if (i < nargs) {
19787c478bd9Sstevel@tonic-gate 		/* Ok, this output was requested */
19797c478bd9Sstevel@tonic-gate 		outp->req = 1;
19807c478bd9Sstevel@tonic-gate 	}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate }
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate static void
19857c478bd9Sstevel@tonic-gate do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *nselp)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	int i;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	if (outp->req != 1) {
19907c478bd9Sstevel@tonic-gate 		return;
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	/*
19947c478bd9Sstevel@tonic-gate 	 * For fields without filtering (CFGA_MATCH_NOFILTER),
19957c478bd9Sstevel@tonic-gate 	 * compare always returns 0 (success)
19967c478bd9Sstevel@tonic-gate 	 */
19977c478bd9Sstevel@tonic-gate 	for (i = 0; i < N_FIELDS; i++) {
19987c478bd9Sstevel@tonic-gate 		/*
19997c478bd9Sstevel@tonic-gate 		 * Note: Order is important for partial match (via strncmp).
20007c478bd9Sstevel@tonic-gate 		 * The first argument for compare must be the filter.
20017c478bd9Sstevel@tonic-gate 		 */
20027c478bd9Sstevel@tonic-gate 		if (all_fields[i].compare(&post_filtp->ldata, outp->ldatap,
20037c478bd9Sstevel@tonic-gate 		    post_filtp->match_type_p[i])) {
20047c478bd9Sstevel@tonic-gate 			outp->req = 0;	/* Blocked by filter */
20057c478bd9Sstevel@tonic-gate 			return;
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	/*
20107c478bd9Sstevel@tonic-gate 	 * Passed through filter
20117c478bd9Sstevel@tonic-gate 	 */
20127c478bd9Sstevel@tonic-gate 	(*nselp)++;
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate static void
20167c478bd9Sstevel@tonic-gate report_no_response(ap_arg_t *arg_array, int nargs)
20177c478bd9Sstevel@tonic-gate {
20187c478bd9Sstevel@tonic-gate 	int i;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	if (nargs == 0) {
20217c478bd9Sstevel@tonic-gate 		return;
20227c478bd9Sstevel@tonic-gate 	}
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	/*
20267c478bd9Sstevel@tonic-gate 	 * nop if no user arguments
20277c478bd9Sstevel@tonic-gate 	 */
20287c478bd9Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
20297c478bd9Sstevel@tonic-gate 		if (arg_array[i].resp == 0) {
20307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
20317c478bd9Sstevel@tonic-gate 			    gettext("%s: No matching library found\n"),
20327c478bd9Sstevel@tonic-gate 			    arg_array[i].arg);
20337c478bd9Sstevel@tonic-gate 		}
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate /*
20387c478bd9Sstevel@tonic-gate  * ldata_compare - compare two attachment point list data structures.
20397c478bd9Sstevel@tonic-gate  */
20407c478bd9Sstevel@tonic-gate static int
20417c478bd9Sstevel@tonic-gate ldata_compare(
20427c478bd9Sstevel@tonic-gate 	const void *vb1,
20437c478bd9Sstevel@tonic-gate 	const void *vb2)
20447c478bd9Sstevel@tonic-gate {
20457c478bd9Sstevel@tonic-gate 	int i;
20467c478bd9Sstevel@tonic-gate 	int res = -1;
20477c478bd9Sstevel@tonic-gate 	cfga_list_data_t *b1, *b2;
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	b1 = *(cfga_list_data_t **)vb1;
20517c478bd9Sstevel@tonic-gate 	b2 = *(cfga_list_data_t **)vb2;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsort_list; i++) {
20547c478bd9Sstevel@tonic-gate 		res = (*(sort_list[i].fld->compare))(b1, b2, CFGA_MATCH_ORDER);
20557c478bd9Sstevel@tonic-gate 		if (res != 0) {
20567c478bd9Sstevel@tonic-gate 			if (sort_list[i].reverse)
20577c478bd9Sstevel@tonic-gate 				res = -res;
20587c478bd9Sstevel@tonic-gate 			break;
20597c478bd9Sstevel@tonic-gate 		}
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	return (res);
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate /*
20667c478bd9Sstevel@tonic-gate  * count_fields - Count the number of fields, using supplied delimiter.
20677c478bd9Sstevel@tonic-gate  */
20687c478bd9Sstevel@tonic-gate static int
20697c478bd9Sstevel@tonic-gate count_fields(char *fspec, char delim)
20707c478bd9Sstevel@tonic-gate {
20717c478bd9Sstevel@tonic-gate 	char *cp = NULL;
20727c478bd9Sstevel@tonic-gate 	int n;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if (fspec == 0 || *fspec == '\0')
20757c478bd9Sstevel@tonic-gate 		return (0);
20767c478bd9Sstevel@tonic-gate 	n = 1;
20777c478bd9Sstevel@tonic-gate 	for (cp = fspec; *cp != '\0'; cp++)
20787c478bd9Sstevel@tonic-gate 		if (*cp == delim)
20797c478bd9Sstevel@tonic-gate 			n++;
20807c478bd9Sstevel@tonic-gate 	return (n);
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate /*
20847c478bd9Sstevel@tonic-gate  * get_field
20857c478bd9Sstevel@tonic-gate  * This function is not a re-implementation of strtok().
20867c478bd9Sstevel@tonic-gate  * There can be null fields - strtok() eats spans of delimiters.
20877c478bd9Sstevel@tonic-gate  */
20887c478bd9Sstevel@tonic-gate static char *
20897c478bd9Sstevel@tonic-gate get_field(char **fspp)
20907c478bd9Sstevel@tonic-gate {
20917c478bd9Sstevel@tonic-gate 	char *cp = NULL, *fld;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	fld = *fspp;
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	if (fld != NULL && *fld == '\0')
20967c478bd9Sstevel@tonic-gate 		fld = NULL;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	if (fld != NULL) {
20997c478bd9Sstevel@tonic-gate 		cp = strchr(*fspp, FDELIM);
21007c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
21017c478bd9Sstevel@tonic-gate 			*fspp = NULL;
21027c478bd9Sstevel@tonic-gate 		} else {
21037c478bd9Sstevel@tonic-gate 			*cp = '\0';
21047c478bd9Sstevel@tonic-gate 			*fspp = cp + 1;
21057c478bd9Sstevel@tonic-gate 			if (*fld == '\0')
21067c478bd9Sstevel@tonic-gate 				fld = NULL;
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	return (fld);
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate /*
21137c478bd9Sstevel@tonic-gate  * process_fields -
21147c478bd9Sstevel@tonic-gate  */
21157c478bd9Sstevel@tonic-gate static int
21167c478bd9Sstevel@tonic-gate process_fields(
21177c478bd9Sstevel@tonic-gate 	int ncol,
21187c478bd9Sstevel@tonic-gate 	struct print_col *list,
21197c478bd9Sstevel@tonic-gate 	int line2,
21207c478bd9Sstevel@tonic-gate 	char *fmt)
21217c478bd9Sstevel@tonic-gate {
21227c478bd9Sstevel@tonic-gate 	struct print_col *pp = NULL;
21237c478bd9Sstevel@tonic-gate 	struct field_info *fldp = NULL;
21247c478bd9Sstevel@tonic-gate 	char *fmtx;
21257c478bd9Sstevel@tonic-gate 	char *fldn;
21267c478bd9Sstevel@tonic-gate 	int err;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	err = 0;
21297c478bd9Sstevel@tonic-gate 	fmtx = fmt;
21307c478bd9Sstevel@tonic-gate 	for (pp = list; pp < &list[ncol]; pp++) {
21317c478bd9Sstevel@tonic-gate 		fldn = get_field(&fmtx);
21327c478bd9Sstevel@tonic-gate 		fldp = &null_field;
21337c478bd9Sstevel@tonic-gate 		if (fldn != NULL) {
21347c478bd9Sstevel@tonic-gate 			struct field_info *tfldp;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 			tfldp = find_field(fldn);
21377c478bd9Sstevel@tonic-gate 			if (tfldp != NULL) {
21387c478bd9Sstevel@tonic-gate 				fldp = tfldp;
21397c478bd9Sstevel@tonic-gate 			} else {
21407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(unk_field),
21417c478bd9Sstevel@tonic-gate 				    cmdname, fldn);
21427c478bd9Sstevel@tonic-gate 				err = 1;
21437c478bd9Sstevel@tonic-gate 			}
21447c478bd9Sstevel@tonic-gate 		}
21457c478bd9Sstevel@tonic-gate 		if (line2) {
21467c478bd9Sstevel@tonic-gate 			pp->line2 = fldp;
21477c478bd9Sstevel@tonic-gate 			if (fldp->width > pp->width)
21487c478bd9Sstevel@tonic-gate 				pp->width = fldp->width;
21497c478bd9Sstevel@tonic-gate 		} else {
21507c478bd9Sstevel@tonic-gate 			pp->line1 = fldp;
21517c478bd9Sstevel@tonic-gate 			pp->width = fldp->width;
21527c478bd9Sstevel@tonic-gate 		}
21537c478bd9Sstevel@tonic-gate 	}
21547c478bd9Sstevel@tonic-gate 	return (err);
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate /*
21587c478bd9Sstevel@tonic-gate  * process_sort_fields -
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate static int
21617c478bd9Sstevel@tonic-gate process_sort_fields(
21627c478bd9Sstevel@tonic-gate 	int nsort,
21637c478bd9Sstevel@tonic-gate 	struct sort_el *list,
21647c478bd9Sstevel@tonic-gate 	char *fmt)
21657c478bd9Sstevel@tonic-gate {
21667c478bd9Sstevel@tonic-gate 	int i;
21677c478bd9Sstevel@tonic-gate 	int rev;
21687c478bd9Sstevel@tonic-gate 	struct field_info *fldp = NULL;
21697c478bd9Sstevel@tonic-gate 	char *fmtx;
21707c478bd9Sstevel@tonic-gate 	char *fldn;
21717c478bd9Sstevel@tonic-gate 	int err;
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	err = 0;
21747c478bd9Sstevel@tonic-gate 	fmtx = fmt;
21757c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsort; i++) {
21767c478bd9Sstevel@tonic-gate 		fldn = get_field(&fmtx);
21777c478bd9Sstevel@tonic-gate 		fldp = &null_field;
21787c478bd9Sstevel@tonic-gate 		rev = 0;
21797c478bd9Sstevel@tonic-gate 		if (fldn != NULL) {
21807c478bd9Sstevel@tonic-gate 			struct field_info *tfldp = NULL;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 			if (*fldn == '-') {
21837c478bd9Sstevel@tonic-gate 				rev = 1;
21847c478bd9Sstevel@tonic-gate 				fldn++;
21857c478bd9Sstevel@tonic-gate 			}
21867c478bd9Sstevel@tonic-gate 			tfldp = find_field(fldn);
21877c478bd9Sstevel@tonic-gate 			if (tfldp != NULL) {
21887c478bd9Sstevel@tonic-gate 				fldp = tfldp;
21897c478bd9Sstevel@tonic-gate 			} else {
21907c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(unk_field),
21917c478bd9Sstevel@tonic-gate 				    cmdname, fldn);
21927c478bd9Sstevel@tonic-gate 				err = 1;
21937c478bd9Sstevel@tonic-gate 			}
21947c478bd9Sstevel@tonic-gate 		}
21957c478bd9Sstevel@tonic-gate 		list[i].reverse = rev;
21967c478bd9Sstevel@tonic-gate 		list[i].fld = fldp;
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 	return (err);
21997c478bd9Sstevel@tonic-gate }
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate /*
22027c478bd9Sstevel@tonic-gate  * print_fields -
22037c478bd9Sstevel@tonic-gate  */
22047c478bd9Sstevel@tonic-gate static cfga_err_t
22057c478bd9Sstevel@tonic-gate print_fields(
22067c478bd9Sstevel@tonic-gate 	int ncol,
22077c478bd9Sstevel@tonic-gate 	struct print_col *list,
22087c478bd9Sstevel@tonic-gate 	int heading,
22097c478bd9Sstevel@tonic-gate 	int line2,
22107c478bd9Sstevel@tonic-gate 	char *delim,
22117c478bd9Sstevel@tonic-gate 	cfga_list_data_t *bdp,
22127c478bd9Sstevel@tonic-gate 	FILE *fp)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	char *del = NULL;
22157c478bd9Sstevel@tonic-gate 	struct print_col *pp = NULL;
22167c478bd9Sstevel@tonic-gate 	struct field_info *fldp = NULL;
22177c478bd9Sstevel@tonic-gate 	static char *outline, *end;
22187c478bd9Sstevel@tonic-gate 	char *lp;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	if (outline == NULL) {
22217c478bd9Sstevel@tonic-gate 		int out_len, delim_len;
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 		delim_len = strlen(delim);
22247c478bd9Sstevel@tonic-gate 		out_len = 0;
22257c478bd9Sstevel@tonic-gate 		for (pp = list; pp < &list[ncol]; pp++) {
22267c478bd9Sstevel@tonic-gate 			out_len += pp->width;
22277c478bd9Sstevel@tonic-gate 			out_len += delim_len;
22287c478bd9Sstevel@tonic-gate 		}
22297c478bd9Sstevel@tonic-gate 		out_len -= delim_len;
22307c478bd9Sstevel@tonic-gate 		outline = config_calloc_check(out_len + 1, 1);
22317c478bd9Sstevel@tonic-gate 		if (outline == NULL) {
22327c478bd9Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
22337c478bd9Sstevel@tonic-gate 		}
22347c478bd9Sstevel@tonic-gate 		end = &outline[out_len + 1];
22357c478bd9Sstevel@tonic-gate 	}
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	lp = outline;
22387c478bd9Sstevel@tonic-gate 	del = "";
22397c478bd9Sstevel@tonic-gate 	for (pp = list; pp < &list[ncol]; pp++) {
22407c478bd9Sstevel@tonic-gate 		fldp = line2 ? pp->line2 : pp->line1;
22417c478bd9Sstevel@tonic-gate 		(void) snprintf(lp, end - lp, "%s", del);
22427c478bd9Sstevel@tonic-gate 		lp += strlen(lp);
22437c478bd9Sstevel@tonic-gate 		if (heading) {
22447c478bd9Sstevel@tonic-gate 			(void) snprintf(lp, end - lp, "%-*s",
22457c478bd9Sstevel@tonic-gate 			    fldp->width, fldp->heading);
22467c478bd9Sstevel@tonic-gate 		} else {
22477c478bd9Sstevel@tonic-gate 			(*fldp->printfn)(bdp, fldp->width, lp);
22487c478bd9Sstevel@tonic-gate 		}
22497c478bd9Sstevel@tonic-gate 		lp += strlen(lp);
22507c478bd9Sstevel@tonic-gate 		del = delim;
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	/*
22547c478bd9Sstevel@tonic-gate 	 * Trim trailing spaces
22557c478bd9Sstevel@tonic-gate 	 */
22567c478bd9Sstevel@tonic-gate 	while (--lp >= outline && *lp == ' ')
22577c478bd9Sstevel@tonic-gate 		*lp = '\0';
22587c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s\n", outline);
22597c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
22607c478bd9Sstevel@tonic-gate }
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate /*
22637c478bd9Sstevel@tonic-gate  * config_calloc_check - perform allocation, check result and
22647c478bd9Sstevel@tonic-gate  * set error indicator
22657c478bd9Sstevel@tonic-gate  */
22667c478bd9Sstevel@tonic-gate static void *
22677c478bd9Sstevel@tonic-gate config_calloc_check(
22687c478bd9Sstevel@tonic-gate 	size_t nelem,
22697c478bd9Sstevel@tonic-gate 	size_t elsize)
22707c478bd9Sstevel@tonic-gate {
22717c478bd9Sstevel@tonic-gate 	void *p;
22727c478bd9Sstevel@tonic-gate 	static char alloc_fail[] =
22737c478bd9Sstevel@tonic-gate "%s: memory allocation failed (%d*%d bytes)\n";
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	p = calloc(nelem, elsize);
22777c478bd9Sstevel@tonic-gate 	if (p == NULL) {
22787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(alloc_fail), cmdname,
22797c478bd9Sstevel@tonic-gate 		    nelem, elsize);
22807c478bd9Sstevel@tonic-gate 	}
22817c478bd9Sstevel@tonic-gate 	return (p);
22827c478bd9Sstevel@tonic-gate }
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate /*
22857c478bd9Sstevel@tonic-gate  * find_arg_type - determine if an argument is an ap_id or an ap_type.
22867c478bd9Sstevel@tonic-gate  */
22877c478bd9Sstevel@tonic-gate static cfga_ap_types_t
22887c478bd9Sstevel@tonic-gate find_arg_type(const char *ap_id)
22897c478bd9Sstevel@tonic-gate {
22907c478bd9Sstevel@tonic-gate 	struct stat sbuf;
22917c478bd9Sstevel@tonic-gate 	cfga_ap_types_t type;
22927c478bd9Sstevel@tonic-gate 	char *mkr = NULL, *cp;
22937c478bd9Sstevel@tonic-gate 	int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
22947c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
22957c478bd9Sstevel@tonic-gate 	char apbuf[MAXPATHLEN];
22967c478bd9Sstevel@tonic-gate 	size_t len;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	/*
23007c478bd9Sstevel@tonic-gate 	 * sanity checks
23017c478bd9Sstevel@tonic-gate 	 */
23027c478bd9Sstevel@tonic-gate 	if (ap_id == NULL || *ap_id == '\0') {
23037c478bd9Sstevel@tonic-gate 		return (UNKNOWN_AP);
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	/*
23077c478bd9Sstevel@tonic-gate 	 * Mask the dynamic component if any
23087c478bd9Sstevel@tonic-gate 	 */
23097c478bd9Sstevel@tonic-gate 	if ((cp = GET_DYN(ap_id)) != NULL) {
23107c478bd9Sstevel@tonic-gate 		len = cp - ap_id;
23117c478bd9Sstevel@tonic-gate 	} else {
23127c478bd9Sstevel@tonic-gate 		len = strlen(ap_id);
23137c478bd9Sstevel@tonic-gate 	}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	if (len >= sizeof (apbuf)) {
23167c478bd9Sstevel@tonic-gate 		return (UNKNOWN_AP);
23177c478bd9Sstevel@tonic-gate 	}
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	(void) strncpy(apbuf, ap_id, len);
23207c478bd9Sstevel@tonic-gate 	apbuf[len] = '\0';
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	/*
23237c478bd9Sstevel@tonic-gate 	 * If it starts with a slash and is stat-able
23247c478bd9Sstevel@tonic-gate 	 * its a physical.
23257c478bd9Sstevel@tonic-gate 	 */
23267c478bd9Sstevel@tonic-gate 	if (*apbuf == '/' && stat(apbuf, &sbuf) == 0) {
23277c478bd9Sstevel@tonic-gate 		return (PHYSICAL_AP_ID);
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	/*
23317c478bd9Sstevel@tonic-gate 	 * Is this a symlink in CFGA_DEV_DIR ?
23327c478bd9Sstevel@tonic-gate 	 */
23337c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s/%s", CFGA_DEV_DIR, apbuf);
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
23367c478bd9Sstevel@tonic-gate 	    stat(path, &sbuf) == 0) {
23377c478bd9Sstevel@tonic-gate 		return (LOGICAL_AP_ID);
23387c478bd9Sstevel@tonic-gate 	}
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	/*
23417c478bd9Sstevel@tonic-gate 	 * Check for ":" which is always present in an ap_id but not maybe
23427c478bd9Sstevel@tonic-gate 	 * present or absent in an ap_type.
23437c478bd9Sstevel@tonic-gate 	 * We need to check that the characters right before the : are digits
23447c478bd9Sstevel@tonic-gate 	 * since an ap_id is of the form <name><instance>:<specific ap name>
23457c478bd9Sstevel@tonic-gate 	 */
23467c478bd9Sstevel@tonic-gate 	if ((mkr = strchr(apbuf, ':')) == NULL)  {
23477c478bd9Sstevel@tonic-gate 		type = AP_TYPE;
23487c478bd9Sstevel@tonic-gate 	} else {
23497c478bd9Sstevel@tonic-gate 		size_ap = strlen(apbuf);
23507c478bd9Sstevel@tonic-gate 		size_mkr = strlen(mkr);
23517c478bd9Sstevel@tonic-gate 		mkr = apbuf;
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 		digit = 0;
23547c478bd9Sstevel@tonic-gate 		for (i = size_ap - size_mkr - 1;  i > 0; i--) {
23557c478bd9Sstevel@tonic-gate 			if ((int)isdigit(mkr[i])) {
23567c478bd9Sstevel@tonic-gate 				digit++;
23577c478bd9Sstevel@tonic-gate 				break;
23587c478bd9Sstevel@tonic-gate 			}
23597c478bd9Sstevel@tonic-gate 		}
23607c478bd9Sstevel@tonic-gate 		if (digit == 0) {
23617c478bd9Sstevel@tonic-gate 			type = AP_TYPE;
23627c478bd9Sstevel@tonic-gate 		} else {
23637c478bd9Sstevel@tonic-gate 			type = LOGICAL_AP_ID;
23647c478bd9Sstevel@tonic-gate 		}
23657c478bd9Sstevel@tonic-gate 	}
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	return (type);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate static char *
23727c478bd9Sstevel@tonic-gate get_dyn(const char *ap_id)
23737c478bd9Sstevel@tonic-gate {
23747c478bd9Sstevel@tonic-gate 	if (ap_id == NULL) {
23757c478bd9Sstevel@tonic-gate 		return (NULL);
23767c478bd9Sstevel@tonic-gate 	}
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	return (strstr(ap_id, CFGA_DYN_SEP));
23797c478bd9Sstevel@tonic-gate }
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate /*
23827c478bd9Sstevel@tonic-gate  * removes the dynamic component
23837c478bd9Sstevel@tonic-gate  */
23847c478bd9Sstevel@tonic-gate static void
23857c478bd9Sstevel@tonic-gate remove_dyn(char *ap_id)
23867c478bd9Sstevel@tonic-gate {
23877c478bd9Sstevel@tonic-gate 	char *cp;
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	if (ap_id == NULL) {
23907c478bd9Sstevel@tonic-gate 		return;
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	cp = strstr(ap_id, CFGA_DYN_SEP);
23947c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
23957c478bd9Sstevel@tonic-gate 		*cp = '\0';
23967c478bd9Sstevel@tonic-gate 	}
23977c478bd9Sstevel@tonic-gate }
2398*3977d6cdSvikram 
2399*3977d6cdSvikram 
2400*3977d6cdSvikram static char *
2401*3977d6cdSvikram s_strdup(char *str)
2402*3977d6cdSvikram {
2403*3977d6cdSvikram 	char *dup;
2404*3977d6cdSvikram 
2405*3977d6cdSvikram 	/*
2406*3977d6cdSvikram 	 * sometimes NULL strings may be passed in (see DEF_COLS2). This
2407*3977d6cdSvikram 	 * is not an error.
2408*3977d6cdSvikram 	 */
2409*3977d6cdSvikram 	if (str == NULL) {
2410*3977d6cdSvikram 		return (NULL);
2411*3977d6cdSvikram 	}
2412*3977d6cdSvikram 
2413*3977d6cdSvikram 	dup = strdup(str);
2414*3977d6cdSvikram 	if (dup == NULL) {
2415*3977d6cdSvikram 		(void) fprintf(stderr,
2416*3977d6cdSvikram 		    "%s \"%s\"\n", gettext("Cannot copy string"), str);
2417*3977d6cdSvikram 		return (NULL);
2418*3977d6cdSvikram 	}
2419*3977d6cdSvikram 
2420*3977d6cdSvikram 	return (dup);
2421*3977d6cdSvikram }
2422