home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d026 / c-kermit.lha / C-kermit / src / ckitio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-16  |  28.6 KB  |  1,155 lines

  1. char *ckxv = "AMIGA tty I/O, 1.0(000), 30 Mar 1986";
  2.  
  3. /*  C K I T I O  --  Terminal and Interrupt Functions for Commodore AMIGA  */
  4.  
  5. /*
  6.  * Copyright (C) 1986, Trustees of Columbia University in the City of
  7.  * New York.  Permission is granted to any individual or institution to
  8.  * use, copy, or redistribute this software, so long as it is not sold
  9.  * for profit, provided this copyright notice is retained.
  10.  */
  11.  
  12. /* Edit History
  13.   27 Feb 1986  - Davide P. Cervone.  Wrote it.  This should be re-written
  14.     to do menu and mouse stuff, but I don't have time for that just now.
  15.     I don't have the ROM Kernal Manuals yet, so I expect that I will have
  16.     done some things "wrong," and that there are better ways to do the
  17.     I/O.  I will try to update this when I get the manuals.  The UnMap
  18.     function will be used, eventually, to allow modifiable keyboard mappings
  19.     (like MacKermit), and should allow for multi-character mappings, as
  20.     well.  Eventually, this should include a VT100 emulator.
  21. */
  22.  
  23. /* C-Kermit interrupt, terminal control & i/o functions for AMIGA systems */
  24.  
  25. char *ckxsys = " Commodore AMIGA";
  26.  
  27.  
  28.  
  29. /*
  30.  Variables available to outside world:
  31.  
  32.    dftty  -- Pointer to default tty name string, like "/dev/tty".
  33.    dfloc  -- 0 if dftty is console, 1 if external line.
  34.    dfprty -- Default parity
  35.    dfflow -- Default flow control
  36.    ckxech -- Flag for who echoes console typein:
  37.      1 - The program (system echo is turned off)
  38.      0 - The system (or front end, or terminal).
  39.    functions that want to do their own echoing should check this flag
  40.    before doing so.
  41.  
  42.    backgrd -- Flag indicating program executing in background ( & on
  43.       end of shell command). Used to ignore INT and QUIT signals.
  44.  
  45.  Functions for assigned communication line (either external or console tty):
  46.  
  47.    ttopen(ttname,local,mdmtyp) -- Open the named tty for exclusive access.
  48.    ttclos()                -- Close & reset the tty, releasing any access lock.
  49.    ttpkt(speed,flow)       -- Put the tty in packet mode and set the speed.
  50.    ttvt(speed,flow)        -- Put the tty in virtual terminal mode.
  51.             or in DIALING or CONNECTED modem control state.
  52.    ttinl(dest,max,timo)    -- Timed read line from the tty.
  53.    ttinc(timo)             -- Timed read character from tty.
  54.    ttchk()                 -- See how many characters in tty input buffer.
  55.    ttxin(n,buf)            -- Read n characters from tty (untimed).
  56.    ttol(string,length)     -- Write a string to the tty.
  57.    ttoc(c)                 -- Write a character to the tty.
  58.    ttflui()                -- Flush tty input buffer.
  59. */
  60.  
  61.  
  62.  
  63. /*
  64. Functions for console terminal:
  65.  
  66.    congm()   -- Get console terminal modes.
  67.    concb(esc) -- Put the console in single-character wakeup mode with no echo.
  68.    conbin(esc) -- Put the console in binary (raw) mode.
  69.    conres()  -- Restore the console to mode obtained by congm().
  70.    conoc(c)  -- Unbuffered output, one character to console.
  71.    conol(s)  -- Unbuffered output, null-terminated string to the console.
  72.    conola(s) -- Unbuffered output, array of strings to the console.
  73.    conxo(n,s) -- Unbuffered output, n characters to the console.
  74.    conchk()  -- Check if characters available at console (bsd 4.2).
  75.       Check if escape char (^\) typed at console (System III/V).
  76.    coninc(timo)  -- Timed get a character from the console.
  77.    conint()  -- Enable terminal interrupts on the console if not background.
  78.    connoi()  -- Disable terminal interrupts on the console if not background.
  79.    contti()  -- Get a character from either console or tty, whichever is first.
  80.  
  81. Time functions
  82.  
  83.    msleep(m) -- Millisecond sleep
  84.    ztime(&s) -- Return pointer to date/time string
  85. */
  86.  
  87. #include <exec/types.h>
  88. #include <exec/nodes.h>
  89. #include <exec/lists.h>
  90. #include <exec/ports.h>
  91. #include <exec/tasks.h>
  92. #include <exec/io.h>
  93. #include <exec/devices.h>
  94. #include <exec/memory.h>
  95. #include <intuition/intuition.h>
  96. #include <intuition/intuitionbase.h>
  97. #include <graphics/gfxbase.h>
  98. #include <graphics/regions.h>
  99. #include <graphics/copper.h>
  100. #include <graphics/gels.h>
  101. #include <devices/keymap.h>
  102. #include <devices/timer.h>
  103. #include <devices/serial.h>
  104. #include <hardware/blit.h>
  105. #include <libraries/dos.h>
  106. #include <stdio.h>
  107. #include <ctype.h>
  108.  
  109. #ifndef max
  110. #define max(a,b) ((a)>(b)?(a):(b))    /* For Manx C (fnf) */
  111. #endif
  112.  
  113. #define INTUITION_REV 1
  114. #define GRAPHICS_REV 1
  115.  
  116. #define SCREENCLR 12    /* character that does clear screen */
  117. #define ERROR_EXIT 10   /* status for exit with error */
  118.  
  119. #define NODEF       -1            /* undefined key */
  120. #define NRMLKEYS     0            /* normal keys */
  121. #define CTRLKEYS     1            /* control keys */
  122. #define UNSHIFT      0            /* unshifted keyboard */
  123. #define SHIFTED      1            /* shifted keyboard */
  124. #define METABIT      0x80         /* 8th bit meta prefixing */
  125.  
  126. #define Puts(s)         fputs(stdout,s)
  127. #define Printf(s)       fprintf(stdout,s)
  128. #define Printf2(s,x)    fprintf(stdout,s,x)
  129.  
  130. char templine[500];
  131.  
  132. #define LONG_DEF
  133. #include "ckcker.h"
  134. #include "ckcdeb.h"         /* Formats for debug() */
  135.  
  136.  
  137.  
  138. /* Declarations */
  139.  
  140. /* dftty is the device name of the default device for file transfer */
  141. /* dfloc is 0 if dftty is the user's console terminal, 1 if an external line */
  142.  
  143.     char *dftty = "SERIAL";
  144.     int dfloc = 1;         /* Default location is local */
  145.     int dfprty = 0;         /* Parity (0 = none) */
  146.     int dfflow = 1;         /* Xon/Xoff flow control */
  147.     int batch = 0;         /* Assume interactive */
  148.  
  149. int ckxech = 0; /* 0 if system normally echoes console characters, else 1 */
  150.  
  151.  
  152. /* Declarations of variables global within this module */
  153.  
  154. static int conif = 0,         /* Console interrupts on/off flag */
  155.     cgmf = 0,                 /* Flag that console modes saved */
  156.     ttychn = 0,               /* TTY i/o channe; */
  157.     conch = NODEF;            /* console input character buffer  */
  158.  
  159. static char escchr;           /* Escape or attn character */
  160.  
  161. #define LEFTEDGE     4
  162. #define TOPEDGE     17
  163.  
  164. int Xpos=3, Ypos=17;  /* current cursor position */
  165. int Xmax, Ymax;       /* current max X and Y values */
  166. int CursorON = -1;    /* cursor on or off */
  167.  
  168. struct IntuitionBase *IntuitionBase;
  169. struct GfxBase *GfxBase;
  170.  
  171. struct timerequest TimerIO;
  172. struct MsgPort TimerMsgPort;
  173.  
  174. extern struct MsgPort *CreatePort();
  175.  
  176. struct IOExtSer *ReadRequest;
  177. static char ReadPort[2];
  178. struct IOExtSer *WriteRequest;
  179. static char WritePort[2];
  180.  
  181. int OpenedRead = 0;
  182. int OpenedWrite = 0;
  183. int OpenedIntui = 0;
  184. int OpenedGfx = 0;
  185. int OpenedWindow = 0;
  186. int OpenedTimer = 0;
  187.  
  188. struct NewWindow NewWindow = {            /* window definition */
  189.    0,
  190.    0,
  191.    640,
  192.    200,
  193.    0,
  194.    1,                     
  195.    CLOSEWINDOW | NEWSIZE | RAWKEY,
  196.    WINDOWCLOSE | SMART_REFRESH | ACTIVATE | WINDOWDRAG |
  197.    WINDOWDEPTH | WINDOWSIZING,
  198.    NULL,
  199.    NULL,
  200.    (UBYTE *) "Amiga Kermit",
  201.    NULL,
  202.    NULL,
  203.    100, 35,
  204.    640, 200,
  205.    WBENCHSCREEN
  206. };
  207.  
  208. struct Window *myWindow;              /* new window */
  209.  
  210.  
  211. OpenTimer()
  212. {
  213.    extern struct Task *FindTask ();
  214.  
  215.    if (OpenDevice(TIMERNAME, UNIT_VBLANK, &TimerIO, 0) != 0)
  216.    {
  217.       Printf("Can't open Timer Device\n");
  218.       doexit(ERROR_EXIT);
  219.    } else {
  220.       OpenedTimer = 1;
  221.    }
  222.    TimerMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  223.    TimerMsgPort.mp_Node.ln_Name = "vt-timer";
  224.    TimerMsgPort.mp_Flags = 0;
  225.    TimerMsgPort.mp_SigBit = AllocSignal(-1);
  226.    TimerMsgPort.mp_SigTask = FindTask((char *)NULL);
  227.    AddPort(&TimerMsgPort);
  228.    TimerIO.tr_node.io_Message.mn_ReplyPort = &TimerMsgPort;
  229. }
  230.  
  231. StartTimer(seconds, micros)
  232. int seconds, micros;
  233. {
  234.    TimerIO.tr_node.io_Command = TR_ADDREQUEST;
  235.    TimerIO.tr_node.io_Flags = IOF_QUICK;
  236.    TimerIO.tr_time.tv_secs = (ULONG) seconds;
  237.    TimerIO.tr_time.tv_micro = (ULONG) micros;
  238.    SendIO(&TimerIO);
  239. }
  240.  
  241. OpenSerial()    /* Open the serial device */
  242. {
  243.    extern void *AllocMem ();
  244.  
  245.    ReadRequest = (struct IOExtSer *) AllocMem(sizeof(*ReadRequest), MEMF_PUBLIC | MEMF_CLEAR);
  246.    ReadRequest->IOSer.io_Message.mn_ReplyPort = CreatePort("Read_RS",0);
  247.    ReadRequest->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  248.    OpenedRead = -1;
  249.    if (OpenDevice(SERIALNAME, NULL, ReadRequest, NULL) != 0)
  250.    {
  251.       Printf("Can't open Serial Device for Reading\n");
  252.       doexit(ERROR_EXIT);
  253.    } else {
  254.       OpenedRead = 1;
  255.    }
  256.  
  257.    WriteRequest = (struct IOExtSer *) AllocMem(sizeof(*WriteRequest), MEMF_PUBLIC | MEMF_CLEAR);
  258.    WriteRequest->IOSer.io_Message.mn_ReplyPort = CreatePort("Write_RS",0);
  259.    WriteRequest->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  260.    OpenedWrite = -1;
  261.    if (OpenDevice(SERIALNAME, NULL, WriteRequest, NULL) != 0)
  262.    {
  263.       Printf("Can't open Serial Device for Writing\n");
  264.       doexit(ERROR_EXIT);
  265.    } else {
  266.       OpenedWrite = 1;
  267.    }
  268.  
  269.    ReadRequest->IOSer.io_Command = SDCMD_SETPARAMS;
  270.    ReadRequest->IOSer.io_Length = 1;
  271.    ReadRequest->IOSer.io_Data = (APTR) &ReadPort[0];
  272.    ReadRequest->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  273.    ReadRequest->io_Baud = 9600;
  274.    ReadRequest->io_ReadLen = 8;
  275.    ReadRequest->io_WriteLen = 8;
  276.  
  277.    WriteRequest->IOSer.io_Command = CMD_WRITE;
  278.    WriteRequest->IOSer.io_Length = 1;
  279.    WriteRequest->IOSer.io_Data = (APTR) &WritePort[0];
  280.    WriteRequest->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  281.  
  282.    DoIO(ReadRequest);
  283.    ReadRequest->IOSer.io_Command = CMD_READ;
  284.    BeginIO(ReadRequest);
  285. }
  286.  
  287. OpenLibraries()
  288. {
  289.    extern void *OpenLibrary ();
  290.  
  291.    if ((IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  292.    {
  293.       Printf("Can't open Intuition Library\n");
  294.       doexit(ERROR_EXIT);
  295.    } else {
  296.       OpenedIntui = 1;
  297.    }
  298.  
  299.    if ((GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", GRAPHICS_REV)) == NULL)
  300.    {
  301.       Printf("Can't open Graphics Library\n");
  302.       doexit(ERROR_EXIT);
  303.    } else {
  304.       OpenedGfx = 1;
  305.    }
  306. }
  307.  
  308. OpenMyWindow()
  309. {
  310.    extern struct Window *OpenWindow ();
  311.  
  312.    if ((myWindow = OpenWindow(&NewWindow)) == NULL)
  313.    {
  314.       Printf("Can't open my window\n");
  315.       doexit(ERROR_EXIT);
  316.    } else {
  317.       OpenedWindow = 1;
  318.    }
  319. }
  320.  
  321. OpenAll()
  322. {
  323.    OpenLibraries();
  324.    OpenTimer();
  325.    OpenSerial();
  326.    OpenMyWindow();
  327. }
  328.  
  329. /*  S Y S I N I T  --  System-dependent program initialization.  */
  330.  
  331. sysinit() {
  332.  
  333.    OpenAll();
  334.    SetAPen(myWindow->RPort,1);
  335.    SetDrMd(myWindow->RPort,COMPLEMENT);
  336.    Xmax = myWindow->Width;
  337.    Ymax = myWindow->Height;
  338.  
  339.    putchar(SCREENCLR);
  340.    return(0);
  341. }
  342.  
  343.  
  344. CloseLibraries()
  345. {
  346.    if (OpenedIntui) CloseLibrary(IntuitionBase);
  347.    if (OpenedGfx)   CloseLibrary(GfxBase);
  348.    OpenedIntui = 0;
  349.    OpenedGfx = 0;
  350. }
  351.  
  352. CloseSerial()
  353. {
  354.    if (OpenedRead)
  355.    {
  356.       if (OpenedRead > 0) CloseDevice(ReadRequest);
  357.       DeletePort(ReadRequest->IOSer.io_Message.mn_ReplyPort);
  358.       FreeMem(ReadRequest,sizeof(*ReadRequest));
  359.       OpenedRead = 0;
  360.    }
  361.  
  362.    if (OpenedWrite)
  363.    {
  364.       if (OpenedWrite > 0) CloseDevice(WriteRequest);
  365.       DeletePort(WriteRequest->IOSer.io_Message.mn_ReplyPort);
  366.       FreeMem(WriteRequest,sizeof(*WriteRequest));
  367.       OpenedWrite = 0;
  368.    }
  369. }
  370.  
  371. CloseTimer()
  372. {
  373.    if (OpenedTimer)
  374.    {
  375.       CloseDevice(&TimerIO);
  376.       RemPort(&TimerMsgPort);
  377.       OpenedTimer = 0;
  378.    }
  379. }
  380.  
  381. CloseMyWindow()
  382. {
  383.    if (OpenedWindow)
  384.    {
  385.       CloseWindow(myWindow);
  386.       OpenedWindow = 0;
  387.    }
  388. }
  389.  
  390.  
  391. /*  S Y S C L N U P  --  Clean up for final exeit from program */
  392.  
  393. sysclnup()
  394. {
  395.    CloseSerial();
  396.    CloseTimer();
  397.    CloseMyWindow();
  398.    CloseLibraries();
  399. }
  400.  
  401.  
  402.  
  403. /*  T T O P E N  --  Open a tty for exclusive access.  */
  404.  
  405. /*  Returns 0 on success, -1 on failure.  */
  406.  
  407. ttopen(ttname,lcl,modem) char *ttname; int *lcl, modem; {
  408.  
  409.    if (strcmp(ttname,"SERIAL")!=0) return(-1);
  410.    ttychn = 1;
  411.    return(0);
  412. }
  413.  
  414.  
  415.  
  416. /*  T T C L O S  --  Close the TTY, releasing any lock.  */
  417.  
  418. ttclos() {
  419.     if (ttychn == 0) return(0);      /* Wasn't open. */
  420.     ttychn = 0;            /* Mark it as closed. */
  421.     return(0);
  422. }
  423.  
  424.  
  425. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  426.  
  427. ttres() {            /* Restore the tty to normal. */
  428.     if (ttychn == 0) return(-1);      /* Not open. */
  429.     debug(F101,"ttres, ttychn","",ttychn);
  430.     return(0);
  431. }
  432.  
  433.  
  434.  
  435. /*  T T P K T  --  Condition the communication line for packets. */
  436. /*      or for modem dialing */
  437.  
  438. #define DIALING   4      /* flags (via flow) for modem handling */
  439. #define CONNECT   5
  440.  
  441. /*  If called with speed > -1, also set the speed.  */
  442.  
  443. /*  Returns 0 on success, -1 on failure.  */
  444.  
  445. ttpkt(speed,flow) int speed, flow; {
  446.     extern char ttname[];
  447.     int s;
  448.     if (ttychn == 0) return(-1);      /* Not open. */
  449.     if ((s=ttsspd(speed)) <= 0)         /* Check the speed */
  450.        return(-1);
  451.     AbortIO(ReadRequest);
  452.     ReadRequest->io_Baud = s;
  453.     ReadRequest->IOSer.io_Command = SDCMD_SETPARAMS;
  454.     DoIO(ReadRequest);
  455.     ReadRequest->IOSer.io_Command = CMD_READ;
  456.     BeginIO(ReadRequest);
  457.     return(0);
  458. }
  459.  
  460.  
  461.  
  462. /*  T T V T -- Condition communication line for use as virtual terminal  */
  463.  
  464. ttvt(speed,flow) int speed, flow; {
  465.     ttpkt(speed,flow);
  466.     return(0);
  467. }
  468.  
  469.  
  470.  
  471. /*  T T S S P D  --  Return the internal baud rate code for 'speed'.  */
  472.  
  473. int speeds[] = {
  474.    300, 1200, 2400, 4800, 9600, 19200, 0 } ;
  475. /* check that all of these are legal */
  476.  
  477. int
  478. ttsspd(speed) int speed; {
  479.     int s;
  480.  
  481.     if (speed < 0)         /* Unknown speed fails   */
  482.        return (0);
  483.     for (s = 0;  speeds[s] != 0 && speeds[s] != speed;  s++) ;
  484.     if (speeds[s] != 0)
  485.        return(speeds[s]);
  486.     else {
  487.        printf2("Unsupported line speed - %d\n",speed);
  488.        printf("Current speed not changed\n");
  489.        return(-1);
  490.     }
  491. }
  492.  
  493.  
  494.  
  495. /*  T T F L U I  --  Flush tty input buffer */
  496.  
  497.  
  498. ttflui()
  499. {
  500.    AbortIO(ReadRequest);
  501.    BeginIO(ReadRequest);
  502.    return(0);
  503. }
  504.  
  505.  
  506. /* Interrupt Functions */
  507.  
  508.  
  509. /*  C O N I N T  --  Console Interrupt setter  */
  510.  
  511. conint(f) int (*f)(); {
  512.  
  513.     if (conif) return(0);         /* Nothing to do if already on. */
  514.     conif = 1;            /* Flag console interrupts on. */
  515.     return(0);
  516. }
  517.  
  518.  
  519. /*  C O N N O I  --  Reset console terminal interrupts */
  520.  
  521. connoi() {            /* Console-no-interrupts */
  522.  
  523.     conif = 0;
  524.     return(0);
  525. }
  526.  
  527.  
  528.  
  529. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer  */
  530.  
  531.  
  532. ttchk()
  533. {
  534.    if (CheckIO(ReadRequest)) return(1); else return(0);
  535. }
  536.  
  537.  
  538. /*  T T X I N  --  Get n characters from tty input buffer  */
  539.  
  540.  
  541. ttxin(n, buff)
  542. int n;
  543. char *buff;
  544. {
  545.    int i;
  546.  
  547.    for (i=n; i > 0; i--) *buff++ = ttinc(0);
  548.    *buff = '\0';
  549.    return(n);
  550. }
  551.  
  552.  
  553. /*  T T O L  --  Similar to "ttinl", but for writing.  */
  554.  
  555. ttol(s,len)
  556. char s[];
  557. int len;
  558. {
  559.    int i;
  560.  
  561.    for (i=0; i<len; i++) ttoc(s[i]);
  562.    return(0);
  563. }
  564.  
  565.  
  566. /*  T T O C  --  Output a character to the communication line  */
  567.  
  568. ttoc(c)
  569. int c;
  570. {
  571.    WritePort[0] = c;
  572.    DoIO(WriteRequest);
  573.    return(0);
  574. }
  575.  
  576.  
  577.  
  578. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  579. /*
  580.   If no break character encountered within "cmax", return "cmax" characters,
  581.   with disposition of any remaining characters undefined.  Otherwise, return
  582.   the characters that were read, including the break character, in "dest" and
  583.   the number of characters read as the value of function, or 0 upon end of
  584.   file, or -1 if an error occurred.  Times out & returns error if not completed
  585.   within "timo" seconds.
  586. */
  587.  
  588. ttinl(dest,cmax,timo,eol)
  589. int cmax, timo;
  590. char dest[];
  591. char eol;
  592. {
  593.    int time = 0;
  594.    int i, c;
  595.  
  596.    if (timo>0)
  597.    {
  598.       StartTimer(timo,0);
  599.       time = -1;  /* timed, but timer already set */
  600.    } else {
  601.       time = 0;
  602.    }
  603.  
  604.    i = 0;
  605.    c = NODEF;
  606.    while (i<cmax && c!=eol)
  607.    {
  608.       if ((c = ttinc(time)) == NODEF) return(-1);
  609.       dest[i++] = c;
  610.    }
  611.    if (timo>0) AbortIO(&TimerIO);
  612.    if (i<cmax) dest[i] = '\0';
  613.    return(i-1);
  614. }
  615.  
  616.  
  617.  
  618. /*  T T I N C --  Read a character from the communication line  */
  619.  
  620. ttinc(timo)
  621. int timo;
  622. {
  623.    int class,code,qualifier;
  624.    struct IntuiMessage *NewMessage;
  625.    int NoChar = -1;
  626.    int c;
  627.    extern struct Message *GetMsg ();
  628.  
  629.    if (timo>0) StartTimer(timo,0);
  630.  
  631.    while (NoChar)
  632.    {
  633.       if (CheckIO(ReadRequest))
  634.       {
  635.          WaitIO(ReadRequest);
  636.          c = ReadPort[0] & 0x7F;
  637.          BeginIO(ReadRequest);
  638.          NoChar = 0;
  639.       } else {
  640.          if (timo)
  641.          {
  642.             Wait ((1 << myWindow->UserPort->mp_SigBit) |
  643.                   (1 << TimerMsgPort.mp_SigBit) |
  644.                   (1 << ReadRequest->IOSer.io_Message.mn_ReplyPort->mp_SigBit));
  645.             if (CheckIO(&TimerIO)) return(-1);
  646.          } else {
  647.             Wait ((1 << myWindow->UserPort->mp_SigBit) |
  648.                   (1 << ReadRequest->IOSer.io_Message.mn_ReplyPort->mp_SigBit));
  649.          }
  650.          while (NewMessage = (struct IntuiMessage *)GetMsg(myWindow->UserPort))
  651.          {
  652.             class = NewMessage->Class;
  653.             code = NewMessage->Code;
  654.             qualifier = NewMessage->Qualifier;
  655.             ReplyMsg(NewMessage);
  656.             switch (class)
  657.             {
  658.                case CLOSEWINDOW:
  659.                   doexit(0);
  660.                   break;
  661.  
  662.                case NEWSIZE:
  663.                   Xmax = myWindow->Width;
  664.                   Ymax = myWindow->Height;
  665.                   conoc(SCREENCLR);
  666.                   break;
  667.  
  668.                case RAWKEY:
  669.                   if (UnMap(&c,code,qualifier)==0) conch = c;
  670.                   break;
  671.  
  672.             }
  673.          }
  674.       }
  675.    }
  676.    if (timo>0) AbortIO(&TimerIO);
  677.    return(c);
  678. }
  679.  
  680.  
  681. /*  T T S N D B  --  Send a BREAK signal  */
  682.  
  683. ttsndb() {
  684.  
  685.     if (ttychn == 0) return(-1);      /* Not open. */
  686.  
  687. /* send a break */
  688.     return(0);
  689. }
  690.  
  691.  
  692.  
  693. /*  T T H A N G  --  Hang up the communications line  */
  694.  
  695. tthang() {
  696.     if (ttychn == 0) return(-1);      /* Not open. */
  697.  
  698. /* hang up */
  699.     return(0);
  700. }
  701.  
  702.  
  703.  
  704. /*  M S L E E P  --  Millisecond version of sleep().  */
  705.  
  706. msleep(m) int m; {
  707.  
  708.     StartTimer(0,m);
  709.     return(0);
  710. }
  711.  
  712. /*  S L E E P  --  Pause for a few seconds */
  713.  
  714. sleep(time)
  715. int time;
  716. {
  717. /*
  718.    StartTimer(time,0);
  719.    Wait (1 << TimerMsgPort.mp_SigBit);
  720. */
  721. }
  722.  
  723.  
  724. /*  Z T I M E  --  Return date/time string  */
  725.  
  726. ztime(s) char **s; {
  727.     *s = "Unknown date/time";
  728. }
  729.  
  730.  
  731.  
  732. /*  C O N G M  --  Get console terminal modes.  */
  733.  
  734. /*
  735.  Saves current console mode, and establishes variables for switching between
  736.  current (presumably normal) mode and other modes.
  737. */
  738.  
  739. congm() {
  740.     cgmf = 1;            /* Flag that we got them. */
  741.     return(0);
  742. }
  743.  
  744.  
  745. /*  C O N C B --  Put console in cbreak mode.  */
  746.  
  747. /*  Returns 0 if ok, -1 if not  */
  748.  
  749. concb(esc) char esc; {
  750.     if (cgmf == 0) congm();      /* Get modes if necessary. */
  751.     escchr = esc;         /* Make this available to other fns */
  752.     ckxech = 1;            /* Program can echo characters */
  753.     return(0);
  754. }
  755.  
  756.  
  757.  
  758. /*  C O N B I N  --  Put console in binary mode  */
  759.  
  760.  
  761. /*  Returns 0 if ok, -1 if not  */
  762.  
  763. conbin(esc) char esc; {
  764.     if (cgmf == 0) congm();      /* Get modes if necessary. */
  765.     escchr = esc;         /* Make this available to other fns */
  766.     ckxech = 1;            /* Program can echo characters */
  767.     return(0);
  768. }
  769.  
  770.  
  771. /*  C O N R E S  --  Restore the console terminal  */
  772.  
  773. conres() {
  774.     if (cgmf == 0) return(0);      /* Don't do anything if modes */
  775.     ckxech = 0;            /* System should echo chars */
  776.     return(0);
  777. }
  778.  
  779.  
  780.  
  781. /*  C O N O C  --  Output a character to the console terminal  */
  782.  
  783.  
  784. conoc(c)
  785. int c;
  786. {
  787.    int CursorChanged;
  788.  
  789.    CursorChanged = 0;
  790.    if (CursorON) {
  791.       XOR_Cursor();
  792.       CursorChanged = 1;
  793.    }
  794.  
  795.    SetDrMd(myWindow->RPort,JAM2);
  796.    switch(c)
  797.    {
  798.       case 0:
  799.          break;
  800.       case '\t':
  801.          Xpos += 64;
  802.          break;
  803.       case '\n':
  804.          Line_Feed();
  805.          break;
  806.       case '\r':
  807.          Xpos = LEFTEDGE;
  808.          break;
  809.       case '\b':
  810.          Xpos = max(Xpos-8,LEFTEDGE);
  811.          break;
  812.       case SCREENCLR:     /* page */
  813.          Xpos = LEFTEDGE;
  814.          Ypos = TOPEDGE;
  815.          SetAPen(myWindow->RPort,0);
  816.          RectFill(myWindow->RPort,2,10,Xmax-19,Ymax-2);
  817.          SetAPen(myWindow->RPort,1);
  818.          break;
  819.       case 7:     /* bell */
  820.          ClipBlit(myWindow->RPort,0,0,myWindow->RPort,0,0,Xmax,Ymax,0x50);
  821.          ClipBlit(myWindow->RPort,0,0,myWindow->RPort,0,0,Xmax,Ymax,0x50);
  822.          break;
  823.       default:
  824.          if (c>31)        /* only print non-control characters */
  825.          {
  826.             char c1;      /* temporary storage for character */
  827.  
  828.             if (Xpos > Xmax-31)
  829.             {
  830.                Xpos = LEFTEDGE;
  831.                Line_Feed();
  832.             }
  833.             c1 = c;
  834.             Move(myWindow->RPort,Xpos,Ypos);
  835.             Text(myWindow->RPort,&c1,1);
  836.             Xpos += 8;
  837.          }
  838.    } /* end of switch */
  839.    SetDrMd(myWindow->RPort,COMPLEMENT);
  840.  
  841.    if (CursorChanged) XOR_Cursor();
  842. }
  843.  
  844. Line_Feed()
  845. {
  846.    if (Ypos > Ymax-12)
  847.       ScrollRaster(myWindow->RPort,0,8,2,11,Xmax-20,Ymax-2);
  848.     else
  849.       Ypos += 8;
  850. }
  851.  
  852. XOR_Cursor()
  853. {
  854.    int cx, cy;
  855.  
  856.    CursorON = !CursorON;
  857.  
  858.    cy = Ypos;
  859.    cx = Xpos+8;
  860.    while (cx > (Xmax-23)) cx -= 8;
  861.  
  862.    SetAPen(myWindow->RPort,3);
  863.    RectFill(myWindow->RPort,cx-8,cy-7,cx,cy+1);
  864.    SetAPen(myWindow->RPort,1);
  865. }
  866.  
  867. conocNL(c)            /*   really a putc command */
  868. int c;
  869. {
  870.    if (c == '\n') conoc('\r');
  871.    conoc(c);
  872. }
  873.  
  874.  
  875. /*  C O N X O  --  Write x characters to the console terminal  */
  876.  
  877. conxo(x,s) char *s; int x;
  878. {
  879.    XOR_Cursor();
  880.    while (x-- > 0)
  881.    {
  882.       if (*s == '\n') conoc('\r');
  883.       conoc(*s++);
  884.    }
  885.    XOR_Cursor();
  886. }
  887.  
  888. /*  C O N O L  --  Write a line to the console terminal  */
  889.  
  890. conol(s)
  891. char *s;
  892. {
  893.    XOR_Cursor();
  894.    while (*s != '\0')
  895.    {
  896.       if (*s == '\n') conoc('\r');
  897.       conoc(*s++);
  898.    }
  899.    XOR_Cursor();
  900. }
  901.  
  902. conolNL(s) char *s;      /* really a puts */
  903. {
  904.    conol(s); conoc('\n'); conoc('\r');
  905. }
  906.  
  907.  
  908. /*  C O N O L A  --  Write an array of lines to the console terminal */
  909.  
  910. conola(s) char *s[]; {
  911.     int i;
  912.     for (i=0 ; *s[i] ; i++) conol(s[i]);
  913. }
  914.  
  915. /*  C O N O L L  --  Output a string followed by CRLF  */
  916.  
  917. conoll(s) char *s; {
  918.     conol(s);
  919.     conol("\r\n");
  920. }
  921.  
  922.  
  923. /*  C O N C H K  --  Check if characters available at console  */
  924.  
  925. conchk() {
  926.    return((conch == NODEF)? 0 : 1);
  927. }
  928.  
  929.  
  930.  
  931. /*  C O N I N C  --  Get a character from the console  */
  932.  
  933.  
  934. coninc(timo)
  935. int timo;
  936. {
  937.    int class,code,qualifier;
  938.    struct IntuiMessage *NewMessage;
  939.    int NoChar = -1;
  940.    int c;
  941.    extern struct Message *GetMsg ();
  942.  
  943. /* Get buffered character, if any */
  944.    if (conch != NODEF)
  945.    {
  946.       c = conch;
  947.       conch = NODEF;
  948.       return(c);
  949.    }
  950.  
  951.    if (timo>0) StartTimer(timo,0);
  952.  
  953.    while (NoChar)
  954.    {
  955.       if (timo)
  956.       {
  957.          Wait ((1 << myWindow->UserPort->mp_SigBit) | (1 << TimerMsgPort.mp_SigBit));
  958.          if (CheckIO(&TimerIO)) return(-1);
  959.       } else {
  960.          Wait(1 << myWindow->UserPort->mp_SigBit);
  961.       }
  962.       while (NewMessage = (struct IntuiMessage *)GetMsg(myWindow->UserPort))
  963.       {
  964.          class = NewMessage->Class;
  965.          code = NewMessage->Code;
  966.          qualifier = NewMessage->Qualifier;
  967.          ReplyMsg(NewMessage);
  968.          switch (class)
  969.          {
  970.             case CLOSEWINDOW:
  971.                doexit(0);
  972.                break;
  973.  
  974.             case NEWSIZE:
  975.                Xmax = myWindow->Width;
  976.                Ymax = myWindow->Height;
  977.                conoc(SCREENCLR);
  978.                cmini();
  979.                prompt();
  980.                break;
  981.  
  982.             case RAWKEY:
  983.                NoChar = UnMap(&c,code,qualifier);
  984.                if (c == '\r') c = '\n';
  985.                if (c == 127)  c = '\b';
  986.                break;
  987.  
  988.          }
  989.       }
  990.    }
  991.    if (timo>0) AbortIO(&TimerIO);
  992.    return(c);
  993. }
  994.  
  995. UnMap(c,code,qualifier)
  996. int *c;
  997. int code, qualifier;
  998. {
  999.    int CapsLock, ShiftType, MapType, MetaChar;
  1000.  
  1001.    static char KeyCode [] [2] [96] =
  1002.    {
  1003.       {      /* normal keys */
  1004.          {      /* unshifted */
  1005.          '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\', NODEF, '0',
  1006.                'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',      NODEF, '1', '2', '3',
  1007.                 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',  NODEF, NODEF, '4', '5', '6', NODEF,
  1008.                  'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',       NODEF,   '.', '7', '8', '9',
  1009.  
  1010.        /*   SP  BS  TAB  RETURN ENTER ESC DEL  */
  1011.             ' ', 8, '\t', '\r', '\r', 27, 127, NODEF, NODEF, NODEF, '-', NODEF,
  1012.        /*   UP     DOWN   LEFT,  RIGHT, */
  1013.             NODEF, NODEF, NODEF, NODEF,
  1014.        /*  Function keys 1 to 10 */
  1015.             NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF,
  1016.        /*                                      help */
  1017.             NODEF, NODEF, NODEF, NODEF, NODEF, '?'
  1018.          },
  1019.          {      /* shifted */
  1020.          '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|',  NODEF, '0',
  1021.                'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}',      NODEF, '1', '2', '3',
  1022.                 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"',   NODEF, NODEF, '4', '5', '6', NODEF,
  1023.                  'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?',       NODEF,   '.', '7', '8', '9',
  1024.  
  1025.        /*   SP  BS  TAB  RETURN ENTER ESC DEL  */
  1026.             ' ', 8, '\t', '\r', '\r', 27, 127, NODEF, NODEF, NODEF, '-', NODEF,
  1027.        /*   UP     DOWN   LEFT,  RIGHT, */
  1028.             NODEF, NODEF, NODEF, NODEF,
  1029.        /*  Function keys 1 to 10 */
  1030.             NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF,
  1031.        /*                                      help */
  1032.             NODEF, NODEF, NODEF, NODEF, NODEF, '?'
  1033.          }
  1034.       },
  1035.  
  1036.       {      /*  Control Keys */
  1037.          {      /* unshifted */
  1038.          '`', '1',  0 , '3', '4', '5',  30, '7', '8', '9', '0',  31, '=',  28,  NODEF, '0',
  1039.                 17,  23,  5 ,  18,  20,  25,  21,  9 ,  15,  16,  27,  29,      NODEF, '1', '2', '3',
  1040.                  1 ,  19,  4 ,  6 ,  7 ,  8 ,  10,  11,  12, ';', '\'',  NODEF, NODEF, '4', '5', '6', NODEF,
  1041.                   26,  24,  3 ,  22,  2 ,  14,  13, ',', '.', '/',       NODEF,   '.', '7', '8', '9',
  1042.  
  1043.        /*   SP  BS  TAB  RETURN ENTER ESC DEL  */
  1044.             ' ', 8, '\t', '\r', '\r', 27, 127, NODEF, NODEF, NODEF, '-', NODEF,
  1045.        /*   UP     DOWN   LEFT,  RIGHT, */
  1046.             NODEF, NODEF, NODEF, NODEF,
  1047.        /*  Function keys 1 to 10 */
  1048.             NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF,
  1049.        /*                                      help */
  1050.             NODEF, NODEF, NODEF, NODEF, NODEF, '?'
  1051.          },
  1052.          {      /* shifted */
  1053.          '~', '!',  0 , '#', '$', '%',  30, '&', '*', '(', ')',  31, '+',  28,  NODEF, '0',
  1054.                 17,  23,  5 ,  18,  20,  25,  21,  9 ,  15,  16,  27,  29,      NODEF, '1', '2', '3',
  1055.                  1 ,  19,  4 ,  6 ,  7 ,  8 ,  10,  11,  12, ':', '"',   NODEF, NODEF, '4', '5', '6', NODEF,
  1056.                   26,  24,  3 ,  22,  2 ,  14,  13, '<', '>', '?',       NODEF,   '.', '7', '8', '9',
  1057.  
  1058.        /*   SP  BS  TAB  RETURN ENTER ESC DEL  */
  1059.             ' ', 8, '\t', '\r', '\r', 27, 127, NODEF, NODEF, NODEF, '-', NODEF,
  1060.        /*   UP     DOWN   LEFT,  RIGHT, */
  1061.             NODEF, NODEF, NODEF, NODEF,
  1062.        /*  Function keys 1 to 10 */
  1063.             NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF, NODEF,
  1064.        /*                                      help */
  1065.             NODEF, NODEF, NODEF, NODEF, NODEF, '?'
  1066.          }
  1067.       }
  1068.    };
  1069.  
  1070.  
  1071.    if (code > 95)
  1072.    {
  1073.       *c = NODEF;
  1074.       return(-1);
  1075.    } else {
  1076.       ShiftType = (qualifier & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT)) ? SHIFTED : UNSHIFT;
  1077.       CapsLock  = (qualifier & IEQUALIFIER_CAPSLOCK) ? 1 : 0;
  1078.       MapType   = (qualifier & IEQUALIFIER_CONTROL)  ? CTRLKEYS : NRMLKEYS;
  1079.       MetaChar  = (qualifier & (IEQUALIFIER_LALT | IEQUALIFIER_RALT)) ? METABIT : 0;
  1080.       if ((*c = KeyCode [MapType] [ShiftType] [code]) == NODEF) {
  1081.          return(-1);
  1082.       } else {
  1083.          if (CapsLock && *c >= 'a' && *c <= 'z') *c -= 32;
  1084.          *c |= MetaChar;
  1085.          return(0);
  1086.       }
  1087.    }
  1088. }
  1089.  
  1090.  
  1091. /*  C O N T T I  --  Get character from console or tty, whichever comes  */
  1092. /*   first.  This is used in conect() when NO_FORK is defined.  */
  1093. /*   src is returned with 1 if the character came from the comm. line, */
  1094. /*   0 if it was from the console, and with -1 if there was any error.  */
  1095.  
  1096.  
  1097. contti(c,src)
  1098. int *c;
  1099. int *src;
  1100. {
  1101.    int class,code,qualifier;
  1102.    struct IntuiMessage *NewMessage;
  1103.    int NoChar = -1;
  1104.  
  1105.    while (NoChar)
  1106.    {
  1107.       if (CheckIO(ReadRequest))
  1108.       {
  1109.          *src = 1;
  1110.          WaitIO(ReadRequest);
  1111.          *c = ReadPort[0] & 0x7F;
  1112.          BeginIO(ReadRequest);
  1113.          NoChar = 0;
  1114.       } else {
  1115.          Wait ((1 << myWindow->UserPort->mp_SigBit) |
  1116.                (1 << ReadRequest->IOSer.io_Message.mn_ReplyPort->mp_SigBit));
  1117.          while (NewMessage = (struct IntuiMessage *)GetMsg(myWindow->UserPort))
  1118.          {
  1119.             class = NewMessage->Class;
  1120.             code = NewMessage->Code;
  1121.             qualifier = NewMessage->Qualifier;
  1122.             ReplyMsg(NewMessage);
  1123.             switch (class)
  1124.             {
  1125.                case CLOSEWINDOW:
  1126.                   doexit(0);
  1127.                   break;
  1128.  
  1129.                case NEWSIZE:
  1130.                   Xmax = myWindow->Width;
  1131.                   Ymax = myWindow->Height;
  1132.                   conoc(SCREENCLR);
  1133.                   break;
  1134.  
  1135.                case RAWKEY:
  1136.                   NoChar = UnMap(c,code,qualifier);
  1137.                   *src = 0;
  1138.                   break;
  1139.  
  1140.             }
  1141.          }
  1142.       }
  1143.    }
  1144.    return(0);
  1145. }
  1146.  
  1147.  
  1148. /*  C A N C I O  --  Cancel any pending i/o requests on the console or the
  1149.         comm. line.
  1150. */
  1151.  
  1152. cancio()  {
  1153.    ttflui();
  1154. }
  1155.