/*- * Copyright (c) 2002 Jordan DeLong * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "editor.h" options_t options; optlist_t *options_hash; void options_init() { options_hash = hash_alloc("options_hash", OPTIONS_BUCKETS, offsetof(opt_t, o_list)); options_add("autoviews", OPT_STRING, &options.autoviews, 0); options_add("tabwidth", OPT_UINT, &options.tabwidth, 1); options_add("killringmax", OPT_UINT, &options.killringmax, 0); options_add("markringmax", OPT_UINT, &options.markringmax, 0); options_add("stickyhomekey", OPT_BOOL, &options.stickyhomekey, 1); options_add("chunkyscroll", OPT_BOOL, &options.chunkyscroll, 1); options_add("noexplicitmark", OPT_BOOL, &options.noexplicitmark, 1); options_add("highbitmeta", OPT_BOOL, &options.highbitmeta, 1); options_add("icasesearch", OPT_BOOL, &options.icasesearch, 1); options_add("autoindent", OPT_BOOL, &options.autoindent, 1); options_add("parenflash", OPT_BOOL, &options.parenflash, 1); options_add("forcefilenl", OPT_BOOL, &options.forcefilenl, 1); options_add("printnlatend", OPT_BOOL, &options.printnlatend, 0); options_add("intrchar", OPT_CHAR, &options.intrchar, 0); options_add("blanklinechar", OPT_CHAR, &options.blanklinechar, 0); /* set default values for options */ options.autoviews = NULL; options.tabwidth = 8; options.killringmax = 60; options.markringmax = 16; options.stickyhomekey = 1; options.chunkyscroll = 1; options.noexplicitmark = 1; options.highbitmeta = 0; options.icasesearch = 0; options.autoindent = 1; options.parenflash = 1; options.forcefilenl = 0; options.printnlatend = 0; options.intrchar = '\003'; /* Control-C */ options.blanklinechar = '~'; } static void delopt(opt_t *opt) { u_char *s; LIST_REMOVE(opt, o_list); if (opt->type == OPT_STRING) { s = *((u_char **) opt->val); if (s) free(s); } free(opt->name); free(opt); } void options_shutdown() { int idx; for (idx = 0; idx < OPTIONS_BUCKETS; idx++) while (!LIST_EMPTY(&options_hash[idx])) delopt(LIST_FIRST(&options_hash[idx])); hash_free(options_hash); } void options_add(u_char *name, int type, void *var, int setable) { opt_t *opt; int idx; opt = ckmalloc(sizeof(opt_t)); opt->type = type; opt->name = ckstrdup(name); opt->val = var; opt->setable = setable; idx = hash_string(OPTIONS_BUCKETS, name); LIST_INSERT_HEAD(&options_hash[idx], opt, o_list); } void options_rm(u_char *name) { opt_t *opt; opt = options_lookup(name); if (opt) delopt(opt); } opt_t *options_lookup(u_char *name) { opt_t *opt; int idx; idx = hash_string(OPTIONS_BUCKETS, name); LIST_FOREACH(opt, &options_hash[idx], o_list) if (!strcmp(opt->name, name)) return opt; return NULL; } void options_set(opt_t *opt, u_char *strval) { u_char **s, *vis; int i; switch (opt->type) { case OPT_INT: *((int *) opt->val) = atoi(strval); break; case OPT_UINT: i = atoi(strval); if (i < 0) i = 0; *((u_int *) opt->val) = i; break; case OPT_BOOL: if (tolower(strval[0] == 't') || strval[0] == '1') i = 1; else i = 0; *((u_int *) opt->val) = i; break; case OPT_CHAR: vis = ckmalloc(strlen(strval) + 1); if (strunvis(vis, strval) != -1) *((u_char *) opt->val) = vis[0]; free(vis); break; case OPT_STRING: s = (u_char **) opt->val; if (*s) free(*s); *s = ckstrdup(strval); break; case OPT_COLOR: if ((i = color_lookup(strval)) != COLOR_NULL) *((int *) opt->val) = i; break; case OPT_CALL: { void (*f)(u_char *); f = opt->val; f(strval); break; } default: assert(0); } }