home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 089.lha / XLisp_2.0 / amigastuff.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  5KB  |  294 lines

  1. /* amigastuff.c - amiga specific routines */
  2.  
  3. #include "xlisp.h"
  4.  
  5. #define LBSIZE 200
  6.  
  7. /* external variables */
  8. extern LVAL s_unbound,true;
  9. extern FILE *tfp;
  10. extern int errno;
  11.  
  12. /* local variables */
  13. static long wfd;
  14. static char lbuf[LBSIZE];
  15. static int lpos[LBSIZE];
  16. static int lindex;
  17. static int lcount;
  18. static int lposition;
  19. static long rseed = 1L;
  20.  
  21. /* external routines */
  22. extern long Open();
  23. extern long WaitForChar();
  24. extern long Execute();
  25.  
  26. /* osinit - initialize */
  27. osinit(banner)
  28.   char *banner;
  29. {
  30.     wfd = Open("RAW:1/1/639/199/XLISP Version 2.0.T9, by David Betz",1006L);
  31.     if (wfd == 0L)
  32.     exit(1);
  33.     while (*banner)
  34.     xputc(*banner++);
  35.     xputc('\r'); xputc('\n');
  36.     lposition = 0;
  37.     lindex = 0;
  38.     lcount = 0;
  39. }
  40.  
  41. /* osfinish - clean up before returning to the operating system */
  42. osfinish()
  43. {
  44.     Close(wfd);
  45. }
  46.  
  47. /* oserror - print an error message */
  48. oserror(msg)
  49.   char *msg;
  50. {
  51.     char buf[100],*p;
  52.     sprintf("error: %s\n",msg);
  53.     for (p = buf; *p; )
  54.     xputc(*p++);
  55. }
  56.  
  57. /* osrand - return a random number between 0 and n-1 */
  58. int osrand(n)
  59.   int n;
  60. {
  61.     long k1;
  62.  
  63.     /* make sure we don't get stuck at zero */
  64.     if (rseed == 0L) rseed = 1L;
  65.  
  66.     /* algorithm taken from Dr. Dobbs Journal, November 1985, page 91 */
  67.     k1 = rseed / 127773L;
  68.     if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
  69.     rseed += 2147483647L;
  70.  
  71.     /* return a random number between 0 and n-1 */
  72.     return ((int)(rseed % (long)n));
  73. }
  74.  
  75. /* osaopen - open an ascii file */
  76. FILE *osaopen(name,mode)
  77.   char *name,*mode;
  78. {
  79.     return (fopen(name,mode));
  80. }
  81.  
  82. /* osbopen - open a binary file */
  83. FILE *osbopen(name,mode)
  84.   char *name,*mode;
  85. {
  86.     return (fopen(name,mode));
  87. }
  88.  
  89. /* osclose - close a file */
  90. int osclose(fp)
  91.   FILE *fp;
  92. {
  93.     return (fclose(fp));
  94. }
  95.  
  96. /* osagetc - get a character from an ascii file */
  97. int osagetc(fp)
  98.   FILE *fp;
  99. {
  100.     return (agetc(fp));
  101. }
  102.  
  103. /* osaputc - put a character to an ascii file */
  104. int osaputc(ch,fp)
  105.   int ch; FILE *fp;
  106. {
  107.     return (aputc(ch,fp));
  108. }
  109.  
  110. /* osbgetc - get a character from a binary file */
  111. int osbgetc(fp)
  112.   FILE *fp;
  113. {
  114.     return (getc(fp));
  115. }
  116.  
  117. /* osbputc - put a character to a binary file */
  118. int osbputc(ch,fp)
  119.   int ch; FILE *fp;
  120. {
  121.     return (putc(ch,fp));
  122. }
  123.  
  124. /* ostgetc - get a character from the terminal */
  125. int ostgetc()
  126. {
  127.     int ch;
  128.  
  129.     /* check for a buffered character */
  130.     if (lcount--)
  131.     return (lbuf[lindex++]);
  132.  
  133.     /* get an input line */
  134.     for (lcount = 0; ; )
  135.     switch (ch = xgetc()) {
  136.     case '\r':
  137.         lbuf[lcount++] = '\n';
  138.         xputc('\r'); xputc('\n'); lposition = 0;
  139.         if (tfp)
  140.             for (lindex = 0; lindex < lcount; ++lindex)
  141.             osaputc(lbuf[lindex],tfp);
  142.         lindex = 0; lcount--;
  143.         return (lbuf[lindex++]);
  144.     case '\010':
  145.     case '\177':
  146.         if (lcount) {
  147.             lcount--;
  148.             while (lposition > lpos[lcount]) {
  149.             xputc('\010'); xputc(' '); xputc('\010');
  150.             lposition--;
  151.             }
  152.         }
  153.         break;
  154.     case '\032':
  155.         xflush();
  156.         return (EOF);
  157.     default:
  158.         if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
  159.             lbuf[lcount] = ch;
  160.             lpos[lcount] = lposition;
  161.             if (ch == '\t')
  162.             do {
  163.                 xputc(' ');
  164.             } while (++lposition & 7);
  165.             else {
  166.             xputc(ch); lposition++;
  167.             }
  168.             lcount++;
  169.         }
  170.         else {
  171.             xflush();
  172.             switch (ch) {
  173.             case '\003':    xltoplevel();    /* control-c */
  174.             case '\007':    xlcleanup();    /* control-g */
  175.             case '\020':    xlcontinue();    /* control-p */
  176.             case '\032':    return (EOF);    /* control-z */
  177.             default:        return (ch);
  178.             }
  179.         }
  180.     }
  181. }
  182.  
  183. /* ostputc - put a character to the terminal */
  184. ostputc(ch)
  185.   int ch;
  186. {
  187.     /* check for control characters */
  188.     oscheck();
  189.  
  190.     /* output the character */
  191.     if (ch == '\n') {
  192.     xputc('\r'); xputc('\n');
  193.     lposition = 0;
  194.     }
  195.     else {
  196.     xputc(ch);
  197.     lposition++;
  198.    }
  199.  
  200.    /* output the character to the transcript file */
  201.    if (tfp)
  202.     osaputc(ch,tfp);
  203. }
  204.  
  205. /* osflush - flush the terminal input buffer */
  206. osflush()
  207. {
  208.     lindex = lcount = lposition = 0;
  209. }
  210.  
  211. /* oscheck - check for control characters during execution */
  212. oscheck()
  213. {
  214.     int ch;
  215.     if (ch = xcheck())
  216.     switch (ch) {
  217.     case '\002':    /* control-b */
  218.         xflush();
  219.         xlbreak("BREAK",s_unbound);
  220.         break;
  221.     case '\003':    /* control-c */
  222.         xflush();
  223.         xltoplevel();
  224.         break;
  225.     case '\024':    /* control-t */
  226.         xinfo();
  227.         break;
  228.     }
  229. }
  230.  
  231. /* xinfo - show information on control-t */
  232. static xinfo()
  233. {
  234.     extern int nfree,gccalls;
  235.     extern long total;
  236.     char buf[80];
  237.     sprintf(buf,"\n[ Free: %d, GC calls: %d, Total: %ld ]",
  238.         nfree,gccalls,total);
  239.     errputstr(buf);
  240. }
  241.  
  242. /* xflush - flush the input line buffer and start a new line */
  243. static xflush()
  244. {
  245.     osflush();
  246.     ostputc('\n');
  247. }
  248.  
  249. /* xgetc - get a character from the terminal without echo */
  250. static int xgetc()
  251. {
  252.     unsigned char buf;
  253.     Read(wfd,&buf,1L);
  254.     return (buf);
  255. }
  256.  
  257. /* xputc - put a character to the terminal */
  258. static xputc(ch)
  259.   int ch;
  260. {
  261.     unsigned char buf;
  262.     buf = ch;
  263.     Write(wfd,&buf,1L);
  264. }
  265.  
  266. /* xcheck - check for a character */
  267. static int xcheck()
  268. {
  269.     if (WaitForChar(wfd,0L) == 0L)
  270.     return (0);
  271.     return (xgetc());
  272. }
  273.  
  274. /* xsystem - execute a system command */
  275. LVAL xsystem()
  276. {
  277.     unsigned char *cmd;
  278.     cmd = getstring(xlgastring());
  279.     xllastarg();
  280.     return (Execute(cmd,0L,wfd) == -1 ? cvfixnum((FIXTYPE)errno) : true);
  281. }
  282.  
  283. /* xgetkey - get a key from the keyboard */
  284. LVAL xgetkey()
  285. {
  286.     xllastarg();
  287.     return (cvfixnum((FIXTYPE)xgetc()));
  288. }
  289.  
  290. /* ossymbols - enter os specific symbols */
  291. ossymbols()
  292. {
  293. }
  294.