home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / FILE / BEAV132S.ZIP / AMIGA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-08  |  14.0 KB  |  495 lines

  1. /* -*-C-*-
  2.  *
  3.  * Module : amiga.c
  4.  *
  5.  * Author : Simon J Raybould.    (sie@fulcrum.bt.co.uk).
  6.  *
  7.  * Date   : Tuesday 11th June 1991.
  8.  *
  9.  * Desc   : amiga specifics for beav binary editor.
  10.  *
  11.  *
  12.  * This file is public domain and you can do what you want with it, even roll
  13.  * it up into a ball and toss it for your cat to chase. I accept no
  14.  * resposibility for it being unfit for any purpose (including a feline toy).
  15.  * Any bugs you can either fix them yourself or tell me and I'll do it.
  16.  * Any major fixes should be reported to me and I will inform the main keeper
  17.  * of beav to be sure they are fixed in the next release. This only applies to
  18.  * bugs in THIS FILE or in AMIGA sections of other files. Any other bugs to the
  19.  * original author.
  20.  *
  21.  * SJR - 25.Aug.91
  22.  *
  23.  *
  24.  */
  25.  
  26. #ifdef AMIGA
  27.  
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <signal.h>
  32. #include <libraries/dosextens.h>
  33. #include <exec/memory.h>
  34. #include <intuition/intuition.h>
  35.  
  36. #include "def.h"
  37.  
  38. #define SCRBUFSIZ        1024    /* buffered screen io */
  39.  
  40. struct NewWindow nw = {
  41.     0, 0, 640,256, -1,-1, NULL,
  42.     WINDOWDEPTH|WINDOWDRAG|SMART_REFRESH|ACTIVATE|BORDERLESS,
  43.     NULL, NULL,
  44.     "BEAV V1.32 Amiga Port by S.J.Raybould (sie@fulcrum.bt.co.uk)  Sep 1991",
  45.     NULL, NULL, 0, 0, 0, 0, WBENCHSCREEN
  46. };
  47.  
  48. /* Opens/allocations we'll need to clean up */
  49. struct Library  *IntuitionBase = NULL;
  50. struct Window   *win = NULL, *OpenWindow();
  51. struct IOStdReq *writeReq = NULL;    /* I/O request block pointer */
  52. struct MsgPort  *writePort = NULL;   /* replyport for writes      */
  53.  
  54. struct IOStdReq *readReq = NULL;     /* I/O request block pointer */
  55. struct MsgPort  *readPort = NULL;    /* replyport for reads       */
  56.  
  57. struct MsgPort  *CreatePort();
  58. BOOL OpenedConsole = FALSE;
  59. UBYTE ibuf;
  60. BYTE OpenConsole();
  61. void CloseConsole(), QueueRead(), ConWrite();
  62.  
  63.  
  64. int     nrow;                   /* Terminal size, rows.         */
  65. int     ncol;                   /* Terminal size, columns.      */
  66. #ifdef CRAP
  67. int tceeol = 3;            /* Costs.                       */
  68. #endif CRAP
  69.  
  70. int kbdpoll;            /* in O_NDELAY mode         */
  71. int kbdqp;            /* there is a char in kbdq  */
  72.  
  73. char scrbuf[SCRBUFSIZ];        /* buffered screen io */
  74. short scrbufpos = 0;        /* current write position */
  75.  
  76. /* CODE TO REPLACE STUFF IN termio.c */
  77.  
  78. /*
  79.  * This function gets called just before we go back home to the command
  80.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  81.  * Another no-operation on CPM.
  82.  */
  83. void
  84. ttclose()
  85. {
  86.     /* Put TTY back in sesible state */
  87.     if(!(CheckIO(readReq))) AbortIO(readReq);
  88.     WaitIO(readReq);
  89.     if(OpenedConsole) CloseConsole(writeReq);
  90.     if(readReq)       DeleteExtIO(readReq);
  91.     if(readPort)      DeletePort(readPort);
  92.     if(writeReq)      DeleteExtIO(writeReq);
  93.     if(writePort)     DeletePort(writePort);
  94.     if(win)           CloseWindow(win);
  95.     if(IntuitionBase) CloseLibrary(IntuitionBase);
  96. }
  97.  
  98. /*
  99.  * Flush terminal buffer. Does real work where the terminal output is buffered
  100.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  101.  */
  102. void
  103. ttflush()
  104. {
  105.     if(scrbufpos>0) {
  106.         ConWrite(writeReq, scrbuf, scrbufpos);
  107.         scrbufpos = 0;
  108.     }
  109. }
  110.  
  111. /*
  112.  * Write a character to the display. On VMS, terminal output is buffered, and
  113.  * we just put the characters in the big array, after checking for overflow.
  114.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  115.  * MS-DOS (use the very very raw console output routine).
  116.  */
  117. void ttputc(c)
  118. {
  119.     if(scrbufpos < SCRBUFSIZ)
  120.         scrbuf[scrbufpos++] = c;
  121.     else {
  122.         ConWrite(writeReq, scrbuf, scrbufpos);
  123.         scrbufpos = 0;
  124.         scrbuf[scrbufpos++] = c;
  125.     }
  126. }
  127.  
  128. void
  129. ttputs(char *str)
  130. {
  131.     while(*str)
  132.         ttputc(*str++);
  133. }
  134.  
  135. /*
  136.  * Read a character from the terminal, performing no editing and doing no echo
  137.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  138.  * simple on CPM, because the system can do exactly what you want.
  139.  */
  140. ttgetc()
  141. {
  142.     char c, ConGetChar();
  143.     static char Buffer[8], ri=0, wi=0;
  144.  
  145.     if(kbdqp)
  146.         kbdqp = FALSE;
  147.     /* If we stil have chars from last time, return them */
  148.     if(ri<wi)
  149.         return Buffer[ri++]&0x7f;
  150.  
  151.     /* Else empty the buffer and start a new read */
  152.     ri=wi=0;
  153.     c = ConGetChar(readPort, &ibuf);
  154.     /*
  155.    * Attempt some translations !
  156.    * This is the place to extend, if you wish to add some more.
  157.    * SEE RKM L&D 1.3 pg 654 for more info.
  158.    */
  159.     if((unsigned char)c == (unsigned char)0x9b) {    /* ANSI esc start */
  160.         c = ConGetChar(readPort, &ibuf);
  161.         switch(c) {
  162.         case 'A':            /* UP */
  163.             Buffer[wi++] = 0x10;    /* ^P */
  164.             break;
  165.         case 'B':            /* DOWN */
  166.             Buffer[wi++] = 0x0e;    /* ^N */
  167.             break;
  168.         case 'C':            /* RIGHT */
  169.             Buffer[wi++] = 0x06;    /* ^F */
  170.             break;
  171.         case 'D':            /* LEFT */
  172.             Buffer[wi++] = 0x02;    /* ^B */
  173.             break;
  174.         case '0':            /* F1 */
  175.             ConGetChar(readPort, &ibuf); /* discard tilde */
  176.             Buffer[wi++] = 0x1b;    /* HELP = "ESC ?" */
  177.             Buffer[wi++] = '?';
  178.             break;
  179.         case '1':            /* F2 or SHIFTED function key */
  180.             c = ConGetChar(readPort, &ibuf); /* Get next char to see if it's a tilde */
  181.             switch(c) {
  182.             case '~':            /* was definately F2 */
  183.                 Buffer[wi++] = 0x1b;    /* mark-set = "ESC ." */
  184.                 Buffer[wi++] = '.';
  185.                 break;
  186.             case '0':            /* SHIFTED F1 */
  187.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  188.                 Buffer[wi++] = 0x18;    /* binding-for-key = "Ctl-X ?" */
  189.                 Buffer[wi++] = '?';
  190.                 break;
  191.             case '1':            /* SHIFTED F2 */
  192.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  193.                 Buffer[wi++] = 0x18;    /* file-read = "Ctl-X Ctl-R" */
  194.                 Buffer[wi++] = 0x12;
  195.                 break;
  196.             case '2':            /* SHIFTED F3 */
  197.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  198.                 Buffer[wi++] = 0x18;    /* file-save = "Ctl-X Ctl-S" */
  199.                 Buffer[wi++] = 0x13;
  200.                 break;
  201.             case '3':            /* SHIFTED F4 */
  202.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  203.                 Buffer[wi++] = 0x18;    /* file-visit = "Ctl-X Ctl-V" */
  204.                 Buffer[wi++] = 0x16;
  205.                 break;
  206.             case '4':            /* SHIFTED F5 */
  207.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  208.                 Buffer[wi++] = 0x18;    /* file-write = "Ctl-X Ctl-W" */
  209.                 Buffer[wi++] = 0x17;
  210.                 break;
  211.             case '5':            /* SHIFTED F6 */
  212.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  213.                 Buffer[wi++] = 0x18;    /* save-all-buffers = "Ctl-X return" */
  214.                 Buffer[wi++] = '\r';
  215.                 break;
  216.             case '6':            /* SHIFTED F7 */
  217.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  218.                 Buffer[wi++] = 0x18;    /* buffer-set-file-name = "Ctl-X Ctl-F" */
  219.                 Buffer[wi++] = 0x06;
  220.                 break;
  221.             case '7':            /* SHIFTED F8 */
  222.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  223.                 Buffer[wi++] = 0x18;    /* insert-file = "Ctl-X TAB" */
  224.                 Buffer[wi++] = '\t';
  225.                 break;
  226.             case '8':            /* SHIFTED F9 */
  227.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  228.                 Buffer[wi++] = 0x18;    /* quit-save-all = "Ctl-X Ctl-E" */
  229.                 Buffer[wi++] = 0x05;
  230.                 break;
  231.             case '9':            /* SHIFTED F10 */
  232.                 ConGetChar(readPort, &ibuf);    /* Discard the tilde */
  233.                 Buffer[wi++] = 0x03;    /* quit-no-save = "Ctl-C" */
  234.                 break;
  235.             }
  236.             break;
  237.         case '2':            /* F3 */
  238.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  239.             Buffer[wi++] = 0x1b;      /* search-forv = "ESC s" */
  240.             Buffer[wi++] = 's';
  241.             break;
  242.         case '3':            /* F4 */
  243.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  244.             Buffer[wi++] = 0x1b;    /* search-again = "ESC t" */
  245.             Buffer[wi++] = 't';
  246.             break;
  247.         case '4':            /* F5 */
  248.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  249.             Buffer[wi++] = 0x1b;    /* replace = "ESC %" */
  250.             Buffer[wi++] = '%';
  251.             break;
  252.         case '5':            /* F6 */
  253.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  254.             Buffer[wi++] = 0x19;    /* yank = "Ctl-Y" */
  255.             break;
  256.         case '6':            /* F7 */
  257.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  258.             Buffer[wi++] = 0x1b;    /* copy-mark-to-cursor = "ESC w" */
  259.             Buffer[wi++] = 'w';
  260.             break;
  261.         case '7':            /* F8 */
  262.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  263.             Buffer[wi++] = 0x17;    /* delete-mark-to-cursor = "Ctl-W" */
  264.             break;
  265.         case '8':            /* F9 */
  266.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  267.             Buffer[wi++] = 0x18;    /* move-to-byte = "Ctl-X G" */
  268.             Buffer[wi++] = 'G';
  269.             break;
  270.         case '9':            /* F10 */
  271.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  272.             Buffer[wi++] = 0x07;    /* abort-cmd = "Ctl-G" */
  273.             break;
  274.         case '?':            /* HELP */
  275.             ConGetChar(readPort, &ibuf);        /* Discard the tilde */
  276.             Buffer[wi++] = 0x1b;    /* help = "ESC ?" */
  277.             Buffer[wi++] = '?';
  278.             break;
  279.         }
  280.         return Buffer[ri++]&0x7f;
  281.     } else    /* not an ANSI sequence */
  282.         return c&0x7f;
  283. }
  284.  
  285. /*
  286.  * This function is called once to set up the terminal device streams.
  287.  * On VMS, it translates TT until it finds the terminal, then assigns
  288.  * a channel to it and sets it raw. On CPM it is a no-op.
  289.  */
  290.  
  291. void ttopen()
  292. {
  293.     int Sig;
  294.     ULONG conreadsig, windowsig;
  295.     BYTE error;
  296.     struct Screen Screen;        /* get a copy of WBENCHSCREEN in here */
  297.  
  298.  
  299.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0))) {
  300.         printf("Can't open intuition\n");
  301.         ttclose();
  302.         exit(10);
  303.     }
  304.     /* Create reply port and io block for writing to console */
  305.     if(!(writePort = CreatePort("LARN.console.write",0))) {
  306.         printf("Can't create write port\n");
  307.         ttclose();
  308.         exit(10);
  309.     }
  310.     if(!(writeReq = (struct IOStdReq *)
  311.         CreateExtIO(writePort,(LONG)sizeof(struct IOStdReq)))) {
  312.         printf("Can't create write request\n");
  313.         ttclose();
  314.         exit(10);
  315.     }
  316.     /* Create reply port and io block for reading from console */
  317.     if(!(readPort = CreatePort("LARN.console.read",0))) {
  318.         printf("Can't create read port\n");
  319.         ttclose();
  320.         exit(10);
  321.     }
  322.     if(!(readReq = (struct IOStdReq *)
  323.         CreateExtIO(readPort,(LONG)sizeof(struct IOStdReq)))) {
  324.         printf("Can't create read request\n");
  325.         ttclose();
  326.         exit(10);
  327.     }
  328.     if(!GetScreenData(&Screen, sizeof(struct Screen), WBENCHSCREEN, NULL)) {
  329.         printf("Can't get screen size\n");
  330.         ttclose();
  331.         exit(10);
  332.     }
  333.     nrow = Screen.Height/8-3;
  334.     ncol = Screen.Width/8;
  335.     nw.Height = Screen.Height;
  336.     nw.Width = Screen.Width;
  337.  
  338.     /* don't allow a larger number of rows than we can handle */
  339.     if (nrow > NROW)
  340.         nrow = NROW;
  341.     /* don't allow a larger number of cols than we can handle */
  342.     if (ncol > NCOL)
  343.         ncol = NCOL;
  344.  
  345.     /* Open a window */
  346.     if(!(win = OpenWindow(&nw))) {
  347.         printf("Can't open window\n");
  348.         ttclose();
  349.         exit(10);
  350.     }
  351.     /* Now, attach a console to the window */
  352.     if(error = OpenConsole(writeReq,readReq,win)) {
  353.         printf("Can't open console.device\n");
  354.         ttclose();
  355.         exit(10);
  356.     } else
  357.         OpenedConsole = TRUE;
  358.  
  359.     QueueRead(readReq,&ibuf); /* send the first console read request */
  360.     conreadsig = 1 << readPort->mp_SigBit;
  361.     windowsig = 1 << win->UserPort->mp_SigBit;
  362.     for(Sig=0; Sig<NSIG; Sig++)
  363.         signal(Sig, SIG_IGN);
  364.  
  365.     kbdpoll = FALSE;
  366.     /* on all screens we are not sure of the initial position of the cursor */
  367.     ttrow = 999;
  368.     ttcol = 999;
  369. }
  370.  
  371. /* END OF TERMIO REPLACEMENT CODE */
  372.  
  373. /* Attach console device to an open Intuition window.
  374.  * This function returns a value of 0 if the console 
  375.  * device opened correctly and a nonzero value (the error
  376.  * returned from OpenDevice) if there was an error.
  377.  */
  378. BYTE OpenConsole(writereq, readreq, window)
  379. struct IOStdReq *writereq;
  380. struct IOStdReq *readreq;
  381. struct Window *window;
  382. {
  383.     BYTE error;
  384.  
  385.     writereq->io_Data = (APTR) window;
  386.     writereq->io_Length = sizeof(struct Window);
  387.     error = OpenDevice("console.device", 0, writereq, 0);
  388.     readreq->io_Device = writereq->io_Device; /* clone required parts */
  389.     readreq->io_Unit   = writereq->io_Unit;
  390.     return(error);
  391. }
  392.  
  393. void CloseConsole(struct IOStdReq *writereq)
  394. {
  395.     CloseDevice(writereq);
  396. }
  397.  
  398. /* Output a single character to a specified console 
  399.  */
  400. void ConPutChar(struct IOStdReq *writereq, UBYTE character)
  401. {
  402.     writereq->io_Command = CMD_WRITE;
  403.     writereq->io_Data = (APTR)&character;
  404.     writereq->io_Length = 1;
  405.     DoIO(writereq);
  406.     /* command works because DoIO blocks until command is done
  407.    * (otherwise ptr to the character could become invalid)
  408.    */
  409. }
  410.  
  411.  
  412. /* Output a stream of known length to a console 
  413.  */
  414. void ConWrite(struct IOStdReq *writereq, UBYTE *string, LONG length)
  415. {
  416.     writereq->io_Command = CMD_WRITE;
  417.     writereq->io_Data = (APTR)string;
  418.     writereq->io_Length = length;
  419.     DoIO(writereq);
  420.     /* command works because DoIO blocks until command is done
  421.    * (otherwise ptr to string could become invalid in the meantime)
  422.    */
  423. }
  424.  
  425.  
  426. /* Output a NULL-terminated string of characters to a console 
  427.  */
  428. void ConPuts(struct IOStdReq *writereq,UBYTE *string)
  429. {
  430.     writereq->io_Command = CMD_WRITE;
  431.     writereq->io_Data = (APTR)string;
  432.     writereq->io_Length = -1;  /* means print till terminating null */
  433.     DoIO(writereq);
  434. }
  435.  
  436. /* Queue up a read request to console, passing it pointer
  437.  * to a buffer into which it can read the character
  438.  */
  439. void QueueRead(struct IOStdReq *readreq, UBYTE *whereto)
  440. {
  441.     readreq->io_Command = CMD_READ;
  442.     readreq->io_Data = (APTR)whereto;
  443.     readreq->io_Length = 1;
  444.     SendIO(readreq);
  445. }
  446.  
  447. struct IOStdReq *readreq; /* ttkeyready() needs to be able to see this */
  448.  
  449. /* Wait for a character
  450.  */
  451. char ConGetChar(struct MsgPort *msgport, UBYTE *whereto)
  452. {
  453.     register temp;
  454.  
  455.     WaitPort(msgport);
  456.     readreq = (struct IOStdReq *)GetMsg(msgport);
  457.     temp = *whereto;               /* get the character */
  458.     QueueRead(readreq,whereto);    /* then re-use the request block*/
  459.     return((char)temp);
  460. }
  461.  
  462. /* typahead():    Check to see if any characters are already in the
  463.         keyboard buffer
  464.    On the amiga, we do this by checking if the outstanding read request
  465.    has been satisfied yet by calling CheckIO().
  466. */
  467. ttkeyready ()
  468. {
  469.     if(!kbdqp)
  470.         kbdqp = CheckIO(readreq)?1:0;
  471.     return kbdqp;
  472. }
  473.  
  474. /* UNIX support stuff */
  475.  
  476. #define BLOCKSIZE  4096
  477.  
  478. link(char *SPath, char *DPath)
  479. {
  480.     int sfd, dfd, Bytes;
  481.     char BlkBuf[BLOCKSIZE];
  482.  
  483.     if((sfd = open(SPath, O_RDONLY)) == -1)
  484.         return -1;
  485.     if((dfd = open(DPath, O_WRONLY|O_CREAT|O_TRUNC)) == -1)
  486.         return -1;
  487.     while((Bytes = read(sfd, BlkBuf, BLOCKSIZE)) > 0)
  488.         write(dfd, BlkBuf, Bytes);
  489.     close(sfd);
  490.     close(dfd);
  491.     return 0;
  492. }
  493.  
  494. #endif /* AMIGA */
  495.