Skip to content

Commit

Permalink
Add debug logging, and implement validation for a couple of commands …
Browse files Browse the repository at this point in the history
…that was missing.
  • Loading branch information
dburkart committed Apr 8, 2019
1 parent 56083f8 commit e8d767e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ ifeq ($(INSTALL_PREFIX),)
INSTALL_PREFIX := /usr/local
endif

ifdef DEBUG
CFLAGS += -DDEBUG
endif

include $(BASE)/Makefile.shared
include $(BASE)/gen/Makefile.env
include $(BASE)/src/Makefile.env
Expand Down
4 changes: 4 additions & 0 deletions Makefile.shared
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ ifeq ($(PLATFORM), Darwin)
CFLAGS += -mmacosx-version-min=10.9
endif

ifdef DEBUG
CFLAGS += -DDEBUG
endif

endif
51 changes: 50 additions & 1 deletion src/AST/Validation/Command.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "ASTBlock.hh"
#include "ASTNumeric.hh"
#include "ASTString.hh"
#include "ASTStringList.hh"
#include "ASTTag.hh"
#include "checksieve.h"
#include "Command.hh"

namespace sieve
Expand Down Expand Up @@ -325,14 +327,57 @@ bool validateSingleArgumentCommand(const ASTNode *node) {
return false;
}

bool validateBreakCommand(const ASTNode *node) {
const ASTCommand *command = dynamic_cast<const ASTCommand*>(node);
size_t size = command->children().size();

if (size == 0)
return true;

if (size != 2)
return false;

const ASTTag *tagChild = dynamic_cast<ASTTag *>(command->children()[0]);
const ASTString *stringChild = dynamic_cast<ASTString *>(command->children()[1]);

if (tagChild == NULL || stringChild == NULL)
return false;

return true;
}

bool validateForeverypartCommand(const ASTNode *node) {
const ASTCommand *command = dynamic_cast<const ASTCommand*>(node);
size_t size = command->children().size();

if (size != 1 && size != 3)
return false;

const ASTBlock *blockChild = dynamic_cast<ASTBlock *>(command->children()[size-1]);
if (blockChild == NULL)
return false;

if (size == 3) {
const ASTTag *tagChild = dynamic_cast<ASTTag *>(command->children()[0]);
const ASTString *stringChild = dynamic_cast<ASTString *>(command->children()[1]);

if (tagChild == NULL || stringChild == NULL)
return false;
}

return true;
}

Command::Command() {
_usage_map["addflag"] = "addflag [<variablename: string>] <list-of-flags: string-list>";
_usage_map["addheader"] = "addheader [:last] <field-name: string> <value: string>";
_usage_map["break"] = "break [:name string]";
_usage_map["deleteheader"] = "deleteheader [:index <fieldno: number> [:last]]\n\t[COMPARATOR] [MATCH-TYPE]\n\t<field-name: string>\n\t[<value-patterns: string-list>]";
_usage_map["discard"] = "discard";
_usage_map["enclose"] = "enclose <:subject string> <:headers string-list> string";
_usage_map["ereject"] = "ereject <reason: string>";
_usage_map["fileinto"] = "fileinto [:flags <list-of-flags: string-list>][:copy] <folder: string>";
_usage_map["foreverypart"] = "foreverypart [:name string] block";
_usage_map["global"] = "global <value: string-list>";
_usage_map["include"] = "include [:global / :personal] [:once] [:optional] <value: string>";
_usage_map["keep"] = "keep [:flags <list-of-flags: string-list>]";
Expand All @@ -348,11 +393,13 @@ Command::Command() {

_validation_fn_map["addflag"] = &validateIMAP4FlagsAction;
_validation_fn_map["addheader"] = &validateAddHeadersCommand;
_validation_fn_map["break"] = &validateBreakCommand;
_validation_fn_map["deleteheader"] = &validateDeleteHeadersCommand;
_validation_fn_map["discard"] = &validateBareCommand;
_validation_fn_map["enclose"] = &validateEncloseCommand;
_validation_fn_map["ereject"] = &validateSingleArgumentCommand;
_validation_fn_map["fileinto"] = &validateFileintoCommand;
_validation_fn_map["foreverypart"] = &validateForeverypartCommand;
_validation_fn_map["global"] = &validateSingleArgumentCommand;
_validation_fn_map["include"] = &validateIncludeCommand;
_validation_fn_map["keep"] = &validateKeepCommand;
Expand All @@ -368,8 +415,10 @@ Command::Command() {
}

bool Command::validate(const ASTCommand *command) {
if (!_validation_fn_map[command->value()])
if (!_validation_fn_map[command->value()]) {
DEBUGLOG(command->value(), "Command is missing validation.")
return true;
}

return _validation_fn_map[command->value()](command);
}
Expand Down
5 changes: 4 additions & 1 deletion src/AST/Validation/Test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ASTString.hh"
#include "ASTStringList.hh"
#include "ASTTag.hh"
#include "checksieve.h"
#include "Test.hh"

namespace sieve {
Expand Down Expand Up @@ -104,8 +105,10 @@ Test::Test() {
}

bool Test::validate(const ASTTest *test) {
if (!_validation_fn_map[test->value()])
if (!_validation_fn_map[test->value()]) {
DEBUGLOG(test->value(), "Test is missing validation.")
return true;
}

return _validation_fn_map[test->value()](test);
}
Expand Down
6 changes: 6 additions & 0 deletions src/checksieve.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#endif
#endif

#ifdef DEBUG
#define DEBUGLOG(i, x) std::cout << "DEBUG: " << i << ": "<< x << std::endl;
#else
#define DEBUGLOG(i, x)
#endif

#include <string>
#include "location.hh"

Expand Down
10 changes: 7 additions & 3 deletions test/5703/actions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ def test_foreverypart(self):
sieve = '''
require ["foreverypart"];
foreverypart { }
foreverypart {
discard;
}
'''
self.assertFalse(checksieve.parse_string(sieve, False))

def test_foreverypart_no_require(self):
sieve = '''
foreverypart { }
foreverypart {
discard;
}
'''
self.assertTrue(checksieve.parse_string(sieve, True))

Expand All @@ -22,7 +26,7 @@ def test_foreverypart_with_name(self):
require "foreverypart";
foreverypart :name "Cc" {
discard;
}
'''
self.assertFalse(checksieve.parse_string(sieve, False))
Expand Down

0 comments on commit e8d767e

Please sign in to comment.