X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=StdLib%2FLibC%2FMain%2FMain.c;h=3a5cca5b2461cc379a8a41c0204c7b76bff51243;hp=47103ce388a31620fa675291025feff45144c78c;hb=d7ce700605e1af0e455e31ec11f19ff21d26b525;hpb=f766dd76fde231ecd4f2e9faf99293e90902cebb diff --git a/StdLib/LibC/Main/Main.c b/StdLib/LibC/Main/Main.c index 47103ce388..3a5cca5b24 100644 --- a/StdLib/LibC/Main/Main.c +++ b/StdLib/LibC/Main/Main.c @@ -8,13 +8,14 @@ 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 - http://opensource.org/licenses/bsd-license.php. + http://opensource.org/licenses/bsd-license. 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 #include +#include #include #include @@ -40,23 +41,30 @@ void __main() ; } -static +/** Clean up data as required by the exit() function. + +**/ void -FinalCleanup( void ) +exitCleanup(INTN ExitVal) { + void (*CleanUp)(void); // Pointer to Cleanup Function int i; - /* Close any open files */ - for(i = OPEN_MAX - 1; i >= 0; --i) { - (void)close(i); // Close properly handles closing a closed file. + 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); } - /* Free the global MainData structure */ - if(gMD != NULL) { - if(gMD->NCmdLine != NULL) { - FreePool( gMD->NCmdLine ); + if (CleanUp != NULL) { + CleanUp(); } - FreePool( gMD ); } } @@ -71,6 +79,13 @@ ArgvConvert(UINTN Argc, CHAR16 **Argv) char *string; INTN nArgvSize; /* Cumulative size of narrow Argv[i] */ +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(); + nArgvSize = Argc; /* Determine space needed for narrow Argv strings. */ for(count = 0; count < Argc; ++count) { @@ -98,6 +113,7 @@ ArgvConvert(UINTN Argc, CHAR16 **Argv) nArgv[count] = string; AVsz = wcstombs(string, Argv[count], nArgvSize); string[AVsz] = 0; /* NULL terminate the argument */ + DEBUG((DEBUG_INFO, "Cvt[%d] %d \"%s\" --> \"%a\"\n", (INT32)count, (INT32)AVsz, Argv[count], nArgv[count])); string += AVsz + 1; nArgvSize -= AVsz + 1; if(nArgvSize < 0) { @@ -119,7 +135,7 @@ ShellAppMain ( struct __filedes *mfd; char **nArgv; INTN ExitVal; - INTN i; + int i; ExitVal = (INTN)RETURN_SUCCESS; gMD = AllocateZeroPool(sizeof(struct __MainData)); @@ -132,7 +148,6 @@ ShellAppMain ( _fltused = 1; errno = 0; EFIerrno = 0; - gMD->FinalCleanup = &FinalCleanup; #ifdef NT32dvm gMD->ClocksPerSecond = 1; // For NT32 only @@ -166,10 +181,33 @@ ShellAppMain ( ExitVal = (INTN)RETURN_INVALID_PARAMETER; } else { - ExitVal = (INTN)main( (int)Argc, nArgv); + 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 ); } } - exit((int)ExitVal); - /* Not Reached */ return ExitVal; }