changeset 125:d03ebefb92a6

Improve internal command handling, use fork() and execlp() instead of system() for opening profiles into external browser's tabs.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 29 Oct 2010 16:59:35 +0300
parents fe4d5f3b486c
children 9ae3d87a686f
files nnchat.c
diffstat 1 files changed, 19 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Fri Oct 29 16:41:08 2010 +0300
+++ b/nnchat.c	Fri Oct 29 16:59:35 2010 +0300
@@ -543,6 +543,11 @@
     return 1;
 }
 
+char * trimLeft(char *buf)
+{
+    while (*buf != 0 && isspace(*buf)) buf++;
+    return buf;
+}
 
 int handleUserInput(const int sock, char *buf, size_t bufLen)
 {
@@ -561,7 +566,7 @@
     } else if (!strncasecmp(buf, "/color ", 7)) {
         /* Change color */
         int tmpInt;
-        if ((tmpInt = getHexColorDef(buf+7)) < 0) {
+        if ((tmpInt = getHexColorDef(trimLeft(buf + 7))) < 0) {
             printMsg("Invalid color value '%s'\n", buf+7);
             return 1;
         }
@@ -571,25 +576,30 @@
         return 0;
     } else if (!strncasecmp(buf, "/w ", 3)) {
         /* Open given username's profile via firefox in a new tab */
-        char *name = buf + 3, *browser = getenv("BROWSER");
-        while (*name && isspace(*name)) name++;
+        char *name = trimLeft(buf + 3),
+             *browser = getenv("BROWSER");
+        pid_t pid;
 
         if (browser == NULL)
             browser = "firefox";
 
+        printMsg("Opening profile for: '%s'\n", name);
+
         tmpStr = nn_encode_str1(name);
-        snprintf(tmpBuf, sizeof(tmpBuf), "%s -remote \"openurl(http://www.newbienudes.com/profile/%s/,new-tab)\"",
-            browser, tmpStr);
+        snprintf(tmpBuf, sizeof(tmpBuf), "openurl(http://www.newbienudes.com/profile/%s/,new-tab)", tmpStr);
         th_free(tmpStr);
 
-        printMsg("Opening profile for: '%s'\n", name);
-        printMsg("%s\n", tmpBuf);
-        system(tmpBuf);
+        if ((pid = fork()) < 0) {
+            printMsg("Could not create sub-process!\n");
+        } else if (pid == 0) {
+            execlp(browser, browser, "-remote", tmpBuf, NULL);
+            exit(errno);
+        }
         return 0;
     } else if (!strncasecmp(buf, "/to ", 4)) {
         /* Set private messaging target */
         th_free(setTarget);
-        setTarget = th_strdup(buf + 4);
+        setTarget = th_strdup(trimLeft(buf + 4));
         printMsg("Set prv target to '%s'\n", setTarget);
         return 0;
     } else if (!strncasecmp(buf, "/who", 4)) {