home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / term-source.lha / SendText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  8.0 KB  |  557 lines

  1. /*
  2. **    SendText.c
  3. **
  4. **    Text sending support routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14.     /* Local variables. */
  15.  
  16. STATIC LONG WaitCount,PromptCount;
  17.  
  18.     /* MatchPrompt():
  19.      *
  20.      *    Search incoming data stream for a match.
  21.      */
  22.  
  23. BOOL
  24. MatchPrompt(register STRPTR Data,register LONG Size,register STRPTR Prompt,register LONG PromptLen)
  25. {
  26.     register UBYTE c,Mask;
  27.  
  28.     if(!Data)
  29.     {
  30.         WaitCount = PromptCount = 0;
  31.  
  32.         return(FALSE);
  33.     }
  34.  
  35.     if(Config -> SerialConfig -> StripBit8)
  36.         Mask = 0x7F;
  37.     else
  38.         Mask = 0xFF;
  39.  
  40.     do
  41.     {
  42.         if(c = ((*Data++) & Mask))
  43.         {
  44.             register BOOL MatchMade;
  45.  
  46.             do
  47.             {
  48.                 MatchMade = FALSE;
  49.  
  50.                 if(PromptCount == WaitCount)
  51.                 {
  52.                     if(c == Prompt[WaitCount] & Mask)
  53.                     {
  54.                         MatchMade = TRUE;
  55.  
  56.                         if(PromptLen == ++PromptCount)
  57.                             return(TRUE);
  58.                     }
  59.                 }
  60.  
  61.                 if(MatchMade)
  62.                     WaitCount++;
  63.                 else
  64.                 {
  65.                     if(WaitCount)
  66.                     {
  67.                         WaitCount = 0;
  68.  
  69.                         PromptCount = 0;
  70.                     }
  71.                     else
  72.                         break;
  73.                 }
  74.             }
  75.             while(!WaitCount);
  76.         }
  77.     }
  78.     while(--Size);
  79.  
  80.     return(FALSE);
  81. }
  82.  
  83.     /* LocalWaitForPrompt(STRPTR Prompt,LONG PromptLen):
  84.      *
  85.      *    Scan the incoming data flow for a certain string.
  86.      */
  87.  
  88. STATIC BOOL
  89. LocalWaitForPrompt(STRPTR Prompt,LONG PromptLen)
  90. {
  91.     ULONG Signals;
  92.  
  93.     MatchPrompt(NULL,0,NULL,0);
  94.  
  95.     if(DataHold)
  96.     {
  97.         DataHold = NULL;
  98.  
  99.         RestartSerial();
  100.     }
  101.  
  102.     if(CheckSerialRead())
  103.         Signals = SIG_SERIAL;
  104.     else
  105.         Signals = NULL;
  106.  
  107.     StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  108.  
  109.     for(;;)
  110.     {
  111.         if(Signals & SIG_SERIAL)
  112.         {
  113.             if(!WaitSerialRead())
  114.             {
  115.                 LONG Length;
  116.  
  117.                 BytesIn++;
  118.  
  119.                 if(Translate_CR_LF)
  120.                     Length = (* Translate_CR_LF)(ReadBuffer,1);
  121.                 else
  122.                     Length = 1;
  123.  
  124.                 if(Length)
  125.                 {
  126.                     ConProcess(ReadBuffer,Length);
  127.  
  128.                     Status = STATUS_READY;
  129.  
  130.                     if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  131.                     {
  132.                         StopTime();
  133.  
  134.                         RestartSerial();
  135.  
  136.                         return(TRUE);
  137.                     }
  138.                 }
  139.  
  140.                 do
  141.                 {
  142.                         /* Check how many bytes are still in
  143.                          * the serial buffer.
  144.                          */
  145.  
  146.                     if(Length = GetSerialWaiting())
  147.                     {
  148.                         if(Length > SerialBufferSize)
  149.                             Length = SerialBufferSize;
  150.  
  151.                         if(!DoSerialRead(ReadBuffer,Length))
  152.                         {
  153.                             BytesIn += Length;
  154.  
  155.                             if(Translate_CR_LF)
  156.                                 Length = (* Translate_CR_LF)(ReadBuffer,Length);
  157.  
  158.                             if(Length)
  159.                             {
  160.                                 ConProcess(ReadBuffer,Length);
  161.  
  162.                                 Status = STATUS_READY;
  163.  
  164.                                 if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
  165.                                 {
  166.                                     StopTime();
  167.  
  168.                                     RestartSerial();
  169.  
  170.                                     return(TRUE);
  171.                                 }
  172.                             }
  173.                         }
  174.                     }
  175.                 }
  176.                 while(Length);
  177.             }
  178.  
  179.             RestartSerial();
  180.         }
  181.  
  182.         if(Signals & SIG_TIMER)
  183.         {
  184.             WaitTime();
  185.  
  186.             return(FALSE);
  187.         }
  188.  
  189.         Signals = Wait(SIG_SERIAL | SIG_TIMER);
  190.     }
  191. }
  192.  
  193.     /* SendLinePrompt(STRPTR Line,LONG Len):
  194.      *
  195.      *    Send text line, wait for prompt.
  196.      */
  197.  
  198. BOOL
  199. SendLinePrompt(STRPTR Line,LONG Len)
  200. {
  201.     LONG i;
  202.  
  203.     if(Len == -1)
  204.         Len = strlen(Line);
  205.  
  206.     while(Len)
  207.     {
  208.         i = 0;
  209.  
  210.         while(i < Len && Line[i] != '\r')
  211.             i++;
  212.  
  213.  
  214.         if(Line[i] == '\r')
  215.         {
  216.             i++;
  217.  
  218.             SerWrite(Line,i);
  219.  
  220.             if(!LocalWaitForPrompt(SendPrompt,SendPromptLen))
  221.                 return(FALSE);
  222.         }
  223.         else
  224.         {
  225.             if(i)
  226.                 SerWrite(Line,i);
  227.         }
  228.  
  229.         Len    -= i;
  230.         Line    += i;
  231.     }
  232.  
  233.     return(TRUE);
  234. }
  235.  
  236.     /* SendLineSimple(STRPTR Line,LONG Len):
  237.      *
  238.      *    Send a text line, no fancy features.
  239.      */
  240.  
  241. BOOL
  242. SendLineSimple(STRPTR Line,LONG Len)
  243. {
  244.     if(Len == -1)
  245.         Len = strlen(Line);
  246.  
  247.     SerWrite(Line,Len);
  248.  
  249.     return(TRUE);
  250. }
  251.  
  252.     /* SendLineDial(STRPTR Line,LONG Len):
  253.      *
  254.      *    The SendLineSimple for the dialer and init commands.
  255.      */
  256.  
  257. BOOL
  258. SendLineDial(STRPTR Line,LONG Len)
  259. {
  260.     if(Len == -1)
  261.         Len = strlen(Line);
  262.  
  263.     if(Config -> ModemConfig -> CharSendDelay)
  264.     {
  265.         ULONG    Seconds    = Config -> ModemConfig -> CharSendDelay / MILLION,
  266.             Micros    = Config -> ModemConfig -> CharSendDelay % MILLION;
  267.  
  268.         while(Len--)
  269.         {
  270.             SerWrite(Line++,1);
  271.  
  272.             DelayTime(Seconds,Micros);
  273.         }
  274.     }
  275.     else
  276.         SerWrite(Line,Len);
  277.  
  278.     return(TRUE);
  279. }
  280.  
  281.     /* SendLineDelay(STRPTR Line,LONG Len):
  282.      *
  283.      *    Send a text line, include a delay where necessary.
  284.      */
  285.  
  286. BOOL
  287. SendLineDelay(STRPTR Line,LONG Len)
  288. {
  289.     if(Len == -1)
  290.         Len = strlen(Line);
  291.  
  292.     while(Len--)
  293.     {
  294.         SerWrite(Line,1);
  295.  
  296.         if(*Line == '\r')
  297.             DelayTime(Config -> ClipConfig -> LineDelay / 100,(Config -> ClipConfig -> LineDelay % 100) * 10000);
  298.         else
  299.             DelayTime(Config -> ClipConfig -> CharDelay / 100,(Config -> ClipConfig -> CharDelay % 100) * 10000);
  300.  
  301.         Line++;
  302.     }
  303.  
  304.     return(TRUE);
  305. }
  306.  
  307.     /* SendLineEcho(STRPTR Line,LONG Len):
  308.      *
  309.      *    Send a text line, wait for single characters to be echoed.
  310.      */
  311.  
  312. BOOL
  313. SendLineEcho(STRPTR Line,LONG Len)
  314. {
  315.     ULONG Signals;
  316.  
  317.     if(DataHold)
  318.     {
  319.         DataHold = NULL;
  320.  
  321.         RestartSerial();
  322.     }
  323.  
  324.     if(Len == -1)
  325.         Len = strlen(Line);
  326.  
  327.     while(Len--)
  328.     {
  329.         SerWrite(Line,1);
  330.  
  331.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  332.  
  333.         FOREVER
  334.         {
  335.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  336.  
  337.             if(Signals & SIG_SERIAL)
  338.             {
  339.                 if(!WaitSerialRead())
  340.                 {
  341.                     LONG Length;
  342.  
  343.                     if(Translate_CR_LF)
  344.                         Length = (* Translate_CR_LF)(ReadBuffer,1);
  345.                     else
  346.                         Length = 1;
  347.  
  348.                     if(Length > Len)
  349.                         Length = Len;
  350.  
  351.                     if(Length)
  352.                     {
  353.                         if(!memcmp(ReadBuffer,Line,Length))
  354.                         {
  355.                             StopTime();
  356.  
  357.                             RestartSerial();
  358.  
  359.                             break;
  360.                         }
  361.                     }
  362.  
  363.                     RestartSerial();
  364.                 }
  365.                 else
  366.                 {
  367.                     StopTime();
  368.  
  369.                     RestartSerial();
  370.  
  371.                     return(FALSE);
  372.                 }
  373.             }
  374.  
  375.             if(Signals & SIG_TIMER)
  376.             {
  377.                 WaitTime();
  378.  
  379.                 return(FALSE);
  380.             }
  381.         }
  382.  
  383.         Line++;
  384.     }
  385.  
  386.     return(TRUE);
  387. }
  388.  
  389.     /* SendLineAnyEcho(STRPTR Line,LONG Len):
  390.      *
  391.      *    Send a text line, wait for characters to be echoed.
  392.      */
  393.  
  394. BOOL
  395. SendLineAnyEcho(STRPTR Line,LONG Len)
  396. {
  397.     ULONG Signals;
  398.  
  399.     if(DataHold)
  400.     {
  401.         DataHold = NULL;
  402.  
  403.         RestartSerial();
  404.     }
  405.  
  406.     if(Len == -1)
  407.         Len = strlen(Line);
  408.  
  409.     while(Len--)
  410.     {
  411.         SerWrite(Line,1);
  412.  
  413.         StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
  414.  
  415.         FOREVER
  416.         {
  417.             Signals = Wait(SIG_TIMER | SIG_SERIAL);
  418.  
  419.             if(Signals & SIG_SERIAL)
  420.             {
  421.                 if(!WaitSerialRead())
  422.                 {
  423.                     StopTime();
  424.  
  425.                     RestartSerial();
  426.  
  427.                     break;
  428.                 }
  429.                 else
  430.                 {
  431.                     StopTime();
  432.  
  433.                     RestartSerial();
  434.  
  435.                     return(FALSE);
  436.                 }
  437.             }
  438.  
  439.             if(Signals & SIG_TIMER)
  440.             {
  441.                 WaitTime();
  442.  
  443.                 return(FALSE);
  444.             }
  445.         }
  446.  
  447.         Line++;
  448.     }
  449.  
  450.     return(TRUE);
  451. }
  452.  
  453.     /* SendLineKeyDelay(STRPTR Line,LONG Len):
  454.      *
  455.      *    Send a text line, include keyboard delay pauses between characters.
  456.      */
  457.  
  458. BOOL
  459. SendLineKeyDelay(STRPTR Line,LONG Len)
  460. {
  461.     struct Preferences Prefs;
  462.  
  463.     if(Len == -1)
  464.         Len = strlen(Line);
  465.  
  466.         /* Get current key repeat delay. */
  467.  
  468.     GetPrefs(&Prefs,offsetof(struct Preferences,KeyRptDelay));
  469.  
  470.         /* Any delay specified at all? */
  471.  
  472.     if(Prefs . KeyRptSpeed . tv_secs || Prefs . KeyRptSpeed . tv_micro)
  473.     {
  474.         while(Len--)
  475.         {
  476.             SerWrite(Line++,1);
  477.  
  478.             if(Len)
  479.                 DelayTime(Prefs . KeyRptSpeed . tv_secs,Prefs . KeyRptSpeed . tv_micro);
  480.         }
  481.     }
  482.     else
  483.         SerWrite(Line,Len);
  484.  
  485.     return(TRUE);
  486. }
  487.  
  488.     /* SendSetup():
  489.      *
  490.      *    Choose the right routine for the text line output job.
  491.      */
  492.  
  493. VOID
  494. SendSetup()
  495. {
  496.         /* Prepare the prompt string. */
  497.  
  498.     if(Config -> ClipConfig -> LinePrompt[0])
  499.         SendPromptLen = TranslateString(Config -> ClipConfig -> LinePrompt,SendPrompt);
  500.     else
  501.     {
  502.         SendPrompt[0] = 0;
  503.         SendPromptLen = 0;
  504.     }
  505.  
  506.         /* Pick the line send routine. */
  507.  
  508.     switch(Config -> ClipConfig -> PacingMode)
  509.     {
  510.         case PACE_DIRECT:
  511.  
  512.             SendLine = SendLineSimple;
  513.             break;
  514.  
  515.         case PACE_ECHO:
  516.  
  517.             if(Config -> ClipConfig -> SendTimeout)
  518.                 SendLine = SendLineEcho;
  519.             else
  520.                 SendLine = SendLineSimple;
  521.  
  522.             break;
  523.  
  524.         case PACE_ANYECHO:
  525.  
  526.             if(Config -> ClipConfig -> SendTimeout)
  527.                 SendLine = SendLineAnyEcho;
  528.             else
  529.                 SendLine = SendLineSimple;
  530.  
  531.             break;
  532.  
  533.         case PACE_PROMPT:
  534.  
  535.             if(Config -> ClipConfig -> SendTimeout)
  536.                 SendLine = SendLinePrompt;
  537.             else
  538.                 SendLine = SendLineSimple;
  539.  
  540.             break;
  541.  
  542.         case PACE_DELAY:
  543.  
  544.             if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  545.                 SendLine = SendLineDelay;
  546.             else
  547.                 SendLine = SendLineSimple;
  548.  
  549.             break;
  550.  
  551.         case PACE_KEYBOARD:
  552.  
  553.             SendLine = SendLineKeyDelay;
  554.             break;
  555.     }
  556. }
  557.