home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / apps / text_ed / elv16b2 / st / atari.c < prev    next >
C/C++ Source or Header  |  1992-05-13  |  7KB  |  361 lines

  1. /* atari.c */
  2.  
  3. /* Author:
  4.  *    Guntram Blohm
  5.  *    Buchenstrasse 19
  6.  *    7904 Erbach, West Germany
  7.  *    Tel. ++49-7305-6997
  8.  *    sorry - no regular network connection
  9.  */
  10.  
  11. /*
  12.  * This file contains the 'standard' functions which are not supported
  13.  * by Atari/Mark Williams, and some other TOS-only requirements.
  14.  */
  15.  
  16. #include "config.h"
  17. #include "vi.h"
  18.  
  19. #if TOS || MINT
  20. #include <osbind.h>
  21.  
  22. # if !MINT /* MiNT's library is a little more complete :-) */
  23.  
  24. /* vi uses mode==0 only ... */
  25. int access(file, mode)
  26.     char *file;
  27. {
  28.     int fd=Fopen(file, 0);
  29.     if (fd<0)
  30.         return -1;
  31.     Fclose(fd);
  32.     return 0;
  33. }
  34.  
  35. char *mktemp(template)
  36.     char *template;
  37. {
  38.     return template;
  39. }
  40.  
  41. # endif
  42.  
  43. # ifndef __GNUC__
  44. char *getcwd(buf, size)
  45.     char *buf;
  46. {
  47.     if (size < 2 + 64)
  48.         return (char *)0;
  49.     buf[0] = Dgetdrv() + 'A';
  50.     buf[1] = ':';
  51.     Dgetpath(buf + 2, 0);
  52.     return buf;
  53. }
  54. # endif
  55.  
  56. /* read -- text mode, compress \r\n to \n
  57.  * warning: might fail when maxlen==1 and at eol
  58.  * ...no longer :-)  -nox
  59.  */
  60.  
  61. int tread(fd, buf, maxlen)
  62.     int fd;
  63.     char *buf;
  64.     int maxlen;
  65. {
  66.     int i, j, nread=read(fd, buf, (unsigned)maxlen);
  67.  
  68.     if (nread && buf[nread-1]=='\r')
  69.     {
  70.         if (--nread)
  71.             lseek(fd, -1l, 1);
  72.         else {
  73.             if (read(fd, buf, 1) && *buf != '\n') {
  74.                 *buf = '\r';
  75.                 lseek(fd, -1l, 1);
  76.             }
  77.             return 1;
  78.         }
  79.     }
  80.     for (i=j=0; j<nread; i++,j++)
  81.     {    if (buf[j]=='\r' && buf[j+1]=='\n')
  82.             j++;
  83.         buf[i]=buf[j];
  84.     }
  85.     return i;
  86. }
  87.  
  88. int twrite(fd, buf, maxlen)
  89.     int fd;
  90.     char *buf;
  91.     int maxlen;
  92. {
  93. # if 1
  94.     int i, j;
  95.     /* uh, we can have that easier...  -nox */
  96.     static char writbuf[BLKSIZE*2];
  97.  
  98.     for (i=j=0; j<maxlen; )
  99.     {
  100.         if ((writbuf[i++]=buf[j++])=='\n')
  101.         {    writbuf[i-1]='\r';
  102.             writbuf[i++]='\n';
  103.         }
  104.     }
  105.     /* ...and make the result a little bit more reasonable too :-)  */
  106.     if (!i || !(maxlen = write(fd, writbuf, (unsigned)i)))
  107.         return 0;
  108.     return j - i + maxlen;
  109. }
  110. # else
  111.     int i, j, nwritten=0, hadnl=0;
  112.     char writbuf[BLKSIZE];
  113.  
  114.     for (i=j=0; j<maxlen; )
  115.     {
  116.         if ((writbuf[i++]=buf[j++])=='\n')
  117.         {    writbuf[i-1]='\r';
  118.             if (i<BLKSIZE)
  119.                 writbuf[i++]='\n';
  120.             else
  121.                 hadnl=1;
  122.         }
  123.         if (i==BLKSIZE)
  124.         {
  125.             write(fd, writbuf, (unsigned)i);
  126.             i=0;
  127.         }
  128.         if (hadnl)
  129.         {
  130.             writbuf[i++]='\n';
  131.             hadnl=0;
  132.         }
  133.     }
  134.     if (i)
  135.         write(fd, writbuf, (unsigned)i);
  136.     return j;
  137. }
  138. # endif
  139.  
  140. /* The "timer" variable is used as a shadow of the system's timer.  Since the
  141.  * system's timer can only be accessed in Supervisor mode, we are forced to
  142.  * do a Supexec(gettime) to copy the system's timer in to the User-mode "timer"
  143.  * variable.
  144.  */
  145. static long timer;
  146. static void gettime()
  147. {
  148.     timer = *(long *)(0x4ba);
  149. }
  150. #endif /* TOS || MINT */
  151.  
  152. #if TOS
  153. /* This function implements a read-with-timeout from the keyboard. */
  154. /*ARGSUSED*/
  155. int ttyread(buf, len, time)
  156.     char    *buf;    /* where to store the gotten characters */
  157.     int    len;    /* maximum number of characters to get -- ignored */
  158.     int    time;    /* maximum time to allow for reading */
  159. {
  160.     int    pos=0;
  161.     long    l;
  162.     long    endtime;
  163.  
  164.     /* compute the ending time, in increments of 1/200th seconds */
  165.     (void) Supexec(gettime);
  166.     endtime = time * 20 + timer;
  167.  
  168.     /* wait until time runs out, or we get a keystroke */
  169.     while (!pos && (!time || (timer-endtime) < 0))
  170.     {
  171.         if (Bconstat(2))
  172.         {
  173.             l = Bconin(2);
  174.             buf[pos] = l;
  175.             if (buf[pos++] == '\0')
  176.             {
  177.                 buf[pos - 1] = '#';
  178.                 buf[pos++] = l >> 16;
  179.             }
  180.         }
  181.         (void) Supexec(gettime);
  182.     }
  183.     return pos;
  184. }
  185.  
  186. /* This function writes characters to the screen */
  187. ttywrite(buf, len)
  188.     char *buf;
  189.     int len;
  190. {
  191.     while (len--)
  192.         Bconout(2, *buf++);
  193. }
  194. #endif /* TOS */
  195.  
  196. #if MINT
  197. extern int __mint;
  198.  
  199. # include <signal.h>
  200. # include <setjmp.h>
  201.  
  202. static jmp_buf env;
  203.  
  204. /* ctputs(): same as tputs(), but recognizes #blink / #noblink to
  205.    set the cursormode (have to do it this way because those are not
  206.    available thru the ST's `VT52' emulator...) */
  207.  
  208. void ctputs(cp, affcnt, outfn)
  209.     char    *cp;
  210.     int    affcnt;
  211.     int    (*outfn)();
  212. {
  213.     if (!strcmp(cp, "#blink")) {
  214.         (void) Cursconf(2, 0);
  215.         return;
  216.     }
  217.     if (!strcmp(cp, "#noblink")) {
  218.         (void) Cursconf(3, 0);
  219.         return;
  220.     }
  221.     tputs(cp, affcnt, outfn);
  222. }
  223.  
  224. /* this turns those shifted cursor- / insert / home keys into
  225.    something useful (without this, they would return digits...) */
  226.  
  227. static long fixkey(l)
  228.     long    l;
  229. {
  230.     if (l < 0 || !(char) l)
  231.         return l;
  232.     switch ((unsigned char) (l >> 16)) {
  233.         case 72:        /* shift- ^    -> PgUp */
  234.             return 73l << 16;
  235.         case 80:        /* shift- v    -> PgDn */
  236.             return 81l << 16;
  237.         case 71:        /* shift-Home    -> End */
  238.             return 79l << 16;
  239.         case 75:        /* shift- <-    -> ctrl- <- */
  240.             return 115l << 16;
  241.         case 77:        /* shift- ->    -> ctrl- -> */
  242.             return 116l << 16;
  243.         case 82:        /* shift-Insert    -> Printscreen */
  244.             return 55l << 16;
  245.     }
  246.     return l;
  247. }
  248.  
  249. /*ARGSUSED*/
  250. static void dummy(signo)
  251.     int    signo;
  252. {
  253.     longjmp(env, 1);
  254. }
  255.  
  256. /* This function implements a read-with-timeout from the keyboard. */
  257. /*ARGSUSED*/
  258. int ttyread(buf, len, time)
  259.     char    *buf;    /* where to store the gotten characters */
  260.     int    len;    /* maximum number of characters to get -- ignored on terminals */
  261.     int    time;    /* maximum time to allow for reading */
  262. {
  263.     int    pos=0;
  264.     long    l;
  265.     long    endtime;
  266.     static    tty;    /* 'y' if reading from tty, or 'n' if not a tty */
  267.     extern    int got_winch;    /* flag from our getsize() SIGWINCH handler */
  268.  
  269.     /* do we know whether this is a tty or not? */
  270.     if (!tty)
  271.         tty = (isatty(0) ? 'y' : 'n');
  272.  
  273.     if (tty != 'y')
  274.         return read(0, buf, (unsigned) len);
  275.  
  276.     if (!__mint) {
  277.         /* no MiNT -> eat cycles :-)  */
  278.  
  279.         /* compute the ending time, in increments of 1/200th seconds */
  280.         (void) Supexec(gettime);
  281.         endtime = time * 20 + timer;
  282.  
  283.         /* wait until time runs out, or we get a keystroke */
  284.         while (!pos && (!time || (timer-endtime) < 0))
  285.         {
  286.             if (*o_stbios ? Bconstat(2) : Cconis())
  287.             {
  288.                 l = fixkey(*o_stbios ? Bconin(2) : Crawcin());
  289.                 if (l < 0)
  290.                     return l;
  291.                 buf[pos] = l;
  292.                 if (buf[pos++] == '\0')
  293.                 {
  294.                     buf[pos-1] = '#';
  295.                     buf[pos++] = l >> 16;
  296.                 }
  297.             }
  298.             (void) Supexec(gettime);
  299.         }
  300.         return pos;
  301.     }
  302.     /* MiNT is there -> do it with alarm()
  303.        [yes MiNT also has select(), but as of MiNT version 0.94
  304.         that won't yet work over e.g. a serial line...] */
  305.  
  306.     /* arrange for timeout */
  307. # if __GNUC__
  308.     signal(SIGALRM, (void (*)()) dummy);
  309. # else
  310.     signal(SIGALRM, dummy);
  311. # endif
  312.     alarm(time);
  313.  
  314.     if (setjmp(env))
  315.         return 0;
  316.     do {
  317.         l = fixkey(*o_stbios ? Bconin(2) : Crawcin());
  318.  
  319.         if (got_winch) {
  320.             got_winch = 0;
  321.             if (*o_lines != LINES || *o_columns != COLS) {
  322.                 *o_lines = LINES;
  323.                 *o_columns = COLS;
  324. #ifndef CRUNCH
  325.                 if (!wset)
  326.                 {
  327.                     *o_window = LINES - 1;
  328.                 }
  329. #endif
  330.                 if (mode != MODE_EX && mode != MODE_COLON)
  331.                     /* pretend the user hit ^L */
  332.                     buf[pos++] = ctrl('L');
  333.             }
  334.         }
  335.         if (l > 0 && (buf[pos++]=l) == '\0')
  336.         {
  337.             buf[pos-1] = '#';
  338.             buf[pos++] = l>>16;
  339.         }
  340.     } while (!pos);
  341.  
  342.     /* cancel the alarm */
  343.     alarm(0);
  344.  
  345.     /* return the number of bytes read */
  346.     return pos;
  347. }
  348.  
  349. void ttywrite(buf, len)
  350.     char    *buf;
  351.     int    len;
  352. {
  353.     if (!*o_stbios) {
  354.         write(1, buf, (unsigned) len);
  355.         return;
  356.     }
  357.     while (len--)
  358.         (void) Bconout(2, *buf++);
  359. }
  360. #endif /* MINT */
  361.