Debian Codegraph @

terps/tads/tads2/msdos/testux.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1991, 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 testux.c - example of TADS user exit
9
10 This is an example of how to write a TADS user exit function. This
11 particular user exit doesn't do anything useful, but it illustrates
12 how to build a user exit.
13
14 Building user exits is an inherently complex process that requires
15 knowledge of C assembler programming. Please consult your compiler
16 manual for details on the operations described below. If you use
17 a different compiler than described below, you may or may not be able
18 to build user exits with it; generally, you'll need the equivalent
19 functionality described below to make it work.
20
21 Building user exits varies by operating system. We expect that most
22 user exits will directly access the computer hardware or the operating
23 system; user exits that do so will be non-portable, so you will have
24 to rewrite the exit for each operating system.
25
26 Note, however, that the user exit mechanism itself, apart from the
27 details of compiling the C code, is portable; if your code doesn't
28 perform any platform-specific hardware or operating system operations,
29 and conforms to the usual C portability rules (with respect to the sizes
30 of types, ordering of bytes, and so on), your C code will be portable;
31 the only thing you'll have to change when porting to a different
32 platform is the procedure to build the executable code. This example
33 program should be fully portable to all platforms that support the TADS
34 external function feature, and should not require any source code
35 changes when moving from platform to platform.
36
37 -------------------------------------------------------------------------
38 To build for 32-bit Windows platforms (Windows 95 and NT) with
39 Microsoft Visual C++ 5.0 or higher:
40
41 Compile this file with the following command (replacing "TadsDirectory"
42 with the path to your TADS executables directory):
43
44 cl /LD /I"TadsDirectory" testux.c /link /export:main /out:myfunc.dll
45
46 This will produce a file called MYFUNC.DLL. Put this DLL file in
47 the same directory as the testux.gam file.
48
49 -------------------------------------------------------------------------
50 To build for 32-bit Windows (95 and NT) with Borland C 4.5 or later:
51
52 First, create the file TESTUX.DEF with the following contents:
53
54 exports
55 main=_main
56
57 Now compile this file using this command, replacing "BorlandLibDir"
58 with the path to your Borland C library files (usually \BC45\LIB),
59 and "TadsDir" with the path to your TADS executables:
60
61 bcc32 -WD -L"BorlandLibDir" -I"TadsDir" testux.c
62
63 Finally, rename the resulting DLL file to MYFUNC.DLL, so that it
64 matches the name of the external function. Put this DLL file in
65 the same directory as the testux.gam file.
66
67 -------------------------------------------------------------------------
68 To build on Macintosh with Think C:
69 Create a new project, and use "Source/Add..." to add this file
70 Use "Project/Set Project Type..." to set the project type
71 to "Code Resource", with a type of XFCN, any number for the
72 resource ID, and myfunc for the name.
73 Use "Project/Build Code Resource..." to compile this file and
74 generate the code resource. For testing purposes, you can
75 add your XFCN to the TADS Compiler's resource fork by checking
76 the "merge" box, finding the TADS Compiler, and typing its
77 name; if you do this, you can use this user exit directly
78 from the compiler's "-d" debug playing mode. When you are
79 ready to make a self-loading executable file from your game,
80 first build your game's self-loading executable, then use
81 "Project/Build Code Resource..." to add it directly (with the
82 "merge" option) to your game's executable file.
83
84 -------------------------------------------------------------------------
85 To build on MS-DOS with Turbo C:
86 Compile this file with tiny memory model (tcc -mt -c -DMSDOS testux.c)
87 Link with tadsux.obj with /t option (tlink /t tadsux testux,testux.bin)
88 Compile the game file (tc testux.t)
89 Add testux.bin to the game file as a code resource
90 (tadsrsc testux.gam -+testux.bin=myfunc)
91
92 -------------------------------------------------------------------------
93 To build on MS-DOS with Microsoft C 6.00 or higher:
94 Compile this file with tiny memory model - be sure to define MSDOS
95 (cl -c -AT -DMSDOS -Gs -Zl testux.c)
96 Link with tadsux.obj (link tadsux testux,testux.exe;)
97 Run the DOS program EXE2BIN on the output (exe2bin testux.exe testux.bin)
98 Compile the game file (tc testux.t)
99 Add testux.bin to the game file as a code resource
100 (tadsrsc testux.gam -+testux.bin=myfunc)
101
102 -------------------------------------------------------------------------
103 To build on Atari ST with Mark Williams C:
104 Compile this file with small model (cc -c -VSMALL testux.c)
105 Link with tadsux.o: ld -o testux.bin -s tadsux.o testux.o
106 Compile the game file (tc testux.t)
107 Add testux.bin to the game file as a code resource
108 (tadsrsc testux.gam -+testux.bin=myfunc)
109
110 If you use a compiler other than MWC, you will need to build using
111 small model (i.e., all addressing is PC-relative) and ensure that
112 the program is linked so that the entrypoint is at offset 0 within
113 the .prg file. Linking as shown above should work with other
114 compilers.
115
116 WARNING! The MWC compiler appears to have a bug with respect to
117 the -VSMALL switch. Although the compiler correctly generates
118 PC-relative addressing for subroutine calls and user data, it
119 still uses absolute addressing for its internal "switch" tables
120 (these are the tables the compiler builds for jumping to the
121 appropriate "case" label in a "switch" statement). Therefore,
122 you cannot use "switch" statements in user exit programs; use
123 an if-else tree (as in the program below) instead.
124
125 If you encounter strange errors (probably bus or address errors),
126 look at the object code your compiler generates, and make sure no
127 load-time relocation fixups are needed. User exits must be entirely
128 free of relocation fixups, since the compiler just loads the
129 binary image of your program and executes it starting at offset 0.
130 */
131
132 #include "tadsexit.h" /* defines needed macros and structures */
133
134
135 /*
136 * The main function of a user exit is always called main(). It has
137 * one argument: a context pointer (the structure is defined in
138 * tadsexit.h).
139 */
140 int main(ctx)
141 tadsuxdef osfar_t *ctx; /* context pointer */
142 {
143 long num;
144 int typ;
145 int err = 0; /* presume that we will be successful */
146 tads_strdesc oldstr;
147 char osfar_t *oldp;
148 char osfar_t *newstr;
149 char osfar_t *p;
150 int len;
151
152 /* get argument type */
153 typ = tads_tostyp(ctx);
154
155 /* figure out what to do based on type */
156 if (typ == TADS_NUMBER)
157 {
158 num = tads_popnum(ctx); /* get the number */
159 num <<= 1; /* double it */
160 tads_pushnum(ctx, num); /* return doubled value */
161 }
162 else if (typ == TADS_SSTRING)
163 {
164 /* get the string descriptor */
165 oldstr = tads_popstr(ctx);
166
167 /* note that this is just a descriptor - get length and text ptr */
168 len = tads_strlen(ctx, oldstr); /* get its length */
169 oldp = tads_strptr(ctx, oldstr); /* get pointer to its text */
170
171 /* allocate space for a new string */
172 newstr = tads_stralo(ctx, len);
173
174 /* reverse the string, copying into new buffer */
175 oldp += len; /* point to END of old string */
176 for (p = newstr ; len ; --len)
177 *p++ = *--oldp;
178
179 /* push the new value - use tads_pushastr, since we allocated it */
180 tads_pushastr(ctx, newstr);
181 }
182 else if (typ == TADS_NIL)
183 {
184 tads_pop(ctx);
185 tads_pushtrue(ctx);
186 }
187 else if (typ == TADS_TRUE)
188 {
189 tads_pop(ctx);
190 tads_pushnil(ctx);
191 }
192 else
193 {
194 /* we can't handle this datatype - return an error */
195 err = 1;
196 }
197
198 return err;
199 }