[a1785f]: config.c Maximize Restore History

Download this file

config.c    233 lines (204 with data), 5.1 kB

  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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* config.c - read configuration file */
/* Copyright (c) 2010, Lantronix, Inc. All rights reserved. */
/*
modification history
--------------------
01a,05aug08,nss written.
*/
#include "cpm.h"
#include <errno.h>
char *conf_file_default1 = "/etc/cpm.conf";
char *conf_file_default2 = "cpm.conf";
static char *skip_brank (char *ptr);
static char *get_token (char *ptr, char *token, int len);
int
read_config (const char *conf_file)
{
FILE *fp = NULL;
char input[128];
char token[CP_GROUP_NAME_LEN];
int line = 0;
cp_group_delete_all ();
if (conf_file)
fp = fopen (conf_file, "r");
if (fp == NULL) {
conf_file = conf_file_default1;
fp = fopen (conf_file, "r");
}
if (fp == NULL) {
conf_file = conf_file_default2;
fp = fopen (conf_file, "r");
}
if (fp == NULL) {
fprintf(stderr, "can't open %s: %s\n", conf_file, strerror(errno));
return (-1);
}
while (fgets(input, sizeof (input), fp)!=NULL){
char *ptr = input;
line ++;
ptr = get_token (ptr, token, sizeof (token));
if (token[0] == '#')
continue;
if (strcasecmp (token, "config") == 0) {
int state = -1;
int cp_num = 0;
ptr = get_token (ptr, token, sizeof (token));
if (strncasecmp (token, "cp", 2) != 0)
goto err;
cp_num = atoi (token+2);
#if 1 /* NSS, add */
if (cp_config_validate (cp_num)) {
fprintf(stderr, "Warning CP%d is invalid\n", cp_num);
continue;
}
#endif
ptr = get_token (ptr, token, sizeof (token));
if (strcmp (token, "{") != 0)
goto err;
while (fgets(input, sizeof (input), fp)!=NULL){
char *ptr = input;
line++;
ptr = get_token (ptr, token, sizeof (token));
if (token[0] == '#')
continue;
if (strcmp (token, "}") == 0) {
if (state != -1) {
if (cp_config_state_set (cp_num, state) != 0)
goto err;
}
break;
}
else if (strcasecmp (token, "type") == 0) {
int value= 0;
ptr = get_token (ptr, token, sizeof (token));
if (strcasecmp(token, "output") == 0)
value = 1;
else if (strcasecmp(token, "input") == 0)
value = 0;
else
goto err;
if (cp_config_type_set (cp_num, value) != 0)
goto err;
}
else if (strcasecmp (token, "state") == 0) {
ptr = get_token (ptr, token, sizeof (token));
if (strcasecmp(token, "enable") == 0)
state = 1;
else if (strcasecmp(token, "disable") == 0)
state = 0;
else
goto err;
/* set at later */
}
else if (strcasecmp (token, "invert") == 0) {
int value= 0;
ptr = get_token (ptr, token, sizeof (token));
if (strcasecmp(token, "enable") == 0)
value = 1;
else if (strcasecmp(token, "disable") == 0)
value = 0;
else
goto err;
if (cp_config_invert_set (cp_num, value) != 0)
goto err;
}
}
}
else if (strcasecmp (token, "group") == 0) {
char name[CP_GROUP_NAME_LEN];
int state = -1;
ptr = get_token (ptr, name, sizeof (name));
if (name[0] == '\0')
goto err;
ptr = get_token (ptr, token, sizeof (token));
if (strcmp (token, "{") != 0)
goto err;
if (cp_group_create (name) != 0)
goto err;
while (fgets(input, sizeof (input), fp)!=NULL){
char *ptr = input;
line++;
ptr = get_token (ptr, token, sizeof (token));
if (token[0] == '#')
continue;
if (strcmp (token, "}") == 0) {
if (state != -1) {
if (cp_group_state_set (name, state) != 0)
goto err;
}
break;
}
else if (strcasecmp (token, "type") == 0) {
int value = 0;
ptr = get_token (ptr, token, sizeof (token));
if (strcasecmp(token, "output") == 0)
value = 1;
else if (strcasecmp(token, "input") == 0)
value = 0;
else
goto err;
if (cp_group_type_set (name, value) != 0)
goto err;
}
else if (strcasecmp (token, "state") == 0) {
ptr = get_token (ptr, token, sizeof (token));
if (strcasecmp(token, "enable") == 0)
state = 1;
else if (strcasecmp(token, "disable") == 0)
state = 0;
else
goto err;
/* set at later */
}
else if (strncasecmp (token, "bit", 3) == 0) {
int bit_num;
int cp_num;
bit_num = atoi (token+3);
ptr = get_token (ptr, token, sizeof (token));
if (strncasecmp (token, "cp", 2) != 0)
goto err;
cp_num = atoi (token+2);
#if 1 /* NSS, add */
if (cp_config_validate (cp_num)) {
fprintf(stderr, "Warning CP%d is invalid\n", cp_num);
continue;
}
#endif
if (cp_group_add (name, bit_num, cp_num) != 0)
goto err;
}
}
}
}
fclose (fp);
return 0;
err:
fprintf(stderr, "Error %s in %d\n", conf_file, line);
err2:
if (fp) fclose (fp);
cp_group_delete_all ();
return -1;
}
static char *
skip_brank (char *ptr)
{
if (!ptr)
return NULL;
while (*ptr == ' ' || *ptr == '\t')
ptr ++;
return ptr;
}
static char *
get_token (char *ptr, char *token, int len)
{
int i = 0;
if (!ptr || !token)
return NULL;
ptr = skip_brank (ptr);
while (' ' < *ptr && i < (len - 1))
token[i++] = *ptr ++;
while (' ' < *ptr)
ptr++;
token[i] = '\0';
return ptr;
}