dovecot-1.2-sieve: Made command line tools return proper exit st...

pigeonhole at rename-it.nl pigeonhole at rename-it.nl
Mon May 3 21:02:44 EEST 2010


details:   http://hg.rename-it.nl/dovecot-1.2-sieve/rev/e06b823fc75f
changeset: 1253:e06b823fc75f
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Mon May 03 20:02:39 2010 +0200
description:
Made command line tools return proper exit status upon failure.

diffstat:

 src/sieve-tools/sieve-test.c     |  33 ++++++++++++++++++++-------------
 src/sieve-tools/sievec.c         |  21 ++++++++++++++-------
 src/sieve-tools/sieved.c         |  16 +++++++++++-----
 src/testsuite/testsuite-common.c |   6 +++---
 src/testsuite/testsuite-common.h |   2 +-
 src/testsuite/testsuite.c        |  16 ++++++++++------
 6 files changed, 59 insertions(+), 35 deletions(-)

diffs (truncated from 394 to 300 lines):

diff -r 9f079160adf8 -r e06b823fc75f src/sieve-tools/sieve-test.c
--- a/src/sieve-tools/sieve-test.c	Fri Apr 30 09:23:35 2010 +0200
+++ b/src/sieve-tools/sieve-test.c	Mon May 03 20:02:39 2010 +0200
@@ -22,6 +22,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <pwd.h>
+#include <sysexits.h>
 
 /* 
  * Configuration
@@ -108,6 +109,7 @@
 	struct ostream *teststream = NULL;
 	bool force_compile = FALSE, execute = FALSE;
 	bool trace = FALSE;
+	int exit_status = EXIT_SUCCESS;
 	int ret;
 
 	sieve_tool_init();
@@ -124,7 +126,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -r argument");
+				i_fatal_status(EX_USAGE, "Missing -r argument");
 			}
 			recipient = argv[i];
 		} else if (strcmp(argv[i], "-f") == 0) {
@@ -132,7 +134,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -f argument");
+				i_fatal_status(EX_USAGE, "Missing -f argument");
 			}
 			sender = argv[i];
 		} else if (strcmp(argv[i], "-m") == 0) {
@@ -140,7 +142,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -m argument");
+				i_fatal_status(EX_USAGE, "Missing -m argument");
 			}
 			mailbox = argv[i];
 		} else if (strcmp(argv[i], "-d") == 0) {
@@ -148,7 +150,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -d argument");
+				i_fatal_status(EX_USAGE, "Missing -d argument");
 			}
 			dumpfile = argv[i];
 		} else if (strcmp(argv[i], "-l") == 0) {
@@ -156,7 +158,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -l argument");
+				i_fatal_status(EX_USAGE, "Missing -l argument");
 			}
 			mailloc = argv[i];
 		} else if (strcmp(argv[i], "-x") == 0) {
@@ -164,7 +166,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], "-S") == 0) {
@@ -174,7 +176,7 @@
 			i++;
 			if (i == argc) {
 				print_help();
-				i_fatal("Missing -S argument");
+				i_fatal_status(EX_USAGE, "Missing -S argument");
 			}
 				
 			file = t_strdup(argv[i]);
@@ -197,7 +199,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]);
@@ -208,18 +210,18 @@
 			mailfile = 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 ( mailfile == NULL ) {
 		print_help();
-		i_fatal("Missing <mail-file> argument");
+		i_fatal_status(EX_USAGE, "Missing <mail-file> argument");
 	}
 
 	sieve_tool_sieve_init(NULL);
@@ -249,7 +251,9 @@
 		main_sbin = sieve_tool_script_open(scriptfile);
 	}
 
-	if ( main_sbin != NULL ) {
+	if ( main_sbin == NULL ) {
+		exit_status = EXIT_FAILURE;
+	} else {
 		/* Dump script */
 		sieve_tool_dump_binary_to(main_sbin, dumpfile);
 	
@@ -402,12 +406,15 @@
 			(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:
 			i_info("final result: unrecognized return value?!");	
+			exit_status = EXIT_FAILURE;
 		}
 
 		if ( teststream != NULL )
@@ -438,5 +445,5 @@
 
 	sieve_tool_deinit();
 	
-	return 0;
+	return exit_status;
 }
diff -r 9f079160adf8 -r e06b823fc75f src/sieve-tools/sievec.c
--- a/src/sieve-tools/sievec.c	Fri Apr 30 09:23:35 2010 +0200
+++ b/src/sieve-tools/sievec.c	Mon May 03 20:02:39 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();
 
@@ -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 )
@@ -170,8 +173,12 @@
 			}
 		
 			sieve_close(&sbin);
+		} else {
+			exit_status = EXIT_FAILURE;
 		}
 	}
 		
 	sieve_tool_deinit();
+
+	return exit_status;
 }
diff -r 9f079160adf8 -r e06b823fc75f src/sieve-tools/sieved.c
--- a/src/sieve-tools/sieved.c	Fri Apr 30 09:23:35 2010 +0200
+++ b/src/sieve-tools/sieved.c	Mon May 03 20:02:39 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();
 
@@ -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 9f079160adf8 -r e06b823fc75f src/testsuite/testsuite-common.c
--- a/src/testsuite/testsuite-common.c	Fri Apr 30 09:23:35 2010 +0200
+++ b/src/testsuite/testsuite-common.c	Mon May 03 20:02:39 2010 +0200


More information about the dovecot-cvs mailing list