2 * START.C - start internal command.
7 * 24-Jul-1999 (Eric Kohl)
10 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
11 * Remove all hardcode string to En.rc
16 #ifdef INCLUDE_CMD_START
19 INT
cmd_start (LPTSTR First
, LPTSTR Rest
)
21 TCHAR szFullName
[CMDLINE_LENGTH
];
22 TCHAR first
[CMDLINE_LENGTH
];
25 TCHAR RestWithoutArgs
[CMDLINE_LENGTH
];
31 TCHAR szFullCmdLine
[CMDLINE_LENGTH
];
32 PROCESS_INFORMATION prci
;
39 RestWithoutArgs
[0] = _T('\0');
41 arg
= split (Rest
, &argc
, FALSE
);
44 for (i
= 0; i
< argc
; i
++)
46 if (!_tcsncmp (arg
[i
], _T("/?"), 2))
49 ConOutResPaging(TRUE
,STRING_START_HELP1
);
52 else if(!_tcsicmp(arg
[i
], _T("/LOW")))
54 Priority
= IDLE_PRIORITY_CLASS
;
56 else if(!_tcsicmp(arg
[i
], _T("/NORMAL")))
58 Priority
= NORMAL_PRIORITY_CLASS
;
60 else if(!_tcsicmp(arg
[i
], _T("/HIGH")))
62 Priority
= HIGH_PRIORITY_CLASS
;
64 else if(!_tcsicmp(arg
[i
], _T("/REALTIME")))
66 Priority
= REALTIME_PRIORITY_CLASS
;
68 else if(!_tcsicmp(arg
[i
], _T("/ABOVENORMAL")))
70 Priority
= ABOVE_NORMAL_PRIORITY_CLASS
;
72 else if(!_tcsicmp(arg
[i
], _T("/BELOWNORMAL")))
74 Priority
= BELOW_NORMAL_PRIORITY_CLASS
;
78 if(RestWithoutArgs
[0] != _T('\0'))
80 _tcscat(RestWithoutArgs
,_T(" "));
82 _tcscat(RestWithoutArgs
,arg
[i
]);
92 comspec
= cmd_alloc ( MAX_PATH
* sizeof(TCHAR
));
95 error_out_of_memory();
99 size
= GetEnvironmentVariable (_T("COMSPEC"), comspec
, MAX_PATH
);
100 if(GetLastError() == ERROR_ENVVAR_NOT_FOUND
)
102 RestWithoutArgs
[0] = _T('c');
103 RestWithoutArgs
[1] = _T('m');
104 RestWithoutArgs
[2] = _T('d');
105 RestWithoutArgs
[3] = _T('\0');
111 comspec
= cmd_realloc(comspec
,size
* sizeof(TCHAR
) );
116 size
= GetEnvironmentVariable (_T("COMSPEC"), comspec
, size
);
122 if(!_tcslen(RestWithoutArgs
))
124 _tcscpy(RestWithoutArgs
,_T("\""));
125 _tcscat(RestWithoutArgs
,comspec
);
126 _tcscat(RestWithoutArgs
,_T("\""));
129 rest
= cmd_alloc ( (_tcslen(RestWithoutArgs
) + 1) * sizeof(TCHAR
));
134 error_out_of_memory();
138 param
= cmd_alloc ( (_tcslen(RestWithoutArgs
) + 1) * sizeof(TCHAR
));
144 error_out_of_memory();
151 _tcscpy(rest
,RestWithoutArgs
);
153 /* Parsing the command that gets called by start, and it's parameters */
154 if(!_tcschr(rest
,_T('\"')))
156 INT count
= _tcslen(rest
);
158 /* find the end of the command and start of the args */
159 for(i
= 0; i
< count
; i
++)
161 if(rest
[i
] == _T(' '))
164 _tcscpy(param
,&rest
[i
+1]);
172 INT count
= _tcslen(rest
);
173 BOOL bInside
= FALSE
;
175 /* find the end of the command and put the arguments in param */
176 for(i
= 0; i
< count
; i
++)
178 if(rest
[i
] == _T('\"'))
180 if((rest
[i
] == _T(' ')) && !bInside
)
182 _tcscpy(param
,&rest
[i
+1]);
188 /* remove any slashes */
191 if(rest
[i
] == _T('\"'))
192 memmove(&rest
[i
],&rest
[i
+ 1], _tcslen(&rest
[i
]) * sizeof(TCHAR
));
198 /* check for a drive change */
200 if (!_tcscmp (first
+ 1, _T(":")) && _istalpha (*first
))
202 TCHAR szPath
[CMDLINE_LENGTH
];
204 _tcscpy (szPath
, _T("A:"));
205 szPath
[0] = _totupper (*first
);
206 SetCurrentDirectory (szPath
);
207 GetCurrentDirectory (CMDLINE_LENGTH
, szPath
);
208 if (szPath
[0] != (TCHAR
)_totupper (*first
))
209 ConErrResPuts (STRING_FREE_ERROR1
);
221 /* get the PATH environment variable and parse it */
222 /* search the PATH environment variable for the binary */
223 if (!SearchForExecutable (rest
, szFullName
))
225 error_bad_command ();
239 /* check if this is a .BAT or .CMD file */
240 if (!_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".bat")) ||
241 !_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".cmd")))
244 memset(szFullCmdLine
,0,CMDLINE_LENGTH
* sizeof(TCHAR
));
247 _tcscpy(szFullCmdLine
,comspec
);
249 memcpy(&szFullCmdLine
[_tcslen(szFullCmdLine
)],_T("\" /K \""), 6 * sizeof(TCHAR
));
250 memcpy(&szFullCmdLine
[_tcslen(szFullCmdLine
)], szFullName
, _tcslen(szFullName
) * sizeof(TCHAR
));
251 memcpy(&szFullCmdLine
[1], &szFullCmdLine
[0], _tcslen(szFullCmdLine
) * sizeof(TCHAR
));
252 szFullCmdLine
[0] = _T('\"');
253 szFullCmdLine
[_tcslen(szFullCmdLine
)] = _T('\"');
257 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName
, rest
);
262 DebugPrintf (_T("[EXEC: %s %s]\n"), szFullName
, rest
);
264 /* build command line for CreateProcess() */
267 _tcscpy (szFullCmdLine
, first
);
271 _tcscat(szFullCmdLine
, _T(" ") );
272 _tcscat (szFullCmdLine
, param
);
276 /* fill startup info */
277 memset (&stui
, 0, sizeof (STARTUPINFO
));
278 stui
.cb
= sizeof (STARTUPINFO
);
279 stui
.dwFlags
= STARTF_USESHOWWINDOW
;
280 stui
.wShowWindow
= SW_SHOWDEFAULT
;
284 bCreate
= CreateProcess (NULL
, szFullCmdLine
, NULL
, NULL
, FALSE
,
285 CREATE_NEW_CONSOLE
| Priority
, NULL
, NULL
, &stui
, &prci
);
289 bCreate
= CreateProcess (szFullName
, szFullCmdLine
, NULL
, NULL
, FALSE
,
290 CREATE_NEW_CONSOLE
| Priority
, NULL
, NULL
, &stui
, &prci
);
298 WaitForSingleObject (prci
.hProcess
, INFINITE
);
299 GetExitCodeProcess (prci
.hProcess
, &dwExitCode
);
300 nErrorLevel
= (INT
)dwExitCode
;
302 CloseHandle (prci
.hThread
);
303 CloseHandle (prci
.hProcess
);
304 /* Get New code page if it has change */
305 InputCodePage
= GetConsoleCP();
306 OutputCodePage
= GetConsoleOutputCP();
310 ErrorMessage(GetLastError (),
311 _T("Error executing CreateProcess()!!\n"));