Coverage Report

Created: 2023-11-27 06:25

/src/tmux/cmd-display-menu.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.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 <sys/types.h>
20
21
#include <stdlib.h>
22
#include <string.h>
23
24
#include "tmux.h"
25
26
/*
27
 * Display a menu on a client.
28
 */
29
30
static enum args_parse_type cmd_display_menu_args_parse(struct args *,
31
            u_int, char **);
32
static enum cmd_retval    cmd_display_menu_exec(struct cmd *,
33
            struct cmdq_item *);
34
static enum cmd_retval    cmd_display_popup_exec(struct cmd *,
35
            struct cmdq_item *);
36
37
const struct cmd_entry cmd_display_menu_entry = {
38
  .name = "display-menu",
39
  .alias = "menu",
40
41
  .args = { "b:c:C:H:s:S:Ot:T:x:y:", 1, -1, cmd_display_menu_args_parse },
42
  .usage = "[-O] [-b border-lines] [-c target-client] "
43
     "[-C starting-choice] [-H selected-style] [-s style] "
44
     "[-S border-style] " CMD_TARGET_PANE_USAGE "[-T title] "
45
     "[-x position] [-y position] name key command ...",
46
47
  .target = { 't', CMD_FIND_PANE, 0 },
48
49
  .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
50
  .exec = cmd_display_menu_exec
51
};
52
53
const struct cmd_entry cmd_display_popup_entry = {
54
  .name = "display-popup",
55
  .alias = "popup",
56
57
  .args = { "Bb:Cc:d:e:Eh:s:S:t:T:w:x:y:", 0, -1, NULL },
58
  .usage = "[-BCE] [-b border-lines] [-c target-client] "
59
     "[-d start-directory] [-e environment] [-h height] "
60
     "[-s style] [-S border-style] " CMD_TARGET_PANE_USAGE
61
     "[-T title] [-w width] [-x position] [-y position] "
62
     "[shell-command]",
63
64
  .target = { 't', CMD_FIND_PANE, 0 },
65
66
  .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
67
  .exec = cmd_display_popup_exec
68
};
69
70
static enum args_parse_type
71
cmd_display_menu_args_parse(struct args *args, u_int idx, __unused char **cause)
72
0
{
73
0
  u_int      i = 0;
74
0
  enum args_parse_type   type = ARGS_PARSE_STRING;
75
76
0
  for (;;) {
77
0
    type = ARGS_PARSE_STRING;
78
0
    if (i == idx)
79
0
      break;
80
0
    if (*args_string(args, i++) == '\0')
81
0
      continue;
82
83
0
    type = ARGS_PARSE_STRING;
84
0
    if (i++ == idx)
85
0
      break;
86
87
0
    type = ARGS_PARSE_COMMANDS_OR_STRING;
88
0
    if (i++ == idx)
89
0
      break;
90
0
  }
91
0
  return (type);
92
0
}
93
94
static int
95
cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
96
    struct args *args, u_int *px, u_int *py, u_int w, u_int h)
