-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.c
169 lines (127 loc) · 2.96 KB
/
cmd.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
161
162
163
164
165
166
167
168
/* rzcmd.c
* 12 Jan 2006
* Scott Bronson
*
* Code used to parse commands out of the command line.
* (i.e. 'rzh --rz="lrz --disable-timeout"').
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "cmd.h"
// Calls the given function once for each individual token found
// in the given string. Right now this routine doesn't handle
// quoting or backslashing though ideally it should.
static int for_tokens(command *cmd, const char *cp, void (*proc)(command *cmd, int tokno, const char *str, int len))
{
const char *tok;
int cnt = 0;
for(;;) {
// skip any whitespace before the token
while(*cp != '\0' && isspace(*cp)) {
cp += 1;
}
if(!*cp) {
break;
}
tok = cp;
// scan past token
while(*cp != '\0' && !isspace(*cp)) {
cp += 1;
}
// we have a token, send it to the proc
if(proc) {
(*proc)(cmd, cnt, tok, cp-tok);
}
cnt += 1;
}
return cnt;
}
// Parses the command name ("ls") out of the executable path ("/bin/ls").
static char* cmd_name_from_path(const char *path)
{
const char *name = (const char*)strrchr(path, '/');
if(name == NULL) {
name = path;
} else {
name += 1; // skip the slash
}
return strdup(name);
}
// Can't rely on strndup on all platforms
// note that this ALWAYS allocates and copies len bytes, even
// if the string is shorter (contrary to the built-in strndup).
static char* my_strndup(const char *str, int len)
{
char *cp = malloc(len+1);
if(cp) {
memcpy(cp, str, len);
cp[len] = '\0';
}
return cp;
}
// Stores the given token into the pre-allocated argument list.
static void store_proc(command *cmd, int tokno, const char *str, int len)
{
cmd->args[tokno+1] = my_strndup(str, len);
}
void cmd_parse(command *cmd, const char *str)
{
const char *cp = str;
int cnt;
// skip any whitespace before the command
while(*cp != '\0' && isspace(*cp)) {
cp += 1;
}
if(!*cp) {
// leave old command unchanged because there was no
// new one specified.
fprintf(stderr, "Warning: null command specified for rz!\n");
return;
}
// beginning of the command
str = cp;
// scan past token
while(*cp != '\0' && !isspace(*cp)) {
cp += 1;
}
cmd_free(cmd);
cmd->path = my_strndup(str, cp-str);
cnt = for_tokens(cmd, cp, NULL);
cnt += 2; // space for argv[0] and the NULL terminator
cmd->args = malloc(cnt * sizeof(char*));
cmd->args[0] = cmd_name_from_path(cmd->path);
if(for_tokens(cmd, cp, store_proc) != cnt-2) {
assert(!"counts didn't match!");
}
cmd->args[cnt-1] = NULL;
}
void cmd_print(const char *str, command *cmd)
{
int i;
printf("%s: ('%s': ", str, cmd->path);
for(i=0; cmd->args[i] != NULL; i++) {
printf(", '%s'", cmd->args[i]);
}
printf(")\n");
}
void cmd_init(command *cmd)
{
cmd->path = NULL;
cmd->args = NULL;
}
void cmd_free(command *cmd)
{
int i;
if(cmd->path) {
free(cmd->path);
}
if(cmd->args) {
for(i=0; cmd->args[i] != NULL; i++) {
free((void*)cmd->args[i]);
}
free(cmd->args);
}
}