dovecot-2.0-pigeonhole: Made command line tools return proper ex...

pigeonhole at rename-it.nl pigeonhole at rename-it.nl
Mon May 3 21:03:01 EEST 2010


details:   http://hg.rename-it.nl/dovecot-2.0-pigeonhole/rev/861ffb523cb9
changeset: 1268:861ffb523cb9
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Mon May 03 20:02:55 2010 +0200
description:
Made command line tools return proper exit status upon failure.

diffstat:

 src/sieve-tools/sieve-test.c     |  10 ++++++++--
 src/sieve-tools/sievec.c         |  27 ++++++++++++++++-----------
 src/sieve-tools/sieved.c         |  18 ++++++++++++------
 src/testsuite/testsuite-common.c |   6 +++---
 src/testsuite/testsuite-common.h |   2 +-
 src/testsuite/testsuite.c        |   5 ++++-
 6 files changed, 44 insertions(+), 24 deletions(-)

diffs (262 lines):

diff -r d2abac5a80c0 -r 861ffb523cb9 src/sieve-tools/sieve-test.c
--- a/src/sieve-tools/sieve-test.c	Sun May 02 10:23:14 2010 +0200
+++ b/src/sieve-tools/sieve-test.c	Mon May 03 20:02:55 2010 +0200
@@ -124,6 +124,7 @@
 	struct ostream *teststream = NULL;
 	bool force_compile = FALSE, execute = FALSE;
 	bool trace = FALSE;
+	int exit_status = EXIT_SUCCESS;
 	int ret, c;
 
 	master_service = master_service_init("sieve-test", 
@@ -266,7 +267,9 @@
 		main_sbin = sieve_tool_script_open(scriptfile);
 	}
 
-	if ( main_sbin != NULL ) {
+	if ( main_sbin == NULL ) {
+		exit_status = EXIT_FAILURE;
+	} else {
 		struct mail_user *mail_user = NULL;
 
 		/* Dump script */
@@ -422,11 +425,14 @@
 			(void) unlink(sieve_binary_path(sbin));
 		case SIEVE_EXEC_FAILURE:
 			i_info("final result: failed; resolved with successful implicit keep");
+			exit_status = EXIT_FAILURE;
 			break;
 		case SIEVE_EXEC_KEEP_FAILED:
 			i_info("final result: utter failure");
+			exit_status = EXIT_FAILURE;
 			break;
 		default:
+			exit_status = EXIT_FAILURE;
 			i_info("final result: unrecognized return value?!");	
 		}
 
@@ -461,5 +467,5 @@
 	mail_storage_service_deinit(&storage_service);
 	master_service_deinit(&master_service);
 	
-	return 0;
+	return exit_status;
 }
diff -r d2abac5a80c0 -r 861ffb523cb9 src/sieve-tools/sievec.c
--- a/src/sieve-tools/sievec.c	Sun May 02 10:23:14 2010 +0200
+++ b/src/sieve-tools/sievec.c	Mon May 03 20:02:55 2010 +0200
@@ -19,6 +19,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <dirent.h>
+#include <sysexits.h>
 
 /*
  * Print help
@@ -44,6 +45,7 @@
 	struct sieve_binary *sbin;
 	bool dump = FALSE;
 	const char *scriptfile, *outfile, *extensions;
+	int exit_status = EXIT_SUCCESS;
 		
 	sieve_tool_init(TRUE);
 
@@ -59,7 +61,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -x argument");
+				i_fatal_status(EX_USAGE, "Missing -x argument");
 			}
 			extensions = argv[i];
 		} else if (strcmp(argv[i], "-P") == 0) {
@@ -69,7 +71,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -P argument");
+				i_fatal_status(EX_USAGE, "Missing -P argument");
 			}
 
 			plugin = t_strdup(argv[i]);
@@ -80,13 +82,13 @@
 			outfile = argv[i];
 		} else {
 			print_help();
-			i_fatal("Unknown argument: %s", argv[i]);
+			i_fatal_status(EX_USAGE, "Unknown argument: %s", argv[i]);
 		}
 	}
 	
 	if ( scriptfile == NULL ) {
 		print_help();
-		i_fatal("Missing <script-file> argument");
+		i_fatal_status(EX_USAGE, "Missing <script-file> argument");
 	}
 	
 	if ( outfile == NULL && dump )
@@ -113,11 +115,12 @@
 		/* Sanity checks on some of the arguments */
 		
 		if ( dump )
-			i_fatal("the -d option is not allowed when scriptfile is a directory."); 
+			i_fatal_status(EX_USAGE, 
+				"the -d option is not allowed when scriptfile is a directory."); 
 		
 		if ( outfile != NULL )
-			i_fatal("the outfile argument is not allowed when scriptfile is a "
-				"directory."); 
+			i_fatal_status(EX_USAGE, 
+				"the outfile argument is not allowed when scriptfile is a directory."); 
 		
 		/* Open the directory */
 		if ( (dirp = opendir(scriptfile)) == NULL )
