diff --git a/NEWS b/NEWS index d212f39..51c2c89 100644 --- a/NEWS +++ b/NEWS @@ -1,1031 +1,1031 @@ Noteworthy changes in version 1.38 (unreleased) [C28/A28/R_] ----------------------------------------------- * Interface changes relative to the 1.37 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_fnameconcat NEW. gpgrt_absfnameconcat NEW. gpgrt_set_confdir NEW. gpgrt_argparser NEW. ARGPARSE_FLAG_SYS NEW. ARGPARSE_FLAG_USER NEW. ARGPARSE_FLAG_VERBOSE NEW. ARGPARSE_FLAG_USERVERS NEW. ARGPARSE_NO_CONFFILE NEW. ARGPARSE_CONFFILE NEW. ARGPARSE_OPT_CONFFILE NEW. ARGPARSE_PERMISSION_ERROR NEW. ARGPARSE_INVALID_META NEW. ARGPARSE_UNKNOWN_META NEW. ARGPARSE_UNEXPECTED_META NEW. ARGPARSE_conffile NEW. ARGPARSE_noconffile NEW. GPGRT_CONFDIR_USER NEW. GPGRT_CONFDIR_SYS NEW. - Release-info: https://dev.gnupg.org/T + Release-info: https://dev.gnupg.org/T4859 Noteworthy changes in version 1.37 (2020-02-07) [C28/A28/R0] ----------------------------------------------- * Fixes a build problems when using Gawk 5.0 [#4459] * Fixes Bourne shell incompatibilities on Solaris. [#4574] * Improves cross-comiling support. [#4643] * On Windows strerror_s is now used to emulate strerror_r. [#4539] * New error codes to map SQLite primary error codes. * Now uses poll(2) instead of select(2) in gpgrt_poll if possible. * Fixes a bug in gpgrt_close. [#4698] * Fixes build problem under Cygwin. [#4474] * Fixes a few minor portability bugs. * Interface changes relative to the 1.36 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_KEYBOXD NEW. GPG_ERR_KEYBOXD NEW. GPG_ERR_NO_SERVICE NEW. GPG_ERR_SERVICE. NEW. GPG_ERR_SQL_OK NEW. GPG_ERR_SQL_ERROR NEW. GPG_ERR_SQL_INTERNAL NEW. GPG_ERR_SQL_PERM NEW. GPG_ERR_SQL_ABORT NEW. GPG_ERR_SQL_BUSY NEW. GPG_ERR_SQL_LOCKED NEW. GPG_ERR_SQL_NOMEM NEW. GPG_ERR_SQL_READONLY NEW. GPG_ERR_SQL_INTERRUPT NEW. GPG_ERR_SQL_IOERR NEW. GPG_ERR_SQL_CORRUPT NEW. GPG_ERR_SQL_NOTFOUND NEW. GPG_ERR_SQL_FULL NEW. GPG_ERR_SQL_CANTOPEN NEW. GPG_ERR_SQL_PROTOCOL NEW. GPG_ERR_SQL_EMPTY NEW. GPG_ERR_SQL_SCHEMA NEW. GPG_ERR_SQL_TOOBIG NEW. GPG_ERR_SQL_CONSTRAINT NEW. GPG_ERR_SQL_MISMATCH NEW. GPG_ERR_SQL_MISUSE NEW. GPG_ERR_SQL_NOLFS NEW. GPG_ERR_SQL_AUTH NEW. GPG_ERR_SQL_FORMAT NEW. GPG_ERR_SQL_RANGE NEW. GPG_ERR_SQL_NOTADB NEW. GPG_ERR_SQL_NOTICE NEW. GPG_ERR_SQL_WARNING NEW. GPG_ERR_SQL_ROW NEW. GPG_ERR_SQL_DONE NEW. Release-info: https://dev.gnupg.org/T4772 Noteworthy changes in version 1.36 (2019-03-19) [C27/A27/R0] ----------------------------------------------- * Two new error codes to better support PIV cards. * Support armv7a-unknown-linux-gnueabihf. * Increased estream buffer sizes for Windows. * Interface changes relative to the 1.34 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_AUTH NEW. GPG_ERR_BAD_AUTH NEW. Release-info: https://dev.gnupg.org/T4413 Noteworthy changes in version 1.35 (2019-01-28) [C26/A26/R1] ----------------------------------------------- * Distribute the correct gpgrt-config. Noteworthy changes in version 1.34 (2019-01-16) [C26/A26/R0] ----------------------------------------------- * Support for riscv32. * New API to allow emergency cleanup after internal fatal errors. * Minor bug and portability fixes. [#4286,#4298 * Interface changes relative to the 1.33 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_abort NEW. gpgrt_add_emergency_cleanup NEW. Release-info: https://dev.gnupg.org/T4296 Noteworthy changes in version 1.33 (2018-12-07) [C25/A25/R0] ----------------------------------------------- * New unified config script gpgrt-config which can now be used by all GnuPG related packages. * Support for ARC and arm64ilp32. * The log functions now sanitize strings printed with the "%s" format specifier. All control characters are C-escaped in the output. Users of that function may want to remove their own escaping to avoid doubling of backslashes. * New fprintf style function to apply a custom filter for string arguments. * New function to compare version strings. * Interface changes relative to the 1.28 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_cmp_version New. gpgrt_string_filter_t New. gpgrt_fprintf_sf New. gpgrt_fprintf_sf_unlocked New. gpgrt_ftruncate New but limited functionality. gpgrt_w32_override_locale New. Release-info: https://dev.gnupg.org/T4205 Noteworthy changes in version 1.32 (2018-07-12) [C24/A24/R3] ----------------------------------------------- * Fixes a problem with gpgrt_fflush and gpgrt_fopencookie. [#4069] * Fixes a problem with the C11 header stdnoreturn.h. [#4002] * The yat2m tool can now also be build on Windows. * Updates translations for Spanish, Russian and Ukrainian. Noteworthy changes in version 1.31 (2018-05-02) [C24/A24/R2] ----------------------------------------------- * Fixes another problem with gpgrt_poll under Windows. [#3937] * New translation for Spanish. Noteworthy changes in version 1.30 (2018-04-30) [C24/A24/R1] ----------------------------------------------- * Fixes a hang on Windows when using gpgrt_poll under nPth. * Build fix for Solaris. [#3869] Noteworthy changes in version 1.29 (2018-04-11) [C24/A24/R0] ----------------------------------------------- * The yat2m tool is during cross-compile now also installed on the host platform. * New option parser and associated functions similar to the one used by GnuPG. * New Base-64 encoder. * Fixes regression in 1.28 for arm64 and w64 builds. * Interface changes relative to the 1.28 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_argparse New. gpgrt_usage New. gpgrt_strusage New. gpgrt_set_strusage New. gpgrt_set_usage_outfnc New. gpgrt_set_fixed_string_mapper New. GPGRT_ENABLE_ARGPARSE_MACROS New macro. gpgrt_b64enc_start New. gpgrt_b64enc_write New. gpgrt_b64enc_finish New. Noteworthy changes in version 1.28 (2018-03-13) [C23/A23/R0] ----------------------------------------------- * The formerly internal yat2m tool is now installed for a native build. * The new files gpgrt.m4 and gpgrt-config are now installed. They can be used instead of gpg-error.m4 and gpg-error-config. * New logging functions similar to those used by GnuPG. * New helper functions for platform abstraction. * Interface changes relative to the 1.27 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_get_errorcount New API. gpgrt_inc_errorcount New API. gpgrt_log_set_sink New API. gpgrt_log_set_socket_dir_cb New API. gpgrt_log_set_pid_suffix_cb New API. gpgrt_log_set_prefix New API. gpgrt_log_get_prefix New API. gpgrt_log_test_fd New API. gpgrt_log_get_fd New API. gpgrt_log_get_stream New API. gpgrt_log New API. gpgrt_logv New API. gpgrt_logv_prefix New API. gpgrt_log_string New API. gpgrt_log_info New API. gpgrt_log_error New API. gpgrt_log_fatal New API. gpgrt_log_bug New API. gpgrt_log_debug New API. gpgrt_log_debug_string New API. gpgrt_log_printf New API. gpgrt_log_flush New API. gpgrt_log_printhex New API. gpgrt_log_clock New API. gpgrt_assert New macro. _gpgrt_log_assert New internal API. GPGRT_LOGLVL_BEGIN New const. GPGRT_LOGLVL_CONT New const. GPGRT_LOGLVL_INFO New const. GPGRT_LOGLVL_WARN New const. GPGRT_LOGLVL_ERROR New const. GPGRT_LOGLVL_FATAL New const. GPGRT_LOGLVL_BUG New const. GPGRT_LOGLVL_DEBUG New const. gpgrt_realloc New API. gpgrt_malloc New API. gpgrt_calloc New API. gpgrt_strdup New API. gpgrt_strconcat New API. gpgrt_w32_reg_query_string New API. gpgrt_getenv New API. gpgrt_setenv New API. gpgrt_mkdir New API. gpgrt_chdir New API. gpgrt_getcwd New API. Noteworthy changes in version 1.27 (2017-02-28) [C22/A22/R0] ----------------------------------------------- * Added a Base64 decoder. * Added support for the sh3 architecture. * Added header gpgrt.h as an alias for gpg-error.h. * Fixed macro GPGRT_GCC_VERSION. * Fixed a race in non-blocking I/O on Windows. * Interface changes relative to the 1.26 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_b64state_t NEW type. gpgrt_b64dec_start NEW. gpgrt_b64dec_proc NEW. gpgrt_b64dec_finish NEW. GPG_ERR_WRONG_NAME NEW. gpgrt.h NEW header. Noteworthy changes in version 1.26 (2016-12-21) [C21/A21/R0] ----------------------------------------------- * New option --desc for gpg-error. * Interface changes relative to the 1.25 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_UNKNOWN_FLAG NEW. GPG_ERR_INV_ORDER NEW. GPG_ERR_ALREADY_FETCHED NEW. GPG_ERR_TRY_LATER NEW. GPG_ERR_SYSTEM_BUG NEW. GPG_ERR_DNS_UNKNOWN NEW. GPG_ERR_DNS_SECTION NEW. GPG_ERR_DNS_ADDRESS NEW. GPG_ERR_DNS_NO_QUERY NEW. GPG_ERR_DNS_NO_ANSWER NEW. GPG_ERR_DNS_CLOSED NEW. GPG_ERR_DNS_VERIFY NEW. GPG_ERR_DNS_TIMEOUT NEW. Noteworthy changes in version 1.25 (2016-11-14) [C20/A20/R0] ----------------------------------------------- * New interface gpgrt_get_syscall_clamp to allow libaries to make use of Libgpg-error's system call wrapper functions. * gpgrt_poll does now work under Windows. * Fixed bug in the locking code when used with the nPth threading library. * Added support for {i686,x86_64}-apple-darwin. * Added new error codes. * Interface changes relative to the 1.23 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_get_syscall_clamp NEW. GPG_ERR_ENGINE_TOO_OLD NEW. GPG_ERR_WINDOW_TOO_SMALL NEW. GPG_ERR_WINDOW_TOO_LARGE NEW. GPG_ERR_MISSING_ENVVAR NEW. GPG_ERR_USER_ID_EXISTS NEW. GPG_ERR_NAME_EXISTS NEW. GPG_ERR_DUP_NAME NEW. GPG_ERR_TOO_OLD NEW. GPG_ERR_TOO_YOUNG NEW. Noteworthy changes in version 1.24 (2016-07-14) [C19/A19/R1] ----------------------------------------------- * Fixes a bug in es_fclose_snatch when used used after es_fseek. * Fixes building without thread support. * New configure option --disable-tests. Noteworthy changes in version 1.23 (2016-06-15) [C19/A19/R0] ----------------------------------------------- * Fixes an assertion failure due to es_flush on read/write streams. * Fixes a bug with a too short memory limit is es_fopenmen. * Cross-build support for powerpc-unknown-linux-gnuspe and tilegx-unknown-linux-gnu architectures. * Interface changes relative to the 1.22 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SUBKEYS_EXP_OR_REV NEW. Noteworthy changes in version 1.22 (2016-04-25) [C18/A18/R0] ----------------------------------------------- * New functions and macros to to provide iconv(3) on Windows. * Support for LeakSanitizer with the gpgrt_annotate_leaked_object inline function. * Interface changes relative to the 1.21 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_DB_CORRUPTED NEW. gpgrt_annotate_leaked_object NEW inline func. GPGRT_ENABLE_W32_ICONV_MACROS NEW. gpgrt_w32_iconv_open NEW. gpgrt_w32_iconv_close NEW. gpgrt_w32_iconv NEW. Noteworthy changes in version 1.21 (2015-12-12) [C17/A17/R0] ----------------------------------------------- * New functions gpgrt_poll and gpgrt_set_nonblock. For now only pipes and sockets on Unix are supported. * Fixes gettext output encoding problems on Windows. * Interface changes relative to the 1.20 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpgrt_set_nonblock NEW. gpgrt_get_nonblock NEW. gpgrt_poll NEW. gpgrt_poll_t NEW type. es_poll_t NEW type. es_set_nonblock NEW macro. es_get_nonblock NEW macro. es_poll NEW macro. GPG_ERR_TRUE NEW. GPG_ERR_FALSE NEW. GPG_ERR_NO_NAME NEW. GPG_ERR_NO_KEY NEW. GPG_ERR_SERVER_FAILED NEW. Noteworthy changes in version 1.20 (2015-08-26) [C16/A16/R0] ----------------------------------------------- * New macros for GCC attributes. * Make es_set_binary actually work for Windows. * Allow building without thread support. * Build without a build timestamp by default. * Interface changes relative to the 1.19 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPGRT_VERSION NEW macro. GPGRT_VERSION_NUMBER NEW macro. GPGRT_INLINE NEW macro. GPGRT_GCC_VERSION NEW macro. GPGRT_ATTR_NORETURN NEW macro. GPGRT_ATTR_PRINTF NEW macro. GPGRT_ATTR_NR_PRINTF NEW macro. GPGRT_ATTR_FORMAT_ARG NEW macro. GPGRT_ATTR_SENTINEL NEW macro. GPGRT_ATTR_USED NEW macro. GPGRT_ATTR_UNUSED NEW macro. GPGRT_ATTR_DEPRECATED NEW macro. GPGRT_ATTR_PURE NEW macro. GPGRT_ATTR_MALLOC NEW macro. GPGRT_HAVE_MACRO_FUNCTION NEW macro. GPGRT_HAVE_PRAGMA_GCC_PUSH NEW macro. Noteworthy changes in version 1.19 (2015-04-10) [C15/A15/R0] ----------------------------------------------- * New set of error codes for use with LDAP. * New options --help and --defines for gpg-error. * Allow building with gcc 5. * Interface changes relative to the 1.18 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_LDAP_* NEW. Noteworthy changes in version 1.18 (2015-01-26) [C14/A14/R0] ----------------------------------------------- * New translations for Hungarian, Portuguese, Russian, and traditional Chinese. Updated other translations. * New error codes. * Interface changes relative to the 1.17 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_FORBIDDEN NEW. GPG_ERR_OBJ_TERM_STATE NEW. GPG_ERR_REQUEST_TOO_SHORT NEW. GPG_ERR_REQUEST_TOO_LONG NEW. GPG_ERR_LEGACY_KEY NEW. Noteworthy changes in version 1.17 (2014-10-15) [C13/A13/R0] ----------------------------------------------- * New error codes for TLS protocol libraries. * New configure option --enable-build-timestamp. * New man page for gpg-error-config. * Interface changes relative to the 1.16 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SOURCE_TLS NEW. GPG_ERR_NO_CERT_CHAIN NEW. GPG_ERR_CERT_TOO_LARGE NEW. GPG_ERR_INV_RECORD NEW. GPG_ERR_BAD_MAC NEW. GPG_ERR_UNEXPECTED_MSG NEW. GPG_ERR_COMPR_FAILED NEW. GPG_ERR_WOULD_WRAP NEW. GPG_ERR_FATAL_ALERT NEW. GPG_ERR_NO_CIPHER NEW. GPG_ERR_MISSING_CLIENT_CERT NEW. GPG_ERR_CLOSE_NOTIFY NEW. GPG_ERR_TICKET_EXPIRED NEW. GPG_ERR_BAD_TICKET NEW. GPG_ERR_UNKNOWN_IDENTITY NEW. GPG_ERR_BAD_HS_CERT NEW. GPG_ERR_BAD_HS_CERT_REQ NEW. GPG_ERR_BAD_HS_CERT_VER NEW. GPG_ERR_BAD_HS_CHANGE_CIPHER NEW. GPG_ERR_BAD_HS_CLIENT_HELLO NEW. GPG_ERR_BAD_HS_SERVER_HELLO NEW. GPG_ERR_BAD_HS_SERVER_HELLO_DONE NEW. GPG_ERR_BAD_HS_FINISHED NEW. GPG_ERR_BAD_HS_SERVER_KEX NEW. GPG_ERR_BAD_HS_CLIENT_KEX NEW. GPG_ERR_BOGUS_STRING NEW. gpgrt_pending NEW. gpgrt_pending_unlocked NEW. Noteworthy changes in version 1.16 (2014-09-18) [C12/A12/R2] ----------------------------------------------- * Support building for iOS. * Fixed a prototype mismatch. * Fix es_fclose for streams opened with "samethread". Noteworthy changes in version 1.15 (2014-09-11) [C12/A12/R1] ----------------------------------------------- * This releases fixes problems with the use of off_t and ssize_t by the estream functions introduced with 1.14. Although this is technically an ABI break on some platforms, we take this as a simple bug fix for 1.14. The new functions are very unlikely in use by any code and thus no breakage should happen. The 1.14 tarball will be removed from the archive. * Add type gpgrt_off_t which is guaranteed to be 64 bit. * Add type gpgrt_ssize_t to make use on Windows easier. On Unix platforms this is an alias for ssize_t. Noteworthy changes in version 1.14 (2014-09-08) [C12/A12/R0] ----------------------------------------------- * Added gpgrt_lock_trylock. * Added the estream library under the name gpgrt and a set of macros to use them with their "es_" names. * Interface changes relative to the 1.13 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_KEY_DISABLED NEW. gpgrt_init NEW macro. gpgrt_check_version NEW. gpgrt_lock_trylock NEW. gpgrt_set_syscall_clamp NEW. gpgrt_set_alloc_func NEW. gpgrt_stream_t NEW. gpgrt_cookie_io_functions_t NEW. gpgrt_syshd_t NEW. GPGRT_SYSHD_NONE NEW. GPGRT_SYSHD_FD NEW. GPGRT_SYSHD_SOCK NEW. GPGRT_SYSHD_RVID NEW. GPGRT_SYSHD_HANDLE NEW. gpgrt_stdin NEW macro. gpgrt_stdout NEW macro. gpgrt_stderr NEW macro. gpgrt_fopen NEW. gpgrt_mopen NEW. gpgrt_fopenmem NEW. gpgrt_fopenmem_init NEW. gpgrt_fdopen NEW. gpgrt_fdopen_nc NEW. gpgrt_sysopen NEW. gpgrt_sysopen_nc NEW. gpgrt_fpopen NEW. gpgrt_fpopen_nc NEW. gpgrt_freopen NEW. gpgrt_fopencookie NEW. gpgrt_fclose NEW. gpgrt_fclose_snatch NEW. gpgrt_onclose NEW. gpgrt_fileno NEW. gpgrt_fileno_unlocked NEW. gpgrt_syshd NEW. gpgrt_syshd_unlocked NEW. gpgrt_flockfile NEW. gpgrt_ftrylockfile NEW. gpgrt_funlockfile NEW. gpgrt_feof NEW. gpgrt_feof_unlocked NEW. gpgrt_ferror NEW. gpgrt_ferror_unlocked NEW. gpgrt_clearerr NEW. gpgrt_clearerr_unlocked NEW. gpgrt_fflush NEW. gpgrt_fseek NEW. gpgrt_fseeko NEW. gpgrt_ftell NEW. gpgrt_ftello NEW. gpgrt_rewind NEW. gpgrt_getc NEW macro. gpgrt_getc_unlocked NEW macro. gpgrt_fgetc NEW. gpgrt_fputc NEW. gpgrt_ungetc NEW. gpgrt_read NEW. gpgrt_write NEW. gpgrt_write_sanitized NEW. gpgrt_write_hexstring NEW. gpgrt_fread NEW. gpgrt_fwrite NEW. gpgrt_fgets NEW. gpgrt_putc NEW macro. gpgrt_putc_unlocked NEW macro. gpgrt_fputs NEW. gpgrt_fputs_unlocked NEW. gpgrt_getline NEW. gpgrt_read_line NEW. gpgrt_free NEW. gpgrt_fprintf NEW. gpgrt_fprintf_unlocked NEW. gpgrt_printf NEW. gpgrt_printf_unlocked NEW. gpgrt_vfprintf NEW. gpgrt_vfprintf_unlocked NEW. gpgrt_setvbuf NEW. gpgrt_setbuf NEW. gpgrt_set_binary NEW. gpgrt_tmpfile NEW. gpgrt_opaque_set NEW. gpgrt_opaque_get NEW. gpgrt_fname_set NEW. gpgrt_fname_get NEW. gpgrt_asprintf NEW. gpgrt_vasprintf NEW. gpgrt_bsprintf NEW. gpgrt_vbsprintf NEW. gpgrt_snprintf NEW. gpgrt_vsnprintf NEW. Noteworthy changes in version 1.13 (2014-04-15) [C11/A11/R0] ----------------------------------------------- * Added a portable mutex API. * The AM_PATH_GPG_ERROR macro now defines GPG_ERROR_MT_CFLAGS and GPG_ERROR_MT_LIBS autoconf output variables for use by programs which need gpgrt based thread support. gpg-error-config has a new option --mt. * Interface changes relative to the 1.12 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_KEY_ON_CARD NEW. GPG_ERR_MAC_ALGO NEW. GPG_ERR_INV_LOCK_OBJ NEW. gpgrt_lock_t NEW. GPGRT_LOCK_INITIALIZER NEW. GPGRT_LOCK_DEFINE NEW. gpgrt_lock_init NEW. gpgrt_lock_lock NEW. gpgrt_lock_unlock NEW. gpgrt_lock_destroy NEW. gpgrt_yield NEW. Noteworthy changes in version 1.12 (2013-06-24) ----------------------------------------------- * Add support for 64 bit Windows (use ./autogen.sh --build-w64). * Fixed parsing and installing of the Windows .def file. * Interface changes relative to the 1.11 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_CRYPT_CTX NEW. GPG_ERR_WRONG_CRYPT_CTX NEW. GPG_ERR_BAD_CRYPT_CTX NEW. GPG_ERR_CRYPT_CTX_CONFLICT NEW. GPG_ERR_BROKEN_PUBKEY NEW. GPG_ERR_BROKEN_SECKEY NEW. Noteworthy changes in version 1.11 (2013-02-25) ----------------------------------------------- * New error source GPG_ERR_SOURCE_ASSUAN for Libassuan related errors. * New macros GPG_ERROR_VERSION and GPG_ERROR_VERSION_NUMBER. New function gpg_error_check_version. * Interface changes relative to the 1.10 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NO_KEYSERVER NEW. GPG_ERR_INV_CURVE NEW. GPG_ERR_UNKNOWN_CURVE NEW. GPG_ERR_DUP_KEY NEW. GPG_ERR_AMBIGUOUS NEW. GPG_ERR_SOURCE_ASSUAN NEW. gpg_error_check_version NEW. GPG_ERROR_VERSION NEW. GPG_ERROR_VERSION_NUMBER NEW. Noteworthy changes in version 1.10 (2010-10-26) ----------------------------------------------- * Using a static library on W32 does now work. * Interface changes relative to the 1.9 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NOT_INITIALIZED NEW. GPG_ERR_MISSING_ISSUER_CERT NEW. GPG_ERR_FULLY_CANCELED NEW. Noteworthy changes in version 1.9 (2010-07-21) ---------------------------------------------- * New function gpg_err_deinit. * Fix building of static lib under W32. * Interface changes relative to the 1.8 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_MISSING_KEY NEW. GPG_ERR_TOO_MANY NEW. GPG_ERR_LIMIT_REACHED NEW. gpg_err_deinit NEW. Noteworthy changes in version 1.8 (2010-05-06) ---------------------------------------------- * Support for WindowsCE. * New option --list for gpg-error. * Interface changes relative to the 1.7 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NOT_ENABLED NEW. GPG_ERR_SOURCE_G13 NEW. GPG_ERR_NO_ENGINE NEW. gpg_err_set_errno NEW. Noteworthy changes in version 1.7 (2008-11-26) ---------------------------------------------- * Minor fixes and a few new error codes. * Interface changes relative to the 1.6 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_NOT_OPERATIONAL NEW GPG_ERR_NO_PASSPHRASE NEW GPG_ERR_NO_PIN NEW Noteworthy changes in version 1.6 (2007-10-29) ---------------------------------------------- * Fixed a build problem under Windows. * Interface changes relative to the 1.4 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_UNFINISHED NEW GPG_ERR_SOURCE_GPA NEW GPG_ERR_SOURCE_KLEO NEW Noteworthy changes in version 1.5 (2006-11-30) ---------------------------------------------- * Minor build system fixes. * Updated gettext. Removed included gettext copy. * gpg-error has a new option --version. Noteworthy changes in version 1.4 (2006-09-14) ---------------------------------------------- * Support for Common Lisp is included. * New error codes for the Assuan IPC library. * New error code GPG_ERR_MISSING_ERRNO to be used in cases when a system accidentally does not set errno but a system error definitely occurred. * New error source GPG_ERR_SOURCE_ANY to allow proper use of libgpg-error even if a specific source is not available. * New convenience functions gpg_err_code_from_syserror and gpg_error_from_syserror which make sure never to return 0. * Interface changes relative to the 1.2 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpg_err_code_from_syserror NEW gpg_error_from_syserror NEW GPG_ERR_SOURCE_ANY NEW GPG_ERR_MISSING_ERRNO NEW GPG_ERR_UNKNOWN_OPTION NEW GPG_ERR_UNKNOWN_COMMAND NEW GPG_ERR_ASS_GENERAL NEW GPG_ERR_ASS_ACCEPT_FAILED NEW GPG_ERR_ASS_CONNECT_FAILED NEW GPG_ERR_ASS_INV_RESPONSE NEW GPG_ERR_ASS_INV_VALUE NEW GPG_ERR_ASS_INCOMPLETE_LINE NEW GPG_ERR_ASS_LINE_TOO_LONG NEW GPG_ERR_ASS_NESTED_COMMANDS NEW GPG_ERR_ASS_NO_DATA_CB NEW GPG_ERR_ASS_NO_INQUIRE_CB NEW GPG_ERR_ASS_NOT_A_SERVER NEW GPG_ERR_ASS_NOT_A_CLIENT NEW GPG_ERR_ASS_SERVER_START NEW GPG_ERR_ASS_READ_ERROR NEW GPG_ERR_ASS_WRITE_ERROR NEW GPG_ERR_ASS_TOO_MUCH_DATA NEW GPG_ERR_ASS_UNEXPECTED_CMD NEW GPG_ERR_ASS_UNKNOWN_CMD NEW GPG_ERR_ASS_SYNTAX NEW GPG_ERR_ASS_CANCELED NEW GPG_ERR_ASS_NO_INPUT NEW GPG_ERR_ASS_NO_OUTPUT NEW GPG_ERR_ASS_PARAMETER NEW GPG_ERR_ASS_UNKNOWN_INQUIRE NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.3 (2006-03-14) ---------------------------------------------- * GNU gettext is included for systems that do not provide it. Noteworthy changes in version 1.2 (2006-03-03) ---------------------------------------------- * New function gpg_err_init, which binds the locale directory to the text domain. This function is a constructor on GCC targets, so it does not need to be called explicitely. The header file defines GPG_ERR_INITIALIZED in this case. This is experimental for now. * "./autogen.sh --build-w32" does now also build a DLL for W32. Translations are not yet provided for this platform. * New error codes GPG_ERR_UNKNOWN_EXTN and GPG_ERR_UNKNOWN_CRIT_EXTN. * New error code GPG_ERR_LOCKED. * New translations included for France, Romania, and Vietnamese. * Interface changes relative to the 1.1 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_UNKNOWN_EXTN NEW GPG_ERR_UNKNOWN_CRIT_EXTN NEW GPG_ERR_LOCKED NEW gpg_err_init NEW GPG_ERR_INITIALIZED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1 (2005-06-20) ---------------------------------------------- * Bug fixes. Noteworthy changes in version 1.0 (2004-07-30) ---------------------------------------------- * Ported to Solaris 2.8. * Added a new error source GPG_ERR_SOURCE_GSTI, and new error codes GPG_ERR_PROTOCOL_VIOLATION and GPG_ERR_INV_MAC for this source. * Interface changes relative to the 0.7 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SOURCE_GSTI NEW GPG_ERR_PROTOCOL_VIOLATION NEW GPG_ERR_INV_MAC NEW GPG_ERR_INV_REQUEST NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.7 (2004-03-07) ---------------------------------------------- * libgpg-error can be built on systems where the errno macros do not evaluate to plain numbers, but expressions. If you want to cross-compile, you might have to set CC_FOR_BUILD, though. * A new tool gpg-error to convert error numbers into symbols into strings is provided. * Interface changes relative to the 0.6 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_LOCALE_PROBLEM NEW GPG_ERR_NOT_LOCKED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.6 (2003-11-14) ---------------------------------------------- * German translation included. * It is now possible to use the inline functions even for non C99 compliant compilers by given e.g. -DGPG_ERR_INLINE=inline when compiling an application using this library. Note, that gcc will use inline anyway. * Interface changes relative to the 0.5 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ GPG_ERR_SOURCE_KSBA NEW GPG_ERR_SOURCE_DIRMNGR NEW GPG_ERR_TRUNCATED NEW GPG_ERR_NO_ENCODING_METHOD NEW GPG_ERR_NO_ENCRYPTION_SCHEME NEW GPG_ERR_NO_SIGNATURE_SCHEME NEW GPG_ERR_INV_ATTR NEW GPG_ERR_NO_VALUE NEW GPG_ERR_NOT_FOUND NEW GPG_ERR_VALUE_NOT_FOUND NEW GPG_ERR_SYNTAX NEW GPG_ERR_INV_CRL NEW GPG_ERR_BAD_BER NEW GPG_ERR_INV_BER NEW GPG_ERR_ELEMENT_NOT_FOUND NEW GPG_ERR_IDENTIFIER_NOT_FOUND NEW GPG_ERR_INV_TAG NEW GPG_ERR_INV_LENGTH NEW GPG_ERR_INV_KEYINFO NEW GPG_ERR_UNEXPECTED_TAG NEW GPG_ERR_NOT_DER_ENCODED, NEW GPG_ERR_NO_CMS_OBJ NEW GPG_ERR_INV_CMS_OBJ NEW GPG_ERR_UNKNOWN_CMS_OBJ, NEW GPG_ERR_UNSUPPORTED_CMS_OBJ NEW GPG_ERR_UNSUPPORTED_ENCODING, NEW GPG_ERR_UNSUPPORTED_CMS_VERSION NEW GPG_ERR_UNKNOWN_ALGORITHM NEW GPG_ERR_ENCODING_PROBLEM NEW GPG_ERR_INV_STATE NEW GPG_ERR_DUP_VALUE, NEW GPG_ERR_MISSING_ACTION NEW GPG_ERR_MODULE_NOT_FOUND NEW GPG_ERR_INV_OID_STRING NEW GPG_ERR_INV_TIME NEW GPG_ERR_INV_CRL_OBJ NEW GPG_ERR_UNSUPPORTED_CRL_VERSION NEW GPG_ERR_INV_CERT_OBJ NEW GPG_ERR_UNKNOWN_NAME NEW GPG_ERR_BUFFER_TOO_SHORT. NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.5 (2003-10-06) ---------------------------------------------- * New thread safe interface gpg_strerror_r. * New error code GPG_ERR_PIN_NOT_SYNCED has been added. * Interface changes relative to the 0.4 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gpg_strerror_r NEW GPG_ERR_PIN_NOT_SYNCED NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 0.4 (2003-09-03) ---------------------------------------------- * Fixed another bug that prevented that system errors were created correctly in the first place. * Use inline in public header file only on C99 compilers. Noteworthy changes in version 0.3 (2003-07-31) ---------------------------------------------- * Fixed bug that prevented that system errors were mapped to error strings correctly. Noteworthy changes in version 0.2 (2003-07-30) ---------------------------------------------- * Value of the error code GPG_ERR_CANCELED was fixed. * New error codes GPG_ERR_WRONG_CARD, GPG_ERR_HARDWARE, GPG_ERR_PIN_BLOCKED and GPG_ERR_USE_CONDITIONS have been added. * The header file has been made C++ clean. * AM_PATH_GPG_ERR has been fixed to work without explicit version number. * The header file now uses inline instead __inline__ for non-GNU compilers. Noteworthy changes in version 0.1 (2003-06-06) ---------------------------------------------- * Initial release. Copyright 2003, 2004, 2005, 2010 g10 Code GmbH This file is free software; as a special exception the author gives unlimited permission to copy and/or distribute it, with or without modifications, as long as this notice is preserved. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, to the extent permitted by law; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/src/argparse.c b/src/argparse.c index 512b5d7..03e033a 100644 --- a/src/argparse.c +++ b/src/argparse.c @@ -1,2431 +1,2552 @@ /* argparse.c - Argument Parser for option handling * Copyright (C) 1997-2001, 2006-2008, 2013-2017 Werner Koch * Copyright (C) 1998-2001, 2006-2008, 2012 Free Software Foundation, Inc. * Copyright (C) 2015-2020 g10 Code GmbH * * This file is part of Libgpg-error. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This file is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1-or-later * * This file was originally a part of GnuPG. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include "gpgrt-int.h" #ifdef HAVE_W32_SYSTEM # define PATHSEP_C ';' # define DIRSEP_C '\\' #else # define PATHSEP_C ':' # define DIRSEP_C '/' #endif /* The malloced configuration directories or NULL. */ static struct { char *user; char *sys; } confdir; /* Hidden argparse flag used to mark the object as initialized. */ #define ARGPARSE_FLAG__INITIALIZED (1<< ((8*SIZEOF_INT)-1)) /* Special short options which are auto-inserterd. Must fit into an * unsigned short. */ #define ARGPARSE_SHORTOPT_HELP 32768 #define ARGPARSE_SHORTOPT_VERSION 32769 #define ARGPARSE_SHORTOPT_WARRANTY 32770 #define ARGPARSE_SHORTOPT_DUMP_OPTIONS 32771 /* A mask for the types. */ #define ARGPARSE_TYPE_MASK 7 /* Mask for the type values. */ /* The states for the gpgrt_argparser machinery. */ enum argparser_states { STATE_init = 0, STATE_open_sys, STATE_open_user, STATE_open_cmdline, STATE_read_sys, STATE_read_user, STATE_read_cmdline, STATE_finished }; /* An internal object used to store the user provided option table and * some meta information. */ typedef struct { unsigned short short_opt; unsigned short ordinal; /* (for --help) */ unsigned int flags; const char *long_opt; /* Points into the user provided table. */ const char *description; /* Points into the user provided table. */ + unsigned int forced:1; /* Forced to use the sysconf value. */ + unsigned int ignore:1; /* Ignore this option everywhere but in + * the sysconf file. */ + unsigned int explicit_ignore:1; /* Ignore was explicitly set. */ } opttable_t; /* Internal object of the public gpgrt_argparse_t object. */ struct _gpgrt_argparse_internal_s { int idx; /* Note that this is saved and restored in _gpgrt_argparser. */ - int inarg; - int stopped; - int insysconfig; /* Processing global config file. */ - int explicit_confopt; /* A conffile option has been given. */ - char *explicit_conffile; /* Malloced name of an explicit conffile. */ - unsigned int opt_flags; /* Current option flags. */ - enum argparser_states state; /* of gpgrt_argparser. */ + int inarg; /* (index into args) */ + unsigned int verbose:1; /* Print diagnostics. */ + unsigned int stopped:1; /* Option processing has stopped. */ + unsigned int in_sysconf:1; /* Processing global config file. */ + unsigned int mark_forced:1; /* Mark options as forced. */ + unsigned int mark_ignore:1; /* Mark options as to be ignored. */ + unsigned int explicit_ignore:1; /* Option has explicitly been set + * to ignore or unignore. */ + unsigned int ignore_all_seen:1; /* [ignore-all] has been seen. */ + unsigned int explicit_confopt:1; /* A conffile option has been given. */ + char *explicit_conffile; /* Malloced name of an explicit + * conffile. */ + unsigned int opt_flags; /* Current option flags. */ + enum argparser_states state; /* State of the gpgrt_argparser. */ const char *last; void *aliases; const void *cur_alias; void *iio_list; estream_t conffp; char *confname; opttable_t *opts; /* Malloced option table. */ unsigned int nopts; /* Number of items in OPTS. */ }; typedef struct alias_def_s *ALIAS_DEF; struct alias_def_s { ALIAS_DEF next; char *name; /* malloced buffer with name, \0, value */ const char *value; /* ptr into name */ }; /* Object to store the names for the --ignore-invalid-option option. This is a simple linked list. */ typedef struct iio_item_def_s *IIO_ITEM_DEF; struct iio_item_def_s { IIO_ITEM_DEF next; char name[1]; /* String with the long option name. */ }; /* The almost always needed user handler for strusage. */ static const char *(*strusage_handler)( int ) = NULL; /* Optional handler to write strings. See _gpgrt_set_usage_outfnc. */ static int (*custom_outfnc) (int, const char *); /* Optional handler to map strings. See _gpgrt_set_fixed_string_mapper. */ static const char *(*fixed_string_mapper)(const char*); static int set_opt_arg (gpgrt_argparse_t *arg, unsigned int flags, char *s); static void show_help (opttable_t *opts, unsigned int nopts,unsigned int flags); static void show_version (void); static int writestrings (int is_error, const char *string, ...) GPGRT_ATTR_SENTINEL(0); static int arg_parse (gpgrt_argparse_t *arg, gpgrt_opt_t *opts, int no_init); /* Return true if the native charset is utf-8. */ static int is_native_utf8 (void) { static char result; if (!result) { const char *p = _gpgrt_strusage (8); if (!p || !*p || !strcmp (p, "utf-8")) result = 1; result |= 128; } return (result & 1); } static char * trim_spaces (char *str) { char *string, *p, *mark; string = str; /* Find first non space character. */ for (p=string; *p && isspace (*(unsigned char*)p) ; p++) ; /* Move characters. */ for ((mark = NULL); (*string = *p); string++, p++) if (isspace (*(unsigned char*)p)) { if (!mark) mark = string; } else mark = NULL; if (mark) *mark = '\0' ; /* Remove trailing spaces. */ return str ; } static const char * map_fixed_string (const char *string) { return fixed_string_mapper? fixed_string_mapper (string) : string; } /* Write STRING and all following const char * arguments either to stdout or, if IS_ERROR is set, to stderr. The list of strings must be terminated by a NULL. */ static int writestrings (int is_error, const char *string, ...) { va_list arg_ptr; const char *s; int count = 0; if (string) { s = string; va_start (arg_ptr, string); do { if (custom_outfnc) custom_outfnc (is_error? 2:1, s); else fputs (s, is_error? stderr : stdout); count += strlen (s); } while ((s = va_arg (arg_ptr, const char *))); va_end (arg_ptr); } return count; } static void flushstrings (int is_error) { if (custom_outfnc) custom_outfnc (is_error? 2:1, NULL); else _gpgrt_fflush (is_error? es_stderr : es_stdout); } static void deinitialize (gpgrt_argparse_t *arg) { if (arg->internal) { xfree (arg->internal->explicit_conffile); xfree (arg->internal->opts); xfree (arg->internal); arg->internal = NULL; } arg->flags &= ARGPARSE_FLAG__INITIALIZED; arg->lineno = 0; arg->err = 0; } /* Our own exit handler to clean up used memory. */ static void my_exit (gpgrt_argparse_t *arg, int code) { deinitialize (arg); exit (code); } static gpg_err_code_t initialize (gpgrt_argparse_t *arg, gpgrt_opt_t *opts, estream_t fp) { /* We use a dedicated flag to detect whether *ARG has been * initialized. This is because the old version of that struct, as * used in GnuPG, had no requirement to zero out all fields of the * object and existing code still sets only argc,argv and flags. */ if (!(arg->flags & ARGPARSE_FLAG__INITIALIZED) || (arg->flags & ARGPARSE_FLAG_RESET) || !arg->internal) { /* Allocate internal data. */ if (!(arg->flags & ARGPARSE_FLAG__INITIALIZED) || !arg->internal) { arg->internal = xtrymalloc (sizeof *arg->internal); if (!arg->internal) return _gpg_err_code_from_syserror (); arg->flags |= ARGPARSE_FLAG__INITIALIZED; /* Mark as initialized. */ } else if (arg->internal->opts) xfree (arg->internal->opts); arg->internal->opts = NULL; arg->internal->nopts = 0; /* Initialize this instance. */ arg->internal->idx = 0; arg->internal->last = NULL; arg->internal->inarg = 0; arg->internal->stopped = 0; - arg->internal->insysconfig = 0; + arg->internal->in_sysconf = 0; + arg->internal->mark_forced = 0; + arg->internal->mark_ignore = 0; + arg->internal->explicit_ignore = 0; + arg->internal->ignore_all_seen = 0; arg->internal->explicit_confopt = 0; arg->internal->explicit_conffile = NULL; arg->internal->opt_flags = 0; arg->internal->state = STATE_init; arg->internal->aliases = NULL; arg->internal->cur_alias = NULL; arg->internal->iio_list = NULL; arg->internal->conffp = NULL; arg->internal->confname = NULL; /* Clear the copy of the option list. */ /* Clear the error indicator. */ arg->err = 0; /* Usually an option file will be parsed from the start. * However, we do not open the stream and thus we have no way to * know the current lineno. Using this flag we can allow the * user to provide a lineno which we don't reset. */ if (fp || arg->internal->conffp || !(arg->flags & ARGPARSE_FLAG_NOLINENO)) arg->lineno = 0; /* Need to clear the reset request. */ arg->flags &= ~ARGPARSE_FLAG_RESET; /* Check initial args. */ if ( *arg->argc < 0 ) _gpgrt_log_bug ("invalid argument passed to gpgrt_argparse\n"); } /* Create an array with pointers to the provided list of options. * Keeping a copy is useful to sort that array and thus do a binary * search and to allow for extra space at the end to insert the * hidden options. An ARGPARSE_FLAG_RESET can be used to reinit * this array. */ if (!arg->internal->opts) { int seen_help = 0; int seen_version = 0; int seen_warranty = 0; int seen_dump_options = 0; int i; for (i=0; opts[i].short_opt; i++) { if (opts[i].long_opt) { if (!strcmp(opts[i].long_opt, "help")) seen_help = 1; else if (!strcmp(opts[i].long_opt, "version")) seen_version = 1; else if (!strcmp(opts[i].long_opt, "warranty")) seen_warranty = 1; else if (!strcmp(opts[i].long_opt, "dump-options")) seen_dump_options = 1; } } i += 4; /* The number of the above internal options. */ i++; /* End of list marker. */ arg->internal->opts = xtrycalloc (i, sizeof *arg->internal->opts); if (!arg->internal->opts) return _gpg_err_code_from_syserror (); for(i=0; opts[i].short_opt; i++) { arg->internal->opts[i].short_opt = opts[i].short_opt; arg->internal->opts[i].flags = opts[i].flags; arg->internal->opts[i].long_opt = opts[i].long_opt; arg->internal->opts[i].description = opts[i].description; arg->internal->opts[i].ordinal = i; } if (!seen_help) { arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_HELP; arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; arg->internal->opts[i].long_opt = "help"; arg->internal->opts[i].description = "@"; arg->internal->opts[i].ordinal = i; i++; } if (!seen_version) { arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_VERSION; arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; arg->internal->opts[i].long_opt = "version"; arg->internal->opts[i].description = "@"; arg->internal->opts[i].ordinal = i; i++; } if (!seen_warranty) { arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_WARRANTY; arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; arg->internal->opts[i].long_opt = "warranty"; arg->internal->opts[i].description = "@"; arg->internal->opts[i].ordinal = i; i++; } if (!seen_dump_options) { arg->internal->opts[i].short_opt = ARGPARSE_SHORTOPT_DUMP_OPTIONS; arg->internal->opts[i].flags = ARGPARSE_TYPE_NONE; arg->internal->opts[i].long_opt = "dump-options"; arg->internal->opts[i].description = "@"; arg->internal->opts[i].ordinal = i; i++; } arg->internal->opts[i].short_opt = 0; /* Note that we do not count the end marker but keep it in the * table anyway as an extra item. */ arg->internal->nopts = i; } if (arg->err) { /* Last option was erroneous. */ const char *s; if (!fp && arg->internal->conffp) fp = arg->internal->conffp; if (fp) { if ( arg->r_opt == ARGPARSE_UNEXPECTED_ARG ) s = _("argument not expected"); else if ( arg->r_opt == ARGPARSE_READ_ERROR ) s = _("read error"); else if ( arg->r_opt == ARGPARSE_KEYWORD_TOO_LONG ) s = _("keyword too long"); else if ( arg->r_opt == ARGPARSE_MISSING_ARG ) s = _("missing argument"); else if ( arg->r_opt == ARGPARSE_INVALID_ARG ) s = _("invalid argument"); else if ( arg->r_opt == ARGPARSE_INVALID_COMMAND ) s = _("invalid command"); else if ( arg->r_opt == ARGPARSE_INVALID_ALIAS ) s = _("invalid alias definition"); else if ( arg->r_opt == ARGPARSE_PERMISSION_ERROR ) s = _("permission error"); else if ( arg->r_opt == ARGPARSE_OUT_OF_CORE ) s = _("out of core"); else if ( arg->r_opt == ARGPARSE_NO_CONFFILE ) s = NULL; /* Error has already been printed. */ else if ( arg->r_opt == ARGPARSE_INVALID_META ) s = _("invalid meta command"); else if ( arg->r_opt == ARGPARSE_UNKNOWN_META ) s = _("unknown meta command"); else if ( arg->r_opt == ARGPARSE_UNEXPECTED_META ) s = _("unexpected meta command"); else s = _("invalid option"); if (s) _gpgrt_log_error ("%s:%u: %s\n", _gpgrt_fname_get (fp), arg->lineno, s); } else { s = arg->internal->last? arg->internal->last:"[??]"; if ( arg->r_opt == ARGPARSE_MISSING_ARG ) _gpgrt_log_error (_("missing argument for option \"%.50s\"\n"), s); else if ( arg->r_opt == ARGPARSE_INVALID_ARG ) _gpgrt_log_error (_("invalid argument for option \"%.50s\"\n"), s); else if ( arg->r_opt == ARGPARSE_UNEXPECTED_ARG ) _gpgrt_log_error (_("option \"%.50s\" does not expect " "an argument\n"), s); else if ( arg->r_opt == ARGPARSE_INVALID_COMMAND ) _gpgrt_log_error (_("invalid command \"%.50s\"\n"), s); else if ( arg->r_opt == ARGPARSE_AMBIGUOUS_OPTION ) _gpgrt_log_error (_("option \"%.50s\" is ambiguous\n"), s); else if ( arg->r_opt == ARGPARSE_AMBIGUOUS_COMMAND ) _gpgrt_log_error (_("command \"%.50s\" is ambiguous\n"),s ); else if ( arg->r_opt == ARGPARSE_OUT_OF_CORE ) _gpgrt_log_error ("%s\n", _("out of core")); else if ( arg->r_opt == ARGPARSE_PERMISSION_ERROR ) _gpgrt_log_error ("%s\n", _("permission error")); else if ( arg->r_opt == ARGPARSE_NO_CONFFILE) ; /* Error has already been printed. */ else if ( arg->r_opt == ARGPARSE_INVALID_META ) _gpgrt_log_error ("%s\n", _("invalid meta command")); else if ( arg->r_opt == ARGPARSE_UNKNOWN_META ) _gpgrt_log_error ("%s\n", _("unknown meta command")); else if ( arg->r_opt == ARGPARSE_UNEXPECTED_META ) _gpgrt_log_error ("%s\n",_("unexpected meta command")); else _gpgrt_log_error (_("invalid option \"%.50s\"\n"), s); } if (arg->err != ARGPARSE_PRINT_WARNING) my_exit (arg, 2); arg->err = 0; } /* Zero out the return value union. */ arg->r.ret_str = NULL; arg->r.ret_long = 0; return 0; } static void store_alias( gpgrt_argparse_t *arg, char *name, char *value ) { /* TODO: replace this dummy function with a rea one * and fix the probelms IRIX has with (ALIAS_DEV)arg.. * used as lvalue */ (void)arg; (void)name; (void)value; #if 0 ALIAS_DEF a = xmalloc( sizeof *a ); a->name = name; a->value = value; a->next = (ALIAS_DEF)arg->internal->aliases; (ALIAS_DEF)arg->internal->aliases = a; #endif } /* Return true if KEYWORD is in the ignore-invalid-option list. */ static int ignore_invalid_option_p (gpgrt_argparse_t *arg, const char *keyword) { IIO_ITEM_DEF item = arg->internal->iio_list; for (; item; item = item->next) if (!strcmp (item->name, keyword)) return 1; return 0; } /* Add the keywords up to the next LF to the list of to be ignored options. After returning FP will either be at EOF or the next character read wll be the first of a new line. The function returns 0 on success or true on malloc failure. */ static int ignore_invalid_option_add (gpgrt_argparse_t *arg, estream_t fp) { IIO_ITEM_DEF item; int c; char name[100]; int namelen = 0; int ready = 0; enum { skipWS, collectNAME, skipNAME, addNAME} state = skipWS; while (!ready) { c = _gpgrt_fgetc (fp); if (c == '\n') ready = 1; else if (c == EOF) { c = '\n'; ready = 1; } again: switch (state) { case skipWS: if (!isascii (c) || !isspace(c)) { namelen = 0; state = collectNAME; goto again; } break; case collectNAME: if (isspace (c)) { state = addNAME; goto again; } else if (namelen < DIM(name)-1) name[namelen++] = c; else /* Too long. */ state = skipNAME; break; case skipNAME: if (isspace (c)) { state = skipWS; goto again; } break; case addNAME: name[namelen] = 0; if (!ignore_invalid_option_p (arg, name)) { item = xtrymalloc (sizeof *item + namelen); if (!item) return 1; strcpy (item->name, name); item->next = (IIO_ITEM_DEF)arg->internal->iio_list; arg->internal->iio_list = item; } state = skipWS; goto again; } } return 0; } /* Clear the entire ignore-invalid-option list. */ static void ignore_invalid_option_clear (gpgrt_argparse_t *arg) { IIO_ITEM_DEF item, tmpitem; for (item = arg->internal->iio_list; item; item = tmpitem) { tmpitem = item->next; xfree (item); } arg->internal->iio_list = NULL; } /* Implementation of the "user" and "group" commands. ARG is the * context. A value of 0 for ALTERNATE requests the "user" command, a * value of "1" the "group" command. ARGS is a non-empty string which * this function is allowed to modify. */ static int handle_meta_user (gpgrt_argparse_t *arg, unsigned int alternate, char *args) { (void)args; - _gpgrt_log_info ("%s:%u: meta command %s is not yet supported\n", - arg->internal->confname, arg->lineno, - alternate? "group":"user"); + if (arg->internal->verbose) + _gpgrt_log_info ("%s:%u: meta command %s is not yet supported\n", + arg->internal->confname, arg->lineno, + alternate? "group":"user"); return 0; } /* Implementation of the "force" command. ARG is the context. A * value of 0 for ALTERNATE is "force", a value of 1 requests an * unforce". ARGS is the empty string and not used. */ static int handle_meta_force (gpgrt_argparse_t *arg, unsigned int alternate, char *args) { - (void)arg; - (void)alternate; (void)args; + arg->internal->mark_forced = alternate? 0 : 1; + return 0; } /* Implementation of the "ignore" command. ARG is the context. A * value of 0 for ALTERNATE is a plain "ignore", a value of 1 request - * an "unignore, a value of 3 requests an "ignore-all". ARGS is the + * an "unignore, a value of 2 requests an "ignore-all". ARGS is the * empty string and not used. */ static int handle_meta_ignore (gpgrt_argparse_t *arg, unsigned int alternate, char *args) { - (void)arg; - (void)alternate; (void)args; + if (!alternate) + { + arg->internal->mark_ignore = 1; + arg->internal->explicit_ignore = 1; + } + else if (alternate == 1) + { + arg->internal->mark_ignore = 0; + arg->internal->explicit_ignore = 1; + } + else + arg->internal->ignore_all_seen = 1; + return 0; } -/* Implementation of the "ignore" command. ARG is the context. - * ALTERNATE is not used. ARGS is the string to log. */ +/* Implementation of the "ignore" command. ARG is the context. If + * ALTERNATE is true the filename is not printed. ARGS is the string + * to log. */ static int handle_meta_echo (gpgrt_argparse_t *arg, unsigned int alternate, char *args) { - (void)alternate; + if (alternate) + _gpgrt_log_info ("%s\n", args); + else + _gpgrt_log_info ("%s:%u: %s\n", + arg->internal->confname, arg->lineno, args); + return 0; +} + + +/* Implementation of the "verbose" command. ARG is the context. If + * ALTERNATE is true the verbosity is disabled. ARGS is not used. */ +static int +handle_meta_verbose (gpgrt_argparse_t *arg, unsigned int alternate, char *args) +{ + (void)args; - _gpgrt_log_info ("%s:%u: %s\n", - arg->internal->confname, arg->lineno, args); + if (alternate) + arg->internal->verbose = 0; + else + arg->internal->verbose = 1; return 0; } /* Handle a meta command. KEYWORD has the content inside the brackets * with leading and trailing spaces removed. The function may modify * KEYWORD. On success 0 is returned, on error an ARGPARSE_ error * code is returned. */ static int handle_metacmd (gpgrt_argparse_t *arg, char *keyword) { static struct { - const char *name; /* Name of the command. */ + const char *name; /* Name of the command. */ unsigned short alternate; /* Use alternate version of the command. */ - unsigned short needarg; /* Command requires an argument. */ + unsigned short needarg:1; /* Command requires an argument. */ + unsigned short always:1; /* Command allowed in all conf files. */ int (*func)(gpgrt_argparse_t *arg, unsigned int alternate, char *args); /*handler*/ } cmds[] = - {{ "user", 0, 1, handle_meta_user }, - { "group", 1, 1, handle_meta_user }, - { "force", 0, 0, handle_meta_force }, - { "+force", 0, 0, handle_meta_force }, - { "-force", 1, 0, handle_meta_force }, - { "ignore", 0, 0, handle_meta_ignore }, - { "+ignore", 0, 0, handle_meta_ignore }, - { "-ignore", 1, 0, handle_meta_ignore }, - { "ignore-all", 2, 0, handle_meta_ignore }, - { "+ignore-all", 2, 0, handle_meta_ignore }, - { "echo", 0, 1, handle_meta_echo } + {{ "user", 0, 1, 0, handle_meta_user }, + { "group", 1, 1, 0, handle_meta_user }, + { "force", 0, 0, 0, handle_meta_force }, + { "+force", 0, 0, 0, handle_meta_force }, + { "-force", 1, 0, 0, handle_meta_force }, + { "ignore", 0, 0, 0, handle_meta_ignore }, + { "+ignore", 0, 0, 0, handle_meta_ignore }, + { "-ignore", 1, 0, 0, handle_meta_ignore }, + { "ignore-all", 2, 0, 0, handle_meta_ignore }, + { "+ignore-all", 2, 0, 0, handle_meta_ignore }, + { "verbose", 0, 0, 1, handle_meta_verbose }, + { "+verbose", 0, 0, 1, handle_meta_verbose }, + { "-verbose", 1, 0, 1, handle_meta_verbose }, + { "echo", 0, 1, 1, handle_meta_echo }, + { "-echo", 1, 1, 1, handle_meta_echo } }; char *rest; int i; - _gpgrt_log_debug ("Handle meta command '%s'\n", keyword); - for (rest = keyword; *rest && !(isascii (*rest) && isspace (*rest)); rest++) ; if (*rest) { *rest++ = 0; trim_spaces (rest); } for (i=0; i < DIM (cmds); i++) if (!strcmp (cmds[i].name, keyword)) break; if (!(i < DIM (cmds))) return ARGPARSE_UNKNOWN_META; if (cmds[i].needarg && !*rest) return ARGPARSE_MISSING_ARG; if (!cmds[i].needarg && *rest) return ARGPARSE_UNEXPECTED_ARG; + if (!arg->internal->in_sysconf && !cmds[i].always) + return ARGPARSE_UNEXPECTED_META; + return cmds[i].func (arg, cmds[i].alternate, rest); } /**************** * Get options from a file. * Lines starting with '#' are comment lines. * Syntax is simply a keyword and the argument. * Valid keywords are all keywords from the long_opt list without * the leading dashes. The special keywords "help", "warranty" and "version" * are not valid here. * The special keyword "alias" may be used to store alias definitions, * which are later expanded like long options. * The option * ignore-invalid-option OPTIONNAMEs * is recognized and updates a list of option which should be ignored if they * are not defined. * Caller must free returned strings. * If called with FP set to NULL command line args are parse instead. * * Q: Should we allow the syntax * keyword = value * and accept for boolean options a value of 1/0, yes/no or true/false? * Note: Abbreviation of options is here not allowed. */ int _gpgrt_argparse (estream_t fp, gpgrt_argparse_t *arg, gpgrt_opt_t *opts_orig) { enum { Ainit, Acomment, /* In a comment line. */ Acopykeyword, /* Collecting a keyword. */ Awaitarg, /* Wait for an argument. */ Acopyarg, /* Copy the argument. */ Akeyword_eol, /* Got keyword at end of line. */ Akeyword_spc, /* Got keyword at space. */ Acopymetacmd, /* Copy a meta command. */ Askipmetacmd, /* Skip spaces after metacmd. */ Askipmetacmd2,/* Skip comment after metacmd. */ Ametacmd, /* Process the metacmd. */ Askipandleave /* Skip the rest of the line and then leave. */ } state; opttable_t *opts; unsigned int nopts; int i, c; int idx = 0; char keyword[100]; char *buffer = NULL; size_t buflen = 0; int in_alias=0; int unread_buf[3]; /* We use an int so that we can store EOF. */ int unread_buf_count = 0; if (arg && !opts_orig) { deinitialize (arg); return 0; } if (!fp) /* Divert to arg_parse() in this case. */ return arg_parse (arg, opts_orig, 0); if (initialize (arg, opts_orig, fp)) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); opts = arg->internal->opts; nopts = arg->internal->nopts; /* If the LINENO is zero we assume that we are at the start of a * file and we skip over a possible Byte Order Mark. */ if (!arg->lineno) { unread_buf[0] = _gpgrt_fgetc (fp); unread_buf[1] = _gpgrt_fgetc (fp); unread_buf[2] = _gpgrt_fgetc (fp); if (unread_buf[0] != 0xef || unread_buf[1] != 0xbb || unread_buf[2] != 0xbf) unread_buf_count = 3; } arg->internal->opt_flags = 0; /* Find the next keyword. */ state = Ainit; i = 0; for (;;) { nextstate: /* Before scanning the next char handle the keyword seen states. */ if (state == Akeyword_eol || state == Akeyword_spc) { + /* We are either at the end of a line or right after a + * keyword. In the latter case we need to find the keyword + * so that we can decide whether an argument is required. */ + /* Check the keyword. */ - for (i=0; i < nopts; i++ ) + for (idx=0; idx < nopts; idx++ ) { - if (opts[i].long_opt && !strcmp (opts[i].long_opt, keyword)) + if (opts[idx].long_opt && !strcmp (opts[idx].long_opt, keyword)) break; } - idx = i; arg->r_opt = opts[idx].short_opt; - if ((opts[idx].flags & ARGPARSE_OPT_IGNORE)) - { - /* Option is configured to be ignored. Start from - * scratch (new line) or process like a comment. */ - state = state == Akeyword_eol? Ainit : Acomment; - i = 0; - } - else if (!(i < nopts)) + if (!(idx < nopts)) { - /* The option is not known - check for internal keywords. */ + /* The option (keyword) is not known - check for + * internal keywords before returning an error. */ if (state == Akeyword_spc && !strcmp (keyword, "alias")) { in_alias = 1; state = Awaitarg; } else if (!strcmp (keyword, "ignore-invalid-option")) { /* We might have keywords as argument - add them to * the list of ignored keywords. Note that we * ignore empty argument lists and thus do not to - * call the function in the Akeyword_eol stae. */ + * call the function in the Akeyword_eol state. */ if (state == Akeyword_spc) { if (ignore_invalid_option_add (arg, fp)) { arg->r_opt = ARGPARSE_OUT_OF_CORE; goto leave; } arg->lineno++; } state = Ainit; i = 0; } else if (ignore_invalid_option_p (arg, keyword)) { /* This invalid option is already in the iio list. */ state = state == Akeyword_eol? Ainit : Acomment; i = 0; } else { arg->r_opt = ((opts[idx].flags & ARGPARSE_OPT_COMMAND) ? ARGPARSE_INVALID_COMMAND : ARGPARSE_INVALID_OPTION); if (state == Akeyword_spc) - { - state = Askipandleave; - } + state = Askipandleave; + else + goto leave; } } else if (state == Akeyword_spc) { /* Known option but need to scan for args. */ state = Awaitarg; } - else + else if ((opts[idx].flags & ARGPARSE_OPT_IGNORE)) + { + /* Known option is configured to be ignored. Start from + * scratch (new line) or process like a comment. */ + state = state == Akeyword_eol? Ainit : Acomment; + i = 0; + } + else /* Known option */ { - /* Known option and at end of line - return option. */ + if (arg->internal->in_sysconf) + { + /* Set the current forced and ignored attributes. */ + if (arg->internal->mark_forced) + opts[idx].forced = 1; + if (arg->internal->mark_ignore) + opts[idx].ignore = 1; + if (arg->internal->explicit_ignore) + opts[idx].explicit_ignore = 1; + } + else /* Non-sysconf file */ + { /* Act upon the forced and ignored attributes. */ + if (opts[idx].ignore || opts[idx].forced) + { + if (arg->internal->verbose) + _gpgrt_log_info ("%s:%u: ignoring option \"--%s\"" + " due to attributes:%s%s\n", + arg->internal->confname, + arg->lineno, + opts[idx].long_opt, + opts[idx].forced? " forced":"", + opts[idx].ignore? " ignore":""); + state = Ainit; + i = 0; + goto nextstate; /* Ignore this one. */ + } + } + + arg->r_opt = opts[idx].short_opt; if (!(opts[idx].flags & ARGPARSE_TYPE_MASK)) arg->r_type = 0; /* Does not take an arg. */ else if ((opts[idx].flags & ARGPARSE_OPT_OPTIONAL) ) arg->r_type = 0; /* Arg is optional. */ else arg->r_opt = ARGPARSE_MISSING_ARG; goto leave; } } /* (end state Akeyword_eol/Akeyword_spc) */ else if (state == Ametacmd) { + /* We are at the end of a line. */ gpgrt_assert (*keyword == '['); trim_spaces (keyword+1); if (!keyword[1]) { arg->r_opt = ARGPARSE_INVALID_META; /* Empty. */ goto leave; } - if (!arg->internal->insysconfig) - { - arg->r_opt = ARGPARSE_UNEXPECTED_META; - goto leave; - } c = handle_metacmd (arg, keyword+1); if (c) { arg->r_opt = c; /* Return error. */ goto leave; } state = Ainit; i = 0; } /* Get the next character from the line. */ if (unread_buf_count) c = unread_buf[3 - unread_buf_count--]; else c = _gpgrt_fgetc (fp); if (c == '\n' || c== EOF ) { /* Handle end of line. */ if ( c != EOF ) arg->lineno++; if (state == Askipandleave) goto leave; else if (state == Acopykeyword) { keyword[i] = 0; state = Akeyword_eol; goto nextstate; } else if (state == Acopymetacmd) { arg->r_opt = ARGPARSE_INVALID_META; /* "]" missing */ goto leave; } else if (state == Askipmetacmd || state == Askipmetacmd2) { state = Ametacmd; goto nextstate; } else if (state == Awaitarg) { /* No argument found at the end of the line. */ if (in_alias) arg->r_opt = ARGPARSE_MISSING_ARG; else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK)) arg->r_type = 0; /* Does not take an arg. */ else if ((opts[idx].flags & ARGPARSE_OPT_OPTIONAL)) arg->r_type = 0; /* No optional argument. */ else arg->r_opt = ARGPARSE_MISSING_ARG; goto leave; } else if (state == Acopyarg) { /* Has an argument at the end of a line. */ if (in_alias) { if (!buffer) arg->r_opt = ARGPARSE_UNEXPECTED_ARG; else { char *p; buffer[i] = 0; p = strpbrk (buffer, " \t"); if (p) { *p++ = 0; trim_spaces (p); } if (!p || !*p) { xfree (buffer); arg->r_opt = ARGPARSE_INVALID_ALIAS; } else { store_alias (arg, buffer, p); } } } else if (!(opts[idx].flags & ARGPARSE_TYPE_MASK)) arg->r_opt = ARGPARSE_UNEXPECTED_ARG; else { char *p; if (!buffer) { keyword[i] = 0; buffer = xtrystrdup (keyword); if (!buffer) arg->r_opt = ARGPARSE_OUT_OF_CORE; } else buffer[i] = 0; if (buffer) { trim_spaces (buffer); p = buffer; if (*p == '"') { /* Remove quotes. */ p++; if (*p && p[strlen(p)-1] == '\"' ) p[strlen(p)-1] = 0; } if (!set_opt_arg (arg, opts[idx].flags, p)) xfree (buffer); else gpgrt_annotate_leaked_object (buffer); } } goto leave; } else if (c == EOF) { ignore_invalid_option_clear (arg); if (_gpgrt_ferror (fp)) arg->r_opt = ARGPARSE_READ_ERROR; else arg->r_opt = 0; /* EOF. */ goto leave; } state = Ainit; i = 0; } /* (end handle end of line) */ else if (state == Askipandleave) ; /* Skip. */ else if (state == Ainit && isascii (c) && isspace(c)) ; /* Skip leading white space. */ else if (state == Ainit && c == '#' ) state = Acomment; /* Start of a comment. */ else if (state == Acomment || state == Askipmetacmd2) ; /* Skip comments. */ else if (state == Askipmetacmd) { if (c == '#') state = Askipmetacmd2; else if (!(isascii (c) && isspace(c))) { arg->r_opt = ARGPARSE_INVALID_META; state = Askipandleave; } } else if (state == Acopykeyword && isascii (c) && isspace(c)) { keyword[i] = 0; state = Akeyword_spc; goto nextstate; } else if (state == Acopymetacmd && c == ']') { keyword[i] = 0; state = Askipmetacmd; goto nextstate; } else if (state == Awaitarg) { /* Skip leading spaces of the argument. */ if (!isascii (c) || !isspace(c)) { i = 0; keyword[i++] = c; state = Acopyarg; } } else if (state == Acopyarg) { /* Collect the argument. */ if (buffer) { if (i < buflen-1) buffer[i++] = c; else { char *tmp; size_t tmplen = buflen + 50; tmp = xtryrealloc (buffer, tmplen); if (tmp) { buflen = tmplen; buffer = tmp; buffer[i++] = c; } else { xfree (buffer); arg->r_opt = ARGPARSE_OUT_OF_CORE; goto leave; } } } else if (i < DIM(keyword)-1) keyword[i++] = c; else { size_t tmplen = DIM(keyword) + 50; buffer = xtrymalloc (tmplen); if (buffer) { buflen = tmplen; memcpy(buffer, keyword, i); buffer[i++] = c; } else { arg->r_opt = ARGPARSE_OUT_OF_CORE; goto leave; } } } else if (i >= DIM(keyword)-1) { arg->r_opt = ARGPARSE_KEYWORD_TOO_LONG; state = Askipandleave; /* Skip rest of line and leave. */ } else if (!i) { state = c == '[' ? Acopymetacmd : Acopykeyword; keyword[i++] = c; } else { keyword[i++] = c; } } leave: return arg->r_opt; } /* Return true if the list of options OPTS has any option marked with * ARGPARSE_OPT_CONFFILE. */ static int any_opt_conffile (opttable_t *opts, unsigned int nopts) { int i; for (i=0; i < nopts; i++ ) if ((opts[i].flags & ARGPARSE_OPT_CONFFILE)) return 1; return 0; } /* Return true if FNAME is an absoluete filename. */ static int is_absfname (const char *fname) { const char *s; #ifdef HAVE_W32_SYSTEM s = strchr (fname, ':'); if (s) s++; else s = fname; #else s = fname; #endif return (*s == '/' #ifdef HAVE_W32_SYSTEM || *s == DIRSEP_C #endif ); } /* If FNAME specifies two files of the form * NAME1:/NAME2 (Unix) * or * NAME1;[x:]/NAME2 (Windows) * return a pointer to the delimiter or NULL if there is none. */ static const char * is_twopartfname (const char *fname) { const char *s; if ((s = strchr (fname, PATHSEP_C)) && is_absfname (s+1) && s != fname) return s; return NULL; } - /* Try to use a version-ed config file name. A version-ed config file * name is one which has the packages version number appended. For * example if the standard config file name is "foo.conf" and the * version of the foo program is 1.2.3-beta1 the following config * files are tried in order until one is readable: * * foo.conf-1.2.3-beta1 * foo.conf-1.2.3 * foo.conf-1.2 * foo.conf-1 * foo.conf * * The argument CONFIGNAME should already be expanded. On success a * newly allocated file name is returned. On error NULL is returned. */ static char * try_versioned_conffile (const char *configname) { const char *version = _gpgrt_strusage (13); char *name; char *dash, *endp; if (!version || !*version) return NULL; /* No program version known. */ name = _gpgrt_strconcat (configname, "-", version, NULL); if (!name) return NULL; /* Oops: Out of core - ignore. */ dash = name + strlen (configname); endp = dash + strlen (dash) - 1; while (endp > dash) { if (!access (name, R_OK)) { return name; } for (; endp > dash; endp--) { if (*endp == '-' || *endp == '.') { *endp = 0; break; } } } _gpgrt_free (name); return NULL; } +/* This function is called after a sysconf file has been read. */ +static void +finish_read_sys (gpgrt_argparse_t *arg) +{ + opttable_t *opts = arg->internal->opts; + unsigned int nopts = arg->internal->nopts; + int i; + + if (arg->internal->ignore_all_seen) + { + /* [ignore-all] was used: Set all options which have not + * explictly been set as ignore or not ignore to ignore. */ + for (i = 0; i < nopts; i++) + { + if (!opts[i].explicit_ignore) + opts[i].ignore = 1; + } + } + + /* Reset all flags which pertain only to sysconf files. */ + arg->internal->in_sysconf = 0; + arg->internal->mark_forced = 0; + arg->internal->mark_ignore = 0; + arg->internal->explicit_ignore = 0; + arg->internal->ignore_all_seen = 0; +} + + /* The full arg parser which handles option files and command line * arguments. The behaviour depends on the combinations of CONFNAME * and the ARGPARSE_FLAG_xxx values: * * | CONFNAME | SYS | USER | Action | * |----------+-----+------+--------------------| * | NULL | - | - | cmdline | * | string | 0 | 1 | user, cmdline | * | string | 1 | 0 | sys, cmdline | * | string | 1 | 1 | sys, user, cmdline | * * Note that if an option has been flagged with ARGPARSE_OPT_CONFFILE * and a type of ARGPARSE_TYPE_STRING that option is not returned but * the specified configuration file is processed directly; if * ARGPARSE_TYPE_NONE is used no user configuration files are * processed and from the system configuration files only those which * are immutable are processed. The string values for CONFNAME shall * not include a directory part because that is taken from the values * set by gpgrt_set_confdir. However, if CONFNAME is a twopart * filename delimited by a colon (semicolon on Windows) with the * second part being an absolute filename, the first part is used for * the SYS file and the the entire second part for the USER file. */ int _gpgrt_argparser (gpgrt_argparse_t *arg, gpgrt_opt_t *opts, const char *confname) { /* First check whether releasing the resources has been requested. */ if (arg && !opts) { deinitialize (arg); return 0; } /* Make sure that the internal data object is ready and also print * warnings or errors from the last iteration. */ if (initialize (arg, opts, NULL)) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); next_state: switch (arg->internal->state) { case STATE_init: if (arg->argc && arg->argv && *arg->argc && any_opt_conffile (arg->internal->opts, arg->internal->nopts)) { /* The list of option allow for conf files * (e.g. gpg's "--option FILE" and "--no-options") * Now check whether one was really given on the command * line. Note that we don't need to run this code if no * argument array was provided. */ int save_argc = *arg->argc; char **save_argv = *arg->argv; unsigned int save_flags = arg->flags; int save_idx = arg->internal->idx; int any_no_conffile = 0; arg->flags = (ARGPARSE_FLAG_KEEP | ARGPARSE_FLAG_NOVERSION | ARGPARSE_FLAG__INITIALIZED); while (arg_parse (arg, opts, 1)) { if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) { arg->internal->explicit_confopt = 1; if (arg->r_type == ARGPARSE_TYPE_STRING && !arg->internal->explicit_conffile) { /* Store the first conffile name. All further * conf file options are not handled. */ arg->internal->explicit_conffile = xtrystrdup (arg->r.ret_str); if (!arg->internal->explicit_conffile) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); } else if (arg->r_type == ARGPARSE_TYPE_NONE) any_no_conffile = 1; } } if (any_no_conffile) { /* A NoConffile option overrides any other conf file option. */ xfree (arg->internal->explicit_conffile); arg->internal->explicit_conffile = NULL; } /* Restore parser. */ *arg->argc = save_argc; *arg->argv = save_argv; arg->flags = save_flags; arg->internal->idx = save_idx; } if (confname && *confname) { if ((arg->flags & ARGPARSE_FLAG_SYS)) arg->internal->state = STATE_open_sys; else if ((arg->flags & ARGPARSE_FLAG_USER)) arg->internal->state = STATE_open_user; else return (arg->r_opt = ARGPARSE_INVALID_ARG); } else arg->internal->state = STATE_open_cmdline; goto next_state; case STATE_open_sys: { /* If it is a two part name take the first part. */ const char *s; char *tmpname = NULL; if ((s = is_twopartfname (confname))) { tmpname = xtrymalloc (s - confname + 1); if (!tmpname) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); memcpy (tmpname, confname, s-confname); tmpname[s-confname] = 0; s = tmpname; } else s = confname; xfree (arg->internal->confname); arg->internal->confname = _gpgrt_fnameconcat (confdir.sys? confdir.sys : "/etc", s, NULL); _gpgrt_free (tmpname); if (!arg->internal->confname) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); } arg->lineno = 0; arg->internal->idx = 0; + arg->internal->verbose = 0; arg->internal->stopped = 0; arg->internal->inarg = 0; _gpgrt_fclose (arg->internal->conffp); arg->internal->conffp = _gpgrt_fopen (arg->internal->confname, "r"); if (!arg->internal->conffp) { - if ((arg->flags & ARGPARSE_FLAG_VERBOSE)) + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) || arg->internal->verbose) _gpgrt_log_info (_("Note: no default option file '%s'\n"), arg->internal->confname); if ((arg->flags & ARGPARSE_FLAG_USER)) arg->internal->state = STATE_open_user; else arg->internal->state = STATE_open_cmdline; goto next_state; } - if ((arg->flags & ARGPARSE_FLAG_VERBOSE)) + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) || arg->internal->verbose) _gpgrt_log_info (_("reading options from '%s'\n"), arg->internal->confname); arg->internal->state = STATE_read_sys; - arg->internal->insysconfig = 1; + arg->internal->in_sysconf = 1; arg->r.ret_str = xtrystrdup (arg->internal->confname); if (!arg->r.ret_str) arg->r_opt = ARGPARSE_OUT_OF_CORE; else { gpgrt_annotate_leaked_object (arg->r.ret_str); arg->r_opt = ARGPARSE_CONFFILE; arg->r_type = ARGPARSE_TYPE_STRING; } break; case STATE_open_user: if (arg->internal->explicit_confopt && arg->internal->explicit_conffile) { /* An explict option to use a specific configuration file * has been given - use that one. */ xfree (arg->internal->confname); arg->internal->confname = xtrystrdup (arg->internal->explicit_conffile); if (!arg->internal->confname) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); } else if (arg->internal->explicit_confopt) { /* An explict option not to use a configuration file has * been given - leap direct to command line reading. */ arg->internal->state = STATE_open_cmdline; goto next_state; } else { /* Use the standard configure file. If it is a two part * name take the second part. If it is the standard name * and ARGPARSE_FLAG_USERVERS is set try versioned config * files. */ const char *s; char *nconf; xfree (arg->internal->confname); if ((s = is_twopartfname (confname))) { arg->internal->confname = _gpgrt_fnameconcat (s + 1, NULL); if (!arg->internal->confname) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); } else { arg->internal->confname = _gpgrt_fnameconcat (confdir.user? confdir.user : "~/.config", confname, NULL); if (!arg->internal->confname) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); if ((arg->flags & ARGPARSE_FLAG_USERVERS) && (nconf = try_versioned_conffile (arg->internal->confname))) { xfree (arg->internal->confname); arg->internal->confname = nconf; } } } arg->lineno = 0; arg->internal->idx = 0; + arg->internal->verbose = 0; arg->internal->stopped = 0; arg->internal->inarg = 0; - arg->internal->insysconfig = 0; + arg->internal->in_sysconf = 0; _gpgrt_fclose (arg->internal->conffp); arg->internal->conffp = _gpgrt_fopen (arg->internal->confname, "r"); if (!arg->internal->conffp) { arg->internal->state = STATE_open_cmdline; if (arg->internal->explicit_confopt) { _gpgrt_log_error (_("option file '%s': %s\n"), arg->internal->confname, strerror (errno)); return (arg->r_opt = ARGPARSE_NO_CONFFILE); } else { - if ((arg->flags & ARGPARSE_FLAG_VERBOSE)) + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) + || arg->internal->verbose) _gpgrt_log_info (_("Note: no default option file '%s'\n"), arg->internal->confname); goto next_state; } } - if ((arg->flags & ARGPARSE_FLAG_VERBOSE)) + if ((arg->flags & ARGPARSE_FLAG_VERBOSE) || arg->internal->verbose) _gpgrt_log_info (_("reading options from '%s'\n"), arg->internal->confname); arg->internal->state = STATE_read_user; arg->r.ret_str = xtrystrdup (arg->internal->confname); if (!arg->r.ret_str) arg->r_opt = ARGPARSE_OUT_OF_CORE; else { gpgrt_annotate_leaked_object (arg->r.ret_str); arg->r_opt = ARGPARSE_CONFFILE; arg->r_type = ARGPARSE_TYPE_STRING; } break; case STATE_open_cmdline: _gpgrt_fclose (arg->internal->conffp); arg->internal->conffp = NULL; xfree (arg->internal->confname); arg->internal->confname = NULL; arg->internal->idx = 0; + arg->internal->verbose = 0; arg->internal->stopped = 0; arg->internal->inarg = 0; - arg->internal->insysconfig = 0; + arg->internal->in_sysconf = 0; if (!arg->argc || !arg->argv || !*arg->argv) { /* No or empty argument vector - don't bother to parse things. */ arg->internal->state = STATE_finished; goto next_state; } arg->r_opt = ARGPARSE_CONFFILE; arg->r_type = ARGPARSE_TYPE_NONE; arg->r.ret_str = NULL; arg->internal->state = STATE_read_cmdline; break; case STATE_read_sys: arg->r_opt = _gpgrt_argparse (arg->internal->conffp, arg, opts); if (!arg->r_opt) { + finish_read_sys (arg); arg->internal->state = STATE_open_user; goto next_state; } if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) goto next_state; /* Already handled - again. */ break; case STATE_read_user: arg->r_opt = _gpgrt_argparse (arg->internal->conffp, arg, opts); if (!arg->r_opt) { arg->internal->state = STATE_open_cmdline; goto next_state; } if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) goto next_state; /* Already handled - again. */ break; case STATE_read_cmdline: arg->r_opt = arg_parse (arg, opts, 1); if (!arg->r_opt) { arg->internal->state = STATE_finished; goto next_state; } if ((arg->internal->opt_flags & ARGPARSE_OPT_CONFFILE)) goto next_state; /* Already handled - again. */ break; case STATE_finished: arg->r_opt = 0; break; } return arg->r_opt; } /* Given the list of options in ARG and a keyword, return the index of * the long option matching KEYWORD. On error -1 is returned for not * found or -2 for ambigious keyword. */ static int find_long_option (gpgrt_argparse_t *arg, const char *keyword) { int i; size_t n; opttable_t *opts = arg->internal->opts; unsigned int nopts = arg->internal->nopts; /* Would be better if we can do a binary search, but it is not * possible to reorder our option table because we would mess up our * help strings. What we can do is: Build an option lookup table * when this function is first invoked. The latter has already been * done. */ if (!*keyword) return -1; for (i=0; i < nopts; i++ ) if (opts[i].long_opt && !strcmp (opts[i].long_opt, keyword)) return i; #if 0 { ALIAS_DEF a; /* see whether it is an alias */ for (a = args->internal->aliases; a; a = a->next) { if (!strcmp( a->name, keyword)) { /* todo: must parse the alias here */ args->internal->cur_alias = a; return -3; /* alias available */ } } } #endif /* Not found. See whether it is an abbreviation. Aliases may not * be abbreviated, though. */ n = strlen (keyword); for (i=0; i < nopts; i++) { if (opts[i].long_opt && !strncmp (opts[i].long_opt, keyword, n)) { int j; for (j=i+1; j < nopts; j++) { if (opts[j].long_opt && !strncmp (opts[j].long_opt, keyword, n) && !(opts[j].short_opt == opts[i].short_opt && opts[j].flags == opts[i].flags ) ) return -2; /* Abbreviation is ambiguous. */ } return i; } } return -1; /* Not found. */ } /* The option parser for command line options. */ static int arg_parse (gpgrt_argparse_t *arg, gpgrt_opt_t *opts_orig, int no_init) { int idx; opttable_t *opts; unsigned int nopts; int argc; char **argv; char *s, *s2; int i; if (no_init) ; else if (initialize (arg, opts_orig, NULL)) return (arg->r_opt = ARGPARSE_OUT_OF_CORE); opts = arg->internal->opts; nopts = arg->internal->nopts; argc = *arg->argc; argv = *arg->argv; idx = arg->internal->idx; if (!idx && argc && !(arg->flags & ARGPARSE_FLAG_ARG0)) { /* Skip the first argument. */ argc--; argv++; idx++; } next_one: if (!argc) { /* No more args. */ arg->r_opt = 0; goto leave; /* Ready. */ } s = *argv; arg->internal->last = s; arg->internal->opt_flags = 0; if (arg->internal->stopped && (arg->flags & ARGPARSE_FLAG_ALL)) { arg->r_opt = ARGPARSE_IS_ARG; /* Not an option but an argument. */ arg->r_type = 2; arg->r.ret_str = s; argc--; argv++; idx++; /* set to next one */ } else if (arg->internal->stopped) { arg->r_opt = 0; goto leave; /* Ready. */ } else if ( *s == '-' && s[1] == '-' ) { /* Long option. */ char *argpos; arg->internal->inarg = 0; if (!s[2] && !(arg->flags & ARGPARSE_FLAG_NOSTOP)) { /* Stop option processing. */ arg->internal->stopped = 1; arg->flags |= ARGPARSE_FLAG_STOP_SEEN; argc--; argv++; idx++; goto next_one; } argpos = strchr( s+2, '=' ); if ( argpos ) *argpos = 0; i = find_long_option (arg, s+2); if ( argpos ) *argpos = '='; if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_HELP) show_help (opts, nopts, arg->flags); else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_VERSION) { if (!(arg->flags & ARGPARSE_FLAG_NOVERSION)) { show_version (); my_exit (arg, 0); } } else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_WARRANTY) { writestrings (0, _gpgrt_strusage (16), "\n", NULL); my_exit (arg, 0); } else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_DUMP_OPTIONS) { for (i=0; i < nopts; i++ ) { if (opts[i].long_opt && !(opts[i].flags & ARGPARSE_OPT_IGNORE)) writestrings (0, "--", opts[i].long_opt, "\n", NULL); } my_exit (arg, 0); } if ( i == -2 ) arg->r_opt = ARGPARSE_AMBIGUOUS_OPTION; else if ( i == -1 ) { arg->r_opt = ARGPARSE_INVALID_OPTION; arg->r.ret_str = s+2; } else arg->r_opt = opts[i].short_opt; + if ( i < 0 ) ; else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) ) { if ( argpos ) { s2 = argpos+1; if ( !*s2 ) s2 = NULL; } else s2 = argv[1]; + if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) { arg->r_type = ARGPARSE_TYPE_NONE; /* Argument is optional. */ } else if ( !s2 ) { arg->r_opt = ARGPARSE_MISSING_ARG; } else if ( !argpos && *s2 == '-' && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) { /* The argument is optional and the next seems to be an option. We do not check this possible option but assume no argument */ arg->r_type = ARGPARSE_TYPE_NONE; } else { set_opt_arg (arg, opts[i].flags, s2); if ( !argpos ) { argc--; argv++; idx++; /* Skip one. */ } } } else { /* Does not take an argument. */ if ( argpos ) arg->r_type = ARGPARSE_UNEXPECTED_ARG; else { arg->internal->opt_flags = opts[i].flags; arg->r_type = 0; } } argc--; argv++; idx++; /* Set to next one. */ } else if ( (*s == '-' && s[1]) || arg->internal->inarg ) { /* Short option. */ int dash_kludge = 0; i = 0; if ( !arg->internal->inarg ) { arg->internal->inarg++; if ( (arg->flags & ARGPARSE_FLAG_ONEDASH) ) { - for (i=0; opts[i].short_opt; i++ ) + for (i=0; i < nopts; i++ ) if ( opts[i].long_opt && !strcmp (opts[i].long_opt, s+1)) { dash_kludge = 1; break; } } } s += arg->internal->inarg; if (!dash_kludge ) { for (i=0; i < nopts; i++ ) if ( opts[i].short_opt == *s ) break; } if ( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) ) show_help (opts, nopts, arg->flags); arg->r_opt = opts[i].short_opt; if (!opts[i].short_opt ) { arg->r_opt = (opts[i].flags & ARGPARSE_OPT_COMMAND)? ARGPARSE_INVALID_COMMAND:ARGPARSE_INVALID_OPTION; arg->internal->inarg++; /* Point to the next arg. */ arg->r.ret_str = s; } else if ( (opts[i].flags & ARGPARSE_TYPE_MASK) ) { if ( s[1] && !dash_kludge ) { s2 = s+1; set_opt_arg (arg, opts[i].flags, s2); } else { s2 = argv[1]; if ( !s2 && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) { arg->r_type = ARGPARSE_TYPE_NONE; arg->internal->opt_flags = opts[i].flags; } else if ( !s2 ) { arg->r_opt = ARGPARSE_MISSING_ARG; } else if ( *s2 == '-' && s2[1] && (opts[i].flags & ARGPARSE_OPT_OPTIONAL) ) { /* The argument is optional and the next seems to be an option. We do not check this possible option but assume no argument. */ arg->r_type = ARGPARSE_TYPE_NONE; arg->internal->opt_flags = opts[i].flags; } else { set_opt_arg (arg, opts[i].flags, s2); argc--; argv++; idx++; /* Skip one. */ } } s = "x"; /* This is so that !s[1] yields false. */ } else { /* Does not take an argument. */ arg->r_type = ARGPARSE_TYPE_NONE; arg->internal->opt_flags = opts[i].flags; arg->internal->inarg++; /* Point to the next arg. */ } if ( !s[1] || dash_kludge ) { /* No more concatenated short options. */ arg->internal->inarg = 0; argc--; argv++; idx++; } } else if ( arg->flags & ARGPARSE_FLAG_MIXED ) { arg->r_opt = ARGPARSE_IS_ARG; arg->r_type = 2; arg->r.ret_str = s; argc--; argv++; idx++; /* Set to next one. */ } else { arg->internal->stopped = 1; /* Stop option processing. */ goto next_one; } + if (arg->r_opt > 0 && i >= 0 && i < nopts + && ((opts[i].ignore && opts[i].explicit_ignore) || opts[i].forced)) + { + _gpgrt_log_info (_("Note: ignoring option \"--%s\"" + " due to global config\n"), + opts[i].long_opt); + goto next_one; /* Skip ignored/forced option. */ + } + leave: *arg->argc = argc; *arg->argv = argv; arg->internal->idx = idx; return arg->r_opt; } /* Returns: -1 on error, 0 for an integer type and 1 for a non integer type argument. */ static int set_opt_arg (gpgrt_argparse_t *arg, unsigned flags, char *s) { int base = (flags & ARGPARSE_OPT_PREFIX)? 0 : 10; long l; arg->internal->opt_flags = flags; switch ( (arg->r_type = (flags & ARGPARSE_TYPE_MASK)) ) { case ARGPARSE_TYPE_LONG: case ARGPARSE_TYPE_INT: errno = 0; l = strtol (s, NULL, base); if ((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) { arg->r_opt = ARGPARSE_INVALID_ARG; return -1; } if (arg->r_type == ARGPARSE_TYPE_LONG) arg->r.ret_long = l; else if ( (l < 0 && l < INT_MIN) || l > INT_MAX ) { arg->r_opt = ARGPARSE_INVALID_ARG; return -1; } else arg->r.ret_int = (int)l; return 0; case ARGPARSE_TYPE_ULONG: while (isascii (*s) && isspace(*s)) s++; if (*s == '-') { arg->r.ret_ulong = 0; arg->r_opt = ARGPARSE_INVALID_ARG; return -1; } errno = 0; arg->r.ret_ulong = strtoul (s, NULL, base); if (arg->r.ret_ulong == ULONG_MAX && errno == ERANGE) { arg->r_opt = ARGPARSE_INVALID_ARG; return -1; } return 0; case ARGPARSE_TYPE_STRING: default: arg->r.ret_str = s; return 1; } } /* Return the length of the option O. This needs to consider the * description as weel as the option name. */ static size_t long_opt_strlen (opttable_t *o) { size_t n = strlen (o->long_opt); if ( o->description && *o->description == '|' ) { const char *s; int is_utf8 = is_native_utf8 (); s=o->description+1; if ( *s != '=' ) n++; /* For a (mostly) correct length calculation we exclude * continuation bytes (10xxxxxx) if we are on a native utf8 * terminal. */ for (; *s && *s != '|'; s++ ) if ( is_utf8 && (*s&0xc0) != 0x80 ) n++; } return n; } /* Qsort compare for show_help. */ static int cmp_ordtbl (const void *a_v, const void *b_v) { const unsigned short *a = a_v; const unsigned short *b = b_v; return *a - *b; } /**************** * Print formatted help. The description string has some special * meanings: * - A description string which is "@" suppresses help output for * this option * - a description,ine which starts with a '@' and is followed by * any other characters is printed as is; this may be used for examples * ans such. * - A description which starts with a '|' outputs the string between this * bar and the next one as arguments of the long option. */ static void show_help (opttable_t *opts, unsigned int nopts, unsigned int flags) { const char *s; char tmp[2]; unsigned int *ordtbl = NULL; show_version (); writestrings (0, "\n", NULL); s = _gpgrt_strusage (42); if (s && *s == '1') { s = _gpgrt_strusage (40); writestrings (1, s, NULL); if (*s && s[strlen(s)] != '\n') writestrings (1, "\n", NULL); } s = _gpgrt_strusage(41); writestrings (0, s, "\n", NULL); if ( nopts ) { /* Auto format the option description. */ int i,j, indent; ordtbl = xtrycalloc (nopts, sizeof *ordtbl); if (!ordtbl) { writestrings (1, "\nOoops: Out of memory whilst printing the help.\n", NULL); goto leave; } /* Get max. length of long options. */ for (i=indent=0; i < nopts; i++ ) { if ( opts[i].long_opt ) if ( !opts[i].description || *opts[i].description != '@' ) if ( (j=long_opt_strlen(opts+i)) > indent && j < 35 ) indent = j; ordtbl[i] = opts[i].ordinal; } qsort (ordtbl, nopts, sizeof *ordtbl, cmp_ordtbl); /* The first option needs to have a description; if not do not * print the help at all. */ if (!opts[ordtbl[0]].description) goto leave; /* Example: " -v, --verbose Viele Sachen ausgeben" */ indent += 10; if ( *opts[ordtbl[0]].description != '@' ) writestrings (0, "Options:", "\n", NULL); for (i=0; i < nopts; i++ ) { s = map_fixed_string (_( opts[ordtbl[i]].description )); if ( s && *s== '@' && !s[1] ) /* Hide this line. */ continue; if ( s && *s == '@' ) /* Unindented comment only line. */ { for (s++; *s; s++ ) { if ( *s == '\n' ) { if( s[1] ) writestrings (0, "\n", NULL); } else { tmp[0] = *s; tmp[1] = 0; writestrings (0, tmp, NULL); } } writestrings (0, "\n", NULL); continue; } j = 3; if ( opts[ordtbl[i]].short_opt < 256 ) { tmp[0] = opts[ordtbl[i]].short_opt; tmp[1] = 0; writestrings (0, " -", tmp, NULL ); if ( !opts[ordtbl[i]].long_opt ) { if (s && *s == '|' ) { writestrings (0, " ", NULL); j++; for (s++ ; *s && *s != '|'; s++, j++ ) { tmp[0] = *s; tmp[1] = 0; writestrings (0, tmp, NULL); } if ( *s ) s++; } } } else writestrings (0, " ", NULL); if ( opts[ordtbl[i]].long_opt ) { tmp[0] = opts[ordtbl[i]].short_opt < 256?',':' '; tmp[1] = 0; j += writestrings (0, tmp, " --", opts[ordtbl[i]].long_opt, NULL); if (s && *s == '|' ) { if ( *++s != '=' ) { writestrings (0, " ", NULL); j++; } for ( ; *s && *s != '|'; s++, j++ ) { tmp[0] = *s; tmp[1] = 0; writestrings (0, tmp, NULL); } if ( *s ) s++; } writestrings (0, " ", NULL); j += 3; } for (;j < indent; j++ ) writestrings (0, " ", NULL); if ( s ) { if ( *s && j > indent ) { writestrings (0, "\n", NULL); for (j=0;j < indent; j++ ) writestrings (0, " ", NULL); } for (; *s; s++ ) { if ( *s == '\n' ) { if ( s[1] ) { writestrings (0, "\n", NULL); for (j=0; j < indent; j++ ) writestrings (0, " ", NULL); } } else { tmp[0] = *s; tmp[1] = 0; writestrings (0, tmp, NULL); } } } writestrings (0, "\n", NULL); } if ( (flags & ARGPARSE_FLAG_ONEDASH) ) writestrings (0, "\n(A single dash may be used " "instead of the double ones)\n", NULL); } if ( (s=_gpgrt_strusage(19)) ) { writestrings (0, "\n", NULL); writestrings (0, s, NULL); } leave: flushstrings (0); xfree (ordtbl); exit (0); } static void show_version () { const char *s; int i; /* Version line. */ writestrings (0, _gpgrt_strusage (11), NULL); if ((s=_gpgrt_strusage (12))) writestrings (0, " (", s, ")", NULL); writestrings (0, " ", _gpgrt_strusage (13), "\n", NULL); /* Additional version lines. */ for (i=20; i < 30; i++) if ((s=_gpgrt_strusage (i))) writestrings (0, s, "\n", NULL); /* Copyright string. */ if ((s=_gpgrt_strusage (14))) writestrings (0, s, "\n", NULL); /* Licence string. */ if( (s=_gpgrt_strusage (10)) ) writestrings (0, s, "\n", NULL); /* Copying conditions. */ if ( (s=_gpgrt_strusage(15)) ) writestrings (0, s, NULL); /* Thanks. */ if ((s=_gpgrt_strusage(18))) writestrings (0, s, NULL); /* Additional program info. */ for (i=30; i < 40; i++ ) if ( (s=_gpgrt_strusage (i)) ) writestrings (0, s, NULL); flushstrings (0); } void _gpgrt_usage (int level) { const char *p; if (!level) { writestrings (1, _gpgrt_strusage(11), " ", _gpgrt_strusage(13), "; ", _gpgrt_strusage (14), "\n", NULL); flushstrings (1); } else if (level == 1) { p = _gpgrt_strusage (40); writestrings (1, p, NULL); if (*p && p[strlen(p)] != '\n') writestrings (1, "\n", NULL); exit (2); } else if (level == 2) { p = _gpgrt_strusage (42); if (p && *p == '1') { p = _gpgrt_strusage (40); writestrings (1, p, NULL); if (*p && p[strlen(p)] != '\n') writestrings (1, "\n", NULL); } writestrings (0, _gpgrt_strusage(41), "\n", NULL); exit (0); } } /* Level * 0: Print copyright string to stderr * 1: Print a short usage hint to stderr and terminate * 2: Print a long usage hint to stdout and terminate * 8: Return NULL for UTF-8 or string with the native charset. * 9: Return the SPDX License tag. * 10: Return license info string * 11: Return the name of the program * 12: Return optional name of package which includes this program. * 13: version string * 14: copyright string * 15: Short copying conditions (with LFs) * 16: Long copying conditions (with LFs) * 17: Optional printable OS name * 18: Optional thanks list (with LFs) * 19: Bug report info *20..29: Additional lib version strings. *30..39: Additional program info (with LFs) * 40: short usage note (with LF) * 41: long usage note (with LF) * 42: Flag string: * First char is '1': * The short usage notes needs to be printed * before the long usage note. */ const char * _gpgrt_strusage (int level) { const char *p = strusage_handler? strusage_handler(level) : NULL; const char *tmp; if ( p ) return map_fixed_string (p); switch ( level ) { case 8: break; /* Default to utf-8. */ case 9: p = "GPL-3.0-or-later"; /* Suggested license. */ break; case 10: tmp = _gpgrt_strusage (9); if (tmp && !strcmp (tmp, "GPL-2.0-or-later")) p = ("License GNU GPL-2.0-or-later "); else if (tmp && !strcmp (tmp, "LGPL-2.1-or-later")) p = ("License GNU LGPL-2.1-or-later "); else /* Default to GPLv3+. */ p = ("License GNU GPL-3.0-or-later "); break; case 11: p = "foo"; break; case 13: p = "0.0"; break; case 14: p = "Copyright (C) YEAR NAME"; break; case 15: p = "This is free software: you are free to change and redistribute it.\n" "There is NO WARRANTY, to the extent permitted by law.\n"; break; case 16: tmp = _gpgrt_strusage (9); if (tmp && !strcmp (tmp, "GPL-2.0-or-later")) p = "This is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" "the Free Software Foundation; either version 2 of the License, or\n" "(at your option) any later version.\n\n" "It is distributed in the hope that it will be useful,\n" "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" "GNU General Public License for more details.\n\n" "You should have received a copy of the GNU General Public License\n" "along with this software. If not, see .\n"; else if (tmp && !strcmp (tmp, "LGPL-2.1-or-later")) p = "This is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU Lesser General Public License as\n" "published by the Free Software Foundation; either version 2.1 of\n" "the License, or (at your option) any later version.\n\n" "It is distributed in the hope that it will be useful,\n" "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" "GNU Lesser General Public License for more details.\n\n" "You should have received a copy of the GNU General Public License\n" "along with this software. If not, see .\n"; else /* Default */ p = "This is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" "the Free Software Foundation; either version 3 of the License, or\n" "(at your option) any later version.\n\n" "It is distributed in the hope that it will be useful,\n" "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" "GNU General Public License for more details.\n\n" "You should have received a copy of the GNU General Public License\n" "along with this software. If not, see .\n"; break; case 40: /* short and long usage */ case 41: p = ""; break; } return p; } /* Set the usage handler. This function is basically a constructor. */ void _gpgrt_set_strusage (const char *(*f)(int) ) { strusage_handler = f; } /* Set a function to write strings which is then used instead of * estream. The first arg of that function is MODE and the second the * STRING to write. A mode of 1 is used for writing to stdout and a * mode of 2 to write to stderr. Other modes are reserved and should * not output anything. A NULL for STRING requests a flush. */ void _gpgrt_set_usage_outfnc (int (*f)(int, const char *)) { custom_outfnc = f; } /* Register function F as a string mapper which takes a string as * argument, replaces known "@FOO@" style macros and returns a new * fixed string. Warning: The input STRING must have been allocated * statically. */ void _gpgrt_set_fixed_string_mapper (const char *(*f)(const char*)) { fixed_string_mapper = f; } /* Register a configuration directory for use by the argparse * functions. The defined values for WHAT are: * * GPGRT_CONFDIR_SYS The systems's configuration dir. * The default is /etc * * GPGRT_CONFDIR_USER The user's configuration directory. * The default is $HOME. * * A trailing slash is ignored; to have the function lookup * configuration files in the current directory, use ".". There is no * error return; more configuraion values may be added in future * revisions of this library. */ void _gpgrt_set_confdir (int what, const char *name) { char *buf, *p; if (what == GPGRT_CONFDIR_SYS) { _gpgrt_free (confdir.sys); buf = confdir.sys = _gpgrt_strdup (name); } else if (what == GPGRT_CONFDIR_USER) { _gpgrt_free (confdir.user); buf = confdir.user = _gpgrt_strdup (name); } else return; if (!buf) _gpgrt_log_fatal ("out of core in %s\n", __func__); #ifdef HAVE_W32_SYSTEM for (p=buf; *p; p++) if (*p == '\\') *p = '/'; #endif /* Strip trailing slashes unless buf is "/" or any other single char * string. */ if (*buf) { for (p=buf + strlen (buf)-1; p > buf; p--) if (*p == '/') *p = 0; else break; } } diff --git a/tests/etc/t-argparse.conf b/tests/etc/t-argparse.conf index c556466..90b9935 100644 --- a/tests/etc/t-argparse.conf +++ b/tests/etc/t-argparse.conf @@ -1,58 +1,62 @@ # Global test config file for t-argparse # Options applied to all user's config files #verbose -[echo Begin global config] - +[-echo Begin global config] +[verbose] [group :staff] # These option are applied to all users of the group staff up until # the next [group] or [user] statement. - +[-verbose] [+force] # All following option are forced and thus ignored when set in user # config files. Valid until the next [user] statement. Take care # that there are often "no-", "disable-", or "enable-" versions of # options; these should be explictly marked as ignored so that they # can't be used to override the force attribute. [ignore] # The compliance is set immutable for these users verbose +[-force] +not-my-option + # The next shall raise an error due to the garpage at the end. #[+ignore] fooo -#[+ignore-all] +[+ignore-all] # All options are ignored. [-ignore] # Comment at line end # Options wich shall not be ignored. */ #no-verbose [user john ] # Options for user john # Change the immutable attribute back to mutable. [-force] #compliance gnupg # Default key for user john my-option 42 # Parsing stops for user WK here. [group * ] # Options for all group which have no specific group sections above [user *] # Options for all users which have no specific user sections above # The default algorithm for new keys is set to this. a-long-option -[echo End global config] + +[-echo End global config] diff --git a/tests/t-argparse.c b/tests/t-argparse.c index 4719819..46b7258 100644 --- a/tests/t-argparse.c +++ b/tests/t-argparse.c @@ -1,153 +1,155 @@ /* t-argparse.c - Check the argparse API * Copyright (C) 2018, 2020 g10 Code GmbH * * This file is part of Libgpg-error. * * Libgpg-error is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * Libgpg-error is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1-or-later */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "t-argparse" #include "t-common.h" static struct { int verbose; int debug; char *outfile; char *crf; int myopt; int echo; int a_long_one; } opt; static const char * my_strusage (int level) { const char *p; switch (level) { case 9: p = "LGPL-2.1-or-later"; break; case 11: p = "t-argparse"; break; default: p = NULL; } return p; } int main (int argc, char **argv) { gpgrt_opt_t opts[] = { ARGPARSE_x ('v', "verbose", NONE, 0, "Laut sein"), ARGPARSE_s_n('e', "echo" , ("Zeile ausgeben, damit wir sehen, " "was wir eingegeben haben")), ARGPARSE_s_n('d', "debug", "Debug\nfalls mal etwas\nschief geht"), ARGPARSE_s_s('o', "output", 0 ), ARGPARSE_o_s('c', "cross-ref", "cross-reference erzeugen\n" ), /* Note that on a non-utf8 terminal the ß might garble the output. */ ARGPARSE_s_n('s', "street","|Straße|set the name of the street to Straße"), ARGPARSE_o_i('m', "my-option", 0), + ARGPARSE_o_i('M', "not-my-option", 0), ARGPARSE_s_n(500, "a-long-option", 0 ), ARGPARSE_conffile(501, "options", "|FILE|read options from FILE"), ARGPARSE_noconffile(502, "no-options", "Ignore conf files"), ARGPARSE_end() }; gpgrt_argparse_t pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL | ARGPARSE_FLAG_MIXED | ARGPARSE_FLAG_ONEDASH | ARGPARSE_FLAG_SYS | ARGPARSE_FLAG_USER /* | ARGPARSE_FLAG_VERBOSE */ ) }; int i; const char *srcdir; int any_warn = 0; gpgrt_set_strusage (my_strusage); srcdir = getenv ("srcdir"); if (!srcdir) srcdir = "."; gpgrt_set_confdir (GPGRT_CONFDIR_USER, srcdir); { char *p = gpgrt_fnameconcat (srcdir, "etc", NULL); gpgrt_set_confdir (GPGRT_CONFDIR_SYS, p); xfree (p); } while (gpgrt_argparser (&pargs, opts, PGM".conf")) { /* printf ("got option %d\n", pargs.r_opt); */ switch (pargs.r_opt) { case ARGPARSE_CONFFILE: printf ("current conffile='%s'\n", - pargs.r.ret_str? pargs.r.ret_str: "[cmdline]"); + pargs.r_type? pargs.r.ret_str: "[cmdline]"); break; case ARGPARSE_IS_ARG : printf ("arg='%s'\n", pargs.r.ret_str); break; case 'v': opt.verbose++; break; case 'e': opt.echo++; break; case 'd': opt.debug++; debug=1;break; case 'o': opt.outfile = pargs.r.ret_str; break; case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; + case 'M': opt.myopt = 0; break; case 500: opt.a_long_one++; break; default : pargs.err = ARGPARSE_PRINT_WARNING; any_warn = 1; break; } } for (i=0; i < argc; i++ ) printf ("%3d -> (%s)\n", i, argv[i] ); if (opt.verbose) puts ("Options:"); if (opt.verbose) printf (" verbose=%d\n", opt.verbose ); if (opt.debug) printf (" debug=%d\n", opt.debug ); if (opt.outfile) printf (" outfile='%s'\n", opt.outfile ); if (opt.crf) printf (" crffile='%s'\n", opt.crf ); if (opt.myopt) printf (" myopt=%d\n", opt.myopt ); if (opt.a_long_one) printf (" a-long-one=%d\n", opt.a_long_one ); if (opt.echo) printf (" echo=%d\n", opt.echo ); gpgrt_argparse (NULL, &pargs, NULL); (void)show; (void)fail; (void)die; return !!any_warn; } diff --git a/tests/t-argparse.conf b/tests/t-argparse.conf index e2c96e2..c689a63 100644 --- a/tests/t-argparse.conf +++ b/tests/t-argparse.conf @@ -1,11 +1,16 @@ # User test config file for t-argparse +[-echo begin of user config] +[+verbose] # Options applied to all user's config files echo # The next should be flagged as unexpected meta command. #[ignore] my-option 4711 +not-my-option + verbose +[-echo end of user config] \ No newline at end of file