void ya_config_parse() { int ret; const char * const envhome = getenv("HOME"); if ((ya.gen_flag & GEN_EXT_CONF) == 0) snprintf(conf_file, CFILELEN, "%s/.config/yabar/yabar.conf", envhome); config_t ya_conf; config_init(&ya_conf); config_set_auto_convert(&ya_conf, CONFIG_TRUE); ret=config_read_file(&ya_conf, conf_file); if (ret == CONFIG_FALSE) { fprintf(stderr, "Error in the config file at line %d : %s\nExiting...\n", config_error_line(&ya_conf), config_error_text(&ya_conf)); config_destroy(&ya_conf); exit(EXIT_SUCCESS); } char *barstr, *blkstr; config_setting_t *barlist_set, *blklist_set; config_setting_t *curbar_set, *curblk_set; barlist_set = config_lookup(&ya_conf, "bar-list"); if(barlist_set == NULL) { fprintf(stderr, "Please add a `bar-list` entry with at least one bar.\nExiting...\n"); config_destroy(&ya_conf); exit(EXIT_SUCCESS); } ya.barnum = config_setting_length(barlist_set); if(ya.barnum < 1) { fprintf(stderr, "Please add at least one bar in the `bar-list` entry.\nExiting...\n"); config_destroy(&ya_conf); exit(EXIT_SUCCESS); } int blknum; for(int i=0; i < ya.barnum; i++) { barstr = (char *)config_setting_get_string_elem(barlist_set, i); curbar_set = config_lookup(&ya_conf, barstr); if(curbar_set == NULL) { fprintf(stderr, "No valid bar with the name (%s), skipping this invalid bar.\n", barstr); //If there is only one bar, continue won't work and we get a segfault. //We should exit anyway because we have one && invalid bar name. if(ya.barnum == 1) exit(EXIT_SUCCESS); continue; } ya_setup_bar(curbar_set); #ifdef OLD_LIBCONFIG blklist_set = config_lookup_from(curbar_set, "block-list"); #else blklist_set = config_setting_lookup(curbar_set, "block-list"); #endif if(blklist_set == NULL) { fprintf(stderr, "No `block-list` entry found for the bar(%s).\n", barstr); continue; } blknum = config_setting_length(blklist_set); if(blknum < 1) { fprintf(stderr, "No blocks in `block-list` entry for the bar(%s).\n", barstr); continue; } for (int i=0; i < blknum; i++) { blkstr = (char *)config_setting_get_string_elem(blklist_set, i); #ifdef OLD_LIBCONFIG curblk_set = config_lookup_from(curbar_set, blkstr); #else curblk_set = config_setting_lookup(curbar_set, blkstr); #endif if(curblk_set == NULL) { fprintf(stderr, "No valid block with the name (%s) in the bar (%s), skipping this block\n", blkstr, barstr); continue; } ya_setup_block(curblk_set); } } config_destroy(&ya_conf); }
// TODO: error handling // TODO: better exit point handling // (freeing resources for valgrind-cleanliness is too tedious at the moment) void options_parse(int argc, char * argv[], struct Options * options) { int opt; // modified when config is loaded on the command line config_t config; config_init(&config); set_default_config(&config); // only to used for -g: print default config config_t config_default; config_init(&config_default); set_default_config(&config_default); // -i flag should only print options after all other // command line arguments have been applied bool print_options = false; while (-1 != (opt = getopt(argc, argv, OPTSTR))) { switch (opt) { case 'c': config_set_auto_convert(&config, CONFIG_TRUE); if (CONFIG_FALSE == config_read_file(&config, optarg)) { // failure reporting fprintf(stderr, "Failed to use config file '%s'!\n", optarg); switch (config_error_type(&config)) { case CONFIG_ERR_NONE: fprintf(stderr, "\tno error reported\n" "\t(This might be a libconfig problem.)\n"); break; case CONFIG_ERR_FILE_IO: fprintf(stderr, "\tfile I/O error\n"); break; case CONFIG_ERR_PARSE: fprintf(stderr, "\tparse error on line %d:\n" "\t%s\n", config_error_line(&config), config_error_text(&config)); break; default: fprintf(stderr, "\tunknown error\n" "\t(A new libconfig version might have introduced" " new kinds of warnings.)\n"); } } break; case 'g': config_write(&config_default, stdout); // be valgrind-clean config_destroy(&config_default); config_destroy(&config); exit(EXIT_SUCCESS); break; case 'i': print_options = true; break; default: /* '?' 'h' */ options_help(*argv); // be valgrind-clean config_destroy(&config_default); config_destroy(&config); exit(EXIT_FAILURE); } } config2options(&config, options); // be valgrind-clean config_destroy(&config_default); config_destroy(&config); if (print_options) options_print(stderr, "", options); }