]> git.lizzy.rs Git - bspwm.git/blob - src/messages.c
b08ca045901cade8da1f966924b817afaf6aecd3
[bspwm.git] / src / messages.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <stdarg.h>
30 #include <inttypes.h>
31 #include <unistd.h>
32 #include "bspwm.h"
33 #include "desktop.h"
34 #include "monitor.h"
35 #include "pointer.h"
36 #include "query.h"
37 #include "rule.h"
38 #include "restore.h"
39 #include "settings.h"
40 #include "tree.h"
41 #include "window.h"
42 #include "common.h"
43 #include "parse.h"
44 #include "messages.h"
45
46 void handle_message(char *msg, int msg_len, FILE *rsp)
47 {
48         int cap = INIT_CAP;
49         int num = 0;
50         char **args = calloc(cap, sizeof(char *));
51
52         if (args == NULL) {
53                 perror("Handle message: calloc");
54                 return;
55         }
56
57         for (int i = 0, j = 0; i < msg_len; i++) {
58                 if (msg[i] == 0) {
59                         args[num++] = msg + j;
60                         j = i + 1;
61                 }
62                 if (num >= cap) {
63                         cap *= 2;
64                         char **new = realloc(args, cap * sizeof(char *));
65                         if (new == NULL) {
66                                 free(args);
67                                 perror("Handle message: realloc");
68                                 return;
69                         } else {
70                                 args = new;
71                         }
72                 }
73         }
74
75         if (num < 1) {
76                 free(args);
77                 fail(rsp, "No arguments given.\n");
78                 return;
79         }
80
81         char **args_orig = args;
82         process_message(args, num, rsp);
83         free(args_orig);
84 }
85
86 void process_message(char **args, int num, FILE *rsp)
87 {
88         if (streq("node", *args)) {
89                 cmd_node(++args, --num, rsp);
90         } else if (streq("desktop", *args)) {
91                 cmd_desktop(++args, --num, rsp);
92         } else if (streq("monitor", *args)) {
93                 cmd_monitor(++args, --num, rsp);
94         } else if (streq("query", *args)) {
95                 cmd_query(++args, --num, rsp);
96         } else if (streq("subscribe", *args)) {
97                 cmd_subscribe(++args, --num, rsp);
98                 return;
99         } else if (streq("wm", *args)) {
100                 cmd_wm(++args, --num, rsp);
101         } else if (streq("rule", *args)) {
102                 cmd_rule(++args, --num, rsp);
103         } else if (streq("config", *args)) {
104                 cmd_config(++args, --num, rsp);
105         } else if (streq("quit", *args)) {
106                 cmd_quit(++args, --num, rsp);
107         } else {
108                 fail(rsp, "Unknown domain or command: '%s'.\n", *args);
109         }
110
111         fflush(rsp);
112         fclose(rsp);
113 }
114
115 void cmd_node(char **args, int num, FILE *rsp)
116 {
117         if (num < 1) {
118                 fail(rsp, "node: Missing arguments.\n");
119                 return;
120         }
121
122         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
123         coordinates_t trg = ref;
124
125         if ((*args)[0] != OPT_CHR) {
126                 int ret;
127                 if ((ret = node_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
128                         num--, args++;
129                 } else {
130                         handle_failure(ret, "node", *args, rsp);
131                         return;
132                 }
133         }
134
135         if (num < 1) {
136                 fail(rsp, "node: Missing commands.\n");
137                 return;
138         }
139
140         bool changed = false;
141
142         while (num > 0) {
143                 if (streq("-f", *args) || streq("--focus", *args)) {
144                         coordinates_t dst = trg;
145                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
146                                 num--, args++;
147                                 int ret;
148                                 if ((ret = node_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
149                                         handle_failure(ret, "node -f", *args, rsp);
150                                         break;
151                                 }
152                         }
153                         if (dst.node == NULL || !focus_node(dst.monitor, dst.desktop, dst.node)) {
154                                 fail(rsp, "");
155                                 break;
156                         }
157                 } else if (streq("-a", *args) || streq("--activate", *args)) {
158                         coordinates_t dst = trg;
159                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
160                                 num--, args++;
161                                 int ret;
162                                 if ((ret = node_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
163                                         handle_failure(ret, "node -a", *args, rsp);
164                                         break;
165                                 }
166                         }
167                         if (dst.node == NULL || !activate_node(dst.monitor, dst.desktop, dst.node)) {
168                                 fail(rsp, "");
169                                 break;
170                         }
171                 } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
172                         num--, args++;
173                         if (num < 1) {
174                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
175                                 break;
176                         }
177                         coordinates_t dst;
178                         int ret;
179                         if ((ret = desktop_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
180                                 bool follow = false;
181                                 if (num > 1 && streq("--follow", *(args+1))) {
182                                         follow = true;
183                                         num--, args++;
184                                 }
185                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus, follow)) {
186                                         trg.monitor = dst.monitor;
187                                         trg.desktop = dst.desktop;
188                                 } else {
189                                         fail(rsp, "");
190                                         break;
191                                 }
192                         } else {
193                                 handle_failure(ret, "node -d", *args, rsp);
194                                 break;
195                         }
196                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
197                         num--, args++;
198                         if (num < 1) {
199                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
200                                 break;
201                         }
202                         coordinates_t dst;
203                         int ret;
204                         if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
205                                 bool follow = false;
206                                 if (num > 1 && streq("--follow", *(args+1))) {
207                                         follow = true;
208                                         num--, args++;
209                                 }
210                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus, follow)) {
211                                         trg.monitor = dst.monitor;
212                                         trg.desktop = dst.monitor->desk;
213                                 } else {
214                                         fail(rsp, "");
215                                         break;
216                                 }
217                         } else {
218                                 handle_failure(ret, "node -m", *args, rsp);
219                                 break;
220                         }
221                 } else if (streq("-n", *args) || streq("--to-node", *args)) {
222                         num--, args++;
223                         if (num < 1) {
224                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
225                                 break;
226                         }
227                         coordinates_t dst;
228                         int ret;
229                         if ((ret = node_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
230                                 bool follow = false;
231                                 if (num > 1 && streq("--follow", *(args+1))) {
232                                         follow = true;
233                                         num--, args++;
234                                 }
235                                 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node, follow)) {
236                                         trg.monitor = dst.monitor;
237                                         trg.desktop = dst.desktop;
238                                 } else {
239                                         fail(rsp, "");
240                                         break;
241                                 }
242                         } else {
243                                 handle_failure(ret, "node -n", *args, rsp);
244                                 break;
245                         }
246                 } else if (streq("-s", *args) || streq("--swap", *args)) {
247                         num--, args++;
248                         if (num < 1) {
249                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
250                                 break;
251                         }
252                         coordinates_t dst;
253                         int ret;
254                         if ((ret = node_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
255                                 bool follow = false;
256                                 if (num > 1 && streq("--follow", *(args+1))) {
257                                         follow = true;
258                                         num--, args++;
259                                 }
260                                 if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node, follow)) {
261                                         trg.monitor = dst.monitor;
262                                         trg.desktop = dst.desktop;
263                                 } else {
264                                         fail(rsp, "");
265                                         break;
266                                 }
267                         } else {
268                                 handle_failure(ret, "node -s", *args, rsp);
269                                 break;
270                         }
271                 } else if (streq("-l", *args) || streq("--layer", *args)) {
272                         num--, args++;
273                         if (num < 1) {
274                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
275                                 break;
276                         }
277                         stack_layer_t lyr;
278                         if (parse_stack_layer(*args, &lyr)) {
279                                 if (!set_layer(trg.monitor, trg.desktop, trg.node, lyr)) {
280                                         fail(rsp, "");
281                                         break;
282                                 }
283                         } else {
284                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
285                                 break;
286                         }
287                 } else if (streq("-t", *args) || streq("--state", *args)) {
288                         num--, args++;
289                         if (num < 1) {
290                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
291                                 break;
292                         }
293                         client_state_t cst;
294                         bool alternate = false;
295                         if ((*args)[0] == '~') {
296                                 alternate = true;
297                                 (*args)++;
298                         }
299                         if (parse_client_state(*args, &cst)) {
300                                 if (alternate && trg.node != NULL && trg.node->client != NULL &&
301                                     trg.node->client->state == cst) {
302                                         cst = trg.node->client->last_state;
303                                 }
304                                 if (!set_state(trg.monitor, trg.desktop, trg.node, cst)) {
305                                         fail(rsp, "");
306                                         break;
307                                 }
308                                 changed = true;
309                         } else {
310                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
311                                 break;
312                         }
313                 } else if (streq("-g", *args) || streq("--flag", *args)) {
314                         num--, args++;
315                         if (num < 1) {
316                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
317                                 break;
318                         }
319                         if (trg.node == NULL) {
320                                 fail(rsp, "");
321                                 break;
322                         }
323                         char *key = strtok(*args, EQL_TOK);
324                         char *val = strtok(NULL, EQL_TOK);
325                         alter_state_t a;
326                         bool b;
327                         if (val == NULL) {
328                                 a = ALTER_TOGGLE;
329                         } else {
330                                 if (parse_bool(val, &b)) {
331                                         a = ALTER_SET;
332                                 } else {
333                                         fail(rsp, "node %s: Invalid value for %s: '%s'.\n", *(args - 1), key, val);
334                                         break;
335                                 }
336                         }
337                         if (streq("hidden", key)) {
338                                 set_hidden(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->hidden));
339                                 changed = true;
340                         } else if (streq("sticky", key)) {
341                                 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->sticky));
342                         } else if (streq("private", key)) {
343                                 set_private(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->private));
344                         } else if (streq("locked", key)) {
345                                 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->locked));
346                         } else if (streq("marked", key)) {
347                                 set_marked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->marked));
348                         } else {
349                                 fail(rsp, "node %s: Invalid key: '%s'.\n", *(args - 1), key);
350                                 break;
351                         }
352                 } else if (streq("-p", *args) || streq("--presel-dir", *args)) {
353                         num--, args++;
354                         if (num < 1) {
355                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
356                                 break;
357                         }
358                         if (trg.node == NULL || trg.node->vacant) {
359                                 fail(rsp, "");
360                                 break;
361                         }
362                         if (streq("cancel", *args)) {
363                                 cancel_presel(trg.monitor, trg.desktop, trg.node);
364                         } else {
365                                 bool alternate = false;
366                                 if ((*args)[0] == '~') {
367                                         alternate = true;
368                                         (*args)++;
369                                 }
370                                 direction_t dir;
371                                 if (parse_direction(*args, &dir)) {
372                                         if (alternate && trg.node->presel != NULL && trg.node->presel->split_dir == dir) {
373                                                 cancel_presel(trg.monitor, trg.desktop, trg.node);
374                                         } else {
375                                                 presel_dir(trg.monitor, trg.desktop, trg.node, dir);
376                                                 if (!IS_RECEPTACLE(trg.node)) {
377                                                         draw_presel_feedback(trg.monitor, trg.desktop, trg.node);
378                                                 }
379                                         }
380                                 } else {
381                                         fail(rsp, "node %s: Invalid argument: '%s%s'.\n", *(args - 1), alternate?"~":"", *args);
382                                         break;
383                                 }
384                         }
385                 } else if (streq("-o", *args) || streq("--presel-ratio", *args)) {
386                         num--, args++;
387                         if (num < 1) {
388                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
389                                 break;
390                         }
391                         if (trg.node == NULL || trg.node->vacant) {
392                                 fail(rsp, "");
393                                 break;
394                         }
395                         double rat;
396                         if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1) {
397                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
398                                 break;
399                         } else {
400                                 presel_ratio(trg.monitor, trg.desktop, trg.node, rat);
401                                 draw_presel_feedback(trg.monitor, trg.desktop, trg.node);
402                         }
403                 } else if (streq("-v", *args) || streq("--move", *args)) {
404                         num--, args++;
405                         if (num < 2) {
406                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
407                                 break;
408                         }
409                         int dx = 0, dy = 0;
410                         if (sscanf(*args, "%i", &dx) == 1) {
411                                 num--, args++;
412                                 if (sscanf(*args, "%i", &dy) == 1) {
413                                         if (!move_client(&trg, dx, dy)) {
414                                                 fail(rsp, "");
415                                                 break;
416                                         }
417                                 } else {
418                                         fail(rsp, "node %s: Invalid dy argument: '%s'.\n", *(args - 3), *args);
419                                         break;
420                                 }
421                         } else {
422                                 fail(rsp, "node %s: Invalid dx argument: '%s'.\n", *(args - 2), *args);
423                                 break;
424                         }
425                 } else if (streq("-z", *args) || streq("--resize", *args)) {
426                         num--, args++;
427                         if (num < 3) {
428                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
429                                 break;
430                         }
431                         resize_handle_t rh;
432                         if (parse_resize_handle(*args, &rh)) {
433                                 num--, args++;
434                                 int dx = 0, dy = 0;
435                                 if (sscanf(*args, "%i", &dx) == 1) {
436                                         num--, args++;
437                                         if (sscanf(*args, "%i", &dy) == 1) {
438                                                 if (!resize_client(&trg, rh, dx, dy, true)) {
439                                                         fail(rsp, "");
440                                                         break;
441                                                 }
442                                         } else {
443                                                 fail(rsp, "node %s: Invalid dy argument: '%s'.\n", *(args - 3), *args);
444                                                 break;
445                                         }
446                                 } else {
447                                         fail(rsp, "node %s: Invalid dx argument: '%s'.\n", *(args - 2), *args);
448                                         break;
449                                 }
450                         } else {
451                                 fail(rsp, "node %s: Invalid resize handle argument: '%s'.\n", *(args - 1), *args);
452                                 break;
453                         }
454                 } else if (streq("-r", *args) || streq("--ratio", *args)) {
455                         num--, args++;
456                         if (num < 1) {
457                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
458                                 break;
459                         }
460                         if (trg.node == NULL) {
461                                 fail(rsp, "");
462                                 break;
463                         }
464                         if ((*args)[0] == '+' || (*args)[0] == '-') {
465                                 float delta;
466                                 if (sscanf(*args, "%f", &delta) == 1) {
467                                         double rat = trg.node->split_ratio;
468                                         if (delta > -1 && delta < 1) {
469                                                 rat += delta;
470                                         } else {
471                                                 int max = (trg.node->split_type == TYPE_HORIZONTAL ? trg.node->rectangle.height : trg.node->rectangle.width);
472                                                 rat = ((max * rat) + delta) / max;
473                                         }
474                                         if (rat > 0 && rat < 1) {
475                                                 set_ratio(trg.node, rat);
476                                         } else {
477                                                 fail(rsp, "");
478                                                 break;
479                                         }
480                                 } else {
481                                         fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
482                                         break;
483                                 }
484                         } else {
485                                 double rat;
486                                 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
487                                         set_ratio(trg.node, rat);
488                                 } else {
489                                         fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
490                                         break;
491                                 }
492                         }
493                         changed = true;
494                 } else if (streq("-F", *args) || streq("--flip", *args)) {
495                         num--, args++;
496                         if (num < 1) {
497                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
498                                 break;
499                         }
500                         if (trg.node == NULL) {
501                                 fail(rsp, "");
502                                 break;
503                         }
504                         flip_t flp;
505                         if (parse_flip(*args, &flp)) {
506                                 flip_tree(trg.node, flp);
507                                 changed = true;
508                         } else {
509                                 fail(rsp, "");
510                                 break;
511                         }
512                 } else if (streq("-R", *args) || streq("--rotate", *args)) {
513                         num--, args++;
514                         if (num < 1) {
515                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
516                                 break;
517                         }
518                         if (trg.node == NULL) {
519                                 fail(rsp, "");
520                                 break;
521                         }
522                         int deg;
523                         if (parse_degree(*args, &deg)) {
524                                 rotate_tree(trg.node, deg);
525                                 changed = true;
526                         } else {
527                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
528                                 break;
529                         }
530                 } else if (streq("-E", *args) || streq("--equalize", *args)) {
531                         if (trg.node == NULL) {
532                                 fail(rsp, "");
533                                 break;
534                         }
535                         equalize_tree(trg.node);
536                         changed = true;
537                 } else if (streq("-B", *args) || streq("--balance", *args)) {
538                         if (trg.node == NULL) {
539                                 fail(rsp, "");
540                                 break;
541                         }
542                         balance_tree(trg.node);
543                         changed = true;
544                 } else if (streq("-C", *args) || streq("--circulate", *args)) {
545                         num--, args++;
546                         if (num < 1) {
547                                 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
548                                 break;
549                         }
550                         if (trg.node == NULL) {
551                                 fail(rsp, "");
552                                 break;
553                         }
554                         circulate_dir_t cir;
555                         if (parse_circulate_direction(*args, &cir)) {
556                                 circulate_leaves(trg.monitor, trg.desktop, trg.node, cir);
557                                 changed = true;
558                         } else {
559                                 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
560                                 break;
561                         }
562                 } else if (streq("-i", *args) || streq("--insert-receptacle", *args)) {
563                         insert_receptacle(trg.monitor, trg.desktop, trg.node);
564                         changed = true;
565                 } else if (streq("-c", *args) || streq("--close", *args)) {
566                         if (num > 1) {
567                                 fail(rsp, "node %s: Trailing commands.\n", *args);
568                                 break;
569                         }
570                         if (trg.node == NULL || locked_count(trg.node) > 0) {
571                                 fail(rsp, "");
572                                 break;
573                         }
574                         close_node(trg.node);
575                         break;
576                 } else if (streq("-k", *args) || streq("--kill", *args)) {
577                         if (num > 1) {
578                                 fail(rsp, "node %s: Trailing commands.\n", *args);
579                                 break;
580                         }
581                         if (trg.node == NULL) {
582                                 fail(rsp, "");
583                                 break;
584                         }
585                         kill_node(trg.monitor, trg.desktop, trg.node);
586                         changed = true;
587                         break;
588                 } else {
589                         fail(rsp, "node: Unknown command: '%s'.\n", *args);
590                         break;
591                 }
592
593                 num--, args++;
594         }
595
596         if (changed) {
597                 arrange(trg.monitor, trg.desktop);
598         }
599 }
600
601 void cmd_desktop(char **args, int num, FILE *rsp)
602 {
603         if (num < 1) {
604                 fail(rsp, "desktop: Missing arguments.\n");
605                 return;
606         }
607
608         coordinates_t ref = {mon, mon->desk, NULL};
609         coordinates_t trg = ref;
610
611         if ((*args)[0] != OPT_CHR) {
612                 int ret;
613                 if ((ret = desktop_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
614                         num--, args++;
615                 } else {
616                         handle_failure(ret, "desktop", *args, rsp);
617                         return;
618                 }
619         }
620
621         if (num < 1) {
622                 fail(rsp, "desktop: Missing commands.\n");
623                 return;
624         }
625
626         bool changed = false;
627
628         while (num > 0) {
629                 if (streq("-f", *args) || streq("--focus", *args)) {
630                         coordinates_t dst = trg;
631                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
632                                 num--, args++;
633                                 int ret;
634                                 if ((ret = desktop_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
635                                         handle_failure(ret, "desktop -f", *args, rsp);
636                                         break;
637                                 }
638                         }
639                         focus_node(dst.monitor, dst.desktop, NULL);
640                 } else if (streq("-a", *args) || streq("--activate", *args)) {
641                         coordinates_t dst = trg;
642                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
643                                 num--, args++;
644                                 int ret;
645                                 if ((ret = desktop_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
646                                         handle_failure(ret, "desktop -a", *args, rsp);
647                                         break;
648                                 }
649                         }
650                         if (activate_desktop(dst.monitor, dst.desktop)) {
651                                 activate_node(dst.monitor, dst.desktop, NULL);
652                         } else {
653                                 fail(rsp, "");
654                                 break;
655                         }
656                 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
657                         num--, args++;
658                         if (num < 1) {
659                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
660                                 break;
661                         }
662                         if (trg.monitor->desk_head == trg.monitor->desk_tail) {
663                                 fail(rsp, "");
664                                 break;
665                         }
666                         coordinates_t dst;
667                         int ret;
668                         if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
669                                 bool follow = false;
670                                 if (num > 1 && streq("--follow", *(args+1))) {
671                                         follow = true;
672                                         num--, args++;
673                                 }
674                                 if (transfer_desktop(trg.monitor, dst.monitor, trg.desktop, follow)) {
675                                         trg.monitor = dst.monitor;
676                                 } else {
677                                         fail(rsp, "");
678                                         break;
679                                 }
680                         } else {
681                                 handle_failure(ret, "desktop -m", *args, rsp);
682                                 break;
683                         }
684                 } else if (streq("-s", *args) || streq("--swap", *args)) {
685                         num--, args++;
686                         if (num < 1) {
687                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
688                                 break;
689                         }
690                         coordinates_t dst;
691                         int ret;
692                         if ((ret = desktop_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
693                                 bool follow = false;
694                                 if (num > 1 && streq("--follow", *(args+1))) {
695                                         follow = true;
696                                         num--, args++;
697                                 }
698                                 if (swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop, follow)) {
699                                         trg.monitor = dst.monitor;
700                                 } else {
701                                         fail(rsp, "");
702                                         break;
703                                 }
704                         } else {
705                                 handle_failure(ret, "desktop -s", *args, rsp);
706                                 break;
707                         }
708                 } else if (streq("-b", *args) || streq("--bubble", *args)) {
709                         num--, args++;
710                         if (num < 1) {
711                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
712                                 break;
713                         }
714                         cycle_dir_t cyc;
715                         if (parse_cycle_direction(*args, &cyc)) {
716                                 desktop_t *d = trg.desktop;
717                                 if (cyc == CYCLE_PREV) {
718                                         if (d->prev == NULL) {
719                                                 while (d->next != NULL) {
720                                                         swap_desktops(trg.monitor, d, trg.monitor, d->next, false);
721                                                 }
722                                         } else {
723                                                 swap_desktops(trg.monitor, d, trg.monitor, d->prev, false);
724                                         }
725                                 } else {
726                                         if (d->next == NULL) {
727                                                 while (d->prev != NULL) {
728                                                         swap_desktops(trg.monitor, d, trg.monitor, d->prev, false);
729                                                 }
730                                         } else {
731                                                 swap_desktops(trg.monitor, d, trg.monitor, d->next, false);
732                                         }
733                                 }
734                         } else {
735                                 fail(rsp, "desktop %s: Invalid argument: '%s'.\n", *(args - 1), *args);
736                                 break;
737                         }
738                 } else if (streq("-l", *args) || streq("--layout", *args)) {
739                         num--, args++;
740                         if (num < 1) {
741                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
742                                 break;
743                         }
744                         bool ret;
745                         layout_t lyt;
746                         cycle_dir_t cyc;
747                         if (parse_cycle_direction(*args, &cyc)) {
748                                 ret = set_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
749                         } else if (parse_layout(*args, &lyt)) {
750                                 ret = set_layout(trg.monitor, trg.desktop, lyt);
751                         } else {
752                                 fail(rsp, "desktop %s: Invalid argument: '%s'.\n", *(args - 1), *args);
753                                 break;
754                         }
755                         if (!ret) {
756                                 fail(rsp, "");
757                                 break;
758                         }
759                 } else if (streq("-n", *args) || streq("--rename", *args)) {
760                         num--, args++;
761                         if (num < 1) {
762                                 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
763                                 break;
764                         }
765                         rename_desktop(trg.monitor, trg.desktop, *args);
766                 } else if (streq("-r", *args) || streq("--remove", *args)) {
767                         if (num > 1) {
768                                 fail(rsp, "desktop %s: Trailing commands.\n", *args);
769                                 break;
770                         }
771                         if (trg.monitor->desk_head != trg.monitor->desk_tail) {
772                                 desktop_t *fallback = trg.desktop->prev == NULL ?
773                                                       trg.desktop->next :
774                                                       trg.desktop->prev;
775                                 merge_desktops(trg.monitor, trg.desktop, trg.monitor, fallback);
776                                 remove_desktop(trg.monitor, trg.desktop);
777                                 return;
778                         } else {
779                                 fail(rsp, "");
780                                 break;
781                         }
782                 } else {
783                         fail(rsp, "desktop: Unknown command: '%s'.\n", *args);
784                         break;
785                 }
786                 num--, args++;
787         }
788
789         if (changed) {
790                 arrange(trg.monitor, trg.desktop);
791         }
792 }
793
794 void cmd_monitor(char **args, int num, FILE *rsp)
795 {
796         if (num < 1) {
797                 fail(rsp, "monitor: Missing arguments.\n");
798                 return;
799         }
800
801         coordinates_t ref = {mon, NULL, NULL};
802         coordinates_t trg = ref;
803
804         if ((*args)[0] != OPT_CHR) {
805                 int ret;
806                 if ((ret = monitor_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
807                         num--, args++;
808                 } else {
809                         handle_failure(ret, "monitor", *args, rsp);
810                         return;
811                 }
812         }
813
814         if (num < 1) {
815                 fail(rsp, "monitor: Missing commands.\n");
816                 return;
817         }
818
819         while (num > 0) {
820                 if (streq("-f", *args) || streq("--focus", *args)) {
821                         coordinates_t dst = trg;
822                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
823                                 num--, args++;
824                                 int ret;
825                                 if ((ret = monitor_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
826                                         handle_failure(ret, "monitor -f", *args, rsp);
827                                         fail(rsp, "");
828                                         return;
829                                 }
830                         }
831                         focus_node(dst.monitor, NULL, NULL);
832                 } else if (streq("-s", *args) || streq("--swap", *args)) {
833                         num--, args++;
834                         if (num < 1) {
835                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
836                                 return;
837                         }
838                         coordinates_t dst;
839                         int ret;
840                         if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
841                                 if (!swap_monitors(trg.monitor, dst.monitor)) {
842                                         fail(rsp, "");
843                                         return;
844                                 }
845                         } else {
846                                 handle_failure(ret, "monitor -s", *args, rsp);
847                                 return;
848                         }
849                 } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
850                         num--, args++;
851                         if (num < 1) {
852                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
853                                 return;
854                         }
855                         desktop_t *d = trg.monitor->desk_head;
856                         while (num > 0 && d != NULL) {
857                                 rename_desktop(trg.monitor, d, *args);
858                                 d = d->next;
859                                 num--, args++;
860                         }
861                         put_status(SBSC_MASK_REPORT);
862                         while (num > 0) {
863                                 add_desktop(trg.monitor, make_desktop(*args, XCB_NONE));
864                                 num--, args++;
865                         }
866                         while (d != NULL) {
867                                 desktop_t *next = d->next;
868                                 if (d == mon->desk) {
869                                         focus_node(trg.monitor, d->prev, d->prev->focus);
870                                 }
871                                 merge_desktops(trg.monitor, d, mon, mon->desk);
872                                 remove_desktop(trg.monitor, d);
873                                 d = next;
874                         }
875                 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
876                         num--, args++;
877                         if (num < 1) {
878                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
879                                 return;
880                         }
881                         while (num > 0) {
882                                 add_desktop(trg.monitor, make_desktop(*args, XCB_NONE));
883                                 num--, args++;
884                         }
885                 } else if (streq("-r", *args) || streq("--remove", *args)) {
886                         if (num > 1) {
887                                 fail(rsp, "monitor %s: Trailing commands.\n", *args);
888                                 return;
889                         }
890                         if (mon_head == mon_tail) {
891                                 fail(rsp, "");
892                                 return;
893                         }
894                         remove_monitor(trg.monitor);
895                         return;
896                 } else if (streq("-o", *args) || streq("--reorder-desktops", *args)) {
897                         num--, args++;
898                         if (num < 1) {
899                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
900                                 return;
901                         }
902                         desktop_t *d = trg.monitor->desk_head;
903                         while (d != NULL && num > 0) {
904                                 desktop_t *next = d->next;
905                                 coordinates_t dst;
906                                 if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) {
907                                         swap_desktops(trg.monitor, d, dst.monitor, dst.desktop, false);
908                                         if (next == dst.desktop) {
909                                                 next = d;
910                                         }
911                                 }
912                                 d = next;
913                                 num--, args++;
914                         }
915                 } else if (streq("-g", *args) || streq("--rectangle", *args)) {
916                         num--, args++;
917                         if (num < 1) {
918                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
919                                 return;
920                         }
921                         xcb_rectangle_t r;
922                         if (parse_rectangle(*args, &r)) {
923                                 update_root(trg.monitor, &r);
924                         } else {
925                                 fail(rsp, "monitor %s: Invalid argument: '%s'.\n", *(args - 1), *args);
926                                 return;
927                         }
928                 } else if (streq("-n", *args) || streq("--rename", *args)) {
929                         num--, args++;
930                         if (num < 1) {
931                                 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
932                                 return;
933                         }
934                         rename_monitor(trg.monitor, *args);
935                 } else {
936                         fail(rsp, "monitor: Unknown command: '%s'.\n", *args);
937                         return;
938                 }
939                 num--, args++;
940         }
941 }
942
943 void cmd_query(char **args, int num, FILE *rsp)
944 {
945         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
946         coordinates_t trg = {NULL, NULL, NULL};
947         monitor_select_t *monitor_sel = NULL;
948         desktop_select_t *desktop_sel = NULL;
949         node_select_t *node_sel = NULL;
950         domain_t dom = DOMAIN_TREE;
951         bool print_ids = true;
952         uint8_t d = 0;
953
954         if (num < 1) {
955                 fail(rsp, "query: Missing arguments.\n");
956                 return;
957         }
958
959         while (num > 0) {
960                 if (streq("-T", *args) || streq("--tree", *args)) {
961                         dom = DOMAIN_TREE, d++;
962                 } else if (streq("-M", *args) || streq("--monitors", *args)) {
963                         dom = DOMAIN_MONITOR, d++;
964                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
965                                 num--, args++;
966                                 int ret;
967                                 coordinates_t tmp = ref;
968                                 if ((ret = monitor_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
969                                         handle_failure(ret, "query -M", *args, rsp);
970                                         goto end;
971                                 }
972                         }
973                 } else if (streq("-D", *args) || streq("--desktops", *args)) {
974                         dom = DOMAIN_DESKTOP, d++;
975                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
976                                 num--, args++;
977                                 int ret;
978                                 coordinates_t tmp = ref;
979                                 if ((ret = desktop_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
980                                         handle_failure(ret, "query -D", *args, rsp);
981                                         goto end;
982                                 }
983                         }
984                 } else if (streq("-N", *args) || streq("--nodes", *args)) {
985                         dom = DOMAIN_NODE, d++;
986                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
987                                 num--, args++;
988                                 int ret;
989                                 coordinates_t tmp = ref;
990                                 if ((ret = node_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
991                                         handle_failure(ret, "query -N", *args, rsp);
992                                         goto end;
993                                 }
994                         }
995                 } else if (streq("-m", *args) || streq("--monitor", *args)) {
996                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
997                                 num--, args++;
998                                 int ret;
999                                 if ((*args)[0] == '.') {
1000                                         free(monitor_sel);
1001                                         monitor_sel = malloc(sizeof(monitor_select_t));
1002                                         *monitor_sel = make_monitor_select();
1003                                         char *desc = copy_string(*args, strlen(*args));
1004                                         if (!parse_monitor_modifiers(desc, monitor_sel)) {
1005                                                 handle_failure(SELECTOR_BAD_MODIFIERS, "query -m", *args, rsp);
1006                                                 free(desc);
1007                                                 goto end;
1008                                         }
1009                                         free(desc);
1010                                 } else if ((ret = monitor_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1011                                         handle_failure(ret, "query -m", *args, rsp);
1012                                         goto end;
1013                                 }
1014                         } else {
1015                                 trg.monitor = ref.monitor;
1016                         }
1017                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
1018                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
1019                                 num--, args++;
1020                                 int ret;
1021                                 if ((*args)[0] == '.') {
1022                                         free(desktop_sel);
1023                                         desktop_sel = malloc(sizeof(desktop_select_t));
1024                                         *desktop_sel = make_desktop_select();
1025                                         char *desc = copy_string(*args, strlen(*args));
1026                                         if (!parse_desktop_modifiers(desc, desktop_sel)) {
1027                                                 handle_failure(SELECTOR_BAD_MODIFIERS, "query -d", *args, rsp);
1028                                                 free(desc);
1029                                                 goto end;
1030                                         }
1031                                         free(desc);
1032                                 } else if ((ret = desktop_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1033                                         handle_failure(ret, "query -d", *args, rsp);
1034                                         goto end;
1035                                 }
1036                         } else {
1037                                 trg.monitor = ref.monitor;
1038                                 trg.desktop = ref.desktop;
1039                         }
1040                 } else if (streq("-n", *args) || streq("--node", *args)) {
1041                         if (num > 1 && *(args + 1)[0] != OPT_CHR) {
1042                                 num--, args++;
1043                                 int ret;
1044                                 if ((*args)[0] == '.') {
1045                                         free(node_sel);
1046                                         node_sel = malloc(sizeof(node_select_t));
1047                                         *node_sel = make_node_select();
1048                                         char *desc = copy_string(*args, strlen(*args));
1049                                         if (!parse_node_modifiers(desc, node_sel)) {
1050                                                 handle_failure(SELECTOR_BAD_MODIFIERS, "query -n", *args, rsp);
1051                                                 free(desc);
1052                                                 goto end;
1053                                         }
1054                                         free(desc);
1055                                 } else if ((ret = node_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1056                                         handle_failure(ret, "query -n", *args, rsp);
1057                                         goto end;
1058                                 }
1059                         } else {
1060                                 trg = ref;
1061                                 if (ref.node == NULL) {
1062                                         fail(rsp, "");
1063                                         goto end;
1064                                 }
1065                         }
1066                 } else if (streq("--names", *args)) {
1067                         print_ids = false;
1068                 } else {
1069                         fail(rsp, "query: Unknown option: '%s'.\n", *args);
1070                         goto end;
1071                 }
1072                 num--, args++;
1073         }
1074
1075         if (d < 1) {
1076                 fail(rsp, "query: No commands given.\n");
1077                 goto end;
1078         }
1079
1080         if (d > 1) {
1081                 fail(rsp, "query: Multiple commands given.\n");
1082                 goto end;
1083         }
1084
1085         if (dom == DOMAIN_TREE && trg.monitor == NULL) {
1086                 fail(rsp, "query -T: No options given.\n");
1087                 goto end;
1088         }
1089
1090         if (!print_ids && (dom == DOMAIN_NODE || dom == DOMAIN_TREE)) {
1091                 fail(rsp, "query -%c: --names only applies to -M and -D.\n", dom == DOMAIN_NODE ? 'N' : 'T');
1092                 goto end;
1093         }
1094
1095         if (dom == DOMAIN_NODE) {
1096                 if (query_node_ids(&ref, &trg, node_sel, rsp) < 1) {
1097                         fail(rsp, "");
1098                 }
1099         } else if (dom == DOMAIN_DESKTOP) {
1100                 if (query_desktop_ids(&ref, &trg, desktop_sel, print_ids ? fprint_desktop_id : fprint_desktop_name, rsp) < 1) {
1101                         fail(rsp, "");
1102                 }
1103         } else if (dom == DOMAIN_MONITOR) {
1104                 if (query_monitor_ids(&ref, &trg, monitor_sel, print_ids ? fprint_monitor_id : fprint_monitor_name, rsp) < 1) {
1105                         fail(rsp, "");
1106                 }
1107         } else {
1108                 if (trg.node != NULL) {
1109                         query_node(trg.node, rsp);
1110                 } else if (trg.desktop != NULL) {
1111                         query_desktop(trg.desktop, rsp);
1112                 } else  {
1113                         query_monitor(trg.monitor, rsp);
1114                 }
1115                 fprintf(rsp, "\n");
1116         }
1117
1118 end:
1119         free(monitor_sel);
1120         free(desktop_sel);
1121         free(node_sel);
1122 }
1123
1124 void cmd_rule(char **args, int num, FILE *rsp)
1125 {
1126         if (num < 1) {
1127                 fail(rsp, "rule: Missing commands.\n");
1128                 return;
1129         }
1130
1131         while (num > 0) {
1132                 if (streq("-a", *args) || streq("--add", *args)) {
1133                         num--, args++;
1134                         if (num < 2) {
1135                                 fail(rsp, "rule %s: Not enough arguments.\n", *(args - 1));
1136                                 return;
1137                         }
1138                         rule_t *rule = make_rule();
1139                         char *class_name = strtok(*args, COL_TOK);
1140                         char *instance_name = strtok(NULL, COL_TOK);
1141                         snprintf(rule->class_name, sizeof(rule->class_name), "%s", class_name);
1142                         snprintf(rule->instance_name, sizeof(rule->instance_name), "%s", instance_name==NULL?MATCH_ANY:instance_name);
1143                         num--, args++;
1144                         size_t i = 0;
1145                         while (num > 0) {
1146                                 if (streq("-o", *args) || streq("--one-shot", *args)) {
1147                                         rule->one_shot = true;
1148                                 } else {
1149                                         for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++) {
1150                                                 rule->effect[i] = (*args)[j];
1151                                         }
1152                                         if (num > 1 && i < sizeof(rule->effect)) {
1153                                                 rule->effect[i++] = ' ';
1154                                         }
1155                                 }
1156                                 num--, args++;
1157                         }
1158                         rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0';
1159                         add_rule(rule);
1160                 } else if (streq("-r", *args) || streq("--remove", *args)) {
1161                         num--, args++;
1162                         if (num < 1) {
1163                                 fail(rsp, "rule %s: Not enough arguments.\n", *(args - 1));
1164                                 return;
1165                         }
1166                         uint16_t idx;
1167                         while (num > 0) {
1168                                 if (parse_index(*args, &idx)) {
1169                                         remove_rule_by_index(idx - 1);
1170                                 } else if (streq("tail", *args)) {
1171                                         remove_rule(rule_tail);
1172                                 } else if (streq("head", *args)) {
1173                                         remove_rule(rule_head);
1174                                 } else {
1175                                         remove_rule_by_cause(*args);
1176                                 }
1177                                 num--, args++;
1178                         }
1179                 } else if (streq("-l", *args) || streq("--list", *args)) {
1180                         list_rules(rsp);
1181                 } else {
1182                         fail(rsp, "rule: Unknown command: '%s'.\n", *args);
1183                         return;
1184                 }
1185                 num--, args++;
1186         }
1187 }
1188
1189 void cmd_wm(char **args, int num, FILE *rsp)
1190 {
1191         if (num < 1) {
1192                 fail(rsp, "wm: Missing commands.\n");
1193                 return;
1194         }
1195
1196         while (num > 0) {
1197                 if (streq("-d", *args) || streq("--dump-state", *args)) {
1198                         query_state(rsp);
1199                         fprintf(rsp, "\n");
1200                 } else if (streq("-l", *args) || streq("--load-state", *args)) {
1201                         num--, args++;
1202                         if (num < 1) {
1203                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1204                                 break;
1205                         }
1206                         if (!restore_state(*args)) {
1207                                 fail(rsp, "");
1208                                 break;
1209                         }
1210                 } else if (streq("-a", *args) || streq("--add-monitor", *args)) {
1211                         num--, args++;
1212                         if (num < 2) {
1213                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1214                                 break;
1215                         }
1216                         char *name = *args;
1217                         num--, args++;
1218                         xcb_rectangle_t r;
1219                         if (parse_rectangle(*args, &r)) {
1220                                 monitor_t *m = make_monitor(name, &r, XCB_NONE);
1221                                 add_monitor(m);
1222                                 add_desktop(m, make_desktop(NULL, XCB_NONE));
1223                         } else {
1224                                 fail(rsp, "wm %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1225                                 break;
1226                         }
1227                 } else if (streq("-O", *args) || streq("--reorder-monitors", *args)) {
1228                         num--, args++;
1229                         if (num < 1) {
1230                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1231                                 return;
1232                         }
1233                         monitor_t *m = mon_head;
1234                         while (m != NULL && num > 0) {
1235                                 monitor_t *next = m->next;
1236                                 coordinates_t dst;
1237                                 if (locate_monitor(*args, &dst)) {
1238                                         swap_monitors(m, dst.monitor);
1239                                         if (next == dst.monitor) {
1240                                                 next = m;
1241                                         }
1242                                 }
1243                                 m = next;
1244                                 num--, args++;
1245                         }
1246                 } else if (streq("-o", *args) || streq("--adopt-orphans", *args)) {
1247                         adopt_orphans();
1248                 } else if (streq("-g", *args) || streq("--get-status", *args)) {
1249                         print_report(rsp);
1250                 } else if (streq("-h", *args) || streq("--record-history", *args)) {
1251                         num--, args++;
1252                         if (num < 1) {
1253                                 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1254                                 break;
1255                         }
1256                         bool b;
1257                         if (parse_bool(*args, &b)) {
1258                                 record_history = b;
1259                         } else {
1260                                 fail(rsp, "wm %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1261                                 break;
1262                         }
1263                 } else if (streq("-r", *args) || streq("--restart", *args)) {
1264                         running = false;
1265                         restart = true;
1266                         break;
1267                 } else {
1268                         fail(rsp, "wm: Unknown command: '%s'.\n", *args);
1269                         break;
1270                 }
1271                 num--, args++;
1272         }
1273 }
1274
1275 void cmd_subscribe(char **args, int num, FILE *rsp)
1276 {
1277         int field = 0;
1278         int count = -1;
1279         FILE *stream = rsp;
1280         char *fifo_path = NULL;
1281         subscriber_mask_t mask;
1282
1283         while (num > 0) {
1284                 if (streq("-c", *args) || streq("--count", *args)) {
1285                         num--, args++;
1286                         if (num < 1) {
1287                                 fail(rsp, "subscribe %s: Not enough arguments.\n", *(args - 1));
1288                                 goto failed;
1289                         }
1290                         if (sscanf(*args, "%i", &count) != 1 || count < 1) {
1291                                 fail(rsp, "subscribe %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1292                                 goto failed;
1293                         }
1294                 } else if (streq("-f", *args) || streq("--fifo", *args)) {
1295                         fifo_path = mktempfifo(FIFO_TEMPLATE);
1296                         if (fifo_path == NULL) {
1297                                 fail(rsp, "subscribe %s: Can't create FIFO.\n", *(args - 1));
1298                                 goto failed;
1299                         }
1300                 } else if (parse_subscriber_mask(*args, &mask)) {
1301                         field |= mask;
1302                 } else {
1303                         fail(rsp, "subscribe: Invalid argument: '%s'.\n", *args);
1304                         goto failed;
1305                 }
1306                 num--, args++;
1307         }
1308
1309         if (field == 0) {
1310                 field = SBSC_MASK_REPORT;
1311         }
1312
1313         if (fifo_path) {
1314                 fprintf(rsp, "%s\n", fifo_path);
1315                 fflush(rsp);
1316                 fclose(rsp);
1317
1318                 stream = fopen(fifo_path, "w");
1319
1320                 if (stream == NULL) {
1321                         perror("subscribe: fopen");
1322                         goto free_fifo_path;
1323                 }
1324         }
1325
1326         subscriber_list_t *sb = make_subscriber(stream, fifo_path, field, count);
1327         add_subscriber(sb);
1328         return;
1329
1330 failed:
1331         fflush(rsp);
1332         fclose(rsp);
1333
1334 free_fifo_path:
1335         if (fifo_path) {
1336                 unlink(fifo_path);
1337                 free(fifo_path);
1338         }
1339 }
1340
1341 void cmd_quit(char **args, int num, FILE *rsp)
1342 {
1343         if (num > 0 && sscanf(*args, "%i", &exit_status) != 1) {
1344                 fail(rsp, "%s: Invalid argument: '%s'.\n", *(args - 1), *args);
1345                 return;
1346         }
1347         running = false;
1348 }
1349
1350 void cmd_config(char **args, int num, FILE *rsp)
1351 {
1352         if (num < 1) {
1353                 fail(rsp, "config: Missing arguments.\n");
1354                 return;
1355         }
1356
1357         coordinates_t ref = {mon, mon->desk, mon->desk->focus};
1358         coordinates_t trg = {NULL, NULL, NULL};
1359
1360         while (num > 0 && (*args)[0] == OPT_CHR) {
1361                 if (streq("-m", *args) || streq("--monitor", *args)) {
1362                         num--, args++;
1363                         if (num < 1) {
1364                                 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1365                                 return;
1366                         }
1367                         int ret;
1368                         if ((ret = monitor_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1369                                 handle_failure(ret, "config -m", *args, rsp);
1370                                 return;
1371                         }
1372                 } else if (streq("-d", *args) || streq("--desktop", *args)) {
1373                         num--, args++;
1374                         if (num < 1) {
1375                                 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1376                                 return;
1377                         }
1378                         int ret;
1379                         if ((ret = desktop_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1380                                 handle_failure(ret, "config -d", *args, rsp);
1381                                 return;
1382                         }
1383                 } else if (streq("-n", *args) || streq("--node", *args)) {
1384                         num--, args++;
1385                         if (num < 1) {
1386                                 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1387                                 return;
1388                         }
1389                         int ret;
1390                         if ((ret = node_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1391                                 handle_failure(ret, "config -n", *args, rsp);
1392                                 return;
1393                         }
1394                 } else {
1395                         fail(rsp, "config: Unknown option: '%s'.\n", *args);
1396                         return;
1397                 }
1398                 num--, args++;
1399         }
1400         if (num == 2) {
1401                 set_setting(trg, *args, *(args + 1), rsp);
1402         } else if (num == 1) {
1403                 get_setting(trg, *args, rsp);
1404         } else {
1405                 fail(rsp, "config: Was expecting 1 or 2 arguments, received %i.\n", num);
1406         }
1407 }
1408
1409 void set_setting(coordinates_t loc, char *name, char *value, FILE *rsp)
1410 {
1411         bool colors_changed = false;
1412 #define SET_DEF_DEFMON_DEFDESK_WIN(k, v) \
1413                 if (loc.node != NULL) { \
1414                         for (node_t *n = first_extrema(loc.node); n != NULL; n = next_leaf(n, loc.node)) { \
1415                                 if (n->client != NULL) { \
1416                                         n->client->k = v; \
1417                                 } \
1418                         } \
1419                 } else if (loc.desktop != NULL) { \
1420                         loc.desktop->k = v; \
1421                         for (node_t *n = first_extrema(loc.desktop->root); n != NULL; n = next_leaf(n, loc.desktop->root)) { \
1422                                 if (n->client != NULL) { \
1423                                         n->client->k = v; \
1424                                 } \
1425                         } \
1426                 } else if (loc.monitor != NULL) { \
1427                         loc.monitor->k = v; \
1428                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) { \
1429                                 d->k = v; \
1430                                 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { \
1431                                         if (n->client != NULL) { \
1432                                                 n->client->k = v; \
1433                                         } \
1434                                 } \
1435                         } \
1436                 } else { \
1437                         k = v; \
1438                         for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1439                                 m->k = v; \
1440                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) { \
1441                                         d->k = v; \
1442                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { \
1443                                                 if (n->client != NULL) { \
1444                                                         n->client->k = v; \
1445                                                 } \
1446                                         } \
1447                                 } \
1448                         } \
1449                 }
1450         if (streq("border_width", name)) {
1451                 unsigned int bw;
1452                 if (sscanf(value, "%u", &bw) != 1) {
1453                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1454                         return;
1455                 }
1456                 SET_DEF_DEFMON_DEFDESK_WIN(border_width, bw)
1457 #undef SET_DEF_DEFMON_DEFDESK_WIN
1458 #define SET_DEF_DEFMON_DESK(k, v) \
1459                 if (loc.desktop != NULL) { \
1460                         loc.desktop->k = v; \
1461                 } else if (loc.monitor != NULL) { \
1462                         loc.monitor->k = v; \
1463                         for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) { \
1464                                 d->k = v; \
1465                         } \
1466                 } else { \
1467                         k = v; \
1468                         for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1469                                 m->k = v; \
1470                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) { \
1471                                         d->k = v; \
1472                                 } \
1473                         } \
1474                 }
1475         } else if (streq("window_gap", name)) {
1476                 int wg;
1477                 if (sscanf(value, "%i", &wg) != 1) {
1478                         fail(rsp, "");
1479                         return;
1480                 }
1481                 SET_DEF_DEFMON_DESK(window_gap, wg)
1482 #undef SET_DEF_DEFMON_DESK
1483 #define SET_DEF_MON_DESK(k, v) \
1484                 if (loc.desktop != NULL) { \
1485                         loc.desktop->k = v; \
1486                 } else if (loc.monitor != NULL) { \
1487                         loc.monitor->k = v; \
1488                 } else { \
1489                         k = v; \
1490                         for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1491                                 m->k = v; \
1492                         } \
1493                 }
1494         } else if (streq("top_padding", name)) {
1495                 int tp;
1496                 if (sscanf(value, "%i", &tp) != 1) {
1497                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1498                         return;
1499                 }
1500                 SET_DEF_MON_DESK(padding.top, tp)
1501         } else if (streq("right_padding", name)) {
1502                 int rp;
1503                 if (sscanf(value, "%i", &rp) != 1) {
1504                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1505                         return;
1506                 }
1507                 SET_DEF_MON_DESK(padding.right, rp)
1508         } else if (streq("bottom_padding", name)) {
1509                 int bp;
1510                 if (sscanf(value, "%i", &bp) != 1) {
1511                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1512                         return;
1513                 }
1514                 SET_DEF_MON_DESK(padding.bottom, bp)
1515         } else if (streq("left_padding", name)) {
1516                 int lp;
1517                 if (sscanf(value, "%i", &lp) != 1) {
1518                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1519                         return;
1520                 }
1521                 SET_DEF_MON_DESK(padding.left, lp)
1522 #undef SET_DEF_MON_DESK
1523         } else if (streq("top_monocle_padding", name)) {
1524                 if (sscanf(value, "%i", &monocle_padding.top) != 1) {
1525                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1526                 }
1527         } else if (streq("right_monocle_padding", name)) {
1528                 if (sscanf(value, "%i", &monocle_padding.right) != 1) {
1529                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1530                 }
1531         } else if (streq("bottom_monocle_padding", name)) {
1532                 if (sscanf(value, "%i", &monocle_padding.bottom) != 1) {
1533                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1534                 }
1535         } else if (streq("left_monocle_padding", name)) {
1536                 if (sscanf(value, "%i", &monocle_padding.left) != 1) {
1537                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1538                 }
1539 #define SET_STR(s) \
1540         } else if (streq(#s, name)) { \
1541                 if (snprintf(s, sizeof(s), "%s", value) < 0) { \
1542                         fail(rsp, ""); \
1543                         return; \
1544                 }
1545         SET_STR(external_rules_command)
1546         SET_STR(status_prefix)
1547 #undef SET_STR
1548         } else if (streq("split_ratio", name)) {
1549                 double r;
1550                 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1) {
1551                         split_ratio = r;
1552                 } else {
1553                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1554                         return;
1555                 }
1556                 return;
1557 #define SET_COLOR(s) \
1558         } else if (streq(#s, name)) { \
1559                 if (!is_hex_color(value)) { \
1560                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1561                         return; \
1562                 } else { \
1563                         snprintf(s, sizeof(s), "%s", value); \
1564                         colors_changed = true; \
1565                 }
1566         SET_COLOR(normal_border_color)
1567         SET_COLOR(active_border_color)
1568         SET_COLOR(focused_border_color)
1569         SET_COLOR(presel_feedback_color)
1570 #undef SET_COLOR
1571         } else if (streq("initial_polarity", name)) {
1572                 child_polarity_t p;
1573                 if (parse_child_polarity(value, &p)) {
1574                         initial_polarity = p;
1575                 } else {
1576                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1577                         return;
1578                 }
1579         } else if (streq("automatic_scheme", name)) {
1580                 automatic_scheme_t a;
1581                 if (parse_automatic_scheme(value, &a)) {
1582                         automatic_scheme = a;
1583                 } else {
1584                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1585                         return;
1586                 }
1587         } else if (streq("mapping_events_count", name)) {
1588                 if (sscanf(value, "%" SCNi8, &mapping_events_count) != 1) {
1589                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1590                         return;
1591                 }
1592         } else if (streq("directional_focus_tightness", name)) {
1593                 tightness_t p;
1594                 if (parse_tightness(value, &p)) {
1595                         directional_focus_tightness = p;
1596                 } else {
1597                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1598                         return;
1599                 }
1600         } else if (streq("ignore_ewmh_fullscreen", name)) {
1601                 state_transition_t m;
1602                 if (parse_state_transition(value, &m)) {
1603                         ignore_ewmh_fullscreen = m;
1604                 } else {
1605                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1606                         return;
1607                 }
1608         } else if (streq("pointer_modifier", name)) {
1609                 if (parse_modifier_mask(value, &pointer_modifier)) {
1610                         ungrab_buttons();
1611                         grab_buttons();
1612                 } else {
1613                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1614                         return;
1615                 }
1616         } else if (streq("pointer_motion_interval", name)) {
1617                 if (sscanf(value, "%u", &pointer_motion_interval) != 1) {
1618                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1619                         return;
1620                 }
1621         } else if (streq("pointer_action1", name) ||
1622                    streq("pointer_action2", name) ||
1623                    streq("pointer_action3", name)) {
1624                 int index = name[14] - '1';
1625                 if (parse_pointer_action(value, &pointer_actions[index])) {
1626                         ungrab_buttons();
1627                         grab_buttons();
1628                 } else {
1629                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1630                         return;
1631                 }
1632         } else if (streq("click_to_focus", name)) {
1633                 if (parse_button_index(value, &click_to_focus)) {
1634                         ungrab_buttons();
1635                         grab_buttons();
1636                 } else {
1637                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1638                         return;
1639                 }
1640         } else if (streq("focus_follows_pointer", name)) {
1641                 bool b;
1642                 if (parse_bool(value, &b)) {
1643                         if (b == focus_follows_pointer) {
1644                                 fail(rsp, "");
1645                                 return;
1646                         }
1647                         focus_follows_pointer = b;
1648                         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1649                                 if (focus_follows_pointer) {
1650                                         window_show(m->root);
1651                                 } else {
1652                                         window_hide(m->root);
1653                                 }
1654                                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1655                                         listen_enter_notify(d->root, focus_follows_pointer);
1656                                 }
1657                         }
1658                         if (focus_follows_pointer) {
1659                                 update_motion_recorder();
1660                         } else {
1661                                 disable_motion_recorder();
1662                         }
1663                         return;
1664                 } else {
1665                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1666                         return;
1667                 }
1668 #define SET_BOOL(s) \
1669         } else if (streq(#s, name)) { \
1670                 if (!parse_bool(value, &s)) { \
1671                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1672                         return; \
1673                 }
1674                 SET_BOOL(presel_feedback)
1675                 SET_BOOL(borderless_monocle)
1676                 SET_BOOL(gapless_monocle)
1677                 SET_BOOL(single_monocle)
1678                 put_status(SBSC_MASK_REPORT);
1679                 SET_BOOL(swallow_first_click)
1680                 SET_BOOL(pointer_follows_focus)
1681                 SET_BOOL(pointer_follows_monitor)
1682                 SET_BOOL(ignore_ewmh_focus)
1683                 SET_BOOL(ignore_ewmh_struts)
1684                 SET_BOOL(center_pseudo_tiled)
1685                 SET_BOOL(honor_size_hints)
1686                 SET_BOOL(removal_adjustment)
1687 #undef SET_BOOL
1688 #define SET_MON_BOOL(s) \
1689         } else if (streq(#s, name)) { \
1690                 if (!parse_bool(value, &s)) { \
1691                         fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1692                         return; \
1693                 } \
1694                 if (s) { \
1695                         update_monitors(); \
1696                 }
1697                 SET_MON_BOOL(remove_disabled_monitors)
1698                 SET_MON_BOOL(remove_unplugged_monitors)
1699                 SET_MON_BOOL(merge_overlapping_monitors)
1700 #undef SET_MON_BOOL
1701         } else {
1702                 fail(rsp, "config: Unknown setting: '%s'.\n", name);
1703                 return;
1704         }
1705
1706         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1707                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1708                         arrange(m, d);
1709                         if (colors_changed) {
1710                                 update_colors_in(d->root, d, m);
1711                         }
1712                 }
1713         }
1714 }
1715
1716 void get_setting(coordinates_t loc, char *name, FILE* rsp)
1717 {
1718         if (streq("split_ratio", name)) {
1719                 fprintf(rsp, "%lf", split_ratio);
1720         } else if (streq("border_width", name)) {
1721                 if (loc.node != NULL) {
1722                         for (node_t *n = first_extrema(loc.node); n != NULL; n = next_leaf(n, loc.node)) {
1723                                 if (n->client != NULL) {
1724                                         fprintf(rsp, "%u", n->client->border_width);
1725                                         break;
1726                                 }
1727                         }
1728                 } else if (loc.desktop != NULL) {
1729                         fprintf(rsp, "%u", loc.desktop->border_width);
1730                 } else if (loc.monitor != NULL) {
1731                         fprintf(rsp, "%u", loc.monitor->border_width);
1732                 } else {
1733                         fprintf(rsp, "%u", border_width);
1734                 }
1735         } else if (streq("window_gap", name)) {
1736                 if (loc.desktop != NULL) {
1737                         fprintf(rsp, "%i", loc.desktop->window_gap);
1738                 } else if (loc.monitor != NULL) {
1739                         fprintf(rsp, "%i", loc.monitor->window_gap);
1740                 } else {
1741                         fprintf(rsp, "%i", window_gap);
1742                 }
1743 #define GET_DEF_MON_DESK(k) \
1744                 if (loc.desktop != NULL) { \
1745                         fprintf(rsp, "%i", loc.desktop->k); \
1746                 } else if (loc.monitor != NULL) { \
1747                         fprintf(rsp, "%i", loc.monitor->k); \
1748                 } else { \
1749                         fprintf(rsp, "%i", k); \
1750                 }
1751         } else if (streq("top_padding", name)) {
1752                 GET_DEF_MON_DESK(padding.top)
1753         } else if (streq("right_padding", name)) {
1754                 GET_DEF_MON_DESK(padding.right)
1755         } else if (streq("bottom_padding", name)) {
1756                 GET_DEF_MON_DESK(padding.bottom)
1757         } else if (streq("left_padding", name)) {
1758                 GET_DEF_MON_DESK(padding.left)
1759 #undef GET_DEF_MON_DESK
1760         } else if (streq("top_monocle_padding", name)) {
1761                 fprintf(rsp, "%i", monocle_padding.top);
1762         } else if (streq("right_monocle_padding", name)) {
1763                 fprintf(rsp, "%i", monocle_padding.right);
1764         } else if (streq("bottom_monocle_padding", name)) {
1765                 fprintf(rsp, "%i", monocle_padding.bottom);
1766         } else if (streq("left_monocle_padding", name)) {
1767                 fprintf(rsp, "%i", monocle_padding.left);
1768         } else if (streq("external_rules_command", name)) {
1769                 fprintf(rsp, "%s", external_rules_command);
1770         } else if (streq("status_prefix", name)) {
1771                 fprintf(rsp, "%s", status_prefix);
1772         } else if (streq("initial_polarity", name)) {
1773                 fprintf(rsp, "%s", CHILD_POL_STR(initial_polarity));
1774         } else if (streq("automatic_scheme", name)) {
1775                 fprintf(rsp, "%s", AUTO_SCM_STR(automatic_scheme));
1776         } else if (streq("mapping_events_count", name)) {
1777                 fprintf(rsp, "%" PRIi8, mapping_events_count);
1778         } else if (streq("directional_focus_tightness", name)) {
1779                 fprintf(rsp, "%s", TIGHTNESS_STR(directional_focus_tightness));
1780         } else if (streq("ignore_ewmh_fullscreen", name)) {
1781                 print_ignore_request(ignore_ewmh_fullscreen, rsp);
1782         } else if (streq("pointer_modifier", name)) {
1783                 print_modifier_mask(pointer_modifier, rsp);
1784         } else if (streq("click_to_focus", name)) {
1785                 print_button_index(click_to_focus, rsp);
1786         } else if (streq("pointer_motion_interval", name)) {
1787                 fprintf(rsp, "%u", pointer_motion_interval);
1788         } else if (streq("pointer_action1", name) ||
1789                    streq("pointer_action2", name) ||
1790                    streq("pointer_action3", name)) {
1791                 int index = name[14] - '1';
1792                 print_pointer_action(pointer_actions[index], rsp);
1793 #define GET_COLOR(s) \
1794         } else if (streq(#s, name)) { \
1795                 fprintf(rsp, "%s", s);
1796         GET_COLOR(normal_border_color)
1797         GET_COLOR(active_border_color)
1798         GET_COLOR(focused_border_color)
1799         GET_COLOR(presel_feedback_color)
1800 #undef GET_COLOR
1801 #define GET_BOOL(s) \
1802         } else if (streq(#s, name)) { \
1803                 fprintf(rsp, "%s", BOOL_STR(s));
1804         GET_BOOL(presel_feedback)
1805         GET_BOOL(borderless_monocle)
1806         GET_BOOL(gapless_monocle)
1807         GET_BOOL(single_monocle)
1808         GET_BOOL(swallow_first_click)
1809         GET_BOOL(focus_follows_pointer)
1810         GET_BOOL(pointer_follows_focus)
1811         GET_BOOL(pointer_follows_monitor)
1812         GET_BOOL(ignore_ewmh_focus)
1813         GET_BOOL(ignore_ewmh_struts)
1814         GET_BOOL(center_pseudo_tiled)
1815         GET_BOOL(honor_size_hints)
1816         GET_BOOL(removal_adjustment)
1817         GET_BOOL(remove_disabled_monitors)
1818         GET_BOOL(remove_unplugged_monitors)
1819         GET_BOOL(merge_overlapping_monitors)
1820 #undef GET_BOOL
1821         } else {
1822                 fail(rsp, "config: Unknown setting: '%s'.\n", name);
1823                 return;
1824         }
1825         fprintf(rsp, "\n");
1826 }
1827
1828 void handle_failure(int code, char *src, char *val, FILE *rsp)
1829 {
1830         switch (code) {
1831                 case SELECTOR_BAD_DESCRIPTOR:
1832                         fail(rsp, "%s: Invalid descriptor found in '%s'.\n", src, val);
1833                         break;
1834                 case SELECTOR_BAD_MODIFIERS:
1835                         fail(rsp, "%s: Invalid modifier found in '%s'.\n", src, val);
1836                         break;
1837                 case SELECTOR_INVALID:
1838                         fail(rsp, "");
1839                         break;
1840         }
1841 }
1842
1843 void fail(FILE *rsp, char *fmt, ...)
1844 {
1845         fprintf(rsp, FAILURE_MESSAGE);
1846         va_list ap;
1847         va_start(ap, fmt);
1848         vfprintf(rsp, fmt, ap);
1849         va_end(ap);
1850 }