Refactor runCommand

This commit is contained in:
Yuriy Skalko 2020-09-11 00:22:55 +03:00 committed by Jean-Marc Lasgouttes
parent c1f8c6c26d
commit fb7b7e5223
7 changed files with 26 additions and 21 deletions

View File

@ -1357,7 +1357,7 @@ Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn,
LYXERR(Debug::INFO, "Running '" << command_str << '\''); LYXERR(Debug::INFO, "Running '" << command_str << '\'');
cmd_ret const ret = runCommand(command_str); cmd_ret const ret = runCommand(command_str);
if (ret.first != 0) { if (!ret.valid) {
if (from_format < LYX_FORMAT) { if (from_format < LYX_FORMAT) {
Alert::error(_("Conversion script failed"), Alert::error(_("Conversion script failed"),
bformat(_("%1$s is from an older version" bformat(_("%1$s is from an older version"

View File

@ -92,7 +92,7 @@ bool layout2layout(FileName const & filename, FileName const & tempfile,
LYXERR(Debug::TCLASS, "Running `" << command_str << '\''); LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
cmd_ret const ret = runCommand(command_str); cmd_ret const ret = runCommand(command_str);
if (ret.first != 0) { if (!ret.valid) {
if (format == LAYOUT_FORMAT) if (format == LAYOUT_FORMAT)
LYXERR0("Conversion of layout with layout2layout.py has failed."); LYXERR0("Conversion of layout with layout2layout.py has failed.");
return false; return false;

View File

@ -1026,7 +1026,7 @@ namespace {
<< "\ninput: '" << data << "'" << endl; << "\ninput: '" << data << "'" << endl;
cmd_ret const ret = runCommand(command); cmd_ret const ret = runCommand(command);
cas_tmpfile.removeFile(); cas_tmpfile.removeFile();
return ret.second; return ret.result;
} }
size_t get_matching_brace(string const & str, size_t i) size_t get_matching_brace(string const & str, size_t i)

View File

@ -1095,38 +1095,40 @@ cmd_ret const runCommand(string const & cmd)
// (Claus Hentschel) Check if popen was successful ;-) // (Claus Hentschel) Check if popen was successful ;-)
if (!inf) { if (!inf) {
lyxerr << "RunCommand:: could not start child process" << endl; lyxerr << "RunCommand:: could not start child process" << endl;
return make_pair(-1, string()); return { false, string() };
} }
string ret; string result;
int c = fgetc(inf); int c = fgetc(inf);
while (c != EOF) { while (c != EOF) {
ret += static_cast<char>(c); result += static_cast<char>(c);
c = fgetc(inf); c = fgetc(inf);
} }
#if defined (_WIN32) #if defined (_WIN32)
WaitForSingleObject(process.hProcess, INFINITE); WaitForSingleObject(process.hProcess, INFINITE);
DWORD pret; DWORD pret;
if (!GetExitCodeProcess(process.hProcess, &pret)) BOOL success = GetExitCodeProcess(process.hProcess, &pret);
pret = -1; bool valid = (pret == 0) && success;
if (!infile.empty()) if (!infile.empty())
CloseHandle(startup.hStdInput); CloseHandle(startup.hStdInput);
CloseHandle(process.hProcess); CloseHandle(process.hProcess);
if (fclose(inf) != 0) if (fclose(inf) != 0)
pret = -1; valid = false;
#elif defined (HAVE_PCLOSE) #elif defined (HAVE_PCLOSE)
int const pret = pclose(inf); int const pret = pclose(inf);
bool const valid = (pret != -1);
#elif defined (HAVE__PCLOSE) #elif defined (HAVE__PCLOSE)
int const pret = _pclose(inf); int const pret = _pclose(inf);
bool const valid = (pret != -1);
#else #else
#error No pclose() function. #error No pclose() function.
#endif #endif
if (pret == -1) if (!valid)
perror("RunCommand:: could not terminate child process"); perror("RunCommand:: could not terminate child process");
return make_pair(pret, ret); return { valid, result };
} }
@ -1174,10 +1176,10 @@ FileName const findtexfile(string const & fil, string const & /*format*/,
cmd_ret const c = runCommand(kpsecmd); cmd_ret const c = runCommand(kpsecmd);
LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n' LYXERR(Debug::LATEX, "kpse status = " << c.valid << '\n'
<< "kpse result = `" << rtrim(c.second, "\n\r") << '\''); << "kpse result = `" << rtrim(c.result, "\n\r") << '\'');
if (c.first != -1) if (c.valid)
return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r")); return FileName(rtrim(to_utf8(from_filesystem8bit(c.result)), "\n\r"));
else else
return FileName(); return FileName();
} }
@ -1223,7 +1225,7 @@ bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfun
LYXERR(Debug::FILES, "Running `" << command_str << '\''); LYXERR(Debug::FILES, "Running `" << command_str << '\'');
cmd_ret const ret = runCommand(command_str); cmd_ret const ret = runCommand(command_str);
if (ret.first != 0) { if (!ret.valid) {
LYXERR0("Could not run file conversion script prefs2prefs.py."); LYXERR0("Could not run file conversion script prefs2prefs.py.");
return false; return false;
} }

View File

@ -328,7 +328,10 @@ bool prefs2prefs(FileName const & filename, FileName const & tempfile,
/// Does file \p file need to be updated by configure.py? /// Does file \p file need to be updated by configure.py?
bool configFileNeedsUpdate(std::string const & file); bool configFileNeedsUpdate(std::string const & file);
typedef std::pair<int, std::string> cmd_ret; struct cmd_ret {
bool valid;
std::string result;
};
cmd_ret const runCommand(std::string const & cmd); cmd_ret const runCommand(std::string const & cmd);

View File

@ -65,7 +65,7 @@ static string const python23_call(string const & binary, bool verbose = false)
smatch sm; smatch sm;
try { try {
static regex const python_reg("\\((\\d*), (\\d*)\\)"); static regex const python_reg("\\((\\d*), (\\d*)\\)");
if (out.first < 0 || !regex_match(out.second, sm, python_reg)) if (!out.valid || !regex_match(out.result, sm, python_reg))
return string(); return string();
} catch(regex_error const & /*e*/) { } catch(regex_error const & /*e*/) {
LYXERR0("Regex error! This should not happen."); LYXERR0("Regex error! This should not happen.");
@ -78,7 +78,7 @@ static string const python23_call(string const & binary, bool verbose = false)
return string(); return string();
if (verbose) if (verbose)
lyxerr << "Found Python " << out.second << "\n"; lyxerr << "Found Python " << out.result << "\n";
// Add the -tt switch so that mixed tab/whitespace // Add the -tt switch so that mixed tab/whitespace
// indentation is an error // indentation is an error
return binary + " -tt"; return binary + " -tt";

View File

@ -229,8 +229,8 @@ void init(int argc, char ** argv[])
// to the outer split which sets cygdrive with its // to the outer split which sets cygdrive with its
// contents until the first blank, discarding the // contents until the first blank, discarding the
// unneeded return value. // unneeded return value.
if (p.first != -1 && prefixIs(p.second, "Prefix")) if (p.valid && prefixIs(p.result, "Prefix"))
split(split(p.second, '\n'), cygdrive, ' '); split(split(p.result, '\n'), cygdrive, ' ');
} }
} }