Debian Codegraph @

macros.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1993-1994 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "rexx.h"
21 #include "rxiface.h"
22
23 #include <stdio.h>
24 #include <setjmp.h>
25 #include <string.h>
26 #include <assert.h>
27
28
29 void killsystem( tsd_t *TSD, sysinfo systm )
30 {
31 if ( systm->environment )
32 {
33 Free_stringTSD( systm->environment ) ;
34 systm->environment = NULL;
35 }
36
37 if (systm->input_file )
38 {
39 Free_stringTSD( systm->input_file ) ;
40 systm->input_file = NULL;
41 }
42
43 if (systm->input_fp )
44 {
45 fclose( systm->input_fp ) ;
46 systm->input_fp = NULL;
47 }
48
49 DestroyInternalParsingTree( TSD, &systm->tree ) ;
50
51 if (systm->currlevel0)
52 {
53 removelevel( TSD, systm->currlevel0 ) ;
54 systm->currlevel0 = NULL;
55 }
56
57 if ( systm->script_exit )
58 {
59 FreeTSD( systm->script_exit ) ;
60 systm->script_exit = NULL;
61 }
62
63 if ( systm->result)
64 {
65 Free_stringTSD( systm->result ) ;
66 systm->result = NULL;
67 }
68
69 if (systm->callstack)
70 {
71 FreeTSD( systm->callstack ) ;
72 systm->callstack = NULL;
73 }
74
75 FreeTSD( systm ) ;
76 }
77
78 streng *do_instore( tsd_t * volatile TSD, const streng *name, paramboxptr args,
79 const streng *envir, int * volatile RetCode, int hooks,
80 const void *instore, unsigned long instore_length,
81 const char *instore_source,
82 unsigned long instore_source_length,
83 const internal_parser_type *ipt,
84 int ctype )
85 {
86 sysinfobox *newsystem, *tmpsys;
87 streng *ptr=NULL;
88 jmp_buf *jbuf;
89 unsigned InterpreterStatus[IPRT_BUFSIZE];
90 tsd_t * volatile saved_TSD;
91 int * volatile saved_RetCode;
92 volatile proclevel oldlevel;
93 volatile int doTermHook=0;
94
95 if ( RetCode )
96 *RetCode = 0;
97
98 SaveInterpreterStatus( TSD, InterpreterStatus );
99 jbuf = (jmp_buf *)MallocTSD( sizeof( jmp_buf ) );
100 assert( !TSD->in_protected );
101
102 saved_TSD = TSD; /* vars used until here */
103 saved_RetCode = RetCode;
104 if ( setjmp( *jbuf ) )
105 {
106 TSD = saved_TSD; /* prevents bugs like 592393 */
107 RetCode = saved_RetCode;
108
109 ptr = TSD->systeminfo->result;
110 TSD->systeminfo->result = NULL;
111 if ( !TSD->instore_is_errorfree )
112 {
113 /*
114 * In case of an error we don't return the error number as the result.
115 * Instead, use the current "result" as the error code. It is set
116 * to a negative value already by errortext().
117 *
118 * ==> errortext() uses a static buffer, if you ever need the value
119 * you have to dup it after the restore of the old system or you
120 * risk an endless loop.
121 */
122 if ( RetCode )
123 *RetCode = atoi( ptr->value );
124
125 ptr = NULL;
126 }
127 }
128 else
129 {
130 nodeptr savecurrentnode = TSD->currentnode; /* pgb fixes bug 595300 */
131
132 TSD->currentnode = NULL;
133
134 newsystem = creat_sysinfo( TSD, Str_dupTSD( envir ) );
135 newsystem->previous = TSD->systeminfo;
136 newsystem->hooks = hooks;
137 newsystem->script_exit = jbuf;
138 newsystem->invoked = ctype;
139 newsystem->input_file = Str_dupstrTSD( name );
140 set_reserved_value( TSD, POOL0_FILE, Str_dupTSD( name ), 0, VFLAG_STR );
141 newsystem->trace_override = newsystem->previous->trace_override;
142 newsystem->ctrlcounter = newsystem->previous->ctrlcounter +
143 newsystem->previous->cstackcnt;
144
145 /*
146 * see note in execute_external
147 * Fixes bug 604219
148 */
149 oldlevel = TSD->currlevel;
150
151 TSD->systeminfo = newsystem;
152 TSD->systeminfo->currlevel0 = TSD->currlevel = newlevel( TSD, NULL );
153 TSD->currlevel->pool = oldlevel->pool + 1;
154
155 TSD->currlevel->args = args;
156
157 if ( ipt )
158 TSD->systeminfo->tree = *ipt;
159 else if ( IsValidTin( (const external_parser_type *)instore, instore_length ) )
160 TSD->systeminfo->tree = ExpandTinnedTree( TSD, (const external_parser_type *)instore, instore_length,
161 instore_source,
162 instore_source_length );
163 else
164 {
165 memset( &TSD->systeminfo->tree, 0, sizeof( TSD->systeminfo->tree ) );
166 }
167 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_INIT ) )
168 hookup( TSD, HOOK_INIT );
169
170 doTermHook = 1;
171
172 if ( TSD->systeminfo->tree.root )
173 ptr = interpret( TSD, TSD->systeminfo->tree.root );
174 else
175 ptr = NULL;
176 TSD->currentnode = savecurrentnode; /* pgb */
177 set_reserved_value( TSD, POOL0_FILE, Str_dupTSD( TSD->systeminfo->previous->input_file ), 0, VFLAG_STR );
178 }
179
180 if ( doTermHook )
181 {
182 /*
183 * Be sure to try it only once.
184 */
185 doTermHook = 0;
186 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_TERMIN ) )
187 hookup( TSD, HOOK_TERMIN );
188 }
189
190 tmpsys = TSD->systeminfo;
191 TSD->systeminfo = TSD->systeminfo->previous;
192 TSD->currlevel = oldlevel;
193 TSD->trace_stat = TSD->currlevel->tracestat;
194
195 tmpsys->currlevel0->args = NULL;
196
197 /*
198 * jbuf will be freed by killsystem.
199 */
200 killsystem( TSD, tmpsys );
201
202 RestoreInterpreterStatus( TSD, InterpreterStatus );
203
204 return ptr;
205 }
206
207 streng *execute_external( tsd_t * volatile TSD, const streng *command,
208 paramboxptr args, const streng *envir,
209 int * volatile RetCode, int hooks, int ctype )
210 {
211 sysinfobox *newsystem, *tmpsys;
212 streng *ptr=NULL;
213 char *path;
214 streng *name;
215 int len;
216 FILE *fptr;
217 jmp_buf *jbuf;
218 internal_parser_type parsing;
219 volatile proclevel oldlevel;
220 unsigned InterpreterStatus[IPRT_BUFSIZE];
221 nodeptr savecurrentnode = TSD->currentnode;
222 tsd_t * volatile saved_TSD;
223 int * volatile saved_RetCode;
224 volatile int doTermHook=0;
225 int iserror=0;
226
227 if ( RetCode )
228 *RetCode = 0;
229
230 SaveInterpreterStatus( TSD, InterpreterStatus );
231 jbuf = (jmp_buf *)MallocTSD( sizeof( jmp_buf ) );
232 assert( !TSD->in_protected );
233
234 saved_TSD = TSD; /* vars used until here */
235 saved_RetCode = RetCode;
236
237 if ( setjmp( *jbuf ) )
238 {
239 TSD = saved_TSD; /* prevents bugs like 592393 */
240 RetCode = saved_RetCode;
241
242 ptr = TSD->systeminfo->result;
243 TSD->systeminfo->result = NULL;
244 if ( !TSD->instore_is_errorfree )
245 {
246 /*
247 * In case of an error we don't return the error number as the result.
248 * Instead, use the current "result" as the error code. It is set
249 * to a negative value already by errortext().
250 *
251 * ==> errortext() uses a static buffer, if you ever need the value
252 * you have to dup it after the restore of the old system or you
253 * risk an endless loop.
254 */
255 if ( RetCode )
256 *RetCode = atoi( ptr->value );
257 /*
258 * Defer the exiting with 40.1 until the jmpbuf stuff is done
259 */
260 iserror = 1;
261 ptr = NULL;
262 }
263 }
264 else
265 {
266 fptr = NULL;
267 /* FGC: Check length first to avoid */
268 /* access of invalid buffer */
269 if ( ( command->len == 7 )
270 && ( memcmp( "<stdin>", command->value, command->len ) == 0 ) )
271 {
272 fptr = stdin;
273 name = Str_dupstrTSD( command );
274 }
275 else
276 {
277 path = (char *) tmpstr_of( TSD, command );
278 while ( rx_isspace( *path ) )
279 path++;
280 len = strlen( path );
281 while ( len > 0 )
282 if ( !rx_isspace( path[len - 1] ) )
283 break;
284 else
285 len--;
286 path[len] = '\0';
287
288 name = get_external_routine( TSD, path, &fptr );
289 if ( !name )
290 {
291 FreeTSD( jbuf );
292 if ( get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
293 {
294 /*
295 * If we can't find the external routine, we should exiterror()
296 * with 43.1 - as per ANSI
297 */
298 exiterror( ERR_ROUTINE_NOT_FOUND, 1, path );
299 }
300 else
301 {
302 /*
303 * The only time this function is called with a non-NULL
304 * RetCode is from client.c, when an external routine
305 * is being executed via the SAA interface. In this case
306 * the error returned is the only thing we should do;
307 * we certainly should NOT be writing anything to stdout
308 * or stderr!
309 */
310 if ( RetCode )
311 {
312 *RetCode = -ERR_PROG_UNREADABLE;
313 }
314 return NULL;
315 }
316 }
317 }
318
319 newsystem = creat_sysinfo( TSD, Str_dupTSD( envir ) );
320 newsystem->previous = TSD->systeminfo;
321 newsystem->hooks = hooks;
322 newsystem->invoked = ctype;
323 newsystem->script_exit = jbuf;
324 newsystem->input_file = name;
325 /* RFE #36 - set .FILE reserved name */
326 set_reserved_value( TSD, POOL0_FILE, Str_dupTSD( name ), 0, VFLAG_STR );
327 newsystem->trace_override = newsystem->previous->trace_override;
328 newsystem->ctrlcounter = newsystem->previous->ctrlcounter +
329 newsystem->previous->cstackcnt;
330
331 oldlevel = TSD->currlevel;
332
333 TSD->systeminfo = newsystem;
334 TSD->systeminfo->currlevel0 = TSD->currlevel = newlevel( TSD, NULL );
335 TSD->currlevel->pool = oldlevel->pool + 1;
336
337 savecurrentnode = TSD->currentnode; /* pgb fixes bug 595300 */
338
339 TSD->currlevel->args = args;
340 TSD->currentnode = NULL;
341
342 fetch_file( TSD, fptr, &parsing );
343 if ( fptr != stdin )
344 fclose( fptr );
345 if ( parsing.result == 0 )
346 {
347 TSD->systeminfo->tree = parsing;
348 /*
349 * Execute any RXINI system exit
350 */
351 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_INIT ) )
352 hookup( TSD, HOOK_INIT );
353
354 doTermHook = 1;
355
356 ptr = interpret( TSD, TSD->systeminfo->tree.root );
357 TSD->currentnode = savecurrentnode; /* pgb */
358 set_reserved_value( TSD, POOL0_FILE, Str_dupTSD( TSD->systeminfo->previous->input_file ), 0, VFLAG_STR );
359 }
360 else
361 {
362 TSD->currentnode = savecurrentnode; /* pgb */
363 ptr = NULL;
364 set_reserved_value( TSD, POOL0_FILE, Str_dupTSD( TSD->systeminfo->previous->input_file ), 0, VFLAG_STR );
365 exiterror( ERR_YACC_SYNTAX, 1, parsing.tline );
366 }
367 }
368 /*
369 * Execute any RXTER system exit
370 */
371 if ( doTermHook )
372 {
373 /*
374 * Be sure to try it only once.
375 */
376 doTermHook = 0;
377 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_TERMIN ) )
378 hookup( TSD, HOOK_TERMIN );
379 }
380 if ( TSD->systeminfo->hooks & HOOK_MASK( HOOK_TERMIN ) )
381 hookup( TSD, HOOK_TERMIN );
382
383 tmpsys = TSD->systeminfo;
384 TSD->systeminfo = TSD->systeminfo->previous;
385 TSD->currlevel = oldlevel;
386 TSD->trace_stat = TSD->currlevel->tracestat;
387
388 tmpsys->currlevel0->args = NULL;
389
390 /*
391 * jbuf will be freed by killsystem
392 */
393 killsystem( TSD, tmpsys );
394
395 /*
396 * If the called routine failed exit with "external routine failed"
397 * depending on OPTION HALT_ON_EXT_CALL_FAIL or with STRICT_ANSI
398 */
399 if ( iserror)
400 {
401 if ( get_options_flag( TSD->currlevel, EXT_HALT_ON_EXT_CALL_FAIL )
402 || get_options_flag( TSD->currlevel, EXT_STRICT_ANSI ) )
403 {
404 char *cmd = (char *) tmpstr_of( TSD, command );
405 exiterror( 40, 1, cmd);
406 }
407 }
408
409 RestoreInterpreterStatus( TSD, InterpreterStatus );
410 /*
411 * Oops, we really ought to handle function-did-not-return-data
412 */
413 return ptr;
414 }
415
416
417 internal_parser_type enter_macro( tsd_t *TSD, const streng *source,
418 void **ept, unsigned long *extlength)
419 {
420 internal_parser_type parsing;
421
422 fetch_string( TSD, source, &parsing );
423 if (parsing.result != 0)
424 {
425 exiterror( ERR_YACC_SYNTAX, 1, parsing.tline ) ;
426 return(parsing);
427 }
428 if (ept && extlength)
429 *ept = TinTree( TSD, &parsing, extlength ) ;
430
431 return( parsing );
432 }
433
434 /*
435 * Takes as input a pointer to a parameter structure, and counts the
436 * number of parameters in it, and return that value. The counting
437 * can be performed in two different manners, either soft or hard.
438 * Soft means that "trailing" ommitted parameters are ignored, hard
439 * means that all parameters are counted. (When counting hard, all
440 * routines stared from Rexx has at least one parameter: the one that
441 * was ommitted. However, when started from C by SAA API, functions
442 * can be started with zero parameters.)
443 */
444 int count_params( cparamboxptr ptr, int soft )
445 {
446 int scnt=0, hcnt=0 ;
447
448 assert( PARAM_TYPE_SOFT && !PARAM_TYPE_HARD ) ;
449
450 for (hcnt=scnt=0; ptr; ptr=ptr->next, hcnt++)
451 if (soft && ptr->value)
452 scnt = hcnt ;
453
454 return ((soft) ? scnt : hcnt ) ;
455
456 }
457
458
459 /*
460 * Takes a pointer to an argument structure as input, together with an
461 * integer. Returns the parameter numbered by the number, or NULL if
462 * either that parameter is omitted or otherwise non-existing.
463 */
464 streng *get_parameter( paramboxptr ptr, int number )
465 {
466 assert( number>0 ) ;
467 for (; ptr && (--number!=0); ptr=ptr->next) ;
468
469 return ((ptr && ptr->value) ? ptr->value : NULL ) ;
470 }