/*

                                   _JNJ`                        
                               .JNMH`                           
                              JMMF`       `;.                   
                            .NMM)          `MN.                 
                            MMM)            (MML                
                           (MMM`             MMML               
                     M  I  D  N  I  G  H  T    C  o  D  E       
                           (NMMF             MHNH               
                            NMML            .MMM                
                             NMML          .NMH                 
                              4MMNL       .#F                   
                               `4HNNL_   `                      
                                   `"""`                        

                          Copyright (C) 2004-2005
       "Ian (Larry) Latter" <ian dot latter at midnightcode dot org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, and mirrored at the Midnight Code web
    site; as at version 2 of the License only.
  
    This program 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
    General Public License for more details.
  
    You should have received a copy of the GNU General Public License
    (version 2) along with this program; if not, write to the Free
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA, or see http://midnightcode.org/gplv2.txt

*/

/* midnightcode-parse.c :: Midnight Code parse library			*/


#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>


#include "midnightcode-parse.h"


int
midnightcode_parse_any(char *haystack, char *needle) {

  while(*needle)
    if(strchr(haystack, *needle++) != NULL)
      return 1;

  return 0;
}


int
midnightcode_parse_yesno(char *string_buf, int default_return) {
  int string_len;


  string_len = strlen(string_buf);

  switch(string_len) {
    case 5:
      if(strcasecmp(string_buf, "false") == 0)
        return 0;
      break;

    case 4:
      if(strcasecmp(string_buf, "true") == 0)
        return 1;
      break;

    case 3:
      if(strcasecmp(string_buf, "yes") == 0)
        return 1;
      if(strcasecmp(string_buf, "yep") == 0)
        return 1;
      if(strcasecmp(string_buf, "yeh") == 0)
        return 1;
      if(strcasecmp(string_buf, "yay") == 0)
        return 1;
      if(strcasecmp(string_buf, "nup") == 0)
        return 0;
      if(strcasecmp(string_buf, "nah") == 0)
        return 0;
      if(strcasecmp(string_buf, "nay") == 0)
        return 0;
      if(strcasecmp(string_buf, "off") == 0)
        return 0;
      break;

    case 2:
      if(strcasecmp(string_buf, "on") == 0)
        return 1;
      if(strcasecmp(string_buf, "no") == 0)
        return 0;
      break;

    case 1:
      if(strcasecmp(string_buf, "y") == 0)
        return 1;
      if(strcasecmp(string_buf, "1") == 0)
        return 1;
      if(strcasecmp(string_buf, "n") == 0)
        return 0;
      if(strcasecmp(string_buf, "0") == 0)
        return 0;
      break;

  }

  return default_return;
}


int
midnightcode_parse_strtoarg(char *string_buf, int string_buf_max,
                            char **args, int args_max) {
  char parse_buf[16384];
  char *parse_src, *string_dst;
  int  arg_index;
  int  string_buf_free;
  int  quote_state;


  /* TODO: change 16384 to malloc of string_buf_max + 1 */
  memset(parse_buf, 0, 16384);
  strncpy(parse_buf, string_buf, sizeof(parse_buf) - 1);
  if(strlen(parse_buf) != strlen(string_buf))
    return -1;
  memset(string_buf, 0, string_buf_max);

  if(args_max == 0)
    args_max = 65535;
  if(args_max < 1)
    args_max = 1;
  args[0] = NULL;

  parse_src = parse_buf;
  string_dst = string_buf;
  arg_index = 1;
  args[arg_index] = string_dst;

  string_buf_free = string_buf_max;
  quote_state = 0;


  while(*parse_src != '\0' && string_buf_free > 0 && arg_index < args_max) {

    if(*parse_src == '\\') {
      parse_src++;
      if(*parse_src == '\\') {
        *string_dst = '\\';
      } else if(*parse_src == '"') {
        *string_dst = '"';
      } else if(*parse_src == 'a') {
        *string_dst = '\a';
      } else if(*parse_src == 'b') {
        *string_dst = '\b';
      } else if(*parse_src == 'f') {
        *string_dst = '\f';
      } else if(*parse_src == 'n') {
        *string_dst = '\n';
      } else if(*parse_src == 'r') {
        *string_dst = '\r';
      } else if(*parse_src == 't') {
        *string_dst = '\t';
      } else if(*parse_src == 'v') {
        *string_dst = '\v';
      } else {
        parse_src--;
        *string_dst = *parse_src;
      }
      string_buf_free--;
      string_dst++;

    } else if(*parse_src == '"') {
      quote_state = !quote_state;

    } else if((*parse_src == ' ') || (*parse_src == '\t')) {
      if(quote_state) {
        *string_dst = *parse_src;
        string_buf_free--;
        string_dst++;
      } else {
        while(*parse_src == ' ' || *parse_src == '\t')
          parse_src++;
        parse_src--;
        *string_dst = '\0';
        string_buf_free--;
        string_dst++;
        arg_index++;
        args[arg_index] = string_dst;
      }

    } else {
      *string_dst = *parse_src;
      string_buf_free--;
      string_dst++;

    }
    parse_src++;

  }

  *string_dst = '\0';
  arg_index++;
  args[arg_index] = NULL;


  return arg_index;
}


