Coverage Report

Created: 2026-10-05 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-find.c
Line
Count
Source
1
/* $OpenBSD: cmd-find.c,v 1.88 2026/09/07 12:05:12 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2015 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 <fnmatch.h>
22
#include <limits.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <unistd.h>
26
27
#include "tmux.h"
28
29
static int  cmd_find_session_better(struct session *, struct session *,
30
        int);
31
static struct session *cmd_find_best_session(struct session **, u_int, int);
32
static int  cmd_find_best_session_with_window(struct cmd_find_state *);
33
static int  cmd_find_best_winlink_with_window(struct cmd_find_state *);
34
35
static const char *cmd_find_map_table(const char *[][2], const char *);
36
37
static void cmd_find_log_state(const char *, struct cmd_find_state *);
38
static int  cmd_find_get_session(struct cmd_find_state *, const char *);
39
static int  cmd_find_get_window(struct cmd_find_state *, const char *, int);
40
static int  cmd_find_get_window_with_session(struct cmd_find_state *,
41
        const char *);
42
static int  cmd_find_get_pane(struct cmd_find_state *, const char *, int);
43
static int  cmd_find_get_pane_with_session(struct cmd_find_state *,
44
        const char *);
45
static int  cmd_find_get_pane_with_window(struct cmd_find_state *,
46
        const char *);
47
48
static const char *cmd_find_session_table[][2] = {
49
  { NULL, NULL }
50
};
51
static const char *cmd_find_window_table[][2] = {
52
  { "{start}", "^" },
53
  { "{last}", "!" },
54
  { "{end}", "$" },
55
  { "{next}", "+" },
56
  { "{previous}", "-" },
57
  { NULL, NULL }
58
};
59
static const char *cmd_find_pane_table[][2] = {
60
  { "{last}", "!" },
61
  { "{next}", "+" },
62
  { "{previous}", "-" },
63
  { "{top}", "top" },
64
  { "{bottom}", "bottom" },
65
  { "{left}", "left" },
66
  { "{right}", "right" },
67
  { "{top-left}", "top-left" },
68
  { "{top-right}", "top-right" },
69
  { "{bottom-left}", "bottom-left" },
70
  { "{bottom-right}", "bottom-right" },
71
  { "{up-of}", "{up-of}" },
72
  { "{down-of}", "{down-of}" },
73
  { "{left-of}", "{left-of}" },
74
  { "{right-of}", "{right-of}" },
75
  { NULL, NULL }
76
};
77
78
/* Find pane containing client if any. */
79
static struct window_pane *
80
cmd_find_inside_pane(struct client *c)
81
0
{
82
0
  struct window_pane  *wp;
83
0
  struct environ_entry  *envent;
84
85
0
  if (c == NULL)
86
0
    return (NULL);
87
88
0
  RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
89
0
    if (wp->fd != -1 && strcmp(wp->tty, c->ttyname) == 0)
90
0
      break;
91
0
  }
92
0
  if (wp == NULL) {
93
0
    envent = environ_find(c->environ, "TMUX_PANE");
94
0
    if (envent != NULL)
95
0
      wp = window_pane_find_by_id_str(envent->value);
96
0
  }
97
0
  if (wp != NULL)
98
0
    log_debug("%s: got pane %%%u (%s)", __func__, wp->id, wp->tty);
99
0
  return (wp);
100
0
}
101
102
/* Is this client better? */
103
static int
104
cmd_find_client_better(struct client *c, struct client *than)
105
0
{
106
0
  if (than == NULL)
107
0
    return (1);
108
0
  return (timercmp(&c->activity_time, &than->activity_time, >));
109
0
}
110
111
/* Find best client for session. */
112
struct client *
113
cmd_find_best_client(struct session *s)
114
0
{
115
0
  struct client *c_loop, *c;
116
117
0
  if (s->attached == 0)
118
0
    s = NULL;
119
120
0
  c = NULL;
121
0
  TAILQ_FOREACH(c_loop, &clients, entry) {
122
0
    if (c_loop->session == NULL)
123
0
      continue;
124
0
    if (s != NULL && c_loop->session != s)
125
0
      continue;
126
0
    if (cmd_find_client_better(c_loop, c))
127
0
      c = c_loop;
128
0
  }
129
0
  return (c);
130
0
}
131
132
/* Is this session better? */
133
static int
134
cmd_find_session_better(struct session *s, struct session *than, int flags)
135
0
{
136
0
  int attached;
137
138
0
  if (than == NULL)
139
0
    return (1);
140
0
  if (flags & CMD_FIND_PREFER_UNATTACHED) {
141
0
    attached = (than->attached != 0);
142
0
    if (attached && s->attached == 0)
143
0
      return (1);
144
0
    else if (!attached && s->attached != 0)
145
0
      return (0);
146
0
  }
147
0
  return (timercmp(&s->activity_time, &than->activity_time, >));
148
0
}
149
150
/* Can this session be usefully targeted? */
151
static int
152
cmd_find_session_valid(struct session *s)
153
0
{
154
0
  if (!session_alive(s) ||
155
0
      s->curw == NULL ||
156
0
      s->curw->window == NULL ||
157
0
      s->curw->window->active == NULL)
158
0
    return (0);
159
0
  return (1);
160
0
}
161
162
/* Find best session from a list, or all if list is NULL. */
163
static struct session *
164
cmd_find_best_session(struct session **slist, u_int ssize, int flags)
165
0
{
166
0
  struct session   *s_loop, *s;
167
0
  u_int     i;
168
169
0
  log_debug("%s: %u sessions to try", __func__, ssize);
170
171
0
  s = NULL;
172
0
  if (slist != NULL) {
173
0
    for (i = 0; i < ssize; i++) {
174
0
      if (!cmd_find_session_valid(slist[i]))
175
0
        continue;
176
0
      if (cmd_find_session_better(slist[i], s, flags))
177
0
        s = slist[i];
178
0
    }
179
0
  } else {
180
0
    RB_FOREACH(s_loop, sessions, &sessions) {
181
0
      if (!cmd_find_session_valid(s_loop))
182
0
        continue;
183
0
      if (cmd_find_session_better(s_loop, s, flags))
184
0
        s = s_loop;
185
0
    }
186
0
  }
187
0
  return (s);
188
0
}
189
190
/* Find best session and winlink for window. */
191
static int
192
cmd_find_best_session_with_window(struct cmd_find_state *fs)
193
4.76k
{
194
4.76k
  struct session  **slist = NULL;
195
4.76k
  u_int     ssize;
196
4.76k
  struct session   *s;
197
198
4.76k
  log_debug("%s: window is @%u", __func__, fs->w->id);
199
200
4.76k
  ssize = 0;
201
4.76k
  RB_FOREACH(s, sessions, &sessions) {
202
0
    if (!session_has(s, fs->w))
203
0
      continue;
204
0
    slist = xreallocarray(slist, ssize + 1, sizeof *slist);
205
0
    slist[ssize++] = s;
206
0
  }
207
4.76k
  if (ssize == 0)
208
4.76k
    goto fail;
209
0
  fs->s = cmd_find_best_session(slist, ssize, fs->flags);
210
0
  if (fs->s == NULL)
211
0
    goto fail;
212
0
  free(slist);
213
0
  return (cmd_find_best_winlink_with_window(fs));
214
215
4.76k
fail:
216
4.76k
  free(slist);
217
4.76k
  return (-1);
218
0
}
219
220
/*
221
 * Find the best winlink for a window (the current if it contains the window,
222
 * otherwise the first).
223
 */
