bringing SDL 1.2.14 from vendor into the main branch
[AROS-Contrib.git] / regina / extlib.c
blob0f9776b269ea05cc9b7f830715d6e8770732edef
1 #ifndef lint
2 static char *RCSid = "$Id$";
3 #endif
5 /*
6 * The Regina Rexx Interpreter
7 * Copyright (C) 1993-1994 Anders Christensen <anders@pvv.unit.no>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "rexx.h"
26 struct extlib_funcbox {
27 struct extlib_funcbox *next, *prev ;
28 streng *name ;
29 int type ;
30 int hash1 ;
33 #define EXTFUNCS_COUNT (sizeof(((tsd_t *)0)->extfuncs) / \
34 sizeof(((tsd_t *)0)->extfuncs[0]))
36 static struct extlib_funcbox *findfunc( const tsd_t *TSD, const streng *name, int *hash,
37 int *hashbox )
39 struct extlib_funcbox *fptr=NULL ;
40 int lhashbox, lhash ;
42 *hash = lhash = hashvalue( name->value, name->len ) ;
43 *hashbox = lhashbox = lhash % EXTFUNCS_COUNT ;
44 for (fptr=TSD->extfuncs[lhashbox]; fptr; fptr=fptr->prev)
45 if (fptr->hash1 == lhash)
46 if (!Str_cmp(name, fptr->name))
47 return fptr ;
49 return NULL ;
52 int delfunc( tsd_t *TSD, const streng *name )
54 struct extlib_funcbox *old=NULL ;
55 int hash, hashbox ;
57 old = findfunc( TSD, name, &hash, &hashbox ) ;
58 if (!old)
59 return 1 ;
61 Free_stringTSD( old->name ) ;
62 if (old==TSD->extfuncs[hashbox])
63 TSD->extfuncs[hashbox] = old->prev ;
64 else
65 old->next->prev = old->prev ;
67 if (old->prev)
68 old->prev->next = old->next ;
70 FreeTSD( old ) ;
71 return 0 ;
74 /* addfunc returns 1 on success, 0 if already defined, -1 if memory is short.
75 * The argument name is used for further operation on success only.
77 int addfunc( tsd_t *TSD, streng *name, int type )
79 struct extlib_funcbox *new=NULL ;
80 int hashbox, hash ;
82 if (findfunc( TSD, name, &hash, &hashbox ))
83 return 0 ;
85 new = MallocTSD( sizeof(struct extlib_funcbox )) ;
86 if (!new)
87 return -1 ;
89 new->name = name ;
90 new->type = type ;
91 new->next = NULL ;
92 new->hash1 = hash ;
93 new->prev = TSD->extfuncs[hashbox] ;
94 if (TSD->extfuncs[hashbox])
95 TSD->extfuncs[hashbox]->next = new ;
96 TSD->extfuncs[hashbox] = new ;
98 return 1 ;
101 int external_func( const tsd_t *TSD, const streng *name )
103 struct extlib_funcbox *ptr=NULL ;
104 int hash, hashbox ;
106 ptr = findfunc( TSD, name, &hash, &hashbox ) ;
107 if (ptr)
108 return 1 ;
109 else
110 return 0 ;