Thanks for the quick reviews. I am attaching an updated patch that I
believe addresses the issues mentioned in a couple of earlier messages
('man' docs and omitting some 'else's).
Regards,
-chaw
diff --git a/docs/pending-release-notes b/docs/pending-release-notes
index 3fd6bfc4..051af34a 100644
--- a/docs/pending-release-notes
+++ b/docs/pending-release-notes
@@ -4,6 +4,7 @@ Things to add to the release notes for the next full release:
NEW FEATURES
------------
+- sortm(1) accepts -empty switch to permit sorting empty sequences without error.
- Contrib support for Gmail API access merged from branch to master.
-----------------
diff --git a/man/sortm.man b/man/sortm.man
index fc5a4482..419abd7b 100644
--- a/man/sortm.man
+++ b/man/sortm.man
@@ -22,6 +22,7 @@ sortm \- sort nmh messages
.IR days ]
.RB [ \-nolimit ]
.RB [ \-check " | " \-nocheck ]
+.RB [ \-empty " | " \-noempty ]
.RB [ \-verbose " | " \-noverbose ]
.ad
.SH DESCRIPTION
@@ -113,6 +114,15 @@ messages, and exits with non-zero status. With the default of
sorts messages with a missing or invalid
\*(lqDate:\*(rq field using their file modification times.
.PP
+When asked to sort an empty sequence,
+.B sortm
+reports an error by default and exits with nonzero status. With the
+.B \-empty
+switch, no error is reported in this case and the exit status is 0.
+The default behavior may also be requested explicitly using the
+.B \-noempty
+switch.
+.PP
When ordering messages based on their dates, if they have the same
dates, their original message order is preserved.
.SH FILES
diff --git a/test/folder/test-sortm b/test/folder/test-sortm
index 78355167..178f66be 100755
--- a/test/folder/test-sortm
+++ b/test/folder/test-sortm
@@ -36,6 +36,7 @@ Usage: sortm [+folder] [msgs] [switches]
-[no]verbose
-[no]all
-[no]check
+ -[no]empty
-version
-help
EOF
@@ -464,6 +465,27 @@ modification time"
scan -width 80 >"$actual"
check "$expected" "$actual"
+# check -empty
+start_test "-empty"
+cat >"$expected" <<EOF
+EOF
+
+folder -create +tsortmz
+run_prog sortm -empty >"$actual" 2>&1
+check "$expected" "$actual"
+
+
+# check -noempty
+start_test "-noempty"
+cat >"$expected" <<EOF
+sortm: no messages in tsortmz
+EOF
+
+folder -create +tsortmz
+set +e
+run_prog sortm -noempty >"$actual" 2>&1
+set -e
+check "$expected" "$actual"
finish_test
exit ${failed:-0}
diff --git a/uip/sortm.c b/uip/sortm.c
index 856c245d..698cc124 100644
--- a/uip/sortm.c
+++ b/uip/sortm.c
@@ -47,6 +47,8 @@
X("noall", 0, NALLMSGS) \
X("check", 0, CHECKSW) \
X("nocheck", 0, NCHECKSW) \
+ X("empty", 0, EMPTYSW) \
+ X("noempty", 0, NEMPTYSW) \
X("version", 0, VERSIONSW) \
X("help", 0, HELPSW) \
@@ -100,6 +102,7 @@ main (int argc, char **argv)
struct msgs *mp;
struct smsg **dlist;
bool checksw = false;
+ bool emptysw = false;
if (nmh_init(argv[0], true, true)) { return 1; }
@@ -186,6 +189,13 @@ main (int argc, char **argv)
case NCHECKSW:
checksw = false;
continue;
+
+ case EMPTYSW:
+ emptysw = true;
+ continue;
+ case NEMPTYSW:
+ emptysw = false;
+ continue;
}
}
if (*cp == '+' || *cp == '@') {
@@ -219,8 +229,11 @@ main (int argc, char **argv)
die("unable to read folder %s", folder);
/* check for empty folder */
- if (mp->nummsg == 0)
- die("no messages in %s", folder);
+ if (mp->nummsg == 0) {
+ if (emptysw)
+ done (0);
+ die("no messages in %s", folder);
+ }
/* parse all the message ranges/sequences and set SELECTED */
for (msgnum = 0; msgnum < msgs.size; msgnum++)
@@ -228,8 +241,11 @@ main (int argc, char **argv)
done (1);
seq_setprev (mp); /* set the previous sequence */
- if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
- die("no messages to sort");
+ if ((nmsgs = read_hdrs (mp, datesw)) <= 0) {
+ if (emptysw)
+ done (0);
+ die("no messages to sort");
+ }
if (checksw && check_failed) {
die("errors found, no messages sorted");