Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * <:copyright-BRCM:2016:DUAL/GPL:standard |
| 4 | * |
| 5 | * Copyright (c) 2016 Broadcom |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * Unless you and Broadcom execute a separate written software license |
| 9 | * agreement governing use of this software, this software is licensed |
| 10 | * to you under the terms of the GNU General Public License version 2 |
| 11 | * (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 12 | * with the following added to such license: |
| 13 | * |
| 14 | * As a special exception, the copyright holders of this software give |
| 15 | * you permission to link this software with independent modules, and |
| 16 | * to copy and distribute the resulting executable under terms of your |
| 17 | * choice, provided that you also meet, for each linked independent |
| 18 | * module, the terms and conditions of the license of that module. |
| 19 | * An independent module is a module which is not derived from this |
| 20 | * software. The special exception does not apply to any modifications |
| 21 | * of the software. |
| 22 | * |
| 23 | * Not withstanding the above, under no circumstances may you combine |
| 24 | * this software in any way with any other Broadcom software provided |
| 25 | * under a license other than the GPL, without Broadcom's express prior |
| 26 | * written consent. |
| 27 | * |
| 28 | * :> |
| 29 | * |
| 30 | *****************************************************************************/ |
| 31 | /* |
| 32 | * cmdline.c - Command line argument helper |
| 33 | */ |
| 34 | |
| 35 | #include <cmdline.h> |
| 36 | |
| 37 | /* Print usage information */ |
| 38 | bcmos_errno cl_print_usage(const char *app, const char *arg, |
| 39 | const cl_argument supported_args[], int num_supported_args, |
| 40 | cl_argument_usage_flags flags) |
| 41 | { |
| 42 | const char *owner = ""; |
| 43 | int i; |
| 44 | |
| 45 | if (arg) |
| 46 | bcmos_printf("*** Error in parameter %s\n", arg); |
| 47 | if (app) |
| 48 | { |
| 49 | const char *p, *fname; |
| 50 | p = strrchr(app, '/'); |
| 51 | fname = p ? p + 1 : app; |
| 52 | bcmos_printf("Usage: %s arguments\n", fname); |
| 53 | } |
| 54 | for (i = 0; i < num_supported_args; i++) |
| 55 | { |
| 56 | const cl_argument *opt = &supported_args[i]; |
| 57 | if ((flags & CL_ARGUMENT_USAGE_FLAG_OWNER) != 0 && |
| 58 | opt->owner && strcmp(opt->owner, owner)) |
| 59 | { |
| 60 | owner = opt->owner; |
| 61 | bcmos_printf("\n%s options:\n", owner); |
| 62 | } |
| 63 | if (opt->short_name) |
| 64 | bcmos_printf("%-3s%c", opt->short_name, opt->long_name ? ',' : ' '); |
| 65 | else |
| 66 | bcmos_printf(" "); |
| 67 | bcmos_printf("%-20s %20s %s\n", |
| 68 | opt->long_name ? opt->long_name : "", |
| 69 | opt->extra_arg ? opt->extra_arg : "", |
| 70 | opt->description ? opt->description : ""); |
| 71 | } |
| 72 | return BCM_ERR_PARM; |
| 73 | } |
| 74 | |
| 75 | /* Get parameter by name */ |
| 76 | const cl_argument *cl_parm_get(const char *name, const cl_argument supported_args[], int num_supported_args) |
| 77 | { |
| 78 | const cl_argument *opt = NULL; |
| 79 | int i; |
| 80 | |
| 81 | /* Make sure that all mandatory parameters are set */ |
| 82 | for (i = 0; i < num_supported_args; i++) |
| 83 | { |
| 84 | opt = &supported_args[i]; |
| 85 | if ((opt->short_name && !strcmp(opt->short_name, name)) || |
| 86 | (opt->long_name && !strcmp(opt->long_name, name))) |
| 87 | break; |
| 88 | } |
| 89 | return (i < num_supported_args) ? opt : NULL; |
| 90 | } |
| 91 | |
| 92 | /* Validate command line parameters */ |
| 93 | bcmos_errno cl_validate(int argc, char *argv[], |
| 94 | const cl_argument supported_args[], int num_supported_args) |
| 95 | { |
| 96 | bcmos_bool parm_set[num_supported_args]; |
| 97 | bcmos_errno rc = BCM_ERR_OK; |
| 98 | int i; |
| 99 | |
| 100 | memset(parm_set, 0, sizeof(parm_set)); |
| 101 | for (i = 1; i < argc; i++) |
| 102 | { |
| 103 | const cl_argument *opt = cl_parm_get(argv[i], supported_args, num_supported_args); |
| 104 | if (opt == NULL) |
| 105 | { |
| 106 | bcmos_printf("*** Invalid parameter: %s\n", argv[i]); |
| 107 | rc = BCM_ERR_PARM; |
| 108 | break; |
| 109 | } |
| 110 | if (opt->extra_arg) |
| 111 | { |
| 112 | ++i; |
| 113 | if (i >= argc) |
| 114 | { |
| 115 | bcmos_printf("*** Argument is missing after %s\n", argv[i-1]); |
| 116 | rc = BCM_ERR_PARM; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | parm_set[opt - supported_args] = BCMOS_TRUE; |
| 121 | } |
| 122 | |
| 123 | /* Make sure that all mandatory parameters are set */ |
| 124 | for (i = 0; i < num_supported_args; i++) |
| 125 | { |
| 126 | const cl_argument *opt = &supported_args[i]; |
| 127 | if ((opt->flags & CL_ARGUMENT_FLAG_MANDATORY) && !parm_set[i]) |
| 128 | { |
| 129 | bcmos_printf("*** Mandatory parameter %s is missing\n", opt->long_name ? opt->long_name : opt->short_name); |
| 130 | rc = BCM_ERR_PARM; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | return rc; |
| 135 | } |