3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-04-07 01:54:10 +00:00

Using next_token() to parse commands

This commit is contained in:
Clifford Wolf 2014-10-10 18:53:03 +02:00
parent 20d85f20db
commit df537a216b

View file

@ -146,35 +146,34 @@ void Pass::extra_args(std::vector<std::string> args, size_t argidx, RTLIL::Desig
void Pass::call(RTLIL::Design *design, std::string command) void Pass::call(RTLIL::Design *design, std::string command)
{ {
std::vector<std::string> args; std::vector<std::string> args;
char *s = strdup(command.c_str()), *sstart = s, *saveptr;
s += strspn(s, " \t\r\n"); std::string cmd_buf = command;
if (*s == 0 || *s == '#') { std::string tok = next_token(cmd_buf, " \t\r\n");
free(sstart);
if (tok.empty() || tok[0] == '#')
return; return;
}
if (*s == '!') { if (tok[0] == '!') {
for (s++; *s == ' ' || *s == '\t'; s++) { } cmd_buf = command.substr(command.find('!') + 1);
char *p = s + strlen(s) - 1; while (!cmd_buf.empty() && (cmd_buf.back() == ' ' || cmd_buf.back() == '\t' ||
while (p >= s && (*p == '\r' || *p == '\n')) cmd_buf.back() == '\r' || cmd_buf.back() == '\n'))
*(p--) = 0; cmd_buf.resize(cmd_buf.size()-1);
log_header("Shell command: %s\n", s); log_header("Shell command: %s\n", cmd_buf.c_str());
int retCode = system(s); int retCode = system(cmd_buf.c_str());
if (retCode != 0) if (retCode != 0)
log_cmd_error("Shell command returned error code %d.\n", retCode); log_cmd_error("Shell command returned error code %d.\n", retCode);
free(sstart);
return; return;
} }
for (char *p = strtok_r(s, " \t\r\n", &saveptr); p; p = strtok_r(NULL, " \t\r\n", &saveptr)) {
std::string str = p; while (!tok.empty()) {
int strsz = str.size(); if (tok == "#")
if (str == "#")
break; break;
if (strsz > 0 && str[strsz-1] == ';') { if (tok.back() == ';') {
int num_semikolon = 0; int num_semikolon = 0;
while (strsz > 0 && str[strsz-1] == ';') while (!tok.empty() && tok.back() == ';')
strsz--, num_semikolon++; tok.resize(tok.size()-1), num_semikolon++;
if (strsz > 0) if (!tok.empty())
args.push_back(str.substr(0, strsz)); args.push_back(tok);
call(design, args); call(design, args);
args.clear(); args.clear();
if (num_semikolon == 2) if (num_semikolon == 2)
@ -182,9 +181,10 @@ void Pass::call(RTLIL::Design *design, std::string command)
if (num_semikolon == 3) if (num_semikolon == 3)
call(design, "clean -purge"); call(design, "clean -purge");
} else } else
args.push_back(str); args.push_back(tok);
tok = next_token(cmd_buf, " \t\r\n");
} }
free(sstart);
call(design, args); call(design, args);
} }