int Settings::setStringSetting(Node type, int intDeviceId, const std::wstring &name, const std::wstring &value, bool parameter) { // already locked if (d->cfg == 0) { return TELLSTICK_ERROR_PERMISSION_DENIED; } std::string strType = getNodeString(type); cfg_t *cfg_device; for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) { cfg_device = cfg_getnsec(d->cfg, strType.c_str(), i); if (cfg_getint(cfg_device, "id") == intDeviceId) { std::string newValue = TelldusCore::wideToString(value); cfg_t *p = cfg_device; if (parameter) { p = cfg_getsec(cfg_device, "parameters"); } cfg_opt_t *opt = cfg_getopt(p, TelldusCore::wideToString(name).c_str()); if (!opt) { return TELLSTICK_ERROR_CONFIG_SYNTAX; } cfg_setstr(p, TelldusCore::wideToString(name).c_str(), newValue.c_str()); FILE *fp = fopen(CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle if (!fp) { return TELLSTICK_ERROR_PERMISSION_DENIED; } cfg_print(d->cfg, fp); fclose(fp); return TELLSTICK_SUCCESS; } } return TELLSTICK_ERROR_DEVICE_NOT_FOUND; }
int main(void) { FILE *fp; char *param; cfg_t *cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); /* set a string parameter to a string including a quote character */ cfg_setstr(cfg, "parameter", "text \" with quotes and \\"); /* print the config to a temporary file */ fp = tmpfile(); fail_unless(fp); cfg_print(cfg, fp); cfg_free(cfg); /* read it back, we expect 'parameter' to include a quote character */ rewind(fp); cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); fail_unless(cfg_parse_fp(cfg, fp) == CFG_SUCCESS); fail_unless(fclose(fp) == 0); param = cfg_getstr(cfg, "parameter"); fail_unless(param); fail_unless(strcmp(param, "text \" with quotes and \\") == 0); cfg_free(cfg); return 0; }
gint config_setstr (const gchar *key, const gchar *val) { config_mutex_lock (); cfg_setstr (tc, key, val); config_mutex_unlock (); return 0; }
/** * Start up the configuration system, using the configuration file given * to get the current values. If the configuration file given does not exist, * go ahead and write out the default config to the file. */ gint config_init (const gchar *config_file) { gint ret = 0; // Can we use a more descriptive name than tc? tc = cfg_init (config_opts, 0); if (g_file_test (config_file, G_FILE_TEST_IS_REGULAR)) { /* Read in the existing configuration options */ ret = cfg_parse (tc, config_file); if (ret == CFG_PARSE_ERROR) { DEBUG_ERROR ("Problem parsing config"); return ret; } else if (ret != CFG_SUCCESS) { DEBUG_ERROR ("Problem parsing config."); return ret; } } /* Deprecate old config settings * This is a lame work around until we get a permenant solution to * libconfuse lacking for this functionality */ const gchar *deprecated_tilda_config_options[] = {"show_on_monitor_number"}; remove_deprecated_config_options(deprecated_tilda_config_options, G_N_ELEMENTS(deprecated_tilda_config_options)); #if VTE_MINOR_VERSION >= 40 /* Deprecate old config settings * This is a lame work around until we get a permenant solution to * libconfuse lacking for this functionality */ const gchar *deprecated_vte_config_options[] = {"word_chars", "image", "scroll_background", "use_image"}; remove_deprecated_config_options (deprecated_vte_config_options, G_N_ELEMENTS(deprecated_vte_config_options)); #else if (cfg_getopt(tc, "use_image")->nvalues < 1) { cfg_setbool(tc, "use_image", FALSE); } if (cfg_getopt(tc, "word_chars")->nvalues < 1) { cfg_setstr(tc, "word_chars", DEFAULT_WORD_CHARS); } if (cfg_getopt(tc, "scroll_background")->nvalues < 1) { cfg_setbool(tc, "scroll_background", TRUE); } #endif #ifndef NO_THREADSAFE g_mutex_init(&mutex); #endif return ret; }
bool Settings::setDeviceState( int intDeviceId, int intDeviceState, const std::wstring &strDeviceStateValue ) { TelldusCore::MutexLocker locker(&mutex); if (d->var_cfg == 0) { return false; } cfg_t *cfg_device; for (int i = 0; i < cfg_size(d->var_cfg, "device"); ++i) { cfg_device = cfg_getnsec(d->var_cfg, "device", i); int deviceId = atoi(cfg_title(cfg_device)); if (deviceId == intDeviceId) { cfg_setint(cfg_device, "state", intDeviceState); cfg_setstr(cfg_device, "stateValue", TelldusCore::wideToString(strDeviceStateValue).c_str()); FILE *fp = fopen(VAR_CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle if(fp == 0) { return false; } cfg_print(d->var_cfg, fp); fclose(fp); return true; } } // The device is not found in the file, we must create it manualy... FILE *fp = fopen(VAR_CONFIG_FILE, "we"); // e for setting O_CLOEXEC on the file handle if(!fp) { fprintf(stderr, "Failed to write state to %s: %s\n", VAR_CONFIG_FILE, strerror(errno)); return false; } cfg_print(d->var_cfg, fp); // Print the config-file fprintf(fp, "device %d {\n}\n", intDeviceId); // Print the new device fclose(fp); // Re-read config-file cfg_free(d->var_cfg); readVarConfig(&d->var_cfg); return false; }
static int conffile_expand_libname(cfg_t *lib) { char *libname; char *hostname; char *s; char *d; char *expanded; struct utsname sysinfo; size_t len; size_t olen; size_t hostlen; size_t verlen; int ret; libname = cfg_getstr(lib, "name"); olen = strlen(libname); /* Fast path */ s = strchr(libname, '%'); if (!s) { libhash = murmur_hash64(libname, olen, 0); return 0; } /* Grab what we need */ ret = uname(&sysinfo); if (ret != 0) { DPRINTF(E_WARN, L_CONF, "Could not get system name: %s\n", strerror(errno)); hostname = "Unknown host"; } else hostname = sysinfo.nodename; hostlen = strlen(hostname); verlen = strlen(VERSION); /* Compute expanded size */ len = olen; s = libname; while (*s) { if (*s == '%') { s++; switch (*s) { case 'h': len += hostlen; break; case 'v': len += verlen; break; } } s++; } expanded = (char *)malloc(len + 1); if (!expanded) { DPRINTF(E_FATAL, L_CONF, "Out of memory\n"); return -1; } memset(expanded, 0, len + 1); /* Do the actual expansion */ s = libname; d = expanded; while (*s) { if (*s == '%') { s++; switch (*s) { case 'h': strcat(d, hostname); d += hostlen; break; case 'v': strcat(d, VERSION); d += verlen; break; } s++; } else { *d = *s; s++; d++; } } cfg_setstr(lib, "name", expanded); libhash = murmur_hash64(expanded, strlen(expanded), 0); free(expanded); return 0; }
void gimmix_config_save (Conf *conf) { FILE *fp; cfg_t *cfg; cfg_opt_t *sopts; cfg_opt_t opts[] = { CFG_SIMPLE_STR ("mpd_hostname", NULL), CFG_SIMPLE_INT ("mpd_port", 0), CFG_SIMPLE_STR ("mpd_password", NULL), CFG_SIMPLE_BOOL ("enable_systray", false), CFG_SIMPLE_BOOL ("enable_notify", false), CFG_END() }; cfg = cfg_init(opts, 0); char *rcfile = cfg_tilde_expand ("~/.gimmixrc"); if((fp = fopen(rcfile, "w"))) { fprintf (fp, "# Gimmix configuration\n"); fprintf (fp, "\n# MPD hostname (default: localhost)\n"); if (conf->hostname) cfg_setstr(cfg, "mpd_hostname", conf->hostname); sopts = cfg_getopt (cfg, "mpd_hostname"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# MPD port (default: 6600)\n"); if (conf->port > 0) cfg_setint(cfg, "mpd_port", conf->port); else cfg_setint(cfg, "mpd_port", 0); sopts = cfg_getopt (cfg, "mpd_port"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# MPD password (leave blank for no password) \n"); if (conf->password) cfg_setstr(cfg, "mpd_password", conf->password); sopts = cfg_getopt (cfg, "mpd_password"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# Enable/Disable systray icon (Enable = true, Disable = false)\n"); if (conf->systray_enable == 1) cfg_setbool(cfg, "enable_systray", true); else cfg_setbool(cfg, "enable_systray", false); sopts = cfg_getopt (cfg, "enable_systray"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# Enable/Disable system tray notifications (Enable = true, Disable = false) \n"); if (conf->notify_enable == 1) cfg_setbool(cfg, "enable_notify", true); else cfg_setbool(cfg, "enable_notify", false); sopts = cfg_getopt (cfg, "enable_notify"); cfg_opt_print (sopts, fp); free (rcfile); fclose (fp); } else { fprintf (stderr, "Error while saving config.\n"); } cfg_free_value (opts); cfg_free (cfg); return; }
int main(void) { static cfg_bool_t verbose = cfg_false; static char *server = NULL; static double delay = 1.356e-32; static char *username = NULL; /* Although the macro used to specify an integer option is called * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64 * architectures), you will get weird effects if you use an int here. * * If you use the regular (non-"simple") options, ie CFG_INT() and use * cfg_getint(), this is not a problem as the data types are implicitly * cast. */ static long int debug = 1; cfg_opt_t opts[] = { CFG_SIMPLE_BOOL("verbose", &verbose), CFG_SIMPLE_STR("server", &server), CFG_SIMPLE_STR("user", &username), CFG_SIMPLE_INT("debug", &debug), CFG_SIMPLE_FLOAT("delay", &delay), CFG_END() }; cfg_t *cfg; /* set default value for the server option */ server = strdup("gazonk"); cfg = cfg_init(opts, 0); cfg_parse(cfg, "simple.conf"); printf("verbose: %s\n", verbose ? "true" : "false"); printf("server: %s\n", server); printf("username: %s\n", username); printf("debug: %ld\n", debug); printf("delay: %G\n", delay); printf("setting username to 'foo'\n"); /* using cfg_setstr here is not necessary at all, the equivalent * code is: * free(username); * username = strdup("foo"); */ cfg_setstr(cfg, "user", "foo"); printf("username: %s\n", username); /* print the parsed values to another file */ { FILE *fp = fopen("simple.conf.out", "w"); cfg_print(cfg, fp); fclose(fp); } cfg_free(cfg); /* You are responsible for freeing string values. */ free(server); free(username); return 0; }
static int parse_args_post(int argc, char *argv[]) { DBG("Parsing arguments post state"); optind = 1; for (;;) { char *gostr = "i:o:p:r:R:s:aUb" \ "L:hvVf:"; struct option long_opts[] = { {"input", 1, 0, 'i'}, {"output", 1, 0, 'o'}, {"policy", 1, 0, 'p'}, {"read-interval", 1, 0, 'r'}, {"rate-interval", 1, 0, 'R'}, {"sleep-interval", 1, 0, 's'}, {"show-all", 0, 0, 'a'}, {"use-si", 0, 0, 'U'}, {"use-bit", 0, 0, 'b'}, {"lifetime", 1, 0, 'L'}, {0, 0, 0, 0}, }; int c = getopt_long(argc, argv, gostr, long_opts, NULL); if (c == -1) break; switch (c) { case 'i': if (input_set(optarg)) return 1; break; case 'o': if (output_set(optarg)) return 1; break; case 'p': cfg_setstr(cfg, "policy", optarg); break; case 'r': cfg_setfloat(cfg, "read_interval", strtod(optarg, NULL)); break; case 'R': cfg_setfloat(cfg, "rate_interval", strtod(optarg, NULL)); break; case 's': cfg_setint(cfg, "sleep_time", strtoul(optarg, NULL, 0)); break; case 'a': cfg_setbool(cfg, "show_all", cfg_true); break; case 'U': cfg_setbool(cfg, "use_si", cfg_true); break; case 'b': cfg_setbool(cfg, "use_bit", cfg_true); break; case 'L': cfg_setint(cfg, "lifetime", strtoul(optarg, NULL, 0)); break; case 'f': /* Already handled in pre getopt loop */ break; default: quit("Aborting...\n"); break; } } return 0; }
static void parse_args_post(int argc, char *argv[]) { optind = 1; for (;;) { char *gostr = "i:I:o:O:p:r:R:s:S:P:wadu:U" \ "L:g:hvVf:"; #ifdef HAVE_GETOPT_LONG struct option long_opts[] = { {"input", 1, 0, 'i'}, {"secondary-input", 1, 0, 'I'}, {"output", 1, 0, 'o'}, {"secondary-output", 1, 0, 'O'}, {"policy", 1, 0, 'p'}, {"read-interval", 1, 0, 'r'}, {"rate-interval", 1, 0, 'R'}, {"sleep-interval", 1, 0, 's'}, {"send-signal", 1, 0, 'S'}, {"pidfile", 1, 0, 'P'}, {"wait-for-signal", 0, 0, 'w'}, {"show-all", 0, 0, 'a'}, {"daemon", 0, 0, 'd'}, {"uid", 1, 0, 'u'}, {"use-si", 0, 0, 'U'}, {"lifetime", 1, 0, 'L'}, {"gid", 1, 0, 'g'}, {0, 0, 0, 0}, }; int c = getopt_long(argc, argv, gostr, long_opts, NULL); #else int c = getopt(argc, argv, gostr; #endif if (c == -1) break; switch (c) { case 'i': input_set(optarg); break; case 'I': input_set_secondary(optarg); break; case 'o': output_set(optarg); break; case 'O': output_set_secondary(optarg); break; case 'p': cfg_setstr(cfg, "policy", optarg); break; case 'r': cfg_setfloat(cfg, "read_interval", strtod(optarg, NULL)); break; case 'R': cfg_setfloat(cfg, "rate_interval", strtod(optarg, NULL)); break; case 's': cfg_setint(cfg, "sleep_time", strtoul(optarg, NULL, 0)); break; case 'S': signal_send(optarg); exit(0); case 'w': cfg_setint(cfg, "signal_driven", 1); break; case 'P': cfg_setstr(cfg, "pidfile", optarg); break; case 'a': cfg_setint(cfg, "show_all", 1); break; case 'd': cfg_setbool(cfg, "daemon", cfg_true); break; case 'u': cfg_setstr(cfg, "uid", optarg); break; case 'U': cfg_setbool(cfg, "use_si", cfg_true); break; case 'L': cfg_setint(cfg, "lifetime", strtoul(optarg, NULL, 0)); break; case 'g': cfg_setstr(cfg, "gid", optarg); break; case 'f': /* Already handled in pre getopt loop */ break; default: quit("Aborting...\n"); break; } } }
int main(int argc, char **argv) { unsigned int i; cfg_t *cfg; unsigned n; int ret; cfg_opt_t proxy_opts[] = { CFG_INT("type", 0, CFGF_NONE), CFG_STR("host", 0, CFGF_NONE), CFG_STR_LIST("exclude", "{localhost, .localnet}", CFGF_NONE), CFG_INT("port", 21, CFGF_NONE), CFG_END() }; cfg_opt_t bookmark_opts[] = { CFG_STR("machine", 0, CFGF_NONE), CFG_INT("port", 21, CFGF_NONE), CFG_STR("login", 0, CFGF_NONE), CFG_STR("password", 0, CFGF_NONE), CFG_STR("directory", 0, CFGF_NONE), CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), CFG_SEC("proxy", proxy_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT("backlog", 42, CFGF_NONE), CFG_STR("probe-device", "eth2", CFGF_NONE), CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), CFG_FLOAT_LIST("delays", "{3.567e2, 0.2, -47.11}", CFGF_NONE), CFG_FUNC("func", &cb_func), CFG_INT_CB("ask-quit", 3, CFGF_NONE, &cb_verify_ask), CFG_INT_LIST_CB("ask-quit-array", "{maybe, yes, no}", CFGF_NONE, &cb_verify_ask), CFG_FUNC("include", &cfg_include), CFG_END() }; #ifndef _WIN32 /* for some reason, MS Visual C++ chokes on this (?) */ printf("Using %s\n\n", confuse_copyright); #endif cfg = cfg_init(opts, CFGF_NOCASE); /* set a validating callback function for bookmark sections */ cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark); ret = cfg_parse(cfg, argc > 1 ? argv[1] : "test.conf"); printf("ret == %d\n", ret); if(ret == CFG_FILE_ERROR) { perror("test.conf"); return 1; } else if(ret == CFG_PARSE_ERROR) { fprintf(stderr, "parse error\n"); return 2; } printf("backlog == %ld\n", cfg_getint(cfg, "backlog")); printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); cfg_setstr(cfg, "probe-device", "lo"); printf("probe device is %s\n", cfg_getstr(cfg, "probe-device")); n = cfg_size(cfg, "bookmark"); printf("%d configured bookmarks:\n", n); for(i = 0; i < n; i++) { cfg_t *pxy; cfg_t *bm = cfg_getnsec(cfg, "bookmark", i); printf(" bookmark #%u (%s):\n", i+1, cfg_title(bm)); printf(" machine = %s\n", cfg_getstr(bm, "machine")); printf(" port = %d\n", (int)cfg_getint(bm, "port")); printf(" login = %s\n", cfg_getstr(bm, "login")); printf(" passive-mode = %s\n", cfg_getbool(bm, "passive-mode") ? "true" : "false"); printf(" directory = %s\n", cfg_getstr(bm, "directory")); printf(" password = %s\n", cfg_getstr(bm, "password")); pxy = cfg_getsec(bm, "proxy"); if(pxy) { int j, m; if(cfg_getstr(pxy, "host") == 0) { printf(" no proxy host is set, setting it to 'localhost'...\n"); /* For sections without CFGF_MULTI flag set, there is * also an extended syntax to get an option in a * subsection: */ cfg_setstr(bm, "proxy|host", "localhost"); } printf(" proxy host is %s\n", cfg_getstr(pxy, "host")); printf(" proxy type is %ld\n", cfg_getint(pxy, "type")); printf(" proxy port is %ld\n", cfg_getint(pxy, "port")); m = cfg_size(pxy, "exclude"); printf(" got %d hosts to exclude from proxying:\n", m); for(j = 0; j < m; j++) { printf(" exclude %s\n", cfg_getnstr(pxy, "exclude", j)); } } else printf(" no proxy settings configured\n"); } printf("delays are (%d):\n", cfg_size(cfg, "delays")); for(i = 0; i < cfg_size(cfg, "delays"); i++) printf(" %G\n", cfg_getnfloat(cfg, "delays", i)); printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); /* Using cfg_setint(), the integer value for the option ask-quit * is not verified by the value parsing callback. * * cfg_setint(cfg, "ask-quit", 4); printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit")); */ /* The following commented line will generate a failed assertion * and abort, since the option "foo" is not declared * * printf("foo == %ld\n", cfg_getint(cfg, "foo")); */ cfg_addlist(cfg, "ask-quit-array", 2, 1, 2); for(i = 0; i < cfg_size(cfg, "ask-quit-array"); i++) printf("ask-quit-array[%d] == %ld\n", i, cfg_getnint(cfg, "ask-quit-array", i)); /* print the parsed values to another file */ { FILE *fp = fopen("test.conf.out", "w"); cfg_set_print_func(cfg, "func", print_func); cfg_set_print_func(cfg, "ask-quit", print_ask); cfg_set_print_func(cfg, "ask-quit-array", print_ask); cfg_print(cfg, fp); fclose(fp); } cfg_free(cfg); return 0; }