Debian Codegraph @

terps/tads/tads2/tadsexit.h

raw symbols SCIP
1 /*
2 * Copyright (c) 1992, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 tadsexit.h - user exit definitions for TADS version 2
10 Function
11 Defines the interface for user exit functions
12 Notes
13 User exits need a way of getting to certain internal TADS functions.
14 These functions are provided as a vector of function pointers in
15 a structure passed to the user exit: the tadsuxdef.
16 Modified
17 04/30/92 MJRoberts - creation
18 */
19
20 #ifndef TADSEXIT_INCLUDED
21 # define TADSEXIT_INCLUDED
22
23 /*
24 * On MS-DOS, user exits are compiled in "tiny" model (suitable for
25 * making into a .COM file), but since TADS is a large-model program,
26 * data and functions provided by TADS must be referenced with "far"
27 * pointers. This keyword is not accepted by compilers on most other
28 * systems, so we'll define the keyword to nothing if we're not on
29 * MS-DOS. Be sure use the compiler option -DMSDOS when building on
30 * MS-DOS!
31 */
32 #ifndef osfar_t
33 # ifdef MSDOS
34 # define osfar_t far
35 # else
36 # define osfar_t
37 # endif /* MSDOS */
38 #endif /* osfar_t */
39
40 /*
41 * TADS datatypes usable by user-exit functions. These types are
42 * returned by tads_tostyp, and used in tads_push for the 'type'
43 * argument.
44 */
45 #define TADS_NUMBER 1 /* a number (long integer) */
46 #define TADS_SSTRING 3 /* constant (single-quoted) string */
47 #define TADS_NIL 5 /* nil (false, no value) */
48 #define TADS_TRUE 8 /* true */
49 /* #define TADS_RSTRING 12 */ /* OBSOLETE - DO NOT USE */
50
51 /*
52 * "String descriptor" type. Use this type to hold the return value of
53 * the tads_popstr() function. Note that a string descriptor is NOT a
54 * pointer to a C-style null-terminated string; instead, you must use
55 * the tads_strlen() and tads_strptr() functions to get the length and
56 * text pointer from a descriptor.
57 */
58 typedef char osfar_t *tads_strdesc;
59
60
61 /*
62 * tadsufdef: run-time system callback function vector.
63 */
64 struct tadsufdef
65 {
66 /* type of top of stack */
67 int (osfar_t *tadsuftyp)(void osfar_t *);
68
69 /* pop a number */
70 long (osfar_t *tadsufnpo)(void osfar_t *);
71
72 /* pop a string */
73 tads_strdesc (osfar_t *tadsufspo)(void osfar_t *);
74
75 /* discard top item of stack */
76 void (osfar_t *tadsufdsc)(void osfar_t *);
77
78 /* push a number */
79 void (osfar_t *tadsufnpu)(void osfar_t *, long);
80
81 /* push TADS string (allocated with tads_stralo) */
82 void (osfar_t *tadsufspu)(void osfar_t *, unsigned char osfar_t *);
83
84 /* push a C-style null-terminated string */
85 void (osfar_t *tadsufcspu)(void osfar_t *, char osfar_t *);
86
87 /* allocate new string */
88 char osfar_t *(osfar_t *tadsufsal)(void osfar_t *, int);
89
90 /* push a logical value */
91 void (osfar_t *tadsuflpu)(void osfar_t *, int);
92 };
93 typedef struct tadsufdef tadsufdef;
94
95 /*
96 * tadsuxdef: context argument passed into user exit function. Used
97 * to identify callback routines.
98 */
99 struct tadsuxdef
100 {
101 struct tadscbdef osfar_t *tadsuxcb; /* callback context */
102 tadsufdef osfar_t *tadsuxuf; /* vector of functions */
103 int tadsuxac; /* argument count */
104 };
105 typedef struct tadsuxdef tadsuxdef;
106
107
108 /* internal service macros */
109 #define tads_vec(ctx) ((ctx)->tadsuxuf)
110 #define tads_c2u(p,i) ((unsigned int)(((unsigned char osfar_t *)(p))[i]))
111
112
113 /*
114 * These macros provide easy access to the TADS access functions. All
115 * of the callback macros require the TADS function vector (passed into
116 * the user exit as its argument) as the first argument.
117 */
118
119 /*
120 * return the datatype (TADS_xxx) of the value on top of the stack
121 * (i.e., the next value that will be returned by tads_pop)
122 */
123 #define tads_tostyp(ctx) ((*tads_vec(ctx)->tadsuftyp)(ctx))
124
125 /*
126 * Pop a number off stack (use when the item on top of the stack is a
127 * number, TADS_NUMBER). Returns a signed long.
128 */
129 #define tads_popnum(ctx) ((*tads_vec(ctx)->tadsufnpo)(ctx))
130
131 /*
132 * Pop a string off the stack. The pointer returned is to a string
133 * descriptor. You will need to use the tads_strlen() and tads_strptr()
134 * functions to get the length of the string and a pointer to its
135 * buffer -- do not attempt to use the returned pointer directly as
136 * a string.
137 */
138 #define tads_popstr(ctx) ((*tads_vec(ctx)->tadsufspo)(ctx))
139
140 /*
141 * Get the length of a string retrieved with tads_popstr().
142 */
143 #define tads_strlen(ctx, str) (tads_c2u(str, 0) + (tads_c2u(str, 1) << 8) - 2)
144
145 /*
146 * Get a pointer to the string buffer from a value retrieved with
147 * tads_popstr(). The value returned points to the actual text of
148 * the string. Note that this text will NOT be null-terminated;
149 * you must use tads_strlen() to determine the length of the string.
150 */
151 #define tads_strptr(ctx, str) ((char osfar_t *)((str) + 2))
152
153 /*
154 * pop next item off stack and discard it (use to remove TADS_TRUE and
155 * TADS_NIL values from the stack)
156 */
157 #define tads_pop(ctx) ((*tads_vec(ctx)->tadsufdsc)(ctx))
158
159 /*
160 * Push a number onto the stack.
161 */
162 #define tads_pushnum(ctx, num) ((*tads_vec(ctx)->tadsufnpu)(ctx, (long)(num)))
163
164 /*
165 * Push a C-style string onto the stack. The string must be a normal
166 * C-style null-terminated string. It should not have been allocated
167 * with tads_stralo().
168 */
169 #define tads_pushcstr(ctx, cstr) ((*tads_vec(ctx)->tadsufcspu)(ctx, str))
170
171 /*
172 * Allocate space for a string. The string written to the buffer
173 * returned does NOT need to be null-terminated, but it can be; the null
174 * byte will be ignored if it is present. The length you allocate will
175 * be the length of the string, regardless of the presence of any null
176 * bytes. When you allocate a string with this function, push it with
177 * the tads_pushastr() function. Do NOT push this string with the
178 * tads_pushcstr() function, which is only for C-style strings. This
179 * function returns a pointer to the buffer you should use for the
180 * string.
181 */
182 #define tads_stralo(ctx, len) ((*tads_vec(ctx)->tadsufsal)(ctx, (int)(len)))
183
184 /*
185 * Push a string allocated with tads_stralo().
186 */
187 #define tads_pushastr(ctx, str) ((*tads_vec(ctx)->tadsufspu)(ctx, str))
188
189
190 /* Push a logical value (true or nil) onto stack */
191 #define tads_pushtrue(ctx) ((*tads_vec(ctx)->tadsuflpu)(ctx, TADS_TRUE))
192 #define tads_pushnil(ctx) ((*tads_vec(ctx)->tadsuflpu)(ctx, TADS_NIL))
193
194
195 /* get number of arguments to this function */
196 #define tads_argc(ctx) ((ctx)->tadsuxac)
197
198
199 #endif /* TADSEXIT_INCLUDED */