2003-08-30 12:47:54 -04:00
|
|
|
/* feature.c: provide, require, and related primitives.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@zoy.org>, Paris
|
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
2003-08-19 15:19:38 -04:00
|
|
|
*/
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "config.h"
|
2003-08-19 15:19:38 -04:00
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "kernel.h"
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
static Object Features;
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Init_Features () {
|
2003-08-19 15:19:38 -04:00
|
|
|
Features = Null;
|
|
|
|
Global_GC_Link (Features);
|
|
|
|
#ifdef CAN_DUMP
|
|
|
|
P_Provide (Intern ("elk:dump"));
|
|
|
|
#endif
|
|
|
|
#ifdef CAN_LOAD_OBJ
|
|
|
|
P_Provide (Intern ("elk:load-object"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Object P_Features () {
|
|
|
|
return Features;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Object P_Featurep (Object sym) {
|
2003-08-19 15:19:38 -04:00
|
|
|
Object member;
|
|
|
|
|
|
|
|
Check_Type (sym, T_Symbol);
|
|
|
|
member = P_Memq (sym, Features);
|
|
|
|
return Truep (member) ? True : False;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Object P_Provide (Object sym) {
|
2003-08-19 15:19:38 -04:00
|
|
|
Object member;
|
|
|
|
|
|
|
|
Check_Type (sym, T_Symbol);
|
|
|
|
member = P_Memq (sym, Features);
|
|
|
|
if (!Truep (member))
|
2003-09-02 04:12:11 -04:00
|
|
|
Features = Cons (sym, Features);
|
2003-08-19 15:19:38 -04:00
|
|
|
return Void;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
static Object Feature_Filename (Object str) {
|
2003-08-19 15:19:38 -04:00
|
|
|
struct S_String *sp = STRING(str);
|
|
|
|
int len = sp->size;
|
|
|
|
char *p;
|
|
|
|
Object s;
|
|
|
|
GC_Node;
|
|
|
|
|
|
|
|
for (p = sp->data+len-1; p >= sp->data && *p != '.'; p--)
|
2003-09-02 04:12:11 -04:00
|
|
|
;
|
2003-08-19 15:19:38 -04:00
|
|
|
if (p >= sp->data)
|
|
|
|
return str;
|
|
|
|
GC_Link (str);
|
|
|
|
s = Make_String ((char *)0, len+4);
|
2003-08-19 15:24:23 -04:00
|
|
|
memcpy (STRING(s)->data, STRING(str)->data, len);
|
|
|
|
memcpy (STRING(s)->data+len, ".scm", 4);
|
2003-08-19 15:19:38 -04:00
|
|
|
GC_Unlink;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Object P_Require (int argc, Object *argv) {
|
2003-08-19 15:19:38 -04:00
|
|
|
Object sym, a[1], isfeature;
|
|
|
|
GC_Node;
|
|
|
|
|
|
|
|
sym = argv[0];
|
|
|
|
GC_Link (sym);
|
|
|
|
isfeature = P_Featurep (sym);
|
|
|
|
if (!Truep (isfeature)) {
|
2003-09-02 04:12:11 -04:00
|
|
|
if (argc == 3)
|
|
|
|
Check_Type (argv[2], T_Environment);
|
|
|
|
a[0] = argc == 1 ? Feature_Filename (SYMBOL(sym)->name) : argv[1];
|
|
|
|
if (Var_Is_True (V_Autoload_Notifyp))
|
|
|
|
Format (Standard_Output_Port, "[Autoloading ~a]~%", 18, 1, a);
|
|
|
|
(void)General_Load (a[0], argc == 3 ? argv[2] : The_Environment);
|
|
|
|
isfeature = P_Featurep (sym);
|
|
|
|
if (!Truep (isfeature))
|
|
|
|
Primitive_Error ("feature ~s was not provided", sym);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
GC_Unlink;
|
|
|
|
return Void;
|
|
|
|
}
|