Commit 34361061 34361061098003122b0ce911a87123f012e79f88 by Sergey Poznyakoff

sortm: update current message number; add testsuite.

* mh/sortm.c (current_num): New variable.
(sort): Keep track of the current message UID.  Store it back to
mailbox if in reorder mode.
(main): Save initial current message UID.  Save global and mailbox
status before exiting.
* mh/mh.h (mh_open_folder): Change last argument name.
* mh_open_folder (mh_open_folder): Change meaning of the 2nd argument:
it contains usual stream flags now.  All uses changed.

* mh/tests/sortm.at: New file.
* mh/tests/testsuite.at: Include sortm.at
* mh/tests/Makefile.am (TESTSUITE_AT): Add sortm.at.
1 parent f5374bc2
......@@ -116,7 +116,7 @@ main (int argc, char **argv)
mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
opt_handler, NULL, &index);
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
if (!component)
{
......
......@@ -721,7 +721,7 @@ main (int argc, char **argv)
argv += index;
VERBOSE ((_("Opening folder `%s'"), mh_current_folder ()));
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
if (inplace)
......@@ -729,7 +729,7 @@ main (int argc, char **argv)
size_t i, count;
VERBOSE ((_("Opening temporary folder `%s'"), tempfolder));
tmpbox = mh_open_folder (tempfolder, 1);
tmpbox = mh_open_folder (tempfolder, MU_STREAM_RDWR|MU_STREAM_CREAT);
VERBOSE ((_("Cleaning up temporary folder")));
mu_mailbox_messages_count (tmpbox, &count);
for (i = 1; i <= count; i++)
......
......@@ -249,7 +249,8 @@ main (int argc, char **argv)
mh_msgset_t msgset;
mu_mailbox_t mbox;
mbox = mh_open_folder (draftfolder, 1);
mbox = mh_open_folder (draftfolder,
MU_STREAM_RDWR|MU_STREAM_CREAT);
mh_msgset_parse (mbox, &msgset,
argc - index, argv + index,
use_draft ? "cur" : "new");
......@@ -275,7 +276,7 @@ main (int argc, char **argv)
mh_msgset_t msgset;
mu_mailbox_t mbox;
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
if (msgset.count != 1)
{
......
......@@ -839,7 +839,7 @@ int
action_pack ()
{
const char *folder_dir = mh_expand_name (NULL, mh_current_folder (), 0);
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), 0);
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
struct pack_tab *pack_tab;
size_t i, count, start;
int status;
......@@ -917,7 +917,7 @@ action_pack ()
/* Fix-up sequences */
if (!dry_run)
{
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
fd.mbox = mbox;
fd.folder_dir = folder_dir;
fd.pack_tab = pack_tab;
......@@ -967,7 +967,7 @@ main (int argc, char **argv)
if (argc - index == 1)
{
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), 0);
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
mh_msgset_current (mbox, &msgset, 0);
mh_global_save_state ();
......
......@@ -503,7 +503,7 @@ main (int argc, char **argv)
}
else
{
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
}
......
......@@ -219,7 +219,7 @@ main (int argc, char **argv)
exit (1);
}
output = mh_open_folder (append_folder, 1);
output = mh_open_folder (append_folder, MU_STREAM_RDWR|MU_STREAM_CREAT);
if ((rc = mu_mailbox_messages_count (output, &lastmsg)) != 0)
{
mu_error (_("cannot read output mailbox: %s"),
......
......@@ -201,7 +201,7 @@ main (int argc, char **argv)
mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
opt_handler, NULL, &index);
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mu_mailbox_get_url (mbox, &url);
mbox_dir = mu_url_to_string (url);
if (memcmp (mbox_dir, "mh:", 3) == 0)
......
......@@ -302,7 +302,7 @@ void mh_audit_close (FILE *fp);
int mh_message_number (mu_message_t msg, size_t *pnum);
mu_mailbox_t mh_open_folder (const char *folder, int create);
mu_mailbox_t mh_open_folder (const char *folder, int flags);
void mh_msgset_parse (mu_mailbox_t mbox, mh_msgset_t *msgset,
int argc, char **argv, char *def);
......
......@@ -443,14 +443,13 @@ mh_message_number (mu_message_t msg, size_t *pnum)
}
mu_mailbox_t
mh_open_folder (const char *folder, int create)
mh_open_folder (const char *folder, int flags)
{
mu_mailbox_t mbox = NULL;
char *name;
int flags = MU_STREAM_RDWR;
name = mh_expand_name (NULL, folder, 1);
if (create && mh_check_folder (name, 1))
if ((flags & MU_STREAM_CREAT) && mh_check_folder (name, 1))
exit (0);
if (mu_mailbox_create_default (&mbox, name))
......@@ -460,9 +459,6 @@ mh_open_folder (const char *folder, int create)
exit (1);
}
if (create)
flags |= MU_STREAM_CREAT;
if (mu_mailbox_open (mbox, flags))
{
mu_error (_("cannot open mailbox %s: %s"), name, strerror (errno));
......@@ -1045,7 +1041,7 @@ mh_draft_message (const char *name, const char *msgspec, char **pname)
mu_mailbox_t mbox;
const char *path;
mbox = mh_open_folder (name, 0);
mbox = mh_open_folder (name, MU_STREAM_RDWR);
if (!mbox)
return 1;
......
......@@ -1684,7 +1684,8 @@ store_handler (mu_message_t msg, msg_part_t part, char *type, char *encoding,
case store_to_folder_msg:
case store_to_folder:
{
mu_mailbox_t mbox = mh_open_folder (name, 1);
mu_mailbox_t mbox = mh_open_folder (name,
MU_STREAM_RDWR|MU_STREAM_CREAT);
size_t uid;
mu_mailbox_uidnext (mbox, &uid);
......@@ -2284,12 +2285,12 @@ edit_forw (char *cmd, struct compose_env *env, mu_message_t *pmsg, int level)
if (ws.ws_wordv[0][0] == '+')
{
mbox = mh_open_folder (ws.ws_wordv[0], 0);
mbox = mh_open_folder (ws.ws_wordv[0], MU_STREAM_RDWR);
i = 1;
}
else
{
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
i = 0;
}
......@@ -2930,7 +2931,7 @@ main (int argc, char **argv)
}
else
{
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
}
......
......@@ -87,7 +87,7 @@ main (int argc, char **argv)
exit (0);
}
mbox = mh_open_folder (current_folder, 0);
mbox = mh_open_folder (current_folder, MU_STREAM_RDWR);
mu_mailbox_messages_count (mbox, &total);
mu_mailbox_get_url (mbox, &url);
......
......@@ -80,7 +80,7 @@ main (int argc, char **argv)
argc -= index;
argv += index;
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
if (uid_option)
......
......@@ -390,7 +390,7 @@ main (int argc, char **argv)
if (pick_parse (lexlist))
return 1;
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
argc -= index;
argv += index;
......
......@@ -104,7 +104,7 @@ open_folders ()
mu_mailbox_t mbox;
mu_iterator_current (itr, (void **)&name);
mbox = mh_open_folder (name, 1);
mbox = mh_open_folder (name, MU_STREAM_RDWR|MU_STREAM_CREAT);
mu_list_append (folder_mbox_list, mbox);
free (name);
}
......@@ -261,7 +261,7 @@ main (int argc, char **argv)
}
else
{
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
status = mh_iterate (mbox, &msgset, refile_iterator, NULL);
......
......@@ -407,7 +407,7 @@ main (int argc, char **argv)
exit (1);
}
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
if (msgset.count != 1)
{
......
......@@ -94,7 +94,7 @@ opt_handler (int key, char *arg, struct argp_state *state)
static char *
current_folder_path ()
{
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), 0);
mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mu_url_t url;
char *p;
mu_mailbox_get_url (mbox, &url);
......
......@@ -73,7 +73,7 @@ main (int argc, char **argv)
mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
opt_handler, NULL, &index);
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
mh_msgset_parse (mbox, &msgset, argc - index, argv + index, "cur");
......
......@@ -179,7 +179,7 @@ main (int argc, char **argv)
exit (1);
}
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
argc -= index;
argv += index;
......
......@@ -765,7 +765,7 @@ main (int argc, char **argv)
const char *path;
size_t i;
mbox = mh_open_folder (draftfolder, 1);
mbox = mh_open_folder (draftfolder, MU_STREAM_RDWR|MU_STREAM_CREAT);
mh_msgset_parse (mbox, &msgset, argc, argv, draftmessage);
mu_mailbox_get_url (mbox, &url);
mu_url_sget_path (url, &path);
......
......@@ -93,6 +93,7 @@ static int verbose;
static mu_mailbox_t mbox;
static const char *mbox_path;
static mh_msgset_t msgset;
static size_t current_num;
#define ACTION_REORDER 0
#define ACTION_DRY_RUN 1
......@@ -193,7 +194,8 @@ opt_handler (int key, char *arg, struct argp_state *state)
/* *********************** Comparison functions **************************** */
struct comp_op {
struct comp_op
{
char *field;
compfun comp;
};
......@@ -219,7 +221,8 @@ addop (char *field, compfun comp)
mu_list_append (oplist, op);
}
struct rem_data {
struct rem_data
{
struct comp_op *op;
compfun comp;
};
......@@ -244,7 +247,8 @@ remop (compfun comp)
mu_list_remove (oplist, d.op);
}
struct comp_data {
struct comp_data
{
int r;
mu_message_t m[2];
};
......@@ -491,7 +495,7 @@ sort ()
switch (algorithm)
{
case ARG_QUICKSORT:
qsort(msgset.list, msgset.count, sizeof(msgset.list[0]),
qsort(msgset.list, msgset.count, sizeof (msgset.list[0]),
comp);
break;
......@@ -528,14 +532,36 @@ sort ()
mh_message_number (msg, &new_num);
transpose (i, oldlist[i]);
if (verbose)
fprintf (stderr, "{%s, %s}\n",
{
fprintf (stderr, "{%s, %s}",
mu_umaxtostr (0, old_num),
mu_umaxtostr (1, new_num));
}
if (old_num == current_num)
{
if (verbose)
fputc ('*', stderr);
current_num = new_num;
}
else if (new_num == current_num)
{
if (verbose)
fputc ('*', stderr);
current_num = old_num;
}
if (verbose)
fputc ('\n', stderr);
if (action == ACTION_REORDER)
swap_message (old_num, new_num);
}
}
}
if (action == ACTION_REORDER)
{
mu_mailbox_close (mbox);
mu_mailbox_open (mbox, MU_STREAM_RDWR);
mh_mailbox_set_cur (mbox, current_num);
}
}
......@@ -560,7 +586,7 @@ main (int argc, char **argv)
exit (1);
}
mbox = mh_open_folder (mh_current_folder (), 0);
mbox = mh_open_folder (mh_current_folder (), MU_STREAM_READ);
mu_mailbox_get_url (mbox, &url);
mbox_path = mu_url_to_string (url);
if (memcmp (mbox_path, "mh:", 3) == 0)
......@@ -569,7 +595,12 @@ main (int argc, char **argv)
argc -= index;
argv += index;
mh_mailbox_get_cur (mbox, &current_num);
mh_msgset_parse (mbox, &msgset, argc, argv, "all");
sort (mbox, msgset);
sort ();
mh_global_save_state ();
mu_mailbox_destroy (&mbox);
return 0;
}
......
......@@ -56,6 +56,7 @@ TESTSUITE_AT = \
pick.at\
scan.at\
send.at\
sortm.at\
refile.at\
repl.at\
rmf.at\
......
......@@ -81,3 +81,4 @@ m4_include([forw.at])
m4_include([repl.at])
m4_include([mhn.at])
m4_include([send.at])
m4_include([sortm.at])
......
......@@ -42,7 +42,7 @@ mh_whatnow_env_from_environ (struct mh_whatnow_env *wh)
size_t i;
struct mu_wordsplit ws;
mh_msgset_t msgset;
mu_mailbox_t mbox = mh_open_folder (folder, 0);
mu_mailbox_t mbox = mh_open_folder (folder, MU_STREAM_RDWR);
if (mu_wordsplit (p, &ws,
MU_WRDSF_DEFFLAGS & ~MU_WRDSF_CESCAPES))
......