/*
 * Copyright (c) 1997-2007, OpenFWTK Development Group
 * All rights reserved. See LICENSE.
 */

/* utils.c */

/* Copyright 1997 by Eberhard Mattes <mattes@azu.informatik.uni-stuttgart.de>
   Donated to the public domain.  No warranty.

   1997-07-22 Initial version
   1997-09-06 Several functions moved to libem
   1997-09-09 Hash stuff moved to libem */

#include <string.h>
#include <limits.h>
#include "emio.h"
#include "libemfw.h"
#include "squid-gw.h"

/* ============================= CONFIGURATION ============================= */

/* Parse the string pointed to by S as rejection policy.  The format
   is "REJ" or "REJ/log", where REJ is "comment", "copy", "escape",
   "drop", or "prefix".  FLAGS controls what values of REJ are valid;
   FLAGS is a combination of RM_COMMENT, RM_COPY, etc. This function
   returns 0 on success, -1 on failure. */

int parse_rej_policy (const char *s, struct rej_policy *dst, unsigned flags)
{
  enum rej v;
  int n, g;

  if (strncmp (s, "comment", 7) == 0)
    {
      v = REJ_COMMENT; n = 7;
    }
  else if (strncmp (s, "copy", 4) == 0)
    {
      v = REJ_COPY; n = 4;
    }
  else if (strncmp (s, "drop", 4) == 0)
    {
      v = REJ_DROP; n = 4;
    }
  else if (strncmp (s, "escape", 6) == 0)
    {
      v = REJ_ESCAPE; n = 6;
    }
  else if (strncmp (s, "prefix", 6) == 0)
    {
      v = REJ_PREFIX; n = 6;
    }
  else
    return -1;

  if (!(flags & REJ_MASK (v)))
    return -1;

  if (s[n] == 0)
    g = 0;
  else if (strcmp (s + n, "/log") == 0)
    g = 1;
  else
    return -1;

  dst->output = v;
  dst->log = g;
  return 0;
}
