Added Eric Kohl's port of freedos command
[reactos.git] / reactos / apps / utils / 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 #define WIN32_LEAN_AND_MEAN
21
22 #include "config.h"
23
24 #include <windows.h>
25 #include <tchar.h>
26 #include <stdarg.h>
27
28 #include "cmd.h"
29
30
31 #define INVALID_SWITCH _T("Invalid switch - /%c\n")
32 #define TOO_MANY_PARAMETERS _T("Too many parameters - %s\n")
33 #define PATH_NOT_FOUND _T("Path not found\n")
34 #define FILE_NOT_FOUND _T("File not found")
35 #define REQ_PARAM_MISSING _T("Required parameter missing\n")
36 #define INVALID_DRIVE _T("Invalid drive specification\n")
37 #define INVALID_PARAM_FORMAT _T("Invalid parameter format - %s\n")
38 #define BADCOMMAND _T("Bad command or filename\n")
39 #define OUT_OF_MEMORY _T("Out of memory error.\n")
40 #define CANNOTPIPE _T("Error! Cannot pipe! Cannot open temporary file!\n")
41
42 #define D_PAUSEMSG _T("Press any key to continue . . .")
43
44
45
46 VOID ErrorMessage (DWORD dwErrorCode, LPTSTR szFormat, ...)
47 {
48 TCHAR szMessage[1024];
49 LPTSTR szError;
50 va_list arg_ptr;
51
52 if (dwErrorCode == ERROR_SUCCESS)
53 return;
54
55 va_start (arg_ptr, szFormat);
56 wvsprintf (szMessage, szFormat, arg_ptr);
57 va_end (arg_ptr);
58
59 #if 1
60
61 if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
62 NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
63 (LPTSTR)&szError, 0, NULL))
64 {
65 ConErrPrintf (_T("%s %s\n"), szError, szMessage);
66 LocalFree (szError);
67 return;
68 }
69 else
70 {
71 ConErrPrintf (_T("Unknown error! Error code: 0x%lx\n"), dwErrorCode);
72 // ConErrPrintf (_T("No error message available!\n"));
73 return;
74 }
75
76 #else
77
78 switch (dwErrorCode)
79 {
80 case ERROR_FILE_NOT_FOUND:
81 szError = _T("File not found --");
82 break;
83
84 case ERROR_PATH_NOT_FOUND:
85 szError = _T("Path not found --");
86 break;
87
88 default:
89 ConErrPrintf (_T("Unknown error! Error code: 0x%lx\n"), dwErrorCode);
90 return;
91 }
92
93 ConErrPrintf (_T("%s %s\n"), szError, szMessage);
94 #endif
95 }
96
97
98
99 VOID error_invalid_switch (TCHAR ch)
100 {
101 ConErrPrintf (INVALID_SWITCH, ch);
102 }
103
104
105 VOID error_too_many_parameters (LPTSTR s)
106 {
107 ConErrPrintf (TOO_MANY_PARAMETERS, s);
108 }
109
110
111 VOID error_path_not_found (VOID)
112 {
113 ConErrPrintf (PATH_NOT_FOUND);
114 }
115
116
117 VOID error_file_not_found (VOID)
118 {
119 ConErrPrintf (FILE_NOT_FOUND);
120 }
121
122
123 VOID error_sfile_not_found (LPTSTR f)
124 {
125 ConErrPrintf (FILE_NOT_FOUND _T(" - %s\n"), f);
126 }
127
128
129 VOID error_req_param_missing (VOID)
130 {
131 ConErrPrintf (REQ_PARAM_MISSING);
132 }
133
134
135 VOID error_invalid_drive (VOID)
136 {
137 ConErrPrintf (INVALID_DRIVE);
138 }
139
140
141 VOID error_bad_command (VOID)
142 {
143 ConErrPrintf (BADCOMMAND);
144 }
145
146
147 VOID error_no_pipe (VOID)
148 {
149 ConErrPrintf (CANNOTPIPE);
150 }
151
152
153 VOID error_out_of_memory (VOID)
154 {
155 ConErrPrintf (OUT_OF_MEMORY);
156 }
157
158
159 VOID error_invalid_parameter_format (LPTSTR s)
160 {
161 ConErrPrintf (INVALID_PARAM_FORMAT, s);
162 }
163
164
165 VOID error_syntax (LPTSTR s)
166 {
167 if (s)
168 ConErrPrintf (_T("Syntax error - %s\n"), s);
169 else
170 ConErrPrintf (_T("Syntax error.\n"));
171 }
172
173
174 VOID msg_pause (VOID)
175 {
176 ConOutPuts (D_PAUSEMSG);
177 }