| 1 | /* $Id: kHlpBareAssert.c 82 2016-08-22 21:01:51Z bird $ */ | 
|---|
| 2 | /** @file | 
|---|
| 3 | * kHlpBare - Assert Backend. | 
|---|
| 4 | */ | 
|---|
| 5 |  | 
|---|
| 6 | /* | 
|---|
| 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net> | 
|---|
| 8 | * | 
|---|
| 9 | * Permission is hereby granted, free of charge, to any person | 
|---|
| 10 | * obtaining a copy of this software and associated documentation | 
|---|
| 11 | * files (the "Software"), to deal in the Software without | 
|---|
| 12 | * restriction, including without limitation the rights to use, | 
|---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|---|
| 14 | * copies of the Software, and to permit persons to whom the | 
|---|
| 15 | * Software is furnished to do so, subject to the following | 
|---|
| 16 | * conditions: | 
|---|
| 17 | * | 
|---|
| 18 | * The above copyright notice and this permission notice shall be | 
|---|
| 19 | * included in all copies or substantial portions of the Software. | 
|---|
| 20 | * | 
|---|
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
|---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | 
|---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
|---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | 
|---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | 
|---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 
|---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 
|---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE. | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | /******************************************************************************* | 
|---|
| 32 | *   Header Files                                                               * | 
|---|
| 33 | *******************************************************************************/ | 
|---|
| 34 | #include <k/kHlpAssert.h> | 
|---|
| 35 | #include <k/kHlpString.h> | 
|---|
| 36 |  | 
|---|
| 37 | #if K_OS == K_OS_DARWIN \ | 
|---|
| 38 | || K_OS == K_OS_FREEBSD \ | 
|---|
| 39 | || K_OS == K_OS_LINUX \ | 
|---|
| 40 | || K_OS == K_OS_NETBSD \ | 
|---|
| 41 | || K_OS == K_OS_OPENBSD \ | 
|---|
| 42 | || K_OS == K_OS_SOLARIS | 
|---|
| 43 | # include <k/kHlpSys.h> | 
|---|
| 44 |  | 
|---|
| 45 | #elif K_OS == K_OS_OS2 | 
|---|
| 46 | # define INCL_BASE | 
|---|
| 47 | # define INCL_ERRORS | 
|---|
| 48 | # include <os2.h> | 
|---|
| 49 |  | 
|---|
| 50 | #elif  K_OS == K_OS_WINDOWS | 
|---|
| 51 | # include <Windows.h> | 
|---|
| 52 |  | 
|---|
| 53 | #else | 
|---|
| 54 | # error "port me" | 
|---|
| 55 | #endif | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 | * Writes a assert string with unix lineendings. | 
|---|
| 60 | * | 
|---|
| 61 | * @param   pszMsg  The string. | 
|---|
| 62 | */ | 
|---|
| 63 | static void kHlpAssertWrite(const char *pszMsg) | 
|---|
| 64 | { | 
|---|
| 65 | #if K_OS == K_OS_DARWIN \ | 
|---|
| 66 | || K_OS == K_OS_FREEBSD \ | 
|---|
| 67 | || K_OS == K_OS_LINUX \ | 
|---|
| 68 | || K_OS == K_OS_NETBSD \ | 
|---|
| 69 | || K_OS == K_OS_OPENBSD \ | 
|---|
| 70 | || K_OS == K_OS_SOLARIS | 
|---|
| 71 | KSIZE cchMsg = kHlpStrLen(pszMsg); | 
|---|
| 72 | kHlpSys_write(2 /* stderr */, pszMsg, cchMsg); | 
|---|
| 73 |  | 
|---|
| 74 | #elif K_OS == K_OS_OS2 ||  K_OS == K_OS_WINDOWS | 
|---|
| 75 | /* | 
|---|
| 76 | * Line by line. | 
|---|
| 77 | */ | 
|---|
| 78 | ULONG       cbWritten; | 
|---|
| 79 | const char *pszNl = kHlpStrChr(pszMsg, '\n'); | 
|---|
| 80 | while (pszNl) | 
|---|
| 81 | { | 
|---|
| 82 | cbWritten = pszNl - pszMsg; | 
|---|
| 83 |  | 
|---|
| 84 | # if K_OS == K_OS_OS2 | 
|---|
| 85 | if (cbWritten) | 
|---|
| 86 | DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten); | 
|---|
| 87 | DosWrite((HFILE)2, "\r\n", 2, &cbWritten); | 
|---|
| 88 | # else /* K_OS == K_OS_WINDOWS */ | 
|---|
| 89 | if (cbWritten) | 
|---|
| 90 | WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL); | 
|---|
| 91 | WriteFile((HANDLE)STD_ERROR_HANDLE, "\r\n", 2, &cbWritten, NULL); | 
|---|
| 92 | # endif | 
|---|
| 93 |  | 
|---|
| 94 | /* next */ | 
|---|
| 95 | pszMsg = pszNl + 1; | 
|---|
| 96 | pszNl = kHlpStrChr(pszMsg, '\n'); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | /* | 
|---|
| 100 | * Remaining incomplete line. | 
|---|
| 101 | */ | 
|---|
| 102 | if (*pszMsg) | 
|---|
| 103 | { | 
|---|
| 104 | cbWritten = kHlpStrLen(pszMsg); | 
|---|
| 105 | # if K_OS == K_OS_OS2 | 
|---|
| 106 | DosWrite((HFILE)2, pszMsg, cbWritten, &cbWritten); | 
|---|
| 107 | # else /* K_OS == K_OS_WINDOWS */ | 
|---|
| 108 | WriteFile((HANDLE)STD_ERROR_HANDLE, pszMsg, cbWritten, &cbWritten, NULL); | 
|---|
| 109 | # endif | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | #else | 
|---|
| 113 | # error "port me" | 
|---|
| 114 | #endif | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | KHLP_DECL(void) kHlpAssertMsg1(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction) | 
|---|
| 119 | { | 
|---|
| 120 | char szLine[16]; | 
|---|
| 121 |  | 
|---|
| 122 | kHlpAssertWrite("\n!!!kLdr Assertion Failed!!!\nExpression: "); | 
|---|
| 123 | kHlpAssertWrite(pszExpr); | 
|---|
| 124 | kHlpAssertWrite("\nAt: "); | 
|---|
| 125 | kHlpAssertWrite(pszFile); | 
|---|
| 126 | kHlpAssertWrite("("); | 
|---|
| 127 | kHlpAssertWrite(kHlpInt2Ascii(szLine, sizeof(szLine), iLine, 10)); | 
|---|
| 128 | kHlpAssertWrite(") "); | 
|---|
| 129 | kHlpAssertWrite(pszFunction); | 
|---|
| 130 | kHlpAssertWrite("\n"); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | KHLP_DECL(void) kHlpAssertMsg2(const char *pszFormat, ...) | 
|---|
| 135 | { | 
|---|
| 136 | kHlpAssertWrite(pszFormat); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|