Adapt poll layer behaviour to match the epoll layer
[lttng-tools.git] / src / common / config / ini.c
1 /*
2 * inih -- simple .INI file parser
3 *
4 * The "inih" library is distributed under the New BSD license:
5 *
6 * Copyright (c) 2009, Brush Technology - All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Brush Technology nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
23 * EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * http://code.google.com/p/inih/
32 */
33
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <common/common.h>
38
39 #include "ini.h"
40
41 #if !INI_USE_STACK
42 #include <stdlib.h>
43 #endif
44
45 #define MAX_SECTION 50
46 #define MAX_NAME 50
47
48 /* Strip whitespace chars off end of given string, in place. Return s. */
49 static char* rstrip(char* s)
50 {
51 char* p = s + strlen(s);
52
53 while (p > s && isspace((unsigned char)(*--p)))
54 *p = '\0';
55 return s;
56 }
57
58 /* Return pointer to first non-whitespace char in given string. */
59 static char* lskip(const char* s)
60 {
61 while (*s && isspace((unsigned char)(*s)))
62 s++;
63 return (char*)s;
64 }
65
66 /*
67 * Return pointer to first char c or ';' comment in given string, or pointer to
68 * null at end of string if neither found. ';' must be prefixed by a whitespace
69 * character to register as a comment.
70 */
71 static char* find_char_or_comment(const char* s, char c)
72 {
73 int was_whitespace = 0;
74
75 while (*s && *s != c && !(was_whitespace && *s == ';')) {
76 was_whitespace = isspace((unsigned char)(*s));
77 s++;
78 }
79 return (char*)s;
80 }
81
82 /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
83 static char* strncpy0(char* dest, const char* src, size_t size)
84 {
85 strncpy(dest, src, size - 1);
86 dest[size - 1] = '\0';
87 return dest;
88 }
89
90 /* See documentation in header file. */
91 LTTNG_HIDDEN
92 int ini_parse_file(FILE* file, ini_entry_handler handler, void* user)
93 {
94 /* Uses a fair bit of stack (use heap instead if you need to) */
95 #if INI_USE_STACK
96 char line[INI_MAX_LINE];
97 #else
98 char* line;
99 #endif
100 char section[MAX_SECTION] = "";
101 char prev_name[MAX_NAME] = "";
102
103 char* start;
104 char* end;
105 char* name;
106 char* value;
107 int lineno = 0;
108 int error = 0;
109
110 #if !INI_USE_STACK
111 line = (char*)zmalloc(INI_MAX_LINE);
112 if (!line) {
113 return -2;
114 }
115 #endif
116
117 /* Scan through file line by line */
118 while (fgets(line, INI_MAX_LINE, file) != NULL) {
119 lineno++;
120
121 start = line;
122 #if INI_ALLOW_BOM
123 if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
124 (unsigned char)start[1] == 0xBB &&
125 (unsigned char)start[2] == 0xBF) {
126 start += 3;
127 }
128 #endif
129 start = lskip(rstrip(start));
130
131 if (*start == ';' || *start == '#') {
132 /*
133 * Per Python ConfigParser, allow '#' comments at
134 * start of line.
135 */
136 }
137 #if INI_ALLOW_MULTILINE
138 else if (*prev_name && *start && start > line) {
139 /* Non-black line with leading whitespace, treat as
140 * continuation of previous name's value
141 * (as per Python ConfigParser).
142 */
143 if (handler(user, section, prev_name, start) < 0 &&
144 !error) {
145 error = lineno;
146 }
147 }
148 #endif
149 else if (*start == '[') {
150 /* A "[section]" line */
151 end = find_char_or_comment(start + 1, ']');
152 if (*end == ']') {
153 *end = '\0';
154 strncpy0(section, start + 1, sizeof(section));
155 *prev_name = '\0';
156 }
157 else if (!error) {
158 /* No ']' found on section line */
159 error = lineno;
160 }
161 }
162 else if (*start && *start != ';') {
163 /* Not a comment, must be a name[=:]value pair */
164 end = find_char_or_comment(start, '=');
165 if (*end != '=') {
166 end = find_char_or_comment(start, ':');
167 }
168 if (*end == '=' || *end == ':') {
169 *end = '\0';
170 name = rstrip(start);
171 value = lskip(end + 1);
172 end = find_char_or_comment(value, '\0');
173 if (*end == ';') {
174 *end = '\0';
175 }
176
177 rstrip(value);
178
179 /*
180 * Valid name[=:]value pair found, call
181 * handler
182 */
183 strncpy0(prev_name, name, sizeof(prev_name));
184 if (handler(user, section, name, value) < 0 &&
185 !error) {
186 error = lineno;
187 }
188 }
189 else if (!error) {
190 /* No '=' or ':' found on name[=:]value line */
191 error = lineno;
192 }
193 }
194 }
195
196 #if !INI_USE_STACK
197 free(line);
198 #endif
199
200 return error;
201 }
202
203 /* See documentation in header file. */
204 LTTNG_HIDDEN
205 int ini_parse(const char* filename, ini_entry_handler handler, void* user)
206 {
207 FILE* file;
208 int error;
209
210 file = fopen(filename, "r");
211 if (!file) {
212 return -1;
213 }
214
215 error = ini_parse_file(file, handler, user);
216 fclose(file);
217 return error;
218 }
This page took 0.050228 seconds and 4 git commands to generate.