slang.h

来自「这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统」· C头文件 代码 · 共 1,285 行 · 第 1/3 页

H
1,285
字号
#ifndef DAVIS_SLANG_H_
#define DAVIS_SLANG_H_
/* -*- mode: C; mode: fold; -*- */
/* Copyright (c) 1992, 1995 John E. Davis
 * All rights reserved.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */
#define SLANG_VERSION 9938
/*{{{ System Dependent Macros and Typedefs */

#if defined(__WATCOMC__) && !defined(__QNX__)
#  ifndef msdos
#    define msdos
#  endif
#  ifndef  DOS386
#    define  DOS386
#  endif
#  ifndef FLOAT_TYPE
#    define FLOAT_TYPE
#  endif
#  ifndef pc_system
#    define pc_system
#  endif
#endif /* __watcomc__ */

#ifdef unix
# ifndef __unix__
#  define __unix__ 1
# endif
#endif

#ifndef __GO32__
# ifdef __unix__
#  define REAL_UNIX_SYSTEM
# endif
#endif

/* Set of the various defines for pc systems.  This includes OS/2 */
#ifdef __MSDOS__
# ifndef msdos
#   define msdos
# endif
# ifndef pc_system
#   define pc_system
# endif
#endif

#ifdef __GO32__
# ifndef pc_system
#   define pc_system
# endif
# ifdef REAL_UNIX_SYSTEM
#   undef REAL_UNIX_SYSTEM
# endif
#endif

#if defined(__EMX__) && defined(OS2)
# ifndef pc_system
#   define pc_system
# endif
# ifndef __os2__
#   define __os2__
# endif
#endif

#ifdef __cplusplus
extern "C" {
#endif
#if 0
}
#endif
/* ---------------------------- Generic Macros ----------------------------- */

/*  __SC__ is defined for Symantec C++
   DOS386 is defined for -mx memory model, 32 bit DOS extender. */

#ifdef VOID
# undef VOID
#endif

#if defined(msdos) && !defined(DOS386) & !defined(__WIN32__) && !defined(__GO32__)
# ifdef __SC__
#  include <dos.h>
# endif
   typedef void *VOID_STAR;
# define VOID void
# include <alloc.h>
#else
# if defined (__cplusplus) || defined(__STDC__)
   typedef void *VOID_STAR;
#  define VOID void
# else
   typedef unsigned char *VOID_STAR;
#  define VOID unsigned char
# endif
#endif

#if 1
   typedef int (*FVOID_STAR)(void);
#else
# define FVOID_STAR VOID_STAR
#endif

#if defined(msdos) && !defined(DOS386) && !defined(__GO32__) && !defined(__WIN32__)
# define SLFREE(buf)  farfree((void far *)(buf))
# define SLMALLOC(x) farmalloc((unsigned long) (x))
# define SLREALLOC(buf, n) farrealloc((void far *) (buf), (unsigned long) (n))
# define SLCALLOC(n, m) farcalloc((unsigned long) (n), (unsigned long) (m))
#else
# if defined(VMS) && !defined(__DECC)
#  define SLFREE VAXC$FREE_OPT
#  define SLMALLOC VAXC$MALLOC_OPT
#  define SLREALLOC VAXC$REALLOC_OPT
#  define SLCALLOC VAXC$CALLOC_OPT
# else
#  define SLFREE(x) free((char *)(x))
#  define SLMALLOC malloc
#  if defined(__cplusplus) && !defined(__BEOS__)
#   define SLREALLOC(p,n) realloc((malloc_t) (p), (n))
#  else
#   define SLREALLOC realloc
#  endif
#  define SLCALLOC calloc
# endif
#endif

#ifdef SL_MALLOC_DEBUG
# undef SLMALLOC
# undef SLCALLOC
# undef SLREALLOC
# undef SLFREE
# define SLMALLOC(x) SLdebug_malloc((unsigned long) (x))
# define SLFREE(x) SLdebug_free((unsigned char *)(x))
# define SLCALLOC(n, m) SLdebug_calloc((unsigned long) (n), (unsigned long)(m))
# define SLREALLOC(p, x) SLdebug_realloc((unsigned char *)(p), (unsigned long)(x))
#endif /* SL_MALLOC_DEBUG */

   extern unsigned char *SLdebug_malloc (unsigned long);
   extern unsigned char *SLdebug_calloc (unsigned long, unsigned long);
   extern unsigned char *SLdebug_realloc (unsigned char *, unsigned long);
   extern void SLdebug_free (unsigned char *);
   extern void SLmalloc_dump_statistics (void);
   extern char *SLstrcpy(register char *, register char *);
   extern int SLstrcmp(register char *, register char *);
   extern char *SLstrncpy(char *, register char *, register  int);

   extern void SLmemset (char *, char, int);
   extern char *SLmemchr (register char *, register char, register int);
   extern char *SLmemcpy (char *, char *, int);
   extern int SLmemcmp (char *, char *, int);

#ifdef float64
# undef float64
#endif