97
0
{
98
0
  struct tty    *tty = &tc->tty;
99
0
  struct cmd_find_state *target = cmdq_get_target(item);
100
0
  struct key_event  *event = cmdq_get_event(item);
101
0
  struct session    *s = tc->session;
102
0
  struct winlink    *wl = target->wl;
103
0
  struct window_pane  *wp = target->wp;
104
0
  struct style_ranges *ranges = NULL;
105
0
  struct style_range  *sr = NULL;
106
0
  const char    *xp, *yp;
107
0
  char      *p;
108
0
  int      top;
109
0
  u_int      line, ox, oy, sx, sy, lines, position;
110
0
  long       n;
111
0
  struct format_tree  *ft;
112
113
  /*
114
   * Work out the position from the -x and -y arguments. This is the
115
   * bottom-left position.
116
   */
117
118
  /* If the popup is too big, stop now. */
119
0
  if (w > tty->sx || h > tty->sy)
120
0
    return (0);
121
122
  /* Create format with mouse position if any. */
123
0
  ft = format_create_from_target(item);
124
0
  if (event->m.valid) {
125
0
    format_add(ft, "popup_mouse_x", "%u", event->m.x);
126
0
    format_add(ft, "popup_mouse_y", "%u", event->m.y);
127
0
  }
128
129
  /*
130
   * If there are any status lines, add this window position and the
131
   * status line position.
132
   */
133
0
  top = status_at_line(tc);
134
0
  if (top != -1) {
135
0
    lines = status_line_size(tc);
136
0
    if (top == 0)
137
0
      top = lines;
138
0
    else
139
0
      top = 0;
140
0
    position = options_get_number(s->options, "status-position");
141
142
0
    for (line = 0; line < lines; line++) {
143
0
      ranges = &tc->status.entries[line].ranges;
144
0
      TAILQ_FOREACH(sr, ranges, entry) {
145
0
        if (sr->type != STYLE_RANGE_WINDOW)
146
0
          continue;
147
0
        if (sr->argument == (u_int)wl->idx)
148
0
          break;
149
0
      }
150
0
      if (sr != NULL)
151
0
        break;
152
0
    }
153
154
0
    if (sr != NULL) {
155
0
      format_add(ft, "popup_window_status_line_x", "%u",
156
0
          sr->start);
157
0
      if (position == 0) {
158
0
        format_add(ft, "popup_window_status_line_y",
159
0
            "%u", line + 1 + h);
160
0
      } else {
161
0
        format_add(ft, "popup_window_status_line_y",
162
0
            "%u", tty->sy - lines + line);
163
0
      }
164
0
    }
165
166
0
    if (position == 0)
167
0
      format_add(ft, "popup_status_line_y", "%u", lines + h);
168
0
    else {
169
0
      format_add(ft, "popup_status_line_y", "%u",
170
0
          tty->sy - lines);
171
0
    }
172
0
  } else
173
0
    top = 0;
174
175
  /* Popup width and height. */
176
0
  format_add(ft, "popup_width", "%u", w);
177
0
  format_add(ft, "popup_height", "%u", h);
178
179
  /* Position so popup is in the centre. */
180
0
  n = (long)(tty->sx - 1) / 2 - w / 2;
181
0
  if (n < 0)
182
0
    format_add(ft, "popup_centre_x", "%u", 0);
183
0
  else
184
0
    format_add(ft, "popup_centre_x", "%ld", n);
185
0
  n = (tty->sy - 1) / 2 + h / 2;
186
0
  if (n >= tty->sy)
187
0
    format_add(ft, "popup_centre_y", "%u", tty->sy - h);
188
0
  else
189
0
    format_add(ft, "popup_centre_y", "%ld", n);
190
191
  /* Position of popup relative to mouse. */
192
0
  if (event->m.valid) {
193
0
    n = (long)event->m.x - w / 2;
194
0
    if (n < 0)
195
0
      format_add(ft, "popup_mouse_centre_x", "%u", 0);
196
0
    else
197
0
      format_add(ft, "popup_mouse_centre_x", "%ld", n);
198
0
    n = event->m.y - h / 2;
199
0
    if (n + h >= tty->sy) {
200
0
      format_add(ft, "popup_mouse_centre_y", "%u",
201
0
          tty->sy - h);
202
0
    } else
203
0
      format_add(ft, "popup_mouse_centre_y", "%ld", n);
204
0
    n = (long)event->m.y + h;
205
0
    if (n >= tty->sy)
206
0
      format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
207
0
    else
208
0
      format_add(ft, "popup_mouse_top", "%ld", n);
209
0
    n = event->m.y - h;
210
0
    if (n < 0)
211
0
      format_add(ft, "popup_mouse_bottom", "%u", 0);
212
0
    else
213
0
      format_add(ft, "popup_mouse_bottom", "%ld", n);
214
0
  }
215
216
  /* Position in pane. */
217
0
  tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
218
0
  n = top + wp->yoff - oy + h;
219
0
  if (n >= tty->sy)
220
0
    format_add(ft, "popup_pane_top", "%u", tty->sy - h);
221
0
  else
222
0
    format_add(ft, "popup_pane_top", "%ld", n);
223
0
  format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
224
0
  format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
225
0
  n = (long)wp->xoff + wp->sx - ox - w;
226
0
  if (n < 0)
227
0
    format_add(ft, "popup_pane_right", "%u", 0);
228
0
  else
229
0
    format_add(ft, "popup_pane_right", "%ld", n);
230
231
  /* Expand horizontal position. */
232
0
  xp = args_get(args, 'x');
233
0
  if (xp == NULL || strcmp(xp, "C") == 0)
234
0
    xp = "#{popup_centre_x}";
235
0
  else if (strcmp(xp, "R") == 0)
236
0
    xp = "#{popup_pane_right}";
237
0
  else if (strcmp(xp, "P") == 0)
238
0
    xp = "#{popup_pane_left}";
239
0
  else if (strcmp(xp, "M") == 0)
240
0
    xp = "#{popup_mouse_centre_x}";
241
0
  else if (strcmp(xp, "W") == 0)
242
0
    xp = "#{popup_window_status_line_x}";
243
0
  p = format_expand(ft, xp);
244
0
  n = strtol(p, NULL, 10);
245
0
  if (n + w >= tty->sx)
246
0
    n = tty->sx - w;
247
0
  else if (n < 0)
248
0
    n = 0;
249
0
  *px = n;
250
0
  log_debug("%s: -x: %s = %s = %u (-w %u)", __func__, xp, p, *px, w);
251
0
  free(p);
252
253
  /* Expand vertical position  */
254
0
  yp = args_get(args, 'y');
255
0
  if (yp == NULL || strcmp(yp, "C") == 0)
256
0
    yp = "#{popup_centre_y}";
257
0
  else if (strcmp(yp, "P") == 0)
258
0
    yp = "#{popup_pane_bottom}";
259
0
  else if (strcmp(yp, "M") == 0)
260
0
    yp = "#{popup_mouse_top}";
261
0
  else if (strcmp(yp, "S") == 0)
262
0
    yp = "#{popup_status_line_y}";
263
0
  else if (strcmp(yp, "W") == 0)
264
0
    yp = "#{popup_window_status_line_y}";
265
0
  p = format_expand(ft, yp);
266
0
  n = strtol(p, NULL, 10);
267
0
  if (n < h)
268
0
    n = 0;
269
0
  else
270
0
    n -= h;
271
0
  if (n + h >= tty->sy)
272
0
    n = tty->sy - h;
273
0
  else if (n < 0)
274
0
    n = 0;
275
0
  *py = n;
276
0
  log_debug("%s: -y: %s = %s = %u (-h %u)", __func__, yp, p, *py, h);
277
0
  free(p);
278
279
0
  format_free(ft);
280
0
  return (1);
281
0
}
282
283
static enum cmd_retval
284
cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
285
0
{
286
0
  struct args   *args = cmd_get_args(self);
287
0
  struct cmd_find_state *target = cmdq_get_target(item);
288
0
  struct key_event  *event = cmdq_get_event(item);
289
0
  struct client   *tc = cmdq_get_target_client(item);
290
0
  struct menu   *menu = NULL;
291
0
  struct menu_item   menu_item;
292
0
  const char    *key, *name, *value;
293
0
  const char    *style = args_get(args, 's');
294
0
  const char    *border_style = args_get(args, 'S');
295
0
  const char    *selected_style = args_get(args, 'H');
296
0
  enum box_lines     lines = BOX_LINES_DEFAULT;
297
0
  char      *title, *cause;
298
0
  int      flags = 0, starting_choice = 0;
299
0
  u_int      px, py, i, count = args_count(args);
300
0
  struct options    *o = target->s->curw->window->options;
301
0
  struct options_entry  *oe;
302
303
304
0
  if (tc->overlay_draw != NULL)
305
0
    return (CMD_RETURN_NORMAL);
306
307
0
  if (args_has(args, 'C')) {
308
0
    if (strcmp(args_get(args, 'C'), "-") == 0)
309
0
      starting_choice = -1;
310
0
    else {
311
0
      starting_choice = args_strtonum(args, 'C', 0, UINT_MAX,
312
0
          &cause);
313
0
      if (cause != NULL) {
314
0
        cmdq_error(item, "starting choice %s", cause);
315
0
        free(cause);
316
0
        return (CMD_RETURN_ERROR);
317
0
      }
318
0
    }
319
0
  }
320
321
0
  if (args_has(args, 'T'))
322
0
    title = format_single_from_target(item, args_get(args, 'T'));
323
0
  else
324
0
    title = xstrdup("");
325
0
  menu = menu_create(title);
326
0
  free(title);
327
328
0
  for (i = 0; i != count; /* nothing */) {
329
0
    name = args_string(args, i++);
330
0
    if (*name == '\0') {
331
0
      menu_add_item(menu, NULL, item, tc, target);
332
0
      continue;
333
0
    }
334
335
0
    if (count - i < 2) {
336
0
      cmdq_error(item, "not enough arguments");
337
0
      menu_free(menu);
338
0
      return (CMD_RETURN_ERROR);
339
0
    }
340
0
    key = args_string(args, i++);
341
342
0
    menu_item.name = name;
343
0
    menu_item.key = key_string_lookup_string(key);
344
0
    menu_item.command = args_string(args, i++);
345
346
0
    menu_add_item(menu, &menu_item, item, tc, target);
347
0
  }
348
0
  if (menu == NULL) {
349
0
    cmdq_error(item, "invalid menu arguments");
350
0
    return (CMD_RETURN_ERROR);
351
0
  }
352
0
  if (menu->count == 0) {
353
0
    menu_free(menu);
354
0
    return (CMD_RETURN_NORMAL);
355
0
  }
356
0
  if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
357
0
      menu->width + 4, menu->count + 2)) {
358
0
    menu_free(menu);
359
0
    return (CMD_RETURN_NORMAL);
360
0
  }
