OpenDNSSEC-signer 2.1.12
adutil.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009-2011 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
32#include "config.h"
33#include "adapter/adutil.h"
34#include "file.h"
35#include "log.h"
36
37#include <ldns/ldns.h>
38
39static const char* adapter_str = "adapter";
40
41
46int
47adutil_readline_frm_file(FILE* fd, char* line, unsigned int* l,
48 int keep_comments)
49{
50 int i = 0;
51 int li = 0;
52 int in_string = 0;
53 int depth = 0;
54 int comments = 0;
55 int c = 0;
56 int lc = 0;
57
58 for (i = 0; i < SE_ADFILE_MAXLINE; i++) {
59 c = ods_fgetc(fd, l);
60 if (comments) {
61 while (c != EOF && (char)c != '\n') {
62 c = ods_fgetc(fd, l);
63 }
64 }
65
66 if (c == EOF) {
67 if (depth != 0) {
68 ods_log_error("[%s] read line: bracket mismatch discovered at "
69 "line %i, missing ')'", adapter_str, l&&*l?*l:0);
70 }
71 if (li > 0) {
72 line[li] = '\0';
73 return li;
74 } else {
75 return -1;
76 }
77 } else if ((char)c == '"' && (char)lc != '\\') {
78 in_string = 1 - in_string; /* swap status */
79 line[li] = (char)c;
80 li++;
81 } else if ((char)c == '(') {
82 if (in_string) {
83 line[li] = (char)c;
84 li++;
85 } else if ((char)lc != '\\') {
86 depth++;
87 line[li] = ' ';
88 li++;
89 } else {
90 line[li] = (char)c;
91 li++;
92 }
93 } else if ((char)c == ')') {
94 if (in_string) {
95 line[li] = (char)c;
96 li++;
97 } else if ((char)lc != '\\') {
98 if (depth < 1) {
99 ods_log_error("[%s] read line: bracket mismatch "
100 "discovered at line %i, missing '('", adapter_str,
101 l&&*l?*l:0);
102 line[li] = '\0';
103 return li;
104 }
105 depth--;
106 line[li] = ' ';
107 li++;
108 } else {
109 line[li] = (char)c;
110 li++;
111 }
112 } else if ((char)c == ';') {
113 if (in_string) {
114 line[li] = (char)c;
115 li++;
116 } else if ((char)lc != '\\' && !keep_comments) {
117 comments = 1;
118 } else {
119 line[li] = (char)c;
120 li++;
121 }
122 } else if ((char)c == '\n' && (char)lc != '\\') {
123 comments = 0;
124 /* if no depth issue, we are done */
125 if (depth == 0) {
126 break;
127 }
128 line[li] = ' ';
129 li++;
130 } else {
131 line[li] = (char)c;
132 li++;
133 }
134 /* continue with line */
135 lc = c;
136 }
137
138 /* done */
139 if (depth != 0) {
140 ods_log_error("[%s] read line: bracket mismatch discovered at line %i,"
141 " missing ')'", adapter_str, l&&*l?*l:0);
142 return li;
143 }
144 line[li] = '\0';
145 return li;
146}
147
148
149/*
150 * Trim trailing whitespace.
151 *
152 */
153void
154adutil_rtrim_line(char* line, int* line_len)
155{
156 int i = strlen(line), nl = 0;
157 int trimmed = 0;
158
159 while (i>0) {
160 --i;
161 if (line[i] == '\n') {
162 nl = 1;
163 }
164 if (line[i] == ' ' || line[i] == '\t' || line[i] == '\n') {
165 line[i] = '\0';
166 trimmed++;
167 } else {
168 break;
169 }
170 }
171 if (nl) {
172 line[++i] = '\n';
173 }
174 *line_len -= trimmed;
175}
176
177
182int
183adutil_whitespace_line(char* line, int line_len)
184{
185 int i;
186 for (i = 0; i < line_len; i++) {
187 if (!isspace((int)line[i])) {
188 return 0;
189 }
190 }
191 return 1;
192}
void adutil_rtrim_line(char *line, int *line_len)
Definition: adutil.c:154
int adutil_readline_frm_file(FILE *fd, char *line, unsigned int *l, int keep_comments)
Definition: adutil.c:47
int adutil_whitespace_line(char *line, int line_len)
Definition: adutil.c:183
#define SE_ADFILE_MAXLINE
Definition: adutil.h:40