blob: 64180d1f50af89a01a7fd745c6fd0e742471febe [file] [log] [blame]
Juan Castillo1218dd52015-07-03 16:23:16 +01001/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo1218dd52015-07-03 16:23:16 +01005 */
6
Juan Castillo212f7382015-12-15 16:37:57 +00007#include <assert.h>
Isla Mitchell99305012017-07-11 14:54:08 +01008#include <cmd_opt.h>
Juan Castillo1218dd52015-07-03 16:23:16 +01009#include <getopt.h>
10#include <stddef.h>
Juan Castillo212f7382015-12-15 16:37:57 +000011#include <stdlib.h>
Juan Castillo212f7382015-12-15 16:37:57 +000012#include "debug.h"
Juan Castillo1218dd52015-07-03 16:23:16 +010013
14/* Command line options */
15static struct option long_opt[CMD_OPT_MAX_NUM+1];
Juan Castillo212f7382015-12-15 16:37:57 +000016static const char *help_msg[CMD_OPT_MAX_NUM+1];
Juan Castillo1218dd52015-07-03 16:23:16 +010017static int num_reg_opt;
18
Juan Castillo212f7382015-12-15 16:37:57 +000019void cmd_opt_add(const cmd_opt_t *cmd_opt)
Juan Castillo1218dd52015-07-03 16:23:16 +010020{
Juan Castillo212f7382015-12-15 16:37:57 +000021 assert(cmd_opt != NULL);
22
Juan Castillo1218dd52015-07-03 16:23:16 +010023 if (num_reg_opt >= CMD_OPT_MAX_NUM) {
Juan Castillo212f7382015-12-15 16:37:57 +000024 ERROR("Out of memory. Please increase CMD_OPT_MAX_NUM\n");
25 exit(1);
Juan Castillo1218dd52015-07-03 16:23:16 +010026 }
Juan Castillo212f7382015-12-15 16:37:57 +000027
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 Castillo1218dd52015-07-03 16:23:16 +010030 long_opt[num_reg_opt].flag = 0;
Juan Castillo212f7382015-12-15 16:37:57 +000031 long_opt[num_reg_opt].val = cmd_opt->long_opt.val;
Juan Castillo1218dd52015-07-03 16:23:16 +010032
Juan Castillo212f7382015-12-15 16:37:57 +000033 help_msg[num_reg_opt] = cmd_opt->help_msg;
34
35 num_reg_opt++;
Juan Castillo1218dd52015-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 Castillo212f7382015-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}