File: library.c

package info (click to toggle)
regina 3.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,952 kB
  • ctags: 7,235
  • sloc: ansic: 50,555; sh: 2,727; lex: 2,298; yacc: 1,498; makefile: 1,017; cpp: 117
file content (1297 lines) | stat: -rw-r--r-- 35,100 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
#ifndef lint
static char *RCSid = "$Id: library.c,v 1.19 2004/04/25 01:34:56 mark Exp $";
#endif

/*
 *  The Regina Rexx Interpreter
 *  Copyright (C) 1992-1994  Anders Christensen <anders@pvv.unit.no>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Sigh, let's live dangerously. We need to remove the definition of
 * _POSIX_SOURCE, in order to get defined some extenstions to POSIX,
 * since dynamic loading is not a part of POSIX.
 */

#include "regina_c.h"
#include "rexxsaa.h"
#define DONT_TYPEDEF_PFN

#ifdef HAVE_GCI
# include "gci/gci.h"
#endif

#include "rexx.h"
#include "rxiface.h"
#include <assert.h>
#include <string.h>

/*
 * Starting after 3.3RC1 we process both the Rexx???Exe as the Rexx???Dll
 * stuff here and only here. An Exe element is an element with a library of
 * NULL.
 */

#define EP_COUNT 133    /* should be a prime for distribution */

#define FUNCS   0
#define EXITS   1
#define SUBCOMS 2

typedef struct { /* lib_tsd: static variables of this module (thread-safe) */
   struct library *     first_library;
   struct library *     orphaned;
   struct entry_point  *ep[3][EP_COUNT]; /* FUNCS, EXITS, SUBCOMS */
   streng *             err_message;
} lib_tsd_t; /* thread-specific but only needed by this module. see
              * init_library
              */

/*
 * init_library initializes the module.
 * Currently, we set up the thread specific data.
 * The function returns 1 on success, 0 if memory is short.
 */
int init_library( tsd_t *TSD )
{
   lib_tsd_t *lt;

   if ( TSD->lib_tsd != NULL )
      return 1;

   if ( ( lt = TSD->lib_tsd = MallocTSD( sizeof( lib_tsd_t ) ) ) == NULL )
      return 0;
   memset( lt, 0, sizeof( lib_tsd_t ) );  /* correct for all values */
   return 1;
}

#ifdef DYNAMIC
/*
 * insert_library inserts the passed library in the linked list of used
 * libraries unconditionally.
 */
static void insert_library( const tsd_t *TSD, struct library *ptr )
{
   lib_tsd_t *lt;

   lt = TSD->lib_tsd;
   ptr->prev = NULL;
   ptr->next = lt->first_library;
   lt->first_library = ptr;
   if ( ptr->next != NULL )
      ptr->next->prev = ptr;
}

/*
 * unlink_orphaned_libs attempts to remove the address space from unused
 * DLLs aka shared libraries.
 *
 * We must be extremely careful. Scenario:
 * Load a function package, e.g. w32funcs. This lets several functions
 * (e.g. win32func???) and win32load and win32unload been registered.
 * When the call of win32unload happens, each function name gets unload and
 * on the last call the entire library gets released. But this happens just
 * when RexxDeregisterFunction(win32unload) happens! The address space doesn't
 * exist any longer and the call does a return to unmapped memory. Crash!
 *
 * Therefore the freeing of the library moves the unused lib to the list of
 * orphaned libs which are removed when it's safe to do so.
 *
 * Alternatively we have to maintain a list of used external entry points
 * which lock the associated libraries only (move to orphaned), others are
 * freed immediately. The disadvantage is the maintainance problem with
 * longjmp/sigjmp.
 *
 * If the flag force is set we assume a clean state which should be set only
 * on reforking or terminating.
 */
static void unlink_orphaned_libs( const tsd_t *TSD, lib_tsd_t *lt, int force )
{
   struct library *ptr;

   if ( !lt->orphaned )
      return;

   if ( !force )
   {
      /*
       * The system is ready to remove every lib if TSD->systeminfo->previous
       * is empty AND TSD->systeminfo->input_name is empty.
       * Otherwise we don't catch the plain main() calls or other calls I can
       * imagine that use the first systeminfo directly.
       */
      if ( TSD->systeminfo )
      {
         if ( TSD->systeminfo->previous || TSD->systeminfo->input_file )
            return;
      }
   }

   while ( ( ptr = lt->orphaned ) != NULL )
   {
      lt->orphaned = ptr->next;
      if ( lt->orphaned )
         lt->orphaned->prev = NULL;

      assert( ptr->used == 0 );

      wrapper_unload( TSD, ptr->handle );
      assert( ptr->name );
      Free_stringTSD( ptr->name );
      FreeTSD( ptr );
   }
}

