home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gwm18a.zip / wool.h < prev    next >
C/C++ Source or Header  |  1995-07-03  |  12KB  |  423 lines

  1. /* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
  2.  * Copyright 1989 Massachusetts Institute of Technology
  3.  */
  4. /***************************\
  5. *                 *
  6. *  various defs for wool   *
  7. *                 *
  8. \***************************/
  9.  
  10. #ifndef INCLUDE_WOOL_H
  11. #define INCLUDE_WOOL_H
  12. #include <setjmp.h>
  13. #ifndef EXT
  14. #include "EXTERN.h"
  15. #endif /* EXT */
  16. #include "machine.h"
  17.  
  18. /* various parameters */
  19.  
  20. /*
  21.  *  Maximum size for:
  22.  *      length of name of CACHED objets (fonts, etc...)
  23.  *     output lines of wool_printf (not wool_puts)
  24.  *     file names (path look-up)
  25.  *     X properties (only "machine name" for now)
  26.  *
  27.  *  Should alway be >= 256, 1024 is fine.
  28.  *  Used only for speed concerns to allocate temp strings in the stack
  29.  */
  30.  
  31. #define MAX_TEMP_STRING_SIZE 1024
  32.  
  33. /* default WLPATH for searching wool files */
  34.  
  35. #ifdef INSTALL_PATH
  36. #define DEFAULT_WLPATH INSTALL_PATH
  37. #else /* INSTALL_PATH */
  38. # ifdef INSTALL_DIR
  39. #define DEFAULT_WLPATH INSTALL_DIR
  40. # else 
  41. #define DEFAULT_WLPATH DEFAULT_DEFAULT_WLPATH
  42. # endif INSTALL_DIR
  43. #endif /* INSTALL_PATH */
  44.  
  45. /* path */
  46.  
  47. EXT    char *wool_path;
  48. EXT     char *wool_user_profile_name, *wool_text_extension;
  49.  
  50. /* shell variables */
  51.  
  52. #define DEFAULT_FONT "fixed"        /* MUST exist! */
  53.  
  54. /* application_name */
  55.  
  56. EXT char *wool_application_NAME INIT(WOOL_APP_NAME);
  57. EXT char *wool_application_name INIT(WOOL_APP_name);
  58.  
  59. /* END OF INSTALLATION PARAMETERS */
  60.  
  61. #ifndef Max
  62. #define Max(x,y)        (((x)<(y))?(y):(x))
  63. #define Min(x,y)        (((x)<(y))?(x):(y))
  64. #ifndef Abs
  65. #define Abs(x)            (((x)>0)?(x):-(x))
  66. #endif
  67. #define FlagOn(mask,flag) ((mask)&(flag))
  68. #endif
  69.  
  70. /* Now, programmer's section... */
  71.  
  72. /* the WOOL_OBJECT type:
  73.  * Each object handled by WOOL is a pointer to a structure including at least:
  74.  *  - a pointer to a list of methods, common to all objects of this type.
  75.  *  - a reference count, integer telling how many objects points to this
  76.  *    one. The object should be freed as soon as this counter goes down to
  77.  *    zero. (first bit is to see if it is not in the zrt, rest is for count)
  78.  */
  79.  
  80. #ifndef DEBUG
  81. #define WOOL_HEADER \
  82.     struct _WOOL_OBJECT *(**type)(); \
  83.     unsigned int reference_count
  84. #else /* DEBUG */
  85. #define WOOL_HEADER \
  86.     struct _WOOL_OBJECT *(**type)(); \
  87.     int reference_count
  88. #endif /* DEBUG */
  89.  
  90. typedef struct _WOOL_OBJECT {
  91.     WOOL_HEADER;
  92. }               *WOOL_OBJECT;
  93.  
  94. typedef 
  95. WOOL_OBJECT(*WOOL_METHOD) ();
  96.  
  97. typedef WOOL_METHOD *WOOL_TYPE;
  98.  
  99. /* wool_error prints the file and linenum of error... */
  100.  
  101. #ifdef DEBUG
  102. #define wool_error(ERRstring,ERRmess) \
  103.     (WOOL_OBJECT) (wool_printf("\nC source file \"%s\"", __FILE__), \
  104.     wool_printf(" line %d\n", __LINE__), \
  105.     _wool_error(ERRstring,ERRmess)) 
  106. #if __STDC__
  107. #define    ASSERT(p)   if (!(p)) wool_error("assertion failed: %s", #p)
  108. #else /* __STDC__ */
  109. #define    ASSERT(p)   if (!(p)) wool_error("assertion failed: %s", "p")
  110. #endif /* __STDC__ */
  111. #else
  112. #define wool_error(s,m)    _wool_error(s,m)
  113. #define    ASSERT(p)
  114. #endif
  115.  
  116. /* global vars */
  117.  
  118. EXT int wool_error_status         INIT(0);/* set to 1 by a wool_error */
  119. EXT int wool_do_print_errors         INIT(1);/* shall we print errors? */
  120. EXT int wool_continue_reading_on_error     INIT(0);/* in files */
  121. EXT int wool_error_in_profile         INIT(0);/* do we abort? */
  122. EXT int wool_max_print_level         INIT(9);/* max list imbrication */
  123. EXT int wool_print_level         INIT(0);/* current value */
  124. EXT char *wool_shell_name        INIT(0);/* var name of forked shell */
  125. EXT WOOL_OBJECT wool_host_name        INIT(0);/* host name running wool */
  126.  
  127. /* first include standards file if missing */
  128.  
  129. #ifndef NULL
  130. #define NULL 0
  131. #endif /* NULL */
  132.  
  133. /* to access easily any object's reference count */
  134.  
  135. #define REF(obj) (((WOOL_OBJECT) (obj))->reference_count)
  136.  
  137. /* Macros to ease initializations */
  138.  
  139. /* macro to define and initialize wool atoms in the code: 
  140.  *     type is WOOL_Atom or WOOL_OBJECT
  141.  *    C_name is the name in the C code (i.e NIL)
  142.  *    wool_value is the string which will represent the atom
  143.  *    value is an object taken as the c_val
  144.  */
  145.  
  146. #define wool_atom_make(type, C_name, wool_name, value) \
  147.     C_name = (type) wool_atom(wool_name); \
  148.     increase_reference(((WOOL_Atom) C_name)->c_val = (WOOL_OBJECT) value)
  149.  
  150. #define wool_atom_with_numeric_value_make(name, value) \
  151.     WLAtom_set(wool_atom(name), WLNumber_make(value))
  152.  
  153. /* type-checking macros */
  154.  
  155. #define must_be_or_nil(typename, arg, n) \
  156.     if (((WOOL_OBJECT) arg) != NIL && (arg) -> type != typename)     \
  157.         bad_argument(arg, n, WOOL_TYPE_P_NAME(typename))
  158.  
  159. #define must_be(typename, object, n) \
  160.     if (object -> type != typename) \
  161.             bad_argument(object, n, WOOL_TYPE_P_NAME(typename))
  162.  
  163. /* get from context */
  164.  
  165. #define get_C_from_object(atom) \
  166.     (long) WOOL_send(WOOL_get_C_value, (atom), ((atom)))
  167.  
  168. #define get_val_from_context(field, atom) \
  169.     increase_reference((WOOL_OBJECT) (field = (void*) \
  170.         WOOL_send(WOOL_eval, atom, (atom))))
  171.  
  172. #define get_bool_from_context(field, atom) \
  173.     field = (WOOL_send(WOOL_eval, atom, (atom)) == NIL) ? 0 : 1
  174.  
  175. /* generic numerical type */
  176. #ifdef PTR_TYPE
  177. typedef PTR_TYPE Num;
  178. #else /* PTR_TYPE */
  179. typedef long Num;
  180. #endif
  181.  
  182. #ifdef CARD32
  183. typedef CARD32 Card32;
  184. #else /* CARD32 */
  185. typedef unsigned long    Card32;
  186. #endif /* CARD32 */
  187.  
  188. /* for alignement problems, gives next valid pointer */
  189. #define ALIGN(ptr) ((((ptr) >> 2) + 1) << 2)
  190.  
  191. #if defined(SYSV) || defined(SVR4)
  192. #include <stdlib.h>
  193. #define NO_MALLOC_DECLARE        /* since already done in stdlib.h */
  194. #endif /* SYSV */
  195.  
  196. #ifndef NO_MALLOC_DECLARE
  197. #ifdef VOID_MALLOC
  198. extern void *malloc(), *realloc(), *calloc();
  199. #else
  200. extern char *malloc(), *realloc(), *calloc();
  201. #endif
  202. #endif
  203.  
  204. #ifndef DO_NOT_REDEFINE_MALLOC
  205. #define Malloc(bytes) malloc(bytes)
  206. #define Free(bytes) free(bytes)
  207. #define Realloc(ptr, bytes) realloc(ptr, bytes)
  208. #define Calloc(ptr, bytes) calloc(ptr, bytes)
  209. #else /* DO_NOT_REDEFINE_MALLOC */
  210. #define Malloc(bytes) wool_malloc(bytes)
  211. #define Free(bytes) wool_free(bytes)
  212. #define Realloc(ptr, bytes) wool_realloc(ptr, bytes)
  213. #define Calloc(ptr, bytes) wool_calloc(ptr, bytes)
  214. #ifdef VOID_MALLOC
  215. extern void *wool_malloc(), *wool_realloc(), *wool_calloc();
  216. #else
  217. extern char *wool_malloc(), *wool_realloc(), *wool_calloc();
  218. #endif
  219.  
  220. #endif /* DO_NOT_REDEFINE_MALLOC */
  221.  
  222. #ifdef USE_STANDARD_MALLOC
  223. /* standard realloc might not like null pointers */
  224. #undef Realloc
  225. #define Realloc(ptr, bytes) ((ptr) ? realloc(ptr, bytes) : malloc(bytes))
  226. #endif
  227.  
  228. #ifndef DEBUG
  229. #define increase_reference(obj) REF(obj) += 2
  230. #define decrease_reference(obj) {if(obj && (REF(obj)-=2)==1)zrt_put(obj);}
  231. #define decrease_reference_non_null(obj) {if((REF(obj)-=2)==1)zrt_put(obj);}
  232. #endif /* DEBUG */
  233.  
  234. #define dft_gc() while (dft_last > dft) Free(*(--dft_last))
  235.  
  236. /* exported functions */
  237.  
  238. EXT int        wool_init();
  239. EXT WOOL_OBJECT _wool_error();
  240. EXT WOOL_OBJECT wool_print();
  241. EXT WOOL_OBJECT wool_subr_make();
  242. EXT WOOL_OBJECT wool_eval();
  243. EXT WOOL_OBJECT wool_read();
  244. EXT int         wool_pool();
  245. EXT WOOL_OBJECT NIL_FUNC();
  246. EXT WOOL_OBJECT wool_type_or_evaluate();
  247. EXT WOOL_OBJECT zrt_put();
  248. EXT WOOL_OBJECT bad_argument();
  249.  
  250. /* exported objects */
  251.  
  252. EXT WOOL_OBJECT wool_read_expr INIT(NULL);
  253. EXT WOOL_OBJECT temp_obj;
  254.  
  255. EXT WOOL_OBJECT  *zrt, *zrt_last;
  256. EXT int     zrt_size, zrt_limit;
  257.  
  258. EXT char **dft, **dft_last, **dft_last_allocated;
  259.  
  260. EXT jmp_buf     wool_error_jmpbuf;
  261. #ifdef NO_VOID
  262. typedef int    *jmp_buf_ptr;    /* this works if jmp_buf is an int[] */
  263. #else
  264. typedef void    *jmp_buf_ptr;    /* this works if jmp_buf is an int[] */
  265. #endif
  266. EXT jmp_buf_ptr wool_goes_here_on_error 
  267. INIT(wool_error_jmpbuf);
  268.  
  269. EXT WOOL_OBJECT NIL;        /* NIL is just a predefined atom */
  270. EXT WOOL_OBJECT TRU;        /* t is just a predefined atom */
  271. EXT WOOL_OBJECT QUOTE;        /* Function needed for parsing */
  272. EXT WOOL_OBJECT UNDEFINED_WOOL_VALUE; /* for undefined value */
  273. EXT WOOL_OBJECT WA_progn;    /* the progn atom */
  274.  
  275. #ifndef NULL
  276. #define NULL 0
  277. #endif /* NULL */
  278.  
  279. #define UNDEFINED (WOOL_OBJECT) 0
  280. #define NARY    -1
  281. #define ANY    0xFFFFFFFF
  282.  
  283. /*
  284.  *  this macro is used to say where to go after an wool_error
  285.  *  NOTE: be sure the function this macros is in won't have returned
  286.  *  before the call to wool_error!
  287.  */
  288.  
  289. #define set_wool_error_resume_point() (\
  290.     calling_function_on_error = calling_function_current \
  291.         - calling_function_stack, \
  292.     wool_stackframe_on_error = wool_current_stackframe, \
  293.     setjmp(wool_goes_here_on_error))
  294.  
  295. #define save_wool_error_resume_point() \
  296.     int old_calling_function_on_error = calling_function_on_error; \
  297.     WOOL_StackFrame old_wool_stackframe_on_error = wool_stackframe_on_error;\
  298.     jmp_buf_ptr old_wool_goes_here_on_error = wool_goes_here_on_error;\
  299.     jmp_buf         current_jmpbuf;\
  300.     Trace('l',("Saving at %s:%d: buf [%d]\n",__FILE__,__LINE__,jmpbuf_checksum(wool_goes_here_on_error)));\
  301.     wool_goes_here_on_error = current_jmpbuf
  302.  
  303.  
  304. #define restore_wool_error_resume_point() \
  305.     calling_function_on_error = old_calling_function_on_error; \
  306.     wool_stackframe_on_error = old_wool_stackframe_on_error; \
  307.     wool_goes_here_on_error = old_wool_goes_here_on_error ;\
  308.     Trace('l',("Restoring at %s:%d: buf [%d]\n",__FILE__,__LINE__,jmpbuf_checksum(wool_goes_here_on_error)))
  309.  
  310. /*
  311.  * error messages
  312.  */
  313.  
  314. #define UNDEFINED_VARIABLE   0
  315. #define BAD_NUMBER_OF_ARGS   1
  316. #define UNDEFINED_FUNCTION   2
  317. #define BAD_DEFUN          3
  318. #define BAD_ARG_TYPE         4
  319. #define RELEASING_ATOM       5
  320. #define BAD_LOCAL_SYNTAX     6
  321. #define NOT_AN_ATOM         7
  322. #define SYNTAX_ERROR         8
  323. #define INTERNAL_ERROR         9
  324. #define TIME_EXCEEDED        10
  325. #define CANNOT_SET        11
  326. #define CANNOT_GET_C_VALUE  12
  327. #define TOO_MANY_PARAMETERS 13
  328. #define NON_WOOL_OBJECT        14
  329. #define UNDEFINED_METHOD    15
  330. #define NO_MEMORY        16
  331. #define NOT_REDEFINABLE     17
  332. #define SILENT_ERROR        18
  333. #define NOT_MODIFIABLE        19
  334. #define USER_ERROR          20    
  335.  
  336. /* the send define:
  337.  * called by WOOL_send(WOOL_MethodName,
  338.  *               object,
  339.  *               (object, parm1, parm2, ... )); NEED parenthesises !!!
  340.  */
  341.  
  342. #ifndef DEBUG
  343. #define WOOL_send(message,object,parms) (*(((object)->type)[message]))parms
  344. #else /* DEBUG */
  345. #define WOOL_send(message,object,parms) \
  346.     (WOOL_current_method = message, \
  347.      wool_send_is_valid(message,object), \
  348.     (WOOL_OBJECT) (*(((object)->type)[message]))parms)
  349. #endif
  350.  
  351. #ifdef SECURE
  352. extern char *wool_methods_names[];
  353. #endif /* SECURE */
  354.  
  355. #define WOOL_TYPE_P_NAME(type) ((WOOL_Atom) (type)[0]) -> p_name
  356.  
  357. /* the methods for all WOOL types */
  358.  
  359. /*    method name          N arity desc */
  360. #define WOOL_type_name         0    /* 0 not a method but the atom of type name */
  361. #define WOOL_eval          1    /* 1 evaluates object */
  362. #define WOOL_print          2    /* 1 print object */
  363. #define WOOL_free         3    /* 1 release storage of object */
  364. #define WOOL_execute          4    /* 2 execute (as CAR of list) */
  365. #define WOOL_set         5    /* 2 set value (called by setq) */
  366. #define WOOL_get_C_value     6    /* 1 extract the raw pointed obj */
  367. #define WOOL_open         7    /* 1 create the X Window */
  368. #define WOOL_close         8    /* 1 destroy it */
  369. #define WOOL_process_event   9    /* 2 trigger the event handler */
  370. #define WOOL_copy        10    /* 1 make a copy of itself */
  371. #define WOOL_get_dimensions 11    /* 2 adjust and get dimensions */
  372. #define WOOL_redraw        12    /* 2 adjust and redraw an object */
  373. #define WOOL_equal        13    /* 2 a == b ? */
  374. #define WOOL_reconfigure    14    /* 2 move after WOOL_redraw */
  375. #define WOOL_setq        15    /* 2 set but do not eval value */
  376. #define WOOL_make        16  /* 1 create a new object */
  377. #define WOOL_silent_eval    17  /* 1 eval but do not break on undefined */
  378.  
  379. /* the undefined methods (aborts!), one per arity */
  380.  
  381. EXT WOOL_OBJECT wool_undefined_method_1();
  382. EXT WOOL_OBJECT wool_undefined_method_2();
  383.  
  384. #ifdef DEBUG
  385. EXT int WOOL_current_method;
  386. #define FREED_MAGIC 0x31416    /* block has been freed */
  387. #else
  388. #define stop_if_in_dbx()
  389. #endif /* DEBUG */
  390.  
  391. #define UndefinedPos    -1
  392.  
  393. /* the user-level debugging functions */
  394.  
  395. EXT int wool_tracing_on INIT(0);
  396. EXT WOOL_OBJECT wool_tracing_on_EXPR INIT(0);
  397. EXT int wool_tracing_level INIT(0);
  398. EXT int wool_still_tracing INIT(0);
  399.  
  400. #if defined(__HIGHC__)
  401. pragma on(POINTERS_COMPATIBLE);
  402. #endif
  403.  
  404. /* debugging-only traces */
  405.  
  406. #ifdef DEBUG
  407. EXT char * WlTraceFlags INIT("");
  408. #define Trace(flag, args) if(strchr(WlTraceFlags, flag)) printf args
  409. #define TraceDo(flag, inst) if(strchr(WlTraceFlags, flag)) inst
  410. #else /* DEBUG */
  411. #define Trace(flag, args)
  412. #define TraceDo(flag, inst) 
  413. #endif /* DEBUG */
  414.  
  415. /* no stats with USE_STANDARD_MALLOC */
  416. #ifdef STATS
  417. #ifdef USE_STANDARD_MALLOC
  418. #undef STATS
  419. #endif
  420. #endif
  421.  
  422. #endif /* INCLUDE_WOOL_H */
  423.