1 /*
2  * Copyright (C) 2003-2015 FreeIPMI Core Team
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #if HAVE_CONFIG_H
20 #include "config.h"
21 #endif /* HAVE_CONFIG_H */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #if STDC_HEADERS
26 #include <string.h>
27 #endif /* STDC_HEADERS */
28 #if HAVE_ARGP_H
29 #include <argp.h>
30 #else /* !HAVE_ARGP_H */
31 #include "freeipmi-argp.h"
32 #endif /* !HAVE_ARGP_H */
33 #include <assert.h>
34 
35 #include "bmc-info.h"
36 #include "bmc-info-argp.h"
37 
38 #include "freeipmi-portability.h"
39 #include "tool-cmdline-common.h"
40 #include "tool-config-file-common.h"
41 
42 const char *argp_program_version =
43   "bmc-info - " PACKAGE_VERSION "\n"
44   "Copyright (C) 2003-2015 FreeIPMI Core Team\n"
45   "This program is free software; you may redistribute it under the terms of\n"
46   "the GNU General Public License.  This program has absolutely no warranty.";
47 
48 const char *argp_program_bug_address =
49   "<" PACKAGE_BUGREPORT ">";
50 
51 static char cmdline_doc[] =
52   "bmc-info - display BMC information";
53 
54 static char cmdline_args_doc[] = "";
55 
56 static struct argp_option cmdline_options[] =
57   {
58     ARGP_COMMON_OPTIONS_DRIVER,
59     ARGP_COMMON_OPTIONS_INBAND,
60     ARGP_COMMON_OPTIONS_OUTOFBAND_HOSTRANGED,
61     ARGP_COMMON_OPTIONS_AUTHENTICATION_TYPE,
62     ARGP_COMMON_OPTIONS_CIPHER_SUITE_ID,
63     ARGP_COMMON_OPTIONS_PRIVILEGE_LEVEL,
64     ARGP_COMMON_OPTIONS_CONFIG_FILE,
65     ARGP_COMMON_OPTIONS_WORKAROUND_FLAGS,
66     ARGP_COMMON_HOSTRANGED_OPTIONS,
67     ARGP_COMMON_OPTIONS_DEBUG,
68     /* legacy */
69     { "guid", GUID_KEY, NULL, OPTION_HIDDEN,
70       "Display only device guid.", 40},
71     { "get-device-id", GET_DEVICE_ID_KEY, NULL, 0,
72       "Display only device ID information.", 41},
73     { "get-device-guid", GET_DEVICE_GUID_KEY, NULL, 0,
74       "Display only device guid.", 42},
75     { "get-system-guid", GET_SYSTEM_GUID_KEY, NULL, 0,
76       "Display only system guid.", 43},
77     { "get-system-info", GET_SYSTEM_INFO_KEY, NULL, 0,
78       "Display only system information.", 44},
79     { "get-channel-info", GET_CHANNEL_INFO_KEY, NULL, 0,
80       "Display only channel information.", 45},
81     { "interpret-oem-data", INTERPRET_OEM_DATA_KEY, NULL, 0,
82       "Attempt to interpret OEM data.", 46},
83     { NULL, 0, NULL, 0, NULL, 0}
84   };
85 
86 static error_t cmdline_parse (int key, char *arg, struct argp_state *state);
87 
88 static struct argp cmdline_argp = { cmdline_options,
89                                     cmdline_parse,
90                                     cmdline_args_doc,
91                                     cmdline_doc };
92 
93 static struct argp cmdline_config_file_argp = { cmdline_options,
94                                                 cmdline_config_file_parse,
95                                                 cmdline_args_doc,
96                                                 cmdline_doc };
97 
98 static error_t
99 cmdline_parse (int key, char *arg, struct argp_state *state)
100 {
101   struct bmc_info_arguments *cmd_args;
102 
103   assert (state);
104 
105   cmd_args = state->input;
106 
107   switch (key)
108     {
109     case GET_DEVICE_ID_KEY:
110       cmd_args->get_device_id++;
111       break;
112       /* legacy */
113     case GUID_KEY:
114     case GET_DEVICE_GUID_KEY:
115       cmd_args->get_device_guid++;
116       break;
117     case GET_SYSTEM_GUID_KEY:
118       cmd_args->get_system_guid++;
119       break;
120     case GET_SYSTEM_INFO_KEY:
121       cmd_args->get_system_info++;
122       break;
123     case GET_CHANNEL_INFO_KEY:
124       cmd_args->get_channel_info++;
125       break;
126     case INTERPRET_OEM_DATA_KEY:
127       cmd_args->interpret_oem_data = 1;
128       break;
129     case ARGP_KEY_ARG:
130       /* Too many arguments. */
131       argp_usage (state);
132       break;
133     case ARGP_KEY_END:
134       break;
135     default:
136       return (common_parse_opt (key, arg, &(cmd_args->common_args)));
137     }
138 
139   return (0);
140 }
141 
142 static void
143 _bmc_info_config_file_parse (struct bmc_info_arguments *cmd_args)
144 {
145   struct config_file_data_bmc_info config_file_data;
146 
147   assert (cmd_args);
148 
149   memset (&config_file_data,
150           '\0',
151           sizeof (struct config_file_data_bmc_info));
152 
153   if (config_file_parse (cmd_args->common_args.config_file,
154                          0,
155                          &(cmd_args->common_args),
156                          CONFIG_FILE_INBAND | CONFIG_FILE_OUTOFBAND | CONFIG_FILE_HOSTRANGE,
157                          CONFIG_FILE_TOOL_BMC_INFO,
158                          &config_file_data) < 0)
159     {
160       fprintf (stderr, "config_file_parse: %s\n", strerror (errno));
161       exit (EXIT_FAILURE);
162     }
163 
164   if (config_file_data.interpret_oem_data_count)
165     cmd_args->interpret_oem_data = config_file_data.interpret_oem_data;
166 }
167 
168 void
169 bmc_info_argp_parse (int argc, char **argv, struct bmc_info_arguments *cmd_args)
170 {
171   assert (argc >= 0);
172   assert (argv);
173   assert (cmd_args);
174 
175   init_common_cmd_args_user (&(cmd_args->common_args));
176 
177   cmd_args->get_device_id = 0;
178   cmd_args->get_device_guid = 0;
179   cmd_args->get_system_guid = 0;
180   cmd_args->get_system_info = 0;
181   cmd_args->get_channel_info = 0;
182   cmd_args->interpret_oem_data = 0;
183 
184   argp_parse (&cmdline_config_file_argp,
185               argc,
186               argv,
187               ARGP_IN_ORDER, NULL,
188               &(cmd_args->common_args));
189 
190   _bmc_info_config_file_parse (cmd_args);
191 
192   argp_parse (&cmdline_argp,
193               argc,
194               argv,
195               ARGP_IN_ORDER,
196               NULL,
197               cmd_args);
198 
199   verify_common_cmd_args (&(cmd_args->common_args));
200 }
201