[BACK]Return to names.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/names.c, Revision 1.17

1.17    ! nicm        1: /* $OpenBSD: names.c,v 1.16 2012/07/10 11:53:01 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 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:
                     21: #include <ctype.h>
                     22: #include <libgen.h>
1.16      nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "tmux.h"
                     28:
1.9       nicm       29: void    window_name_callback(unused int, unused short, void *);
1.1       nicm       30: char   *parse_window_name(const char *);
                     31:
                     32: void
1.9       nicm       33: queue_window_name(struct window *w)
1.1       nicm       34: {
1.9       nicm       35:        struct timeval  tv;
1.1       nicm       36:
1.9       nicm       37:        tv.tv_sec = 0;
                     38:        tv.tv_usec = NAME_INTERVAL * 1000L;
1.1       nicm       39:
1.13      nicm       40:        if (event_initialized(&w->name_timer))
                     41:                evtimer_del(&w->name_timer);
1.9       nicm       42:        evtimer_set(&w->name_timer, window_name_callback, w);
                     43:        evtimer_add(&w->name_timer, &tv);
                     44: }
1.5       nicm       45:
1.10      nicm       46: /* ARGSUSED */
1.9       nicm       47: void
                     48: window_name_callback(unused int fd, unused short events, void *data)
                     49: {
                     50:        struct window   *w = data;
                     51:        char            *name, *wname;
1.17    ! nicm       52:
        !            53:        if (w->active == NULL)
        !            54:                return;
1.1       nicm       55:
1.15      nicm       56:        if (!options_get_number(&w->options, "automatic-rename")) {
                     57:                if (event_initialized(&w->name_timer))
                     58:                        event_del(&w->name_timer);
                     59:                return;
                     60:        }
                     61:        queue_window_name(w);
1.9       nicm       62:
                     63:        if (w->active->screen != &w->active->base)
                     64:                name = NULL;
                     65:        else
                     66:                name = get_proc_name(w->active->fd, w->active->tty);
                     67:        if (name == NULL)
                     68:                wname = default_window_name(w);
                     69:        else {
1.11      nicm       70:                /*
1.9       nicm       71:                 * If tmux is using the default command, it will be a login
                     72:                 * shell and argv[0] may have a - prefix. Remove this if it is
                     73:                 * present. Ick.
                     74:                 */
                     75:                if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
                     76:                    name != NULL && name[0] == '-' && name[1] != '\0')
                     77:                        wname = parse_window_name(name + 1);
1.1       nicm       78:                else
1.4       nicm       79:                                wname = parse_window_name(name);
1.16      nicm       80:                free(name);
1.9       nicm       81:        }
1.11      nicm       82:
1.9       nicm       83:        if (w->active->fd == -1) {
                     84:                xasprintf(&name, "%s[dead]", wname);
1.16      nicm       85:                free(wname);
1.9       nicm       86:                wname = name;
                     87:        }
1.11      nicm       88:
1.12      nicm       89:        if (strcmp(wname, w->name)) {
                     90:                window_set_name(w, wname);
1.9       nicm       91:                server_status_window(w);
1.1       nicm       92:        }
1.16      nicm       93:        free(wname);
1.1       nicm       94: }
                     95:
                     96: char *
                     97: default_window_name(struct window *w)
                     98: {
                     99:        if (w->active->screen != &w->active->base)
                    100:                return (xstrdup("[tmux]"));
1.4       nicm      101:        if (w->active->cmd != NULL && *w->active->cmd != '\0')
                    102:                return (parse_window_name(w->active->cmd));
1.6       nicm      103:        return (parse_window_name(w->active->shell));
1.1       nicm      104: }
                    105:
                    106: char *
                    107: parse_window_name(const char *in)
                    108: {
                    109:        char    *copy, *name, *ptr;
                    110:
                    111:        name = copy = xstrdup(in);
                    112:        if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
                    113:                name = name + (sizeof "exec ") - 1;
                    114:
                    115:        while (*name == ' ')
                    116:                name++;
                    117:        if ((ptr = strchr(name, ' ')) != NULL)
                    118:                *ptr = '\0';
                    119:
                    120:        if (*name != '\0') {
                    121:                ptr = name + strlen(name) - 1;
1.2       ray       122:                while (ptr > name && !isalnum((u_char)*ptr))
1.1       nicm      123:                        *ptr-- = '\0';
                    124:        }
                    125:
                    126:        if (*name == '/')
                    127:                name = basename(name);
                    128:        name = xstrdup(name);
1.16      nicm      129:        free(copy);
1.1       nicm      130:        return (name);
                    131: }