Skip to content

Commit

Permalink
cli(tcl): pass argumets down to tcl only if -- opt used
Browse files Browse the repository at this point in the history
  • Loading branch information
anonkey committed Oct 11, 2023
1 parent 7c1ca1a commit f1cbcfd
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions kernel/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,39 @@
# include <unistd.h>
#else
char *optarg;
int optind = 1, optcur = 1;
int optind = 1, optcur = 1, optopt = 0;
int getopt(int argc, char **argv, const char *optstring)
{
if (optind >= argc || argv[optind][0] != '-')
return -1;

bool takes_arg = false;
int opt = argv[optind][optcur];
optopt = argv[optind][optcur];

if (optopt == '-') {
optind++;
return -1;
}

for (int i = 0; optstring[i]; i++)
if (opt == optstring[i] && optstring[i + 1] == ':')
if (optopt == optstring[i] && optstring[i + 1] == ':')
takes_arg = true;

if (!takes_arg) {
if (argv[optind][++optcur] == 0)
optind++, optcur = 1;
return opt;
return optopt;
}

if (argv[optind][++optcur]) {
optarg = argv[optind++] + optcur;
optcur = 1;
return opt;
return optopt;
}

optarg = argv[++optind];
optind++, optcur = 1;
return opt;
return optopt;
}
#endif

Expand Down Expand Up @@ -225,6 +231,7 @@ int main(int argc, char **argv)
std::string depsfile = "";
std::string topmodule = "";
std::string perffile = "";
bool skip_args_left = false;
bool scriptfile_tcl = false;
bool print_banner = true;
bool print_stats = true;
Expand Down Expand Up @@ -519,6 +526,7 @@ int main(int argc, char **argv)
exit(1);
}
}
skip_args_left = optopt == '-';

if (log_errfile == NULL) {
log_files.push_back(stdout);
Expand Down Expand Up @@ -566,7 +574,7 @@ int main(int argc, char **argv)
run_pass(vdef_cmd);
}

if (!scriptfile_tcl) {
if (!(scriptfile_tcl && skip_args_left)) {
while (optind < argc)
if (run_frontend(argv[optind++], frontend_command))
run_shell = false;
Expand All @@ -578,11 +586,13 @@ int main(int argc, char **argv)
if (!scriptfile.empty()) {
if (scriptfile_tcl) {
#ifdef YOSYS_ENABLE_TCL
int tcl_argc = argc - optind;
int tcl_argc = skip_args_left ? argc - optind : 0;
std::vector<Tcl_Obj*> script_args;
Tcl_Interp *interp = yosys_get_tcl_interp();
for (int i = optind; i < argc; ++i)
script_args.push_back(Tcl_NewStringObj(argv[i], strlen(argv[i])));
if (skip_args_left) {
for (int i = optind; i < argc; ++i)
script_args.push_back(Tcl_NewStringObj(argv[i], strlen(argv[i])));
}

Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argc", 4), NULL, Tcl_NewIntObj(tcl_argc), 0);
Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv", 4), NULL, Tcl_NewListObj(tcl_argc, script_args.data()), 0);
Expand Down

0 comments on commit f1cbcfd

Please sign in to comment.