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

1.27    ! nicm        1: /* $OpenBSD: names.c,v 1.26 2015/08/28 13:26:41 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:
                     26: #include "tmux.h"
                     27:
                     28: void
1.27    ! nicm       29: check_window_name(struct window *w)
1.1       nicm       30: {
1.27    ! nicm       31:        char    *name;
1.17      nicm       32:
                     33:        if (w->active == NULL)
                     34:                return;
1.25      nicm       35:
1.27    ! nicm       36:        if (!options_get_number(&w->options, "automatic-rename"))
1.15      nicm       37:                return;
1.26      nicm       38:
                     39:        if (~w->active->flags & PANE_CHANGED)
                     40:                return;
                     41:        w->active->flags &= ~PANE_CHANGED;
1.9       nicm       42:
1.22      nicm       43:        name = format_window_name(w);
                     44:        if (strcmp(name, w->name) != 0) {
1.27    ! nicm       45:                log_debug("@%u new name %s (was %s)", w->id, name, w->name);
1.22      nicm       46:                window_set_name(w, name);
1.9       nicm       47:                server_status_window(w);
1.27    ! nicm       48:        } else
        !            49:                log_debug("@%u name not changed (still %s)", w->id, w->name);
        !            50:
1.22      nicm       51:        free(name);
1.1       nicm       52: }
                     53:
                     54: char *
                     55: default_window_name(struct window *w)
                     56: {
1.23      nicm       57:        char    *cmd, *s;
                     58:
                     59:        cmd = cmd_stringify_argv(w->active->argc, w->active->argv);
                     60:        if (cmd != NULL && *cmd != '\0')
                     61:                s = parse_window_name(cmd);
                     62:        else
                     63:                s = parse_window_name(w->active->shell);
                     64:        free(cmd);
                     65:        return (s);
1.1       nicm       66: }
                     67:
                     68: char *
1.22      nicm       69: format_window_name(struct window *w)
                     70: {
                     71:        struct format_tree      *ft;
                     72:        char                    *fmt, *name;
                     73:
                     74:        ft = format_create();
1.24      nicm       75:        format_defaults_window(ft, w);
                     76:        format_defaults_pane(ft, w->active);
1.22      nicm       77:
                     78:        fmt = options_get_string(&w->options, "automatic-rename-format");
                     79:        name = format_expand(ft, fmt);
                     80:
                     81:        format_free(ft);
                     82:        return (name);
                     83: }
                     84:
                     85: char *
1.21      nicm       86: parse_window_name(const char *in)
1.1       nicm       87: {
1.21      nicm       88:        char    *copy, *name, *ptr;
1.1       nicm       89:
                     90:        name = copy = xstrdup(in);
1.21      nicm       91:        if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
                     92:                name = name + (sizeof "exec ") - 1;
1.1       nicm       93:
1.22      nicm       94:        while (*name == ' ' || *name == '-')
1.1       nicm       95:                name++;
                     96:        if ((ptr = strchr(name, ' ')) != NULL)
                     97:                *ptr = '\0';
                     98:
                     99:        if (*name != '\0') {
                    100:                ptr = name + strlen(name) - 1;
1.2       ray       101:                while (ptr > name && !isalnum((u_char)*ptr))
1.1       nicm      102:                        *ptr-- = '\0';
                    103:        }
                    104:
                    105:        if (*name == '/')
                    106:                name = basename(name);
                    107:        name = xstrdup(name);
1.16      nicm      108:        free(copy);
1.1       nicm      109:        return (name);
                    110: }