1 /*
2  * sqsh_func.c - Routines for manipulating user defined sqsh functions
3  *
4  * Copyright (C) 1995, 1996 by Scott C. Gray
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, write to the Free Software
18  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * You may contact the author :
21  *   e-mail:  gray@voicenet.com
22  *            grays@xtend-tech.com
23  *            gray@xenotropic.com
24  */
25 #include <stdio.h>
26 #include "sqsh_config.h"
27 #include "sqsh_error.h"
28 #include "sqsh_avl.h"
29 #include "sqsh_func.h"
30 
31 /*-- Current Version --*/
32 #if !defined(lint) && !defined(__LINT__)
33 static char RCS_Id[] = "$Id: sqsh_func.c,v 1.1.1.1 2004/04/07 12:35:04 chunkm0nkey Exp $";
34 USE(RCS_Id)
35 #endif /* !defined(lint) */
36 
37 /*-- Prototypes --*/
38 static func_t*  func_create    _ANSI_ARGS(( char*, char* ));
39 static void     func_destroy   _ANSI_ARGS(( func_t* ));
40 static int      func_cmp       _ANSI_ARGS(( func_t*, func_t* ));
41 
funcset_create()42 funcset_t* funcset_create()
43 {
44 	funcset_t  *fs;
45 
46 	/*-- Allocate a new command set --*/
47 	if ((fs = (funcset_t*)malloc( sizeof(funcset_t) )) == NULL)
48 	{
49 		sqsh_set_error( SQSH_E_NOMEM, NULL );
50 		return NULL ;
51 	}
52 
53 	/*-- Allocate a tree for our commands --*/
54 	if ((fs->fs_funcs =
55 		avl_create((avl_cmp_t*)func_cmp, (avl_destroy_t*)func_destroy)) == NULL)
56 	{
57 		free( fs );
58 		return NULL;
59 	}
60 
61 	sqsh_set_error( SQSH_E_NONE, NULL );
62 	return fs;
63 }
64 
funcset_add(fs,func_name,func_body)65 int funcset_add( fs, func_name, func_body )
66 	funcset_t *fs;
67 	char      *func_name;
68 	char      *func_body;
69 {
70 	func_t    *f;
71 
72 	/*-- Validate arguments --*/
73 	if (fs == NULL || func_name == NULL || func_body == NULL )
74 	{
75 		sqsh_set_error( SQSH_E_BADPARAM, NULL );
76 		return False;
77 	}
78 
79 	/*-- Create a new command structure --*/
80 	if ((f = func_create( func_name, func_body )) == NULL)
81 		return False;
82 
83 	/*-- And stick it in the tree --*/
84 	if (avl_insert( fs->fs_funcs, (void*)f ) == False)
85 		return False;
86 
87 	sqsh_set_error( SQSH_E_NONE, NULL );
88 	return True;
89 }
90 
funcset_get(fs,func_name)91 func_t* funcset_get( fs, func_name )
92 	funcset_t  *fs;
93 	char       *func_name;
94 {
95 	func_t    *f, func_search;
96 
97 	if (fs == NULL || func_name == NULL)
98 	{
99 		sqsh_set_error( SQSH_E_BADPARAM, NULL );
100 		return NULL;
101 	}
102 
103 	func_search.func_name = func_name;
104 	f = (func_t*)avl_find( fs->fs_funcs, (void*)&func_search );
105 
106 	if (f == NULL)
107 	{
108 		sqsh_set_error( SQSH_E_EXIST, NULL );
109 		return NULL;
110 	}
111 
112 	sqsh_set_error( SQSH_E_NONE, NULL );
113 	return f;
114 }
115 
funcset_destroy(fs)116 int funcset_destroy( fs )
117 	funcset_t *fs;
118 {
119 	if (fs == NULL)
120 		return False;
121 
122 	if (fs->fs_funcs != NULL)
123 		avl_destroy( fs->fs_funcs );
124 	free( fs );
125 
126 	return True;
127 }
128 
func_create(func_name,func_body)129 static func_t* func_create( func_name, func_body )
130 	char    *func_name;
131 	char    *func_body;
132 {
133 	func_t  *f;
134 
135 	if ((f = (func_t*)malloc(sizeof(func_t))) == NULL)
136 	{
137 		sqsh_set_error( SQSH_E_NOMEM, NULL );
138 		return NULL;
139 	}
140 
141 	if ((f->func_name = sqsh_strdup(func_name)) == NULL)
142 	{
143 		free(f);
144 		sqsh_set_error( SQSH_E_NOMEM, NULL );
145 		return NULL;
146 	}
147 
148 	if ((f->func_body = sqsh_strdup(func_body)) == NULL)
149 	{
150 		free(f->func_name);
151 		free(f);
152 		sqsh_set_error( SQSH_E_NOMEM, NULL );
153 		return NULL;
154 	}
155 
156 	return f;
157 }
158 
func_cmp(f1,f2)159 static int func_cmp( f1, f2 )
160 	func_t  *f1;
161 	func_t  *f2;
162 {
163 	return strcmp( f1->func_name, f2->func_name );
164 }
165 
func_destroy(f)166 static void func_destroy( f )
167 	func_t  *f;
168 {
169 	if (f != NULL)
170 	{
171 		if (f->func_name != NULL)
172 			free( f->func_name );
173 		free( f );
174 	}
175 }
176