1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: error.c 327 2006-07-25 18:24:42Z root $
|
---|
5 |
|
---|
6 | Error reporting
|
---|
7 |
|
---|
8 | Copyright (c) 1993-98 M. Kimes
|
---|
9 | Copyright (c) 2004, 2006 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 |
|
---|
21 | ***********************************************************************/
|
---|
22 |
|
---|
23 | #define INCL_DOS
|
---|
24 | #define INCL_DOSERRORS
|
---|
25 | #define INCL_WIN
|
---|
26 |
|
---|
27 | #include <os2.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <string.h>
|
---|
31 | #include <stdarg.h>
|
---|
32 |
|
---|
33 | #include "fm3dll.h"
|
---|
34 | #include "fm3str.h"
|
---|
35 |
|
---|
36 | #pragma data_seg(DATA1)
|
---|
37 | #pragma alloc_text(FMINPUT,Win_Error,Dos_Error,saymsg,showMsg)
|
---|
38 |
|
---|
39 | static APIRET showMsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszMsg);
|
---|
40 |
|
---|
41 | //== Win_Error: report Win...() error ===
|
---|
42 |
|
---|
43 | VOID Win_Error(HWND hwndErr, HWND hwndOwner, PCSZ pszFileName, ULONG ulLineNo, PCSZ pszFmt,...)
|
---|
44 | {
|
---|
45 | PERRINFO pErrInfoBlk; /* Pointer to ERRINFO structure filled
|
---|
46 | by WinGetErrorInfo */
|
---|
47 | PSZ pszOffset; /* Pointer to current error message returned
|
---|
48 | by WinGetErrorInfo */
|
---|
49 | CHAR szMsg[4096];
|
---|
50 | PSZ psz;
|
---|
51 | HAB hab;
|
---|
52 | va_list va;
|
---|
53 |
|
---|
54 | if (hwndErr == NULLHANDLE)
|
---|
55 | hab = (HAB)0;
|
---|
56 | else
|
---|
57 | hab = WinQueryAnchorBlock(hwndErr);
|
---|
58 |
|
---|
59 | // Format callers message
|
---|
60 | va_start(va, pszFmt);
|
---|
61 | vsprintf(szMsg, pszFmt, va);
|
---|
62 | va_end(va);
|
---|
63 |
|
---|
64 | if (strchr(szMsg, ' ') == NULL)
|
---|
65 | strcat(szMsg, " failed"); // Assume simple function name
|
---|
66 |
|
---|
67 | // Append file name and line number
|
---|
68 | sprintf(szMsg + strlen(szMsg),
|
---|
69 | GetPString(IDS_GENERR1TEXT),
|
---|
70 | pszFileName, ulLineNo, " ");
|
---|
71 |
|
---|
72 | // Get last PM error for the current thread
|
---|
73 | pErrInfoBlk = WinGetErrorInfo(hab);
|
---|
74 | if (!pErrInfoBlk) {
|
---|
75 | psz = szMsg + strlen(szMsg);
|
---|
76 | strcpy(psz, " WinGetError failed");
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | if (!hwndOwner)
|
---|
80 | hwndOwner = HWND_DESKTOP;
|
---|
81 | /* Find message offset in array of message offsets
|
---|
82 | Assume 1 message - fixme?
|
---|
83 | */
|
---|
84 | pszOffset = ((PSZ) pErrInfoBlk) + pErrInfoBlk -> offaoffszMsg;
|
---|
85 | /* Address error message in array of messages and
|
---|
86 | append error message to source code linenumber
|
---|
87 | */
|
---|
88 | psz = szMsg + strlen(szMsg);
|
---|
89 | sprintf(psz, "#0x%04x \"", ERRORIDERROR(pErrInfoBlk -> idError));
|
---|
90 | psz += strlen(psz);
|
---|
91 | strcpy(psz, ((PSZ)pErrInfoBlk) + *(PSHORT)pszOffset);
|
---|
92 | psz += strlen(psz);
|
---|
93 | strcpy(psz, "\"");
|
---|
94 | WinFreeErrorInfo(pErrInfoBlk); // Free resource segment
|
---|
95 | }
|
---|
96 |
|
---|
97 | showMsg(MB_ENTER | MB_ICONEXCLAMATION,
|
---|
98 | hwndOwner,
|
---|
99 | GetPString(IDS_GENERR2TEXT), // Titlebar message
|
---|
100 | szMsg); // Formatted message
|
---|
101 |
|
---|
102 | } // Win_Error
|
---|
103 |
|
---|
104 | //== Dos_Error: report Dos...() error ===
|
---|
105 |
|
---|
106 | INT Dos_Error(ULONG mb_type, ULONG ulRC, HWND hwndOwner, PCSZ pszFileName,
|
---|
107 | ULONG ulLineNo, PCSZ pszFmt,...)
|
---|
108 | {
|
---|
109 | CHAR szMsg[4096];
|
---|
110 | ULONG Class; // Error class
|
---|
111 | ULONG action; // Error action
|
---|
112 | ULONG Locus; // Error location
|
---|
113 | ULONG ulMsgLen;
|
---|
114 | CHAR *pszMsgStart;
|
---|
115 | CHAR *psz;
|
---|
116 | va_list va;
|
---|
117 |
|
---|
118 | if (!ulRC)
|
---|
119 | return MBID_ENTER; // Should not have been called
|
---|
120 |
|
---|
121 | // Format caller's message
|
---|
122 | va_start(va, pszFmt);
|
---|
123 | vsprintf(szMsg, pszFmt, va);
|
---|
124 | va_end(va);
|
---|
125 |
|
---|
126 | if (strchr(szMsg, ' ') == NULL)
|
---|
127 | strcat(szMsg, " failed"); // Assume simple function name
|
---|
128 |
|
---|
129 | DosErrClass(ulRC, &Class, &action, &Locus);
|
---|
130 |
|
---|
131 | sprintf(szMsg + strlen(szMsg),
|
---|
132 | GetPString(IDS_DOSERR1TEXT),
|
---|
133 | pszFileName,
|
---|
134 | ulLineNo,
|
---|
135 | ulRC,
|
---|
136 | GetPString(IDS_ERRCLASS1TEXT + (Class - 1)),
|
---|
137 | GetPString(IDS_ERRACTION1TEXT + (action - 1)),
|
---|
138 | GetPString(IDS_ERRLOCUS1TEXT + (Locus - 1)));
|
---|
139 | pszMsgStart = szMsg + strlen(szMsg) + 1;
|
---|
140 | // Get message leaving space for NL separator
|
---|
141 | if (!DosGetMessage(NULL, 0L, (PCHAR)pszMsgStart + 1, 1024, ulRC, "OSO001.MSG", &ulMsgLen) ||
|
---|
142 | !DosGetMessage(NULL, 0L, (PCHAR)pszMsgStart + 1, 1024, ulRC, "OSO001H.MSG", &ulMsgLen))
|
---|
143 | {
|
---|
144 | // Got message
|
---|
145 | pszMsgStart[ulMsgLen + 1] = 0; // Terminate
|
---|
146 | *(pszMsgStart - 1) = '\n'; // Stuff NL before message text
|
---|
147 | *pszMsgStart = '\"'; // Prefix message text with quote
|
---|
148 |
|
---|
149 | psz = pszMsgStart + ulMsgLen; // Point at last char
|
---|
150 | // Chop trailing NL CR TAB
|
---|
151 | while (*psz &&
|
---|
152 | (*psz == '\r' || *psz == '\n' || *psz == ' ' || *psz == '\t'))
|
---|
153 | {
|
---|
154 | *psz-- = 0;
|
---|
155 | }
|
---|
156 | strcat(psz, "\""); // Append trailing quote
|
---|
157 |
|
---|
158 | // Convert CR and NL combos to single space
|
---|
159 | psz = pszMsgStart;
|
---|
160 | while (*psz)
|
---|
161 | {
|
---|
162 | if (*psz == '\n' || *psz == '\r')
|
---|
163 | {
|
---|
164 | while (*(psz + 1) == '\n' || *(psz + 1) == '\r')
|
---|
165 | memmove(psz, psz + 1, strlen(psz));
|
---|
166 | *psz = ' ';
|
---|
167 | }
|
---|
168 | else
|
---|
169 | psz++;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return showMsg(mb_type | MB_ICONEXCLAMATION,
|
---|
174 | hwndOwner,
|
---|
175 | GetPString(IDS_DOSERR2TEXT), // Title
|
---|
176 | szMsg);
|
---|
177 |
|
---|
178 | } // Dos_Error
|
---|
179 |
|
---|
180 | //== Runtime_Error: report runtime library error ===
|
---|
181 |
|
---|
182 | VOID Runtime_Error(PCSZ pszSrcFile, UINT uSrcLineNo, PCSZ pszFmt,...)
|
---|
183 | {
|
---|
184 | CHAR szMsg[4096];
|
---|
185 | va_list va;
|
---|
186 | PSZ psz;
|
---|
187 |
|
---|
188 | // Format caller's message
|
---|
189 | va_start(va, pszFmt);
|
---|
190 | vsprintf(szMsg, pszFmt, va);
|
---|
191 | va_end(va);
|
---|
192 |
|
---|
193 | if (strchr(szMsg, ' ') == NULL)
|
---|
194 | strcat(szMsg, " failed"); // Assume simple function name
|
---|
195 |
|
---|
196 | sprintf(szMsg + strlen(szMsg),
|
---|
197 | // GetPString(IDS_DOSERR1TEXT), fixme
|
---|
198 | "\nModule: %s\nLinenumber: %u",
|
---|
199 | pszSrcFile,
|
---|
200 | uSrcLineNo);
|
---|
201 |
|
---|
202 | showMsg(MB_ICONEXCLAMATION,HWND_DESKTOP,DEBUG_STRING,szMsg);
|
---|
203 |
|
---|
204 | } // Runtime_Error
|
---|
205 |
|
---|
206 | // fixme to be rename to Misc_Error
|
---|
207 |
|
---|
208 | //=== saymsg: report misc error ===
|
---|
209 |
|
---|
210 | APIRET saymsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszFmt,...)
|
---|
211 | {
|
---|
212 | CHAR szMsg[4096];
|
---|
213 | va_list va;
|
---|
214 |
|
---|
215 | va_start(va, pszFmt);
|
---|
216 | vsprintf(szMsg, pszFmt, va);
|
---|
217 | va_end(va);
|
---|
218 |
|
---|
219 | return showMsg(mb_type,
|
---|
220 | hwnd,
|
---|
221 | pszTitle,
|
---|
222 | szMsg);
|
---|
223 | } // saymsg
|
---|
224 |
|
---|
225 | //=== showMsg: report misc error ===
|
---|
226 |
|
---|
227 | static APIRET showMsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszMsg)
|
---|
228 | {
|
---|
229 | if ((mb_type & (MB_YESNO | MB_YESNOCANCEL)) == 0)
|
---|
230 | {
|
---|
231 | fputs(pszMsg, stderr);
|
---|
232 | fputc('\n', stderr);
|
---|
233 | fflush(stderr);
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (!hwnd)
|
---|
237 | hwnd = HWND_DESKTOP;
|
---|
238 |
|
---|
239 | DosBeep(250,100);
|
---|
240 |
|
---|
241 | return WinMessageBox(HWND_DESKTOP, // Parent
|
---|
242 | hwnd, // Owner
|
---|
243 | (PSZ)pszMsg,
|
---|
244 | (PSZ)pszTitle,
|
---|
245 | 0, // help id
|
---|
246 | mb_type | MB_MOVEABLE);
|
---|
247 | } // showMsg
|
---|