Delete file user_variables.js (duplicates content in utils.js)
[conkeror.git] / modules / process.js
blobca8ffa6505ad94b6983dbb85307702e01df17bae
1 require("thread.js");
2 require("interactive.js");
4 const WINDOWS = (get_os() == "WINNT");
5 const POSIX = !WINDOWS;
6 const PATH = getenv("PATH").split(POSIX ? ":" : ";"); 
7 const path_component_regexp = POSIX ? /^[^/]+$/ : /^[^/\\]+$/;
9 function get_file_in_path(name) {
10     var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
11     if (!path_component_regexp.test(name)) {
12         // Absolute path
13         try {
14             file.initWithPath(name);
15             if (file.exists())
16                 return file;
17         } catch (e) {}
18         return null;
19     } else {
20         // Relative path
21         for (var i = 0; i < PATH.length; ++i) {
22             try {
23                 file.initWithPath(PATH[i]);
24                 file.appendRelativePath(name);
25                 if (file.exists())
26                     return file;
27             } catch(e) {}
28         }
29     }
30     return null;
33 function spawn_process_sync(program, args, blocking) {
34     var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
35     var file = get_file_in_path(program);
36     if (!file)
37         throw new Error("invalid executable: " + program);
38     process.init(file);
39     return process.run(!!blocking, args, args.length);
42 function spawn_process(program, args) {
43     var result = yield in_new_thread(spawn_process_sync, program, args, true);
44     yield co_return(result);
47 function shell_command_sync(cwd, cmd, blocking) {
48     if (POSIX) {
49         var full_cmd = "cd \"" + shell_quote(cwd) + "\"; " + cmd;
50         return spawn_process_sync(getenv("SHELL") || "/bin/sh",
51                                   ["-c", full_cmd],
52                                   blocking);
53     } else {
55         var full_cmd = "";
56         if (cwd.match(/[a-z]:/i)) {
57             full_cmd += cwd.substring(0,2) + " && ";
58         }
59         full_cmd += "cd \"" + shell_quote(cwd) + "\" && " + cmd;
61         /* Need to convert the single command-line into a list of
62          * arguments that will then get converted back into a
63          * command-line by Mozilla. */
64         var out = ["/C"];
65         var cur_arg = "";
66         var quoting = false;
67         for (var i = 0; i < full_cmd.length; ++i) {
68             var ch = full_cmd[i];
69             if (ch == " ") {
70                 if (quoting) {
71                     cur_arg += ch;
72                 } else {
73                     out.push(cur_arg);
74                     cur_arg = "";
75                 }
76                 continue;
77             }
78             if (ch == "\"") {
79                 quoting = !quoting;
80                 continue;
81             }
82             cur_arg += ch;
83         }
84         if (cur_arg.length > 0)
85             out.push(cur_arg);
86         return spawn_process_sync("cmd.exe", out, blocking);
87     }
90 function shell_command(cwd, cmd) {
91     var result = yield in_new_thread(shell_command_sync, cwd, cmd, true);
92     yield co_return(result);
95 var PATH_programs = null;
96 function get_shell_command_completer() {
97     if (PATH_programs == null) {
98         PATH_programs = [];
99         var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
100         for (var i = 0; i < PATH.length; ++i) {
101             try {
102                 file.initWithPath(PATH[i]);
103                 var entries = file.directoryEntries;
104                 while (entries.hasMoreElements()) {
105                     var entry = entries.getNext().QueryInterface(Ci.nsIFile);
106                     PATH_programs.push(entry.leafName);
107                 }
108             } catch (e) {}
109         }
110         PATH_programs.sort();
111     }
113     return prefix_completer($completions = PATH_programs,
114                             $get_string = function (x) { return x; });
117 // use default
118 minibuffer_auto_complete_preferences["shell-command"] = null;
120 /* FIXME: support a relative or full path as well as PATH commands */
121 define_keywords("$cwd");
122 minibuffer.prototype.read_shell_command = function () {
123     keywords(arguments, $history = "shell-command");
124     var prompt = arguments.$prompt || "Shell command [" + arguments.$cwd + "]:";
125     var result = yield this.read(
126         $prompt = prompt,
127         $history = "shell-command",
128         $auto_complete = "shell-command",
129         $select,
130         $validator = function (x, m) {
131             var s = x.replace(/^\s+|\s+$/g, '');
132             if (s.length == 0) {
133                 m.message("A blank shell command is not allowed.");
134                 return false;
135             }
136             return true;
137         },
138         forward_keywords(arguments),
139         $completer = get_shell_command_completer());
140     yield co_return(result);
143 function shell_command_with_argument_sync(cwd, cmdline, argument, blocking) {
144     if (!cmdline.match("{}")) {
145         cmdline = cmdline + " \"" + shell_quote(argument) + "\"";
146     } else {
147         cmdline = cmdline.replace("{}", "\"" + shell_quote(argument) + "\"");
148     }
149     return shell_command_sync(cwd, cmdline, blocking);
152 function shell_command_with_argument(cwd, cmdline, argument) {
153     var result = yield in_new_thread(shell_command_with_argument_sync, cwd, cmdline, argument, true);
154     yield co_return(result);