• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // MainAr.cpp
2 
3 #include "StdAfx.h"
4 
5 #ifdef _WIN32
6 #include "../../../../C/DllSecur.h"
7 #endif
8 #include "../../../../C/CpuArch.h"
9 
10 #include "../../../Common/MyException.h"
11 #include "../../../Common/StdOutStream.h"
12 
13 #include "../../../Windows/ErrorMsg.h"
14 #include "../../../Windows/NtCheck.h"
15 
16 #include "../Common/ArchiveCommandLine.h"
17 #include "../Common/ExitCode.h"
18 
19 #include "ConsoleClose.h"
20 
21 using namespace NWindows;
22 
23 extern
24 CStdOutStream *g_StdStream;
25 CStdOutStream *g_StdStream = NULL;
26 extern
27 CStdOutStream *g_ErrStream;
28 CStdOutStream *g_ErrStream = NULL;
29 
30 extern int Main2(
31   #ifndef _WIN32
32   int numArgs, char *args[]
33   #endif
34 );
35 
36 static const char * const kException_CmdLine_Error_Message = "Command Line Error:";
37 static const char * const kExceptionErrorMessage = "ERROR:";
38 static const char * const kUserBreakMessage  = "Break signaled";
39 static const char * const kMemoryExceptionMessage = "ERROR: Can't allocate required memory!";
40 static const char * const kUnknownExceptionMessage = "Unknown Error";
41 static const char * const kInternalExceptionMessage = "\n\nInternal Error #";
42 
FlushStreams()43 static void FlushStreams()
44 {
45   if (g_StdStream)
46     g_StdStream->Flush();
47 }
48 
PrintError(const char * message)49 static void PrintError(const char *message)
50 {
51   FlushStreams();
52   if (g_ErrStream)
53     *g_ErrStream << "\n\n" << message << endl;
54 }
55 
56 #if defined(_WIN32) && defined(_UNICODE) && !defined(_WIN64) && !defined(UNDER_CE)
57 #define NT_CHECK_FAIL_ACTION *g_StdStream << "Unsupported Windows version"; return NExitCode::kFatalError;
58 #endif
59 
CheckIsa()60 static inline bool CheckIsa()
61 {
62   // __try
63   {
64     // some compilers (e2k) support SSE/AVX, but cpuid() can be unavailable or return lower isa support
65 #ifdef MY_CPU_X86_OR_AMD64
66     #if defined(__AVX2__)
67       if (!CPU_IsSupported_AVX2())
68         return false;
69     #elif defined(__AVX__)
70       if (!CPU_IsSupported_AVX())
71         return false;
72     #elif defined(__SSE2__) && !defined(MY_CPU_AMD64) || defined(_M_IX86_FP) && (_M_IX86_FP >= 2)
73       if (!CPU_IsSupported_SSE2())
74         return false;
75     #elif defined(__SSE__) && !defined(MY_CPU_AMD64) || defined(_M_IX86_FP) && (_M_IX86_FP >= 1)
76       if (!CPU_IsSupported_SSE() ||
77           !CPU_IsSupported_CMOV())
78         return false;
79     #endif
80 #endif
81     /*
82     __asm
83     {
84       _emit 0fH
85       _emit 038H
86       _emit 0cbH
87       _emit (0c0H + 0 * 8 + 0)
88     }
89     */
90     return true;
91   }
92   /*
93   __except (EXCEPTION_EXECUTE_HANDLER)
94   {
95     return false;
96   }
97   */
98 }
99 
main(int numArgs,char * args[])100 int Z7_CDECL main
101 (
102   #ifndef _WIN32
103   int numArgs, char *args[]
104   #endif
105 )
106 {
107   g_ErrStream = &g_StdErr;
108   g_StdStream = &g_StdOut;
109 
110   // #if (defined(_MSC_VER) && defined(_M_IX86))
111   if (!CheckIsa())
112   {
113     PrintError("ERROR: processor doesn't support required ISA extension");
114     return NExitCode::kFatalError;
115   }
116   // #endif
117 
118   NT_CHECK
119 
120   NConsoleClose::CCtrlHandlerSetter ctrlHandlerSetter;
121   int res = 0;
122 
123   try
124   {
125     #ifdef _WIN32
126     My_SetDefaultDllDirectories();
127     #endif
128 
129     res = Main2(
130     #ifndef _WIN32
131     numArgs, args
132     #endif
133     );
134   }
135   catch(const CNewException &)
136   {
137     PrintError(kMemoryExceptionMessage);
138     return (NExitCode::kMemoryError);
139   }
140   catch(const NConsoleClose::CCtrlBreakException &)
141   {
142     PrintError(kUserBreakMessage);
143     return (NExitCode::kUserBreak);
144   }
145   catch(const CMessagePathException &e)
146   {
147     PrintError(kException_CmdLine_Error_Message);
148     if (g_ErrStream)
149       *g_ErrStream << e << endl;
150     return (NExitCode::kUserError);
151   }
152   catch(const CSystemException &systemError)
153   {
154     if (systemError.ErrorCode == E_OUTOFMEMORY)
155     {
156       PrintError(kMemoryExceptionMessage);
157       return (NExitCode::kMemoryError);
158     }
159     if (systemError.ErrorCode == E_ABORT)
160     {
161       PrintError(kUserBreakMessage);
162       return (NExitCode::kUserBreak);
163     }
164     if (g_ErrStream)
165     {
166       PrintError("System ERROR:");
167       *g_ErrStream << NError::MyFormatMessage(systemError.ErrorCode) << endl;
168     }
169     return (NExitCode::kFatalError);
170   }
171   catch(NExitCode::EEnum exitCode)
172   {
173     FlushStreams();
174     if (g_ErrStream)
175       *g_ErrStream << kInternalExceptionMessage << exitCode << endl;
176     return (exitCode);
177   }
178   catch(const UString &s)
179   {
180     if (g_ErrStream)
181     {
182       PrintError(kExceptionErrorMessage);
183       *g_ErrStream << s << endl;
184     }
185     return (NExitCode::kFatalError);
186   }
187   catch(const AString &s)
188   {
189     if (g_ErrStream)
190     {
191       PrintError(kExceptionErrorMessage);
192       *g_ErrStream << s << endl;
193     }
194     return (NExitCode::kFatalError);
195   }
196   catch(const char *s)
197   {
198     if (g_ErrStream)
199     {
200       PrintError(kExceptionErrorMessage);
201       *g_ErrStream << s << endl;
202     }
203     return (NExitCode::kFatalError);
204   }
205   catch(const wchar_t *s)
206   {
207     if (g_ErrStream)
208     {
209       PrintError(kExceptionErrorMessage);
210       *g_ErrStream << s << endl;
211     }
212     return (NExitCode::kFatalError);
213   }
214   catch(int t)
215   {
216     if (g_ErrStream)
217     {
218       FlushStreams();
219       *g_ErrStream << kInternalExceptionMessage << t << endl;
220       return (NExitCode::kFatalError);
221     }
222   }
223   catch(...)
224   {
225     PrintError(kUnknownExceptionMessage);
226     return (NExitCode::kFatalError);
227   }
228 
229   return res;
230 }
231