home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term33so.lha / termSendText.c < prev    next >
C/C++ Source or Header  |  1993-04-30  |  8KB  |  508 lines

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