[MINOR] halog: add -hs/-HS to filter by HTTP status code range

The code was merged with the error code checking which is very similar and
which shares the same information. The new test adds about 1% slowdown to
error checking but makes it more reliable when facing wrongly formated
status codes.
diff --git a/contrib/halog/halog.c b/contrib/halog/halog.c
index 8c48d0e..39e4320 100644
--- a/contrib/halog/halog.c
+++ b/contrib/halog/halog.c
@@ -97,9 +97,12 @@
 			      FILT_COUNT_URL_TTOT|FILT_COUNT_URL_TAVG|FILT_COUNT_URL_TTOTO|FILT_COUNT_URL_TAVGO)
 
 #define FILT_HTTP_ONLY       0x200000
-#define FILT_TERM_CODE_NAME 0x400000
+#define FILT_TERM_CODE_NAME  0x400000
 #define FILT_INVERT_TERM_CODE_NAME 0x800000
 
+#define FILT_HTTP_STATUS           0x1000000
+#define FILT_INVERT_HTTP_STATUS    0x2000000
+
 unsigned int filter = 0;
 unsigned int filter_invert = 0;
 const char *line;
@@ -123,7 +126,7 @@
 		"%s"
 		"Usage: halog [-q] [-c] [-v] {-gt|-pct|-st|-tc|-srv|-u|-uc|-ue|-ua|-ut|-uao|-uto}\n"
 		"       [-s <skip>] [-e|-E] [-H] [-rt|-RT <time>] [-ad <delay>] [-ac <count>]\n"
-		"       [-tcn|-TCN <termcode>] < log\n"
+		"       [-tcn|-TCN <termcode>] [ -hs|-HS [min][:[max]] ] < log\n"
 		"\n",
 		msg ? msg : ""
 		);
@@ -409,6 +412,7 @@
 	int val, test;
 	int filter_acc_delay = 0, filter_acc_count = 0;
 	int filter_time_resp = 0;
+	int filt_http_status_low = 0, filt_http_status_high = 0;
 	int skip_fields = 1;
 
 	void (*line_filter)(const char *accept_field, const char *time_field, struct timer **tptr) = NULL;
@@ -481,6 +485,24 @@
 			filter |= FILT_TERM_CODE_NAME | FILT_INVERT_TERM_CODE_NAME;
 			filter_term_code_name = *argv;
 		}
+		else if (strcmp(argv[0], "-hs") == 0 || strcmp(argv[0], "-HS") == 0) {
+			char *sep, *str;
+
+			if (argc < 2) die("missing option for -hs/-HS ([min]:[max])");
+			filter |= FILT_HTTP_STATUS;
+			if (argv[0][1] == 'H')
+				filter |= FILT_INVERT_HTTP_STATUS;
+
+			argc--; argv++;
+			str = *argv;
+			sep = strchr(str, ':');  /* [min]:[max] */
+			if (!sep)
+				sep = str; /* make max point to min */
+			else
+				*sep++ = 0;
+			filt_http_status_low = *str ? atol(str) : 0;
+			filt_http_status_high = *sep ? atol(sep) : 65535;
+		}
 		else if (strcmp(argv[0], "-u") == 0)
 			filter |= FILT_COUNT_URL_ONLY;
 		else if (strcmp(argv[0], "-uc") == 0)
@@ -613,8 +635,8 @@
 			test &= (tps >= filter_time_resp) ^ !!(filter & FILT_INVERT_TIME_RESP);
 		}
 
-		if (filter & FILT_ERRORS_ONLY) {
-			/* only report erroneous status codes */
+		if (filter & (FILT_ERRORS_ONLY | FILT_HTTP_STATUS)) {
+			/* Check both error codes (-1, 5xx) and status code ranges */
 			if (time_field)
 				b = field_start(time_field, STATUS_FIELD - TIME_FIELD + 1);
 			else
@@ -625,12 +647,12 @@
 				continue;
 			}
 
-			if (*b == '-') {
-				test &= !!(filter & FILT_INVERT_ERRORS);
-			} else {
-				val = strl2ui(b, 3);
-				test &= (val >= 500 && val <= 599) ^ !!(filter & FILT_INVERT_ERRORS);
-			}
+			val = str2ic(b);
+			if (filter & FILT_ERRORS_ONLY)
+				test &= (val < 0 || (val >= 500 && val <= 599)) ^ !!(filter & FILT_INVERT_ERRORS);
+
+			if (filter & FILT_HTTP_STATUS)
+				test &= (val >= filt_http_status_low && val <= filt_http_status_high) ^ !!(filter & FILT_INVERT_HTTP_STATUS);
 		}
 
 		if (filter & FILT_TERM_CODE_NAME) {