1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: error.c 613 2007-04-19 00:06:38Z stevenhl $
|
---|
5 |
|
---|
6 | Error reporting
|
---|
7 |
|
---|
8 | Copyright (c) 1993-98 M. Kimes
|
---|
9 | Copyright (c) 2004, 2007 Steven H. Levine
|
---|
10 |
|
---|
11 | 12 Aug 04 SHL Comments
|
---|
12 | 23 May 05 SHL Move saymsg here
|
---|
13 | 24 May 05 SHL Rename General_Error to more accurate Win_Error
|
---|
14 | 24 May 05 SHL Rename saymsg to more accurate Misc_Error
|
---|
15 | 24 May 05 SHL Rework Win_Error args and clean up logic
|
---|
16 | 27 May 05 SHL Rework to use common showMsg
|
---|
17 | 14 Aug 05 SHL showMsg: suppress write to stdout if not error message
|
---|
18 | 13 Jul 06 SHL Add Runtime_Error
|
---|
19 | 22 Jul 06 SHL Optimize calling sequences
|
---|
20 | 26 Jul 06 SHL Add ..._Error2
|
---|
21 | 16 Aug 06 SHL Tweak message formatting
|
---|
22 | 07 Jan 07 GKY Move error strings etc. to string file
|
---|
23 | 18 Apr 07 SHL showMsg: correct selective logging checks
|
---|
24 |
|
---|
25 | ***********************************************************************/
|
---|
26 |
|
---|
27 | #define INCL_DOS
|
---|
28 | #define INCL_DOSERRORS
|
---|
29 | #define INCL_WIN
|
---|
30 |
|
---|
31 | #include <os2.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <stdio.h>
|
---|
34 | #include <string.h>
|
---|
35 | #include <stdarg.h>
|
---|
36 |
|
---|
37 | #include "fm3dll.h"
|
---|
38 | #include "fm3str.h"
|
---|
39 |
|
---|
40 | #pragma data_seg(DATA1)
|
---|
41 | #pragma alloc_text(FMINPUT,Win_Error,Dos_Error,saymsg,showMsg)
|
---|
42 |
|
---|
43 | static APIRET showMsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszMsg, BOOL wantLog);
|
---|
44 |
|
---|
45 | //== Win_Error: report Win...() error using passed message string ===
|
---|
46 |
|
---|
47 | VOID Win_Error(HWND hwndErr, HWND hwndOwner, PCSZ pszFileName, ULONG ulLineNo,
|
---|
48 | PCSZ pszFmt, ...)
|
---|
49 | {
|
---|
50 | PERRINFO pErrInfoBlk; /* Pointer to ERRINFO structure filled
|
---|
51 | by WinGetErrorInfo */
|
---|
52 | PSZ pszOffset; /* Pointer to current error message returned
|
---|
53 | by WinGetErrorInfo */
|
---|
54 | CHAR szMsg[4096];
|
---|
55 | PSZ psz;
|
---|
56 | HAB hab;
|
---|
57 | va_list va;
|
---|
58 |
|
---|
59 | if (hwndErr == NULLHANDLE)
|
---|
60 | hab = (HAB) 0;
|
---|
61 | else
|
---|
62 | hab = WinQueryAnchorBlock(hwndErr);
|
---|
63 |
|
---|
64 | // Format callers message
|
---|
65 | va_start(va, pszFmt);
|
---|
66 | szMsg[sizeof(szMsg) - 1] = 0;
|
---|
67 | vsprintf(szMsg, pszFmt, va);
|
---|
68 | va_end(va);
|
---|
69 |
|
---|
70 | if (szMsg[sizeof(szMsg) - 1]) {
|
---|
71 | fprintf(stderr, "Buffer overflow in Win_Error - need %u bytes\n", strlen(szMsg) + 1);
|
---|
72 | fflush(stderr);
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (strchr(szMsg, ' ') == NULL)
|
---|
76 | strcat(szMsg, " failed."); // Assume simple function name
|
---|
77 |
|
---|
78 | // Append file name and line number
|
---|
79 | sprintf(szMsg + strlen(szMsg),
|
---|
80 | GetPString(IDS_GENERR1TEXT), pszFileName, ulLineNo, " ");
|
---|
81 |
|
---|
82 | // Get last PM error for the current thread
|
---|
83 | pErrInfoBlk = WinGetErrorInfo(hab);
|
---|
84 | if (!pErrInfoBlk) {
|
---|
85 | psz = szMsg + strlen(szMsg);
|
---|
86 | strcpy(psz, " WinGetErrorInfo failed.");
|
---|
87 | }
|
---|
88 | else {
|
---|
89 | if (!hwndOwner)
|
---|
90 | hwndOwner = HWND_DESKTOP;
|
---|
91 | /* Find message offset in array of message offsets
|
---|
92 | Assume 1 message - fixme?
|
---|
93 | */
|
---|
94 | pszOffset = ((PSZ) pErrInfoBlk) + pErrInfoBlk->offaoffszMsg;
|
---|
95 | /* Address error message in array of messages and
|
---|
96 | append error message to source code linenumber
|
---|
97 | */
|
---|
98 | psz = szMsg + strlen(szMsg);
|
---|
99 | sprintf(psz, "#0x%04x \"", ERRORIDERROR(pErrInfoBlk->idError));
|
---|
100 | psz += strlen(psz);
|
---|
101 | strcpy(psz, ((PSZ) pErrInfoBlk) + *(PSHORT) pszOffset);
|
---|
102 | psz += strlen(psz);
|
---|
103 | strcpy(psz, "\"");
|
---|
104 | WinFreeErrorInfo(pErrInfoBlk); // Free resource segment
|
---|
105 | }
|
---|
106 |
|
---|
107 | showMsg(MB_ENTER | MB_ICONEXCLAMATION, hwndOwner, GetPString(IDS_GENERR2TEXT),
|
---|
108 | szMsg, TRUE);
|
---|
109 |
|
---|
110 | } // Win_Error
|
---|
111 |
|
---|
112 | //== Win_Error2: report Win...() error using passed message id ===
|
---|
113 |
|
---|
114 | VOID Win_Error2(HWND hwndErr, HWND hwndOwner, PCSZ pszFileName,
|
---|
115 | ULONG ulLineNo, UINT idMsg)
|
---|
116 | {
|
---|
117 | Win_Error(hwndErr, hwndOwner, pszFileName, ulLineNo, GetPString(idMsg));
|
---|
118 |
|
---|
119 | } // Win_Error2
|
---|
120 |
|
---|
121 | //== Dos_Error: report Dos...() error using passed message string ===
|
---|
122 |
|
---|
123 | INT Dos_Error(ULONG mb_type, ULONG ulRC, HWND hwndOwner, PCSZ pszFileName,
|
---|
124 | ULONG ulLineNo, PCSZ pszFmt, ...)
|
---|
125 | {
|
---|
126 | CHAR szMsg[4096];
|
---|
127 | ULONG Class; // Error class
|
---|
128 | ULONG action; // Error action
|
---|
129 | ULONG Locus; // Error location
|
---|
130 | ULONG ulMsgLen;
|
---|
131 | CHAR *pszMsgStart;
|
---|
132 | CHAR *psz;
|
---|
133 | va_list va;
|
---|
134 |
|
---|
135 | if (!ulRC)
|
---|
136 | return MBID_ENTER; // Should not have been called
|
---|
137 |
|
---|
138 | // Format caller's message
|
---|
139 | va_start(va, pszFmt);
|
---|
140 | szMsg[sizeof(szMsg) - 1] = 0;
|
---|
141 | vsprintf(szMsg, pszFmt, va);
|
---|
142 | va_end(va);
|
---|
143 |
|
---|
144 | if (szMsg[sizeof(szMsg) - 1]) {
|
---|
145 | fprintf(stderr, "Buffer overflow in Dos_Error - need %u bytes\n", strlen(szMsg) + 1);
|
---|
146 | fflush(stderr);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (strchr(szMsg, ' ') == NULL)
|
---|
150 | strcat(szMsg, " failed."); // Assume simple function name
|
---|
151 |
|
---|
152 | DosErrClass(ulRC, &Class, &action, &Locus);
|
---|
153 |
|
---|
154 | sprintf(szMsg + strlen(szMsg),
|
---|
155 | GetPString(IDS_DOSERR1TEXT),
|
---|
156 | pszFileName,
|
---|
157 | ulLineNo,
|
---|
158 | ulRC,
|
---|
159 | GetPString(IDS_ERRCLASS1TEXT + (Class - 1)),
|
---|
160 | GetPString(IDS_ERRACTION1TEXT + (action - 1)),
|
---|
161 | GetPString(IDS_ERRLOCUS1TEXT + (Locus - 1)));
|
---|
162 | pszMsgStart = szMsg + strlen(szMsg) + 1;
|
---|
163 | // Get message leaving space for NL separator
|
---|
164 | if (!DosGetMessage
|
---|
165 | (NULL, 0L, (PCHAR) pszMsgStart + 1, 1024, ulRC, "OSO001.MSG", &ulMsgLen)
|
---|
166 | || !DosGetMessage(NULL, 0L, (PCHAR) pszMsgStart + 1, 1024, ulRC,
|
---|
167 | "OSO001H.MSG", &ulMsgLen)) {
|
---|
168 | // Got message
|
---|
169 | pszMsgStart[ulMsgLen + 1] = 0; // Terminate
|
---|
170 | *(pszMsgStart - 1) = '\n'; // Stuff NL before message text
|
---|
171 | *pszMsgStart = '\"'; // Prefix message text with quote
|
---|
172 |
|
---|
173 | psz = pszMsgStart + ulMsgLen; // Point at last char
|
---|
174 | // Chop trailing NL CR TAB
|
---|
175 | while (*psz &&
|
---|
176 | (*psz == '\r' || *psz == '\n' || *psz == ' ' || *psz == '\t')) {
|
---|
177 | *psz-- = 0;
|
---|
178 | }
|
---|
179 | strcat(psz, "\""); // Append trailing quote
|
---|
180 |
|
---|
181 | // Convert CR and NL combos to single space
|
---|
182 | psz = pszMsgStart;
|
---|
183 | while (*psz) {
|
---|
184 | if (*psz == '\n' || *psz == '\r') {
|
---|
185 | while (*(psz + 1) == '\n' || *(psz + 1) == '\r')
|
---|
186 | memmove(psz, psz + 1, strlen(psz));
|
---|
187 | *psz = ' ';
|
---|
188 | }
|
---|
189 | else
|
---|
190 | psz++;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | return showMsg(mb_type | MB_ICONEXCLAMATION, hwndOwner, GetPString(IDS_DOSERR2TEXT),
|
---|
195 | szMsg, TRUE);
|
---|
196 |
|
---|
197 | } // Dos_Error
|
---|
198 |
|
---|
199 | //== Dos_Error2: report Dos...() error using passed message id ===
|
---|
200 |
|
---|
201 | INT Dos_Error2(ULONG mb_type, ULONG ulRC, HWND hwndOwner, PCSZ pszFileName,
|
---|
202 | ULONG ulLineNo, UINT idMsg)
|
---|
203 | {
|
---|
204 | return Dos_Error(mb_type, ulRC, hwndOwner, pszFileName, ulLineNo,
|
---|
205 | GetPString(idMsg));
|
---|
206 | } // Dos_Error2
|
---|
207 |
|
---|
208 | //== Runtime_Error: report runtime library error using passed message string ===
|
---|
209 |
|
---|
210 | VOID Runtime_Error(PCSZ pszSrcFile, UINT uSrcLineNo, PCSZ pszFmt, ...)
|
---|
211 | {
|
---|
212 | CHAR szMsg[4096];
|
---|
213 | va_list va;
|
---|
214 | PSZ psz;
|
---|
215 |
|
---|
216 | // Format caller's message
|
---|
217 | va_start(va, pszFmt);
|
---|
218 | szMsg[sizeof(szMsg) - 1] = 0;
|
---|
219 | vsprintf(szMsg, pszFmt, va);
|
---|
220 | va_end(va);
|
---|
221 |
|
---|
222 | if (szMsg[sizeof(szMsg) - 1]) {
|
---|
223 | fprintf(stderr, "Buffer overflow in Runtime_Error - need %u bytes\n", strlen(szMsg) + 1);
|
---|
224 | fflush(stderr);
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (strchr(szMsg, ' ') == NULL)
|
---|
228 | strcat(szMsg, " failed."); // Assume simple function name
|
---|
229 |
|
---|
230 | sprintf(szMsg + strlen(szMsg),
|
---|
231 | GetPString(IDS_GENERR1TEXT), pszSrcFile, uSrcLineNo);
|
---|
232 |
|
---|
233 | showMsg(MB_ICONEXCLAMATION, HWND_DESKTOP, DEBUG_STRING, szMsg, TRUE);
|
---|
234 |
|
---|
235 | } // Runtime_Error
|
---|
236 |
|
---|
237 | //== Runtime_Error2: report runtime library error using passed message id ===
|
---|
238 |
|
---|
239 | VOID Runtime_Error2(PCSZ pszSrcFile, UINT uSrcLineNo, UINT idMsg)
|
---|
240 | {
|
---|
241 | Runtime_Error(pszSrcFile, uSrcLineNo, GetPString(idMsg));
|
---|
242 |
|
---|
243 | } // Runtime_Error2
|
---|
244 |
|
---|
245 | // fixme to be rename to Misc_Error
|
---|
246 |
|
---|
247 | //=== saymsg: report misc error using passed message ===
|
---|
248 |
|
---|
249 | APIRET saymsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszFmt, ...)
|
---|
250 | {
|
---|
251 | CHAR szMsg[4096];
|
---|
252 | va_list va;
|
---|
253 |
|
---|
254 | va_start(va, pszFmt);
|
---|
255 | szMsg[sizeof(szMsg) - 1] = 0;
|
---|
256 | vsprintf(szMsg, pszFmt, va);
|
---|
257 | va_end(va);
|
---|
258 |
|
---|
259 | if (szMsg[sizeof(szMsg) - 1]) {
|
---|
260 | fprintf(stderr, "Buffer overflow in saymsg - need %u bytes\n", strlen(szMsg) + 1);
|
---|
261 | fflush(stderr);
|
---|
262 | }
|
---|
263 |
|
---|
264 | return showMsg(mb_type, hwnd, pszTitle, szMsg, FALSE);
|
---|
265 |
|
---|
266 | } // saymsg
|
---|
267 |
|
---|
268 | //=== showMsg: display error popup ===
|
---|
269 |
|
---|
270 | static APIRET showMsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszMsg, BOOL wantLog)
|
---|
271 | {
|
---|
272 | if (wantLog) {
|
---|
273 | fputs(pszMsg, stderr);
|
---|
274 | fputc('\n', stderr);
|
---|
275 | fflush(stderr);
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (!hwnd)
|
---|
279 | hwnd = HWND_DESKTOP;
|
---|
280 |
|
---|
281 | DosBeep(250, 100);
|
---|
282 |
|
---|
283 | return WinMessageBox(HWND_DESKTOP, // Parent
|
---|
284 | hwnd, // Owner
|
---|
285 | (PSZ) pszMsg, (PSZ) pszTitle, 0, // help id
|
---|
286 | mb_type | MB_MOVEABLE);
|
---|
287 | } // showMsg
|
---|