Coverage Report

Created: 2026-08-13 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-display-menu.c
Line
Count
Source
1
/* $OpenBSD: cmd-display-menu.c,v 1.52 2026/07/14 19:07:03 nicm Exp $ */
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:MOt:T:x:y:", 1, -1, cmd_display_menu_args_parse },
42
  .usage = "[-MO] [-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:kNs:S:t:T:w:x:y:", 0, -1, NULL },
58
  .usage = "[-BCEkN] [-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 [argument ...]]",
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_popup_pos(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
  /* Position of the previous menu, for -x/-y L. */
130
0
  format_add(ft, "popup_last_x", "%u", target->w->menu_last_px);
131
0
  format_add(ft, "popup_last_y", "%u", target->w->menu_last_py + h);
132
133
  /*
134
   * If there are any status lines, add this window position and the
135
   * status line position.
136
   */
137
0
  top = status_at_line(tc);
138
0
  if (top != -1) {
139
0
    lines = status_line_size(tc);
140
0
    if (top == 0)
141
0
      top = lines;
142
0
    else
143
0
      top = 0;
144
0
    position = options_get_number(s->options, "status-position");
145
146
0
    for (line = 0; line < lines; line++) {
147
0
      ranges = &tc->status.entries[line].ranges;
148
0
      TAILQ_FOREACH(sr, ranges, entry) {
149
0
        if (sr->type != STYLE_RANGE_WINDOW)
150
0
          continue;
151
0
        if (sr->argument == (u_int)wl->idx)
152
0
          break;
153
0
      }
154
0
      if (sr != NULL)
155
0
        break;
156
0
    }
157
158
0
    if (sr != NULL) {
159
0
      format_add(ft, "popup_window_status_line_x", "%u",
160
0
          sr->start);
161
0
      if (position == 0) {
162
0
        format_add(ft, "popup_window_status_line_y",
163
0
            "%u", line + 1 + h);
164
0
      } else {
165
0
        format_add(ft, "popup_window_status_line_y",
166
0
            "%u", tty->sy - lines + line);
167
0
      }
168
0
    }
169
170
0
    if (position == 0)
171
0
      format_add(ft, "popup_status_line_y", "%u", lines + h);
172
0
    else {
173
0
      format_add(ft, "popup_status_line_y", "%u",
174
0
          tty->sy - lines);
175
0
    }
176
0
  } else
177
0
    top = 0;
178
179
  /* Popup width and height. */
180
0
  format_add(ft, "popup_width", "%u", w);
181
0
  format_add(ft, "popup_height", "%u", h);
182
183
  /* Position so popup is in the centre. */
184
0
  n = (long)(tty->sx - 1) / 2 - w / 2;
185
0
  if (n < 0)
186
0
    format_add(ft, "popup_centre_x", "%u", 0);
187
0
  else
188
0
    format_add(ft, "popup_centre_x", "%ld", n);
189
0
  n = (tty->sy - 1) / 2 + h / 2;
190
0
  if (n >= tty->sy)
191
0
    format_add(ft, "popup_centre_y", "%u", tty->sy - h);
192
0
  else
193
0
    format_add(ft, "popup_centre_y", "%ld", n);
194
195
  /* Position of popup relative to mouse. */
196
0
  if (event->m.valid) {
197
0
    n = (long)event->m.x - w / 2;
198
0
    if (n < 0)
199
0
      format_add(ft, "popup_mouse_centre_x", "%u", 0);
200
0
    else
201
0
      format_add(ft, "popup_mouse_centre_x", "%ld", n);
202
0
    n = event->m.y - h / 2;
203
0
    if (n + h >= tty->sy) {
204
0
      format_add(ft, "popup_mouse_centre_y", "%u",
205
0
          tty->sy - h);
206
0
    } else
207
0
      format_add(ft, "popup_mouse_centre_y", "%ld", n);
208
0
    n = (long)event->m.y + h;
209
0
    if (n >= tty->sy)
210
0
      format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
211
0
    else
212
0
      format_add(ft, "popup_mouse_top", "%ld", n);
213
0
    n = event->m.y - h;
214
0
    if (n < 0)
215
0
      format_add(ft, "popup_mouse_bottom", "%u", 0);
216
0
    else
217
0
      format_add(ft, "popup_mouse_bottom", "%ld", n);
218
0
  }
219
220
  /* Position in pane. */
221
0
  tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
222
0
  n = top + wp->yoff - oy + h;
223
0
  if (n >= tty->sy)
224
0
    format_add(ft, "popup_pane_top", "%u", tty->sy - h);
225
0
  else
226
0
    format_add(ft, "popup_pane_top", "%ld", n);
227
0
  format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
228
0
  format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
229
0
  n = (long)wp->xoff + wp->sx - ox - w;
230
0
  if (n < 0)
231
0
    format_add(ft, "popup_pane_right", "%u", 0);
232
0
  else
233
0
    format_add(ft, "popup_pane_right", "%ld", n);
234
235
  /* Expand horizontal position. */
236
0
  xp = args_get(args, 'x');
237
0
  if (xp == NULL || strcmp(xp, "C") == 0)
238
0
    xp = "#{popup_centre_x}";
239
0
  else if (strcmp(xp, "R") == 0)
240
0
    xp = "#{popup_pane_right}";
241
0
  else if (strcmp(xp, "P") == 0)
242
0
    xp = "#{popup_pane_left}";
243
0
  else if (strcmp(xp, "M") == 0)
244
0
    xp = "#{popup_mouse_centre_x}";
245
0
  else if (strcmp(xp, "L") == 0)
246
0
    xp = "#{popup_last_x}";
247
0
  else if (strcmp(xp, "W") == 0)
248
0
    xp = "#{popup_window_status_line_x}";
249
0
  p = format_expand(ft, xp);
250
0
  n = strtol(p, NULL, 10);
251
0
  if (n + w >= tty->sx)
252
0
    n = tty->sx - w;
253
0
  else if (n < 0)
254
0
    n = 0;
255
0
  *px = n;
256
0
  log_debug("%s: -x: %s = %s = %u (-w %u)", __func__, xp, p, *px, w);
257
0
  free(p);
258
259
  /* Expand vertical position  */
260
0
  yp = args_get(args, 'y');
261
0
  if (yp == NULL || strcmp(yp, "C") == 0)
262
0
    yp = "#{popup_centre_y}";
263
0
  else if (strcmp(yp, "P") == 0)
264
0
    yp = "#{popup_pane_bottom}";
265
0
  else if (strcmp(yp, "M") == 0)
266
0
    yp = "#{popup_mouse_top}";
267
0
  else if (strcmp(yp, "L") == 0)
268
0
    yp = "#{popup_last_y}";
269
0
  else if (strcmp(yp, "S") == 0)
270
0
    yp = "#{popup_status_line_y}";
271
0
  else if (strcmp(yp, "W") == 0)
272
0
    yp = "#{popup_window_status_line_y}";
273
0
  p = format_expand(ft, yp);
274
0
  n = strtol(p, NULL, 10);
275
0
  if (n < h)
276
0
    n = 0;
277
0
  else
278
0
    n -= h;
279
0
  if (n + h >= tty->sy)
280
0
    n = tty->sy - h;
281
0
  else if (n < 0)
282
0
    n = 0;
283
0
  *py = n;
284
0
  log_debug("%s: -y: %s = %s = %u (-h %u)", __func__, yp, p, *py, h);
285
0
  free(p);
286
287
0
  format_free(ft);
288
0
  return (1);
289
0
}
290
291
static int
292
cmd_display_menu_get_menu_pos(struct client *tc, struct cmdq_item *item,
293
    struct args *args, u_int *px, u_int *py, u_int w, u_int h)
