2 * START.C - start internal command.
7 * 24-Jul-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
10 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
11 * Remove all hardcode string to En.rc
17 #ifdef INCLUDE_CMD_START
20 INT
cmd_start (LPTSTR First
, LPTSTR Rest
)
22 TCHAR szFullName
[CMDLINE_LENGTH
];
23 TCHAR first
[CMDLINE_LENGTH
];
31 TCHAR szFullCmdLine
[CMDLINE_LENGTH
];
32 PROCESS_INFORMATION prci
;
37 if (_tcsncmp (Rest
, _T("/?"), 2) == 0)
39 ConOutResPaging(TRUE
,STRING_START_HELP1
);
44 comspec
= malloc ( MAX_PATH
* sizeof(TCHAR
));
47 error_out_of_memory();
51 size
= GetEnvironmentVariable (_T("COMSPEC"), comspec
, 512);
52 if(GetLastError() == ERROR_ENVVAR_NOT_FOUND
)
60 comspec
= realloc(comspec
,size
* sizeof(TCHAR
) );
65 size
= GetEnvironmentVariable (_T("COMSPEC"), comspec
, size
);
73 _tcscpy(Rest
,_T("\""));
74 _tcscat(Rest
,comspec
);
75 _tcscat(Rest
,_T("\""));
78 rest
= malloc ( _tcslen(Rest
) + 1 * sizeof(TCHAR
));
83 error_out_of_memory();
87 param
=malloc ( _tcslen(Rest
) + 1 * sizeof(TCHAR
));
93 error_out_of_memory();
102 /* Parsing the command that gets called by start, and it's parameters */
103 if(!_tcschr(rest
,_T('\"')))
106 INT count
= _tcslen(rest
);
108 /* find the end of the command and start of the args */
109 for(i
= 0; i
< count
; i
++)
111 if(rest
[i
] == _T(' '))
114 _tcscpy(param
,&rest
[i
]);
123 INT count
= _tcslen(rest
);
124 BOOL bInside
= FALSE
;
126 /* find the end of the command and put the arguments in param */
127 for(i
= 0; i
< count
; i
++)
129 if(rest
[i
] == _T('\"'))
131 if((rest
[i
] == _T(' ')) && !bInside
)
133 _tcscpy(param
,&rest
[i
]);
139 /* remove any slashes */
142 if(rest
[i
] == _T('\"'))
143 memmove(&rest
[i
],&rest
[i
+ 1], _tcslen(&rest
[i
]) * sizeof(TCHAR
));
149 /* check for a drive change */
151 if (!_tcscmp (first
+ 1, _T(":")) && _istalpha (*first
))
153 TCHAR szPath
[CMDLINE_LENGTH
];
155 _tcscpy (szPath
, _T("A:"));
156 szPath
[0] = _totupper (*first
);
157 SetCurrentDirectory (szPath
);
158 GetCurrentDirectory (CMDLINE_LENGTH
, szPath
);
159 if (szPath
[0] != (TCHAR
)_totupper (*first
))
160 ConErrResPuts (STRING_FREE_ERROR1
);
172 /* get the PATH environment variable and parse it */
173 /* search the PATH environment variable for the binary */
174 if (!SearchForExecutable (rest
, szFullName
))
176 error_bad_command ();
190 /* check if this is a .BAT or .CMD file */
191 if (!_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".bat")) ||
192 !_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".cmd")))
195 memset(szFullCmdLine
,0,CMDLINE_LENGTH
* sizeof(TCHAR
));
198 _tcscpy(szFullCmdLine
,comspec
);
200 memcpy(&szFullCmdLine
[_tcslen(szFullCmdLine
)],_T("\" /K \""), 6 * sizeof(TCHAR
));
201 memcpy(&szFullCmdLine
[_tcslen(szFullCmdLine
)], szFullName
, _tcslen(szFullName
) * sizeof(TCHAR
));
202 memcpy(&szFullCmdLine
[1], &szFullCmdLine
[0], _tcslen(szFullCmdLine
) * sizeof(TCHAR
));
203 szFullCmdLine
[0] = _T('\"');
204 szFullCmdLine
[_tcslen(szFullCmdLine
)] = _T('\"');
208 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName
, rest
);
213 DebugPrintf (_T("[EXEC: %s %s]\n"), szFullName
, rest
);
215 /* build command line for CreateProcess() */
218 _tcscpy (szFullCmdLine
, first
);
222 _tcscat(szFullCmdLine
, _T(" ") );
223 _tcscat (szFullCmdLine
, param
);
227 /* fill startup info */
228 memset (&stui
, 0, sizeof (STARTUPINFO
));
229 stui
.cb
= sizeof (STARTUPINFO
);
230 stui
.dwFlags
= STARTF_USESHOWWINDOW
;
231 stui
.wShowWindow
= SW_SHOWDEFAULT
;
235 bCreate
= CreateProcess (NULL
, szFullCmdLine
, NULL
, NULL
, FALSE
,
236 CREATE_NEW_CONSOLE
, NULL
, NULL
, &stui
, &prci
);
240 bCreate
= CreateProcess (szFullName
, szFullCmdLine
, NULL
, NULL
, FALSE
,
241 CREATE_NEW_CONSOLE
, NULL
, NULL
, &stui
, &prci
);
249 WaitForSingleObject (prci
.hProcess
, INFINITE
);
250 GetExitCodeProcess (prci
.hProcess
, &dwExitCode
);
251 nErrorLevel
= (INT
)dwExitCode
;
253 CloseHandle (prci
.hThread
);
254 CloseHandle (prci
.hProcess
);
255 /* Get New code page if it has change */
256 InputCodePage
= GetConsoleCP();
257 OutputCodePage
= GetConsoleOutputCP();
261 ErrorMessage(GetLastError (),
262 _T("Error executing CreateProcess()!!\n"));