| 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 |
#define NO_CTYPE_REPLACEMENT |
| 21 |
#include "rexx.h" |
| 22 |
#include <locale.h> |
| 23 |
#include <ctype.h> |
| 24 |
/* Do _not_ use stddef, anders! _not_ stddef! Remember that!!! |
| 25 |
(it breaks on suns running gcc without fixincludes */ |
| 26 |
#include <stdio.h> |
| 27 |
#include <limits.h> |
| 28 |
#if defined(HAVE_ASSERT_H) |
| 29 |
# include <assert.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#if defined(TIME_WITH_SYS_TIME) |
| 33 |
# include <sys/time.h> |
| 34 |
# include <time.h> |
| 35 |
#else |
| 36 |
# if defined(HAVE_SYS_TIME_H) |
| 37 |
# include <sys/time.h> |
| 38 |
# else |
| 39 |
# include <time.h> |
| 40 |
# endif |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(_AMIGA) |
| 44 |
const char *Version="$VER: "PARSE_VERSION_STRING" "__AMIGADATE__" $"; |
| 45 |
# if defined(__SASC) |
| 46 |
static const long __stack = 0x6000; |
| 47 |
# endif |
| 48 |
#endif |
| 49 |
|
| 50 |
#if defined(WIN32) |
| 51 |
# include <windows.h> |
| 52 |
#endif |
| 53 |
/* |
| 54 |
* The idea is to initialize the character operation table only once. |
| 55 |
* Everything is cached until the end of the process. |
| 56 |
* We need the uppercase/lowercase conversion table and we need a table |
| 57 |
* for the state information of each character. |
| 58 |
* |
| 59 |
* One great benefit is a homogeneous world of characters where an |
| 60 |
* attribute "is an uppercase letter" persists between different library |
| 61 |
* calls. If one has ever tried these stupid HP printer drivers for NT, |
| 62 |
* he/she will like this feature. |
| 63 |
*/ |
| 64 |
|
| 65 |
PROTECTION_VAR( locale_info ) |
| 66 |
static char *locale_lc_ctype = NULL; |
| 67 |
unsigned char_info[257] = {0, }; /* |
| 68 |
* Last char indicates what attributes |
| 69 |
* have been loaded. |
| 70 |
*/ |
| 71 |
|
| 72 |
|
| 73 |
unsigned char u_to_l[256] = { /* initially the identity */ |
| 74 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 75 |
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 76 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 77 |
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 78 |
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 79 |
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 80 |
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 81 |
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, |
| 82 |
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 83 |
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 84 |
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 85 |
0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, |
| 86 |
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 87 |
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 88 |
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 89 |
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, |
| 90 |
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, |
| 91 |
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, |
| 92 |
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, |
| 93 |
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, |
| 94 |
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
| 95 |
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, |
| 96 |
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, |
| 97 |
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
| 98 |
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 99 |
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, |
| 100 |
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, |
| 101 |
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, |
| 102 |
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
| 103 |
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 104 |
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
| 105 |
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
| 106 |
}; |
| 107 |
|
| 108 |
unsigned char l_to_u[256] = { /* initially the identity */ |
| 109 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 110 |
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 111 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 112 |
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 113 |
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, |
| 114 |
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, |
| 115 |
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 116 |
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, |
| 117 |
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 118 |
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 119 |
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 120 |
0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, |
| 121 |
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 122 |
0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 123 |
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 124 |
0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, |
| 125 |
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, |
| 126 |
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, |
| 127 |
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, |
| 128 |
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, |
| 129 |
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
| 130 |
0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, |
| 131 |
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, |
| 132 |
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
| 133 |
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 134 |
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, |
| 135 |
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, |
| 136 |
0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, |
| 137 |
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
| 138 |
0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, |
| 139 |
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
| 140 |
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
| 141 |
}; |
| 142 |
|
| 143 |
/* |
| 144 |
* This function must be called at the very start of the program. It sets |
| 145 |
* the used character locale. Valid values are the empty string or an OS |
| 146 |
* known value. |
| 147 |
*/ |
| 148 |
void set_locale_info( const char *info ) |
| 149 |
{ |
| 150 |
setlocale( LC_CTYPE, info ); |
| 151 |
#ifdef LC_MESSAGES |
| 152 |
setlocale( LC_MESSAGES, info ); |
| 153 |
#endif |
| 154 |
} |
| 155 |
|
| 156 |
/* |
| 157 |
* This function loads one piece of character information. The information |
| 158 |
* is stored system wide, and the information will persist over a fork() call. |
| 159 |
*/ |
| 160 |
static void load_info( unsigned infobit ) |
| 161 |
{ |
| 162 |
#define AUTO_LOAD( name, rxbit ) { for ( i = 0; i < 256; i++ ) { \ |
| 163 |
if ( name( i ) ) \ |
| 164 |
char_info[i] |= rxbit; \ |
| 165 |
} \ |
| 166 |
} |
| 167 |
char *current_locale; |
| 168 |
int i; |
| 169 |
|
| 170 |
THREAD_PROTECT( locale_info ) |
| 171 |
|
| 172 |
/* |
| 173 |
* The desired information may have been collected during the |
| 174 |
* THREAD_PROTECT step by another thread. Test it. |
| 175 |
*/ |
| 176 |
if ( ( char_info[256] & infobit ) == 0 ) |
| 177 |
{ |
| 178 |
if ( locale_lc_ctype == NULL ) |
| 179 |
{ |
| 180 |
/* |
| 181 |
* Get the current locale for every following usage. |
| 182 |
*/ |
| 183 |
if ( ( current_locale = setlocale( LC_CTYPE, NULL ) ) == NULL ) |
| 184 |
current_locale = "C"; |
| 185 |
if ( ( current_locale = strdup( current_locale ) ) == NULL ) |
| 186 |
current_locale = "C"; |
| 187 |
|
| 188 |
locale_lc_ctype = current_locale; |
| 189 |
setlocale( LC_CTYPE, current_locale ); |
| 190 |
} |
| 191 |
current_locale = setlocale( LC_CTYPE, NULL ); |
| 192 |
|
| 193 |
/* |
| 194 |
* The standard locale is loaded. Now collect the infos. |
| 195 |
*/ |
| 196 |
switch ( infobit ) |
| 197 |
{ |
| 198 |
case RX_ISLOWER: |
| 199 |
AUTO_LOAD( islower, infobit ); |
| 200 |
for ( i = 0; i < 256; i++ ) |
| 201 |
{ |
| 202 |
if ( char_info[i] & RX_ISLOWER ) |
| 203 |
u_to_l[(unsigned char) toupper( i )] = (unsigned char) i; |
| 204 |
} |
| 205 |
break; |
| 206 |
|
| 207 |
case RX_ISUPPER: |
| 208 |
AUTO_LOAD( isupper, infobit ); |
| 209 |
for ( i = 0; i < 256; i++ ) |
| 210 |
{ |
| 211 |
if ( char_info[i] & RX_ISUPPER ) |
| 212 |
l_to_u[(unsigned char) tolower( i )] = (unsigned char) i; |
| 213 |
} |
| 214 |
break; |
| 215 |
|
| 216 |
case RX_ISALPHA: |
| 217 |
AUTO_LOAD( isalpha, infobit ); |
| 218 |
break; |
| 219 |
|
| 220 |
case RX_ISALNUM: |
| 221 |
AUTO_LOAD( isalnum, infobit ); |
| 222 |
break; |
| 223 |
|
| 224 |
case RX_ISDIGIT: |
| 225 |
AUTO_LOAD( isdigit, infobit ); |
| 226 |
break; |
| 227 |
|
| 228 |
case RX_ISXDIGIT: |
| 229 |
AUTO_LOAD( isxdigit, infobit ); |
| 230 |
break; |
| 231 |
|
| 232 |
case RX_ISPUNCT: |
| 233 |
AUTO_LOAD( ispunct, infobit ); |
| 234 |
break; |
| 235 |
|
| 236 |
case RX_ISSPACE: |
| 237 |
AUTO_LOAD( isspace, infobit ); |
| 238 |
break; |
| 239 |
|
| 240 |
case RX_ISPRINT: |
| 241 |
AUTO_LOAD( isprint, infobit ); |
| 242 |
break; |
| 243 |
|
| 244 |
case RX_ISGRAPH: |
| 245 |
AUTO_LOAD( isgraph, infobit ); |
| 246 |
break; |
| 247 |
|
| 248 |
case RX_ISCNTRL: |
| 249 |
AUTO_LOAD( iscntrl, infobit ); |
| 250 |
break; |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
setlocale( LC_CTYPE, current_locale ); |
| 255 |
char_info[256] |= infobit; |
| 256 |
} |
| 257 |
|
| 258 |
THREAD_UNPROTECT( locale_info ) |
| 259 |
#undef AUTO_LOAD |
| 260 |
} |
| 261 |
|
| 262 |
#define Is_expand( c, bit ) if ( ! ( char_info[256] & bit ) ) \ |
| 263 |
load_info( bit ); \ |
| 264 |
return char_info[(unsigned char) c] & bit |
| 265 |
int Islower( int c ) |
| 266 |
{ |
| 267 |
Is_expand( c, RX_ISLOWER ); |
| 268 |
} |
| 269 |
|
| 270 |
int Isupper( int c ) |
| 271 |
{ |
| 272 |
Is_expand( c, RX_ISUPPER ); |
| 273 |
} |
| 274 |
|
| 275 |
int Isalpha( int c ) |
| 276 |
{ |
| 277 |
Is_expand( c, RX_ISALPHA ); |
| 278 |
} |
| 279 |
|
| 280 |
int Isalnum( int c ) |
| 281 |
{ |
| 282 |
Is_expand( c, RX_ISALNUM ); |
| 283 |
} |
| 284 |
|
| 285 |
int Isdigit( int c ) |
| 286 |
{ |
| 287 |
Is_expand( c, RX_ISDIGIT ); |
| 288 |
} |
| 289 |
|
| 290 |
int Isxdigit( int c ) |
| 291 |
{ |
| 292 |
Is_expand( c, RX_ISXDIGIT ); |
| 293 |
} |
| 294 |
|
| 295 |
int Ispunct( int c ) |
| 296 |
{ |
| 297 |
Is_expand( c, RX_ISPUNCT ); |
| 298 |
} |
| 299 |
|
| 300 |
int Isspace( int c ) |
| 301 |
{ |
| 302 |
Is_expand( c, RX_ISSPACE ); |
| 303 |
} |
| 304 |
|
| 305 |
int Isprint( int c ) |
| 306 |
{ |
| 307 |
Is_expand( c, RX_ISPRINT ); |
| 308 |
} |
| 309 |
|
| 310 |
int Isgraph( int c ) |
| 311 |
{ |
| 312 |
Is_expand( c, RX_ISGRAPH ); |
| 313 |
} |
| 314 |
|
| 315 |
int Iscntrl( int c ) |
| 316 |
{ |
| 317 |
Is_expand( c, RX_ISCNTRL ); |
| 318 |
} |
| 319 |
#undef Is_expand |
| 320 |
|
| 321 |
int Toupper( int c ) |
| 322 |
{ |
| 323 |
if ( ! ( char_info[256] & RX_ISUPPER ) ) |
| 324 |
load_info( RX_ISUPPER ); |
| 325 |
return l_to_u[ (unsigned char) c ]; |
| 326 |
} |
| 327 |
|
| 328 |
int Tolower( int c ) |
| 329 |
{ |
| 330 |
if ( ! ( char_info[256] & RX_ISLOWER ) ) |
| 331 |
load_info( RX_ISLOWER ); |
| 332 |
return u_to_l[ (unsigned char) c ]; |
| 333 |
} |
| 334 |
|
| 335 |
void mem_upper( void *m, int length ) |
| 336 |
{ |
| 337 |
unsigned char *c = (unsigned char *) m; |
| 338 |
|
| 339 |
if ( ! ( char_info[256] & RX_ISUPPER ) ) |
| 340 |
load_info( RX_ISUPPER ); |
| 341 |
|
| 342 |
while ( length-- > 0 ) |
| 343 |
{ |
| 344 |
*c = l_to_u[ *c ]; |
| 345 |
c++; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
void mem_lower( void *m, int length ) |
| 350 |
{ |
| 351 |
unsigned char *c = (unsigned char *) m; |
| 352 |
|
| 353 |
if ( ! ( char_info[256] & RX_ISLOWER ) ) |
| 354 |
load_info( RX_ISLOWER ); |
| 355 |
|
| 356 |
while ( length-- > 0 ) |
| 357 |
{ |
| 358 |
*c = u_to_l[ *c ]; |
| 359 |
c++; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
void mem_upperrx64( void *m, rx_64 length ) |
| 364 |
{ |
| 365 |
unsigned char *c = (unsigned char *) m; |
| 366 |
|
| 367 |
if ( ! ( char_info[256] & RX_ISUPPER ) ) |
| 368 |
load_info( RX_ISUPPER ); |
| 369 |
|
| 370 |
while ( length-- > 0 ) |
| 371 |
{ |
| 372 |
*c = l_to_u[ *c ]; |
| 373 |
c++; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void mem_lowerrx64( void *m, rx_64 length ) |
| 378 |
{ |
| 379 |
unsigned char *c = (unsigned char *) m; |
| 380 |
|
| 381 |
if ( ! ( char_info[256] & RX_ISLOWER ) ) |
| 382 |
load_info( RX_ISLOWER ); |
| 383 |
|
| 384 |
while ( length-- > 0 ) |
| 385 |
{ |
| 386 |
*c = u_to_l[ *c ]; |
| 387 |
c++; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
int mem_cmpic( const void *buf1, const void *buf2, int len ) |
| 392 |
/* |
| 393 |
* Function : Compares two memory buffers for equality; |
| 394 |
* case insensitive. Same as memicmp() Microsoft C. |
| 395 |
* Parameters: buf1 - first buffer |
| 396 |
* buf2 - second buffer |
| 397 |
* len - number of characters to compare. |
| 398 |
* Return : <0 if buf1 < buf2 |
| 399 |
* =0 if buf1 = buf2 |
| 400 |
* >0 if buf1 > buf2 |
| 401 |
*/ |
| 402 |
{ |
| 403 |
const unsigned char *b1 = (const unsigned char *) buf1; |
| 404 |
const unsigned char *b2 = (const unsigned char *) buf2; |
| 405 |
unsigned char c1,c2; |
| 406 |
|
| 407 |
if ( ! ( char_info[256] & RX_ISLOWER ) ) |
| 408 |
load_info( RX_ISLOWER ); |
| 409 |
|
| 410 |
while ( len-- > 0 ) |
| 411 |
{ |
| 412 |
c1 = u_to_l[ *b1 ]; |
| 413 |
c2 = u_to_l[ *b2 ]; |
| 414 |
if ( c1 != c2 ) |
| 415 |
return( (int) c1 ) - ( (int) c2 ); |
| 416 |
b1++; |
| 417 |
b2++; |
| 418 |
} |
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
void getsecs( time_t *secs, time_t *usecs ) |
| 423 |
{ |
| 424 |
#if defined(HAVE_GETTIMEOFDAY) |
| 425 |
struct timeval times ; |
| 426 |
|
| 427 |
gettimeofday(×, NULL) ; |
| 428 |
*secs = times.tv_sec ; |
| 429 |
*usecs = times.tv_usec ; |
| 430 |
|
| 431 |
if (times.tv_usec < 0) |
| 432 |
{ |
| 433 |
*usecs = (times.tv_usec + 1000000) ; |
| 434 |
*secs = times.tv_sec - 1 ; |
| 435 |
} |
| 436 |
assert( *secs>=0 && *usecs>=0 ) ; |
| 437 |
|
| 438 |
#elif defined(HAVE_FTIME) |
| 439 |
struct timeb timebuffer; |
| 440 |
|
| 441 |
ftime(&timebuffer); |
| 442 |
*secs = timebuffer.time; |
| 443 |
*usecs = timebuffer.millitm * 1000; |
| 444 |
assert( *secs>=0 && *usecs>=0 ) ; |
| 445 |
#else |
| 446 |
*secs = time(NULL) ; |
| 447 |
*usecs = 0 ; |
| 448 |
assert( *secs>=0 && *usecs>=0 ) ; |
| 449 |
#endif |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
const char *system_type( void ) |
| 454 |
{ |
| 455 |
#if defined(VMS) |
| 456 |
return "VMS" ; |
| 457 |
#elif defined(OS2) |
| 458 |
return "OS/2" ; |
| 459 |
#elif defined(DOS) |
| 460 |
# if defined(__EMX__) |
| 461 |
if (_osmode == DOS_MODE) |
| 462 |
return("DOS"); |
| 463 |
else |
| 464 |
return("OS/2"); |
| 465 |
#else |
| 466 |
return("DOS"); |
| 467 |
# endif |
| 468 |
#elif defined(SKYOS) |
| 469 |
return "SKYOS" ; |
| 470 |
#elif defined(__CYGWIN__) |
| 471 |
return "UNIX" ; |
| 472 |
#elif defined(WIN64) || defined(_WIN64) |
| 473 |
return "WIN64" ; |
| 474 |
#elif defined(WIN32) || defined(_WIN32) |
| 475 |
return "WIN32" ; |
| 476 |
#elif defined(_AMIGA) || defined(AMIGA) |
| 477 |
return "AMIGA" ; |
| 478 |
#elif defined(__QNX__) |
| 479 |
return "QNX" ; |
| 480 |
#elif defined(__BEOS__) |
| 481 |
return "BEOS" ; |
| 482 |
#elif defined(__HAIKU__) |
| 483 |
return "HAIKU" ; |
| 484 |
#elif defined(__WINS__) |
| 485 |
return "EPOC32-WINS" ; |
| 486 |
#elif defined(__EPOC32__) |
| 487 |
return "EPOC32-MARM" ; |
| 488 |
#elif defined(__APPLE__) && defined(__MACH__) |
| 489 |
# include <TargetConditionals.h> |
| 490 |
# if TARGET_IPHONE_SIMULATOR == 1 |
| 491 |
return "iOS (Simulator)" ; |
| 492 |
# elif TARGET_OS_IPHONE == 1 |
| 493 |
return "iOS" ; |
| 494 |
# elif TARGET_OS_MAC == 1 |
| 495 |
return "UNIX" ; |
| 496 |
# endif |
| 497 |
#else |
| 498 |
return "UNIX" ; |
| 499 |
#endif |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
#if !defined(HAVE_STRERROR) |
| 504 |
/* |
| 505 |
* Sigh! This must probably be done this way, although it's incredibly |
| 506 |
* backwards. Some versions of gcc comes with a complete set of ANSI C |
| 507 |
* include files, which contains the definition of strerror(). However, |
| 508 |
* that function does not exist in the default libraries of SunOS. |
| 509 |
* To circumvent that problem, strerror() is #define'd to get_sys_errlist() |
| 510 |
* in config.h, and here follows the definition of that function. |
| 511 |
* Originally, strerror() was #defined to sys_errlist[x], but that does |
| 512 |
* not work if string.h contains a declaration of the (non-existing) |
| 513 |
* function strerror(). |
| 514 |
* |
| 515 |
* So, this is a mismatch between the include files and the library, and |
| 516 |
* it should not create problems for Regina. However, the _user_ will not |
| 517 |
* encounter any problems until he compiles Regina, so we'll have to |
| 518 |
* clean up after a buggy installation of the C compiler! |
| 519 |
*/ |
| 520 |
const char *get_sys_errlist( int num ) |
| 521 |
{ |
| 522 |
extern char *sys_errlist[] ; |
| 523 |
return sys_errlist[num] ; |
| 524 |
} |
| 525 |
#endif |
| 526 |
|
| 527 |
#define NANO_SECS_IN_SEC 1000000000 |
| 528 |
|
| 529 |
double cpu_time( void ) |
| 530 |
{ |
| 531 |
#if defined(CLOCK_PROCESS_CPUTIME_ID) |
| 532 |
/* clock_gettime() is more accurate than clock() */ |
| 533 |
struct timespec tp; |
| 534 |
rx_64u nsecs = 0; |
| 535 |
clock_gettime( CLOCK_PROCESS_CPUTIME_ID, &tp ); |
| 536 |
nsecs = (tp.tv_sec * NANO_SECS_IN_SEC) + tp.tv_nsec; |
| 537 |
return ((double)(nsecs)/(double)(NANO_SECS_IN_SEC)); |
| 538 |
#elif defined(WIN32) || defined(WIN64) |
| 539 |
/* |
| 540 |
* Windows clock() does not follow the ISO standard; of course!! |
| 541 |
* It returns elapsed time, so we have to write something special just for |
| 542 |
* Windows; I hate Windows!! |
| 543 |
*/ |
| 544 |
FILETIME creation_time; |
| 545 |
FILETIME exit_time; |
| 546 |
FILETIME kernel_time; |
| 547 |
FILETIME user_time; |
| 548 |
uint64_t usec = 0; |
| 549 |
if ( GetProcessTimes( GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time ) ) |
| 550 |
{ |
| 551 |
/* Convert kernel and user times to microseconds with rounding. */ |
| 552 |
uint64_t kernel_usec = ((((uint64_t) kernel_time.dwHighDateTime << 32) | (uint64_t) kernel_time.dwLowDateTime) + 5) / 10; |
| 553 |
uint64_t user_usec = ((((uint64_t) user_time.dwHighDateTime << 32) | (uint64_t) user_time.dwLowDateTime) + 5) / 10; |
| 554 |
/* sum kernel and user time */ |
| 555 |
usec = kernel_usec + user_usec; |
| 556 |
} |
| 557 |
return ((double)(usec))/((double)(1000000U)) ; |
| 558 |
#else |
| 559 |
# ifndef CLOCKS_PER_SEC |
| 560 |
/* |
| 561 |
* Lots of systems don't seem to get this ANSI C piece of code correctly |
| 562 |
* but most of them seems to use one million ... Using a million for |
| 563 |
* those systems that haven't defined CLOCKS_PER_SEC may give an incorrect |
| 564 |
* value if clock() does not return microseconds! |
| 565 |
*/ |
| 566 |
# define CLOCKS_PER_SEC 1000000 |
| 567 |
# endif |
| 568 |
return ((double)(clock()))/((double)(CLOCKS_PER_SEC)) ; |
| 569 |
#endif |
| 570 |
} |
| 571 |
|
| 572 |
/* HIGHBIT is an unsigned with the highest bit set */ |
| 573 |
#define HIGHBIT (((unsigned) 1) << ((sizeof(unsigned) * CHAR_BIT) - 1)) |
| 574 |
|
| 575 |
/* hashvalue computes a value for hashing from a string content. Use |
| 576 |
* hashvalue_ic for a case insensitive version. |
| 577 |
* length may less than 0. The string length is computed by strlen() in this |
| 578 |
* case. |
| 579 |
* The return value might be modified by the %-operator for real hash |
| 580 |
* values. |
| 581 |
*/ |
| 582 |
unsigned hashvalue(const char *string, int length) |
| 583 |
{ |
| 584 |
unsigned retval = 0, wrap ; |
| 585 |
const unsigned char *ptr = (const unsigned char *) string; /* unsigned char makes it fast */ |
| 586 |
unsigned char c; |
| 587 |
|
| 588 |
if (length < 0) |
| 589 |
length = strlen(string); |
| 590 |
|
| 591 |
while (length--) { |
| 592 |
c = *ptr++; /* Yes this is slower but believe in your optimizer! */ |
| 593 |
retval ^= c; |
| 594 |
wrap = (retval & HIGHBIT) ? 1 : 0; |
| 595 |
retval <<= 1; |
| 596 |
retval |= wrap; |
| 597 |
} |
| 598 |
|
| 599 |
return(retval); |
| 600 |
} |
| 601 |
|
| 602 |
/* hashvalue_ic computes a value for hashing from a string content. Use |
| 603 |
* hashvalue for a case significant version. This is case insensitive. |
| 604 |
* length may less than 0. The string length is computed by strlen() in this |
| 605 |
* case. |
| 606 |
* The return value might be modified by the %-operator for real hash |
| 607 |
* values. |
| 608 |
*/ |
| 609 |
unsigned hashvalue_ic(const char *string, int length) |
| 610 |
{ |
| 611 |
unsigned retval = 0, wrap ; |
| 612 |
const unsigned char *ptr = (const unsigned char *) string; /* unsigned char makes it fast */ |
| 613 |
unsigned char c; |
| 614 |
|
| 615 |
if ( length < 0 ) |
| 616 |
length = strlen( string ); |
| 617 |
|
| 618 |
if ( ! ( char_info[256] & RX_ISLOWER ) ) |
| 619 |
load_info( RX_ISLOWER ); |
| 620 |
|
| 621 |
while ( length-- ) { |
| 622 |
c = u_to_l[ *ptr ]; |
| 623 |
ptr++; |
| 624 |
retval ^= c; |
| 625 |
wrap = (retval & HIGHBIT) ? 1 : 0; |
| 626 |
retval <<= 1; |
| 627 |
retval |= wrap; |
| 628 |
} |
| 629 |
|
| 630 |
return(retval); |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
* hashvalue_var computes a value for hashing from a variable name. |
| 635 |
* The computing starts at the start'th character in name and stops either |
| 636 |
* at the end of the string or at the next dot. The later one only happens |
| 637 |
* if stop != NULL. In this case, the dot's position is returned in *stop. |
| 638 |
* Note: This is one of the most time-consuming routines. Be careful. |
| 639 |
*/ |
| 640 |
unsigned hashvalue_var( const streng *name, int start, int *stop ) |
| 641 |
{ |
| 642 |
unsigned sum, idx; |
| 643 |
const char *ch1, *ech0; |
| 644 |
|
| 645 |
if ( ( char_info[256] & ( RX_ISLOWER | RX_ISDIGIT ) ) != |
| 646 |
( RX_ISLOWER | RX_ISDIGIT ) ) |
| 647 |
{ |
| 648 |
/* |
| 649 |
* The above conditional is the fastest check. Now do the slow |
| 650 |
* individual check. It speeds up all things. |
| 651 |
*/ |
| 652 |
if ( ! ( char_info[256] & RX_ISLOWER ) ) |
| 653 |
load_info( RX_ISLOWER ); |
| 654 |
if ( ! ( char_info[256] & RX_ISDIGIT ) ) |
| 655 |
load_info( RX_ISDIGIT ); |
| 656 |
} |
| 657 |
|
| 658 |
ch1 = name->value; |
| 659 |
ech0 = Str_end( name ); |
| 660 |
|
| 661 |
ch1 += start; |
| 662 |
sum = idx = 0; |
| 663 |
for (; ch1 < ech0; ch1++ ) |
| 664 |
{ |
| 665 |
if ( *ch1 == '.' ) |
| 666 |
{ |
| 667 |
if ( stop ) |
| 668 |
break; |
| 669 |
else |
| 670 |
continue; |
| 671 |
} |
| 672 |
if ( char_info[(unsigned char) *ch1] & RX_ISDIGIT ) |
| 673 |
idx = idx * 10 + (unsigned) ( *ch1 - '0' ); |
| 674 |
else |
| 675 |
{ |
| 676 |
if ( idx ) |
| 677 |
{ |
| 678 |
sum += u_to_l[ (unsigned char) *ch1 ] + idx; |
| 679 |
idx = 0; |
| 680 |
} |
| 681 |
else |
| 682 |
sum += u_to_l[ (unsigned char) *ch1 ]; |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
if ( stop ) |
| 687 |
*stop = ch1 - name->value; |
| 688 |
|
| 689 |
return sum + idx; |
| 690 |
} |
| 691 |
|
| 692 |
/* |
| 693 |
* Because this modules defines the helper functions for rx_isspace we |
| 694 |
* don't have access to this defined macro. Just re-define it locally. |
| 695 |
*/ |
| 696 |
#define is_expand( c, bit, func ) ( ( char_info[256] & bit ) ? \ |
| 697 |
( char_info[(unsigned char) c] & bit ) : func( c ) ) |
| 698 |
#define rx_isspace( c ) is_expand( c, RX_ISSPACE , Isspace ) |
| 699 |
|
| 700 |
/* |
| 701 |
* nextarg parses source for the next argument in unix shell terms. If target |
| 702 |
* is given, it must consist of enough free characters to hold the result + |
| 703 |
* one byte for the terminator. If len != NULL it will become the length of |
| 704 |
* the string (which might not been return if target == NULL). The return value |
| 705 |
* is either NULL or a new start value for nextarg. |
| 706 |
* escape is the current escape value which should be used, must be set. |
| 707 |
*/ |
| 708 |
static const char *nextarg(const char *source, unsigned *len, char *target, |
| 709 |
char escape) |
| 710 |
{ |
| 711 |
unsigned l; |
| 712 |
char c, term; |
| 713 |
|
| 714 |
if (len != NULL) |
| 715 |
*len = 0; |
| 716 |
if (target != NULL) |
| 717 |
*target = '\0'; |
| 718 |
l = 0; /* cached length */ |
| 719 |
|
| 720 |
if (source == NULL) |
| 721 |
return NULL; |
| 722 |
|
| 723 |
while (rx_isspace(*source)) /* jump over initial spaces */ |
| 724 |
source++; |
| 725 |
if (*source == '\0') |
| 726 |
return NULL; |
| 727 |
|
| 728 |
do { |
| 729 |
/* There's something to return. Check for delimiters */ |
| 730 |
term = *source++; |
| 731 |
|
| 732 |
if ((term == '\'') || (term == '\"')) |
| 733 |
{ |
| 734 |
while ((c = *source++) != term) { |
| 735 |
if (c == escape) |
| 736 |
c = *source++; |
| 737 |
if (c == '\0') /* stray \ at EOS is equiv to normal EOS */ |
| 738 |
{ |
| 739 |
/* empty string is valid! */ |
| 740 |
if (len != NULL) |
| 741 |
*len = l; |
| 742 |
if (target != NULL) |
| 743 |
*target = '\0'; |
| 744 |
return source - 1; /* next try returns NULL */ |
| 745 |
} |
| 746 |
l++; |
| 747 |
if (target != NULL) |
| 748 |
*target++ = c; |
| 749 |
} |
| 750 |
} |
| 751 |
else /* whitespace delimiters */ |
| 752 |
{ |
| 753 |
c = term; |
| 754 |
while (!rx_isspace(c) && (c != '\'') && (c != '\"')) { |
| 755 |
if (c == escape) |
| 756 |
c = *source++; |
| 757 |
if (c == '\0') /* stray \ at EOS is equiv to normal EOS */ |
| 758 |
{ |
| 759 |
/* at least a stray \ was found, empty string checked in |
| 760 |
* the very beginning. |
| 761 |
*/ |
| 762 |
if (len != NULL) |
| 763 |
*len = l; |
| 764 |
if (target != NULL) |
| 765 |
*target = '\0'; |
| 766 |
return source - 1; /* next try returns NULL */ |
| 767 |
} |
| 768 |
l++; |
| 769 |
if (target != NULL) |
| 770 |
*target++ = c; |
| 771 |
c = *source++; |
| 772 |
} |
| 773 |
source--; /* undo the "wrong" character */ |
| 774 |
} |
| 775 |
} while (!rx_isspace(*source)); |
| 776 |
|
| 777 |
if (len != NULL) |
| 778 |
*len = l; |
| 779 |
if (target != NULL) |
| 780 |
*target = '\0'; |
| 781 |
return source; |
| 782 |
} |
| 783 |
|
| 784 |
/* |
| 785 |
* makeargs chops string into arguments and returns an array of x+1 strings if |
| 786 |
* string contains x args. The last argument is NULL. This function usually is |
| 787 |
* called from the subprocess if fork/exec is used. |
| 788 |
* Example: "xx y" -> { "xx", "y", NULL } |
| 789 |
* escape must be the escape character of the command line and is usually ^ |
| 790 |
* or \ |
| 791 |
*/ |
| 792 |
char **makeargs(const char *string, char escape) |
| 793 |
{ |
| 794 |
char **retval; |
| 795 |
const char *p; |
| 796 |
int i, argc = 0; |
| 797 |
unsigned size; |
| 798 |
|
| 799 |
p = string; /* count the number of strings */ |
| 800 |
while ((p = nextarg(p, NULL, NULL, escape)) != NULL) |
| 801 |
argc++; |
| 802 |
if ((retval = (char **)malloc((argc + 1) * sizeof(char *))) == NULL) |
| 803 |
return(NULL); |
| 804 |
|
| 805 |
p = string; /* count each string length */ |
| 806 |
for (i = 0; i < argc; i++) |
| 807 |
{ |
| 808 |
p = nextarg(p, &size, NULL, escape); |
| 809 |
if ((retval[i] = (char *)malloc(size + 1)) == NULL) |
| 810 |
{ |
| 811 |
i--; |
| 812 |
while (i >= 0) |
| 813 |
free(retval[i--]); |
| 814 |
free(retval); |
| 815 |
return(NULL); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
p = string; /* assign each string */ |
| 820 |
for (i = 0; i < argc; i++) |
| 821 |
p = nextarg(p, NULL, retval[i], escape); |
| 822 |
retval[argc] = NULL; |
| 823 |
|
| 824 |
return(retval); |
| 825 |
} |
| 826 |
|
| 827 |
/* splitoffarg chops string into two different pieces: The first argument and |
| 828 |
* all other (uninterpreted) arguments. The first argument is returned in a |
| 829 |
* freshly allocated string. The rest is a pointer somewhere within string |
| 830 |
* and returned in *trailer. The return value is allocated by malloc(). |
| 831 |
* Example: "xx y" -> returns "xx", *trailer == "xx y"+2 |
| 832 |
* escape must be the escape character of the command line and is usually ^ |
| 833 |
* or \ |
| 834 |
*/ |
| 835 |
char *splitoffarg(const char *string, const char **trailer, char escape) |
| 836 |
{ |
| 837 |
unsigned size; |
| 838 |
char *retval; |
| 839 |
const char *t; |
| 840 |
|
| 841 |
if (trailer != NULL) |
| 842 |
*trailer = ""; /* just a default */ |
| 843 |
nextarg(string, &size, NULL, escape); |
| 844 |
if ((retval = malloc(size + 1)) == NULL) /* don't change to internal allocation routine */ |
| 845 |
return(NULL); |
| 846 |
|
| 847 |
t = nextarg(string, NULL, retval, escape); |
| 848 |
if (trailer != NULL) |
| 849 |
*trailer = t; |
| 850 |
return(retval); |
| 851 |
} |
| 852 |
|
| 853 |
/* |
| 854 |
* nextarg parses source for the next argument as a simple word. If target |
| 855 |
* is given, it must consist of enough free characters to hold the result + |
| 856 |
* one byte for the terminator. If len != NULL it will become the length of |
| 857 |
* the string (which might not been return if target == NULL). The return value |
| 858 |
* is either NULL or a new start value for nextarg. |
| 859 |
*/ |
| 860 |
static const char *nextsimplearg(const char *source, unsigned *len, |
| 861 |
char *target) |
| 862 |
{ |
| 863 |
unsigned l; |
| 864 |
char c; |
| 865 |
|
| 866 |
if (len != NULL) |
| 867 |
*len = 0; |
| 868 |
if (target != NULL) |
| 869 |
*target = '\0'; |
| 870 |
l = 0; /* cached length */ |
| 871 |
|
| 872 |
if (source == NULL) |
| 873 |
return(NULL); |
| 874 |
|
| 875 |
while (rx_isspace(*source)) /* jump over initial spaces */ |
| 876 |
source++; |
| 877 |
if (*source == '\0') |
| 878 |
return(NULL); |
| 879 |
|
| 880 |
c = *source++; |
| 881 |
|
| 882 |
while (!rx_isspace(c)) |
| 883 |
{ |
| 884 |
if (c == '\0') /* stray \ at EOS is equiv to normal EOS */ |
| 885 |
{ |
| 886 |
/* something's found, therefore we don't have to return NULL */ |
| 887 |
if (len != NULL) |
| 888 |
*len = l; |
| 889 |
if (target != NULL) |
| 890 |
*target = '\0'; |
| 891 |
return(source - 1); /* next try returns NULL */ |
| 892 |
} |
| 893 |
l++; |
| 894 |
if (target != NULL) |
| 895 |
*target++ = c; |
| 896 |
c = *source++; |
| 897 |
} |
| 898 |
source--; /* undo the "wrong" character */ |
| 899 |
|
| 900 |
if (len != NULL) |
| 901 |
*len = l; |
| 902 |
if (target != NULL) |
| 903 |
*target = '\0'; |
| 904 |
return(source); |
| 905 |
} |
| 906 |
|
| 907 |
/* |
| 908 |
* makesimpleargs chops string into arguments and returns an array of x+1 |
| 909 |
* strings if string contains x args. The last argument is NULL. This function |
| 910 |
* usually is called from the subprocess if fork/exec is used. |
| 911 |
* Example: "xx y" -> { "xx", "y", NULL } |
| 912 |
*/ |
| 913 |
char **makesimpleargs(const char *string) |
| 914 |
{ |
| 915 |
char **retval; |
| 916 |
const char *p; |
| 917 |
int i, argc = 0; |
| 918 |
unsigned size; |
| 919 |
|
| 920 |
p = string; /* count the number of strings */ |
| 921 |
while ((p = nextsimplearg(p, NULL, NULL)) != NULL) |
| 922 |
argc++; |
| 923 |
if ((retval = malloc((argc + 1) * sizeof(char *))) == NULL) |
| 924 |
return(NULL); |
| 925 |
|
| 926 |
p = string; /* count each string length */ |
| 927 |
for (i = 0; i < argc; i++) |
| 928 |
{ |
| 929 |
p = nextsimplearg(p, &size, NULL); |
| 930 |
if ((retval[i] = malloc(size + 1)) == NULL) |
| 931 |
{ |
| 932 |
i--; |
| 933 |
while (i >= 0) |
| 934 |
free(retval[i--]); |
| 935 |
free(retval); |
| 936 |
return(NULL); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
p = string; /* assign each string */ |
| 941 |
for (i = 0; i < argc; i++) |
| 942 |
p = nextsimplearg(p, NULL, retval[i]); |
| 943 |
|
| 944 |
return(retval); |
| 945 |
} |
| 946 |
|
| 947 |
/* |
| 948 |
* destroyargs destroys the array created by makeargs |
| 949 |
*/ |
| 950 |
void destroyargs(char **args) |
| 951 |
{ |
| 952 |
char **run = args; |
| 953 |
|
| 954 |
while (*run) { |
| 955 |
free(*run); |
| 956 |
run++; |
| 957 |
} |
| 958 |
free(args); |
| 959 |
} |