dovecot-2.1-pigeonhole: ManageSieve: cleaned up parser and updat...

pigeonhole at rename-it.nl pigeonhole at rename-it.nl
Sat Jan 28 15:32:35 EET 2012


details:   http://hg.rename-it.nl/dovecot-2.1-pigeonhole/rev/b2a456e15ed5
changeset: 1600:b2a456e15ed5
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Sat Jan 28 14:25:41 2012 +0100
description:
ManageSieve: cleaned up parser and updated it to match newer structure of imap parser.

diffstat:

 src/lib-managesieve/Makefile.am             |    2 +
 src/lib-managesieve/managesieve-arg.c       |  129 +++++++++++++++++++++
 src/lib-managesieve/managesieve-arg.h       |  111 ++++++++++++++++++
 src/lib-managesieve/managesieve-parser.c    |  172 +++++++++-------------------
 src/lib-managesieve/managesieve-parser.h    |   91 +--------------
 src/managesieve-login/client-authenticate.c |   17 +-
 src/managesieve-login/client-authenticate.h |    4 +-
 src/managesieve-login/client.c              |   23 ++-
 src/managesieve-login/managesieve-proxy.c   |   23 ++-
 src/managesieve/cmd-havespace.c             |    6 +-
 src/managesieve/cmd-noop.c                  |    8 +-
 src/managesieve/cmd-putscript.c             |    9 +-
 src/managesieve/managesieve-client.c        |   15 +-
 src/managesieve/managesieve-client.h        |    9 +-
 14 files changed, 358 insertions(+), 261 deletions(-)

diffs (truncated from 1089 to 300 lines):

diff -r 968410a02493 -r b2a456e15ed5 src/lib-managesieve/Makefile.am
--- a/src/lib-managesieve/Makefile.am	Fri Jan 27 18:57:42 2012 +0100
+++ b/src/lib-managesieve/Makefile.am	Sat Jan 28 14:25:41 2012 +0100
@@ -5,9 +5,11 @@
 	-I$(top_srcdir)
 
 libmanagesieve_a_SOURCES = \
+	managesieve-arg.c \
 	managesieve-quote.c \
 	managesieve-parser.c 
 
 noinst_HEADERS = \
+	managesieve-arg.h \
 	managesieve-quote.h \
 	managesieve-parser.h 
