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