Annotation of src/usr.bin/tmux/options-cmd.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 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 <stdlib.h>
! 22: #include <string.h>
! 23:
! 24: #include "tmux.h"
! 25:
! 26: void
! 27: set_option_string(struct cmd_ctx *ctx, struct options *oo,
! 28: const struct set_option_entry *entry, char *value)
! 29: {
! 30: if (value == NULL) {
! 31: ctx->error(ctx, "empty value");
! 32: return;
! 33: }
! 34:
! 35: options_set_string(oo, entry->name, "%s", value);
! 36: ctx->info(ctx, "set option: %s -> %s", entry->name, value);
! 37: }
! 38:
! 39: void
! 40: set_option_number(struct cmd_ctx *ctx, struct options *oo,
! 41: const struct set_option_entry *entry, char *value)
! 42: {
! 43: long long number;
! 44: const char *errstr;
! 45:
! 46: if (value == NULL) {
! 47: ctx->error(ctx, "empty value");
! 48: return;
! 49: }
! 50:
! 51: number = strtonum(value, entry->minimum, entry->maximum, &errstr);
! 52: if (errstr != NULL) {
! 53: ctx->error(ctx, "value is %s: %s", errstr, value);
! 54: return;
! 55: }
! 56: options_set_number(oo, entry->name, number);
! 57: ctx->info(ctx, "set option: %s -> %lld", entry->name, number);
! 58: }
! 59:
! 60: void
! 61: set_option_key(struct cmd_ctx *ctx, struct options *oo,
! 62: const struct set_option_entry *entry, char *value)
! 63: {
! 64: int key;
! 65:
! 66: if (value == NULL) {
! 67: ctx->error(ctx, "empty value");
! 68: return;
! 69: }
! 70:
! 71: if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
! 72: ctx->error(ctx, "unknown key: %s", value);
! 73: return;
! 74: }
! 75: options_set_number(oo, entry->name, key);
! 76: ctx->info(ctx,
! 77: "set option: %s -> %s", entry->name, key_string_lookup_key(key));
! 78: }
! 79:
! 80: void
! 81: set_option_colour(struct cmd_ctx *ctx, struct options *oo,
! 82: const struct set_option_entry *entry, char *value)
! 83: {
! 84: int colour;
! 85:
! 86: if (value == NULL) {
! 87: ctx->error(ctx, "empty value");
! 88: return;
! 89: }
! 90:
! 91: if ((colour = colour_fromstring(value)) == -1) {
! 92: ctx->error(ctx, "bad colour: %s", value);
! 93: return;
! 94: }
! 95:
! 96: options_set_number(oo, entry->name, colour);
! 97: ctx->info(ctx,
! 98: "set option: %s -> %s", entry->name, colour_tostring(colour));
! 99: }
! 100:
! 101: void
! 102: set_option_attributes(struct cmd_ctx *ctx, struct options *oo,
! 103: const struct set_option_entry *entry, char *value)
! 104: {
! 105: int attr;
! 106:
! 107: if (value == NULL) {
! 108: ctx->error(ctx, "empty value");
! 109: return;
! 110: }
! 111:
! 112: if ((attr = attributes_fromstring(value)) == -1) {
! 113: ctx->error(ctx, "bad attributes: %s", value);
! 114: return;
! 115: }
! 116:
! 117: options_set_number(oo, entry->name, attr);
! 118: ctx->info(ctx,
! 119: "set option: %s -> %s", entry->name, attributes_tostring(attr));
! 120: }
! 121:
! 122: void
! 123: set_option_flag(struct cmd_ctx *ctx, struct options *oo,
! 124: const struct set_option_entry *entry, char *value)
! 125: {
! 126: int flag;
! 127:
! 128: if (value == NULL || *value == '\0')
! 129: flag = !options_get_number(oo, entry->name);
! 130: else {
! 131: if ((value[0] == '1' && value[1] == '\0') ||
! 132: strcasecmp(value, "on") == 0 ||
! 133: strcasecmp(value, "yes") == 0)
! 134: flag = 1;
! 135: else if ((value[0] == '0' && value[1] == '\0') ||
! 136: strcasecmp(value, "off") == 0 ||
! 137: strcasecmp(value, "no") == 0)
! 138: flag = 0;
! 139: else {
! 140: ctx->error(ctx, "bad value: %s", value);
! 141: return;
! 142: }
! 143: }
! 144:
! 145: options_set_number(oo, entry->name, flag);
! 146: ctx->info(ctx,
! 147: "set option: %s -> %s", entry->name, flag ? "on" : "off");
! 148: }
! 149:
! 150: void
! 151: set_option_choice(struct cmd_ctx *ctx, struct options *oo,
! 152: const struct set_option_entry *entry, char *value)
! 153: {
! 154: const char **choicep;
! 155: int n, choice = -1;
! 156:
! 157: if (value == NULL) {
! 158: ctx->error(ctx, "empty value");
! 159: return;
! 160: }
! 161:
! 162: n = 0;
! 163: for (choicep = entry->choices; *choicep != NULL; choicep++) {
! 164: n++;
! 165: if (strncmp(*choicep, value, strlen(value)) != 0)
! 166: continue;
! 167:
! 168: if (choice != -1) {
! 169: ctx->error(ctx, "ambiguous option: %s", value);
! 170: return;
! 171: }
! 172: choice = n - 1;
! 173: }
! 174: if (choice == -1) {
! 175: ctx->error(ctx, "unknown option: %s", value);
! 176: return;
! 177: }
! 178:
! 179: options_set_number(oo, entry->name, choice);
! 180: ctx->info(ctx,
! 181: "set option: %s -> %s", entry->name, entry->choices[choice]);
! 182: }