home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd4.lzh / SRC / oskstuff.c < prev    next >
Text File  |  1990-03-11  |  6KB  |  301 lines

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