# HG changeset patch # User Matti Hamalainen # Date 1288360775 -10800 # Node ID d03ebefb92a6d03394cacc1d5f91f207060c678d # Parent fe4d5f3b486cdbb59fdef47e04e70d4d07aefb95 Improve internal command handling, use fork() and execlp() instead of system() for opening profiles into external browser's tabs. diff -r fe4d5f3b486c -r d03ebefb92a6 nnchat.c --- 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)) {