source: trunk/dll/error.c@ 551

Last change on this file since 551 was 551, checked in by Gregg Young, 18 years ago

Indentation cleanup

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