1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
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 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
31 typedef struct setter_s
{
32 int (*func
)(array_t
*args
);
44 static int command_alias(array_t
*args
)
51 n
= command_data
.list
;
54 vlprintf(CN_INFO
, "alias: %s %s",n
->name
,n
->value
);
60 a
= array_get_string(args
,0);
61 c
= array_join(args
," ",1);
63 n
= nvp_get(&command_data
.list
,a
);
66 nvp_set(&command_data
.list
,a
,c
,NULL
);
71 vlprintf(CN_WARN
, "alias: Cannot unias built-in command '%s'",a
);
75 nvp_set(&command_data
.list
,a
,c
,NULL
);
81 static int command_help(array_t
*args
)
86 char* c
= array_get_string(args
,0);
90 n
= nvp_get(&command_data
.list
,c
);
96 /* TODO: implement the help() functionality */
101 vlprintf(CN_INFO
, "%s [arguments]",c
);
107 static int control_forward(array_t
*args
)
109 vlprintf(CN_INFO
, "forward");
113 /* initialise commands */
116 command_data
.mutex
= mutex_create();
117 command_add("help",command_help
);
118 command_add("set",config_set_command
);
119 command_add("unset",config_set_command
);
120 command_add("exec",config_load_command
);
121 command_add("ignore",config_ignore_command
);
122 command_add("alias",command_alias
);
123 command_add("bind",event_bind
);
124 command_add("forward",control_forward
);
126 command_add("forward",control_forward);
127 command_add("backward",control_backward);
128 command_add("left",control_left);
129 command_add("right",control_right);
130 command_add("jump",control_jump);
131 command_add("sneak",control_sneak);
132 command_add("inventory",control_inventory);
133 command_add("examine",control_examine);
134 command_add("use",control_use);
135 command_add("chat",control_chat);
136 command_add("fly",control_fly);
137 command_add("up",control_up);
138 command_add("down",control_down);
139 command_add("run",control_run);
140 command_add("dig",control_dig);
141 command_add("place",control_place);
142 command_add("wield0",control_wield0);
143 command_add("wield1",control_wield1);
144 command_add("wield2",control_wield2);
145 command_add("wield3",control_wield3);
146 command_add("wield4",control_wield4);
147 command_add("wield5",control_wield5);
148 command_add("wield6",control_wield6);
149 command_add("wield7",control_wield7);
150 command_add("wieldnext",control_wieldnext);
151 command_add("wieldprev",control_wieldprev);
152 command_add("console",control_console);
153 command_add("capture",control_capture);
159 /* register a new command function */
160 int command_add(char* name
, int (*func
)(array_t
*args
))
165 n
= nvp_get(&command_data
.list
,name
);
172 s
= malloc(sizeof(setter_t
));
178 nvp_set(&command_data
.list
,name
,NULL
,s
);
183 /* apply a command */
184 int command_apply(char* name
, char* value
)
190 int (*func
)(array_t
*args
);
192 mutex_lock(command_data
.mutex
);
194 n
= nvp_get(&command_data
.list
,name
);
196 mutex_unlock(command_data
.mutex
);
197 vlprintf(CN_WARN
, "Unknown command: '%s'",name
);
203 strcpy(buff
,n
->value
);
204 mutex_unlock(command_data
.mutex
);
206 return command_execf("%s %s",buff
,value
);
208 return command_exec(buff
);
213 mutex_unlock(command_data
.mutex
);
214 vlprintf(CN_WARN
, "Invalid command: '%s'",name
);
221 mutex_unlock(command_data
.mutex
);
222 vlprintf(CN_WARN
, "Invalid command: '%s'",name
);
228 mutex_unlock(command_data
.mutex
);
230 args
= array_split(value
," ",1);
239 /* execute a command */
240 int command_exec(char* str
)
249 spc
= strchr(cmd
,' ');
254 vlprintf(CN_INFO
, "%s",cmd
);
260 r
= command_apply(cmd
,args
);
264 r
= command_apply(cmd
,NULL
);
270 /* execute a command from a formatted string */
271 int command_execf(char* str
, ...)
277 l
= vsnprintf(buff
,1024,str
,ap
);
283 return command_exec(buff
);
286 /* save alias to file */
287 void command_save(file_t
*f
)
289 nvp_t
*n
= command_data
.list
;
292 file_writef(f
,"alias %s %s\n",n
->name
,n
->value
);