Be more clever about picking window name.

This commit is contained in:
Nicholas Marriott 2009-01-26 22:57:20 +00:00
parent 4d7e555a48
commit 9cde0c2477
8 changed files with 173 additions and 36 deletions

View File

@ -1,5 +1,6 @@
26 January 2009 26 January 2009
* Be more clever about picking the right process to create the window name.
* Don't balls up the terminal on UTF-8 combined characters. Don't support them * Don't balls up the terminal on UTF-8 combined characters. Don't support them
properly either - they are just discarded for the moment. properly either - they are just discarded for the moment.
@ -1013,7 +1014,7 @@
(including mutt, emacs). No status bar yet and no key remapping or other (including mutt, emacs). No status bar yet and no key remapping or other
customisation. customisation.
$Id: CHANGES,v 1.235 2009-01-26 20:57:42 nicm Exp $ $Id: CHANGES,v 1.236 2009-01-26 22:57:18 nicm Exp $
LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr
LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms

26
names.c
View File

@ -1,4 +1,4 @@
/* $Id: names.c,v 1.1 2009-01-20 19:35:03 nicm Exp $ */ /* $Id: names.c,v 1.2 2009-01-26 22:57:19 nicm Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -31,8 +31,7 @@ set_window_names(void)
{ {
struct window *w; struct window *w;
u_int i; u_int i;
pid_t pgrp; char *name, *wname;
char *name;
struct timeval tv, tv2; struct timeval tv, tv2;
if (gettimeofday(&tv, NULL) != 0) if (gettimeofday(&tv, NULL) != 0)
@ -53,21 +52,20 @@ set_window_names(void)
timeradd(&w->name_timer, &tv2, &w->name_timer); timeradd(&w->name_timer, &tv2, &w->name_timer);
if (w->active->screen != &w->active->base) if (w->active->screen != &w->active->base)
pgrp = -1; name = NULL;
else if ((pgrp = tcgetpgrp(w->active->fd)) == w->pgrp)
continue;
w->pgrp = pgrp;
name = get_argv0(pgrp);
if (pgrp == -1 || name == NULL)
name = default_window_name(w);
else else
name = parse_window_name(name); name = get_argv0(w->active->fd, w->active->tty);
if (strcmp(name, w->name) == 0) if (name == NULL)
wname = default_window_name(w);
else {
wname = parse_window_name(name);
xfree(name); xfree(name);
}
if (strcmp(wname, w->name) == 0)
xfree(wname);
else { else {
xfree(w->name); xfree(w->name);
w->name = name; w->name = wname;
server_status_window(w); server_status_window(w);
} }
} }

View File

@ -1,4 +1,4 @@
/* $Id: osdep-darwin.c,v 1.1 2009-01-20 22:17:53 nicm Exp $ */ /* $Id: osdep-darwin.c,v 1.2 2009-01-26 22:57:19 nicm Exp $ */
/* /*
* Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org> * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
@ -23,7 +23,7 @@
#include <stdlib.h> #include <stdlib.h>
char *get_argv0(pid_t); char *get_argv0(int, char *);
/* /*
* XXX This actually returns the executable path, not the process's argv[0]. * XXX This actually returns the executable path, not the process's argv[0].
@ -32,13 +32,15 @@ char *get_argv0(pid_t);
*/ */
char * char *
get_argv0(pid_t pgrp) get_argv0(int fd, __attribute__ ((unused)) char *tty)
{ {
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 }; int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
size_t size; size_t size;
struct kinfo_proc kp; struct kinfo_proc kp;
mib[3] = pgrp; if ((mib[3] = tcgetpgrp(fd)) == -1)
return (NULL)
size = sizeof kp; size = sizeof kp;
if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1 || if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1 ||
kp.kp_proc.p_comm[0] == '\0') kp.kp_proc.p_comm[0] == '\0')

View File

