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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
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 <platform.h>
#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)
{
assert(writer != NULL);
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)
{
assert(writer != NULL);
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_)
{
assert(writer != NULL);
/* 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)
{
assert(writer != NULL);
return vfprintf(writer->file, fmt, ap);
}
/*********************************************************************/
static size_t FileWriterWriteLen(Writer *writer, const char *str, size_t len_)
{
assert(writer != NULL);
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)
{
assert(writer != NULL);
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)
{
assert(writer != NULL);
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)
{
assert(writer != NULL);
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)
{
assert(writer != NULL);
if (writer->type != WT_STRING)
{
ProgrammingError("Wrong writer type");
}
return writer->string.len;
}
/*********************************************************************/
const char *StringWriterData(const Writer *writer)
{
assert(writer != NULL);
if (writer->type != WT_STRING)
{
ProgrammingError("Wrong writer type");
}
return writer->string.data;
}
/*********************************************************************/
void WriterClose(Writer *writer)
{
assert(writer != NULL);
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
{
assert(writer != NULL);
if (writer->type != WT_STRING)
{
ProgrammingError("Wrong writer type");
}
char *data = writer->string.data;
free(writer);
return data;
}
FILE *FileWriterDetach(Writer *writer)
{
assert(writer != NULL);
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 && options[i].val > 0)
{
// 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 Component *component,
const struct option options[], const char *const hints[],
const Description *commands, bool command_first,
bool accepts_file_argument)
{
assert (component != NULL);
assert (component->name != NULL);
WriterWriteF(w, "Usage: %s%s [OPTIONS]%s%s\n", component->name,
(commands && command_first) ? " COMMAND" : "",
(commands && !command_first) ? " COMMAND" : "",
accepts_file_argument ? " [FILE]" : "");
if ((commands != NULL) && command_first)
{
WriterWriteCommands(w, commands);
}
WriterWriteOptions(w, options, hints);
if ((commands != NULL) && !command_first)
{
WriterWriteCommands(w, commands);
}
if (component->website != NULL) {
WriterWriteF(w, "\nWebsite: %s\n", component->website);
}
if (component->copyright != NULL) {
WriterWriteF(w, "This software is Copyright %s.\n", component->copyright);
}
}
|