Coverage Report

Created: 2026-06-12 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-show-prompt-history.c
Line
Count
Source
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 prompt-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 prompt-type]",
47
48
  .flags = CMD_AFTERHOOK,
49
  .exec = cmd_show_prompt_history_exec
50
};
51
52
static enum cmd_retval
53
cmd_show_prompt_history_exec(struct cmd *self, struct cmdq_item *item)
54
0
{
55
0
  struct args   *args = cmd_get_args(self);
56
0
  const char    *typestr = args_get(args, 'T');
57
0
  enum prompt_type   type;
58
0
  u_int      tidx, hidx;
59
60
0
  if (cmd_get_entry(self) == &cmd_clear_prompt_history_entry) {
61
0
    if (typestr == NULL) {
62
0
      for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
63
0
        for (hidx = 0; hidx < status_prompt_hsize[tidx];
64
0
             hidx++)
65
0
          free(status_prompt_hlist[tidx][hidx]);
66
0
        free(status_prompt_hlist[tidx]);
67
0
        status_prompt_hlist[tidx] = NULL;
68
0
        status_prompt_hsize[tidx] = 0;
69
0
      }
70
0
    } else {
71
0
      type = status_prompt_type(typestr);
72
0
      if (type == PROMPT_TYPE_INVALID) {
73
0
        cmdq_error(item, "invalid type: %s", typestr);
74
0
        return (CMD_RETURN_ERROR);
75
0
      }
76
0
      for (hidx = 0; hidx < status_prompt_hsize[type]; hidx++)
77
0
        free(status_prompt_hlist[type][hidx]);
78
0
      free(status_prompt_hlist[type]);
79
0
      status_prompt_hlist[type] = NULL;
80
0
      status_prompt_hsize[type] = 0;
81
0
    }
82
83
0
    return (CMD_RETURN_NORMAL);
84
0
  }
85
86
0
  if (typestr == NULL) {
87
0
    for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
88
0
      cmdq_print(item, "History for %s:\n",
89
0
          status_prompt_type_string(tidx));
90
0
      for (hidx = 0; hidx < status_prompt_hsize[tidx];
91
0
          hidx++) {
92
0
        cmdq_print(item, "%d: %s", hidx + 1,
93
0
            status_prompt_hlist[tidx][hidx]);
94
0
      }
95
0
      cmdq_print(item, "%s", "");
96
0
    }
97
0
  } else {
98
0
    type = status_prompt_type(typestr);
99
0
    if (type == PROMPT_TYPE_INVALID) {
100
0
      cmdq_error(item, "invalid type: %s", typestr);
101
0
      return (CMD_RETURN_ERROR);
102
0
    }
103
0
    cmdq_print(item, "History for %s:\n",
104
0
        status_prompt_type_string(type));
105
0
    for (hidx = 0; hidx < status_prompt_hsize[type]; hidx++) {
106
0
      cmdq_print(item, "%d: %s", hidx + 1,
107
0
          status_prompt_hlist[type][hidx]);
108
0
    }
109
0
    cmdq_print(item, "%s", "");
110
0
  }
111
112
0
  return (CMD_RETURN_NORMAL);
113
0
}