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

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