1 /*
2  * sqsh_cmd.c - Routines for manipulating named function pointers
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_cmd.h"
30 
31 /*-- Current Version --*/
32 #if !defined(lint) && !defined(__LINT__)
33 static char RCS_Id[] = "$Id: sqsh_cmd.c,v 1.1.1.1 2004/04/07 12:35:05 chunkm0nkey Exp $" ;
34 USE(RCS_Id)
35 #endif /* !defined(lint) */
36 
37 /*-- Prototypes --*/
38 static cmd_t*   cmd_create    _ANSI_ARGS(( char*, cmd_f* )) ;
39 static void     cmd_destroy   _ANSI_ARGS(( cmd_t* )) ;
40 static int      cmd_cmp       _ANSI_ARGS(( cmd_t*, cmd_t* )) ;
41 
cmdset_create()42 cmdset_t* cmdset_create()
43 {
44 	cmdset_t  *cs ;
45 
46 	/*-- Allocate a new command set --*/
47 	if( (cs = (cmdset_t*)malloc( sizeof(cmdset_t) )) == NULL ) {
48 		sqsh_set_error( SQSH_E_NOMEM, NULL ) ;
49 		return NULL ;
50 	}
51 
52 	/*-- Allocate a tree for our commands --*/
53 	if( (cs->cs_cmd = avl_create( (avl_cmp_t*)cmd_cmp,
54 	                              (avl_destroy_t*)cmd_destroy )) == NULL ) {
55 		free( cs ) ;
56 		return NULL ;
57 	}
58 
59 	sqsh_set_error( SQSH_E_NONE, NULL ) ;
60 	return cs ;
61 }
62 
cmdset_add(cs,cmd_name,cmd_ptr)63 int cmdset_add( cs, cmd_name, cmd_ptr )
64 	cmdset_t *cs ;
65 	char     *cmd_name ;
66 	cmd_f    *cmd_ptr ;
67 {
68 	cmd_t    *c ;
69 
70 	/*-- Validate arguments --*/
71 	if( cs == NULL || cmd_name == NULL || cmd_ptr == NULL ) {
72 		sqsh_set_error( SQSH_E_BADPARAM, NULL ) ;
73 		return False ;
74 	}
75 
76 	/*-- Create a new command structure --*/
77 	if( (c = cmd_create( cmd_name, cmd_ptr )) == NULL )
78 		return False ;
79 
80 	/*-- And stick it in the tree --*/
81 	if( avl_insert( cs->cs_cmd, (void*)c ) == False )
82 		return False ;
83 
84 	sqsh_set_error( SQSH_E_NONE, NULL ) ;
85 	return True ;
86 }
87 
cmdset_get(cs,cmd_name)88 cmd_t* cmdset_get( cs, cmd_name )
89 	cmdset_t  *cs ;
90 	char      *cmd_name ;
91 {
92 	cmd_t    *c, cmd_search ;
93 
94 	if( cs == NULL || cmd_name == NULL ) {
95 		sqsh_set_error( SQSH_E_BADPARAM, NULL ) ;
96 		return NULL ;
97 	}
98 
99 	cmd_search.cmd_name = cmd_name ;
100 	c = (cmd_t*)avl_find( cs->cs_cmd, (void*)&cmd_search ) ;
101 
102 	if( c == NULL ) {
103 		sqsh_set_error( SQSH_E_EXIST, NULL ) ;
104 		return NULL ;
105 	}
106 
107 	sqsh_set_error( SQSH_E_NONE, NULL ) ;
108 	return c ;
109 }
110 
cmdset_destroy(cs)111 int cmdset_destroy( cs )
112 	cmdset_t *cs ;
113 {
114 	if( cs == NULL )
115 		return False ;
116 
117 	if( cs->cs_cmd != NULL )
118 		avl_destroy( cs->cs_cmd ) ;
119 	free( cs ) ;
120 
121 	return True ;
122 }
123 
cmd_create(cmd_name,cmd_ptr)124 static cmd_t* cmd_create( cmd_name, cmd_ptr )
125 	char   *cmd_name ;
126 	cmd_f  *cmd_ptr ;
127 {
128 	cmd_t  *c ;
129 
130 	if( (c = (cmd_t*)malloc(sizeof(cmd_t))) == NULL )  {
131 		sqsh_set_error( SQSH_E_NOMEM, NULL ) ;
132 		return NULL ;
133 	}
134 	if( (c->cmd_name = sqsh_strdup( cmd_name )) == NULL ) {
135 		free(c) ;
136 		sqsh_set_error( SQSH_E_NOMEM, NULL ) ;
137 		return NULL ;
138 	}
139 
140 	c->cmd_ptr   = cmd_ptr ;
141 	return c;
142 }
143 
cmd_cmp(c1,c2)144 static int cmd_cmp( c1, c2 )
145 	cmd_t  *c1 ;
146 	cmd_t  *c2 ;
147 {
148 	return strcmp( c1->cmd_name, c2->cmd_name ) ;
149 }
150 
cmd_destroy(c)151 static void cmd_destroy( c )
152 	cmd_t  *c ;
153 {
154 	if( c != NULL ) {
155 		if( c->cmd_name != NULL )
156 			free( c->cmd_name ) ;
157 		free( c ) ;
158 	}
159 }
160