Allow unlimited number of arguments.
Use `strtok_r' to divide up string into arguments. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@1049 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
53
lib/sl/sl.c
53
lib/sl/sl.c
@@ -117,35 +117,48 @@ int
|
|||||||
sl_loop (SL_cmd *cmds, char *prompt)
|
sl_loop (SL_cmd *cmds, char *prompt)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
int count;
|
unsigned max_count;
|
||||||
char *ptr[17];
|
char **ptr;
|
||||||
int i;
|
|
||||||
|
max_count = 17;
|
||||||
|
ptr = malloc(max_count * sizeof(*ptr));
|
||||||
|
if (ptr == NULL) {
|
||||||
|
printf ("sl_loop: failed to allocate %u bytes of memory\n",
|
||||||
|
max_count * sizeof(*ptr));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char *p;
|
char *buf;
|
||||||
char **a = ptr;
|
unsigned count;
|
||||||
SL_cmd *c;
|
SL_cmd *c;
|
||||||
|
|
||||||
buf = readline(prompt);
|
buf = readline(prompt);
|
||||||
if(buf == NULL)
|
if(buf == NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
p = buf;
|
|
||||||
count = 0;
|
|
||||||
if(*buf)
|
if(*buf)
|
||||||
add_history(buf);
|
add_history(buf);
|
||||||
for (;;) {
|
count = 0;
|
||||||
while (*p == ' ' || *p == '\t')
|
{
|
||||||
p++;
|
char *foo;
|
||||||
if (*p == '\0')
|
char *p;
|
||||||
break;
|
|
||||||
*a++ = p;
|
for(p = strtok_r (buf, " \t", &foo);
|
||||||
++count;
|
p;
|
||||||
while (*p != '\0' && *p != ' ' && *p != '\t')
|
p = strtok_r (NULL, " \t", &foo)) {
|
||||||
p++;
|
if(count == max_count) {
|
||||||
if (*p == '\0')
|
max_count *= 2;
|
||||||
break;
|
ptr = realloc (ptr, max_count * sizeof(*ptr));
|
||||||
*p++ = '\0';
|
if (ptr == NULL) {
|
||||||
|
printf ("sl_loop: failed to allocate %u "
|
||||||
|
"bytes of memory\n",
|
||||||
|
max_count * sizeof(*ptr));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ptr[count++] = p;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
c = sl_match (cmds, ptr[0], 0);
|
c = sl_match (cmds, ptr[0], 0);
|
||||||
@@ -156,6 +169,6 @@ sl_loop (SL_cmd *cmds, char *prompt)
|
|||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
free (ptr);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user