1 // MainAr.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../../Common/MyException.h"
6 #include "../../../Common/StdOutStream.h"
7 
8 #include "../../../Windows/ErrorMsg.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 = NULL;
19 CStdOutStream *g_ErrStream = NULL;
20 
21 extern int Main2(
22   #ifndef _WIN32
23   int numArgs, char *args[]
24   #endif
25 );
26 
27 static const char * const kException_CmdLine_Error_Message = "Command Line Error:";
28 static const char * const kExceptionErrorMessage = "ERROR:";
29 static const char * const kUserBreakMessage  = "Break signaled";
30 static const char * const kMemoryExceptionMessage = "ERROR: Can't allocate required memory!";
31 static const char * const kUnknownExceptionMessage = "Unknown Error";
32 static const char * const kInternalExceptionMessage = "\n\nInternal Error #";
33 
FlushStreams()34 static void FlushStreams()
35 {
36   if (g_StdStream)
37     g_StdStream->Flush();
38 }
39 
PrintError(const char * message)40 static void PrintError(const char *message)
41 {
42   FlushStreams();
43   if (g_ErrStream)
44     *g_ErrStream << "\n\n" << message << endl;
45 }
46 
47 #define NT_CHECK_FAIL_ACTION *g_StdStream << "Unsupported Windows version"; return NExitCode::kFatalError;
48 
main(int numArgs,char * args[])49 int MY_CDECL main
50 (
51   #ifndef _WIN32
52   int numArgs, char *args[]
53   #endif
54 )
55 {
56   g_ErrStream = &g_StdErr;
57   g_StdStream = &g_StdOut;
58 
59   NT_CHECK
60 
61   NConsoleClose::CCtrlHandlerSetter ctrlHandlerSetter;
62   int res = 0;
63 
64   try
65   {
66     res = Main2(
67     #ifndef _WIN32
68     numArgs, args
69     #endif
70     );
71   }
72   catch(const CNewException &)
73   {
74     PrintError(kMemoryExceptionMessage);
75     return (NExitCode::kMemoryError);
76   }
77   catch(const NConsoleClose::CCtrlBreakException &)
78   {
79     PrintError(kUserBreakMessage);
80     return (NExitCode::kUserBreak);
81   }
82   catch(const CArcCmdLineException &e)
83   {
84     PrintError(kException_CmdLine_Error_Message);
85     if (g_ErrStream)
86       *g_ErrStream << e << endl;
87     return (NExitCode::kUserError);
88   }
89   catch(const CSystemException &systemError)
90   {
91     if (systemError.ErrorCode == E_OUTOFMEMORY)
92     {
93       PrintError(kMemoryExceptionMessage);
94       return (NExitCode::kMemoryError);
95     }
96     if (systemError.ErrorCode == E_ABORT)
97     {
98       PrintError(kUserBreakMessage);
99       return (NExitCode::kUserBreak);
100     }
101     if (g_ErrStream)
102     {
103       PrintError("System ERROR:");
104       *g_ErrStream << NError::MyFormatMessage(systemError.ErrorCode) << endl;
105     }
106     return (NExitCode::kFatalError);
107   }
108   catch(NExitCode::EEnum &exitCode)
109   {
110     FlushStreams();
111     if (g_ErrStream)
112       *g_ErrStream << kInternalExceptionMessage << exitCode << endl;
113     return (exitCode);
114   }
115   catch(const UString &s)
116   {
117     if (g_ErrStream)
118     {
119       PrintError(kExceptionErrorMessage);
120       *g_ErrStream << s << endl;
121     }
122     return (NExitCode::kFatalError);
123   }
124   catch(const AString &s)
125   {
126     if (g_ErrStream)
127     {
128       PrintError(kExceptionErrorMessage);
129       *g_ErrStream << s << endl;
130     }
131     return (NExitCode::kFatalError);
132   }
133   catch(const char *s)
134   {
135     if (g_ErrStream)
136     {
137       PrintError(kExceptionErrorMessage);
138       *g_ErrStream << s << endl;
139     }
140     return (NExitCode::kFatalError);
141   }
142   catch(const wchar_t *s)
143   {
144     if (g_ErrStream)
145     {
146       PrintError(kExceptionErrorMessage);
147       *g_ErrStream << s << endl;
148     }
149     return (NExitCode::kFatalError);
150   }
151   catch(int t)
152   {
153     if (g_ErrStream)
154     {
155       FlushStreams();
156       *g_ErrStream << kInternalExceptionMessage << t << endl;
157       return (NExitCode::kFatalError);
158     }
159   }
160   catch(...)
161   {
162     PrintError(kUnknownExceptionMessage);
163     return (NExitCode::kFatalError);
164   }
165 
166   return res;
167 }
168