home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / xlisp_20.sit / macstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-29  |  2.4 KB  |  157 lines

  1. /* macstuff.c - macintosh interface routines for xlisp */
  2.  
  3. #include <stdio.h>
  4.  
  5. /* program limits */
  6. #define LINEMAX     200    /* maximum line length */
  7.  
  8. /* externals */
  9. extern FILE *tfp;
  10. extern int x;
  11.  
  12. /* local variables */
  13. static char linebuf[LINEMAX+1],*lineptr;
  14. static int linepos[LINEMAX],linelen;
  15. static long rseed = 1L;
  16.  
  17. osinit(name)
  18.   char *name;
  19. {
  20.     /* initialize the mac interface routines */
  21.     macinit();
  22.  
  23.     /* initialize the line editor */
  24.     linelen = 0;
  25. }
  26.  
  27. osfinish()
  28. {
  29. }
  30.  
  31. oserror(msg)
  32. {
  33.     char line[100],*p;
  34.     sprintf(line,"error: %s\n",msg);
  35.     for (p = line; *p != '\0'; ++p)
  36.     ostputc(*p);
  37. }
  38.  
  39. int osrand(n)
  40.   int n;
  41. {
  42.     long k1;
  43.     
  44.     /* make sure we don't get stuck at zero */
  45.     if (rseed == 0L) rseed = 1L;
  46.     
  47.     /* algorithm taken from Dr. Dobbs Journal, November 1985, Page 91 */
  48.     k1 = rseed / 127773L;
  49.     if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
  50.     rseed += 2147483647L;
  51.     
  52.     /* return a random number between 0 and n-1 */
  53.     return ((int)(rseed % (long)n));
  54. }
  55.  
  56. FILE *osaopen(name,mode)
  57.   char *name,*mode;
  58. {
  59.     return (fopen(name,mode));
  60. }
  61.  
  62. FILE *osbopen(name,mode)
  63.   char *name,*mode;
  64. {
  65.     char nmode[4];
  66.     strcpy(nmode,mode); strcat(nmode,"b");
  67.     return (fopen(name,nmode));
  68. }
  69.  
  70. int osclose(fp)
  71.   FILE *fp;
  72. {
  73.     return (fclose(fp));
  74. }
  75.  
  76. int osagetc(fp)
  77.   FILE *fp;
  78. {
  79.     return (getc(fp));
  80. }
  81.  
  82. int osbgetc(fp)
  83.   FILE *fp;
  84. {
  85.     return (getc(fp));
  86. }
  87.  
  88. int osaputc(ch,fp)
  89.   int ch; FILE *fp;
  90. {
  91.     return (putc(ch,fp));
  92. }
  93.  
  94. int osbputc(ch,fp)
  95.   int ch; FILE *fp;
  96. {
  97.     return (putc(ch,fp));
  98. }
  99.  
  100. int ostgetc()
  101. {
  102.     int ch,i;
  103.  
  104.     if (linelen--) return (*lineptr++);
  105.     linelen = 0;
  106.     while ((ch = scrgetc()) != '\r')
  107.     switch (ch) {
  108.     case EOF:
  109.         return (ostgetc());
  110.     case '\010':
  111.         if (linelen > 0) {
  112.         linelen--;
  113.         while (x > linepos[linelen])
  114.             scrdelete();
  115.         }
  116.         break;
  117.     default:
  118.         if (linelen < LINEMAX) {
  119.             linebuf[linelen] = ch;
  120.         linepos[linelen] = x;
  121.         linelen++;
  122.         }
  123.         scrputc(ch);
  124.         break;
  125.     }
  126.     linebuf[linelen++] = '\n';
  127.     scrputc('\r'); scrputc('\n');
  128.     if (tfp)
  129.     for (i = 0; i < linelen; ++i)
  130.         osaputc(linebuf[i],tfp);
  131.     lineptr = linebuf; linelen--;
  132.     return (*lineptr++);
  133. }
  134.  
  135. int ostputc(ch)
  136.   int ch;
  137. {
  138.     if (ch == '\n')
  139.     scrputc('\r');
  140.     scrputc(ch);
  141.     if (tfp)
  142.     osaputc(ch,tfp);
  143.     return (1);
  144. }
  145.  
  146. osflush()
  147. {
  148.     lineptr = linebuf;
  149.     linelen = 0;
  150. }
  151.  
  152. oscheck()
  153. {
  154.     DoEvent();
  155. }
  156.  
  157.