1 /* 2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <cmd_opt.h> 9 #include <getopt.h> 10 #include <stddef.h> 11 #include <stdlib.h> 12 #include "debug.h" 13 14 /* Command line options */ 15 static struct option long_opt[CMD_OPT_MAX_NUM+1]; 16 static const char *help_msg[CMD_OPT_MAX_NUM+1]; 17 static int num_reg_opt; 18 cmd_opt_add(const cmd_opt_t * cmd_opt)19void cmd_opt_add(const cmd_opt_t *cmd_opt) 20 { 21 assert(cmd_opt != NULL); 22 23 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++; 36 } 37 cmd_opt_get_array(void)38const struct option *cmd_opt_get_array(void) 39 { 40 return long_opt; 41 } 42 cmd_opt_get_name(int idx)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 } 51 cmd_opt_get_help_msg(int idx)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 } 60