[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.20

1.20    ! nicm        1: /* $OpenBSD: names.c,v 1.19 2013/03/22 10:31:22 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.20    ! nicm       29: void    window_name_callback(int, short, void *);
        !            30: char   *parse_window_name(struct window *, const char *);
1.1       nicm       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.9       nicm       46: void
                     47: window_name_callback(unused int fd, unused short events, void *data)
                     48: {
                     49:        struct window   *w = data;
                     50:        char            *name, *wname;
1.17      nicm       51:
                     52:        if (w->active == NULL)
                     53:                return;
1.1       nicm       54:
1.15      nicm       55:        if (!options_get_number(&w->options, "automatic-rename")) {
                     56:                if (event_initialized(&w->name_timer))
                     57:                        event_del(&w->name_timer);
                     58:                return;
                     59:        }
                     60:        queue_window_name(w);
1.9       nicm       61:
                     62:        if (w->active->screen != &w->active->base)
                     63:                name = NULL;
                     64:        else
                     65:                name = get_proc_name(w->active->fd, w->active->tty);
                     66:        if (name == NULL)
                     67:                wname = default_window_name(w);
                     68:        else {
1.11      nicm       69:                /*
1.9       nicm       70:                 * If tmux is using the default command, it will be a login
                     71:                 * shell and argv[0] may have a - prefix. Remove this if it is
                     72:                 * present. Ick.
                     73:                 */
                     74:                if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
                     75:                    name != NULL && name[0] == '-' && name[1] != '\0')
1.20    ! nicm       76:                        wname = parse_window_name(w, name + 1);
1.1       nicm       77:                else
1.20    ! nicm       78:                        wname = parse_window_name(w, name);
1.16      nicm       79:                free(name);
1.9       nicm       80:        }
1.11      nicm       81:
1.9       nicm       82:        if (w->active->fd == -1) {
                     83:                xasprintf(&name, "%s[dead]", wname);
1.16      nicm       84:                free(wname);
1.9       nicm       85:                wname = name;
                     86:        }
1.11      nicm       87:
1.12      nicm       88:        if (strcmp(wname, w->name)) {
                     89:                window_set_name(w, wname);
1.9       nicm       90:                server_status_window(w);
1.1       nicm       91:        }
1.16      nicm       92:        free(wname);
1.1       nicm       93: }
                     94:
                     95: char *
                     96: default_window_name(struct window *w)
                     97: {
                     98:        if (w->active->screen != &w->active->base)
                     99:                return (xstrdup("[tmux]"));
1.4       nicm      100:        if (w->active->cmd != NULL && *w->active->cmd != '\0')
1.20    ! nicm      101:                return (parse_window_name(w, w->active->cmd));
        !           102:        return (parse_window_name(w, w->active->shell));
1.1       nicm      103: }
                    104:
                    105: char *
1.20    ! nicm      106: parse_window_name(struct window *w, const char *in)
1.1       nicm      107: {
1.20    ! nicm      108:        char    *copy, *name, *ptr, *prefix;
        !           109:        size_t   prefixlen;
        !           110:
        !           111:        prefix = options_get_string(&w->options, "command-prefix");
        !           112:        prefixlen = strlen(prefix);
1.1       nicm      113:
                    114:        name = copy = xstrdup(in);
1.20    ! nicm      115:        if (strncmp(name, prefix, prefixlen) == 0)
        !           116:                name = name + prefixlen;
1.1       nicm      117:
                    118:        while (*name == ' ')
                    119:                name++;
                    120:        if ((ptr = strchr(name, ' ')) != NULL)
                    121:                *ptr = '\0';
                    122:
                    123:        if (*name != '\0') {
                    124:                ptr = name + strlen(name) - 1;
1.2       ray       125:                while (ptr > name && !isalnum((u_char)*ptr))
1.1       nicm      126:                        *ptr-- = '\0';
                    127:        }
                    128:
                    129:        if (*name == '/')
                    130:                name = basename(name);
                    131:        name = xstrdup(name);
1.16      nicm      132:        free(copy);
1.1       nicm      133:        return (name);
                    134: }