294
0
{
295
0
  struct cmd_find_state *target = cmdq_get_target(item);
296
0
  struct key_event  *event = cmdq_get_event(item);
297
0
  struct session    *s = tc->session;
298
0
  struct winlink    *wl = target->wl;
299
0
  struct window   *window = target->w;
300
0
  struct window_pane  *wp = target->wp;
301
0
  struct style_ranges *ranges = NULL;
302
0
  struct style_range  *sr = NULL;
303
0
  const char    *xp, *yp;
304
0
  char      *p;
305
0
  u_int      line, ox, oy, sx, sy, lines, position;
306
0
  long       n, max_x, max_y, mouse_x = 0, mouse_y = 0;
307
0
  struct format_tree  *ft;
308
309
0
  max_x = window->sx > w ? window->sx - w : 0;
310
0
  max_y = window->sy > h ? window->sy - h : 0;
311
312
0
  tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
313
314
0
  ft = format_create_from_target(item);
315
0
  if (event->m.valid) {
316
0
    mouse_x = event->m.x + ox;
317
0
    if (event->m.statusat == 0) {
318
0
      if (event->m.y >= event->m.statuslines)
319
0
        mouse_y = event->m.y - event->m.statuslines + oy;
320
0
      else
321
0
        mouse_y = oy;
322
0
    } else if (event->m.statusat > 0 &&
323
0
        event->m.y >= (u_int)event->m.statusat) {
324
0
      mouse_y = oy + sy - 1;
325
0
    } else
326
0
      mouse_y = event->m.y + oy;
327
0
    format_add(ft, "popup_mouse_x", "%ld", mouse_x);
328
0
    format_add(ft, "popup_mouse_y", "%ld", mouse_y);
329
0
  }
330
331
0
  format_add(ft, "popup_last_x", "%u", window->menu_last_px);
332
0
  format_add(ft, "popup_last_y", "%u", window->menu_last_py + h);
333
334
0
  lines = status_line_size(tc);
335
0
  position = options_get_number(s->options, "status-position");
336
0
  if (status_at_line(tc) != -1 && lines != 0) {
337
0
    for (line = 0; line < lines; line++) {
338
0
      ranges = &tc->status.entries[line].ranges;
339
0
      TAILQ_FOREACH(sr, ranges, entry) {
340
0
        if (sr->type != STYLE_RANGE_WINDOW)
341
0
          continue;
342
0
        if (sr->argument == (u_int)wl->idx)
343
0
          break;
344
0
      }
345
0
      if (sr != NULL)
346
0
        break;
347
0
    }
348
0
    if (sr != NULL) {
349
0
      format_add(ft, "popup_window_status_line_x", "%u",
350
0
          sr->start + ox);
351
0
      if (position == 0) {
352
0
        format_add(ft, "popup_window_status_line_y",
353
0
            "%u", h);
354
0
      } else {
355
0
        format_add(ft, "popup_window_status_line_y",
356
0
            "%u", window->sy);
357
0
      }
358
0
    }
359
0
    if (position == 0)
360
0
      format_add(ft, "popup_status_line_y", "%u", h);
361
0
    else
362
0
      format_add(ft, "popup_status_line_y", "%u", window->sy);
363
0
  }
364
365
0
  format_add(ft, "popup_width", "%u", w);
366
0
  format_add(ft, "popup_height", "%u", h);
367
368
0
  n = ((long)window->sx - 1) / 2 - w / 2;
369
0
  if (n < 0)
370
0
    format_add(ft, "popup_centre_x", "%u", 0);
371
0
  else
372
0
    format_add(ft, "popup_centre_x", "%ld", n);
373
0
  n = ((long)window->sy - 1) / 2 + h / 2;
374
0
  if (n >= window->sy)
375
0
    format_add(ft, "popup_centre_y", "%ld", max_y);
376
0
  else
377
0
    format_add(ft, "popup_centre_y", "%ld", n);
378
379
0
  if (event->m.valid) {
380
0
    n = mouse_x - w / 2;
381
0
    if (n < 0)
382
0
      format_add(ft, "popup_mouse_centre_x", "%u", 0);
383
0
    else
384
0
      format_add(ft, "popup_mouse_centre_x", "%ld", n);
385
0
    n = mouse_y - h / 2;
386
0
    if (n + h >= window->sy)
387
0
      format_add(ft, "popup_mouse_centre_y", "%ld", max_y);
388
0
    else
389
0
      format_add(ft, "popup_mouse_centre_y", "%ld", n);
390
0
    n = mouse_y + h;
391
0
    if (n >= window->sy)
392
0
      format_add(ft, "popup_mouse_top", "%u", window->sy - 1);
393
0
    else
394
0
      format_add(ft, "popup_mouse_top", "%ld", n);
395
0
    n = mouse_y - h;
396
0
    if (n < 0)
397
0
      format_add(ft, "popup_mouse_bottom", "%u", 0);
398
0
    else
399
0
      format_add(ft, "popup_mouse_bottom", "%ld", n);
400
0
  }
401
402
0
  n = wp->yoff + h;
403
0
  if (n >= window->sy)
404
0
    format_add(ft, "popup_pane_top", "%ld", max_y);
405
0
  else
406
0
    format_add(ft, "popup_pane_top", "%ld", n);
407
0
  format_add(ft, "popup_pane_bottom", "%u", wp->yoff + wp->sy);
408
0
  format_add(ft, "popup_pane_left", "%u", wp->xoff);
409
0
  n = (long)wp->xoff + wp->sx - w;
410
0
  if (n < 0)
411
0
    format_add(ft, "popup_pane_right", "%u", 0);
412
0
  else
413
0
    format_add(ft, "popup_pane_right", "%ld", n);
414
415
0
  xp = args_get(args, 'x');
416
0
  if (xp == NULL || strcmp(xp, "C") == 0)
417
0
    xp = "#{popup_centre_x}";
418
0
  else if (strcmp(xp, "R") == 0)
419
0
    xp = "#{popup_pane_right}";
420
0
  else if (strcmp(xp, "P") == 0)
421
0
    xp = "#{popup_pane_left}";
422
0
  else if (strcmp(xp, "M") == 0)
423
0
    xp = "#{popup_mouse_centre_x}";
424
0
  else if (strcmp(xp, "L") == 0)
425
0
    xp = "#{popup_last_x}";
426
0
  else if (strcmp(xp, "W") == 0)
427
0
    xp = "#{popup_window_status_line_x}";
428
0
  p = format_expand(ft, xp);
429
0
  n = strtol(p, NULL, 10);
430
0
  if (n < 0)
431
0
    n = 0;
432
0
  if (n > max_x)
433
0
    n = max_x;
434
0
  *px = n;
435
0
  log_debug("%s: -x: %s = %s = %u (-w %u)", __func__, xp, p, *px, w);
436
0
  free(p);
437
438
0
  yp = args_get(args, 'y');
439
0
  if (yp == NULL || strcmp(yp, "C") == 0)
440
0
    yp = "#{popup_centre_y}";
441
0
  else if (strcmp(yp, "P") == 0)
442
0
    yp = "#{popup_pane_bottom}";
443
0
  else if (strcmp(yp, "M") == 0)
444
0
    yp = "#{popup_mouse_top}";
445
0
  else if (strcmp(yp, "L") == 0)
446
0
    yp = "#{popup_last_y}";
447
0
  else if (strcmp(yp, "S") == 0)
448
0
    yp = "#{popup_status_line_y}";
449
0
  else if (strcmp(yp, "W") == 0)
450
0
    yp = "#{popup_window_status_line_y}";
451
0
  p = format_expand(ft, yp);
452
0
  n = strtol(p, NULL, 10);
453
0
  if (n < h)
454
0
    n = 0;
455
0
  else
456
0
    n -= h;
457
0
  if (n < 0)
458
0
    n = 0;
459
0
  if (n > max_y)
460
0
    n = max_y;
461
0
  *py = n;
462
0
  log_debug("%s: -y: %s = %s = %u (-h %u)", __func__, yp, p, *py, h);
463
0
  free(p);
464
465
0
  format_free(ft);
466
0
  return (1);
467
0
}
468
469
static enum cmd_retval
470
cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
471
0
{
472
0
  struct args   *args = cmd_get_args(self);
473
0
  struct cmd_find_state *target = cmdq_get_target(item);
474
0
  struct key_event  *event = cmdq_get_event(item);
475
0
  struct client   *tc = cmdq_get_target_client(item);
476
0
  struct menu   *menu = NULL;
477
0
  struct menu_item   menu_item;
478
0
  const char    *key, *name, *value;
479
0
  const char    *style = args_get(args, 's');
480
0
  const char    *border_style = args_get(args, 'S');
481
0
  const char    *selected_style = args_get(args, 'H');
482
0
  enum box_lines     lines = BOX_LINES_DEFAULT;
483
0
  char      *title, *cause = NULL;
484
0
  int      flags = 0, starting_choice = 0;
485
0
  u_int      px, py, i, count = args_count(args);
486
0
  struct options    *o = target->s->curw->window->options;
487
0
  struct options_entry  *oe;
488
489
0
  if (args_has(args, 'C')) {
490
0
    if (strcmp(args_get(args, 'C'), "-") == 0)
491
0
      starting_choice = -1;
492
0
    else {
493
0
      starting_choice = args_strtonum(args, 'C', 0, UINT_MAX,
494
0
          &cause);
495
0
      if (cause != NULL) {
496
0
        cmdq_error(item, "starting choice %s", cause);
497
0
        goto fail;
498
0
      }
499
0
    }
500
0
  }
501
502
0
  if (args_has(args, 'T'))
503
0
    title = format_single_from_target(item, args_get(args, 'T'));
504
0
  else
505
0
    title = xstrdup("");
506
0
  menu = menu_create(title);
507
0
  free(title);
508
509
0
  for (i = 0; i != count; /* nothing */) {
510
0
    name = args_string(args, i++);
511
0
    if (*name == '\0') {
512
0
      menu_add_item(menu, NULL, item, tc, target);
513
0
      continue;
514
0
    }
515
516
0
    if (count - i < 2) {
517
0
      cmdq_error(item, "not enough arguments");
518
0
      goto fail;
519
0
    }
520
0
    key = args_string(args, i++);
521
522
0
    menu_item.name = name;
523
0
    menu_item.key = key_string_lookup_string(key);
524
0
    menu_item.command = args_string(args, i++);
525
526
0
    menu_add_item(menu, &menu_item, item, tc, target);
527
0
  }
528
0
  if (menu == NULL) {
529
0
    cmdq_error(item, "invalid menu arguments");
530
0
    goto fail;
531
0
  }
532
0
  if (menu->count == 0)
533
0
    goto out;
534
0
  if (!cmd_display_menu_get_menu_pos(tc, item, args, &px, &py,
535
0
      menu->width + 4, menu->count + 2))
536
0
    goto out;
537
538
0
  value = args_get(args, 'b');
539
0
  if (value != NULL) {
540
0
    oe = options_get(o, "menu-border-lines");
541
0
    lines = options_find_choice(options_table_entry(oe), value,
542
0
        &cause);
543
0
    if (lines == -1) {
544
0
      cmdq_error(item, "menu-border-lines %s", cause);
545
0
      goto fail;
546
0
    }
547
0
  }
548
549
0
  if (args_has(args, 'O'))
550
0
    flags |= MENU_STAYOPEN;
551
0
  if (!event->m.valid && !args_has(args, 'M'))
552
0
    flags |= MENU_NOMOUSE;
553
0
  if (menu_display(menu, flags, starting_choice, item, px, py, tc, lines,
554
0
      style, selected_style, border_style, target, NULL, NULL) != 0)
555
0
    goto out;
556
0
  return (CMD_RETURN_NORMAL);
557
558
0
out:
559
0
  menu_free(menu);
560
0
  return (CMD_RETURN_NORMAL);
561
562
0
fail:
563
0
  free(cause);
564
0
  menu_free(menu);
565
0
  return (CMD_RETURN_ERROR);
566
0
}
567
568
static enum cmd_retval
569
cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
570
0
{
571
0
  struct args   *args = cmd_get_args(self);
572
0
  struct cmd_find_state *target = cmdq_get_target(item);
573
0
  struct session    *s = target->s;
574
0
  struct client   *tc = cmdq_get_target_client(item);
575
0
  struct tty    *tty = &tc->tty;
576
0
  const char    *value, *shell, *shellcmd = NULL;
577
0
  const char    *style = args_get(args, 's');
578
0
  const char    *border_style = args_get(args, 'S');
579
0
  char      *cwd = NULL, *cause = NULL, **argv = NULL;
580
0
  char      *title = NULL;
581
0
  int      modify = popup_present(tc);
582
0
  int      flags = -1, argc = 0;
583
0
  enum box_lines     lines = BOX_LINES_DEFAULT;
584
0
  u_int      px, py, w, h, count = args_count(args);
585
0
  struct args_value *av;
586
0
  struct environ    *env = NULL;
587
0
  struct options    *o = s->curw->window->options;
588
0
  struct options_entry  *oe;
589
590
0
  if (args_has(args, 'C')) {
591
0
    server_client_clear_overlay(tc);
592
0
    return (CMD_RETURN_NORMAL);
593
0
  }
594
0
  if (!modify && tc->overlay_draw != NULL)
595
0
    return (CMD_RETURN_NORMAL);
596
597
0
  if (!modify) {
598
0
    h = tty->sy / 2;
599
0
    if (args_has(args, 'h')) {
600
0
      h = args_percentage(args, 'h', 1, tty->sy, tty->sy,
601
0
          &cause);
602
0
      if (cause != NULL) {
603
0
        cmdq_error(item, "height %s", cause);
604
0
        goto fail;
605
0
      }
606
0
    }
607
608
0
    w = tty->sx / 2;
609
0
    if (args_has(args, 'w')) {
610
0
      w = args_percentage(args, 'w', 1, tty->sx, tty->sx,
611
0
          &cause);
612
0
      if (cause != NULL) {
613
0
        cmdq_error(item, "width %s", cause);
614
0
        goto fail;
615
0
      }
616
0
    }
617
618
0
    if (w > tty->sx)
619
0
      w = tty->sx;
620
0
    if (h > tty->sy)
621
0
      h = tty->sy;
622
0
    if (!cmd_display_menu_get_popup_pos(tc, item, args, &px, &py,
623
0
        w, h))
624
0
      goto out;
625
626
0
    value = args_get(args, 'd');
627
0
    if (value != NULL)
628
0
      cwd = format_single_from_target(item, value);
629
0
    else
630
0
      cwd = xstrdup(server_client_get_cwd(tc, s));
631
0
    if (count == 0) {
632
0
      shellcmd = options_get_string(s->options,
633
0
          "default-command");
634
0
    } else if (count == 1)
635
0
      shellcmd = args_string(args, 0);
636
0
    if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
637
0
      shellcmd = NULL;
638
0
      shell = options_get_string(s->options, "default-shell");
639
0
      if (!checkshell(shell))
640
0
        shell = _PATH_BSHELL;
641
0
      cmd_append_argv(&argc, &argv, shell);
642
0
    } else
643
0
      args_to_vector(args, &argc, &argv);
644
645
0
    if (args_has(args, 'e') >= 1) {
646
0
      env = environ_create();
647
0
      av = args_first_value(args, 'e');
648
0
      while (av != NULL) {
649
0
        environ_put(env, av->string, 0);
650
0
        av = args_next_value(av);
651
0
      }
652
0
    }
653
0
  }
