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

1.33    ! nicm        1: /* $OpenBSD: names.c,v 1.32 2015/12/08 08:34:18 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:
1.29      nicm       28: void   name_time_callback(int, short, void *);
                     29: int    name_time_expired(struct window *, struct timeval *);
1.28      nicm       30:
1.1       nicm       31: void
1.31      nicm       32: name_time_callback(__unused int fd, __unused short events, void *arg)
1.1       nicm       33: {
1.29      nicm       34:        struct window   *w = arg;
1.28      nicm       35:
1.29      nicm       36:        /* The event loop will call check_window_name for us on the way out. */
                     37:        log_debug("@%u name timer expired", w->id);
                     38: }
                     39:
                     40: int
                     41: name_time_expired(struct window *w, struct timeval *tv)
                     42: {
                     43:        struct timeval  offset;
1.28      nicm       44:
1.29      nicm       45:        timersub(tv, &w->name_time, &offset);
                     46:        if (offset.tv_sec != 0 || offset.tv_usec > NAME_INTERVAL)
                     47:                return (0);
                     48:        return (NAME_INTERVAL - offset.tv_usec);
1.28      nicm       49: }
                     50:
                     51: void
1.29      nicm       52: check_window_name(struct window *w)
1.28      nicm       53: {
1.29      nicm       54:        struct timeval   tv, next;
1.28      nicm       55:        char            *name;
1.29      nicm       56:        int              left;
1.17      nicm       57:
                     58:        if (w->active == NULL)
                     59:                return;
1.25      nicm       60:
1.30      nicm       61:        if (!options_get_number(w->options, "automatic-rename"))
1.29      nicm       62:                return;
                     63:
                     64:        if (~w->active->flags & PANE_CHANGED) {
                     65:                log_debug("@%u active pane not changed", w->id);
1.15      nicm       66:                return;
1.28      nicm       67:        }
1.29      nicm       68:        log_debug("@%u active pane changed", w->id);
1.26      nicm       69:
1.29      nicm       70:        gettimeofday(&tv, NULL);
                     71:        left = name_time_expired(w, &tv);
                     72:        if (left != 0) {
                     73:                if (!event_initialized(&w->name_event))
                     74:                        evtimer_set(&w->name_event, name_time_callback, w);
                     75:                if (!evtimer_pending(&w->name_event, NULL)) {
                     76:                        log_debug("@%u name timer queued (%d left)", w->id, left);
                     77:                        timerclear(&next);
                     78:                        next.tv_usec = left;
                     79:                        event_add(&w->name_event, &next);
                     80:                } else
                     81:                        log_debug("@%u name timer already queued (%d left)", w->id, left);
1.26      nicm       82:                return;
1.29      nicm       83:        }
                     84:        memcpy(&w->name_time, &tv, sizeof w->name_time);
                     85:        if (event_initialized(&w->name_event))
                     86:                evtimer_del(&w->name_event);
                     87:
1.26      nicm       88:        w->active->flags &= ~PANE_CHANGED;
1.9       nicm       89:
1.22      nicm       90:        name = format_window_name(w);
                     91:        if (strcmp(name, w->name) != 0) {
1.29      nicm       92:                log_debug("@%u new name %s (was %s)", w->id, name, w->name);
1.22      nicm       93:                window_set_name(w, name);
1.9       nicm       94:                server_status_window(w);
1.29      nicm       95:        } else
                     96:                log_debug("@%u name not changed (still %s)", w->id, w->name);
                     97:
1.22      nicm       98:        free(name);
1.1       nicm       99: }
                    100:
                    101: char *
                    102: default_window_name(struct window *w)
                    103: {
1.23      nicm      104:        char    *cmd, *s;
                    105:
                    106:        cmd = cmd_stringify_argv(w->active->argc, w->active->argv);
                    107:        if (cmd != NULL && *cmd != '\0')
                    108:                s = parse_window_name(cmd);
                    109:        else
                    110:                s = parse_window_name(w->active->shell);
                    111:        free(cmd);
                    112:        return (s);
1.1       nicm      113: }
                    114:
                    115: char *
1.22      nicm      116: format_window_name(struct window *w)
                    117: {
                    118:        struct format_tree      *ft;
                    119:        char                    *fmt, *name;
                    120:
1.33    ! nicm      121:        ft = format_create(NULL, 0);
1.24      nicm      122:        format_defaults_window(ft, w);
                    123:        format_defaults_pane(ft, w->active);
1.22      nicm      124:
1.30      nicm      125:        fmt = options_get_string(w->options, "automatic-rename-format");
1.22      nicm      126:        name = format_expand(ft, fmt);
                    127:
                    128:        format_free(ft);
                    129:        return (name);
                    130: }
                    131:
                    132: char *
1.21      nicm      133: parse_window_name(const char *in)
1.1       nicm      134: {
1.21      nicm      135:        char    *copy, *name, *ptr;
1.1       nicm      136:
                    137:        name = copy = xstrdup(in);
1.21      nicm      138:        if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
                    139:                name = name + (sizeof "exec ") - 1;
1.1       nicm      140:
1.22      nicm      141:        while (*name == ' ' || *name == '-')
1.1       nicm      142:                name++;
                    143:        if ((ptr = strchr(name, ' ')) != NULL)
                    144:                *ptr = '\0';
                    145:
                    146:        if (*name != '\0') {
                    147:                ptr = name + strlen(name) - 1;
1.2       ray       148:                while (ptr > name && !isalnum((u_char)*ptr))
1.1       nicm      149:                        *ptr-- = '\0';
                    150:        }
                    151:
                    152:        if (*name == '/')
                    153:                name = basename(name);
                    154:        name = xstrdup(name);
1.16      nicm      155:        free(copy);
1.1       nicm      156:        return (name);
                    157: }