Commit 36e8269d 36e8269d52475b4e4e5869873c835ead51d82ccb by Sergey Poznyakoff

mh: fix the use of mhl.forward in forw.

* mh/mhl.forward: New file.
* mh/forw.c (mh_option): the -format option is boolean.
(mhl_filter): Rename to mhl_filter_file.
(opt_handler) <ARG_FORMAT>: Use default mhl.filter.
<ARG_NOFORMAT>: Set encap_clear.
<ARG_FILTER>: Use mh_find_file to determine filter file name.
(finish_draft): Use MHL filter file if encap == encap_mhl.
* mh/mh.h (mh_find_file): New prototype.
* mh/mh_init.c (mh_find_file): New file.
(mh_read_formfile): Use mh_find_file to locate the file.
* mh/tests/forw.at: Test forw -format.
* mh/tests/mhparam.at: Update the -all test.
* tests/testsuite.at (MH_SETUP): Set the "mhetcdir" component.
* mh/scan.c (opt_handler): Exit if mh_read_formfile failed.
1 parent 2ef58f17
......@@ -97,8 +97,9 @@ BUILT_SOURCES= \
MAINTAINERCLEANFILES=$(BUILT_SOURCES)
mhlib_DATA = components replcomps replgroupcomps mhl.format $(LISPSRC)
EXTRA_DIST = components replcomps replgroupcomps mhl.format\
mhlib_DATA = components replcomps replgroupcomps mhl.format mhl.forward\
$(LISPSRC)
EXTRA_DIST = components replcomps replgroupcomps mhl.format mhl.forward\
mailutils-mh.eli mh_fmtgram.y pick.y mh_alias.y mh_alias.l
DISTCLEANFILES = mailutils-mh.el
......
......@@ -78,7 +78,7 @@ struct mh_option mh_option[] = {
{ "build" },
{ "file", MH_OPT_ARG, "msgfile" },
{ "form", MH_OPT_ARG, "formatfile" },
{ "format", MH_OPT_ARG, "string" },
{ "format", MH_OPT_BOOL },
{ "draftfolder", MH_OPT_ARG, "folder" },
{ "nodraftfolder" },
{ "draftmessage" },
......@@ -103,7 +103,9 @@ static char *formfile;
struct mh_whatnow_env wh_env = { 0 };
static int initial_edit = 1;
static const char *whatnowproc;
static char *mhl_filter = NULL; /* --filter flag */
static char *mhl_filter_file = NULL; /* --filter flag */
static int build_only = 0; /* --build flag */
static int annotate = 0; /* --annotate flag */
static enum encap_type encap = encap_clear; /* controlled by --format, --form
......@@ -184,15 +186,18 @@ opt_handler (int key, char *arg, struct argp_state *state)
if (is_true (arg))
{
encap = encap_mhl;
break;
mh_find_file ("mhl.forward", &mhl_filter_file);
}
/*FALLTHRU*/
case ARG_NOFORMAT:
if (encap == encap_mhl)
else
encap = encap_clear;
break;
case ARG_NOFORMAT:
encap = encap_clear;
break;
case ARG_FILTER:
mh_find_file (arg, &mhl_filter_file);
encap = encap_mhl;
break;
......@@ -381,23 +386,16 @@ finish_draft ()
}
else
{
if (!mhl_filter)
if (encap == encap_mhl)
{
char *s = mh_expand_name (MHLIBDIR, "mhl.forward", 0);
if (access (s, R_OK) == 0)
mhl_filter = "mhl.forward";
free (s);
if (mhl_filter_file)
{
format = mhl_format_compile (mhl_filter_file);
if (!format)
exit (1);
}
}
if (mhl_filter)
{
char *s = mh_expand_name (MHLIBDIR, mhl_filter, 0);
format = mhl_format_compile (s);
if (!format)
exit (1);
free (s);
}
if (annotate)
{
wh_env.anno_field = "Forwarded";
......
......@@ -314,6 +314,7 @@ void mh_msgset_free (mh_msgset_t *msgset);
void mh_msgset_uids (mu_mailbox_t mbox, mh_msgset_t *msgset);
char *mh_get_dir (void);
int mh_find_file (const char *name, char **resolved_name);
char *mh_expand_name (const char *base, const char *name, int is_folder);
void mh_quote (const char *in, char **out);
void mh_expand_aliases (mu_message_t msg, mu_address_t *addr_to,
......@@ -366,6 +367,7 @@ void mh_annotate (mu_message_t msg, char *field, char *text, int date);
#define MHL_DISABLE_BODY 8
mu_list_t mhl_format_compile (char *name);
int mhl_format_run (mu_list_t fmt, int width, int length, int flags,
mu_message_t msg, mu_stream_t output);
void mhl_format_destroy (mu_list_t *fmt);
......
......@@ -77,20 +77,34 @@ mh_read_formfile (char *name, char **pformat)
char *ptr;
size_t off = 0;
char *format_str;
if (stat (name, &st))
char *file_name;
int rc;
rc = mh_find_file (name, &file_name);
if (rc)
{
mu_error (_("cannot stat format file %s: %s"), name, strerror (errno));
mu_error (_("cannot access format file %s: %s"), name, strerror (rc));
return -1;
}
fp = fopen (name, "r");
if (stat (file_name, &st))
{
mu_error (_("cannot stat format file %s: %s"), file_name,
strerror (errno));
free (file_name);
return -1;
}
fp = fopen (file_name, "r");
if (!fp)
{
mu_error (_("cannot open format file %s: %s"), name, strerror (errno));
mu_error (_("cannot open format file %s: %s"), file_name,
strerror (errno));
free (file_name);
return -1;
}
free (file_name);
format_str = xmalloc (st.st_size+1);
while ((ptr = fgets (format_str + off, st.st_size - off + 1, fp)) != NULL)
{
......@@ -520,6 +534,68 @@ mh_expand_name (const char *base, const char *name, int is_folder)
}
int
mh_find_file (const char *name, char **resolved_name)
{
char *s;
int rc;
if (name[0] == '/' ||
(name[0] == '.' && name[1] == '/') ||
(name[0] == '.' && name[1] == '.' && name[2] == '/'))
{
if (access (name, R_OK) == 0)
{
*resolved_name = xstrdup (name);
return 0;
}
return errno;
}
if (name[0] == '~')
{
s = mu_tilde_expansion (name, "/", NULL);
if (access (s, R_OK) == 0)
{
*resolved_name = s;
return 0;
}
return errno;
}
s = mh_expand_name (NULL, name, 0);
if (access (s, R_OK) == 0)
{
*resolved_name = s;
return 0;
}
if (errno != ENOENT)
mu_diag_output (MU_DIAG_WARNING,
_("cannot access %s: %s"), s, mu_strerror (errno));
free (s);
s = mh_expand_name (mh_global_profile_get ("mhetcdir", MHLIBDIR), name, 0);
if (access (s, R_OK) == 0)
{
*resolved_name = s;
return 0;
}
if (errno != ENOENT)
mu_diag_output (MU_DIAG_WARNING,
_("cannot access %s: %s"), s, mu_strerror (errno));
free (s);
*resolved_name = xstrdup (name);
if (access (name, R_OK) == 0)
return 0;
rc = errno;
if (rc != ENOENT)
mu_diag_output (MU_DIAG_WARNING,
_("cannot access %s: %s"), s, mu_strerror (rc));
return rc;
}
int
mh_iterate (mu_mailbox_t mbox, mh_msgset_t *msgset,
mh_iterator_fp itr, void *data)
{
......
; This is the default mhl.format file for Mailutils MH.
;
; GNU Mailutils -- a suite of utilities for electronic mail
; Copyright (C) 2003, 2010 Free Software Foundation, Inc.
; Distributed under GPLv3+. See <http://gnu.org/licenses/gpl.html>.
;
width=80,overflowtext=,overflowoffset=10
leftadjust,compress,compwidth=9
Date:formatfield="%<(nodate{text})%{text}%|%(tws{text})%>"
From:
To:
cc:
Subject:
:
body:nocomponent,overflowoffset=0,noleftadjust,nocompress
......@@ -99,7 +99,8 @@ opt_handler (int key, char *arg, struct argp_state *state)
break;
case ARG_FORM:
mh_read_formfile (arg, &format_str);
if (mh_read_formfile (arg, &format_str))
exit (1);
break;
case ARG_FORMAT:
......
......@@ -80,7 +80,68 @@ Subject: test input
message body
])
MH_CHECK([forw msgs],[forw01 forw-msgs],[
MH_CHECK([forw -format msg],[forw01 forw-format-msg],[
mkdir Mail/inbox
AT_DATA([Mail/inbox/1],[From: gray
To: root
Subject: test input
message body
])
dir=`pwd`
echo quit | forwcmd -format +inbox 1 | sed "s|$dir/*||;s| *$||"
echo == Mail/draft ==
cat Mail/draft
echo == Message ==
# FIXME: AMD adds this header to the first message. Find a better way.
# See also the same sed hacks below.
sed '/^X-IMAPbase/d' Mail/inbox/1
],
[0],
[-- Editor invocation: Mail/draft
-- Input file:
To:
cc:
Subject:
--------
------- Forwarded message
From: gray
To: root
Subject: test input
message body
------- End of Forwarded message
-- Input file end
What now? draft left on "Mail/draft".
== Mail/draft ==
To:
cc:
Subject:
--------
------- Forwarded message
From: gray
To: root
Subject: test input
message body
------- End of Forwarded message
Seen by mhed
== Message ==
From: gray
To: root
Subject: test input
message body
])
MH_CHECK([forw msgs],[forw02 forw-msgs],[
mkdir Mail/inbox
AT_DATA([Mail/inbox/1],[From: gray
To: root
......@@ -171,7 +232,7 @@ Subject: 2nd message
2nd message body
])
MH_CHECK([forw -build msg],[forw02 forw-build-msg],[
MH_CHECK([forw -build msg],[forw03 forw-build-msg],[
mkdir Mail/inbox
AT_DATA([Mail/inbox/1],[From: gray
To: root
......@@ -211,7 +272,7 @@ Subject: test input
message body
])
MH_CHECK([forw -build -mime msgs],[forw03 forw-build-mime-msg],[
MH_CHECK([forw -build -mime msgs],[forw04 forw-build-mime-msg],[
mkdir Mail/inbox
AT_DATA([Mail/inbox/1],[From: gray
To: root
......@@ -257,7 +318,7 @@ Subject: 2nd message
2nd message body
])
MH_CHECK([forw -draftfolder],[forw04 forw-draftfolder draftfolder],[
MH_CHECK([forw -draftfolder],[forw05 forw-draftfolder draftfolder],[
mkdir Mail/inbox
mkdir Mail/drafts
AT_DATA([Mail/inbox/1],[From: gray
......@@ -316,7 +377,7 @@ Subject: test input
message body
])
MH_CHECK([forw -file],[forw05 forw-file],[
MH_CHECK([forw -file],[forw06 forw-file],[
AT_DATA([infile],[From: gray
To: root
Subject: test input
......
......@@ -22,10 +22,11 @@ Sequence-Negation: not
Draft-Folder: Mail/drafts
Aliasfile: .mh_aliases
EOT
mhparam -all | tr '\t' ' ' | sed 's/^Path:.*/Path: Mail/;s/ */ /g'
mhparam -all | tr '\t' ' ' | sed 's/^Path:.*/Path: Mail/;s/^mhetcdir:.*/mhetcdir: dir/;s/ */ /g'
],
[0],
[Path: Mail
mhetcdir: dir
Sequence-Negation: not
Draft-Folder: Mail/drafts
Aliasfile: .mh_aliases
......
......@@ -21,7 +21,10 @@ test -d Mail || mkdir Mail
dir=`pwd`
MH=$dir/mh_profile
export MH
echo "Path: $dir/Mail" > $MH
cat > $MH <<EOT
Path: $dir/Mail
mhetcdir: $abs_top_srcdir/mh
EOT
exec <&-
])
......