361
362
0
  value = args_get(args, 'b');
363
0
  if (value != NULL) {
364
0
    oe = options_get(o, "menu-border-lines");
365
0
    lines = options_find_choice(options_table_entry(oe), value,
366
0
        &cause);
367
0
    if (lines == -1) {
368
0
      cmdq_error(item, "menu-border-lines %s", cause);
369
0
      free(cause);
370
0
      return (CMD_RETURN_ERROR);
371
0
    }
372
0
  }
373
374
0
  if (args_has(args, 'O'))
375
0
    flags |= MENU_STAYOPEN;
376
0
  if (!event->m.valid)
377
0
    flags |= MENU_NOMOUSE;
378
0
  if (menu_display(menu, flags, starting_choice, item, px, py, tc, lines,
379
0
      style, selected_style, border_style, target, NULL, NULL) != 0)
380
0
    return (CMD_RETURN_NORMAL);
381
0
  return (CMD_RETURN_WAIT);
382
0
}
383
384
static enum cmd_retval
385
cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
386
0
{
387
0
  struct args   *args = cmd_get_args(self);
388
0
  struct cmd_find_state *target = cmdq_get_target(item);
389
0
  struct session    *s = target->s;
390
0
  struct client   *tc = cmdq_get_target_client(item);
391
0
  struct tty    *tty = &tc->tty;
392
0
  const char    *value, *shell, *shellcmd = NULL;
393
0
  const char    *style = args_get(args, 's');
394
0
  const char    *border_style = args_get(args, 'S');
395
0
  char      *cwd, *cause = NULL, **argv = NULL, *title;
396
0
  int      flags = 0, argc = 0;
397
0
  enum box_lines     lines = BOX_LINES_DEFAULT;
398
0
  u_int      px, py, w, h, count = args_count(args);
399
0
  struct args_value *av;
400
0
  struct environ    *env = NULL;
401
0
  struct options    *o = s->curw->window->options;
402
0
  struct options_entry  *oe;
403
404
0
  if (args_has(args, 'C')) {
405
0
    server_client_clear_overlay(tc);
406
0
    return (CMD_RETURN_NORMAL);
407
0
  }
408
0
  if (tc->overlay_draw != NULL)
409
0
    return (CMD_RETURN_NORMAL);
410
411
0
  h = tty->sy / 2;
412
0
  if (args_has(args, 'h')) {
413
0
    h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause);
414
0
    if (cause != NULL) {
415
0
      cmdq_error(item, "height %s", cause);
416
0
      free(cause);
417
0
      return (CMD_RETURN_ERROR);
418
0
    }
419
0
  }
