Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ instead of jq's .object.object[arrayindex]|command
Although uclcmd commands are not actually 'piped' and only the 'each' command
can be stacked. Running other commands just runs them sequentially

uclcmd currently targets the master branch of [vstakhov/libucl](https://github.com/vstakhov/libucl), as some used features are not available in an existing release.
2 changes: 1 addition & 1 deletion uclcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ char *include_file = NULL;
int
main(int argc, char *argv[])
{
atexit(cleanup);
int ret = 0, i = 0;
bool verbfound = false;
verbmap_t cmdmap[] =
Expand Down Expand Up @@ -163,4 +164,3 @@ cleanup()
ucl_object_unref(set_obj);
}
}

2 changes: 1 addition & 1 deletion uclcmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ int get_cmd_type(const ucl_object_t *obj, char *nodepath,
const char *command_str, char *remaining_commands, int recurse);
int get_cmd_values(const ucl_object_t *obj, char *nodepath,
const char *command_str, char *remaining_commands, int recurse);

void asprintf_check_enomem(int retcode);

#endif /* UCLCMD_H_ */
35 changes: 26 additions & 9 deletions uclcmd_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,25 @@
* $FreeBSD$
*/

#include <errno.h>

#include "uclcmd.h"

void
asprintf_check_enomem(int retcode) {
/*
* This method is called with the return code of asprintf() as parameter.
* asprintf returns -1 when it cannot allocate memory.
* See printf(3) for details.
*/
if (retcode != -1)
return;
errno = ENOMEM;
fprintf(stderr, "ENOMEM(%d): Could not allocate memory.\n", ENOMEM);
cleanup();
abort();
}

char*
expand_subkeys(const ucl_object_t *obj, char *nodepath)
{
Expand Down Expand Up @@ -170,31 +187,31 @@ type_as_string (const ucl_object_t *obj)
if (obj == NULL) {
return NULL;
} else if (ucl_object_type(obj) == UCL_OBJECT) {
asprintf(&ret, "UCL_OBJECT");
asprintf_check_enomem(asprintf(&ret, "UCL_OBJECT"));
}
else if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&ret, "UCL_ARRAY");
asprintf_check_enomem(asprintf(&ret, "UCL_ARRAY"));
}
else if (ucl_object_type(obj) == UCL_INT) {
asprintf(&ret, "UCL_INT");
asprintf_check_enomem(asprintf(&ret, "UCL_INT"));
}
else if (ucl_object_type(obj) == UCL_FLOAT) {
asprintf(&ret, "UCL_FLOAT");
asprintf_check_enomem(asprintf(&ret, "UCL_FLOAT"));
}
else if (ucl_object_type(obj) == UCL_STRING) {
asprintf(&ret, "UCL_STRING");
asprintf_check_enomem(asprintf(&ret, "UCL_STRING"));
}
else if (ucl_object_type(obj) == UCL_BOOLEAN) {
asprintf(&ret, "UCL_BOOLEAN");
asprintf_check_enomem(asprintf(&ret, "UCL_BOOLEAN"));
}
else if (ucl_object_type(obj) == UCL_TIME) {
asprintf(&ret, "UCL_TIME");
asprintf_check_enomem(asprintf(&ret, "UCL_TIME"));
}
else if (ucl_object_type(obj) == UCL_USERDATA) {
asprintf(&ret, "UCL_USERDATA");
asprintf_check_enomem(asprintf(&ret, "UCL_USERDATA"));
}
else if (ucl_object_type(obj) == UCL_NULL) {
asprintf(&ret, "UCL_NULL");
asprintf_check_enomem(asprintf(&ret, "UCL_NULL"));
}

return ret;
Expand Down
45 changes: 21 additions & 24 deletions uclcmd_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ get_main(int argc, char *argv[])
get_mode(argv[k]);
}

cleanup();

if (nonewline) {
printf("\n");
}
Expand All @@ -152,10 +150,9 @@ get_mode(char *requested_node)
char *cmd = requested_node;
char *node_name = strsep(&cmd, "|");
char *command_str = strsep(&cmd, "|");
char *nodepath = NULL;
char *nodepath = "";
int command_count = 0, i;

asprintf(&nodepath, "");
found_object = root_obj;

