From 45896c53b989cf2bd19a92927316eee347e8fe79 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 2 Aug 2017 15:29:18 +0100 Subject: [PATCH] move the message functions to the library --- Makefile.am | 4 +- curs_lib.c | 2 +- globals.h | 3 -- lib.h | 4 -- lib/Makefile.am | 4 +- lib/lib.h | 2 + lib/lib_memory.c | 3 +- lib/lib_message.c | 93 +++++++++++++++++++++++++++++++++++ extlib.c => lib/lib_message.h | 22 +++------ main.c | 4 +- po/POTFILES.in | 2 +- protos.h | 2 +- 12 files changed, 113 insertions(+), 32 deletions(-) create mode 100644 lib/lib_message.c rename extlib.c => lib/lib_message.h (65%) diff --git a/Makefile.am b/Makefile.am index 08bdc8afa..5ca4975b7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -84,7 +84,7 @@ EXTRA_mutt_SOURCES = browser.h mbyte.h mutt_idna.c mutt_idna.h \ EXTRA_DIST = account.h attach.h bcache.h browser.h buffer.h buffy.h \ ChangeLog.md charset.h CODE_OF_CONDUCT.md compress.h copy.h \ - COPYRIGHT extlib.c filter.h functions.h gen_defs globals.h \ + COPYRIGHT filter.h functions.h gen_defs globals.h \ group.h hash.h history.h init.h keymap.h lib.h LICENSE.md mailbox.h \ mapping.h mbyte.h mime.h mime.types mutt.h mutt_commands.h \ mutt_curses.h mutt_idna.h mutt_lua.h mutt_menu.h mutt_notmuch.h \ @@ -98,7 +98,7 @@ EXTRA_DIST = account.h attach.h bcache.h browser.h buffer.h buffy.h \ EXTRA_SCRIPTS = -pgpring_SOURCES = extlib.c lib.c pgppubring.c +pgpring_SOURCES = lib.c pgppubring.c pgpring_LDADD = $(LIBOBJS) $(NCRYPT_LIBS) $(INTLLIBS) $(LIBLIB) pgpring_DEPENDENCIES = $(LIBOBJS) $(NCRYPT_DEPS) $(INTLDEPS) $(LIBLIBDEPS) diff --git a/curs_lib.c b/curs_lib.c index 23a394d10..6515d4708 100644 --- a/curs_lib.c +++ b/curs_lib.c @@ -831,7 +831,7 @@ void mutt_endwin(const char *msg) errno = e; } -void mutt_perror(const char *s) +void mutt_perror_debug(const char *s) { char *p = strerror(errno); diff --git a/globals.h b/globals.h index 485894a6c..4cb791ee7 100644 --- a/globals.h +++ b/globals.h @@ -37,9 +37,6 @@ #include "sort.h" #endif /* MAIN_C */ -WHERE void (*mutt_error)(const char *, ...); -WHERE void (*mutt_message)(const char *, ...); - WHERE struct Context *Context; WHERE char Errorbuf[STRING]; diff --git a/lib.h b/lib.h index 29cc2d835..76ed85eb7 100644 --- a/lib.h +++ b/lib.h @@ -91,10 +91,6 @@ static inline int is_email_wsp(char c) */ -#ifndef _EXTLIB_C -extern void (*mutt_error)(const char *, ...); -#endif - #ifdef DEBUG extern char debugfilename[_POSIX_PATH_MAX]; extern FILE *debugfile; diff --git a/lib/Makefile.am b/lib/Makefile.am index dc0665b99..178037d3e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,12 +3,12 @@ include $(top_srcdir)/flymake.am AUTOMAKE_OPTIONS = 1.6 foreign -EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h lib_exit.h lib_md5.h lib_memory.h lib_sha1.h +EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h lib_exit.h lib_md5.h lib_memory.h lib_message.h lib_sha1.h AM_CPPFLAGS = -I$(top_srcdir) noinst_LIBRARIES = liblib.a noinst_HEADERS = -liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c lib_exit.c lib_md5.c lib_memory.c lib_sha1.c +liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c lib_exit.c lib_md5.c lib_memory.c lib_message.c lib_sha1.c diff --git a/lib/lib.h b/lib/lib.h index 687e94d40..bd645bef3 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -34,6 +34,7 @@ * -# @subpage exit * -# @subpage md5 * -# @subpage memory + * -# @subpage message * -# @subpage sha1 */ @@ -46,6 +47,7 @@ #include "lib_exit.h" #include "lib_md5.h" #include "lib_memory.h" +#include "lib_message.h" #include "lib_sha1.h" #endif /* _LIB_LIB_H */ diff --git a/lib/lib_memory.c b/lib/lib_memory.c index 0b2ea4b8c..2385bfec2 100644 --- a/lib/lib_memory.c +++ b/lib/lib_memory.c @@ -41,8 +41,7 @@ #include #include "lib_memory.h" #include "lib_exit.h" -// #include "lib_message.h" -void mutt_error(const char *, ...); +#include "lib_message.h" /** * safe_calloc - Allocate zeroed memory on the heap diff --git a/lib/lib_message.c b/lib/lib_message.c new file mode 100644 index 000000000..170510187 --- /dev/null +++ b/lib/lib_message.c @@ -0,0 +1,93 @@ +/** + * @file + * Message logging + * + * @authors + * Copyright (C) 2017 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program 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 General Public License along with + * this program. If not, see . + */ + +/** + * @page message Message logging + * + * Display informational messages for the user. + * + * These library stubs print the messages to stdout/stderr. + * + * | Function | Description + * | :---------------- | :-------------------------------------------- + * | default_error() | Display an error message + * | default_message() | Display an informative message + * | default_perror() | Lookup a standard error message (using errno) + */ + +#include "config.h" +#include +#include +#include +#include +#include "lib_memory.h" + +/** + * default_error - Display an error message + * @param format printf-like formatting string + * @param ... Arguments to format + * + * This stub function writes to stderr. + */ +static void default_error(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + fputc('\n', stderr); +} + +void (*mutt_error)(const char *, ...) = default_error; + +/** + * default_message - Display an informative message + * @param format printf-like formatting string + * @param ... Arguments to format + * + * This stub function writes to stdout. + */ +static void default_message(const char *format, ...) +{ + va_list ap; + va_start(ap, format); + vfprintf(stdout, format, ap); + va_end(ap); + fputc('\n', stdout); +} + +void (*mutt_message)(const char *, ...) = default_message; + +/** + * default_perror - Lookup a standard error message (using errno) + * @param message Prefix message to display + * + * This stub function writes to stderr. + */ +static void default_perror(const char *message) +{ + char *p = strerror(errno); + + mutt_error("%s: %s (errno = %d)", message, p ? p : _("unknown error"), errno); +} + +void (*mutt_perror)(const char *) = default_perror; diff --git a/extlib.c b/lib/lib_message.h similarity index 65% rename from extlib.c rename to lib/lib_message.h index 3c17da597..7d962ac09 100644 --- a/extlib.c +++ b/lib/lib_message.h @@ -1,9 +1,9 @@ /** * @file - * Helper function for standalone tools + * Message logging * * @authors - * Copyright (C) 1999-2000 Thomas Roessler + * Copyright (C) 2017 Richard Russon * * @copyright * This program is free software: you can redistribute it and/or modify it under @@ -20,17 +20,11 @@ * this program. If not, see . */ -/* - * Some simple dummies, so we can reuse the routines from - * lib.c in external programs. - */ - -#define WHERE -#define _EXTLIB_C - -#include "config.h" -#include -#include "lib.h" +#ifndef _LIB_MESSAGE_H +#define _LIB_MESSAGE_H -void (*mutt_error)(const char *, ...) = mutt_nocurses_error; +void (*mutt_error) (const char *format, ...); +void (*mutt_message)(const char *format, ...); +void (*mutt_perror) (const char *message); +#endif /* _LIB_MESSAGE_H */ diff --git a/main.c b/main.c index f98a56b48..d20cb8828 100644 --- a/main.c +++ b/main.c @@ -235,8 +235,8 @@ int main(int argc, char **argv, char **env) } #endif - mutt_error = mutt_nocurses_error; - mutt_message = mutt_nocurses_error; + mutt_message = mutt_error; /* send messages to stderr, too */ + mutt_perror = mutt_perror_debug; (void) mutt_rand32(); umask(077); diff --git a/po/POTFILES.in b/po/POTFILES.in index eb2b4fe25..a13cf8a04 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -22,7 +22,6 @@ doc/makedoc.c edit.c editmsg.c enter.c -extlib.c filter.c flags.c from.c @@ -65,6 +64,7 @@ lib/lib_date.c lib/lib_exit.c lib/lib_md5.c lib/lib_memory.c +lib/lib_message.c lib/lib_sha1.c main.c mbox.c diff --git a/protos.h b/protos.h index c446f0d01..6700d1698 100644 --- a/protos.h +++ b/protos.h @@ -223,7 +223,7 @@ void _mutt_mktemp(char *s, size_t slen, const char *prefix, const char *suffix, void mutt_paddstr(int n, const char *s); void mutt_parse_mime_message(struct Context *ctx, struct Header *cur); void mutt_parse_part(FILE *fp, struct Body *b); -void mutt_perror(const char *s); +void mutt_perror_debug(const char *s); void mutt_prepare_envelope(struct Envelope *env, int final); void mutt_unprepare_envelope(struct Envelope *env); void mutt_pretty_mailbox(char *s, size_t buflen); -- 2.40.0