From f1fb9827faec228246062f2cd8e38f0dd3338e3a Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Tue, 24 Mar 2015 20:16:29 +0900 Subject: Added ini file support --- Kconfig | 2 +- Makefile | 2 +- articles.h | 4 +- bbsconfig.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ bbsconfig.h | 18 +++++-- buildinfo.h | 2 +- captcha.c | 2 - kconfig.h | 7 --- libterm/Makefile | 2 +- libterm/term.c | 11 ++++- libterm/term.h | 3 ++ microbbs.c | 10 +++- sysinfo.c | 12 +++++ sysinfo.h | 2 + user.c | 4 +- 15 files changed, 204 insertions(+), 23 deletions(-) diff --git a/Kconfig b/Kconfig index 8c91bc3..7ab7a51 100644 --- a/Kconfig +++ b/Kconfig @@ -44,7 +44,7 @@ menuconfig LOGIN if LOGIN config USER_DEFAULT_DIR string "Defaul user data directory" - default "./users" + default "./users/" endif menuconfig ARTICLES diff --git a/Makefile b/Makefile index 1b2afef..9bf3049 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PROJECT=microbbs CC=gcc -CFLAGS= +CFLAGS=-g3 SOURCES=articles.c bbsconfig.c buildinfo.c captcha.c door.c file_use.c ini.c list.c logs.c mmm.c motd.c sds.c session.c statistics.c sysinfo.c telnetd.c textview.c todo.c user.c vote.c OBJECTS=$(SOURCES:.c=.o) BUILD_DIR=build_dir diff --git a/articles.h b/articles.h index a0ad530..8cfc9f6 100644 --- a/articles.h +++ b/articles.h @@ -3,8 +3,8 @@ #define _XOPEN_SOURCE 500 -#ifndef _BSD_SOURCE - #define _BSD_SOURCE +#ifndef _DEFAULT_SOURCE + #define _DEFAULT_SOURCE #endif #define _POSIX_C_SOURCE 200809L diff --git a/bbsconfig.c b/bbsconfig.c index 5ad6a58..4eed4a6 100644 --- a/bbsconfig.c +++ b/bbsconfig.c @@ -1,2 +1,148 @@ #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; +} + + diff --git a/bbsconfig.h b/bbsconfig.h index 55be15a..bcd2100 100644 --- a/bbsconfig.h +++ b/bbsconfig.h @@ -3,7 +3,15 @@ #include #include +#include +#include +#include +#include +#include +#define ASSERT assert + +#include "kconfig.h" #include "list.h" #include "sds.h" #include "file_use.h" @@ -17,17 +25,18 @@ typedef struct bbs_config { //todo configs //list of filenames that considered to be todos + char *todo_dir; List *todo_files; //directories where could be articles - List *article_dirs; + char *article_dir; + + //if defined this then there is directory where all needed is in standart layout + char *root_dir; //directory with all user inis char *user_dir; - //show motd messages? - int motd; - //write syslogs? int syslog; @@ -42,6 +51,7 @@ bbs_config g_config; extern bbs_config g_config; #endif +int config_default(); int config_from_ini( const char *config_file ); int config_from_argv( int argc, char **argv ); diff --git a/buildinfo.h b/buildinfo.h index 03b99d9..296879e 100644 --- a/buildinfo.h +++ b/buildinfo.h @@ -4,7 +4,7 @@ #include #include -#define BUILD_VERSION "0.2.2" +#define BUILD_VERSION "0.2.3" #define BUILD_DATE (__DATE__) void print_build_info(); diff --git a/captcha.c b/captcha.c index 9ee89a8..ea38d85 100644 --- a/captcha.c +++ b/captcha.c @@ -9,8 +9,6 @@ int captcha_test1( term_screen *ts) { int ret = 0; - const int s_size = 2; - char s[s_size]; int column=0, row=0; int c=0; diff --git a/kconfig.h b/kconfig.h index 7810247..ec26978 100644 --- a/kconfig.h +++ b/kconfig.h @@ -1,12 +1,5 @@ #ifndef __KCONFIG_H #define __KCONFIG_H -#define CONFIG_TODO -#define CONFIG_TODO_DEFAULT_FILE "todo/todo.txt" -#define CONFIG_DOORGAMES -#define CONFIG_DOOR_DEFAULT_DIR "./door/" #define CONFIG_LOGIN #define CONFIG_USER_DEFAULT_DIR "./users/" -#define CONFIG_ARTICLES -#define CONFIG_MOTD -#define CONFIG_CAPTCHA #endif diff --git a/libterm/Makefile b/libterm/Makefile index 457eea6..36e9932 100644 --- a/libterm/Makefile +++ b/libterm/Makefile @@ -1,7 +1,7 @@ PROJECT=libterm CC=gcc LD=ld -CFLAGS= +CFLAGS=-g3 make: $(CC) $(CFLAGS) -c term.c diff --git a/libterm/term.c b/libterm/term.c index 532dc92..241b650 100644 --- a/libterm/term.c +++ b/libterm/term.c @@ -250,17 +250,26 @@ exit_error: } +#include //clean terminal with escape command int term_clr_scr( term_screen *ts ) { int ret = 0; - if ( write( ts->ofd, T_ESC "[H" T_ESC "[2J", 7 ) <= 0 ){}; + char s[] = T_ESC "[H" T_ESC "[2J"; + + + ASSERT( strlen(s)>0 ); + ASSERT( ts != NULL ); + ASSERT( ts->ofd > 0 ); + + if ( write( ts->ofd, s, strlen(s) ) <= 0 ){}; return ret; } + //set terminal default input/output behavior int term_set_raw_mode( term_screen *ts ) { diff --git a/libterm/term.h b/libterm/term.h index 51af31d..878cda4 100644 --- a/libterm/term.h +++ b/libterm/term.h @@ -12,10 +12,13 @@ #include #include #include +#include #include "screen_modes.h" #include "debug.h" +#define ASSERT assert + enum TERM_KEY_ACTION { KEY_NULL = 0, /* NULL */ CTRL_A = 1, /* Ctrl+a */ diff --git a/microbbs.c b/microbbs.c index 92aa1ed..cb8cd68 100644 --- a/microbbs.c +++ b/microbbs.c @@ -25,6 +25,14 @@ int main( int argc, char **argv ) int main_menu_input=0; char main_menu_cmd; +#ifdef NDEBUG + printf("Assertions on\n"); +#endif + + //initialise default config options + config_default(); + config_from_argv( argc, argv ); + term_screen ts; memset( (void *)&ts, 0, sizeof(ts) ); term_init( &ts ); term_set_raw_mode( &ts ); @@ -38,7 +46,7 @@ int main( int argc, char **argv ) //lunch captcha and try to detect if its random bot #ifdef CONFIG_CAPTCHA - if ( captcha_test1( ts ) != 1) + if ( captcha_test1( &ts ) != 1) { goto exit_restore_terminal; } diff --git a/sysinfo.c b/sysinfo.c index 72bd41b..b43d4e5 100644 --- a/sysinfo.c +++ b/sysinfo.c @@ -24,6 +24,18 @@ int bbs_sysinfo( term_screen *ts ) term_cur_set_c( ts, 0 ); printf("Ideological support: irc://hacking.allowed.org#default\n"); + term_cur_set_c( ts, 0 ); + printf("Root dir: %s\n",g_config.root_dir ); + + term_cur_set_c( ts, 0 ); + printf("Users dir: %s\n", g_config.user_dir ); + + term_cur_set_c( ts, 0 ); + printf("Article dir: %s\n", g_config.article_dir ); + + term_cur_set_c( ts, 0 ); + printf("Syslog : %d\n", g_config.syslog ); + //main loop wait while press something term_cur_set_r( ts, term_get_maxrow( ts ) ); term_cur_set_c( ts, 0 ); diff --git a/sysinfo.h b/sysinfo.h index 2705a90..2fb9079 100644 --- a/sysinfo.h +++ b/sysinfo.h @@ -3,6 +3,8 @@ #include "buildinfo.h" +#include "bbsconfig.h" + #include "libterm/term.h" #include "libterm/term_io.h" diff --git a/user.c b/user.c index 0c0ac94..6810142 100644 --- a/user.c +++ b/user.c @@ -109,13 +109,13 @@ static int user_cfg_handler( void *user, const char *section, const char *name, const char *value ) { user_config_file *cfg = (user_config_file *)user; - - #define MATCH(s,n) strcmp(section,s) == 0 && strcmp(name,n)==0 +#define MATCH(s,n) strcmp(section,s) == 0 && strcmp(name,n)==0 if ( MATCH("user","password") ) { cfg->password = strdup( value ); } +#undef MATCH return 1; } -- cgit v1.2.3