224
static int
225
cmd_find_best_winlink_with_window(struct cmd_find_state *fs)
226
0
{
227
0
  struct winlink   *wl, *wl_loop;
228
229
0
  log_debug("%s: window is @%u", __func__, fs->w->id);
230
231
0
  wl = NULL;
232
0
  if (fs->s->curw != NULL && fs->s->curw->window == fs->w)
233
0
    wl = fs->s->curw;
234
0
  else {
235
0
    RB_FOREACH(wl_loop, winlinks, &fs->s->windows) {
236
0
      if (wl_loop->window == fs->w) {
237
0
        wl = wl_loop;
238
0
        break;
239
0
      }
240
0
    }
241
0
  }
242
0
  if (wl == NULL)
243
0
    return (-1);
244
0
  fs->wl = wl;
245
0
  fs->idx = fs->wl->idx;
246
0
  return (0);
247
0
}
248
249
/* Maps string in table. */
250
static const char *
251
cmd_find_map_table(const char *table[][2], const char *s)
252
0
{
253
0
  u_int i;
254
255
0
  for (i = 0; table[i][0] != NULL; i++) {
256
0
    if (strcmp(s, table[i][0]) == 0)
257
0
      return (table[i][1]);
258
0
  }
259
0
  return (s);
260
0
}
261
262
/* Find session from string. Fills in s. */
263
static int
264
cmd_find_get_session(struct cmd_find_state *fs, const char *session)
265
0
{
266
0
  struct session  *s, *s_loop;
267
0
  struct client *c;
268
269
0
  log_debug("%s: %s", __func__, session);
270
271
  /* Check for session ids starting with $. */
272
0
  if (*session == '$') {
273
0
    fs->s = session_find_by_id_str(session);
274
0
    if (fs->s == NULL)
275
0
      return (-1);
276
0
    return (0);
277
0
  }
278
279
  /* Look for exactly this session. */
280
0
  fs->s = session_find(session);
281
0
  if (fs->s != NULL)
282
0
    return (0);
283
284
  /* Look for as a client. */
285
0
  c = cmd_find_client(NULL, session, 1);
286
0
  if (c != NULL && c->session != NULL) {
287
0
    fs->s = c->session;
288
0
    return (0);
289
0
  }
290
291
  /* Stop now if exact only. */
292
0
  if (fs->flags & CMD_FIND_EXACT_SESSION)
293
0
    return (-1);
294
295
  /* Otherwise look for prefix. */
296
0
  s = NULL;
297
0
  RB_FOREACH(s_loop, sessions, &sessions) {
298
0
    if (strncmp(session, s_loop->name, strlen(session)) == 0) {
299
0
      if (s != NULL)
300
0
        return (-1);
301
0
      s = s_loop;
302
0
    }
303
0
  }
304
0
  if (s != NULL) {
305
0
    fs->s = s;
306
0
    return (0);
307
0
  }
308
309
  /* Then as a pattern. */
310
0
  s = NULL;
311
0
  RB_FOREACH(s_loop, sessions, &sessions) {
312
0
    if (fnmatch(session, s_loop->name, 0) == 0) {
313
0
      if (s != NULL)
314
0
        return (-1);
315
0
      s = s_loop;
316
0
    }
317
0
  }
318
0
  if (s != NULL) {
319
0
    fs->s = s;
320
0
    return (0);
321
0
  }
322
323
0
  return (-1);
324
0
}
325
326
/* Find window from string. Fills in s, wl, w. */
327
static int
328
cmd_find_get_window(struct cmd_find_state *fs, const char *window, int only)
329
0
{
330
0
  log_debug("%s: %s", __func__, window);
331
332
  /* Check for window ids starting with @. */
333
0
  if (*window == '@') {
334
0
    fs->w = window_find_by_id_str(window);
335
0
    if (fs->w == NULL)
336
0
      return (-1);
337
0
    return (cmd_find_best_session_with_window(fs));
338
0
  }
339
340
  /* Not a window id, so use the current session. */
341
0
  fs->s = fs->current->s;
342
343
  /* We now only need to find the winlink in this session. */
344
0
  if (cmd_find_get_window_with_session(fs, window) == 0)
345
0
    return (0);
346
347
  /* Otherwise try as a session itself. */
348
0
  if (!only && cmd_find_get_session(fs, window) == 0) {
349
0
    fs->wl = fs->s->curw;
350
0
    fs->w = fs->wl->window;
351
0
    if (~fs->flags & CMD_FIND_WINDOW_INDEX)
352
0
      fs->idx = fs->wl->idx;
353
0
    return (0);
354
0
  }
355
356
0
  return (-1);
357
0
}
358
359
/*
360
 * Find window from string, assuming it is in given session. Needs s, fills in
361
 * wl and w.
362
 */
