using Php;

namespace PhpNative {
    public string shell_exec (string command) {
        string? standard_output = null;
        string? standard_error = null;
        int exit_status = 0;
        try {
            string[] argv = { "/bin/sh", "-c", command };
            Process.spawn_sync (null, argv, null, SpawnFlags.SEARCH_PATH, null, out standard_output, out standard_error, out exit_status);
        } catch (Error e) {
            return "";
        }

        if (standard_output == null) {
            return "";
        }

        return standard_output;
    }

    public string shell_exec_static (string command, string php_file, int php_line) throws Error {
        return shell_exec (command);
    }

    public zval shell_exec_zval (zval command, string php_file, int php_line) throws Error {
        return zval.string_value (shell_exec_static (zval_to_string (command), php_file, php_line));
    }
}
