Debian Codegraph @

library.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992-1994 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*
21 * Sigh, let's live dangerously. We need to remove the definition of
22 * _POSIX_SOURCE, in order to get defined some extenstions to POSIX,
23 * since dynamic loading is not a part of POSIX.
24 */
25
26 #include "regina_c.h"
27 #include "rexxsaa.h"
28 #define DONT_TYPEDEF_PFN
29
30 #ifdef HAVE_GCI
31 # include "gci/gci.h"
32 #endif
33
34 #include "rexx.h"
35 #include "rxiface.h"
36 #include <assert.h>
37 #include <string.h>
38
39 /*
40 * Starting after 3.3RC1 we process both the Rexx???Exe as the Rexx???Dll
41 * stuff here and only here. An Exe element is an element with a library of
42 * NULL.
43 */
44
45 #define EP_COUNT 1361 /* should be a prime for distribution */
46
47 #define FUNCS 0
48 #define EXITS 1
49 #define SUBCOMS 2
50
51 typedef struct { /* lib_tsd: static variables of this module (thread-safe) */
52 struct library * first_library;
53 struct library * orphaned;
54 struct entry_point *ep[3][EP_COUNT]; /* FUNCS, EXITS, SUBCOMS */
55 streng * err_message;
56 } lib_tsd_t; /* thread-specific but only needed by this module. see
57 * init_library
58 */
59 /*
60 * External functions, exits etc should be global; fixes bug 3489415
61 * This is not implemented yet as doing this would require all access
62 * to ltGlobal to be protected so multiple threads can't be adding/deleting
63 * items fro the list of external functions/exits - TODO
64 */
65 #ifdef TODO_WHEN_TS
66 static lib_tsd_t *ltGlobal=NULL;
67 #endif
68 /*
69 * init_library initializes the module.
70 * Currently, we set up the thread specific data.
71 * The function returns 1 on success, 0 if memory is short.
72 */
73 int init_library( tsd_t *TSD )
74 {
75 #ifdef TODO_WHEN_TS
76 if ( TSD->lib_tsd != NULL )
77 return 1;
78
79 if ( ltGlobal == NULL )
80 {
81 if ( ( ltGlobal = MallocTSD( sizeof( lib_tsd_t ) ) ) == NULL )
82 return 0;
83 memset( ltGlobal, 0, sizeof( lib_tsd_t ) ); /* correct for all values */
84 }
85 TSD->lib_tsd = ltGlobal;
86 #else
87 lib_tsd_t *lt;
88
89 if ( TSD->lib_tsd != NULL )
90 return 1;
91
92 if ( ( TSD->lib_tsd = MallocTSD( sizeof( lib_tsd_t ) ) ) == NULL )
93 return 0;
94 lt = (lib_tsd_t *)TSD->lib_tsd;
95 memset( lt, 0, sizeof( lib_tsd_t ) ); /* correct for all values */
96 #endif
97 return 1;
98 }
99
100 #ifdef DYNAMIC
101 /*
102 * insert_library inserts the passed library in the linked list of used
103 * libraries unconditionally.
104 */
105 static void insert_library( const tsd_t *TSD, struct library *ptr )
106 {
107 lib_tsd_t *lt;
108
109 lt = (lib_tsd_t *)TSD->lib_tsd;
110 ptr->prev = NULL;
111 ptr->next = lt->first_library;
112 lt->first_library = ptr;
113 if ( ptr->next != NULL )
114 ptr->next->prev = ptr;
115 }
116
117 /*
118 * unlink_orphaned_libs attempts to remove the address space from unused
119 * DLLs aka shared libraries.
120 *
121 * We must be extremely careful. Scenario:
122 * Load a function package, e.g. w32funcs. This lets several functions
123 * (e.g. win32func???) and win32load and win32unload been registered.
124 * When the call of win32unload happens, each function name gets unload and
125 * on the last call the entire library gets released. But this happens just
126 * when RexxDeregisterFunction(win32unload) happens! The address space doesn't
127 * exist any longer and the call does a return to unmapped memory. Crash!
128 *
129 * Therefore the freeing of the library moves the unused lib to the list of
130 * orphaned libs which are removed when it's safe to do so.
131 *
132 * Alternatively we have to maintain a list of used external entry points
133 * which lock the associated libraries only (move to orphaned), others are
134 * freed immediately. The disadvantage is the maintainance problem with
135 * longjmp/sigjmp.
136 *
137 * If the flag force is set we assume a clean state which should be set only
138 * on reforking or terminating.
139 */
140 static void unlink_orphaned_libs( const tsd_t *TSD, lib_tsd_t *lt, int force )
141 {
142 struct library *ptr;
143
144 if ( !lt->orphaned )
145 return;
146
147 if ( !force )
148 {
149 /*
150 * The system is ready to remove every lib if TSD->systeminfo->previous
151 * is empty AND TSD->systeminfo->input_name is empty.
152 * Otherwise we don't catch the plain main() calls or other calls I can
153 * imagine that use the first systeminfo directly.
154 */
155 if ( TSD->systeminfo )
156 {
157 if ( TSD->systeminfo->previous || TSD->systeminfo->input_file )
158 return;
159 }
160 }
161
162 while ( ( ptr = lt->orphaned ) != NULL )
163 {
164 lt->orphaned = ptr->next;
165 if ( lt->orphaned )
166 lt->orphaned->prev = NULL;
167
168 assert( ptr->used == 0 );
169
170 wrapper_unload( TSD, ptr->handle );
171 assert( ptr->name );
172 Free_stringTSD( ptr->name );
173 FreeTSD( ptr );
174 }
175 }
176
177 /*
178 * remove_library removes the passed library from the linked list of used
179 * libraries unconditionally.
180 * The library will be unloaded and the name and the passed structure will be
181 * freed later when it's safe to do so.
182 * See unlink_orphaned_libs.
183 */
184 static void remove_library( const tsd_t *TSD, struct library *ptr )
185 {
186 lib_tsd_t *lt;
187
188 assert( ptr->used == 0 );
189
190 lt = (lib_tsd_t *)TSD->lib_tsd;
191 if ( ptr->next )
192 ptr->next->prev = ptr->prev;
193
194 if ( ptr->prev )
195 ptr->prev->next = ptr->next;
196 else
197 lt->first_library = ptr->next;
198
199 ptr->next = lt->orphaned;
200 if ( lt->orphaned )
201 lt->orphaned->prev = ptr;
202
203 lt->orphaned = ptr;
204
205 /*
206 * Now try to remove it really.
207 */
208 unlink_orphaned_libs( TSD, lt, 0 );
209 }
210 #endif
211
212 /*
213 * remove_entry removes the passed library entry from the linked list of used
214 * library entries unconditionally.
215 * The slot must be either FUNCS, EXITS, or SUBCOMS.
216 * Used memory will be freed and the holding library will be removed if this
217 * entry was the last entry used of the library.
218 */
219 static void remove_entry( tsd_t *TSD, struct entry_point *fptr, int slot )
220 {
221 lib_tsd_t *lt;
222
223 assert( slot >= FUNCS && slot <= SUBCOMS );
224
225 lt = (lib_tsd_t *)TSD->lib_tsd;
226 if ( fptr->name )
227 Free_stringTSD( fptr->name );
228 #if defined(HAVE_GCI) && defined(DYNAMIC)
229 if ( ( fptr->special.gci_info != NULL ) && ( slot == FUNCS ) )
230 GCI_remove_structure( TSD, (GCI_treeinfo *)fptr->special.gci_info );
231 #endif
232 if ( fptr->next )
233 fptr->next->prev = fptr->prev;
234 if ( fptr->prev )
235 fptr->prev->next = fptr->next;
236 else
237 lt->ep[slot][fptr->hash % EP_COUNT] = fptr->next;
238
239 #ifdef DYNAMIC
240 if ( fptr->lib != NULL )
241 {
242 assert( fptr->lib->used > 0 );
243 if ( --fptr->lib->used == 0 )
244 remove_library( TSD, fptr->lib );
245 }
246 #endif
247
248 FreeTSD( fptr );
249 }
250
251 /*
252 * free_orphaned_libs disconnect from unused DLLs, see unlink_orphaned_libs.
253 */
254 void free_orphaned_libs( tsd_t *TSD )
255 {
256 #ifdef DYNAMIC
257 lib_tsd_t *lt = (lib_tsd_t *)TSD->lib_tsd;
258
259 unlink_orphaned_libs( TSD, lt, 0 );
260 #endif
261 }
262
263 /*
264 * purge_library frees all used memory used by every entry point that is
265 * registered and unloads every library.
266 * This routine is a little bit slow.
267 */
268 void purge_library( tsd_t *TSD )
269 {
270 struct entry_point *ep, *save_ep;
271 lib_tsd_t *lt;
272 int i, j;
273
274 lt = (lib_tsd_t *)TSD->lib_tsd;
275 if ( lt->first_library != NULL )
276 {
277 for ( i = FUNCS; i <= SUBCOMS; i++ )
278 {
279 for ( j = 0; j < EP_COUNT; j++ )
280 {
281 if ( ( ep = lt->ep[i][j] ) != NULL )
282 {
283 do {
284 save_ep = ep;
285 remove_entry( TSD, ep, i );
286 if ( ( ep = lt->ep[i][j] ) == save_ep )
287 break;
288 } while ( ep != NULL );
289 if ( lt->first_library == NULL )
290 goto fastEnd;
291 }
292 }
293 }
294 }
295 fastEnd:
296 assert( lt->first_library == NULL );
297 lt->first_library = NULL;
298 #ifdef DYNAMIC
299 unlink_orphaned_libs( TSD, lt, 1 );
300 #endif
301 assert( lt->orphaned == NULL );
302 lt->orphaned = NULL;
303 memset( lt->ep, 0, sizeof( lt->ep ) );
304 }
305
306 #ifdef DYNAMIC
307 /*
308 * find_library returns the internal structure associated with the passed
309 * library name or NULL if such a library doesn't exist.
310 */
311 struct library *find_library( const tsd_t *TSD, const streng *name )
312 {
313 struct library *lptr;
314 lib_tsd_t *lt;
315
316 lt = (lib_tsd_t *)TSD->lib_tsd;
317 lptr = lt->first_library;
318 for ( ; lptr; lptr = lptr->next )
319 {
320 if ( !Str_cmp( name, lptr->name ) )
321 return lptr;
322 }
323
324 return NULL;
325 }
326 #endif
327
328 /*
329 * add_entry creates a new library entry from the passed data and inserts it
330 * in the linked list of used entries unconditionally.
331 * The slot must be either FUNCS, EXITS, or SUBCOMS.
332 * rxname is the name that can be used by a REXX script.
333 * addr is the entry point of the function/exit hook/subcom hook.
334 * lptr is a loaded library or NULL for a call of RexxRegister???Exe.
335 * Either gci_info or user_area may be set depending on the kind of the entry.
336 *
337 * The internal counter of the library isn't incremented.
338 */
339 static void add_entry( const tsd_t *TSD, int slot, const streng *rxname,
340 PFN addr,
341 struct library *lptr, void *gci_info,
342 void *user_area )
343 {
344 int hash0;
345 lib_tsd_t *lt;
346 struct entry_point *fptr;
347
348 assert( slot >= FUNCS && slot <= SUBCOMS );
349 lt = (lib_tsd_t *)TSD->lib_tsd;
350
351 fptr = (struct entry_point *)MallocTSD( sizeof( struct entry_point ) );
352 fptr->name = Str_upper( Str_dupstrTSD( rxname ) );
353 fptr->hash = hashvalue( rxname->value, rxname->len );
354 fptr->addr = addr;
355 fptr->lib = lptr;
356 memset( &fptr->special, 0, sizeof( fptr->special ) );
357 if ( slot == FUNCS )
358 fptr->special.gci_info = gci_info;
359 else
360 {
361 if ( user_area != NULL )
362 memcpy( fptr->special.user_area, user_area,
363 sizeof ( fptr->special.user_area ) );
364 }
365
366 hash0 = fptr->hash % EP_COUNT;
367 fptr->next = lt->ep[slot][hash0];
368 lt->ep[slot][hash0] = fptr;
369 fptr->prev = NULL;
370 if ( fptr->next )
371 fptr->next->prev = fptr;
372 }
373
374 /*
375 * find_entry_point returns NULL if no entry is found. Returns the exact entry
376 * if both the name and the library match. Returns any entry with a fitting
377 * name if the library doesn't match but the name exists.
378 * library may be NULL for entries registered by RexxRegister???Exe.
379 * The slot must be either FUNCS, EXITS, or SUBCOMS.
380 */
381 static struct entry_point *find_entry_point( const tsd_t *TSD,
382 const streng *name,
383 void *library,
384 int slot )
385 {
386 struct entry_point *lptr;
387 unsigned hash, hash0;
388 lib_tsd_t *lt;
389 struct entry_point *retval = NULL;
390
391 lt = (lib_tsd_t *)TSD->lib_tsd;
392 hash = hashvalue( name->value, name->len );
393 hash0 = hash % EP_COUNT;
394 for ( lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
395 {
396 if ( hash == lptr->hash )
397 if ( Str_cmp( name, lptr->name ) == 0 )
398 {
399 if ( lptr->lib == library )
400 return lptr;
401 else
402 retval = lptr;
403 }
404 }
405
406 return retval;
407 }
408
409 /*
410 * find_first_entry_point returns NULL if no entry is found and returns the
411 * most recent hook otherwise.
412 * The slot must be either FUNCS, EXITS, or SUBCOMS.
413 */
414 static struct entry_point *find_first_entry_point( const tsd_t *TSD,
415 const streng *name,
416 int slot )
417 {
418 struct entry_point *lptr;
419 unsigned hash, hash0;
420 lib_tsd_t *lt;
421
422 lt = (lib_tsd_t *)TSD->lib_tsd;
423 hash = hashvalue( name->value, name->len );
424 hash0 = hash % EP_COUNT;
425 for ( lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
426 {
427 if ( hash == lptr->hash )
428 if ( Str_cmp( name, lptr->name ) == 0 )
429 return lptr;
430 }
431
432 return NULL;
433 }
434
435 /*
436 * find_all_entries returns 0 if no entry is found. Otherwise it returns the
437 * number of all matching entries with the given name, different in the module
438 * name only.
439 * The slot must be either FUNCS, EXITS, or SUBCOMS.
440 * *list will be set to a list of all available entries.
441 *
442 * This function is slow.
443 */
444 static int find_all_entries( const tsd_t *TSD, const streng *name, int slot,
445 struct entry_point ***list )
446 {
447 struct entry_point *lptr, **array;
448 unsigned hash, hash0;
449 lib_tsd_t *lt;
450 int cnt;
451
452 lt = (lib_tsd_t *)TSD->lib_tsd;
453 hash = hashvalue( name->value, name->len );
454 hash0 = hash % EP_COUNT;
455 for ( cnt = 0, lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
456 {
457 if ( hash == lptr->hash )
458 if ( Str_cmp( name, lptr->name ) == 0 )
459 cnt++;
460 }
461
462 if ( cnt == 0 )
463 {
464 *list = NULL;
465 return 0;
466 }
467
468 array = (struct entry_point **)MallocTSD( cnt * sizeof( struct entry_point * ) );
469 *list = array;
470
471 for ( cnt = 0, lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
472 {
473 if ( hash == lptr->hash )
474 if ( Str_cmp( name, lptr->name ) == 0 )
475 array[cnt++] = lptr;
476 }
477
478 return cnt;
479 }
480
481 /*
482 * set_err_message replaces the current error message by a new one that will
483 * be assembled as the concatenation of the two passed messages.
484 * The created string will be returned by RxFuncErrMsg().
485 */
486 void set_err_message( const tsd_t *TSD, const char *message1,
487 const char *message2 )
488 {
489 lib_tsd_t *lt;
490 int size;
491
492 lt = (lib_tsd_t *)TSD->lib_tsd;
493 if ( lt->err_message )
494 Free_stringTSD( lt->err_message );
495
496 size = strlen( message1 ) + strlen( message2 );
497 lt->err_message = Str_makeTSD( size + 1 );
498 if ( lt->err_message )
499 {
500 strcpy( lt->err_message->value, message1 );
501 strcat( lt->err_message->value, message2 );
502 lt->err_message->len = size;
503 }
504 }
505
506 /*
507 * load_entry creates a new library entry from the passed data and inserts it
508 * in the linked list of used entries.
509 * lptr is a loaded library or NULL for a call of RexxRegister???Exe.
510 * rxname is the name that can be used by a REXX script.
511 * objnam will be used if lptr != NULL only and is the name of the hook
512 * or function that is exported by the library.
513 * entry will be used if lptr == NULL only and is the entry point of the hook
514 * or function.
515 * The slot must be either FUNCS, EXITS, or SUBCOMS.
516 * Either gci_info or user_area may be set depending on the kind of the entry.
517 *
518 * Return codes:
519 * 0 on success.
520 * 1 if the function is defined already.
521 * 1 if the hook is defined already and bound to the same library. The new
522 * hook is rejected.
523 * 2 if the hook is defined already and bound to another library. The new
524 * hook is accepted.
525 * 3 if objnam isn't exported by lptr.
526 * 4 if external libraries are not supported.
527 */
528 static int load_entry( const tsd_t *TSD, struct library *lptr,
529 const streng *rxname, const streng *objnam,
530 PFN entry,
531 int slot, void *gci_info, void *user_area )
532 {
533 int result=0;
534 struct entry_point *fptr;
535
536 assert( ( lptr != NULL ) ^ ( entry != NULL ) );
537 assert( rxname != NULL );
538 assert( slot >= FUNCS && slot <= SUBCOMS );
539 /*
540 * Check the exceptions first.
541 */
542 if ( ( fptr = find_entry_point( TSD, rxname, lptr, slot ) ) != NULL )
543 {
544 /*
545 * EXITS and SUBCOMS may have the same callable name bound to different
546 * modules.
547 */
548 if ( ( slot == FUNCS ) || ( fptr->lib == lptr ) )
549 return 1;
550 /*
551 * must be a hook with the same name in a different module.
552 */
553 result = 2;
554 }
555
556 if ( lptr )
557 {
558 assert( objnam != NULL );
559 #ifdef DYNAMIC
560 if ( ( entry = wrapper_get_addr( TSD, lptr, objnam ) ) == NULL )
561 return 3;
562 lptr->used++;
563 #else
564 return 4;
565 #endif
566 }
567
568 add_entry( TSD, slot, rxname, entry, lptr, gci_info, user_area );
569 return result;
570 }
571
572 /*
573 * unload_entry removes a known library entry from the linked list of known
574 * entries.
575 *
576 * rxname is the name that can be used by a REXX script.
577 * module is the name of the library and may be NULL for a generic request
578 * or is a RexxRegister???Exe registered funcion/hook shall be unloaded.
579 * The slot must be either FUNCS, EXITS, or SUBCOMS.
580 *
581 * Return codes:
582 * 0 on success.
583 * 1 if the function/hook is not defined or a hook with this name is bound
584 * to different modules and the module name is not given.
585 */
586 static int unload_entry( tsd_t *TSD, const streng *rxname,
587 const streng *module, int slot )
588 {
589 struct entry_point *fptr, **list;
590 struct library *lib;
591 int cnt;
592
593 #ifdef DYNAMIC
594 if ( module == NULL )
595 lib = NULL;
596 else
597 {
598 if ( ( lib = find_library( TSD, module ) ) == NULL )
599 return 1;
600 }
601 #else
602 if ( module != NULL )
603 return 1;
604 lib = NULL;
605 #endif
606
607 fptr = find_entry_point( TSD, rxname, lib, slot );
608 if ( fptr == NULL )
609 return 1;
610
611 if ( fptr->lib == lib )
612 {
613 remove_entry( TSD, fptr, slot );
614 return 0;
615 }
616
617 /*
618 * Not a properly matching function. Check for the "wildcard" library.
619 */
620 if ( lib != NULL )
621 return 1;
622
623 /*
624 * We need it the hard way. Check if more than one entry is registered.
625 */
626 cnt = find_all_entries( TSD, rxname, slot, &list );
627 if ( cnt > 1 )
628 {
629 FreeTSD( list );
630 return 1;
631 }
632
633 remove_entry( TSD, *list, slot );
634 FreeTSD( list );
635 return 0;
636 }
637
638 /*
639 * loadrxfunc adds a new function to the set of registered entry points.
640 *
641 * lptr is a loaded library or NULL for a call of RexxRegisterFunctionExe.
642 * rxname is the name that can be used by a REXX script.
643 * objnam will be used if lptr != NULL only and is the name of the function
644 * that is exported by the library.
645 * entry will be used if lptr == NULL only and is the entry point of the
646 * function.
647 * gci_info may be set depending on whether RxFuncDefine is used.
648 *
649 * Returns a return code suitable for RexxRegisterFunction???.
650 */
651 static int loadrxfunc( const tsd_t *TSD, struct library *lptr,
652 const streng *rxname, const streng *objnam,
653 PFN entry,
654 void *gci_info )
655 {
656 int rc;
657
658 rc = load_entry( TSD, lptr, rxname, objnam, entry, FUNCS, gci_info, NULL );
659 switch ( rc )
660 {
661 case 0: return 0; /* RXFUNC_OK */
662 case 1: return 10; /* RXFUNC_DEFINED */
663 case 3: return 50; /* RXFUNC_ENTNOTFND */
664 case 4: return 60; /* RXFUNC_NOTINIT */
665 }
666 assert ( rc != 0 );
667 return 10000 + rc; /* something not recognisable */
668 }
669
670 /*
671 * loadrxhook adds a new exit/subcom hook to the set of registered entry
672 * points.
673 *
674 * lptr is a loaded library or NULL for a call of RexxRegister???Exe.
675 * rxname is the name that can be used in a hook list.
676 * objnam will be used if lptr != NULL only and is the name of the hook that
677 * that is exported by the library.
678 * entry will be used if lptr == NULL only and is the entry point of the hook.
679 * user_area is the passed parameter called UserArea of the Registration.
680 * The slot must be either EXITS or SUBCOMS.
681 *
682 * Returns a return code suitable for RexxRegister???.
683 */
684 static int loadrxhook( const tsd_t *TSD, struct library *lptr,
685 const streng *rxname, const streng *objnam,
686 PFN entry,
687 void *user_area, int slot )
688 {
689 int rc;
690
691 rc = load_entry( TSD, lptr, rxname, objnam, entry, slot, NULL, user_area );
692 switch ( rc )
693 {
694 case 0: return 0; /* RX???_OK */
695 case 1: return 30; /* RX???_NOTREG */
696 case 2: return 10; /* RX???_DUP */
697 case 3: return 50; /* RX???_LOADERR */
698 case 4: return 1004; /* RX???_NOTINIT */
699 }
700 assert ( rc != 0 );
701 return 10000 + rc; /* something not recognisable */
702 }
703
704 /*
705 * unloadrxfunc removes a registered function entry point.
706 *
707 * rxname is the name that can be used by a REXX script.
708 *
709 * Returns a return code suitable for RexxDeregisterFunction.
710 */
711 static int unloadrxfunc( tsd_t *TSD, const streng *rxname )
712 {
713 assert( rxname != NULL );
714
715 if ( unload_entry( TSD, rxname, NULL, FUNCS ) == 0 )
716 return 0;
717 return 30; /* RXFUNC_NOTREG */
718 }
719
720 /*
721 * unloadrxhook removes a registered exit/subcom hook entry point.
722 *
723 * rxname is the name that can be used in a hook list.
724 * module is the name of the module that contains the hook or NULL if either
725 * the generic hook should be removed or if a RexxRegister???Exe-hook should
726 * be removed. The later one has precedence.
727 * The slot must be either EXITS or SUBCOMS.
728 *
729 * Returns a return code suitable for RexxDeregister???.
730 */
731 static int unloadrxhook( tsd_t *TSD, const streng *rxname,
732 const streng *module, int slot )
733 {
734 assert( rxname != NULL );
735
736 if ( unload_entry( TSD, rxname, module, slot ) == 0 )
737 return 0;
738 return 30; /* RX???_NOTREG */
739 }
740
741 /*
742 * rex_funcadd processes a RexxRegisterFunctionDll() or
743 * RexxRegisterFunctionExe() request.
744 *
745 * rxname is the name that can be used by a REXX script.
746 * module is the name of the library and may be NULL only a
747 * RexxRegisterFunctionExe is processed.
748 * objnam will be used if module != NULL only and is the name of the function
749 * that is exported by the library.
750 * entry will be used if module == NULL only and is the entry point of the
751 * function.
752 * gci_info may be set depending on whether RxFuncDefine is used.
753 *
754 * Returns a return code suitable for RexxRegisterFunction???.
755 */
756 static int rex_funcadd( const tsd_t *TSD, const streng *rxname,
757 const streng *module, const streng *objnam,
758 PFN entry,
759 void *gci_info )
760 {
761 struct library *lptr=NULL;
762 int rc;
763 #ifdef DYNAMIC
764 void *handle;
765 int newhandle = 0;
766 #endif
767 streng *regutil=Str_crestr( "regutil" );
768 streng *rexxutil=Str_crestr( "rexxutil" );
769
770 assert( rxname != NULL );
771
772 if ( module != NULL )
773 {
774 assert( entry == NULL );
775 #ifdef DYNAMIC
776 if ( Str_ccmp( module, rexxutil ) == 0 )
777 {
778 if ( ( lptr = find_library( TSD, regutil ) ) == NULL )
779 {
780 newhandle = 1;
781 handle = wrapper_load( TSD, regutil ) ;
782 if ( handle )
783 {
784 lptr = (struct library *)MallocTSD( sizeof( struct library )) ;
785 lptr->name = Str_dupstrTSD( regutil ) ;
786 lptr->handle = handle ;
787 lptr->used = 0l;
788 }
789 else
790 {
791 Free_stringTSD( regutil );
792 Free_stringTSD( rexxutil );
793 return 40; /* RXFUNC_MODNOTFND */
794 }
795 insert_library( TSD, lptr ) ;
796 }
797 }
798 if ( lptr == NULL )
799 {
800 if ( ( lptr = find_library( TSD, module ) ) == NULL )
801 {
802 newhandle = 1;
803 handle = wrapper_load( TSD, module ) ;
804 if ( handle )
805 {
806 lptr = (struct library *)MallocTSD( sizeof( struct library )) ;
807 lptr->name = Str_dupstrTSD( module ) ;
808 lptr->handle = handle ;
809 lptr->used = 0l;
810 }
811 else
812 {
813 Free_stringTSD( regutil );
814 Free_stringTSD( rexxutil );
815 return 40; /* RXFUNC_MODNOTFND */
816 }
817 insert_library( TSD, lptr ) ;
818 }
819 }
820 #else
821 Free_stringTSD( regutil );
822 Free_stringTSD( rexxutil );
823 return 60; /* RXFUNC_NOTINIT */
824 #endif
825 }
826 else
827 {
828 assert( entry != NULL );
829 }
830 if ( ( rc = loadrxfunc( TSD, lptr, rxname, objnam, entry, gci_info ) ) != 0 )
831 {
832 #ifdef DYNAMIC
833 if ( newhandle )
834 remove_library( TSD, lptr );
835 #endif
836 }
837 Free_stringTSD( regutil );
838 Free_stringTSD( rexxutil );
839 return rc;
840 }
841
842 /*
843 * rex_hookadd processes a RexxRegisterExitDll(), RexxRegisterExitExe(),
844 * RexxRegisterSubcomDll(), or RexxRegisterSubcomExe() request.
845 *
846 * rxname is the name that can be used in a hook list.
847 * module is the name of the library and may be NULL only a RexxRegister???Exe
848 * is processed.
849 * objnam will be used if module != NULL only and is the name of the hook
850 * that is exported by the library.
851 * entry will be used if module == NULL only and is the entry point of the
852 * hook.
853 * user_area is the passed parameter called UserArea of the Registration.
854 * The slot must be either EXITS or SUBCOMS.
855 *
856 * Returns a return code suitable for RexxRegister???.
857 */
858 static int rex_hookadd( const tsd_t *TSD, const streng *rxname,
859 const streng *module, const streng *objnam,
860 PFN entry,
861 void *user_area, int slot )
862 {
863 struct library *lptr=NULL;
864 int rc;
865 #ifdef DYNAMIC
866 void *handle;
867 int newhandle = 0;
868 #endif
869
870 assert( rxname != NULL );
871
872 if ( module != NULL )
873 {
874 assert( entry == NULL );
875 #ifdef DYNAMIC
876 if ( ( lptr = find_library( TSD, module ) ) == NULL )
877 {
878 newhandle = 1;
879 handle = wrapper_load( TSD, module ) ;
880 if ( handle )
881 {
882 lptr = (struct library *)MallocTSD( sizeof( struct library )) ;
883 lptr->name = Str_dupstrTSD( module ) ;
884 lptr->handle = handle ;
885 lptr->used = 0l;
886 }
887 else
888 {
889 return 50; /* RX???_LOADERR */
890 }
891 insert_library( TSD, lptr ) ;
892 }
893 #else
894 return 1004; /* RX???_NOTINIT */
895 #endif
896 }
897 else
898 {
899 assert( entry != NULL );
900 }
901 rc = loadrxhook( TSD, lptr, rxname, objnam, entry, user_area, slot );
902 if ( ( rc != 0 ) && ( rc != 10 ) )
903 {
904 #ifdef DYNAMIC
905 if ( newhandle )
906 remove_library( TSD, lptr );
907 #endif
908 }
909 return rc;
910 }
911
912 /*
913 * rex_rxfuncerrmsg implements the BIF RxFuncErrMsg.
914 */
915 streng *rex_rxfuncerrmsg( tsd_t *TSD, cparamboxptr parms )
916 {
917 #ifdef DYNAMIC
918 lib_tsd_t *lt;
919 #endif
920
921 checkparam( parms, 0, 0, "RXFUNCERRMSG" );
922
923 #ifdef DYNAMIC
924 lt = (lib_tsd_t *)TSD->lib_tsd;
925 if ( lt->err_message )
926 return Str_dupTSD( lt->err_message );
927 else
928 return nullstringptr();
929 #else
930 return Str_creTSD( "Module doesn't support dynamic linking; are you running the \"regina\" executable?" );
931 #endif
932 }
933
934 /*
935 * rex_rxfuncquery implements the BIF RxFuncQuery.
936 */
937 streng *rex_rxfuncquery( tsd_t *TSD, cparamboxptr parms )
938 {
939 #ifdef DYNAMIC
940 streng *name;
941 struct entry_point *fptr;
942 #endif
943
944 checkparam( parms, 1, 1, "RXFUNCQUERY" );
945
946 #ifdef DYNAMIC
947 name = Str_upper( Str_dupTSD( parms->value ) );
948 fptr = find_entry_point( TSD, name, NULL, FUNCS );
949 Free_stringTSD( name );
950
951 if ( fptr )
952 return int_to_streng( TSD, 0 );
953 return int_to_streng( TSD, 1 );
954 #else
955 return int_to_streng( TSD, 1 );
956 #endif
957 }
958
959
960 /*
961 * rex_rxfuncadd implements the BIF RxFuncAdd.
962 * The returned value is suitable for RexxRegisterFunctionDll.
963 *
964 * Parameters:
965 * 1) name of the function to be added (in Rexx)
966 * 2) name of object file to link in
967 * 3) name of the function to be added (in the object file)
968 */
969 streng *rex_rxfuncadd( tsd_t *TSD, cparamboxptr parms )
970 {
971 #ifdef DYNAMIC
972 streng *rxname;
973 streng *module, *objnam;
974 int rc;
975 #endif
976
977 if ( TSD->restricted )
978 exiterror( ERR_RESTRICTED, 1, "RXFUNCADD" );
979
980 checkparam( parms, 2, 3, "RXFUNCADD" );
981
982 #ifdef DYNAMIC
983 rxname = Str_upper( Str_dupTSD( parms->value ) );
984 objnam = parms->value;
985 module = ( parms = parms->next )->value;
986 if ( ( parms->next != NULL ) && ( parms->next->value != NULL ) )
987 objnam = parms->next->value;
988
989 rc = rex_funcadd( TSD, rxname, module, objnam, NULL, NULL );
990 Free_stringTSD( rxname );
991 return int_to_streng( TSD, rc );
992 #else
993 return int_to_streng( TSD, 60 ); /* RXFUNC_NOTINIT */
994 #endif
995 }
996
997 #ifdef HAVE_GCI
998 /*
999 * rex_rxfuncdefine implements the BIF RxFuncDefine.
1000 *
1001 * parameters:
1002 * 1) name of the function to be added (in Rexx)
1003 * 2) name of object file to link in
1004 * 3) name of the function to be added (in the object file)
1005 * 4) name of a stem containing the definition of the function
1006 */
1007 streng *rex_rxfuncdefine( tsd_t *TSD, cparamboxptr parms )
1008 {
1009 #ifdef DYNAMIC
1010 streng *rxname,*module,*objnam,*def_stem;
1011 void *gci_info;
1012 int rc;
1013 #endif
1014
1015 if ( TSD->restricted )
1016 exiterror( ERR_RESTRICTED, 1, "RXFUNCDEFINE" );
1017
1018 checkparam( parms, 4, 4, "RXFUNCDEFINE" );
1019
1020 #ifdef DYNAMIC
1021 rxname = Str_upper( Str_dupTSD( parms->value ) );
1022 objnam = parms->value;
1023 module = ( parms = parms->next )->value;
1024 parms = parms->next;
1025 if ( parms->value != NULL )
1026 objnam = parms->value;
1027 def_stem = parms->next->value;
1028
1029 if ( ( rc = GCI_checkDefinition( TSD, def_stem, &gci_info ) ) != 0 )
1030 {
1031 Free_stringTSD( rxname );
1032 return int_to_streng( TSD, 1 );
1033 }
1034
1035 rc = rex_funcadd( TSD, rxname, module, objnam, NULL, gci_info );
1036 Free_stringTSD( rxname );
1037 if ( rc )
1038 GCI_remove_structure( TSD, (GCI_treeinfo *)gci_info );
1039 return int_to_streng( TSD, rc );
1040 #else
1041 return int_to_streng( TSD, 60 ); /* RXFUNC_NOTINIT */
1042 #endif
1043 }
1044
1045 /*
1046 * rex_gciprefixchar implements the BIF GciPrefixChar.
1047 *
1048 * parameters:
1049 * 1) new prefix character
1050 */
1051 streng *rex_gciprefixchar( tsd_t *TSD, cparamboxptr parms )
1052 {
1053 static const char valid[] = " !?_#$@"; /* last 3 are Regina specific */
1054 char oldval[2], newval[2];
1055 streng *value;
1056
1057 checkparam( parms, 0, 1, "GCIPREFIXCHAR" );
1058
1059 oldval[0] = TSD->gci_prefix[0];
1060 oldval[1] = TSD->gci_prefix[1];
1061 newval[1] = '\0';
1062
1063 value = parms->value;
1064 if ( value )
1065 {
1066 if ( Str_len( value ) == 0 )
1067 {
1068 newval[0] = '\0';
1069 }
1070 else if ( Str_len( value ) > 1 )
1071 {
1072 exiterror( ERR_INCORRECT_CALL, 23, "GCIPREFIXCHAR", 1, tmpstr_of( TSD, value ) );
1073 }
1074 else
1075 {
1076 newval[0] = Str_val( value )[0];
1077 if ( newval[0] )
1078 {
1079 if ( strchr( valid, (int) newval[0] ) == NULL )
1080 {
1081 exiterror( ERR_INCORRECT_CALL, 28, "GCIPREFIXCHAR", 1, valid, newval );
1082 }
1083 }
1084 if ( newval[0] == ' ' )
1085 {
1086 newval[0] = '\0';
1087 }
1088 }
1089 }
1090 else
1091 {
1092 newval[0] = oldval[0];
1093 }
1094
1095 TSD->gci_prefix[0] = newval[0];
1096 TSD->gci_prefix[1] = newval[1];
1097
1098 return Str_creTSD( oldval );
1099 }
1100 #endif
1101
1102 /*
1103 * rex_rxfuncdrop implements the BIF RxFuncDrop.
1104 * The returned value is suitable for RexxDeregisterFunction.
1105 */
1106 streng *rex_rxfuncdrop( tsd_t *TSD, cparamboxptr parms )
1107 {
1108 streng *name;
1109
1110 checkparam( parms, 1, 1, "RXFUNCDROP" );
1111 name = Str_upper( parms->value );
1112
1113 return int_to_streng( TSD, unloadrxfunc( TSD, name ) );
1114 }
1115
1116 /*
1117 * IfcRegFunc is the interface function for RexxRegisterFunctionExe and
1118 * RexxRegisterFunctionDll.
1119 * Either entry or module and objnam must be set.
1120 */
1121 int IfcRegFunc( const tsd_t *TSD, const char *rxname, const char *module,
1122 const char *objnam,
1123 PFN entry )
1124 {
1125 int rc;
1126 streng *ext;
1127 streng *intr=NULL;
1128 streng *lib=NULL;
1129
1130 ext = Str_upper( Str_creTSD( rxname ) );
1131 if ( module && objnam )
1132 {
1133 intr = Str_creTSD( objnam );
1134 lib = Str_creTSD( module );
1135 }
1136
1137 rc = rex_funcadd( TSD, ext, lib, intr, entry, NULL );
1138
1139 Free_stringTSD( ext );
1140 if ( intr && lib )
1141 {
1142 Free_stringTSD( intr );
1143 Free_stringTSD( lib );
1144 }
1145
1146 return rc;
1147 }
1148
1149 /*
1150 * IfcRegHook is the interface function for RexxRegisterExitExe,
1151 * RexxRegisterExitDll, RexxRegisterSubcomExe, RexxRegisterSubcomtDll.
1152 * Either entry or module and objnam must be set.
1153 */
1154 static int IfcRegHook( const tsd_t *TSD, const char *rxname,
1155 const char *module, const char *objnam,
1156 PFN entry,
1157 void *user_area, int slot )
1158 {
1159 int rc;
1160 streng *ext;
1161 streng *intr=NULL;
1162 streng *lib=NULL;
1163
1164 ext = Str_upper( Str_creTSD( rxname ) );
1165 if ( module && objnam )
1166 {
1167 intr = Str_creTSD( objnam );
1168 lib = Str_creTSD( module );
1169 }
1170
1171 rc = rex_hookadd( TSD, ext, lib, intr, entry, user_area, slot );
1172
1173 Free_stringTSD( ext );
1174 if ( intr && lib )
1175 {
1176 Free_stringTSD( intr );
1177 Free_stringTSD( lib );
1178 }
1179
1180 return rc;
1181 }
1182
1183 /*
1184 * IfcRegExit is the interface function for RexxRegisterExitExe or
1185 * RexxRegisterExitDll.
1186 * Either entry or module and objnam must be set.
1187 */
1188 int IfcRegExit( const tsd_t *TSD, const char *rxname, const char *module,
1189 const char *objnam,
1190 PFN entry,
1191 void *user_area )
1192 {
1193 return IfcRegHook( TSD, rxname, module, objnam, entry, user_area, EXITS );
1194 }
1195
1196 /*
1197 * IfcRegSubcom is the interface function for RexxRegisterSubcomExe or
1198 * RexxRegisterSubcomDll.
1199 * Either entry or module and objnam must be set.
1200 */
1201 int IfcRegSubcom( const tsd_t *TSD, const char *rxname, const char *module,
1202 const char *objnam,
1203 PFN entry,
1204 void *user_area )
1205 {
1206 streng *env;
1207 env = Str_creTSD( rxname );
1208 set_subcomed_envir( TSD, env, 1 );
1209 Free_stringTSD( env );
1210 return IfcRegHook( TSD, rxname, module, objnam, entry, user_area, SUBCOMS );
1211 }
1212
1213 /*
1214 * IfcDelFunc is the interface function for RexxDeregisterFunction.
1215 */
1216 int IfcDelFunc( tsd_t *TSD, const char *rxname )
1217 {
1218 int rc;
1219 streng *ext;
1220
1221 ext = Str_upper( Str_creTSD( rxname ) );
1222 rc = unloadrxfunc( TSD, ext );
1223 Free_stringTSD( ext );
1224
1225 return rc;
1226 }
1227
1228 /*
1229 * IfcDelHook is the interface function for RexxDeregisterExit or
1230 * RexxDeregisterSubcom.
1231 */
1232 static int IfcDelHook( tsd_t *TSD, const char *rxname, const char *module,
1233 int slot )
1234 {
1235 int rc;
1236 streng *ext,*mod;
1237
1238 ext = Str_upper( Str_creTSD( rxname ) );
1239 if ( module != NULL )
1240 mod = Str_creTSD( module );
1241 else
1242 mod = NULL;
1243 rc = unloadrxhook( TSD, ext, mod, slot );
1244 Free_stringTSD( ext );
1245 if ( mod != NULL )
1246 Free_stringTSD( mod );
1247
1248 return rc;
1249 }
1250
1251 /*
1252 * IfcDelExit is the interface function for RexxDeregisterExit.
1253 */
1254 int IfcDelExit( tsd_t *TSD, const char *rxname, const char *module )
1255 {
1256 return IfcDelHook( TSD, rxname, module, EXITS );
1257 }
1258
1259 /*
1260 * IfcDelSubcom is the interface function for RexxDeregisterSubcom.
1261 */
1262 int IfcDelSubcom( tsd_t *TSD, const char *rxname, const char *module )
1263 {
1264 streng *env;
1265 env = Str_creTSD( rxname );
1266 set_subcomed_envir( TSD, env, 0 );
1267 Free_stringTSD( env );
1268 return IfcDelHook( TSD, rxname, module, SUBCOMS );
1269 }
1270
1271 /*
1272 * IfcQueryFunc is the interface function for RexxQueryFunction.
1273 */
1274 int IfcQueryFunc( const tsd_t *TSD, const char *rxname )
1275 {
1276 int rc;
1277 streng *ext;
1278
1279 ext = Str_upper( Str_creTSD( rxname ) );
1280 rc = ( find_entry_point( TSD, ext, NULL, FUNCS ) != NULL ) ? 0 : 30;
1281 Free_stringTSD( ext );
1282
1283 return rc;
1284 }
1285
1286 /*
1287 * IfcQueryHook is the interface function for RexxQueryExit or RexxQuerySubcom.
1288 */
1289 static int IfcQueryHook( const tsd_t *TSD, const char *rxname,
1290 const char *module, int slot, void *user_area )
1291 {
1292 streng *ext;
1293 struct entry_point *fptr,**list;
1294 struct library *lib;
1295 int cnt;
1296 #ifdef DYNAMIC
1297 streng *mod;
1298 #endif
1299
1300 ext = Str_upper( Str_creTSD( rxname ) );
1301 if ( module != NULL )
1302 {
1303 #ifdef DYNAMIC
1304 mod = Str_creTSD( module );
1305 lib = find_library( TSD, mod );
1306 Free_stringTSD( mod );
1307 if ( lib == NULL )
1308 {
1309 Free_stringTSD( ext );
1310 return 30; /* RX???_NOTREG */
1311 }
1312 #else
1313 return 1004; /* RX???_NOTINIT */
1314 #endif
1315 }
1316 else
1317 lib = NULL;
1318
1319 fptr = find_entry_point( TSD, ext, lib, slot );
1320
1321 if ( fptr == NULL )
1322 {
1323 Free_stringTSD( ext );
1324 return 30; /* RX???_NOTREG */
1325 }
1326
1327 if ( fptr->lib != lib )
1328 {
1329 /*
1330 * Found via wildcard mechanism, check if more than one element exists
1331 * and if a wildcard is allowed.
1332 */
1333 if ( lib != NULL )
1334 {
1335 Free_stringTSD( ext );
1336 return 30; /* RX???_NOTREG */
1337 }
1338
1339 cnt = find_all_entries( TSD, ext, slot, &list );
1340 FreeTSD( list );
1341 Free_stringTSD( ext );
1342
1343 if ( cnt > 1 )
1344 return 30; /* RX???_NOTREG */
1345 }
1346 else
1347 Free_stringTSD( ext );
1348
1349 if ( user_area != NULL )
1350 memcpy( user_area, fptr->special.user_area,
1351 sizeof ( fptr->special.user_area ) );
1352 return 0;
1353 }
1354
1355 /*
1356 * IfcQueryExit is the interface function for RexxQueryExit.
1357 */
1358 int IfcQueryExit( const tsd_t *TSD, const char *rxname, const char *module,
1359 void *user_area )
1360 {
1361 return IfcQueryHook( TSD, rxname, module, EXITS, user_area );
1362 }
1363
1364 /*
1365 * IfcQuerySubcom is the interface function for RexxQuerySubcom.
1366 */
1367 int IfcQuerySubcom( const tsd_t *TSD, const char *rxname, const char *module,
1368 void *user_area )
1369 {
1370 return IfcQueryHook( TSD, rxname, module, SUBCOMS, user_area );
1371 }
1372
1373 struct entry_point *loaded_lib_func( const tsd_t *TSD, const streng *name )
1374 {
1375 struct entry_point *box;
1376 streng *upp;
1377
1378 upp = Str_upper( Str_dupTSD( name ) );
1379 box = find_first_entry_point( TSD, upp, FUNCS );
1380 Free_stringTSD( upp );
1381
1382 return box;
1383 }
1384
1385 /*
1386 * exit_hook returns the most recent exit handler of the given name.
1387 * The value may be NULL if no hook is registered.
1388 */
1389 struct entry_point *exit_hook( const tsd_t *TSD, const char *env, int len )
1390 {
1391 streng *name;
1392 struct entry_point *ret;
1393
1394 name = Str_upper( Str_ncreTSD( env, len ) );
1395 ret = find_first_entry_point( TSD, name, EXITS );
1396 Free_stringTSD( name );
1397
1398 return ret;
1399 }
1400
1401 /*
1402 * subcom_hook returns the most recent subcom handler of the given name.
1403 * The value may be NULL if no hook is registered.
1404 */
1405 struct entry_point *subcom_hook( const tsd_t *TSD, const char *com, int len )
1406 {
1407 streng *name;
1408 struct entry_point *ret;
1409
1410 name = Str_upper( Str_ncreTSD( com, len ) );
1411 ret = find_first_entry_point( TSD, name, SUBCOMS );
1412 Free_stringTSD( name );
1413
1414 return ret;
1415 }