if (strlen(node_name) == 0) {
Expand All @@ -180,7 +177,7 @@ get_mode(char *requested_node)
}
found_object = ucl_lookup_path_char(found_object, node_name, input_sepchar);
free(nodepath);
asprintf(&nodepath, "%s", node_name);
asprintf_check_enomem(asprintf(&nodepath, "%s", node_name));
}

while (command_str != NULL) {
Expand Down Expand Up @@ -403,12 +400,12 @@ get_cmd_values(const ucl_object_t *obj, char *nodepath,
continue;
}
if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&newkey, "%c%i", output_sepchar, arrindex);
asprintf_check_enomem(asprintf(&newkey, "%c%i", output_sepchar, arrindex));
arrindex++;
} else {
newkey = __DECONST(char *, ucl_object_key(cur));
if (newkey != NULL) {
asprintf(&newkey, "%c%s", output_sepchar, ucl_object_key(cur));
asprintf_check_enomem(asprintf(&newkey, "%c%s", output_sepchar, ucl_object_key(cur)));
}
}
output_key(cur, nodepath, newkey);
Expand Down Expand Up @@ -482,7 +479,7 @@ get_cmd_recurse(const ucl_object_t *obj, char *nodepath,
ucl_object_t *arrlen = NULL;

arrlen = ucl_object_fromint(obj->len);
asprintf(&tmpkeyname, "%c%s", output_sepchar, "_length");
asprintf_check_enomem(asprintf(&tmpkeyname, "%c%s", output_sepchar, "_length"));
output_chunk(arrlen, nodepath, tmpkeyname);
free(tmpkeyname);
}
Expand All @@ -494,7 +491,7 @@ get_cmd_recurse(const ucl_object_t *obj, char *nodepath,
keylist = expand_subkeys(obj, nodepath);
if (keylist != NULL) {
keystr = ucl_object_fromstring(keylist);
asprintf(&tmpkeyname, "%c%s", output_sepchar, "_keys");
asprintf_check_enomem(asprintf(&tmpkeyname, "%c%s", output_sepchar, "_keys"));
output_chunk(keystr, nodepath, tmpkeyname);
free(tmpkeyname);
free(keylist);
Expand All @@ -505,24 +502,24 @@ get_cmd_recurse(const ucl_object_t *obj, char *nodepath,
char *newkey = NULL;
char *newnodepath = NULL;
if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&newkey, "%c%i", output_sepchar, arrindex);
asprintf_check_enomem(asprintf(&newkey, "%c%i", output_sepchar, arrindex));
arrindex++;
} else if (strlen(nodepath) == 0) {
asprintf(&newkey, "%s", ucl_object_key(cur));
} else {
asprintf(&newkey, "%c%s", output_sepchar, ucl_object_key(cur));
asprintf_check_enomem(asprintf(&newkey, "%c%s", output_sepchar, ucl_object_key(cur)));
}
if (ucl_object_type(cur) == UCL_OBJECT ||
ucl_object_type(cur) == UCL_ARRAY) {
it2 = NULL;
while ((cur2 = ucl_iterate_object(cur, &it2, false))) {
if (nodepath != NULL && strlen(nodepath) > 0) {
asprintf(&newnodepath, "%s%s", nodepath, newkey);
asprintf_check_enomem(asprintf(&newnodepath, "%s%s", nodepath, newkey));
} else {
if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&newnodepath, "%i", arrindex);
asprintf_check_enomem(asprintf(&newnodepath, "%i", arrindex));
} else {
asprintf(&newnodepath, "%s", ucl_object_key(cur2));
asprintf_check_enomem(asprintf(&newnodepath, "%s", ucl_object_key(cur2)));
}
}
recurse_level = process_get_command(cur2,
Expand Down Expand Up @@ -560,10 +557,10 @@ get_cmd_each(const ucl_object_t *obj, char *nodepath,
while ((cur = ucl_iterate_object(obj, &it, true))) {
char *newkey = NULL;
if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&newkey, "%c%i", output_sepchar, arrindex);
asprintf_check_enomem(asprintf(&newkey, "%c%i", output_sepchar, arrindex));
arrindex++;
} else {
asprintf(&newkey, "%c%s", output_sepchar, ucl_object_key(cur));
asprintf_check_enomem(asprintf(&newkey, "%c%s", output_sepchar, ucl_object_key(cur)));
}
if (cur->next != 0 && cur->type != UCL_ARRAY) {
/* Implicit array */
Expand All @@ -586,12 +583,12 @@ get_cmd_each(const ucl_object_t *obj, char *nodepath,
while ((cur = ucl_iterate_object(obj, &it, true))) {
char *newnodepath = NULL;
if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&newnodepath, "%s%c%i", nodepath, output_sepchar,
arrindex);
asprintf_check_enomem(asprintf(&newnodepath, "%s%c%i", nodepath, output_sepchar,
arrindex));
arrindex++;
} else {
asprintf(&newnodepath, "%s%c%s", nodepath, output_sepchar,
ucl_object_key(cur));
asprintf_check_enomem(asprintf(&newnodepath, "%s%c%s", nodepath, output_sepchar,
ucl_object_key(cur)));
}
if (cur->next != 0 && cur->type != UCL_ARRAY) {
/* Implicit array */
Expand Down Expand Up @@ -648,12 +645,12 @@ get_cmd_none(const ucl_object_t *obj, char *nodepath,
if (next_command != NULL) {
char *newnodepath = NULL;
if (ucl_object_type(obj) == UCL_ARRAY) {
asprintf(&newnodepath, "%s%c%i", nodepath, output_sepchar,
arrindex);
asprintf_check_enomem(asprintf(&newnodepath, "%s%c%i", nodepath, output_sepchar,
arrindex));
arrindex++;
} else {
asprintf(&newnodepath, "%s%c%s", nodepath, output_sepchar,
ucl_object_key(cur));
asprintf_check_enomem(asprintf(&newnodepath, "%s%c%s", nodepath, output_sepchar,
ucl_object_key(cur)));
}
if (debug > 2) {
fprintf(stderr, "DEBUG: Calling recurse with %s.%s on %s\n",
Expand Down
2 changes: 0 additions & 2 deletions uclcmd_merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ merge_main(int argc, char *argv[])
ret = 1;
}

cleanup();

if (nonewline) {
printf("\n");
}
Expand Down
4 changes: 1 addition & 3 deletions uclcmd_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ output_main(int argc, char *argv[])

ucl_obj_dump(root_obj, 0);

cleanup();

if (nonewline) {
printf("\n");
}
Expand Down Expand Up @@ -173,7 +171,7 @@ output_key(const ucl_object_t *obj, char *nodepath, const char *inkey)
char *key;

if (inkey == NULL) {
asprintf(&key, "");
key = "";
} else {
key = strdup(inkey);
}
Expand Down
6 changes: 1 addition & 5 deletions uclcmd_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ parse_file(struct ucl_parser *parser, const char *filename)
if (ucl_parser_get_error(parser)) {
fprintf(stderr, "Error occured: %s\n",
ucl_parser_get_error(parser));
cleanup();
exit(2);
}

obj = ucl_parser_get_object(parser);
if (ucl_parser_get_error(parser)) {
fprintf(stderr, "Error: Parse Error occured: %s\n",
ucl_parser_get_error(parser));
cleanup();
exit(3);
}

Expand All @@ -72,7 +70,7 @@ parse_input(struct ucl_parser *parser, FILE *source)
/* There must be a better way to detect a string */
ucl_parser_clear_error(parser);
success = true;
asprintf(&data, "%s", inbuf);
asprintf_check_enomem(asprintf(&data, "%s", inbuf));
obj = ucl_object_fromstring_common(data, 0, UCL_STRING_PARSE);
} else {
obj = ucl_parser_get_object(parser);
Expand All @@ -81,7 +79,6 @@ parse_input(struct ucl_parser *parser, FILE *source)
if (ucl_parser_get_error(parser)) {
fprintf(stderr, "Error: Parse Error occured: %s\n",
ucl_parser_get_error(parser));
cleanup();
exit(3);
}

Expand All @@ -107,7 +104,6 @@ parse_string(struct ucl_parser *parser, char *data)
if (ucl_parser_get_error(parser)) {
fprintf(stderr, "Error: Parse Error occured: %s\n",
ucl_parser_get_error(parser));
cleanup();
exit(3);
}

Expand Down
2 changes: 0 additions & 2 deletions uclcmd_remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ remove_main(int argc, char *argv[])
}
get_mode("");

cleanup();

if (nonewline) {
printf("\n");
}
Expand Down
2 changes: 0 additions & 2 deletions uclcmd_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ set_main(int argc, char *argv[])
ret = 1;
}

cleanup();

if (nonewline) {
printf("\n");
}
Expand Down