4 * Command-line splitter function.
5 * Lon Hohberger <lon@metamorphism.com>
11 typedef enum _command_state
{
19 * Break up a command string into argv values. Rudimentary.
20 * Destroys the value of cmdline (inserts 0s in order to keep argv
21 * pointers 0-terminated in typical C-style)
24 command_split(char *cmdline
, int *argc
, char **argv
, int max
)
26 char *cmd_copy
= NULL
;
27 command_state_t state
= 0;
28 int offset
= 0, copy_offset
= 0, ret
= 1;
30 if (max
<= 0 || argv
== NULL
|| argc
== NULL
) {
35 cmd_copy
= strdup(cmdline
);
39 memset(cmdline
, 0, strlen(cmdline
)+1);
42 while (cmd_copy
[copy_offset
]) {
43 switch(cmd_copy
[copy_offset
]) {
48 if (state
& (STATE_LITERAL
| STATE_QUOTE
))
50 if (state
& STATE_COPY
) {
52 cmdline
[offset
++] = 0;
61 if (state
& STATE_LITERAL
)
63 state
|= STATE_LITERAL
;
68 if (state
& STATE_LITERAL
)
70 if (state
& STATE_QUOTE
) {
71 state
&= ~STATE_QUOTE
;
79 if (!(state
& STATE_COPY
)) {
83 argv
[(*argc
)++] = &cmdline
[offset
];
86 state
&= ~STATE_LITERAL
;
88 cmdline
[offset
] = cmd_copy
[copy_offset
];