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

1.1     ! nicm        1: /* $OpenBSD$ */
        !             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>
        !            23: #include <string.h>
        !            24: #include <unistd.h>
        !            25:
        !            26: #include "tmux.h"
        !            27:
        !            28: char   *parse_window_name(const char *);
        !            29:
        !            30: void
        !            31: set_window_names(void)
        !            32: {
        !            33:        struct window   *w;
        !            34:        u_int            i;
        !            35:        char            *name, *wname;
        !            36:        struct timeval   tv, tv2;
        !            37:
        !            38:        if (gettimeofday(&tv, NULL) != 0)
        !            39:                fatal("gettimeofday");
        !            40:
        !            41:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
        !            42:                w = ARRAY_ITEM(&windows, i);
        !            43:                if (w == NULL || w->active == NULL)
        !            44:                        continue;
        !            45:                if (!options_get_number(&w->options, "automatic-rename"))
        !            46:                        continue;
        !            47:
        !            48:                if (timercmp(&tv, &w->name_timer, <))
        !            49:                        continue;
        !            50:                memcpy(&w->name_timer, &tv, sizeof w->name_timer);
        !            51:                tv2.tv_sec = 0;
        !            52:                tv2.tv_usec = NAME_INTERVAL * 1000L;
        !            53:                timeradd(&w->name_timer, &tv2, &w->name_timer);
        !            54:
        !            55:                if (w->active->screen != &w->active->base)
        !            56:                        name = NULL;
        !            57:                else
        !            58:                        name = get_proc_name(w->active->fd, w->active->tty);
        !            59:                if (name == NULL)
        !            60:                        wname = default_window_name(w);
        !            61:                else {
        !            62:                        wname = parse_window_name(name);
        !            63:                        xfree(name);
        !            64:                }
        !            65:
        !            66:                if (strcmp(wname, w->name) == 0)
        !            67:                        xfree(wname);
        !            68:                else {
        !            69:                        xfree(w->name);
        !            70:                        w->name = wname;
        !            71:                        server_status_window(w);
        !            72:                }
        !            73:        }
        !            74: }
        !            75:
        !            76: char *
        !            77: default_window_name(struct window *w)
        !            78: {
        !            79:        if (w->active->screen != &w->active->base)
        !            80:                return (xstrdup("[tmux]"));
        !            81:        return (parse_window_name(w->active->cmd));
        !            82: }
        !            83:
        !            84: char *
        !            85: parse_window_name(const char *in)
        !            86: {
        !            87:        char    *copy, *name, *ptr;
        !            88:
        !            89:        name = copy = xstrdup(in);
        !            90:        if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
        !            91:                name = name + (sizeof "exec ") - 1;
        !            92:
        !            93:        while (*name == ' ')
        !            94:                name++;
        !            95:        if ((ptr = strchr(name, ' ')) != NULL)
        !            96:                *ptr = '\0';
        !            97:
        !            98:        if (*name != '\0') {
        !            99:                ptr = name + strlen(name) - 1;
        !           100:                while (ptr > name && !isalnum(*ptr))
        !           101:                        *ptr-- = '\0';
        !           102:        }
        !           103:
        !           104:        if (*name == '/')
        !           105:                name = basename(name);
        !           106:        name = xstrdup(name);
        !           107:        xfree(copy);
        !           108:        return (name);
        !           109: }
        !           110: