1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2021 Anindya Mukherjee <anindya49@hotmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include "tmux.h"
20 
21 #include <stdlib.h>
22 
23 /*
24  * Show or clear prompt history.
25  */
26 
27 static enum cmd_retval        cmd_show_prompt_history_exec(struct cmd *,
28                                   struct cmdq_item *);
29 
30 const struct cmd_entry cmd_show_prompt_history_entry = {
31           .name = "show-prompt-history",
32           .alias = "showphist",
33 
34           .args = { "T:", 0, 0, NULL },
35           .usage = "[-T type]",
36 
37           .flags = CMD_AFTERHOOK,
38           .exec = cmd_show_prompt_history_exec
39 };
40 
41 const struct cmd_entry cmd_clear_prompt_history_entry = {
42           .name = "clear-prompt-history",
43           .alias = "clearphist",
44 
45           .args = { "T:", 0, 0, NULL },
46           .usage = "[-T type]",
47 
48           .flags = CMD_AFTERHOOK,
49           .exec = cmd_show_prompt_history_exec
50 };
51 
52 static enum cmd_retval
cmd_show_prompt_history_exec(struct cmd * self,struct cmdq_item * item)53 cmd_show_prompt_history_exec(struct cmd *self, struct cmdq_item *item)
54 {
55           struct args                   *args = cmd_get_args(self);
56           const char                    *typestr = args_get(args, 'T');
57           enum prompt_type     type;
58           u_int                          tidx, hidx;
59 
60           if (cmd_get_entry(self) == &cmd_clear_prompt_history_entry) {
61                     if (typestr == NULL) {
62                               for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
63                                         free(status_prompt_hlist[tidx]);
64                                         status_prompt_hlist[tidx] = NULL;
65                                         status_prompt_hsize[tidx] = 0;
66                               }
67                     } else {
68                               type = status_prompt_type(typestr);
69                               if (type == PROMPT_TYPE_INVALID) {
70                                         cmdq_error(item, "invalid type: %s", typestr);
71                                         return (CMD_RETURN_ERROR);
72                               }
73                               free(status_prompt_hlist[type]);
74                               status_prompt_hlist[type] = NULL;
75                               status_prompt_hsize[type] = 0;
76                     }
77 
78                     return (CMD_RETURN_NORMAL);
79           }
80 
81           if (typestr == NULL) {
82                     for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
83                               cmdq_print(item, "History for %s:\n",
84                                   status_prompt_type_string(tidx));
85                               for (hidx = 0; hidx < status_prompt_hsize[tidx];
86                                   hidx++) {
87                                         cmdq_print(item, "%d: %s", hidx + 1,
88                                             status_prompt_hlist[tidx][hidx]);
89                               }
90                               cmdq_print(item, "%s", "");
91                     }
92           } else {
93                     type = status_prompt_type(typestr);
94                     if (type == PROMPT_TYPE_INVALID) {
95                               cmdq_error(item, "invalid type: %s", typestr);
96                               return (CMD_RETURN_ERROR);
97                     }
98                     cmdq_print(item, "History for %s:\n",
99                         status_prompt_type_string(type));
100                     for (hidx = 0; hidx < status_prompt_hsize[type]; hidx++) {
101                               cmdq_print(item, "%d: %s", hidx + 1,
102                                   status_prompt_hlist[type][hidx]);
103                     }
104                     cmdq_print(item, "%s", "");
105           }
106 
107           return (CMD_RETURN_NORMAL);
108 }
109