diff options
Diffstat (limited to 'mimeview/mimeview.c')
-rw-r--r-- | mimeview/mimeview.c | 111 |
1 files changed, 46 insertions, 65 deletions
diff --git a/mimeview/mimeview.c b/mimeview/mimeview.c index 6a29c2558..093a8adb3 100644 --- a/mimeview/mimeview.c +++ b/mimeview/mimeview.c @@ -1,5 +1,5 @@ /* GNU Mailutils -- a suite of utilities for electronic mail - Copyright (C) 2005-2019 Free Software Foundation, Inc. + Copyright (C) 2005-2024 Free Software Foundation, Inc. GNU Mailutils is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,16 +18,22 @@ # include <config.h> #endif -#include <mimeview.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <unistd.h> #include <fcntl.h> -#include "mailutils/cli.h" -#include "mailutils/argcv.h" - +#include <mailutils/alloc.h> +#include <mailutils/cli.h> +#include <mailutils/argcv.h> +#include <mailutils/nls.h> +#include <mailutils/io.h> +#include <mailutils/header.h> +#include <mailutils/errno.h> +#include <mailutils/stream.h> +#include <mailutils/stdstream.h> +#include <mailutils/mimetypes.h> #include "mailcap.h" static int dry_run; /* Dry run mode */ @@ -38,8 +44,6 @@ static char *mimetypes_config = DEFAULT_CUPS_CONFDIR; static char *no_ask_types; /* List of MIME types for which no questions should be asked */ static int interactive = -1; -char const *mimeview_file; /* Name of the file to view */ -mu_stream_t mimeview_stream; /* The corresponding stream */ static void @@ -65,7 +69,7 @@ cli_debug (struct mu_parseopt *po, struct mu_option *opt, lev = MU_DEBUG_LEVEL_UPTO (MU_DEBUG_TRACE2); else { - mu_debug_get_category_level (MU_DEBCAT_APP, &lev); + mu_debug_get_category_level (MU_DEBCAT_MIMETYPES, &lev); for (; *arg; arg++) { switch (*arg) @@ -87,7 +91,7 @@ cli_debug (struct mu_parseopt *po, struct mu_option *opt, } } } - mu_debug_set_category_level (MU_DEBCAT_APP, lev); + mu_debug_set_category_level (MU_DEBCAT_MIMETYPES, lev); } static void @@ -160,39 +164,6 @@ static char *capa[] = { NULL }; -static int -open_file (char const *name) -{ - int rc; - struct stat st; - - if (stat (name, &st)) - { - mu_error (_("cannot stat `%s': %s"), name, mu_strerror (errno)); - return -1; - } - if (!S_ISREG (st.st_mode) && !S_ISLNK (st.st_mode)) - { - mu_error (_("not a regular file or symbolic link: `%s'"), name); - return -1; - } - - mimeview_file = name; - rc = mu_file_stream_create (&mimeview_stream, mimeview_file, MU_STREAM_READ); - if (rc) - { - mu_error (_("Cannot open `%s': %s"), name, mu_strerror (rc)); - return -1; - } - return 0; -} - -void -close_file () -{ - mu_stream_close (mimeview_stream); -} - void display_file (const char *file, const char *type) { @@ -218,14 +189,14 @@ display_file (const char *file, const char *type) argv[3] = "-c"; argv[4] = (char*) type; - argv[5] = (char*) mimeview_file; + argv[5] = (char*) file; argv[6] = NULL; - if (mu_debug_level_p (MU_DEBCAT_APP, MU_DEBUG_TRACE0)) + if (mu_debug_level_p (MU_DEBCAT_MIMETYPES, MU_DEBUG_TRACE0)) { char *string; mu_argcv_string (6, argv, &string); - mu_debug (MU_DEBCAT_APP, MU_DEBUG_TRACE0, + mu_debug (MU_DEBCAT_MIMETYPES, MU_DEBUG_TRACE0, (_("executing %s...\n"), string)); free (string); } @@ -244,9 +215,20 @@ display_file (const char *file, const char *type) mu_error (_("cannot create header: %s"), mu_strerror (status)); else { - display_stream_mailcap (mimeview_file, mimeview_stream, hdr, - no_ask_types, interactive, dry_run, - MU_DEBCAT_APP); + mu_stream_t str; + + status = mu_file_stream_create (&str, file, MU_STREAM_READ); + if (status) + { + mu_error (_("Cannot open `%s': %s"), file, mu_strerror (status)); + } + else + { + display_stream_mailcap (file, str, hdr, + no_ask_types, interactive, dry_run, + MU_DEBCAT_MIMETYPES); + mu_stream_destroy (&str); + } mu_header_destroy (&hdr); } } @@ -255,6 +237,8 @@ display_file (const char *file, const char *type) int main (int argc, char **argv) { + mu_mimetypes_t mt; + MU_APP_INIT_NLS (); interactive = isatty (fileno (stdin)); @@ -263,32 +247,29 @@ main (int argc, char **argv) if (dry_run) { mu_debug_level_t lev; - mu_debug_get_category_level (MU_DEBCAT_APP, &lev); + mu_debug_get_category_level (MU_DEBCAT_MIMETYPES, &lev); lev |= MU_DEBUG_LEVEL_UPTO (MU_DEBUG_TRACE2); - mu_debug_set_category_level (MU_DEBCAT_APP, lev); + mu_debug_set_category_level (MU_DEBCAT_MIMETYPES, lev); } if (argc == 0 && !lint) { - mu_error (_("no files given")); + mu_error ("%s", _("no files given")); return 1; } - if (mimetypes_parse (mimetypes_config)) + if ((mt = mu_mimetypes_open (mimetypes_config)) == NULL) return 1; - if (lint) - return 0; - - while (argc--) - { - const char *type; - char const *file = *argv++; - if (open_file (file)) - continue; - type = get_file_type (); - display_file (file, type); - close_file (); + if (!lint) + { + while (argc--) + { + char const *file = *argv++; + char const *type = mu_mimetypes_file_type (mt, file); + display_file (file, type); + } } - + mu_mimetypes_close (mt); + return 0; } |