363
static int
364
cmd_find_get_window_with_session(struct cmd_find_state *fs, const char *window)
365
0
{
366
0
  struct winlink  *wl;
367
0
  const char  *errstr;
368
0
  int    idx, n, exact;
369
0
  struct session  *s;
370
371
0
  log_debug("%s: %s", __func__, window);
372
0
  exact = (fs->flags & CMD_FIND_EXACT_WINDOW);
373
374
  /*
375
   * Start with the current window as the default. So if only an index is
376
   * found, the window will be the current.
377
   */
378
0
  fs->wl = fs->s->curw;
379
0
  fs->w = fs->wl->window;
380
381
  /* Check for window ids starting with @. */
382
0
  if (*window == '@') {
383
0
    fs->w = window_find_by_id_str(window);
384
0
    if (fs->w == NULL || !session_has(fs->s, fs->w))
385
0
      return (-1);
386
0
    return (cmd_find_best_winlink_with_window(fs));
387
0
  }
388
389
  /* Try as an offset. */
390
0
  if (!exact && (window[0] == '+' || window[0] == '-')) {
391
0
    if (window[1] != '\0') {
392
0
      n = strtonum(window + 1, 1, INT_MAX, &errstr);
393
0
      if (errstr != NULL)
394
0
        return (-1);
395
0
    } else
396
0
      n = 1;
397
0
    s = fs->s;
398
0
    if (fs->flags & CMD_FIND_WINDOW_INDEX) {
399
0
      if (window[0] == '+') {
400
0
        if (INT_MAX - s->curw->idx < n)
401
0
          return (-1);
402
0
        fs->idx = s->curw->idx + n;
403
0
      } else {
404
0
        if (n > s->curw->idx)
405
0
          return (-1);
406
0
        fs->idx = s->curw->idx - n;
407
0
      }
408
0
      return (0);
409
0
    }
410
0
    if (window[0] == '+')
411
0
      fs->wl = winlink_next_by_number(s->curw, s, n);
412
0
    else
413
0
      fs->wl = winlink_previous_by_number(s->curw, s, n);
414
0
    if (fs->wl != NULL) {
415
0
      fs->idx = fs->wl->idx;
416
0
      fs->w = fs->wl->window;
417
0
      return (0);
418
0
    }
419
0
  }
420
421
  /* Try special characters. */
422
0
  if (!exact) {
423
0
    if (strcmp(window, "!") == 0) {
424
0
      fs->wl = TAILQ_FIRST(&fs->s->lastw);
425
0
      if (fs->wl == NULL)
426
0
        return (-1);
427
0
      fs->idx = fs->wl->idx;
428
0
      fs->w = fs->wl->window;
429
0
      return (0);
430
0
    } else if (strcmp(window, "^") == 0) {
431
0
      fs->wl = RB_MIN(winlinks, &fs->s->windows);
432
0
      if (fs->wl == NULL)
433
0
        return (-1);
434
0
      fs->idx = fs->wl->idx;
435
0
      fs->w = fs->wl->window;
436
0
      return (0);
437
0
    } else if (strcmp(window, "$") == 0) {
438
0
      fs->wl = RB_MAX(winlinks, &fs->s->windows);
439
0
      if (fs->wl == NULL)
440
0
        return (-1);
441
0
      fs->idx = fs->wl->idx;
442
0
      fs->w = fs->wl->window;
443
0
      return (0);
444
0
    }
445
0
  }
446
447
  /* First see if this is a valid window index in this session. */
448
0
  if (window[0] != '+' && window[0] != '-') {
449
0
    idx = strtonum(window, 0, INT_MAX, &errstr);
450
0
    if (errstr == NULL) {
451
0
      fs->wl = winlink_find_by_index(&fs->s->windows, idx);
452
0
      if (fs->wl != NULL) {
453
0
        fs->idx = fs->wl->idx;
454
0
        fs->w = fs->wl->window;
455
0
        return (0);
456
0
      }
457
0
      if (fs->flags & CMD_FIND_WINDOW_INDEX) {
458
0
        fs->idx = idx;
459
0
        return (0);
460
0
      }
461
0
    }
462
0
  }
463
464
  /* Look for exact matches, error if more than one. */
465
0
  fs->wl = NULL;
466
0
  RB_FOREACH(wl, winlinks, &fs->s->windows) {
467
0
    if (strcmp(window, wl->window->name) == 0) {
468
0
      if (fs->wl != NULL)
469
0
        return (-1);
470
0
      fs->wl = wl;
471
0
    }
472
0
  }
473
0
  if (fs->wl != NULL) {
474
0
    fs->idx = fs->wl->idx;
475
0
    fs->w = fs->wl->window;
476
0
    return (0);
477
0
  }
478
479
  /* Stop now if exact only. */
480
0
  if (exact)
481
0
    return (-1);
482
483
  /* Try as the start of a window name, error if multiple. */
484
0
  fs->wl = NULL;
485
0
  RB_FOREACH(wl, winlinks, &fs->s->windows) {
486
0
    if (strncmp(window, wl->window->name, strlen(window)) == 0) {
487
0
      if (fs->wl != NULL)
488
0
        return (-1);
489
0
      fs->wl = wl;
490
0
    }
491
0
  }
492
0
  if (fs->wl != NULL) {
493
0
    fs->idx = fs->wl->idx;
494
0
    fs->w = fs->wl->window;
495
0
    return (0);
496
0
  }
497
498
  /* Now look for pattern matches, again error if multiple. */
499
0
  fs->wl = NULL;
500
0
  RB_FOREACH(wl, winlinks, &fs->s->windows) {
501
0
    if (fnmatch(window, wl->window->name, 0) == 0) {
502
0
      if (fs->wl != NULL)
503
0
        return (-1);
504
0
      fs->wl = wl;
505
0
    }
506
0
  }
507
0
  if (fs->wl != NULL) {
508
0
    fs->idx = fs->wl->idx;
509
0
    fs->w = fs->wl->window;
510
0
    return (0);
511
0
  }
512
513
0
  return (-1);
514
0
}
515
516
/* Find pane from string. Fills in s, wl, w, wp. */
517
static int
518
cmd_find_get_pane(struct cmd_find_state *fs, const char *pane, int only)
519
0
{
520
0
  log_debug("%s: %s", __func__, pane);
521
522
  /* Check for pane ids starting with %. */
523
0
  if (*pane == '%') {
524
0
    fs->wp = window_pane_find_by_id_str(pane);
525
0
    if (fs->wp == NULL)
526
0
      return (-1);
527
0
    fs->w = fs->wp->window;
528
0
    return (cmd_find_best_session_with_window(fs));
529
0
  }
530
531
  /* Not a pane id, so try the current session and window. */
532
0
  fs->s = fs->current->s;
533
0
  fs->wl = fs->current->wl;
534
0
  fs->idx = fs->current->idx;
535
0
  fs->w = fs->current->w;
536
537
  /* We now only need to find the pane in this window. */
538
0
  if (cmd_find_get_pane_with_window(fs, pane) == 0)
539
0
    return (0);
540
541
  /* Otherwise try as a window itself (this will also try as session). */
542
0
  if (!only && cmd_find_get_window(fs, pane, 0) == 0) {
543
0
    fs->wp = fs->w->active;
544
0
    return (0);
545
0
  }
546
547
0
  return (-1);
548
0
}
549
550
/*
551
 * Find pane from string, assuming it is in given session. Needs s, fills in wl
552
 * and w and wp.
553
 */
