]> git.lyx.org Git - lyx.git/commitdiff
Refactor runCommand
authorYuriy Skalko <yuriy.skalko@gmail.com>
Thu, 10 Sep 2020 21:22:55 +0000 (00:22 +0300)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 11 Sep 2020 14:13:23 +0000 (16:13 +0200)
src/Buffer.cpp
src/TextClass.cpp
src/mathed/MathExtern.cpp
src/support/filetools.cpp
src/support/filetools.h
src/support/os.cpp
src/support/os_win32.cpp

index 9de281daf35a5ed505541b6f1482664e8b759727..82fd0899c427eea3a5530375dc9811ca89dc5f91 100644 (file)
@@ -1357,7 +1357,7 @@ Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn,
        LYXERR(Debug::INFO, "Running '" << command_str << '\'');
 
        cmd_ret const ret = runCommand(command_str);
-       if (ret.first != 0) {
+       if (!ret.valid) {
                if (from_format < LYX_FORMAT) {
                        Alert::error(_("Conversion script failed"),
                                bformat(_("%1$s is from an older version"
index eb92612d847b6d9bad0ead06aa018324b53164fe..4cc5eba0edf59d7832585af6733fcaeae7ae58b6 100644 (file)
@@ -92,7 +92,7 @@ bool layout2layout(FileName const & filename, FileName const & tempfile,
        LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
 
        cmd_ret const ret = runCommand(command_str);
-       if (ret.first != 0) {
+       if (!ret.valid) {
                if (format == LAYOUT_FORMAT)
                        LYXERR0("Conversion of layout with layout2layout.py has failed.");
                return false;
index 311b32389533edb494223d0c3ef8a01f3737b8ba..18340e11c89eb46944d4730c4cad355a2d29e459 100644 (file)
@@ -1026,7 +1026,7 @@ namespace {
                       << "\ninput: '" << data << "'" << endl;
                cmd_ret const ret = runCommand(command);
                cas_tmpfile.removeFile();
-               return ret.second;
+               return ret.result;
        }
 
        size_t get_matching_brace(string const & str, size_t i)
index d6b27856cb81df2876fdb9f484768d27989f0312..151d78c56841fc03cd3eecac822cfa31aff072b3 100644 (file)
@@ -1095,38 +1095,40 @@ cmd_ret const runCommand(string const & cmd)
        // (Claus Hentschel) Check if popen was successful ;-)
        if (!inf) {
                lyxerr << "RunCommand:: could not start child process" << endl;
-               return make_pair(-1, string());
+               return { false, string() };
        }
 
-       string ret;
+       string result;
        int c = fgetc(inf);
        while (c != EOF) {
-               ret += static_cast<char>(c);
+               result += static_cast<char>(c);
                c = fgetc(inf);
        }
 
 #if defined (_WIN32)
        WaitForSingleObject(process.hProcess, INFINITE);
        DWORD pret;
-       if (!GetExitCodeProcess(process.hProcess, &pret))
-               pret = -1;
+       BOOL success = GetExitCodeProcess(process.hProcess, &pret);
+       bool valid = (pret == 0) && success;
        if (!infile.empty())
                CloseHandle(startup.hStdInput);
        CloseHandle(process.hProcess);
        if (fclose(inf) != 0)
-               pret = -1;
+               valid = false;
 #elif defined (HAVE_PCLOSE)
        int const pret = pclose(inf);
+       bool const valid = (pret != -1);
 #elif defined (HAVE__PCLOSE)
        int const pret = _pclose(inf);
+       bool const valid = (pret != -1);
 #else
 #error No pclose() function.
 #endif
 
-       if (pret == -1)
+       if (!valid)
                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);
 
-       LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
-                << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
-       if (c.first != -1)
-               return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
+       LYXERR(Debug::LATEX, "kpse status = " << c.valid << '\n'
+                << "kpse result = `" << rtrim(c.result, "\n\r") << '\'');
+       if (c.valid)
+               return FileName(rtrim(to_utf8(from_filesystem8bit(c.result)), "\n\r"));
        else
                return FileName();
 }
@@ -1223,7 +1225,7 @@ bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfun
        LYXERR(Debug::FILES, "Running `" << 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.");
                return false;
        }
index b0f5f7f3797f24fbb72c3282d73723bb7e0d9bea..e66af9972f7e0b4cb17ba9fa0c35b09b8006ce1c 100644 (file)
@@ -328,7 +328,10 @@ bool prefs2prefs(FileName const & filename, FileName const & tempfile,
 /// Does file \p file need to be updated by configure.py?
 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);
 
index 616b9c6936d4a16594f239fbb2dc1d2332db1358..3a5c2e59a25eaf4e04032864e5df607a0147a78c 100644 (file)
@@ -65,7 +65,7 @@ static string const python23_call(string const & binary, bool verbose = false)
        smatch sm;
        try {
                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();
        } catch(regex_error const & /*e*/) {
                LYXERR0("Regex error! This should not happen.");
@@ -78,7 +78,7 @@ static string const python23_call(string const & binary, bool verbose = false)
                return string();
 
        if (verbose)
-               lyxerr << "Found Python " << out.second << "\n";
+               lyxerr << "Found Python " << out.result << "\n";
        // Add the -tt switch so that mixed tab/whitespace
        // indentation is an error
        return binary + " -tt";
index e4f9ebd0fcbd9a15405f082545bd3b2f108a3e7a..536d2a64460d07854c2b1900e2776a027d3b8b39 100644 (file)
@@ -229,8 +229,8 @@ void init(int argc, char ** argv[])
                        // to the outer split which sets cygdrive with its
                        // contents until the first blank, discarding the
                        // unneeded return value.
-                       if (p.first != -1 && prefixIs(p.second, "Prefix"))
-                               split(split(p.second, '\n'), cygdrive, ' ');
+                       if (p.valid && prefixIs(p.result, "Prefix"))
+                               split(split(p.result, '\n'), cygdrive, ' ');
                }
        }