From: Karsten Ballüder Date: Fri, 19 Mar 1999 10:57:18 +0000 (+0000) Subject: Fixed wxExecute() to handle filenames with spaces and quoted arguments. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/fad866f4b787486962e5237a90d6ead53b5c0f3b Fixed wxExecute() to handle filenames with spaces and quoted arguments. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1945 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index a0614ab927..340f3d547c 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -39,7 +39,7 @@ #include #include // for O_WRONLY and friends #include // nanosleep() and/or usleep() - +#include // isspace() #ifdef HAVE_UNAME #include // for uname() #endif // HAVE_UNAME @@ -132,24 +132,61 @@ int wxKill(long pid, int sig) return kill(pid, sig); } +#define WXEXECUTE_NARGS 127 + long wxExecute( const wxString& command, bool sync, wxProcess *process ) { - static const char *IFS = " \t\n"; - wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" ); int argc = 0; - char *argv[127]; - char *tmp = new char[command.Len() + 1]; - strcpy(tmp, command); - - argv[argc++] = strtok(tmp, IFS); - while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL) - /* loop */ ; + char *argv[WXEXECUTE_NARGS]; + wxString argument; + const char *cptr = command.c_str(); + char quotechar = '\0'; // is arg quoted? + bool escaped = FALSE; + do + { + argument=""; + quotechar = '\0'; + // eat leading whitespace: + while(*cptr && isspace(*cptr)) + cptr++; + if(*cptr == '\'' || *cptr == '"') + quotechar = *cptr++; + do + { + if(*cptr == '\\' && ! escaped) + { + escaped = TRUE; + cptr++; + continue; + } + // all other characters: + argument += *cptr ++; + escaped = FALSE; + // Have we reached the end of the argument? + if((*cptr == quotechar && ! escaped) + || (quotechar == '\0' && isspace(*cptr)) + || *cptr == '\0') + { + wxASSERT(argc < WXEXECUTE_NARGS); + argv[argc] = new char[argument.Len()+1]; + strcpy(argv[argc], argument.c_str()); + argc++; + // if not at end of buffer, swallow last character: + if(*cptr) cptr++; + break; // done with this one, start over + } + }while(*cptr); + }while(*cptr); + argv[argc] = NULL; + long lRc = wxExecute(argv, sync, process); - delete [] tmp; + argc = 0; + while(argv[argc]) + delete [] argv[argc++]; return lRc; }