home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / telecomm / terms / term-4.1-source.lha / Extras / Source / term-Source.lha / termSerial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  37.9 KB  |  2,074 lines

  1. /*
  2. **    termSerial.c
  3. **
  4. **    Serial driver support routines
  5. **
  6. **    Copyright © 1990-1994 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Local copy of serial driver name and unit number. */
  13.  
  14. STATIC UBYTE __far    SerialDevice[40];
  15. STATIC LONG        UnitNumber = -1;
  16.  
  17. STATIC BOOLEAN        SerialLocked = FALSE;
  18.  
  19.     /* SetFlags(BOOLEAN Read):
  20.      *
  21.      *    Set the contents of a serial device request according
  22.      *    to the current configuration settings.
  23.      */
  24.  
  25. STATIC BYTE __regargs
  26. SetFlags(BOOLEAN Read)
  27. {
  28.     if(Read)
  29.     {
  30.         return(SetSerialWriteAttributes(
  31.             SERA_Baud,        Config -> SerialConfig -> BaudRate,
  32.             SERA_BreakTime,        Config -> SerialConfig -> BreakLength,
  33.             SERA_BitsPerChar,    Config -> SerialConfig -> BitsPerChar,
  34.             SERA_StopBits,        Config -> SerialConfig -> StopBits,
  35.             SERA_BufferSize,    Config -> SerialConfig -> SerialBufferSize,
  36.             SERA_Parity,        Config -> SerialConfig -> Parity,
  37.             SERA_Handshaking,    Config -> SerialConfig -> HandshakingProtocol,
  38.             SERA_HighSpeed,        Config -> SerialConfig -> HighSpeed,
  39.             SERA_Shared,        Config -> SerialConfig -> Shared,
  40.         TAG_DONE));
  41.     }
  42.     else
  43.     {
  44.         return(SetSerialWriteAttributes(
  45.             SERA_Baud,        Config -> SerialConfig -> BaudRate,
  46.             SERA_BreakTime,        Config -> SerialConfig -> BreakLength,
  47.             SERA_BitsPerChar,    Config -> SerialConfig -> BitsPerChar,
  48.             SERA_StopBits,        Config -> SerialConfig -> StopBits,
  49.             SERA_BufferSize,    Config -> SerialConfig -> SerialBufferSize,
  50.             SERA_Parity,        Config -> SerialConfig -> Parity,
  51.             SERA_Handshaking,    Config -> SerialConfig -> HandshakingProtocol,
  52.             SERA_HighSpeed,        Config -> SerialConfig -> HighSpeed,
  53.             SERA_Shared,        Config -> SerialConfig -> Shared,
  54.         TAG_DONE));
  55.     }
  56. }
  57.  
  58.     /* SendBreak():
  59.      *
  60.      *    Transmit a break signal.
  61.      */
  62.  
  63. VOID
  64. SendBreak()
  65. {
  66.     BYTE OldStatus = Status;
  67.  
  68.     Status = STATUS_BREAKING;
  69.  
  70.     DoSerialCmd(SDCMD_BREAK);
  71.  
  72.     Status = OldStatus;
  73. }
  74.  
  75.     /* HangUp():
  76.      *
  77.      *    Hang up the line.
  78.      */
  79.  
  80. VOID
  81. HangUp()
  82. {
  83.     BYTE OldStatus = Status;
  84.  
  85.     StopSerialWrite();
  86.  
  87.     Status = STATUS_HANGUP;
  88.  
  89.         /* Are we to drop the DTR line
  90.          * before sending the hangup
  91.          * string?
  92.          */
  93.  
  94.     if(Config -> ModemConfig -> DropDTR && WriteRequest)
  95.     {
  96.         /* Let's be nice and try to transmit the
  97.          * `drop the line' command before
  98.          * trying to close and reopen the driver.
  99.          */
  100.  
  101.         WriteRequest -> IOSer . io_Command    = SIOCMD_SETCTRLLINES;
  102.         WriteRequest -> IOSer . io_Offset    = SIOB_DTRF;
  103.         WriteRequest -> IOSer . io_Length    = 0;
  104.  
  105.             /* Transmit the command. */
  106.  
  107.         if(!DoIO(WriteRequest))
  108.         {
  109.                 /* Wait a bit... */
  110.  
  111.             WaitTime(1,0);
  112.  
  113.                 /* Raise the line again. */
  114.  
  115.             WriteRequest -> IOSer . io_Command    = SIOCMD_SETCTRLLINES;
  116.             WriteRequest -> IOSer . io_Offset    = SIOB_DTRF;
  117.             WriteRequest -> IOSer . io_Length    = SIOB_DTRF;
  118.  
  119.             DoIO(WriteRequest);
  120.         }
  121.         else
  122.         {
  123.                 /* Do it the standard way: close and reopen
  124.                  * the serial driver (the serial.device is
  125.                  * supposed to drop the DTR line when closed).
  126.                  */
  127.  
  128.             if(!DropDTR())
  129.             {
  130.                 if(!MyEasyRequest(Window,LocaleString(MSG_TERMMAIN_FAILED_TO_REOPEN_UNIT_TXT),LocaleString(MSG_TERMMAIN_IGNORE_QUIT_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber))
  131.                     MainTerminated = TRUE;
  132.             }
  133.         }
  134.     }
  135.  
  136.         /* Transmit the hangup command. */
  137.  
  138.     if(Config -> ModemConfig -> ModemHangup[0])
  139.         SerialCommand(Config -> ModemConfig -> ModemHangup);
  140.  
  141.         /* Reset to old status. */
  142.  
  143.     Status = OldStatus;
  144. }
  145.  
  146.     /* DropDTR():
  147.      *
  148.      *    Drop the data terminal ready signal (i.e. close, wait a bit
  149.      *    and then reopen the serial drive).
  150.      */
  151.  
  152. BYTE
  153. DropDTR()
  154. {
  155.         /* Finish all serial read activity. */
  156.  
  157.     ClearSerial();
  158.  
  159.         /* Do we have any channels to work with? */
  160.  
  161.     if(ReadRequest && WriteRequest)
  162.     {
  163.             /* Close the device. */
  164.  
  165.         CloseDevice(ReadRequest);
  166.  
  167.             /* Wait a bit. */
  168.  
  169.         WaitTime(1,0);
  170.  
  171.             /* Reopen the driver. */
  172.  
  173.         if(!OpenDevice(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,ReadRequest,0))
  174.         {
  175.             struct MsgPort *WritePort = WriteRequest -> IOSer . io_Message . mn_ReplyPort;
  176.  
  177.             ResetSerialRead();
  178.             ResetSerialWrite();
  179.  
  180.                 /* Fill in the rest. */
  181.  
  182.             CopyMem(ReadRequest,WriteRequest,sizeof(struct IOExtSer));
  183.  
  184.             WriteRequest -> IOSer . io_Message . mn_ReplyPort = WritePort;
  185.  
  186.             SetFlags(FALSE);
  187.  
  188.             DoSerialCmd(SDCMD_SETPARAMS);
  189.  
  190.                 /* Restart read activity. */
  191.  
  192.             RestartSerial(FALSE);
  193.  
  194.             return(TRUE);
  195.         }
  196.         else
  197.             DeleteSerial();
  198.     }
  199.     else
  200.     {
  201.         DeleteSerial();
  202.  
  203.         return(TRUE);
  204.     }
  205.  
  206.     return(FALSE);
  207. }
  208.  
  209.     /* CopyWriteFlags():
  210.      *
  211.      *    Update configuration with serial settings.
  212.      */
  213.  
  214. VOID
  215. CopyWriteFlags()
  216. {
  217.     ULONG    Baud,
  218.         BreakTime,
  219.         BitsPerChar,
  220.         StopBits,
  221.         BufferSize,
  222.         Parity,
  223.         Handshaking,
  224.         HighSpeed,
  225.         Shared;
  226.  
  227.     GetSerialWriteAttributes(
  228.         SERA_Baud,        &Baud,
  229.         SERA_BreakTime,        &BreakTime,
  230.         SERA_BitsPerChar,    &BitsPerChar,
  231.         SERA_StopBits,        &StopBits,
  232.         SERA_BufferSize,    &BufferSize,
  233.         SERA_Parity,        &Parity,
  234.         SERA_Handshaking,    &Handshaking,
  235.         SERA_HighSpeed,        &HighSpeed,
  236.         SERA_Shared,        &Shared,
  237.     TAG_DONE);
  238.  
  239.     Config -> SerialConfig -> BaudRate        = Baud;
  240.     Config -> SerialConfig -> BreakLength        = BreakTime;
  241.     Config -> SerialConfig -> BitsPerChar        = BitsPerChar;
  242.     Config -> SerialConfig -> StopBits        = StopBits;
  243.     Config -> SerialConfig -> SerialBufferSize    = BufferSize;
  244.     Config -> SerialConfig -> Parity        = Parity;
  245.     Config -> SerialConfig -> HandshakingProtocol    = Handshaking;
  246.     Config -> SerialConfig -> HighSpeed        = HighSpeed;
  247.     Config -> SerialConfig -> Shared        = Shared;
  248. }
  249.  
  250.     /* CallMenu(STRPTR Name,ULONG Code):
  251.      *
  252.      *    Call a menu function either through the name of the corresponding
  253.      *    menu item or a menu number.
  254.      */
  255.  
  256. STATIC VOID __inline
  257. CallMenu(STRPTR Name,ULONG Code)
  258. {
  259.     WORD MenuNum = -1,Item = 0,Sub = 0,i;
  260.  
  261.         /* Are we to look for a name? */
  262.  
  263.     if(Name)
  264.     {
  265.         WORD Len = strlen(Name);
  266.  
  267.             /* Scan the menu list... */
  268.  
  269.         for(i = 0 ; TermMenu[i] . nm_Type != NM_END ; i++)
  270.         {
  271.             switch(TermMenu[i] . nm_Type)
  272.             {
  273.                 case NM_TITLE:
  274.  
  275.                     MenuNum++;
  276.                     Item = Sub = 0;
  277.                     break;
  278.  
  279.                 case NM_ITEM:
  280.  
  281.                     Sub = 0;
  282.                     break;
  283.             }
  284.  
  285.                 /* Did we get a valid name string? */
  286.  
  287.             if(TermMenu[i] . nm_Label != NM_BARLABEL)
  288.             {
  289.                     /* Does the name match our template? */
  290.  
  291.                 if(!Strnicmp(TermMenu[i] . nm_Label,Name,Len))
  292.                 {
  293.                     struct MenuItem *MenuItem = ItemAddress(Menu,FULLMENUNUM(MenuNum,Item,Sub));
  294.  
  295.                     if(MenuItem)
  296.                         HandleMenuCode((ULONG)TermMenu[i] . nm_UserData,NULL);
  297.  
  298.                     break;
  299.                 }
  300.             }
  301.  
  302.             switch(TermMenu[i] . nm_Type)
  303.             {
  304.                 case NM_ITEM:
  305.  
  306.                     Item++;
  307.                     break;
  308.  
  309.                 case NM_SUB:
  310.  
  311.                     Sub++;
  312.                     break;
  313.             }
  314.         }
  315.     }
  316.     else
  317.     {
  318.         WORD    TheMenu    =  Code % 100,
  319.             TheItem    = (Code / 100) % 100,
  320.             TheSub    =  Code / 10000;
  321.  
  322.             /* Scan the menu list... */
  323.  
  324.         for(i = 0 ; TermMenu[i] . nm_Type != NM_END ; i++)
  325.         {
  326.             switch(TermMenu[i] . nm_Type)
  327.             {
  328.                 case NM_TITLE:
  329.  
  330.                     MenuNum++;
  331.                     Item = Sub = 0;
  332.                     break;
  333.  
  334.                 case NM_ITEM:
  335.  
  336.                     Sub = 0;
  337.                     break;
  338.             }
  339.  
  340.                 /* Is it the menu number we want? */
  341.  
  342.             if(TheMenu == MenuNum && TheItem == Item && TheSub == Sub)
  343.             {
  344.                 if(TermMenu[i] . nm_Label != NM_BARLABEL)
  345.                 {
  346.                     struct MenuItem *MenuItem = ItemAddress(Menu,FULLMENUNUM(MenuNum,Item,Sub));
  347.  
  348.                     if(MenuItem)
  349.                         HandleMenuCode((ULONG)TermMenu[i] . nm_UserData,NULL);
  350.                 }
  351.  
  352.                 break;
  353.             }
  354.  
  355.             switch(TermMenu[i] . nm_Type)
  356.             {
  357.                 case NM_ITEM:
  358.  
  359.                     Item++;
  360.                     break;
  361.  
  362.                 case NM_SUB:
  363.  
  364.                     Sub++;
  365.                     break;
  366.             }
  367.         }
  368.     }
  369. }
  370.  
  371.     /* SerialCommand(STRPTR String):
  372.      *
  373.      *    Send a command string to the serial line and
  374.      *    interprete the control sequences.
  375.      */
  376.  
  377. VOID __regargs
  378. SerialCommand(STRPTR String)
  379. {
  380.     BYTE    (*  SendLineLocal)(register STRPTR,register LONG);
  381.  
  382.     LONG    Count = 0,i,Len = strlen(String);
  383.  
  384.     BYTE    GotControl    = FALSE,
  385.         GotEscape    = FALSE;
  386.  
  387.     SendLineLocal = SendLine;
  388.  
  389.         /* Scan the string. */
  390.  
  391.     for(i = 0 ; i < Len ; i++)
  392.     {
  393.             /* We are looking for plain characters
  394.              * and the control ('\') and escape
  395.              * ('^') characters.
  396.              */
  397.  
  398.         if(!GotControl && !GotEscape)
  399.         {
  400.                 /* Got a control character,
  401.                  * the next byte will probably be
  402.                  * a command sequence.
  403.                  */
  404.  
  405.             if(String[i] == '\\')
  406.             {
  407.                 GotControl = TRUE;
  408.                 continue;
  409.             }
  410.  
  411.                 /* Got an escape character,
  412.                  * the next byte will be some
  413.                  * kind of control character
  414.                  * (such as XON, XOF, bell, etc.).
  415.                  */
  416.  
  417.             if(String[i] == '^')
  418.             {
  419.                 GotEscape = TRUE;
  420.                 continue;
  421.             }
  422.  
  423.                 /* This tells us to wait another
  424.                  * second before continuing with
  425.                  * the scanning.
  426.                  */
  427.  
  428.             if(String[i] == '~')
  429.             {
  430.                 if(Count)
  431.                 {
  432.                     (*SendLineLocal)(SharedBuffer,Count);
  433.  
  434.                     Count = 0;
  435.                 }
  436.  
  437.                 WaitTime(0,MILLION / 2);
  438.  
  439.                 HandleSerial();
  440.  
  441.                 continue;
  442.             }
  443.  
  444.                 /* Stuff the character into the
  445.                  * buffer.
  446.                  */
  447.  
  448.             SharedBuffer[Count++] = String[i];
  449.         }
  450.         else
  451.         {
  452.                 /* Convert the character to a control
  453.                  * style character (^C, etc.).
  454.                  */
  455.  
  456.             if(GotEscape)
  457.             {
  458.                 if(ToUpper(String[i]) >= 'A' && ToUpper(String[i]) <= '_')
  459.                     SharedBuffer[Count++] = ToUpper(String[i]) - '@';
  460.                 else
  461.                     SharedBuffer[Count++] = String[i];
  462.  
  463.                 GotEscape = FALSE;
  464.             }
  465.  
  466.                 /* The next character represents a command. */
  467.  
  468.             if(GotControl)
  469.             {
  470.                 switch(ToUpper(String[i]))
  471.                 {
  472.                         /* Fall back to default send mode. */
  473.  
  474.                     case '0':
  475.  
  476.                         if(Count)
  477.                         {
  478.                             (*SendLineLocal)(SharedBuffer,Count);
  479.  
  480.                             Count = 0;
  481.                         }
  482.  
  483.                         SendLineLocal = SendLine;
  484.                         break;
  485.  
  486.                         /* Select `direct' send mode. */
  487.  
  488.                     case '1':
  489.  
  490.                         if(Count)
  491.                         {
  492.                             (*SendLineLocal)(SharedBuffer,Count);
  493.  
  494.                             Count = 0;
  495.                         }
  496.  
  497.                         SendLineLocal = SendLineSimple;
  498.                         break;
  499.  
  500.                         /* Select `echo' send mode. */
  501.  
  502.                     case '2':
  503.  
  504.                         if(Count)
  505.                         {
  506.                             (*SendLineLocal)(SharedBuffer,Count);
  507.  
  508.                             Count = 0;
  509.                         }
  510.  
  511.                         if(Config -> ClipConfig -> SendTimeout)
  512.                             SendLineLocal = SendLineEcho;
  513.                         else
  514.                             SendLineLocal = SendLineSimple;
  515.  
  516.                         break;
  517.  
  518.                         /* Select `any echo' send mode. */
  519.  
  520.                     case '3':
  521.  
  522.                         if(Count)
  523.                         {
  524.                             (*SendLineLocal)(SharedBuffer,Count);
  525.  
  526.                             Count = 0;
  527.                         }
  528.  
  529.                         if(Config -> ClipConfig -> SendTimeout)
  530.                             SendLineLocal = SendLineAnyEcho;
  531.                         else
  532.                             SendLineLocal = SendLineSimple;
  533.  
  534.                         break;
  535.  
  536.                         /* Select `prompt' send mode. */
  537.  
  538.                     case '4':
  539.  
  540.                         if(Count)
  541.                         {
  542.                             (*SendLineLocal)(SharedBuffer,Count);
  543.  
  544.                             Count = 0;
  545.                         }
  546.  
  547.                         if(Config -> ClipConfig -> SendTimeout)
  548.                             SendLineLocal = SendLinePrompt;
  549.                         else
  550.                             SendLineLocal = SendLineSimple;
  551.  
  552.                         break;
  553.  
  554.                         /* Select `delay' send mode. */
  555.  
  556.                     case '5':
  557.  
  558.                         if(Count)
  559.                         {
  560.                             (*SendLineLocal)(SharedBuffer,Count);
  561.  
  562.                             Count = 0;
  563.                         }
  564.  
  565.                         if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
  566.                             SendLineLocal = SendLineDelay;
  567.                         else
  568.                             SendLineLocal = SendLineSimple;
  569.  
  570.                         break;
  571.  
  572.                         /* Select `keyboard' send mode. */
  573.  
  574.                     case '6':
  575.  
  576.                         if(Count)
  577.                         {
  578.                             (*SendLineLocal)(SharedBuffer,Count);
  579.  
  580.                             Count = 0;
  581.                         }
  582.  
  583.                         SendLineLocal = SendLineKeyDelay;
  584.                         break;
  585.  
  586.                         /* Translate code. */
  587.  
  588.                     case '*':
  589.  
  590.                         i++;
  591.  
  592.                         while(i < Len && String[i] == ' ')
  593.                             i++;
  594.  
  595.                         if(i < Len)
  596.                         {
  597.                             UBYTE DummyBuffer[5],j = 0,Char;
  598.  
  599.                             if(String[i] >= '0' && String[i] <= '9')
  600.                             {
  601.                                 while(j < 3 && i < Len)
  602.                                 {
  603.                                     Char = String[i++];
  604.  
  605.                                     if(Char >= '0' && Char <= '9')
  606.                                         DummyBuffer[j++] = Char;
  607.                                     else
  608.                                     {
  609.                                         i--;
  610.  
  611.                                         break;
  612.                                     }
  613.                                 }
  614.                             }
  615.                             else
  616.                             {
  617.                                 while(j < 4 && i < Len)
  618.                                 {
  619.                                     Char = ToLower(String[i++]);
  620.  
  621.                                     if((Char >= '0' && Char <= '9') || (Char >= 'a' && Char <= 'z'))
  622.                                         DummyBuffer[j++] = Char;
  623.                                     else
  624.                                     {
  625.                                         i--;
  626.  
  627.                                         break;
  628.                                     }
  629.                                 }
  630.                             }
  631.  
  632.                             DummyBuffer[j] = 0;
  633.  
  634.                             SharedBuffer[Count++] = NameToCode(DummyBuffer);
  635.                         }
  636.  
  637.                         i--;
  638.  
  639.                         break;
  640.  
  641.                         /* Execute an AmigaDOS command. */
  642.  
  643.                     case 'D':
  644.  
  645.                         if(!InRexx)
  646.                         {
  647.                             if(!WeAreBlocking)
  648.                             {
  649.                                 BlockWindows();
  650.  
  651.                                 SendAmigaDOSCommand(&String[i + 1]);
  652.  
  653.                                 ReleaseWindows();
  654.                             }
  655.                             else
  656.                                 SendAmigaDOSCommand(&String[i + 1]);
  657.                         }
  658.  
  659.                         return;
  660.  
  661.                         /* Execute an ARexx command. */
  662.  
  663.                     case 'A':
  664.  
  665.                         if(!InRexx)
  666.                         {
  667.                             if(!WeAreBlocking)
  668.                             {
  669.                                 BlockWindows();
  670.  
  671.                                 SendARexxCommand(&String[i + 1]);
  672.  
  673.                                 ReleaseWindows();
  674.                             }
  675.                             else
  676.                                 SendARexxCommand(&String[i + 1]);
  677.                         }
  678.  
  679.                         return;
  680.  
  681.                         /* Add the control character ('\'). */
  682.  
  683.                     case '\\':
  684.  
  685.                         SharedBuffer[Count++] = '\\';
  686.                         break;
  687.  
  688.                         /* This is a backspace. */
  689.  
  690.                     case 'B':
  691.  
  692.                         SharedBuffer[Count++] = '\b';
  693.                         break;
  694.  
  695.                         /* This is a form feed. */
  696.  
  697.                     case 'F':
  698.  
  699.                         SharedBuffer[Count++] = '\f';
  700.                         break;
  701.  
  702.                         /* This is a line feed. */
  703.  
  704.                     case 'N':
  705.  
  706.                         SharedBuffer[Count++] = '\n';
  707.                         break;
  708.  
  709.                         /* Send the current password. */
  710.  
  711.                     case 'P':
  712.  
  713.                         if(Password[0])
  714.                         {
  715.                             if(Count)
  716.                             {
  717.                                 (*SendLineLocal)(SharedBuffer,Count);
  718.  
  719.                                 Count = 0;
  720.                             }
  721.  
  722.                             (*SendLineLocal)(Password,strlen(Password));
  723.                         }
  724.  
  725.                         break;
  726.  
  727.                         /* This is a carriage return. */
  728.  
  729.                     case 'R':
  730.  
  731.                         SharedBuffer[Count++] = '\r';
  732.                         break;
  733.  
  734.                         /* This is a tab. */
  735.  
  736.                     case 'T':
  737.  
  738.                         SharedBuffer[Count++] = '\t';
  739.                         break;
  740.  
  741.                         /* Send the current user name. */
  742.  
  743.                     case 'U':
  744.  
  745.                         if(UserName[0])
  746.                         {
  747.                             if(Count)
  748.                             {
  749.                                 (*SendLineLocal)(SharedBuffer,Count);
  750.  
  751.                                 Count = 0;
  752.                             }
  753.  
  754.                             (*SendLineLocal)(UserName,strlen(UserName));
  755.                         }
  756.  
  757.                         break;
  758.  
  759.                         /* Send a break across the serial line. */
  760.  
  761.                     case 'X':
  762.  
  763.                         if(Count)
  764.                         {
  765.                             (*SendLineLocal)(SharedBuffer,Count);
  766.  
  767.                             Count = 0;
  768.                         }
  769.  
  770.                         SendBreak();
  771.  
  772.                         break;
  773.  
  774.                         /* Feed the contents of the
  775.                          * clipboard into the input
  776.                          * stream.
  777.                          */
  778.  
  779.                     case 'I':
  780.  
  781.                         if(Count)
  782.                             (*SendLineLocal)(SharedBuffer,Count);
  783.  
  784.                         Count = LoadClip(SharedBuffer,256);
  785.  
  786.                         break;
  787.  
  788.                         /* Send a string to the clipboard. */
  789.  
  790.                     case 'G':
  791.  
  792.                         if(String[i + 1])
  793.                             SaveClip(&String[i + 1],strlen(&String[i + 1]));
  794.  
  795.                         return;
  796.  
  797.                         /* Send a string to the clipboard. */
  798.  
  799.                     case 'H':
  800.  
  801.                         if(String[i + 1])
  802.                             AddClip(&String[i + 1],strlen(&String[i + 1]));
  803.  
  804.                         return;
  805.  
  806.                         /* Produce the escape character. */
  807.  
  808.                     case 'E':
  809.  
  810.                         SharedBuffer[Count++] = ESC;
  811.                         break;
  812.  
  813.                         /* Call a menu item. */
  814.  
  815.                     case 'C':
  816.  
  817.                         i++;
  818.  
  819.                             /* Scan for a menu number or
  820.                              * a single quote...
  821.                              */
  822.  
  823.                         while(i < Len)
  824.                         {
  825.                             if(String[i] >= '0' && String[i] <= '9')
  826.                                 break;
  827.  
  828.                             if(String[i] == '\'')
  829.                                 break;
  830.  
  831.                             if(String[i] != ' ')
  832.                                 break;
  833.  
  834.                             i++;
  835.                         }
  836.  
  837.                         if(i < Len)
  838.                         {
  839.                             UBYTE DummyBuffer[256];
  840.  
  841.                                 /* Did we get a quote? */
  842.  
  843.                             if(String[i] == '\'')
  844.                             {
  845.                                 LONG Start = ++i;
  846.  
  847.                                 if(String[Start])
  848.                                 {
  849.                                     WORD Length;
  850.  
  851.                                     while(i < Len)
  852.                                     {
  853.                                         if(String[i] != '\'')
  854.                                             i++;
  855.                                         else
  856.                                             break;
  857.                                     }
  858.  
  859.                                     if(String[i] == '\'')
  860.                                         Length = i - Start;
  861.                                     else
  862.                                         Length = i - Start + 1;
  863.  
  864.                                     memcpy(DummyBuffer,&String[Start],Length);
  865.  
  866.                                     DummyBuffer[Length] = 0;
  867.  
  868.                                     CallMenu(DummyBuffer,0);
  869.                                 }
  870.                             }
  871.                             else
  872.                             {
  873.                                 if(String[i] >= '0' && String[i] <= '9')
  874.                                 {
  875.                                     LONG Start = i,Length;
  876.  
  877.                                     while(i < Len)
  878.                                     {
  879.                                         if(String[i] >= '0' && String[i] <= '9')
  880.                                             i++;
  881.                                         else
  882.                                             break;
  883.                                     }
  884.  
  885.                                     if(i == Start)
  886.                                         Length = 1;
  887.                                     else
  888.                                         Length = i - Start;
  889.  
  890.                                     memcpy(DummyBuffer,&String[Start],Length);
  891.  
  892.                                     DummyBuffer[Length] = 0;
  893.  
  894.                                     CallMenu(NULL,Atol(DummyBuffer));
  895.                                 }
  896.                             }
  897.                         }
  898.  
  899.                         break;
  900.  
  901.                         /* Stuff the character into the buffer. */
  902.  
  903.                     default:
  904.  
  905.                         SharedBuffer[Count++] = String[i];
  906.                         break;
  907.                 }
  908.  
  909.                 GotControl = FALSE;
  910.             }
  911.         }
  912.  
  913.             /* If the buffer is full, release it. */
  914.  
  915.         if(Count == 256)
  916.         {
  917.             (*SendLineLocal)(SharedBuffer,Count);
  918.  
  919.             Count = 0;
  920.         }
  921.     }
  922.  
  923.     if(Count)
  924.         (*SendLineLocal)(SharedBuffer,Count);
  925. }
  926.  
  927.     /* SerWriteVerbatim(APTR Buffer,LONG Size,BOOLEAN Echo):
  928.      *
  929.      *    The `no fancy features' version of SerWrite().
  930.      */
  931.  
  932. VOID __regargs
  933. SerWriteVerbatim(APTR Buffer,LONG Size,BOOLEAN Echo)
  934. {
  935.     if(XProtocolBase && (TransferBits & XPRS_USERMON))
  936.         Size = XProtocolUserMon(XprIO,Buffer,Size,Size);
  937.  
  938.     if(Size > 0)
  939.     {
  940.         if(Echo)
  941.         {
  942.             if(Marking)
  943.                 DropMarker();
  944.  
  945.             StartSerialWrite(Buffer,Size);
  946.  
  947.             ConProcess(Buffer,Size);
  948.  
  949.             WaitSerialWrite();
  950.         }
  951.         else
  952.             DoSerialWrite(Buffer,Size);
  953.  
  954.         BytesOut += Size;
  955.     }
  956. }
  957.  
  958.     /* SerWrite(APTR Buffer,LONG Size):
  959.      *
  960.      *    Send a number of bytes across the serial line.
  961.      */
  962.  
  963. VOID __regargs
  964. SerWrite(APTR Buffer,LONG Size)
  965. {
  966.     if(Size < 0)
  967.         Size = strlen(Buffer);
  968.  
  969.     if(Size)
  970.     {
  971.         if(RememberInput)
  972.         {
  973.             RememberInputText(Buffer,Size);
  974.  
  975.             if(((UBYTE *)Buffer)[Size - 1] == '\r' && RecordingLine)
  976.             {
  977.                 RememberSpill();
  978.  
  979.                 RecordingLine = FALSE;
  980.  
  981.                 RememberOutput = TRUE;
  982.                 RememberInput = FALSE;
  983.  
  984.                 CheckItem(MEN_RECORD_LINE,FALSE);
  985.             }
  986.         }
  987.         else
  988.         {
  989.             if(RememberOutput)
  990.             {
  991.                 RememberInputText(Buffer,Size);
  992.  
  993.                 RememberSpill();
  994.             }
  995.         }
  996.  
  997.         if(WriteRequest)
  998.         {
  999.             STATIC UBYTE __far TranslateData[512];
  1000.  
  1001.                 /* xpr wants to see the data before it is
  1002.                  * transferred.
  1003.                  */
  1004.  
  1005.             if(XProtocolBase && (TransferBits & XPRS_USERMON))
  1006.             {
  1007.                 if(Size = XProtocolUserMon(XprIO,Buffer,Size,Size))
  1008.                 {
  1009.                     if(SendTable)
  1010.                     {
  1011.                         struct TranslationHandle Handle;
  1012.  
  1013.                             /* Set up for buffer translation. */
  1014.  
  1015.                         TranslateSetup(&Handle,Buffer,Size,TranslateData,512,SendTable);
  1016.  
  1017.                             /* Full or half duplex? */
  1018.  
  1019.                         if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1020.                         {
  1021.                             if(SerWriteBypass)
  1022.                             {
  1023.                                     /* Process the data... */
  1024.  
  1025.                                 while(Size = TranslateBuffer(&Handle))
  1026.                                 {
  1027.                                     if((*SerWriteBypass)(TranslateData,Size))
  1028.                                     {
  1029.                                             /* ...and send it. */
  1030.  
  1031.                                         DoSerialWrite(TranslateData,Size);
  1032.                                     }
  1033.  
  1034.                                     BytesOut += Size;
  1035.                                 }
  1036.                             }
  1037.                             else
  1038.                             {
  1039.                                     /* Process the data... */
  1040.  
  1041.                                 while(Size = TranslateBuffer(&Handle))
  1042.                                 {
  1043.                                         /* ...and send it. */
  1044.  
  1045.                                     DoSerialWrite(TranslateData,Size);
  1046.  
  1047.                                     BytesOut += Size;
  1048.                                 }
  1049.                             }
  1050.                         }
  1051.                         else
  1052.                         {
  1053.                             if(Marking)
  1054.                                 DropMarker();
  1055.  
  1056.                             if(SerWriteBypass)
  1057.                             {
  1058.                                 while(Size = TranslateBuffer(&Handle))
  1059.                                 {
  1060.                                     if((*SerWriteBypass)(TranslateData,Size))
  1061.                                     {
  1062.                                             /* Process the data while it is transferred. */
  1063.  
  1064.                                         StartSerialWrite(TranslateData,Size);
  1065.  
  1066.                                         ConProcess(Buffer,Size);
  1067.  
  1068.                                         WaitSerialWrite();
  1069.                                     }
  1070.  
  1071.                                     BytesOut += Size;
  1072.                                 }
  1073.                             }
  1074.                             else
  1075.                             {
  1076.                                 while(Size = TranslateBuffer(&Handle))
  1077.                                 {
  1078.                                         /* Process the data while it is transferred. */
  1079.  
  1080.                                     StartSerialWrite(TranslateData,Size);
  1081.  
  1082.                                     ConProcess(TranslateData,Size);
  1083.  
  1084.                                     WaitSerialWrite();
  1085.  
  1086.                                     BytesOut += Size;
  1087.                                 }
  1088.                             }
  1089.                         }
  1090.                     }
  1091.                     else
  1092.                     {
  1093.                         if(SerWriteBypass)
  1094.                         {
  1095.                             if((*SerWriteBypass)(Buffer,Size))
  1096.                             {
  1097.                                     /* If full duplex is enabled, send the entire
  1098.                                      * buffer without processing.
  1099.                                      */
  1100.  
  1101.                                 if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1102.                                     DoSerialWrite(Buffer,Size);
  1103.                                 else
  1104.                                 {
  1105.                                         /* Process the data while it is transferred. */
  1106.  
  1107.                                     StartSerialWrite(Buffer,Size);
  1108.  
  1109.                                     if(Marking)
  1110.                                         DropMarker();
  1111.  
  1112.                                     ConProcess(Buffer,Size);
  1113.  
  1114.                                     WaitSerialWrite();
  1115.                                 }
  1116.                             }
  1117.                             else
  1118.                             {
  1119.                                 if(Config -> SerialConfig -> Duplex != DUPLEX_FULL)
  1120.                                 {
  1121.                                     if(Marking)
  1122.                                         DropMarker();
  1123.  
  1124.                                     ConProcess(Buffer,Size);
  1125.                                 }
  1126.                             }
  1127.                         }
  1128.                         else
  1129.                         {
  1130.                                 /* If full duplex is enabled, send the entire
  1131.                                  * buffer without processing.
  1132.                                  */
  1133.  
  1134.                             if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1135.                                 DoSerialWrite(Buffer,Size);
  1136.                             else
  1137.                             {
  1138.                                     /* Process the data while it is transferred. */
  1139.  
  1140.                                 StartSerialWrite(Buffer,Size);
  1141.  
  1142.                                 if(Marking)
  1143.                                     DropMarker();
  1144.  
  1145.                                 ConProcess(Buffer,Size);
  1146.  
  1147.                                 WaitSerialWrite();
  1148.                             }
  1149.                         }
  1150.  
  1151.                         BytesOut += Size;
  1152.                     }
  1153.                 }
  1154.             }
  1155.             else
  1156.             {
  1157.                 if(SendTable)
  1158.                 {
  1159.                     struct TranslationHandle Handle;
  1160.  
  1161.                         /* Set up for buffer translation. */
  1162.  
  1163.                     TranslateSetup(&Handle,Buffer,Size,TranslateData,512,SendTable);
  1164.  
  1165.                         /* Full or half duplex? */
  1166.  
  1167.                     if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1168.                     {
  1169.                         if(SerWriteBypass)
  1170.                         {
  1171.                                 /* Process the data... */
  1172.  
  1173.                             while(Size = TranslateBuffer(&Handle))
  1174.                             {
  1175.                                 if((*SerWriteBypass)(TranslateData,Size))
  1176.                                 {
  1177.                                         /* ...and send it. */
  1178.  
  1179.                                     DoSerialWrite(TranslateData,Size);
  1180.                                 }
  1181.  
  1182.                                 BytesOut += Size;
  1183.                             }
  1184.                         }
  1185.                         else
  1186.                         {
  1187.                                 /* Process the data... */
  1188.  
  1189.                             while(Size = TranslateBuffer(&Handle))
  1190.                             {
  1191.                                     /* ...and send it. */
  1192.  
  1193.                                 DoSerialWrite(TranslateData,Size);
  1194.  
  1195.                                 BytesOut += Size;
  1196.                             }
  1197.                         }
  1198.                     }
  1199.                     else
  1200.                     {
  1201.                         if(Marking)
  1202.                             DropMarker();
  1203.  
  1204.                         if(SerWriteBypass)
  1205.                         {
  1206.                             while(Size = TranslateBuffer(&Handle))
  1207.                             {
  1208.                                 if((*SerWriteBypass)(TranslateData,Size))
  1209.                                 {
  1210.                                         /* Process the data while it is transferred. */
  1211.  
  1212.                                     StartSerialWrite(TranslateData,Size);
  1213.  
  1214.                                     ConProcess(TranslateData,Size);
  1215.  
  1216.                                     WaitSerialWrite();
  1217.                                 }
  1218.  
  1219.                                 BytesOut += Size;
  1220.                             }
  1221.                         }
  1222.                         else
  1223.                         {
  1224.                             while(Size = TranslateBuffer(&Handle))
  1225.                             {
  1226.                                     /* Process the data while it is transferred. */
  1227.  
  1228.                                 StartSerialWrite(TranslateData,Size);
  1229.  
  1230.                                 ConProcess(TranslateData,Size);
  1231.  
  1232.                                 WaitSerialWrite();
  1233.  
  1234.                                 BytesOut += Size;
  1235.                             }
  1236.                         }
  1237.                     }
  1238.                 }
  1239.                 else
  1240.                 {
  1241.                     if(SerWriteBypass)
  1242.                     {
  1243.                         if((*SerWriteBypass)(Buffer,Size))
  1244.                         {
  1245.                                 /* If full duplex is enabled, send the entire
  1246.                                  * buffer without processing.
  1247.                                  */
  1248.  
  1249.                             if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1250.                                 DoSerialWrite(Buffer,Size);
  1251.                             else
  1252.                             {
  1253.                                     /* Process the data while it is transferred. */
  1254.  
  1255.                                 StartSerialWrite(Buffer,Size);
  1256.  
  1257.                                 if(Marking)
  1258.                                     DropMarker();
  1259.  
  1260.                                 ConProcess(Buffer,Size);
  1261.  
  1262.                                 WaitSerialWrite();
  1263.                             }
  1264.                         }
  1265.                     }
  1266.                     else
  1267.                     {
  1268.                             /* If full duplex is enabled, send the entire
  1269.                              * buffer without processing.
  1270.                              */
  1271.  
  1272.                         if(Config -> SerialConfig -> Duplex == DUPLEX_FULL)
  1273.                             DoSerialWrite(Buffer,Size);
  1274.                         else
  1275.                         {
  1276.                                 /* Process the data while it is transferred. */
  1277.  
  1278.                             StartSerialWrite(Buffer,Size);
  1279.  
  1280.                             if(Marking)
  1281.                                 DropMarker();
  1282.  
  1283.                             ConProcess(Buffer,Size);
  1284.  
  1285.                             WaitSerialWrite();
  1286.                         }
  1287.                     }
  1288.  
  1289.                     BytesOut += Size;
  1290.                 }
  1291.             }
  1292.         }
  1293.     }
  1294. }
  1295.  
  1296.     /* RestartSerial():
  1297.      *
  1298.      *    Restart read/write activity on the serial line.
  1299.      */
  1300.  
  1301. VOID __regargs
  1302. RestartSerial(BYTE Override)
  1303. {
  1304.     StartSerialRead(ReadBuffer,1);
  1305. }
  1306.  
  1307.     /* ClearSerial():
  1308.      *
  1309.      *    Terminate all read/write activity on the serial line.
  1310.      */
  1311.  
  1312. VOID
  1313. ClearSerial()
  1314. {
  1315.     StopSerialRead();
  1316.  
  1317.     DoSerialCmd(CMD_CLEAR);
  1318. }
  1319.  
  1320.     /* DeleteSerial():
  1321.      *
  1322.      *    Close the serial device and release all associated
  1323.      *    resources.
  1324.      */
  1325.  
  1326. VOID
  1327. DeleteSerial()
  1328. {
  1329.     BYTE Closed = FALSE;
  1330.  
  1331.     if(ReadRequest)
  1332.     {
  1333.         if(ReadRequest -> IOSer . io_Device)
  1334.         {
  1335.             struct Device    *Device;
  1336.             UBYTE         Name[MAX_FILENAME_LENGTH];
  1337.  
  1338.             StopSerialRead();
  1339.  
  1340.             if(!Config -> SerialConfig -> Shared)
  1341.             {
  1342.                 ReadRequest -> IOSer . io_Command = CMD_RESET;
  1343.  
  1344.                 DoIO(ReadRequest);
  1345.             }
  1346.  
  1347.             strcpy(Name,ReadRequest -> IOSer . io_Device -> dd_Library . lib_Node . ln_Name);
  1348.  
  1349.             CloseDevice(ReadRequest);
  1350.  
  1351.             Forbid();
  1352.  
  1353.             if(Device = (struct Device *)FindName(&SysBase -> DeviceList,Name))
  1354.                 RemDevice(Device);
  1355.  
  1356.             Permit();
  1357.  
  1358.             Closed = TRUE;
  1359.         }
  1360.  
  1361.         DeleteIORequest(ReadRequest);
  1362.  
  1363.         ReadRequest = NULL;
  1364.     }
  1365.  
  1366.     if(WriteRequest)
  1367.     {
  1368.         if(WriteRequest -> IOSer . io_Device && !Closed)
  1369.         {
  1370.             struct Device    *Device;
  1371.             UBYTE         Name[MAX_FILENAME_LENGTH];
  1372.  
  1373.             StopSerialWrite();
  1374.  
  1375.             strcpy(Name,WriteRequest -> IOSer . io_Device -> dd_Library . lib_Node . ln_Name);
  1376.  
  1377.             CloseDevice(WriteRequest);
  1378.  
  1379.             Forbid();
  1380.  
  1381.             if(Device = (struct Device *)FindName(&SysBase -> DeviceList,Name))
  1382.                 RemDevice(Device);
  1383.  
  1384.             Permit();
  1385.         }
  1386.  
  1387.         if(WriteRequest -> IOSer . io_Message . mn_ReplyPort)
  1388.             DeleteMsgPort(WriteRequest -> IOSer . io_Message . mn_ReplyPort);
  1389.  
  1390.         DeleteIORequest(WriteRequest);
  1391.  
  1392.         WriteRequest = NULL;
  1393.     }
  1394.  
  1395.     if(ReadPort)
  1396.     {
  1397.         DeleteMsgPort(ReadPort);
  1398.  
  1399.         ReadPort = NULL;
  1400.     }
  1401.  
  1402.     if(ReadBuffer)
  1403.     {
  1404.         FreeVecPooled(ReadBuffer);
  1405.  
  1406.         ReadBuffer = NULL;
  1407.     }
  1408.  
  1409.     if(HostReadBuffer)
  1410.     {
  1411.         FreeVecPooled(HostReadBuffer);
  1412.  
  1413.         HostReadBuffer = NULL;
  1414.     }
  1415.  
  1416.     if(StripBuffer)
  1417.     {
  1418.         FreeVecPooled(StripBuffer);
  1419.  
  1420.         StripBuffer = NULL;
  1421.     }
  1422.  
  1423.     if(OwnDevUnitBase && SerialDevice[0] && UnitNumber != -1 && SerialLocked)
  1424.     {
  1425.         FreeDevUnit(SerialDevice,UnitNumber);
  1426.  
  1427.         SerialDevice[0] = 0;
  1428.  
  1429.         UnitNumber = -1;
  1430.     }
  1431.  
  1432.     if(OwnDevBit != -1)
  1433.     {
  1434.         FreeSignal(OwnDevBit);
  1435.  
  1436.         OwnDevBit = -1;
  1437.     }
  1438.  
  1439.     SerialLocked = FALSE;
  1440.  
  1441.     ResetSerialRead();
  1442.     ResetSerialWrite();
  1443. }
  1444.  
  1445.     /* GetSerialError(LONG Error,BYTE *Reset):
  1446.      *
  1447.      *    Return an error message for each possible serial device error.
  1448.      */
  1449.  
  1450. STRPTR __regargs
  1451. GetSerialError(LONG Error,BYTE *Reset)
  1452. {
  1453.     if(Reset)
  1454.         *Reset = FALSE;
  1455.  
  1456.     switch(Error)
  1457.     {
  1458.         case IOERR_BADLENGTH:
  1459.         case IOERR_BADADDRESS:
  1460.         case IOERR_SELFTEST:
  1461.         case IOERR_OPENFAIL:
  1462.  
  1463.             return(LocaleString(MSG_SERIAL_OPENFAILURE_TXT));
  1464.  
  1465.         case SerErr_DevBusy:
  1466.  
  1467.             return(LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT));
  1468.  
  1469.         case SerErr_BaudMismatch:
  1470.  
  1471.             if(Reset)
  1472.                 *Reset = TRUE;
  1473.  
  1474.             return(LocaleString(MSG_SERIAL_ERROR_BAUDMISMATCH_TXT));
  1475.  
  1476.         case SerErr_BufErr:
  1477.  
  1478.             if(Reset)
  1479.                 *Reset = TRUE;
  1480.  
  1481.             return(LocaleString(MSG_SERIAL_ERROR_BUFERR_TXT));
  1482.  
  1483.         case SerErr_InvParam:
  1484.  
  1485.             if(Reset)
  1486.                 *Reset = TRUE;
  1487.  
  1488.             return(LocaleString(MSG_SERIAL_ERROR_INVPARAM_TXT));
  1489.  
  1490.         case SerErr_LineErr:
  1491.  
  1492.             return(LocaleString(MSG_SERIAL_ERROR_LINEERR_TXT));
  1493.  
  1494.         case SerErr_ParityErr:
  1495.  
  1496.             if(Reset)
  1497.                 *Reset = TRUE;
  1498.  
  1499.             return(LocaleString(MSG_SERIAL_ERROR_PARITYERR_TXT));
  1500.  
  1501.         case SerErr_TimerErr:
  1502.  
  1503.             return(LocaleString(MSG_SERIAL_ERROR_TIMERERR_TXT));
  1504.  
  1505.         case SerErr_BufOverflow:
  1506.  
  1507.             if(Reset)
  1508.                 *Reset = TRUE;
  1509.  
  1510.             return(LocaleString(MSG_SERIAL_ERROR_BUFOVERFLOW_TXT));
  1511.  
  1512.         case SerErr_NoDSR:
  1513.  
  1514.             return(LocaleString(MSG_SERIAL_ERROR_NODSR_TXT));
  1515.  
  1516.     /*    case SerErr_UnitBusy:    */
  1517.         case IOERR_UNITBUSY:
  1518.         case 16:
  1519.  
  1520.             return(LocaleString(MSG_SERIAL_ERROR_UNIT_BUSY_TXT));
  1521.  
  1522.         default:
  1523.  
  1524.             return(NULL);
  1525.     }
  1526. }
  1527.  
  1528.     /* CreateSerial():
  1529.      *
  1530.      *    Create handles for the serial device and open it.
  1531.      */
  1532.  
  1533. STRPTR
  1534. CreateSerial()
  1535. {
  1536.     struct MsgPort *WritePort;
  1537.  
  1538.         /* If OwnDevUnit.library is available, try to lock
  1539.          * the serial driver.
  1540.          */
  1541.  
  1542. Start:    if(OwnDevUnitBase && Config -> SerialConfig -> UseOwnDevUnit)
  1543.     {
  1544.         STRPTR Error;
  1545.  
  1546.             /* Allocate a signal for OwnDevUnit, in case we might need it. */
  1547.  
  1548.         OwnDevBit = AllocSignal(-1);
  1549.  
  1550.             /* Attempt to lock the device unit... */
  1551.  
  1552.         if(Error = AttemptDevUnit(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,TermIDString,OwnDevBit == -1 ? NULL : OwnDevBit))
  1553.         {
  1554.                 /* Check for error type if any. */
  1555.  
  1556.             if(!Strnicmp(Error,ODUERR_LEADCHAR,1))
  1557.                 SPrintf(SharedBuffer,LocaleString(MSG_SERIAL_ERROR_ACCESSING_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,&Error[1]);
  1558.             else
  1559.                 SPrintf(SharedBuffer,LocaleString(MSG_SERIAL_DEVICE_IN_USE_TXT),Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,Error);
  1560.  
  1561.             SerialDevice[0] = 0;
  1562.  
  1563.             UnitNumber = -1;
  1564.  
  1565.             SerialLocked = FALSE;
  1566.  
  1567.             return(SharedBuffer);
  1568.         }
  1569.         else
  1570.         {
  1571.             strcpy(SerialDevice,Config -> SerialConfig -> SerialDevice);
  1572.  
  1573.             UnitNumber = Config -> SerialConfig -> UnitNumber;
  1574.  
  1575.             SerialLocked = TRUE;
  1576.         }
  1577.     }
  1578.     else
  1579.         SerialLocked = FALSE;
  1580.  
  1581.     Update_CR_LF_Translation();
  1582.  
  1583.     if(ReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1584.     {
  1585.         if(StripBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1586.         {
  1587.             if(XProtocolBase && (TransferBits & XPRS_HOSTNOWAIT))
  1588.                 HostReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY);
  1589.             else
  1590.                 HostReadBuffer = NULL;
  1591.  
  1592.             if(ReadPort = (struct MsgPort *)CreateMsgPort())
  1593.             {
  1594.                 if(ReadRequest = (struct IOExtSer *)CreateIORequest(ReadPort,sizeof(struct IOExtSer)))
  1595.                 {
  1596.                     LONG Error;
  1597.  
  1598.                     SetFlags(TRUE);
  1599.  
  1600.                     if(!(Error = OpenDevice(Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber,ReadRequest,0)))
  1601.                     {
  1602.                         if(WritePort = (struct MsgPort *)CreateMsgPort())
  1603.                         {
  1604.                             if(WriteRequest = (struct IOExtSer *)CreateIORequest(WritePort,sizeof(struct IOExtSer)))
  1605.                             {
  1606.                                 CopyMem(ReadRequest,WriteRequest,sizeof(struct IOExtSer));
  1607.  
  1608.                                 WriteRequest -> IOSer . io_Message . mn_ReplyPort = WritePort;
  1609.  
  1610.                                 ResetSerialRead();
  1611.                                 ResetSerialWrite();
  1612.  
  1613.                                 SetFlags(TRUE);
  1614.                                 SetFlags(FALSE);
  1615.  
  1616.                                 /* If RTS/CTS (7 wire handshaking) is
  1617.                                  * selected, have a look at the DSR
  1618.                                  * line to see whether the modem
  1619.                                  * connected is willing to support
  1620.                                  * this handshaking mode.
  1621.                                  */
  1622.  
  1623.                                 if(Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_RTSCTS_DSR)
  1624.                                 {
  1625.                                         /* If the line happens to
  1626.                                          * be high, there is no
  1627.                                          * DSR signal present.
  1628.                                          */
  1629.  
  1630. Retry:                                    if(GetSerialStatus() & CIAF_COMDSR)
  1631.                                     {
  1632.                                         if(MyEasyRequest(Window,LocaleString(MSG_SERIAL_NO_DSR_SIGNAL_TXT),LocaleString(MSG_SERIAL_RETRY_CANCEL_TXT)))
  1633.                                             goto Retry;
  1634.                                         else
  1635.                                         {
  1636.                                             DeleteSerial();
  1637.  
  1638.                                             Config -> SerialConfig -> HandshakingProtocol = HANDSHAKING_NONE;
  1639.  
  1640.                                             SerialMessage = LocaleString(MSG_SERIAL_NO_DSR_SIGNAL_HANDSHAKING_DISABLED_TXT);
  1641.  
  1642.                                             goto Start;
  1643.                                         }
  1644.                                     }
  1645.                                 }
  1646.  
  1647.                                 RestartSerial(FALSE);
  1648.  
  1649.                                 return(NULL);
  1650.                             }
  1651.                         }
  1652.                         else
  1653.                         {
  1654.                             SerialMessage = NULL;
  1655.  
  1656.                             DeleteSerial();
  1657.  
  1658.                             return(LocaleString(MSG_SERIAL_FAILED_TO_CREATE_WRITE_PORT_TXT));
  1659.                         }
  1660.                     }
  1661.                     else
  1662.                     {
  1663.                         STRPTR String;
  1664.  
  1665.                         ReadRequest -> IOSer . io_Device = NULL;
  1666.  
  1667.                         DeleteSerial();
  1668.  
  1669.                         SerialMessage = NULL;
  1670.  
  1671.                         if(!(String = GetSerialError(Error,NULL)))
  1672.                             String = LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT);
  1673.  
  1674.                         SPrintf(SharedBuffer,String,Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber);
  1675.  
  1676.                         return(SharedBuffer);
  1677.                     }
  1678.                 }
  1679.             }
  1680.             else
  1681.             {
  1682.                 SerialMessage = NULL;
  1683.  
  1684.                 DeleteSerial();
  1685.  
  1686.                 return(LocaleString(MSG_SERIAL_FAILED_TO_CREATE_READ_PORT_TXT));
  1687.             }
  1688.         }
  1689.     }
  1690.  
  1691.     SerialMessage = NULL;
  1692.  
  1693.     DeleteSerial();
  1694.  
  1695.     return(LocaleString(MSG_SERIAL_NOT_ENOUGH_MEMORY_TXT));
  1696. }
  1697.  
  1698.     /* ReconfigureSerial(struct Window *Window,struct SerialSettings *SerialConfig):
  1699.      *
  1700.      *    Reconfigure the serial driver according to the new
  1701.      *    serial settings.
  1702.      */
  1703.  
  1704. BYTE __regargs
  1705. ReconfigureSerial(struct Window *Window,struct SerialSettings *SerialConfig)
  1706. {
  1707.     BYTE    Success    = RECONFIGURE_NOCHANGE,
  1708.         IsNew;
  1709.  
  1710.         /* Are new settings to be installed or have they already
  1711.          * been copied to the correct locations?
  1712.          */
  1713.  
  1714.     if(SerialConfig)
  1715.     {
  1716.         if(memcmp(Config -> SerialConfig,SerialConfig,sizeof(struct SerialSettings)))
  1717.             IsNew = TRUE;
  1718.         else
  1719.             IsNew = FALSE;
  1720.     }
  1721.     else
  1722.     {
  1723.         if(memcmp(Config -> SerialConfig,PrivateConfig -> SerialConfig,sizeof(struct SerialSettings)))
  1724.             IsNew = TRUE;
  1725.         else
  1726.             IsNew = FALSE;
  1727.     }
  1728.  
  1729.         /* Any changes in the serial configuration area? */
  1730.  
  1731.     if(IsNew)
  1732.     {
  1733.         BYTE SameDevice = TRUE;
  1734.  
  1735.             /* Any new data to swap in? */
  1736.  
  1737.         if(SerialConfig)
  1738.         {
  1739.                 /* Store the previous settings. */
  1740.  
  1741.             SaveConfig(Config,PrivateConfig);
  1742.  
  1743.                 /* Install the new settings. */
  1744.  
  1745.             memcpy(Config -> SerialConfig,SerialConfig,sizeof(struct SerialSettings));
  1746.         }
  1747.  
  1748.             /* Any device name or unit number change? */
  1749.  
  1750.         if(strcmp(PrivateConfig -> SerialConfig -> SerialDevice,Config -> SerialConfig -> SerialDevice) || PrivateConfig -> SerialConfig -> UnitNumber != Config -> SerialConfig -> UnitNumber || PrivateConfig -> SerialConfig -> UseOwnDevUnit != Config -> SerialConfig -> UseOwnDevUnit)
  1751.             SameDevice = FALSE;
  1752.         else
  1753.         {
  1754.                 /* Handshaking mode changed to RTS/CTS protocol? */
  1755.  
  1756.             if((PrivateConfig -> SerialConfig -> HandshakingProtocol == HANDSHAKING_NONE && Config -> SerialConfig -> HandshakingProtocol != HANDSHAKING_NONE) || (PrivateConfig -> SerialConfig -> HandshakingProtocol != HANDSHAKING_NONE && Config -> SerialConfig -> HandshakingProtocol == HANDSHAKING_NONE))
  1757.                 SameDevice = FALSE;
  1758.         }
  1759.  
  1760.             /* Stop any IO activity. */
  1761.  
  1762.         if(ReadRequest)
  1763.             ClearSerial();
  1764.         else
  1765.             SameDevice = FALSE;
  1766.  
  1767.             /* No dramatic changes? Simply change the parameters. */
  1768.  
  1769.         if(SameDevice)
  1770.         {
  1771.             LONG Error;
  1772.  
  1773.             if(PrivateConfig -> SerialConfig -> SerialBufferSize != Config -> SerialConfig -> SerialBufferSize)
  1774.             {
  1775.                 STRPTR NewReadBuffer;
  1776.  
  1777.                 if(NewReadBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1778.                 {
  1779.                     STRPTR NewStripBuffer;
  1780.  
  1781.                     if(NewStripBuffer = AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY))
  1782.                     {
  1783.                         FreeVecPooled(ReadBuffer);
  1784.                         FreeVecPooled(StripBuffer);
  1785.  
  1786.                         ReadBuffer    = NewReadBuffer;
  1787.                         StripBuffer    = NewStripBuffer;
  1788.                     }
  1789.                     else
  1790.                     {
  1791.                         FreeVecPooled(NewReadBuffer);
  1792.  
  1793.                         Config -> SerialConfig -> SerialBufferSize = PrivateConfig -> SerialConfig -> SerialBufferSize;
  1794.                     }
  1795.                 }
  1796.                 else
  1797.                     Config -> SerialConfig -> SerialBufferSize = PrivateConfig -> SerialConfig -> SerialBufferSize;
  1798.             }
  1799.  
  1800.             Update_CR_LF_Translation();
  1801.  
  1802.                 /* Use new parameters... */
  1803.  
  1804.             if(!(Error = SetFlags(FALSE)))
  1805.                 Error = SetFlags(TRUE);
  1806.  
  1807.                 /* Trouble? */
  1808.  
  1809.             if(Error)
  1810.             {
  1811.                 STRPTR    String;
  1812.                 BYTE    Reset;
  1813.  
  1814.                     /* Query the error message. */
  1815.  
  1816.                 if(!(String = GetSerialError(Error,&Reset)))
  1817.                     String = LocaleString(MSG_SERIAL_ERROR_DEVBUSY_TXT);
  1818.  
  1819.                     /* Build the device name/unit number string. */
  1820.  
  1821.                 SPrintf(SharedBuffer,String,Config -> SerialConfig -> SerialDevice,Config -> SerialConfig -> UnitNumber);
  1822.  
  1823.                     /* Display the error requester. */
  1824.  
  1825.                 LT_LockWindow(Window);
  1826.  
  1827.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SharedBuffer);
  1828.  
  1829.                 LT_UnlockWindow(Window);
  1830.  
  1831.                     /* Is a serial driver reset required? */
  1832.  
  1833.                 if(Reset)
  1834.                 {
  1835.                         /* Execute the reset command. */
  1836.  
  1837.                     DoSerialCmd(CMD_RESET);
  1838.  
  1839.                         /* Copy the serial driver settings
  1840.                          * to the global configuration.
  1841.                          */
  1842.  
  1843.                     CopyWriteFlags();
  1844.  
  1845.                         /* Also set the read request driver
  1846.                          * flags.
  1847.                          */
  1848.  
  1849.                     SetFlags(TRUE);
  1850.                 }
  1851.             }
  1852.  
  1853.                 /* Restart read request. */
  1854.  
  1855.             RestartSerial(FALSE);
  1856.         }
  1857.         else
  1858.         {
  1859.             STRPTR Error;
  1860.  
  1861.                 /* Shut down the serial driver. */
  1862.  
  1863.             DeleteSerial();
  1864.  
  1865.                 /* Reinitialize the serial driver. */
  1866.  
  1867.             if(!(Error = CreateSerial()))
  1868.             {
  1869.                     /* Free the work buffer. */
  1870.  
  1871.                 if(StripBuffer)
  1872.                     FreeVecPooled(StripBuffer);
  1873.  
  1874.                     /* Try to allocate a new serial data work buffer. */
  1875.  
  1876.                 if(!(StripBuffer = (STRPTR)AllocVecPooled(Config -> SerialConfig -> SerialBufferSize,MEMF_ANY)))
  1877.                     Error = LocaleString(MSG_GLOBAL_NO_AUX_BUFFERS_TXT);
  1878.             }
  1879.  
  1880.                 /* Trouble? */
  1881.  
  1882.             if(Error)
  1883.             {
  1884.                     /* Display the error requester. */
  1885.  
  1886.                 LT_LockWindow(Window);
  1887.  
  1888.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),Error);
  1889.  
  1890.                     /* Clean up the mess :-( */
  1891.  
  1892.                 DeleteSerial();
  1893.  
  1894.                 LT_UnlockWindow(Window);
  1895.  
  1896.                 Success = RECONFIGURE_FAILURE;
  1897.             }
  1898.             else
  1899.             {
  1900.                     /* Are we to display an error message? */
  1901.  
  1902.                 if(SerialMessage)
  1903.                 {
  1904.                     MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  1905.  
  1906.                     SerialMessage = NULL;
  1907.                 }
  1908.             }
  1909.         }
  1910.     }
  1911.  
  1912.     return(Success);
  1913. }
  1914.  
  1915.     /* ReopenSerial():
  1916.      *
  1917.      *    Reopen the serial driver.
  1918.      */
  1919.  
  1920. VOID
  1921. ReopenSerial()
  1922. {
  1923.     BYTE    SerialClosed = TRUE;
  1924.     STRPTR    Error;
  1925.  
  1926.     do
  1927.     {
  1928.         if(Error = CreateSerial())
  1929.         {
  1930.             DeleteSerial();
  1931.  
  1932.             switch(MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_TERMMAIN_RETRY_IGNORE_QUIT_TXT),Error))
  1933.             {
  1934.                 case 2:    SerialClosed = FALSE;
  1935.                     break;
  1936.  
  1937.                 case 0:    MainTerminated = TRUE;
  1938.                     break;
  1939.             }
  1940.         }
  1941.         else
  1942.         {
  1943.             SerialClosed = FALSE;
  1944.  
  1945.             if(SerialMessage)
  1946.             {
  1947.                 MyEasyRequest(Window,LocaleString(MSG_GLOBAL_TERM_HAS_A_PROBLEM_TXT),LocaleString(MSG_GLOBAL_CONTINUE_TXT),SerialMessage);
  1948.  
  1949.                 SerialMessage = NULL;
  1950.             }
  1951.         }
  1952.     }
  1953.     while(SerialClosed);
  1954. }
  1955.  
  1956.     /* HandleSerial():
  1957.      *
  1958.      *    Handle the data coming in from the serial line.
  1959.      */
  1960.  
  1961. BYTE
  1962. HandleSerial()
  1963. {
  1964.     BYTE MoreData = FALSE;
  1965.  
  1966.     if(ReadPort && !ReleaseSerial && Status != STATUS_HOLDING)
  1967.     {
  1968.         if(HostReadBuffer)
  1969.         {
  1970.             LONG Length = XProtocolHostMon(XprIO,HostReadBuffer,0,SerialBufferSize);
  1971.  
  1972.             if(Translate_CR_LF)
  1973.                 Length = (* Translate_CR_LF)(HostReadBuffer,Length);
  1974.  
  1975.             if(Length)
  1976.             {
  1977.                 if(Marking)
  1978.                     DropMarker();
  1979.  
  1980.                 ConProcess(HostReadBuffer,Length);
  1981.  
  1982.                 if(Status == STATUS_HOLDING)
  1983.                     return(FALSE);
  1984.             }
  1985.  
  1986.             MoreData = TRUE;
  1987.         }
  1988.  
  1989.         if(DataHold)
  1990.         {
  1991.             register STRPTR    Data    = DataHold;
  1992.             register LONG    Size    = DataSize;
  1993.  
  1994.             DataHold = NULL;
  1995.  
  1996.             if(Marking)
  1997.                 DropMarker();
  1998.  
  1999.             (* ConTransfer)(Data,Size);
  2000.  
  2001.             MoreData = TRUE;
  2002.  
  2003.             RestartSerial(FALSE);
  2004.  
  2005.             return(MoreData);
  2006.         }
  2007.  
  2008.             /* Any news? */
  2009.  
  2010.         if(CheckSerialRead())
  2011.         {
  2012.             MoreData = TRUE;
  2013.  
  2014.             if(!WaitSerialRead())
  2015.             {
  2016.                 if(Marking)
  2017.                     DropMarker();
  2018.  
  2019.                 BytesIn++;
  2020.  
  2021.                 if(Translate_CR_LF)
  2022.                 {
  2023.                     register LONG Size = (* Translate_CR_LF)(ReadBuffer,1);
  2024.  
  2025.                     if(Size)
  2026.                         (* ConTransfer)(ReadBuffer,Size);
  2027.                 }
  2028.                 else
  2029.                     (* ConTransfer)(ReadBuffer,1);
  2030.  
  2031.                 if(Status != STATUS_HOLDING && !DataHold)
  2032.                 {
  2033.                     ULONG Length;
  2034.  
  2035.                         /* Check how many bytes are still in
  2036.                          * the serial buffer.
  2037.                          */
  2038.  
  2039.                     if(Length = GetSerialWaiting())
  2040.                     {
  2041.                         ULONG Max = SerialBufferSize;
  2042.  
  2043.                         if(Max > Config -> SerialConfig -> Quantum)
  2044.                             Max = Config -> SerialConfig -> Quantum;
  2045.  
  2046.                         if(Length > Max)
  2047.                             Length = Max;
  2048.  
  2049.                         if(!DoSerialRead(ReadBuffer,Length))
  2050.                         {
  2051.                             BytesIn += Length;
  2052.  
  2053.                             if(Translate_CR_LF)
  2054.                             {
  2055.                                 if(Length = (* Translate_CR_LF)(ReadBuffer,Length))
  2056.                                     (* ConTransfer)(ReadBuffer,Length);
  2057.                             }
  2058.                             else
  2059.                                 (* ConTransfer)(ReadBuffer,Length);
  2060.                         }
  2061.                     }
  2062.                 }
  2063.             }
  2064.  
  2065.             if(DataHold)
  2066.                 ClrSignal(SIG_SERIAL);
  2067.             else
  2068.                 RestartSerial(FALSE);
  2069.         }
  2070.     }
  2071.  
  2072.     return(MoreData);
  2073. }
  2074.