| 1 |
/* |
| 2 |
* The Regina Rexx Interpreter |
| 3 |
* Copyright (C) 1992-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 |
#if defined( __EMX__ ) && !defined(DOS) |
| 21 |
# include <os2.h> |
| 22 |
# define DONT_TYPEDEF_PFN |
| 23 |
#endif |
| 24 |
|
| 25 |
#if defined(__WATCOMC__) && defined(OS2) |
| 26 |
# define INCL_DOS |
| 27 |
# include <os2.h> |
| 28 |
# define DONT_TYPEDEF_PFN |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "rexx.h" |
| 32 |
#include <stdio.h> |
| 33 |
#if !defined(VMS) /* MH 10-06-96 */ |
| 34 |
# ifdef HAVE_UNISTD_H |
| 35 |
# include <unistd.h> /* MH 10-06-96 */ |
| 36 |
# endif |
| 37 |
# ifdef HAVE_PWD_H |
| 38 |
# include <pwd.h> /* MH 10-06-96 */ |
| 39 |
# endif /* MH 10-06-96 */ |
| 40 |
#endif |
| 41 |
|
| 42 |
#if defined(WIN32) |
| 43 |
# ifdef _MSC_VER |
| 44 |
# if _MSC_VER >= 1100 |
| 45 |
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */ |
| 46 |
# pragma warning(disable: 4115 4201 4214 4514) |
| 47 |
# endif |
| 48 |
# endif |
| 49 |
# include <windows.h> |
| 50 |
# include <conio.h> |
| 51 |
# ifdef _MSC_VER |
| 52 |
# if _MSC_VER >= 1100 |
| 53 |
# pragma warning(default: 4115 4201 4214) |
| 54 |
# endif |
| 55 |
# endif |
| 56 |
#endif |
| 57 |
|
| 58 |
extern void getcallstack( tsd_t *TSD, streng *stem ); |
| 59 |
|
| 60 |
streng *rex_userid( tsd_t *TSD, cparamboxptr parms ) |
| 61 |
{ |
| 62 |
#if defined(WIN32) |
| 63 |
char buf[100]; |
| 64 |
DWORD bufsize=sizeof( buf ); |
| 65 |
#endif |
| 66 |
checkparam( parms, 0, 0 , "USERID" ) ; |
| 67 |
#if defined(VMS) || defined(MAC) || ( defined(__WATCOMC__) && !defined(__QNX__) ) || defined(_MSC_VER) || defined(_AMIGA) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(__EPOC32__) || defined(__LCC__) |
| 68 |
# if defined(WIN32) && !defined(WDOSX) |
| 69 |
if ( GetUserName( buf, &bufsize ) ) |
| 70 |
{ |
| 71 |
return( Str_creTSD( buf ) ) ; |
| 72 |
} |
| 73 |
else |
| 74 |
return nullstringptr( ) ; |
| 75 |
# else |
| 76 |
return nullstringptr( ) ; |
| 77 |
# endif |
| 78 |
#else |
| 79 |
{ |
| 80 |
struct passwd *pwuid; |
| 81 |
pwuid = getpwuid( getuid( ) ); |
| 82 |
if ( pwuid ) |
| 83 |
return( Str_creTSD( pwuid->pw_name ) ) ; |
| 84 |
else |
| 85 |
return nullstringptr( ) ; |
| 86 |
} |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
* Returns the file name of the specified caller level |
| 92 |
* - default is immediate parent |
| 93 |
* RFE #35 |
| 94 |
*/ |
| 95 |
streng *rex_getcaller( tsd_t *TSD, cparamboxptr parms ) |
| 96 |
{ |
| 97 |
long i,level=1; |
| 98 |
struct systeminfobox *curr; |
| 99 |
|
| 100 |
checkparam( parms, 0, 1 , "GETCALLER" ) ; |
| 101 |
|
| 102 |
if (parms->value) |
| 103 |
{ |
| 104 |
level = atopos( TSD, parms->value, "GETCALLER", 1 ) ; |
| 105 |
} |
| 106 |
for ( curr = TSD->systeminfo, i = 0; i < level; i++, curr = curr->previous) |
| 107 |
{ |
| 108 |
if ( !curr->previous ) |
| 109 |
return nullstringptr( ) ; |
| 110 |
} |
| 111 |
return Str_dupTSD( curr->input_file ); |
| 112 |
#if 0 |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
if ( TSD->systeminfo->previous ) |
| 117 |
return Str_dupTSD( TSD->systeminfo->previous->input_file ); |
| 118 |
else |
| 119 |
return nullstringptr( ) ; |
| 120 |
} |
| 121 |
#endif |
| 122 |
} |
| 123 |
|
| 124 |
streng *rex_getcallstack( tsd_t *TSD, cparamboxptr parms ) |
| 125 |
{ |
| 126 |
checkparam( parms, 0, 1 , "GETCALLSTACK" ) ; |
| 127 |
if (parms->value) |
| 128 |
getcallstack( TSD, parms->value ); |
| 129 |
else |
| 130 |
// exiterror( ERR_INCORRECT_CALL, 28, "GETCALLSTACK", "", tmpstr_of( TSD, parms->value ) ) ; |
| 131 |
getcallstack( TSD, NULL ); |
| 132 |
|
| 133 |
return nullstringptr( ) ; |
| 134 |
} |
| 135 |
|
| 136 |
streng *rex_rxqueue( tsd_t *TSD, cparamboxptr parms ) |
| 137 |
{ |
| 138 |
char opt='?'; |
| 139 |
streng *result=NULL; |
| 140 |
int rc; |
| 141 |
|
| 142 |
checkparam( parms, 1, 3 , "RXQUEUE" ) ; |
| 143 |
|
| 144 |
opt = getoptionchar( TSD, parms->value, "RXQUEUE", 1, "CDGS", "T" ) ; |
| 145 |
switch ( opt ) |
| 146 |
{ |
| 147 |
case 'C': /* Create */ |
| 148 |
if ( ( parms->next ) |
| 149 |
&& ( parms->next->value ) ) |
| 150 |
rc = create_queue( TSD, parms->next->value, &result ); |
| 151 |
/* result created by create_queue() */ |
| 152 |
else |
| 153 |
rc = create_queue( TSD, NULL, &result ); |
| 154 |
|
| 155 |
if ( result == NULL ) |
| 156 |
{ |
| 157 |
if ( rc == 5 ) |
| 158 |
exiterror( ERR_EXTERNAL_QUEUE, 104, tmpstr_of( TSD, parms->next->value ) ); |
| 159 |
else |
| 160 |
exiterror( ERR_EXTERNAL_QUEUE, 99, rc, "Creating from stack" ); |
| 161 |
} |
| 162 |
|
| 163 |
/* result created by create_queue() or an internal error occurred */ |
| 164 |
break; |
| 165 |
case 'D': /* Delete */ |
| 166 |
if ( ( parms->next ) |
| 167 |
&& ( parms->next->value ) ) |
| 168 |
result = int_to_streng( TSD, delete_queue(TSD, parms->next->value ) ); |
| 169 |
/* result created here */ |
| 170 |
else |
| 171 |
exiterror( ERR_INCORRECT_CALL, 5, "RXQUEUE", 2 ); |
| 172 |
break; |
| 173 |
case 'G': /* Get */ |
| 174 |
if ( ( parms->next ) |
| 175 |
&& ( parms->next->value ) ) |
| 176 |
exiterror( ERR_INCORRECT_CALL, 4, "RXQUEUE", 1 ); |
| 177 |
else |
| 178 |
result = get_queue( TSD ); |
| 179 |
/* result created by get_queue() */ |
| 180 |
break; |
| 181 |
case 'S': /* Set */ |
| 182 |
if ( ( parms->next ) |
| 183 |
&& ( parms->next->value ) ) |
| 184 |
result = Str_dup_TSD( TSD, set_queue( TSD, parms->next->value ) ); |
| 185 |
else |
| 186 |
exiterror( ERR_INCORRECT_CALL, 5, "RXQUEUE", 2 ); |
| 187 |
break; |
| 188 |
case 'T': /* Timeout */ |
| 189 |
if ( ( parms->next ) |
| 190 |
&& ( parms->next->value ) ) |
| 191 |
result = int_to_streng( TSD, timeout_queue(TSD, parms->next->value, NULL ) ); |
| 192 |
/* result created here */ |
| 193 |
else |
| 194 |
exiterror( ERR_INCORRECT_CALL, 5, "RXQUEUE", 3 ); |
| 195 |
break; |
| 196 |
} |
| 197 |
return result ; |
| 198 |
} |
| 199 |
|
| 200 |
char *mygetenv( const tsd_t *TSD, const char *name, char *buf, int bufsize ) |
| 201 |
{ |
| 202 |
char *ptr=NULL; |
| 203 |
#ifdef WIN32 |
| 204 |
DWORD ret=0; |
| 205 |
#else |
| 206 |
char *ptr1=NULL; |
| 207 |
#endif |
| 208 |
|
| 209 |
#ifdef WIN32 |
| 210 |
if (!buf) |
| 211 |
{ |
| 212 |
ptr = MallocTSD(100); |
| 213 |
if (!ptr) |
| 214 |
return NULL; |
| 215 |
ret = GetEnvironmentVariable( name, ptr, 100 ); |
| 216 |
if (ret == 0) |
| 217 |
{ |
| 218 |
FreeTSD(ptr); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
if (ret > 100) |
| 222 |
{ |
| 223 |
FreeTSD(ptr); |
| 224 |
ptr = MallocTSD(ret); |
| 225 |
if (!ptr) |
| 226 |
return NULL; |
| 227 |
ret = GetEnvironmentVariable( name, ptr, ret ); |
| 228 |
if (ret == 0) |
| 229 |
{ |
| 230 |
FreeTSD(ptr); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
} |
| 234 |
return ptr; |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
ret = GetEnvironmentVariable( name, buf, bufsize ); |
| 239 |
if (ret == 0) |
| 240 |
{ |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
if (ret > (DWORD) bufsize) |
| 244 |
{ |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
return buf; |
| 248 |
} |
| 249 |
#else |
| 250 |
ptr = getenv(name); |
| 251 |
if (!ptr) |
| 252 |
return NULL; |
| 253 |
if (!buf) |
| 254 |
{ |
| 255 |
ptr1 = (char *)MallocTSD((int)strlen(ptr)+1); |
| 256 |
if (!ptr1) |
| 257 |
return NULL; |
| 258 |
strcpy(ptr1, ptr); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
if (strlen(ptr) > (size_t) bufsize-1) |
| 263 |
return NULL; |
| 264 |
strcpy(buf, ptr); |
| 265 |
ptr1 = buf; |
| 266 |
} |
| 267 |
return ptr1; |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
#if !defined(__WINS__) && !defined(__EPOC32__) |
| 272 |
static int actually_pause = 1; |
| 273 |
|
| 274 |
/* |
| 275 |
* These functions are used to allow Regina to display "Press ENTER key to exit..." |
| 276 |
* in the console if it is NOT started from a console. |
| 277 |
*/ |
| 278 |
static void do_pause_at_exit( void ) |
| 279 |
{ |
| 280 |
int ch; |
| 281 |
if ( actually_pause ) |
| 282 |
{ |
| 283 |
printf("\nPress ENTER key to exit..."); |
| 284 |
fflush( stdout ); |
| 285 |
ch = getchar(); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
void set_pause_at_exit( void ) |
| 290 |
{ |
| 291 |
atexit( do_pause_at_exit ); |
| 292 |
} |
| 293 |
#endif |
| 294 |
|
| 295 |
/* You are not allowed to use TSD or __regina_get_tsd() here! */ |
| 296 |
void *IfcAllocateMemory( unsigned long size ) |
| 297 |
{ |
| 298 |
void *ret; |
| 299 |
#if defined( WIN32 ) |
| 300 |
/* We now use the Virtual-functions instead of Global... */ |
| 301 |
ret = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_READWRITE ) ; |
| 302 |
return ret; |
| 303 |
#elif defined( __EMX__ ) && !defined(DOS) |
| 304 |
if (_osmode == OS2_MODE) |
| 305 |
{ |
| 306 |
if ( (BOOL)DosAllocMem( &ret, size, fPERM|PAG_COMMIT ) ) |
| 307 |
return NULL; |
| 308 |
else |
| 309 |
return ret; |
| 310 |
} |
| 311 |
else /* DOS or something else */ |
| 312 |
{ |
| 313 |
ret = (void *)malloc( size ); |
| 314 |
return ret; |
| 315 |
} |
| 316 |
#elif defined( OS2 ) |
| 317 |
if ( (BOOL)DosAllocMem( &ret, size, fPERM|PAG_COMMIT ) ) |
| 318 |
return NULL; |
| 319 |
else |
| 320 |
return ret; |
| 321 |
#else |
| 322 |
ret = (void *)malloc( size ); |
| 323 |
return ret; |
| 324 |
#endif |
| 325 |
} |
| 326 |
|
| 327 |
/* You are not allowed to use TSD or __regina_get_tsd() here! */ |
| 328 |
unsigned long IfcFreeMemory( void *ptr ) |
| 329 |
{ |
| 330 |
#if defined( WIN32 ) |
| 331 |
/* In opposite to most(!) of the documentation from Microsoft we shouldn't |
| 332 |
* decommit and release together. This won't work at least under NT4SP6a. |
| 333 |
* We can first decommit and then release or release at once. FGC. |
| 334 |
*/ |
| 335 |
VirtualFree( ptr, 0, MEM_RELEASE ) ; |
| 336 |
#elif defined( __EMX__ ) && !defined(DOS) |
| 337 |
if (_osmode == OS2_MODE) |
| 338 |
DosFreeMem( ptr ); |
| 339 |
else /* DOS or something else */ |
| 340 |
free( ptr ); |
| 341 |
#elif defined( OS2 ) |
| 342 |
DosFreeMem( ptr ); |
| 343 |
#else |
| 344 |
free( ptr ); |
| 345 |
#endif |
| 346 |
return 0; |
| 347 |
} |