#include "bbsconfig.h" int config_default() { #ifdef CONFIG_TODO g_config.todo_dir = CONFIG_TODO_DEFAULT_FILE; #endif //g_config.todo_files = llist_new(); g_config.todo_files = NULL; g_config.article_dir = NULL; #ifdef CONFIG_LOGIN g_config.user_dir = CONFIG_USER_DEFAULT_DIR; #else g_config.user_dir = NULL; #endif g_config.root_dir = "./"; g_config.syslog = 0; g_config.debug = 0; return 0; } /* bbs config file [bbs] rootdir string tododir string articledir string userdir string syslog int */ static int bbs_cfg_handler( void *user, const char *section, const char *name, const char *value ) { bbs_config *cfg = (bbs_config *)user; #define MATCH(s,n) strcmp(section,s) == 0 && strcmp(name,n)==0 if ( MATCH("bbs","rootdir") ) { cfg->root_dir = strdup( value ); } else if ( MATCH("bbs","tododir") ) { cfg->todo_dir = strdup( value ); } else if ( MATCH("bbs","articledir") ) { cfg->article_dir = strdup( value ); } else if ( MATCH("bbs","userdir") ) { cfg->user_dir = strdup( value ); } else if ( MATCH("bbs","syslog") ) { cfg->syslog = atoi( value )&0x01; } #undef MATCH return 1; } int config_from_ini( const char *config_file ) { int ret=-1; bbs_config cfg; memset( (void *)&cfg, 0, sizeof(bbs_config)); ASSERT(config_file != NULL); //check patch if file excists if ( access(config_file, R_OK) < 0 ) { printf("Cannot open ini config file: %s\n", strerror(errno)); ret = -1; return ret; } //parse config file if ( ini_parse( config_file, bbs_cfg_handler, &cfg ) < 0 ) { printf("Cannot parse config file\n"); ret = -1; } g_config = cfg; return ret; } int config_from_argv( int argc, char **argv ) { int ret=-1; int c=0; int iflag=0; char *ini_file_dir=NULL; while ( (c = getopt(argc, argv, "a:i:r:u:st:")) != -1 ) { switch (c) { case 'a': g_config.article_dir = optarg; break; case 'i': iflag = 1; ini_file_dir = optarg; break; case 'r': g_config.root_dir = optarg; break; case 'u': g_config.user_dir = optarg; break; case 's': g_config.syslog = 1; break; case 't': g_config.todo_dir = optarg; break; case '?': if (( optopt == 'a') || (optopt == 'i') || (optopt == 'r') || (optopt == 'u') || (optopt == 't')) printf("Option -%c requires argument\n", optopt ); else if( isprint(c) ) printf("Unknown option -%c\n", optopt); else printf("Unknown option character\n"); break; default: printf("Unknown option\n"); ret = -1; } } if ( iflag == 1) { config_from_ini( ini_file_dir ); } return ret; }