test_basic.c revision 268896
1249269Sed/* Copyright (c) 2013, Vsevolod Stakhov 2249269Sed * All rights reserved. 3249269Sed * 4249269Sed * Redistribution and use in source and binary forms, with or without 5249269Sed * modification, are permitted provided that the following conditions are met: 6249269Sed * * Redistributions of source code must retain the above copyright 7249269Sed * notice, this list of conditions and the following disclaimer. 8249269Sed * * Redistributions in binary form must reproduce the above copyright 9249269Sed * notice, this list of conditions and the following disclaimer in the 10249269Sed * documentation and/or other materials provided with the distribution. 11249269Sed * 12249269Sed * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY 13249269Sed * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14249269Sed * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15249269Sed * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 16249269Sed * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17249269Sed * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18249269Sed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 19249269Sed * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20249269Sed * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21249269Sed * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22249269Sed */ 23249269Sed 24249269Sed#include "ucl.h" 25249269Sed#include "ucl_internal.h" 26249269Sed 27249269Sedint 28249269Sedmain (int argc, char **argv) 29249269Sed{ 30249269Sed char inbuf[8192], *test_in = NULL; 31249269Sed struct ucl_parser *parser = NULL, *parser2 = NULL; 32249269Sed ucl_object_t *obj; 33249269Sed FILE *in, *out; 34249269Sed unsigned char *emitted = NULL; 35249269Sed const char *fname_in = NULL, *fname_out = NULL; 36249269Sed int ret = 0, inlen, opt, json = 0; 37249269Sed 38249269Sed while ((opt = getopt(argc, argv, "j")) != -1) { 39249269Sed switch (opt) { 40249269Sed case 'j': 41249269Sed json = 1; 42249269Sed break; 43249269Sed default: /* '?' */ 44249269Sed fprintf (stderr, "Usage: %s [-j] [in] [out]\n", 45249269Sed argv[0]); 46249269Sed exit (EXIT_FAILURE); 47249269Sed } 48249269Sed } 49249269Sed 50249269Sed argc -= optind; 51249269Sed argv += optind; 52249269Sed 53249269Sed switch (argc) { 54249269Sed case 1: 55249269Sed fname_in = argv[0]; 56249269Sed break; 57249269Sed case 2: 58249269Sed fname_in = argv[0]; 59249269Sed fname_out = argv[1]; 60249269Sed break; 61249269Sed } 62249269Sed 63249269Sed if (fname_in != NULL) { 64249269Sed in = fopen (fname_in, "r"); 65249269Sed if (in == NULL) { 66249269Sed exit (-errno); 67249269Sed } 68249269Sed } 69249269Sed else { 70249269Sed in = stdin; 71249269Sed } 72249269Sed parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); 73249269Sed ucl_parser_register_variable (parser, "ABI", "unknown"); 74249269Sed 75249269Sed if (fname_in != NULL) { 76249269Sed ucl_parser_set_filevars (parser, fname_in, true); 77249269Sed } 78249269Sed 79249269Sed while (!feof (in)) { 80249269Sed memset (inbuf, 0, sizeof (inbuf)); 81249269Sed if (fread (inbuf, 1, sizeof (inbuf) - 1, in) == 0) { 82249269Sed break; 83249269Sed } 84249269Sed inlen = strlen (inbuf); 85249269Sed test_in = malloc (inlen); 86249269Sed memcpy (test_in, inbuf, inlen); 87249269Sed ucl_parser_add_chunk (parser, test_in, inlen); 88249269Sed } 89249269Sed fclose (in); 90249269Sed 91249269Sed if (fname_out != NULL) { 92249269Sed out = fopen (fname_out, "w"); 93249269Sed if (out == NULL) { 94249269Sed exit (-errno); 95249269Sed } 96249269Sed } 97249269Sed else { 98249269Sed out = stdout; 99249269Sed } 100249269Sed if (ucl_parser_get_error (parser) != NULL) { 101249269Sed fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser)); 102249269Sed ret = 1; 103249269Sed goto end; 104249269Sed } 105249269Sed obj = ucl_parser_get_object (parser); 106249269Sed if (json) { 107249269Sed emitted = ucl_object_emit (obj, UCL_EMIT_JSON); 108249269Sed } 109249269Sed else { 110249269Sed emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 111249269Sed } 112249269Sed ucl_parser_free (parser); 113249269Sed ucl_object_unref (obj); 114249269Sed parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); 115249269Sed ucl_parser_add_string (parser2, emitted, 0); 116249269Sed 117249269Sed if (ucl_parser_get_error(parser2) != NULL) { 118249269Sed fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2)); 119249269Sed fprintf (out, "%s\n", emitted); 120249269Sed ret = 1; 121249269Sed goto end; 122249269Sed } 123249269Sed if (emitted != NULL) { 124249269Sed free (emitted); 125249269Sed } 126249269Sed obj = ucl_parser_get_object (parser2); 127249269Sed if (json) { 128249269Sed emitted = ucl_object_emit (obj, UCL_EMIT_JSON); 129249269Sed } 130249269Sed else { 131249269Sed emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); 132249269Sed } 133249269Sed 134249269Sed fprintf (out, "%s\n", emitted); 135249269Sed ucl_object_unref (obj); 136249269Sed 137249269Sedend: 138249269Sed if (emitted != NULL) { 139249269Sed free (emitted); 140249269Sed } 141249269Sed if (parser2 != NULL) { 142249269Sed ucl_parser_free (parser2); 143249269Sed } 144249269Sed if (test_in != NULL) { 145249269Sed free (test_in); 146249269Sed } 147249269Sed 148249269Sed fclose (out); 149249269Sed 150249269Sed return ret; 151249269Sed} 152249269Sed