void config_cred(char *header, char *base, struct config_cred *cred) { char *s; char *name; asprintf(&name, "%s-user", base); if ((s = config_string(header, name)) != NULL) { struct passwd *pw; if (!strcmp(s, "*")) cred->uid = -1U; else if ((pw = getpwnam(s)) == NULL) Eprintf("Unknown user `%s' in %s:%s config entry\n", s, header, name); else cred->uid = pw->pw_uid; } free(name); asprintf(&name, "%s-group", base); if ((s = config_string(header, name)) != NULL) { struct group *gr; if (!strcmp(s, "*")) cred->gid = -1U; else if ((gr = getgrnam(s)) == NULL) Eprintf("Unknown group `%s' in %s:%s config entry\n", header, name, s); else cred->gid = gr->gr_gid; } free(name); }
TEST_F(NginxStringConfigTest, Comment) { std::string config_string("foo bar; # comment here"); EXPECT_TRUE(ParseString(config_string)); EXPECT_EQ(1, config_.statements_.size()); EXPECT_EQ("foo", config_.statements_.at(0)->tokens_.at(0)); EXPECT_EQ("bar", config_.statements_.at(0)->tokens_.at(1)); }
void client_cleanup(void) { char *path = config_string("server", "socket-path"); if (!path) path = SOCKET_PATH; unlink(path); }
void bus_setup(void) { bus_trigger = config_string("socket", "bus-uc-threshold-trigger"); if (bus_trigger && trigger_check(bus_trigger) < 0) { SYSERRprintf("Cannot access bus threshold trigger `%s'", bus_trigger); exit(1); } iomca_trigger = config_string("socket", "iomca-threshold-trigger"); if (iomca_trigger && trigger_check(iomca_trigger) < 0) { SYSERRprintf("Cannot access iomca threshold trigger `%s'", iomca_trigger); exit(1); } }
TEST_F(NginxStringConfigTest, StatementQuotes) { std::string config_string("foo bar \"\'quotes\'\";"); EXPECT_TRUE(ParseString(config_string)); EXPECT_EQ("foo", config_.statements_.at(0)->tokens_.at(0)); EXPECT_EQ("bar", config_.statements_.at(0)->tokens_.at(1)); EXPECT_EQ("\"\'quotes\'\"", config_.statements_.at(0)->tokens_.at(2)); }
int main(int ac, char **av) { if (!av[1]) printf("need config file\n"), exit(1); if (parse_config_file(av[1]) < 0) printf("cannot parse config file\n"), exit(1); char *type; char *header; char *name; int n; while (scanf("%as %as %as", &type, &header, &name) == 3) { switch (type[0]) { case 'n': if (config_number(header, name, "%d", &n) < 0) printf("Cannot parse number %s %s\n", header, name); else printf("res %d\n", n); break; case 's': printf("res %s\n", config_string(header, name)); break; case 'b': printf("res %d\n", config_bool(header, name)); break; default: printf("unknown type %s\n", type); break; } free(type); free(header); free(name); } return 0; }
TEST_F(NginxStringConfigTest, ComplexStatement) { std::string config_string("foo bar 127.0.0.1 loo=dar;"); EXPECT_TRUE(ParseString(config_string)); EXPECT_EQ("foo", config_.statements_.at(0)->tokens_.at(0)); EXPECT_EQ("bar", config_.statements_.at(0)->tokens_.at(1)); EXPECT_EQ("127.0.0.1", config_.statements_.at(0)->tokens_.at(2)); EXPECT_EQ("loo=dar", config_.statements_.at(0)->tokens_.at(3)); }
TEST_F(NginxStringConfigTest, NestedBlock) { std::string config_string("foo { bar loo; }"); EXPECT_TRUE(ParseString(config_string)); EXPECT_EQ("foo", config_.statements_.at(0)->tokens_.at(0)); EXPECT_EQ("bar", config_.statements_.at(0)->child_block_.get() ->statements_.at(0)->tokens_.at(0)); EXPECT_EQ("loo", config_.statements_.at(0)->child_block_.get() ->statements_.at(0)->tokens_.at(1)); }
int config_number(const char *header, const char *name, char *fmt, void *val) { char *str = config_string(header, name); if (str == NULL) return -1; if (sscanf(str, fmt, val) != 1) { unparseable("numerical", header, name); return -1; } return 0; }
int config_choice(const char *header, const char *name, const struct config_choice *c) { char *str = config_string(header, name); if (!str) return -1; for (; c->name; c++) { if (!strcasecmp(str, c->name)) return c->val; } unparseable("choice", header, name); return -1; }
/**************************************************************************** * config_disk_info ****************************************************************************/ static cw_bool_t config_disk_info( struct config *cfg, struct disk *dsk) { cw_char_t info[GLOBAL_MAX_NAME_SIZE]; config_string(cfg, info, sizeof (info)); if (! disk_set_info(dsk, info)) debug_error(); return (CW_BOOL_OK); }
uint32_t system_locale( void ) { uint32_t localeval = 0; char localestr[4]; const char* locale = config_string( HASH_USER, HASH_LOCALE ); if( ( locale == LOCALE_BLANK ) || ( string_length( locale ) != 4 ) ) locale = config_string( HASH_APPLICATION, HASH_LOCALE ); if( ( locale == LOCALE_BLANK ) || ( string_length( locale ) != 4 ) ) locale = config_string( HASH_FOUNDATION, HASH_LOCALE ); if( ( locale == LOCALE_BLANK ) || ( string_length( locale ) != 4 ) ) return _system_user_locale(); #define _LOCALE_CHAR_TO_LOWERCASE(x) (((unsigned char)(x) >= 'A') && ((unsigned char)(x) <= 'Z')) ? (((unsigned char)(x)) | (32)) : (x) #define _LOCALE_CHAR_TO_UPPERCASE(x) (((unsigned char)(x) >= 'a') && ((unsigned char)(x) <= 'z')) ? (((unsigned char)(x)) & (~32)) : (x) localestr[0] = _LOCALE_CHAR_TO_LOWERCASE( locale[0] ); localestr[1] = _LOCALE_CHAR_TO_LOWERCASE( locale[1] ); localestr[2] = _LOCALE_CHAR_TO_UPPERCASE( locale[2] ); localestr[3] = _LOCALE_CHAR_TO_UPPERCASE( locale[3] ); memcpy( &localeval, localestr, 4 ); return localeval; }
static NOINLINE char* _expand_string( hash_t section_current, char* str ) { char* expanded; char* variable; unsigned int var_pos, var_end_pos, variable_length, separator, var_offset; hash_t section, key; expanded = str; var_pos = string_find_string( expanded, "$(", 0 ); while( var_pos != STRING_NPOS ) { var_end_pos = string_find( expanded, ')', var_pos + 2 ); FOUNDATION_ASSERT_MSG( var_end_pos != STRING_NPOS, "Malformed config variable statement" ); variable = string_substr( expanded, var_pos, ( var_end_pos != STRING_NPOS ) ? ( 1 + var_end_pos - var_pos ) : STRING_NPOS ); section = section_current; key = 0; variable_length = string_length( variable ); separator = string_find( variable, ':', 0 ); if( separator != STRING_NPOS ) { if( separator != 2 ) section = hash( variable + 2, separator - 2 ); var_offset = separator + 1; } else { var_offset = 2; } key = hash( variable + var_offset, variable_length - ( var_offset + ( variable[ variable_length - 1 ] == ')' ? 1 : 0 ) ) ); if( expanded == str ) expanded = string_clone( str ); if( section != HASH_ENVIRONMENT ) expanded = string_replace( expanded, variable, config_string( section, key ), false ); else expanded = string_replace( expanded, variable, _expand_environment( key, variable + var_offset ), false ); string_deallocate( variable ); var_pos = string_find_string( expanded, "$(", 0 ); } #if BUILD_ENABLE_DEBUG_CONFIG if( str != expanded ) log_debugf( HASH_CONFIG, "Expanded config value \"%s\" to \"%s\"", str, expanded ); #endif return expanded; }
int config_trigger(const char *header, const char *base, struct bucket_conf *bc) { char *s; char *name; int n; asprintf(&name, "%s-threshold", base); s = config_string(header, name); if (s) { if (bucket_conf_init(bc, s) < 0) { unparseable("trigger", header, name); return -1; } } free(name); asprintf(&name, "%s-trigger", base); s = config_string(header, name); if (s) { /* no $PATH */ if (trigger_check(s) != 0) { SYSERRprintf("Trigger `%s' not executable\n", s); exit(1); } bc->trigger = s; } free(name); bc->log = 0; asprintf(&name, "%s-log", base); n = config_bool(header, name); if (n >= 0) bc->log = n; free(name); return 0; }
static sp_session * create_session(PyObject *client, PyObject *settings) { sp_session_config config; sp_session* session; sp_error error; memset(&config, 0, sizeof(config)); config.api_version = SPOTIFY_API_VERSION; config.userdata = (void*)client; config.callbacks = &g_callbacks; config.cache_location = ""; config.user_agent = "pyspotify-fallback"; config_data(settings, "application_key", &config.application_key, &config.application_key_size); config_string(settings, "cache_location", &config.cache_location); config_string(settings, "settings_location", &config.settings_location); config_string(settings, "user_agent", &config.user_agent); config_string(client, "proxy", &config.proxy); config_string(client, "proxy_username", &config.proxy_username); config_string(client, "proxy_password", &config.proxy_password); debug_printf("cache_location = %s", config.cache_location); debug_printf("settings_location = %s", config.settings_location); debug_printf("user_agent = %s", config.user_agent); debug_printf("proxy = %s", config.proxy); debug_printf("proxy_username = %s", config.proxy_username); debug_printf("proxy_password = %s", config.proxy_password); debug_printf("application_key_size = %zu", config.application_key_size); if (PyErr_Occurred() != NULL) { return NULL; } if (strlen(config.user_agent) > 255) { PyErr_SetString(SpotifyError, "user_agent may not be longer than 255."); return NULL; } if (config.application_key_size == 0) { PyErr_SetString(SpotifyError, "application_key must be provided."); return NULL; } debug_printf("creating session..."); /* TODO: Figure out if we should ever release the session */ error = sp_session_create(&config, &session); if (error != SP_ERROR_OK) { PyErr_SetString(SpotifyError, sp_error_message(error)); return NULL; } session_constructed = 1; g_session = session; return session; }
bool config_int(config_t c, const char* name, int* ret) { const char *str = config_string(c, name); if (!str) return false; char *eptr = NULL; errno = 0; *ret = strtol(str, &eptr, 10); if (*eptr || errno) { fprintf(stderr, "Configuration parameter %s value is invalid\n", name); return false; } return true; }
char *config_string(const char *header, const char *name) { struct header *hdr; unsigned h = hash(name); for (hdr = hlist; hdr; hdr = hdr->next) { if (!strcmp(hdr->name, header)) { struct opt *o; for (o = hdr->opts[h]; o; o = o->next) { if (!strcmp(o->name, name)) return o->val; } } } if (strcmp(header, "global")) return config_string("global", name); return NULL; }
/* Send a command to the mcelog server and dump output */ void ask_server(char *command) { struct sockaddr_un sun; int fd; FILE * fp; int n; char buf[1024]; char *path = config_string("server", "socket-path"); if (!path) path = SOCKET_PATH; fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { SYSERRprintf("client socket"); return; } sun.sun_family = AF_UNIX; sun.sun_path[sizeof(sun.sun_path)-1] = 0; strncpy(sun.sun_path, path, sizeof(sun.sun_path)-1); if (connect(fd, (struct sockaddr *)&sun, sizeof(struct sockaddr_un)) < 0) SYSERRprintf("client connect"); n = strlen(command); if (write(fd, command, n) != n) SYSERRprintf("client command write"); if ((fp = fdopen(fd, "r")) != NULL) { while (fgets(buf, sizeof buf, fp)) { n = strlen(buf); if (n >= 5 && !memcmp(buf + n - 5, "done\n", 5)) { fclose(fp); return; } fputs(buf, stdout); } fclose(fp); } SYSERRprintf("client read"); }
/* Use getopt_long struct option array to process config file */ void config_options(struct option *opts, int (*func)(int)) { for (; opts->name; opts++) { if (!opts->has_arg) { if (config_bool("global", opts->name) != 1) continue; if (opts->flag) { *(opts->flag) = opts->val; continue; } } else { char *s = config_string("global", opts->name); if (s == NULL) continue; optarg = s; } func(opts->val); } }
TEST_F(NginxStringConfigTest, ParseEmpty) { std::string config_string(""); EXPECT_FALSE(ParseString(config_string)); }
static int bootstrap_pkg(void) { struct url *u; FILE *remote; FILE *config; char *site; struct dns_srvinfo *mirrors, *current; /* To store _https._tcp. + hostname + \0 */ char zone[MAXHOSTNAMELEN + 13]; char url[MAXPATHLEN]; char conf[MAXPATHLEN]; char tmppkg[MAXPATHLEN]; const char *packagesite, *mirror_type; char buf[10240]; char pkgstatic[MAXPATHLEN]; int fd, retry, ret, max_retry; struct url_stat st; off_t done, r; time_t now; time_t last; done = 0; last = 0; max_retry = 3; ret = -1; remote = NULL; config = NULL; current = mirrors = NULL; printf("Bootstrapping pkg please wait\n"); if (config_string(PACKAGESITE, &packagesite) != 0) { warnx("No PACKAGESITE defined"); return (-1); } if (config_string(MIRROR_TYPE, &mirror_type) != 0) { warnx("No MIRROR_TYPE defined"); return (-1); } snprintf(url, MAXPATHLEN, "%s/Latest/pkg.txz", packagesite); snprintf(tmppkg, MAXPATHLEN, "%s/pkg.txz.XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP); if ((fd = mkstemp(tmppkg)) == -1) { warn("mkstemp()"); return (-1); } retry = max_retry; u = fetchParseURL(url); while (remote == NULL) { if (retry == max_retry) { if (strcmp(u->scheme, "file") != 0 && strcasecmp(mirror_type, "srv") == 0) { snprintf(zone, sizeof(zone), "_%s._tcp.%s", u->scheme, u->host); mirrors = dns_getsrvinfo(zone); current = mirrors; } } if (mirrors != NULL) strlcpy(u->host, current->host, sizeof(u->host)); remote = fetchXGet(u, &st, ""); if (remote == NULL) { --retry; if (retry <= 0) goto fetchfail; if (mirrors == NULL) { sleep(1); } else { current = current->next; if (current == NULL) current = mirrors; } } } if (remote == NULL) goto fetchfail; while (done < st.size) { if ((r = fread(buf, 1, sizeof(buf), remote)) < 1) break; if (write(fd, buf, r) != r) { warn("write()"); goto cleanup; } done += r; now = time(NULL); if (now > last || done == st.size) last = now; } if (ferror(remote)) goto fetchfail; if ((ret = extract_pkg_static(fd, pkgstatic, MAXPATHLEN)) == 0) ret = install_pkg_static(pkgstatic, tmppkg); snprintf(conf, MAXPATHLEN, "%s/etc/pkg.conf", getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE); if (access(conf, R_OK) == -1) { site = strrchr(url, '/'); if (site == NULL) goto cleanup; site[0] = '\0'; site = strrchr(url, '/'); if (site == NULL) goto cleanup; site[0] = '\0'; config = fopen(conf, "w+"); if (config == NULL) goto cleanup; fprintf(config, "packagesite: %s\n", url); fclose(config); } goto cleanup; fetchfail: warnx("Error fetching %s: %s", url, fetchLastErrString); fprintf(stderr, "A pre-built version of pkg could not be found for your system.\n"); fprintf(stderr, "Consider changing PACKAGESITE or installing it from ports: 'ports-mgmt/pkg'.\n"); cleanup: if (remote != NULL) fclose(remote); close(fd); unlink(tmppkg); return (ret); }
hash_t config_string_hash( hash_t section, hash_t key ) { const char* value = config_string( section, key ); return value ? hash( value, string_length( value ) ) : HASH_EMPTY_STRING; }
static const char *config_control(struct control **as) { const char *t = 0; struct control *a, *b; struct simple_list *l; b = *as; while (b && b->locations) b = b->next; MAKE(a, struct control); a->locations = 0; a->alias = 0; a->clients = 0; if (b) { a->index_names = b->index_names; a->accesses = b->accesses; a->mimes = b->mimes; a->symlinksok = b->symlinksok; a->path_args_ok = b->path_args_ok; a->loglevel = b->loglevel; a->admin = b->admin; a->refresh = b->refresh; } else { a->index_names = 0; a->accesses = 0; a->mimes = 0; a->symlinksok = 0; a->path_args_ok = 0; a->loglevel = 0; a->admin = 0; a->refresh = 0; } a->next = *as; *as = a; GETOPEN(); while (NOTCLOSE()) { REQWORD(); if (strceq(tokbuf, c_location)) { MAKE(l, struct simple_list); GETSTRING(); chopslash(tokbuf); COPY(l->name, tokbuf); if (a->locations) { l->next = a->locations->next; a->locations->next = l; } else { l->next = l; a->locations = l; } } else if (strceq(tokbuf, c_alias)) { GETSTRING(); chopslash(tokbuf); COPY(a->alias, tokbuf); } else if (strceq(tokbuf, c_symlinks)) t = config_flag(&a->symlinksok); else if (strceq(tokbuf, c_path_args)) t = config_flag(&a->path_args_ok); else if (strceq(tokbuf, c_loglevel)) t = config_int(&a->loglevel); else if (strceq(tokbuf, c_index_names)) t = config_list(&a->index_names); else if (strceq(tokbuf, c_access)) t = config_access(&a->accesses); else if (strceq(tokbuf, c_clients)) t = config_access(&a->clients); else if (strceq(tokbuf, c_types)) t = config_mime(&a->mimes, M_TYPE); else if (strceq(tokbuf, c_specials)) t = config_mime(&a->mimes, M_SPECIAL); else if (strceq(tokbuf, c_admin)) t = config_string(&a->admin); else if (strceq(tokbuf, c_refresh)) t = config_int(&a->refresh); else t = e_keyword; if (t) return t; } if (a->alias && (a->locations == 0)) return e_bad_alias; return 0; }