Annotation of src/usr.bin/tmux/options-cmd.c, Revision 1.5
1.5 ! nicm 1: /* $OpenBSD: options-cmd.c,v 1.4 2009/09/21 14:56:03 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 <stdlib.h>
22: #include <string.h>
23:
24: #include "tmux.h"
1.3 nicm 25:
26: const char *
27: set_option_print(const struct set_option_entry *entry, struct options_entry *o)
28: {
29: static char out[BUFSIZ];
30: const char *s;
1.5 ! nicm 31: struct keylist *keylist;
! 32: u_int i;
1.3 nicm 33:
34: *out = '\0';
35: switch (entry->type) {
36: case SET_OPTION_STRING:
37: xsnprintf(out, sizeof out, "\"%s\"", o->str);
38: break;
39: case SET_OPTION_NUMBER:
40: xsnprintf(out, sizeof out, "%lld", o->num);
41: break;
1.5 ! nicm 42: case SET_OPTION_KEYS:
! 43: keylist = o->data;
! 44: for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
! 45: strlcat(out, key_string_lookup_key(
! 46: ARRAY_ITEM(keylist, i)), sizeof out);
! 47: if (i != ARRAY_LENGTH(keylist) - 1)
! 48: strlcat(out, ",", sizeof out);
! 49: }
1.3 nicm 50: break;
51: case SET_OPTION_COLOUR:
52: s = colour_tostring(o->num);
53: xsnprintf(out, sizeof out, "%s", s);
54: break;
55: case SET_OPTION_ATTRIBUTES:
56: s = attributes_tostring(o->num);
57: xsnprintf(out, sizeof out, "%s", s);
58: break;
59: case SET_OPTION_FLAG:
60: if (o->num)
61: strlcpy(out, "on", sizeof out);
62: else
63: strlcpy(out, "off", sizeof out);
64: break;
65: case SET_OPTION_CHOICE:
66: s = entry->choices[o->num];
67: xsnprintf(out, sizeof out, "%s", s);
68: break;
69: }
70: return (out);
71: }
1.1 nicm 72:
73: void
74: set_option_string(struct cmd_ctx *ctx, struct options *oo,
1.2 nicm 75: const struct set_option_entry *entry, char *value, int append)
1.1 nicm 76: {
1.4 nicm 77: struct options_entry *o;
78: char *oldvalue, *newvalue;
1.2 nicm 79:
1.1 nicm 80: if (value == NULL) {
81: ctx->error(ctx, "empty value");
82: return;
83: }
84:
1.2 nicm 85: if (append) {
86: oldvalue = options_get_string(oo, entry->name);
87: xasprintf(&newvalue, "%s%s", oldvalue, value);
88: } else
89: newvalue = value;
90:
1.4 nicm 91: o = options_set_string(oo, entry->name, "%s", newvalue);
92: ctx->info(
93: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.2 nicm 94:
95: if (newvalue != value)
96: xfree(newvalue);
1.1 nicm 97: }
98:
99: void
100: set_option_number(struct cmd_ctx *ctx, struct options *oo,
101: const struct set_option_entry *entry, char *value)
102: {
1.4 nicm 103: struct options_entry *o;
104: long long number;
105: const char *errstr;
1.1 nicm 106:
107: if (value == NULL) {
108: ctx->error(ctx, "empty value");
109: return;
110: }
111:
112: number = strtonum(value, entry->minimum, entry->maximum, &errstr);
113: if (errstr != NULL) {
114: ctx->error(ctx, "value is %s: %s", errstr, value);
115: return;
116: }
1.4 nicm 117:
118: o = options_set_number(oo, entry->name, number);
119: ctx->info(
120: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.1 nicm 121: }
122:
123: void
1.5 ! nicm 124: set_option_keys(struct cmd_ctx *ctx, struct options *oo,
1.1 nicm 125: const struct set_option_entry *entry, char *value)
126: {
1.4 nicm 127: struct options_entry *o;
1.5 ! nicm 128: struct keylist *keylist;
! 129: char *copyvalue, *ptr, *str;
! 130: int key;
1.1 nicm 131:
132: if (value == NULL) {
133: ctx->error(ctx, "empty value");
134: return;
135: }
136:
1.5 ! nicm 137: keylist = xmalloc(sizeof *keylist);
! 138: ARRAY_INIT(keylist);
! 139:
! 140: ptr = copyvalue = xstrdup(value);
! 141: while ((str = strsep(&ptr, ",")) != NULL) {
! 142: if ((key = key_string_lookup_string(str)) == KEYC_NONE) {
! 143: xfree(keylist);
! 144: ctx->error(ctx, "unknown key: %s", str);
! 145: xfree(copyvalue);
! 146: return;
! 147: }
! 148: ARRAY_ADD(keylist, key);
1.1 nicm 149: }
1.5 ! nicm 150: xfree(copyvalue);
1.4 nicm 151:
1.5 ! nicm 152: o = options_set_data(oo, entry->name, keylist, xfree);
1.4 nicm 153: ctx->info(
154: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.1 nicm 155: }
156:
157: void
158: set_option_colour(struct cmd_ctx *ctx, struct options *oo,
159: const struct set_option_entry *entry, char *value)
160: {
1.4 nicm 161: struct options_entry *o;
162: int colour;
1.1 nicm 163:
164: if (value == NULL) {
165: ctx->error(ctx, "empty value");
166: return;
167: }
168:
169: if ((colour = colour_fromstring(value)) == -1) {
170: ctx->error(ctx, "bad colour: %s", value);
171: return;
172: }
173:
1.4 nicm 174: o = options_set_number(oo, entry->name, colour);
175: ctx->info(
176: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.1 nicm 177: }
178:
179: void
180: set_option_attributes(struct cmd_ctx *ctx, struct options *oo,
181: const struct set_option_entry *entry, char *value)
182: {
1.4 nicm 183: struct options_entry *o;
184: int attr;
1.1 nicm 185:
186: if (value == NULL) {
187: ctx->error(ctx, "empty value");
188: return;
189: }
190:
191: if ((attr = attributes_fromstring(value)) == -1) {
192: ctx->error(ctx, "bad attributes: %s", value);
193: return;
194: }
195:
1.4 nicm 196: o = options_set_number(oo, entry->name, attr);
197: ctx->info(
198: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.1 nicm 199: }
200:
201: void
202: set_option_flag(struct cmd_ctx *ctx, struct options *oo,
203: const struct set_option_entry *entry, char *value)
204: {
1.4 nicm 205: struct options_entry *o;
206: int flag;
1.1 nicm 207:
208: if (value == NULL || *value == '\0')
209: flag = !options_get_number(oo, entry->name);
210: else {
211: if ((value[0] == '1' && value[1] == '\0') ||
212: strcasecmp(value, "on") == 0 ||
213: strcasecmp(value, "yes") == 0)
214: flag = 1;
215: else if ((value[0] == '0' && value[1] == '\0') ||
216: strcasecmp(value, "off") == 0 ||
217: strcasecmp(value, "no") == 0)
218: flag = 0;
219: else {
220: ctx->error(ctx, "bad value: %s", value);
221: return;
222: }
223: }
224:
1.4 nicm 225: o = options_set_number(oo, entry->name, flag);
226: ctx->info(
227: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.1 nicm 228: }
229:
230: void
231: set_option_choice(struct cmd_ctx *ctx, struct options *oo,
232: const struct set_option_entry *entry, char *value)
233: {
1.4 nicm 234: struct options_entry *o;
235: const char **choicep;
236: int n, choice = -1;
1.1 nicm 237:
238: if (value == NULL) {
239: ctx->error(ctx, "empty value");
240: return;
241: }
242:
243: n = 0;
244: for (choicep = entry->choices; *choicep != NULL; choicep++) {
245: n++;
246: if (strncmp(*choicep, value, strlen(value)) != 0)
247: continue;
248:
249: if (choice != -1) {
250: ctx->error(ctx, "ambiguous option: %s", value);
251: return;
252: }
253: choice = n - 1;
254: }
255: if (choice == -1) {
256: ctx->error(ctx, "unknown option: %s", value);
257: return;
258: }
259:
1.4 nicm 260: o = options_set_number(oo, entry->name, choice);
261: ctx->info(
262: ctx, "set option: %s -> %s", o->name, set_option_print(entry, o));
1.1 nicm 263: }