Leaked source code of windows server 2003
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.

210 lines
4.9 KiB

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