654
655
0
  value = args_get(args, 'b');
656
0
  if (args_has(args, 'B'))
657
0
    lines = BOX_LINES_NONE;
658
0
  else if (value != NULL) {
659
0
    oe = options_get(o, "popup-border-lines");
660
0
    lines = options_find_choice(options_table_entry(oe), value,
661
0
        &cause);
662
0
    if (cause != NULL) {
663
0
      cmdq_error(item, "popup-border-lines %s", cause);
664
0
      goto fail;
665
0
    }
666
0
  }
667
668
0
  if (args_has(args, 'T'))
669
0
    title = format_single_from_target(item, args_get(args, 'T'));
670
0
  else
671
0
    title = xstrdup("");
672
673
0
  if (args_has(args, 'N') || !modify)
674
0
    flags = 0;
675
0
  if (args_has(args, 'E') > 1) {
676
0
    if (flags == -1)
677
0
      flags = 0;
678
0
    flags |= POPUP_CLOSEEXITZERO;
679
0
  } else if (args_has(args, 'E')) {
680
0
    if (flags == -1)
681
0
      flags = 0;
682
0
    flags |= POPUP_CLOSEEXIT;
683
0
  }
684
0
  if (args_has(args, 'k')) {
685
0
    if (flags == -1)
686
0
      flags = 0;
687
0
    flags |= POPUP_CLOSEANYKEY;
688
0
  }
689
690
0
  if (modify) {
691
0
    popup_modify(tc, title, style, border_style, lines, flags);
692
0
    goto out;
693
0
  }
694
0
  if (popup_display(flags, lines, item, px, py, w, h, env, shellcmd, argc,
695
0
      argv, cwd, title, tc, s, style, border_style, NULL, NULL) != 0)
696
0
    goto out;
697
0
  environ_free(env);
698
0
  free(cwd);
699
0
  free(title);
700
0
  cmd_free_argv(argc, argv);
701
0
  return (CMD_RETURN_WAIT);
702
703
0
out:
704
0
  cmd_free_argv(argc, argv);
705
0
  environ_free(env);
706
0
  free(cwd);
707
0
  free(title);
708
0
  return (CMD_RETURN_NORMAL);
709
710
0
fail:
711
0
  free(cause);
712
0
  cmd_free_argv(argc, argv);
713
0
  environ_free(env);
714
0
  free(cwd);
715
0
  free(title);
716
0
  return (CMD_RETURN_ERROR);
717
0
}