4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
27 cmd_list_parse(int argc
, char **argv
, const char* file
, u_int line
,
30 struct cmd_list
*cmdlist
;
33 size_t arglen
, new_argc
;
34 char **copy_argv
, **new_argv
;
36 copy_argv
= cmd_copy_argv(argc
, argv
);
38 cmdlist
= xcalloc(1, sizeof *cmdlist
);
39 cmdlist
->references
= 1;
40 TAILQ_INIT(&cmdlist
->list
);
43 for (i
= 0; i
< argc
; i
++) {
44 arglen
= strlen(copy_argv
[i
]);
45 if (arglen
== 0 || copy_argv
[i
][arglen
- 1] != ';')
47 copy_argv
[i
][arglen
- 1] = '\0';
49 if (arglen
> 1 && copy_argv
[i
][arglen
- 2] == '\\') {
50 copy_argv
[i
][arglen
- 2] = ';';
54 new_argc
= i
- lastsplit
;
55 new_argv
= copy_argv
+ lastsplit
;
59 cmd
= cmd_parse(new_argc
, new_argv
, file
, line
, cause
);
62 TAILQ_INSERT_TAIL(&cmdlist
->list
, cmd
, qentry
);
67 if (lastsplit
!= argc
) {
68 cmd
= cmd_parse(argc
- lastsplit
, copy_argv
+ lastsplit
,
72 TAILQ_INSERT_TAIL(&cmdlist
->list
, cmd
, qentry
);
75 cmd_free_argv(argc
, copy_argv
);
79 cmd_list_free(cmdlist
);
80 cmd_free_argv(argc
, copy_argv
);
85 cmd_list_free(struct cmd_list
*cmdlist
)
87 struct cmd
*cmd
, *cmd1
;
89 if (--cmdlist
->references
!= 0)
92 TAILQ_FOREACH_SAFE(cmd
, &cmdlist
->list
, qentry
, cmd1
) {
93 TAILQ_REMOVE(&cmdlist
->list
, cmd
, qentry
);
103 cmd_list_print(struct cmd_list
*cmdlist
, char *buf
, size_t len
)
109 TAILQ_FOREACH(cmd
, &cmdlist
->list
, qentry
) {
112 off
+= cmd_print(cmd
, buf
+ off
, len
- off
);
115 if (TAILQ_NEXT(cmd
, qentry
) != NULL
)
116 off
+= xsnprintf(buf
+ off
, len
- off
, " ; ");