420
421
0
  w = tty->sx / 2;
422
0
  if (args_has(args, 'w')) {
423
0
    w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause);
424
0
    if (cause != NULL) {
425
0
      cmdq_error(item, "width %s", cause);
426
0
      free(cause);
427
0
      return (CMD_RETURN_ERROR);
428
0
    }
429
0
  }
430
431
0
  if (w > tty->sx)
432
0
    w = tty->sx;
433
0
  if (h > tty->sy)
434
0
    h = tty->sy;
435
0
  if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
436
0
    return (CMD_RETURN_NORMAL);
437
438
0
  value = args_get(args, 'b');
439
0
  if (args_has(args, 'B'))
440
0
    lines = BOX_LINES_NONE;
441
0
  else if (value != NULL) {
442
0
    oe = options_get(o, "popup-border-lines");
443
0
    lines = options_find_choice(options_table_entry(oe), value,
444
0
        &cause);
445
0
    if (cause != NULL) {
446
0
      cmdq_error(item, "popup-border-lines %s", cause);
447
0
      free(cause);
448
0
      return (CMD_RETURN_ERROR);
449
0
    }
450
0
  }
451
452
0
  value = args_get(args, 'd');
453
0
  if (value != NULL)
454
0
    cwd = format_single_from_target(item, value);
