... and because I'm on a run, yet another patch. It enables users to use
-1 as an alias to "all screens" when running a UICB. Also, it replaces a
usage of atoi with strtol and strtok with strsep, which are similar
functions that allow more fine grained error detection and (with strsep)
are implemented cleaner in most libc implementations.

-- 
    Gregor Best
From cc41235c9d8169dc6252489cfd962753206c5c22 Mon Sep 17 00:00:00 2001
From: Gregor Best <g...@ring0.de>
Date: Thu, 23 May 2013 12:19:56 +0200
Subject: [PATCH] UICB: allow running a uicb on all screens

-1 is an alias to "run on all screens"

Signed-off-by: Gregor Best <g...@ring0.de>
---
 awesome-client.1.txt |  3 +++
 uicb.c               | 58 ++++++++++++++++++++++++++++------------------------
 2 files changed, 34 insertions(+), 27 deletions(-)

diff --git a/awesome-client.1.txt b/awesome-client.1.txt
index 909460b..39368fe 100644
--- a/awesome-client.1.txt
+++ b/awesome-client.1.txt
@@ -44,6 +44,9 @@ To zoom focused window on screen 0:
 
     echo 0 client_zoom | awesome-client
 
+To execute a command on all active screens, use -1 as the screen number:
+
+    echo -1 tag_view 3 | awesome-client
 
 SEE ALSO
 --------
diff --git a/uicb.c b/uicb.c
index 68dba6f..2e177ca 100644
--- a/uicb.c
+++ b/uicb.c
@@ -135,6 +135,23 @@ uicb_spawn(int screen, char *arg)
     wait(0);
 }
 
+static void
+__uicb_run_args(Uicb* fun, int screen, char *cmd) {
+    char *argcpy = NULL;
+    const char *arg;
+    ssize_t len;
+
+    if (cmd && (a_strlen(cmd) > 0)) {
+        ssize_t len = a_strlen(cmd);
+        argcpy = p_new(char, len);
+        a_strncpy(argcpy, len + 1, cmd, len);
+    }
+
+    fun(screen, argcpy);
+    if (argcpy)
+        p_delete(&argcpy);
+}
+
 /** Run the uicb
  * \param cmd the uicb command to parse
  * \return 0 on succes, -1 on failure
@@ -144,31 +161,25 @@ __uicb_run(char *cmd)
 {
     char *p, *argcpy;
     const char *arg;
+    char *tmp;
     int screen;
     ssize_t len;
     Uicb *uicb;
 
     len = a_strlen(cmd);
-    p = strtok(cmd, " ");
-    if (!p)
-    {
-        warn("ignoring malformed command\n");
-        return -1;
-    }
-    screen = atoi(cmd);
-    if(screen >= globalconf.screens_info->nscreen || screen < 0)
-    {
-        warn("invalid screen specified: %i\n", screen);
+
+    p = strsep(&cmd, " ");
+    screen = (int) strtol(p, &tmp, 10);
+    if ((*tmp != '\0') || (screen > globalconf.screens_info->nscreen) || 
(screen < -1)) {
+        warn("invalid screen: %s\n", p);
         return -1;
     }
 
-    p = strtok(NULL, " ");
-    if (!p)
-    {
-        warn("ignoring malformed command.\n");
+    p = strsep(&cmd, " ");
+    if ((!p) || (*p == '\0')) {
+        warn("ignoring malformed command\n");
         return -1;
     }
-
     uicb = name_func_lookup(p, UicbList);
     if (!uicb)
     {
@@ -176,18 +187,11 @@ __uicb_run(char *cmd)
         return -1;
     }
 
-    if (p + a_strlen(p) < cmd + len)
-    {
-        arg = p + a_strlen(p) + 1;
-        len = a_strlen(arg);
-        /* Allow our callees to modify this string. */
-        argcpy = p_new(char, len + 1);
-        a_strncpy(argcpy, len + 1,  arg, len);
-        uicb(screen, argcpy);
-        p_delete(&argcpy);
-    }
-    else
-        uicb(screen, NULL);
+    if (screen == -1) {
+        for (int x = 0; x < globalconf.screens_info->nscreen; x++)
+            __uicb_run_args(uicb, x, cmd);
+    } else
+        __uicb_run_args(uicb, screen, cmd);
 
     return 0;
 }
-- 
1.8.2.2

Attachment: pgp50aJPoWusG.pgp
Description: PGP signature

Reply via email to