widget/xremoteclient/mozilla-xremote-client.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim:expandtab:shiftwidth=4:tabstop=4:
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include <stdio.h>
michael@0 9 #include <stdlib.h>
michael@0 10 #include <string.h>
michael@0 11 #include <plgetopt.h>
michael@0 12 #include "XRemoteClient.h"
michael@0 13
michael@0 14 static void print_usage(void);
michael@0 15
michael@0 16 int main(int argc, char **argv)
michael@0 17 {
michael@0 18 nsresult rv;
michael@0 19 XRemoteClient client;
michael@0 20 char *browser = 0;
michael@0 21 char *profile = 0;
michael@0 22 char *username = 0;
michael@0 23 char *command = 0;
michael@0 24
michael@0 25 if (argc < 2) {
michael@0 26 print_usage();
michael@0 27 return 4;
michael@0 28 }
michael@0 29
michael@0 30 PLOptStatus os;
michael@0 31 PLOptState *opt = PL_CreateOptState(argc, argv, "ha:u:p:");
michael@0 32 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
michael@0 33 if (PL_OPT_BAD == os) {
michael@0 34 print_usage();
michael@0 35 return 4;
michael@0 36 }
michael@0 37
michael@0 38 switch (opt->option) {
michael@0 39 case 'h':
michael@0 40 print_usage();
michael@0 41 return 4;
michael@0 42 break;
michael@0 43 case 'u':
michael@0 44 username = strdup(opt->value);
michael@0 45 break;
michael@0 46 case 'a':
michael@0 47 browser = strdup(opt->value);
michael@0 48 break;
michael@0 49 case 'p':
michael@0 50 profile = strdup(opt->value);
michael@0 51 break;
michael@0 52 case 0:
michael@0 53 command = strdup(opt->value);
michael@0 54 default:
michael@0 55 break;
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 rv = client.Init();
michael@0 60 // failed to connect to the X server
michael@0 61 if (NS_FAILED(rv))
michael@0 62 return 1;
michael@0 63
michael@0 64 // send the command - it doesn't get any easier than this
michael@0 65 bool success = false;
michael@0 66 char *error = 0;
michael@0 67 rv = client.SendCommand(browser, username, profile, command, nullptr,
michael@0 68 &error, &success);
michael@0 69
michael@0 70 // failed to send command
michael@0 71 if (NS_FAILED(rv)) {
michael@0 72 fprintf(stderr, "%s: Error: Failed to send command: ", argv[0]);
michael@0 73 if (error) {
michael@0 74 fprintf(stderr, "%s\n", error);
michael@0 75 free(error);
michael@0 76 }
michael@0 77 else {
michael@0 78 fprintf(stderr, "No error string reported..\n");
michael@0 79 }
michael@0 80
michael@0 81 return 3;
michael@0 82 }
michael@0 83
michael@0 84 // no running window found
michael@0 85 if (!success) {
michael@0 86 fprintf(stderr, "%s: Error: Failed to find a running server.\n", argv[0]);
michael@0 87 return 2;
michael@0 88 }
michael@0 89
michael@0 90 // else, everything is fine.
michael@0 91 return 0;
michael@0 92 }
michael@0 93
michael@0 94 /* static */
michael@0 95 void print_usage(void) {
michael@0 96 fprintf(stderr, "Usage: mozilla-xremote-client [-a firefox|thunderbird|mozilla|any]\n");
michael@0 97 fprintf(stderr, " [-u <username>]\n");
michael@0 98 fprintf(stderr, " [-p <profile>] COMMAND\n");
michael@0 99 }

mercurial