aboutsummaryrefslogtreecommitdiff
path: root/src/Exec.cpp
blob: e0ae30646c7e434125f7cc504ac1484a5ec012b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "../include/Exec.hpp"
#include "../include/env.hpp"

using namespace std;

namespace sibs
{
#if OS_FAMILY == OS_FAMILY_POSIX
    Result<ExecResult> exec(const _tinydir_char_t *cmd, bool print)
    {
        char buffer[128];
        std::string result;
        FILE *pipe = popen(cmd, "r");
        if(!pipe)
            return Result<ExecResult>::Err("popen() failed");

        while(!feof(pipe))
        {
            if(fgets(buffer, 128, pipe))
            {
                result += buffer;
                if(print)
                    printf("%s", buffer);
            }
        }

        int processCloseResult = pclose(pipe);
        if(WIFEXITED(processCloseResult))
        {
            int returned = WEXITSTATUS(processCloseResult);
            ExecResult execResult;
            execResult.execStdout = result;
            execResult.exitCode = returned;
            return Result<ExecResult>::Ok(execResult);
        }
        else if(WIFSIGNALED(processCloseResult))
        {
            int signum = WSTOPSIG(processCloseResult);
            string errMsg = "Exited due to receiving signal ";
            errMsg += to_string(signum);
            return Result<ExecResult>::Err(errMsg);
        }
        else if(WIFSTOPPED(processCloseResult))
        {
            int signum = WSTOPSIG(processCloseResult);
            string errMsg = "Stopped due to receiving signal ";
            errMsg += to_string(signum);
            return Result<ExecResult>::Err(errMsg);
        }
        else
        {
            string errMsg = "exec unexpected error on pclose: ";
            errMsg += to_string(processCloseResult);
            return Result<ExecResult>::Err(errMsg);
        }
    }
#else
    // TODO(Windows): Redirect stdout (and stderr) to string
    Result<ExecResult> exec(const _tinydir_char_t *cmd, bool print)
    {
        char buffer[128];
        std::string result;

        STARTUPINFO startupInfo;
        ZeroMemory(&startupInfo, sizeof(startupInfo));
        startupInfo.cb = sizeof(startupInfo);

        PROCESS_INFORMATION processInfo;
        ZeroMemory(&processInfo, sizeof(processInfo));

        DWORD exitCode;
        
        if (!CreateProcess(NULL, (LPWSTR)cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo))
        {
            string errMsg = "exec unexpected error: ";
            errMsg += toUtf8(getLastErrorAsString());
            return Result<ExecResult>::Err(errMsg);
        }

        WaitForSingleObject(processInfo.hProcess, INFINITE);
        GetExitCodeProcess(processInfo.hProcess, &exitCode);
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);

        if (exitCode == 0)
        {
            ExecResult execResult;
            execResult.execStdout = result;
            execResult.exitCode = exitCode;
            return Result<ExecResult>::Ok(execResult);
        }
        else
        {
            string errMsg = "Exited with non-zero exit code (";
            errMsg += to_string(exitCode);
            errMsg += "): ";
            errMsg += toUtf8(getLastErrorAsString());
            return Result<ExecResult>::Err(errMsg);
        }
    }
#endif
}