home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / xlispdos / source / msstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-01  |  3.8 KB  |  199 lines

  1. /* msstuff.c - ms-dos specific routines */
  2.  
  3. #include "xlisp.h"
  4.  
  5. #define LBSIZE 200
  6.  
  7. /* external routines */
  8. extern double ran();
  9.  
  10. /* external variables */
  11. extern NODE *s_unbound,*true;
  12. extern int prompt;
  13. extern int errno;
  14. extern FILE *tfp;
  15.  
  16. /* line buffer variables */
  17. static char lbuf[LBSIZE];
  18. static int  lpos[LBSIZE];
  19. static int lindex;
  20. static int lcount;
  21. static int lposition;
  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. /* osrand - return a random number between 0 and n-1 */
  39. int osrand(n)
  40.   int n;
  41. {
  42.     n = (int)(ran() * (double)n);
  43.     return (n < 0 ? -n : n);
  44. }
  45.  
  46. /* osgetc - get a character from the terminal */
  47. int osgetc(fp)
  48.   FILE *fp;
  49. {
  50.     int ch;
  51.  
  52.     /* check for input from a file other than stdin */
  53.     if (fp != stdin)
  54.     return (agetc(fp));
  55.  
  56.     /* check for a buffered character */
  57.     if (lcount--)
  58.     return (lbuf[lindex++]);
  59.  
  60.     /* get an input line */
  61.     for (lcount = 0; ; )
  62.     switch (ch = xgetc()) {
  63.     case '\r':
  64.         lbuf[lcount++] = '\n';
  65.         xputc('\r'); xputc('\n'); lposition = 0;
  66.         if (tfp)
  67.             for (lindex = 0; lindex < lcount; ++lindex)
  68.             osputc(lbuf[lindex],tfp);
  69.         lindex = 0; lcount--;
  70.         return (lbuf[lindex++]);
  71.     case '\010':
  72.     case '\177':
  73.         if (lcount) {
  74.             lcount--;
  75.             while (lposition > lpos[lcount]) {
  76.             xputc('\010'); xputc(' '); xputc('\010');
  77.             lposition--;
  78.             }
  79.         }
  80.         break;
  81.     case '\032':
  82.         xflush();
  83.         return (EOF);
  84.     default:
  85.         if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
  86.             lbuf[lcount] = ch;
  87.             lpos[lcount] = lposition;
  88.             if (ch == '\t')
  89.             do {
  90.                 xputc(' ');
  91.             } while (++lposition & 7);
  92.             else {
  93.             xputc(ch); lposition++;
  94.             }
  95.             lcount++;
  96.         }
  97.         else {
  98.             xflush();
  99.             switch (ch) {
  100.             case '\003':    xltoplevel();    /* control-c */
  101.             case '\007':    xlcleanup();    /* control-g */
  102.             case '\020':    xlcontinue();    /* control-p */
  103.             case '\032':    return (EOF);    /* control-z */
  104.             default:        return (ch);
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110. /* osputc - put a character to the terminal */
  111. osputc(ch,fp)
  112.   int ch; FILE *fp;
  113. {
  114.     /* check for output to something other than stdout */
  115.     if (fp != stdout)
  116.     return (aputc(ch,fp));
  117.  
  118.     /* check for control characters */
  119.     oscheck();
  120.  
  121.     /* output the character */
  122.     if (ch == '\n') {
  123.     xputc('\r'); xputc('\n');
  124.     lposition = 0;
  125.     }
  126.     else {
  127.     xputc(ch);
  128.     lposition++;
  129.    }
  130.  
  131.    /* output the character to the transcript file */
  132.    if (tfp)
  133.     osputc(ch,tfp);
  134. }
  135.  
  136. /* oscheck - check for control characters during execution */
  137. oscheck()
  138. {
  139.     int ch;
  140.     if (ch = xcheck())
  141.     switch (ch) {
  142.     case '\002':    xflush(); xlbreak("BREAK",s_unbound); break;
  143.     case '\003':    xflush(); xltoplevel(); break;
  144.     }
  145. }
  146.  
  147. /* xflush - flush the input line buffer */
  148. static xflush()
  149. {
  150.     lindex = lcount = 0;
  151.     osputc('\n',stdout);
  152.     prompt = 1;
  153. }
  154.  
  155. /* xgetc - get a character from the terminal without echo */
  156. static int xgetc()
  157. {
  158.     return (bdos(7));
  159. }
  160.  
  161. /* xputc - put a character to the terminal */
  162. static xputc(ch)
  163.   int ch;
  164. {
  165.     bdos(6,ch);
  166. }
  167.  
  168. /* xcheck - check for a character */
  169. static int xcheck()
  170. {
  171.     return (bdos(6,0xFF));
  172. }
  173.  
  174. /* xdos - execute a dos command */
  175. NODE *xdos(args)
  176.   NODE *args;
  177. {
  178.     char *cmd;
  179.     cmd = xlmatch(STR,&args)->n_str;
  180.     xllastarg(args);
  181.     return (system(cmd) == -1 ? cvfixnum((FIXNUM)errno) : true);
  182. }
  183.  
  184. /* xgetkey - get a key from the keyboard */
  185. NODE *xgetkey(args)
  186.   NODE *args;
  187. {
  188.     xllastarg(args);
  189.     return (cvfixnum((FIXNUM)xgetc()));
  190. }
  191.  
  192. /* osfinit - initialize pc specific functions */
  193. osfinit()
  194. {
  195.     xlsubr("DOS",        SUBR,    xdos);
  196.     xlsubr("GET-KEY",        SUBR,    xgetkey);
  197. }
  198.  
  199.