Home | History | Annotate | Download | only in Console
      1 // MainAr.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include "Common/MyException.h"
      6 #include "Common/StdOutStream.h"
      7 
      8 #include "Windows/Error.h"
      9 #include "Windows/NtCheck.h"
     10 
     11 #include "../Common/ArchiveCommandLine.h"
     12 #include "../Common/ExitCode.h"
     13 
     14 #include "ConsoleClose.h"
     15 
     16 using namespace NWindows;
     17 
     18 CStdOutStream *g_StdStream = 0;
     19 
     20 extern int Main2(
     21   #ifndef _WIN32
     22   int numArgs, const char *args[]
     23   #endif
     24 );
     25 
     26 static const char *kExceptionErrorMessage = "\n\nError:\n";
     27 static const char *kUserBreak  = "\nBreak signaled\n";
     28 static const char *kMemoryExceptionMessage = "\n\nERROR: Can't allocate required memory!\n";
     29 static const char *kUnknownExceptionMessage = "\n\nUnknown Error\n";
     30 static const char *kInternalExceptionMessage = "\n\nInternal Error #";
     31 
     32 #define NT_CHECK_FAIL_ACTION (*g_StdStream) << "Unsupported Windows version"; return NExitCode::kFatalError;
     33 
     34 int MY_CDECL main
     35 (
     36   #ifndef _WIN32
     37   int numArgs, const char *args[]
     38   #endif
     39 )
     40 {
     41   g_StdStream = &g_StdOut;
     42 
     43   NT_CHECK
     44 
     45   NConsoleClose::CCtrlHandlerSetter ctrlHandlerSetter;
     46   int res = 0;
     47   try
     48   {
     49     res = Main2(
     50     #ifndef _WIN32
     51     numArgs, args
     52     #endif
     53     );
     54   }
     55   catch(const CNewException &)
     56   {
     57     (*g_StdStream) << kMemoryExceptionMessage;
     58     return (NExitCode::kMemoryError);
     59   }
     60   catch(const NConsoleClose::CCtrlBreakException &)
     61   {
     62     (*g_StdStream) << endl << kUserBreak;
     63     return (NExitCode::kUserBreak);
     64   }
     65   catch(const CArchiveCommandLineException &e)
     66   {
     67     (*g_StdStream) << kExceptionErrorMessage << e << endl;
     68     return (NExitCode::kUserError);
     69   }
     70   catch(const CSystemException &systemError)
     71   {
     72     if (systemError.ErrorCode == E_OUTOFMEMORY)
     73     {
     74       (*g_StdStream) << kMemoryExceptionMessage;
     75       return (NExitCode::kMemoryError);
     76     }
     77     if (systemError.ErrorCode == E_ABORT)
     78     {
     79       (*g_StdStream) << endl << kUserBreak;
     80       return (NExitCode::kUserBreak);
     81     }
     82     UString message;
     83     NError::MyFormatMessage(systemError.ErrorCode, message);
     84     (*g_StdStream) << endl << endl << "System error:" << endl << message << endl;
     85     return (NExitCode::kFatalError);
     86   }
     87   catch(NExitCode::EEnum &exitCode)
     88   {
     89     (*g_StdStream) << kInternalExceptionMessage << exitCode << endl;
     90     return (exitCode);
     91   }
     92   /*
     93   catch(const NExitCode::CMultipleErrors &multipleErrors)
     94   {
     95     (*g_StdStream) << endl << multipleErrors.NumErrors << " errors" << endl;
     96     return (NExitCode::kFatalError);
     97   }
     98   */
     99   catch(const UString &s)
    100   {
    101     (*g_StdStream) << kExceptionErrorMessage << s << endl;
    102     return (NExitCode::kFatalError);
    103   }
    104   catch(const AString &s)
    105   {
    106     (*g_StdStream) << kExceptionErrorMessage << s << endl;
    107     return (NExitCode::kFatalError);
    108   }
    109   catch(const char *s)
    110   {
    111     (*g_StdStream) << kExceptionErrorMessage << s << endl;
    112     return (NExitCode::kFatalError);
    113   }
    114   catch(int t)
    115   {
    116     (*g_StdStream) << kInternalExceptionMessage << t << endl;
    117     return (NExitCode::kFatalError);
    118   }
    119   catch(...)
    120   {
    121     (*g_StdStream) << kUnknownExceptionMessage;
    122     return (NExitCode::kFatalError);
    123   }
    124   return  res;
    125 }
    126