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