-
Notifications
You must be signed in to change notification settings - Fork 298
/
options.c
215 lines (186 loc) · 6.22 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
/*
* 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 */
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))
{
if(omc_flag[j])
{
warningStreamPrint(LOG_STDOUT, 0, "each command line option can only be used once: %s", argv[i]);
return 1;
}
omc_flag[j] = 1;
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))
{
if(omc_flag[j])
{
warningStreamPrint(LOG_STDOUT, 0, "each command line option can only be used once: %s", argv[i]);
return 1;
}
omc_flag[j] = 1;
omc_flagValue[j] = (char*)getFlagValue(FLAG_NAME[j], 1, argv+i);
i++;
found=1;
#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))
{
if(omc_flag[j])
{
warningStreamPrint(LOG_STDOUT, 0, "each command line option can only be used once: %s", argv[i]);
return 1;
}
omc_flag[j] = 1;
omc_flagValue[j] = (char*)getOption(FLAG_NAME[j], 1, argv+i);
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 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;
}