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