1999-06-06 15:24:04 +00:00
|
|
|
/*
|
|
|
|
* WCMD - Wine-compatible command line interface.
|
|
|
|
*
|
2001-06-04 02:55:38 +00:00
|
|
|
* (C) 1999 - 2001 D A Pickles
|
1999-06-06 15:24:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* - Cannot handle parameters in quotes
|
|
|
|
* - Lots of functionality missing from builtins
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wcmd.h"
|
|
|
|
|
|
|
|
char *inbuilt[] = {"ATTRIB", "CALL", "CD", "CHDIR", "CLS", "COPY", "CTTY",
|
|
|
|
"DATE", "DEL", "DIR", "ECHO", "ERASE", "FOR", "GOTO",
|
|
|
|
"HELP", "IF", "LABEL", "MD", "MKDIR", "MOVE", "PATH", "PAUSE",
|
|
|
|
"PROMPT", "REM", "REN", "RENAME", "RD", "RMDIR", "SET", "SHIFT",
|
|
|
|
"TIME", "TYPE", "VERIFY", "VER", "VOL", "EXIT"};
|
|
|
|
|
2000-08-01 22:02:18 +00:00
|
|
|
HINSTANCE hinst;
|
|
|
|
DWORD errorlevel;
|
1999-06-26 10:24:08 +00:00
|
|
|
int echo_mode = 1, verify_mode = 0;
|
1999-06-06 15:24:04 +00:00
|
|
|
char nyi[] = "Not Yet Implemented\n\n";
|
|
|
|
char newline[] = "\n";
|
2001-06-04 02:55:38 +00:00
|
|
|
char version_string[] = "WCMD Version 0.17\n\n";
|
1999-06-06 15:24:04 +00:00
|
|
|
char anykey[] = "Press any key to continue: ";
|
|
|
|
char quals[MAX_PATH], param1[MAX_PATH], param2[MAX_PATH];
|
1999-06-26 10:24:08 +00:00
|
|
|
BATCH_CONTEXT *context = NULL;
|
1999-06-06 15:24:04 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Main entry point. This is a console application so we have a main() not a
|
|
|
|
* winmain().
|
|
|
|
*/
|
|
|
|
|
2002-01-29 02:59:22 +00:00
|
|
|
int main (int argc, char *argv[]) {
|
1999-06-06 15:24:04 +00:00
|
|
|
|
|
|
|
char string[1024], args[MAX_PATH], param[MAX_PATH];
|
|
|
|
int status, i;
|
|
|
|
DWORD count;
|
|
|
|
HANDLE h;
|
|
|
|
|
|
|
|
args[0] = param[0] = '\0';
|
|
|
|
if (argc > 1) {
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (argv[i][0] == '/') {
|
|
|
|
strcat (args, argv[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcat (param, argv[i]);
|
|
|
|
strcat (param, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a console and set it up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status = FreeConsole ();
|
|
|
|
if (!status) WCMD_print_error();
|
|
|
|
status = AllocConsole();
|
|
|
|
if (!status) WCMD_print_error();
|
1999-07-10 11:36:45 +00:00
|
|
|
SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT |
|
1999-06-06 15:24:04 +00:00
|
|
|
ENABLE_PROCESSED_INPUT);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute any command-line options.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strstr(args, "/q") != NULL) {
|
|
|
|
WCMD_echo ("OFF");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(args, "/c") != NULL) {
|
|
|
|
WCMD_process_command (param);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strstr(args, "/k") != NULL) {
|
|
|
|
WCMD_process_command (param);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is an AUTOEXEC.BAT file, try to execute it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
GetFullPathName ("\\autoexec.bat", sizeof(string), string, NULL);
|
2001-01-02 20:40:58 +00:00
|
|
|
h = CreateFile (string, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
1999-06-06 15:24:04 +00:00
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle (h);
|
2000-03-24 20:40:41 +00:00
|
|
|
#if 0
|
|
|
|
WCMD_batch (string, " ");
|
|
|
|
#endif
|
1999-06-06 15:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop forever getting commands and executing them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
WCMD_version ();
|
|
|
|
while (TRUE) {
|
|
|
|
WCMD_show_prompt ();
|
1999-07-10 11:36:45 +00:00
|
|
|
ReadFile (GetStdHandle(STD_INPUT_HANDLE), string, sizeof(string), &count, NULL);
|
1999-06-06 15:24:04 +00:00
|
|
|
if (count > 1) {
|
|
|
|
string[count-1] = '\0'; /* ReadFile output is not null-terminated! */
|
|
|
|
if (string[count-2] == '\r') string[count-2] = '\0'; /* Under Windoze we get CRLF! */
|
|
|
|
if (lstrlen (string) != 0) {
|
2001-06-04 02:55:38 +00:00
|
|
|
if (strchr(string,'|') != NULL) {
|
|
|
|
WCMD_pipe (string);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WCMD_process_command (string);
|
|
|
|
}
|
1999-06-06 15:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Process one command. If the command is EXIT this routine does not return.
|
|
|
|
* We will recurse through here executing batch files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void WCMD_process_command (char *command) {
|
|
|
|
|
|
|
|
char cmd[1024];
|
|
|
|
char *p;
|
|
|
|
int status, i;
|
|
|
|
DWORD count;
|
1999-07-10 11:36:45 +00:00
|
|
|
HANDLE old_stdin = 0, old_stdout = 0, h;
|
2000-08-01 02:14:33 +00:00
|
|
|
char *whichcmd;
|
1999-06-06 15:24:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand up environment variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status = ExpandEnvironmentStrings (command, cmd, sizeof(cmd));
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Changing default drive has to be handled as a special case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((cmd[1] == ':') && IsCharAlpha (cmd[0]) && (strlen(cmd) == 2)) {
|
|
|
|
status = SetCurrentDirectory (cmd);
|
|
|
|
if (!status) WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
2000-08-01 02:14:33 +00:00
|
|
|
|
|
|
|
/* Dont issue newline WCMD_output (newline); @JED*/
|
1999-06-06 15:24:04 +00:00
|
|
|
|
1999-07-10 11:36:45 +00:00
|
|
|
/*
|
|
|
|
* Redirect stdin and/or stdout if required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((p = strchr(cmd,'<')) != NULL) {
|
|
|
|
h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_READ, 0, NULL, OPEN_EXISTING,
|
2001-01-02 20:40:58 +00:00
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
1999-07-10 11:36:45 +00:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
old_stdin = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, h);
|
|
|
|
}
|
|
|
|
if ((p = strchr(cmd,'>')) != NULL) {
|
|
|
|
h = CreateFile (WCMD_parameter (++p, 0, NULL), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
|
2001-01-02 20:40:58 +00:00
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
1999-07-10 11:36:45 +00:00
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
old_stdout = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
SetStdHandle (STD_OUTPUT_HANDLE, h);
|
|
|
|
*--p = '\0';
|
|
|
|
}
|
|
|
|
if ((p = strchr(cmd,'<')) != NULL) *p = '\0';
|
|
|
|
|
2000-08-01 02:14:33 +00:00
|
|
|
/*
|
|
|
|
* Strip leading whitespaces, and a '@' if supplied
|
|
|
|
*/
|
|
|
|
whichcmd = WCMD_strtrim_leading_spaces(cmd);
|
|
|
|
if (whichcmd[0] == '@') whichcmd++;
|
|
|
|
|
1999-06-06 15:24:04 +00:00
|
|
|
/*
|
|
|
|
* Check if the command entered is internal. If it is, pass the rest of the
|
|
|
|
* line down to the command. If not try to run a program.
|
|
|
|
*/
|
|
|
|
|
|
|
|
count = 0;
|
2000-08-01 02:14:33 +00:00
|
|
|
while (IsCharAlphaNumeric(whichcmd[count])) {
|
1999-06-06 15:24:04 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
for (i=0; i<=WCMD_EXIT; i++) {
|
|
|
|
if (CompareString (LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT,
|
2000-08-01 02:14:33 +00:00
|
|
|
whichcmd, count, inbuilt[i], -1) == 2) break;
|
1999-06-06 15:24:04 +00:00
|
|
|
}
|
2000-08-01 02:14:33 +00:00
|
|
|
p = WCMD_strtrim_leading_spaces (&whichcmd[count]);
|
1999-06-06 15:24:04 +00:00
|
|
|
WCMD_parse (p, quals, param1, param2);
|
|
|
|
switch (i) {
|
|
|
|
|
|
|
|
case WCMD_ATTRIB:
|
|
|
|
WCMD_setshow_attrib ();
|
|
|
|
break;
|
|
|
|
case WCMD_CALL:
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_batch (param1, p, 1);
|
1999-06-06 15:24:04 +00:00
|
|
|
break;
|
|
|
|
case WCMD_CD:
|
|
|
|
case WCMD_CHDIR:
|
|
|
|
WCMD_setshow_default ();
|
|
|
|
break;
|
|
|
|
case WCMD_CLS:
|
|
|
|
WCMD_clear_screen ();
|
|
|
|
break;
|
|
|
|
case WCMD_COPY:
|
|
|
|
WCMD_copy ();
|
|
|
|
break;
|
|
|
|
case WCMD_CTTY:
|
|
|
|
WCMD_change_tty ();
|
|
|
|
break;
|
|
|
|
case WCMD_DATE:
|
|
|
|
WCMD_setshow_date ();
|
|
|
|
break;
|
|
|
|
case WCMD_DEL:
|
|
|
|
case WCMD_ERASE:
|
|
|
|
WCMD_delete (0);
|
|
|
|
break;
|
|
|
|
case WCMD_DIR:
|
|
|
|
WCMD_directory ();
|
|
|
|
break;
|
|
|
|
case WCMD_ECHO:
|
2000-08-01 02:14:33 +00:00
|
|
|
/* Use the unstripped version of the following data - step over the space */
|
|
|
|
/* but only if a parameter follows */
|
|
|
|
if (strlen(&whichcmd[count]) > 0)
|
|
|
|
WCMD_echo(&whichcmd[count+1]);
|
|
|
|
else
|
|
|
|
WCMD_echo(&whichcmd[count]);
|
|
|
|
break;
|
1999-06-06 15:24:04 +00:00
|
|
|
case WCMD_FOR:
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_for (p);
|
1999-06-06 15:24:04 +00:00
|
|
|
break;
|
|
|
|
case WCMD_GOTO:
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_goto ();
|
1999-06-06 15:24:04 +00:00
|
|
|
break;
|
|
|
|
case WCMD_HELP:
|
|
|
|
WCMD_give_help (p);
|
|
|
|
break;
|
|
|
|
case WCMD_IF:
|
1999-07-10 11:36:45 +00:00
|
|
|
WCMD_if (p);
|
1999-06-06 15:24:04 +00:00
|
|
|
break;
|
|
|
|
case WCMD_LABEL:
|
|
|
|
WCMD_volume (1, p);
|
|
|
|
break;
|
|
|
|
case WCMD_MD:
|
|
|
|
case WCMD_MKDIR:
|
|
|
|
WCMD_create_dir ();
|
|
|
|
break;
|
|
|
|
case WCMD_MOVE:
|
|
|
|
WCMD_move ();
|
|
|
|
break;
|
|
|
|
case WCMD_PATH:
|
|
|
|
WCMD_setshow_path ();
|
|
|
|
break;
|
|
|
|
case WCMD_PAUSE:
|
|
|
|
WCMD_pause ();
|
|
|
|
break;
|
|
|
|
case WCMD_PROMPT:
|
|
|
|
WCMD_setshow_prompt ();
|
|
|
|
break;
|
|
|
|
case WCMD_REM:
|
|
|
|
break;
|
|
|
|
case WCMD_REN:
|
|
|
|
case WCMD_RENAME:
|
|
|
|
WCMD_rename ();
|
|
|
|
break;
|
|
|
|
case WCMD_RD:
|
|
|
|
case WCMD_RMDIR:
|
|
|
|
WCMD_remove_dir ();
|
|
|
|
break;
|
|
|
|
case WCMD_SET:
|
|
|
|
WCMD_setshow_env (p);
|
|
|
|
break;
|
|
|
|
case WCMD_SHIFT:
|
|
|
|
WCMD_shift ();
|
|
|
|
break;
|
|
|
|
case WCMD_TIME:
|
|
|
|
WCMD_setshow_time ();
|
|
|
|
break;
|
|
|
|
case WCMD_TYPE:
|
|
|
|
WCMD_type ();
|
|
|
|
break;
|
|
|
|
case WCMD_VER:
|
|
|
|
WCMD_version ();
|
|
|
|
break;
|
|
|
|
case WCMD_VERIFY:
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_verify (p);
|
1999-06-06 15:24:04 +00:00
|
|
|
break;
|
|
|
|
case WCMD_VOL:
|
|
|
|
WCMD_volume (0, p);
|
|
|
|
break;
|
|
|
|
case WCMD_EXIT:
|
|
|
|
ExitProcess (0);
|
|
|
|
default:
|
2000-08-01 02:14:33 +00:00
|
|
|
WCMD_run_program (whichcmd);
|
1999-06-06 15:24:04 +00:00
|
|
|
};
|
1999-07-10 11:36:45 +00:00
|
|
|
if (old_stdin) {
|
|
|
|
CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, old_stdin);
|
|
|
|
}
|
|
|
|
if (old_stdout) {
|
|
|
|
CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
|
|
|
|
SetStdHandle (STD_OUTPUT_HANDLE, old_stdout);
|
|
|
|
}
|
1999-06-06 15:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* WCMD_run_program
|
|
|
|
*
|
|
|
|
* Execute a command line as an external program. If no extension given then
|
|
|
|
* precedence is given to .BAT files. Must allow recursion.
|
|
|
|
*
|
|
|
|
* FIXME: Case sensitivity in suffixes!
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_run_program (char *command) {
|
|
|
|
|
|
|
|
STARTUPINFO st;
|
|
|
|
PROCESS_INFORMATION pe;
|
2000-11-02 20:07:07 +00:00
|
|
|
SHFILEINFO psfi;
|
|
|
|
DWORD console;
|
1999-06-06 15:24:04 +00:00
|
|
|
BOOL status;
|
|
|
|
HANDLE h;
|
2000-11-02 20:07:07 +00:00
|
|
|
HINSTANCE hinst;
|
1999-06-06 15:24:04 +00:00
|
|
|
char filetorun[MAX_PATH];
|
|
|
|
|
|
|
|
WCMD_parse (command, quals, param1, param2); /* Quick way to get the filename */
|
|
|
|
if (strpbrk (param1, "\\:") == NULL) { /* No explicit path given */
|
|
|
|
if ((strchr (param1, '.') == NULL) || (strstr (param1, ".bat") != NULL)) {
|
|
|
|
if (SearchPath (NULL, param1, ".bat", sizeof(filetorun), filetorun, NULL)) {
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_batch (filetorun, command, 0);
|
1999-06-06 15:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* Explicit path given */
|
|
|
|
if (strstr (param1, ".bat") != NULL) {
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_batch (param1, command, 0);
|
1999-06-06 15:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (strchr (param1, '.') == NULL) {
|
|
|
|
strcpy (filetorun, param1);
|
|
|
|
strcat (filetorun, ".bat");
|
2001-01-02 20:40:58 +00:00
|
|
|
h = CreateFile (filetorun, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
1999-06-06 15:24:04 +00:00
|
|
|
if (h != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle (h);
|
1999-06-26 10:24:08 +00:00
|
|
|
WCMD_batch (param1, command, 0);
|
1999-06-06 15:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No batch file found, assume executable */
|
|
|
|
|
2000-11-02 20:07:07 +00:00
|
|
|
hinst = FindExecutable (param1, NULL, filetorun);
|
|
|
|
if ((int)hinst < 32) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console = SHGetFileInfo (filetorun, 0, &psfi, sizeof(psfi), SHGFI_EXETYPE);
|
|
|
|
if (!console) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
return;
|
|
|
|
}
|
1999-06-06 15:24:04 +00:00
|
|
|
ZeroMemory (&st, sizeof(STARTUPINFO));
|
|
|
|
st.cb = sizeof(STARTUPINFO);
|
|
|
|
status = CreateProcess (NULL, command, NULL, NULL, FALSE,
|
|
|
|
0, NULL, NULL, &st, &pe);
|
|
|
|
if (!status) {
|
|
|
|
WCMD_print_error ();
|
|
|
|
}
|
2000-11-02 20:07:07 +00:00
|
|
|
if (!HIWORD(console)) WaitForSingleObject (pe.hProcess, INFINITE);
|
2000-08-01 22:02:18 +00:00
|
|
|
GetExitCodeProcess (pe.hProcess, &errorlevel);
|
|
|
|
if (errorlevel == STILL_ACTIVE) errorlevel = 0;
|
1999-06-06 15:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* WCMD_show_prompt
|
|
|
|
*
|
|
|
|
* Display the prompt on STDout
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_show_prompt () {
|
|
|
|
|
|
|
|
int status;
|
|
|
|
char out_string[MAX_PATH], curdir[MAX_PATH], prompt_string[MAX_PATH];
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
status = GetEnvironmentVariable ("PROMPT", prompt_string, sizeof(prompt_string));
|
|
|
|
if ((status == 0) || (status > sizeof(prompt_string))) {
|
|
|
|
lstrcpy (prompt_string, "$N$G");
|
|
|
|
}
|
|
|
|
p = prompt_string;
|
|
|
|
q = out_string;
|
|
|
|
*q = '\0';
|
|
|
|
while (*p != '\0') {
|
|
|
|
if (*p != '$') {
|
|
|
|
*q++ = *p++;
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p++;
|
|
|
|
switch (toupper(*p)) {
|
|
|
|
case '$':
|
|
|
|
*q++ = '$';
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
*q++ = '|';
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, q, MAX_PATH);
|
|
|
|
while (*q) q++;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
*q++ = '\E';
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
*q++ = '>';
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
*q++ = '<';
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
status = GetCurrentDirectory (sizeof(curdir), curdir);
|
|
|
|
if (status) {
|
|
|
|
*q++ = curdir[0];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
status = GetCurrentDirectory (sizeof(curdir), curdir);
|
|
|
|
if (status) {
|
|
|
|
lstrcat (q, curdir);
|
|
|
|
while (*q) q++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
*q++ = '=';
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL, q, MAX_PATH);
|
|
|
|
while (*q) q++;
|
|
|
|
break;
|
|
|
|
case '_':
|
|
|
|
*q++ = '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WCMD_output (out_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* WCMD_print_error
|
|
|
|
*
|
2000-08-01 22:02:18 +00:00
|
|
|
* Print the message for GetLastError
|
1999-06-06 15:24:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_print_error () {
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
DWORD error_code;
|
2000-08-01 22:02:18 +00:00
|
|
|
int status;
|
1999-06-06 15:24:04 +00:00
|
|
|
|
|
|
|
error_code = GetLastError ();
|
2000-08-01 22:02:18 +00:00
|
|
|
status = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, error_code, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
|
|
|
|
if (!status) {
|
|
|
|
WCMD_output ("FIXME: Cannot display message for error %d, status %d\n",
|
|
|
|
error_code, GetLastError());
|
|
|
|
return;
|
1999-06-06 15:24:04 +00:00
|
|
|
}
|
2000-08-01 22:02:18 +00:00
|
|
|
WCMD_output (lpMsgBuf);
|
|
|
|
LocalFree ((HLOCAL)lpMsgBuf);
|
|
|
|
WCMD_output (newline);
|
1999-06-06 15:24:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_parse - parse a command into parameters and qualifiers.
|
|
|
|
*
|
|
|
|
* On exit, all qualifiers are concatenated into q, the first string
|
|
|
|
* not beginning with "/" is in p1 and the
|
|
|
|
* second in p2. Any subsequent non-qualifier strings are lost.
|
|
|
|
* Parameters in quotes are handled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_parse (char *s, char *q, char *p1, char *p2) {
|
|
|
|
|
|
|
|
int p = 0;
|
|
|
|
|
|
|
|
*q = *p1 = *p2 = '\0';
|
|
|
|
while (TRUE) {
|
|
|
|
switch (*s) {
|
|
|
|
case '/':
|
|
|
|
*q++ = *s++;
|
|
|
|
while ((*s != '\0') && (*s != ' ') && *s != '/') {
|
|
|
|
*q++ = toupper (*s++);
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
s++;
|
|
|
|
while ((*s != '\0') && (*s != '"')) {
|
|
|
|
if (p == 0) *p1++ = *s++;
|
|
|
|
else if (p == 1) *p2++ = *s++;
|
|
|
|
else s++;
|
|
|
|
}
|
|
|
|
if (p == 0) *p1 = '\0';
|
|
|
|
if (p == 1) *p2 = '\0';
|
|
|
|
p++;
|
|
|
|
if (*s == '"') s++;
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
while ((*s != '\0') && (*s != ' ') && (*s != '/')) {
|
|
|
|
if (p == 0) *p1++ = *s++;
|
|
|
|
else if (p == 1) *p2++ = *s++;
|
|
|
|
else s++;
|
|
|
|
}
|
|
|
|
if (p == 0) *p1 = '\0';
|
|
|
|
if (p == 1) *p2 = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output - send output to current standard output device.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_output (char *format, ...) {
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
char string[1024];
|
|
|
|
DWORD count;
|
|
|
|
|
|
|
|
va_start(ap,format);
|
|
|
|
vsprintf (string, format, ap);
|
1999-07-10 11:36:45 +00:00
|
|
|
WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), string, lstrlen(string), &count, NULL);
|
1999-06-06 15:24:04 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2000-08-01 02:14:33 +00:00
|
|
|
/*******************************************************************
|
|
|
|
* WCMD_output_asis - send output to current standard output device.
|
|
|
|
* without formatting eg. when message contains '%'
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_output_asis (char *message) {
|
|
|
|
DWORD count;
|
|
|
|
WriteFile (GetStdHandle(STD_OUTPUT_HANDLE), message, lstrlen(message), &count, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-06 15:24:04 +00:00
|
|
|
|
2000-08-01 22:02:18 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* WCMD_strtrim_leading_spaces
|
|
|
|
*
|
|
|
|
* Remove leading spaces from a string. Return a pointer to the first
|
1999-06-06 15:24:04 +00:00
|
|
|
* non-space character. Does not modify the input string
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *WCMD_strtrim_leading_spaces (char *string) {
|
|
|
|
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = string;
|
|
|
|
while (*ptr == ' ') ptr++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2000-08-01 22:02:18 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* WCMD_strtrim_trailing_spaces
|
|
|
|
*
|
|
|
|
* Remove trailing spaces from a string. This routine modifies the input
|
1999-06-06 15:24:04 +00:00
|
|
|
* string by placing a null after the last non-space character
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_strtrim_trailing_spaces (char *string) {
|
|
|
|
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ptr = string + lstrlen (string) - 1;
|
|
|
|
while ((*ptr == ' ') && (ptr >= string)) {
|
|
|
|
*ptr = '\0';
|
|
|
|
ptr--;
|
|
|
|
}
|
|
|
|
}
|
2001-06-04 02:55:38 +00:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* WCMD_pipe
|
|
|
|
*
|
|
|
|
* Handle pipes within a command - the DOS way using temporary files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WCMD_pipe (char *command) {
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
char temp_path[MAX_PATH], temp_file[MAX_PATH], temp_file2[MAX_PATH], temp_cmd[1024];
|
|
|
|
|
|
|
|
GetTempPath (sizeof(temp_path), temp_path);
|
|
|
|
GetTempFileName (temp_path, "WCMD", 0, temp_file);
|
|
|
|
p = strchr(command, '|');
|
|
|
|
*p++ = '\0';
|
|
|
|
wsprintf (temp_cmd, "%s > %s", command, temp_file);
|
|
|
|
WCMD_process_command (temp_cmd);
|
|
|
|
command = p;
|
|
|
|
while ((p = strchr(command, '|'))) {
|
|
|
|
*p++ = '\0';
|
|
|
|
GetTempFileName (temp_path, "WCMD", 0, temp_file2);
|
|
|
|
wsprintf (temp_cmd, "%s < %s > %s", command, temp_file, temp_file2);
|
|
|
|
WCMD_process_command (temp_cmd);
|
|
|
|
DeleteFile (temp_file);
|
|
|
|
lstrcpy (temp_file, temp_file2);
|
|
|
|
command = p;
|
|
|
|
}
|
|
|
|
wsprintf (temp_cmd, "%s < %s", command, temp_file);
|
|
|
|
WCMD_process_command (temp_cmd);
|
|
|
|
DeleteFile (temp_file);
|
|
|
|
}
|
|
|
|
|