dhcpd code changes

Ashmath Khan hashmat.email at gmail.com
Wed Nov 11 10:32:01 UTC 2009


Hi Jeffrey and others,

- Add values for the new operators to the expr_op enum in includes/tree.h
> - Modify parse_expression() in common/parse.c to recognize <, >, <=, and
>  >= as operators (for <= and >=, look at how it already handles BANG to
>  distinguish ! from !=) and turn them into expressions using the new
>  operators
> - Add cases to the various functions in common/tree.c to handle recognizing
>  the new operators as boolean expressions, evaluating them, and rejecting
>  them when another expression type is called for.
> - Add cases to print_subexpression() in common/print.c to handle printing
>  expressions involving the new operators.  Don't forget this, or you'll
>  get incorrect lease file entries for leases which have triggers using
>  the new operators.


I did the code changes. Right now, I want only '<=' and '>=' to be
supported.
Please see below the code changes for the functions parse_expression,
evaluate_boolean_expression and write_expression.
Please let me know if you have any comments, if the code is ok.

Function parse_expression:
              case LESS:
        token = next_token (&val, (unsigned *)0, cfile);
        token = peek_token (&val, (unsigned *)0, cfile);
        if (token != EQUAL) {
            parse_warn (cfile, "< in boolean context without =");
            *lose = 1;
            skip_to_semi (cfile);
            if (lhs)
                expression_dereference (&lhs, MDL);
            return 0;
        }
        next_op = expr_less_equal;
        context = expression_context (rhs);

                break;

              case GREATER:
        token = next_token (&val, (unsigned *)0, cfile);
        token = peek_token (&val, (unsigned *)0, cfile);
        if (token != EQUAL) {
            parse_warn (cfile, "> in boolean context without =");
            *lose = 1;
            skip_to_semi (cfile);
            if (lhs)
                expression_dereference (&lhs, MDL);
            return 0;
        }
        next_op = expr_greater_equal;
        context = expression_context (rhs);
.....
        case expr_not_equal:
        case expr_equal:
        case expr_greater_equal:
        case expr_less_equal:

        if ((rhs_context != context_data_or_numeric) &&
            (rhs_context != context_data) &&
            (rhs_context != context_numeric) &&
            (rhs_context != context_any)) {
            parse_warn (cfile, "expecting data/numeric expression");
            skip_to_semi (cfile);
            expression_dereference (&rhs, MDL);
            *lose = 1;
            return 0;
        }
        break;

Function evaluate_boolean_expression:
          case expr_less_equal:
          case expr_greater_equal:
        bv = obv = (struct binding_value *)0;
        sleft = evaluate_expression (&bv, packet, lease, client_state,
                         in_options, cfg_options, scope,
                         expr -> data.equal [0], MDL);
        sright = evaluate_expression (&obv, packet, lease,
                          client_state, in_options,
                          cfg_options, scope,
                          expr -> data.equal [1], MDL);
        if (sleft && sright) {
            if (bv -> type != obv -> type)
            *result = 0;
            else {
            switch (obv -> type) {
              case binding_boolean: /* this may not be applicable in real */
                if (bv -> value.boolean < obv -> value.boolean)
                *result = expr -> op == expr_less_equal;
                else if(bv -> value.boolean > obv -> value.boolean)
                *result = expr -> op == expr_greater_equal;
                else
                *result = 1;
                break;

              case binding_data:/* for now do comparison only if len is
equal */
                if ((bv -> value.data.len == obv -> value.data.len) &&
                memcmp (bv -> value.data.data,
                     obv -> value.data.data,
                     obv -> value.data.len) < 0)
                *result = expr -> op == expr_less_equal;
                else if ((bv -> value.data.len == obv -> value.data.len) &&
                memcmp (bv -> value.data.data,
                     obv -> value.data.data,
                     obv -> value.data.len) > 0)
                *result = expr -> op == expr_greater_equal;
                else if(bv -> value.data.len == obv -> value.data.len)
                *result = 1;
                else
                *result = 0;
                break;

              case binding_numeric:
                if (bv -> value.intval < obv -> value.intval)
                *result = expr -> op == expr_less_equal;
                else if (bv -> value.intval > obv -> value.intval
                *result = expr -> op == expr_greater_equal;
                else
                *result = 1;
                break;

              case binding_dns: /* no support for now */
#if defined (NSUPDATE)
                *result = 0;
#else
                *result = 0;
#endif
                break;

              case binding_function: /* no support for now */
                *result = 0;
                break;
              default:
                *result = 0;
                break;
            }
            }
        } else if (!sleft && !sright)
            *result = 1;
        else    /* should the result be zero for this ? */
            *result = 0;

#if defined (DEBUG_EXPRESSIONS)
        log_debug ("bool: %sequal = %s",
               expr -> op == expr_less_equal ? "less" : "greater",
               (*result ? "true" : "false"));
#endif
        if (sleft)
            binding_value_dereference (&bv, MDL);
        if (sright)
            binding_value_dereference (&obv, MDL);
        return 1;

Function write_expression:
          case expr_less_equal:
        s = "<=";
        goto binary;

          case expr_greater_equal:
        s = ">=";
        goto binary;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.isc.org/pipermail/dhcp-workers/attachments/20091111/d6a43466/attachment.html>


More information about the dhcp-workers mailing list