]> git.proxmox.com Git - mirror_edk2.git/blobdiff - StdLib/LibC/Main/Main.c
Add Socket Libraries.
[mirror_edk2.git] / StdLib / LibC / Main / Main.c
index 47103ce388a31620fa675291025feff45144c78c..3a5cca5b2461cc379a8a41c0204c7b76bff51243 100644 (file)
@@ -8,13 +8,14 @@
   This program and the accompanying materials are licensed and made available under\r
   the terms and conditions of the BSD License that accompanies this distribution.\r
   The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php.\r
+  http://opensource.org/licenses/bsd-license.\r
 \r
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 **/\r
 #include  <Uefi.h>\r
 #include  <Library/UefiLib.h>\r
+#include  <Library/DebugLib.h>\r
 \r
 #include  <Library/ShellCEntryLib.h>\r
 #include  <Library/MemoryAllocationLib.h>\r
@@ -40,23 +41,30 @@ void __main()
   ;\r
 }\r
 \r
-static\r
+/** Clean up data as required by the exit() function.\r
+\r
+**/\r
 void\r
-FinalCleanup( void )\r
+exitCleanup(INTN ExitVal)\r
 {\r
+  void (*CleanUp)(void);   // Pointer to Cleanup Function\r
   int i;\r
 \r
-  /* Close any open files */\r
-  for(i = OPEN_MAX - 1; i >= 0; --i) {\r
-    (void)close(i);   // Close properly handles closing a closed file.\r
+  if(gMD != NULL) {\r
+    gMD->ExitValue = (int)ExitVal;\r
+    CleanUp = gMD->cleanup; // Preserve the pointer to the Cleanup Function\r
+\r
+    // Call all registered atexit functions in reverse order\r
+    i = gMD->num_atexit;\r
+    if( i > 0) {\r
+      do {\r
+        (gMD->atexit_handler[--i])();\r
+      } while( i > 0);\r
   }\r
 \r
-  /* Free the global MainData structure */\r
-  if(gMD != NULL) {\r
-    if(gMD->NCmdLine != NULL) {\r
-      FreePool( gMD->NCmdLine );\r
+    if (CleanUp != NULL) {\r
+      CleanUp();\r
     }\r
-    FreePool( gMD );\r
   }\r
 }\r
 \r
@@ -71,6 +79,13 @@ ArgvConvert(UINTN Argc, CHAR16 **Argv)
   char   *string;\r
   INTN    nArgvSize;  /* Cumulative size of narrow Argv[i] */\r
 \r
+DEBUG_CODE_BEGIN();\r
+  Print(L"ArgvConvert called with %d arguments.\n", Argc);\r
+  for(count = 0; count < ((Argc > 5)? 5: Argc); ++count) {\r
+    Print(L"Argument[%d] = \"%s\".\n", count, Argv[count]);\r
+  }\r
+DEBUG_CODE_END();\r
+\r
   nArgvSize = Argc;\r
   /* Determine space needed for narrow Argv strings. */\r
   for(count = 0; count < Argc; ++count) {\r
@@ -98,6 +113,7 @@ ArgvConvert(UINTN Argc, CHAR16 **Argv)
     nArgv[count] = string;\r
     AVsz = wcstombs(string, Argv[count], nArgvSize);\r
     string[AVsz] = 0;   /* NULL terminate the argument */\r
+    DEBUG((DEBUG_INFO, "Cvt[%d] %d \"%s\" --> \"%a\"\n", (INT32)count, (INT32)AVsz, Argv[count], nArgv[count]));\r
     string += AVsz + 1;\r
     nArgvSize -= AVsz + 1;\r
     if(nArgvSize < 0) {\r
@@ -119,7 +135,7 @@ ShellAppMain (
   struct __filedes   *mfd;\r
   char              **nArgv;\r
   INTN   ExitVal;\r
-  INTN   i;\r
+  int                 i;\r
 \r
   ExitVal = (INTN)RETURN_SUCCESS;\r
   gMD = AllocateZeroPool(sizeof(struct __MainData));\r
@@ -132,7 +148,6 @@ ShellAppMain (
     _fltused              = 1;\r
     errno                 = 0;\r
     EFIerrno              = 0;\r
-    gMD->FinalCleanup     = &FinalCleanup;\r
 \r
 #ifdef NT32dvm\r
     gMD->ClocksPerSecond  = 1;  // For NT32 only\r
@@ -166,10 +181,33 @@ ShellAppMain (
       ExitVal = (INTN)RETURN_INVALID_PARAMETER;\r
     }\r
     else {\r
-      ExitVal = (INTN)main( (int)Argc, nArgv);\r
+      if( setjmp(gMD->MainExit) == 0) {\r
+        ExitVal = (INTN)main( (int)Argc, gMD->NArgV);\r
+        exitCleanup(ExitVal);\r
+      }\r
+      /* You reach here if:\r
+          * normal return from main()\r
+          * call to _Exit(), either directly or through exit().\r
+      */\r
+      ExitVal = (INTN)gMD->ExitValue;\r
+    }\r
+\r
+    if( ExitVal == EXIT_FAILURE) {\r
+      ExitVal = RETURN_ABORTED;\r
+    }\r
+\r
+    /* Close any open files */\r
+    for(i = OPEN_MAX - 1; i >= 0; --i) {\r
+      (void)close(i);   // Close properly handles closing a closed file.\r
+    }\r
+\r
+    /* Free the global MainData structure */\r
+    if(gMD != NULL) {\r
+      if(gMD->NCmdLine != NULL) {\r
+        FreePool( gMD->NCmdLine );\r
+      }\r
+      FreePool( gMD );\r
   }\r
   }\r
-  exit((int)ExitVal);\r
-  /* Not Reached */\r
   return ExitVal;\r
 }\r