455
0
  else
456
0
    cwd = xstrdup(server_client_get_cwd(tc, s));
457
0
  if (count == 0)
458
0
    shellcmd = options_get_string(s->options, "default-command");
459
0
  else if (count == 1)
460
0
    shellcmd = args_string(args, 0);
461
0
  if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
462
0
    shellcmd = NULL;
463
0
    shell = options_get_string(s->options, "default-shell");
464
0
    if (!checkshell(shell))
465
0
      shell = _PATH_BSHELL;
466
0
    cmd_append_argv(&argc, &argv, shell);
467
0
  } else
468
0
    args_to_vector(args, &argc, &argv);
469
470
0
  if (args_has(args, 'e') >= 1) {
471
0
    env = environ_create();
472
0
    av = args_first_value(args, 'e');
473
0
    while (av != NULL) {
474
0
      environ_put(env, av->string, 0);
475
0
      av = args_next_value(av);
476
0
    }
477
0
  }
478
479
0
  if (args_has(args, 'T'))
480
0
    title = format_single_from_target(item, args_get(args, 'T'));
481
0
  else
482
0
    title = xstrdup("");
483
0
  if (args_has(args, 'E') > 1)
484
0
    flags |= POPUP_CLOSEEXITZERO;
485
0
  else if (args_has(args, 'E'))
486
0
    flags |= POPUP_CLOSEEXIT;
487
0
  if (popup_display(flags, lines, item, px, py, w, h, env, shellcmd, argc,
488
0
      argv, cwd, title, tc, s, style, border_style, NULL, NULL) != 0) {
489
0
    cmd_free_argv(argc, argv);
490
0
    if (env != NULL)
491
0
      environ_free(env);
492
0
    free(cwd);
493
0
    free(title);
494
0
    return (CMD_RETURN_NORMAL);
495
0
  }
496
0
  if (env != NULL)
497
0
    environ_free(env);
498
0
  free(cwd);
499
0
  free(title);
500
0
  cmd_free_argv(argc, argv);
501
0
  return (CMD_RETURN_WAIT);
502
0
}