home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archimedes / aratio.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  18KB  |  653 lines

  1. /* -> c.ckatio
  2.  */
  3.  
  4. /*  C K A T I O  */
  5.  
  6. #include "ckcdeb.h"
  7. #ifdef ANSI
  8. #include <signal.h>
  9. #include <time.h>
  10. #include "ckatio.h"
  11. #include "ckafio.h"
  12. #include "ckamis.h"
  13. #include <string.h>
  14. #endif
  15.  
  16. #ifdef ARTHUR
  17. #include "plib.h"
  18. #endif
  19.  
  20. #ifdef ARTHUR
  21. #define SIGQUIT SIGTERM
  22. #endif
  23.  
  24. #ifdef PANOS
  25. extern int XSetEventStatus() asm;
  26. #define SIGon 1
  27. #define SIGoff 0
  28. extern     XStandardTime() asm;
  29. extern     XCloseStream() asm;
  30. extern     XSFlushOutput() asm;
  31. extern int XSWriteByte() asm; 
  32. extern int XSBlockWrite() asm;
  33. extern int XSBlockRead() asm;
  34. extern int XSReadByte() asm;
  35. extern int XBytesOutstanding() asm;
  36. extern int XFindInput() asm;
  37. extern int XFindOutput() asm;
  38. #endif
  39.  
  40. char *ckxv   = "Arthur tty I/O, July 23 1985";
  41. char *ckxsys = "Acorn Computers Arthur";
  42.  
  43. /* C-Kermit interrupt, terminal control & i/o functions for Arthur */
  44.  
  45. /*
  46. Author: Graham Toal & Richard Cownie, VLSI Tools Group (graham%acorn@ukc)
  47. Copyright (C) 1986, Acorn Computers Plc
  48.  
  49. Based on originial code & interface by: Frank da Cruz (SY.FDC@CU20B),
  50. Columbia University Center for Computing Activities, January 1985.
  51. Copyright (C) 1985, Trustees of Columbia University in the City of New York.
  52.  
  53. Permission is granted to any individual or institution to use, copy, or
  54. redistribute this software so long as it is not sold for profit, provided
  55. this copyright notice is retained. 
  56.  
  57. Modified by Cosmos Nicolaou 26/8/87 to run over Arthur 
  58.  
  59. */
  60.  
  61. /*
  62.  Variables available to outside world:
  63.  
  64.   dftty  -- Pointer to default tty name string, like "/dev/tty".
  65.   dfloc  -- 0 if dftty is console, 1 if external line.
  66.   dfprty -- Default parity
  67.   dfflow -- Default flow control
  68.   ckxech -- Flag for who echoes console typein:
  69.               1 - The program (system echo is turned off)
  70.               0 - The system (or front end, or terminal).
  71.             functions that want to do their own echoing should check this
  72.             flag before doing so.
  73.   backgrd -- Flag indicating program executing in background
  74.               - not applicable
  75.  
  76.  Functions for assigned communication line (either external or console tty):
  77.  
  78.   ttopen(ttname,local,mdmtyp) -- Open the named tty for exclusive access.
  79.   ttclos()               -- Close & reset the tty, releasing any access lock.
  80.   tthang()               -- Hangup phone line
  81.   ttsndb()               -- Send a break signal
  82.   ttpkt(speed,flow)      -- Put the tty in packet mode and set the speed.
  83.   ttvt(speed,flow)       -- Put the tty in virtual terminal mode.
  84.         or in DIALING or CONNECTED modem control state.
  85.   ttres()                -- Restore terminal to normal mode
  86.   ttinl(dest,max,timo)    -- Timed read line from the tty.
  87.   ttinc(timo)             -- Timed read character from tty.
  88.   ttchk()                 -- See how many characters in tty input buffer.
  89.   ttxin(n,buf)            -- Read n characters from tty (untimed).
  90.   ttol(string,length)     -- Write a string to the tty.
  91.   ttoc(c)                 -- Write a character to the tty.
  92.   ttflui()                -- Flush tty input buffer.
  93. */
  94.  
  95.  
  96. /*
  97. Functions for console terminal:
  98.  
  99.   congm()   -- Get console terminal modes.
  100.   concb(esc) -- Put the console in single-character wakeup mode with no echo.
  101.   conbin(esc) -- Put the console in binary (raw) mode.
  102.   conres()  -- Restore the console to mode obtained by congm().
  103.   conoc(c)  -- Unbuffered output, one character to console.
  104.   conol(s)  -- Unbuffered output, null-terminated string to the console.
  105.   conoll(s) -- Unbuffered output, string + CR LF to the console.
  106.   conola(s) -- Unbuffered output, array of strings to the console.
  107.   conxo(n,s) -- Unbuffered output, n characters to the console.
  108.   conchk()  -- Check if characters available at console (bsd 4.2).
  109.     Check if escape char (^\) typed at console (System III/V).
  110.   coninc(timo)  -- Timed get a character from the console.
  111.   conin()   -- untimed get from console (new)
  112.   conint()  -- Enable terminal interrupts on the console if not background.
  113.   connoi()  -- Disable terminal interrupts on the console if not background.
  114.  
  115. Time functions
  116.  
  117.   msleep(m) -- Millisecond sleep
  118.   ztime(&s) -- Return pointer to date/time string
  119. */
  120.  
  121.  
  122. /* Declarations */
  123.  
  124. #include <stdio.h>
  125.  
  126.     char *dftty = "RS423:";             /* dftty is the device name of
  127.                                            the default device for file
  128.                                            transfer */
  129.     int dfloc = 1;                      /* dfloc: 0 if dftty is the user's
  130.                                            console terminal, 1 if an
  131.                                            external line */
  132.     int dfprty = 0;         /* Parity (0 = none) */
  133.     int dfflow = 1;         /* Xon/Xoff flow control */
  134.     int backgrd = 0;            /* Assume in foreground (no '&' ) */
  135.  
  136.     int ckxech = 0;                     /* 0 if system normally echoes 
  137.                                            console characters, else 1 */
  138.  /* Arthur - changed from 0 to -1 */
  139.     int hostin = -1;
  140.     int hostout = -1; /* Handles to RS423: in each direction. */
  141.     int OldMode;      /* old ckxech */
  142.     int ConsoleIn = -1;
  143.     int ConsoleOut = -1; /* Currently selected console IO streams */
  144.     int OldConsoleIn;    /* Save-variable for changing stdin from
  145.                              raw to cooked... */
  146.     int rawkb;
  147.     int filteredkb;     /* handles to raw & filtered kb streams */
  148.  
  149. #ifdef ANSI
  150. static int
  151.     conif = 0, conesc = 0;  /* conesc - has <esc> been typed? */
  152. #else
  153. static int
  154.     conif = 0; conesc = 0;  /* conesc - has <esc> been typed? */
  155. #endif
  156.  
  157. /*******************************************************/
  158. /*  T T O P E N  --  Open a tty for exclusive access.  */
  159. /*  Returns 0 on success, -1 on failure.               */
  160. /*******************************************************/
  161.  
  162. #ifdef ANSI
  163. int
  164. #endif
  165. ttopen(ttname,lcl,modem) char *ttname; int *lcl, modem; {
  166. /*
  167.   If called with lcl < 0, sets value of lcl as follows:
  168.   0: the terminal named by ttname is the job's controlling terminal.
  169.   1: the terminal named by ttname is not the job's controlling terminal.
  170. */
  171.   hostin  = XFindInput(ttname, strlen(ttname));
  172.   hostout = XFindOutput(ttname, strlen(ttname));
  173.   if ((hostin > 0) && (hostout > 0)) {
  174.     *lcl = 1;      /* RS423: <> TT: */
  175.     return(0);
  176.   } else {
  177.     return(-1);
  178.   }
  179. }
  180.  
  181. /*********************************************************/
  182. /*  T T C L O S  --  Close the TTY, releasing any lock.  */
  183. /*********************************************************/
  184.  
  185. #ifdef ANSI
  186. void
  187. #endif
  188. ttclos() {
  189.     if (hostin >= 0)
  190.     {
  191.         XCloseStream(hostin);
  192.         hostin = -1;
  193.     }
  194.  
  195.     if (hostout >= 0)
  196.     {
  197.         XCloseStream(hostout);
  198.         hostout = -1;
  199.     }
  200.   
  201. }
  202.  
  203. /*************************************/
  204. /*  T T H A N G -- Hangup phone line */
  205. /*************************************/
  206. #ifdef ANSI
  207. void
  208. #endif
  209. tthang() {}
  210.  
  211. /*******************************************************/
  212. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  213. /*******************************************************/
  214. #ifdef ANSI
  215. void
  216. #endif
  217. ttres() {}
  218.  
  219. /*****************************************************************/
  220. /*  T T P K T  --  Condition the communication line for packets. */
  221. /*                 or for modem dialing                          */
  222. /*  If called with speed > -1, also set the speed.               */
  223. /*  Returns 0 on success, -1 on failure.                         */
  224. /*****************************************************************/
  225.  
  226. #ifdef ANSI
  227. int
  228. #endif
  229. ttpkt(speed,flow) int speed, flow; {
  230.   if (speed > -1) setbaud(speed);
  231.   return(0);
  232. }
  233.  
  234. /*************************************************************************/
  235. /*  T T V T -- Condition communication line for use as virtual terminal  */
  236. /*************************************************************************/
  237.  
  238. #ifdef ANSI
  239. void
  240. #endif
  241. ttvt(speed,flow) int speed, flow; {
  242.   if (speed > -1) setbaud(speed);
  243. }
  244.  
  245. /********************************************/
  246. /*  T T F L U I  --  Flush tty input buffer */
  247. /********************************************/
  248.  
  249. #ifdef ANSI
  250. void
  251. #endif
  252. ttflui() {
  253.   /* loop around testing XBytesOutstanding() until none left */
  254.   while (XBytesOutstanding(hostin) > 0) XSReadByte(hostin);
  255. }
  256.  
  257. /***********************************************/
  258. /*  C O N I N T  --  Console Interrupt setter  */
  259. /***********************************************/
  260.  
  261. #ifdef ANSI
  262. void
  263. #endif
  264. #ifdef ANSI
  265. esctrp( i ) int i; {                      /* trap console escapes */
  266. #else
  267. esctrp() {                       /* trap console escapes */
  268. #endif
  269.     conesc = 1; signal(SIGQUIT,0);
  270. }
  271.  
  272. #ifdef ANSI
  273. void
  274. #endif
  275. #ifdef ANSI
  276. conint(f) void (*f)(); {         /* Set an interrupt trap. */
  277. #else
  278. conint(f) int (*f)(); {         /* Set an interrupt trap. */
  279. #endif
  280.     signal(SIGINT, &esctrp);       /* console escape in pkt modes */
  281.     conesc = 0;                      /* clear out pending escapes */
  282.     signal(SIGQUIT, f);           /* Function to trap to. */
  283.     conif = 1;             /* Flag console interrupts on. */
  284. }
  285.  
  286. /*******************************************************/
  287. /*  C O N N O I  --  Reset console terminal interrupts */
  288. /*******************************************************/
  289.  
  290. #ifdef ANSI
  291. void
  292. #endif
  293. connoi() {
  294.   signal(SIGQUIT, 0);
  295.   conif = 0;
  296. }
  297.  
  298. /****************************************************************************/
  299. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer */
  300. /****************************************************************************/
  301.  
  302. #ifdef ANSI
  303. int
  304. #endif
  305. ttchk() {
  306.   return(XBytesOutstanding(hostin));
  307. }
  308.  
  309. /***********************************************************/
  310. /*  T T X I N  --  Get n characters from tty input buffer  */
  311. /***********************************************************/
  312.  
  313. #ifdef ANSI
  314. void
  315. #endif
  316. ttxin(n,buf) int n; char *buf; {
  317.   /* XSBlockRead(hostin, n, buf); may not work in Arthur v1.1 */
  318. #ifdef ANSI
  319.   while (n-- > 0) *buf++ = XSReadByte(hostin);
  320. #else
  321.   while (n-- > 0) XSReadByte(hostin, *buf++);
  322. #endif
  323. }
  324.  
  325. /*******************************************************/
  326. /*  T T O L  --  Similar to "ttinl", but for writing.  */
  327. /*******************************************************/
  328.  
  329. #ifdef ANSI
  330. void
  331. #endif
  332. ttol(s,n) int n; char *s; {
  333.  
  334.   XSBlockWrite(hostout, n, s); /*may not work in Arthur v1.1 */
  335. /*  while (n-- > 0) XSWriteByte(hostout, *s++);*/
  336.   XSFlushOutput(hostout);
  337. }
  338.  
  339. /***************************************************************/
  340. /*  T T O C  --  Output a character to the communication line  */
  341. /***************************************************************/
  342.  
  343. #ifdef ANSI
  344. void
  345. #endif
  346. ttoc(
  347. #ifdef ANSI
  348. char c)
  349. #else
  350. c) char c;
  351. #endif
  352. {
  353.   XSWriteByte(hostout, c); XSFlushOutput(hostout);
  354. }
  355.  
  356. /**************************************************************************/
  357. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  358. /**************************************************************************/
  359.  
  360. #ifdef ANSI
  361. int
  362. #endif
  363. ttinl(dest,max,timo,eol) int max,timo,eol; char *dest; 
  364. {
  365. /*
  366.   If no break character encountered within "max", return "max" characters,
  367.   with disposition of any remaining characters undefined.  Otherwise, return
  368.   the characters that were read, including the break character, in "dest" and
  369.   the number of characters read as the value of function, or 0 upon end of
  370.   file, or -1 if an error occurred.  Times out & returns error if not
  371.   completed within "timo" seconds.
  372. */
  373.   int zone, ch, i, j, n, ngot = 0;
  374.   timeval start,now;
  375.   if (timo) gettimeofday(&start, &zone);
  376.   if (max == 0) return(0);
  377.   for (;;) {
  378.     for (i = 0; i < 16; i++) {
  379.       n = XBytesOutstanding(hostin);
  380.       for (j = 0; j < n; j++) {
  381.         if ((dest[ngot++] = XSReadByte(hostin)) == eol) return(ngot);
  382.     if (ngot >= max) return(ngot);
  383.       }
  384.     }
  385.     if (timo) {
  386.       gettimeofday(&now, &zone);
  387.       if (timerdiff(start, now) > (100*timo)) return(-1); /* timed out */
  388.     }
  389.   }
  390. }
  391.  
  392. /****************************************************************/
  393. /*  T T I N C --  Read a character from the communication line  */
  394. /****************************************************************/
  395.  
  396. #ifdef ANSI
  397. int
  398. #endif
  399. ttinc(timo) int timo; 
  400. {
  401.   int zone,j; timeval start,now;
  402.   if (timo) gettimeofday(&start, &zone);
  403.   for(;;) {
  404.     for(j = 0; j < 32; j++) {
  405.       if (XBytesOutstanding(hostin)) return(XSReadByte(hostin)); /* got ch */
  406.     }
  407.     if (timo) {
  408.       gettimeofday(&now, &zone);
  409.       if (timerdiff(start, now) > (100*timo)) return(-1); /* timed out */
  410.     }
  411.   }
  412. }
  413.  
  414. /******************************************/
  415. /*  T T S N D B  --  Send a BREAK signal  */
  416. /******************************************/
  417.  
  418. #ifdef ANSI
  419. void
  420. #endif
  421. ttsndb() {}
  422.  
  423. /******************************************************/
  424. /*  M S L E E P  --  Millisecond version of sleep().  */
  425. /******************************************************/
  426.  
  427. #ifdef ANSI
  428. int
  429. #endif
  430. msleep(m) int m; 
  431. {
  432.   int zone; timeval start,now;
  433.   gettimeofday(&start, &zone);
  434.   for(;;) {
  435.     gettimeofday(&now, &zone);
  436.     if ((10 * timerdiff(start, now)) > m) return(0);
  437.   }
  438. }
  439.  
  440. /********************************************/
  441. /*  Z T I M E  --  Return date/time string  */
  442. /********************************************/
  443.  
  444. #ifdef ANSI
  445. void
  446. #endif
  447. ztime(s) char **s; 
  448. { static char timestring[32];
  449.   XStandardTime(timestring, 32); *s = timestring;
  450. }
  451.  
  452. /************************************************/
  453. /*  C O N G M  --  Get console terminal modes.  */
  454. /************************************************/
  455.  
  456. #ifdef ANSI
  457. void
  458. #endif
  459. congm() {
  460. /*
  461.  Saves current console mode, and establishes variables for switching between 
  462.  current (presumably normal) mode and other modes.
  463. */
  464. /* Arthur version does this by assigning stdout to local saved copy */
  465. #ifndef ARTHUR
  466. /* What's the point when there is no difference in mode! */
  467. OldConsoleIn = ConsoleIn;
  468. #endif
  469. OldMode = ckxech;
  470. }
  471.  
  472. /***********************************************/
  473. /*  C O N C B --  Put console in cbreak mode.  */
  474. /***********************************************/
  475.  
  476. #ifdef ANSI
  477. int
  478. #endif
  479. concb(
  480. #ifdef ANSI
  481. char esc)
  482. #else
  483. esc) char esc;
  484. #endif
  485. {
  486.  
  487. #ifdef ARTHUR
  488.   if( ConsoleIn == -1 )
  489.     ConsoleIn = XFindInput("RAWKB:", 6);
  490.  
  491.   if( ConsoleOut == -1 )
  492.     ConsoleOut = XFindOutput("VDU:", 4);
  493.  
  494. #else
  495.   ConsoleIn = XFindInput("RAWKB:", 6);
  496.   ConsoleOut = XFindOutput("VDU:", 4);
  497. #endif
  498.  
  499.   ckxech = 1; /* no echo */
  500.   return(0);
  501. }
  502.  
  503. /*************************************************/
  504. /*  C O N B I N  --  Put console in binary mode  */
  505. /*************************************************/
  506.  
  507. #ifdef ANSI
  508. int
  509. #endif
  510. conbin(
  511. #ifdef ANSI
  512. char esc)
  513. #else
  514. esc) char esc;
  515. #endif
  516. {
  517.  
  518. #ifdef ARTHUR
  519.   if( ConsoleIn == -1 )
  520.     ConsoleIn = XFindInput("RAWKB:", 6);
  521.  
  522.   if( ConsoleOut == -1 )
  523.     ConsoleOut = XFindOutput("VDU:", 4);
  524. #else
  525.   ConsoleIn = XFindInput("RAWKB:", 6);
  526.   ConsoleOut = XFindOutput("VDU:", 4);
  527. #endif
  528.  
  529.   ckxech = 1; /* No echo */
  530.   return(0);
  531. }
  532.  
  533. /***************************************************/
  534. /*  C O N R E S  --  Restore the console terminal  */
  535. /***************************************************/
  536.  
  537. #ifdef ANSI
  538. int
  539. #endif
  540. conres() {
  541. #ifndef ARTHUR
  542. /* What's the point when there is no difference in mode! */
  543.   ConsoleIn = OldConsoleIn;
  544. #endif
  545.   ckxech = OldMode;
  546.   return(0);
  547. }
  548.  
  549. /***************************************************************/
  550. /*  C O N O C  --  Output a character to the console terminal  */
  551. /***************************************************************/
  552.  
  553. #ifdef ANSI
  554. void
  555. #endif
  556. conoc(
  557. #ifdef ANSI
  558. char c)
  559. #else
  560. c) char c;
  561. #endif
  562. {
  563.   XSWriteByte(ConsoleOut, c); XSFlushOutput(ConsoleOut);
  564. }
  565.  
  566. /***************************************************************/
  567. /*  C O N X O  --  Write x characters to the console terminal  */
  568. /***************************************************************/
  569.  
  570. #ifdef ANSI
  571. void
  572. #endif
  573. conxo(x,s) char *s; int x; {
  574.   XSBlockWrite(ConsoleOut, x, s); XSFlushOutput(ConsoleOut);
  575. }
  576.  
  577. /*********************************************************/
  578. /*  C O N O L  --  Write a line to the console terminal  */
  579. /*********************************************************/
  580.  
  581. #ifdef ANSI
  582. void
  583. #endif
  584. conol(s) char *s; {
  585.   conxo(strlen(s), s);
  586. }
  587.  
  588. /*********************************************************************/
  589. /*  C O N O L A  --  Write an array of lines to the console terminal */
  590. /*********************************************************************/
  591.  
  592. #ifdef ANSI
  593. void
  594. #endif
  595. conola(s) char *s[]; {
  596.   int i;
  597.   for (i=0; *s[i]; i++) conol(s[i]);
  598. }
  599.  
  600. /*******************************************************/
  601. /*  C O N O L L  --  Output a string followed by CRLF  */
  602. /*******************************************************/
  603.  
  604. #ifdef ANSI
  605. void
  606. #endif
  607. conoll(s) char *s; {
  608.   conol(s); conoc('\r'); conoc('\n');
  609. }
  610.  
  611. /***************************************************************/
  612. /*  C O N C H K  --  Check if characters available at console  */
  613. /***************************************************************/
  614.  
  615. #ifdef ANSI
  616. int
  617. #endif
  618. conchk() {
  619.   if (conesc) { conesc = 0; return(1); }
  620.   return(XBytesOutstanding(ConsoleIn));
  621. }
  622.  
  623. /*******************************************************/
  624. /*  C O N I N C  --  Get a character from the console  */
  625. /*******************************************************/
  626.  
  627. #ifdef ANSI
  628. int
  629. #endif
  630. coninc(timo) int timo; 
  631. {
  632.   int zone,j; timeval start,now;
  633.   if (timo) gettimeofday(&start, &zone);
  634.   for(;;) {
  635.     for(j = 0; j < 32; j++) { if (conchk()) return(conin()); }
  636.     if (timo) {
  637.       gettimeofday(&now, &zone);
  638.       if (timerdiff(start, now) > (100*timo)) return(-1); /* timed out */
  639.     }
  640.   }
  641. }
  642.  
  643. /*******************************************************/
  644. /*  C O N I N    --  Get a character from the console  */
  645. /*******************************************************/
  646.  
  647. #ifdef ANSI
  648. int
  649. #endif
  650. conin() {
  651.   return(XSReadByte(ConsoleIn));
  652. }
  653.