Coverage Report

Created: 2025-11-19 06:37

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
        free(status_prompt_hlist[tidx]);
64
0
        status_prompt_hlist[tidx] = NULL;
65
0
        status_prompt_hsize[tidx] = 0;
66
0
      }
67
0
    } else {
68
0
      type = status_prompt_type(typestr);
69
0
      if (type == PROMPT_TYPE_INVALID) {
70
0
        cmdq_error(item, "invalid type: %s", typestr);
71
0
        return (CMD_RETURN_ERROR);
72
0
      }
73
0
      free(status_prompt_hlist[type]);
74
0
      status_prompt_hlist[type] = NULL;
75
0
      status_prompt_hsize[type] = 0;
76
0
    }
77
78
0
    return (CMD_RETURN_NORMAL);
79
0
  }
80
81
0
  if (typestr == NULL) {
82
0
    for (tidx = 0; tidx < PROMPT_NTYPES; tidx++) {
83
0
      cmdq_print(item, "History for %s:\n",
84
0
          status_prompt_type_string(tidx));
85
0
      for (hidx = 0; hidx < status_prompt_hsize[tidx];
86
0
          hidx++) {
87
0
        cmdq_print(item, "%d: %s", hidx + 1,
88
0
            status_prompt_hlist[tidx][hidx]);
89
0
      }
90
0
      cmdq_print(item, "%s", "");
91
0
    }
92
0
  } else {
93
0
    type = status_prompt_type(typestr);
94
0
    if (type == PROMPT_TYPE_INVALID) {
95
0
      cmdq_error(item, "invalid type: %s", typestr);
96
0
      return (CMD_RETURN_ERROR);
97
0
    }
98
0
    cmdq_print(item, "History for %s:\n",
99
0
        status_prompt_type_string(type));
100
0
    for (hidx = 0; hidx < status_prompt_hsize[type]; hidx++) {
101
0
      cmdq_print(item, "%d: %s", hidx + 1,
102
0
          status_prompt_hlist[type][hidx]);
103
0
    }
104
0
    cmdq_print(item, "%s", "");
105
0
  }
106
107
0
  return (CMD_RETURN_NORMAL);
108
0
}