69 #include <sys/types.h>
73 #define CMDBUFSIZE 1024
74 #define MAX_COMMAND_ARGS 400
77 #define CMDDEFNAME "/bin/ksh"
78 #elif defined(OPSYS_SUN)
79 #define CMDDEFNAME "/bin/sh"
81 #define CMDDEFNAME "/bin/bash"
84 #define UNKNOWN_COMMAND 127
86 #define SYSENV "command"
91 #define MAX_VALUE 1280
103 return OREF_INITIALADDRESS;
109 char *Env_Var_String = NULL;
110 size_t size, allocsize;
113 size_t i,j,k,l,iLength, copyval;
114 char namebufcurr[1281];
116 char *array, *runarray, *runptr, *endptr, *maxptr;
117 char temparray[1281];
124 bool HitFlag =
false;
127 allocsize = 1281 * 2;
129 memset(temparray,
'\0',
sizeof(temparray));
144 while ((*st) && (*st ==
' '))
149 iLength = strlen(st) + 1;
161 for (;*Environment != NULL; Environment++)
163 size = strlen(*Environment)+1;
164 Env_Var_String = (
char *)malloc(size);
165 memcpy(Env_Var_String,*Environment,size);
166 putenv(Env_Var_String);
175 if ( !(strchr(name,
'=')) && (flag !=
UNSET_FLAG) )
179 if ( (strchr(name,
'|')) || (strchr(name,
'>')) || (strstr(name,
">>")) )
186 rc = context->False();
193 if ( (strchr(name,
'=')) && (flag ==
UNSET_FLAG) )
198 for (i=0;(name[i]!=
'=')&&(i<iLength);i++)
200 cmd_name[i] = name[i];
211 strcpy(value, &(name[i]));
212 array = (
char *) malloc(1281);
213 strcpy(array, cmd_name);
214 array[strlen(cmd_name)] =
'=';
216 runarray = array + strlen(array);
218 endptr = runptr + strlen(value);
221 while ((tmpptr = (strchr(runptr,
'$'))) != 0)
225 copyval = tmpptr - runptr;
228 while ((runarray + copyval) > maxptr)
230 array = (
char *) realloc(array, allocsize);
231 runarray = array + strlen(array);
232 maxptr = array + allocsize - 1;
233 allocsize = allocsize * 2;
235 memcpy(runarray,runptr, copyval);
236 runarray= runarray + copyval;
241 for (j = 0;(*runptr !=
'/') && (*runptr !=
':') && (*runptr !=
'$') &&
244 memcpy(&(temparray[j]), runptr,1);
251 for (;(*Environment != NULL) && (hit == NULL) ;Environment++)
255 for (k=0;(*np!=
'=')&&(k<255);np++,k++)
257 memcpy(&(namebufcurr[k]),np,1);
260 namebufcurr[k] =
'\0';
262 if (!strcmp(temparray,namebufcurr))
271 while ((runarray + strlen(np)) > maxptr)
273 array = (
char *) realloc(array, allocsize);
274 runarray = array + strlen(array);
275 maxptr = array + allocsize - 1;
276 allocsize = allocsize * 2;
278 strcpy(runarray, np);
279 runarray = runarray + strlen(np);
289 while ((runarray + strlen(runptr)) > maxptr)
291 array = (
char *) realloc(array, allocsize);
292 runarray = array + strlen(array);
293 maxptr = array + allocsize - 1;
294 allocsize = allocsize * 2;
296 strcpy(runarray, runptr);
297 runarray = runarray + strlen(runptr);
303 while ((runarray + strlen(value)) > maxptr)
305 array = (
char *) realloc(array, allocsize);
306 runarray = array + strlen(array);
307 maxptr = array + allocsize - 1;
308 allocsize = allocsize * 2;
310 strcpy(runarray,value);
311 runarray = runarray + strlen(runptr);
317 for (;*Environment != NULL;Environment++)
321 for (i=0;(*np!=
'=')&&(i<255);np++,i++)
323 memcpy(&(namebufcurr[i]),np,1);
326 namebufcurr[i] =
'\0';
328 if (!strcmp(cmd_name,namebufcurr))
336 size = strlen(array)+1;
337 Env_Var_String = (
char *)malloc(size);
338 memcpy(Env_Var_String, array, size);
339 int errCode = putenv(Env_Var_String);
343 context->RaiseCondition(
"ERROR", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
347 rc = context->False();
355 rc = context->False();
367 size_t size = strlen(s) + 1;
368 char *unquoted = (
char*)malloc(
sizeof(
char)*size);
369 if ( unquoted == NULL )
384 else if ( c ==
'\\' )
403 const char *home_dir = NULL;
404 char *dir_buf = NULL;
409 while ((*st) && (*st ==
' '))
413 if ((!*st) || (strlen(cmd) == 2))
415 home_dir = getenv(
"HOME");
420 dir_buf = (
char *)malloc(strlen(home_dir)+1);
421 strcpy(dir_buf, home_dir);
423 else if (*(st) ==
'~' && (*(st+1) ==
'\0' || *(st+1) ==
'/'|| *(st+1) ==
' ' ))
429 home_dir = getenv(
"HOME");
435 dir_buf = (
char *)malloc(strlen(home_dir)+strlen(st)+2);
441 sprintf(dir_buf,
"%s/%s", home_dir, st);
446 home_dir = getenv(
"HOME");
448 dir_buf = (
char *)malloc(strlen(home_dir)+2);
453 sprintf(dir_buf,
"%s/", home_dir);
456 else if (*(st) ==
'~')
459 slash = strchr(st,
'/');
464 if (ppwd == NULL || ppwd->pw_dir == NULL)
469 dir_buf = (
char *)malloc(strlen(ppwd->pw_dir)+2);
475 sprintf(dir_buf,
"%s/", ppwd->pw_dir);
480 memcpy(username, st, slash - st);
481 username[slash - st] =
'\0';
483 ppwd = getpwnam(username);
484 if (ppwd == NULL || ppwd->pw_dir == NULL)
490 dir_buf = (
char *)malloc(strlen(ppwd->pw_dir)+strlen(slash)+2);
496 sprintf(dir_buf,
"%s/%s", ppwd->pw_dir, slash);
501 dir_buf = strdup(st);
504 char *unquoted =
unquote(dir_buf);
505 if (unquoted == NULL)
509 int errCode = chdir(unquoted);
516 context->RaiseCondition(
"ERROR", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
520 rc = context->False();
535 char *cmd = strdup(parm_cmd);
537 char *end = cmd + strlen(cmd);
547 for (
char *pos = cmd; pos < end; pos++)
549 while (*pos==
' ' || *pos==
'\t')
570 while (*pos!=
' ' && *pos!=
'\t' && *pos!=
'\0')
598 const char *cmd = context->StringData(command);
599 const char *envName = context->StringData(address);
605 bool noDirectInvoc =
false;
606 bool inQuotes =
false;
609 for (i = 0; i<strlen(cmd); i++)
615 else if (cmd[i] ==
'\\')
619 else if (cmd[i] ==
'"')
621 inQuotes = !inQuotes;
628 if (!inQuotes && (strchr(
"<>|&", cmd[i]) != NULL))
630 noDirectInvoc =
true;
639 size_t commandLen = strlen(cmd);
641 if (strcmp(cmd,
"cd") == 0)
648 else if (commandLen >= 3)
651 strncpy(tmp, cmd, 3);
653 if (strcmp(
"cd ",tmp) == 0)
660 strncpy(tmp, cmd, 4);
662 if (strcmp(
"set ",tmp) == 0)
669 strncpy(tmp, cmd, 6);
678 strncpy(tmp, cmd, 7);
696 if (strlen(envName) == 0)
709 errCode = system( cmd );
710 if ( errCode >= 256 )
712 errCode = errCode / 256;
723 waitpid ( pid, &status, 0);
724 if (WIFEXITED(status))
727 errCode = WEXITSTATUS(status);
731 errCode = -(WTERMSIG(status));
742 execl(
"/bin/sh",
"sh",
"-c", cmd, NULL);
746 execl(
"/bin/ksh",
"ksh",
"-c", cmd, NULL);
750 execl(
"/bin/bsh",
"bsh",
"-c", cmd, NULL);
754 execl(
"/bin/csh",
"csh",
"-c", cmd, NULL);
758 execl(
"/bin/bash",
"bash",
"-c", cmd, NULL);
767 execvp(args[0], args);
768 perror(
" *E* Address COMMAND");
773 execl(
"/bin/sh",
"sh",
"-c", cmd, NULL);
781 context->RaiseCondition(
"FAILURE", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
783 else if (errCode != 0)
786 context->RaiseCondition(
"ERROR", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
788 return context->False();
void addCommandHandler(const char *name, const char *registeredName)
void registerCommandHandlers(InterpreterInstance *i)
static RexxString * getDefaultAddressName()
static int strCaselessCompare(const char *opt1, const char *opt2)
struct _RexxStringObject * RexxStringObject
struct _RexxObjectPtr * RexxObjectPtr
bool scan_cmd(const char *parm_cmd, char **argPtr)
char * unquote(const char *s)
bool sys_process_cd(RexxExitContext *context, const char *cmd, RexxObjectPtr rc)
RexxObjectPtr RexxEntry systemCommandHandler(RexxExitContext *context, RexxStringObject address, RexxStringObject command)
bool sys_process_export(RexxExitContext *context, const char *cmd, RexxObjectPtr &rc, int flag)