Skip to content

Commit

Permalink
- revise command line arguments for c-runtime (third part)
Browse files Browse the repository at this point in the history
  - better warnings. If a flag is used twice, we will now report an error. Previously we considered only the first one without any notification.
  - consistence check. If new flags are added without a description, we get an error (during simulation – that should be improved to an error during compilation).
  - it is no longer possible to introduce flags that do not occur in the help-text
  - now all flags (with values) can be used with <-f=value> as well as <-f value>
  - new debug-output to verify used and interpreted command line options (runtime needs to be compiled with #define USE_DEBUG_OUTPUT)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15275 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Feb 22, 2013
1 parent d95eb3a commit 3492485
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 95 deletions.
4 changes: 1 addition & 3 deletions Compiler/runtime/systemimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2095,9 +2095,7 @@ char* System_getSimulationHelpText(int detailed)
if (FLAG_TYPE[i] == FLAG_TYPE_FLAG) {
cur += snprintf(cur, 8191-(buf-cur), "<-%s>\n %s\n", FLAG_NAME[i], desc[i]);
} else if (FLAG_TYPE[i] == FLAG_TYPE_OPTION) {
cur += snprintf(cur, 8191-(buf-cur), "<-%s=value>\n %s\n", FLAG_NAME[i], desc[i]);
} else if (FLAG_TYPE[i] == FLAG_TYPE_FLAG_VALUE) {
cur += snprintf(cur, 8191-(buf-cur), "<-%s value>\n %s\n", FLAG_NAME[i], desc[i]);
cur += snprintf(cur, 8191-(buf-cur), "<-%s=value> or <-%s value>\n %s\n", FLAG_NAME[i], FLAG_NAME[i], desc[i]);
} else {
cur += snprintf(cur, 8191-(buf-cur), "[unknown flag-type] <-%s>\n", FLAG_NAME[i]);
}
Expand Down
119 changes: 105 additions & 14 deletions SimulationRuntime/c/simulation/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,108 @@
#include <string.h>
#include <stdio.h>

int flagSet(const char*, int, char**); /* -f */
int optionSet(const char *option, int argc, char** argv); /* -f=value */
const char* getOption(const char*, int, char **); /* -f=value; returns NULL if not found */
const char* getFlagValue(const char *, int , char **); /* -f value; returns NULL if not found */

int omc_flag[FLAG_MAX];
char *omc_flagValue[FLAG_MAX];

int checkCommandLineArguments(int argc, char **argv)
{
int i,j;
for(i=1; i<argc; ++i)

/* This works not that well - but is probably better than no check */
ASSERT(!strcmp(FLAG_NAME[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_NAME");
ASSERT(!strcmp(FLAG_DESC[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_DESC");
ASSERT(!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
DEBUG(LOG_STDOUT, "used command line options");
INDENT(LOG_STDOUT);
for(i=1; i<argc; ++i)
DEBUG1(LOG_STDOUT, "%s", argv[i]);
RELEASE(LOG_STDOUT);

DEBUG(LOG_STDOUT, "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_TYPE[j] == FLAG_TYPE_FLAG_VALUE) && flagSet(FLAG_NAME[j],1,argv+i) && (++i < argc)) ||
((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && optionSet(FLAG_NAME[j],1,argv+i))) {
if ((FLAG_TYPE[j] == FLAG_TYPE_FLAG) && flagSet(FLAG_NAME[j], 1, argv+i))
{
if(omc_flag[j])
{
WARNING1(LOG_STDOUT, "each command line option can only be used once: %s", argv[i]);
return 1;
}

omc_flag[j] = 1;
found=1;

#ifdef USE_DEBUG_OUTPUT
INDENT(LOG_STDOUT);
DEBUG1(LOG_STDOUT, "-%s", FLAG_NAME[j]);
RELEASE(LOG_STDOUT);
#endif

break;
}
else if((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && flagSet(FLAG_NAME[j], 1, argv+i) && (i+1 < argc))
{
if(omc_flag[j])
{
WARNING1(LOG_STDOUT, "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
INDENT(LOG_STDOUT);
DEBUG2(LOG_STDOUT, "-%s %s", FLAG_NAME[j], omc_flagValue[j]);
RELEASE(LOG_STDOUT);
#endif

break;
}
else if((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && optionSet(FLAG_NAME[j], 1, argv+i))
{
if(omc_flag[j])
{
WARNING1(LOG_STDOUT, "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
INDENT(LOG_STDOUT);
DEBUG2(LOG_STDOUT, "-%s=%s", FLAG_NAME[j], omc_flagValue[j]);
RELEASE(LOG_STDOUT);
#endif
break;
}
}
if (!found) {

if(!found)
{
WARNING1(LOG_STDOUT, "invalid command line option: %s", argv[i]);
return 1;
}
Expand All @@ -61,27 +147,32 @@ int checkCommandLineArguments(int argc, char **argv)
int flagSet(const char *option, int argc, char** argv)
{
int i;
for (i=0; i<argc;i++)
for(i=0; i<argc; i++)
{
if (argv[i][0] == '-' && 0==strcmp(option,argv[i]+1))
if((argv[i][0] == '-') && (0 == strcmp(option, argv[i]+1)))
return 1;
}
return 0;
}

int helpFlagSet(int argc, char** argv)
{
return flagSet("?", argc, argv) || flagSet("help", argc, argv);
}

int optionSet(const char *option, int argc, char** argv)
{
return getOption(option,argc,argv) != NULL;
return getOption(option, argc, argv) != NULL;
}

/* returns the value of a flag on the form -flagname=value */
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;
}
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;
}
Expand All @@ -90,9 +181,9 @@ const char* getOption(const char *option, int argc, char **argv)
const char* getFlagValue(const char *option, int argc, char **argv)
{
int i;
for(i=0; i<argc-1;i++)
for(i=0; i<argc; i++)
{
if (argv[i][0] == '-' && 0==strcmp(option,argv[i]+1))
if((argv[i][0] == '-') && (0 == strcmp(option, argv[i]+1)))
return argv[i+1];
}
return NULL;
Expand Down
13 changes: 7 additions & 6 deletions SimulationRuntime/c/simulation/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@
#include "simulation_options.h"

#ifdef __cplusplus
extern "C" {
extern "C" {
#endif

extern int omc_flag[FLAG_MAX];
extern char *omc_flagValue[FLAG_MAX];

int helpFlagSet(int argc, char** argv);
int checkCommandLineArguments(int argc, char **argv);

int flagSet(const char*, int, char**); /* -f */
int optionSet(const char *option, int argc, char** argv); /* -f=value */
const char* getOption(const char*, int, char **); /* -f=value; returns NULL if not found */
const char* getFlagValue(const char *, int , char **); /* -f value; returns NULL if not found */
#ifdef __cplusplus
}
}
#endif

#endif
8 changes: 4 additions & 4 deletions SimulationRuntime/c/simulation/simulation_input_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ void read_input_xml(int argc, char **argv,
std::map<std::string, modelica_integer>::iterator it, itParam;

/* read the filename from the command line (if any) */
if (optionSet("f",argc,argv)) {
filename = getOption("f",argc,argv);
if(omc_flag[FLAG_F]) {
filename = omc_flagValue[FLAG_F];
} else {
/* no file given on the command line? use the default */
filename = string(modelData->modelFilePrefix)+"_init.xml"; /* model_name defined in generated code for model.*/
Expand Down Expand Up @@ -283,8 +283,8 @@ void read_input_xml(int argc, char **argv,
}

// deal with override
const char* override = getFlagValue("override", argc, argv);
const char* overrideFile = getFlagValue("overrideFile", argc, argv);
const char* override = omc_flagValue[FLAG_OVERRIDE];
const char* overrideFile = omc_flagValue[FLAG_OVERRIDE_FILE];
doOverride(mi, modelData, override, overrideFile);

/* read all the DefaultExperiment values */
Expand Down

0 comments on commit 3492485

Please sign in to comment.