]> git.lizzy.rs Git - bspwm.git/blobdiff - bspc.c
Enhance external rules example scripts
[bspwm.git] / bspc.c
diff --git a/bspc.c b/bspc.c
index 3dc8547ee8a52505f91300ba16032d6fb548fb3c..5a5b1511c42ecb1b8f2171e35a8ab6c229af72ab 100644 (file)
--- a/bspc.c
+++ b/bspc.c
@@ -1,80 +1,83 @@
-#include <stdarg.h>
-#include <stdio.h>
+/* * Copyright (c) 2012-2013 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:
+ *
+ *  * 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.
+ *
+ * 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
+ * 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
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 #include <stdlib.h>
-#include <string.h>
+#ifdef __OpenBSD__
+#include <sys/types.h>
+#endif
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <ctype.h>
+#include "helpers.h"
 #include "common.h"
 
-static void logmsg(FILE *stream, char *fmt, va_list ap) {
-    vfprintf(stream, fmt, ap);
-}
-
-static void warn(char *warnfmt, ...) {
-    va_list ap;
-    va_start(ap, warnfmt);
-    logmsg(stderr, warnfmt, ap);
-    va_end(ap);
-}
-
-__attribute__((noreturn))
-static void err(char *errfmt, ...) {
-    va_list ap;
-    va_start(ap, errfmt);
-    logmsg(stderr, errfmt, ap);
-    va_end(ap);
-    exit(EXIT_FAILURE);
-}
-
 int main(int argc, char *argv[])
 {
-    int sock_fd;
+    int fd;
     struct sockaddr_un sock_address;
-    size_t msglen = 0;
-    char msg[BUFSIZ];
-    char rsp[BUFSIZ];
+    char msg[BUFSIZ], rsp[BUFSIZ];
 
     if (argc < 2)
-        err("invalid number of arguments: %d\n", argc);
+        err("No arguments given.\n");
 
-    char *sock_path = getenv(SOCKET_ENV_VAR);
-    if (sock_path == NULL || strlen(sock_path) == 0)
-        warn("environmental variable '%s' is not set or empty - using default value: %s\n", SOCKET_ENV_VAR, DEFAULT_SOCKET_PATH);
-    else if (sizeof(sock_address.sun_path) <= strlen(sock_path))
-        err("value too long for environmental variable '%s'\n", SOCKET_ENV_VAR);
+    char *sp = getenv(SOCKET_ENV_VAR);
 
     sock_address.sun_family = AF_UNIX;
-    strncpy(sock_address.sun_path, (sock_path == NULL ? DEFAULT_SOCKET_PATH: sock_path), sizeof(sock_address.sun_path));
-    sock_address.sun_path[sizeof(sock_address.sun_path) - 1] = 0;
+    snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
 
-    for (int offset = 0, len = BUFSIZ, n = 0; --argc && ++argv && len > 0; offset += n, len -= n)
-        n = snprintf(msg + offset, len, "%s ", *argv);
+    argc--, argv++;
+    int msg_len = 0;
 
-    msglen = strlen(msg);
-    if (msg[msglen - 1] ==  ' ')
-        msg[--msglen] = '\0';
+    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;
+    }
 
-    sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
-    if (sock_fd == -1)
-        err("failed to create socket\n");
+    if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+        err("Failed to create the socket.\n");
 
-    if (connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
-        err("failed to connect to socket\n");
+    if (connect(fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
+        err("Failed to connect to the socket.\n");
 
-    if (send(sock_fd, msg, msglen, 0) == -1)
-        err("failed to send data\n");
+    if (send(fd, msg, msg_len, 0) == -1)
+        err("Failed to send the data.\n");
 
-    int n = recv(sock_fd, rsp, sizeof(rsp), 0);
-    if (n == -1)
-        err("failed to get response\n");
-    else if (n > 0) {
-        rsp[n] = '\0';
-        printf("%s\n", rsp);
+    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 (sock_fd)
-        close(sock_fd);
-
-    return EXIT_SUCCESS;
+    close(fd);
+    return ret;
 }