diff -r 968410a02493 -r b2a456e15ed5 src/lib-managesieve/managesieve-arg.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-managesieve/managesieve-arg.c	Sat Jan 28 14:25:41 2012 +0100
@@ -0,0 +1,129 @@
+/* Copyright (c) 2002-2012 Pigeonhole authors, see the included COPYING file
+ */
+
+#include "lib.h"
+#include "managesieve-arg.h"
+
+bool managesieve_arg_get_atom
+(const struct managesieve_arg *arg, const char **str_r)
+{
+	if (arg->type != MANAGESIEVE_ARG_ATOM)
+		return FALSE;
+
+	*str_r = arg->_data.str;
+	return TRUE;
+}
+
+bool managesieve_arg_get_number
+(const struct managesieve_arg *arg, uoff_t *number_r)
+{
+	const char *data;
+	uoff_t num = 0;
+	size_t i;
+
+	if ( arg->type != MANAGESIEVE_ARG_ATOM )
+		return FALSE;
+
+	data = arg->_data.str;
+	for ( i = 0; i < arg->str_len; i++ ) {
+		uoff_t newnum;
+
+		if (data[i] < '0' || data[i] > '9')
+			return FALSE;
+
+		newnum = num*10 + (data[i] -'0');
+		if ( newnum < num )
+			return FALSE;
+
+		num = newnum;
+	}
+	
+	*number_r = num;
+	return TRUE;    
+}
+
+bool managesieve_arg_get_quoted
+(const struct managesieve_arg *arg, const char **str_r)
+{
+	if (arg->type != MANAGESIEVE_ARG_STRING)
+		return FALSE;
+
+	*str_r = arg->_data.str;
+	return TRUE;
+}
+
+bool managesieve_arg_get_string
+(const struct managesieve_arg *arg, const char **str_r)
+{
+	if (arg->type != MANAGESIEVE_ARG_STRING
+		&& arg->type != MANAGESIEVE_ARG_LITERAL)
+		return FALSE;
+
+	*str_r = arg->_data.str;
+	return TRUE;
+}
+
+bool managesieve_arg_get_string_stream
+(const struct managesieve_arg *arg, struct istream **stream_r)
+{
+	if ( arg->type != MANAGESIEVE_ARG_STRING_STREAM )
+		return FALSE;
+
+	*stream_r = arg->_data.str_stream;
+	return TRUE;
+}
+
+bool managesieve_arg_get_list
+(const struct managesieve_arg *arg, const struct managesieve_arg **list_r)
+{
+	unsigned int count;
+
+	return managesieve_arg_get_list_full(arg, list_r, &count);
+}
+
+bool managesieve_arg_get_list_full
+(const struct managesieve_arg *arg, const struct managesieve_arg **list_r,
+	unsigned int *list_count_r)
+{
+	unsigned int count;
+
+	if (arg->type != MANAGESIEVE_ARG_LIST)
+		return FALSE;
+
+	*list_r = array_get(&arg->_data.list, &count);
+
+	/* drop MANAGESIEVE_ARG_EOL from list size */
+	i_assert(count > 0);
+	*list_count_r = count - 1;
+	return TRUE;
+}
+
+struct istream *managesieve_arg_as_string_stream
+(const struct managesieve_arg *arg)
+{
+	struct istream *stream;
+
+	if (!managesieve_arg_get_string_stream(arg, &stream))
+		i_unreached();
+	return stream;
+}
+
+const struct managesieve_arg *
+managesieve_arg_as_list(const struct managesieve_arg *arg)
+{
+	const struct managesieve_arg *ret;
+
+	if (!managesieve_arg_get_list(arg, &ret))
+		i_unreached();
+	return ret;
+}
+
+bool managesieve_arg_atom_equals
+(const struct managesieve_arg *arg, const char *str)
+{
+	const char *value;
+
+	if (!managesieve_arg_get_atom(arg, &value))
+		return FALSE;
+	return strcasecmp(value, str) == 0;
+}
diff -r 968410a02493 -r b2a456e15ed5 src/lib-managesieve/managesieve-arg.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-managesieve/managesieve-arg.h	Sat Jan 28 14:25:41 2012 +0100
@@ -0,0 +1,111 @@
+/* Copyright (c) 2002-2012 Pigeonhole authors, see the included COPYING file
+ */
+
+#ifndef __MANAGESIEVE_ARG_H
+#define __MANAGESIEVE_ARG_H
+
+#include "array.h"
+
+/*
+ * QUOTED-SPECIALS    = <"> / "\"
+ */
+#define IS_QUOTED_SPECIAL(c) \
+	((c) == '"' || (c) == '\\')
+
+/* 
+ * ATOM-SPECIALS      = "(" / ")" / "{" / SP / CTL / QUOTED-SPECIALS
+ */
+#define IS_ATOM_SPECIAL(c) \
+	((c) == '(' || (c) == ')' || (c) == '{' || \
+	 (c) <= 32 || (c) == 0x7f || \
+	 IS_QUOTED_SPECIAL(c)) 
+
+/* 
+ * CHAR               = %x01-7F
+ */
+#define IS_CHAR(c) \
+	(((c) & 0x80) == 0)
+
+/* 
+ * TEXT-CHAR          = %x01-09 / %x0B-0C / %x0E-7F
+ *                       ;; any CHAR except CR and LF
+ */
+#define IS_TEXT_CHAR(c) \
+	(IS_CHAR(c) && (c) != '\r' && (c) != '\n')
+
+/*
+ * SAFE-CHAR          = %x01-09 / %x0B-0C / %x0E-21 /
+ *                      %x23-5B / %x5D-7F
+ *                      ;; any TEXT-CHAR except QUOTED-SPECIALS
+ */
+#define IS_SAFE_CHAR(c) \
+	(IS_TEXT_CHAR(c) && !IS_QUOTED_SPECIAL(c))
+
+enum managesieve_arg_type {
+	MANAGESIEVE_ARG_NONE = 0,
+	MANAGESIEVE_ARG_ATOM,
+	MANAGESIEVE_ARG_STRING,
+	MANAGESIEVE_ARG_STRING_STREAM,
+
+	MANAGESIEVE_ARG_LIST,
+
+	/* literals are returned as MANAGESIEVE_ARG_STRING by default */
+	MANAGESIEVE_ARG_LITERAL,
+
+	MANAGESIEVE_ARG_EOL /* end of argument list */
+};
+
+ARRAY_DEFINE_TYPE(managesieve_arg_list, struct managesieve_arg);
+struct managesieve_arg {
+	enum managesieve_arg_type type;
+	struct managesieve_arg *parent; /* always of type MANAGESIEVE_ARG_LIST */
+
+	/* Set when _data.str is set */
+	size_t str_len;
+
+	union {
+		const char *str;
+		struct istream *str_stream;
+		ARRAY_TYPE(managesieve_arg_list) list;
+	} _data;
+};
+
+#define MANAGESIEVE_ARG_IS_EOL(arg) \
+	((arg)->type == MANAGESIEVE_ARG_EOL)
+
+bool managesieve_arg_get_atom
+	(const struct managesieve_arg *arg, const char **str_r)
+	ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_number
+	(const struct managesieve_arg *arg, uoff_t *number_r)
+	ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_quoted
+	(const struct managesieve_arg *arg, const char **str_r)
+	ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_string
+	(const struct managesieve_arg *arg, const char **str_r)
+	ATTR_WARN_UNUSED_RESULT;
+
+bool managesieve_arg_get_string_stream
+	(const struct managesieve_arg *arg, struct istream **stream_r)
+	ATTR_WARN_UNUSED_RESULT;
+
+bool managesieve_arg_get_list
+	(const struct managesieve_arg *arg, const struct managesieve_arg **list_r)
+	ATTR_WARN_UNUSED_RESULT;
+bool managesieve_arg_get_list_full
+	(const struct managesieve_arg *arg, const struct managesieve_arg **list_r,
+		unsigned int *list_count_r)
+	ATTR_WARN_UNUSED_RESULT;
+
+/* Similar to above, but assumes that arg is already of correct type. */
+struct istream *managesieve_arg_as_string_stream
+	(const struct managesieve_arg *arg);
+const struct managesieve_arg *managesieve_arg_as_list
+	(const struct managesieve_arg *arg);
+
+/* Returns TRUE if arg is atom and case-insensitively matches str */
+bool managesieve_arg_atom_equals
+	(const struct managesieve_arg *arg, const char *str);
+
+#endif
diff -r 968410a02493 -r b2a456e15ed5 src/lib-managesieve/managesieve-parser.c
--- a/src/lib-managesieve/managesieve-parser.c	Fri Jan 27 18:57:42 2012 +0100
+++ b/src/lib-managesieve/managesieve-parser.c	Sat Jan 28 14:25:41 2012 +0100
@@ -11,7 +11,7 @@
 #define is_linebreak(c) \
 	((c) == '\r' || (c) == '\n')
 
