Skyld AV  0.6
On access virus scanning for Linux
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
conf.c
Go to the documentation of this file.
1 /*
2  * File: conf.c
3  *
4  * Copyright 2013 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
39 #include <stdio.h>
40 #include <string.h>
41 #include "conf.h"
42 
47 static void skipComment(FILE *file) {
48  while (!feof(file)) {
49  char c;
50  c = fgetc(file);
51  if (c == '\n') {
52  break;
53  }
54  }
55 }
56 
62 static void getToken(FILE *file, char *token, int *newline) {
63  char c = 0x00;
64  char *pos = token;
65  int count = CONF_VALUE_MAX_LEN - 1;
66  *token = 0x00;
67  *newline = 0;
68 
69  // skip leading whitespace
70  while (!feof(file)) {
71  c = fgetc(file);
72  if (c == '#') {
73  skipComment(file);
74  *newline = 1;
75  return;
76  } else if (c == '\n') {
77  *newline = 1;
78  return;
79  } else if (c > ' ') {
80  break;
81  }
82  }
83  while (!feof(file)) {
84  if (c < ' ') {
85  *newline = 1;
86  return;
87  }
88  switch (c) {
89  case '#':
90  skipComment(file);
91  *newline = 1;
92  return;
93  case '\\':
94  c = fgetc(file);
95  if (feof(file) || c < ' ') {
96  *newline = 1;
97  return;
98  }
99  break;
100  case ' ':
101  case ',':
102  return;
103  }
104  if (count > 0) {
105  *pos = c;
106  pos++;
107  }
108  *pos = 0x00;
109  count--;
110  c = fgetc(file);
111  }
112 }
113 
123 int parseConfigurationFile(char *filename, conf_cb cb, void *info) {
124  int newline = 0;
125  int ret = 0;
126  FILE *file;
127  char key[CONF_VALUE_MAX_LEN];
128  char value[CONF_VALUE_MAX_LEN];
129 
130  file = fopen(filename, "r");
131  if (file == NULL) {
132  fprintf(stderr, "file '%s' not found\n", filename);
133  return 1;
134  }
135 
136  while (!feof(file)) {
137  getToken(file, key, &newline);
138  if (newline || *key == 0x00) {
139  continue;
140  }
141  if (*key == '=') {
142  fprintf(stderr, "missing key in '%s'\n", filename);
143  ret = 1;
144  break;
145  }
146  getToken(file, value, &newline);
147  if (strcmp(value, "=")) {
148  fprintf(stderr, "missing '=' in '%s'\n", filename);
149  ret = 1;
150  break;
151  }
152  getToken(file, value, &newline);
153  for (;;) {
154  if (cb == NULL) {
155  printf("%s = %s\n", key, value);
156  } else {
157  if (cb(key, value, info)) {
158  printf("Invalid entry in '%s': %s = %s\n",
159  filename, key, value);
160  ret = 1;
161  };
162  }
163  if (newline) {
164  break;
165  }
166  getToken(file, value, &newline);
167  if (0 == strcmp(value, "")) {
168  break;
169  }
170  }
171  }
172  fclose(file);
173  return ret;
174 }
static void getToken(FILE *file, char *token, int *newline)
Gets token.
Definition: conf.c:62
#define CONF_VALUE_MAX_LEN
Definition: conf.h:32
int parseConfigurationFile(char *filename, conf_cb cb, void *info)
Parses configuration file. If cb is NULL the key value pairs are output to the console. Returns 0 if successful.
Definition: conf.c:123
static void skipComment(FILE *file)
Skips comment.
Definition: conf.c:47
int(* conf_cb)(const char *key, const char *value, void *info)
Definition: conf.h:34
Analyze configuration file.