#ifndef FLOAT64_TYPEDEFED
# define FLOAT64_TYPEDEFED
  typedef double float64;
#endif


/*}}}*/

/*{{{ Interpreter Typedefs */

#define SLANG_MAX_NAME_LEN 30
/* maximum length of an identifier */
/* first char in identifiers is the hash */

   /* Note that long is used for addresses instead of void *.  The reason for
    * this is that I have a gut feeling that sizeof (long) > sizeof(void *)
    * on some machines.  This is certainly the case for MSDOS where addresses
    * can be 16 bit.
    */
typedef struct SLang_Name_Type
  {
#ifdef SLANG_STATS
     int n;			       /* number of times referenced */
#endif
     char name[SLANG_MAX_NAME_LEN + 2]; /* [0] is hash */

     unsigned char sub_type;

/* Values for main_type may be as follows.  The particlular values are
 * for compatability.
 */
#define SLANG_LVARIABLE		0x01
#define SLANG_INTRINSIC 	0x06
#define SLANG_FUNCTION  	0x07
#define SLANG_GVARIABLE 	0x0D
#define SLANG_IVARIABLE 	0x0E           /* intrinsic variables */
/* Note!!! For Macro MAKE_VARIABLE below to work, SLANG_IVARIABLE Must
   be 1 less than SLANG_RVARIABLE!!! */
#define SLANG_RVARIABLE		0x0F	       /* read only variable */
     unsigned char main_type;
     long addr;
  }
SLang_Name_Type;


typedef struct SLang_Load_Type
{
   long name;			       /* file name, string address, ... */
   long handle;			       /* FILE *, string address, etc... */

   char *ptr;			       /* input pointer to next line in object
					* to be read.
					*/
   /* Things below here are used by S-Lang. */
   int type;			       /* 'F' = file, 'S' = String, etc.. */
   char *buf;			       /* buffer for file, etc... */
   char *(*read)(struct SLang_Load_Type *);   /* function to call to read obj */
   int n;			       /* line number, etc... */
   char token[256];		       /* token to be parsed */
   int ofs;			       /* offset from buf where last read
					* took place
					*/
   int top_level;		       /* 1 if at top level of parsing */
} SLang_Load_Type;

#if defined(ultrix) && !defined(__GNUC__)
# ifndef NO_PROTOTYPES
#  define NO_PROTOTYPES
# endif
#endif

#ifndef NO_PROTOTYPES
# define _PROTO(x) x
#else
# define _PROTO(x) ()
#endif

typedef struct SL_OOBinary_Type
{
   unsigned char sub_type;	       /* partner type for binary op */

   /* The function take the binary op as first argument, the operand types
    * form the second and third parameters and the last two parameters are
    * pointers to the objects themselves.  It is up to the function to push
    * the result on the stack.  It must return 1 if it handled the operation
    * return zero if the operation is not defined.
    */
   int (*binary_function)_PROTO((int, unsigned char, unsigned char,
				  VOID_STAR, VOID_STAR));

   struct SL_OOBinary_Type *next;
}
SL_OOBinary_Type;

typedef struct
{
   /* Methods */
   void (*destroy)_PROTO((VOID_STAR));
   /* called to delete/free the object */
   char *(*string)_PROTO((VOID_STAR));
   /* returns a string representation of the object */
   int (*unary_function)_PROTO((int, unsigned char, VOID_STAR));
   /* unary operation function */
   SL_OOBinary_Type *binary_ops;

   int (*copy_function)_PROTO((unsigned char, VOID_STAR));
   /* This function is called do make a copy of the object */
} SLang_Class_Type;

extern SLang_Class_Type *SLang_Registered_Types[256];

typedef struct
{
   unsigned char main_type;	       /* SLANG_RVARIABLE, etc.. */
   unsigned char sub_type;	       /* int, string, etc... */
   long *obj;			       /* address of user structure */

   /* Everything below is considered private */
   unsigned int count;		       /* number of references */
}
SLuser_Object_Type;


/*}}}*/
/*{{{ Interpreter Function Prototypes */

  extern volatile int SLang_Error;
  /* Non zero if error occurs.  Must be reset to zero to continue. */

  extern int SLang_Traceback;
  /* If non-zero, dump an S-Lang traceback upon error.  Available as
     _traceback in S-Lang. */

  extern char *SLang_User_Prompt;
  /* Prompt to use when reading from stdin */
  extern int SLang_Version;

  extern void (*SLang_Error_Routine)(char *);
  /* Pointer to application dependent error messaging routine.  By default,
     messages are displayed on stderr. */

  extern void (*SLang_Exit_Error_Hook)(char *);
  extern void SLang_exit_error (char *);
  extern void (*SLang_Dump_Routine)(char *);
  /* Called if S-Lang traceback is enabled as well as other debugging
     routines (e.g., trace).  By default, these messages go to stderr. */

  extern void (*SLang_Interrupt)(void);
  /* function to call whenever inner interpreter is entered.  This is
     a good place to set SLang_Error to USER_BREAK. */

  extern void (*SLang_User_Clear_Error)(void);
  /* function that gets called when '_clear_error' is called. */
  extern int (*SLang_User_Open_Slang_Object)(SLang_Load_Type *);
  extern int (*SLang_User_Close_Slang_Object)(SLang_Load_Type *);
  /* user defined loading routines. */


  /* If non null, these call C functions before and after a slang function. */
  extern void (*SLang_Enter_Function)(char *);
  extern void (*SLang_Exit_Function)(char *);


