1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/* xlsys.c - xlisp builtin system functions */
/* Copyright (c) 1985, by David Michael Betz
All Rights Reserved
Permission is granted for unrestricted non-commercial use */
/* HISTORY
*
* 11-Dec-09 Roger Dannenberg
* Added getenv
*
* 28-Apr-03 Dominic Mazzoni
* Eliminated some compiler warnings
*
* 25-Oct-87 Roger Dannenberg at NeXT
* profiling code added: enable with (PROFILE t), disable with
* (PROFILE nil). While enabled, the profile code counts evals
* within functions and macros. The count is only for evals
* directly within the form; i.e. only the count of the most
* top-most function or macro form on the stack is incremented.
* Also, counts are only maintained for named functions and macros
* because the count itself is on the property list of the function
* or macro name under the *PROFILE* property. If a function or
* macro is entered and the *PROFILE* does not exist, the property
* is created with initial value 0, and the name is inserted at the
* head of the list stored as the value of *PROFILE*. Thus, *PROFILE*
* will list the functions that were touched, and the *PROFILE* property
* of each function gives some idea of how much time it consumed.
* See the file profile.lsp for helpful profiling functions.
*/
#include "xlisp.h"
/* profile variables */
static FIXTYPE invisible_counter;
FIXTYPE *profile_count_ptr = &invisible_counter;
FIXTYPE profile_flag = FALSE;
/* external variables */
extern jmp_buf top_level;
extern FILE *tfp;
extern int xl_main_loop;
/* external symbols */
extern LVAL a_subr,a_fsubr,a_cons,a_symbol;
extern LVAL a_fixnum,a_flonum,a_string,a_object,a_stream;
extern LVAL a_vector,a_closure,a_char,a_ustream;
extern LVAL k_verbose,k_print;
extern LVAL s_true;
/* external routines */
extern FILE *osaopen();
extern LVAL exttype();
/* xget_env - get the value of an environment variable */
LVAL xget_env(void)
{
const char *name = (char *) getstring(xlgetfname());
char *val;
/* check for too many arguments */
xllastarg();
/* get the value of the environment variable */
val = getenv(name);
return (val ? cvstring(val) : NULL);
}
/* xload - read and evaluate expressions from a file */
LVAL xload(void)
{
unsigned char *name;
int vflag,pflag;
LVAL arg;
/* get the file name */
name = getstring(xlgetfname());
/* get the :verbose flag */
if (xlgetkeyarg(k_verbose,&arg))
vflag = (arg != NIL);
else
vflag = TRUE;
/* get the :print flag */
if (xlgetkeyarg(k_print,&arg))
pflag = (arg != NIL);
else
pflag = FALSE;
/* load the file */
return (xlload((char *) name, vflag, pflag) ? s_true : NIL);
}
/* xtranscript - open or close a transcript file */
LVAL xtranscript(void)
{
unsigned char *name;
/* get the transcript file name */
name = (moreargs() ? getstring(xlgetfname()) : NULL);
xllastarg();
/* close the current transcript */
if (tfp) osclose(tfp);
/* open the new transcript */
tfp = (name ? osaopen((char *) name,"w") : NULL);
/* return T if a transcript is open, NIL otherwise */
return (tfp ? s_true : NIL);
}
/* xtype - return type of a thing */
LVAL xtype(void)
{
LVAL arg;
if (!(arg = xlgetarg()))
return (NIL);
switch (ntype(arg)) {
case SUBR: return (a_subr);
case FSUBR: return (a_fsubr);
case CONS: return (a_cons);
case SYMBOL: return (a_symbol);
case FIXNUM: return (a_fixnum);
case FLONUM: return (a_flonum);
case STRING: return (a_string);
case OBJECT: return (a_object);
case STREAM: return (a_stream);
case VECTOR: return (a_vector);
case CLOSURE: return (a_closure);
case CHAR: return (a_char);
case USTREAM: return (a_ustream);
case EXTERN: return (exttype(arg));
default: xlfail("bad node type");
return NIL; /* never happens */
}
}
/* xbaktrace - print the trace back stack */
LVAL xbaktrace(void)
{
LVAL num;
int n;
if (moreargs()) {
num = xlgafixnum();
n = getfixnum(num);
}
else
n = -1;
xllastarg();
xlbaktrace(n);
return (NIL);
}
/* xquit - get out of read/eval/print loop */
LVAL xquit()
{
xllastarg();
xl_main_loop = FALSE;
return NIL;
}
/* xexit does not return anything, so turn off "no return value" warning" */
/* #pragma warning(disable: 4035) */
/* xexit - get out of xlisp */
LVAL xexit(void)
{
xllastarg();
xlisp_wrapup();
return NIL; /* never happens */
}
#ifdef PEEK_AND_POKE
/* xpeek - peek at a location in memory */
LVAL xpeek(void)
{
LVAL num;
int *adr;
/* get the address */
num = xlgafixnum(); adr = (int *)getfixnum(num);
xllastarg();
/* return the value at that address */
return (cvfixnum((FIXTYPE)*adr));
}
/* xpoke - poke a value into memory */
LVAL xpoke(void)
{
LVAL val;
int *adr;
/* get the address and the new value */
val = xlgafixnum(); adr = (int *)getfixnum(val);
val = xlgafixnum();
xllastarg();
/* store the new value */
*adr = (int)getfixnum(val);
/* return the new value */
return (val);
}
/* xaddrs - get the address of an XLISP node */
LVAL xaddrs(void)
{
LVAL val;
/* get the node */
val = xlgetarg();
xllastarg();
/* return the address of the node */
return (cvfixnum((FIXTYPE)val));
}
#endif PEEK_AND_POKE
/* xprofile - turn profiling on and off */
LVAL xprofile()
{
LVAL flag, result;
/* get the argument */
flag = xlgetarg();
xllastarg();
result = (profile_flag ? s_true : NIL);
profile_flag = !null(flag);
/* turn off profiling right away: */
if (!profile_flag) profile_count_ptr = &invisible_counter;
return result;
}
#ifdef DEBUG_INPUT
FILE *debug_input_fp = NULL;
FILE *to_input_buffer = NULL;
FILE *read_by_xlisp = NULL;
LVAL xstartrecordio()
{
to_input_buffer = fopen("to-input-buffer.txt", "w");
read_by_xlisp = fopen("read-by-xlisp.txt", "w");
if (!to_input_buffer || !read_by_xlisp) {
return NIL;
}
return s_true;
}
LVAL xstoprecordio()
{
if (to_input_buffer) fclose(to_input_buffer);
if (read_by_xlisp) fclose(read_by_xlisp);
to_input_buffer = NULL;
read_by_xlisp = NULL;
return NIL;
}
#endif
|