Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / cfgadm / cfgadm.c
blob7468c1b8ccc731c489f6c33f0ceca2c715ab505c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * This is the main program file for the configuration administration
29 * command as set out in manual page cfgadm(8). It uses the configuration
30 * administration library interface, libcfgadm, as set out in manual
31 * page config_admin(3X).
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <langinfo.h>
39 #include <time.h>
40 #include <assert.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/sunddi.h>
45 #include <sys/openpromio.h>
46 #include <sys/ddi_impldefs.h>
47 #include <sys/systeminfo.h>
48 #include <ctype.h>
49 #include <zone.h>
50 #include <getresponse.h>
52 #include <config_admin.h>
53 #include "cfgadm.h"
55 #define S_FREE(x) (free(x), (x) = NULL)
56 #define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
58 * forward declarations
60 static char *basename(char *);
61 static void cfgadm_error(int, char *);
62 static int confirm_interactive(void *, const char *);
63 static int confirm_no(void *, const char *);
64 static int confirm_yes(void *, const char *);
65 static void usage(void);
66 static void usage_field(void);
67 static int extract_list_suboptions(char *, char **, char **, char **,
68 int *, char **, char **, char **);
69 static int message_output(void *appdata_ptr, const char *message);
70 static void *config_calloc_check(size_t, size_t);
71 static cfga_ap_types_t find_arg_type(const char *);
74 static int compare_ap_id(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
75 static int compare_r_state(cfga_list_data_t *, cfga_list_data_t *,
76 match_type_t);
77 static int compare_o_state(cfga_list_data_t *, cfga_list_data_t *,
78 match_type_t);
79 static int compare_cond(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
80 static int compare_time(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
81 static int compare_info(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
82 static int compare_type(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
83 static int compare_busy(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
84 static int compare_class(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
85 static int compare_null(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
86 static void print_log_id(cfga_list_data_t *, int, char *);
87 static void print_r_state(cfga_list_data_t *, int, char *);
88 static void print_o_state(cfga_list_data_t *, int, char *);
89 static void print_cond(cfga_list_data_t *, int, char *);
90 static void print_time(cfga_list_data_t *, int, char *);
91 static void print_time_p(cfga_list_data_t *, int, char *);
92 static void print_info(cfga_list_data_t *, int, char *);
93 static void print_type(cfga_list_data_t *, int, char *);
94 static void print_busy(cfga_list_data_t *, int, char *);
95 static void print_phys_id(cfga_list_data_t *, int, char *);
96 static void print_class(cfga_list_data_t *, int, char *);
97 static void print_null(cfga_list_data_t *, int, char *);
98 static int count_fields(char *, char);
99 static int process_sort_fields(int, struct sort_el *, char *);
100 static int process_fields(int, struct print_col *, int, char *);
101 static cfga_err_t print_fields(int, struct print_col *, int, int, char *,
102 cfga_list_data_t *, FILE *);
103 static int ldata_compare(const void *, const void *);
105 static void arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts,
106 int dyn_exp);
107 static void out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs,
108 int no_dyn);
109 static void report_no_response(ap_arg_t *arg_array, int napids_to_list);
111 static cfga_err_t set_log_flt(cfga_list_data_t *p, const char *val);
112 static cfga_err_t set_type_flt(cfga_list_data_t *p, const char *val);
113 static cfga_err_t set_class_flt(cfga_list_data_t *p, const char *val);
115 static char *get_dyn(const char *ap_id);
116 static void remove_dyn(char *ap_id);
117 static char *s_strdup(char *str);
120 * global data
122 /* command name for messages */
123 static char *cmdname;
126 * control for comparing, printing and filtering cfga_list_data
127 * NOTE:Field names (i.e. member 0 of field_info struct) may not contain '('.
128 * The post filtering code depends on it.
129 * NOTE:A NULL value for the set_filter member indicates that filtering based
130 * on that field is currently not supported.
132 static struct field_info all_fields[] = {
133 {"ap_id", "Ap_Id", SZ_EL(ap_log_id), compare_ap_id, print_log_id, set_log_flt},
134 {"r_state", "Receptacle", STATE_WIDTH, compare_r_state, print_r_state, NULL},
135 {"o_state", "Occupant", STATE_WIDTH, compare_o_state, print_o_state, NULL},
136 {"condition", "Condition", COND_WIDTH, compare_cond, print_cond, NULL},
137 {"status_time", "When", TIME_WIDTH, compare_time, print_time, NULL},
138 {"status_time_p", "When", TIME_P_WIDTH, compare_time, print_time_p, NULL},
139 {"info", "Information", SZ_EL(ap_info), compare_info, print_info, NULL},
140 {"type", "Type", SZ_EL(ap_type), compare_type, print_type, set_type_flt},
141 {"busy", "Busy", 8, compare_busy, print_busy, NULL},
142 {"physid", "Phys_Id", SZ_EL(ap_phys_id), compare_ap_id, print_phys_id, NULL},
143 {"class", "Class", SZ_EL(ap_class), compare_class, print_class, set_class_flt}
146 #define PREFILT_CLASS_STR "class="
148 typedef struct {
149 cfga_list_data_t ldata; /* Selection criteria */
150 match_type_t match_type_p[N_FIELDS]; /* Type of match */
151 } post_filter_t;
153 static struct field_info null_field =
154 {"null", "", 0, compare_null, print_null, NULL};
156 static struct sort_el *sort_list; /* Used in ldata_compare() */
157 static int nsort_list;
158 static char unk_field[] = "%s: field \"%s\" unknown\n";
160 static char aptype_no_dyn[] = "%s: Invalid ap_id: %s\n";
162 /* strings that make up the usage message */
163 static char *usage_tab[] = {
164 " %s [-f] [-y|-n] [-v] [-o hardware_opts ] -c function ap_id [ap_id...]\n",
165 " %s [-f] [-y|-n] [-v] [-o hardware_opts ] -x function ap_id [ap_id...]\n",
166 " %s [-v] [-s listing_options ] [-o hardware_opts ] [-a]\n"
167 "\t[-l [ap_id|ap_type...]]\n",
168 " %s [-v] [-o hardware_opts ] -t ap_id [ap_id...]\n",
169 " %s [-v] [-o hardware_opts ] -h [ap_id|ap_type...]\n",
172 /* Type of matches currently supported by the select sub-option */
173 static match_cvt_t match_type_array[] = {
174 {"partial", CFGA_MATCH_PARTIAL},
175 {"exact", CFGA_MATCH_EXACT}
178 #define N_MATCH_TYPES (sizeof (match_type_array)/sizeof (match_type_array[0]))
180 static cfga_err_t setup_filter(const char *selectp, const char *matchp,
181 post_filter_t *post_filtp, char **prefilt_optpp);
182 static cfga_err_t parse_select_opt(const char *selectp,
183 post_filter_t *post_filtp, match_type_t match_type);
184 static int do_config_list(int, char *[], cfga_list_data_t *, int, char *,
185 char *, char *, int, char *, post_filter_t *, int);
186 static void do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *jp);
190 * main - the main routine of cfgadm, processes the command line
191 * and dispatches functions off to libraries.
194 main(
195 int argc,
196 char *argv[])
198 extern char *optarg;
199 extern int optind;
200 int c;
201 char *subopts;
202 char *subvalue;
203 char *const *ap_args = NULL;
204 cfga_cmd_t sc_opt = CFGA_CMD_NONE;
205 struct cfga_confirm confirm;
206 struct cfga_msg message;
207 int ret = CFGA_ERROR;
208 int i;
209 char *estrp = NULL;
210 cfga_op_t action = CFGA_OP_NONE;
211 char *plat_opts = NULL;
212 char *act_arg = NULL;
213 enum confirm confarg = CONFIRM_DEFAULT;
214 char *list_opts = NULL;
215 cfga_flags_t flags = 0;
216 int arg_error = 0;
217 int dyn_exp = 0;
219 estrp = NULL;
220 if (argc > 0)
221 cmdname = basename(argv[0]);
222 else
223 cmdname = "cfgadm";
224 (void) setlocale(LC_ALL, "");
225 #if !defined(TEXT_DOMAIN)
226 #define TEXT_DOMAIN "SYS_TEST"
227 #endif
228 (void) textdomain(TEXT_DOMAIN);
230 while ((c = getopt(argc, argv, OPTIONS)) != EOF) {
231 static char dup_action[] =
232 "%s: more than one action specified (-c,-l,-t,-x)\n";
233 static char dup_option[] =
234 "%s: more than one -%c option specified\n";
235 switch (c) {
236 case 'a':
237 if (dyn_exp) {
238 arg_error = 1;
239 (void) fprintf(stderr, gettext(dup_option),
240 cmdname, c);
242 dyn_exp = 1;
243 break;
244 case 'c':
245 if (action != CFGA_OP_NONE) {
246 arg_error = 1;
247 (void) fprintf(stderr, gettext(dup_action),
248 cmdname);
250 action = CFGA_OP_CHANGE_STATE;
251 subopts = optarg;
252 subvalue = NULL;
254 * Reject -c suboption if they are unrecognized
255 * or more than one or have a associated value.
257 if ((sc_opt = getsubopt(&subopts, state_opts,
258 &subvalue)) == -1 || *subopts != '\0' ||
259 subvalue != NULL) {
260 arg_error = 1;
261 break;
263 break;
264 case 'f':
265 if ((flags & CFGA_FLAG_FORCE) != 0) {
266 arg_error = 1;
267 (void) fprintf(stderr, gettext(dup_option),
268 cmdname, c);
270 flags |= CFGA_FLAG_FORCE;
271 break;
272 case 'h':
273 if (action != CFGA_OP_NONE) {
274 arg_error = 1;
275 (void) fprintf(stderr, gettext(dup_action),
276 cmdname);
278 action = CFGA_OP_HELP;
279 break;
280 case 'l':
281 if (action != CFGA_OP_NONE) {
282 arg_error = 1;
283 (void) fprintf(stderr, gettext(dup_action),
284 cmdname);
286 action = CFGA_OP_LIST;
287 break;
288 case 'n':
289 if (confarg != CONFIRM_DEFAULT) {
290 arg_error = 1;
291 (void) fprintf(stderr, gettext(dup_option),
292 cmdname, c);
294 confarg = CONFIRM_NO;
295 break;
296 case 'o':
297 if (plat_opts != NULL) {
298 arg_error = 1;
299 (void) fprintf(stderr, gettext(dup_option),
300 cmdname, c);
302 plat_opts = optarg;
303 break;
304 case 's':
305 if (list_opts != NULL) {
306 arg_error = 1;
307 (void) fprintf(stderr, gettext(dup_option),
308 cmdname, c);
310 list_opts = optarg;
311 break;
312 case 't':
313 if (action != CFGA_OP_NONE) {
314 arg_error = 1;
315 (void) fprintf(stderr, gettext(dup_action),
316 cmdname);
318 action = CFGA_OP_TEST;
319 break;
320 case 'x':
321 if (action != CFGA_OP_NONE) {
322 arg_error = 1;
323 (void) fprintf(stderr, gettext(dup_action),
324 cmdname);
326 action = CFGA_OP_PRIVATE;
327 act_arg = optarg;
328 break;
329 case 'v':
330 if ((flags & CFGA_FLAG_VERBOSE) != 0) {
331 arg_error = 1;
332 (void) fprintf(stderr, gettext(dup_option),
333 cmdname, c);
335 flags |= CFGA_FLAG_VERBOSE;
336 break;
337 case 'y':
338 if (confarg != CONFIRM_DEFAULT) {
339 arg_error = 1;
340 (void) fprintf(stderr, gettext(dup_option),
341 cmdname, c);
343 confarg = CONFIRM_YES;
344 break;
345 case '?': /* getopts issues message is this case */
346 default: /* catch programming errors */
347 arg_error = 1;
348 break;
352 /* default action is list */
353 if (action == CFGA_OP_NONE)
354 action = CFGA_OP_LIST;
356 /* -s and -a option only for list */
357 if (action != CFGA_OP_LIST && (list_opts != NULL || dyn_exp)) {
358 arg_error = 1;
361 if (arg_error) {
362 usage();
363 exit(EXIT_ARGERROR);
364 /*NOTREACHED*/
367 if (getzoneid() != GLOBAL_ZONEID) {
368 cfgadm_error(CFGA_NOTSUPP,
369 gettext("cfgadm can only be run from the global zone"));
370 exit(EXIT_NOTSUPP);
373 ap_args = &argv[optind];
376 * If neither -n of -y was specified, interactive confirmation
377 * is used. Check if the program has terminal I/O and
378 * enforce -n if not.
380 (void) memset(&confirm, 0, sizeof (confirm));
381 if (action == CFGA_OP_CHANGE_STATE || action == CFGA_OP_PRIVATE) {
382 if (confarg == CONFIRM_DEFAULT &&
383 !(isatty(fileno(stdin)) && isatty(fileno(stderr))))
384 confarg = CONFIRM_NO;
385 switch (confarg) {
386 case CONFIRM_DEFAULT:
387 confirm.confirm = confirm_interactive;
388 break;
389 case CONFIRM_NO:
390 confirm.confirm = confirm_no;
391 break;
392 case CONFIRM_YES:
393 confirm.confirm = confirm_yes;
394 break;
395 default: /* paranoia */
396 abort();
397 /*NOTREACHED*/
402 * set up message output routine
404 message.message_routine = message_output;
406 switch (action) {
407 case CFGA_OP_CHANGE_STATE:
408 /* Sanity check - requires an argument */
409 if ((argc - optind) <= 0) {
410 usage();
411 break;
413 /* Sanity check - args cannot be ap_types */
414 for (i = 0; i < (argc - optind); i++) {
415 if (find_arg_type(ap_args[i]) == AP_TYPE) {
416 usage();
417 exit(EXIT_ARGERROR);
418 /*NOTREACHED*/
421 ret = config_change_state(sc_opt, argc - optind, ap_args,
422 plat_opts, &confirm, &message, &estrp, flags);
423 if (ret != CFGA_OK)
424 cfgadm_error(ret, estrp);
425 break;
426 case CFGA_OP_PRIVATE:
427 /* Sanity check - requires an argument */
428 if ((argc - optind) <= 0) {
429 usage();
430 break;
432 /* Sanity check - args cannot be ap_types */
433 for (i = 0; i < (argc - optind); i++) {
434 if (find_arg_type(ap_args[i]) == AP_TYPE) {
435 usage();
436 exit(EXIT_ARGERROR);
437 /*NOTREACHED*/
441 ret = config_private_func(act_arg, argc - optind, ap_args,
442 plat_opts, &confirm, &message, &estrp, flags);
444 if (ret != CFGA_OK)
445 cfgadm_error(ret, estrp);
446 break;
447 case CFGA_OP_TEST:
448 /* Sanity check - requires an argument */
449 if ((argc - optind) <= 0) {
450 usage();
451 break;
454 if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
455 usage();
456 exit(EXIT_ARGERROR);
457 /*NOTREACHED*/
460 /* Sanity check - args cannot be ap_types */
461 for (i = 0; i < (argc - optind); i++) {
462 if (find_arg_type(ap_args[i]) == AP_TYPE) {
463 usage();
464 exit(EXIT_ARGERROR);
465 /*NOTREACHED*/
468 ret = config_test(argc - optind, ap_args, plat_opts, &message,
469 &estrp, flags);
470 if (ret != CFGA_OK)
471 cfgadm_error(ret, estrp);
472 break;
473 case CFGA_OP_HELP:
475 if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
476 usage();
477 exit(EXIT_ARGERROR);
478 /*NOTREACHED*/
481 /* always do usage? */
482 usage();
483 ret = config_help(argc - optind, ap_args, &message, plat_opts,
484 flags);
485 if (ret != CFGA_OK)
486 cfgadm_error(ret, estrp);
487 break;
489 case CFGA_OP_LIST: {
491 * Note that we leak the strdup strings below (we never free
492 * them). This is ok in this context since cfgadm is
493 * a short lived process that will exit shortly freeing
494 * the memory.
496 cfga_list_data_t *list_array = NULL;
497 int nlist = 0;
498 char *sort_fields = s_strdup(DEF_SORT_FIELDS);
499 char *cols = s_strdup(DEF_COLS);
500 char *cols2 = s_strdup(DEF_COLS2);
501 int noheadings = 0;
502 char *delim = s_strdup(DEF_DELIM);
503 int exitcode = EXIT_OK;
504 int i;
505 int type = 0;
506 char *selectp = NULL, *matchp = NULL, *prefilt_optp = NULL;
507 post_filter_t *post_filtp = NULL;
509 if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
510 usage();
511 exit(EXIT_ARGERROR);
512 /*NOTREACHED*/
515 if (flags & CFGA_FLAG_VERBOSE) {
516 cols = s_strdup(DEF_COLS_VERBOSE);
517 cols2 = s_strdup(DEF_COLS2_VERBOSE);
520 if (list_opts != NULL && !extract_list_suboptions(list_opts,
521 &sort_fields, &cols, &cols2, &noheadings, &delim,
522 &selectp, &matchp)) {
523 usage_field();
524 exit(EXIT_ARGERROR);
525 /*NOTREACHED*/
529 * Scan any args and see if there are any ap_types.
530 * If there are we get all attachment point stats and
531 * then filter what gets printed.
534 type = 0;
535 for (i = 0; i < (argc - optind); i++) {
536 if (find_arg_type(ap_args[i]) == AP_TYPE) {
537 type = 1;
538 /* ap_types cannot have dynamic components */
539 if (get_dyn(ap_args[i]) != NULL) {
540 (void) fprintf(stderr,
541 gettext(aptype_no_dyn),
542 cmdname, ap_args[i]);
543 exit(EXIT_ARGERROR);
544 /*NOTREACHED*/
546 break;
550 /* Setup filter */
551 post_filtp = config_calloc_check(1, sizeof (*post_filtp));
552 if (post_filtp == NULL) {
553 exit(EXIT_OPFAILED);
554 /*NOTREACHED*/
556 if (setup_filter(selectp, matchp, post_filtp, &prefilt_optp)
557 != CFGA_OK) {
558 S_FREE(post_filtp);
559 exit(EXIT_ARGERROR);
560 /*NOTREACHED*/
563 list_array = NULL;
564 exitcode = EXIT_OK;
567 * Check for args. No args means find all libs
568 * and call the cfga_list_ext routine with no ap_ids specified.
569 * With args, if any one of the args are ap_types we
570 * again find all attachment points as in the
571 * no-args case above and then select which attachment points
572 * are actually displayed.
574 if (((argc - optind) == 0) || (type == 1)) {
576 * No args, or atleast 1 ap_type arg
578 ret = config_list_ext(0, NULL, &list_array,
579 &nlist, plat_opts, prefilt_optp, &estrp,
580 dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
581 } else {
583 * If the args are all ap_ids (no ap_types) we call the
584 * cfga_list_ext routine with those specific ap_ids.
586 ret = config_list_ext(argc - optind, ap_args,
587 &list_array, &nlist, plat_opts, prefilt_optp,
588 &estrp, dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
591 S_FREE(prefilt_optp);
593 if (ret == CFGA_OK) {
595 if (do_config_list(
596 (argc - optind), &argv[optind], list_array, nlist,
597 sort_fields, cols, cols2, noheadings, delim,
598 post_filtp, dyn_exp) != CFGA_OK) {
599 exitcode = EXIT_ARGERROR;
600 } else {
601 exitcode = EXIT_OK;
604 S_FREE(list_array);
605 S_FREE(post_filtp);
607 if (estrp != NULL && *estrp != '\0')
608 cfgadm_error(CFGA_NOTSUPP, estrp);
609 if (exitcode != EXIT_OK) {
610 exit(exitcode);
611 /*NOTREACHED*/
613 } else {
615 S_FREE(post_filtp);
616 cfgadm_error(ret, estrp);
618 break;
620 default: /* paranoia */
621 abort();
622 /*NOTREACHED*/
625 if (ret == CFGA_NOTSUPP) {
626 return (EXIT_NOTSUPP);
627 } else if (ret != CFGA_OK) {
628 return (EXIT_OPFAILED);
629 } else {
630 return (EXIT_OK);
632 /*NOTREACHED*/
636 * usage - outputs the usage help message.
638 static void
639 usage(void)
641 int i;
643 (void) fprintf(stderr, "%s\n", gettext("Usage:"));
644 for (i = 0; i < sizeof (usage_tab)/sizeof (usage_tab[0]); i++) {
645 (void) fprintf(stderr, gettext(usage_tab[i]), cmdname);
650 * Emit an error message.
651 * As a side-effect the hardware specific error message is deallocated
652 * as described in config_admin(3X).
654 static void
655 cfgadm_error(int errnum, char *estrp)
657 const char *ep;
659 ep = config_strerror(errnum);
660 if (ep == NULL)
661 ep = gettext("configuration administration unknown error");
662 if (estrp != NULL && *estrp != '\0') {
663 (void) fprintf(stderr, "%s: %s: %s\n", cmdname, ep, estrp);
664 } else {
665 (void) fprintf(stderr, "%s: %s\n", cmdname, ep);
667 if (estrp != NULL)
668 free((void *)estrp);
669 if (errnum == CFGA_INVAL)
670 usage();
674 * confirm_interactive - prompt user for confirmation
676 static int
677 confirm_interactive(
678 void *appdata_ptr,
679 const char *message)
681 static int inited = 0;
684 * First time through initialisation. In the original
685 * version of this command this function is only called once,
686 * but this function is generalized for the future.
688 if (!inited) {
689 init_yes();
690 inited = 1;
693 (void) fprintf(stderr, "%s? ", message);
694 return (yes());
697 /*ARGSUSED*/
698 static int
699 confirm_no(
700 void *appdata_ptr,
701 const char *message)
703 return (0);
706 /*ARGSUSED*/
707 static int
708 confirm_yes(
709 void *appdata_ptr,
710 const char *message)
712 return (1);
716 * Find base name of filename.
718 static char *
719 basename(
720 char *cp)
722 char *sp;
724 if ((sp = strrchr(cp, '/')) != NULL)
725 return (sp + 1);
726 return (cp);
729 /*ARGSUSED*/
730 static int
731 message_output(
732 void *appdata_ptr,
733 const char *message)
735 (void) fprintf(stderr, "%s", message);
736 return (CFGA_OK);
741 * extract_list_suboptions - process list option string
743 static int
744 extract_list_suboptions(
745 char *arg,
746 char **sortpp,
747 char **colspp,
748 char **cols2pp,
749 int *noheadingsp,
750 char **delimpp,
751 char **selectpp,
752 char **matchpp)
754 char *value = NULL;
755 int subopt = 0;
756 int err = 0;
758 while (*arg != '\0') {
759 static char need_value[] =
760 "%s: sub-option \"%s\" requires a value\n";
761 static char no_value[] =
762 "%s: sub-option \"%s\" does not take a value\n";
763 static char unk_subopt[] =
764 "%s: sub-option \"%s\" unknown\n";
765 char **pptr;
767 subopt = getsubopt(&arg, list_options, &value);
768 switch (subopt) {
769 case LIST_SORT:
770 pptr = sortpp;
771 goto valcom;
772 case LIST_COLS:
773 pptr = colspp;
774 goto valcom;
775 case LIST_COLS2:
776 pptr = cols2pp;
777 goto valcom;
778 case LIST_SELECT:
779 pptr = selectpp;
780 goto valcom;
781 case LIST_MATCH:
782 pptr = matchpp;
783 goto valcom;
784 case LIST_DELIM:
785 pptr = delimpp;
786 valcom:
787 if (value == NULL) {
788 (void) fprintf(stderr, gettext(need_value),
789 cmdname, list_options[subopt]);
790 err = 1;
791 } else
792 *pptr = value;
793 break;
794 case LIST_NOHEADINGS:
795 if (value != NULL) {
796 (void) fprintf(stderr, gettext(no_value),
797 cmdname, list_options[subopt]);
798 err = 1;
799 } else
800 *noheadingsp = 1;
801 break;
802 default:
803 (void) fprintf(stderr, gettext(unk_subopt),
804 cmdname, value);
805 err = 1;
806 break;
809 return (err == 0);
812 static cfga_err_t
813 setup_prefilter(post_filter_t *post_filtp, char **prefilt_optpp)
815 size_t len;
816 const char *clopt = PREFILT_CLASS_STR;
817 int idx;
820 *prefilt_optpp = NULL;
822 /* Get the index for the "class" field */
823 for (idx = 0; idx < N_FIELDS; idx++) {
824 if (strcmp(all_fields[idx].name, PREFILT_CLASS_STR) == 0)
825 break;
829 * Currently pre-filter available only for class fld w/ EXACT match
831 if (idx >= N_FIELDS ||
832 post_filtp->match_type_p[idx] != CFGA_MATCH_EXACT) {
833 return (CFGA_OK);
836 len = strlen(clopt) + strlen(post_filtp->ldata.ap_class) + 1;
837 if ((*prefilt_optpp = config_calloc_check(1, len)) == NULL) {
838 return (CFGA_LIB_ERROR);
841 (void) strcpy(*prefilt_optpp, clopt);
842 (void) strcat(*prefilt_optpp, post_filtp->ldata.ap_class);
845 * Since it is being pre-filtered, this attribute does not need
846 * post-filtering.
848 post_filtp->match_type_p[idx] = CFGA_MATCH_NOFILTER;
849 if (all_fields[idx].set_filter != NULL) {
850 (void) all_fields[idx].set_filter(&post_filtp->ldata, "");
853 return (CFGA_OK);
856 static cfga_err_t
857 set_attrval(
858 const char *attr,
859 const char *val,
860 post_filter_t *post_filtp,
861 match_type_t match_type)
863 int fld = 0;
864 cfga_err_t ret = CFGA_ERROR;
866 for (fld = 0; fld < N_FIELDS; fld++) {
867 if (strcmp(attr, all_fields[fld].name) == 0)
868 break;
871 /* Valid field or is the select option supported for this field */
872 if (fld >= N_FIELDS || all_fields[fld].set_filter == NULL) {
873 return (CFGA_ATTR_INVAL);
876 if ((ret = all_fields[fld].set_filter(&post_filtp->ldata, val))
877 == CFGA_OK) {
878 post_filtp->match_type_p[fld] = match_type;
881 return (ret);
885 static char inval_optarg[] =
886 "%s: invalid value \"%s\" for %s suboption.\n";
889 * Parses the "select" string and fills in the post_filter structure
891 static cfga_err_t
892 parse_select_opt(
893 const char *selectp,
894 post_filter_t *post_filtp,
895 match_type_t match_type)
897 parse_state_t state = CFGA_PSTATE_INIT;
898 char *cp = NULL, *optstr = NULL, *attr = NULL, *val = NULL;
899 int bal = 0; /* Tracks balancing */
900 char chr;
901 cfga_err_t ret;
904 if (selectp == NULL || post_filtp == NULL) {
905 return (CFGA_ERROR);
908 optstr = config_calloc_check(1, strlen(selectp) + 1);
909 if (optstr == NULL) {
910 return (CFGA_LIB_ERROR);
913 (void) strcpy(optstr, selectp);
915 /* Init */
916 ret = CFGA_ATTR_INVAL;
917 bal = 0;
918 cp = attr = optstr;
919 state = CFGA_PSTATE_INIT;
921 for (; *cp != '\0'; cp++) {
922 switch (state) {
923 case CFGA_PSTATE_INIT:
924 if (*cp != LEFT_PAREN)
925 break;
926 *cp = '\0';
927 val = cp + 1;
928 bal = 1;
929 state = CFGA_PSTATE_ATTR_DONE;
930 break;
931 case CFGA_PSTATE_ATTR_DONE:
932 chr = *cp;
933 switch (chr) {
934 case LEFT_PAREN:
935 bal++;
936 break;
937 case RIGHT_PAREN:
938 bal--;
939 if (bal == 0) {
940 *cp = '\0';
941 state = CFGA_PSTATE_VAL_DONE;
943 break;
945 break;
946 case CFGA_PSTATE_VAL_DONE:
947 if (*cp != ':') {
948 state = CFGA_PSTATE_ERR;
949 goto out;
952 *cp = '\0';
953 if (set_attrval(attr, val, post_filtp,
954 match_type) != CFGA_OK) {
955 state = CFGA_PSTATE_ERR;
956 goto out;
958 state = CFGA_PSTATE_INIT;
959 attr = cp + 1;
960 break;
961 default:
962 state = CFGA_PSTATE_ERR;
963 /* FALLTHROUGH */
964 case CFGA_PSTATE_ERR:
965 goto out;
969 /*FALLTHRU*/
970 out:
971 if (state == CFGA_PSTATE_VAL_DONE) {
972 ret = set_attrval(attr, val, post_filtp, match_type);
973 } else {
974 ret = CFGA_ATTR_INVAL;
977 if (ret != CFGA_OK) {
978 (void) fprintf(stderr, gettext(inval_optarg), cmdname,
979 selectp, list_options[LIST_SELECT]);
982 S_FREE(optstr);
983 return (ret);
988 static cfga_err_t
989 setup_filter(
990 const char *selectp,
991 const char *matchp,
992 post_filter_t *post_filtp,
993 char **prefilt_optpp)
995 cfga_err_t ret = CFGA_ERROR;
996 match_type_t match_type = CFGA_MATCH_NOFILTER;
997 int i;
999 static char match_needs_select[] =
1000 "%s: %s suboption can only be used with %s suboption.\n";
1003 *prefilt_optpp = NULL;
1006 * Initial: no filtering.
1007 * CFGA_MATCH_NOFILTER is NOT a valid user input
1009 for (i = 0; i < N_FIELDS; i++) {
1010 post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
1013 /* Determine type of match */
1014 if (matchp == NULL && selectp == NULL) {
1015 /* No filtering */
1016 return (CFGA_OK);
1017 } else if (matchp == NULL && selectp != NULL) {
1018 match_type = CFGA_DEFAULT_MATCH;
1019 } else if (matchp != NULL && selectp == NULL) {
1020 /* If only match specified, select criteria also needed */
1021 (void) fprintf(stderr, gettext(match_needs_select),
1022 cmdname, list_options[LIST_MATCH],
1023 list_options[LIST_SELECT]);
1024 return (CFGA_ERROR);
1025 } else {
1026 for (i = 0; i < N_MATCH_TYPES; i++) {
1027 if (strcmp(matchp, match_type_array[i].str) == 0) {
1028 match_type = match_type_array[i].type;
1029 break;
1032 if (i >= N_MATCH_TYPES) {
1033 (void) fprintf(stderr, gettext(inval_optarg), cmdname,
1034 matchp, list_options[LIST_MATCH]);
1035 return (CFGA_ERROR);
1039 if ((ret = parse_select_opt(selectp, post_filtp, match_type))
1040 != CFGA_OK) {
1041 return (ret);
1044 /* Handle pre-filtering. */
1045 if ((ret = setup_prefilter(post_filtp, prefilt_optpp)) != CFGA_OK) {
1046 /* Cleanup */
1047 for (i = 0; i < N_FIELDS; i++) {
1048 post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
1050 return (ret);
1054 return (CFGA_OK);
1058 * compare_ap_id - compare two ap_id's
1060 * For partial matches, argument order is significant. The filtering criterion
1061 * should be the first argument.
1064 static int
1065 compare_ap_id(
1066 cfga_list_data_t *p1,
1067 cfga_list_data_t *p2,
1068 match_type_t match_type)
1071 switch (match_type) {
1072 case CFGA_MATCH_NOFILTER:
1073 return (0); /* No filtering. all pass */
1074 case CFGA_MATCH_PARTIAL:
1075 return (strncmp(p1->ap_log_id, p2->ap_log_id,
1076 strlen(p1->ap_log_id)));
1077 case CFGA_MATCH_EXACT:
1078 return (strcmp(p1->ap_log_id, p2->ap_log_id));
1079 case CFGA_MATCH_ORDER:
1080 default:
1081 return (config_ap_id_cmp(p1->ap_log_id, p2->ap_log_id));
1086 * print_log_id - print logical ap_id
1088 static void
1089 print_log_id(
1090 cfga_list_data_t *p,
1091 int width,
1092 char *lp)
1094 (void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_log_id),
1095 p->ap_log_id);
1099 * set_log_flt - Setup filter for logical ap_id
1101 static cfga_err_t
1102 set_log_flt(
1103 cfga_list_data_t *p,
1104 const char *val)
1106 if (strlen(val) > sizeof (p->ap_log_id) - 1)
1107 return (CFGA_ATTR_INVAL);
1109 (void) strcpy(p->ap_log_id, val);
1111 return (CFGA_OK);
1115 * set_type_flt - Setup filter for type field
1118 static cfga_err_t
1119 set_type_flt(
1120 cfga_list_data_t *p,
1121 const char *val)
1123 if (strlen(val) > sizeof (p->ap_type) - 1)
1124 return (CFGA_ATTR_INVAL);
1126 (void) strcpy(p->ap_type, val);
1128 return (CFGA_OK);
1132 * set_class_flt - Setup filter for class field
1134 static cfga_err_t
1135 set_class_flt(
1136 cfga_list_data_t *p,
1137 const char *val)
1139 if (strlen(val) > sizeof (p->ap_class) - 1)
1140 return (CFGA_ATTR_INVAL);
1142 (void) strcpy(p->ap_class, val);
1144 return (CFGA_OK);
1149 * compare_r_state - compare receptacle state of two ap_id's
1151 static int
1152 compare_r_state(
1153 cfga_list_data_t *p1,
1154 cfga_list_data_t *p2,
1155 match_type_t match_type)
1157 switch (match_type) {
1158 case CFGA_MATCH_NOFILTER: /* no filtering. pass all */
1159 return (0);
1160 case CFGA_MATCH_ORDER:
1161 default:
1162 return (p1->ap_r_state - p2->ap_r_state);
1167 * compare_o_state - compare occupant state of two ap_id's
1169 static int
1170 compare_o_state(
1171 cfga_list_data_t *p1,
1172 cfga_list_data_t *p2,
1173 match_type_t match_type)
1175 switch (match_type) {
1176 case CFGA_MATCH_NOFILTER: /* no filtering. all pass */
1177 return (0);
1178 case CFGA_MATCH_ORDER:
1179 default:
1180 return (p1->ap_o_state - p2->ap_o_state);
1185 * compare_busy - compare busy field of two ap_id's
1187 static int
1188 compare_busy(
1189 cfga_list_data_t *p1,
1190 cfga_list_data_t *p2,
1191 match_type_t match_type)
1194 switch (match_type) {
1195 case CFGA_MATCH_NOFILTER: /* no filtering. all pass */
1196 return (0);
1197 case CFGA_MATCH_ORDER:
1198 default:
1199 return (p1->ap_busy - p2->ap_busy);
1204 * print_r_state - print receptacle state
1206 static void
1207 print_r_state(
1208 cfga_list_data_t *p,
1209 int width,
1210 char *lp)
1212 char *cp;
1214 switch (p->ap_r_state) {
1215 case CFGA_STAT_EMPTY:
1216 cp = "empty";
1217 break;
1218 case CFGA_STAT_CONNECTED:
1219 cp = "connected";
1220 break;
1221 case CFGA_STAT_DISCONNECTED:
1222 cp = "disconnected";
1223 break;
1224 default:
1225 cp = "???";
1226 break;
1228 (void) sprintf(lp, "%-*s", width, cp);
1232 * print_o_state - print occupant state
1234 static void
1235 print_o_state(
1236 cfga_list_data_t *p,
1237 int width,
1238 char *lp)
1240 char *cp;
1242 switch (p->ap_o_state) {
1243 case CFGA_STAT_UNCONFIGURED:
1244 cp = "unconfigured";
1245 break;
1246 case CFGA_STAT_CONFIGURED:
1247 cp = "configured";
1248 break;
1249 default:
1250 cp = "???";
1251 break;
1253 (void) sprintf(lp, "%-*s", width, cp);
1257 * compare_cond - compare condition field of two ap_id's
1259 static int
1260 compare_cond(
1261 cfga_list_data_t *p1,
1262 cfga_list_data_t *p2,
1263 match_type_t match_type)
1266 switch (match_type) {
1267 case CFGA_MATCH_NOFILTER:
1268 return (0);
1269 case CFGA_MATCH_ORDER:
1270 default:
1271 return (p1->ap_cond - p2->ap_cond);
1276 * print_cond - print attachment point condition
1278 static void
1279 print_cond(
1280 cfga_list_data_t *p,
1281 int width,
1282 char *lp)
1284 char *cp;
1286 switch (p->ap_cond) {
1287 case CFGA_COND_UNKNOWN:
1288 cp = "unknown";
1289 break;
1290 case CFGA_COND_UNUSABLE:
1291 cp = "unusable";
1292 break;
1293 case CFGA_COND_FAILING:
1294 cp = "failing";
1295 break;
1296 case CFGA_COND_FAILED:
1297 cp = "failed";
1298 break;
1299 case CFGA_COND_OK:
1300 cp = "ok";
1301 break;
1302 default:
1303 cp = "???";
1304 break;
1306 (void) sprintf(lp, "%-*s", width, cp);
1310 * compare_time - compare time field of two ap_id's
1312 static int
1313 compare_time(
1314 cfga_list_data_t *p1,
1315 cfga_list_data_t *p2,
1316 match_type_t match_type)
1318 switch (match_type) {
1319 case CFGA_MATCH_NOFILTER:
1320 return (0);
1321 case CFGA_MATCH_ORDER:
1322 default:
1323 return (p1->ap_status_time - p2->ap_status_time);
1329 * print_time - print time from cfga_list_data.
1330 * Time print based on ls(1).
1332 static void
1333 print_time(
1334 cfga_list_data_t *p,
1335 int width,
1336 char *lp)
1338 static time_t year, now;
1339 time_t stime;
1340 char time_buf[50]; /* array to hold day and time */
1342 if (year == 0) {
1343 now = time(NULL);
1344 year = now - 6L*30L*24L*60L*60L; /* 6 months ago */
1345 now = now + 60;
1347 stime = p->ap_status_time;
1348 if (stime == (time_t)-1) {
1349 (void) sprintf(lp, "%-*s", width, gettext("unavailable"));
1350 return;
1353 if ((stime < year) || (stime > now)) {
1354 (void) strftime(time_buf, sizeof (time_buf),
1355 dcgettext(NULL, FORMAT1, LC_TIME), localtime(&stime));
1356 } else {
1357 (void) strftime(time_buf, sizeof (time_buf),
1358 dcgettext(NULL, FORMAT2, LC_TIME), localtime(&stime));
1360 (void) sprintf(lp, "%-*s", width, time_buf);
1364 * print_time_p - print time from cfga_list_data.
1366 static void
1367 print_time_p(
1368 cfga_list_data_t *p,
1369 int width,
1370 char *lp)
1372 struct tm *tp;
1373 char tstr[TIME_P_WIDTH+1];
1375 tp = localtime(&p->ap_status_time);
1376 (void) sprintf(tstr, "%04d%02d%02d%02d%02d%02d", tp->tm_year + 1900,
1377 tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
1378 (void) sprintf(lp, "%-*s", width, tstr);
1382 * compare_info - compare info from two cfga_list_data structs
1384 static int
1385 compare_info(
1386 cfga_list_data_t *p1,
1387 cfga_list_data_t *p2,
1388 match_type_t match_type)
1390 switch (match_type) {
1391 case CFGA_MATCH_NOFILTER:
1392 return (0);
1393 case CFGA_MATCH_ORDER:
1394 default:
1395 return (strncmp(p1->ap_info, p2->ap_info,
1396 sizeof (p2->ap_info)));
1401 * print_info - print info from cfga_list_data struct
1403 static void
1404 print_info(
1405 cfga_list_data_t *p,
1406 int width,
1407 char *lp)
1409 (void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_info), p->ap_info);
1413 * compare_type - compare type from two cfga_list_data structs
1415 * For partial matches, argument order is significant. The filtering criterion
1416 * should be the first argument.
1418 static int
1419 compare_type(
1420 cfga_list_data_t *p1,
1421 cfga_list_data_t *p2,
1422 match_type_t match_type)
1424 switch (match_type) {
1425 case CFGA_MATCH_NOFILTER:
1426 return (0);
1427 case CFGA_MATCH_PARTIAL:
1428 return (strncmp(p1->ap_type, p2->ap_type, strlen(p1->ap_type)));
1429 case CFGA_MATCH_EXACT:
1430 case CFGA_MATCH_ORDER:
1431 default:
1432 return (strncmp(p1->ap_type, p2->ap_type,
1433 sizeof (p2->ap_type)));
1438 * print_type - print type from cfga_list_data struct
1440 static void
1441 print_type(
1442 cfga_list_data_t *p,
1443 int width,
1444 char *lp)
1446 (void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_type), p->ap_type);
1451 * compare_class - compare class from two cfga_list_data structs
1453 * For partial matches, argument order is significant. The filtering criterion
1454 * should be the first argument.
1456 static int
1457 compare_class(
1458 cfga_list_data_t *p1,
1459 cfga_list_data_t *p2,
1460 match_type_t match_type)
1463 switch (match_type) {
1464 case CFGA_MATCH_NOFILTER:
1465 return (0);
1466 case CFGA_MATCH_PARTIAL:
1467 return (strncmp(p1->ap_class, p2->ap_class,
1468 strlen(p1->ap_class)));
1469 case CFGA_MATCH_EXACT:
1470 case CFGA_MATCH_ORDER:
1471 default:
1472 return (strncmp(p1->ap_class, p2->ap_class,
1473 sizeof (p2->ap_class)));
1478 * print_class - print class from cfga_list_data struct
1480 static void
1481 print_class(
1482 cfga_list_data_t *p,
1483 int width,
1484 char *lp)
1486 (void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_class), p->ap_class);
1489 * print_busy - print busy from cfga_list_data struct
1491 /* ARGSUSED */
1492 static void
1493 print_busy(
1494 cfga_list_data_t *p,
1495 int width,
1496 char *lp)
1498 if (p->ap_busy)
1499 (void) sprintf(lp, "%-*.*s", width, width, "y");
1500 else
1501 (void) sprintf(lp, "%-*.*s", width, width, "n");
1505 * print_phys_id - print physical ap_id
1507 static void
1508 print_phys_id(
1509 cfga_list_data_t *p,
1510 int width,
1511 char *lp)
1513 (void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_phys_id),
1514 p->ap_phys_id);
1519 * find_field - find the named field
1521 static struct field_info *
1522 find_field(char *fname)
1524 struct field_info *fldp;
1526 for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++)
1527 if (strcmp(fname, fldp->name) == 0)
1528 return (fldp);
1529 return (NULL);
1533 * usage_field - print field usage
1535 static void
1536 usage_field()
1538 struct field_info *fldp = NULL;
1539 const char *sep;
1540 static char field_list[] = "%s: print or sort fields must be one of:";
1542 (void) fprintf(stderr, gettext(field_list), cmdname);
1543 sep = "";
1545 for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++) {
1546 (void) fprintf(stderr, "%s %s", sep, fldp->name);
1547 sep = ",";
1549 (void) fprintf(stderr, "\n");
1553 * compare_null - null comparison routine
1555 /*ARGSUSED*/
1556 static int
1557 compare_null(
1558 cfga_list_data_t *p1,
1559 cfga_list_data_t *p2,
1560 match_type_t match_type)
1562 return (0);
1566 * print_null - print out a field of spaces
1568 /*ARGSUSED*/
1569 static void
1570 print_null(
1571 cfga_list_data_t *p,
1572 int width,
1573 char *lp)
1575 (void) sprintf(lp, "%-*s", width, "");
1579 * do_config_list - directs the output of the listing functions
1581 static int
1582 do_config_list(
1583 int l_argc,
1584 char *l_argv[],
1585 cfga_list_data_t *statlist,
1586 int nlist,
1587 char *sortp,
1588 char *colsp,
1589 char *cols2p,
1590 int noheadings,
1591 char *delimp,
1592 post_filter_t *post_filtp,
1593 int dyn_exp)
1595 int nprcols = 0, ncols2 = 0;
1596 struct print_col *prnt_list = NULL;
1597 int napids_to_list = 0;
1598 FILE *fp = NULL;
1599 int f_err;
1600 cfga_list_data_t **sel_boards = NULL;
1601 int nsel = 0;
1602 int i, j;
1603 cfga_err_t ret;
1605 ap_arg_t *arg_array = NULL;
1606 ap_out_t *out_array = NULL;
1609 sort_list = NULL;
1610 f_err = 0;
1611 fp = stdout;
1612 nsort_list = count_fields(sortp, FDELIM);
1613 if (nsort_list != 0) {
1614 sort_list = config_calloc_check(nsort_list,
1615 sizeof (*sort_list));
1616 if (sort_list == NULL) {
1617 ret = CFGA_LIB_ERROR;
1618 goto out;
1620 f_err |= process_sort_fields(nsort_list, sort_list, sortp);
1621 } else
1622 sort_list = NULL;
1624 nprcols = count_fields(colsp, FDELIM);
1625 if ((ncols2 = count_fields(cols2p, FDELIM)) > nprcols)
1626 nprcols = ncols2;
1627 if (nprcols != 0) {
1628 prnt_list = config_calloc_check(nprcols, sizeof (*prnt_list));
1629 if (prnt_list == NULL) {
1630 ret = CFGA_LIB_ERROR;
1631 goto out;
1633 f_err |= process_fields(nprcols, prnt_list, 0, colsp);
1634 if (ncols2 != 0)
1635 f_err |= process_fields(nprcols, prnt_list, 1, cols2p);
1636 } else
1637 prnt_list = NULL;
1639 if (f_err) {
1640 usage_field();
1641 ret = CFGA_ERROR;
1642 goto out;
1645 /* Create an array of all user args (if any) */
1646 if (l_argc != 0) {
1647 int i, j;
1649 napids_to_list = 0;
1651 for (i = 0; i < l_argc; i++) {
1652 napids_to_list += count_fields(l_argv[i], ARG_DELIM);
1655 arg_array = config_calloc_check(napids_to_list,
1656 sizeof (*arg_array));
1657 if (arg_array == NULL) {
1658 ret = CFGA_LIB_ERROR;
1659 goto out;
1662 for (i = 0, j = 0; i < l_argc; i++) {
1663 int n;
1665 n = count_fields(l_argv[i], ARG_DELIM);
1666 if (n == 0) {
1667 continue;
1668 } else if (n == 1) {
1669 arg_array[j].arg = l_argv[i];
1670 arg_array[j].resp = 0;
1671 j++;
1672 } else {
1673 char *cp, *ncp;
1675 cp = l_argv[i];
1676 for (;;) {
1677 arg_array[j].arg = cp;
1678 arg_array[j].resp = 0;
1679 j++;
1680 ncp = strchr(cp, ARG_DELIM);
1681 if (ncp == NULL)
1682 break;
1683 *ncp = '\0';
1684 cp = ncp + 1;
1688 assert(j == napids_to_list);
1689 } else {
1690 napids_to_list = 0;
1691 arg_array = NULL;
1694 assert(nlist != 0);
1696 out_array = config_calloc_check(nlist, sizeof (*out_array));
1697 if (out_array == NULL) {
1698 ret = CFGA_LIB_ERROR;
1699 goto out;
1703 /* create a list of output stat data */
1704 for (i = 0; i < nlist; i++) {
1705 out_array[i].ldatap = &statlist[i];
1706 out_array[i].req = 0;
1710 * Mark all user input which got atleast 1 stat data in response
1712 for (i = 0; i < napids_to_list; i++) {
1713 arg_got_resp(&arg_array[i], out_array, nlist, dyn_exp);
1717 * Process output data
1719 nsel = 0;
1720 for (i = 0; i < nlist; i++) {
1722 * Mark all the stats which were actually requested by user
1724 out_was_req(&out_array[i], arg_array, napids_to_list, 0);
1725 if (out_array[i].req == 0 && dyn_exp) {
1727 * Try again without the dynamic component for the
1728 * if dynamic expansion was requested.
1730 out_was_req(&out_array[i], arg_array,
1731 napids_to_list, 1);
1735 * post filter data which was actually requested
1737 if (out_array[i].req == 1) {
1738 do_post_filter(&out_array[i], post_filtp, &nsel);
1742 sel_boards = config_calloc_check(nsel, sizeof (*sel_boards));
1743 if (sel_boards == NULL) {
1744 ret = CFGA_LIB_ERROR;
1745 goto out;
1748 for (i = 0, j = 0; i < nlist; i++) {
1749 if (out_array[i].req == 1) {
1750 sel_boards[j] = out_array[i].ldatap;
1751 j++;
1755 assert(j == nsel);
1758 * Print headings even if no list entries - Bug or feature ?
1760 if (!noheadings && prnt_list != NULL) {
1761 if ((ret = print_fields(nprcols, prnt_list, 1, 0,
1762 delimp, NULL, fp)) != CFGA_OK) {
1763 goto out;
1765 if (ncols2 != 0) {
1766 if ((ret = print_fields(nprcols, prnt_list, 1,
1767 1, delimp, NULL, fp)) != CFGA_OK) {
1768 goto out;
1773 if (nsel != 0) {
1774 if (sort_list != NULL && nsel > 1) {
1775 qsort(sel_boards, nsel, sizeof (sel_boards[0]),
1776 ldata_compare);
1779 if (prnt_list != NULL) {
1780 for (i = 0; i < nsel; i++) {
1781 if ((ret = print_fields(nprcols,
1782 prnt_list, 0, 0, delimp, sel_boards[i], fp))
1783 != CFGA_OK)
1784 goto out;
1785 if (ncols2 != 0) {
1786 if ((ret = print_fields(
1787 nprcols, prnt_list, 0, 1, delimp,
1788 sel_boards[i], fp)) != CFGA_OK)
1789 goto out;
1795 * Go thru the argument list and notify user about args
1796 * which did not have a match
1798 report_no_response(arg_array, napids_to_list);
1799 ret = CFGA_OK;
1800 /*FALLTHRU*/
1801 out:
1802 S_FREE(sel_boards);
1803 S_FREE(arg_array);
1804 S_FREE(out_array);
1806 S_FREE(sort_list);
1807 S_FREE(prnt_list);
1809 return (ret);
1814 * Mark all user inputs which got a response
1816 static void
1817 arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts, int dyn_exp)
1819 int i;
1820 cfga_ap_types_t type;
1823 if (nouts == 0) {
1824 return;
1827 type = find_arg_type(inp->arg);
1830 * Go through list of output stats and check if argument
1831 * produced that output
1833 for (i = 0; i < nouts; i++) {
1834 if (type == PHYSICAL_AP_ID) {
1835 if (config_ap_id_cmp(out_array[i].ldatap->ap_phys_id,
1836 inp->arg) == 0) {
1837 break;
1839 } else if (type == LOGICAL_AP_ID) {
1840 if (config_ap_id_cmp(out_array[i].ldatap->ap_log_id,
1841 inp->arg) == 0) {
1842 break;
1844 } else if (type == AP_TYPE) {
1846 * An AP_TYPE argument cannot generate dynamic
1847 * attachment point stats unless dynamic expansion was
1848 * requested by user.
1850 if (!dyn_exp && get_dyn(out_array[i].ldatap->ap_log_id)
1851 != NULL) {
1852 continue;
1855 if (strncmp(out_array[i].ldatap->ap_log_id, inp->arg,
1856 strlen(inp->arg)) == 0) {
1857 break;
1859 } else {
1860 return;
1864 if (i < nouts) {
1865 inp->resp = 1;
1869 /* Mark all stat data which were requested by user */
1870 static void
1871 out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs, int no_dyn)
1873 int i;
1874 cfga_ap_types_t type = UNKNOWN_AP;
1875 char physid[MAXPATHLEN], logid[MAXPATHLEN];
1878 /* If no user args, all output is acceptable */
1879 if (nargs == 0) {
1880 outp->req = 1;
1881 return;
1885 (void) snprintf(physid, sizeof (physid), "%s",
1886 outp->ldatap->ap_phys_id);
1887 (void) snprintf(logid, sizeof (logid), "%s", outp->ldatap->ap_log_id);
1890 * Do comparison with or without dynamic component as requested by
1891 * user.
1893 if (no_dyn) {
1894 /* Remove the dynamic component */
1895 remove_dyn(physid);
1896 remove_dyn(logid);
1899 for (i = 0; i < nargs; i++) {
1900 type = find_arg_type(in_array[i].arg);
1901 if (type == PHYSICAL_AP_ID) {
1903 if (config_ap_id_cmp(in_array[i].arg, physid) == 0) {
1904 break;
1906 } else if (type == LOGICAL_AP_ID) {
1908 if (config_ap_id_cmp(in_array[i].arg, logid) == 0) {
1909 break;
1911 } else if (type == AP_TYPE) {
1913 * Aptypes cannot generate dynamic attachment
1914 * points unless dynamic expansion is specified.
1915 * in which case this routine would be called a
1916 * 2nd time with the no_dyn flag set and there
1917 * would be no dynamic ap_ids.
1919 if (get_dyn(logid) != NULL) {
1920 continue;
1923 if (strncmp(in_array[i].arg, logid,
1924 strlen(in_array[i].arg)) == 0) {
1925 break;
1927 } else {
1928 continue;
1932 if (i < nargs) {
1933 /* Ok, this output was requested */
1934 outp->req = 1;
1939 static void
1940 do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *nselp)
1942 int i;
1944 if (outp->req != 1) {
1945 return;
1949 * For fields without filtering (CFGA_MATCH_NOFILTER),
1950 * compare always returns 0 (success)
1952 for (i = 0; i < N_FIELDS; i++) {
1954 * Note: Order is important for partial match (via strncmp).
1955 * The first argument for compare must be the filter.
1957 if (all_fields[i].compare(&post_filtp->ldata, outp->ldatap,
1958 post_filtp->match_type_p[i])) {
1959 outp->req = 0; /* Blocked by filter */
1960 return;
1965 * Passed through filter
1967 (*nselp)++;
1970 static void
1971 report_no_response(ap_arg_t *arg_array, int nargs)
1973 int i;
1975 if (nargs == 0) {
1976 return;
1981 * nop if no user arguments
1983 for (i = 0; i < nargs; i++) {
1984 if (arg_array[i].resp == 0) {
1985 (void) fprintf(stderr,
1986 gettext("%s: No matching library found\n"),
1987 arg_array[i].arg);
1993 * ldata_compare - compare two attachment point list data structures.
1995 static int
1996 ldata_compare(
1997 const void *vb1,
1998 const void *vb2)
2000 int i;
2001 int res = -1;
2002 cfga_list_data_t *b1, *b2;
2005 b1 = *(cfga_list_data_t **)vb1;
2006 b2 = *(cfga_list_data_t **)vb2;
2008 for (i = 0; i < nsort_list; i++) {
2009 res = (*(sort_list[i].fld->compare))(b1, b2, CFGA_MATCH_ORDER);
2010 if (res != 0) {
2011 if (sort_list[i].reverse)
2012 res = -res;
2013 break;
2017 return (res);
2021 * count_fields - Count the number of fields, using supplied delimiter.
2023 static int
2024 count_fields(char *fspec, char delim)
2026 char *cp = NULL;
2027 int n;
2029 if (fspec == 0 || *fspec == '\0')
2030 return (0);
2031 n = 1;
2032 for (cp = fspec; *cp != '\0'; cp++)
2033 if (*cp == delim)
2034 n++;
2035 return (n);
2039 * get_field
2040 * This function is not a re-implementation of strtok().
2041 * There can be null fields - strtok() eats spans of delimiters.
2043 static char *
2044 get_field(char **fspp)
2046 char *cp = NULL, *fld;
2048 fld = *fspp;
2050 if (fld != NULL && *fld == '\0')
2051 fld = NULL;
2053 if (fld != NULL) {
2054 cp = strchr(*fspp, FDELIM);
2055 if (cp == NULL) {
2056 *fspp = NULL;
2057 } else {
2058 *cp = '\0';
2059 *fspp = cp + 1;
2060 if (*fld == '\0')
2061 fld = NULL;
2064 return (fld);
2068 * process_fields -
2070 static int
2071 process_fields(
2072 int ncol,
2073 struct print_col *list,
2074 int line2,
2075 char *fmt)
2077 struct print_col *pp = NULL;
2078 struct field_info *fldp = NULL;
2079 char *fmtx;
2080 char *fldn;
2081 int err;
2083 err = 0;
2084 fmtx = fmt;
2085 for (pp = list; pp < &list[ncol]; pp++) {
2086 fldn = get_field(&fmtx);
2087 fldp = &null_field;
2088 if (fldn != NULL) {
2089 struct field_info *tfldp;
2091 tfldp = find_field(fldn);
2092 if (tfldp != NULL) {
2093 fldp = tfldp;
2094 } else {
2095 (void) fprintf(stderr, gettext(unk_field),
2096 cmdname, fldn);
2097 err = 1;
2100 if (line2) {
2101 pp->line2 = fldp;
2102 if (fldp->width > pp->width)
2103 pp->width = fldp->width;
2104 } else {
2105 pp->line1 = fldp;
2106 pp->width = fldp->width;
2109 return (err);
2113 * process_sort_fields -
2115 static int
2116 process_sort_fields(
2117 int nsort,
2118 struct sort_el *list,
2119 char *fmt)
2121 int i;
2122 int rev;
2123 struct field_info *fldp = NULL;
2124 char *fmtx;
2125 char *fldn;
2126 int err;
2128 err = 0;
2129 fmtx = fmt;
2130 for (i = 0; i < nsort; i++) {
2131 fldn = get_field(&fmtx);
2132 fldp = &null_field;
2133 rev = 0;
2134 if (fldn != NULL) {
2135 struct field_info *tfldp = NULL;
2137 if (*fldn == '-') {
2138 rev = 1;
2139 fldn++;
2141 tfldp = find_field(fldn);
2142 if (tfldp != NULL) {
2143 fldp = tfldp;
2144 } else {
2145 (void) fprintf(stderr, gettext(unk_field),
2146 cmdname, fldn);
2147 err = 1;
2150 list[i].reverse = rev;
2151 list[i].fld = fldp;
2153 return (err);
2157 * print_fields -
2159 static cfga_err_t
2160 print_fields(
2161 int ncol,
2162 struct print_col *list,
2163 int heading,
2164 int line2,
2165 char *delim,
2166 cfga_list_data_t *bdp,
2167 FILE *fp)
2169 char *del = NULL;
2170 struct print_col *pp = NULL;
2171 struct field_info *fldp = NULL;
2172 static char *outline, *end;
2173 char *lp;
2175 if (outline == NULL) {
2176 int out_len, delim_len;
2178 delim_len = strlen(delim);
2179 out_len = 0;
2180 for (pp = list; pp < &list[ncol]; pp++) {
2181 out_len += pp->width;
2182 out_len += delim_len;
2184 out_len -= delim_len;
2185 outline = config_calloc_check(out_len + 1, 1);
2186 if (outline == NULL) {
2187 return (CFGA_LIB_ERROR);
2189 end = &outline[out_len + 1];
2192 lp = outline;
2193 del = "";
2194 for (pp = list; pp < &list[ncol]; pp++) {
2195 fldp = line2 ? pp->line2 : pp->line1;
2196 (void) snprintf(lp, end - lp, "%s", del);
2197 lp += strlen(lp);
2198 if (heading) {
2199 (void) snprintf(lp, end - lp, "%-*s",
2200 fldp->width, fldp->heading);
2201 } else {
2202 (*fldp->printfn)(bdp, fldp->width, lp);
2204 lp += strlen(lp);
2205 del = delim;
2209 * Trim trailing spaces
2211 while (--lp >= outline && *lp == ' ')
2212 *lp = '\0';
2213 (void) fprintf(fp, "%s\n", outline);
2214 return (CFGA_OK);
2218 * config_calloc_check - perform allocation, check result and
2219 * set error indicator
2221 static void *
2222 config_calloc_check(
2223 size_t nelem,
2224 size_t elsize)
2226 void *p;
2227 static char alloc_fail[] =
2228 "%s: memory allocation failed (%d*%d bytes)\n";
2231 p = calloc(nelem, elsize);
2232 if (p == NULL) {
2233 (void) fprintf(stderr, gettext(alloc_fail), cmdname,
2234 nelem, elsize);
2236 return (p);
2240 * find_arg_type - determine if an argument is an ap_id or an ap_type.
2242 static cfga_ap_types_t
2243 find_arg_type(const char *ap_id)
2245 struct stat sbuf;
2246 cfga_ap_types_t type;
2247 char *mkr = NULL, *cp;
2248 int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
2249 char path[MAXPATHLEN];
2250 char apbuf[MAXPATHLEN];
2251 size_t len;
2255 * sanity checks
2257 if (ap_id == NULL || *ap_id == '\0') {
2258 return (UNKNOWN_AP);
2262 * Mask the dynamic component if any
2264 if ((cp = GET_DYN(ap_id)) != NULL) {
2265 len = cp - ap_id;
2266 } else {
2267 len = strlen(ap_id);
2270 if (len >= sizeof (apbuf)) {
2271 return (UNKNOWN_AP);
2274 (void) strncpy(apbuf, ap_id, len);
2275 apbuf[len] = '\0';
2278 * If it starts with a slash and is stat-able
2279 * its a physical.
2281 if (*apbuf == '/' && stat(apbuf, &sbuf) == 0) {
2282 return (PHYSICAL_AP_ID);
2286 * Is this a symlink in CFGA_DEV_DIR ?
2288 (void) snprintf(path, sizeof (path), "%s/%s", CFGA_DEV_DIR, apbuf);
2290 if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
2291 stat(path, &sbuf) == 0) {
2292 return (LOGICAL_AP_ID);
2296 * Check for ":" which is always present in an ap_id but not maybe
2297 * present or absent in an ap_type.
2298 * We need to check that the characters right before the : are digits
2299 * since an ap_id is of the form <name><instance>:<specific ap name>
2301 if ((mkr = strchr(apbuf, ':')) == NULL) {
2302 type = AP_TYPE;
2303 } else {
2304 size_ap = strlen(apbuf);
2305 size_mkr = strlen(mkr);
2306 mkr = apbuf;
2308 digit = 0;
2309 for (i = size_ap - size_mkr - 1; i > 0; i--) {
2310 if ((int)isdigit(mkr[i])) {
2311 digit++;
2312 break;
2315 if (digit == 0) {
2316 type = AP_TYPE;
2317 } else {
2318 type = LOGICAL_AP_ID;
2322 return (type);
2326 static char *
2327 get_dyn(const char *ap_id)
2329 if (ap_id == NULL) {
2330 return (NULL);
2333 return (strstr(ap_id, CFGA_DYN_SEP));
2337 * removes the dynamic component
2339 static void
2340 remove_dyn(char *ap_id)
2342 char *cp;
2344 if (ap_id == NULL) {
2345 return;
2348 cp = strstr(ap_id, CFGA_DYN_SEP);
2349 if (cp != NULL) {
2350 *cp = '\0';
2355 static char *
2356 s_strdup(char *str)
2358 char *dup;
2361 * sometimes NULL strings may be passed in (see DEF_COLS2). This
2362 * is not an error.
2364 if (str == NULL) {
2365 return (NULL);
2368 dup = strdup(str);
2369 if (dup == NULL) {
2370 (void) fprintf(stderr,
2371 "%s \"%s\"\n", gettext("Cannot copy string"), str);
2372 return (NULL);
2375 return (dup);