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