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

Annotation of src/usr.bin/tmux/options.c, Revision 1.12

1.12    ! nicm        1: /* $OpenBSD: options.c,v 1.11 2014/10/20 23:57:14 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2008 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 <stdarg.h>
1.8       nicm       22: #include <stdlib.h>
1.1       nicm       23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Option handling; each option has a name, type and value and is stored in
1.9       nicm       29:  * a red-black tree.
1.1       nicm       30:  */
                     31:
1.7       nicm       32: RB_GENERATE(options_tree, options_entry, entry, options_cmp);
1.1       nicm       33:
                     34: int
                     35: options_cmp(struct options_entry *o1, struct options_entry *o2)
                     36: {
                     37:        return (strcmp(o1->name, o2->name));
                     38: }
                     39:
                     40: void
                     41: options_init(struct options *oo, struct options *parent)
                     42: {
1.7       nicm       43:        RB_INIT(&oo->tree);
1.1       nicm       44:        oo->parent = parent;
                     45: }
                     46:
                     47: void
                     48: options_free(struct options *oo)
                     49: {
                     50:        struct options_entry    *o;
                     51:
1.7       nicm       52:        while (!RB_EMPTY(&oo->tree)) {
                     53:                o = RB_ROOT(&oo->tree);
                     54:                RB_REMOVE(options_tree, &oo->tree, o);
1.8       nicm       55:                free(o->name);
1.1       nicm       56:                if (o->type == OPTIONS_STRING)
1.8       nicm       57:                        free(o->str);
                     58:                free(o);
1.1       nicm       59:        }
                     60: }
                     61:
                     62: struct options_entry *
                     63: options_find1(struct options *oo, const char *name)
                     64: {
                     65:        struct options_entry    p;
                     66:
                     67:        p.name = (char *) name;
1.7       nicm       68:        return (RB_FIND(options_tree, &oo->tree, &p));
1.1       nicm       69: }
                     70:
                     71: struct options_entry *
                     72: options_find(struct options *oo, const char *name)
                     73: {
                     74:        struct options_entry    *o, p;
                     75:
                     76:        p.name = (char *) name;
1.7       nicm       77:        o = RB_FIND(options_tree, &oo->tree, &p);
1.1       nicm       78:        while (o == NULL) {
                     79:                oo = oo->parent;
                     80:                if (oo == NULL)
                     81:                        break;
1.7       nicm       82:                o = RB_FIND(options_tree, &oo->tree, &p);
1.1       nicm       83:        }
                     84:        return (o);
                     85: }
                     86:
1.2       nicm       87: void
1.1       nicm       88: options_remove(struct options *oo, const char *name)
                     89: {
                     90:        struct options_entry    *o;
                     91:
                     92:        if ((o = options_find1(oo, name)) == NULL)
1.2       nicm       93:                return;
1.1       nicm       94:
1.7       nicm       95:        RB_REMOVE(options_tree, &oo->tree, o);
1.8       nicm       96:        free(o->name);
1.1       nicm       97:        if (o->type == OPTIONS_STRING)
1.8       nicm       98:                free(o->str);
                     99:        free(o);
1.1       nicm      100: }
                    101:
1.11      nicm      102: struct options_entry *
1.1       nicm      103: options_set_string(struct options *oo, const char *name, const char *fmt, ...)
                    104: {
                    105:        struct options_entry    *o;
                    106:        va_list                  ap;
                    107:
                    108:        if ((o = options_find1(oo, name)) == NULL) {
                    109:                o = xmalloc(sizeof *o);
                    110:                o->name = xstrdup(name);
1.7       nicm      111:                RB_INSERT(options_tree, &oo->tree, o);
1.10      nicm      112:                memcpy(&o->style, &grid_default_cell, sizeof o->style);
1.1       nicm      113:        } else if (o->type == OPTIONS_STRING)
1.8       nicm      114:                free(o->str);
1.1       nicm      115:
                    116:        va_start(ap, fmt);
                    117:        o->type = OPTIONS_STRING;
1.3       nicm      118:        xvasprintf(&o->str, fmt, ap);
1.1       nicm      119:        va_end(ap);
1.4       nicm      120:        return (o);
1.1       nicm      121: }
                    122:
                    123: char *
                    124: options_get_string(struct options *oo, const char *name)
                    125: {
                    126:        struct options_entry    *o;
                    127:
                    128:        if ((o = options_find(oo, name)) == NULL)
                    129:                fatalx("missing option");
                    130:        if (o->type != OPTIONS_STRING)
                    131:                fatalx("option not a string");
1.3       nicm      132:        return (o->str);
1.1       nicm      133: }
                    134:
1.4       nicm      135: struct options_entry *
1.1       nicm      136: options_set_number(struct options *oo, const char *name, long long value)
                    137: {
                    138:        struct options_entry    *o;
                    139:
                    140:        if ((o = options_find1(oo, name)) == NULL) {
                    141:                o = xmalloc(sizeof *o);
                    142:                o->name = xstrdup(name);
1.7       nicm      143:                RB_INSERT(options_tree, &oo->tree, o);
1.10      nicm      144:                memcpy(&o->style, &grid_default_cell, sizeof o->style);
1.1       nicm      145:        } else if (o->type == OPTIONS_STRING)
1.8       nicm      146:                free(o->str);
1.1       nicm      147:
                    148:        o->type = OPTIONS_NUMBER;
1.3       nicm      149:        o->num = value;
1.4       nicm      150:        return (o);
1.1       nicm      151: }
                    152:
                    153: long long
                    154: options_get_number(struct options *oo, const char *name)
                    155: {
                    156:        struct options_entry    *o;
                    157:
                    158:        if ((o = options_find(oo, name)) == NULL)
                    159:                fatalx("missing option");
                    160:        if (o->type != OPTIONS_NUMBER)
                    161:                fatalx("option not a number");
1.3       nicm      162:        return (o->num);
1.10      nicm      163: }
                    164:
                    165: struct options_entry *
                    166: options_set_style(struct options *oo, const char *name, const char *value,
                    167:     int append)
                    168: {
                    169:        struct options_entry    *o;
1.12    ! nicm      170:        struct grid_cell         tmpgc;
1.10      nicm      171:
1.12    ! nicm      172:        o = options_find1(oo, name);
        !           173:        if (o == NULL || !append)
        !           174:                memcpy(&tmpgc, &grid_default_cell, sizeof tmpgc);
        !           175:        else
        !           176:                memcpy(&tmpgc, &o->style, sizeof tmpgc);
        !           177:
        !           178:        if (style_parse(&grid_default_cell, &tmpgc, value) == -1)
        !           179:                return (NULL);
        !           180:
        !           181:        if (o == NULL) {
1.10      nicm      182:                o = xmalloc(sizeof *o);
                    183:                o->name = xstrdup(name);
                    184:                RB_INSERT(options_tree, &oo->tree, o);
                    185:        } else if (o->type == OPTIONS_STRING)
                    186:                free(o->str);
                    187:
                    188:        o->type = OPTIONS_STYLE;
1.12    ! nicm      189:        memcpy(&o->style, &tmpgc, sizeof o->style);
1.10      nicm      190:        return (o);
                    191: }
                    192:
                    193: struct grid_cell *
                    194: options_get_style(struct options *oo, const char *name)
                    195: {
                    196:        struct options_entry    *o;
                    197:
                    198:        if ((o = options_find(oo, name)) == NULL)
                    199:                fatalx("missing option");
                    200:        if (o->type != OPTIONS_STYLE)
                    201:                fatalx("option not a style");
                    202:        return (&o->style);
1.1       nicm      203: }