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

Annotation of src/usr.bin/tmux/attributes.c, Revision 1.9

1.9     ! nicm        1: /* $OpenBSD: attributes.c,v 1.8 2018/10/18 07:57:57 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
                      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 <string.h>
                     22:
                     23: #include "tmux.h"
                     24:
                     25: const char *
1.7       nicm       26: attributes_tostring(int attr)
1.1       nicm       27: {
1.8       nicm       28:        static char     buf[512];
1.5       nicm       29:        size_t          len;
1.1       nicm       30:
1.3       nicm       31:        if (attr == 0)
1.4       nicm       32:                return ("none");
1.1       nicm       33:
1.9     ! nicm       34:        len = xsnprintf(buf, sizeof buf, "%s%s%s%s%s%s%s%s%s%s%s%s%s",
1.6       nicm       35:            (attr & GRID_ATTR_BRIGHT) ? "bright," : "",
                     36:            (attr & GRID_ATTR_DIM) ? "dim," : "",
                     37:            (attr & GRID_ATTR_UNDERSCORE) ? "underscore," : "",
                     38:            (attr & GRID_ATTR_BLINK)? "blink," : "",
                     39:            (attr & GRID_ATTR_REVERSE) ? "reverse," : "",
                     40:            (attr & GRID_ATTR_HIDDEN) ? "hidden," : "",
1.7       nicm       41:            (attr & GRID_ATTR_ITALICS) ? "italics," : "",
1.8       nicm       42:            (attr & GRID_ATTR_STRIKETHROUGH) ? "strikethrough," : "",
                     43:            (attr & GRID_ATTR_UNDERSCORE_2) ? "double-underscore," : "",
                     44:            (attr & GRID_ATTR_UNDERSCORE_3) ? "curly-underscore," : "",
                     45:            (attr & GRID_ATTR_UNDERSCORE_4) ? "dotted-underscore," : "",
1.9     ! nicm       46:            (attr & GRID_ATTR_UNDERSCORE_5) ? "dashed-underscore," : "",
        !            47:            (attr & GRID_ATTR_OVERLINE) ? "overline," : "");
1.5       nicm       48:        if (len > 0)
                     49:                buf[len - 1] = '\0';
1.1       nicm       50:
                     51:        return (buf);
                     52: }
                     53:
                     54: int
                     55: attributes_fromstring(const char *str)
                     56: {
                     57:        const char      delimiters[] = " ,|";
1.7       nicm       58:        int             attr;
1.1       nicm       59:        size_t          end;
1.8       nicm       60:        u_int           i;
                     61:        struct {
                     62:                const char*     name;
                     63:                int             attr;
                     64:        } table[] = {
                     65:                { "bright", GRID_ATTR_BRIGHT },
                     66:                { "bold", GRID_ATTR_BRIGHT },
                     67:                { "dim", GRID_ATTR_DIM },
                     68:                { "underscore", GRID_ATTR_UNDERSCORE },
                     69:                { "blink", GRID_ATTR_BLINK },
                     70:                { "reverse", GRID_ATTR_REVERSE },
                     71:                { "hidden", GRID_ATTR_HIDDEN },
                     72:                { "italics", GRID_ATTR_ITALICS },
                     73:                { "strikethrough", GRID_ATTR_STRIKETHROUGH },
                     74:                { "double-underscore", GRID_ATTR_UNDERSCORE_2 },
                     75:                { "curly-underscore", GRID_ATTR_UNDERSCORE_3 },
                     76:                { "dotted-underscore", GRID_ATTR_UNDERSCORE_4 },
1.9     ! nicm       77:                { "dashed-underscore", GRID_ATTR_UNDERSCORE_5 },
        !            78:                { "overline", GRID_ATTR_OVERLINE }
1.8       nicm       79:        };
1.1       nicm       80:
                     81:        if (*str == '\0' || strcspn(str, delimiters) == 0)
                     82:                return (-1);
                     83:        if (strchr(delimiters, str[strlen(str) - 1]) != NULL)
                     84:                return (-1);
                     85:
1.4       nicm       86:        if (strcasecmp(str, "default") == 0 || strcasecmp(str, "none") == 0)
1.1       nicm       87:                return (0);
                     88:
1.3       nicm       89:        attr = 0;
1.1       nicm       90:        do {
                     91:                end = strcspn(str, delimiters);
1.8       nicm       92:                for (i = 0; i < nitems(table); i++) {
                     93:                        if (end != strlen(table[i].name))
                     94:                                continue;
                     95:                        if (strncasecmp(str, table[i].name, end) == 0) {
                     96:                                attr |= table[i].attr;
                     97:                                break;
                     98:                        }
                     99:                }
                    100:                if (i == nitems(table))
1.1       nicm      101:                        return (-1);
                    102:                str += end + strspn(str + end, delimiters);
                    103:        } while (*str != '\0');
                    104:
1.3       nicm      105:        return (attr);
1.1       nicm      106: }