fix potential bug when cluster allocation fails;
[unix-history] / usr / src / usr.bin / tn3270 / api / apilib.c
CommitLineData
28e559a0
GM
1#include "../api/api.h"
2
f81b28ad
GM
3#include "apilib.h"
4
5int
6 api_sup_errno = 0, /* Supervisor error number */
7 api_sup_fcn_id = 0, /* Supervisor function id (0x12) */
8 api_fcn_errno = 0, /* Function error number */
9 api_fcn_fcn_id = 0; /* Function ID (0x6b, etc.) */
10
11static int
12 gate_sessmgr = 0,
13 gate_keyboard = 0,
14 gate_copy = 0,
15 gate_oiam = 0;
16
17/*
18 * Issue an API request, with reg structures supplied by the caller.
19 *
20 * Only certain routines need this (supervisor services come to mind).
21 */
22
cee740d6
GM
23static int
24api_issue_regs(ah, al, bh, bl, cx, dx, parms, length, regs, sregs)
f81b28ad
GM
25int ah, al, bh, bl, cx, dx;
26char *parms;
cee740d6 27int length;
f81b28ad
GM
28union REGS *regs;
29struct SREGS *sregs;
30{
31 char far *ourseg = parms;
32
33 regs->h.ah = ah;
34 regs->h.al = al;
35 regs->h.bh = bh;
36 regs->h.bl = bl;
37 regs->x.cx = cx;
38 regs->x.dx = dx;
39 sregs->es = (int) FP_SEG(ourseg);
40 regs->x.di = (int) FP_OFF(ourseg);
41
cd746900 42#if defined(MSDOS)
f81b28ad 43 int86x(API_INTERRUPT_NUMBER, regs, regs, sregs);
cd746900
GM
44#endif /* defined(MSDOS) */
45#if defined(unix)
cee740d6 46 api_exch_api(regs, sregs, parms, length);
cd746900
GM
47#endif /* defined(unix) */
48
f81b28ad
GM
49 if (regs->h.cl != 0) {
50 api_sup_errno = regs->h.cl;
51 return -1;
52 } else {
53 return 0;
54 }
55}
56
57
58/*
59 * Issue an API request without requiring caller to supply
60 * registers. Most routines use this.
61 */
28e559a0 62
cee740d6
GM
63static int
64api_issue(ah, al, bh, bl, cx, dx, parms, length)
28e559a0
GM
65int
66 ah,
67 al,
68 bh,
69 bl,
70 cx,
71 dx;
72char *parms;
cee740d6 73int length; /* Length of parms */
28e559a0
GM
74{
75 union REGS regs;
76 struct SREGS sregs;
28e559a0 77
cee740d6 78 return api_issue_regs(ah, al, bh, bl, cx, dx, parms, length, &regs, &sregs);
28e559a0 79}
f81b28ad
GM
80\f
81/*
82 * Supervisor Services
83 */
28e559a0
GM
84
85int
86api_name_resolve(name)
87char *name;
88{
89 NameResolveParms parms;
90 int i;
91 union REGS regs;
92 struct SREGS sregs;
28e559a0
GM
93
94 for (i = 0; i < sizeof parms.gate_name; i++) {
95 if (*name) {
96 parms.gate_name[i] = *name++;
97 } else {
98 parms.gate_name[i] = ' ';
99 }
100 }
101
cee740d6 102 if (api_issue_regs(NAME_RESOLUTION, 0, 0, 0, 0, 0, &parms, sizeof parms, &regs, &sregs)
f81b28ad 103 == -1) {
28e559a0
GM
104 return -1;
105 } else {
106 return regs.x.dx;
107 }
108}
7e20fa70
GM
109
110#if defined(unix)
111/*
112 * Block until the oia or ps is modified.
113 */
114
115int
116api_ps_or_oia_modified()
117{
118 union REGS regs;
119 struct SREGS sregs;
120
121 if (api_issue_regs(PS_OR_OIA_MODIFIED, 0, 0, 0, 0, 0, 0, 0, &regs, &sregs)
122 == -1) {
123 return -1;
124 } else {
125 return 0;
126 }
127}
128#endif /* defined(unix) */
f81b28ad
GM
129\f
130/*
131 * Session Information Services
132 */
133
e99f17a9 134api_query_session_id(parms)
f81b28ad
GM
135QuerySessionIdParms *parms;
136{
137 if (api_issue(0x09, QUERY_SESSION_ID, 0x80, 0x20, 0,
cee740d6 138 gate_sessmgr, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
139 api_fcn_errno = 0;
140 api_fcn_fcn_id = 0;
141 return -1;
142 } else if (parms->rc == 0) {
143 return 0;
144 } else {
145 api_fcn_errno = parms->rc;
146 api_fcn_fcn_id = parms->function_id;
147 return -1;
148 }
149}
150
151
e99f17a9 152api_query_session_parameters(parms)
f81b28ad
GM
153QuerySessionParametersParms *parms;
154{
e99f17a9 155 if (api_issue(0x09, QUERY_SESSION_PARAMETERS, 0x80, 0x20, 0,
cee740d6 156 gate_sessmgr, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
157 api_fcn_errno = 0;
158 api_fcn_fcn_id = 0;
159 return -1;
160 } else if (parms->rc == 0) {
161 return 0;
162 } else {
163 api_fcn_errno = parms->rc;
164 api_fcn_fcn_id = parms->function_id;
165 return -1;
166 }
167}
168
169api_query_session_cursor(parms)
170QuerySessionCursorParms *parms;
171{
e99f17a9 172 if (api_issue(0x09, QUERY_SESSION_CURSOR, 0x80, 0x20, 0xff,
cee740d6 173 gate_sessmgr, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
174 api_fcn_errno = 0;
175 api_fcn_fcn_id = 0;
176 return -1;
177 } else if (parms->rc == 0) {
178 return 0;
179 } else {
180 api_fcn_errno = parms->rc;
181 api_fcn_fcn_id = parms->function_id;
182 return -1;
183 }
184}
185\f
186/*
187 * Keyboard Services
188 */
189
190api_connect_to_keyboard(parms)
191ConnectToKeyboardParms *parms;
192{
193 if (api_issue(0x09, CONNECT_TO_KEYBOARD, 0x80, 0x20, 0,
cee740d6 194 gate_keyboard, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
195 api_fcn_errno = 0;
196 api_fcn_fcn_id = 0;
197 return -1;
198 } else if (parms->rc == 0) {
199 return 0;
200 } else {
201 api_fcn_errno = parms->rc;
202 api_fcn_fcn_id = parms->function_id;
203 return -1;
204 }
205}
206
207
208api_disconnect_from_keyboard(parms)
209DisconnectFromKeyboardParms *parms;
210{
211 if (api_issue(0x09, DISCONNECT_FROM_KEYBOARD, 0x80, 0x20, 0,
cee740d6 212 gate_keyboard, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
213 api_fcn_errno = 0;
214 api_fcn_fcn_id = 0;
215 return -1;
216 } else if (parms->rc == 0) {
217 return 0;
218 } else {
219 api_fcn_errno = parms->rc;
220 api_fcn_fcn_id = parms->function_id;
221 return -1;
222 }
223}
224
225
226api_write_keystroke(parms)
227WriteKeystrokeParms *parms;
228{
229 if (api_issue(0x09, WRITE_KEYSTROKE, 0x80, 0x20, 0,
cee740d6 230 gate_keyboard, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
231 api_fcn_errno = 0;
232 api_fcn_fcn_id = 0;
233 return -1;
234 } else if (parms->rc == 0) {
235 return 0;
236 } else {
237 api_fcn_errno = parms->rc;
238 api_fcn_fcn_id = parms->function_id;
239 return -1;
240 }
241}
242
243
244api_disable_input(parms)
245DisableInputParms *parms;
246{
247 if (api_issue(0x09, DISABLE_INPUT, 0x80, 0x20, 0,
cee740d6 248 gate_keyboard, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
249 api_fcn_errno = 0;
250 api_fcn_fcn_id = 0;
251 return -1;
252 } else if (parms->rc == 0) {
253 return 0;
254 } else {
255 api_fcn_errno = parms->rc;
256 api_fcn_fcn_id = parms->function_id;
257 return -1;
258 }
259}
260
261api_enable_input(parms)
262EnableInputParms *parms;
263{
264 if (api_issue(0x09, ENABLE_INPUT, 0x80, 0x20, 0,
cee740d6 265 gate_keyboard, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
266 api_fcn_errno = 0;
267 api_fcn_fcn_id = 0;
268 return -1;
269 } else if (parms->rc == 0) {
270 return 0;
271 } else {
272 api_fcn_errno = parms->rc;
273 api_fcn_fcn_id = parms->function_id;
274 return -1;
275 }
276}
277\f
278/*
279 * Copy Services
280 */
281
282api_copy_string(parms)
283CopyStringParms *parms;
284{
e99f17a9 285 if (api_issue(0x09, COPY_STRING, 0x80, 0x20, 0xff,
cee740d6 286 gate_copy, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
287 api_fcn_errno = 0;
288 api_fcn_fcn_id = 0;
289 return -1;
290 } else if (parms->rc == 0) {
291 return 0;
292 } else {
293 api_fcn_errno = parms->rc;
294 api_fcn_fcn_id = parms->function_id;
295 return -1;
296 }
297}
298
299/*
300 * Operator Information Area Services
301 */
302
303api_read_oia_group(parms)
304ReadOiaGroupParms *parms;
305{
e99f17a9 306 if (api_issue(0x09, READ_OIA_GROUP, 0x80, 0x20, 0xff,
cee740d6 307 gate_oiam, (char *)parms, sizeof *parms) == -1) {
f81b28ad
GM
308 api_fcn_errno = 0;
309 api_fcn_fcn_id = 0;
310 return -1;
311 } else if (parms->rc == 0) {
312 return 0;
313 } else {
314 api_fcn_errno = parms->rc;
315 api_fcn_fcn_id = parms->function_id;
316 return -1;
317 }
318}
319\f
d024bcc4
GM
320/*
321 * The "we are done" routine. This gets called last.
322 */
323
324api_finish()
325{
326#if defined(unix)
327 if (api_close_api() == -1) {
328 return -1;
329 } else {
330 return 0;
331 }
332#endif /* defined(unix) */
333}
334
335
f81b28ad
GM
336/*
337 * The initialization routine. Be sure to call this first.
338 */
339
340api_init()
341{
342 union REGS regs;
343 struct SREGS sregs;
344
cd746900 345#if defined(MSDOS)
f81b28ad
GM
346 regs.h.ah = 0x35;
347 regs.h.al = API_INTERRUPT_NUMBER;
348 intdosx(&regs, &regs, &sregs);
349
350 if ((regs.x.bx == 0) && (sregs.es == 0)) {
351 return 0; /* Interrupt not being handled */
352 }
cd746900
GM
353#endif defined(MSDOS)
354#if defined(unix)
355 if (api_open_api(0) == -1) {
356 return 0;
357 }
358#endif /* defined(unix) */
f81b28ad
GM
359
360 gate_sessmgr = api_name_resolve("SESSMGR");
361 gate_keyboard = api_name_resolve("KEYBOARD");
362 gate_copy = api_name_resolve("COPY");
363 gate_oiam = api_name_resolve("OIAM");
364
365 if ((gate_sessmgr == gate_keyboard) ||
366 (gate_sessmgr == gate_copy) ||
367 (gate_sessmgr == gate_oiam) ||
368 (gate_keyboard == gate_copy) ||
369 (gate_keyboard == gate_oiam) ||
370 (gate_copy == gate_oiam)) {
371 return 0; /* Interrupt doesn't seem correct */
372 }
373 return 1;
374}