-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathwriter.c
331 lines (272 loc) · 8.3 KB
/
writer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <writer.h>
#include <misc_lib.h>
#include <alloc.h>
typedef enum
{
WT_STRING,
WT_FILE,
} WriterType;
typedef struct
{
char *data;
size_t len; /* Does not include trailing zero */
size_t allocated; /* Includes trailing zero */
} StringWriterImpl;
struct Writer_
{
WriterType type;
union
{
StringWriterImpl string;
FILE *file;
};
};
/*********************************************************************/
Writer *FileWriter(FILE *file)
{
Writer *writer = xcalloc(1, sizeof(Writer));
writer->type = WT_FILE;
writer->file = file;
return writer;
}
/*********************************************************************/
Writer *StringWriter(void)
{
Writer *writer = xcalloc(1, sizeof(Writer));
writer->type = WT_STRING;
writer->string.data = xstrdup("");
writer->string.allocated = 1;
writer->string.len = 0;
return writer;
}
/*********************************************************************/
static void StringWriterReallocate(Writer *writer, size_t extra_length)
{
writer->string.allocated = MAX(writer->string.allocated * 2, writer->string.len + extra_length + 1);
writer->string.data = xrealloc(writer->string.data, writer->string.allocated);
}
static size_t StringWriterWriteChar(Writer *writer, char c)
{
if (writer->string.len + 2 > writer->string.allocated)
{
StringWriterReallocate(writer, 2);
}
writer->string.data[writer->string.len] = c;
writer->string.data[writer->string.len + 1] = '\0';
writer->string.len++;
return 1;
}
static size_t StringWriterWriteLen(Writer *writer, const char *str, size_t len_)
{
/* NB: str[:len_] may come from read(), which hasn't '\0'-terminated */
size_t len = strnlen(str, len_);
if (writer->string.len + len + 1 > writer->string.allocated)
{
StringWriterReallocate(writer, len);
}
memcpy(writer->string.data + writer->string.len, str, len);
writer->string.data[writer->string.len + len] = '\0';
writer->string.len += len;
return len;
}
/*********************************************************************/
static size_t FileWriterWriteF(Writer *writer, const char *fmt, va_list ap)
{
return vfprintf(writer->file, fmt, ap);
}
/*********************************************************************/
static size_t FileWriterWriteLen(Writer *writer, const char *str, size_t len_)
{
size_t len = strnlen(str, len_);
#ifdef CFENGINE_TEST
return CFENGINE_TEST_fwrite(str, 1, len, writer->file);
#else
return fwrite(str, 1, len, writer->file);
#endif
}
/*********************************************************************/
size_t WriterWriteF(Writer *writer, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
size_t size = WriterWriteVF(writer, fmt, ap);
va_end(ap);
return size;
}
/*********************************************************************/
size_t WriterWriteVF(Writer *writer, const char *fmt, va_list ap)
{
if (writer->type == WT_STRING)
{
char *str = NULL;
xvasprintf(&str, fmt, ap);
size_t size = StringWriterWriteLen(writer, str, INT_MAX);
free(str);
return size;
}
else
{
return FileWriterWriteF(writer, fmt, ap);
}
}
/*********************************************************************/
size_t WriterWriteLen(Writer *writer, const char *str, size_t len)
{
if (writer->type == WT_STRING)
{
return StringWriterWriteLen(writer, str, len);
}
else
{
return FileWriterWriteLen(writer, str, len);
}
}
/*********************************************************************/
size_t WriterWrite(Writer *writer, const char *str)
{
return WriterWriteLen(writer, str, INT_MAX);
}
/*********************************************************************/
size_t WriterWriteChar(Writer *writer, char c)
{
if (writer->type == WT_STRING)
{
return StringWriterWriteChar(writer, c);
}
else
{
char s[2] = { c, '\0' };
return FileWriterWriteLen(writer, s, 1);
}
}
/*********************************************************************/
size_t StringWriterLength(const Writer *writer)
{
if (writer->type != WT_STRING)
{
ProgrammingError("Wrong writer type");
}
return writer->string.len;
}
/*********************************************************************/
const char *StringWriterData(const Writer *writer)
{
if (writer->type != WT_STRING)
{
ProgrammingError("Wrong writer type");
}
return writer->string.data;
}
/*********************************************************************/
void WriterClose(Writer *writer)
{
if (writer->type == WT_STRING)
{
free(writer->string.data);
}
else
{
#ifdef CFENGINE_TEST
CFENGINE_TEST_fclose(writer->file);
#else
fclose(writer->file);
#endif
}
free(writer);
}
/*********************************************************************/
char *StringWriterClose(Writer *writer)
// NOTE: transfer of ownership for allocated return value
{
if (writer->type != WT_STRING)
{
ProgrammingError("Wrong writer type");
}
char *data = writer->string.data;
free(writer);
return data;
}
FILE *FileWriterDetach(Writer *writer)
{
if (writer->type != WT_FILE)
{
ProgrammingError("Wrong writer type");
}
FILE *file = writer->file;
free(writer);
return file;
}
static void WriterWriteOptions(Writer *w, const struct option options[],
const char *const hints[])
{
WriterWriteF(w, "\nOptions:\n");
for (int i = 0; options[i].name != NULL; i++)
{
char short_option[] = ", -*";
if (options[i].val < 128)
{
// Within ASCII range, means there is a short option.
short_option[3] = options[i].val;
}
else
{
// No short option.
short_option[0] = '\0';
}
if (options[i].has_arg)
{
WriterWriteF(w, " --%-12s%s value - %s\n", options[i].name,
short_option, hints[i]);
}
else
{
WriterWriteF(w, " --%-12s%-10s - %s\n", options[i].name,
short_option, hints[i]);
}
}
}
static void WriterWriteCommands(Writer *w, const Description *commands)
{
assert(commands != NULL);
WriterWriteF(w, "\nCommands:\n");
for (int i = 0; commands[i].name != NULL; i++)
{
WriterWriteF(w, " %-12s - %s.\n", commands[i].name,
commands[i].description);
WriterWriteF(w, " %-12s Usage: %s\n", "", commands[i].usage);
}
}
void WriterWriteHelp(Writer *w, const char *component,
const struct option options[], const char *const hints[],
bool accepts_file_argument, const Description *commands)
{
WriterWriteF(w, "Usage: %s [OPTIONS] %s%s\n", component,
commands ? "command " : "",
accepts_file_argument ? " [FILE]" : "");
WriterWriteOptions(w, options, hints);
if (commands != NULL)
{
WriterWriteCommands(w, commands);
}
WriterWriteF(w, "\nWebsite: http://www.cfengine.com\n");
WriterWriteF(w, "This software is Copyright (C) "
"2008,2010-present CFEngine AS.\n");
}