]> git.sur5r.net Git - i3/i3/blob - i3-msg/main.c
Add proper return code for i3-msg
[i3/i3] / i3-msg / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * i3-msg/main.c: Utility which sends messages to a running i3-instance using
8  * IPC via UNIX domain sockets.
9  *
10  * This (in combination with libi3/ipc_send_message.c and
11  * libi3/ipc_recv_message.c) serves as an example for how to send your own
12  * messages to i3.
13  *
14  * Additionally, it’s even useful sometimes :-).
15  *
16  */
17 #include "libi3.h"
18
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <err.h>
29 #include <stdint.h>
30 #include <getopt.h>
31 #include <limits.h>
32
33 #include <yajl/yajl_parse.h>
34 #include <yajl/yajl_version.h>
35
36 #include <xcb/xcb.h>
37 #include <xcb/xcb_aux.h>
38
39 #include <i3/ipc.h>
40
41 /*
42  * Having verboselog() and errorlog() is necessary when using libi3.
43  *
44  */
45 void verboselog(char *fmt, ...) {
46     va_list args;
47
48     va_start(args, fmt);
49     vfprintf(stdout, fmt, args);
50     va_end(args);
51 }
52
53 void errorlog(char *fmt, ...) {
54     va_list args;
55
56     va_start(args, fmt);
57     vfprintf(stderr, fmt, args);
58     va_end(args);
59 }
60
61 static char *last_key = NULL;
62
63 typedef struct reply_t {
64     bool success;
65     char *error;
66     char *input;
67     char *errorposition;
68 } reply_t;
69
70 static int exit_code = 0;
71 static reply_t last_reply;
72
73 static int reply_boolean_cb(void *params, int val) {
74     if (strcmp(last_key, "success") == 0)
75         last_reply.success = val;
76     return 1;
77 }
78
79 static int reply_string_cb(void *params, const unsigned char *val, size_t len) {
80     char *str = scalloc(len + 1, 1);
81     strncpy(str, (const char *)val, len);
82     if (strcmp(last_key, "error") == 0)
83         last_reply.error = str;
84     else if (strcmp(last_key, "input") == 0)
85         last_reply.input = str;
86     else if (strcmp(last_key, "errorposition") == 0)
87         last_reply.errorposition = str;
88     else
89         free(str);
90     return 1;
91 }
92
93 static int reply_start_map_cb(void *params) {
94     return 1;
95 }
96
97 static int reply_end_map_cb(void *params) {
98     if (!last_reply.success) {
99         if (last_reply.input) {
100             fprintf(stderr, "ERROR: Your command: %s\n", last_reply.input);
101             fprintf(stderr, "ERROR:               %s\n", last_reply.errorposition);
102         }
103         fprintf(stderr, "ERROR: %s\n", last_reply.error);
104         exit_code = 2;
105     }
106     return 1;
107 }
108
109 static int reply_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
110     free(last_key);
111     last_key = scalloc(keyLen + 1, 1);
112     strncpy(last_key, (const char *)keyVal, keyLen);
113     return 1;
114 }
115
116 static yajl_callbacks reply_callbacks = {
117     .yajl_boolean = reply_boolean_cb,
118     .yajl_string = reply_string_cb,
119     .yajl_start_map = reply_start_map_cb,
120     .yajl_map_key = reply_map_key_cb,
121     .yajl_end_map = reply_end_map_cb,
122 };
123
124 /*******************************************************************************
125  * Config reply callbacks
126  *******************************************************************************/
127
128 static char *config_last_key = NULL;
129
130 static int config_string_cb(void *params, const unsigned char *val, size_t len) {
131     char *str = scalloc(len + 1, 1);
132     strncpy(str, (const char *)val, len);
133     if (strcmp(config_last_key, "config") == 0) {
134         fprintf(stdout, "%s", str);
135     }
136     free(str);
137     return 1;
138 }
139
140 static int config_start_map_cb(void *params) {
141     return 1;
142 }
143
144 static int config_end_map_cb(void *params) {
145     return 1;
146 }
147
148 static int config_map_key_cb(void *params, const unsigned char *keyVal, size_t keyLen) {
149     config_last_key = scalloc(keyLen + 1, 1);
150     strncpy(config_last_key, (const char *)keyVal, keyLen);
151     return 1;
152 }
153
154 static yajl_callbacks config_callbacks = {
155     .yajl_string = config_string_cb,
156     .yajl_start_map = config_start_map_cb,
157     .yajl_map_key = config_map_key_cb,
158     .yajl_end_map = config_end_map_cb,
159 };
160
161 int main(int argc, char *argv[]) {
162 #if defined(__OpenBSD__)
163     if (pledge("stdio rpath unix", NULL) == -1)
164         err(EXIT_FAILURE, "pledge");
165 #endif
166     char *socket_path = NULL;
167     int o, option_index = 0;
168     uint32_t message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
169     char *payload = NULL;
170     bool quiet = false;
171     bool monitor = false;
172
173     static struct option long_options[] = {
174         {"socket", required_argument, 0, 's'},
175         {"type", required_argument, 0, 't'},
176         {"version", no_argument, 0, 'v'},
177         {"quiet", no_argument, 0, 'q'},
178         {"monitor", no_argument, 0, 'm'},
179         {"help", no_argument, 0, 'h'},
180         {0, 0, 0, 0}};
181
182     char *options_string = "s:t:vhqm";
183
184     while ((o = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
185         if (o == 's') {
186             free(socket_path);
187             socket_path = sstrdup(optarg);
188         } else if (o == 't') {
189             if (strcasecmp(optarg, "command") == 0) {
190                 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
191             } else if (strcasecmp(optarg, "run_command") == 0) {
192                 message_type = I3_IPC_MESSAGE_TYPE_RUN_COMMAND;
193             } else if (strcasecmp(optarg, "get_workspaces") == 0) {
194                 message_type = I3_IPC_MESSAGE_TYPE_GET_WORKSPACES;
195             } else if (strcasecmp(optarg, "get_outputs") == 0) {
196                 message_type = I3_IPC_MESSAGE_TYPE_GET_OUTPUTS;
197             } else if (strcasecmp(optarg, "get_tree") == 0) {
198                 message_type = I3_IPC_MESSAGE_TYPE_GET_TREE;
199             } else if (strcasecmp(optarg, "get_marks") == 0) {
200                 message_type = I3_IPC_MESSAGE_TYPE_GET_MARKS;
201             } else if (strcasecmp(optarg, "get_bar_config") == 0) {
202                 message_type = I3_IPC_MESSAGE_TYPE_GET_BAR_CONFIG;
203             } else if (strcasecmp(optarg, "get_binding_modes") == 0) {
204                 message_type = I3_IPC_MESSAGE_TYPE_GET_BINDING_MODES;
205             } else if (strcasecmp(optarg, "get_version") == 0) {
206                 message_type = I3_IPC_MESSAGE_TYPE_GET_VERSION;
207             } else if (strcasecmp(optarg, "get_config") == 0) {
208                 message_type = I3_IPC_MESSAGE_TYPE_GET_CONFIG;
209             } else if (strcasecmp(optarg, "send_tick") == 0) {
210                 message_type = I3_IPC_MESSAGE_TYPE_SEND_TICK;
211             } else if (strcasecmp(optarg, "subscribe") == 0) {
212                 message_type = I3_IPC_MESSAGE_TYPE_SUBSCRIBE;
213             } else {
214                 printf("Unknown message type\n");
215                 printf("Known types: run_command, get_workspaces, get_outputs, get_tree, get_marks, get_bar_config, get_binding_modes, get_version, get_config, send_tick, subscribe\n");
216                 exit(EXIT_FAILURE);
217             }
218         } else if (o == 'q') {
219             quiet = true;
220         } else if (o == 'm') {
221             monitor = true;
222         } else if (o == 'v') {
223             printf("i3-msg " I3_VERSION "\n");
224             return 0;
225         } else if (o == 'h') {
226             printf("i3-msg " I3_VERSION "\n");
227             printf("i3-msg [-s <socket>] [-t <type>] [-m] <message>\n");
228             return 0;
229         } else if (o == '?') {
230             exit(EXIT_FAILURE);
231         }
232     }
233
234     if (monitor && message_type != I3_IPC_MESSAGE_TYPE_SUBSCRIBE) {
235         fprintf(stderr, "The monitor option -m is used with -t SUBSCRIBE exclusively.\n");
236         exit(EXIT_FAILURE);
237     }
238
239     /* Use all arguments, separated by whitespace, as payload.
240      * This way, you don’t have to do i3-msg 'mark foo', you can use
241      * i3-msg mark foo */
242     while (optind < argc) {
243         if (!payload) {
244             payload = sstrdup(argv[optind]);
245         } else {
246             char *both;
247             sasprintf(&both, "%s %s", payload, argv[optind]);
248             free(payload);
249             payload = both;
250         }
251         optind++;
252     }
253
254     if (!payload)
255         payload = sstrdup("");
256
257     int sockfd = ipc_connect(socket_path);
258     if (ipc_send_message(sockfd, strlen(payload), message_type, (uint8_t *)payload) == -1)
259         err(EXIT_FAILURE, "IPC: write()");
260     free(payload);
261
262     uint32_t reply_length;
263     uint32_t reply_type;
264     uint8_t *reply;
265     int ret;
266     if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
267         if (ret == -1)
268             err(EXIT_FAILURE, "IPC: read()");
269         exit(1);
270     }
271     if (reply_type != message_type)
272         errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected %d", reply_type, message_type);
273     /* For the reply of commands, have a look if that command was successful.
274      * If not, nicely format the error message. */
275     if (reply_type == I3_IPC_REPLY_TYPE_COMMAND) {
276         yajl_handle handle = yajl_alloc(&reply_callbacks, NULL, NULL);
277         yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
278         yajl_free(handle);
279
280         switch (state) {
281             case yajl_status_ok:
282                 break;
283             case yajl_status_client_canceled:
284             case yajl_status_error:
285                 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
286         }
287
288         if (!quiet) {
289             printf("%.*s\n", reply_length, reply);
290         }
291     } else if (reply_type == I3_IPC_REPLY_TYPE_CONFIG) {
292         yajl_handle handle = yajl_alloc(&config_callbacks, NULL, NULL);
293         yajl_status state = yajl_parse(handle, (const unsigned char *)reply, reply_length);
294         yajl_free(handle);
295
296         switch (state) {
297             case yajl_status_ok:
298                 break;
299             case yajl_status_client_canceled:
300             case yajl_status_error:
301                 errx(EXIT_FAILURE, "IPC: Could not parse JSON reply.");
302         }
303     } else if (reply_type == I3_IPC_REPLY_TYPE_SUBSCRIBE) {
304         do {
305             free(reply);
306             if ((ret = ipc_recv_message(sockfd, &reply_type, &reply_length, &reply)) != 0) {
307                 if (ret == -1)
308                     err(EXIT_FAILURE, "IPC: read()");
309                 exit(1);
310             }
311
312             if (!(reply_type & I3_IPC_EVENT_MASK)) {
313                 errx(EXIT_FAILURE, "IPC: Received reply of type %d but expected an event", reply_type);
314             }
315
316             if (!quiet) {
317                 fprintf(stdout, "%.*s\n", reply_length, reply);
318                 fflush(stdout);
319             }
320         } while (monitor);
321     } else {
322         if (!quiet) {
323             printf("%.*s\n", reply_length, reply);
324         }
325     }
326
327     free(reply);
328
329     close(sockfd);
330
331     return exit_code;
332 }