2011-04-27 23:42:16 +02:00
|
|
|
/** @file
|
|
|
|
Establish the program environment and the "main" entry point.
|
|
|
|
|
|
|
|
All of the global data in the gMD structure is initialized to 0, NULL, or
|
|
|
|
SIG_DFL; as appropriate.
|
|
|
|
|
2012-03-24 02:19:06 +01:00
|
|
|
Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
|
2011-04-27 23:42:16 +02:00
|
|
|
This program and the accompanying materials are licensed and made available under
|
|
|
|
the terms and conditions of the BSD License that accompanies this distribution.
|
|
|
|
The full text of the license may be found at
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
http://opensource.org/licenses/bsd-license.
|
2011-04-27 23:42:16 +02:00
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
**/
|
|
|
|
#include <Uefi.h>
|
|
|
|
#include <Library/UefiLib.h>
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
#include <Library/DebugLib.h>
|
2011-04-27 23:42:16 +02:00
|
|
|
|
|
|
|
#include <Library/ShellCEntryLib.h>
|
|
|
|
#include <Library/MemoryAllocationLib.h>
|
|
|
|
#include <Library/TimerLib.h>
|
|
|
|
|
|
|
|
#include <LibConfig.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2011-06-28 04:34:10 +02:00
|
|
|
#include <stdlib.h>
|
2011-04-27 23:42:16 +02:00
|
|
|
#include <string.h>
|
2011-11-11 01:32:31 +01:00
|
|
|
#include <time.h>
|
2011-04-27 23:42:16 +02:00
|
|
|
#include <MainData.h>
|
2011-11-11 01:32:31 +01:00
|
|
|
#include <unistd.h>
|
2011-04-27 23:42:16 +02:00
|
|
|
|
2011-06-28 04:34:10 +02:00
|
|
|
extern int main( int, char**);
|
2011-04-27 23:42:16 +02:00
|
|
|
extern int __sse2_available;
|
|
|
|
|
|
|
|
struct __MainData *gMD;
|
|
|
|
|
|
|
|
/* Worker function to keep GCC happy. */
|
|
|
|
void __main()
|
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
/** Clean up data as required by the exit() function.
|
|
|
|
|
|
|
|
**/
|
2011-06-28 04:34:10 +02:00
|
|
|
void
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
exitCleanup(INTN ExitVal)
|
2011-06-28 04:34:10 +02:00
|
|
|
{
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
void (*CleanUp)(void); // Pointer to Cleanup Function
|
2011-06-28 04:34:10 +02:00
|
|
|
int i;
|
|
|
|
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
if(gMD != NULL) {
|
|
|
|
gMD->ExitValue = (int)ExitVal;
|
|
|
|
CleanUp = gMD->cleanup; // Preserve the pointer to the Cleanup Function
|
|
|
|
|
|
|
|
// Call all registered atexit functions in reverse order
|
|
|
|
i = gMD->num_atexit;
|
|
|
|
if( i > 0) {
|
|
|
|
do {
|
|
|
|
(gMD->atexit_handler[--i])();
|
|
|
|
} while( i > 0);
|
2011-06-28 04:34:10 +02:00
|
|
|
}
|
|
|
|
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
if (CleanUp != NULL) {
|
|
|
|
CleanUp();
|
2011-06-28 04:34:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create mbcs versions of the Argv strings. */
|
|
|
|
static
|
|
|
|
char **
|
|
|
|
ArgvConvert(UINTN Argc, CHAR16 **Argv)
|
|
|
|
{
|
2012-03-24 02:19:06 +01:00
|
|
|
ssize_t AVsz; /* Size of a single nArgv string, or -1 */
|
2011-06-28 04:34:10 +02:00
|
|
|
UINTN count;
|
|
|
|
char **nArgv;
|
|
|
|
char *string;
|
|
|
|
INTN nArgvSize; /* Cumulative size of narrow Argv[i] */
|
|
|
|
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
DEBUG_CODE_BEGIN();
|
|
|
|
Print(L"ArgvConvert called with %d arguments.\n", Argc);
|
|
|
|
for(count = 0; count < ((Argc > 5)? 5: Argc); ++count) {
|
|
|
|
Print(L"Argument[%d] = \"%s\".\n", count, Argv[count]);
|
|
|
|
}
|
|
|
|
DEBUG_CODE_END();
|
|
|
|
|
2011-06-28 04:34:10 +02:00
|
|
|
nArgvSize = Argc;
|
|
|
|
/* Determine space needed for narrow Argv strings. */
|
|
|
|
for(count = 0; count < Argc; ++count) {
|
2012-03-24 02:19:06 +01:00
|
|
|
AVsz = (ssize_t)wcstombs(NULL, Argv[count], ARG_MAX);
|
2011-06-28 04:34:10 +02:00
|
|
|
if(AVsz < 0) {
|
|
|
|
Print(L"ABORTING: Argv[%d] contains an unconvertable character.\n", count);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
/* Not Reached */
|
|
|
|
}
|
|
|
|
nArgvSize += AVsz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reserve space for the converted strings. */
|
|
|
|
gMD->NCmdLine = (char *)AllocateZeroPool(nArgvSize+1);
|
|
|
|
if(gMD->NCmdLine == NULL) {
|
|
|
|
Print(L"ABORTING: Insufficient memory.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
/* Not Reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert Argument Strings. */
|
|
|
|
nArgv = gMD->NArgV;
|
|
|
|
string = gMD->NCmdLine;
|
|
|
|
for(count = 0; count < Argc; ++count) {
|
|
|
|
nArgv[count] = string;
|
2012-10-12 19:58:30 +02:00
|
|
|
AVsz = wcstombs(string, Argv[count], nArgvSize) + 1;
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
DEBUG((DEBUG_INFO, "Cvt[%d] %d \"%s\" --> \"%a\"\n", (INT32)count, (INT32)AVsz, Argv[count], nArgv[count]));
|
2012-10-06 00:00:43 +02:00
|
|
|
string += AVsz;
|
|
|
|
nArgvSize -= AVsz;
|
2011-06-28 04:34:10 +02:00
|
|
|
if(nArgvSize < 0) {
|
|
|
|
Print(L"ABORTING: Internal Argv[%d] conversion error.\n", count);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
/* Not Reached */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gMD->NArgV;
|
|
|
|
}
|
|
|
|
|
2011-04-27 23:42:16 +02:00
|
|
|
INTN
|
|
|
|
EFIAPI
|
|
|
|
ShellAppMain (
|
|
|
|
IN UINTN Argc,
|
|
|
|
IN CHAR16 **Argv
|
|
|
|
)
|
|
|
|
{
|
2011-06-28 04:34:10 +02:00
|
|
|
struct __filedes *mfd;
|
|
|
|
char **nArgv;
|
2011-04-27 23:42:16 +02:00
|
|
|
INTN ExitVal;
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
int i;
|
2011-04-27 23:42:16 +02:00
|
|
|
|
|
|
|
ExitVal = (INTN)RETURN_SUCCESS;
|
|
|
|
gMD = AllocateZeroPool(sizeof(struct __MainData));
|
|
|
|
if( gMD == NULL ) {
|
|
|
|
ExitVal = (INTN)RETURN_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Initialize data */
|
|
|
|
__sse2_available = 0;
|
|
|
|
_fltused = 1;
|
|
|
|
errno = 0;
|
|
|
|
EFIerrno = 0;
|
|
|
|
|
2011-11-11 01:32:31 +01:00
|
|
|
gMD->ClocksPerSecond = 1;
|
|
|
|
gMD->AppStartTime = (clock_t)((UINT32)time(NULL));
|
2011-04-27 23:42:16 +02:00
|
|
|
|
|
|
|
// Initialize file descriptors
|
|
|
|
mfd = gMD->fdarray;
|
|
|
|
for(i = 0; i < (FOPEN_MAX); ++i) {
|
|
|
|
mfd[i].MyFD = (UINT16)i;
|
|
|
|
}
|
|
|
|
|
2012-12-11 22:19:14 +01:00
|
|
|
i = open("stdin:", (O_RDONLY | O_TTY_INIT), 0444);
|
2011-06-28 04:34:10 +02:00
|
|
|
if(i == 0) {
|
2012-12-11 22:19:14 +01:00
|
|
|
i = open("stdout:", (O_WRONLY | O_TTY_INIT), 0222);
|
2011-06-28 04:34:10 +02:00
|
|
|
if(i == 1) {
|
|
|
|
i = open("stderr:", O_WRONLY, 0222);
|
2011-04-27 23:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-28 04:34:10 +02:00
|
|
|
if(i != 2) {
|
2011-04-27 23:42:16 +02:00
|
|
|
Print(L"ERROR Initializing Standard IO: %a.\n %r\n",
|
|
|
|
strerror(errno), EFIerrno);
|
|
|
|
}
|
|
|
|
|
2011-06-28 04:34:10 +02:00
|
|
|
/* Create mbcs versions of the Argv strings. */
|
|
|
|
nArgv = ArgvConvert(Argc, Argv);
|
|
|
|
if(nArgv == NULL) {
|
|
|
|
ExitVal = (INTN)RETURN_INVALID_PARAMETER;
|
2011-04-27 23:42:16 +02:00
|
|
|
}
|
2011-06-28 04:34:10 +02:00
|
|
|
else {
|
Add Socket Libraries.
Add Posix functions for porting compatibility.
Fix compliance issues with ISO/IEC 9899:199409
New Functions:
setenv(), fparseln(), GetFileNameFromPath(), rename(),
realpath(), setprogname(), getprogname(), strlcat(), strlcpy(),
strsep(), setitimer(), getitimer(), timegm(), getopt(), basename(),
mkstemp(), ffs(), vsnprintf(), snprintf(), getpass(), usleep(), select(),
writev(), strcasecmp(), getcwd(), chdir(), tcgetpgrp(), getpgrp(), gettimeofday(),
bcopy(),
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12061 6f19259b-4bc3-4df7-8a09-765794883524
2011-07-30 02:30:44 +02:00
|
|
|
if( setjmp(gMD->MainExit) == 0) {
|
|
|
|
ExitVal = (INTN)main( (int)Argc, gMD->NArgV);
|
|
|
|
exitCleanup(ExitVal);
|
|
|
|
}
|
|
|
|
/* You reach here if:
|
|
|
|
* normal return from main()
|
|
|
|
* call to _Exit(), either directly or through exit().
|
|
|
|
*/
|
|
|
|
ExitVal = (INTN)gMD->ExitValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ExitVal == EXIT_FAILURE) {
|
|
|
|
ExitVal = RETURN_ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close any open files */
|
|
|
|
for(i = OPEN_MAX - 1; i >= 0; --i) {
|
|
|
|
(void)close(i); // Close properly handles closing a closed file.
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the global MainData structure */
|
|
|
|
if(gMD != NULL) {
|
|
|
|
if(gMD->NCmdLine != NULL) {
|
|
|
|
FreePool( gMD->NCmdLine );
|
|
|
|
}
|
|
|
|
FreePool( gMD );
|
2011-04-27 23:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ExitVal;
|
|
|
|
}
|