Annotation of src/usr.bin/tmux/tmux.c, Revision 1.96
1.96 ! nicm 1: /* $OpenBSD: tmux.c,v 1.95 2010/12/06 22:51:02 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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: #include <sys/stat.h>
21:
22: #include <errno.h>
1.55 nicm 23: #include <event.h>
1.91 nicm 24: #include <fcntl.h>
1.1 nicm 25: #include <paths.h>
26: #include <pwd.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
31: #include "tmux.h"
32:
33: #ifdef DEBUG
1.54 nicm 34: extern char *malloc_options;
1.1 nicm 35: #endif
36:
1.63 nicm 37: struct options global_options; /* server options */
1.12 nicm 38: struct options global_s_options; /* session options */
39: struct options global_w_options; /* window options */
1.29 nicm 40: struct environ global_environ;
1.1 nicm 41:
1.78 nicm 42: struct event_base *ev_base;
43:
1.92 nicm 44: char *cfg_file;
45: char *shell_cmd;
1.1 nicm 46: int debug_level;
47: time_t start_time;
1.92 nicm 48: char socket_path[MAXPATHLEN];
1.41 nicm 49: int login_shell;
1.92 nicm 50: char *environ_path;
51: pid_t environ_pid;
52: u_int environ_idx;
1.70 nicm 53:
1.1 nicm 54: __dead void usage(void);
1.92 nicm 55: void parseenvironment(void);
56: char *makesocketpath(const char *);
1.55 nicm 57:
1.1 nicm 58: __dead void
59: usage(void)
60: {
1.4 sobrado 61: fprintf(stderr,
1.52 nicm 62: "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n"
1.41 nicm 63: " [-S socket-path] [command [flags]]\n",
1.1 nicm 64: __progname);
65: exit(1);
66: }
67:
68: void
69: logfile(const char *name)
70: {
71: char *path;
72:
73: log_close();
74: if (debug_level > 0) {
1.32 nicm 75: xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
1.1 nicm 76: log_open_file(debug_level, path);
77: xfree(path);
78: }
79: }
80:
1.39 nicm 81: const char *
82: getshell(void)
83: {
84: struct passwd *pw;
85: const char *shell;
86:
87: shell = getenv("SHELL");
88: if (checkshell(shell))
89: return (shell);
90:
91: pw = getpwuid(getuid());
92: if (pw != NULL && checkshell(pw->pw_shell))
93: return (pw->pw_shell);
94:
95: return (_PATH_BSHELL);
96: }
97:
98: int
99: checkshell(const char *shell)
100: {
101: if (shell == NULL || *shell == '\0' || areshell(shell))
102: return (0);
103: if (access(shell, X_OK) != 0)
104: return (0);
105: return (1);
106: }
107:
108: int
109: areshell(const char *shell)
110: {
111: const char *progname, *ptr;
112:
113: if ((ptr = strrchr(shell, '/')) != NULL)
114: ptr++;
115: else
116: ptr = shell;
117: progname = __progname;
118: if (*progname == '-')
119: progname++;
120: if (strcmp(ptr, progname) == 0)
121: return (1);
122: return (0);
123: }
124:
1.50 nicm 125: void
1.92 nicm 126: parseenvironment(void)
1.50 nicm 127: {
1.70 nicm 128: char *env, *path_pid, *pid_idx, buf[256];
1.50 nicm 129: size_t len;
130: const char *errstr;
131: long long ll;
132:
1.92 nicm 133: environ_pid = -1;
1.50 nicm 134: if ((env = getenv("TMUX")) == NULL)
135: return;
136:
1.70 nicm 137: if ((path_pid = strchr(env, ',')) == NULL || path_pid == env)
1.50 nicm 138: return;
1.70 nicm 139: if ((pid_idx = strchr(path_pid + 1, ',')) == NULL)
1.50 nicm 140: return;
1.70 nicm 141: if ((pid_idx == path_pid + 1 || pid_idx[1] == '\0'))
142: return;
143:
144: /* path */
145: len = path_pid - env;
1.92 nicm 146: environ_path = xmalloc(len + 1);
147: memcpy(environ_path, env, len);
148: environ_path[len] = '\0';
1.50 nicm 149:
1.70 nicm 150: /* pid */
151: len = pid_idx - path_pid - 1;
1.50 nicm 152: if (len > (sizeof buf) - 1)
153: return;
1.70 nicm 154: memcpy(buf, path_pid + 1, len);
1.50 nicm 155: buf[len] = '\0';
156:
157: ll = strtonum(buf, 0, LONG_MAX, &errstr);
158: if (errstr != NULL)
159: return;
1.92 nicm 160: environ_pid = ll;
1.50 nicm 161:
1.70 nicm 162: /* idx */
1.92 nicm 163: ll = strtonum(pid_idx + 1, 0, UINT_MAX, &errstr);
1.50 nicm 164: if (errstr != NULL)
165: return;
1.92 nicm 166: environ_idx = ll;
1.50 nicm 167: }
168:
1.1 nicm 169: char *
1.92 nicm 170: makesocketpath(const char *label)
1.1 nicm 171: {
172: char base[MAXPATHLEN], *path;
173: struct stat sb;
174: u_int uid;
175:
176: uid = getuid();
1.32 nicm 177: xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid);
1.1 nicm 178:
179: if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
180: return (NULL);
181:
182: if (lstat(base, &sb) != 0)
183: return (NULL);
184: if (!S_ISDIR(sb.st_mode)) {
185: errno = ENOTDIR;
186: return (NULL);
187: }
188: if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
189: errno = EACCES;
190: return (NULL);
191: }
192:
193: xasprintf(&path, "%s/%s", base, label);
194: return (path);
195: }
196:
1.55 nicm 197: __dead void
198: shell_exec(const char *shell, const char *shellcmd)
199: {
200: const char *shellname, *ptr;
201: char *argv0;
1.91 nicm 202: int mode;
1.55 nicm 203:
204: ptr = strrchr(shell, '/');
205: if (ptr != NULL && *(ptr + 1) != '\0')
206: shellname = ptr + 1;
207: else
208: shellname = shell;
209: if (login_shell)
210: xasprintf(&argv0, "-%s", shellname);
211: else
212: xasprintf(&argv0, "%s", shellname);
213: setenv("SHELL", shell, 1);
1.90 nicm 214:
1.91 nicm 215: if ((mode = fcntl(STDIN_FILENO, F_GETFL)) != -1)
216: fcntl(STDIN_FILENO, F_SETFL, mode & ~O_NONBLOCK);
217: if ((mode = fcntl(STDOUT_FILENO, F_GETFL)) != -1)
218: fcntl(STDOUT_FILENO, F_SETFL, mode & ~O_NONBLOCK);
219: if ((mode = fcntl(STDERR_FILENO, F_GETFL)) != -1)
220: fcntl(STDERR_FILENO, F_SETFL, mode & ~O_NONBLOCK);
1.90 nicm 221: closefrom(STDERR_FILENO + 1);
1.55 nicm 222:
223: execl(shell, argv0, "-c", shellcmd, (char *) NULL);
224: fatal("execl failed");
225: }
226:
1.1 nicm 227: int
228: main(int argc, char **argv)
229: {
1.92 nicm 230: struct passwd *pw;
231: struct options *oo, *so, *wo;
232: struct keylist *keylist;
233: char *s, *path, *label, *home, **var;
1.94 nicm 234: int opt, flags, quiet, keys;
1.54 nicm 235:
236: #ifdef DEBUG
237: malloc_options = (char *) "AFGJPX";
238: #endif
1.1 nicm 239:
1.94 nicm 240: quiet = flags = 0;
1.92 nicm 241: label = path = NULL;
1.41 nicm 242: login_shell = (**argv == '-');
1.46 nicm 243: while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) {
1.41 nicm 244: switch (opt) {
1.1 nicm 245: case '2':
246: flags |= IDENTIFY_256COLOURS;
247: flags &= ~IDENTIFY_88COLOURS;
248: break;
249: case '8':
250: flags |= IDENTIFY_88COLOURS;
251: flags &= ~IDENTIFY_256COLOURS;
252: break;
1.46 nicm 253: case 'c':
1.92 nicm 254: if (shell_cmd != NULL)
255: xfree(shell_cmd);
256: shell_cmd = xstrdup(optarg);
1.37 nicm 257: break;
1.1 nicm 258: case 'f':
1.42 nicm 259: if (cfg_file != NULL)
1.2 ray 260: xfree(cfg_file);
1.1 nicm 261: cfg_file = xstrdup(optarg);
1.41 nicm 262: break;
263: case 'l':
264: login_shell = 1;
1.1 nicm 265: break;
266: case 'L':
267: if (label != NULL)
268: xfree(label);
269: label = xstrdup(optarg);
270: break;
1.37 nicm 271: case 'q':
1.63 nicm 272: quiet = 1;
1.37 nicm 273: break;
1.1 nicm 274: case 'S':
275: if (path != NULL)
276: xfree(path);
277: path = xstrdup(optarg);
278: break;
279: case 'u':
280: flags |= IDENTIFY_UTF8;
281: break;
282: case 'v':
283: debug_level++;
284: break;
1.53 deraadt 285: default:
1.1 nicm 286: usage();
1.53 deraadt 287: }
288: }
1.1 nicm 289: argc -= optind;
290: argv += optind;
291:
1.92 nicm 292: if (shell_cmd != NULL && argc != 0)
1.46 nicm 293: usage();
294:
1.1 nicm 295: log_open_tty(debug_level);
296:
1.15 nicm 297: if (!(flags & IDENTIFY_UTF8)) {
298: /*
299: * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
300: * exist (in that order) to contain UTF-8, it is a safe
301: * assumption that either they are using a UTF-8 terminal, or
302: * if not they know that output from UTF-8-capable programs may
303: * be wrong.
304: */
305: if ((s = getenv("LC_ALL")) == NULL) {
306: if ((s = getenv("LC_CTYPE")) == NULL)
307: s = getenv("LANG");
308: }
1.26 nicm 309: if (s != NULL && (strcasestr(s, "UTF-8") != NULL ||
310: strcasestr(s, "UTF8") != NULL))
1.15 nicm 311: flags |= IDENTIFY_UTF8;
312: }
313:
1.42 nicm 314: environ_init(&global_environ);
1.62 nicm 315: for (var = environ; *var != NULL; var++)
1.42 nicm 316: environ_put(&global_environ, *var);
1.63 nicm 317:
318: options_init(&global_options, NULL);
319: oo = &global_options;
1.64 nicm 320: options_set_number(oo, "quiet", quiet);
1.65 nicm 321: options_set_number(oo, "escape-time", 500);
1.88 nicm 322: options_set_number(oo, "exit-unattached", 0);
1.42 nicm 323:
1.12 nicm 324: options_init(&global_s_options, NULL);
1.42 nicm 325: so = &global_s_options;
326: options_set_number(so, "base-index", 0);
327: options_set_number(so, "bell-action", BELL_ANY);
328: options_set_number(so, "buffer-limit", 9);
329: options_set_string(so, "default-command", "%s", "");
1.82 nicm 330: options_set_string(so, "default-path", "%s", "");
1.42 nicm 331: options_set_string(so, "default-shell", "%s", getshell());
332: options_set_string(so, "default-terminal", "screen");
1.88 nicm 333: options_set_number(so, "destroy-unattached", 0);
1.81 nicm 334: options_set_number(so, "detach-on-destroy", 1);
335: options_set_number(so, "display-panes-active-colour", 1);
1.42 nicm 336: options_set_number(so, "display-panes-colour", 4);
337: options_set_number(so, "display-panes-time", 1000);
338: options_set_number(so, "display-time", 750);
339: options_set_number(so, "history-limit", 2000);
340: options_set_number(so, "lock-after-time", 0);
1.45 nicm 341: options_set_string(so, "lock-command", "lock -np");
1.48 nicm 342: options_set_number(so, "lock-server", 1);
1.42 nicm 343: options_set_number(so, "message-attr", 0);
344: options_set_number(so, "message-bg", 3);
345: options_set_number(so, "message-fg", 0);
1.58 nicm 346: options_set_number(so, "message-limit", 20);
1.49 nicm 347: options_set_number(so, "mouse-select-pane", 0);
1.75 nicm 348: options_set_number(so, "pane-active-border-bg", 8);
349: options_set_number(so, "pane-active-border-fg", 2);
1.66 nicm 350: options_set_number(so, "pane-border-bg", 8);
351: options_set_number(so, "pane-border-fg", 8);
1.42 nicm 352: options_set_number(so, "repeat-time", 500);
353: options_set_number(so, "set-remain-on-exit", 0);
354: options_set_number(so, "set-titles", 0);
1.43 nicm 355: options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\"");
1.42 nicm 356: options_set_number(so, "status", 1);
357: options_set_number(so, "status-attr", 0);
358: options_set_number(so, "status-bg", 2);
359: options_set_number(so, "status-fg", 0);
360: options_set_number(so, "status-interval", 15);
361: options_set_number(so, "status-justify", 0);
362: options_set_string(so, "status-left", "[#S]");
363: options_set_number(so, "status-left-attr", 0);
364: options_set_number(so, "status-left-bg", 8);
365: options_set_number(so, "status-left-fg", 8);
366: options_set_number(so, "status-left-length", 10);
367: options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
368: options_set_number(so, "status-right-attr", 0);
369: options_set_number(so, "status-right-bg", 8);
370: options_set_number(so, "status-right-fg", 8);
371: options_set_number(so, "status-right-length", 40);
372: options_set_string(so, "terminal-overrides",
373: "*88col*:colors=88,*256col*:colors=256");
1.93 nicm 374: options_set_string(so, "update-environment",
375: "DISPLAY "
376: "SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION "
377: "WINDOWID "
378: "XAUTHORITY");
1.42 nicm 379: options_set_number(so, "visual-activity", 0);
380: options_set_number(so, "visual-bell", 0);
381: options_set_number(so, "visual-content", 0);
1.95 nicm 382: options_set_number(so, "visual-silence", 0);
1.44 nicm 383:
384: keylist = xmalloc(sizeof *keylist);
385: ARRAY_INIT(keylist);
386: ARRAY_ADD(keylist, '\002');
387: options_set_data(so, "prefix", keylist, xfree);
1.1 nicm 388:
1.12 nicm 389: options_init(&global_w_options, NULL);
1.42 nicm 390: wo = &global_w_options;
391: options_set_number(wo, "aggressive-resize", 0);
1.72 nicm 392: options_set_number(wo, "alternate-screen", 1);
1.42 nicm 393: options_set_number(wo, "automatic-rename", 1);
394: options_set_number(wo, "clock-mode-colour", 4);
395: options_set_number(wo, "clock-mode-style", 1);
396: options_set_number(wo, "force-height", 0);
397: options_set_number(wo, "force-width", 0);
398: options_set_number(wo, "main-pane-height", 24);
1.96 ! nicm 399: options_set_number(wo, "main-pane-width", 80);
1.42 nicm 400: options_set_number(wo, "mode-attr", 0);
401: options_set_number(wo, "mode-bg", 3);
402: options_set_number(wo, "mode-fg", 0);
403: options_set_number(wo, "mode-mouse", 0);
404: options_set_number(wo, "monitor-activity", 0);
405: options_set_string(wo, "monitor-content", "%s", "");
1.95 nicm 406: options_set_number(wo, "monitor-silence", 0);
1.42 nicm 407: options_set_number(wo, "window-status-attr", 0);
408: options_set_number(wo, "window-status-bg", 8);
409: options_set_number(wo, "window-status-current-attr", 0);
410: options_set_number(wo, "window-status-current-bg", 8);
411: options_set_number(wo, "window-status-current-fg", 8);
412: options_set_number(wo, "window-status-fg", 8);
1.79 nicm 413: options_set_number(wo, "window-status-alert-attr", GRID_ATTR_REVERSE);
414: options_set_number(wo, "window-status-alert-bg", 8);
415: options_set_number(wo, "window-status-alert-fg", 8);
1.60 nicm 416: options_set_string(wo, "window-status-format", "#I:#W#F");
417: options_set_string(wo, "window-status-current-format", "#I:#W#F");
1.73 nicm 418: options_set_string(wo, "word-separators", " -_@");
1.59 nicm 419: options_set_number(wo, "xterm-keys", 0);
1.62 nicm 420: options_set_number(wo, "remain-on-exit", 0);
1.47 nicm 421: options_set_number(wo, "synchronize-panes", 0);
1.42 nicm 422:
1.62 nicm 423: if (flags & IDENTIFY_UTF8) {
1.42 nicm 424: options_set_number(so, "status-utf8", 1);
425: options_set_number(wo, "utf8", 1);
426: } else {
427: options_set_number(so, "status-utf8", 0);
428: options_set_number(wo, "utf8", 0);
429: }
1.94 nicm 430:
431: keys = MODEKEY_EMACS;
432: if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
433: if (strrchr(s, '/') != NULL)
434: s = strrchr(s, '/') + 1;
435: if (strstr(s, "vi") != NULL)
436: keys = MODEKEY_VI;
437: }
438: options_set_number(so, "status-keys", keys);
439: options_set_number(wo, "mode-keys", keys);
1.1 nicm 440:
1.92 nicm 441: /* Locate the configuration file. */
1.1 nicm 442: if (cfg_file == NULL) {
443: home = getenv("HOME");
444: if (home == NULL || *home == '\0') {
445: pw = getpwuid(getuid());
446: if (pw != NULL)
447: home = pw->pw_dir;
448: }
449: xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
1.69 nicm 450: if (access(cfg_file, R_OK) != 0 && errno == ENOENT) {
1.1 nicm 451: xfree(cfg_file);
452: cfg_file = NULL;
453: }
454: }
1.62 nicm 455:
1.68 nicm 456: /*
1.92 nicm 457: * Figure out the socket path. If specified on the command-line with -S
458: * or -L, use it, otherwise try $TMUX or assume -L default.
1.68 nicm 459: */
1.92 nicm 460: parseenvironment();
1.68 nicm 461: if (path == NULL) {
1.92 nicm 462: /* If no -L, use the environment. */
1.68 nicm 463: if (label == NULL) {
1.92 nicm 464: if (environ_path != NULL)
465: path = xstrdup(environ_path);
466: else
1.68 nicm 467: label = xstrdup("default");
468: }
469:
470: /* -L or default set. */
471: if (label != NULL) {
1.92 nicm 472: if ((path = makesocketpath(label)) == NULL) {
1.68 nicm 473: log_warn("can't create socket");
474: exit(1);
475: }
476: }
1.1 nicm 477: }
1.68 nicm 478: if (label != NULL)
479: xfree(label);
1.92 nicm 480: if (realpath(path, socket_path) == NULL)
481: strlcpy(socket_path, path, sizeof socket_path);
482: xfree(path);
1.1 nicm 483:
1.92 nicm 484: /* Set process title. */
485: setproctitle("%s (%s)", __progname, socket_path);
1.1 nicm 486:
1.92 nicm 487: /* Pass control to the client. */
1.78 nicm 488: ev_base = event_init();
1.92 nicm 489: exit(client_main(argc, argv, flags));
1.1 nicm 490: }