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 size_t size = strlen(home_dir)+strlen(st)+2;
436 dir_buf = (
char *)malloc(size);
442 snprintf(dir_buf, size,
"%s/%s", home_dir, st);
447 home_dir = getenv(
"HOME");
449 size_t size = strlen(home_dir)+2;
450 dir_buf = (
char *)malloc(size);
455 snprintf(dir_buf, size,
"%s/", home_dir);
458 else if (*(st) ==
'~')
461 slash = strchr(st,
'/');
466 if (ppwd == NULL || ppwd->pw_dir == NULL)
471 size_t size = strlen(ppwd->pw_dir)+2;
472 dir_buf = (
char *)malloc(size);
478 snprintf(dir_buf, size,
"%s/", ppwd->pw_dir);
483 memcpy(username, st, slash - st);
484 username[slash - st] =
'\0';
486 ppwd = getpwnam(username);
487 if (ppwd == NULL || ppwd->pw_dir == NULL)
493 size_t size = strlen(ppwd->pw_dir)+strlen(slash)+2;
494 dir_buf = (
char *)malloc(size);
500 snprintf(dir_buf, size,
"%s/%s", ppwd->pw_dir, slash);
505 dir_buf = strdup(st);
508 char *unquoted =
unquote(dir_buf);
509 if (unquoted == NULL)
513 int errCode = chdir(unquoted);
520 context->RaiseCondition(
"ERROR", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
524 rc = context->False();
539 char *cmd = strdup(parm_cmd);
541 char *end = cmd + strlen(cmd);
551 for (
char *pos = cmd; pos < end; pos++)
553 while (*pos==
' ' || *pos==
'\t')
574 while (*pos!=
' ' && *pos!=
'\t' && *pos!=
'\0')
602 const char *cmd = context->StringData(command);
603 const char *envName = context->StringData(address);
609 bool noDirectInvoc =
false;
610 bool inQuotes =
false;
613 for (i = 0; i<strlen(cmd); i++)
619 else if (cmd[i] ==
'\\')
623 else if (cmd[i] ==
'"')
625 inQuotes = !inQuotes;
632 if (!inQuotes && (strchr(
"<>|&", cmd[i]) != NULL))
634 noDirectInvoc =
true;
643 size_t commandLen = strlen(cmd);
645 if (strcmp(cmd,
"cd") == 0)
652 else if (commandLen >= 3)
655 strncpy(tmp, cmd, 3);
657 if (strcmp(
"cd ",tmp) == 0)
664 strncpy(tmp, cmd, 4);
666 if (strcmp(
"set ",tmp) == 0)
673 strncpy(tmp, cmd, 6);
682 strncpy(tmp, cmd, 7);
700 if (strlen(envName) == 0)
713 errCode = system( cmd );
714 if ( errCode >= 256 )
716 errCode = errCode / 256;
727 waitpid ( pid, &status, 0);
728 if (WIFEXITED(status))
731 errCode = WEXITSTATUS(status);
735 errCode = -(WTERMSIG(status));
746 execl(
"/bin/sh",
"sh",
"-c", cmd, NULL);
750 execl(
"/bin/ksh",
"ksh",
"-c", cmd, NULL);
754 execl(
"/bin/bsh",
"bsh",
"-c", cmd, NULL);
758 execl(
"/bin/csh",
"csh",
"-c", cmd, NULL);
762 execl(
"/bin/bash",
"bash",
"-c", cmd, NULL);
771 execvp(args[0], args);
772 perror(
" *E* Address COMMAND");
777 execl(
"/bin/sh",
"sh",
"-c", cmd, NULL);
785 context->RaiseCondition(
"FAILURE", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
787 else if (errCode != 0)
790 context->RaiseCondition(
"ERROR", context->String(cmd), NULL, context->WholeNumberToObject(errCode));
792 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)