home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff369.lzh / VaxTerm / src / main.c < prev    next >
C/C++ Source or Header  |  1990-08-15  |  23KB  |  659 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <exec/io.h>
  6. #include <devices/keymap.h>
  7. #include <devices/serial.h>
  8. #include <devices/console.h>
  9. #include <devices/audio.h>
  10. #include <libraries/dos.h>
  11. #include <hardware/blit.h>
  12. #include <graphics/sprite.h>
  13. #include <intuition/preferences.h>
  14. #include <intuition/intuition.h>
  15. #include <proto/intuition.h>
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18. #include <proto/graphics.h>
  19.  
  20. #include "main.h"
  21.  
  22. #include <graphics/gfxbase.h>
  23.  
  24. char infotext[] = 
  25.     "\x1b#3\x9b""1mV A X t e r m\x9b""0m  v2.4   "
  26.     "\x9b""3mBy T. Mickelsson\x9b""0m\x0D\n"
  27.     "\x1b#4\x9b""1mV A X t e r m\x9b""0m  v2.4   "
  28.     "\x9b""3mBy T. Mickelsson\x9b""0m\x0D\n"
  29.     "\n\x1b#6\x9b""1mVMS \x9b""22moriented \x9b""1m\x9b""4m"
  30.     "VT220 Terminal Emulator""\x9b""22m\x9b""24m\x0D\n\n"
  31.     "\x9b""4mSpecial keys\x9b""0m\x0D\n\n"
  32.     "\x9b""1mF1\x9b""0m        hold/release screen\x0D\n"
  33.     "\x9b""1mF2\x9b""0m        printer on/off\x0D\n"
  34.     "\x9b""1mF3\x9b""0m        set-up\x0D\n"
  35.     "\x9b""1mF4\x9b""0m        file transfers/host set-up/quit\x0D\n"
  36.     "\x9b""1mF5\x9b""0m        break\x0D\n\n"
  37.     "Right mouse button toggles the mouse pointer and screen gadget.\x0D\n\n"
  38.     "When the printer will be activated for the first time, "
  39.     "file PRINTER.SETUP will\x0D\n"
  40.     "be sent to printer to modify the default printer settings.\x0D\n\n"
  41.     "The commands for the host set-up will be read from the "
  42.     "HOST.SETUP file.\x0D\n\n"
  43.     "File transfers will work only when the terminal emulator "
  44.     "is logged into the\x0D\n"
  45.     "VMS-system. The system must be ready to accept DCL-command from "
  46.     "the terminal.\x0D\n\n";
  47.  
  48. struct NewScreen nscr = {
  49.     0,0,WIDTH,0,DEPTH,
  50.     BACKGROUND_PEN,FOREGROUND_PEN,HIRES,CUSTOMSCREEN,
  51.     NULL,(UBYTE *)"Right mouse button to toggles this title!  "
  52.     "Use F4 and 'Q' keys to quit!",NULL,NULL };
  53.  
  54. struct NewWindow nwin = {
  55.     0,0,WIDTH,0,FOREGROUND_PEN,FOREGROUND_PEN,
  56.     MOUSEBUTTONS|ACTIVEWINDOW|INACTIVEWINDOW,
  57.     SMART_REFRESH|BACKDROP|BORDERLESS|ACTIVATE|NOCAREREFRESH|RMBTRAP,
  58.     NULL,NULL,NULL,NULL,NULL,0,0,0,0,CUSTOMSCREEN };
  59.  
  60. struct Library *IntuitionBase;
  61. struct SimpleSprite sp = { NULL,YSIZE,0,0,-1 };
  62.  
  63. USHORT  rows;
  64. char    line[80],cdir[64],fdir[64];
  65.  
  66. void _main(char *argv)
  67. {
  68. struct GfxBase          *gb;
  69. struct console          console,*con;
  70. struct serial           serial,*ser;
  71. register LONG           len;
  72. char                    *cp;
  73. BPTR                    fp;
  74. USHORT                  stat;
  75.  
  76. if (*argv) {
  77.     if (cp = strchr(argv,' ')) *cp = '\0';
  78.     cp = argv;
  79.     cp = cp + strlen(cp) - 1;
  80.     while((*cp != ':')&&(*cp != '/')) {
  81.         if (cp == argv) {
  82.             cp--;
  83.             break;
  84.             }
  85.         cp--;
  86.         }
  87.     cp++;
  88.     *cp = '\0';
  89.     getcd(0,cdir);
  90.     chdir(argv);
  91.     getcd(0,fdir);
  92.     chdir(cdir);
  93.     }
  94. else getcd(0,fdir);
  95. strcpy(line,"c:assign VTDIR: \"");
  96. strcat(line,fdir);
  97. strcat(line,"\"");
  98. if (fp = OPEN_FOR_WRITING(NILDEVICE)) {
  99.     Execute(line,NULL,fp);
  100.     CLOSEFILE(fp);
  101.     }
  102. con = &console;
  103. ser = &serial;
  104. con->sp = &sp;
  105. con->ser = ser;
  106. if ((gb=(struct GfxBase *)OpenLibrary("graphics.library",0)) == NULL) return;
  107. rows = gb->NormalDisplayRows / YSIZE;
  108. nscr.Height = rows * YSIZE;
  109. nwin.Height = rows * YSIZE;
  110. CloseLibrary((struct Library *)gb);
  111. if ((IntuitionBase = OpenLibrary("intuition.library",0)) != NULL) {
  112.     if (!(openserial(ser))) {
  113.         if (fp = OPEN_FOR_WRITING(NILDEVICE)) {
  114.             Execute("Assign FONTS: VTDIR:fonts",NULL,fp);
  115.             CLOSEFILE(fp);
  116.             }
  117.         stat = openconsole(con);
  118.         if (fp = OPEN_FOR_WRITING(NILDEVICE)) {
  119.             Execute("Assign FONTS: sys:fonts",NULL,fp);
  120.             CLOSEFILE(fp);
  121.             }
  122.         if (!stat) {
  123.             strcpy(con->buf,infotext);
  124.             interpret(con,strlen(con->buf));
  125.             readsetup(con);
  126.             while (con->gstat & ON) {
  127.                 len = readconsole(con);
  128.                 if (len) writeserial(con,con->buf,len);
  129.                 if (!(con->gstat & HOLD)) {
  130.                     len = readserial(con,1);
  131.                     if (len) {
  132.                         interpret(con,len);
  133.                         if (con->gstat & PRINT) WRITE(con->prt,con->buf,len);
  134.                         }
  135.                     else cursorout(con);
  136.                     }
  137.                 if (con->gstat & SEND_BREAK) {
  138.                     con->gstat &= (MASK - SEND_BREAK);
  139.                     send_break(ser);
  140.                     }
  141.                 else if (con->gstat & TRANSFER) {
  142.                     con->gstat &= (MASK - TRANSFER);
  143.                     transfer(con);
  144.                     }
  145.                 else if (con->gstat & SETUP) {
  146.                     con->gstat &= (MASK - SETUP);
  147.                     setup(con);
  148.                     }
  149.                 }
  150.             closeconsole(con);
  151.             }
  152.         closeserial(ser);
  153.         }
  154.     CloseLibrary(IntuitionBase);
  155.     }
  156. }
  157.  
  158. readconsole(register struct console *con)
  159. {
  160. register struct IOStdReq    *rreq;
  161. register USHORT             len;
  162. register char               *cp;
  163. struct IntuiMessage         *mes;
  164. ULONG                       class;
  165. USHORT                      code;
  166.  
  167. len = 0;
  168. if (!(con->gstat & LOCKED)) {
  169.     if (GetMsg(con->rport) != NULL) {
  170.         cp = con->buf;
  171.         *cp++ = con->character;
  172.         len = 1;
  173.         rreq = con->rreq;
  174.         rreq->io_Flags |= IOF_QUICK;
  175.         while (len < (BUFSIZE - SECSIZE)) {
  176.             BeginIO((struct IORequest *)rreq);
  177.             if (!(rreq->io_Flags & IOF_QUICK)) return((int)len);
  178.             *cp++ = con->character;
  179.             len++;
  180.             }
  181.         }
  182.     }
  183. if (mes = (struct IntuiMessage *)GetMsg(con->win->UserPort)) {
  184.     class = mes->Class;
  185.     code = mes->Code;
  186.     ReplyMsg((struct Message *)mes);
  187.     switch (class) {
  188.         case MOUSEBUTTONS:
  189.             switch(code) {
  190.                 case MENUUP:
  191.                     if (con->gstat & GAD) {
  192.                         ShowTitle(con->scr,FALSE);
  193.                         SetPointer(con->win,con->dummy,PYSIZE,PXSIZE,0,0);
  194.                         con->gstat &= (MASK - GAD);
  195.                         }
  196.                     else {
  197.                         ShowTitle(con->scr,TRUE);
  198.                         ClearPointer(con->win);
  199.                         con->gstat |= GAD;
  200.                         }
  201.                     break;
  202.                 default: ;
  203.                 }
  204.             break;
  205.         case INACTIVEWINDOW:
  206.             SetRast(&con->srp,0);
  207.             break;
  208.         case ACTIVEWINDOW:
  209.             initcursor(con);
  210.             DRAWCURSOR(con->rp,&con->srp,con->row,con->col);
  211.             break;
  212.         default: ;
  213.         }
  214.     }
  215. return((LONG)len);
  216. }
  217.  
  218. openconsole(register struct console *con)
  219. {
  220. allocaudio(con);
  221. if ((con->wport = CreatePort("console_write",0)) != NULL) {
  222.     if ((con->wreq = CreateStdIO(con->wport)) != NULL) {
  223.         if ((con->rport = CreatePort("console_read",0)) != NULL) {
  224.             if ((con->rreq = CreateStdIO(con->rport)) != NULL) {
  225.                 if ((con->scr = OpenScreen(&nscr)) != NULL) {
  226.                     ShowTitle(con->scr,FALSE);
  227.                     nwin.Screen = con->scr;
  228.                     if ((con->win = OpenWindow(&nwin)) != NULL) {
  229.                         con->wreq->io_Data = (APTR)con->win;
  230.                         con->wreq->io_Length = sizeof(*con->win);
  231.                         if (!(OpenDevice("console.device",0,
  232.                                         (struct IORequest *)con->wreq,0))) {
  233.                             con->wreq->io_Command = CD_SETKEYMAP;
  234.                             con->wreq->io_Length = sizeof(struct KeyMap);
  235.                             con->wreq->io_Data = (APTR)&keymap;
  236.                             DoIO((struct IORequest *)con->wreq);
  237.                             con->wreq->io_Command = CMD_WRITE;
  238.                             con->rreq->io_Command = CMD_READ;
  239.                             con->rreq->io_Data = (APTR)&con->character;
  240.                             con->rreq->io_Length = 1;
  241.                             con->rreq->io_Device = con->wreq->io_Device;
  242.                             con->rreq->io_Unit = con->wreq->io_Unit;
  243.                             if (!(allocmem(con))) {
  244.                                 SetPointer(con->win,con->dummy,
  245.                                             PYSIZE,PXSIZE,0,0);
  246.                                 con->rp = con->win->RPort;
  247.                                 if (!(openfonts(con))) {
  248.                                     SendIO((struct IORequest *)con->rreq);
  249.                                     InitBitMap(&con->sbm,1,4 * XSIZE,YSIZE);
  250.                                     InitRastPort(&con->srp);
  251.                                     con->srp.BitMap = &con->sbm;
  252.                                     initcursor(con);
  253.                                     if (GetSprite(con->sp,3) != -1) {
  254.                                         SetDrMd(con->rp,JAM2);
  255.                                         con->prt = NULL;
  256.                                         reset(con);
  257.                                         return(0);
  258.                                         }
  259.                                     closefonts(con);
  260.                                     }
  261.                                 ClearPointer(con->win);
  262.                                 freemem(con);
  263.                                 }
  264.                             CloseDevice((struct IORequest *)con->wreq);
  265.                             }
  266.                         CloseWindow(con->win);
  267.                         }
  268.                     CloseScreen(con->scr);
  269.                     }
  270.                 DeleteStdIO(con->rreq);
  271.                 }
  272.             DeletePort(con->rport);
  273.             }
  274.         DeleteStdIO(con->wreq);
  275.         }
  276.     DeletePort(con->wport);
  277.     }
  278. return(-1);
  279. }
  280.  
  281. initcursor(register struct console *con)
  282. {
  283. register USHORT *wpt,cnt;
  284.  
  285. wpt = con->dat;
  286. *wpt++ = 0;
  287. *wpt++ = 0;
  288. con->sbm.Planes[0] = (PLANEPTR)wpt;
  289. for (cnt = 0; cnt < YSIZE * 2; cnt++) *wpt++ = 0x7800;
  290. *wpt++ = 0xFFFF;
  291. *wpt = 0xFF7F;
  292. con->sp->posctldata = con->dat;
  293. return(0);
  294. }
  295.  
  296. closeconsole(register struct console *con)
  297. {
  298. freeaudio(con);
  299. if (con->prt) CLOSEFILE(con->prt);
  300. FreeSprite(con->sp->num);
  301. closefonts(con);
  302. if (!(con->gstat & GAD)) ClearPointer(con->win);
  303. freemem(con);
  304. CloseDevice((struct IORequest *)con->wreq);
  305. CloseWindow(con->win);
  306. CloseScreen(con->scr);
  307. DeleteStdIO(con->rreq);
  308. DeletePort(con->rport);
  309. DeleteStdIO(con->wreq);
  310. DeletePort(con->wport);
  311. return(0);
  312. }
  313.  
  314. allocmem(register struct console *con)
  315. {
  316. if ((con->dummy = (UWORD *)AllocMem(DUMMYSIZE,MEMF_CHIP|MEMF_CLEAR))!=NULL) {
  317.     if ((con->dat = (UWORD *)AllocMem(SPRITESIZE,MEMF_CHIP)) != NULL) {
  318.         if ((con->conv = AllocMem(CONVSIZE,0)) != NULL) {
  319.             if ((con->buf = AllocMem(BUFSIZE,0)) != NULL) {
  320.                 if ((con->rows = (UBYTE *)AllocMem(rows + 1,0)) != NULL)
  321.                     return(0);
  322.                 FreeMem(con->conv,CONVSIZE);
  323.                 }
  324.             FreeMem(con->buf,BUFSIZE);
  325.             }
  326.         FreeMem((char *)con->dat,SPRITESIZE);
  327.         }
  328.     FreeMem((char *)con->dummy,DUMMYSIZE);
  329.     }
  330. return(-1);
  331. }
  332.  
  333. freemem(register struct console *con)
  334. {
  335. FreeMem((char *)con->rows,rows + 1);
  336. FreeMem(con->conv,CONVSIZE);
  337. FreeMem(con->buf,BUFSIZE);
  338. FreeMem((char *)con->dat,SPRITESIZE);
  339. FreeMem((char *)con->dummy,DUMMYSIZE);
  340. return(0);
  341. }
  342.  
  343. static UBYTE alloc[] = { 15,14,13,11,7,3,5,6,9,10,12,8,4,2,1 };
  344. static UBYTE wave[] = { 0,63,127,63,0,-64,-128,-64 };
  345.  
  346. beep(struct console *con)
  347. {
  348. register struct IOAudio *audio = con->audio;
  349. register UBYTE          *temp;
  350.  
  351. if (audio != NULL) {
  352.     audio->ioa_Request.io_Unit = con->channels;
  353.     if (con->sound) {
  354.         if (GetMsg(audio->ioa_Request.io_Message.mn_ReplyPort) == NULL)
  355.             return(-1);
  356.         }
  357.     BeginIO((struct IORequest *)audio);
  358.     if (audio->ioa_Request.io_Error) {
  359.         while (GetMsg(audio->ioa_Request.io_Message.mn_ReplyPort) == NULL);
  360.         temp = audio->ioa_Data;
  361.         audio->ioa_Request.io_Command = ADCMD_ALLOCATE;
  362.         audio->ioa_Request.io_Flags = ADIOF_NOWAIT | IOF_QUICK;
  363.         audio->ioa_Data = alloc;
  364.         audio->ioa_Length = sizeof(alloc);
  365.         BeginIO((struct IORequest *)audio);
  366.         audio->ioa_Request.io_Command = CMD_WRITE;
  367.         audio->ioa_Request.io_Flags = ADIOF_PERVOL;
  368.         audio->ioa_Data = temp;
  369.         audio->ioa_Length = sizeof(wave);
  370.         if (audio->ioa_Request.io_Error) con->sound = 0;
  371.         else {
  372.             con->channels = audio->ioa_Request.io_Unit;
  373.             BeginIO((struct IORequest *)audio);
  374.             con->sound = 1;
  375.             }
  376.         }
  377.     else con->sound = 1;
  378.     }
  379. return(0);
  380. }
  381.  
  382. allocaudio(struct console *con)
  383. {
  384. register struct IOAudio *audio = con->audio;
  385.  
  386. audio = (struct IOAudio*)AllocMem(sizeof(struct IOAudio),MEMF_CLEAR);
  387. if (audio != NULL) {
  388.     audio->ioa_Request.io_Message.mn_ReplyPort = CreatePort("beep",0);
  389.     if (audio->ioa_Request.io_Message.mn_ReplyPort != NULL) {
  390.         audio->ioa_Request.io_Message.mn_Node.ln_Pri = BEEPPRIORITY;
  391.         audio->ioa_Data = alloc;
  392.         audio->ioa_Length = sizeof(alloc);
  393.         if (!(OpenDevice(AUDIONAME,0,(struct IORequest *)audio,0))) {
  394.             audio->ioa_Data = AllocMem(sizeof(wave),MEMF_CHIP);
  395.             if (audio->ioa_Data != NULL) {
  396.                 memcpy(audio->ioa_Data,wave,sizeof(wave));
  397.                 audio->ioa_Request.io_Command = CMD_WRITE;
  398.                 audio->ioa_Request.io_Flags = ADIOF_PERVOL;
  399.                 audio->ioa_Length = sizeof(wave);
  400.                 audio->ioa_Period = BEEPPERIOD;
  401.                 audio->ioa_Volume = BEEPVOLUME;
  402.                 audio->ioa_Cycles = BEEPCYCLES;
  403.                 con->audio = audio;
  404.                 con->channels = audio->ioa_Request.io_Unit;
  405.                 con->sound = 0;
  406.                 return(0);
  407.                 }
  408.             CloseDevice((struct IORequest *)audio);
  409.             }
  410.         DeletePort(audio->ioa_Request.io_Message.mn_ReplyPort);
  411.         }
  412.     FreeMem((char *)audio,sizeof(struct IOAudio));
  413.     }
  414. con->audio = NULL;
  415. return(-1);
  416. }
  417.  
  418. freeaudio(struct console *con)
  419. {
  420. register struct IOAudio *audio = con->audio;
  421.  
  422. if (audio != NULL) {
  423.     audio->ioa_Request.io_Unit = con->channels;
  424.     FreeMem((char *)audio->ioa_Data,sizeof(wave));
  425.     CloseDevice((struct IORequest *)audio);
  426.     DeletePort(audio->ioa_Request.io_Message.mn_ReplyPort);
  427.     FreeMem((char *)audio,sizeof(struct IOAudio));
  428.     }
  429. return(0);
  430. }
  431.  
  432. readserial(register struct console *con,SHORT linemode)
  433. {
  434. register struct serial      *ser;
  435. register struct IOExtSer    *rreq;
  436. register char               *cp,c;
  437. register LONG               len;
  438.  
  439. ser = con->ser;
  440. cp = con->buf;
  441. rreq = ser->rreq;
  442. if (ser->requested) {
  443.     if ((GetMsg(ser->rport)) == NULL) return(0);
  444.     if (rreq->IOSer.io_Error) {
  445.         ser->requested = 0;
  446.         return(0);
  447.         }
  448.     c = ser->character;
  449.     if (c == END) {
  450.         rreq->IOSer.io_Flags &= (0xFF - IOF_QUICK);
  451.         rreq->IOSer.io_Error = 0;
  452.         BeginIO((struct IORequest *)rreq);
  453.         if (rreq->IOSer.io_Error) {
  454.             GetMsg(ser->rport);
  455.             ser->requested = 0;
  456.             }
  457.         return(0);
  458.         }
  459.     *cp++ = c;
  460.     len = 1;
  461.     }
  462. else len = 0;
  463. rreq->IOSer.io_Flags |= IOF_QUICK;
  464. rreq->IOSer.io_Error = 0;
  465. ser->requested = 1;
  466. while (len < BUFSIZE) {
  467.     BeginIO((struct IORequest *)rreq);
  468.     if (rreq->IOSer.io_Error) {
  469.         GetMsg(ser->rport);
  470.         ser->requested = 0;
  471.         return(len);
  472.         }
  473.     if (!(rreq->IOSer.io_Flags & IOF_QUICK)) return(len);
  474.     c = ser->character;
  475.     if (c == END) {
  476.         rreq->IOSer.io_Flags &= (0xFF - IOF_QUICK);
  477.         BeginIO((struct IORequest *)rreq);
  478.         if (rreq->IOSer.io_Error) {
  479.             GetMsg(ser->rport);
  480.             ser->requested = 0;
  481.             }
  482.         return(len);
  483.         }
  484.     *cp++ = c;
  485.     len++;
  486.     if (linemode) {
  487.         if ((c == LF)||(c == FF)) {
  488.             rreq->IOSer.io_Flags &= (0xFF - IOF_QUICK);
  489.             BeginIO((struct IORequest *)rreq);
  490.             if (rreq->IOSer.io_Error) {
  491.                 GetMsg(ser->rport);
  492.                 ser->requested = 0;
  493.                 }
  494.             return(len);
  495.             }
  496.         }
  497.     }
  498. return(len);
  499. }
  500.  
  501. writeserial(register struct console *con,char *cp,LONG len)
  502. {
  503. register struct serial      *ser;
  504. register struct IOExtSer    *wreq;
  505.  
  506. ser = con->ser;
  507. len = convert(con,cp,len);
  508. wreq = ser->wreq;
  509. if (!(con->gstat & LOCKED)) {
  510.     wreq->IOSer.io_Length = len;
  511.     wreq->IOSer.io_Data = (APTR)con->conv;
  512.     DoIO((struct IORequest *)wreq);
  513.     }
  514. return(0);
  515. }
  516.  
  517. writeasync(register struct console *con,LONG len)
  518. {
  519. register struct serial      *ser;
  520. register struct IOExtSer    *wreq;
  521. struct IntuiMessage         *mes;
  522. ULONG                       class;
  523. USHORT                      code,clicks = 0;
  524.  
  525. ser = con->ser;
  526. wreq = ser->wreq;
  527. wreq->IOSer.io_Length = len;
  528. wreq->IOSer.io_Data = (APTR)con->conv;
  529. BeginIO((struct IORequest *)wreq);
  530. while ((GetMsg(ser->wport)) == NULL) {
  531.     readserial(con,0);
  532.     if (mes = (struct IntuiMessage *)GetMsg(con->win->UserPort)) {
  533.         class = mes->Class;
  534.         code = mes->Code;
  535.         ReplyMsg((struct Message *)mes);
  536.         if ((class == MOUSEBUTTONS)&&(code == SELECTDOWN)) {
  537.             clicks++;
  538.             if (clicks == ABORTASYNC) {
  539.                 AbortIO((struct IORequest *)wreq);
  540.                 GetMsg(ser->wport);
  541.                 return(1);
  542.                 }
  543.             Delay(ABORTDELAY);
  544.             }
  545.         }
  546.     }
  547. return(0);
  548. }
  549.  
  550. send_break(register struct serial *ser)
  551. {
  552. register struct IOExtSer    *wreq;
  553. UBYTE                       flags;
  554.  
  555. wreq = ser->wreq;
  556. flags = wreq->IOSer.io_Flags;
  557. wreq->IOSer.io_Flags &= (0xFF - (IOF_QUICK | SERF_QUEUEDBRK));
  558. wreq->IOSer.io_Command = SDCMD_BREAK;
  559. DoIO((struct IORequest *)wreq);
  560. wreq->IOSer.io_Command = CMD_WRITE;
  561. wreq->IOSer.io_Flags = flags;
  562. return(0);
  563. }
  564.  
  565. openserial(register struct serial *ser)
  566. {
  567. struct Preferences  prefs;
  568.  
  569. if ((ser->wport = CreatePort("serial_write",0)) != NULL) {
  570.     if ((ser->wreq = (struct IOExtSer *)CreateExtIO(ser->wport,
  571.                                     sizeof(struct IOExtSer))) != NULL) {
  572.         if ((ser->rport = CreatePort("serial_read",0)) != NULL) {
  573.             if ((ser->rreq = (struct IOExtSer *)CreateExtIO(ser->rport,
  574.                                     sizeof(struct IOExtSer))) != NULL) {
  575.                 if (!(OpenDevice("serial.device",1,
  576.                                     (struct IORequest *)ser->wreq,0))) {
  577.                     GetPrefs(&prefs,sizeof(struct Preferences));
  578.                     ser->rreq->IOSer.io_Device = ser->wreq->IOSer.io_Device;
  579.                     ser->rreq->IOSer.io_Unit = ser->wreq->IOSer.io_Unit;
  580.                     if (prefs.SerParShk & 0xf0)
  581.                         ser->rreq->io_SerFlags = SERF_PARTY_ON;
  582.                     else ser->rreq->io_SerFlags = 0;
  583.                     if ((prefs.SerParShk >> 4) == SPARITY_ODD)
  584.                         ser->rreq->io_SerFlags |= SERF_PARTY_ODD;
  585.                     if ((prefs.SerParShk & 0x0f) != SHSHAKE_XON)
  586.                         ser->rreq->io_SerFlags |= SERF_XDISABLED;
  587.                     switch (prefs.BaudRate) {
  588.                         case BAUD_110:      ser->rreq->io_Baud = 110;
  589.                                             break;
  590.                         case BAUD_300:      ser->rreq->io_Baud = 300;
  591.                                             break;
  592.                         case BAUD_1200:     ser->rreq->io_Baud = 1200;
  593.                                             break;
  594.                         case BAUD_2400:     ser->rreq->io_Baud = 2400;
  595.                                             break;
  596.                         case BAUD_4800:     ser->rreq->io_Baud = 4800;
  597.                                             break;
  598.                         case BAUD_9600:     ser->rreq->io_Baud = 9600;
  599.                                             break;
  600.                         case BAUD_19200:    ser->rreq->io_Baud = 19200;
  601.                                             break;
  602.                         case BAUD_MIDI:     ser->rreq->io_Baud = 32768;
  603.                                             break;
  604.                         default:            ser->rreq->io_Baud = 2400;
  605.                                             }
  606.                     ser->rreq->io_ReadLen = 8 - (prefs.SerRWBits >> 4);
  607.                     ser->rreq->io_WriteLen = 8 - (prefs.SerRWBits & 0x0f);
  608.                     ser->rreq->io_StopBits = (prefs.SerStopBuf >> 4) + 1;
  609.                     ser->rreq->io_CtlChar = 0x11130506;
  610.                     switch (prefs.SerStopBuf & 0x0f) {
  611.                         case SBUF_512:      ser->rreq->io_RBufLen = 512;
  612.                                             break;
  613.                         case SBUF_1024:     ser->rreq->io_RBufLen = 1024;
  614.                                             break;
  615.                         case SBUF_2048:     ser->rreq->io_RBufLen = 2048;
  616.                                             break;
  617.                         case SBUF_4096:     ser->rreq->io_RBufLen = 4096;
  618.                                             break;
  619.                         case SBUF_8000:     ser->rreq->io_RBufLen = 8000;
  620.                                             break;
  621.                         case SBUF_16000:    ser->rreq->io_RBufLen = 16000;
  622.                                             break;
  623.                         default:            ser->rreq->io_RBufLen = 1024;
  624.                         }
  625.                     ser->rreq->io_BrkTime = 500000L;
  626.                     ser->rreq->IOSer.io_Command = SDCMD_SETPARAMS;
  627.                     if (!(DoIO((struct IORequest *)ser->rreq))) {
  628.                         ser->rreq->IOSer.io_Command = CMD_READ;
  629.                         ser->rreq->IOSer.io_Data = (APTR)&ser->character;
  630.                         ser->rreq->IOSer.io_Length = 1;
  631.                         ser->rreq->IOSer.io_Error = 0;
  632.                         ser->wreq->IOSer.io_Command = CMD_WRITE;
  633.                         BeginIO((struct IORequest *)ser->rreq);
  634.                         ser->requested = 1;
  635.                         if (ser->rreq->IOSer.io_Error == 0) return(0);
  636.                         }
  637.                     CloseDevice((struct IORequest *)ser->wreq);
  638.                     }
  639.                 DeleteExtIO((struct IORequest *)ser->rreq);
  640.                 }
  641.             DeletePort(ser->rport);
  642.             }
  643.         DeleteExtIO((struct IORequest *)ser->wreq);
  644.         }
  645.     DeletePort(ser->wport);
  646.     }
  647. return(-1);
  648. }
  649.  
  650. closeserial(register struct serial *ser)
  651. {
  652. CloseDevice((struct IORequest *)ser->wreq);
  653. DeleteExtIO((struct IORequest *)ser->rreq);
  654. DeletePort(ser->rport);
  655. DeleteExtIO((struct IORequest *)ser->wreq);
  656. DeletePort(ser->wport);
  657. return(0);
  658. }
  659.