home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / language / xschm22 / src / ststuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  5.5 KB  |  300 lines

  1. /* ststuff.c - atari-st specific routines */
  2.  
  3. #include <osbind.h>
  4. #include "xscheme.h"
  5.  
  6. #define STRMAX         100             /* maximum length of a string constant */
  7. /* char buf[STRMAX+1] = { 0 }; */
  8. static char buf[200];
  9.  
  10. #define LBSIZE 200
  11.  
  12. /* set MWC memory parameters */
  13. long _stksize = 16384;    /* stack must be 16K */
  14.  
  15. /* external variables */
  16. extern LVAL s_unbound,true;
  17. extern int errno;
  18. extern FILE *tfp;
  19. extern char buf[];
  20.  
  21. /* line buffer variables */
  22. static char lbuf[LBSIZE];
  23. static int  lpos[LBSIZE];
  24. static int lindex;
  25. static int lcount;
  26. static int lposition;
  27.  
  28. /* osinit - initialize */
  29. osinit(banner)
  30.   char *banner;
  31. {
  32.     printf("\033v%s\n",banner);
  33.     lposition = 0;
  34.     lindex = 0;
  35.     lcount = 0;
  36. }
  37.  
  38. /* osfinish - clean up before a return to the operating system */
  39. osfinish()
  40. {
  41. }
  42.  
  43. /* oserror - print an error message */
  44. oserror(msg)
  45.   char *msg;
  46. {
  47.     printf("error: %s\n",msg);
  48. }
  49.  
  50. /* osrand - return a random number between 0 and n-1 */
  51. int osrand(n)
  52.   int n;
  53. {
  54.     return (rand() % n);
  55. }
  56.  
  57. /* osaopen - open an ascii file */
  58. FILE *osaopen(name,mode)
  59.   char *name,*mode;
  60. {
  61.     return (fopen(name,mode));
  62. }
  63.  
  64. /* osbopen - open a binary file */
  65. FILE *osbopen(name,mode)
  66.   char *name,*mode;
  67. {
  68.     char rmode[5];
  69.     strcpy(rmode,mode); strcat(rmode,"b");
  70.     return (fopen(name,rmode));
  71. }
  72.  
  73. /* osclose - close a file */
  74. int osclose(fp)
  75.   FILE *fp;
  76. {
  77.     return (fclose(fp));
  78. }
  79.  
  80. /* osagetc - get a character from an ascii file */
  81. int osagetc(fp)
  82.   FILE *fp;
  83. {
  84.     return (getc(fp));
  85. }
  86.  
  87. /* osaputc - put a character to an ascii file */
  88. int osaputc(ch,fp)
  89.   int ch; FILE *fp;
  90. {
  91.     return (putc(ch,fp));
  92. }
  93.  
  94. /* osbgetc - get a character from a binary file */
  95. int osbgetc(fp)
  96.   FILE *fp;
  97. {
  98.     return (getc(fp));
  99. }
  100.  
  101. /* osbputc - put a character to a binary file */
  102. int osbputc(ch,fp)
  103.   int ch; FILE *fp;
  104. {
  105.     return (putc(ch,fp));
  106. }
  107.  
  108. /* ostgetc - get a character from the terminal */
  109. int ostgetc()
  110. {
  111.     int ch;
  112.  
  113.     /* check for a buffered character */
  114.     if (lcount--)
  115.     return (lbuf[lindex++]);
  116.  
  117.     /* get an input line */
  118.     for (lcount = 0; ; )
  119.     switch (ch = xgetc()) {
  120.     case '\r':
  121.         lbuf[lcount++] = '\n';
  122.         xputc('\r'); xputc('\n'); lposition = 0;
  123.         if (tfp)
  124.             for (lindex = 0; lindex < lcount; ++lindex)
  125.             osaputc(lbuf[lindex],tfp);
  126.         lindex = 0; lcount--;
  127.         return (lbuf[lindex++]);
  128.     case '\010':
  129.     case '\177':
  130.         if (lcount) {
  131.             lcount--;
  132.             while (lposition > lpos[lcount]) {
  133.             xputc('\010'); xputc(' '); xputc('\010');
  134.             lposition--;
  135.             }
  136.         }
  137.         break;
  138.     case '\032':
  139.         xflush();
  140.         return (EOF);
  141.     default:
  142.         if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
  143.             lbuf[lcount] = ch;
  144.             lpos[lcount] = lposition;
  145.             if (ch == '\t')
  146.             do {
  147.                 xputc(' ');
  148.             } while (++lposition & 7);
  149.             else {
  150.             xputc(ch); lposition++;
  151.             }
  152.             lcount++;
  153.         }
  154.         else {
  155.             xflush();
  156.             switch (ch) {
  157.                     case '\002':        xlbreak("CTL-b",TRUE);  /* control-b */
  158.             case '\003':    xltoplevel();        /* control-c */
  159.             case '\007':    xlcleanup();        /* control-g */
  160.             case '\020':    xlcontinue();        /* control-p */
  161.             case '\032':    return (EOF);        /* control-z */
  162.             default:        return (ch);
  163.             }
  164.         }
  165.     }
  166. }
  167.  
  168. /* ostputc - put a character to the terminal */
  169. ostputc(ch)
  170.   int ch;
  171. {
  172.     /* check for control characters */
  173.     oscheck();
  174.  
  175.     /* output the character */
  176.     if (ch == '\n') {
  177.     xputc('\r'); xputc('\n');
  178.     lposition = 0;
  179.     }
  180.     else {
  181.     xputc(ch);
  182.     lposition++;
  183.    }
  184.  
  185.    /* output the character to the transcript file */
  186.    if (tfp)
  187.     osaputc(ch,tfp);
  188. }
  189.  
  190. /* oscheck - check for control characters during execution */
  191. oscheck()
  192. {
  193.     int ch;
  194.     if (ch = xcheck())
  195.     switch (ch) {
  196.     case '\002':    xflush(); xlbreak("BREAK",s_unbound); break;
  197.     case '\003':    xflush(); xltoplevel(); break;
  198.     }
  199. }
  200.  
  201. /* osflush - flush the input line buffer */
  202. osflush()
  203. {
  204.     lindex = lcount = 0;
  205. }
  206.  
  207. /* ostell - get the current file position */
  208. long ostell(fp)
  209.   FILE *fp;
  210. {
  211.     return (ftell(fp));
  212. }
  213.  
  214. /* osseek - set the current file position */
  215. int osseek(fp,offset,whence)
  216.   FILE *fp; long offset; int whence;
  217. {
  218.     return (fseek(fp,offset,whence));
  219. }
  220.  
  221.  
  222. /* xflush - flush the input line buffer */
  223. static xflush()
  224. {
  225.     ostputc('\n');
  226.     osflush();
  227. }
  228.  
  229. /* xgetc - get a character from the terminal without echo */
  230. static int xgetc()
  231. {
  232.     int ch;
  233.     while ((ch = Crawio(0xFF)) == 0)
  234.     ;
  235.     return (ch & 0xFF);
  236. }
  237.  
  238. /* xputc - put a character to the terminal */
  239. static xputc(ch)
  240.   int ch;
  241. {
  242.     Crawio(ch);
  243. }
  244.  
  245. /* xcheck - check for a character */
  246. static int xcheck()
  247. {
  248.     return (Crawio(0xFF));
  249. }
  250.  
  251. /* file name extension table */
  252. char *ext[] = { ".prg",".tos",".ttp",NULL };
  253.  
  254. /* xsystem - the built-in function 'system' */
  255. LVAL xsystem()
  256. {
  257.     char *str,*p,cmd[100];
  258.     int cmdlen,sts,i;
  259.  
  260.     /* get the command string */
  261.     str = getstring(xlgastring());
  262.     xllastarg();
  263.  
  264.     /* get the command name */
  265.     for (p = cmd, cmdlen = 0; *str && !isspace(*str); ++cmdlen)
  266.     *p++ = *str++;
  267.     *p = '\0';
  268.  
  269.     /* skip spaces between the command name and the arguments */
  270.     while (*str && isspace(*str))
  271.     ++str;
  272.  
  273.     /* make a counted ascii argument list */
  274.     for (p = &buf[1], buf[0] = '\0'; *str; ++buf[0])
  275.     *p++ = *str++;
  276.     *p = '\0';
  277.  
  278.     /* try each extension */
  279.     for (i = 0; ext[i]; ++i) {
  280.     strcpy(&cmd[cmdlen],ext[i]);
  281.     if ((sts = Pexec(0,cmd,buf,"")) != -33)
  282.         break;
  283.     }
  284.  
  285.     /* return the completion status */
  286.     return (cvfixnum((FIXTYPE)sts));
  287. }
  288.  
  289. /* xgetkey - get a key from the keyboard */
  290. LVAL xgetkey()
  291. {
  292.     xllastarg();
  293.     return (cvfixnum((FIXTYPE)xgetc()));
  294. }
  295.  
  296. /* ossymbols - lookup important symbols */
  297. ossymbols()
  298. {
  299. }
  300.