coroutine.js: call dump_error for exceptions due to programming errors
[conkeror.git] / modules / command-line.js
blob3e388efa53a6f25d46a8d4daa72bcaa393410695
1 /**
2  * (C) Copyright 2007 John J. Foerch
3  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 var command_line_handlers = [];
11 define_variable("url_remoting_fn", load_url_in_new_window);
13 function load_url_in_new_window(url, ctx) {
14     make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
17 function load_url_in_new_buffer(url, ctx) {
18     create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
19                                     OPEN_NEW_BUFFER, true /* focus the new window */);
22 function command_line_handler(name, suppress_default, handler)
24     command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
27 function command_line_param_handler(name, suppress_default, handler)
29     command_line_handlers[name] = { suppress_default: suppress_default,
30                                     param: true,
31                                     func: handler };
34 command_line_handler("batch", true);
35 command_line_param_handler("e", false, function (expr, ctx) {
36         eval(expr);
37     });
39 command_line_param_handler("E", false, function (expr, ctx) {});
41 command_line_param_handler("chrome", true, function (uri, ctx) {
42         try {
43             make_chrome_window(uri);
44         } catch (e) { dump_error(e); }
45     });
46 command_line_param_handler("q", false, function () {
47         dumpln ("w: -q may only be used as the first argument.");
48     });
50 command_line_param_handler("cwd", false, function (dir, ctx) {
51         if (ctx.config == null)
52             ctx.config = {};
53         ctx.config.cwd = dir;
54     });
57 function handle_command_line(cmdline)
59     try {
60         var suppress_default = false;
61         var suppress_rc = false;
63         var i = 0;
65         /* -q must be the first argument, if it is given */
66         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
67         {
68             suppress_rc = true;
69             i++;
70         }
72         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
74         if (initial_launch) {
75             let j = i;
76             while (j + 1 < cmdline.length) {
77                 if (cmdline.getArgument(j) == "-E") {
78                     eval(cmdline.getArgument(j+1));
79                     cmdline.removeArguments(j, j+1);
80                 } else
81                     ++j;
82             }
84             let load_default_modules = get_pref("conkeror.loadDefaultModules");
85             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
86             try {
87                 let branch = preferences.getBranch("conkeror.load.");
88                 for each (let m in branch.getChildList("", {})) {
89                     let val;
90                     try {
91                         val = branch.getIntPref(m);
92                     } catch (e) {
93                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
94                     }
95                     if ((val > 0 && (load_default_modules > 0 ||
96                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
97                         (val >= 0 && load_mods.test(m)))
98                         require(m + ".js");
99                 }
100             } catch (e) {dump_error(e);}
101         }
103         if (! suppress_rc && initial_launch)
104         {
105             try {
106                 load_rc ();
107             } catch (e) { dump (e + "\n"); }
108         } else if (suppress_rc && ! initial_launch) {
109             dumpln ("w: attempt to suppress load_rc in remote invocation");
110         }
111         var ctx = {}; // command-line processing context
113         for (; i < cmdline.length; ++i)
114         {
115             var arg = cmdline.getArgument(i);
116             if (arg[0] == '-') {
117                 var arg1 = arg.substring(1);
118                 if (arg1 in command_line_handlers) {
119                     var handler = command_line_handlers[arg1];
120                     if (handler.suppress_default)
121                         suppress_default = true;
122                     if (handler.func) {
123                         if (handler.param) {
124                             i++; // increment the argument counter to skip the parameter
125                             if (i >= cmdline.length) {
126                                 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
127                                 continue;
128                             }
129                             var param = cmdline.getArgument (i);
130                             handler.func(param, ctx);
131                         } else {
132                             handler.func(ctx);
133                         }
134                     }
135                     continue;
136                 }
137             }
139             // something other than a switch was passed on the command
140             // line.  suppress the default window, and call the
141             // user-configurable remoting function on it.
142             //
143             suppress_default = true;
144             url_remoting_fn (arg, ctx);
145         }
147         // we are greedy and handle all command line arguments.  remove
148         // everything from the command line object, so no other
149         // components can see them.
150         //
151         if (cmdline.length > 0) {
152             cmdline.removeArguments(0, cmdline.length - 1);
153         }
155         // no args were found for url_remoting_fn, and no switches
156         // explicitly suppressed the creation of a default window
157         // (e.g. -batch or -daemon)
158         //
159         if (! suppress_default) {
160             url_remoting_fn(homepage, ctx);
161         }
162     } catch (e) {
163         dumpln("Error processing command line.");
164         dump_error(e);
165     }