1 /* -*- mode: c ; c-file-style: "canonware-c-style" -*-
2  ******************************************************************************
3  *
4  * Copyright (C) 1996-2005 Jason Evans <jasone@canonware.com>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice(s), this list of conditions and the following disclaimer
12  *    unmodified other than the allowable addition of one or more
13  *    copyright notices.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice(s), this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  ******************************************************************************
32  *
33  * Version: Onyx 5.1.2
34  *
35  ******************************************************************************/
36 
37 uint32_t
38 nxo_l_thread_token(cw_nxo_t *a_nxo, cw_nxo_threadp_t *a_threadp,
39 		   const char *a_str, uint32_t a_len);
40 
41 #ifndef CW_USE_INLINES
42 cw_nxo_t *
43 nxo_l_thread_trapped_arg_get(cw_nxo_t *a_nxo);
44 
45 #ifdef CW_REGEX
46 cw_nxo_regex_cache_t *
47 nxo_l_thread_regex_cache_get(cw_nxo_t *a_nxo);
48 #endif
49 
50 bool
51 nxoe_l_thread_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter);
52 
53 cw_nxoe_t *
54 nxoe_l_thread_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset);
55 #endif
56 
57 #if (defined(CW_USE_INLINES) || defined(CW_NXO_THREAD_C_))
58 CW_INLINE cw_nxo_t *
nxo_l_thread_trapped_arg_get(cw_nxo_t * a_nxo)59 nxo_l_thread_trapped_arg_get(cw_nxo_t *a_nxo)
60 {
61     cw_nxoe_thread_t *thread;
62 
63     cw_check_ptr(a_nxo);
64     cw_dassert(a_nxo->magic == CW_NXO_MAGIC);
65 
66     thread = (cw_nxoe_thread_t *) a_nxo->o.nxoe;
67     cw_dassert(thread->nxoe.magic == CW_NXOE_MAGIC);
68     cw_assert(thread->nxoe.type == NXOT_THREAD);
69 
70     return &thread->trapped_arg;
71 }
72 
73 #ifdef CW_REGEX
74 CW_INLINE cw_nxo_regex_cache_t *
nxo_l_thread_regex_cache_get(cw_nxo_t * a_nxo)75 nxo_l_thread_regex_cache_get(cw_nxo_t *a_nxo)
76 {
77     cw_nxoe_thread_t *thread;
78 
79     cw_check_ptr(a_nxo);
80     cw_dassert(a_nxo->magic == CW_NXO_MAGIC);
81 
82     thread = (cw_nxoe_thread_t *) a_nxo->o.nxoe;
83     cw_dassert(thread->nxoe.magic == CW_NXOE_MAGIC);
84     cw_assert(thread->nxoe.type == NXOT_THREAD);
85 
86     return &thread->regex_cache;
87 }
88 #endif
89 
90 CW_INLINE bool
nxoe_l_thread_delete(cw_nxoe_t * a_nxoe,uint32_t a_iter)91 nxoe_l_thread_delete(cw_nxoe_t *a_nxoe, uint32_t a_iter)
92 {
93     cw_nxoe_thread_t *thread;
94 
95     thread = (cw_nxoe_thread_t *) a_nxoe;
96 
97     cw_check_ptr(thread);
98     cw_dassert(thread->nxoe.magic == CW_NXOE_MAGIC);
99     cw_assert(thread->nxoe.type == NXOT_THREAD);
100 
101     if (thread->tok_str != thread->buffer)
102     {
103 	/* This shouldn't happen, since it indicates that there is an unaccepted
104 	 * token.  However, it's really the caller's fault, so just clean up. */
105 	nxa_free(thread->tok_str, thread->buffer_len);
106     }
107 
108 #ifdef CW_REGEX
109     nxo_l_regex_cache_delete(&thread->regex_cache);
110 #endif
111 
112     nxa_free(thread, sizeof(cw_nxoe_thread_t));
113 
114     return false;
115 }
116 
117 CW_INLINE cw_nxoe_t *
nxoe_l_thread_ref_iter(cw_nxoe_t * a_nxoe,bool a_reset)118 nxoe_l_thread_ref_iter(cw_nxoe_t *a_nxoe, bool a_reset)
119 {
120     cw_nxoe_t *retval;
121     cw_nxoe_thread_t *thread;
122     /* Used for remembering the current state of reference iteration.  This
123      * function is only called by the garbage collector, so using a static
124      * variable works fine. */
125     static uint32_t ref_iter;
126 
127     thread = (cw_nxoe_thread_t *) a_nxoe;
128 
129     if (a_reset)
130     {
131 	ref_iter = 0;
132     }
133 
134     for (retval = NULL; retval == NULL; ref_iter++)
135     {
136 	switch (ref_iter)
137 	{
138 	    case 0:
139 	    {
140 		retval = nxo_nxoe_get(&thread->estack);
141 		break;
142 	    }
143 	    case 1:
144 	    {
145 		retval = nxo_nxoe_get(&thread->istack);
146 		break;
147 	    }
148 	    case 2:
149 	    {
150 		retval = nxo_nxoe_get(&thread->ostack);
151 		break;
152 	    }
153 	    case 3:
154 	    {
155 		retval = nxo_nxoe_get(&thread->dstack);
156 		break;
157 	    }
158 #ifdef CW_OOP
159 #define CW_REF_ITER_CSTACK 1
160 	    case 4:
161 	    {
162 		retval = nxo_nxoe_get(&thread->cstack);
163 		break;
164 	    }
165 #else
166 #define CW_REF_ITER_CSTACK 0
167 #endif
168 	    case (4 + CW_REF_ITER_CSTACK):
169 	    {
170 		retval = nxo_nxoe_get(&thread->tstack);
171 		break;
172 	    }
173 	    case (5 + CW_REF_ITER_CSTACK):
174 	    {
175 		retval = nxo_nxoe_get(&thread->stdin_nxo);
176 		break;
177 	    }
178 	    case (6 + CW_REF_ITER_CSTACK):
179 	    {
180 		retval = nxo_nxoe_get(&thread->stdout_nxo);
181 		break;
182 	    }
183 	    case (7 + CW_REF_ITER_CSTACK):
184 	    {
185 		retval = nxo_nxoe_get(&thread->stderr_nxo);
186 		break;
187 	    }
188 	    case (8 + CW_REF_ITER_CSTACK):
189 	    {
190 		retval = nxo_nxoe_get(&thread->trapped_arg);
191 		break;
192 	    }
193 #ifdef CW_REGEX
194 	    case (9 + CW_REF_ITER_CSTACK):
195 	    {
196 		retval = nxo_l_regex_cache_ref_iter(&thread->regex_cache, true);
197 		if (retval == NULL)
198 		{
199 		    /* Avoid looping and hitting the default case. */
200 		    goto RETURN;
201 		}
202 		break;
203 	    }
204 #endif
205 #undef CW_REF_ITER_CSTACK
206 	    default:
207 	    {
208 #ifdef CW_REGEX
209 		retval = nxo_l_regex_cache_ref_iter(&thread->regex_cache,
210 						    false);
211 #else
212 		retval = NULL;
213 #endif
214 		goto RETURN;
215 	    }
216 	}
217     }
218 
219     RETURN:
220     return retval;
221 }
222 #endif /* (defined(CW_USE_INLINES) || defined(CW_NXO_THREAD_C_)) */
223