-#define LIST_ALLOC_SIZE 7
+#define LIST_INIT_COUNT 7
 
 enum arg_parse_type {
 	ARG_PARSE_NONE = 0,
@@ -30,8 +30,9 @@
 
 	/* reset by managesieve_parser_reset(): */
 	size_t line_size;
-	struct managesieve_arg_list *root_list;
-	struct managesieve_arg_list *cur_list;
+	ARRAY_TYPE(managesieve_arg_list) root_list;
+	ARRAY_TYPE(managesieve_arg_list) *cur_list;
+	struct managesieve_arg *list_arg;
 
 	enum arg_parse_type cur_type;
 	size_t cur_pos; /* parser position in input buffer */
@@ -52,22 +53,6 @@
 static struct istream *quoted_string_istream_create
 	(struct managesieve_parser *parser);
 
-/* @UNSAFE */
-#define LIST_REALLOC(parser, old_list, new_size) \
-	p_realloc((parser)->pool, old_list, \
-		  sizeof(struct managesieve_arg_list) + \
-		  (old_list == NULL ? 0 : \
-		   sizeof(struct managesieve_arg_list) * (old_list)->alloc), \
-		  sizeof(struct managesieve_arg_list) * (new_size))
-
-static void managesieve_args_realloc(struct managesieve_parser *parser, size_t size)


More information about the dovecot-cvs mailing list