summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/mailutils/msgset.h5
-rw-r--r--libmailutils/msgset/Makefile.am2
-rw-r--r--libmailutils/msgset/first.c41
-rw-r--r--libmailutils/msgset/last.c41
-rw-r--r--libmailutils/tests/msgset.at12
-rw-r--r--libmailutils/tests/msgset.c35
6 files changed, 131 insertions, 5 deletions
diff --git a/include/mailutils/msgset.h b/include/mailutils/msgset.h
index 09b24f420..8f3b6e1bf 100644
--- a/include/mailutils/msgset.h
+++ b/include/mailutils/msgset.h
@@ -34,7 +34,7 @@ struct mu_msgrange
message. */
#define MU_MSGNO_LAST 0
-#define MU_MSGSET_NUM 0 /* Message set operates on sequence numbers */
+#define MU_MSGSET_NUM 0 /* Message set operates on sequence numbers */
#define MU_MSGSET_UID 1 /* Message set operates on UIDs */
#define MU_MSGSET_MODE_MASK 0x0f
@@ -65,6 +65,9 @@ int mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset);
int mu_msgset_count (mu_msgset_t mset, size_t *pcount);
int mu_msgset_is_empty (mu_msgset_t mset);
+
+int mu_msgset_first (mu_msgset_t msgset, size_t *ret);
+int mu_msgset_last (mu_msgset_t msgset, size_t *ret);
typedef int (*mu_msgset_msgno_action_t) (size_t _n, void *_call_data);
typedef int (*mu_msgset_message_action_t) (size_t _n, mu_message_t _msg,
diff --git a/libmailutils/msgset/Makefile.am b/libmailutils/msgset/Makefile.am
index d9e67309e..1dd608ad5 100644
--- a/libmailutils/msgset/Makefile.am
+++ b/libmailutils/msgset/Makefile.am
@@ -27,12 +27,14 @@ libmsgset_la_SOURCES = \
getitr.c\
getlist.c\
getmbox.c\
+ first.c\
foreachnum.c\
foreachmsgno.c\
foreachuid.c\
foreachmsg.c\
free.c\
isempty.c\
+ last.c\
locate.c\
negate.c\
parse.c\
diff --git a/libmailutils/msgset/first.c b/libmailutils/msgset/first.c
new file mode 100644
index 000000000..0c8aa8306
--- /dev/null
+++ b/libmailutils/msgset/first.c
@@ -0,0 +1,41 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2017 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
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Mailutils is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+int
+mu_msgset_first (mu_msgset_t msgset, size_t *ret)
+{
+ int rc;
+ struct mu_msgrange const *range;
+
+ if (mu_msgset_is_empty (msgset))
+ return EINVAL;
+ if (!ret)
+ return MU_ERR_OUT_PTR_NULL;
+ rc = mu_msgset_aggregate (msgset);
+ if (rc)
+ return rc;
+ rc = mu_list_head (msgset->list, (void **) &range);
+ if (rc == 0)
+ *ret = range->msg_beg;
+ return rc;
+}
diff --git a/libmailutils/msgset/last.c b/libmailutils/msgset/last.c
new file mode 100644
index 000000000..4b4d1df04
--- /dev/null
+++ b/libmailutils/msgset/last.c
@@ -0,0 +1,41 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2017 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
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GNU Mailutils is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include <mailutils/types.h>
+#include <mailutils/errno.h>
+#include <mailutils/list.h>
+#include <mailutils/msgset.h>
+#include <mailutils/sys/msgset.h>
+
+int
+mu_msgset_last (mu_msgset_t msgset, size_t *ret)
+{
+ int rc;
+ struct mu_msgrange const *range;
+
+ if (mu_msgset_is_empty (msgset))
+ return EINVAL;
+ if (!ret)
+ return MU_ERR_OUT_PTR_NULL;
+ rc = mu_msgset_aggregate (msgset);
+ if (rc)
+ return rc;
+ rc = mu_list_tail (msgset->list, (void **) &range);
+ if (rc == 0)
+ *ret = range->msg_end;
+ return rc;
+}
diff --git a/libmailutils/tests/msgset.at b/libmailutils/tests/msgset.at
index 788a4413c..6f1825581 100644
--- a/libmailutils/tests/msgset.at
+++ b/libmailutils/tests/msgset.at
@@ -171,5 +171,17 @@ MSGSET([subtract an open range with smaller left boundary],
[3
])
+MSGSET([first],
+[msgset-first],
+[-msgset='3,10:20' -first],
+[3
+])
+
+MSGSET([last],
+[msgset-last],
+[-msgset='3,10:20' -last],
+[20
+])
+
dnl ------------------------------------------------------------------
m4_popdef([MSGSET]) \ No newline at end of file
diff --git a/libmailutils/tests/msgset.c b/libmailutils/tests/msgset.c
index 5fc7ddd04..6d6a06dc9 100644
--- a/libmailutils/tests/msgset.c
+++ b/libmailutils/tests/msgset.c
@@ -73,13 +73,37 @@ parse_msgset (const char *arg)
return msgset;
}
+static void
+print_all (mu_msgset_t msgset)
+{
+ MU_ASSERT (mu_msgset_print (mu_strout, msgset));
+ mu_printf ("\n");
+}
+
+static void
+print_first (mu_msgset_t msgset)
+{
+ size_t n;
+ MU_ASSERT (mu_msgset_first (msgset, &n));
+ printf ("%zu\n", n);
+}
+
+static void
+print_last (mu_msgset_t msgset)
+{
+ size_t n;
+ MU_ASSERT (mu_msgset_last (msgset, &n));
+ printf ("%zu\n", n);
+}
+
int
main (int argc, char **argv)
{
int i;
char *msgset_string = NULL;
mu_msgset_t msgset;
-
+ void (*print) (mu_msgset_t) = print_all;
+
mu_set_program_name (argv[0]);
for (i = 1; i < argc; i++)
{
@@ -88,7 +112,7 @@ main (int argc, char **argv)
if (strcmp (arg, "-h") == 0 || strcmp (arg, "-help") == 0)
{
mu_printf ("usage: %s [-msgset=SET] [-add=X[:Y]] [-del=X[:Y]] "
- "[-addset=SET] [-delset=SET] ...\n",
+ "[-addset=SET] [-delset=SET] [-first] [-last]...\n",
mu_program_name);
return 0;
}
@@ -142,14 +166,17 @@ main (int argc, char **argv)
mu_msgset_free (tset);
}
}
+ else if (strcmp (arg, "-first") == 0)
+ print = print_first;
+ else if (strcmp (arg, "-last") == 0)
+ print = print_last;
else
{
mu_error ("unknown option %s", arg);
return 1;
}
}
- MU_ASSERT (mu_msgset_print (mu_strout, msgset));
- mu_printf ("\n");
+ print (msgset);
mu_msgset_free (msgset);
return 0;

Return to:

Send suggestions and report system problems to the System administrator.