-
Notifications
You must be signed in to change notification settings - Fork 296
/
options.c
305 lines (254 loc) · 9.45 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
* CONDITIONS OF OSMC-PL.
*
*/
#include "options.h"
#include "../util/omc_error.h"
#include "simulation_runtime.h"
#include <string.h>
#include <stdio.h>
static int flagSet(const char*, int, char**); /* -f */
static int optionSet(const char *option, int argc, char** argv); /* -f=value */
static const char* getOption(const char*, int, char **); /* -f=value; returns NULL if not found */
static const char* getFlagValue(const char *, int , char **); /* -f value; returns NULL if not found */
static int handle_repeated_option(int flag_index, char **argv_loc, int is_sticky);
static int handle_repeated_flag(int flag_index);
int omc_flag[FLAG_MAX];
const char *omc_flagValue[FLAG_MAX];
int helpFlagSet(int argc, char** argv)
{
return flagSet("?", argc, argv) || flagSet("help", argc, argv);
}
#if !defined(OMC_MINIMAL_RUNTIME)
int setLogFormat(int argc, char** argv)
{
const char* value = getOption(FLAG_NAME[FLAG_LOG_FORMAT], argc, argv);
if (NULL == value) {
value = getFlagValue(FLAG_NAME[FLAG_LOG_FORMAT], argc, argv);
}
if (NULL != value) {
if (0 == strcmp(value, "xml")) {
setStreamPrintXML(1);
} else if (0 == strcmp(value, "xmltcp")) {
setStreamPrintXML(2);
} else if (0 == strcmp(value, "text")) {
setStreamPrintXML(0);
} else {
warningStreamPrint(LOG_STDOUT, 0, "invalid command line option: -logFormat=%s, expected text, xml, or xmltcp", value);
return 1;
}
}
return 0;
}
#endif
int checkCommandLineArguments(int argc, char **argv)
{
int i,j;
/* This works not that well - but is probably better than no check */
assertStreamPrint(NULL, !strcmp(FLAG_NAME[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_NAME");
assertStreamPrint(NULL, !strcmp(FLAG_DESC[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_DESC");
assertStreamPrint(NULL, !strcmp(FLAG_DETAILED_DESC[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_DETAILED_DESC");
for(i=0; i<FLAG_MAX; ++i)
{
omc_flag[i] = 0;
omc_flagValue[i] = NULL;
}
#ifdef USE_DEBUG_OUTPUT
debugStreamPrint(LOG_STDOUT, 1, "used command line options");
for(i=1; i<argc; ++i)
debugStreamPrint(LOG_STDOUT, 0, "%s", argv[i]);
messageClose(LOG_STDOUT);
debugStreamPrint(LOG_STDOUT, 1, "interpreted command line options");
#endif
for(i=1; i<argc; ++i)
{
int found=0;
for(j=1; j<FLAG_MAX; ++j)
{
if((FLAG_TYPE[j] == FLAG_TYPE_FLAG) && flagSet(FLAG_NAME[j], 1, argv+i))
{
// Flag is not yet set.
if(!omc_flag[j]) {
omc_flag[j] = 1;
}
// Flag is already specified earlier. Check repetition policy.
else if(!handle_repeated_flag(j)) {
// repetition is invlaid for this Flag
return 1;
}
// All good.
found=1;
#ifdef USE_DEBUG_OUTPUT
debugStreamPrint(LOG_STDOUT, 0, "-%s", FLAG_NAME[j]);
#endif
break;
}
else if((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && flagSet(FLAG_NAME[j], 1, argv+i) && (i+1 < argc))
{
// Option is not yet set.
if(!omc_flag[j]) {
omc_flag[j] = 1;
omc_flagValue[j] = (char*)getFlagValue(FLAG_NAME[j], 1, argv+i);
}
// Option is already specified earlier. Check repetition policy.
else if(!handle_repeated_option(j, argv+i, 0 /*Not sticky*/)) {
// repetition is invlaid for this option
return 1;
}
// All good.
found = 1;
i++;
#ifdef USE_DEBUG_OUTPUT
debugStreamPrint(LOG_STDOUT, 0, "-%s %s", FLAG_NAME[j], omc_flagValue[j]);
#endif
break;
}
else if((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && optionSet(FLAG_NAME[j], 1, argv+i))
{
// Option is not yet set.
if (!omc_flag[j]) {
omc_flag[j] = 1;
omc_flagValue[j] = (char*)getOption(FLAG_NAME[j], 1, argv+i);
}
// Option is already specified earlier. Check repetition policy.
else if (!handle_repeated_option(j, argv+i, 1 /*Sticky*/)) {
// repetition is invlaid for this option
return 1;
}
// All good.
found = 1;
#ifdef USE_DEBUG_OUTPUT
debugStreamPrint(LOG_STDOUT, 0, "-%s=%s", FLAG_NAME[j], omc_flagValue[j]);
#endif
break;
}
}
if(!found)
{
#ifdef USE_DEBUG_OUTPUT
messageClose(LOG_STDOUT);
#endif
warningStreamPrint(LOG_STDOUT, 0, "invalid command line option: %s", argv[i]);
return 1;
}
}
#ifdef USE_DEBUG_OUTPUT
messageClose(LOG_STDOUT);
#endif
return 0;
}
static int handle_repeated_flag(int flag_index) {
const char* flag_name = FLAG_NAME[flag_index];
flag_repeat_policy repeat_policy = FLAG_REPEAT_POLICIES[flag_index];
if(repeat_policy == FLAG_REPEAT_POLICY_IGNORE) {
warningStreamPrint(LOG_STDOUT, 0, "Command line flag '%s' specified again. Ignoring."
, flag_name);
return 1;
}
if(repeat_policy == FLAG_REPEAT_POLICY_FORBID) {
errorStreamPrint(LOG_STDOUT, 0, "Command line flag '%s' can be specified only once.", flag_name);
return 0;
}
if(repeat_policy == FLAG_REPEAT_POLICY_REPLACE) {
errorStreamPrint(LOG_STDOUT, 0, "Command line flag %s is supposed to be replaced on repetition. This option does not apply for flags. Fix the repetition policy for the flag.", flag_name);
return 0;
}
if(repeat_policy == FLAG_REPEAT_POLICY_COMBINE) {
errorStreamPrint(LOG_STDOUT, 0, "Command line flag %s is supposed to be combined on repetition. This option does not apply for flags. Fix the repetition policy for the flag.", flag_name);
return 0;
}
errorStreamPrint(LOG_STDOUT, 0, "Error: Unknow repetition policy for command line flag %s.", flag_name);
return 0;
}
static int handle_repeated_option(int flag_index, char **argv_loc, int is_sticky) {
const char* flag_name = FLAG_NAME[flag_index];
flag_repeat_policy repeat_policy = FLAG_REPEAT_POLICIES[flag_index];
const char* old_value = omc_flagValue[flag_index];
if(repeat_policy == FLAG_REPEAT_POLICY_IGNORE) {
warningStreamPrint(LOG_STDOUT, 0, "Command line option '%s' specified again. Keeping the first value '%s' and ignoring the rest."
, flag_name, old_value);
return 1;
}
if(repeat_policy == FLAG_REPEAT_POLICY_FORBID) {
errorStreamPrint(LOG_STDOUT, 0, "Command line option '%s' can be specified only once.", flag_name);
return 0;
}
const char* new_value;
if(is_sticky) // lv=LOG_STATS
new_value = (char*)getOption(flag_name, 1, argv_loc);
else // lv LOG_STATS
new_value = (char*)getFlagValue(flag_name, 1, argv_loc);
if(repeat_policy == FLAG_REPEAT_POLICY_REPLACE) {
omc_flagValue[flag_index] = new_value;
warningStreamPrint(LOG_STDOUT, 0, "Command line option '%s' specified again. Value has been overriden from '%s' to '%s'."
, flag_name, old_value, new_value);
return 1;
}
if(repeat_policy == FLAG_REPEAT_POLICY_COMBINE) {
errorStreamPrint(LOG_STDOUT, 0, "Command line option %s is supposed to be combined on repetition. This has not bee implemented yet", flag_name);
return 0;
}
errorStreamPrint(LOG_STDOUT, 0, "Error: Unknow repetition policy for command line option %s.", flag_name);
return 0;
}
static int flagSet(const char *option, int argc, char** argv)
{
int i;
for(i=0; i<argc; i++) {
if((argv[i][0] == '-') && (0 == strcmp(option, argv[i]+1))) {
return 1;
}
}
return 0;
}
static int optionSet(const char *option, int argc, char** argv)
{
return getOption(option, argc, argv) != NULL;
}
/* returns the value of a flag on the form -flagname=value */
static const char* getOption(const char *option, int argc, char **argv)
{
int optLen = strlen(option), i;
for(i=0; i<argc; i++) {
if((argv[i][0] == '-') && (0 == strncmp(option, argv[i]+1, optLen)) && (argv[i][optLen+1] == '=')) {
return argv[i] + optLen + 2;
}
}
return NULL;
}
/* returns the value of a flag on the form -flagname value */
static const char* getFlagValue(const char *option, int argc, char **argv)
{
int i;
for(i=0; i<argc; i++) {
if((argv[i][0] == '-') && (0 == strcmp(option, argv[i]+1))) {
return argv[i+1];
}
}
return NULL;
}