63 #define CMDBUFSIZE32S 260
64 #define CMDBUFSIZENT 8092
65 #define CMDDEFNAME32S "COMMAND.COM"
66 #define CMDDEFNAMENT "CMD.EXE"
67 #define UNKNOWN_COMMAND 1
71 #define COMSPEC "COMSPEC"
73 #define SHOWWINDOWFLAGS SW_HIDE
83 return OREF_INITIALADDRESS;
95 eqsign = strchr(cmd,
'=');
102 while ((*st) && (*st ==
' '))
110 strncpy(name, st, eqsign-st);
111 name[eqsign-st]=
'\0';
113 if (ExpandEnvironmentStrings(eqsign+1, value, 4095) && SetEnvironmentVariable(name,value))
115 rc = context->False();
119 context->RaiseCondition(
"ERROR", context->String(command),
NULLOBJECT, context->WholeNumberToObject(GetLastError()));
128 char *unquoted = (
char*)malloc(
sizeof(
char) * strlen(s) + 1);
130 if (unquoted != NULL)
133 while ( (*p = *s++) != 0 )
154 while ((*st) && (*st ==
' '))
163 if ((strlen(st) == 2) && (st[1] ==
':'))
165 rc = _chdrive(toupper( *st ) -
'A' + 1);
170 if (unquoted == NULL)
174 rc = _chdir(unquoted);
179 context->RaiseCondition(
"ERROR", context->String(command),
NULLOBJECT, context->WholeNumberToObject(GetLastError()));
183 res = context->False();
205 STARTUPINFO siStartInfo;
206 PROCESS_INFORMATION piProcInfo;
211 ZeroMemory(&siStartInfo,
sizeof(siStartInfo));
212 ZeroMemory(&piProcInfo,
sizeof(piProcInfo));
216 siStartInfo.cb =
sizeof(siStartInfo);
218 siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
219 siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
220 siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
221 titleChanged = GetConsoleTitle(ctitle, 255) != 0;
222 siStartInfo.lpTitle = (LPSTR)cmdstring_ptr;
223 creationFlags = GetPriorityClass(GetCurrentProcess()) | CREATE_NEW_PROCESS_GROUP;
224 if (!siStartInfo.hStdInput && !siStartInfo.hStdOutput && !titleChanged)
229 siStartInfo.dwFlags |= STARTF_USESHOWWINDOW;
233 creationFlags |= CREATE_NEW_CONSOLE;
240 siStartInfo.dwFlags = STARTF_USESTDHANDLES;
244 siStartInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
249 if (CreateProcess(NULL,
250 (LPSTR)cmdstring_ptr,
264 SetConsoleTitle(siStartInfo.lpTitle);
269 if (WAIT_FAILED != WaitForSingleObject ( piProcInfo.hProcess, INFINITE ) )
272 GetExitCodeProcess(piProcInfo.hProcess, &rc);
277 context->RaiseCondition(
"FAILURE", context->String(command),
NULLOBJECT, context->WholeNumberToObject(rc));
286 SetConsoleTitle(ctitle);
288 CloseHandle(piProcInfo.hThread);
289 CloseHandle(piProcInfo.hProcess);
303 context->RaiseCondition(
"ERROR", context->String(command),
NULLOBJECT, context->Int32ToObject(rc));
308 result = context->False();
333 const char *cmd = context->StringData(command);
334 const char *cl_opt =
" /c ";
335 const char *interncmd;
353 size_t quoteCount = 0;
354 bool noDirectInvoc =
false;
355 bool inQuotes =
false;
358 for (i = 0; i < strlen(interncmd); i++)
360 if (interncmd[i] ==
'"')
362 inQuotes = !inQuotes;
370 if (!inQuotes && (strchr(
"<>|&", interncmd[i]) != NULL))
372 noDirectInvoc =
true;
373 if ( quoteCount > 2 )
385 while (interncmd[j] ==
' ')
395 strncpy(tmp, &interncmd[j], 4);
398 if (!stricmp(
"set ",tmp))
407 strncpy(tmp, &interncmd[j], 3);
409 if (!stricmp(
"cd ",tmp))
418 if ((tmp[1] ==
':') && ((tmp[2] ==
' ') || (!tmp[2])))
420 int code = _chdrive(toupper( tmp[0] ) -
'A' + 1);
423 context->RaiseCondition(
"ERROR", command,
NULLOBJECT, context->WholeNumberToObject(code));
429 return context->False();
436 strncpy(tmp, &interncmd[j], 6);
438 noDirectInvoc = stricmp(
"start ",tmp) == 0;
444 const char *sys_cmd_handler;
451 if ( (sys_cmd_handler = getenv(
COMSPEC)) == NULL )
458 size_t maxBufferSize = strlen(sys_cmd_handler) + 1
460 + strlen(&interncmd[j])
465 char *cmdstring_ptr = cmdstring;
470 cmdstring_ptr = (
char *)LocalAlloc(LPTR, maxBufferSize);
471 if ( cmdstring_ptr == NULL )
474 context->String(
"Failed to allocate memory"));
489 _strupr(strcpy(cmdstring_ptr, &interncmd[j]));
490 bool searchFile = strstr(cmdstring_ptr,
"CMD") != NULL;
494 if (cmdstring_ptr[0] ==
'\"')
497 while (cmdstring_ptr[i] && (cmdstring_ptr[i] !=
'\"'))
501 cmdstring_ptr[i]=
'\0';
503 else if (cmdstring_ptr[0] ==
'\'')
506 while (cmdstring_ptr[i] && (cmdstring_ptr[i] !=
'\''))
510 cmdstring_ptr[i]=
'\0';
514 while (cmdstring_ptr[i] && (cmdstring_ptr[i] !=
' '))
518 cmdstring_ptr[i]=
'\0';
523 bool fileFound = count != 0 && count <= maxBufferSize;
526 cmdstring_ptr = cmdstring;
528 if (fileFound && !stricmp(sys_cmd_handler, cmdstring_ptr))
540 if (
sysCommandNT(context, cmd, &interncmd[j],
true, result))
542 if ( cmdstring_ptr != cmdstring )
545 LocalFree(cmdstring_ptr);
556 strcpy(cmdstring_ptr,sys_cmd_handler);
563 if (!( (strlen(interncmd) > j+1) && (interncmd[j] ==
'/')
564 && ((interncmd[j+1] ==
'k') || (interncmd[j+1] ==
'K'))
565 && ((interncmd[j+2] ==
' ') || (interncmd[j+2] ==
'\0')) ))
567 strcat(cmdstring_ptr,cl_opt);
572 strcat(cmdstring_ptr,
" ");
578 strcat(cmdstring_ptr,
"\"");
579 strcat(cmdstring_ptr,interncmd);
580 strcat(cmdstring_ptr,
"\"");
584 strcat(cmdstring_ptr,interncmd);
588 if (!
sysCommandNT(context, cmd, cmdstring_ptr,
false, result))
591 context->RaiseCondition(
"FAILURE", context->String(cmd),
NULLOBJECT, context->WholeNumberToObject(GetLastError()));
593 if ( cmdstring_ptr != cmdstring )
596 LocalFree(cmdstring_ptr);
601 if ( cmdstring_ptr != cmdstring )
604 LocalFree(cmdstring_ptr);
void addCommandHandler(const char *name, const char *registeredName)
InterpreterInstance * instance
void registerCommandHandlers(InterpreterInstance *i)
static bool exceptionConsole
static bool explicitConsole
static RexxString * getDefaultAddressName()
static HANDLE exceptionHostProcess
static ULONG exceptionHostProcessId
#define Rexx_Error_System_resources_user_defined
struct _RexxStringObject * RexxStringObject
struct _RexxObjectPtr * RexxObjectPtr
int SearchPath(int SearchFlag, const char *path, const char *filename, char *buf, size_t buf_size)
bool sys_process_set(RexxExitContext *context, const char *command, const char *cmd, RexxObjectPtr &rc)
bool sysCommandNT(RexxExitContext *context, const char *command, const char *cmdstring_ptr, bool direct, RexxObjectPtr &result)
char * unquote(const char *s)
bool sys_process_cd(RexxExitContext *context, const char *command, const char *cmd, RexxObjectPtr &res)
RexxObjectPtr RexxEntry systemCommandHandler(RexxExitContext *context, RexxStringObject address, RexxStringObject command)