Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.4 KiB

4 years ago
  1. /* common.c
  2. *
  3. * Common functions useful for Windows programs.
  4. */
  5. //#include <windows.h>
  6. #include <stdarg.h>
  7. #include "graphic.h"
  8. #include "profile.h"
  9. #ifdef DEBUG // On NT, ntavi.h might do an undef debug...
  10. #include "common.h"
  11. /* globals */
  12. int giDebugLevel = -1; // current debug level (0 = disabled)
  13. int gfhDebugFile = -1; // file handle for debug output (or -1)
  14. int giTimingLevel = -1;
  15. /* InitializeDebugOutput(szAppName)
  16. *
  17. * Read the current debug level of this application (named <szAppName>)
  18. * from the [debug] section of win.ini, as well as the current location
  19. * for debug output.
  20. */
  21. void FAR PASCAL
  22. InitializeDebugOutput(LPSTR szAppName)
  23. {
  24. char achLocation[300]; // debug output location
  25. /* debugging is disabled by default (and if an error occurs below) */
  26. giDebugLevel = -1;
  27. gfhDebugFile = -1;
  28. /* get the debug output location */
  29. if ( (mmGetProfileStringA("debug", "Location", "", achLocation,
  30. sizeof(achLocation)) == sizeof(achLocation)) ||
  31. (achLocation[0] == 0) )
  32. return;
  33. if (achLocation[0] == '>')
  34. {
  35. /* <achLocation> is the name of a file to overwrite (if
  36. * a single '>' is given) or append to (if '>>' is given)
  37. */
  38. if (achLocation[1] == '>')
  39. gfhDebugFile = _lopen(achLocation + 2, OF_WRITE);
  40. else
  41. gfhDebugFile = _lcreat(achLocation + 1, 0);
  42. if (gfhDebugFile < 0)
  43. return;
  44. if (achLocation[1] == '>')
  45. _llseek(gfhDebugFile, 0, SEEK_END);
  46. }
  47. else
  48. if (lstrcmpiA(achLocation, "aux") == 0)
  49. {
  50. /* use OutputDebugString() for debug output */
  51. }
  52. else
  53. if ((lstrcmpiA(achLocation, "com1") == 0)
  54. || (lstrcmpiA(achLocation, "com2") == 0))
  55. {
  56. gfhDebugFile = _lopen(achLocation, OF_WRITE);
  57. }
  58. else
  59. {
  60. /* invalid "location=" -- keep debugging disabled */
  61. return;
  62. }
  63. /* get the debug level */
  64. giDebugLevel = mmGetProfileIntA("debug", szAppName, 0);
  65. giTimingLevel = mmGetProfileIntA("debug", "Timing", 0);
  66. }
  67. /* TerminateDebugOutput()
  68. *
  69. * Terminate debug output for this application.
  70. */
  71. void FAR PASCAL
  72. TerminateDebugOutput(void)
  73. {
  74. if (gfhDebugFile >= 0)
  75. _lclose(gfhDebugFile);
  76. gfhDebugFile = -1;
  77. giDebugLevel = -1;
  78. }
  79. /* _Assert(szExpr, szFile, iLine)
  80. *
  81. * If <fExpr> is TRUE, then do nothing. If <fExpr> is FALSE, then display
  82. * an "assertion failed" message box allowing the user to abort the program,
  83. * enter the debugger (the "Retry" button), or igore the error.
  84. *
  85. * <szFile> is the name of the source file; <iLine> is the line number
  86. * containing the _Assert() call.
  87. */
  88. #ifndef _WIN32
  89. #pragma optimize("", off)
  90. #define ASSERTPREFIX
  91. #else
  92. #define ASSERTPREFIX "(NT) "
  93. #endif
  94. void FAR PASCAL
  95. _Assert(char *szExp, char *szFile, int iLine)
  96. {
  97. static char ach[300]; // debug output (avoid stack overflow)
  98. int id;
  99. int iExitCode;
  100. void FAR PASCAL DebugBreak(void);
  101. /* display error message */
  102. if (szExp)
  103. wsprintfA(ach, ASSERTPREFIX "(%s)\nFile %s, line %d", (LPSTR)szExp, (LPSTR)szFile, iLine);
  104. else
  105. wsprintfA(ach, ASSERTPREFIX "File %s, line %d", (LPSTR)szFile, iLine);
  106. MessageBeep(MB_ICONHAND);
  107. id = MessageBoxA(NULL, ach, "Assertion Failed",
  108. #ifdef BIDI
  109. MB_RTL_READING |
  110. #endif
  111. MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE);
  112. /* abort, debug, or ignore */
  113. switch (id)
  114. {
  115. case IDABORT:
  116. /* kill this application */
  117. iExitCode = 0;
  118. #ifndef _WIN32
  119. _asm
  120. {
  121. mov ah, 4Ch
  122. mov al, BYTE PTR iExitCode
  123. int 21h
  124. }
  125. #else
  126. FatalAppExit(0, TEXT("Good Bye"));
  127. #endif // WIN16
  128. break;
  129. case IDRETRY:
  130. /* break into the debugger */
  131. DebugBreak();
  132. break;
  133. case IDIGNORE:
  134. /* ignore the assertion failure */
  135. break;
  136. }
  137. }
  138. #ifndef _WIN32
  139. #pragma optimize("", on)
  140. #endif
  141. /* _DebugPrintf(szFormat, ...)
  142. *
  143. * If the application's debug level is at or above <iDebugLevel>,
  144. * then output debug string <szFormat> with formatting codes
  145. * replaced with arguments in the argument list pointed to by <szArg1>.
  146. */
  147. #define MODNAME "MCIAVI"
  148. void FAR CDECL
  149. _DebugPrintf(LPSTR szFormat, ...)
  150. {
  151. static char ach[300]; // debug output (avoid stack overflow)
  152. int cch; // length of debug output string
  153. #ifndef _WIN32
  154. NPSTR pchSrc, pchDst;
  155. if (*szFormat == '!') {
  156. ++szFormat;
  157. }
  158. wvsprintf(ach, szFormat, (LPVOID)(&szFormat+1));
  159. #else
  160. UINT n;
  161. va_list va;
  162. va_start(va, szFormat);
  163. if (*szFormat == '!') {
  164. ++szFormat;
  165. n=0;
  166. } else if (*szFormat=='.') {
  167. n=0;
  168. } else {
  169. n = wsprintfA(ach, MODNAME ": (tid %x) ", GetCurrentThreadId());
  170. }
  171. wvsprintfA(ach+n, szFormat, va);
  172. va_end(va);
  173. #endif
  174. #ifndef _WIN32
  175. /* expand the newlines into carrige-return-line-feed pairs;
  176. * first, figure out how long the new (expanded) string will be
  177. */
  178. for (pchSrc = pchDst = ach; *pchSrc != 0; pchSrc++, pchDst++)
  179. if (*pchSrc == '\n')
  180. pchDst++;
  181. /* is <ach> large enough? */
  182. cch = pchDst - ach;
  183. Assert(cch < sizeof(ach));
  184. *pchDst-- = 0;
  185. /* working backwards, expand \n's to \r\n's */
  186. while (pchSrc-- > ach)
  187. if ((*pchDst-- = *pchSrc) == '\n')
  188. *pchDst-- = '\r';
  189. #else
  190. cch = strlen(ach);
  191. #endif //no expansion on Win32
  192. /* output the debug string */
  193. if (gfhDebugFile > 0)
  194. _lwrite(gfhDebugFile, ach, cch);
  195. else {
  196. OutputDebugStringA(ach);
  197. }
  198. }
  199. #endif