554
static int
555
cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane)
556
0
{
557
0
  log_debug("%s: %s", __func__, pane);
558
559
  /* Check for pane ids starting with %. */
560
0
  if (*pane == '%') {
561
0
    fs->wp = window_pane_find_by_id_str(pane);
562
0
    if (fs->wp == NULL)
563
0
      return (-1);
564
0
    fs->w = fs->wp->window;
565
0
    return (cmd_find_best_winlink_with_window(fs));
566
0
  }
567
568
  /* Otherwise use the current window. */
569
0
  fs->wl = fs->s->curw;
570
0
  fs->idx = fs->wl->idx;
571
0
  fs->w = fs->wl->window;
572
573
  /* Now we just need to look up the pane. */
574
0
  return (cmd_find_get_pane_with_window(fs, pane));
575
0
}
576
577
/*
578
 * Find pane from string, assuming it is in the given window. Needs w, fills in
579
 * wp.
580
 */
581
static int
582
cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane)
583
0
{
584
0
  const char    *errstr;
585
0
  int      idx;
586
0
  struct window_pane  *wp;
587
0
  u_int      n;
588
589
0
  log_debug("%s: %s", __func__, pane);
590
591
  /* Check for pane ids starting with %. */
592
0
  if (*pane == '%') {
593
0
    fs->wp = window_pane_find_by_id_str(pane);
594
0
    if (fs->wp == NULL)
595
0
      return (-1);
596
0
    if (fs->wp->window != fs->w)
597
0
      return (-1);
598
0
    return (0);
599
0
  }
600
601
  /* Try special characters. */
602
0
  if (strcmp(pane, "!") == 0) {
603
0
    fs->wp = TAILQ_FIRST(&fs->w->last_panes);
604
0
    if (fs->wp == NULL)
605
0
      return (-1);
606
0
    return (0);
607
0
  } else if (strcmp(pane, "{up-of}") == 0) {
608
0
    fs->wp = window_pane_find_up(fs->w->active);
609
0
    if (fs->wp == NULL)
610
0
      return (-1);
611
0
    return (0);
612
0
  } else if (strcmp(pane, "{down-of}") == 0) {
613
0
    fs->wp = window_pane_find_down(fs->w->active);
614
0
    if (fs->wp == NULL)
615
0
      return (-1);
616
0
    return (0);
617
0
  } else if (strcmp(pane, "{left-of}") == 0) {
618
0
    fs->wp = window_pane_find_left(fs->w->active);
619
0
    if (fs->wp == NULL)
620
0
      return (-1);
621
0
    return (0);
622
0
  } else if (strcmp(pane, "{right-of}") == 0) {
623
0
    fs->wp = window_pane_find_right(fs->w->active);
624
0
    if (fs->wp == NULL)
625
0
      return (-1);
626
0
    return (0);
627
0
  }
628
629
  /* Try as an offset. */
630
0
  if (pane[0] == '+' || pane[0] == '-') {
631
0
    if (pane[1] != '\0') {
632
0
      n = strtonum(pane + 1, 1, INT_MAX, &errstr);
633
0
      if (errstr != NULL)
634
0
        return (-1);
635
0
    } else
636
0
      n = 1;
637
0
    wp = fs->w->active;
638
0
    if (pane[0] == '+')
639
0
      fs->wp = window_pane_next_by_number(fs->w, wp, n);
640
0
    else
641
0
      fs->wp = window_pane_previous_by_number(fs->w, wp, n);
642
0
    if (fs->wp != NULL)
643
0
      return (0);
644
0
  }
645
646
  /* Get pane by index. */
647
0
  idx = strtonum(pane, 0, INT_MAX, &errstr);
648
0
  if (errstr == NULL) {
649
0
    fs->wp = window_pane_at_index(fs->w, idx);
650
0
    if (fs->wp != NULL)
651
0
      return (0);
652
0
  }
653
654
  /* Try as a description. */
655
0
  fs->wp = window_find_string(fs->w, pane);
656
0
  if (fs->wp != NULL)
657
0
    return (0);
658
659
0
  return (-1);
660
0
}
661
662
/* Clear state. */
663
void
664
cmd_find_clear_state(struct cmd_find_state *fs, int flags)
665
23.8k
{
666
23.8k
  memset(fs, 0, sizeof *fs);
667
668
23.8k
  fs->flags = flags;
669
670
23.8k
  fs->idx = -1;
671
23.8k
}
672
673
/* Check if state is empty. */
674
int
675
cmd_find_empty_state(struct cmd_find_state *fs)
676
0
{
677
0
  if (fs->s == NULL && fs->wl == NULL && fs->w == NULL && fs->wp == NULL)
678
0
    return (1);
679
0
  return (0);
680
0
}
681
682
/* Check if a state if valid. */
683
int
684
cmd_find_valid_state(struct cmd_find_state *fs)
685
0
{
686
0
  struct winlink  *wl;
687
688
0
  if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL)
689
0
    return (0);
690
691
0
  if (!session_alive(fs->s))
692
0
    return (0);
693
694
0
  RB_FOREACH(wl, winlinks, &fs->s->windows) {
695
0
    if (wl->window == fs->w && wl == fs->wl)
696
0
      break;
697
0
  }
698
0
  if (wl == NULL)
699
0
    return (0);
700
701
0
  if (fs->w != fs->wl->window)
702
0
    return (0);
703
704
0
  return (window_has_pane(fs->w, fs->wp));
