From 5e5e5b1cbcb98eda7e0c52a367c66b944b480eda Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sat, 16 May 2015 19:22:46 +0900 Subject: Added default config file loading. Add functionality for bbs todo file. --- articles.h | 4 ++-- bbsconfig.h | 3 +++ file_use.c | 6 +++--- libterm/term.c | 11 +---------- libterm/term.h | 3 --- microbbs.c | 10 +--------- todo.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- todo.h | 3 +++ user.c | 3 ++- 9 files changed, 76 insertions(+), 29 deletions(-) diff --git a/articles.h b/articles.h index 8cfc9f6..a0ad530 100644 --- a/articles.h +++ b/articles.h @@ -3,8 +3,8 @@ #define _XOPEN_SOURCE 500 -#ifndef _DEFAULT_SOURCE - #define _DEFAULT_SOURCE +#ifndef _BSD_SOURCE + #define _BSD_SOURCE #endif #define _POSIX_C_SOURCE 200809L diff --git a/bbsconfig.h b/bbsconfig.h index bcd2100..459aeae 100644 --- a/bbsconfig.h +++ b/bbsconfig.h @@ -37,6 +37,9 @@ typedef struct bbs_config //directory with all user inis char *user_dir; + //show motd messages? + int motd; + //write syslogs? int syslog; diff --git a/file_use.c b/file_use.c index 0cfa823..8b72e25 100644 --- a/file_use.c +++ b/file_use.c @@ -170,8 +170,8 @@ f_file* f_file_open( const char *fname, int flags ) { f_file *ret = NULL; - char *f_flags_write="r"; - char *f_flags_read="w"; + char *f_flags_write="w"; + char *f_flags_read="r"; char *f_flags_rw="r+"; char *f_flags_tmp=NULL; @@ -207,7 +207,7 @@ f_file* f_file_open( const char *fname, int flags ) ERROR("Unknown flag for opening \n"); goto exit_close_f; } - ret->fid = fopen( fname, "r" ); + ret->fid = fopen( fname, f_flags_tmp ); if ( ret->fid == NULL ) { ERROR("Cannot open file\n"); diff --git a/libterm/term.c b/libterm/term.c index 241b650..532dc92 100644 --- a/libterm/term.c +++ b/libterm/term.c @@ -250,26 +250,17 @@ exit_error: } -#include //clean terminal with escape command int term_clr_scr( term_screen *ts ) { int ret = 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 ){}; + if ( write( ts->ofd, T_ESC "[H" T_ESC "[2J", 7 ) <= 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 878cda4..51af31d 100644 --- a/libterm/term.h +++ b/libterm/term.h @@ -12,13 +12,10 @@ #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 cb8cd68..92aa1ed 100644 --- a/microbbs.c +++ b/microbbs.c @@ -25,14 +25,6 @@ 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 ); @@ -46,7 +38,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/todo.c b/todo.c index 745db49..aa074b2 100644 --- a/todo.c +++ b/todo.c @@ -59,7 +59,7 @@ int bbs_todo( term_screen *ts, const char *fname) { term_clr_scr( ts ); term_cur_set_c( ts, 0 ); - printf("(Q)uit"); fflush( stdout ); + printf("(A)dd,(Q)uit"); fflush( stdout ); //dispaly todo list row = term_get_maxrow( ts ); @@ -95,6 +95,11 @@ int bbs_todo( term_screen *ts, const char *fname) quit_loop = 1; break; + case 'a': + case 'A': + bbs_todo_add( ts, todo_list, todo_fname ); + break; + case 's': case 'S': { @@ -119,4 +124,59 @@ int bbs_todo( term_screen *ts, const char *fname) return ret; } +int bbs_todo_add( term_screen *ts, List *todo, const char *fname) +{ + int ret = -1; + int fret = -1; + const int buf_size = 64; + char buf[buf_size]; + + if ( ts == NULL ) + return -1; + + if ( fname == NULL ) + return -1; + + term_clr_scr( ts ); + term_cur_set_c( ts, 0 ); + term_cur_set_r( ts, 0 ); + printf("Add new todo task:\n"); fflush( stdout ); + + memset( buf, 0, buf_size ); + term_cur_set_c( ts, 0 ); + fret = term_readline( ts, buf, buf_size, READLINE_ALPHA ); + if ( fret > 0 ) + { + char *l = malloc( fret ); + memcpy( l, buf, fret ); + llist_push( todo, l ); + bbs_todo_save( fname, todo ); + ret = 0; + } else + { + return -1; + } + + return ret; +} + +int bbs_todo_save( const char *fname, List *todo ) +{ + int ret=-1; + + f_file *f=NULL; + struct ListNode *iter=todo->first; + + f = f_file_open( fname, F_FILE_WRITE ); + while ( iter->next != NULL ) + { + f_file_write( f, strlen(iter->val), iter->val ); + printf("%s\n", iter->val ); + iter = iter->next; + } + f_file_close( f ); + + return ret; +} + #endif \ No newline at end of file diff --git a/todo.h b/todo.h index 79bff9c..724a2c8 100644 --- a/todo.h +++ b/todo.h @@ -3,6 +3,7 @@ //part of libterm #include "libterm/term.h" +#include "libterm/term_io.h" #include "logs.h" #include "file_use.h" #include "list.h" @@ -10,5 +11,7 @@ #include "kconfig.h" int bbs_todo( term_screen *, const char *); +int bbs_todo_add( term_screen*, List*, const char*); +int bbs_todo_save( const char*, List* ); #endif \ No newline at end of file diff --git a/user.c b/user.c index 6810142..5e2ef19 100644 --- a/user.c +++ b/user.c @@ -109,7 +109,8 @@ 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") ) { -- cgit v1.2.3