home *** CD-ROM | disk | FTP | other *** search
- /* msstuff.c - ms-dos specific routines */
-
- #include "xlisp.h"
-
- #define LBSIZE 200
-
- /* external variables */
- extern LVAL s_unbound,true;
- extern FILE *tfp;
- extern int errno;
-
- /* local variables */
- static char lbuf[LBSIZE];
- static int lpos[LBSIZE];
- static int lindex;
- static int lcount;
- static int lposition;
- static long rseed = 1L;
-
- #ifdef _TURBOC_
- # include <dos.h>
- # define xputc(c) bdos(6,c,0)
- # define xcheck() bdos(6,0xFF,0)
-
- void xinfo(void) ;
- void xflush(void) ;
-
- extern unsigned _stklen = 16384u ;
- #endif _TURBOC_
-
- #ifdef PROTOTYPES
- int xgetc(void) ;
- #endif PROTOTYPES
-
- /* osinit - initialize */
- void osinit(banner)
- char *banner;
- {
- printf("%s\n",banner);
- lposition = 0;
- lindex = 0;
- lcount = 0;
- }
-
- /* osfinish - clean up before returning to the operating system */
- void osfinish()
- {
- }
-
- /* oserror - print an error message */
- void oserror(msg)
- char *msg;
- {
- printf("error: %s\n",msg);
- }
-
- /* osrand - return a random number between 0 and n-1 */
- int osrand(n)
- int n;
- {
- long k1;
-
- /* make sure we don't get stuck at zero */
- if (rseed == 0L) rseed = 1L;
-
- /* algorithm taken from Dr. Dobbs Journal, November 1985, page 91 */
- k1 = rseed / 127773L;
- if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
- rseed += 2147483647L;
-
- /* return a random number between 0 and n-1 */
- return ((int)(rseed % (long)n));
- }
-
- /* osaopen - open an ascii file */
- #ifndef osaopen
- FILE *osaopen(name,mode)
- char *name,*mode;
- {
- return (fopen(name,mode));
- }
- #endif
-
- /* osbopen - open a binary file */
- FILE *osbopen(name,mode)
- char *name,*mode;
- {
- char bmode[10];
- strcpy(bmode,mode); strcat(bmode,"b");
- return (fopen(name,bmode));
- }
-
- /* osclose - close a file */
- #ifndef osclose
- int osclose(fp)
- FILE *fp;
- {
- return (fclose(fp));
- }
- #endif osclose
-
- /* osagetc - get a character from an ascii file */
- #ifndef osagetc
- int osagetc(fp)
- FILE *fp;
- {
- return (getc(fp));
- }
- #endif
-
- /* osaputc - put a character to an ascii file */
- #ifndef osaputc
- int osaputc(ch,fp)
- int ch; FILE *fp;
- {
- return (putc(ch,fp));
- }
- #endif
-
- /* osbgetc - get a character from a binary file */
- #ifndef osbgetc
- int osbgetc(fp)
- FILE *fp;
- {
- return (getc(fp));
- }
- #endif osbgetc
-
- /* osbputc - put a character to a binary file */
- #ifndef osbputc
- int osbputc(ch,fp)
- int ch; FILE *fp;
- {
- return (putc(ch,fp));
- }
- #endif
-
- /* ostgetc - get a character from the terminal */
- int ostgetc()
- {
- int ch;
-
- /* check for a buffered character */
- if (lcount--)
- return (lbuf[lindex++]);
-
- /* get an input line */
- for (lcount = 0; ; )
- switch (ch = xgetc()) {
- case '\r':
- lbuf[lcount++] = '\n';
- xputc('\r'); xputc('\n'); lposition = 0;
- if (tfp)
- for (lindex = 0; lindex < lcount; ++lindex)
- osaputc(lbuf[lindex],tfp);
- lindex = 0; lcount--;
- return (lbuf[lindex++]);
- case '\010':
- case '\177':
- if (lcount) {
- lcount--;
- while (lposition > lpos[lcount]) {
- xputc('\010'); xputc(' '); xputc('\010');
- lposition--;
- }
- }
- break;
- case '\032':
- xflush();
- return (EOF);
- default:
- if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
- lbuf[lcount] = ch;
- lpos[lcount] = lposition;
- if (ch == '\t')
- do {
- xputc(' ');
- } while (++lposition & 7);
- else {
- xputc(ch); lposition++;
- }
- lcount++;
- }
- else {
- xflush();
- switch (ch) {
- case '\003': xltoplevel(); /* control-c */
- case '\007': xlcleanup(); /* control-g */
- case '\020': xlcontinue(); /* control-p */
- case '\032': return (EOF); /* control-z */
- default: return (ch);
- }
- }
- }
- }
-
- /* ostputc - put a character to the terminal */
- void ostputc(ch)
- int ch;
- {
- /* check for control characters */
- oscheck();
-
- /* output the character */
- if (ch == '\n') {
- xputc('\r'); xputc('\n');
- lposition = 0;
- }
- else {
- xputc(ch);
- lposition++;
- }
-
- /* output the character to the transcript file */
- if (tfp)
- osaputc(ch,tfp);
- }
-
- /* osflush - flush the terminal input buffer */
- void osflush()
- {
- lindex = lcount = lposition = 0;
- }
-
- /* oscheck - check for control characters during execution */
- void oscheck()
- {
- int ch;
- if (ch = xcheck())
- switch (ch) {
- case '\002': /* control-b */
- xflush();
- xlbreak("BREAK",s_unbound);
- break;
- case '\003': /* control-c */
- xflush();
- xltoplevel();
- break;
- case '\024': /* control-t */
- xinfo();
- break;
- }
- }
-
- /* xinfo - show information on control-t */
- static void xinfo()
- {
- extern int nfree,gccalls;
- extern long total;
- char buf[80];
- sprintf(buf,"\n[ Free: %d, GC calls: %d, Total: %ld ]",
- nfree,gccalls,total);
- errputstr(buf);
- }
-
- /* xflush - flush the input line buffer and start a new line */
- static void xflush()
- {
- osflush();
- ostputc('\n');
- }
-
- /* xgetc - get a character from the terminal without echo */
- static int xgetc()
- {
- return (bdos(7,0,0) & 0xFF);
- }
-
- /* xputc - put a character to the terminal */
- #ifndef xputc
- static void xputc(ch)
- int ch;
- {
- bdos(6,ch);
- }
- #endif xputc
-
- /* xcheck - check for a character */
- #ifndef xcheck
- static int xcheck()
- {
- return (bdos(6,0xFF));
- }
- #endif xcheck
-
- /* xsystem - execute a system command */
- LVAL xsystem()
- {
- char *cmd="COMMAND";
- if (moreargs())
- cmd = (char *)getstring(xlgastring());
- xllastarg();
- return (system(cmd) == 0 ? true : cvfixnum((FIXTYPE)errno));
- }
-
- /* xgetkey - get a key from the keyboard */
- LVAL xgetkey()
- {
- xllastarg();
- return (cvfixnum((FIXTYPE)xgetc()));
- }
-
- /* xbutlast - (butlast list &optional (n 1)) */
- LVAL xbutlast( void )
- {
- unsigned len ;
- unsigned n ;
- LVAL list = xlgalist() ;
- LVAL p, q, result ;
-
- for (len = 0, p = list ; consp(p) ; len++)
- p = cdr(p) ;
- if (moreargs())
- {
- p = xlgetarg() ;
- n = floatp(p) ? (unsigned) getflonum(p) : (unsigned) getfixnum(p) ;
- }
- else
- n = 1 ;
-
- if (len > n)
- {
- p = list ;
- result = q = consa(car(p)) ;
- p = cdr(p) ;
- }
- else
- return NIL ;
- while (--len > n)
- {
- rplacd(q,consa(car(p))) ;
- p = cdr(p) ;
- q = cdr(q) ;
- }
- return result ;
- }
-
- /* xnbutlast - (nbutlast list &optional (n 1)) */
- LVAL xnbutlast( void )
- {
- unsigned len ;
- unsigned n ;
- LVAL list = xlgalist() ;
- LVAL p ;
-
- for (len = 0, p = list ; consp(p) ; len++)
- p = cdr(p) ;
- if (moreargs())
- {
- p = xlgetarg() ;
- n = floatp(p) ? (unsigned) getflonum(p) : (unsigned) getfixnum(p) ;
- }
- else
- n = 1 ;
-
- p = NIL ;
- if (len > n)
- {
- p = list ;
- while (--len > n)
- list = cdr(list) ;
- rplacd(list,NIL) ;
- }
- return p ;
- }
-
- LVAL _xcopytree( LVAL arg )
- {
- if (consp(arg))
- return cons(_xcopytree( car(arg) ),_xcopytree( cdr(arg) )) ;
- else
- return arg ;
- }
-
- /* xcopytree - (copy-tree obj) */
- LVAL xcopytree( void )
- {
- return _xcopytree( xlgalist() ) ;
- }
-
- /* xnreverse - (nreverse list) */
- LVAL xnreverse( void )
- {
- LVAL prev = NULL ;
- LVAL curr = xlgalist() ;
- LVAL next ;
-
- xllastarg() ;
- if (curr == NIL)
- return NIL ;
- else
- next = cdr(curr) ;
- while (next)
- {
- rplacd(curr,prev) ;
- prev = curr ;
- curr = next ;
- next = cdr(next) ;
- }
- rplacd(curr,prev);
- return curr ;
- }
-
- /* (every pred arglist ... ) -- return T if pred returns T for every set of args */
- LVAL xevery( void )
- {
- LVAL pred = xlgetarg() ;
- LVAL p ;
- LVAL result = true ;
- LVAL *argv, *newfp ;
- int argc ;
- int done ;
-
- if (!moreargs())
- return NIL ;
- argv = xlargv ; /* remember the original parm list */
- argc = xlargc ;
- do {
- xlargv = argv ; /* restore original parm list */
- xlargc = argc ;
- done = FALSE ;
-
- /* build a new argument stack frame */
- newfp = xlsp ;
- pusharg(cvfixnum((FIXTYPE)(newfp - xlfp)));
- pusharg(pred);
- pusharg(cvfixnum((FIXTYPE)argc));
- xlfp = newfp ;
-
- while (moreargs())
- {
- p = xlgalist() ; /* operate on next arg */
- if (consp(p))
- {
- pusharg(car(p)) ;
- xlargv[-1] = cdr(p) ;
- }
- else
- {
- pusharg(NIL) ;
- done = TRUE ;
- }
- }
- } while (!done && (result = xlapply(argc)) != NIL) ;
- return (result != NIL) ? true : NIL ;
- }
-
- /* ossymbols - enter os specific symbols */
- void ossymbols()
- {
- }
-