forked from scanmem/scanmem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
160 lines (124 loc) · 4.38 KB
/
commands.c
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
/*
$Id: commands.c,v 1.8 2007-06-05 19:58:03+01 taviso Exp taviso $
Copyright (C) 2006,2007,2009 Tavis Ormandy <taviso@sdf.lonestar.org>
Copyright (C) 2009 Eli Dupree <elidupree@charter.net>
Copyright (C) 2009,2010 WANG Lu <coolwanglu@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <assert.h>
#include <strings.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include "commands.h"
#include "show_message.h"
/*
* registercommand - add the command and a pointer to its handler to the commands list.
*
* so that free(data) works when destroying the list, I just concatenate the string
* with the command structure. I could have used a static vector of commands, but this
* way I can add aliases and macros at runtime (planned in future).
*
*/
/*lint -esym(818,handler) dont declare handler const */
bool registercommand(const char *command, void *handler, list_t * commands,
char *shortdoc, char *longdoc)
{
command_t *data;
assert(commands != NULL);
if (command != NULL) {
if ((data = malloc(sizeof(command_t) + strlen(command) + 1)) == NULL) {
show_error("sorry, there was a memory allocation problem.\n");
return false;
}
data->command = (char *) data + sizeof(*data);
/* command points to the extra space allocated after data */
strcpy(data->command, command);
} else {
if ((data = malloc(sizeof(command_t))) == NULL) {
show_error("sorry, there was a memory allocation problem.\n");
return false;
}
data->command = NULL;
}
data->handler = handler;
data->shortdoc = shortdoc;
data->longdoc = longdoc;
/* add new command to list */
if (l_append(commands, NULL, data) == -1) {
free(data);
return false;
}
return true;
}
bool execcommand(globals_t * vars, const char *commandline)
{
unsigned argc;
char *str = NULL, *tok = NULL;
char **argv = NULL;
command_t *err = NULL;
bool ret = false;
list_t *commands = vars->commands;
element_t *np = NULL;
assert(commandline != NULL);
assert(commands != NULL);
vars->current_cmdline = commandline;
np = commands->head;
str = tok = strdupa(commandline);
/* tokenize command line into an argument vector */
for (argc = 0; tok; argc++, str = NULL) {
/* make enough size for another pointer (+1 for NULL at end) */
if ((argv = realloc(argv, (argc + 1) * sizeof(char *))) == NULL) {
show_error("sorry there was a memory allocation error.\n");
return false;
}
/* insert next token */
argv[argc] = tok = strtok(str, " \t");
}
assert(argc >= 1);
assert(argv != NULL);
/* just leading whitespace? */
if (argv[0] == NULL) {
free(argv);
/* legal i guess, just dont do anything */
return true;
}
/* search commands list for appropriate handler */
while (np) {
command_t *command = np->data;
/* check if this command matches */
if (command->command == NULL) {
/* the default handler has a NULL command */
err = command;
} else if (strcasecmp(argv[0], command->command) == 0) {
/* match found, execute handler */
ret = command->handler(vars, argv, argc - 1);
free(argv);
return ret;
}
np = np->next;
}
/* no match, if there was a default handler found, run it now */
if (err != NULL) {
ret = err->handler(vars, argv, argc - 1);
}
free(argv);
return ret;
}