| 1 | /* simpleparse.h - Minimalistic parser |
| 2 | Copyright (C) 2008 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 | /* This is a minimalistic parser for command-line arguments and |
| 21 | configuration files. Design goals are: |
| 22 | |
| 23 | - small and flexible, |
| 24 | - no global variables, |
| 25 | - uses callbacks. */ |
| 26 | |
| 27 | #ifndef SIMPLEPARSE_H |
| 28 | #define SIMPLEPARSE_H |
| 29 | |
| 30 | #include <poldi.h> |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdarg.h> |
| 34 | |
| 35 | #include <util/simplelog.h> |
| 36 | |
| 37 | enum simpleparse_arg |
| 38 | { |
| 39 | SIMPLEPARSE_ARG_NONE, |
| 40 | SIMPLEPARSE_ARG_OPTIONAL, |
| 41 | SIMPLEPARSE_ARG_REQUIRED |
| 42 | }; |
| 43 | |
| 44 | typedef struct |
| 45 | { |
| 46 | int id; |
| 47 | const char *long_opt; |
| 48 | int short_opt; |
| 49 | enum simpleparse_arg arg; |
| 50 | unsigned flags; |
| 51 | const char *description; /* optional option description */ |
| 52 | } simpleparse_opt_spec_t; |
| 53 | typedef struct simpleparse_handle *simpleparse_handle_t; |
| 54 | |
| 55 | gpg_error_t simpleparse_create (simpleparse_handle_t *handle); |
| 56 | void simpleparse_destroy (simpleparse_handle_t handle); |
| 57 | |
| 58 | void simpleparse_set_loghandle (simpleparse_handle_t handle, |
| 59 | log_handle_t loghandle); |
| 60 | |
| 61 | typedef gpg_error_t (*simpleparse_parse_cb_t) (void *cookie, |
| 62 | simpleparse_opt_spec_t spec, const char *arg); |
| 63 | |
| 64 | void simpleparse_set_parse_cb (simpleparse_handle_t handle, |
| 65 | simpleparse_parse_cb_t parse_cb, void *cookie); |
| 66 | |
| 67 | typedef const char *(*simpleparse_i18n_cb_t) (void *cookie, const char *msg); |
| 68 | |
| 69 | void simpleparse_set_i18n_cb (simpleparse_handle_t handle, |
| 70 | simpleparse_i18n_cb_t i18n_cb, void *cookie); |
| 71 | |
| 72 | gpg_error_t simpleparse_set_specs (simpleparse_handle_t handle, simpleparse_opt_spec_t *specs); |
| 73 | |
| 74 | void simpleparse_set_name (simpleparse_handle_t handle, const char *program_name); |
| 75 | void simpleparse_set_package (simpleparse_handle_t handle, const char *package_name); |
| 76 | void simpleparse_set_copyright (simpleparse_handle_t handle, const char *copyright_info); |
| 77 | void simpleparse_set_version (simpleparse_handle_t handle, const char *program_version); |
| 78 | void simpleparse_set_bugaddress (simpleparse_handle_t handle, const char *bugaddress); |
| 79 | void simpleparse_set_author (simpleparse_handle_t handle, const char *author); |
| 80 | void simpleparse_set_license (simpleparse_handle_t handle, const char *license); |
| 81 | void simpleparse_set_description (simpleparse_handle_t handle, const char *program_description); |
| 82 | void simpleparse_set_syntax (simpleparse_handle_t handle, const char *syntax_description); |
| 83 | void simpleparse_set_streams (simpleparse_handle_t handle, FILE *stream_stdout, FILE *stream_stderr); |
| 84 | |
| 85 | gpg_error_t simpleparse_parse (simpleparse_handle_t handle, unsigned int flags, |
| 86 | unsigned int argc, const char **argv, const char ***rest_args); |
| 87 | |
| 88 | gpg_error_t simpleparse_parse_file (simpleparse_handle_t handle, unsigned int flags, |
| 89 | const char *filename); |
| 90 | |
| 91 | gpg_error_t simpleparse_parse_stream (simpleparse_handle_t handle, unsigned int flags, |
| 92 | FILE *stream); |
| 93 | |
| 94 | #endif |