4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
26 static u_int cmd_list_next_group
= 1;
31 struct cmd_list
*cmdlist
;
33 cmdlist
= xcalloc(1, sizeof *cmdlist
);
34 cmdlist
->references
= 1;
35 cmdlist
->group
= cmd_list_next_group
++;
36 TAILQ_INIT(&cmdlist
->list
);
41 cmd_list_append(struct cmd_list
*cmdlist
, struct cmd
*cmd
)
43 cmd
->group
= cmdlist
->group
;
44 TAILQ_INSERT_TAIL(&cmdlist
->list
, cmd
, qentry
);
48 cmd_list_move(struct cmd_list
*cmdlist
, struct cmd_list
*from
)
50 struct cmd
*cmd
, *cmd1
;
52 TAILQ_FOREACH_SAFE(cmd
, &from
->list
, qentry
, cmd1
) {
53 TAILQ_REMOVE(&from
->list
, cmd
, qentry
);
54 TAILQ_INSERT_TAIL(&cmdlist
->list
, cmd
, qentry
);
56 cmdlist
->group
= cmd_list_next_group
++;
60 cmd_list_free(struct cmd_list
*cmdlist
)
62 struct cmd
*cmd
, *cmd1
;
64 if (--cmdlist
->references
!= 0)
67 TAILQ_FOREACH_SAFE(cmd
, &cmdlist
->list
, qentry
, cmd1
) {
68 TAILQ_REMOVE(&cmdlist
->list
, cmd
, qentry
);
76 cmd_list_print(struct cmd_list
*cmdlist
, int escaped
)
83 buf
= xcalloc(1, len
);
85 TAILQ_FOREACH(cmd
, &cmdlist
->list
, qentry
) {
86 this = cmd_print(cmd
);
88 len
+= strlen(this) + 4;
89 buf
= xrealloc(buf
, len
);
91 strlcat(buf
, this, len
);
92 if (TAILQ_NEXT(cmd
, qentry
) != NULL
) {
94 strlcat(buf
, " \\; ", len
);
96 strlcat(buf
, " ; ", len
);