/*= -*- c-file-style: "linux"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server.
 * $Id: string.c,v 1.11 2001/08/01 09:05:34 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "includes.h"

/*
 * Get the next token from a string, return False if none found.
 *
 * \param ptr points to a pointer the line to be scanned, updated to
 * point after the token.
 *
 * \param ptr on return, points to the start of the token.
 *
 * Based on a routine by GJC@VILLAGE.COM. 
 * Extensively modified by Andrew.Tridgell@anu.edu.au
 *
 * Static variables removed by mbp@samba.org */
char const * n4_next_token(TALLOC_CTX *pool, char const **line, char *sep)
{
    const char *s;
    size_t len=1;
    char const *start;

    if (!line)
        return NULL;

    s = *line;

    /* default to simple separators */
    if (!sep) sep = " \t\n\r";

    /* find the first non sep char */
    while (*s && strchr(sep,*s)) s++;

    /* nothing left? */
    if (! *s)
        return NULL;

    start = s;
    /* copy over the token */
    for (; *s && !strchr(sep, *s); s++) 
	    len++;
   
    *line = s;
    
    return talloc_strdupn(pool, start, len-1);
}


/**
 * Split a string into an argv-style array.
 **/
char **n4_split_string (TALLOC_CTX *pool,
			 char const *line)
{
	const int max_strings = 256;
	int i = 0;
	char **p, **argv;
	
	p = argv = (char **) talloc (pool, sizeof argv[0] * max_strings);
	while ((*p++ = n4_next_quoted_inpool (pool, &line))
	       && (i++ < max_strings))
		;
	*p = 0;

	return argv;
}


/**
 * Return the next quoted string from \p line, using vaguely unixy
 * quoting.  \\ escapes all characters, and single and double quotes
 * escape each other.
 *
 * Returns NULL if there are no more quoted strings.
 **/
char *n4_next_quoted_inpool (TALLOC_CTX *pool,
			    char const **line)
{
    const char                  *s;
    char                        *buf, *c;
    size_t                      len;
    gboolean                    in_single = FALSE, in_double = FALSE;
    
    if (!line)
        return NULL;

    len = n4_count_next_quoted(*line);

    if (len == 0)
        return NULL;

    c = buf = talloc(pool, len);

    for (s = *line; isspace(*s); s++)
        ;

    for (; ; s++) {
        if ((!in_single && !in_double && isspace(*s)) || !*s) {
            *c++ = '\0';
            goto DONE;
        } else if (*s == '\\') {
            *c = *++s;
            if (!*c)
                goto DONE;
            c++;
            // yeah, i know this could be one line
        } else if (!in_double && *s == '\'') {
            in_single = !in_single;
        } else if (!in_single && *s == '\"') {
            in_double = !in_double;
        } else {
            *c++ = *s;
        }
    }
 DONE:

    n4_assert(strlen(buf)+1 == len);
    *line = s;

    return buf;
}


size_t n4_count_next_quoted(char const *line)
{
    char const *s;
    size_t                      len = 0;
    gboolean                    in_single = FALSE, in_double = FALSE;

    if (!line)
        return 0;

    for (s = line; isspace(*s); s++)
        ;

    /* if there is only leading space and no content at all, then we
       return 0 because the resulting string will be NULL, not even
       '\0'. */
    if (!*s)
        return 0;

    for (; ; s++) {
        if ((!in_single && !in_double && isspace(*s)) || !*s) {
            return ++len;
        } else if (*s == '\\') {
            len++;              // the backslash and following character
                                // collapse into one
            if (!*++s)
                return len;
        } else if (!in_double && *s == '\'') {
            in_single = !in_single;
        } else if (!in_single && *s == '\"') {
            in_double = !in_double;
        } else {
            len++;
        }
    }
}
