• 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     #if defined(__AVX2__)
65       if (!CPU_IsSupported_AVX2())
66         return false;
67     #elif defined(__AVX__)
68       if (!CPU_IsSupported_AVX())
69         return false;
70     #elif defined(__SSE2__) && !defined(MY_CPU_AMD64) || defined(_M_IX86_FP) && (_M_IX86_FP >= 2)
71       if (!CPU_IsSupported_SSE2())
72         return false;
73     #elif defined(__SSE__) && !defined(MY_CPU_AMD64) || defined(_M_IX86_FP) && (_M_IX86_FP >= 1)
74       if (!CPU_IsSupported_SSE() ||
75           !CPU_IsSupported_CMOV())
76         return false;
77     #endif
78     /*
79     __asm
80     {
81       _emit 0fH
82       _emit 038H
83       _emit 0cbH
84       _emit (0c0H + 0 * 8 + 0)
85     }
86     */
87     return true;
88   }
89   /*
90   __except (EXCEPTION_EXECUTE_HANDLER)
91   {
92     return false;
93   }
94   */
95 }
96 
main(int numArgs,char * args[])97 int Z7_CDECL main
98 (
99   #ifndef _WIN32
100   int numArgs, char *args[]
101   #endif
102 )
103 {
104   g_ErrStream = &g_StdErr;
105   g_StdStream = &g_StdOut;
106 
107   // #if (defined(_MSC_VER) && defined(_M_IX86))
108   if (!CheckIsa())
109   {
110     PrintError("ERROR: processor doesn't support required ISA extension");
111     return NExitCode::kFatalError;
112   }
113   // #endif
114 
115   NT_CHECK
116 
117   NConsoleClose::CCtrlHandlerSetter ctrlHandlerSetter;
118   int res = 0;
119 
120   try
121   {
122     #ifdef _WIN32
123     My_SetDefaultDllDirectories();
124     #endif
125 
126     res = Main2(
127     #ifndef _WIN32
128     numArgs, args
129     #endif
130     );
131   }
132   catch(const CNewException &)
133   {
134     PrintError(kMemoryExceptionMessage);
135     return (NExitCode::kMemoryError);
136   }
137   catch(const NConsoleClose::CCtrlBreakException &)
138   {
139     PrintError(kUserBreakMessage);
140     return (NExitCode::kUserBreak);
141   }
142   catch(const CMessagePathException &e)
143   {
144     PrintError(kException_CmdLine_Error_Message);
145     if (g_ErrStream)
146       *g_ErrStream << e << endl;
147     return (NExitCode::kUserError);
148   }
149   catch(const CSystemException &systemError)
150   {
151     if (systemError.ErrorCode == E_OUTOFMEMORY)
152     {
153       PrintError(kMemoryExceptionMessage);
154       return (NExitCode::kMemoryError);
155     }
156     if (systemError.ErrorCode == E_ABORT)
157     {
158       PrintError(kUserBreakMessage);
159       return (NExitCode::kUserBreak);
160     }
161     if (g_ErrStream)
162     {
163       PrintError("System ERROR:");
164       *g_ErrStream << NError::MyFormatMessage(systemError.ErrorCode) << endl;
165     }
166     return (NExitCode::kFatalError);
167   }
168   catch(NExitCode::EEnum exitCode)
169   {
170     FlushStreams();
171     if (g_ErrStream)
172       *g_ErrStream << kInternalExceptionMessage << exitCode << endl;
173     return (exitCode);
174   }
175   catch(const UString &s)
176   {
177     if (g_ErrStream)
178     {
179       PrintError(kExceptionErrorMessage);
180       *g_ErrStream << s << endl;
181     }
182     return (NExitCode::kFatalError);
183   }
184   catch(const AString &s)
185   {
186     if (g_ErrStream)
187     {
188       PrintError(kExceptionErrorMessage);
189       *g_ErrStream << s << endl;
190     }
191     return (NExitCode::kFatalError);
192   }
193   catch(const char *s)
194   {
195     if (g_ErrStream)
196     {
197       PrintError(kExceptionErrorMessage);
198       *g_ErrStream << s << endl;
199     }
200     return (NExitCode::kFatalError);
201   }
202   catch(const wchar_t *s)
203   {
204     if (g_ErrStream)
205     {
206       PrintError(kExceptionErrorMessage);
207       *g_ErrStream << s << endl;
208     }
209     return (NExitCode::kFatalError);
210   }
211   catch(int t)
212   {
213     if (g_ErrStream)
214     {
215       FlushStreams();
216       *g_ErrStream << kInternalExceptionMessage << t << endl;
217       return (NExitCode::kFatalError);
218     }
219   }
220   catch(...)
221   {
222     PrintError(kUnknownExceptionMessage);
223     return (NExitCode::kFatalError);
224   }
225 
226   return res;
227 }
228