Following an earlier suggestion, I am attaching a patch that adds -empty
and (the default) -noempty switches to sortm. (I am unsure of the
preferred way of submitting it and can resubmit in a different way if
desired. I think it's small enough to be amenable to a quick browse
and/or test.)

The -empty switch results in sorting an empty sequence not being treated
as an error, producing normal termination (exit status 0) instead.  The
patch also adds a couple of test cases and some documentation to the man
page and pending-release-notes.

I tried to follow the conventions I observed but it's my first time
working with this codebase so please excuse and correct any
inconsistencies, omissions, or other errors.

A 'make check' with the modified code passes all tests except for
test-mhical. That one fails for me on an unmodified checkout from git as
of today as well (using default configure options except for --prefix),
and I have not had a chance to figure out the details of why yet.  It
may well be something strange with my setup.

Regards,

-chaw

diff --git a/docs/pending-release-notes b/docs/pending-release-notes
index fa6393d1..7f7b6452 100644
--- a/docs/pending-release-notes
+++ b/docs/pending-release-notes
@@ -4,6 +4,8 @@ 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.
+
 -----------------
 OBSOLETE FEATURES
 -----------------
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..672dbf9c 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,14 @@ main (int argc, char **argv)
 	die("unable to read folder %s", folder);
 
     /* check for empty folder */
-    if (mp->nummsg == 0)
+    if (mp->nummsg == 0) {
+      if (emptysw) {
+        done (0);
+      }
+      else {
 	die("no messages in %s", folder);
+      }
+    }
 
     /* parse all the message ranges/sequences and set SELECTED */
     for (msgnum = 0; msgnum < msgs.size; msgnum++)
@@ -228,8 +244,14 @@ main (int argc, char **argv)
 	    done (1);
     seq_setprev (mp);	/* set the previous sequence */
 
-    if ((nmsgs = read_hdrs (mp, datesw)) <= 0)
+    if ((nmsgs = read_hdrs (mp, datesw)) <= 0) {
+      if (emptysw) {
+        done (0);
+      }
+      else {
 	die("no messages to sort");
+      }
+    }
 
     if (checksw  &&  check_failed) {
 	die("errors found, no messages sorted");

Reply via email to