Coverage Report

Created: 2025-08-08 06:46

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