]> git.lizzy.rs Git - bspwm.git/blobdiff - bspc.c
Version 0.9
[bspwm.git] / bspc.c
diff --git a/bspc.c b/bspc.c
index 1617afc0659ec21c2e4ac6a031c7eb80bb395380..b50a8fcee57a20a290a82eb887f5aae1b38a08c9 100644 (file)
--- a/bspc.c
+++ b/bspc.c
@@ -1,23 +1,23 @@
-/* * Copyright (c) 2012-2013 Bastien Dejean
+/* Copyright (c) 2012, Bastien Dejean
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
  *
- *  * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
  *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 int main(int argc, char *argv[])
 {
-    int fd;
-    struct sockaddr_un sock_address;
-    char msg[BUFSIZ], rsp[BUFSIZ];
+       int fd;
+       struct sockaddr_un sock_address;
+       char msg[BUFSIZ], rsp[BUFSIZ];
 
-    if (argc < 2)
-        err("No arguments given.\n");
+       if (argc < 2)
+               err("No arguments given.\n");
 
-    sock_address.sun_family = AF_UNIX;
-    char *sp = getenv(SOCKET_ENV_VAR);
-    if (sp != NULL)
-        snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp);
-    else
-        snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, getenv("DISPLAY"));
+       sock_address.sun_family = AF_UNIX;
+       char *sp;
 
-    argc--, argv++;
-    int msg_len = 0;
+       if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+               err("Failed to create the socket.\n");
 
-    for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, rem -= n, argc--, argv++) {
-        n = snprintf(msg + offset, rem, "%s%c", *argv, 0);
-        msg_len += n;
-    }
+       sp = getenv(SOCKET_ENV_VAR);
+       if (sp != NULL) {
+               snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp);
+       } else {
+               char *host = NULL;
+               int dn = 0, sn = 0;
+               if (xcb_parse_display(NULL, &host, &dn, &sn) != 0)
+                       snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, host, dn, sn);
+               free(host);
+       }
 
-    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
-        err("Failed to create the socket.\n");
+       if (connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
+               err("Failed to connect to the socket.\n");
 
-    if (connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
-        err("Failed to connect to the socket.\n");
+       argc--, argv++;
+       int msg_len = 0;
 
-    if (send(fd, msg, msg_len, 0) == -1)
-        err("Failed to send the data.\n");
+       for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, rem -= n, argc--, argv++) {
+               n = snprintf(msg + offset, rem, "%s%c", *argv, 0);
+               msg_len += n;
+       }
 
-    int ret = EXIT_SUCCESS, nb;
-    while ((nb = recv(fd, rsp, sizeof(rsp), 0)) > 0) {
-        if (nb == 1 && rsp[0] == MESSAGE_FAILURE) {
-            ret = EXIT_FAILURE;
-        } else {
-            int end = MIN(nb, (int) sizeof(rsp) - 1);
-            rsp[end--] = '\0';
-            while (end >= 0 && isspace(rsp[end]))
-                rsp[end--] = '\0';
-            printf("%s\n", rsp);
-            fflush(stdout);
-        }
-    }
+       if (send(fd, msg, msg_len, 0) == -1)
+               err("Failed to send the data.\n");
 
-    close(fd);
-    return ret;
+       int ret = 0, nb;
+       while ((nb = recv(fd, rsp, sizeof(rsp), 0)) > 0) {
+               if (nb == 1 && rsp[0] < MSG_LENGTH) {
+                       ret = rsp[0];
+                       if (ret == MSG_UNKNOWN) {
+                               warn("Unknown command.\n");
+                       } else if (ret == MSG_SYNTAX) {
+                               warn("Invalid syntax.\n");
+                       }
+               } else {
+                       int end = MIN(nb, (int) sizeof(rsp) - 1);
+                       rsp[end--] = '\0';
+                       while (end >= 0 && isspace(rsp[end]))
+                               rsp[end--] = '\0';
+                       printf("%s\n", rsp);
+                       fflush(stdout);
+               }
+       }
+
+       close(fd);
+       return ret;
 }