/* Parse comma separate list into words.
   Copyright (C) 2009-2017 National Technology & Engineering Solutions of
   Sandia, LLC (NTESS).  Under the terms of Contract DE-NA0003525 with
   NTESS, the U.S. Government retains certain rights in this software.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are
   met:

       * Redistributions of source code must retain the above copyright
         notice, this list of conditions and the following disclaimer.

       * Redistributions in binary form must reproduce the above
         copyright notice, this list of conditions and the following
         disclaimer in the documentation and/or other materials provided
         with the distribution.

       * Neither the name of NTESS nor the names of its
         contributors may be used to endorse or promote products derived
         from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If
   not, write to the Free Software Foundation, Inc., 51 Franklin St,
   Fifth Floor, Boston, MA 02110-1301 USA.  */

/* This code has been modified from the original to conform to the action
   of the getsubopt() function contained in the C library of many Unix
   platforms. Namely, the equal sign ("=") is NOT returned in the valuep
   string if it is the first character.

   Modified by Gary L. Hennigan, SNL, Dept. 9221, 3 Feb 1997 */

#include <cstring> // for strchr, memchr, memcmp

/* Parse comma separated suboption from *OPTIONP and match against
   strings in TOKENS.  If found return index and set *VALUEP to
   optional value introduced by an equal sign.  If the suboption is
   not part of TOKENS return in *VALUEP beginning of unknown
   suboption.  On exit *OPTIONP is set to the beginning of the next
   token or at the terminating NUL character.  */
int md_getsubopt(char **optionp, const char **tokens, char **valuep)
{
  char *endp, *vstart;
  int   cnt;

  if (**optionp == '\0') {
    return -1;
  }

  /* Find end of next token.  */
  endp = strchr(*optionp, ',');
  if (endp == nullptr) {
    endp = strchr(*optionp, '\0');
  }

  /* Find start of value.  */
  vstart = reinterpret_cast<char *>(memchr(*optionp, '=', endp - *optionp));
  if (vstart == nullptr) {
    vstart = endp;
  }

  /* Try to match the characters between *OPTIONP and VSTART against
     one of the TOKENS.  */
  for (cnt = 0; tokens[cnt] != nullptr; ++cnt) {
    if (memcmp(*optionp, tokens[cnt], vstart - *optionp) == 0 &&
        tokens[cnt][vstart - *optionp] == '\0') {
      /* We found the current option in TOKENS.  */
      *valuep = vstart != endp ? vstart + 1 : nullptr;

      if (*endp != '\0') {
        *endp++ = '\0';
      }
      *optionp = endp;

      return cnt;
    }
  }

  /* The current suboption does not match any option.  */
  *valuep = *optionp;

  if (*endp != '\0') {
    *endp++ = '\0';
  }
  *optionp = endp;

  return -1;
}