705
0
}
706
707
/* Copy a state. */
708
void
709
cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src)
710
0
{
711
0
  dst->s = src->s;
712
0
  dst->wl = src->wl;
713
0
  dst->idx = src->idx;
714
0
  dst->w = src->w;
715
0
  dst->wp = src->wp;
716
0
}
717
718
/* Log the result. */
719
static void
720
cmd_find_log_state(const char *prefix, struct cmd_find_state *fs)
721
0
{
722
0
  if (fs->s != NULL)
723
0
    log_debug("%s: s=$%u %s", prefix, fs->s->id, fs->s->name);
724
0
  else
725
0
    log_debug("%s: s=none", prefix);
726
0
  if (fs->wl != NULL) {
727
0
    log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx,
728
0
        fs->wl->window == fs->w, fs->w->id, fs->w->name);
729
0
  } else
730
0
    log_debug("%s: wl=none", prefix);
731
0
  if (fs->wp != NULL)
732
0
    log_debug("%s: wp=%%%u", prefix, fs->wp->id);
733
0
  else
734
0
    log_debug("%s: wp=none", prefix);
735
0
  if (fs->idx != -1)
736
0
    log_debug("%s: idx=%d", prefix, fs->idx);
737
0
  else
738
0
    log_debug("%s: idx=none", prefix);
739
0
}
740
741
/* Find state from a session. */
742
void
743
cmd_find_from_session(struct cmd_find_state *fs, struct session *s, int flags)
744
0
{
745
0
  cmd_find_clear_state(fs, flags);
746
747
0
  fs->s = s;
748
0
  fs->wl = fs->s->curw;
749
0
  fs->w = fs->wl->window;
750
0
  fs->wp = fs->w->active;
751
752
0
  cmd_find_log_state(__func__, fs);
753
0
}
754
755
/* Find state from a winlink. */
756
void
757
cmd_find_from_winlink(struct cmd_find_state *fs, struct winlink *wl, int flags)
758
0
{
759
0
  cmd_find_clear_state(fs, flags);
760
761
0
  fs->s = wl->session;
762
0
  fs->wl = wl;
763
0
  fs->w = wl->window;
764
0
  fs->wp = wl->window->active;
765
766
0
  cmd_find_log_state(__func__, fs);
767
0
}
768
769
/* Find state from a session and window. */
770
int
771
cmd_find_from_session_window(struct cmd_find_state *fs, struct session *s,
772
    struct window *w, int flags)
773
0
{
774
0
  cmd_find_clear_state(fs, flags);
775
776
0
  fs->s = s;
777
0
  fs->w = w;
778
0
  if (cmd_find_best_winlink_with_window(fs) != 0) {
779
0
    cmd_find_clear_state(fs, flags);
780
0
    return (-1);
781
0
  }
782
0
  fs->wp = fs->w->active;
783
784
0
  cmd_find_log_state(__func__, fs);
785
0
  return (0);
786
0
}
787
788
/* Find state from a window. */
789
int
790
cmd_find_from_window(struct cmd_find_state *fs, struct window *w, int flags)
791
4.76k
{
792
4.76k
  cmd_find_clear_state(fs, flags);
793
794
4.76k
  fs->w = w;
795
4.76k
  if (cmd_find_best_session_with_window(fs) != 0) {
796
4.76k
    cmd_find_clear_state(fs, flags);
797
4.76k
    return (-1);
798
4.76k
  }
799
0
  if (cmd_find_best_winlink_with_window(fs) != 0) {
800
0
    cmd_find_clear_state(fs, flags);
801
0
    return (-1);
802
0
  }
803
0
  fs->wp = fs->w->active;
804
805
0
  cmd_find_log_state(__func__, fs);
806
0
  return (0);
807
0
}
808
809
/* Find state from a winlink and pane. */
810
void
811
cmd_find_from_winlink_pane(struct cmd_find_state *fs, struct winlink *wl,
812
    struct window_pane *wp, int flags)
813
0
{
814
0
  cmd_find_clear_state(fs, flags);
815
816
0
  fs->s = wl->session;
817
0
  fs->wl = wl;
818
0
  fs->idx = fs->wl->idx;
819
0
  fs->w = fs->wl->window;
820
0
  fs->wp = wp;
821
822
0
  cmd_find_log_state(__func__, fs);
823
0
}
824
825
/* Find state from a pane. */
826
int
827
cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp, int flags)
828
1.10k
{
829
1.10k
  if (cmd_find_from_window(fs, wp->window, flags) != 0)
830
1.10k
    return (-1);
831
0
  fs->wp = wp;
832
833
0
  cmd_find_log_state(__func__, fs);
834
0
  return (0);
835
1.10k
}
836
837
/* Find state from nothing. */
838
int
839
cmd_find_from_nothing(struct cmd_find_state *fs, int flags)
840
0
{
841
0
  cmd_find_clear_state(fs, flags);
842
843
0
  fs->s = cmd_find_best_session(NULL, 0, flags);
844
0
  if (fs->s == NULL) {
845
0
    cmd_find_clear_state(fs, flags);
846
0
    return (-1);
847
0
  }
848
0
  fs->wl = fs->s->curw;
849
0
  fs->idx = fs->wl->idx;
850
0
  fs->w = fs->wl->window;
851
0
  fs->wp = fs->w->active;
852
853
0
  cmd_find_log_state(__func__, fs);
854
0
  return (0);
855
0
}
856
857
/* Find state from mouse. */
858
int
859
cmd_find_from_mouse(struct cmd_find_state *fs, struct mouse_event *m, int flags)
860
0
{
861
0
  cmd_find_clear_state(fs, flags);
862
863
0
  if (!m->valid)
864
0
    return (-1);
865
866
0
  fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
867
0
  if (fs->wp == NULL) {
868
0
    cmd_find_clear_state(fs, flags);
869
0
    return (-1);
870
0
  }
871
0
  fs->w = fs->wl->window;
872
873
0
  cmd_find_log_state(__func__, fs);
874
0
  return (0);
875
0
}
876
877
/* Find state from client. */
878
int
879
cmd_find_from_client(struct cmd_find_state *fs, struct client *c, int flags)
880
0
{
881
0
  struct window_pane  *wp;
882
883
  /* If no client, treat as from nothing. */
884
0
  if (c == NULL)
885
0
    return (cmd_find_from_nothing(fs, flags));
886
887
  /* If this is an attached client, all done. */
888
0
  if (c->session != NULL) {
889
0
    cmd_find_clear_state(fs, flags);
890
891
0
    fs->wp = c->session->curw->window->active;
892
0
    if (fs->wp == NULL) {
893
0
      cmd_find_from_session(fs, c->session, flags);
894
0
      return (0);
895
0
    }
896
0
    fs->s = c->session;
897
0
    fs->wl = fs->s->curw;
898
0
    fs->w = fs->wl->window;
899
900
0
    cmd_find_log_state(__func__, fs);
901
0
    return (0);
902
0
  }
903
0
  cmd_find_clear_state(fs, flags);
904
905
  /*
906
   * If this is an unattached client running in a pane, we can use that
907
   * to limit the list of sessions to those containing that pane.
908
   */
909
0
  wp = cmd_find_inside_pane(c);
910
0
  if (wp == NULL)
911
0
    goto unknown_pane;
912
913
  /*
914
   * Don't have a session, or it doesn't have this pane. Try all
915
   * sessions.
916
   */
917
0
  fs->w = wp->window;
918
0
  if (cmd_find_best_session_with_window(fs) != 0) {
919
    /*
920
     * The window may have been destroyed but the pane
921
     * still on all_window_panes due to something else
922
     * holding a reference.
923
     */
924
0
    goto unknown_pane;
925
0
  }
926
0
  fs->wl = fs->s->curw;
927
0
  fs->w = fs->wl->window;
928
0
  fs->wp = fs->w->active; /* use active pane */
929
930
0
  cmd_find_log_state(__func__, fs);
931
0
  return (0);
932
933
0
unknown_pane:
934
  /* We can't find the pane so need to guess. */
935
0
  return (cmd_find_from_nothing(fs, flags));
936
0
}
937
938
/*
939
 * Split target into pieces and resolve for the given type. Fills in the given
940
 * state. Returns 0 on success or -1 on error.
941
 */
