4 * Command-line splitter function.
5 * Lon Hohberger <lon@metamorphism.com>
12 typedef enum _command_state
{
20 * Break up a command string into argv values. Rudimentary.
21 * Destroys the value of cmdline (inserts 0s in order to keep argv
22 * pointers 0-terminated in typical C-style)
25 command_split(char *cmdline
, int *argc
, char **argv
, int max
)
27 char *cmd_copy
= NULL
;
28 command_state_t state
= 0;
29 int offset
= 0, copy_offset
= 0, ret
= 1;
31 if (max
<= 0 || argv
== NULL
|| argc
== NULL
) {
36 cmd_copy
= strdup(cmdline
);
40 memset(cmdline
, 0, strlen(cmdline
)+1);
43 while (cmd_copy
[copy_offset
]) {
44 switch(cmd_copy
[copy_offset
]) {
49 if (state
& (STATE_LITERAL
| STATE_QUOTE
))
51 if (state
& STATE_COPY
) {
53 cmdline
[offset
++] = 0;
62 if (state
& STATE_LITERAL
)
64 state
|= STATE_LITERAL
;
69 if (state
& STATE_LITERAL
)
71 if (state
& STATE_QUOTE
) {
72 state
&= ~STATE_QUOTE
;
80 if (!(state
& STATE_COPY
)) {
84 argv
[(*argc
)++] = &cmdline
[offset
];
87 state
&= ~STATE_LITERAL
;
89 cmdline
[offset
] = cmd_copy
[copy_offset
];