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.

140 lines
3.5 KiB

  1. /*****************************************************************************
  2. *
  3. * assert.c
  4. *
  5. *****************************************************************************/
  6. #include "m4.h"
  7. #include <tchar.h>
  8. /*****************************************************************************
  9. *
  10. * PrintPtszCtchPtszVa
  11. *
  12. * Perform printf-style formatting, but much more restricted.
  13. *
  14. * %s - null-terminated string
  15. * %P - snapped TOKEN structure
  16. * %d - decimal number
  17. *
  18. *****************************************************************************/
  19. UINT STDCALL
  20. PrintPtszCtchPtszVa(PTSTR ptszBuf, CTCH ctchBuf, PCTSTR ptszFormat, va_list ap)
  21. {
  22. PTSTR ptsz = ptszBuf;
  23. PTSTR ptszMac = ptszBuf + ctchBuf - 1;
  24. PCTSTR ptszSrc;
  25. TCHAR tszBuf[15]; /* worst-case 32-bit integer */
  26. PTOK ptok;
  27. CTCH ctch;
  28. while (ptsz < ptszMac) {
  29. if (*ptszFormat != '%') {
  30. *ptsz++ = *ptszFormat;
  31. if (*ptszFormat == TEXT('\0'))
  32. return (UINT)(ptsz - ptszBuf - 1);
  33. ptszFormat++;
  34. continue;
  35. }
  36. /*
  37. * Found a formatting character.
  38. */
  39. ptszFormat++;
  40. switch (*ptszFormat) {
  41. /*
  42. * %s - null-terminated string, as much as will fit
  43. */
  44. case 's':
  45. ptszSrc = va_arg(ap, PCTSTR);
  46. while (*ptszSrc && ptsz < ptszMac)
  47. {
  48. *ptsz++ = *ptszSrc++;
  49. }
  50. break;
  51. /*
  52. * %d - decimal integer
  53. */
  54. case 'd':
  55. PrintPtchPtchV(tszBuf, TEXT("%d"), va_arg(ap, int));
  56. ptszSrc = tszBuf;
  57. while (*ptszSrc && ptsz < ptszMac)
  58. {
  59. *ptsz++ = *ptszSrc++;
  60. }
  61. break;
  62. /*
  63. * %P - snapped token
  64. */
  65. case 'P':
  66. ptok = va_arg(ap, PTOK);
  67. AssertSPtok(ptok);
  68. Assert(fClosedPtok(ptok));
  69. ctch = ptok->ctch;
  70. ptszSrc = ptok->u.ptch;
  71. while (ctch && *ptszSrc && ptsz < ptszMac)
  72. {
  73. *ptsz++ = *ptszSrc++;
  74. ctch--;
  75. }
  76. break;
  77. case '%':
  78. *ptsz++ = TEXT('%');
  79. break;
  80. default:
  81. Assert(0); break;
  82. }
  83. ptszFormat++;
  84. }
  85. *ptsz++ = TEXT('\0');
  86. return (UINT)(ptsz - ptszBuf - 1);
  87. }
  88. /*****************************************************************************
  89. *
  90. * Die
  91. *
  92. * Squirt a message and die.
  93. *
  94. *****************************************************************************/
  95. NORETURN void CDECL
  96. Die(PCTSTR ptszFormat, ...)
  97. {
  98. TCHAR tszBuf[1024];
  99. va_list ap;
  100. CTCH ctch;
  101. va_start(ap, ptszFormat);
  102. ctch = PrintPtszCtchPtszVa(tszBuf, cA(tszBuf), ptszFormat, ap);
  103. va_end(ap);
  104. cbWriteHfPvCb(hfErr, tszBuf, cbCtch(ctch));
  105. exit(1);
  106. }
  107. #ifdef DEBUG
  108. /*****************************************************************************
  109. *
  110. * AssertPszPszLn
  111. *
  112. * An assertion just failed. pszExpr is the expression, pszFile is the
  113. * filename, and iLine is the line number.
  114. *
  115. *****************************************************************************/
  116. NORETURN int STDCALL
  117. AssertPszPszLn(PCSTR pszExpr, PCSTR pszFile, int iLine)
  118. {
  119. Die(TEXT("Assertion failed: `%s' at %s(%d)") EOL, pszExpr, pszFile, iLine);
  120. return 0;
  121. }
  122. #endif