-
Notifications
You must be signed in to change notification settings - Fork 9
/
README
120 lines (94 loc) · 4.89 KB
/
README
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
How to Use subprocess.jsm in your Add-on
----------------------------------------
1. copy subprocess.jsm and subprocess_worker_*.js into modules/
2. add this line to chrome.manifest:
resource EXTENSION modules/
3. import it where needed:
Components.utils.import("resource://EXTENSION/subprocess.jsm");
This object allows to start a process, and read/write data to/from it
using stdin/stdout/stderr streams.
Usage example:
var p = subprocess.call({
command: '/bin/foo',
arguments: ['-v', 'foo'],
environment: [ "XYZ=abc", "MYVAR=def" ],
charset: 'UTF-8',
workdir: '/home/foo',
//stdin: "some value to write to stdin\nfoobar",
stdin: function(stdin) {
stdin.write("some value to write to stdin\nfoobar");
stdin.close();
},
stdout: function(data) {
dump("got data on stdout:" + data + "\n");
},
stderr: function(data) {
dump("got data on stderr:" + data + "\n");
},
done: function(result) {
dump("process terminated with " + result.exitCode + "\n");
},
mergeStderr: false
});
p.wait(); // wait for the subprocess to terminate,
// this will block the main thread,
// only do if you can wait that long
Description of Parameters
-------------------------
Apart from <command>, all arguments are optional.
command: either a |nsIFile| object pointing to an executable file or a
String containing the platform-dependent path to an executable
file.
arguments: optional string array containing the arguments to the command.
environment: optional string array containing environment variables to pass
to the command. The array elements must have the form
"VAR=data". Please note that if environment is defined, it
replaces any existing environment variables for the subprocess.
charset: Output is decoded with given charset and a string is returned.
If charset is undefined, "UTF-8" is used as default.
To get binary data, set this to null and the returned string
is not decoded in any way.
workdir: Optional; either a |nsIFile| object or string containing the
platform-dependent path to a directory to become the current
working directory of the subprocess.
stdin: Optional input data for the process to be passed on standard
input. stdin can either be a string or a function.
A |string| gets written to stdin and stdin gets closed;
A |function| gets passed an object with write and close function.
Please note that the write() function will return almost immediately;
data is always written asynchronously on a separate thread.
stdout: An optional function that can receive output data from the
process. The stdout-function is called asynchronously; it can be
called mutliple times during the execution of a process.
At a minimum at each occurance of \n or \r.
Please note that null-characters might need to be escaped
with something like 'data.replace(/\0/g, "\\0");'.
stderr: An optional function that can receive stderr data from the
process. The stderr-function is called asynchronously; it can be
called mutliple times during the execution of a process. Please
note that null-characters might need to be escaped with
something like 'data.replace(/\0/g, "\\0");'.
(on windows it only gets called once right now)
done: Optional function that is called when the process has terminated.
The exit code from the process available via result.exitCode. If
stdout is not defined, then the output from stdout is available
via result.stdout. stderr data is in result.stderr
mergeStderr: Optional boolean value. If true, stderr is merged with stdout;
no data will be provided to stderr.
Description of object returned by subprocess.call(...)
------------------------------------------------------
The object returned by subprocess.call offers a few methods that can be
executed:
wait(): waits for the subprocess to terminate. It is not required to use
wait; done will be called in any case when the subprocess terminated.
kill(): kill the subprocess. Any open pipes will be closed and
done will be called.
Other methods in subprocess
---------------------------
The following functions help debugging and provide logging facilities.
registerDebugHandler(functionRef): register a handler that is called to get
debugging information
registerLogHandler(functionRef): register a handler that is called to get error
messages
example:
subprocess.registerLogHandler( function(s) { dump(s); } );