/* Functions: */

   extern int init_SLang(void);
   /* This function is mandatory and must be called by all applications */
   extern int init_SLfiles(void);
   /* called if fputs, fgets, etc are need in S-Lang */
   extern int init_SLmath(void);
   /* called if math functions sin, cos, etc... are needed. */

   extern int init_SLunix(void);
   /* unix system functions chmod, stat, etc... */

   extern int init_SLmatrix(void);

   extern int SLang_add_table(SLang_Name_Type *, char *);
   /* add application dependent function table p1 to S-Lang.  A name p2 less
    *  than 32 characters must also be supplied.
    * Returns 0 upon failure or 1 upon success. */

   extern int SLang_add_global_variable (char *);
   extern int SLang_load_object(SLang_Load_Type *);
   extern int SLang_load_file(char *);
   /* Load a file of S-Lang code for interpreting.  If the parameter is
   NULL, input comes from stdin. */

   extern void SLang_restart(int);
   /* should be called if an error occurs.  If the passed integer is
    * non-zero, items are popped off the stack; otherwise, the stack is
    * left intact.  Any time the stack is believed to be trashed, this routine
    * should be called with a non-zero argument (e.g., if setjmp/longjmp is
    * called). */

   extern void SLang_byte_compile_file(char *, int *);
   /* takes a file of S-Lang code and ``byte-compiles'' it for faster
    * loading.  The new filename is equivalent to the old except that a `c' is
    * appended to the name.  (e.g., init.sl --> init.slc).  If the second
    * parameter is non-zero, preprocess the file only.
    */

   extern void SLang_autoload(char *, char *);
   /* Automatically load S-Lang function p1 from file p2.  This function
      is also available via S-Lang */

   extern char *SLang_load_string(char *);
   /* Like SLang_load_file except input is from a null terminated string. */

   extern void SLang_do_pop(void);
   /* pops item off stack and frees any memory associated with it */

   extern int SLang_pop_integer(int *);
   /* pops integer *p0 from the stack.  Returns 0 upon success and non-zero
    * if the stack is empty or a type mismatch occurs, setting SLang_Error.
    */

   extern int SLpop_string (char **);
   extern int SLang_pop_string(char **, int *);
   /* pops string *p0 from stack.  If *p1 is non-zero, the string must be
    * freed after its use.  DO NOT FREE p0 if *p1 IS ZERO! Returns 0 upon
    * success */

   extern int SLang_pop_float(float64 *, int *, int *);
   /* Pops float *p1 from stack.  If *p3 is non-zero, *p1 was derived
      from the integer *p2. Returns zero upon success. */

   extern SLuser_Object_Type *SLang_pop_user_object (unsigned char);
   extern void SLang_free_user_object (SLuser_Object_Type *);
   extern void SLang_free_intrinsic_user_object (SLuser_Object_Type *);
   /* This is like SLang_free_user_object but is meant to free those
    * that have been declared as intrinsic variables by the application.
    * Normally an application would never need to call this.
    */

   extern void SLang_push_user_object (SLuser_Object_Type *);
   extern SLuser_Object_Type *SLang_create_user_object (unsigned char);

   extern int SLang_add_unary_op (unsigned char, FVOID_STAR);
   extern int SLang_add_binary_op (unsigned char, unsigned char, FVOID_STAR);
   extern int SLang_register_class (unsigned char, FVOID_STAR, FVOID_STAR);
   extern int SLang_add_copy_operation (unsigned char, FVOID_STAR);

   extern long *SLang_pop_pointer(unsigned char *, unsigned char *, int *);
   /* Returns a pointer to object of type *p1,*p2 on top of stack.
      If *p3 is non-zero, the Object must be freed after use. */


   extern void SLang_push_float(float64);
   /* Push Float onto stack */

   extern void SLang_push_string(char *);
   /* Push string p1 onto stack */

   extern void SLang_push_integer(int);
   /* push integer p1 on stack */

   extern void SLang_push_malloced_string(char *);
   /* The normal SLang_push_string mallocs space for the string.  This one
      does not.  DO NOT FREE IT IF YOU USE THIS ROUTINE */

   extern int SLang_is_defined(char *);
   /* Return non-zero is p1 is defined otherwise returns 0. */

   extern int SLang_run_hooks(char *, char *, char *);
   /* calls S-Lang function p1 pushing strings p2 and p3 onto the stack
    * first.  If either string is NULL, it is not pushed. If p1 is not
    * defined, 0 is returned. */

   extern int SLang_execute_function(char *);
   /* Call S-Lang function p1.  Returns 0 if the function is not defined
    * and 1 if it is.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?