diff options
author | TingPing <tingping@tingping.se> | 2014-04-25 13:41:38 -0400 |
---|---|---|
committer | TingPing <tingping@fedoraproject.org> | 2014-06-04 10:38:23 -0400 |
commit | 5849a0588e7b12003f8b9afcfcdc31f46c6b2329 (patch) | |
tree | 0eb7fa462318506be8b4303a9b32cb6a888c47cf /src/common | |
parent | b8c02f71d9bbaf805534d5327d8c3935a3b87636 (diff) |
Use glib to parse and launch commands for util_exec
Fixes #958
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/hexchat.c | 35 | ||||
-rw-r--r-- | src/common/hexchatc.h | 1 | ||||
-rw-r--r-- | src/common/outbound.c | 4 | ||||
-rw-r--r-- | src/common/util.c | 165 | ||||
-rw-r--r-- | src/common/util.h | 4 |
5 files changed, 6 insertions, 203 deletions
diff --git a/src/common/hexchat.c b/src/common/hexchat.c index 8729df33..3afe5f75 100644 --- a/src/common/hexchat.c +++ b/src/common/hexchat.c @@ -991,47 +991,12 @@ hexchat_exit (void) fe_exit (); } -#ifndef WIN32 - -static int -child_handler (gpointer userdata) -{ - int pid = GPOINTER_TO_INT (userdata); - - if (waitpid (pid, 0, WNOHANG) == pid) - return 0; /* remove timeout handler */ - return 1; /* keep the timeout handler */ -} - -#endif - void hexchat_exec (const char *cmd) { -#ifdef WIN32 util_exec (cmd); -#else - int pid = util_exec (cmd); - if (pid != -1) - /* zombie avoiding system. Don't ask! it has to be like this to work - with zvt (which overrides the default handler) */ - fe_timeout_add (1000, child_handler, GINT_TO_POINTER (pid)); -#endif } -void -hexchat_execv (char * const argv[]) -{ -#ifdef WIN32 - util_execv (argv); -#else - int pid = util_execv (argv); - if (pid != -1) - /* zombie avoiding system. Don't ask! it has to be like this to work - with zvt (which overrides the default handler) */ - fe_timeout_add (1000, child_handler, GINT_TO_POINTER (pid)); -#endif -} static void set_locale (void) diff --git a/src/common/hexchatc.h b/src/common/hexchatc.h index a999dcf2..9d603870 100644 --- a/src/common/hexchatc.h +++ b/src/common/hexchatc.h @@ -57,6 +57,5 @@ void session_free (session *killsess); void lag_check (void); void hexchat_exit (void); void hexchat_exec (const char *cmd); -void hexchat_execv (char * const argv[]); #endif diff --git a/src/common/outbound.c b/src/common/outbound.c index 5dd0d05f..53dd742d 100644 --- a/src/common/outbound.c +++ b/src/common/outbound.c @@ -1841,8 +1841,10 @@ cmd_exec (struct session *sess, char *tbuf, char *word[], char *word_eol[]) char **argv; int argc; - my_poptParseArgvString (cmd, &argc, &argv); + g_shell_parse_argv (cmd, &argc, &argv, NULL); execvp (argv[0], argv); + + g_strfreev (argv); } /* not reached unless error */ /*printf("exec error\n");*/ diff --git a/src/common/util.c b/src/common/util.c index 69ddaff4..2dc0034b 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1298,171 +1298,10 @@ country_search (char *pattern, void *ud, void (*print)(void *, char *, ...)) } } -/* I think gnome1.0.x isn't necessarily linked against popt, ah well! */ -/* !!! For now use this inlined function, or it would break fe-text building */ -/* .... will find a better solution later. */ -/*#ifndef USE_GNOME*/ - -/* this is taken from gnome-libs 1.2.4 */ -#define POPT_ARGV_ARRAY_GROW_DELTA 5 - -int my_poptParseArgvString(const char * s, int * argcPtr, char *** argvPtr) { - char * buf, * bufStart, * dst; - const char * src; - char quote = '\0'; - int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA; - char ** argv = malloc(sizeof(*argv) * argvAlloced); - const char ** argv2; - int argc = 0; - int i, buflen; - - buflen = strlen(s) + 1; -/* bufStart = buf = alloca(buflen);*/ - bufStart = buf = malloc (buflen); - memset(buf, '\0', buflen); - - src = s; - argv[argc] = buf; - - while (*src) { - if (quote == *src) { - quote = '\0'; - } else if (quote) { - if (*src == '\\') { - src++; - if (!*src) { - free(argv); - free(bufStart); - return 1; - } - if (*src != quote) *buf++ = '\\'; - } - *buf++ = *src; - /*} else if (isspace((unsigned char) *src)) {*/ - } else if (*src == ' ') { - if (*argv[argc]) { - buf++, argc++; - if (argc == argvAlloced) { - char **temp; - argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA; - temp = realloc(argv, sizeof(*argv) * argvAlloced); - if (temp) - argv = temp; - else - { - free(argv); - free(bufStart); - return 1; - } - } - argv[argc] = buf; - } - } else switch (*src) { - case '"': - case '\'': - quote = *src; - break; - case '\\': - src++; - if (!*src) { - free(argv); - free(bufStart); - return 1; - } - /* fallthrough */ - default: - *buf++ = *src; - } - - src++; - } - - if (strlen(argv[argc])) { - argc++, buf++; - } - - dst = malloc((argc + 1) * sizeof(*argv) + (buf - bufStart)); - argv2 = (void *) dst; - dst += (argc + 1) * sizeof(*argv); - memcpy((void *)argv2, argv, argc * sizeof(*argv)); - argv2[argc] = NULL; - memcpy(dst, bufStart, buf - bufStart); - - for (i = 0; i < argc; i++) { - argv2[i] = dst + (argv[i] - bufStart); - } - - free(argv); - - *argvPtr = (char **)argv2; /* XXX don't change the API */ - *argcPtr = argc; - - free (bufStart); - - return 0; -} - -int +void util_exec (const char *cmd) { - char **argv; - int argc; -#ifndef WIN32 - int pid; - int fd; -#endif - - if (my_poptParseArgvString (cmd, &argc, &argv) != 0) - return -1; - -#ifndef WIN32 - pid = fork (); - if (pid == -1) - { - free (argv); - return -1; - } - if (pid == 0) - { - /* Now close all open file descriptors except stdin, stdout and stderr */ - for (fd = 3; fd < 1024; fd++) close(fd); - execvp (argv[0], argv); - _exit (0); - } else - { - free (argv); - return pid; - } -#else - spawnvp (_P_DETACH, argv[0], argv); - free (argv); - return 0; -#endif -} - -int -util_execv (char * const argv[]) -{ -#ifndef WIN32 - int pid, fd; - - pid = fork (); - if (pid == -1) - return -1; - if (pid == 0) - { - /* Now close all open file descriptors except stdin, stdout and stderr */ - for (fd = 3; fd < 1024; fd++) close(fd); - execv (argv[0], argv); - _exit (0); - } else - { - return pid; - } -#else - spawnv (_P_DETACH, argv[0], argv); - return 0; -#endif + g_spawn_command_line_async (cmd, NULL); } unsigned long diff --git a/src/common/util.h b/src/common/util.h index f2fe2ecb..5231e56d 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -37,7 +37,6 @@ extern const unsigned char rfc_tolowertab[]; -int my_poptParseArgvString(const char * s, int * argcPtr, char *** argvPtr); char *expand_homedir (char *file); void path_part (char *file, char *path, int pathlen); int match (const char *mask, const char *string); @@ -50,8 +49,7 @@ char *nocasestrstr (const char *text, const char *tofind); char *country (char *); void country_search (char *pattern, void *ud, void (*print)(void *, char *, ...)); char *get_sys_str (int with_cpu); -int util_exec (const char *cmd); -int util_execv (char * const argv[]); +void util_exec (const char *cmd); #define STRIP_COLOR 1 #define STRIP_ATTRIB 2 #define STRIP_HIDDEN 4 |