Ticket #1177: openmoko-terminal2-patch-g-option-full-hacky.diff

File openmoko-terminal2-patch-g-option-full-hacky.diff, 3.2 KB (added by haakeyar-mailinglists@…, 11 years ago)

A bit more hacky patch that implements -e/--command and -x/--execute options.

  • src/mainwindow.vala

     
    3636    private ToolButton btn_zoom_out; 
    3737    private ToolButton btn_paste; 
    3838 
     39    private static string cmdln_parsed_initial_command; 
    3940    private string initial_command; 
    4041 
    4142    public MainWindow() 
     
    204205        show_all(); 
    205206        Gtk.main(); 
    206207    } 
    207  
     208     
     209    const OptionEntry[] options = { 
     210        { "command", 'e', 0, OptionArg.STRING, out cmdln_parsed_initial_command, "Execute COMMAND inside the terminal.", "COMMAND" }, 
     211        { "execute", 'x', 0, OptionArg.STRING, out cmdln_parsed_initial_command, "Execute the remainder of the command line inside the terminal.", null }, 
     212        { null } 
     213    }; 
     214     
    208215    static int main (string[] args) { 
    209         if ( args.length == 2 || args.length > 3 ) 
     216        // Do a pre-search for the -x or --execute argument 
     217        string x_args = ""; // For some reason, I am unable to modify args directly in the loop, but it is nice to use the regular parsing for -x/--execute too, after doing this pre-search. 
     218                            // Therefore, I create a temporary string that I will initally modify, and then move the changes back into args. 
     219                            // For some reason, this doesn't work when this variable is initialized inside the if in the for-loop, so it has to be done out here. 
     220                            // Someone with better knowledge of Vala and C, will probably come up with a much better solution. 
     221        for(int i = 0; i < args.length; i++) { 
     222            if ( args[i] == "-x" || args[i] == "--execute" ) 
     223            { 
     224                int argument_index = i + 1; 
     225                if(args.length == argument_index) // Missing argument 
     226                { 
     227                    break; // The regular parsing will take care of this. 
     228                } 
     229                x_args = args[argument_index]; 
     230                for (i=argument_index+1; i < args.length; i++) 
     231                { 
     232                    x_args = x_args.concat(" \"", args[i].escape(null), "\""); // Escape and put quotes around to make arguments with spaces and quotes and things like that work like expected. I don't think it will hurt to put quotes around the args even if they didn't originally have any, but it will probably need some testing. 
     233                } 
     234                args.length = argument_index + 1; 
     235                args[argument_index] = x_args; 
     236            } 
     237        } 
     238         
     239        try { 
     240            Gtk.init_with_args(ref args.length, ref args, " - a lightweight terminal for the OpenMoko environment", options, null); 
     241        } catch (Error e) 
    210242        { 
    211             stdout.printf( "Usage: %s [ -e <initial command> ]\n", args[0] ); 
    212             return 0; 
     243            stderr.printf("Error: %s\n", e.message); 
     244            return 1;  
    213245        } 
    214  
    215         Gtk.init(ref args); 
     246         
    216247        var window = new MainWindow(); 
    217         if ( args.length == 3 && args[1] == "-e" ) 
    218             window.setup_command( args[2] ); 
     248        if ( cmdln_parsed_initial_command != null ) 
     249        { 
     250            window.setup_command( cmdln_parsed_initial_command ); 
     251        } 
    219252        window.run(); 
    220253 
    221254        return 0;