blob: 64180d1f50af89a01a7fd745c6fd0e742471febe [file] [log] [blame]
Juan Castilload2c1a92015-07-03 16:23:16 +01001/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castilload2c1a92015-07-03 16:23:16 +01005 */
6
Juan Castillo159807e2015-12-15 16:37:57 +00007#include <assert.h>
Isla Mitchell2a4b4b712017-07-11 14:54:08 +01008#include <cmd_opt.h>
Juan Castilload2c1a92015-07-03 16:23:16 +01009#include <getopt.h>
10#include <stddef.h>
Juan Castillo159807e2015-12-15 16:37:57 +000011#include <stdlib.h>
Juan Castillo159807e2015-12-15 16:37:57 +000012#include "debug.h"
Juan Castilload2c1a92015-07-03 16:23:16 +010013
14/* Command line options */
15static struct option long_opt[CMD_OPT_MAX_NUM+1];
Juan Castillo159807e2015-12-15 16:37:57 +000016static const char *help_msg[CMD_OPT_MAX_NUM+1];
Juan Castilload2c1a92015-07-03 16:23:16 +010017static int num_reg_opt;
18
Juan Castillo159807e2015-12-15 16:37:57 +000019void cmd_opt_add(const cmd_opt_t *cmd_opt)
Juan Castilload2c1a92015-07-03 16:23:16 +010020{
Juan Castillo159807e2015-12-15 16:37:57 +000021 assert(cmd_opt != NULL);
Juan Castilload2c1a92015-07-03 16:23:16 +010022
Juan Castillo159807e2015-12-15 16:37:57 +000023 if (num_reg_opt >= CMD_OPT_MAX_NUM) {
24 ERROR("Out of memory. Please increase CMD_OPT_MAX_NUM\n");
25 exit(1);
26 }
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;
30 long_opt[num_reg_opt].flag = 0;
31 long_opt[num_reg_opt].val = cmd_opt->long_opt.val;
32
33 help_msg[num_reg_opt] = cmd_opt->help_msg;
34
35 num_reg_opt++;
Juan Castilload2c1a92015-07-03 16:23:16 +010036}
37
38const struct option *cmd_opt_get_array(void)
39{
40 return long_opt;
41}
42
43const 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 Castillo159807e2015-12-15 16:37:57 +000051
52const 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}