942
int
943
cmd_find_target(struct cmd_find_state *fs, struct cmdq_item *item,
944
    const char *target, enum cmd_find_type type, int flags)
945
0
{
946
0
  struct mouse_event  *m;
947
0
  struct client   *c;
948
0
  struct cmd_find_state  current;
949
0
  char      *colon, *period, *copy = NULL, tmp[256];
950
0
  const char    *session, *window, *pane, *s;
951
0
  int      window_only = 0, pane_only = 0;
952
953
  /* Can fail flag implies quiet. */
954
0
  if (flags & CMD_FIND_CANFAIL)
955
0
    flags |= CMD_FIND_QUIET;
956
957
  /* Log the arguments. */
958
0
  if (type == CMD_FIND_PANE)
959
0
    s = "pane";
960
0
  else if (type == CMD_FIND_WINDOW)
961
0
    s = "window";
962
0
  else if (type == CMD_FIND_SESSION)
963
0
    s = "session";
964
0
  else
965
0
    s = "unknown";
966
0
  *tmp = '\0';
967
0
  if (flags & CMD_FIND_PREFER_UNATTACHED)
968
0
    strlcat(tmp, "PREFER_UNATTACHED,", sizeof tmp);
969
0
  if (flags & CMD_FIND_QUIET)
970
0
    strlcat(tmp, "QUIET,", sizeof tmp);
971
0
  if (flags & CMD_FIND_WINDOW_INDEX)
972
0
    strlcat(tmp, "WINDOW_INDEX,", sizeof tmp);
973
0
  if (flags & CMD_FIND_DEFAULT_MARKED)
974
0
    strlcat(tmp, "DEFAULT_MARKED,", sizeof tmp);
975
0
  if (flags & CMD_FIND_EXACT_SESSION)
976
0
    strlcat(tmp, "EXACT_SESSION,", sizeof tmp);
977
0
  if (flags & CMD_FIND_EXACT_WINDOW)
978
0
    strlcat(tmp, "EXACT_WINDOW,", sizeof tmp);
979
0
  if (flags & CMD_FIND_CANFAIL)
980
0
    strlcat(tmp, "CANFAIL,", sizeof tmp);
981
0
  if (*tmp != '\0')
982
0
    tmp[strlen(tmp) - 1] = '\0';
983
0
  else
984
0
    strlcat(tmp, "NONE", sizeof tmp);
985
0
  log_debug("%s: target %s, type %s, item %p, flags %s", __func__,
986
0
      target == NULL ? "none" : target, s, item, tmp);
987
988
  /* Clear new state. */
989
0
  cmd_find_clear_state(fs, flags);
990
991
  /* Find current state. */
992
0
  if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
993
0
    fs->current = &marked_pane;
994
0
    log_debug("%s: current is marked pane", __func__);
995
0
  } else if (cmd_find_valid_state(cmdq_get_current(item))) {
996
0
    fs->current = cmdq_get_current(item);
997
0
    log_debug("%s: current is from queue", __func__);
998
0
  } else if (cmd_find_from_client(&current, cmdq_get_client(item),
999
0
      flags) == 0) {
1000
0
    fs->current = &current;
1001
0
    log_debug("%s: current is from client", __func__);
1002
0
  } else {
1003
0
    if (~flags & CMD_FIND_QUIET)
1004
0
      cmdq_error(item, "no current target");
1005
0
    goto error;
1006
0
  }
1007
0
  if (!cmd_find_valid_state(fs->current))
1008
0
    fatalx("invalid current find state");
1009
1010
  /* An empty or NULL target is the current. */
1011
0
  if (target == NULL || *target == '\0')
1012
0
    goto current;
1013
1014
0
  if (strcmp(target, "@") == 0 ||
1015
0
      strcmp(target, "{active}") == 0 ||
1016
0
      strcmp(target, "{current}") == 0) {
1017
0
    c = cmdq_get_client(item);
1018
0
    if (c == NULL || c->session == NULL) {
1019
0
      cmdq_error(item, "no current client");
1020
0
      goto error;
1021
0
    }
1022
0
    fs->wl = c->session->curw;
1023
0
    fs->wp = c->session->curw->window->active;
1024
0
    fs->w = c->session->curw->window;
1025
0
    goto found;
1026
0
  }
1027
1028
  /* Mouse target is a plain = or {mouse}. */
1029
0
  if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
1030
0
    m = &cmdq_get_event(item)->m;
