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