1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
/********************************************
cast.c
copyright 2009-2021,2024, Thomas E. Dickey
copyright 1991-1995,1996, Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/
/*
* $MawkId: cast.c,v 1.35 2024/12/14 21:21:20 tom Exp $
*/
#define Visible_CELL
#define Visible_RE_DATA
#define Visible_STRING
#include <mawk.h>
#include <field.h>
#include <memory.h>
#include <scan.h>
const int mpow2[NUM_CELL_TYPES] =
{1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
#define isXDIGIT(c) \
(scan_code[(c)] == SC_DIGIT \
|| ((c) >= 'A' && (c) <= 'F') \
|| ((c) >= 'a' && (c) <= 'f'))
static void
string_to_double(CELL *cp)
{
const char *q = string(cp)->str;
const char *r = string(cp)->len + q;
cp->dval = 0.0;
/* if non-posix, disallow hexadecimal, infinity and not-a-number */
if (!posix_space_flag) {
const char *s;
while ((q != r) && (scan_code[(UChar) q[0]] == SC_SPACE))
q++;
if (q == r)
goto done;
s = q;
if (scan_code[(UChar) q[0]] == SC_PLUS ||
scan_code[(UChar) q[0]] == SC_MINUS)
q++;
if (q[0] == '0') {
if (q + 1 == r) /* just "0" is legal */
goto done;
if (q[1] == 'x' || q[1] == 'X') {
if (q + 2 == r) /* "0x" by itself is zero */
goto done;
if (isXDIGIT((UChar) q[2])) /* ignore hexadecimal */
goto done;
}
} else if (scan_code[(UChar) q[0]] != SC_DIGIT && q[0] != '.') {
goto done; /* ignore non-number such as "inf" */
}
q = s;
}
errno = 0;
#ifdef FPE_TRAPS_ON /* look for overflow error */
cp->dval = strtod(q, (char **) 0);
if (errno && cp->dval != 0.0) /* ignore underflow */
rt_error("overflow converting %s to double", q);
#else
cp->dval = strtod(q, (char **) 0);
#endif
free_STRING(string(cp));
done:
cp->type = C_DOUBLE;
}
void
cast1_to_d(CELL *cp)
{
switch (cp->type) {
case C_NOINIT:
cp->dval = 0.0;
cp->type = C_DOUBLE;
break;
case C_DOUBLE:
break;
case C_MBSTRN:
case C_STRING:
string_to_double(cp);
break;
case C_STRNUM:
/* don't need to convert, but do need to free the STRING part */
free_STRING(string(cp));
cp->type = C_DOUBLE;
break;
default:
bozo("cast on bad type");
}
}
void
cast2_to_d(CELL *cp)
{
switch (cp->type) {
case C_NOINIT:
cp->dval = 0.0;
break;
case C_DOUBLE:
goto two;
case C_STRNUM:
free_STRING(string(cp));
break;
case C_MBSTRN:
case C_STRING:
string_to_double(cp);
break;
default:
bozo("cast on bad type");
}
cp->type = C_DOUBLE;
two:
cp++;
switch (cp->type) {
case C_NOINIT:
cp->dval = 0.0;
break;
case C_DOUBLE:
return;
case C_STRNUM:
free_STRING(string(cp));
break;
case C_MBSTRN:
case C_STRING:
string_to_double(cp);
break;
default:
bozo("cast on bad type");
}
cp->type = C_DOUBLE;
}
#define DoubleToString(target,source) \
if (IsMaxBound(fabs(source->dval))) { \
sprintf(target, UNSIGNED_FORMAT, source->dval); \
} else if (source->dval >= (double) Max_ULong) { \
ULong lval = d_to_UL(source->dval); \
if (lval == source->dval) { \
sprintf(target, ULONG_FMT, lval); \
} else { \
sprintf(target, string(CONVFMT)->str, source->dval); \
} \
} else { \
Long lval = d_to_L(source->dval); \
if (lval == source->dval) { \
sprintf(target, LONG_FMT, lval); \
} else { \
sprintf(target, string(CONVFMT)->str, source->dval); \
} \
}
void
cast1_to_s(CELL *cp)
{
char xbuff[260];
switch (cp->type) {
case C_NOINIT:
null_str.ref_cnt++;
cp->ptr = (PTR) & null_str;
break;
case C_DOUBLE:
DoubleToString(xbuff, cp);
cp->ptr = (PTR) new_STRING(xbuff);
break;
case C_STRING:
return;
case C_MBSTRN:
case C_STRNUM:
break;
default:
bozo("bad type on cast");
}
cp->type = C_STRING;
}
void
cast2_to_s(CELL *cp)
{
char xbuff[260];
switch (cp->type) {
case C_NOINIT:
null_str.ref_cnt++;
cp->ptr = (PTR) & null_str;
break;
case C_DOUBLE:
DoubleToString(xbuff, cp);
cp->ptr = (PTR) new_STRING(xbuff);
break;
case C_STRING:
goto two;
case C_MBSTRN:
case C_STRNUM:
break;
default:
bozo("bad type on cast");
}
cp->type = C_STRING;
two:
cp++;
switch (cp->type) {
case C_NOINIT:
null_str.ref_cnt++;
cp->ptr = (PTR) & null_str;
break;
case C_DOUBLE:
DoubleToString(xbuff, cp);
cp->ptr = (PTR) new_STRING(xbuff);
break;
case C_STRING:
return;
case C_MBSTRN:
case C_STRNUM:
break;
default:
bozo("bad type on cast");
}
cp->type = C_STRING;
}
void
cast_to_RE(CELL *cp)
{
register PTR p;
if (cp->type < C_STRING)
cast1_to_s(cp);
p = re_compile(string(cp));
no_leaks_re_ptr(p);
free_STRING(string(cp));
cp->type = C_RE;
cp->ptr = p;
}
void
cast_for_split(CELL *cp)
{
#ifndef NO_INTERVAL_EXPR
static const char meta[] = "^$.*+?|[](){}";
#else
static const char meta[] = "^$.*+?|[]()";
#endif
size_t len;
if (cp->type < C_STRING)
cast1_to_s(cp);
if ((len = string(cp)->len) == 1) {
int c;
if ((c = string(cp)->str[0]) == ' ') {
free_STRING(string(cp));
cp->type = C_SPACE;
return;
} else if (c == 0) {
#ifdef LOCAL_REGEXP
char temp[1];
temp[0] = (char) c;
free_STRING(string(cp));
cp->ptr = (PTR) new_STRING1(temp, (size_t) 1);
#else
/*
* A null is not a meta character, but strchr will match it anyway.
* For now, there's no reason to compile a null as a regular
* expression - just return a string containing the single
* character. That is used in a special case in set_rs_shadow().
*/
char temp[2];
temp[0] = (char) c;
free_STRING(string(cp));
cp->ptr = (PTR) new_STRING1(temp, (size_t) 1);
return;
#endif
} else if ((strchr) (meta, c)) {
static char xbuff[] = "\\X";
xbuff[1] = (char) c;
free_STRING(string(cp));
cp->ptr = (PTR) new_STRING(xbuff);
}
} else if (len == 0) {
free_STRING(string(cp));
cp->type = C_SNULL;
return;
}
cast_to_RE(cp);
}
/* input: cp-> a CELL of type C_MBSTRN (maybe strnum)
test it -- casting it to the appropriate type
which is C_STRING or C_STRNUM
*/
void
check_strnum(CELL *cp)
{
char *temp;
register unsigned char *s, *q;
char code;
cp->type = C_STRING; /* assume not C_STRNUM */
s = (unsigned char *) string(cp)->str;
q = s + string(cp)->len;
while (scan_code[*s] == SC_SPACE)
s++;
if (s == q)
return;
while ((code = scan_code[q[-1]]) == SC_SPACE)
q--;
if (code != SC_DIGIT && code != SC_DOT)
return;
switch (scan_code[*s]) {
case SC_PLUS:
case SC_MINUS:
if (!posix_space_flag) {
if ((code = scan_code[s[1]]) != SC_DIGIT && code != SC_DOT)
return;
}
/* FALLTHRU */
case SC_DIGIT:
if (!posix_space_flag) {
if (*s == '0') {
if (s[1] == 'x' || s[1] == 'X')
return;
}
}
/* FALLTHRU */
case SC_DOT:
errno = 0;
#ifdef FPE_TRAPS_ON
cp->dval = strtod((char *) s, &temp);
/* make overflow pure string */
if (errno && cp->dval != 0.0)
break;
#else
cp->dval = strtod((char *) s, &temp);
#endif
#ifdef ERANGE
if (errno == ERANGE)
break;
#endif
if ((char *) q <= temp) {
cp->type = C_STRNUM;
/* <= instead of == , for some buggy strtod
e.g. Apple Unix */
}
break;
}
}
/* cast a CELL to a replacement cell */
void
cast_to_REPL(CELL *cp)
{
register STRING *sval;
if (cp->type < C_STRING)
cast1_to_s(cp);
sval = (STRING *) cp->ptr;
cellcpy(cp, repl_compile(sval));
free_STRING(sval);
}
/* convert a double to Int (this is not as simple as a
cast because the results are undefined if it won't fit).
Truncate large values to +Max_Int or -Max_Int
Send nans to -Max_Int
*/
Int
d_to_I(double d)
{
Int result;
if (d >= (double) Max_Int) {
result = Max_Int;
} else if (d < 0) {
if (-d <= (double) Max_Int) {
result = (Int) d;
} else {
result = -Max_Int;
}
} else {
result = (Int) d;
}
return result;
}
Long
d_to_L(double d)
{
Long result;
if (d >= (double) Max_Long) {
result = Max_Long;
} else if (d < 0) {
if (-d <= (double) Max_Long) {
result = (Long) d;
} else {
result = -Max_Long;
}
} else {
result = (Long) d;
}
return result;
}
ULong
d_to_UL(double d)
{
ULong result;
if (d >= (double) Max_ULong) {
result = Max_ULong;
} else if (d < 0) {
if (-d < (double) Max_ULong) {
result = ((Max_ULong + (ULong) d) + 1);
} else {
result = -Max_ULong;
}
} else {
result = (ULong) d;
}
return result;
}
#ifdef NO_LEAKS
typedef struct _all_cells {
struct _all_cells *next;
char ptr;
CELL *cp;
} ALL_CELLS;
static ALL_CELLS *all_cells;
/*
* Some regular expressions are parsed, and the pointer stored in the byte-code
* where we cannot distinguish it from other constants. Keep a list here, to
* free on exit for auditing.
*/
void
no_leaks_cell(CELL *cp)
{
ALL_CELLS *p = calloc(1, sizeof(ALL_CELLS));
p->next = all_cells;
p->cp = cp;
p->ptr = 0;
all_cells = p;
}
void
no_leaks_cell_ptr(CELL *cp)
{
ALL_CELLS *p = calloc(1, sizeof(ALL_CELLS));
p->next = all_cells;
p->cp = cp;
p->ptr = 1;
all_cells = p;
}
void
cell_leaks(void)
{
while (all_cells != NULL) {
ALL_CELLS *next = all_cells->next;
if (all_cells->ptr) {
zfree(all_cells->cp, sizeof(CELL));
} else {
free_cell_data(all_cells->cp);
}
free(all_cells);
all_cells = next;
}
}
#endif
|