Path: blob/main/contrib/libucl/tests/test_basic.c
102597 views
/* Copyright (c) 2013, Vsevolod Stakhov1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10*11* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY12* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED13* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE14* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY15* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES16* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;17* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND18* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT19* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS20* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.21*/2223#include "ucl.h"24#include "ucl_internal.h"25#include <sys/types.h>26#include <fcntl.h>27#include <unistd.h>282930int31main (int argc, char **argv)32{33char *inbuf = NULL;34struct ucl_parser *parser = NULL, *parser2 = NULL;35ucl_object_t *obj, *comments = NULL;36ssize_t bufsize, r;37FILE *in, *out;38unsigned char *emitted = NULL;39const char *fname_in = NULL, *fname_out = NULL;40int ret = 0, opt, json = 0, compact = 0, yaml = 0,41save_comments = 0, skip_macro = 0,42flags, fd_out, fd_in, use_fd = 0, msgpack_input = 0;43struct ucl_emitter_functions *func;4445while ((opt = getopt(argc, argv, "fjcyCMm")) != -1) {46switch (opt) {47case 'j':48json = 1;49break;50case 'c':51compact = 1;52break;53case 'C':54save_comments = 1;55break;56case 'y':57yaml = 1;58break;59case 'M':60skip_macro = true;61break;62case 'm':63msgpack_input = 1;64break;65case 'f':66use_fd = true;67break;68default: /* '?' */69fprintf (stderr, "Usage: %s [-jcy] [-CM] [-f] [in] [out]\n",70argv[0]);71exit (EXIT_FAILURE);72}73}7475argc -= optind;76argv += optind;7778switch (argc) {79case 1:80fname_in = argv[0];81break;82case 2:83fname_in = argv[0];84fname_out = argv[1];85break;86}8788if (!use_fd) {89if (fname_in != NULL) {90in = fopen (fname_in, "r");91if (in == NULL) {92exit (-errno);93}94}95else {96in = stdin;97}98}99else {100if (fname_in != NULL) {101fd_in = open (fname_in, O_RDONLY);102if (fd_in == -1) {103exit (-errno);104}105}106else {107fd_in = STDIN_FILENO;108}109}110111flags = UCL_PARSER_KEY_LOWERCASE;112113if (save_comments) {114flags |= UCL_PARSER_SAVE_COMMENTS;115}116117if (skip_macro) {118flags |= UCL_PARSER_DISABLE_MACRO;119}120121parser = ucl_parser_new (flags);122ucl_parser_register_variable (parser, "ABI", "unknown");123124if (fname_in != NULL) {125ucl_parser_set_filevars (parser, fname_in, true);126}127128if (!use_fd) {129inbuf = malloc (BUFSIZ);130bufsize = BUFSIZ;131r = 0;132133while (!feof (in) && !ferror (in)) {134if (r == bufsize) {135inbuf = realloc (inbuf, bufsize * 2);136bufsize *= 2;137if (inbuf == NULL) {138perror ("realloc");139exit (EXIT_FAILURE);140}141}142r += fread (inbuf + r, 1, bufsize - r, in);143}144145if (ferror (in)) {146fprintf (stderr, "Failed to read the input file.\n");147exit (EXIT_FAILURE);148}149150ucl_parser_add_chunk_full (parser, (const unsigned char *)inbuf, r,1510, UCL_DUPLICATE_APPEND,152msgpack_input ? UCL_PARSE_MSGPACK : UCL_PARSE_UCL);153fclose (in);154}155else {156ucl_parser_add_fd (parser, fd_in);157close (fd_in);158}159160if (!use_fd) {161if (fname_out != NULL) {162out = fopen (fname_out, "w");163if (out == NULL) {164exit (-errno);165}166}167else {168out = stdout;169}170}171else {172if (fname_out != NULL) {173fd_out = open (fname_out, O_WRONLY | O_CREAT, 00644);174if (fd_out == -1) {175exit (-errno);176}177}178else {179fd_out = STDOUT_FILENO;180}181}182183184if (ucl_parser_get_error (parser) != NULL) {185fprintf (out, "Error occurred (phase 1): %s\n",186ucl_parser_get_error(parser));187ret = 1;188goto end;189}190191obj = ucl_parser_get_object (parser);192193if (save_comments) {194comments = ucl_object_ref (ucl_parser_get_comments (parser));195}196197if (json) {198if (compact) {199emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT);200}201else {202emitted = ucl_object_emit (obj, UCL_EMIT_JSON);203}204}205else if (yaml) {206emitted = ucl_object_emit (obj, UCL_EMIT_YAML);207}208else {209emitted = NULL;210func = ucl_object_emit_memory_funcs ((void **)&emitted);211212if (func != NULL) {213ucl_object_emit_full (obj, UCL_EMIT_CONFIG, func, comments);214ucl_object_emit_funcs_free (func);215}216}217218#if 0219fprintf (out, "%s\n****\n", emitted);220#endif221222ucl_parser_free (parser);223ucl_object_unref (obj);224parser2 = ucl_parser_new (flags);225ucl_parser_add_string (parser2, (const char *)emitted, 0);226227if (ucl_parser_get_error(parser2) != NULL) {228fprintf (out, "Error occurred (phase 2): %s\n",229ucl_parser_get_error(parser2));230fprintf (out, "%s\n", emitted);231ret = 1;232goto end;233}234235if (emitted != NULL) {236free (emitted);237}238if (comments) {239ucl_object_unref (comments);240comments = NULL;241}242243if (save_comments) {244comments = ucl_object_ref (ucl_parser_get_comments (parser2));245}246247obj = ucl_parser_get_object (parser2);248249if (!use_fd) {250func = ucl_object_emit_file_funcs (out);251}252else {253func = ucl_object_emit_fd_funcs (fd_out);254}255256if (func != NULL) {257if (json) {258if (compact) {259ucl_object_emit_full (obj, UCL_EMIT_JSON_COMPACT,260func, comments);261}262else {263ucl_object_emit_full (obj, UCL_EMIT_JSON,264func, comments);265}266}267else if (yaml) {268ucl_object_emit_full (obj, UCL_EMIT_YAML,269func, comments);270}271else {272ucl_object_emit_full (obj, UCL_EMIT_CONFIG,273func, comments);274}275276ucl_object_emit_funcs_free (func);277}278279if (!use_fd) {280fprintf (out, "\n");281fclose (out);282}283else {284write (fd_out, "\n", 1);285close (fd_out);286}287288ucl_object_unref (obj);289290end:291if (parser2 != NULL) {292ucl_parser_free (parser2);293}294if (comments) {295ucl_object_unref (comments);296}297if (inbuf != NULL) {298free (inbuf);299}300301return ret;302}303304305