update for HEAD-2003050101
[reactos.git] / subsys / system / cmd / error.c
1 /*
2  *  ERROR.C - error reporting functions.
3  *
4  *
5  *  History:
6  *
7  *    07/12/98 (Rob Lake)
8  *        started
9  *
10  *    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
11  *        added config.h include
12  *
13  *    24-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
14  *        Redirection safe!
15  *
16  *    02-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
17  *        Use FormatMessage() for error reports.
18  */
19
20 #include "config.h"
21
22 #include <windows.h>
23 #include <tchar.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "cmd.h"
28
29
30 #define INVALID_SWITCH          _T("Invalid switch - /%c\n")
31 #define TOO_MANY_PARAMETERS     _T("Too many parameters - %s\n")
32 #define PATH_NOT_FOUND          _T("Path not found\n")
33 #define FILE_NOT_FOUND          _T("File not found\n")
34 #define REQ_PARAM_MISSING       _T("Required parameter missing\n")
35 #define INVALID_DRIVE           _T("Invalid drive specification\n")
36 #define INVALID_PARAM_FORMAT    _T("Invalid parameter format - %s\n")
37 #define BADCOMMAND              _T("Bad command or filename\n")
38 #define OUT_OF_MEMORY           _T("Out of memory error.\n")
39 #define CANNOTPIPE              _T("Error!  Cannot pipe!  Cannot open temporary file!\n")
40
41 #define D_PAUSEMSG              _T("Press any key to continue . . .")
42
43
44
45 VOID ErrorMessage (DWORD dwErrorCode, LPTSTR szFormat, ...)
46 {
47         TCHAR  szMessage[1024];
48         LPTSTR szError;
49         va_list arg_ptr;
50
51         if (dwErrorCode == ERROR_SUCCESS)
52                 return;
53
54         va_start (arg_ptr, szFormat);
55         _vstprintf (szMessage, szFormat, arg_ptr);
56         va_end (arg_ptr);
57
58 #ifndef __REACTOS__
59
60         if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
61                                            NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
62                                            (LPTSTR)&szError, 0, NULL))
63         {
64                 ConErrPrintf (_T("%s %s\n"), szError, szMessage);
65                 LocalFree (szError);
66                 return;
67         }
68         else
69         {
70                 ConErrPrintf (_T("Unknown error! Error code: 0x%lx\n"), dwErrorCode);
71                 return;
72         }
73
74 #else
75
76         switch (dwErrorCode)
77         {
78                 case ERROR_FILE_NOT_FOUND:
79                         szError = _T("File not found --");
80                         break;
81
82                 case ERROR_PATH_NOT_FOUND:
83                         szError = _T("Path not found --");
84                         break;
85
86                 default:
87                         ConErrPrintf (_T("Unknown error! Error code: 0x%lx\n"), dwErrorCode);
88                         return;
89         }
90
91         ConErrPrintf (_T("%s %s\n"), szError, szMessage);
92 #endif
93 }
94
95
96
97 VOID error_invalid_switch (TCHAR ch)
98 {
99         ConErrPrintf (INVALID_SWITCH, ch);
100 }
101
102
103 VOID error_too_many_parameters (LPTSTR s)
104 {
105         ConErrPrintf (TOO_MANY_PARAMETERS, s);
106 }
107
108
109 VOID error_path_not_found (VOID)
110 {
111         ConErrPrintf (PATH_NOT_FOUND);
112 }
113
114
115 VOID error_file_not_found (VOID)
116 {
117         ConErrPrintf (FILE_NOT_FOUND);
118 }
119
120
121 VOID error_sfile_not_found (LPTSTR f)
122 {
123         ConErrPrintf (FILE_NOT_FOUND _T(" - %s\n"), f);
124 }
125
126
127 VOID error_req_param_missing (VOID)
128 {
129         ConErrPrintf (REQ_PARAM_MISSING);
130 }
131
132
133 VOID error_invalid_drive (VOID)
134 {
135         ConErrPrintf (INVALID_DRIVE);
136 }
137
138
139 VOID error_bad_command (VOID)
140 {
141         ConErrPrintf (BADCOMMAND);
142 }
143
144
145 VOID error_no_pipe (VOID)
146 {
147         ConErrPrintf (CANNOTPIPE);
148 }
149
150
151 VOID error_out_of_memory (VOID)
152 {
153         ConErrPrintf (OUT_OF_MEMORY);
154 }
155
156
157 VOID error_invalid_parameter_format (LPTSTR s)
158 {
159         ConErrPrintf (INVALID_PARAM_FORMAT, s);
160 }
161
162
163 VOID error_syntax (LPTSTR s)
164 {
165         if (s)
166                 ConErrPrintf (_T("Syntax error - %s\n"), s);
167         else
168                 ConErrPrintf (_T("Syntax error.\n"));
169 }
170
171
172 VOID msg_pause (VOID)
173 {
174         ConOutPuts (D_PAUSEMSG);
175 }