Bumped version to 3.11.6; Updated ChangeLog.
[collectd.git] / src / libconfig / conf_space.c
1 #include "compat.h"
2 #include "libconfig.h"
3 #include "libconfig_private.h"
4 #include "conf_space.h"
5
6 #ifdef HAVE_STDIO_H
7 #include <stdio.h>
8 #endif
9
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
13
14 int lc_process_conf_space(const char *appname, const char *configfile) {
15         LC_FILE *configfp = NULL;
16         const char *local_lc_errfile;
17         char linebuf[LC_LINEBUF_LEN] = {0}, *linebuf_ptr = NULL;
18         char *cmd = NULL, *value = NULL, *sep = NULL;
19         char *fgetsret = NULL;
20         int local_lc_errline;
21         int lcpvret = -1;
22         int retval = 0;
23         lc_err_t save_lc_errno = LC_ERR_NONE;
24
25         local_lc_errfile = configfile;
26         local_lc_errline = 0;
27
28         if (appname == NULL || configfile == NULL) {
29                 lc_errfile = local_lc_errfile;
30                 lc_errline = local_lc_errline;
31                 lc_errno = LC_ERR_INVDATA;
32                 return(-1);
33         }
34
35         configfp = lc_fopen(configfile, "r");
36
37         if (configfp == NULL) {
38                 lc_errfile = local_lc_errfile;
39                 lc_errline = local_lc_errline;
40                 lc_errno = LC_ERR_CANTOPEN;
41                 return(-1);
42         }
43
44         while (1) {
45                 fgetsret = lc_fgets(linebuf, sizeof(linebuf) - 1, configfp);
46                 if (fgetsret == NULL) {
47                         break;
48                 }
49                 if (lc_feof(configfp)) {
50                         break;
51                 }
52
53                 local_lc_errline++;
54
55                 linebuf_ptr = &linebuf[strlen(linebuf) - 1];
56                 while (*linebuf_ptr < ' ' && linebuf_ptr >= linebuf) {
57                         *linebuf_ptr = '\0';
58                         linebuf_ptr--;
59                 }
60
61                 linebuf_ptr = &linebuf[0];
62                 while (*linebuf_ptr == ' ') {
63                         linebuf_ptr++;
64                 }
65
66                 if (*linebuf_ptr == '#' || *linebuf_ptr == '\0') {
67                         continue;
68                 }
69
70
71                 sep = strpbrk(linebuf_ptr, " \t");
72                 cmd = linebuf_ptr;
73                 if (sep != NULL) {
74                         *sep = '\0';
75                         sep++;
76                         while (*sep == ' ' || *sep == '\t') {
77                                 sep++;
78                         }
79                         value = sep;
80                 } else {
81                         value = NULL;
82                 }
83
84                 save_lc_errno = lc_errno;
85                 lc_errno = LC_ERR_NONE;
86                 lcpvret = lc_process_var(cmd, NULL, value, LC_FLAGS_VAR);
87                 if (lcpvret < 0) {
88                         if (lc_errno == LC_ERR_NONE) {
89 #ifdef DEBUG
90                                 fprintf(stderr, "Invalid command: \"%s\"\n", cmd);
91 #endif
92                                 lc_errno = LC_ERR_INVCMD;
93                         } else {
94 #ifdef DEBUG
95                                 fprintf(stderr, "Error processing command (command was valid, but an error occured, errno was set)\n");
96 #endif
97                         }
98                         lc_errfile = local_lc_errfile;
99                         lc_errline = local_lc_errline;
100                         retval = -1;
101                 } else {
102                         lc_errno = save_lc_errno;
103                 }
104         }
105
106         lc_fclose(configfp);
107
108         return(retval);
109 }