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