home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / SOFTWARE / LIBS / PCL4C43.ZIP / MODEM_IO.C (.txt) < prev    next >
Text File  |  1995-03-27  |  7KB  |  323 lines

  1. /*
  2. **   -- MODEM_IO.C --
  3. **
  4. **  Define USE_WIN_IO to 1 if using the WIN_IO window tile
  5. **  routines, else set USE_WIN_IO to 0.
  6. */
  7.  
  8. #define USE_WIN_IO 0
  9.  
  10. /**********************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "pcl4c.h"
  17. #include "modem_io.h"
  18.  
  19. #if USE_WIN_IO
  20. #include "win_io.h"
  21. #endif
  22.  
  23. #define FALSE 0
  24. #define TRUE !FALSE
  25. #define ONE_SECOND 18
  26. #define CR 0x0d
  27.  
  28. /*** globals ***/
  29.  
  30. static int  Debug = 0;
  31. static int  LastPort;         /* last port referenced */
  32. static char MatchString[80];  /* ModemWaitFor() match string */
  33. static int  MatchLength= 0;   /* string length */
  34. static int  MatchCount = 0;   /* # sub-strings */
  35. static struct
  36.   {char *Start;               /* ptr to 1st char of string */
  37.    char *Ptr;                 /* working ptr */
  38.   } MatchList[10];
  39.  
  40. /*** PRIVATE functions ***/
  41.  
  42. static int BreakTest(void)
  43. {/* User BREAK ? */
  44.  if(SioBrkKey()||SioKeyPress())
  45.     {
  46. #if USE_WIN_IO
  47.      WinPutString(SCR_WIN,"User BREAK\n");
  48. #else
  49.      printf("User BREAK\n");
  50. #endif
  51.      return(TRUE);
  52.     }
  53.  return(FALSE);
  54. }
  55.  
  56. void MatchInit(char *S)
  57. {int  i;
  58.  char C;
  59.  char *Ptr;
  60.  MatchCount = 0;
  61.  strncpy(MatchString,S,80);
  62.  MatchLength = strlen(MatchString);
  63.  Ptr = MatchString;
  64.  MatchList[MatchCount].Start = Ptr;
  65.  MatchList[MatchCount++].Ptr = Ptr;
  66.  while(*Ptr)
  67.    {if(*Ptr=='|')
  68.       {/* mark start of next string */
  69.        MatchList[MatchCount].Start = Ptr + 1;
  70.        MatchList[MatchCount++].Ptr = Ptr + 1;
  71.       }
  72.     Ptr++;
  73.    }
  74. }
  75.  
  76. void MatchUpper(void)
  77. {int i;
  78.  char *Ptr;
  79.  Ptr = MatchString;
  80.  for(i=0;i<MatchLength;i++)
  81.    {*Ptr = toupper(*Ptr);
  82.     Ptr++;
  83.    }
  84. }
  85.  
  86. int MatchChar(char C)
  87. {int  i;
  88.  char *Ptr;
  89.  char *Start;
  90.  /* consider each string in turn */
  91.  for(i=0;i<MatchCount;i++)
  92.    {Ptr = MatchList[i].Ptr;
  93.     Start = MatchList[i].Start;
  94.     if(*Ptr==C)
  95.       {/* char C matches */
  96.        Ptr++;
  97.        if((*Ptr=='|')||(*Ptr=='\0'))
  98.          {MatchList[i].Ptr = Start;
  99.           return i;
  100.          }
  101.        else MatchList[i].Ptr = Ptr;
  102.       }
  103.     else
  104.       {/* char C does NOT match */
  105.        MatchList[i].Ptr = Start;
  106.        /* look again if was not 1st char  */
  107.        if(Ptr!=Start) i--;
  108.       }
  109.    }
  110.  return -1;
  111. }
  112.  
  113. /*** PUBLIC functions ***/
  114.  
  115. /* echos incoming to screen */
  116.  
  117. void ModemEcho(int Port,int Echo)
  118. {int rc;
  119.  long Time;
  120.  Time = SioTimer();
  121.  while(SioTimer() < Time+(long)Echo)
  122.    {
  123. #if USE_WIN_IO
  124.     rc = CharGet(Port,1);
  125.     if(rc>=0) WinPutChar(SCR_WIN,(char)rc);
  126. #else
  127.     rc = SioGetc(Port,1);
  128.     if(rc>=0) printf("%c",(char)rc);
  129. #endif
  130.    }
  131. }
  132.  
  133. /* send string to modem & get echo */
  134.  
  135. int ModemSendTo(
  136.   int  Port,       /* port to talk to */
  137.   int  Pace,       /* inter-char delay */
  138.   char *String)    /* string to send to modem */
  139. {int i, rc;
  140.  char c;
  141.  int Code;
  142.  long Time;
  143.  char Temp[80];
  144.  if(Debug)
  145.    {sprintf(Temp," [Sending '%s'] ",String);
  146. #if USE_WIN_IO
  147.     WinPutString(SCR_WIN,Temp);
  148. #else
  149.     printf("%s\n",Temp);
  150. #endif
  151.    }
  152.  for(i=0;i<strlen(String);)
  153.     {/* User BREAK ? */
  154.      if(BreakTest()) return(FALSE);
  155.      /* delay <Pace> tics */
  156.      if(Pace>0) SioDelay(Pace);
  157.      /* fetch character */
  158.      c = String[i++];
  159.      switch(c)
  160.         {case '^':
  161.             /* next char is control char */
  162.             c = String[i++] - '@';
  163.             break;
  164.          case '!':
  165.             /* replace ! with carriage return */
  166.             c = CR;
  167.             break;
  168.          case '~':
  169.             /* delay 1/2 second */
  170.             SioDelay(ONE_SECOND/2);
  171.             c = ' ';
  172.             break;
  173.          case ' ':
  174.             /* delay 1/4 second */
  175.             SioDelay(ONE_SECOND/4);
  176.             c = ' ';
  177.             break;
  178.         } /* end switch */
  179.      /* transmit as 7 bit ASCII character */
  180. #if USE_WIN_IO
  181.      CharPut(Port,(char)(0x7f & c));
  182. #else
  183.      SioPutc(Port,(char)(0x7f & c));
  184. #endif
  185.     }
  186.  return(TRUE);
  187. } /* end SendTo */
  188.  
  189. /* wait for 'String' */
  190.  
  191. /* NOTES:
  192. **  (1)  Will return NULL if no match, else '0' if 1st, '2' if 2nd, etc.
  193. **       where String = "<1st substr>|<2nd substr>| ..."
  194. **  (2)  Example call: ModemWaitFor(COM1,180,FALSE,"more ?|Menu:");
  195. */
  196.  
  197. char ModemWaitFor(
  198.   int  Port,       /* Port to talk to */
  199.   int  Tics,       /* wait in tics for string */
  200.   int  CaseFlag,   /* TRUE = case sensitive compares */
  201.   char *String)    /* string to wait for */
  202. {int i, k;
  203.  int rc;
  204.  char C;
  205.  int Code;
  206.  long Time;
  207.  char Temp[80];
  208.  /* wait for string */
  209.  Time = SioTimer();
  210.  MatchInit(String);
  211.  if(!CaseFlag) MatchUpper();
  212.  if(Debug)
  213.    {sprintf(Temp," [Awaiting '%s'] ",String);
  214. #if USE_WIN_IO
  215.     WinPutString(SCR_WIN,Temp);
  216. #else
  217.     printf("%s\n",Temp);
  218. #endif
  219.    }
  220.  while(SioTimer()<(Time+(long)Tics))
  221.    {/* User BREAK ? */
  222.     if(BreakTest()) return(FALSE);
  223.     /* wait for next character */
  224. #if USE_WIN_IO
  225.     Code = CharGet(Port,1);
  226. #else
  227.     Code = SioGetc(Port,1);
  228. #endif
  229.     if(Code<-1) return(FALSE);
  230.     if(Code>=0)
  231.       {/* echo char */
  232. #if USE_WIN_IO
  233.        WinPutChar(SCR_WIN,(char)Code);
  234. #else
  235.        printf("%c",(char)Code);
  236. #endif
  237.        /* case sensitive ? */
  238.        if(CaseFlag) C = (char)Code;
  239.        else C = toupper( (char)Code );
  240.        /* does char match ? */
  241.        rc = MatchChar(C);
  242.        if(rc>=0) return ('0' + rc);
  243.       }
  244.    } /* end for(i) */
  245.  return 0;
  246. }
  247.  
  248. /* enter command state */
  249.  
  250. /* NOTE: assumes escape char = '+' & guard time = 1 sec */
  251.  
  252. void ModemCmdState(int Port)
  253. {int  i;
  254.  char Temp[80];
  255.  /* delay a bit over 1 second */
  256.  SioDelay(ONE_SECOND+ONE_SECOND/4);
  257.  /* send Escape Code exactly 3 times */
  258.  for(i=0;i<3;i++)
  259.     {
  260. #if USE_WIN_IO
  261.      CharPut(Port,'+');
  262. #else
  263.      SioPutc(Port,'+');
  264. #endif
  265.      SioDelay(ONE_SECOND/4);
  266.     }
  267.  /* delay again */
  268.  SioDelay(ONE_SECOND+ONE_SECOND/4);
  269. } /* end ModemCmdState */
  270.  
  271. /* hangup phone (in command state) */
  272.  
  273. void ModemHangup(int Port)
  274. {/* enter command state */
  275.  ModemCmdState(Port);
  276.  /* hangup ! */
  277.  ModemSendTo(Port,4,"!AT!");
  278.  ModemEcho(Port,10);
  279.  ModemSendTo(Port,4,"ATH0!");
  280. } /* end Hangup */
  281.  
  282. /* wait for continuous quiet (no incoming serial data) */
  283.  
  284. int ModemQuiet(
  285.   int  Port,       /* Port to talk to */
  286.   int  Tics)       /* # tics quiet required */
  287. {int i;
  288.  int Code;
  289.  long CharTime;
  290.  /* set up */
  291.  CharTime = SioTimer();
  292.  while(1)
  293.    {/* User BREAK ? */
  294.     if(BreakTest())
  295.       {
  296.        return(FALSE);
  297.       }
  298.     /* wait for next character */
  299. #if USE_WIN_IO
  300.     Code = CharGet(Port,1);
  301. #else
  302.     Code = SioGetc(Port,1);
  303. #endif
  304.     if(Code<-1) return(FALSE);
  305.     if(Code>=0)
  306.       {CharTime = SioTimer();
  307. #if USE_WIN_IO
  308.        WinPutChar(SCR_WIN,(char)Code);
  309. #else
  310.        printf("%c",(char)Code);
  311. #endif
  312.       }
  313.     else
  314.       {/* ==-1, timed out */
  315.        if(SioTimer() >= CharTime+(long)Tics) return TRUE;
  316.       }
  317.    } /* end while */
  318. }
  319.  
  320. void ModemDebug(void)
  321. {Debug = TRUE;
  322. }
  323.