4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
35 extern char *malloc_options
;
38 struct options global_options
; /* server options */
39 struct options global_s_options
; /* session options */
40 struct options global_w_options
; /* window options */
41 struct environ global_environ
;
43 struct event_base
*ev_base
;
49 char socket_path
[MAXPATHLEN
];
53 __dead
void usage(void);
54 char *makesocketpath(const char *);
60 "usage: %s [-2lquv] [-c shell-command] [-f file] [-L socket-name]\n"
61 " [-S socket-path] [command [flags]]\n",
67 logfile(const char *name
)
71 if (debug_level
> 0) {
72 xasprintf(&path
, "tmux-%s-%ld.log", name
, (long) getpid());
73 log_open(debug_level
, path
);
84 shell
= getenv("SHELL");
85 if (checkshell(shell
))
88 pw
= getpwuid(getuid());
89 if (pw
!= NULL
&& checkshell(pw
->pw_shell
))
90 return (pw
->pw_shell
);
92 return (_PATH_BSHELL
);
96 checkshell(const char *shell
)
98 if (shell
== NULL
|| *shell
== '\0' || *shell
!= '/')
102 if (access(shell
, X_OK
) != 0)
108 areshell(const char *shell
)
110 const char *progname
, *ptr
;
112 if ((ptr
= strrchr(shell
, '/')) != NULL
)
116 progname
= __progname
;
117 if (*progname
== '-')
119 if (strcmp(ptr
, progname
) == 0)
125 makesocketpath(const char *label
)
127 char base
[MAXPATHLEN
], realbase
[MAXPATHLEN
], *path
, *s
;
132 if ((s
= getenv("TMUX_TMPDIR")) != NULL
&& *s
!= '\0')
133 xsnprintf(base
, sizeof base
, "%s/", s
);
134 else if ((s
= getenv("TMPDIR")) != NULL
&& *s
!= '\0')
135 xsnprintf(base
, sizeof base
, "%s/tmux-%u", s
, uid
);
137 xsnprintf(base
, sizeof base
, "%s/tmux-%u", _PATH_TMP
, uid
);
139 if (mkdir(base
, S_IRWXU
) != 0 && errno
!= EEXIST
)
142 if (lstat(base
, &sb
) != 0)
144 if (!S_ISDIR(sb
.st_mode
)) {
148 if (sb
.st_uid
!= uid
|| (!S_ISDIR(sb
.st_mode
) &&
149 sb
.st_mode
& (S_IRWXG
|S_IRWXO
)) != 0) {
154 if (realpath(base
, realbase
) == NULL
)
155 strlcpy(realbase
, base
, sizeof realbase
);
157 xasprintf(&path
, "%s/%s", realbase
, label
);
162 setblocking(int fd
, int state
)
166 if ((mode
= fcntl(fd
, F_GETFL
)) != -1) {
171 fcntl(fd
, F_SETFL
, mode
);
176 shell_exec(const char *shell
, const char *shellcmd
)
178 const char *shellname
, *ptr
;
181 ptr
= strrchr(shell
, '/');
182 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
187 xasprintf(&argv0
, "-%s", shellname
);
189 xasprintf(&argv0
, "%s", shellname
);
190 setenv("SHELL", shell
, 1);
192 setblocking(STDIN_FILENO
, 1);
193 setblocking(STDOUT_FILENO
, 1);
194 setblocking(STDERR_FILENO
, 1);
195 closefrom(STDERR_FILENO
+ 1);
197 execl(shell
, argv0
, "-c", shellcmd
, (char *) NULL
);
198 fatal("execl failed");
202 main(int argc
, char **argv
)
205 char *s
, *path
, *label
, **var
, tmp
[MAXPATHLEN
];
209 int opt
, flags
, quiet
, keys
, session
;
212 malloc_options
= (char *) "AFGJPX";
215 setlocale(LC_TIME
, "");
219 login_shell
= (**argv
== '-');
220 while ((opt
= getopt(argc
, argv
, "2c:Cdf:lL:qS:uUv")) != -1) {
223 flags
|= CLIENT_256COLOURS
;
227 shell_cmd
= xstrdup(optarg
);
230 if (flags
& CLIENT_CONTROL
)
231 flags
|= CLIENT_CONTROLCONTROL
;
233 flags
|= CLIENT_CONTROL
;
237 cfg_file
= xstrdup(optarg
);
244 label
= xstrdup(optarg
);
251 path
= xstrdup(optarg
);
254 flags
|= CLIENT_UTF8
;
266 if (shell_cmd
!= NULL
&& argc
!= 0)
269 if (!(flags
& CLIENT_UTF8
)) {
271 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
272 * exist (in that order) to contain UTF-8, it is a safe
273 * assumption that either they are using a UTF-8 terminal, or
274 * if not they know that output from UTF-8-capable programs may
277 if ((s
= getenv("LC_ALL")) == NULL
|| *s
== '\0') {
278 if ((s
= getenv("LC_CTYPE")) == NULL
|| *s
== '\0')
281 if (s
!= NULL
&& (strcasestr(s
, "UTF-8") != NULL
||
282 strcasestr(s
, "UTF8") != NULL
))
283 flags
|= CLIENT_UTF8
;
286 environ_init(&global_environ
);
287 for (var
= environ
; *var
!= NULL
; var
++)
288 environ_put(&global_environ
, *var
);
289 if (getcwd(tmp
, sizeof tmp
) != NULL
)
290 environ_set(&global_environ
, "PWD", tmp
);
292 options_init(&global_options
, NULL
);
293 options_table_populate_tree(server_options_table
, &global_options
);
294 options_set_number(&global_options
, "quiet", quiet
);
296 options_init(&global_s_options
, NULL
);
297 options_table_populate_tree(session_options_table
, &global_s_options
);
298 options_set_string(&global_s_options
, "default-shell", "%s", getshell());
300 options_init(&global_w_options
, NULL
);
301 options_table_populate_tree(window_options_table
, &global_w_options
);
303 /* Enable UTF-8 if the first client is on UTF-8 terminal. */
304 if (flags
& CLIENT_UTF8
) {
305 options_set_number(&global_s_options
, "status-utf8", 1);
306 options_set_number(&global_s_options
, "mouse-utf8", 1);
307 options_set_number(&global_w_options
, "utf8", 1);
310 /* Override keys to vi if VISUAL or EDITOR are set. */
311 if ((s
= getenv("VISUAL")) != NULL
|| (s
= getenv("EDITOR")) != NULL
) {
312 if (strrchr(s
, '/') != NULL
)
313 s
= strrchr(s
, '/') + 1;
314 if (strstr(s
, "vi") != NULL
)
317 keys
= MODEKEY_EMACS
;
318 options_set_number(&global_s_options
, "status-keys", keys
);
319 options_set_number(&global_w_options
, "mode-keys", keys
);
322 /* Locate the configuration file. */
323 if (cfg_file
== NULL
) {
324 home
= getenv("HOME");
325 if (home
== NULL
|| *home
== '\0') {
326 pw
= getpwuid(getuid());
333 xasprintf(&cfg_file
, "%s/.tmux.conf", home
);
334 if (access(cfg_file
, R_OK
) != 0 && errno
== ENOENT
) {
341 /* Get path from environment. */
343 if (s
!= NULL
&& sscanf(s
, "%255[^,],%lld,%d", in
, &pid
, &session
) == 3)
344 environ_path
= xstrdup(in
);
347 * Figure out the socket path. If specified on the command-line with -S
348 * or -L, use it, otherwise try $TMUX or assume -L default.
351 /* If no -L, use the environment. */
353 if (environ_path
!= NULL
)
354 path
= xstrdup(environ_path
);
356 label
= xstrdup("default");
359 /* -L or default set. */
361 if ((path
= makesocketpath(label
)) == NULL
) {
362 fprintf(stderr
, "can't create socket: %s\n",
370 if (strlcpy(socket_path
, path
, sizeof socket_path
) >= sizeof socket_path
) {
371 fprintf(stderr
, "socket path too long: %s\n", path
);
376 /* Set process title. */
377 setproctitle("%s (%s)", __progname
, socket_path
);
379 /* Pass control to the client. */
380 ev_base
= event_init();
381 exit(client_main(argc
, argv
, flags
));