@ -1,4 +1,4 @@
/* $Id: osdep-freebsd.c,v 1.2 2009-01-20 22:17:53 nicm Exp $ */ /* $Id: osdep-freebsd.c,v 1.3 2009-01-26 22:57:19 nicm Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -19,7 +19,10 @@
#ifdef __FreeBSD__ #ifdef __FreeBSD__
#include <sys/param.h> #include <sys/param.h>
#include <sys/proc.h>
#include <sys/stat.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/user.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
@ -28,19 +31,80 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
char *get_argv0(pid_t); char *get_argv0(int, char *);
char *get_proc_argv0(pid_t);
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
char * char *
get_argv0(pid_t pgrp) get_argv0(__attribute__ ((unused)) int fd, char *tty)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_TTY, 0 };
struct stat sb;
size_t len;
struct kinfo_proc *buf, *newbuf, *p, *bestp;
char *procname;
u_int i;
buf = NULL;
if (stat(tty, &sb) == -1)
return (NULL);
mib[3] = sb.st_rdev;
retry:
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
return (NULL);
len = (len * 5) / 4;
if ((newbuf = realloc(buf, len)) == NULL) {
free(buf);
return (NULL);
}
buf = newbuf;
if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
if (errno == ENOMEM)
goto retry;
free(buf);
return (NULL);
}
bestp = NULL;
for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
p = &buf[i];
if (bestp == NULL)
bestp = p;
if (p->ki_stat != SRUN && p->ki_stat != SIDL)
continue;
if (p->ki_estcpu < bestp->ki_estcpu)
continue;
if (p->ki_slptime > bestp->ki_slptime)
continue;
bestp = p;
}
procname = get_proc_argv0(bestp->ki_pid);
if (procname == NULL || *procname == '\0') {
free(procname);
procname = strdup(bestp->ki_comm);
}
free(buf);
return (procname);
}
char *
get_proc_argv0(pid_t pid)
{ {
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0 }; int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0 };
size_t size; size_t size;
char *args, *args2, *procname; char *args, *args2, *procname;
mib[3] = pid;
procname = NULL; procname = NULL;
mib[3] = pgrp;
args = NULL; args = NULL;
size = 128; size = 128;
while (size < SIZE_MAX / 2) { while (size < SIZE_MAX / 2) {

View File

@ -1,4 +1,4 @@
/* $Id: osdep-linux.c,v 1.1 2009-01-20 19:35:03 nicm Exp $ */ /* $Id: osdep-linux.c,v 1.2 2009-01-26 22:57:19 nicm Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -27,16 +27,23 @@
#include "tmux.h" #include "tmux.h"
char * char *
get_argv0(pid_t pgrp) get_argv0(int fd, unused char *tty)
{ {
FILE *f; FILE *f;
char *path, *buf; char *path, *buf;
size_t len; size_t len;
int ch; int ch;
pid_t pgrp;
if ((pgrp = tcgetpgrp(fd)) == -1)
return (NULL);
xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp); xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp);
if ((f = fopen(path, "r")) == NULL) if ((f = fopen(path, "r")) == NULL) {
xfree(path);
return (NULL); return (NULL);
}
xfree(path);
len = 0; len = 0;
buf = NULL; buf = NULL;

View File

@ -1,4 +1,4 @@
/* $Id: osdep-openbsd.c,v 1.2 2009-01-20 22:17:53 nicm Exp $ */ /* $Id: osdep-openbsd.c,v 1.3 2009-01-26 22:57:19 nicm Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -20,6 +20,7 @@
#include <sys/param.h> #include <sys/param.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/stat.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
@ -27,10 +28,73 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
char *get_argv0(pid_t); char *get_argv0(int, char *);
char *get_proc_argv0(pid_t);
char * char *
get_argv0(pid_t pgrp) get_argv0(__attribute__ ((unused)) int fd, char *tty)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_TTY, 0 };
struct stat sb;
size_t len;
struct kinfo_proc *buf, *newbuf;
struct proc *p, *bestp;
char *procname;
u_int i;
buf = NULL;
if (stat(tty, &sb) == -1)
return (NULL);
mib[3] = sb.st_rdev;
retry:
if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
return (NULL);
len = (len * 5) / 4;
if ((newbuf = realloc(buf, len)) == NULL) {
free(buf);
return (NULL);
}
buf = newbuf;
if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
if (errno == ENOMEM)
goto retry;
free(buf);
return (NULL);
}
bestp = NULL;
for (i = 0; i < len / sizeof (struct kinfo_proc); i++) {
p = &buf[i].kp_proc;
if (bestp == NULL)
bestp = p;
if (p->p_stat != SRUN &&
p->p_stat != SIDL &&
p->p_stat != SONPROC)
continue;
if (p->p_estcpu < bestp->p_estcpu)
continue;
if (p->p_slptime > bestp->p_slptime)
continue;
bestp = p;
}
procname = get_proc_argv0(bestp->p_pid);
if (procname == NULL || *procname == '\0') {
free(procname);
procname = strdup(bestp->p_comm);
}
free(buf);
return (procname);
}
char *
get_proc_argv0(pid_t pid)
{ {
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, 0, KERN_PROC_ARGV }; int mib[4] = { CTL_KERN, KERN_PROC_ARGS, 0, KERN_PROC_ARGV };
size_t size; size_t size;
@ -38,7 +102,7 @@ get_argv0(pid_t pgrp)
procname = NULL; procname = NULL;
mib[2] = pgrp; mib[2] = pid;
args = NULL; args = NULL;
size = 128; size = 128;

5
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.250 2009-01-25 19:00:10 tcunha Exp $ */ /* $Id: tmux.h,v 1.251 2009-01-26 22:57:19 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -592,6 +592,7 @@ struct window_pane {
char *cwd; char *cwd;
int fd; int fd;
char tty[TTY_NAME_MAX];
struct buffer *in; struct buffer *in;
struct buffer *out; struct buffer *out;
@ -1503,7 +1504,7 @@ int utf8_width(u_int);
char *section_string(char *, size_t, size_t, size_t); char *section_string(char *, size_t, size_t, size_t);
/* osdep-*.c */ /* osdep-*.c */
char *get_argv0(pid_t); char *get_argv0(int, char *);
/* buffer.c */ /* buffer.c */
struct buffer *buffer_create(size_t); struct buffer *buffer_create(size_t);

View File

@ -1,4 +1,4 @@
/* $Id: window.c,v 1.62 2009-01-23 16:59:14 nicm Exp $ */ /* $Id: window.c,v 1.63 2009-01-26 22:57:20 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -569,7 +569,7 @@ window_pane_spawn(struct window_pane *wp,
tv.tv_usec = NAME_INTERVAL * 1000L; tv.tv_usec = NAME_INTERVAL * 1000L;
timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer); timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
switch (forkpty(&wp->fd, NULL, NULL, &ws)) { switch (forkpty(&wp->fd, wp->tty, NULL, &ws)) {
case -1: case -1:
wp->fd = -1; wp->fd = -1;
xasprintf(cause, "%s: %s", cmd, strerror(errno)); xasprintf(cause, "%s: %s", cmd, strerror(errno));