int append_path_value(char *key, char *value_to_append) { char *str, *str2; int result; if ((str = get_configuration_value(key)) != NULL) { str2 = fizmo_malloc(strlen(str) + strlen(value_to_append) + 2); strcpy(str2, str); strcat(str2, ":"); strcat(str2, value_to_append); TRACE_LOG("Appended path: %s.\n", str2); result = set_configuration_value(key, str2); free(str2); return result; } else return set_configuration_value(key, value_to_append); }
struct z_story_list *update_fizmo_story_list() { #ifdef DISABLE_CONFIGFILES return NULL; #else // DISABLE_CONFIGFILES struct z_story_list *result; struct z_story_list_entry *entry; char *str, *str_copy, *path; z_file *file; struct babel_info *babel; int i; nof_files_found = 0; nof_files_searched = 0; nof_directories_searched = 0; ensure_dot_fizmo_dir_exists(); babel = load_babel_info(); if (babel_files_have_changed(babel) == true) { // Don't load current list of story, rebuild index with the newly // changed babel data. result = get_empty_z_story_list(); store_babel_info_timestamps(babel); } else { // Babel data is the same, use pre-indexed story list. result = get_z_story_list(); i = 0; while (i < result->nof_entries) { entry = result->entries[i]; if ((file = fsi->openfile( entry->filename, FILETYPE_DATA, FILEACCESS_READ)) == NULL) remove_entry_from_list(result, entry); else { fsi->closefile(file); i++; } } } if ((str = getenv("ZCODE_PATH")) == NULL) str = getenv("INFOCOM_PATH"); if (str != NULL) set_configuration_value("z-code-path", str); if ((str = getenv("ZCODE_ROOT_PATH")) != NULL) set_configuration_value("z-code-root-path", str); if ((str = get_configuration_value("z-code-path")) != NULL) { path = strtok(str, ":"); while (path != NULL) { TRACE_LOG("Counting for token \"%s\".\n", path); nof_files_found += count_files(path, false); path = strtok(NULL, ":"); } } if ((str = get_configuration_value("z-code-root-path")) != NULL) { str_copy = strdup(str); path = strtok(str_copy, ":"); while (path != NULL) { TRACE_LOG("Counting for token \"%s\".\n", path); nof_files_found += count_files(path, true); path = strtok(NULL, ":"); } free(str_copy); } TRACE_LOG("nof_files_found: %d, %d\n", nof_files_found, NUMBER_OF_FILES_TO_SHOW_PROGRESS_FOR); if (nof_files_found >= NUMBER_OF_FILES_TO_SHOW_PROGRESS_FOR) show_progress = true; else show_progress = false; //printf("\n"); // newline for \r-progress indicator //build_filelist(".", result, false, babel); if ((str = get_configuration_value("z-code-path")) != NULL) { str_copy = strdup(str); path = strtok(str_copy, ":"); while (path != NULL) { build_filelist(path, result, false, babel); path = strtok(NULL, ":"); } free(str_copy); } if ((str = get_configuration_value("z-code-root-path")) != NULL) { str_copy = strdup(str); path = strtok(str_copy, ":"); while (path != NULL) { build_filelist(path, result, true, babel); path = strtok(NULL, ":"); } free(str_copy); } if (show_progress == true) printf("\n"); TRACE_LOG("noffiles: %d\n", result->nof_entries); save_story_list(result); store_babel_info_timestamps(babel); free_babel_info(babel); return result; #endif // DISABLE_CONFIGFILES }
int main ( int argc, char* argv[] ) { char *distroname = NULL; int valid_args , c; while (1) { int option_index = 0; c = getopt_long_only( argc , argv , "d:" , long_options , &option_index ); if (c == -1) break; switch (c) { case 'd': distroname = malloc( strlen(optarg) * sizeof(char) ); strcpy( distroname , optarg ); break; } } valid_args = argc - ( optind - 1 ); if ( valid_args < 2 ) { print_usage( argv[0] ); exit(1); } if ( strcmp( argv[optind] , "register" ) == 0 ) { if ( valid_args != 4 ) { printf( "Usage register\n" ); exit(1); } } if ( strcmp( argv[optind] , "login" ) == 0 ) { if ( valid_args != 2 ) { printf( "Usage login\n" ); exit(1); } } if ( strcmp( argv[optind] , "loginout" ) == 0 ) { if ( valid_args != 3 ) { printf( "Usage logout\n" ); exit(1); } } // TODO : Use a define instead of 256 AMEBA_SESSID_MAX_LENGTH char *sessid = malloc( 256 * sizeof(char) ); memset( sessid , '\0' , 256 ); if ( strncmp( argv[optind] , "login" , 5 ) == 0 ) { GKeyFile *config = get_configuration(); if ( ! check_configuration( config , 1 ) ) { printf("No valid configuration exists\n"); free_configuration( config ); exit(2); } login_node( get_configuration_value( config , "url" ) , get_configuration_value( config , "uuid" ) , sessid ); free_configuration( config ); } if ( strcmp( argv[optind] , "loginout" ) == 0 ) { GKeyFile *config = get_configuration(); if ( ! check_configuration( config , 1 ) ) { printf("No valid configuration exists\n"); free_configuration( config ); exit(2); } logout_node( get_configuration_value( config , "url" ) , sessid , argv[optind+1] ); free_configuration( config ); } if ( strcmp( argv[optind] , "register" ) == 0 ) { nodeinfo *nodedata = get_nodeinfo( argv[optind+2] , distroname ); if ( ! nodedata ) { exit(-255); } register_node( argv[optind+1] , nodedata ); free_nodeinfo( nodedata ); } free(sessid); exit(0); }
char *get_configuration_value(char *key) { int i = 0; char **interface_option_names; if (key == NULL) return NULL; TRACE_LOG("Retrieving config value: %s.\n", key); if (strcmp(key, "i18n-search-path") == 0) { // Forward to i18n, since this is in tools and cannot access the // "config.c" file. return get_i18n_search_path(); } else if (strcmp(key, "locale") == 0) { return get_current_locale_name_in_utf8(); } else if ( (strcmp(key, "background-color-name") == 0) || (strcmp(key, "foreground-color-name") == 0) ) { return z_colour_names[ atoi( get_configuration_value( ( strcmp(key, "background-color-name") == 0 ? "background-color" : "foreground-color")))]; } else { while (configuration_options[i].name != NULL) { TRACE_LOG("i:%d, name:%s.\n", i, configuration_options[i].name); if (strcmp(configuration_options[i].name, key) == 0) { // Boolean options if ( (strcmp(key, "disable-external-streams") == 0) || (strcmp(key, "disable-restore") == 0) || (strcmp(key, "disable-save") == 0) || (strcmp(key, "disable-sound") == 0) || (strcmp(key, "enable-font3-conversion") == 0) || (strcmp(key, "quetzal-umem") == 0) || (strcmp(key, "restore-after-save-and-quit-file-before-read") == 0) || (strcmp(key, "save-and-quit-file-before-read") == 0) || (strcmp(key, "set-tandy-flag") == 0) || (strcmp(key, "start-command-recording-when-story-starts") == 0) || (strcmp(key, "start-script-when-story-starts") == 0) || (strcmp(key, "start-file-input-when-story-starts") == 0) || (strcmp(key, "disable-stream-2-hyphenation") == 0) || (strcmp(key, "disable-stream-2-wrap") == 0) || (strcmp(key, "sync-transcript") == 0) || (strcmp(key, "dont-set-locale-from-config") == 0) ) { if (configuration_options[i].value == NULL) { TRACE_LOG("return \"false\".\n"); return config_false_value; } else { TRACE_LOG("return \"%s\".\n", configuration_options[i].value); return configuration_options[i].value; } } // String options else if ( (strcmp(key, "random-mode") == 0) || (strcmp(key, "z-code-path") == 0) || (strcmp(key, "z-code-root-path") == 0) || (strcmp(key, "autosave-filename") == 0) || (strcmp(key, "savegame-path") == 0) || (strcmp(key, "savegame-default-filename") == 0) || (strcmp(key, "transcript-filename") == 0) || (strcmp(key, "input-command-filename") == 0) || (strcmp(key, "record-command-filename") == 0) || (strcmp(key, "background-color") == 0) || (strcmp(key, "foreground-color") == 0) || (strcmp(key, "save-text-history-paragraphs") == 0) || (strcmp(key, "stream-2-line-width") == 0) || (strcmp(key, "stream-2-left-margin") == 0) || (strcmp(key, "max-undo-steps") == 0) ) { TRACE_LOG("Returning value at %p.\n", configuration_options[i].value); return configuration_options[i].value; } else { // Internal error: config-key was found in configuration_options[], // but not in the hardcoded keys above. TRACE_LOG("Unknown config key: \"%s\".", key); return NULL; } } i++; } if (active_interface != NULL) { if ((interface_option_names = active_interface->get_config_option_names()) != NULL) { i = 0; while (interface_option_names[i] != NULL) if (strcmp(interface_option_names[i++], key) == 0) return active_interface->get_config_value(key); } } if (active_sound_interface != NULL) { if ((interface_option_names = active_sound_interface->get_config_option_names()) != NULL) { i = 0; while (interface_option_names[i] != NULL) if (strcmp(interface_option_names[i++], key) == 0) return active_sound_interface->get_config_value(key); } } TRACE_LOG("Unknown config key: \"%s\".", key); return NULL; } }
int set_configuration_value(char *key, char* new_unexpanded_value) { int i, return_code, result; char *new_value = NULL; char buf[BUFSIZE]; short color_code; char *endptr; if (key == NULL) return -1; if (new_unexpanded_value != NULL) if ((new_value = expand_configuration_value(new_unexpanded_value)) == NULL) return -1; #ifdef ENABLE_TRACING TRACE_LOG("Setting configuration key \"%s\".\n", key); if (new_value != NULL) { TRACE_LOG("New value: %s at %p.\n", new_value, new_value); } #endif //ENABLE_TRACING i = 0; while (configuration_options[i].name != NULL) { TRACE_LOG("i:%d, name:%s.\n", i, configuration_options[i].name); if (strcmp(configuration_options[i].name, key) == 0) { // Parse option values which cannot be simply copied: if (strcmp(key, "locale") == 0) { TRACE_LOG("Trying to set locale to \"%s\".\n", new_value); return_code = (strcmp(get_configuration_value( "dont-set-locale-from-config"), "true") == 0) ? 0 : set_current_locale_name(new_value); free(new_value); return return_code; } else if (strcmp(key, "random-mode") == 0) { if (new_value == NULL) return -1; else if (strcmp(new_value, "random") == 0) { if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = new_value; seed_random_generator(); return 0; } else if (strcmp(new_value, "predictable") == 0) { if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = new_value; TRACE_LOG("stored value: %p.\n", configuration_options[i].value); seed_random_generator(); return 0; } else return -1; } else if (strcmp(key, "i18n-search-path") == 0) { // Forward to i18n, since this is in tools and cannot access the // "config.c" file. return_code = set_i18n_search_path(new_value); free(new_value); return return_code; } // Options for values which can simply be copied. else if ( (strcmp(key, "z-code-path") == 0) || (strcmp(key, "z-code-root-path") == 0) || (strcmp(key, "autosave-filename") == 0) || (strcmp(key, "savegame-path") == 0) || (strcmp(key, "savegame-default-filename") == 0) || (strcmp(key, "transcript-filename") == 0) || (strcmp(key, "save-text-history-paragraphs") == 0) || (strcmp(key, "input-command-filename") == 0) || (strcmp(key, "record-command-filename") == 0) ) { if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = new_value; return 0; } // Integer values else if ( (strcmp(key, "stream-2-line-width") == 0) || (strcmp(key, "stream-2-left-margin") == 0) || (strcmp(key, "max-undo-steps") == 0) ) { if (new_value == NULL) return -1; if (strlen(new_value) == 0) { free(new_value); return -1; } strtol(new_value, &endptr, 10); if (*endptr != 0) { free(new_value); return -1; } if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = new_value; return 0; } // Color options else if (strcmp(key, "foreground-color") == 0) { if (new_value == NULL) return -1; color_code = color_name_to_z_colour(new_value); free(new_value); if (color_code == -1) return -1; if (snprintf(buf, BUFSIZE, "%d", color_code) >= BUFSIZE) return -1; if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = fizmo_strdup(buf); default_foreground_colour = color_code; return 0; } else if (strcmp(key, "background-color") == 0) { if (new_value == NULL) return -1; color_code = color_name_to_z_colour(new_value); free(new_value); if (color_code == -1) return -1; if (snprintf(buf, BUFSIZE, "%d", color_code) >= BUFSIZE) return -1; if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = fizmo_strdup(buf); default_background_colour = color_code; return 0; } // Boolean options else if ( (strcmp(key, "disable-external-streams") == 0) || (strcmp(key, "disable-restore") == 0) || (strcmp(key, "disable-save") == 0) || (strcmp(key, "disable-sound") == 0) || (strcmp(key, "enable-font3-conversion") == 0) || (strcmp(key, "quetzal-umem") == 0) || (strcmp(key, "random-mode") == 0) || (strcmp(key, "restore-after-save-and-quit-file-before-read") == 0) || (strcmp(key, "save-and-quit-file-before-read") == 0) || (strcmp(key, "set-tandy-flag") == 0) || (strcmp(key, "start-command-recording-when-story-starts") == 0) || (strcmp(key, "start-script-when-story-starts") == 0) || (strcmp(key, "start-file-input-when-story-starts") == 0) || (strcmp(key, "disable-stream-2-hyphenation") == 0) || (strcmp(key, "disable-stream-2-wrap") == 0) || (strcmp(key, "sync-transcript") == 0) || (strcmp(key, "dont-set-locale-from-config") == 0) ) { if ( (new_value == NULL) || (*new_value == 0) || (strcmp(new_value, config_true_value) == 0) ) { if (new_value != NULL) free(new_value); if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = fizmo_strdup(config_true_value); return 0; } else if ((new_value != NULL) && (strcmp(new_value, config_false_value)==0)) { free(new_value); if (configuration_options[i].value != NULL) free(configuration_options[i].value); configuration_options[i].value = fizmo_strdup(config_false_value); return -1; } else { free(new_value); return -1; } } else { i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_UNKNOWN_CONFIGURATION_OPTION_P0S, -0x0101, key); } } i++; } if (active_interface != NULL) { result = active_interface->parse_config_parameter(key, new_value); if (result == -1) { i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_INVALID_VALUE_P0S_FOR_PARAMETER_P1S, -0x0101, key, new_value); } } if (active_sound_interface == NULL) return -2; else { result = active_sound_interface->parse_config_parameter(key, new_value); if (result == -1) { i18n_translate_and_exit( libfizmo_module_name, i18n_libfizmo_INVALID_VALUE_P0S_FOR_PARAMETER_P1S, -0x0101, key, new_value); } } return result; }