1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
}
|