/*
 * remove_library removes the passed library from the linked list of used
 * libraries unconditionally.
 * The library will be unloaded and the name and the passed structure will be
 * freed later when it's safe to do so.
 * See unlink_orphaned_libs.
 */
static void remove_library( const tsd_t *TSD, struct library *ptr )
{
   lib_tsd_t *lt;

   assert( ptr->used == 0 );

   lt = TSD->lib_tsd;
   if ( ptr->next )
      ptr->next->prev = ptr->prev;

   if ( ptr->prev )
      ptr->prev->next = ptr->next;
   else
      lt->first_library = ptr->next;

   ptr->next = lt->orphaned;
   if ( lt->orphaned )
      lt->orphaned->prev = ptr;

   lt->orphaned = ptr;

   /*
    * Now try to remove it really.
    */
   unlink_orphaned_libs( TSD, lt, 0 );
}
#endif

/*
 * remove_entry removes the passed library entry from the linked list of used
 * library entries unconditionally.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 * Used memory will be freed and the holding library will be removed if this
 * entry was the last entry used of the library.
 */
static void remove_entry( tsd_t *TSD, struct entry_point *fptr, int slot )
{
   lib_tsd_t *lt;

   assert( slot >= FUNCS && slot <= SUBCOMS );

   lt = TSD->lib_tsd;
   if ( fptr->name )
      Free_stringTSD( fptr->name );
#if defined(HAVE_GCI) && defined(DYNAMIC)
   if ( ( fptr->special.gci_info != NULL ) && ( slot == FUNCS ) )
      GCI_remove_structure( TSD, fptr->special.gci_info );
#endif
   if ( fptr->next )
      fptr->next->prev = fptr->prev;
   if ( fptr->prev )
      fptr->prev->next = fptr->next;
   else
      lt->ep[slot][fptr->hash % EP_COUNT] = fptr->next;

#ifdef DYNAMIC
   if ( fptr->lib != NULL )
   {
      assert( fptr->lib->used > 0 );
      if ( --fptr->lib->used == 0 )
         remove_library( TSD, fptr->lib );
   }
#endif

   FreeTSD( fptr );
}

/*
 * free_orphaned_libs disconnect from unused DLLs, see unlink_orphaned_libs.
 */
void free_orphaned_libs( tsd_t *TSD )
{
#ifdef DYNAMIC
   lib_tsd_t *lt = TSD->lib_tsd;

   unlink_orphaned_libs( TSD, lt, 0 );
#else
   (TSD = TSD);
#endif
}

/*
 * purge_library frees all used memory used by every entry point that is
 * registered and unloads every library.
 * This routine is a little bit slow.
 */
void purge_library( tsd_t *TSD )
{
   struct entry_point *ep, *save_ep;
   lib_tsd_t *lt;
   int i, j;

   lt = TSD->lib_tsd;
   if ( lt->first_library != NULL )
   {
      for ( i = FUNCS; i <= SUBCOMS; i++ )
      {
         for ( j = 0; j < EP_COUNT; j++ )
         {
            if ( ( ep = lt->ep[i][j] ) != NULL )
            {
               do {
                  save_ep = ep;
                  remove_entry( TSD, ep, i );
                  if ( ( ep = lt->ep[i][j] ) == save_ep )
                     break;
               } while ( ep != NULL );
               if ( lt->first_library == NULL )
                  goto fastEnd;
            }
         }
      }
   }
   fastEnd:
   assert( lt->first_library == NULL );
   lt->first_library = NULL;
#ifdef DYNAMIC
   unlink_orphaned_libs( TSD, lt, 1 );
#endif
   assert( lt->orphaned == NULL );
   lt->orphaned = NULL;
   memset( lt->ep, 0, sizeof( lt->ep ) );
}

#ifdef DYNAMIC
/*
 * find_library returns the internal structure associated with the passed
 * library name or NULL if such a library doesn't exists.
 */
struct library *find_library( const tsd_t *TSD, const streng *name )
{
   struct library *lptr;
   lib_tsd_t *lt;

   lt = TSD->lib_tsd;
   lptr = lt->first_library;
   for ( ; lptr; lptr = lptr->next )
   {
      if ( !Str_cmp( name, lptr->name ) )
         return lptr;
   }

   return NULL;
}
#endif

/*
 * add_entry creates a new library entry from the passed data and inserts it
 * in the linked list of used entries unconditionally.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 * rxname is the name that can be used by a REXX script.
 * addr is the entry point of the function/exit hook/subcom hook.
 * lptr is a loaded library or NULL for a call of RexxRegister???Exe.
 * Either gci_info or user_area may be set depending on the kind of the entry.
 *
 * The internal counter of the library isn't incremented.
 */
