~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to libphutil/src/future/exec/execx.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Execute a command and capture stdout and stderr. If the command exits with
 
5
 * a nonzero error code, a @{class:CommandException} will be thrown. If you need
 
6
 * to manually handle error conditions, use @{function:exec_manual}.
 
7
 *
 
8
 *   list ($stdout, $stderr) = execx('ls %s', $file);
 
9
 *
 
10
 * @param  string  sprintf()-style command pattern to execute.
 
11
 * @param  ...     Arguments to sprintf pattern.
 
12
 * @return array   List of stdout and stderr.
 
13
 */
 
14
function execx($cmd /* , ... */) {
 
15
  $args = func_get_args();
 
16
  $future = newv('ExecFuture', $args);
 
17
  return $future->resolvex();
 
18
}
 
19
 
 
20
 
 
21
/**
 
22
 * Execute a command and capture stdout, stderr, and the return value.
 
23
 *
 
24
 *   list ($err, $stdout, $stderr) = exec_manual('ls %s', $file);
 
25
 *
 
26
 * When invoking this function, you must manually handle the error condition.
 
27
 * Error flows can often be simplified by using @{function:execx} instead,
 
28
 * which throws an exception when it encounters an error.
 
29
 *
 
30
 * @param  string  sprintf()-style command pattern to execute.
 
31
 * @param  ...     Arguments to sprintf pattern.
 
32
 * @return array   List of return code, stdout, and stderr.
 
33
 */
 
34
function exec_manual($cmd /* , ... */) {
 
35
  $args = func_get_args();
 
36
  $ef = newv('ExecFuture', $args);
 
37
  return $ef->resolve();
 
38
}
 
39
 
 
40
 
 
41
/**
 
42
 * Wrapper for @{class:PhutilExecPassthru}.
 
43
 *
 
44
 * @param  string  sprintf()-style command pattern to execute.
 
45
 * @param  ...     Arguments to sprintf pattern.
 
46
 * @return int     Return code.
 
47
 */
 
48
function phutil_passthru($cmd /* , ... */) {
 
49
  $args = func_get_args();
 
50
  return newv('PhutilExecPassthru', $args)->execute();
 
51
}
 
52
 
 
53
 
 
54
/**
 
55
 * Return a human-readable signal name (like "SIGINT" or "SIGKILL") for a given
 
56
 * signal number.
 
57
 *
 
58
 * @param   int     Signal number.
 
59
 * @return  string  Human-readable signal name.
 
60
 */
 
61
function phutil_get_signal_name($signo) {
 
62
  // These aren't always defined; try our best to look up the signal name.
 
63
  $constant_names = array(
 
64
    'SIGHUP',
 
65
    'SIGINT',
 
66
    'SIGQUIT',
 
67
    'SIGILL',
 
68
    'SIGTRAP',
 
69
    'SIGABRT',
 
70
    'SIGIOT',
 
71
    'SIGBUS',
 
72
    'SIGFPE',
 
73
    'SIGUSR1',
 
74
    'SIGSEGV',
 
75
    'SIGUSR2',
 
76
    'SIGPIPE',
 
77
    'SIGALRM',
 
78
    'SIGTERM',
 
79
    'SIGSTKFLT',
 
80
    'SIGCLD',
 
81
    'SIGCHLD',
 
82
    'SIGCONT',
 
83
    'SIGTSTP',
 
84
    'SIGTTIN',
 
85
    'SIGTTOU',
 
86
    'SIGURG',
 
87
    'SIGXCPU',
 
88
    'SIGXFSZ',
 
89
    'SIGVTALRM',
 
90
    'SIGPROF',
 
91
    'SIGWINCH',
 
92
    'SIGPOLL',
 
93
    'SIGIO',
 
94
    'SIGPWR',
 
95
    'SIGSYS',
 
96
    'SIGBABY',
 
97
  );
 
98
 
 
99
  $signal_names = array();
 
100
  foreach ($constant_names as $constant) {
 
101
    if (defined($constant)) {
 
102
      $signal_names[constant($constant)] = $constant;
 
103
    }
 
104
  }
 
105
 
 
106
  return idx($signal_names, $signo);
 
107
}