static int parse_timemaster_settings(char **settings, struct timemaster_config *config) { char *name, *value; int r = 0; for (; *settings; settings++) { parse_setting(*settings, &name, &value); if (!strcasecmp(name, "ntp_program")) { if (!strcasecmp(value, "chronyd")) { config->ntp_program = CHRONYD; } else if (!strcasecmp(value, "ntpd")) { config->ntp_program = NTPD; } else { pr_err("unknown ntp program %s", value); return 1; } } else if (!strcasecmp(name, "rundir")) { replace_string(value, &config->rundir); } else if (!strcasecmp(name, "first_shm_segment")) { r = parse_int(value, &config->first_shm_segment); } else { pr_err("unknown timemaster setting %s", name); return 1; } if (r) { pr_err("invalid value %s for %s", value, name); return 1; } } return 0; }
static void parse_config (PinPointPoint *point, const char *config) { GString *str = g_string_new (""); const char *p; for (p = config; *p; p++) { if (*p != '[') continue; p++; g_string_truncate (str, 0); while (*p && *p != ']' && *p != '\n') { g_string_append_c (str, *p); p++; } if (*p == ']') parse_setting (point, str->str); } g_string_free (str, TRUE); }
/** \brief Reads settings from config. * \param fd The open file. * \param out The structure to populate/adjust. * \return 0 on success. * Reads the settings. **/ int read_settings(int fd, struct admin_s *out){ char b; int i; char *buff; char abuff[1024]; char bbuff[1024]; int mode, esc; if (fd == -1) return -1; if (read(fd,0,0)) return -1; abuff[0] = bbuff[0] = 0; i = 0; mode = 0; esc = 0; buff = abuff; b = 1; while (b){ int val; if (! read(fd, &b, 1)) b = 0; buff[i] = b; buff[i+1] = 0; if (esc){ esc = 0; continue; } switch (b){ case '#': while (b != '\n'){ b = 0; if (! read(fd, &b, 1)) break; } //Continue into newline case '\n'://apply buff[i] = 0; buff = abuff; i = -1; //set setting val = parse_setting(abuff, bbuff, &(out->settings), out); if (val) return 0; abuff[0] = bbuff[0] = 0; break; case '=': //set buff[i] = 0; buff = bbuff; i = -1; break; case '\\': //escaped next esc = 0; i--; break; } i++; } return 0; }
static struct source *source_ptp_parse(char *parameter, char **settings) { char *name, *value; struct source *source; int r = 0; source = xmalloc(sizeof(*source)); source->type = PTP_DOMAIN; source->ptp.delay = DEFAULT_PTP_DELAY; source->ptp.ntp_poll = DEFAULT_PTP_NTP_POLL; source->ptp.phc2sys_poll = DEFAULT_PTP_PHC2SYS_POLL; source->ptp.interfaces = (char **)parray_new(); source->ptp.ptp4l_settings = (char **)parray_new(); source->ptp.ntp_options = xstrdup(""); if (parse_int(parameter, &source->ptp.domain)) { pr_err("invalid ptp_domain number %s", parameter); goto failed; } for (; *settings; settings++) { parse_setting(*settings, &name, &value); if (!strcasecmp(name, "delay")) { r = parse_double(value, &source->ptp.delay); } else if (!strcasecmp(name, "ntp_poll")) { r = parse_int(value, &source->ptp.ntp_poll); } else if (!strcasecmp(name, "phc2sys_poll")) { r = parse_int(value, &source->ptp.phc2sys_poll); } else if (!strcasecmp(name, "ptp4l_option")) { parray_append((void ***)&source->ptp.ptp4l_settings, xstrdup(value)); } else if (!strcasecmp(name, "ntp_options")) { replace_string(value, &source->ptp.ntp_options); } else if (!strcasecmp(name, "interfaces")) { parse_words(value, &source->ptp.interfaces); } else { pr_err("unknown ptp_domain setting %s", name); goto failed; } if (r) { pr_err("invalid value %s for %s", value, name); goto failed; } } if (!*source->ptp.interfaces) { pr_err("no interfaces specified for ptp_domain %d", source->ptp.domain); goto failed; } return source; failed: source_destroy(source); return NULL; }
int main() { char buf[100]; struct PokerState state; memset(&state, 0, sizeof(state)); if( !Init_Hand_Eval() ) { fprintf(stderr, "ERR: unable to Init_Hand_Eval\n"); return 1; } while( fgets(buf, sizeof(buf), stdin) != NULL ) { if( buf[0] == '\n' ) { continue; } const int maxparts = 10; char *part[maxparts]; int parts = split(buf, part, maxparts); if( parts == 3 && strcmp(part[0], "Settings") == 0 ) { parse_setting(part[1], part[2], &state.settings); } else if( parts == 3 && strcmp(part[0], "Match") == 0 ) { parse_matchinfo(part[1], part[2], &state); } else if( parts == 3 && state.settings.myname != NULL ) { struct PokerPlayer *player = strcmp(part[0], state.settings.myname) ? &state.villain : &state.hero; if( player->name == NULL ) { player->name = strdup(part[0]); } if( strcmp(player->name, part[0]) != 0 ) { fprintf(stderr, "WARN: ``%s'' does not match villain name ``%s''\n", part[0], player->name); } else { parse_playerinfo(part[1], part[2], player); if( strcmp(part[1], "hand") == 0 ) { player_store_handstrength(&state, player); } } } else if( parts == 2 && strcmp(part[0], "go") == 0 ) { player_store_handstrength(&state, &state.hero); state.timeout = atol(part[1]); go(&state); fflush(stdout); } else if( parts == 2 && strcmp(part[0], "setup") == 0 ) { state.timeout = atol(part[1]); setup(&state); fflush(stdout); } else { int i; fprintf(stderr, "WARN: unable to understand %d part command%s: ``", parts, (state.settings.myname?"":" (don't know my name yet)")); for( i = 0; i < parts; ++i ) { fprintf(stderr, (i==0)?"%s":" %s", part[i]); } fprintf(stderr, "''\n"); } } return 0; }
static struct source *source_ntp_parse(char *parameter, char **settings) { char *name, *value; struct source *source; int r = 0; if (!*parameter) { pr_err("missing address for ntp_server"); return NULL; } source = xmalloc(sizeof(*source)); source->type = NTP_SERVER; source->ntp.address = xstrdup(parameter); source->ntp.minpoll = DEFAULT_NTP_MINPOLL; source->ntp.maxpoll = DEFAULT_NTP_MAXPOLL; source->ntp.iburst = 0; source->ntp.ntp_options = xstrdup(""); for (; *settings; settings++) { parse_setting(*settings, &name, &value); if (!strcasecmp(name, "minpoll")) { r = parse_int(value, &source->ntp.minpoll); } else if (!strcasecmp(name, "maxpoll")) { r = parse_int(value, &source->ntp.maxpoll); } else if (!strcasecmp(name, "iburst")) { r = parse_bool(value, &source->ntp.iburst); } else if (!strcasecmp(name, "ntp_options")) { replace_string(value, &source->ntp.ntp_options); } else { pr_err("unknown ntp_server setting %s", name); goto failed; } if (r) { pr_err("invalid value %s for %s", value, name); goto failed; } } return source; failed: source_destroy(source); return NULL; }
static int parse_program_settings(char **settings, struct program_config *config) { char *name, *value; for (; *settings; settings++) { parse_setting(*settings, &name, &value); if (!strcasecmp(name, "path")) { replace_string(value, &config->path); } else if (!strcasecmp(name, "options")) { parse_words(value, &config->options); } else { pr_err("unknown program setting %s", name); return 1; } } return 0; }
static struct source *source_ntp_parse(char *parameter, char **settings) { char *name, *value; struct ntp_server ntp_server; struct source *source; int r = 0; if (!*parameter) { pr_err("missing address for ntp_server"); return NULL; } ntp_server.address = parameter; ntp_server.minpoll = DEFAULT_NTP_MINPOLL; ntp_server.maxpoll = DEFAULT_NTP_MAXPOLL; ntp_server.iburst = 0; for (; *settings; settings++) { parse_setting(*settings, &name, &value); if (!strcasecmp(name, "minpoll")) { r = parse_int(value, &ntp_server.minpoll); } else if (!strcasecmp(name, "maxpoll")) { r = parse_int(value, &ntp_server.maxpoll); } else if (!strcasecmp(name, "iburst")) { r = parse_bool(value, &ntp_server.iburst); } else { pr_err("unknown ntp_server setting %s", name); return NULL; } if (r) { pr_err("invalid value %s for %s", value, name); return NULL; } } source = malloc(sizeof(*source)); source->type = NTP_SERVER; source->ntp = ntp_server; source->ntp.address = strdup(source->ntp.address); return source; }
/* * env_parse_customlcd() - parse custom lcd params from an environment variable. * * @custom_lcd_params: The environment variable containing the lcd params. * * Returns -1 on failure, 0 on success. */ static int parse_customlcd(char *custom_lcd_params) { char params_cpy[160]; char *setting; strncpy(params_cpy, custom_lcd_params, 160); setting = strtok(params_cpy, ","); while (setting) { if (parse_setting(setting) < 0) return -1; setting = strtok(NULL, ","); } /* Currently we don't support changing this via custom lcd params */ panel_cfg.data_lines = LCD_INTERFACE_24_BIT; panel_cfg.gfx_format = GFXFORMAT_RGB16; /* See dvi predefines note */ return 0; }
setting_t *setting_create(const char *file) { int fd; int len; char *f_buf; char *buf; struct stat st; setting_t *setting = NULL; fd = open(file, O_RDONLY); if(fd == -1) { fprintf(stderr, "open \"%s\": %s\n", file, strerror(errno)); return NULL; } if(-1 == fstat(fd, &st)) { perror("fstat"); close(fd); return NULL; } len = st.st_size; f_buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); if(f_buf == MAP_FAILED) { perror("mmap"); close(fd); return NULL; } buf = (char *)mem_malloc(len + 1); memcpy(buf, f_buf, len); buf[len] = 0; setting = parse_setting(buf); close(fd); munmap(f_buf, len); mem_free(buf); return setting; }