@@ -144,8 +147,7 @@
 				sbin = sieve_tool_script_compile(file, dp->d_name);
 
 				if ( sbin != NULL ) {
-					sieve_save(sbin, NULL);
-		
+					sieve_save(sbin, NULL);		
 					sieve_close(&sbin);
 				}
 			}
@@ -153,8 +155,7 @@
    
 		/* Close the directory */
 		if ( closedir(dirp) < 0 ) 
-			i_fatal("closedir(%s) failed: %m", scriptfile);
- 	
+			i_fatal("closedir(%s) failed: %m", scriptfile); 	
 	} else {
 		/* Script file (i.e. not a directory)
 		 * 
@@ -170,8 +171,12 @@
 			}
 		
 			sieve_close(&sbin);
+		} else {
+			exit_status = EXIT_FAILURE;
 		}
 	}
 		
 	sieve_tool_deinit();
+
+	return exit_status;
 }
diff -r d2abac5a80c0 -r 861ffb523cb9 src/sieve-tools/sieved.c
--- a/src/sieve-tools/sieved.c	Sun May 02 10:23:14 2010 +0200
+++ b/src/sieve-tools/sieved.c	Mon May 03 20:02:55 2010 +0200
@@ -17,6 +17,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <sysexits.h>
 
 /*
  * Print help
@@ -40,6 +41,7 @@
 	int i;
 	struct sieve_binary *sbin;
 	const char *binfile, *outfile, *extensions;
+	int exit_status = EXIT_SUCCESS;
 	
 	sieve_tool_init(TRUE);
 
@@ -52,7 +54,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -x argument");
+				i_fatal_status(EX_USAGE, "Missing -x argument");
 			}
 			extensions = argv[i];
 		} else if (strcmp(argv[i], "-P") == 0) {
@@ -62,7 +64,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -P argument");
+				i_fatal_status(EX_USAGE, "Missing -P argument");
 			}
 
 			plugin = t_strdup(argv[i]);
@@ -73,13 +75,13 @@
 			outfile = argv[i];
 		} else {
 			print_help();
-			i_fatal("unknown argument: %s", argv[i]);
+			i_fatal_status(EX_USAGE, "unknown argument: %s", argv[i]);
 		}
 	}
 	
 	if ( binfile == NULL ) {
 		print_help();
-		i_fatal("missing <sieve-binary> argument");
+		i_fatal_status(EX_USAGE, "missing <sieve-binary> argument");
 	}
 
 	sieve_tool_sieve_init(NULL);
@@ -101,9 +103,13 @@
 		sieve_tool_dump_binary_to(sbin, outfile == NULL ? "-" : outfile);
 	
 		sieve_close(&sbin);
-	} else 
+	} else {
 		i_error("failed to load binary: %s", binfile);
-	
+		exit_status = EXIT_FAILURE;
+	}
+
 	sieve_tool_deinit();
+
+	return exit_status;
 }
 
diff -r d2abac5a80c0 -r 861ffb523cb9 src/testsuite/testsuite-common.c
--- a/src/testsuite/testsuite-common.c	Sun May 02 10:23:14 2010 +0200
+++ b/src/testsuite/testsuite-common.c	Mon May 03 20:02:55 2010 +0200
@@ -186,15 +186,15 @@
 	str_free(&test_name);
 }
 
-int testsuite_testcase_result(void)
+bool testsuite_testcase_result(void)
 {
 	if ( test_failures > 0 ) {
 		printf("\nFAIL: %d of %d tests failed.\n\n", test_failures, test_index);
-		return 1;
+		return FALSE;
 	}
 
 	printf("\nPASS: %d tests succeeded.\n\n", test_index);
-	return 0;
+	return TRUE;
 }
 
 /*
diff -r d2abac5a80c0 -r 861ffb523cb9 src/testsuite/testsuite-common.h
--- a/src/testsuite/testsuite-common.h	Sun May 02 10:23:14 2010 +0200
+++ b/src/testsuite/testsuite-common.h	Mon May 03 20:02:55 2010 +0200
@@ -142,7 +142,7 @@
 void testsuite_test_succeed(string_t *reason);
 
 void testsuite_testcase_fail(const char *reason);
-int testsuite_testcase_result(void);
+bool testsuite_testcase_result(void);
 
 /*
  * Testsuite temporary directory
diff -r d2abac5a80c0 -r 861ffb523cb9 src/testsuite/testsuite.c
--- a/src/testsuite/testsuite.c	Sun May 02 10:23:14 2010 +0200
+++ b/src/testsuite/testsuite.c	Mon May 03 20:02:55 2010 +0200
@@ -290,5 +290,8 @@
 	mail_storage_service_deinit(&storage_service);
 	master_service_deinit(&master_service);
 
-	return testsuite_testcase_result();
+	if ( !testsuite_testcase_result() )
+		return EXIT_FAILURE;
+
+	return EXIT_SUCCESS;
 }


More information about the dovecot-cvs mailing list