1031
0
    switch (type) {
1032
0
    case CMD_FIND_PANE:
1033
0
      fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
1034
0
      if (fs->wp != NULL) {
1035
0
        fs->w = fs->wl->window;
1036
0
        break;
1037
0
      }
1038
      /* FALLTHROUGH */
1039
0
    case CMD_FIND_WINDOW:
1040
0
    case CMD_FIND_SESSION:
1041
0
      fs->wl = cmd_mouse_window(m, &fs->s);
1042
0
      if (fs->wl == NULL && fs->s != NULL)
1043
0
        fs->wl = fs->s->curw;
1044
0
      if (fs->wl != NULL) {
1045
0
        fs->w = fs->wl->window;
1046
0
        fs->wp = fs->w->active;
1047
0
      }
1048
0
      break;
1049
0
    }
1050
0
    if (fs->wp == NULL) {
1051
0
      if (~flags & CMD_FIND_QUIET)
1052
0
        cmdq_error(item, "no mouse target");
1053
0
      goto error;
1054
0
    }
1055
0
    goto found;
1056
0
  }
1057
1058
  /* Marked target is a plain ~ or {marked}. */
1059
0
  if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
1060
0
    if (!server_check_marked()) {
1061
0
      if (~flags & CMD_FIND_QUIET)
1062
0
        cmdq_error(item, "no marked target");
1063
0
      goto error;
1064
0
    }
1065
0
    cmd_find_copy_state(fs, &marked_pane);
1066
0
    goto found;
1067
0
  }
1068
1069
  /* Find separators if they exist. */
1070
0
  copy = xstrdup(target);
1071
0
  colon = strchr(copy, ':');
1072
0
  if (colon != NULL)
1073
0
    *colon++ = '\0';
1074
0
  if (colon == NULL)
1075
0
    period = strchr(copy, '.');
1076
0
  else
1077
0
    period = strchr(colon, '.');
1078
0
  if (period != NULL)
1079
0
    *period++ = '\0';
1080
1081
  /* Set session, window and pane parts. */
1082
0
  session = window = pane = NULL;
1083
0
  if (colon != NULL && period != NULL) {
1084
0
    session = copy;
1085
0
    window = colon;
1086
0
    window_only = 1;
1087
0
    pane = period;
1088
0
    pane_only = 1;
1089
0
  } else if (colon != NULL && period == NULL) {
1090
0
    session = copy;
1091
0
    window = colon;
1092
0
    window_only = 1;
1093
0
  } else if (colon == NULL && period != NULL) {
1094
0
    window = copy;
1095
0
    pane = period;
1096
0
    pane_only = 1;
1097
0
  } else {
1098
0
    if (*copy == '$')
1099
0
      session = copy;
1100
0
    else if (*copy == '@')
1101
0
      window = copy;
1102
0
    else if (*copy == '%')
1103
0
      pane = copy;
1104
0
    else {
1105
0
      switch (type) {
1106
0
      case CMD_FIND_SESSION:
1107
0
        session = copy;
1108
0
        break;
1109
0
      case CMD_FIND_WINDOW:
1110
0
        window = copy;
1111
0
        break;
1112
0
      case CMD_FIND_PANE:
1113
0
        pane = copy;
1114
0
        break;
1115
0
      }
1116
0
    }
1117
0
  }
1118
1119
  /* Set exact match flags. */
1120
0
  if (session != NULL && *session == '=') {
1121
0
    session++;
1122
0
    fs->flags |= CMD_FIND_EXACT_SESSION;
1123
0
  }
1124
0
  if (window != NULL && *window == '=') {
1125
0
    window++;
1126
0
    fs->flags |= CMD_FIND_EXACT_WINDOW;
1127
0
  }
1128
1129
  /* Empty is the same as NULL. */
1130
0
  if (session != NULL && *session == '\0')
1131
0
    session = NULL;
1132
0
  if (window != NULL && *window == '\0')
1133
0
    window = NULL;
1134
0
  if (pane != NULL && *pane == '\0')
1135
0
    pane = NULL;
1136
1137
  /* Map though conversion table. */
1138
0
  if (session != NULL)
1139
0
    session = cmd_find_map_table(cmd_find_session_table, session);
1140
0
  if (window != NULL)
1141
0
    window = cmd_find_map_table(cmd_find_window_table, window);
1142
0
  if (pane != NULL)
1143
0
    pane = cmd_find_map_table(cmd_find_pane_table, pane);
1144
1145
0
  if (session != NULL || window != NULL || pane != NULL) {
1146
0
    log_debug("%s: target %s is %s%s%s%s%s%s",
1147
0
        __func__, target,
1148
0
        session == NULL ? "" : "session ",
1149
0
        session == NULL ? "" : session,
1150
0
        window == NULL ? "" : "window ",
1151
0
        window == NULL ? "" : window,
1152
0
        pane == NULL ? "" : "pane ",
1153
0
        pane == NULL ? "" : pane);
1154
0
  }
1155
1156
  /* No pane is allowed if want an index. */
1157
0
  if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
1158
0
    if (~flags & CMD_FIND_QUIET)
1159
0
      cmdq_error(item, "can't specify pane here");
1160
0
    goto error;
1161
0
  }
1162
1163
  /* If the session isn't NULL, look it up. */
1164
0
  if (session != NULL) {
1165
    /* This will fill in session. */
1166
0
    if (cmd_find_get_session(fs, session) != 0)
1167
0
      goto no_session;
1168
1169
    /* If window and pane are NULL, use that session's current. */
1170
0
    if (window == NULL && pane == NULL) {
1171
0
      fs->wl = fs->s->curw;
1172
0
      fs->idx = -1;
1173
0
      fs->w = fs->wl->window;
1174
0
      fs->wp = fs->w->active;
1175
0
      goto found;
1176
0
    }
1177
1178
    /* If window is present but pane not, find window in session. */
1179
0
    if (window != NULL && pane == NULL) {
1180
      /* This will fill in winlink and window. */
1181
0
      if (cmd_find_get_window_with_session(fs, window) != 0)
1182
0
        goto no_window;
1183
0
      if (fs->wl != NULL) /* can be NULL if index only */
1184
0
        fs->wp = fs->wl->window->active;
1185
0
      goto found;
1186
0
    }
1187
1188
    /* If pane is present but window not, find pane. */
1189
0
    if (window == NULL && pane != NULL) {
1190
      /* This will fill in winlink and window and pane. */
1191
0
      if (cmd_find_get_pane_with_session(fs, pane) != 0)
1192
0
        goto no_pane;
1193
0
      goto found;
1194
0
    }
1195
1196
    /*
1197
     * If window and pane are present, find both in session. This
1198
     * will fill in winlink and window.
1199
     */
1200
0
    if (cmd_find_get_window_with_session(fs, window) != 0)
1201
0
      goto no_window;
1202
    /* This will fill in pane. */
1203
0
    if (cmd_find_get_pane_with_window(fs, pane) != 0)
1204
0
      goto no_pane;
1205
0
    goto found;
1206
0
  }
