| 1 | /* parse-test.c - test program for simpleparse. |
| 2 | Copyright (C) 2008, 2009 g10 Code GmbH |
| 3 | |
| 4 | This file is part of Poldi. |
| 5 | |
| 6 | Poldi is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | Poldi is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | /* For Poldi I wrote a minimalistic library for parsing configuration |
| 21 | files and command-line arguments named "simpleparse". This is a |
| 22 | test program for simpleparse. -mo */ |
| 23 | |
| 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdarg.h> |
| 27 | |
| 28 | #include <gpg-error.h> |
| 29 | |
| 30 | #include <simpleparse.h>cxx . . $ `<file>/src/util/simpleparse.h`/ |
| 31 | #include <simplelog.h>cxx . . $ `<file>/src/util/simplelog.h`/ |
| 32 | |
| 33 | enum opt_idscxx . . $ opt_ids# |
| 34 | { |
| 35 | FOOcxx . . $ FOO. = 1, |
| 36 | BARcxx . . $ BAR., |
| 37 | BAZcxx . . $ BAZ. |
| 38 | }; |
| 39 | |
| 40 | static simpleparse_opt_spec_tcxx . . $ simpleparse_opt_spec_t# opt_specscxx . . $ opt_specs.[] = |
| 41 | { |
| 42 | { FOOcxx . . $ FOO., "foo", 'f', SIMPLEPARSE_ARG_REQUIREDcxx . . $ SIMPLEPARSE_ARG_REQUIRED., 0, "the foo switch requires an argument" }, |
| 43 | { BARcxx . . $ BAR., "bar", 'b', SIMPLEPARSE_ARG_OPTIONALcxx . . $ SIMPLEPARSE_ARG_OPTIONAL., 0, "the bar switch takes an optional argument" }, |
| 44 | { BAZcxx . . $ BAZ., "baz", 0, SIMPLEPARSE_ARG_NONEcxx . . $ SIMPLEPARSE_ARG_NONE., 0, "the baz switch takes no argument" }, |
| 45 | { 0 } |
| 46 | }; |
| 47 | |
| 48 | static gpg_error_tcxx . . $ gpg_error_t# |
| 49 | parsecbcxx . . $ parsecb(c7ea03b5806a970). (void *cookie, simpleparse_opt_spec_tcxx . . $ simpleparse_opt_spec_t# spec, const char *arg) |
| 50 | { |
| 51 | const char *prefix = cookie; |
| 52 | |
| 53 | printfcxx . . $ printf(581fddd72a8db8fc). ("[%s] opt: '%s', argument: '%s'\n", prefix, spec.long_optcxx . . $ $anonymous_type_9ecc77981daa53a4_0#long_opt., arg); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int |
| 59 | maincxx . . $ main(d8e43b79a186046c). (int argc, const char **argv) |
| 60 | { |
| 61 | simpleparse_handle_tcxx . . $ simpleparse_handle_t# handle = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 62 | gpg_error_tcxx . . $ gpg_error_t# err = 0; |
| 63 | const char **rest_args; |
| 64 | log_handle_tcxx . . $ log_handle_t# loghandle = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 65 | |
| 66 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (argc > 0); |
| 67 | |
| 68 | /* Init. */ |
| 69 | err = log_createcxx . . $ log_create(ae9175ffffc76e55). (&loghandle); |
| 70 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (!err); |
| 71 | |
| 72 | err = log_set_backend_streamcxx . . $ log_set_backend_stream(79a3f3539912b772). (loghandle, stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 73 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (!err); |
| 74 | |
| 75 | err = simpleparse_createcxx . . $ simpleparse_create(66008bf8c18601a7). (&handle); |
| 76 | assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`! (!err); |
| 77 | |
| 78 | simpleparse_set_parse_cbcxx . . $ simpleparse_set_parse_cb(3172d0a439858735). (handle, parsecbcxx . . $ parsecb(c7ea03b5806a970)., "parse-test parser"); |
| 79 | simpleparse_set_loghandlecxx . . $ simpleparse_set_loghandle(160e2747cca04437). (handle, loghandle); |
| 80 | simpleparse_set_specscxx . . $ simpleparse_set_specs(79a7ca40abe9bcfd). (handle, opt_specscxx . . $ opt_specs.); |
| 81 | |
| 82 | /* Parse command-line arguments. */ |
| 83 | err = simpleparse_parsecxx . . $ simpleparse_parse(62ade082e5fc30eb). (handle, 0, argc - 1, argv + 1, &rest_args); |
| 84 | if (err) |
| 85 | { |
| 86 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "simpleparse_parse returned error: %s\n", |
| 87 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | printfcxx . . $ printf(581fddd72a8db8fc). ("Rest args: "); |
| 92 | if (rest_args) |
| 93 | { |
| 94 | while (*rest_args) |
| 95 | { |
| 96 | printfcxx . . $ printf(581fddd72a8db8fc). ("%s%s", *rest_args, *(rest_args + 1) ? ", " : ""); |
| 97 | rest_args++; |
| 98 | } |
| 99 | } |
| 100 | printfcxx . . $ printf(581fddd72a8db8fc). ("\n"); |
| 101 | |
| 102 | /* Parse stdin as config file. */ |
| 103 | err = simpleparse_parse_streamcxx . . $ simpleparse_parse_stream(c210c2fed664be1f). (handle, 0, stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!); |
| 104 | if (err) |
| 105 | { |
| 106 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac). (stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "simpleparse_parse_stream returned error: %s\n", |
| 107 | gpg_strerrorcxx . . $ gpg_strerror(81a3ae90da26a711). (err)); |
| 108 | goto out; |
| 109 | } |
| 110 | |
| 111 | out: |
| 112 | |
| 113 | simpleparse_destroycxx . . $ simpleparse_destroy(f8a3a57ff17a2201). (handle); |
| 114 | |
| 115 | return !!err; |
| 116 | } |