summaryrefslogtreecommitdiffstats
path: root/src/utils/cmd.cc
blob: a9dba49bbbb5a5a5346e2779abdec3afb207cba9 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# include "cmd.hh"
# include "astroid.hh"
# include "vector_utils.hh"
# include "config.hh"

# include <glibmm.h>
# include <boost/filesystem.hpp>

using std::endl;
using std::string;

namespace bfs = boost::filesystem;

namespace Astroid {
  Cmd::Cmd (ustring _prefix, ustring _cmd, ustring _undo_cmd) : Cmd (_cmd, _undo_cmd) {
    prefix = _prefix + ": ";
  }

  Cmd::Cmd (ustring _cmd, ustring _undo_cmd) {
    cmd = substitute (_cmd);
    undo_cmd = substitute (_undo_cmd);
  }

  Cmd::Cmd () {

  }

  int Cmd::run () {
    return execute (false);
  }

  bool Cmd::undoable () {
    return !undo_cmd.empty ();
  }

  int Cmd::undo () {
    if (!undoable ()) {
      LOG (error) << "cmd: tried to undo non-undoable command: " << cmd;
      return 1;
    }
    return execute (true);
  }

  int Cmd::execute (bool undo) {
    ustring c = (!undo ? cmd : undo_cmd);

    LOG (info) << "cmd: running: " << c;
    int exit;
    string _stdout, _stderr;

    string _cmd = c;
    try {
      Glib::spawn_command_line_sync (_cmd, &_stdout, &_stderr, &exit);
    } catch (Glib::SpawnError &ex) {
      if (exit == 0) exit = -1;
      LOG (error) << "cmd: " << prefix << "failed to execute: '" << _cmd << "': " << ex.what ();
    }

    if (!_stdout.empty ()) {
      for (auto &l : VectorUtils::split_and_trim (_stdout, "\n")) {
        if (!l.empty ()) LOG (debug) << "cmd: " << prefix << l;
      }
    }

    if (!_stderr.empty ()) {
      for (auto &l : VectorUtils::split_and_trim (_stderr, "\n")) {
        if (!l.empty ()) LOG (error) << "cmd: " << prefix << l;
      }
    }

    return (exit == 0);
  }


  ustring Cmd::substitute (const ustring _cmd) {
    ustring ncmd = _cmd;

    ustring key = "hooks::";
    std::size_t fnd = ncmd.find (key);
    if (fnd != std::string::npos) {
      ncmd.replace (fnd, key.length (), (astroid->standard_paths ().config_dir / bfs::path("hooks/")).c_str ());
    }

    return ncmd;
  }

  bool Cmd::pipe (ustring cmd, const ustring& _stdin, ustring& _stdout, ustring &_stderr) {
    LOG (info) << "cmd: running: " << cmd;

    try {
      int pid;
      int stdin;
      int stdout;
      int stderr;
      std::vector<std::string> args = Glib::shell_parse_argv (cmd);

      Glib::spawn_async_with_pipes ("",
          args,
          Glib::SPAWN_DO_NOT_REAP_CHILD |
          Glib::SPAWN_SEARCH_PATH,
          sigc::slot <void> (),
          &pid,
          &stdin,
          &stdout,
          &stderr
          );

      refptr<Glib::IOChannel> ch_stdin;
      refptr<Glib::IOChannel> ch_stdout;
      refptr<Glib::IOChannel> ch_stderr;
      ch_stdin  = Glib::IOChannel::create_from_fd (stdin);
      ch_stdout = Glib::IOChannel::create_from_fd (stdout);
      ch_stderr = Glib::IOChannel::create_from_fd (stderr);

      ch_stdin->write (_stdin);
      ch_stdin->close ();

      ch_stderr->read_to_end (_stderr);
      ch_stderr->close ();

      if (!_stderr.empty ()) {
        LOG (error) << "cmd: " << _stderr;
      }

      ch_stdout->read_to_end (_stdout);
      ch_stdout->close ();

      g_spawn_close_pid (pid);

    } catch (Glib::SpawnError &ex) {
      LOG (error) << "cmd: failed to execute: '" << cmd << "': " << ex.what ();
      return false;
    }


    return true;
  }
}