Remove old buffer when renaming rather than complaining, GitHub issue
[tmux-openbsd.git] / cmd-list.c
blob82ffe55c02fdb251fc3f7c75e86a31e304312274
1 /* $OpenBSD$ */
3 /*
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>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
26 static u_int cmd_list_next_group = 1;
28 struct cmd_list *
29 cmd_list_new(void)
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);
37 return (cmdlist);
40 void
41 cmd_list_append(struct cmd_list *cmdlist, struct cmd *cmd)
43 cmd->group = cmdlist->group;
44 TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry);
47 void
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++;
59 void
60 cmd_list_free(struct cmd_list *cmdlist)
62 struct cmd *cmd, *cmd1;
64 if (--cmdlist->references != 0)
65 return;
67 TAILQ_FOREACH_SAFE(cmd, &cmdlist->list, qentry, cmd1) {
68 TAILQ_REMOVE(&cmdlist->list, cmd, qentry);
69 cmd_free(cmd);
72 free(cmdlist);
75 char *
76 cmd_list_print(struct cmd_list *cmdlist, int escaped)
78 struct cmd *cmd;
79 char *buf, *this;
80 size_t len;
82 len = 1;
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) {
93 if (escaped)
94 strlcat(buf, " \\; ", len);
95 else
96 strlcat(buf, " ; ", len);
99 free(this);
102 return (buf);