Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 8 | #include <getopt.h> |
| 9 | #include <stddef.h> |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 11 | #include <cmd_opt.h> |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 12 | #include "debug.h" |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 13 | |
| 14 | /* Command line options */ |
| 15 | static struct option long_opt[CMD_OPT_MAX_NUM+1]; |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 16 | static const char *help_msg[CMD_OPT_MAX_NUM+1]; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 17 | static int num_reg_opt; |
| 18 | |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 19 | void cmd_opt_add(const cmd_opt_t *cmd_opt) |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 20 | { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 21 | assert(cmd_opt != NULL); |
| 22 | |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 23 | if (num_reg_opt >= CMD_OPT_MAX_NUM) { |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 24 | ERROR("Out of memory. Please increase CMD_OPT_MAX_NUM\n"); |
| 25 | exit(1); |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 26 | } |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 27 | |
| 28 | long_opt[num_reg_opt].name = cmd_opt->long_opt.name; |
| 29 | long_opt[num_reg_opt].has_arg = cmd_opt->long_opt.has_arg; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 30 | long_opt[num_reg_opt].flag = 0; |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 31 | long_opt[num_reg_opt].val = cmd_opt->long_opt.val; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 32 | |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 33 | help_msg[num_reg_opt] = cmd_opt->help_msg; |
| 34 | |
| 35 | num_reg_opt++; |
Juan Castillo | 1218dd5 | 2015-07-03 16:23:16 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | const struct option *cmd_opt_get_array(void) |
| 39 | { |
| 40 | return long_opt; |
| 41 | } |
| 42 | |
| 43 | const char *cmd_opt_get_name(int idx) |
| 44 | { |
| 45 | if (idx >= num_reg_opt) { |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | return long_opt[idx].name; |
| 50 | } |
Juan Castillo | 212f738 | 2015-12-15 16:37:57 +0000 | [diff] [blame] | 51 | |
| 52 | const char *cmd_opt_get_help_msg(int idx) |
| 53 | { |
| 54 | if (idx >= num_reg_opt) { |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | return help_msg[idx]; |
| 59 | } |