void initConfig() { _config_param = parameter_new(); /* config-dir */ char *home = getenv("HOME"); if (home == NULL) { error_set(ERROR_CONFIG_HOME, "Unable to get home directory."); return; } STRINGBUFFER *tmp_cfg = stringbuffer_new(); /* config-directory */ stringbuffer_append(tmp_cfg, home); stringbuffer_append(tmp_cfg, SEPARATOR); stringbuffer_append(tmp_cfg, CONFIG_DIRECTORY); _config_directory = stringbuffer_clone(tmp_cfg); /* config-file */ stringbuffer_append(tmp_cfg, SEPARATOR); stringbuffer_append(tmp_cfg, CONFIG_FILE); _config_file = stringbuffer_clone(tmp_cfg); printf ( "CONFIGFILE:\t%s\n", stringbuffer_getTextPointer(_config_file) ); stringbuffer_free(tmp_cfg); /* load config */ _config_param = parameter_new(); parameter_loadFromFile(_config_param, stringbuffer_getTextPointer(_config_file)); checkParameter(CONFIG_DIRECTORY_STORE_KEY, CONFIG_DIRECTORY_STORE_VALUE); checkParameter(CONFIG_DIRECTORY_RESTORE_KEY, CONFIG_DIRECTORY_RESTORE_VALUE); checkParameter(CONFIG_DIRECTORY_KEY_KEY, CONFIG_DIRECTORY_KEY_VALUE); /* make directories */ char *tmp_dir; /* make store directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_STORE_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); /* make restore directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_RESTORE_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); /* make key directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_KEY_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); }
static inline void toplevel_handler(void) { uint8_t parameter_button = 0xFF; // Search through the list of parameter buttons pressed, and find the first for (uint8_t i = 5; i < 16; i++) { if (button_pressed(i)) { parameter_button = i; if (button_on(BTN_SHIFT)) parameter_button |= 0x80; break; } } for (uint8_t i = 0; i < 8; i++) { // Handle the case where the main button is currently being pressed if (button_on(i)) { enum parameter_id id = get_parameter_id(i, parameter_button); // If a parameter button is being pressed and is a valid button for // the current main button, set up a getvalue session. if (id != 0xFF) { ui_getvalue_session.button1 = i; ui_getvalue_session.button2 = parameter_button; ui_getvalue_session.parameter = parameter_get(id); mode |= MODE_GETVALUE; return; } } // In the case where the main button has been depressed else if (button_depressed(i)) { struct parameter parameter = parameter_get(main_buttons[i].depress_parameter); if (parameter.type == BOOL) *parameter.target ^= 1; else { ui_getvalue_session.button1 = i; ui_getvalue_session.button2 = 0xFF; ui_getvalue_session.parameter = parameter; mode |= MODE_GETVALUE; return; } } } }
void checkParameter(char *key, char *value) { if (parameter_get(_config_param, key) == NULL) { STRINGBUFFER *tmp = stringbuffer_clone(_config_directory); stringbuffer_append(tmp, SEPARATOR); stringbuffer_append(tmp, value); parameter_add(_config_param, key, stringbuffer_getTextPointer(tmp)); stringbuffer_free(tmp); } }
/** * @brief Expands option values * * @param[in] ctx_p Context * @param[in] arg An allocated string with unexpanded value. Will be free'd * @param[out] macro_count_p A pointer to count of found macro-s * @param[out] expand_count_p A pointer to count of expanded macro-s * @param[in] parameter_get A function to resolve macro-s * @param[in] parameter_get_arg An argument to the function * * @retval char * Pointer to newly allocated string, if successful * @retval NULL On error * */ char *parameter_expand ( ctx_t *ctx_p, char *arg, int exceptionflags, int *macro_count_p, int *expand_count_p, const char * ( *parameter_get ) ( const char *variable_name, void *arg ), void *parameter_get_arg ) { debug ( 9, "(ctx_p, \"%s\" [%p], ...)", arg, arg ); char *ret = NULL; size_t ret_size = 0, ret_len = 0; #ifdef PARANOID if ( arg == NULL ) { errno = EINVAL; return NULL; } #endif if ( macro_count_p != NULL ) *macro_count_p = 0; if ( expand_count_p != NULL ) *expand_count_p = 0; char *ptr = &arg[-1]; while ( 1 ) { ptr++; switch ( *ptr ) { case 0: if ( ret == NULL ) { debug ( 3, "Expanding value \"%s\" to \"%s\" (case #1)", arg, arg ); return arg; } ret[ret_len] = 0; debug ( 3, "Expanding value \"%s\" to \"%s\" (case #0)", arg, ret ); free ( arg ); return ret; case '%': { if ( ptr[1] == '%' ) { ret[ret_len++] = * ( ptr++ ); break; } debug ( 25, "A macro" ); char nest_searching = 1; char *ptr_nest = ptr; while ( nest_searching ) { ptr_nest++; switch ( *ptr_nest ) { case 0: ret[ret_len] = 0; if ( ! ( exceptionflags & 1 ) ) warning ( "Unexpected end of macro-substitution \"%s\" in value \"%s\"; result value is \"%s\"", ptr, arg, ret ); free ( arg ); return ret; case '%': { char *variable_name; const char *variable_value; size_t variable_value_len; if ( macro_count_p != NULL ) ( *macro_count_p )++; nest_searching = 0; *ptr_nest = 0; variable_name = &ptr[1]; debug ( 15, "The macro is \"%s\"", variable_name ); if ( !strcmp ( variable_name, "PID" ) ) { debug ( 35, "\"PID\"", variable_name ); if ( !*ctx_p->pid_str ) { snprintf ( ctx_p->pid_str, 64, "%u", ctx_p->pid ); ctx_p->pid_str_len = strlen ( ctx_p->pid_str ); } variable_value = ctx_p->pid_str; variable_value_len = ctx_p->pid_str_len; } else if ( *variable_name >= 'A' && *variable_name <= 'Z' && ( exceptionflags & 4 ) ) { // Lazy substitution, preserving the value debug ( 35, "Lazy substitution", variable_name ); variable_value = ptr; variable_value_len = ( ptr_nest - ptr + 1 ); parameter_get ( variable_name, parameter_get_arg ); } else { // Substituting debug ( 35, "Substitution", variable_name ); errno = 0; variable_value = parameter_get ( variable_name, parameter_get_arg ); if ( variable_value == NULL ) { if ( ! ( exceptionflags & 2 ) && ( errno != ENOENT ) ) warning ( "Variable \"%s\" is not set (%s)", variable_name, strerror ( errno ) ); *ptr_nest = '%'; errno = 0; break; } variable_value_len = strlen ( variable_value ); if ( expand_count_p != NULL ) ( *expand_count_p )++; } *ptr_nest = '%'; if ( ret_len + variable_value_len + 1 >= ret_size ) { ret_size = ret_len + variable_value_len + 1 + ALLOC_PORTION; ret = xrealloc ( ret, ret_size ); } memcpy ( &ret[ret_len], variable_value, variable_value_len ); ret_len += variable_value_len; break; } } } ptr = ptr_nest; break; } default: { if ( ret_len + 2 >= ret_size ) { ret_size += ALLOC_PORTION + 2; ret = xrealloc ( ret, ret_size ); } ret[ret_len++] = *ptr; break; } } } error ( "Unknown internal error" ); return arg; }
char *getConfig(const char *key) { return parameter_get(_config_param, key); }
void config_load(const char *filename) { if (!file_exists(filename)) message_error("properties-file not found"); memset(&_config, '\0', sizeof(config_t)); char *tmp; PARAMETER *param = parameter_new(); parameter_loadFromFile(param, filename); /* root */ tmp = parameter_get(param, "root"); if (tmp == NULL) _config.root[0] = '\0'; else { strcpy(_config.root, tmp); free(tmp); } /* data */ tmp = parameter_get(param, "data"); if (tmp == NULL) _config.data[0] = '\0'; else { strcpy(_config.data, tmp); free(tmp); } /* exec */ tmp = parameter_get(param, "exec"); if (tmp == NULL) _config.exec[0] = '\0'; else { strcpy(_config.exec, tmp); free(tmp); } /* temp */ tmp = parameter_get(param, "temp"); if (tmp == NULL) _config.temp[0] = '\0'; else { strcpy(_config.temp, tmp); free(tmp); } /* pipe */ tmp = parameter_get(param, "pipe"); if (tmp == NULL) _config.pipe[0] = '\0'; else { strcpy(_config.pipe, tmp); free(tmp); } /* address */ tmp = parameter_get(param, "address"); if (tmp == NULL) _config.address[0] = '\0'; else { strcpy(_config.address, tmp); free(tmp); } /* port */ tmp = parameter_get(param, "port"); if (tmp == NULL) _config.port = -1; else { _config.port = number_toInteger(tmp); free(tmp); } /* timeout */ tmp = parameter_get(param, "timeout"); if (tmp == NULL) _config.timeout = -1; else { _config.timeout = number_toInteger(tmp); free(tmp); } /* challenge */ tmp = parameter_get(param, "challenge"); if (tmp == NULL) _config.challenge[0] = '\0'; else { strcpy(_config.challenge, tmp); free(tmp); } /* debug */ tmp = parameter_get(param, "debug"); if (tmp == NULL) _config.debug = FALSE; else { _config.debug = boolean_toBoolean(tmp); free(tmp); } parameter_free(param); }