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