static void add_entry( const tsd_t *TSD, int slot, const streng *rxname,
                       PFN addr, struct library *lptr, void *gci_info,
                       void *user_area )
{
   int hash0;
   lib_tsd_t *lt;
   struct entry_point *fptr;

   assert( slot >= FUNCS && slot <= SUBCOMS );
   lt = TSD->lib_tsd;

   fptr = MallocTSD( sizeof( struct entry_point ) );
   fptr->name = Str_upper( Str_dupstrTSD( rxname ) );
   fptr->hash = hashvalue( rxname->value, rxname->len );
   fptr->addr = addr;
   fptr->lib = lptr;
   memset( &fptr->special, 0, sizeof( fptr->special ) );
   if ( slot == FUNCS )
      fptr->special.gci_info = gci_info;
   else
   {
      if ( user_area != NULL )
         memcpy( fptr->special.user_area, user_area,
                 sizeof ( fptr->special.user_area ) );
   }

   hash0 = fptr->hash % EP_COUNT;
   fptr->next = lt->ep[slot][hash0];
   lt->ep[slot][hash0] = fptr;
   fptr->prev = NULL;
   if ( fptr->next )
      fptr->next->prev = fptr;
}

/*
 * find_entry_point returns NULL if no entry is found. Returns the exact entry
 * if both the name and the library match. Returns any entry with a fitting
 * name if the library doesn't match but the name exists.
 * library may be NULL for entries registered by RexxRegister???Exe.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 */
