addon-sdk/source/lib/sdk/system/child_process/README.md

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:c731c59ddee9
1 # subprocess
2
3
4 Created by [Jan Gerber](j@mailb.org), with contributions from [Patrick Brunschwig](patrick@enigmail.net), subprocess.jsm is used as the underlying library, supporting the Addon-SDK's implementation of Node's `child-process` API.
5
6 `subprocess.jsm` is originally from [http://hg.mozilla.org/ipccode/](http://hg.mozilla.org/ipccode/).
7
8 How to Use subprocess.jsm in your Add-on
9 ----------------------------------------
10
11 1. copy subprocess.jsm and subprocess_worker_*.js into the modules/ directory
12 of your add-on.
13
14 2. add this line to chrome.manifest:
15 resource EXTENSION modules/
16
17 3. import it where needed:
18 Components.utils.import("resource://EXTENSION/subprocess.jsm");
19
20 This object allows to start a process, and read/write data to/from it
21 using stdin/stdout/stderr streams.
22
23 Usage example:
24
25 var p = subprocess.call({
26 command: '/bin/foo',
27 arguments: ['-v', 'foo'],
28 environment: [ "XYZ=abc", "MYVAR=def" ],
29 charset: 'UTF-8',
30 workdir: '/home/foo',
31 //stdin: "some value to write to stdin\nfoobar",
32 stdin: function(stdin) {
33 stdin.write("some value to write to stdin\nfoobar");
34 stdin.close();
35 },
36 stdout: function(data) {
37 dump("got data on stdout:" + data + "\n");
38 },
39 stderr: function(data) {
40 dump("got data on stderr:" + data + "\n");
41 },
42 done: function(result) {
43 dump("process terminated with " + result.exitCode + "\n");
44 },
45 mergeStderr: false
46 });
47
48 p.wait(); // wait for the subprocess to terminate,
49 // this will block the main thread,
50 // only do if you can wait that long
51
52
53 Description of subprocess.call(...) Parameters
54 ----------------------------------------------
55 Apart from <command>, all arguments are optional.
56
57 command: either a |nsIFile| object pointing to an executable file or a
58 String containing the platform-dependent path to an executable
59 file.
60
61 arguments: optional string array containing the arguments to the command.
62
63 environment: optional string array containing environment variables to pass
64 to the command. The array elements must have the form
65 "VAR=data". Please note that if environment is defined, it
66 replaces any existing environment variables for the subprocess.
67
68 charset: Output is decoded with given charset and a string is returned.
69 If charset is undefined, "UTF-8" is used as default.
70 To get binary data, set this to null and the returned string
71 is not decoded in any way.
72
73 workdir: Optional; either a |nsIFile| object or string containing the
74 platform-dependent path to a directory to become the current
75 working directory of the subprocess.
76
77 stdin: Optional input data for the process to be passed on standard
78 input. stdin can either be a string or a function.
79 A |string| gets written to stdin and stdin gets closed;
80 A |function| gets passed an object with write and close function.
81 Please note that the write() function will return almost immediately;
82 data is always written asynchronously on a separate thread.
83
84 stdout: An optional function that can receive output data from the
85 process. The stdout-function is called asynchronously; it can be
86 called mutliple times during the execution of a process.
87 At a minimum at each occurance of \n or \r.
88 Please note that null-characters might need to be escaped
89 with something like 'data.replace(/\0/g, "\\0");'.
90
91 stderr: An optional function that can receive stderr data from the
92 process. The stderr-function is called asynchronously; it can be
93 called mutliple times during the execution of a process. Please
94 note that null-characters might need to be escaped with
95 something like 'data.replace(/\0/g, "\\0");'.
96 (on windows it only gets called once right now)
97
98 done: Optional function that is called when the process has terminated.
99 The exit code from the process available via result.exitCode. If
100 stdout is not defined, then the output from stdout is available
101 via result.stdout. stderr data is in result.stderr
102
103 mergeStderr: Optional boolean value. If true, stderr is merged with stdout;
104 no data will be provided to stderr.
105
106
107 Description of object returned by subprocess.call(...)
108 ------------------------------------------------------
109 The object returned by subprocess.call offers a few methods that can be
110 executed:
111
112 wait(): waits for the subprocess to terminate. It is not required to use
113 wait; done will be called in any case when the subprocess terminated.
114
115 kill(): kill the subprocess. Any open pipes will be closed and
116 done will be called.
117
118
119 Other methods exported by subprocess
120 ------------------------------------
121 The following functions help debugging and provide logging facilities.
122
123 registerDebugHandler(functionRef): register a handler that is called to get
124 debugging information
125 registerLogHandler(functionRef): register a handler that is called to get error
126 messages
127
128 example:
129 subprocess.registerLogHandler( function(s) { dump(s); } );

mercurial