home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ibmtool.zip / msg.c < prev    next >
Text File  |  1997-11-07  |  9KB  |  292 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*      FILE: MSG.C                                                           */
  4. /*                                                                            */
  5. /*   PURPOSE: This file contains the Message window code.                     */
  6. /*                                                                            */
  7. /* FUNCTIONS: InitMsg                                                         */
  8. /*            PrintMsg                                                        */
  9. /*            hexprint                                                        */
  10. /*            DoMessageWindow                                                 */
  11. /*                                                                            */
  12. /* GLOBAL DATA ACCESSED                                                       */
  13. /*      BOOLEAN  HelpMode                                                     */
  14. /*      BOOLEAN  ScriptMode                                                   */
  15. /*      BOOLEAN  SLog                                                         */
  16. /*      FILE    *ScriptFile                                                   */
  17. /*              *LogFile                                                      */
  18. /*      WINDOW   Msg                                                          */
  19. /*                                                                            */
  20. /******************************************************************************/
  21.  
  22. #define INCL_SUB
  23. #define INCL_BASE
  24. #define INCL_DOS
  25. #include <os2.h>
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <malloc.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include "ndis.h"
  33. #include "ibmtool.h"
  34. #include "ioctl.h"
  35. #include "intrface.h"
  36.  
  37. MESSAGE  Message[MAXMSGS];
  38. MESSAGE *MsgTop, *CurrMsg, *MsgHead, *ViewBot;
  39. int      MsgCnt = 0;
  40.  
  41. void InitMsg ()
  42. {
  43.   int i;
  44.  
  45.   // initialize the message queue
  46.   ViewBot = MsgHead = CurrMsg = MsgTop = &Message[0];
  47.  
  48.   for (i=0; i<MAXMSGS - 1; ++i)
  49.      Message[i].next = &Message[i+1];
  50.   Message[i].next = &Message[0];
  51.   for (i=MAXMSGS - 1; i>0; --i)
  52.      Message[i].prev = &Message[i-1];
  53.   Message[i].prev = &Message[MAXMSGS - 1];
  54. }
  55.  
  56. void PrintMsg (BYTE mAttr, char *fmt, ...)
  57. {
  58.   BYTE     bAttr[2] = " ";
  59.   char     str[80];                     /* Buffer to build sting into   */
  60.   int      i;
  61.   va_list  argptr;                      /* Argument list pointer        */
  62.   MESSAGE *pmsg;
  63.  
  64.   va_start (argptr, fmt);               /* Initialize va_ functions     */
  65.   vsprintf (str, fmt, argptr);          /* prints string to buffer      */
  66.   va_end (argptr);                      /* Close va_ functions          */
  67.  
  68.   if (SLog) fprintf (LogFile, "%s\n", str);
  69.   if (ScriptMode)
  70.     {
  71.      printf ("%s\n", str);
  72.      return;
  73.     }
  74.  
  75.   strcpy (CurrMsg->Msg, str);
  76.   CurrMsg->MsgAttr = (mAttr == 0) ? Msg.Attr : mAttr;
  77.  
  78.   bAttr[1] = Msg.Attr;
  79.   if (MsgCnt > Msg.Lines - 1)
  80.     {
  81.      if (ViewBot->next != CurrMsg)
  82.        {
  83.         ViewBot = CurrMsg->prev;
  84.         MsgTop = ViewBot;
  85.         for (i=0; i<Msg.Lines - 1; ++i)
  86.            MsgTop = MsgTop->prev;
  87.         if (!HelpMode)
  88.           {
  89.            ClearWindow (Msg);
  90.            pmsg = MsgTop;
  91.            for (i=0; i<Msg.Lines; ++i)
  92.              {
  93.               VioWrtCharStrAtt (pmsg->Msg, strlen (pmsg->Msg),
  94.                                 Msg.Top+i, Msg.Left, &pmsg->MsgAttr, 0);
  95.               pmsg = pmsg->next;
  96.              }
  97.           } /* endif */
  98.        }
  99.      MsgTop = MsgTop->next;
  100.      ViewBot = ViewBot->next;
  101.      if (MsgTop == MsgHead) MsgHead = MsgHead->next;
  102.      if (!HelpMode)
  103.        {
  104.         VioScrollUp (Msg.Top, Msg.Left, Msg.Bottom, -1, 1, bAttr, 0);
  105.         VioWrtCharStrAtt (CurrMsg->Msg, strlen (CurrMsg->Msg),
  106.                           Msg.Bottom, Msg.Left, &CurrMsg->MsgAttr, 0);
  107.        } /* endif */
  108.     }
  109.   else
  110.     {
  111.      ViewBot = CurrMsg;
  112.      if (!HelpMode)
  113.         VioWrtCharStrAtt (CurrMsg->Msg, strlen (CurrMsg->Msg),
  114.                           Msg.Top + MsgCnt, Msg.Left, &CurrMsg->MsgAttr, 0);
  115.     }
  116.  
  117.   CurrMsg = CurrMsg->next;
  118.   if (++MsgCnt == MAXMSGS) MsgCnt = MAXMSGS;
  119.  
  120. }
  121.  
  122. void hexprint (char far *buffer, int length, BOOLEAN pause)
  123. {
  124.   char        hexstr[80] = " xxxx  ";
  125.   char        ascstr[30] = "   ";
  126.   char        tstr[5];
  127.   char       *p = hexstr + 7;
  128.   int         i, j, linecnt = 0, hexcnt = 0;
  129.   KBDKEYINFO  KbdInfo;
  130.  
  131.   for (i=1; i<length+1; ++i)
  132.     {
  133.      sprintf (p, "%02X ", (unsigned char) buffer[i-1]);
  134.      if (buffer[i-1] >= 0x20 && buffer[i-1] <= 0x7E)
  135.        {
  136.         sprintf (tstr, "%c", buffer[i-1]);
  137.         strcat (ascstr, tstr);
  138.        }
  139.      else
  140.         strcat (ascstr, "·");
  141.      if (i && !(i % 16))
  142.        {
  143.         strcat (hexstr, ascstr);
  144.         sprintf (tstr, "%04X ", hexcnt);
  145.         for (j=0; j<4; ++j)
  146.            hexstr[j+1] = tstr[j];
  147.         hexcnt += 16;
  148.         PrintMsg (0, hexstr);
  149.         if (++linecnt > Msg.Lines - 2)
  150.           {
  151.            linecnt = 0;
  152.            if (pause && i < length)
  153.              {
  154.               PrintMsg (0, "---More---");
  155.               KbdCharIn (&KbdInfo, IO_WAIT, 0);
  156.              } /* endif */
  157.           }
  158.         strcpy (hexstr, " xxxx  ");
  159.         strcpy (ascstr, "   ");
  160.         p = hexstr + 7;
  161.        }
  162.      else
  163.         p += 3;
  164.     } /* endfor */
  165.  
  166.   if ((i - 1) % 16)
  167.     {
  168.      sprintf (tstr, "%04X ", hexcnt);
  169.      for (j=0; j<4; ++j)
  170.         hexstr[j+1] = tstr[j];
  171.      memset (p, ' ', (16 - ((i - 1) % 16)) * 3);
  172.      *(p + (16 - ((i - 1) % 16)) * 3) = '\0';
  173.      strcat (hexstr, ascstr);
  174.      PrintMsg (0, hexstr);
  175.     }
  176.  
  177. }
  178.  
  179. void DoMessageWindow ()
  180. {
  181.   BYTE           bAttr[2] = " ";
  182.   int            i, j, lines;
  183.   USHORT         row, col;
  184.   BOOLEAN        done = FALSE;
  185.   MESSAGE       *pmsg;
  186.   KBDKEYINFO     kbdInfo;
  187.   VIOCURSORINFO  CursorInfo;
  188.  
  189.   bAttr[1] = Msg.Attr;
  190.   VioSetCurPos (Msg.Top, Msg.Left, 0);
  191.  
  192.   while (!done)
  193.     {
  194.      KbdCharIn (&kbdInfo, IO_WAIT, 0);
  195.      VioGetCurPos (&row, &col, 0);
  196.      switch (kbdInfo.chScan)
  197.        {
  198.         case TAB:
  199.           done = TRUE;
  200.           break;
  201.  
  202.         case UP:
  203.           if (row > Msg.Top)
  204.             {
  205.              VioSetCurPos (row - 1, col, 0);
  206.              break;
  207.             }
  208.           if (MsgTop != MsgHead)
  209.             {
  210.              MsgTop = MsgTop->prev;
  211.              ViewBot = ViewBot->prev;
  212.              VioScrollDn (Msg.Top, Msg.Left, Msg.Bottom, -1, 1, bAttr, 0);
  213.              VioWrtCharStrAtt (MsgTop->Msg, strlen (MsgTop->Msg),
  214.                                Msg.Top, 0, &MsgTop->MsgAttr, 0);
  215.             }
  216.           break;
  217.  
  218.         case DOWN:
  219.           if (row < MAXROW)
  220.             {
  221.              VioSetCurPos (row + 1, col, 0);
  222.              break;
  223.             }
  224.           if (MsgCnt > Msg.Lines - 1 && ViewBot->next != CurrMsg)
  225.             {
  226.              MsgTop = MsgTop->next;
  227.              ViewBot = ViewBot->next;
  228.              VioScrollUp (Msg.Top, Msg.Left, Msg.Bottom, -1, 1, bAttr, 0);
  229.              VioWrtCharStrAtt (ViewBot->Msg, strlen (ViewBot->Msg),
  230.                                Msg.Bottom, 0, &ViewBot->MsgAttr, 0);
  231.             }
  232.           break;
  233.  
  234.         case PGUP:
  235.           if (MsgTop == MsgHead)
  236.             {
  237.              VioSetCurPos (Msg.Top, Msg.Left, 0);
  238.              break;
  239.             }
  240.  
  241.           i = 0;
  242.           while (MsgTop != MsgHead && i < Msg.Lines)
  243.             {
  244.              MsgTop = MsgTop->prev;
  245.              ViewBot = ViewBot->prev;
  246.              ++i;
  247.             } /* endwhile */
  248.  
  249.           ClearWindow (Msg);
  250.  
  251.           pmsg = MsgTop;
  252.           for (i=0; i<Msg.Lines; ++i)
  253.             {
  254.              VioWrtCharStrAtt (pmsg->Msg, strlen (pmsg->Msg),
  255.                                Msg.Top+i, Msg.Left, &pmsg->MsgAttr, 0);
  256.              pmsg = pmsg->next;
  257.             }
  258.           break;
  259.  
  260.         case PGDN:
  261.           if (MsgCnt < Msg.Lines) break;
  262.  
  263.           if (ViewBot->next == CurrMsg)
  264.             {
  265.              VioSetCurPos (Msg.Bottom, Msg.Left, 0);
  266.              break;
  267.             }
  268.  
  269.           i = 0;
  270.           while (ViewBot->next != CurrMsg && i < Msg.Lines)
  271.             {
  272.              ViewBot = ViewBot->next;
  273.              MsgTop = MsgTop->next;
  274.              ++i;
  275.             } /* endwhile */
  276.  
  277.           ClearWindow (Msg);
  278.  
  279.           pmsg = MsgTop;
  280.           for (i=0; i<Msg.Lines; ++i)
  281.             {
  282.              VioWrtCharStrAtt (pmsg->Msg, strlen (pmsg->Msg),
  283.                                Msg.Top+i, Msg.Left, &pmsg->MsgAttr, 0);
  284.              pmsg = pmsg->next;
  285.             }
  286.           break;
  287.        } /* endswitch */
  288.     } /* endwhile */
  289.  
  290. }
  291.  
  292.