static struct entry_point *find_entry_point( const tsd_t *TSD,
                                             const streng *name,
                                             void *library,
                                             int slot )
{
   struct entry_point *lptr;
   unsigned hash, hash0;
   lib_tsd_t *lt;
   struct entry_point *retval = NULL;

   lt = TSD->lib_tsd;
   hash = hashvalue( name->value, name->len );
   hash0 = hash % EP_COUNT;
   for ( lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
   {
      if ( hash == lptr->hash )
         if ( Str_cmp( name, lptr->name ) == 0 )
         {
            if ( lptr->lib == library )
               return lptr;
            else
               retval = lptr;
         }
   }

   return retval;
}

/*
 * find_first_entry_point returns NULL if no entry is found and returns the
 * most recent hook otherwise.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 */
static struct entry_point *find_first_entry_point( const tsd_t *TSD,
                                                   const streng *name,
                                                   int slot )
{
   struct entry_point *lptr;
   unsigned hash, hash0;
   lib_tsd_t *lt;

   lt = TSD->lib_tsd;
   hash = hashvalue( name->value, name->len );
   hash0 = hash % EP_COUNT;
   for ( lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
   {
      if ( hash == lptr->hash )
         if ( Str_cmp( name, lptr->name ) == 0 )
            return lptr;
   }

   return NULL;
}

/*
 * find_all_entries returns 0 if no entry is found. Otherwise it returns the
 * number of all matching entries with the given name, different in the module
 * name only.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 * *list will be set to a list of all available entries.
 *
 * This function is slow.
 */
static int find_all_entries( const tsd_t *TSD, const streng *name, int slot,
                             struct entry_point ***list )
{
   struct entry_point *lptr, **array;
   unsigned hash, hash0;
   lib_tsd_t *lt;
   int cnt;

   lt = TSD->lib_tsd;
   hash = hashvalue( name->value, name->len );
   hash0 = hash % EP_COUNT;
   for ( cnt = 0, lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
   {
      if ( hash == lptr->hash )
         if ( Str_cmp( name, lptr->name ) == 0 )
            cnt++;
   }

   if ( cnt == 0 )
   {
      *list = NULL;
      return 0;
   }

   array = MallocTSD( cnt * sizeof( struct entry_point * ) );
   *list = array;

   for ( cnt = 0, lptr = lt->ep[slot][hash0]; lptr; lptr = lptr->next )
   {
      if ( hash == lptr->hash )
         if ( Str_cmp( name, lptr->name ) == 0 )
            array[cnt++] = lptr;
   }

   return cnt;
}

/*
 * set_err_message replaces the current error message by a new one that will
 * be assembled as the concatenation of the two passed messages.
 * The created string will be returned by RxFuncErrMsg().
 */
void set_err_message( const tsd_t *TSD, const char *message1,
                      const char *message2 )
{
   lib_tsd_t *lt;
   int size;

   lt = TSD->lib_tsd;
   if ( lt->err_message )
      Free_stringTSD( lt->err_message );

   size = strlen( message1 ) + strlen( message2 );
   lt->err_message = Str_makeTSD( size + 1 );
   if ( lt->err_message )
   {
      strcpy( lt->err_message->value, message1 );
      strcat( lt->err_message->value, message2 );
      lt->err_message->len = size;
   }
}

/*
 * load_entry creates a new library entry from the passed data and inserts it
 * in the linked list of used entries.
 * lptr is a loaded library or NULL for a call of RexxRegister???Exe.
 * rxname is the name that can be used by a REXX script.
 * objnam will be used if lptr != NULL only and is the name of the hook
 * or function that is exported by the library.
 * entry will be used if lptr == NULL only and is the entry point of the hook
 * or function.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 * Either gci_info or user_area may be set depending on the kind of the entry.
 *
 * Return codes:
 *    0 on success.
 *    1 if the function is defined already.
 *    1 if the hook is defined already and bound to the same library. The new
 *      hook is rejected.
 *    2 if the hook is defined already and bound to another library. The new
 *      hook is accepted.
 *    3 if objnam isn't exported by lptr.
 *    4 if external libraries are not supported.
 */
static int load_entry( const tsd_t *TSD, struct library *lptr,
                       const streng *rxname, const streng *objnam, PFN entry,
                       int slot, void *gci_info, void *user_area )
{
   int result=0;
   struct entry_point *fptr;

   assert( ( lptr != NULL ) ^ ( entry != NULL ) );
   assert( rxname != NULL );
   assert( slot >= FUNCS && slot <= SUBCOMS );

   /*
    * Check the exceptions first.
    */
   if ( ( fptr = find_entry_point( TSD, rxname, lptr, slot ) ) != NULL )
   {
      /*
       * EXITS and SUBCOMS may have the same callable name bound to different
       * modules.
       */
      if ( ( slot == FUNCS ) || ( fptr->lib == lptr ) )
         return 1;
      /*
       * must be a hook with the same name in a different module.
       */
      result = 2;
   }

   if ( lptr )
   {
      assert( objnam != NULL );
#ifdef DYNAMIC
      if ( ( entry = wrapper_get_addr( TSD, lptr, objnam ) ) == NULL )
         return 3;
      lptr->used++;
#else
      return 4;
#endif
   }

   add_entry( TSD, slot, rxname, entry, lptr, gci_info, user_area );
   return result;
}

/*
 * unload_entry removes a known library entry from the linked list of known
 * entries.
 *
 * rxname is the name that can be used by a REXX script.
 * module is the name of the library and may be NULL for a generic request
 * or is a RexxRegister???Exe registered funcion/hook shall be unloaded.
 * The slot must be either FUNCS, EXISTS, or SUBCOMS.
 *
 * Return codes:
 *    0 on success.
 *    1 if the function/hook is not defined or a hook with this name is bound
 *      to different modules and the module name is not given.
 */
static int unload_entry( tsd_t *TSD, const streng *rxname,
                         const streng *module, int slot )
{
   struct entry_point *fptr, **list;
   struct library *lib;
   int cnt;

#ifdef DYNAMIC
   if ( module == NULL )
      lib = NULL;
   else
   {
      if ( ( lib = find_library( TSD, module ) ) == NULL )
         return 1;
   }
#else
   if ( module != NULL )
      return 1;
   lib = NULL;
#endif

   fptr = find_entry_point( TSD, rxname, lib, slot );
   if ( fptr == NULL )
      return 1;

   if ( fptr->lib == lib )
   {
      remove_entry( TSD, fptr, slot );
      return 0;
   }

   /*
    * Not a properly matching function. Check for the "wildcard" library.
    */
   if ( lib != NULL )
      return 1;

   /*
    * We need it the hard way. Check if more than one entry is registered.
    */
   cnt = find_all_entries( TSD, rxname, slot, &list );
   if ( cnt > 1 )
   {
      FreeTSD( list );
      return 1;
   }

   remove_entry( TSD, *list, slot );
   FreeTSD( list );
   return 0;
}

/*
 * loadrxfunc adds a new function to the set of registered entry points.
 *
 * lptr is a loaded library or NULL for a call of RexxRegisterFunctionExe.
 * rxname is the name that can be used by a REXX script.
 * objnam will be used if lptr != NULL only and is the name of the function
 * that is exported by the library.
 * entry will be used if lptr == NULL only and is the entry point of the
 * function.
 * gci_info may be set depending on whether RxFuncDefine is used.
 *
 * Returns a return code suitable for RexxRegisterFunction???.
 */
static int loadrxfunc( const tsd_t *TSD, struct library *lptr,
                       const streng *rxname, const streng *objnam, PFN entry,
                       void *gci_info )
{
   int rc;

   rc = load_entry( TSD, lptr, rxname, objnam, entry, FUNCS, gci_info, NULL );
   switch ( rc )
   {
      case 0:  return 0;   /* RXFUNC_OK */
      case 1:  return 10;  /* RXFUNC_DEFINED */
      case 3:  return 50;  /* RXFUNC_ENTNOTFND */
      case 4:  return 60;  /* RXFUNC_NOTINIT */
   }
   assert ( rc != 0 );
   return 10000 + rc; /* something not recognisable */
}

/*
 * loadrxhook adds a new exit/subcom hook to the set of registered entry
 * points.
 *
 * lptr is a loaded library or NULL for a call of RexxRegister???Exe.
 * rxname is the name that can be used in a hook list.
 * objnam will be used if lptr != NULL only and is the name of the hook that
 * that is exported by the library.
 * entry will be used if lptr == NULL only and is the entry point of the hook.
 * user_area is the passed parameter called UserArea of the Registration.
 * The slot must be either EXISTS or SUBCOMS.
 *
 * Returns a return code suitable for RexxRegister???.
 */
static int loadrxhook( const tsd_t *TSD, struct library *lptr,
                       const streng *rxname, const streng *objnam, PFN entry,
                       void *user_area, int slot )
{
   int rc;

   rc = load_entry( TSD, lptr, rxname, objnam, entry, slot, NULL, user_area );
   switch ( rc )
   {
      case 0:  return 0;     /* RX???_OK */
      case 1:  return 30;    /* RX???_NOTREG */
      case 2:  return 10;    /* RX???_DUP */
      case 3:  return 50;    /* RX???_LOADERR */
      case 4:  return 1004;  /* RX???_NOTINIT */
   }
   assert ( rc != 0 );
   return 10000 + rc; /* something not recognisable */
}

/*
 * unloadrxhook removes a registered function entry point.
 *
 * rxname is the name that can be used by a REXX script.
 *
 * Returns a return code suitable for RexxDeregisterFunction.
 */
static int unloadrxfunc( tsd_t *TSD, const streng *rxname )
{
   assert( rxname != NULL );

   if ( unload_entry( TSD, rxname, NULL, FUNCS ) == 0 )
      return 0;
   return 30; /* RXFUNC_NOTREG */
}

/*
 * unloadrxhook removes a registered exit/subcom hook entry point.
 *
 * rxname is the name that can be used in a hook list.
 * module is the name of the module that contains the hook or NULL if either
 * the generic hook should be removed or if a RexxRegister???Exe-hook should
 * be removed. The later one has precedence.
 * The slot must be either EXISTS or SUBCOMS.
 *
 * Returns a return code suitable for RexxDeregister???.
 */
static int unloadrxhook( tsd_t *TSD, const streng *rxname,
                         const streng *module, int slot )
{
   assert( rxname != NULL );

   if ( unload_entry( TSD, rxname, module, slot ) == 0 )
      return 0;
   return 30; /* RX???_NOTREG */
}

/*
 * rex_funcadd processes a RexxRegisterFunctionDll() or
 * RexxRegisterFunctionExe() request.
 *
 * rxname is the name that can be used by a REXX script.
 * module is the name of the library and may be NULL only a
 * RexxRegisterFunctionExe is processed.
 * objnam will be used if module != NULL only and is the name of the function
 * that is exported by the library.
 * entry will be used if module == NULL only and is the entry point of the
 * function.
 * gci_info may be set depending on whether RxFuncDefine is used.
 *
 * Returns a return code suitable for RexxRegisterFunction???.
 */
static int rex_funcadd( const tsd_t *TSD, const streng *rxname,
                        const streng *module, const streng *objnam, PFN entry,
                        void *gci_info )
{
   struct library *lptr=NULL;
   int rc;
#ifdef DYNAMIC
   void *handle;
   int new = 0;
#endif

   assert( rxname != NULL );

   if ( module != NULL )
   {
      assert( entry == NULL );
#ifdef DYNAMIC
      if ( ( lptr = find_library( TSD, module ) ) == NULL )
      {
         new = 1;
         handle = wrapper_load( TSD, module ) ;
         if ( handle )
         {
            lptr = MallocTSD( sizeof( struct library )) ;
            lptr->name = Str_dupstrTSD( module ) ;
            lptr->handle = handle ;
            lptr->used = 0l;
         }
         else
         {
            return 40; /* RXFUNC_MODNOTFND */
         }
         insert_library( TSD, lptr ) ;
      }
#else
      return 60; /* RXFUNC_NOTINIT */
#endif
   }
   else
   {
      assert( entry != NULL );
   }
   if ( ( rc = loadrxfunc( TSD, lptr, rxname, objnam, entry, gci_info ) ) != 0 )
   {
#ifdef DYNAMIC
      if ( new )
         remove_library( TSD, lptr );
#endif
   }
   return rc;
}

/*
 * rex_hookadd processes a RexxRegisterExitDll(), RexxRegisterExitExe(),
 * RexxRegisterSubcomDll(), or RexxRegisterSubcomExe() request.
 *
 * rxname is the name that can be used in a hook list.
 * module is the name of the library and may be NULL only a RexxRegister???Exe
 * is processed.
 * objnam will be used if module != NULL only and is the name of the hook
 * that is exported by the library.
 * entry will be used if module == NULL only and is the entry point of the
 * hook.
 * user_area is the passed parameter called UserArea of the Registration.
 * The slot must be either EXISTS or SUBCOMS.
 *
 * Returns a return code suitable for RexxRegister???.
 */
static int rex_hookadd( const tsd_t *TSD, const streng *rxname,
                        const streng *module, const streng *objnam, PFN entry,
                        void *user_area, int slot )
{
   struct library *lptr=NULL;
   int rc;
#ifdef DYNAMIC
   void *handle;
   int new = 0;
#endif

   assert( rxname != NULL );

   if ( module != NULL )
   {
      assert( entry == NULL );
#ifdef DYNAMIC
      if ( ( lptr = find_library( TSD, module ) ) == NULL )
      {
         new = 1;
         handle = wrapper_load( TSD, module ) ;
         if ( handle )
         {
            lptr = MallocTSD( sizeof( struct library )) ;
            lptr->name = Str_dupstrTSD( module ) ;
            lptr->handle = handle ;
            lptr->used = 0l;
         }
         else
         {
            return 50; /* RX???_LOADERR */
         }
         insert_library( TSD, lptr ) ;
      }
#else
      return 1004; /* RX???_NOTINIT */
#endif
   }
   else
   {
      assert( entry != NULL );
   }
   rc = loadrxhook( TSD, lptr, rxname, objnam, entry, user_area, slot );
   if ( ( rc != 0 ) && ( rc != 10 ) )
   {
#ifdef DYNAMIC
      if ( new )
         remove_library( TSD, lptr );
#endif
   }
   return rc;
}

/*
 * rex_rxfuncerrmsg implements the BIF RxFuncErrMsg.
 */
streng *rex_rxfuncerrmsg( tsd_t *TSD, cparamboxptr parms )
{
#ifdef DYNAMIC
   lib_tsd_t *lt;
#endif

   checkparam( parms, 0, 0, "RXFUNCERRMSG" );

#ifdef DYNAMIC
   lt = TSD->lib_tsd;
   if ( lt->err_message )
      return Str_dupTSD( lt->err_message );
   else
      return nullstringptr();
#else
   return Str_creTSD( "Platform doesn't support dynamic linking" );
#endif
}

/*
 * rex_rxfuncquery implements the BIF RxFuncQuery.
 */
streng *rex_rxfuncquery( tsd_t *TSD, cparamboxptr parms )
{
#ifdef DYNAMIC
   streng *name;
   struct entry_point *fptr;
#endif

   checkparam( parms, 1, 1, "RXFUNCQUERY" );

#ifdef DYNAMIC
   name = Str_upper( Str_dupTSD( parms->value ) );
   fptr = find_entry_point( TSD, name, NULL, FUNCS );
   Free_stringTSD( name );

   if ( fptr )
      return int_to_streng( TSD, 0 );
   /*
    * FIXME: We have to discuss whether to return a stupid 1 or a
    *        more informational 30/60 for RXFUNC_NOTREG or RXFUNC_NOTINIT
    */
   /* return int_to_streng( TSD, 30 ); */ /* RXFUNC_NOTREG */
   return int_to_streng( TSD, 1 );
#else
   /* return int_to_streng( TSD, 60 ); */ /* RXFUNC_NOTINIT */
   return int_to_streng( TSD, 1 );
#endif
}


/*
 * rex_rxfuncadd implements the BIF RxFuncAdd.
 * The returned value is suitable for RexxRegisterFunctionDll.
 *
 * Parameters:
 *   1) name of the function to be added (in Rexx)
 *   2) name of object file to link in
 *   3) name of the function to be added (in the object file)
 */
streng *rex_rxfuncadd( tsd_t *TSD, cparamboxptr parms )
{
#ifdef DYNAMIC
   streng *rxname;
   streng *module, *objnam;
   int rc;
#endif

   if ( TSD->restricted )
      exiterror( ERR_RESTRICTED, 1, "RXFUNCADD" );

   checkparam( parms, 2, 3, "RXFUNCADD" );

#ifdef DYNAMIC
   rxname = Str_upper( Str_dupTSD( parms->value ) );
   objnam = parms->value;
   module = ( parms = parms->next )->value;
   if ( ( parms->next != NULL ) && ( parms->next->value != NULL ) )
      objnam = parms->next->value;

   rc = rex_funcadd( TSD, rxname, module, objnam, NULL, NULL );
   Free_stringTSD( rxname );
   return int_to_streng( TSD, rc );
#else
   return int_to_streng( TSD, 60 ); /* RXFUNC_NOTINIT */
#endif
}

#ifdef HAVE_GCI
/*
 * rex_rxfuncdefine implements the BIF RxFuncDefine.
 *
 * parameters:
 *   1) name of the function to be added (in Rexx)
 *   2) name of object file to link in
 *   3) name of the function to be added (in the object file)
 *   4) name of a stem containing the definition of the function
 */
streng *rex_rxfuncdefine( tsd_t *TSD, cparamboxptr parms )
{
#ifdef DYNAMIC
   streng *rxname,*module,*objnam,*def_stem;
   void *gci_info;
   int rc;
#endif

   if ( TSD->restricted )
      exiterror( ERR_RESTRICTED, 1, "RXFUNCDEFINE" );

   checkparam( parms, 4, 4, "RXFUNCDEFINE" );

#ifdef DYNAMIC
   rxname = Str_upper( Str_dupTSD( parms->value ) );
   objnam = parms->value;
   module = ( parms = parms->next )->value;
   parms = parms->next;
   if ( parms->value != NULL )
      objnam = parms->value;
   def_stem = parms->next->value;

   if ( ( rc = GCI_checkDefinition( TSD, def_stem, &gci_info ) ) != 0 )
   {
      Free_stringTSD( rxname );
      return int_to_streng( TSD, 1 );
   }

   rc = rex_funcadd( TSD, rxname, module, objnam, NULL, gci_info );
   Free_stringTSD( rxname );
   if ( rc )
      GCI_remove_structure( TSD, gci_info );
   return int_to_streng( TSD, rc );
#else
   return int_to_streng( TSD, 60 ); /* RXFUNC_NOTINIT */
#endif
}
#endif

/*
 * rex_rxfuncdrop implements the BIF RxFuncDrop.
 * The returned value is suitable for RexxDeregisterFunction.
 */
streng *rex_rxfuncdrop( tsd_t *TSD, cparamboxptr parms )
{
   streng *name;

   checkparam( parms, 1, 1, "RXFUNCDROP" );
   name = Str_upper( parms->value );

   return int_to_streng( TSD, unloadrxfunc( TSD, name ) );
}

/*
 * IfcRegFunc is the interface function for RexxRegisterFunctionExe and
 * RexxRegisterFunctionDll.
 * Either entry or module and objnam must be set.
 */
int IfcRegFunc( const tsd_t *TSD, const char *rxname, const char *module,
                const char *objnam, PFN entry )
{
   int rc;
   streng *ext;
   streng *intr=NULL;
   streng *lib=NULL;

   ext = Str_upper( Str_creTSD( rxname ) );
   if ( module && objnam )
   {
      intr = Str_creTSD( objnam );
      lib = Str_creTSD( module );
   }

   rc = rex_funcadd( TSD, ext, lib, intr, entry, NULL );

   Free_stringTSD( ext );
   if ( intr && lib )
   {
      Free_stringTSD( intr );
      Free_stringTSD( lib );
   }

   return rc;
}

/*
 * IfcRegHook is the interface function for RexxRegisterExitExe,
 * RexxRegisterExitDll, RexxRegisterSubcomExe, RexxRegisterSubcomtDll.
 * Either entry or module and objnam must be set.
 */
static int IfcRegHook( const tsd_t *TSD, const char *rxname,
                       const char *module, const char *objnam, PFN entry,
                       void *user_area, int slot )
{
   int rc;
   streng *ext;
   streng *intr=NULL;
   streng *lib=NULL;

   ext = Str_upper( Str_creTSD( rxname ) );
   if ( module && objnam )
   {
      intr = Str_creTSD( objnam );
      lib = Str_creTSD( module );
   }

   rc = rex_hookadd( TSD, ext, lib, intr, entry, user_area, slot );

   Free_stringTSD( ext );
   if ( intr && lib )
   {
      Free_stringTSD( intr );
      Free_stringTSD( lib );
   }

   return rc;
}

/*
 * IfcRegExit is the interface function for RexxRegisterExitExe or
 * RexxRegisterExitDll.
 * Either entry or module and objnam must be set.
 */
int IfcRegExit( const tsd_t *TSD, const char *rxname, const char *module,
                const char *objnam, PFN entry, void *user_area )
{
   return IfcRegHook( TSD, rxname, module, objnam, entry, user_area, EXITS );
}

/*
 * IfcRegSubcom is the interface function for RexxRegisterSubcomExe or
 * RexxRegisterSubcomDll.
 * Either entry or module and objnam must be set.
 */
int IfcRegSubcom( const tsd_t *TSD, const char *rxname, const char *module,
                  const char *objnam, PFN entry, void *user_area )
{
   return IfcRegHook( TSD, rxname, module, objnam, entry, user_area, SUBCOMS );
}

/*
 * IfcDelFunc is the interface function for RexxDeregisterFunction.
 */
int IfcDelFunc( tsd_t *TSD, const char *rxname )
{
   int rc;
   streng *ext;

   ext = Str_upper( Str_creTSD( rxname ) );
   rc = unloadrxfunc( TSD, ext );
   Free_stringTSD( ext );

   return rc;
}

/*
 * IfcDelHook is the interface function for RexxDeregisterExit or
 * RexxDeregisterSubcom.
 */
static int IfcDelHook( tsd_t *TSD, const char *rxname, const char *module,
                       int slot )
{
   int rc;
   streng *ext,*mod;

   ext = Str_upper( Str_creTSD( rxname ) );
   if ( module != NULL )
      mod = Str_creTSD( module );
   else
      mod = NULL;
   rc = unloadrxhook( TSD, ext, mod, slot );
   Free_stringTSD( ext );
   if ( mod != NULL )
      Free_stringTSD( mod );

   return rc;
}

/*
 * IfcDelExit is the interface function for RexxDeregisterExit.
 */
int IfcDelExit( tsd_t *TSD, const char *rxname, const char *module )
{
   return IfcDelHook( TSD, rxname, module, EXITS );
}

/*
 * IfcDelSubcom is the interface function for RexxDeregisterSubcom.
 */
int IfcDelSubcom( tsd_t *TSD, const char *rxname, const char *module )
{
   return IfcDelHook( TSD, rxname, module, SUBCOMS );
}

/*
 * IfcQueryFunc is the interface function for RexxQueryFunction.
 */
int IfcQueryFunc( const tsd_t *TSD, const char *rxname )
{
   int rc;
   streng *ext;

   ext = Str_upper( Str_creTSD( rxname ) );
   rc = ( find_entry_point( TSD, ext, NULL, FUNCS ) != NULL ) ? 0 : 30;
   Free_stringTSD( ext );

   return rc;
}

/*
 * IfcQueryHook is the interface function for RexxQueryExit or RexxQuerySubcom.
 */
static int IfcQueryHook( const tsd_t *TSD, const char *rxname,
                         const char *module, int slot, void *user_area )
{
   streng *ext;
   struct entry_point *fptr,**list;
   struct library *lib;
   int cnt;
#ifdef DYNAMIC
   streng *mod;
#endif

   ext = Str_upper( Str_creTSD( rxname ) );
   if ( module != NULL )
   {
#ifdef DYNAMIC
      mod = Str_creTSD( module );
      lib = find_library( TSD, mod );
      Free_stringTSD( mod );
      if ( lib == NULL )
      {
         Free_stringTSD( ext );
         return 30; /* RX???_NOTREG */
      }
#else
      return 1004; /* RX???_NOTINIT */
#endif
   }
   else
      lib = NULL;

   fptr = find_entry_point( TSD, ext, lib, slot );

   if ( fptr == NULL )
   {
      Free_stringTSD( ext );
      return 30; /* RX???_NOTREG */
   }

   if ( fptr->lib != lib )
   {
      /*
       * Found via wildcard mechanism, check if more than one element exists
       * and if a wildcard is allowed.
       */
      if ( lib != NULL )
      {
         Free_stringTSD( ext );
         return 30; /* RX???_NOTREG */
      }

      cnt = find_all_entries( TSD, ext, slot, &list );
      FreeTSD( list );
      Free_stringTSD( ext );

      if ( cnt > 1 )
         return 30; /* RX???_NOTREG */
   }
   else
      Free_stringTSD( ext );

   if ( user_area != NULL )
      memcpy( user_area, fptr->special.user_area,
              sizeof ( fptr->special.user_area ) );
   return 0;
}

/*
 * IfcQueryExit is the interface function for RexxQueryExit.
 */
int IfcQueryExit( const tsd_t *TSD, const char *rxname, const char *module,
                  void *user_area )
{
   return IfcQueryHook( TSD, rxname, module, EXITS, user_area );
}

/*
 * IfcQuerySubcom is the interface function for RexxQuerySubcom.
 */
int IfcQuerySubcom( const tsd_t *TSD, const char *rxname, const char *module,
                    void *user_area )
{
   return IfcQueryHook( TSD, rxname, module, SUBCOMS, user_area );
}

struct entry_point *loaded_lib_func( const tsd_t *TSD, const streng *name )
{
   struct entry_point *box;
   streng *upp;

   upp = Str_upper( Str_dupTSD( name ) );
   box = find_first_entry_point( TSD, upp, FUNCS );
   Free_stringTSD( upp );

   return box;
}

/*
 * exit_hook returns the most recent exit handler of the given name.
 * The value may be NULL if no hook is registered.
 */
struct entry_point *exit_hook( const tsd_t *TSD, const char *env, int len )
{
   streng *name;
   struct entry_point *ret;

   name = Str_upper( Str_ncreTSD( env, len ) );
   ret = find_first_entry_point( TSD, name, EXITS );
   Free_stringTSD( name );

   return ret;
}

/*
 * subcom_hook returns the most recent subcom handler of the given name.
 * The value may be NULL if no hook is registered.
 */
struct entry_point *subcom_hook( const tsd_t *TSD, const char *com, int len )
{
   streng *name;
   struct entry_point *ret;

   name = Str_upper( Str_ncreTSD( com, len ) );
   ret = find_first_entry_point( TSD, name, SUBCOMS );
   Free_stringTSD( name );

   return ret;
}