1207
1208
  /* No session. If window and pane, try them. */
1209
0
  if (window != NULL && pane != NULL) {
1210
    /* This will fill in session, winlink and window. */
1211
0
    if (cmd_find_get_window(fs, window, window_only) != 0)
1212
0
      goto no_window;
1213
    /* This will fill in pane. */
1214
0
    if (cmd_find_get_pane_with_window(fs, pane) != 0)
1215
0
      goto no_pane;
1216
0
    goto found;
1217
0
  }
1218
1219
  /* If just window is present, try it. */
1220
0
  if (window != NULL && pane == NULL) {
1221
    /* This will fill in session, winlink and window. */
1222
0
    if (cmd_find_get_window(fs, window, window_only) != 0)
1223
0
      goto no_window;
1224
0
    if (fs->wl != NULL) /* can be NULL if index only */
1225
0
      fs->wp = fs->wl->window->active;
1226
0
    goto found;
1227
0
  }
1228
1229
  /* If just pane is present, try it. */
1230
0
  if (window == NULL && pane != NULL) {
1231
    /* This will fill in session, winlink, window and pane. */
1232
0
    if (cmd_find_get_pane(fs, pane, pane_only) != 0)
1233
0
      goto no_pane;
1234
0
    goto found;
1235
0
  }
1236
1237
0
current:
1238
  /* Use the current session. */
1239
0
  cmd_find_copy_state(fs, fs->current);
1240
0
  if (flags & CMD_FIND_WINDOW_INDEX)
1241
0
    fs->idx = -1;
1242
0
  goto found;
1243
1244
0
error:
1245
0
  fs->current = NULL;
1246
0
  log_debug("%s: error", __func__);
1247
1248
0
  free(copy);
1249
0
  if (flags & CMD_FIND_CANFAIL)
1250
0
    return (0);
1251
0
  return (-1);
1252
1253
0
found:
1254
0
  fs->current = NULL;
1255
0
  cmd_find_log_state(__func__, fs);
1256
1257
0
  free(copy);
1258
0
  return (0);
1259
1260
0
no_session:
1261
0
  if (~flags & CMD_FIND_QUIET)
1262
0
    cmdq_error(item, "can't find session: %s", session);
1263
0
  goto error;
1264
1265
0
no_window:
1266
0
  if (~flags & CMD_FIND_QUIET)
1267
0
    cmdq_error(item, "can't find window: %s", window);
1268
0
  goto error;
1269
1270
0
no_pane:
1271
0
  if (~flags & CMD_FIND_QUIET)
1272
0
    cmdq_error(item, "can't find pane: %s", pane);
1273
0
  goto error;
1274
0
}
1275
1276
/* Find the current client. */
1277
static struct client *
1278
cmd_find_current_client(struct cmdq_item *item, int quiet)
1279
0
{
1280
0
  struct client   *c = NULL, *found;
1281
0
  struct session    *s;
1282
0
  struct window_pane  *wp;
1283
0
  struct cmd_find_state  fs;
1284
1285
0
  if (item != NULL)
1286
0
    c = cmdq_get_client(item);
1287
0
  if (c != NULL && c->session != NULL)
1288
0
    return (c);
1289
1290
0
  found = NULL;
1291
0
  if (c != NULL && (wp = cmd_find_inside_pane(c)) != NULL) {
1292
0
    cmd_find_clear_state(&fs, CMD_FIND_QUIET);
1293
0
    fs.w = wp->window;
1294
0
    if (cmd_find_best_session_with_window(&fs) == 0)
1295
0
      found = cmd_find_best_client(fs.s);
1296
0
  } else {
1297
0
    s = cmd_find_best_session(NULL, 0, CMD_FIND_QUIET);
1298
0
    if (s != NULL)
1299
0
      found = cmd_find_best_client(s);
1300
0
  }
1301
0
  if (found == NULL && item != NULL && !quiet)
1302
0
    cmdq_error(item, "no current client");
1303
0
  log_debug("%s: no target, return %p", __func__, found);
1304
0
  return (found);
1305
0
}
1306
1307
/* Find the target client or report an error and return NULL. */
1308
struct client *
1309
cmd_find_client(struct cmdq_item *item, const char *target, int quiet)
1310
0
{
1311
0
  struct client *c;
1312
0
  char    *copy;
1313
0
  size_t     size;
1314
1315
  /* A NULL argument means the current client. */
1316
0
  if (target == NULL)
1317
0
    return (cmd_find_current_client(item, quiet));
1318
0
  copy = xstrdup(target);
1319
1320
  /* Trim a single trailing colon if any. */
1321
0
  size = strlen(copy);
1322
0
  if (size != 0 && copy[size - 1] == ':')
1323
0
    copy[size - 1] = '\0';
1324
1325
  /* Check name and path of each client. */
1326
0
  TAILQ_FOREACH(c, &clients, entry) {
1327
0
    if (c->session == NULL)
1328
0
      continue;
1329
0
    if (strcmp(copy, c->name) == 0)
1330
0
      break;
1331
1332
0
    if (*c->ttyname == '\0')
1333
0
      continue;
1334
0
    if (strcmp(copy, c->ttyname) == 0)
1335
0
      break;
1336
0
    if (strncmp(c->ttyname, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
1337
0
      continue;
1338
0
    if (strcmp(copy, c->ttyname + (sizeof _PATH_DEV) - 1) == 0)
1339
0
      break;
1340
0
  }
1341
1342
  /* If no client found, report an error. */
1343
0
  if (c == NULL && !quiet)
1344
0
    cmdq_error(item, "can't find client: %s", copy);
1345
1346
0
  free(copy);
1347
0
  log_debug("%s: target %s, return %p", __func__, target, c);
1348
0
  return (c);
1349
0
}