File: command-line.js

package info (click to toggle)
conkeror 0.9~git080629-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,132 kB
  • ctags: 906
  • sloc: sh: 340; ansic: 246; xml: 105; makefile: 77
file content (173 lines) | stat: -rw-r--r-- 5,985 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * (C) Copyright 2007 John J. Foerch
 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

var command_line_handlers = [];

define_variable("url_remoting_fn", load_url_in_new_window);

function load_url_in_new_window(url, ctx) {
    make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
}

function load_url_in_new_buffer(url, ctx) {
    create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
                                    OPEN_NEW_BUFFER, true /* focus the new window */);
}

function command_line_handler(name, suppress_default, handler)
{
    command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
}

function command_line_param_handler(name, suppress_default, handler)
{
    command_line_handlers[name] = { suppress_default: suppress_default,
                                    param: true,
                                    func: handler };
}

command_line_handler("batch", true);
command_line_param_handler("e", true, function (expr, ctx) {
        eval(expr);
    });

command_line_param_handler("E", false, function (expr, ctx) {});

command_line_param_handler("chrome", true, function (uri, ctx) {
        try {
            make_chrome_window(uri);
        } catch (e) { dump_error(e); }
    });
command_line_param_handler("q", false, function () {
        dumpln ("w: -q may only be used as the first argument.");
    });

command_line_param_handler("cwd", false, function (dir, ctx) {
        if (ctx.config == null)
            ctx.config = {};
        ctx.config.cwd = dir;
    });

command_line_param_handler("f", true, function (command) {
        var ctx = {
            window: window_watcher.activeWindow
        };
       call_interactively(ctx, command);
    });

function handle_command_line(cmdline)
{
    try {
        var suppress_default = false;
        var suppress_rc = false;

        var i = 0;

        /* -q must be the first argument, if it is given */
        if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
        {
            suppress_rc = true;
            i++;
        }

        var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);

        if (initial_launch) {
            let j = i;
            while (j + 1 < cmdline.length) {
                if (cmdline.getArgument(j) == "-E") {
                    eval(cmdline.getArgument(j+1));
                    cmdline.removeArguments(j, j+1);
                } else
                    ++j;
            }

            let load_default_modules = get_pref("conkeror.loadDefaultModules");
            let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
            try {
                let branch = preferences.getBranch("conkeror.load.");
                for each (let m in branch.getChildList("", {})) {
                    let val;
                    try {
                        val = branch.getIntPref(m);
                    } catch (e) {
                        dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
                    }
                    if ((val > 0 && (load_default_modules > 0 ||
                                     ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
                        (val >= 0 && load_mods.test(m)))
                        require(m + ".js");
                }
            } catch (e) {dump_error(e);}
        }

        if (! suppress_rc && initial_launch)
        {
            try {
                load_rc ();
            } catch (e) { dump (e + "\n"); }
        } else if (suppress_rc && ! initial_launch) {
            dumpln ("w: attempt to suppress load_rc in remote invocation");
        }
        var ctx = {}; // command-line processing context

        for (; i < cmdline.length; ++i)
        {
            var arg = cmdline.getArgument(i);
            if (arg[0] == '-') {
                var arg1 = arg.substring(1);
                if (arg1 in command_line_handlers) {
                    var handler = command_line_handlers[arg1];
                    if (handler.suppress_default)
                        suppress_default = true;
                    if (handler.func) {
                        if (handler.param) {
                            i++; // increment the argument counter to skip the parameter
                            if (i >= cmdline.length) {
                                dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
                                continue;
                            }
                            var param = cmdline.getArgument (i);
                            handler.func(param, ctx);
                        } else {
                            handler.func(ctx);
                        }
                    }
                    continue;
                }
            }

            // something other than a switch was passed on the command
            // line.  suppress the default window, and call the
            // user-configurable remoting function on it.
            //
            suppress_default = true;
            url_remoting_fn (arg, ctx);
        }

        // we are greedy and handle all command line arguments.  remove
        // everything from the command line object, so no other
        // components can see them.
        //
        if (cmdline.length > 0) {
            cmdline.removeArguments(0, cmdline.length - 1);
        }

        // no args were found for url_remoting_fn, and no switches
        // explicitly suppressed the creation of a default window
        // (e.g. -batch or -daemon)
        //
        if (! suppress_default) {
            url_remoting_fn(homepage, ctx);
        }
    } catch (e) {
        dumpln("Error processing command line.");
        dump_error(e);
    }
}