home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / DBWIN / DBWINDLL.C_ / DBWINDLL.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  8.8 KB  |  410 lines

  1. #include "dbwindlp.h"
  2.  
  3. UINT        flagsOutput = 0;
  4. UINT        modeOutput = OMD_NONE;  // Output mode
  5. BOOL        fDebugSystem = FALSE;
  6.  
  7. // Output buffer
  8.  
  9. char        achBuffer[CCHBUFFERMAX];
  10. UINT        cchBuffer = 0;
  11. int         cLinesBuffer = 0;
  12. HWND        hwndBufferNotify = NULL;
  13.  
  14. // Task filter
  15.  
  16. HTASK htaskFilter = NULL;
  17. char szTaskFilter[MAX_MODULE_NAME];
  18.  
  19.  
  20. BOOL CALLBACK LibMain(HINSTANCE hinst, UINT wDS, UINT cbHeap, DWORD unused)
  21. {
  22.     // Nothing to do.
  23.  
  24.     return TRUE;
  25. }
  26.  
  27. BOOL CALLBACK _export _loadds _WEP(BOOL fSystemExit)
  28. {
  29.     // Nothing to do.
  30.  
  31.     return TRUE;
  32. }
  33.  
  34. BOOL WINAPI _export _loadds OutputInit(void)
  35. {
  36.     fDebugSystem = GetSystemMetrics(SM_DEBUG);
  37.  
  38.     flagsOutput = 0;
  39.  
  40.     if (modeOutput == OMD_MONO || MonoQuery())
  41.         flagsOutput |= DBOF_HASMONO;
  42.  
  43.     if (modeOutput == OMD_COM1 || ComQuery(1))
  44.         flagsOutput |= DBOF_HASCOM1;
  45.  
  46.     if (modeOutput == OMD_COM2 || ComQuery(2))
  47.         flagsOutput |= DBOF_HASCOM2;
  48.  
  49.     return TRUE;
  50. }
  51.  
  52. BOOL WINAPI _export _loadds SetOutputMode(UINT mode)
  53. {
  54.     if (mode == OMD_MONO && !(flagsOutput & DBOF_HASMONO))
  55.         mode = OMD_NONE;
  56.  
  57.     if (mode == OMD_COM1 && !(flagsOutput & DBOF_HASCOM1))
  58.         mode = OMD_NONE;
  59.  
  60.     if (mode == OMD_COM2 && !(flagsOutput & DBOF_HASCOM2))
  61.         mode = OMD_NONE;
  62.  
  63.     if (mode != modeOutput)
  64.     {
  65.         if (modeOutput != OMD_NONE)
  66.             NotifyUnRegister(NULL);
  67.  
  68.         switch (modeOutput)
  69.         {
  70.         case OMD_NONE:
  71.         case OMD_BUFFER:
  72.             break;
  73.  
  74.         case OMD_COM1:
  75.         case OMD_COM2:
  76.             ComClose();
  77.             break;
  78.  
  79.         case OMD_MONO:
  80.             MonoClose();
  81.             break;
  82.         }
  83.  
  84.         modeOutput = mode;
  85.  
  86.         switch (mode)
  87.         {
  88.         case OMD_NONE:
  89.         case OMD_BUFFER:
  90.             break;
  91.  
  92.         case OMD_COM1:
  93.             if (!ComOpen(1))
  94.                 modeOutput = OMD_NONE;
  95.             break;
  96.  
  97.         case OMD_COM2:
  98.             if (!ComOpen(2))
  99.                 modeOutput = OMD_NONE;
  100.             break;
  101.  
  102.         case OMD_MONO:
  103.             if (!MonoOpen())
  104.                 modeOutput = OMD_NONE;
  105.             break;
  106.  
  107.         default:
  108.             modeOutput = OMD_BUFFER;
  109.             break;
  110.         }
  111.  
  112.         if (modeOutput != OMD_NONE)
  113.             NotifyRegister(NULL, NotifyCallback, NF_NORMAL | NF_RIP);
  114.     }
  115.  
  116.     // Return FALSE if the mode wasn't changed the way the caller asked.
  117.     //
  118.     return modeOutput == mode;
  119. }
  120.  
  121. UINT WINAPI _export _loadds GetOutputMode(void)
  122. {
  123.     return modeOutput;
  124. }
  125.  
  126. UINT WINAPI _export _loadds GetOutputFlags(void)
  127. {
  128.     return flagsOutput;
  129. }
  130.  
  131. void WINAPI _export _loadds SetBufferNotify(HWND hwndNotify)
  132. {
  133.     hwndBufferNotify = hwndNotify;
  134. }
  135.  
  136. BOOL WINAPI _export _loadds GetOutputBufferInfo(OUTBUFINFO FAR* lpobi)
  137. {
  138.     if (modeOutput != OMD_BUFFER)
  139.         return FALSE;
  140.  
  141.     lpobi->lpch = achBuffer;
  142.     lpobi->cch = cchBuffer;
  143.     lpobi->cLines = cLinesBuffer;
  144.  
  145.     return TRUE;
  146. }
  147.  
  148. void WINAPI _export _loadds ResetBuffer()
  149. {
  150.     cLinesBuffer = 0;
  151.     cchBuffer = 0;
  152. }
  153.  
  154. BOOL WINAPI _export _loadds SetTaskFilter(LPCSTR lpszTaskName)
  155. {
  156.     if (lstrlen(lpszTaskName) <= MAX_MODULE_NAME - 1)
  157.     {
  158.         lstrcpy(szTaskFilter, lpszTaskName);
  159.         return TRUE;
  160.     }
  161.     return FALSE;
  162. }
  163.  
  164. void WINAPI _export _loadds GetTaskFilter(LPSTR lpszTaskName, int cch)
  165. {
  166.     lstrcpyn(lpszTaskName, szTaskFilter, cch);
  167. }
  168.  
  169. BOOL CALLBACK _export _loadds NotifyCallback(WORD id, DWORD dwData)
  170. {
  171.     BOOL fHandled;
  172.     static char szNotify[128];
  173.     static int cReenter = 0;
  174.  
  175.     // If we're not outputting anything,
  176.     // just return FALSE to chain to next handler.
  177.     //
  178.     if (modeOutput == OMD_NONE)
  179.         return FALSE;
  180.  
  181. #ifdef DEBUG
  182.     // For debugging, allow a few levels of recursion so we can
  183.     // see any debug output messages generated by DBWin itself.
  184.     //
  185.     if (cReenter != 0)
  186.         _asm { int 3 }
  187. #else
  188.     // Don't allow any reentrancy at all.
  189.     //
  190.     if (cReenter != 0)
  191.         return FALSE;
  192. #endif
  193.  
  194.     cReenter++;
  195.     fHandled = FALSE;
  196.  
  197.     switch (id)
  198.     {
  199.     case NFY_STARTTASK:
  200.         TaskFilterStartTask();
  201.         break;
  202.  
  203.     case NFY_EXITTASK:
  204.         TaskFilterExitTask();
  205.         break;
  206.  
  207.     case NFY_INCHAR:
  208.         switch (modeOutput)
  209.         {
  210.         case OMD_COM1:
  211.         case OMD_COM2:
  212.             fHandled = (BOOL)ComIn();
  213.             break;
  214.  
  215.         default:
  216.             fHandled = (BOOL)'i';
  217.         }
  218.         break;
  219.  
  220.     case NFY_OUTSTR:
  221.         if (!CheckTaskFilter())
  222.             break;
  223.  
  224.         switch (modeOutput)
  225.         {
  226.         case OMD_BUFFER:
  227.             fHandled = BufferOut((LPCSTR)dwData);
  228.             break;
  229.  
  230.         case OMD_MONO:
  231.             fHandled = MonoOut((LPCSTR)dwData);
  232.             break;
  233.  
  234.         case OMD_COM1:
  235.         case OMD_COM2:
  236.             fHandled = ComOut((LPCSTR)dwData);
  237.             break;
  238.         }
  239.         break;
  240.  
  241.     case NFY_RIP:
  242.         if (!fDebugSystem)
  243.         {
  244.             if (!CheckTaskFilter())
  245.                 break;
  246.             FormatRip(szNotify, (NFYRIP FAR*)dwData);
  247.             cReenter--;
  248.             OutputDebugString(szNotify);
  249.             cReenter++;
  250.         }
  251.         break;
  252.  
  253.     case NFY_LOGERROR:
  254.         if (!fDebugSystem)
  255.         {
  256.             if (!CheckTaskFilter())
  257.                 break;
  258.             FormatLogError(szNotify, (NFYLOGERROR FAR*)dwData);
  259.             cReenter--;
  260.             OutputDebugString(szNotify);
  261.             cReenter++;
  262.         }
  263.         break;
  264.  
  265.     case NFY_LOGPARAMERROR:
  266.         if (!fDebugSystem)
  267.         {
  268.             if (!CheckTaskFilter())
  269.                 break;
  270.             FormatLogParamError(szNotify, (NFYLOGPARAMERROR FAR*)dwData);
  271.             cReenter--;
  272.             OutputDebugString(szNotify);
  273.             cReenter++;
  274.         }
  275.         break;
  276.     }
  277.  
  278.     cReenter--;
  279.     return fHandled;
  280. }
  281.  
  282. BOOL BufferOut(LPCSTR lpszOut)
  283. {
  284.     UINT cch;
  285.     int cLines;
  286.  
  287.     cch = DBStrCpy(NULL, lpszOut, &cLines);
  288.  
  289.     if (cch > (sizeof(achBuffer) - cchBuffer))
  290.     {
  291.         MessageBeep(0);
  292.         return FALSE;
  293.     }
  294.  
  295.     DBStrCpy(&achBuffer[cchBuffer], lpszOut, NULL);
  296.  
  297.     cchBuffer += cch;
  298.     cLinesBuffer += cLines;
  299.  
  300.     // If buffer was originally empty, wake up the main loop.
  301.     //
  302.     if (cchBuffer == cch)
  303.         PostMessage(hwndBufferNotify, WM_BUFFERNOTEMPTY, 0, 0L);
  304.  
  305.     return TRUE;
  306. }
  307.  
  308. // This function performs a string copy (or string length if pszDst is NULL)
  309. // It also ensures the destination string is properly formatted for the
  310. // edit controls: line terminators must be CR followed by LF, in that orderc.
  311. // If pcLines != NULL, returns number of lines in the string in *pcLines
  312. //
  313. UINT DBStrCpy(char* pszDst, LPCSTR pszSrc, int far* pcLines)
  314. {
  315.     BYTE ch;
  316.     UINT cch = 0;
  317.     int cLines = 0;
  318.  
  319.     while (ch = *pszSrc++)
  320.     {
  321.         // If we find a CR or LF, then store a CR/LF in
  322.         // the output string.
  323.         //
  324.         if (ch == 0x0d || ch == 0x0a)
  325.         {
  326.             cch += 2;
  327.             cLines++;
  328.             if (pszDst)
  329.             {
  330.                 *pszDst++ = 0x0d;
  331.                 *pszDst++ = 0x0a;
  332.             }
  333.  
  334.             // Skip any other CRs or LFs we find.
  335.             //
  336.             while ((ch = *pszSrc) && (ch == 0x0d || ch == 0x0a))
  337.                 ++pszSrc;
  338.         }
  339.         else
  340.         {
  341.             cch++;
  342.             if (pszDst)
  343.                 *pszDst++ = ch;
  344.         }
  345.     }
  346.     if (pszDst)
  347.         *pszDst = 0;
  348.  
  349.     if (pcLines)
  350.         *pcLines = cLines;
  351.  
  352.     return cch;
  353. }
  354.  
  355. BOOL CheckTaskFilter()
  356. {
  357.     return (szTaskFilter[0] == 0 || htaskFilter == GetCurrentTask());
  358. }
  359.  
  360. void TaskFilterStartTask(void)
  361. {
  362.     HTASK htask;
  363.     TASKENTRY te;
  364.  
  365.     if (htaskFilter || szTaskFilter[0] == 0)
  366.         return;
  367.  
  368.     htask = GetCurrentTask();
  369.  
  370.     te.dwSize = sizeof(te);
  371.     if (TaskFindHandle(&te, htask))
  372.     {
  373.         int i;
  374.  
  375.         // Case-insensitive comparision of task name strings
  376.         //
  377.         for (i = 0; i < sizeof(szTaskFilter); i++)
  378.         {
  379.             char ch1;
  380.             char ch2;
  381.  
  382.             ch1 = szTaskFilter[i];
  383.             if (ch1 >= 'a' && ch1 <= 'z')
  384.                 ch1 -= 'a' - 'A';
  385.             ch2 = te.szModule[i];
  386.             if (ch2 >= 'a' && ch2 <= 'z')
  387.                 ch1 -= 'a' - 'A';
  388.  
  389.             if (ch1 != ch2)
  390.                 break;
  391.  
  392.             // If we got to the end of the string,
  393.             // we have a match.  Set up the filter task
  394.             // and continue.
  395.             //
  396.             if (ch1 == 0)
  397.             {
  398.                 htaskFilter = htask;
  399.                 break;
  400.             }
  401.         }
  402.     }
  403. }
  404.  
  405. void TaskFilterExitTask(void)
  406. {
  407.     if (GetCurrentTask() == htaskFilter)
  408.         htaskFilter = NULL;
  409. }
  410.