Re: negative boolean config expressions patch

From: Alex Rousskov <rousskov_at_measurement-factory.com>
Date: Fri, 07 Mar 2014 15:40:00 -0700

On 03/07/2014 11:04 AM, Nikolai Gorchilov wrote:
> Dear Squid Devs,
>
> In order to simplify an already complicated squid config I needed a
> negative boolean check like this:
> if ${process_number} != 1
> ... do something in case this is NOT kid1
> endif

The "else" clause is also supported, but I agree that we should add
support for more operators, including !=.

> Here's the patch, in case you find it interesting for inclusion in the trunk:
> ===[cut]===
> --- src/cache_cf.cc 2014-03-07 18:18:30.041298048 +0200
> +++ src/cache_cf.cc 2014-03-07 18:19:48.773297871 +0200
> @@ -429,9 +429,10 @@
> } else if (strcmp(expr, "false") == 0) {
> return false;
> } else if (const char* equation = strchr(expr, '=')) {
> + int positive = (int) expr[equation - expr - 1] != '!';
> const char* rvalue = skip_ws(equation + 1);
> - char* lvalue = (char*)xmalloc(equation - expr + 1);
> - xstrncpy(lvalue, expr, equation - expr + 1);
> + char* lvalue = (char*)xmalloc(equation - expr + positive);
> + xstrncpy(lvalue, expr, equation - expr + positive);
> trim_trailing_ws(lvalue);
>
> long number1;
> @@ -442,7 +443,7 @@
> fatalf("String is not a integer number: '%s'\n", rvalue);
>
> xfree(lvalue);
> - return number1 == number2;
> + return positive == 1 ? number1 == number2 : number1 != number2;
> }
> fatalf("Unable to evaluate expression '%s'\n", expr);
> return false; // this place cannot be reached
> ===[cut]===

> P.S. Pls. excuse my coding style, I didn't do any c++ since early 90s
> :)

You probably felt comfortable with the original code above then. It is
as unC++ as it gets :-)!

> I don't mind if you rewrite it completely :)

Yes, this needs to be rewritten to avoid the assumption that "equation"
and "expr" are always different. More importantly, if somebody works on
polishing this, please reshape the code to add support for all common
comparison operators for integers (=,!=,<.>,>=,<=).

Thank you,

Alex.
Received on Fri Mar 07 2014 - 22:40:03 MST

This archive was generated by hypermail 2.2.0 : Sat Mar 08 2014 - 12:00:11 MST