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