15 #include <memcached/config_parser.h>
16 #include <memcached/util.h>
32 static int trim_copy(
char *dest,
size_t size,
const char *src,
33 const char **end,
char stop) {
34 while (isspace(*src)) {
42 const char *lastchar = src + strlen(src) - 1;
43 while (lastchar > src && isspace(*lastchar)) {
46 if (lastchar < src || *lastchar ==
'\\') {
49 assert(lastchar >= src);
52 if ((*dest = *src) ==
'\\') {
61 }
while (!(n == size || src > lastchar || ((*src == stop) && !escape) || *src ==
'\0'));
76 const char *ptr = str;
78 while (*ptr !=
'\0') {
79 while (isspace(*ptr)) {
89 if (trim_copy(key,
sizeof(key), ptr, &end,
'=') == -1) {
91 fprintf(error,
"ERROR: Invalid key, starting at: <%s>\n", ptr);
98 if (trim_copy(value,
sizeof(value), ptr, &end,
';') == -1) {
100 fprintf(error,
"ERROR: Invalid value, starting at: <%s>\n", ptr);
111 while (items[ii].key != NULL) {
112 if (strcmp(key, items[ii].key) == 0) {
113 if (items[ii].found) {
115 fprintf(error,
"WARNING: Found duplicate entry for \"%s\"\n",
120 switch (items[ii].datatype) {
126 for (
char *p = sfx; *p !=
'\0'; ++p) {
128 char *ptr = strchr(value, *p);
130 ptr = strchr(value, toupper(*p));
140 if (safe_strtoull(value, &val)) {
141 *items[ii].
value.dt_size = (size_t)(val * multiplier);
142 items[ii].
found =
true;
151 if (safe_strtof(value, &val)) {
152 *items[ii].
value.dt_float = val;
153 items[ii].
found =
true;
160 *items[ii].
value.dt_string = strdup(value);
161 items[ii].
found =
true;
164 if (strcasecmp(value,
"true") == 0 || strcasecmp(value,
"on") == 0) {
165 *items[ii].
value.dt_bool =
true;
166 items[ii].
found =
true;
167 }
else if (strcasecmp(value,
"false") == 0 || strcasecmp(value,
"off") == 0) {
168 *items[ii].
value.dt_bool =
false;
169 items[ii].
found =
true;
176 int r = read_config_file(value, items, error);
188 fprintf(error,
"Invalid entry, Key: <%s> Value: <%s>\n",
198 if (items[ii].key == NULL) {
200 fprintf(error,
"Unsupported key: <%s>\n", key);
208 static int read_config_file(
const char *fname,
struct config_item items[],
210 FILE *fp = fopen(fname,
"r");
213 fprintf(error,
"Failed to open file: %s\n", fname);
220 while (fgets(line,
sizeof(line), fp) != NULL && ret != -1L) {
221 if (line[0] ==
'#') {
226 int r = parse_config(line, items, error);