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