/* -*- mode: c ; c-file-style: "canonware-c-style" -*-
 ******************************************************************************
 *
 * Copyright (C) 1996-2004 Jason Evans <jasone@canonware.com>.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice(s), this list of conditions and the following disclaimer
 *    unmodified other than the allowable addition of one or more
 *    copyright notices.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice(s), this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``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 HOLDER(S) 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.
 *
 ******************************************************************************
 *
 * Version: Onyx 5.1.1
 *
 ******************************************************************************/

uint32_t
nxo_l_thread_token(cw_nxo_t *a_nxo, cw_nxo_threadp_t *a_threadp,
		   const char *a_str, uint32_t a_len);

#ifndef CW_USE_INLINES
cw_nxo_t *
nxo_l_thread_trapped_arg_get(cw_nxo_t *a_nxo);

#ifdef CW_REGEX
cw_nxo_regex_cache_t *
nxo_l_thread_regex_cache_get(cw_nxo_t *a_nxo);
#endif

bool
nxoe_l_thread_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter);

cw_nxoe_t *
nxoe_l_thread_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset);
#endif

#if (defined(CW_USE_INLINES) || defined(CW_NXO_THREAD_C_))
CW_INLINE cw_nxo_t *
nxo_l_thread_trapped_arg_get(cw_nxo_t *a_nxo)
{
    cw_nxoe_thread_t *thread;

    cw_check_ptr(a_nxo);
    cw_dassert(a_nxo->magic == CW_NXO_MAGIC);

    thread = (cw_nxoe_thread_t *) a_nxo->o.nxoe;
    cw_dassert(thread->nxoe.magic == CW_NXOE_MAGIC);
    cw_assert(thread->nxoe.type == NXOT_THREAD);

    return &thread->trapped_arg;
}

#ifdef CW_REGEX
CW_INLINE cw_nxo_regex_cache_t *
nxo_l_thread_regex_cache_get(cw_nxo_t *a_nxo)
{
    cw_nxoe_thread_t *thread;

    cw_check_ptr(a_nxo);
    cw_dassert(a_nxo->magic == CW_NXO_MAGIC);

    thread = (cw_nxoe_thread_t *) a_nxo->o.nxoe;
    cw_dassert(thread->nxoe.magic == CW_NXOE_MAGIC);
    cw_assert(thread->nxoe.type == NXOT_THREAD);

    return &thread->regex_cache;
}
#endif

CW_INLINE bool
nxoe_l_thread_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter)
{
    cw_nxoe_thread_t *thread;

    thread = (cw_nxoe_thread_t *) a_nxoe;

    cw_check_ptr(thread);
    cw_dassert(thread->nxoe.magic == CW_NXOE_MAGIC);
    cw_assert(thread->nxoe.type == NXOT_THREAD);

    if (thread->tok_str != thread->buffer)
    {
	/* This shouldn't happen, since it indicates that there is an unaccepted
	 * token.  However, it's really the caller's fault, so just clean up. */
	nxa_free(thread->tok_str, thread->buffer_len);
    }

#ifdef CW_REGEX
    nxo_l_regex_cache_delete(&thread->regex_cache);
#endif

    nxa_free(thread, sizeof(cw_nxoe_thread_t));

    return false;
}

CW_INLINE cw_nxoe_t *
nxoe_l_thread_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset)
{
    cw_nxoe_t *retval;
    cw_nxoe_thread_t *thread;
    /* Used for remembering the current state of reference iteration.  This
     * function is only called by the garbage collector, so using a static
     * variable works fine. */
    static uint32_t ref_iter;

    thread = (cw_nxoe_thread_t *) a_nxoe;

    if (a_reset)
    {
	ref_iter = 0;
    }

    for (retval = NULL; retval == NULL; ref_iter++)
    {
	switch (ref_iter)
	{
	    case 0:
	    {
		retval = nxo_nxoe_get(&thread->estack);
		break;
	    }
	    case 1:
	    {
		retval = nxo_nxoe_get(&thread->istack);
		break;
	    }
	    case 2:
	    {
		retval = nxo_nxoe_get(&thread->ostack);
		break;
	    }
	    case 3:
	    {
		retval = nxo_nxoe_get(&thread->dstack);
		break;
	    }
#ifdef CW_OOP
#define CW_REF_ITER_CSTACK 1
	    case 4:
	    {
		retval = nxo_nxoe_get(&thread->cstack);
		break;
	    }
#else
#define CW_REF_ITER_CSTACK 0
#endif
	    case (4 + CW_REF_ITER_CSTACK):
	    {
		retval = nxo_nxoe_get(&thread->tstack);
		break;
	    }
	    case (5 + CW_REF_ITER_CSTACK):
	    {
		retval = nxo_nxoe_get(&thread->stdin_nxo);
		break;
	    }
	    case (6 + CW_REF_ITER_CSTACK):
	    {
		retval = nxo_nxoe_get(&thread->stdout_nxo);
		break;
	    }
	    case (7 + CW_REF_ITER_CSTACK):
	    {
		retval = nxo_nxoe_get(&thread->stderr_nxo);
		break;
	    }
	    case (8 + CW_REF_ITER_CSTACK):
	    {
		retval = nxo_nxoe_get(&thread->trapped_arg);
		break;
	    }
#ifdef CW_REGEX
	    case (9 + CW_REF_ITER_CSTACK):
	    {
		retval = nxo_l_regex_cache_ref_iter(&thread->regex_cache, true);
		if (retval == NULL)
		{
		    /* Avoid looping and hitting the default case. */
		    goto RETURN;
		}
		break;
	    }
#endif
#undef CW_REF_ITER_CSTACK
	    default:
	    {
#ifdef CW_REGEX
		retval = nxo_l_regex_cache_ref_iter(&thread->regex_cache,
						    false);
#else
		retval = NULL;
#endif
		goto RETURN;
	    }
	}
    }

    RETURN:
    return retval;
}
#endif /* (defined(CW_USE_INLINES) || defined(CW_NXO_THREAD_C_)) */
