Debian Codegraph @

terps/tads/tads2/msdos/os0tc.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/OS0TC.C,v 1.2 1999/05/17 02:52:18 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1992, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 os0tc.c - os mainline for tads2 compiler
15 Function
16 invokes compiler from operating system command line
17 Notes
18 none
19 Modified
20 04/02/92 MJRoberts - creation
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "os.h"
26
27 #ifdef __DPMI16__
28 #include <dos.h>
29 #include <windows.h>
30 #include "ltk.h"
31
32
33 /*
34 * real-mode break handler (see the installation routine below for
35 * information)
36 */
37 static void _interrupt realmode_break()
38 {
39 asm {
40 push ds
41 push si
42
43 /* get the int 23h vector pointer into DS:SI */
44 mov si, 0
45 mov ds, si
46 mov si, 0x23 * 4
47 lds si, [si]
48
49 /* set the byte 2 bytes before the handler (me) */
50 /* -- this byte is the flag that indicates ^C was hit */
51 sub si, 2
52 mov byte ptr [si], 1
53
54 pop si
55 pop ds
56 }
57 }
58
59 /*
60 * Protected-mode pointer to the real-mode interrupt handler. The
61 * handler's entrypoint is 2 bytes above this pointer; the first byte at
62 * this location is a flag which, when non-zero, means that a ^C
63 * occurred.
64 */
65 static char *brk_pointer;
66
67 static void _interrupt _loadds protmode_break()
68 {
69 *brk_pointer = 1;
70 }
71
72 /*
73 * Set up a control-break interrupt handler for DPMI mode. With a DPMI
74 * DOS extender, it's necessary to hook the int 23h ^C interrupt in
75 * real-mode in order to make sure we get notified of a control-break or
76 * ^C. We allocate a real-mode segment, copy our handler into that
77 * segment, and call the DPMI server to set the int 23h vector to point
78 * to our real-mode handler. The handler merely sets a flag (which is
79 * the two bytes immediately before the handler's entrypoint, so that we
80 * can reference it from either mode) which we can test in the
81 * compiler's main loop.
82 */
83 static void init_dpmi16_break(void)
84 {
85 unsigned long realaddr;
86 unsigned realseg;
87 unsigned realofs;
88 unsigned protseg;
89 unsigned protofs;
90 char *protptr;
91 unsigned int_page_seg;
92 char *int_page_ptr;
93
94 /* allocate a block of real-mode memory big enough for the handler */
95 realaddr = GlobalDosAlloc(0x200);
96 realseg = realaddr >> 16;
97 protseg = realaddr & 0xffff;
98
99 /* set up a protected-mode pointer to the real-mode memory */
100 realofs = 0;
101 protptr = MK_FP(protseg, realofs);
102
103 /* copy the interrupt handler */
104 memcpy(protptr + 2, realmode_break, 100);
105
106 /* set the real-mode interrupt 23h vector to point to the handler */
107 asm {
108 /* point the int 23h (DOS break) vector to our handler */
109 mov cx, realseg
110 mov dx, realofs
111 add dx, 2
112 mov bl, 0x23
113 mov ax, 0x0201
114 int 0x31
115
116 /* for good measure, handle int 1b (BIOS break) the same way */
117 mov cx, realseg
118 mov dx, realofs
119 add dx, 2
120 mov bl, 0x1b
121 mov ax, 0x0201
122 int 0x31
123 }
124
125 /* save the location of the ^C flag, and clear the flag */
126 brk_pointer = protptr;
127 *brk_pointer = 0;
128
129 /* set the protected-mode interrupt 23h and 1bh vectors as well */
130 protseg = FP_SEG(protmode_break);
131 protofs = FP_OFF(protmode_break);
132 asm {
133 /* set int 23h to point to our handler */
134 mov cx, protseg
135 mov dx, protofs
136 mov bl, 0x23
137 mov ax, 0x0205
138 int 0x31
139
140 /* handle int 1bh the same way */
141 mov cx, protseg
142 mov dx, protofs
143 mov bl, 0x1b
144 mov ax, 0x0205
145 int 0x31
146 }
147
148 /*
149 * The Borland DPMI server seems to ignore a request to install a
150 * real-mode int 23h handler, even though it's documented as working
151 * correctly. To deal with this, install our int 23h handler the
152 * old-fashioned way, by poking it directly into the interrupt
153 * page.
154 */
155 asm
156 {
157 mov ax, 0x0002
158 mov bx, 0x0000
159 int 0x31
160 mov int_page_seg, ax
161 }
162 int_page_ptr = MK_FP(int_page_seg, 0);
163 *(long *)(int_page_ptr + (0x23 * 4)) =
164 (long)MK_FP(realseg, realofs + 2);
165 *(long *)(int_page_ptr + (0x1b * 4)) =
166 (long)MK_FP(realseg, realofs + 2);
167 }
168
169 /*
170 * Yield CPU to other processes, and check the ^C status. Checks the
171 * word preceding the real-mode interrupt 23h handler, since this word
172 * is set to a non-zero value when the interrupt 23h handler is invoked
173 * (which happens in response to a ^C). In any case, there's nothing we
174 * need to do to yield the CPU - this is merely a break check.
175 */
176 int os_yield(void)
177 {
178 return *brk_pointer;
179 }
180
181
182 #endif /* __DPMI16__ */
183
184
185 int main(argc, argv)
186 int argc;
187 char *argv[];
188 {
189 int tcdmain(int argc, char *argv[], char *);
190 char *config_name;
191
192 config_name = "config.tc";
193 #ifdef __DPMI16__
194 ltkini(16384);
195 init_dpmi16_break();
196 /* config_name = "config16.tc"; */
197 #else
198 #endif
199 return(os0main(argc, argv, tcdmain, "i", config_name));
200 }