home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 387a.lha / AmigaXLisp_v2.1 / amigastuff.c < prev    next >
C/C++ Source or Header  |  1990-05-29  |  6KB  |  299 lines

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