Coverage Report

Created: 2024-09-08 06:17

/src/tmux/cmd-new-session.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2007 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 <errno.h>
22
#include <fcntl.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <termios.h>
26
#include <unistd.h>
27
28
#include "tmux.h"
29
30
/*
31
 * Create a new session and attach to the current terminal unless -d is given.
32
 */
33
34
0
#define NEW_SESSION_TEMPLATE "#{session_name}:"
35
36
static enum cmd_retval  cmd_new_session_exec(struct cmd *, struct cmdq_item *);
37
38
const struct cmd_entry cmd_new_session_entry = {
39
  .name = "new-session",
40
  .alias = "new",
41
42
  .args = { "Ac:dDe:EF:f:n:Ps:t:x:Xy:", 0, -1, NULL },
43
  .usage = "[-AdDEPX] [-c start-directory] [-e environment] [-F format] "
44
     "[-f flags] [-n window-name] [-s session-name] "
45
     CMD_TARGET_SESSION_USAGE " [-x width] [-y height] "
46
     "[shell-command]",
47
48
  .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
49
50
  .flags = CMD_STARTSERVER,
51
  .exec = cmd_new_session_exec
52
};
53
54
const struct cmd_entry cmd_has_session_entry = {
55
  .name = "has-session",
56
  .alias = "has",
57
58
  .args = { "t:", 0, 0, NULL },
59
  .usage = CMD_TARGET_SESSION_USAGE,
60
61
  .target = { 't', CMD_FIND_SESSION, 0 },
62
63
  .flags = 0,
64
  .exec = cmd_new_session_exec
65
};
66
67
static enum cmd_retval
68
cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
69
0
{
70
0
  struct args   *args = cmd_get_args(self);
71
0
  struct cmd_find_state *current = cmdq_get_current(item);
72
0
  struct cmd_find_state *target = cmdq_get_target(item);
73
0
  struct client   *c = cmdq_get_client(item);
74
0
  struct session    *s, *as, *groupwith = NULL;
75
0
  struct environ    *env;
76
0
  struct options    *oo;
77
0
  struct termios     tio, *tiop;
78
0
  struct session_group  *sg = NULL;
79
0
  const char    *errstr, *template, *group, *tmp;
80
0
  char      *cause, *cwd = NULL, *cp, *newname = NULL;
81
0
  char      *name, *prefix = NULL;
82
0
  int      detached, already_attached, is_control = 0;
83
0
  u_int      sx, sy, dsx, dsy, count = args_count(args);
84
0
  struct spawn_context   sc = { 0 };
85
0
  enum cmd_retval    retval;
86
0
  struct cmd_find_state    fs;
87
0
  struct args_value *av;
88
89
0
  if (cmd_get_entry(self) == &cmd_has_session_entry) {
90
    /*
91
     * cmd_find_target() will fail if the session cannot be found,
92
     * so always return success here.
93
     */
94
0
    return (CMD_RETURN_NORMAL);
95
0
  }
96
97
0
  if (args_has(args, 't') && (count != 0 || args_has(args, 'n'))) {
98
0
    cmdq_error(item, "command or window name given with target");
99
0
    return (CMD_RETURN_ERROR);
100
0
  }
101
102
0
  tmp = args_get(args, 's');
103
0
  if (tmp != NULL) {
104
0
    name = format_single(item, tmp, c, NULL, NULL, NULL);
105
0
    newname = session_check_name(name);
106
0
    if (newname == NULL) {
107
0
      cmdq_error(item, "invalid session: %s", name);
108
0
      free(name);
109
0
      return (CMD_RETURN_ERROR);
110
0
    }
111
0
    free(name);
112
0
  }
113
0
  if (args_has(args, 'A')) {
114
0
    if (newname != NULL)
115
0
      as = session_find(newname);
116
0
    else
117
0
      as = target->s;
118
0
    if (as != NULL) {
119
0
      retval = cmd_attach_session(item, as->name,
120
0
          args_has(args, 'D'), args_has(args, 'X'), 0, NULL,
121
0
          args_has(args, 'E'), args_get(args, 'f'));
122
0
      free(newname);
123
0
      return (retval);
124
0
    }
125
0
  }
126
0
  if (newname != NULL && session_find(newname) != NULL) {
127
0
    cmdq_error(item, "duplicate session: %s", newname);
128
0
    goto fail;
129
0
  }
130
131
  /* Is this going to be part of a session group? */
132
0
  group = args_get(args, 't');
133
0
  if (group != NULL) {
134
0
    groupwith = target->s;
135
0
    if (groupwith == NULL)
136
0
      sg = session_group_find(group);
137
0
    else
138
0
      sg = session_group_contains(groupwith);
139
0
    if (sg != NULL)
140
0
      prefix = xstrdup(sg->name);
141
0
    else if (groupwith != NULL)
142
0
      prefix = xstrdup(groupwith->name);
143
0
    else {
144
0
      prefix = session_check_name(group);
145
0
      if (prefix == NULL) {
146
0
        cmdq_error(item, "invalid session group: %s",
147
0
            group);
148
0
        goto fail;
149
0
      }
150
0
    }
151
0
  }
152
153
  /* Set -d if no client. */
154
0
  detached = args_has(args, 'd');
155
0
  if (c == NULL)
156
0
    detached = 1;
157
0
  else if (c->flags & CLIENT_CONTROL)
158
0
    is_control = 1;
159
160
  /* Is this client already attached? */
161
0
  already_attached = 0;
162
0
  if (c != NULL && c->session != NULL)
163
0
    already_attached = 1;
164
165
  /* Get the new session working directory. */
166
0
  if ((tmp = args_get(args, 'c')) != NULL)
167
0
    cwd = format_single(item, tmp, c, NULL, NULL, NULL);
168
0
  else
169
0
    cwd = xstrdup(server_client_get_cwd(c, NULL));
170
171
  /*
172
   * If this is a new client, check for nesting and save the termios
173
   * settings (part of which is used for new windows in this session).
174
   *
175
   * tcgetattr() is used rather than using tty.tio since if the client is
176
   * detached, tty_open won't be called. It must be done before opening
177
   * the terminal as that calls tcsetattr() to prepare for tmux taking
178
   * over.
179
   */
180
0
  if (!detached &&
181
0
      !already_attached &&
182
0
      c->fd != -1 &&
183
0
      (~c->flags & CLIENT_CONTROL)) {
184
0
    if (server_client_check_nested(cmdq_get_client(item))) {
185
0
      cmdq_error(item, "sessions should be nested with care, "
186
0
          "unset $TMUX to force");
187
0
      goto fail;
188
0
    }
189
0
    if (tcgetattr(c->fd, &tio) != 0)
190
0
      fatal("tcgetattr failed");
191
0
    tiop = &tio;
192
0
  } else
193
0
    tiop = NULL;
194
195
  /* Open the terminal if necessary. */
196
0
  if (!detached && !already_attached) {
197
0
    if (server_client_open(c, &cause) != 0) {
198
0
      cmdq_error(item, "open terminal failed: %s", cause);
199
0
      free(cause);
200
0
      goto fail;
201
0
    }
202
0
  }
203
204
  /* Get default session size. */
205
0
  if (args_has(args, 'x')) {
206
0
    tmp = args_get(args, 'x');
207
0
    if (strcmp(tmp, "-") == 0) {
208
0
      if (c != NULL)
209
0
        dsx = c->tty.sx;
210
0
      else
211
0
        dsx = 80;
212
0
    } else {
213
0
      dsx = strtonum(tmp, 1, USHRT_MAX, &errstr);
214
0
      if (errstr != NULL) {
215
0
        cmdq_error(item, "width %s", errstr);
216
0
        goto fail;
217
0
      }
218
0
    }
219
0
  } else
220
0
    dsx = 80;
221
0
  if (args_has(args, 'y')) {
222
0
    tmp = args_get(args, 'y');
223
0
    if (strcmp(tmp, "-") == 0) {
224
0
      if (c != NULL)
225
0
        dsy = c->tty.sy;
226
0
      else
227
0
        dsy = 24;
228
0
    } else {
229
0
      dsy = strtonum(tmp, 1, USHRT_MAX, &errstr);
230
0
      if (errstr != NULL) {
231
0
        cmdq_error(item, "height %s", errstr);
232
0
        goto fail;
233
0
      }
234
0
    }
235
0
  } else
236
0
    dsy = 24;
237
238
  /* Find new session size. */
239
0
  if (!detached && !is_control) {
240
0
    sx = c->tty.sx;
241
0
    sy = c->tty.sy;
242
0
    if (sy > 0 && options_get_number(global_s_options, "status"))
243
0
      sy--;
244
0
  } else {
245
0
    tmp = options_get_string(global_s_options, "default-size");
246
0
    if (sscanf(tmp, "%ux%u", &sx, &sy) != 2) {
247
0
      sx = dsx;
248
0
      sy = dsy;
249
0
    } else {
250
0
      if (args_has(args, 'x'))
251
0
        sx = dsx;
252
0
      if (args_has(args, 'y'))
253
0
        sy = dsy;
254
0
    }
255
0
  }
256
0
  if (sx == 0)
257
0
    sx = 1;
258
0
  if (sy == 0)
259
0
    sy = 1;
260
261
  /* Create the new session. */
262
0
  oo = options_create(global_s_options);
263
0
  if (args_has(args, 'x') || args_has(args, 'y')) {
264
0
    if (!args_has(args, 'x'))
265
0
      dsx = sx;
266
0
    if (!args_has(args, 'y'))
267
0
      dsy = sy;
268
0
    options_set_string(oo, "default-size", 0, "%ux%u", dsx, dsy);
269
0
  }
270
0
  env = environ_create();
271
0
  if (c != NULL && !args_has(args, 'E'))
272
0
    environ_update(global_s_options, c->environ, env);
273
0
  av = args_first_value(args, 'e');
274
0
  while (av != NULL) {
275
0
    environ_put(env, av->string, 0);
276
0
    av = args_next_value(av);
277
0
  }
278
0
  s = session_create(prefix, newname, cwd, env, oo, tiop);
279
280
  /* Spawn the initial window. */
281
0
  sc.item = item;
282
0
  sc.s = s;
283
0
  if (!detached)
284
0
    sc.tc = c;
285
286
0
  sc.name = args_get(args, 'n');
287
0
  args_to_vector(args, &sc.argc, &sc.argv);
288
289
0
  sc.idx = -1;
290
0
  sc.cwd = args_get(args, 'c');
291
292
0
  sc.flags = 0;
293
294
0
  if (spawn_window(&sc, &cause) == NULL) {
295
0
    session_destroy(s, 0, __func__);
296
0
    cmdq_error(item, "create window failed: %s", cause);
297
0
    free(cause);
298
0
    goto fail;
299
0
  }
300
301
  /*
302
   * If a target session is given, this is to be part of a session group,
303
   * so add it to the group and synchronize.
304
   */
305
0
  if (group != NULL) {
306
0
    if (sg == NULL) {
307
0
      if (groupwith != NULL) {
308
0
        sg = session_group_new(groupwith->name);
309
0
        session_group_add(sg, groupwith);
310
0
      } else
311
0
        sg = session_group_new(group);
312
0
    }
313
0
    session_group_add(sg, s);
314
0
    session_group_synchronize_to(s);
315
0
    session_select(s, RB_MIN(winlinks, &s->windows)->idx);
316
0
  }
317
0
  notify_session("session-created", s);
318
319
  /*
320
   * Set the client to the new session. If a command client exists, it is
321
   * taking this session and needs to get MSG_READY and stay around.
322
   */
323
0
  if (!detached) {
324
0
    if (args_has(args, 'f'))
325
0
      server_client_set_flags(c, args_get(args, 'f'));
326
0
    if (!already_attached) {
327
0
      if (~c->flags & CLIENT_CONTROL)
328
0
        proc_send(c->peer, MSG_READY, -1, NULL, 0);
329
0
    } else if (c->session != NULL)
330
0
      c->last_session = c->session;
331
0
    server_client_set_session(c, s);
332
0
    if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
333
0
      server_client_set_key_table(c, NULL);
334
0
  }
335
336
  /* Print if requested. */
337
0
  if (args_has(args, 'P')) {
338
0
    if ((template = args_get(args, 'F')) == NULL)
339
0
      template = NEW_SESSION_TEMPLATE;
340
0
    cp = format_single(item, template, c, s, s->curw, NULL);
341
0
    cmdq_print(item, "%s", cp);
342
0
    free(cp);
343
0
  }
344
345
0
  if (!detached)
346
0
    c->flags |= CLIENT_ATTACHED;
347
0
  if (!args_has(args, 'd'))
348
0
    cmd_find_from_session(current, s, 0);
349
350
0
  cmd_find_from_session(&fs, s, 0);
351
0
  cmdq_insert_hook(s, item, &fs, "after-new-session");
352
353
0
  if (cfg_finished)
354
0
    cfg_show_causes(s);
355
356
0
  if (sc.argv != NULL)
357
0
    cmd_free_argv(sc.argc, sc.argv);
358
0
  free(cwd);
359
0
  free(newname);
360
0
  free(prefix);
361
0
  return (CMD_RETURN_NORMAL);
362
363
0
fail:
364
0
  if (sc.argv != NULL)
365
0
    cmd_free_argv(sc.argc, sc.argv);
366
0
  free(cwd);
367
0
  free(newname);
368
0
  free(prefix);
369
0
  return (CMD_RETURN_ERROR);
370
0
}