xref: /freebsd/contrib/wpa/src/utils/config.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*
2  * Configuration parsing
3  * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "utils/config.h"
12 #include "common.h"
13 
14 
15 static int newline_terminated(const char *buf, size_t buflen)
16 {
17 	size_t len = os_strlen(buf);
18 	if (len == 0)
19 		return 0;
20 	if (len == buflen - 1 && buf[buflen - 1] != '\r' &&
21 	    buf[len - 1] != '\n')
22 		return 0;
23 	return 1;
24 }
25 
26 
27 static void skip_line_end(FILE *stream)
28 {
29 	char buf[100];
30 	while (fgets(buf, sizeof(buf), stream)) {
31 		buf[sizeof(buf) - 1] = '\0';
32 		if (newline_terminated(buf, sizeof(buf)))
33 			return;
34 	}
35 }
36 
37 
38 char * wpa_config_get_line(char *s, int size, FILE *stream, int *line,
39 			   char **_pos)
40 {
41 	char *pos, *end, *sstart;
42 
43 	while (fgets(s, size, stream)) {
44 		(*line)++;
45 		s[size - 1] = '\0';
46 		if (!newline_terminated(s, size)) {
47 			/*
48 			 * The line was truncated - skip rest of it to avoid
49 			 * confusing error messages.
50 			 */
51 			wpa_printf(MSG_INFO, "Long line in configuration file "
52 				   "truncated");
53 			skip_line_end(stream);
54 		}
55 		pos = s;
56 
57 		/* Skip white space from the beginning of line. */
58 		while (*pos == ' ' || *pos == '\t' || *pos == '\r')
59 			pos++;
60 
61 		/* Skip comment lines and empty lines */
62 		if (*pos == '#' || *pos == '\n' || *pos == '\0')
63 			continue;
64 
65 		/*
66 		 * Remove # comments unless they are within a double quoted
67 		 * string.
68 		 */
69 		sstart = os_strchr(pos, '"');
70 		if (sstart)
71 			sstart = os_strrchr(sstart + 1, '"');
72 		if (!sstart)
73 			sstart = pos;
74 		end = os_strchr(sstart, '#');
75 		if (end)
76 			*end-- = '\0';
77 		else
78 			end = pos + os_strlen(pos) - 1;
79 
80 		/* Remove trailing white space. */
81 		while (end > pos &&
82 		       (*end == '\n' || *end == ' ' || *end == '\t' ||
83 			*end == '\r'))
84 			*end-- = '\0';
85 
86 		if (*pos == '\0')
87 			continue;
88 
89 		if (_pos)
90 			*_pos = pos;
91 		return pos;
92 	}
93 
94 	if (_pos)
95 		*_pos = NULL;
96 	return NULL;
97 }
98