3 #include "libconfig_private.h"
4 #include "conf_section.h"
14 int lc_process_conf_section(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 qualifbuf[LC_LINEBUF_LEN] = {0};
19 char *cmd = NULL, *value = NULL, *sep = NULL, *cmdend = NULL;
20 char *currsection = NULL;
21 char *fgetsret = NULL;
23 int invalid_section = 1, ignore_section = 0;
26 lc_err_t save_lc_errno = LC_ERR_NONE;
28 local_lc_errfile = configfile;
31 if (appname == NULL || configfile == NULL) {
32 lc_errfile = local_lc_errfile;
33 lc_errline = local_lc_errline;
34 lc_errno = LC_ERR_INVDATA;
38 configfp = lc_fopen(configfile, "r");
40 if (configfp == NULL) {
41 lc_errfile = local_lc_errfile;
42 lc_errline = local_lc_errline;
43 lc_errno = LC_ERR_CANTOPEN;
48 fgetsret = lc_fgets(linebuf, sizeof(linebuf) - 1, configfp);
49 if (fgetsret == NULL) {
52 if (lc_feof(configfp)) {
58 /* Remove trailing crap (but not spaces). */
59 linebuf_ptr = &linebuf[strlen(linebuf) - 1];
60 while (*linebuf_ptr < ' ' && linebuf_ptr >= linebuf) {
65 /* Handle section header. */
66 if (linebuf[0] == '[' && linebuf[strlen(linebuf) - 1] == ']') {
67 linebuf[strlen(linebuf) - 1] = '\0';
68 linebuf_ptr = &linebuf[1];
70 /* If a section was open, close it. */
71 if (currsection != NULL) {
72 lcpvret = lc_process_var(currsection, NULL, NULL, LC_FLAGS_SECTIONEND);
75 fprintf(stderr, "Invalid section terminating: \"%s\"\n", currsection);
81 /* Open new section. */
82 currsection = strdup(linebuf_ptr);
83 lcpvret = lc_process_var(currsection, NULL, NULL, LC_FLAGS_SECTIONSTART);
86 fprintf(stderr, "Invalid section: \"%s\"\n", currsection);
89 lc_errfile = local_lc_errfile;
90 lc_errline = local_lc_errline;
91 lc_errno = LC_ERR_INVSECTION;
98 if (lcpvret == LC_CBRET_IGNORESECTION) {
104 /* Remove leading spaces. */
105 linebuf_ptr = &linebuf[0];
106 while (*linebuf_ptr == ' ') {
110 /* Drop comments and blank lines. */
111 if (*linebuf_ptr == ';' || *linebuf_ptr == '\0') {
115 /* Don't handle things for a section that doesn't exist. */
116 if (invalid_section == 1) {
118 fprintf(stderr, "Ignoring line (because invalid section): %s\n", linebuf);
123 /* Don't process commands if this section is specifically ignored. */
124 if (ignore_section == 1) {
126 fprintf(stderr, "Ignoring line (because ignored section): %s\n", linebuf);
131 /* Find the command and the data in the line. */
132 cmdend = sep = strpbrk(linebuf_ptr, "=");
135 fprintf(stderr, "Invalid line: \"%s\"\n", linebuf);
140 /* Delete space at the end of the command. */
141 cmdend--; /* It currently derefs to the seperator.. */
142 while (*cmdend <= ' ') {
149 /* Delete the seperator char and any leading space. */
152 while (*sep == ' ' || *sep == '\t') {
157 /* Create the fully qualified variable name. */
158 if (currsection == NULL) {
159 strncpy(qualifbuf, cmd, sizeof(qualifbuf) - 1);
161 snprintf(qualifbuf, sizeof(qualifbuf) - 1, "%s.%s", currsection, cmd);
164 /* Call the parent and tell them we have data. */
165 save_lc_errno = lc_errno;
166 lc_errno = LC_ERR_NONE;
167 lcpvret = lc_process_var(qualifbuf, NULL, value, LC_FLAGS_VAR);
169 if (lc_errno == LC_ERR_NONE) {
171 fprintf(stderr, "Invalid command: \"%s\"\n", cmd);
173 lc_errno = LC_ERR_INVCMD;
176 fprintf(stderr, "Error processing command (command was valid, but an error occured, errno was set)\n");
179 lc_errfile = local_lc_errfile;
180 lc_errline = local_lc_errline;
183 lc_errno = save_lc_errno;
187 /* Close any open section, and clean-up. */
188 if (currsection != NULL) {
189 lcpvret = lc_process_var(currsection, NULL, NULL, LC_FLAGS_SECTIONEND);
192 fprintf(stderr, "Invalid section terminating: \"%s\"\n", currsection);