home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / comm / Misc / SerialRouter / SerialRouter.c < prev    next >
C/C++ Source or Header  |  1994-06-19  |  7KB  |  142 lines

  1. /*
  2.     SerialRouter 1.0 Copyright 1994 Barry McConnell.
  3.     Opens two serial devices, and copies data between them.
  4.     If the second device is console.device, creates a window for it.
  5. */
  6.  
  7. #include <exec/memory.h>
  8. #include <exec/io.h>
  9. #include <devices/serial.h>
  10. #include <intuition/intuition.h>
  11. #include <dos/dos.h>
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14. #include <proto/dos.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. void __regargs __chkabort(void) {}  /* turn off SAS/C v6 CTRL-C checking */
  20.  
  21. static UBYTE *version = "$VER: SerialRouter 1.0 (19.6.94)";
  22.  
  23. void main(int argc, char *argv[])
  24. {
  25. struct Library *IntuitionBase;
  26. struct IORequest *read1 = 0, *write1 = 0, *read2 = 0, *write2 = 0;
  27. struct MsgPort *port = 0;
  28. struct Window *window = 0;
  29. ULONG signal;
  30. char data1, data2;
  31.  
  32. if (argc == 5)  /* no defaults! */
  33.     {
  34.     if (IntuitionBase = OpenLibrary("intuition.library", 37))
  35.         {
  36.         if (port = CreateMsgPort())
  37.             {
  38.             if ((read1 = CreateIORequest(port, sizeof(struct IOExtSer)))  /* for communicating with the device */
  39.                 && (read2 = CreateIORequest(port, sizeof(struct IOExtSer)))
  40.                 && (write1 = CreateIORequest(port, sizeof(struct IOExtSer)))
  41.                 && (write2 = CreateIORequest(port, sizeof(struct IOExtSer))))
  42.                 {
  43.                 ((struct IOExtSer *)read1) -> io_SerFlags |= (SERF_RAD_BOOGIE | SERF_SHARED);  /* high-speed mode */
  44.                 if (strcmp(argv[3], "console.device"))  /* special case */
  45.                     ((struct IOExtSer *)read2) -> io_SerFlags |= (SERF_RAD_BOOGIE | SERF_SHARED);  /* probably a serial device */
  46.                 else
  47.                     {
  48.                     window = OpenWindowTags(NULL,   WA_Left, 50, WA_Top, 50, WA_Width, 600, WA_Height, 400,
  49.                                                     WA_MinWidth, 80, WA_MaxWidth, -1, WA_MinHeight, 55, WA_MaxHeight, -1,
  50.                                                     WA_Title, argv[1], WA_SizeGadget, TRUE, WA_DragBar, TRUE,
  51.                                                     WA_DepthGadget, TRUE, WA_Activate, TRUE, WA_RMBTrap, TRUE,
  52.                                                     TAG_END);
  53.                     ((struct IOStdReq *)read2) -> io_Data = window;
  54.                     }
  55.  
  56.                 if ((OpenDevice(argv[1], atoi(argv[2]), read1, NULL)) == 0)
  57.                     {
  58.                     if ((OpenDevice(argv[3], atoi(argv[4]), read2, NULL)) == 0)
  59.                         {
  60.                         printf("SerialRouter v1.0 Copyright 1994 Barry McConnell\n");
  61.                         printf("%s unit %s <-> %s unit %s\n", argv[1], argv[2], argv[3], argv[4]);
  62.                         printf("Ctrl-C exits\n");
  63.  
  64.                         ((struct IOExtSer *)read1) -> IOSer.io_Length = 1;
  65.                         ((struct IOExtSer *)read2) -> IOSer.io_Length = 1;
  66.                         CopyMem(read1, write1, sizeof(struct IOExtSer));  /* duplicate copy for writes */
  67.                         CopyMem(read2, write2, sizeof(struct IOExtSer));
  68.                         ((struct IOExtSer *)read1) -> IOSer.io_Data = &data1;
  69.                         ((struct IOExtSer *)read2) -> IOSer.io_Data = &data2;
  70.                         ((struct IOExtSer *)write1) -> IOSer.io_Data = &data2;
  71.                         ((struct IOExtSer *)write2) -> IOSer.io_Data = &data1;
  72.                         read1 -> io_Command = CMD_READ;
  73.                         read2 -> io_Command = CMD_READ;
  74.                         write1 -> io_Command = CMD_WRITE;
  75.                         write2 -> io_Command = CMD_WRITE;
  76.  
  77.                         SendIO(read1);  /* non-blocking read to kick it all off */
  78.                         SendIO(read2);
  79.  
  80.                         do  /* main loop copying between units */
  81.                             {
  82.                             signal = Wait(1L << port -> mp_SigBit | SIGBREAKF_CTRL_C);  /* wait for something to happen */
  83.                             if (CheckIO(read1))  /* check which one completed */
  84.                                 {
  85.                                 WaitIO(read1);  /* remove it from port */
  86.                                 DoIO(write2);  /* blocking write; already pointing to the data we read */
  87.                                 ((struct IOExtSer *)read1) -> IOSer.io_Length = 1;
  88.                                 ((struct IOExtSer *)read1) -> IOSer.io_Data = &data1;
  89.                                 read1 -> io_Command = CMD_READ;  /* ready for next time round */
  90.                                 ((struct IOExtSer *)write2) -> IOSer.io_Length = 1;
  91.                                 ((struct IOExtSer *)write2) -> IOSer.io_Data = &data1;
  92.                                 write2 -> io_Command = CMD_WRITE;
  93.                                 SendIO(read1);  /* restart this */
  94.                                 }
  95.                             if (CheckIO(read2))  /* could also have been this */
  96.                                 {
  97.                                 WaitIO(read2);  /* remove it from port */
  98.                                 DoIO(write1);  /* blocking write; already pointing to the data we read */
  99.                                 ((struct IOExtSer *)read2) -> IOSer.io_Length = 1;
  100.                                 ((struct IOExtSer *)read2) -> IOSer.io_Data = &data2;
  101.                                 read2 -> io_Command = CMD_READ;
  102.                                 ((struct IOExtSer *)write1) -> IOSer.io_Length = 1;
  103.                                 ((struct IOExtSer *)write1) -> IOSer.io_Data = &data2;
  104.                                 SendIO(read2);  /* restart this */
  105.                                 write1 -> io_Command = CMD_WRITE;
  106.                                 }
  107.                             }
  108.                         while (!(signal & SIGBREAKF_CTRL_C));  /* quit when user presses Ctrl-C */
  109.  
  110.                         AbortIO(read1);  /* clean up */
  111.                         AbortIO(read2);
  112.                         WaitIO(read1);  /* wait until everything can be safely freed */
  113.                         WaitIO(read2);
  114.  
  115.                         CloseDevice(read2);
  116.                         }
  117.                     else printf("Can't open %s unit %s\n", argv[3], argv[4]);
  118.  
  119.                     CloseDevice(read1);
  120.                     }
  121.                 else printf("Can't open %s unit %s\n", argv[1], argv[2]);
  122.  
  123.                 DeleteIORequest(read1);
  124.                 DeleteIORequest(read2);
  125.                 DeleteIORequest(write1);
  126.                 DeleteIORequest(write2);
  127.                 }
  128.             else printf("Can't create IO requests (out of memory?)\n");
  129.  
  130.             DeleteMsgPort(port);
  131.             if (window)
  132.                 CloseWindow(window);
  133.             }
  134.         else printf("Can't create message port (out of memory?)\n");
  135.  
  136.         CloseLibrary(IntuitionBase);
  137.         }
  138.     else printf("This utility requires at least Release 2!\n");
  139.     }
  140. else printf("Usage: SerialRouter <device1> <unit1> <device2> <unit2>\n");
  141. }
  142.