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