-
Notifications
You must be signed in to change notification settings - Fork 11
/
uclcmd.c
192 lines (179 loc) · 5.64 KB
/
uclcmd.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
/*-
* Copyright (c) 2014-2015 Allan Jude <allanjude@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include "uclcmd.h"
/*
* XXX: TODO:
* Error checking on strtoul etc
* Does ucl_object_insert_key_common need to respect NO_IMPLICIT_ARRAY
*/
int debug = 0, expand = 0, mode = 0, noop = 0, nonewline = 0;
int show_keys = 0, show_raw = 0;
int pflags = 0;
bool firstline = true, shvars = false;
int output_type = 254;
ucl_object_t *root_obj = NULL;
ucl_object_t *set_obj = NULL;
struct ucl_parser *parser = NULL;
struct ucl_parser *setparser = NULL;
char input_sepchar = '.';
char output_sepchar = '.';
const char *filename = NULL;
const char *outfile = NULL;
FILE *output = NULL;
char *include_file = NULL;
/*
* This application provides a shell scripting friendly interface for reading
* and writing UCL config files, using libUCL.
*
* http://xkcd.com/1513/
*
*/
int
main(int argc, char *argv[])
{
int ret = 0, i = 0;
bool verbfound = false;
verbmap_t cmdmap[] =
{
{ "get", get_main },
{ "set", set_main },
{ "merge", merge_main },
{ "remove", remove_main },
{ "del", remove_main },
{ "dump", output_main },
{ "help", (verb_func_t) usage },
{ "version", (verb_func_t) version },
{ NULL, NULL }
};
if (argc < 2) {
usage();
}
output = stdout;
for (i = 0; cmdmap[i].verb; i++) {
if (strcasecmp(cmdmap[i].verb, argv[1]) != 0)
continue;
verbfound = true;
/* Remove the verb */
argv[1] = argv[0];
argc--;
argv++;
/*
for (ret = 0; ret < argc; ret++) {
printf("argv[%d] = %s\n", ret, argv[ret]);
}
*/
if (cmdmap[i].callback != NULL) {
ret = cmdmap[i].callback(argc, argv);
}
break;
}
if (!verbfound) {
/* unknown verb */
usage();
}
return(ret);
}
void
usage()
{
fprintf(stderr, "%s\n",
"Usage: uclcmd get [-cdeIjklmNquy] [-D char] [-f file] [-o file] variable\n"
" uclcmd set [-cdIjmnuy] [-t type] [-D char] [-f file] [-i file] [-o file] variable [UCL]\n"
" uclcmd merge [-cdIjmnuy] [-D char] [-f file] [-i file] [-o file] variable\n"
" uclcmd remove [-cdIjmnuy] [-D char] [-f file] [-o file] variable\n"
"\n"
"COMMON OPTIONS:\n"
" -c --cjson output compacted JSON\n"
" -d --debug enable verbose debugging output\n"
" -D --delimiter character to use as element delimiter (default is .)\n"
" -e --expand Output the list of keys when encountering an object\n"
" -f --file path to a file to read or write\n"
" -I --foldcase fold all keys to lowercase (make matching insensitive)\n"
" -j --json output pretty JSON\n"
" -k --keys show key=value rather than just the value\n"
" -l --shellvars keys are output with underscores as delimiter\n"
" -m --msgpack output MSGPACK\n"
" -n --noop do not save changes to file, only output to STDOUT\n"
" -N --nonewline separate output with spaces rather than newlines\n"
" -o --output file to write output to, defaults to STDOUT\n"
" -q --noquotes do not enclose strings in quotes\n"
" -t --type make the new element this type\n"
" -u --ucl output universal config language\n"
" -y --yaml output YAML\n"
" variable The key of the variable to read, in object notation\n"
" UCL A block of UCL to be written to the specified variable\n"
"\n"
"GET OPTIONS:\n"
"\n"
"SET OPTIONS:\n"
" -i --input use indicated file as additional input (for combining)\n"
"\n"
"MERGE OPTIONS:\n"
" -i --input use indicated file as additional input (for merging)\n"
"\n"
"REMOVE OPTIONS:\n"
"\n"
"EXAMPLES:\n"
" uclcmd get --file vmconfig .name\n"
" \"value\"\n"
"\n"
" uclcmd get --file vmconfig --keys --noquotes array.1.name\n"
" array.1.name=value\n"
"\n"
" uclcmd get --file vmconfig --keys --shellvars array.1.name\n"
" array_1_name=\"value\"\n"
"\n");
exit(1);
}
void
version()
{
fprintf(stderr, "uclcmd version %s\n", UCLCMD_VERSION_STRING);
}
void
cleanup()
{
if (parser != NULL) {
ucl_parser_free(parser);
}
if (setparser != NULL) {
ucl_parser_free(setparser);
}
if (root_obj != NULL) {
ucl_object_unref(root_obj);
}
if (set_obj != NULL) {
ucl_object_unref(set_obj);
}
if (nonewline) {
fprintf(output, "\n");
}
if (output != stdout) {
output_close(output);
}
}