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
];
26 TCHAR RestWithoutArgs
[CMDLINE_LENGTH
];
32 TCHAR szFullCmdLine
[CMDLINE_LENGTH
];
33 PROCESS_INFORMATION prci
;
40 RestWithoutArgs
[0] = _T('\0');
42 arg
= split (Rest
, &argc
, FALSE
);
45 for (i
= 0; i
< argc
; i
++)
47 if (!_tcsncmp (arg
[i
], _T("/?"), 2))
50 ConOutResPaging(TRUE
,STRING_START_HELP1
);
53 else if(!_tcsicmp(arg
[i
], _T("/LOW")))
55 Priority
= IDLE_PRIORITY_CLASS
;
57 else if(!_tcsicmp(arg
[i
], _T("/NORMAL")))
59 Priority
= NORMAL_PRIORITY_CLASS
;
61 else if(!_tcsicmp(arg
[i
], _T("/HIGH")))
63 Priority
= HIGH_PRIORITY_CLASS
;
65 else if(!_tcsicmp(arg
[i
], _T("/REALTIME")))
67 Priority
= REALTIME_PRIORITY_CLASS
;
69 else if(!_tcsicmp(arg
[i
], _T("/ABOVENORMAL")))
71 Priority
= ABOVE_NORMAL_PRIORITY_CLASS
;
73 else if(!_tcsicmp(arg
[i
], _T("/BELOWNORMAL")))
75 Priority
= BELOW_NORMAL_PRIORITY_CLASS
;
79 if(RestWithoutArgs
[0] != _T('\0'))
81 _tcscat(RestWithoutArgs
,_T(" "));
83 _tcscat(RestWithoutArgs
,arg
[i
]);
93 comspec
= malloc ( MAX_PATH
* sizeof(TCHAR
));
96 error_out_of_memory();
100 size
= GetEnvironmentVariable (_T("COMSPEC"), comspec
, 512);
101 if(GetLastError() == ERROR_ENVVAR_NOT_FOUND
)
103 RestWithoutArgs
[0] = _T('c');
104 RestWithoutArgs
[1] = _T('m');
105 RestWithoutArgs
[2] = _T('d');
106 RestWithoutArgs
[3] = _T('\0');
112 comspec
= realloc(comspec
,size
* sizeof(TCHAR
) );
117 size
= GetEnvironmentVariable (_T("COMSPEC"), comspec
, size
);
123 if(!_tcslen(RestWithoutArgs
))
125 _tcscpy(RestWithoutArgs
,_T("\""));
126 _tcscat(RestWithoutArgs
,comspec
);
127 _tcscat(RestWithoutArgs
,_T("\""));
130 rest
= malloc ( _tcslen(RestWithoutArgs
) + 1 * sizeof(TCHAR
));
135 error_out_of_memory();
139 param
=malloc ( _tcslen(RestWithoutArgs
) + 1 * sizeof(TCHAR
));
145 error_out_of_memory();
152 _tcscpy(rest
,RestWithoutArgs
);
154 /* Parsing the command that gets called by start, and it's parameters */
155 if(!_tcschr(rest
,_T('\"')))
157 INT count
= _tcslen(rest
);
159 /* find the end of the command and start of the args */
160 for(i
= 0; i
< count
; i
++)
162 if(rest
[i
] == _T(' '))
165 _tcscpy(param
,&rest
[i
+1]);
173 INT count
= _tcslen(rest
);
174 BOOL bInside
= FALSE
;
176 /* find the end of the command and put the arguments in param */
177 for(i
= 0; i
< count
; i
++)
179 if(rest
[i
] == _T('\"'))
181 if((rest
[i
] == _T(' ')) && !bInside
)
183 _tcscpy(param
,&rest
[i
+1]);
189 /* remove any slashes */
192 if(rest
[i
] == _T('\"'))
193 memmove(&rest
[i
],&rest
[i
+ 1], _tcslen(&rest
[i
]) * sizeof(TCHAR
));
199 /* check for a drive change */
201 if (!_tcscmp (first
+ 1, _T(":")) && _istalpha (*first
))
203 TCHAR szPath
[CMDLINE_LENGTH
];
205 _tcscpy (szPath
, _T("A:"));
206 szPath
[0] = _totupper (*first
);
207 SetCurrentDirectory (szPath
);
208 GetCurrentDirectory (CMDLINE_LENGTH
, szPath
);
209 if (szPath
[0] != (TCHAR
)_totupper (*first
))
210 ConErrResPuts (STRING_FREE_ERROR1
);
222 /* get the PATH environment variable and parse it */
223 /* search the PATH environment variable for the binary */
224 if (!SearchForExecutable (rest
, szFullName
))
226 error_bad_command ();
240 /* check if this is a .BAT or .CMD file */
241 if (!_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".bat")) ||
242 !_tcsicmp (_tcsrchr (szFullName
, _T('.')), _T(".cmd")))
245 memset(szFullCmdLine
,0,CMDLINE_LENGTH
* sizeof(TCHAR
));
248 _tcscpy(szFullCmdLine
,comspec
);
250 memcpy(&szFullCmdLine
[_tcslen(szFullCmdLine
)],_T("\" /K \""), 6 * sizeof(TCHAR
));
251 memcpy(&szFullCmdLine
[_tcslen(szFullCmdLine
)], szFullName
, _tcslen(szFullName
) * sizeof(TCHAR
));
252 memcpy(&szFullCmdLine
[1], &szFullCmdLine
[0], _tcslen(szFullCmdLine
) * sizeof(TCHAR
));
253 szFullCmdLine
[0] = _T('\"');
254 szFullCmdLine
[_tcslen(szFullCmdLine
)] = _T('\"');
258 DebugPrintf (_T("[BATCH: %s %s]\n"), szFullName
, rest
);
263 DebugPrintf (_T("[EXEC: %s %s]\n"), szFullName
, rest
);
265 /* build command line for CreateProcess() */
268 _tcscpy (szFullCmdLine
, first
);
272 _tcscat(szFullCmdLine
, _T(" ") );
273 _tcscat (szFullCmdLine
, param
);
277 /* fill startup info */
278 memset (&stui
, 0, sizeof (STARTUPINFO
));
279 stui
.cb
= sizeof (STARTUPINFO
);
280 stui
.dwFlags
= STARTF_USESHOWWINDOW
;
281 stui
.wShowWindow
= SW_SHOWDEFAULT
;
285 bCreate
= CreateProcess (NULL
, szFullCmdLine
, NULL
, NULL
, FALSE
,
286 CREATE_NEW_CONSOLE
| Priority
, NULL
, NULL
, &stui
, &prci
);
290 bCreate
= CreateProcess (szFullName
, szFullCmdLine
, NULL
, NULL
, FALSE
,
291 CREATE_NEW_CONSOLE
| Priority
, NULL
, NULL
, &stui
, &prci
);
299 WaitForSingleObject (prci
.hProcess
, INFINITE
);
300 GetExitCodeProcess (prci
.hProcess
, &dwExitCode
);
301 nErrorLevel
= (INT
)dwExitCode
;
303 CloseHandle (prci
.hThread
);
304 CloseHandle (prci
.hProcess
);
305 /* Get New code page if it has change */
306 InputCodePage
= GetConsoleCP();
307 OutputCodePage
= GetConsoleOutputCP();
311 ErrorMessage(GetLastError (),
312 _T("Error executing CreateProcess()!!\n"));