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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
|
/*
* field.c - routines for dealing with fields and record parsing
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2009 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "awk.h"
/*
* In case that the system doesn't have isblank().
* Don't bother with autoconf ifdef junk, just force it.
* See dfa.c and regex_internal.h and regcomp.c. Bleah.
*/
static int
is_blank(int c)
{
return c == ' ' || c == '\t';
}
typedef void (* Setfunc) P((long, char *, long, NODE *));
static long (*parse_field) P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static void rebuild_record P((void));
static long re_parse_field P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static long def_parse_field P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static long posix_def_parse_field P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static long null_parse_field P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static long sc_parse_field P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static long fw_parse_field P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
static void set_element P((long num, char * str, long len, NODE *arr));
static void grow_fields_arr P((long num));
static void set_field P((long num, char *str, long len, NODE *dummy));
static void update_PROCINFO P((char *subscript, char *str));
static char *parse_extent; /* marks where to restart parse of record */
static long parse_high_water = 0; /* field number that we have parsed so far */
static long nf_high_water = 0; /* size of fields_arr */
static int resave_fs;
static NODE *save_FS; /* save current value of FS when line is read,
* to be used in deferred parsing
*/
static int *FIELDWIDTHS = NULL;
NODE **fields_arr; /* array of pointers to the field nodes */
int field0_valid; /* $(>0) has not been changed yet */
int default_FS; /* TRUE when FS == " " */
Regexp *FS_re_yes_case = NULL;
Regexp *FS_re_no_case = NULL;
Regexp *FS_regexp = NULL;
NODE *Null_field = NULL;
/* using_FIELDWIDTHS --- static function, macro to avoid overhead */
#define using_FIELDWIDTHS() (parse_field == fw_parse_field)
/* init_fields --- set up the fields array to start with */
void
init_fields()
{
emalloc(fields_arr, NODE **, sizeof(NODE *), "init_fields");
fields_arr[0] = Nnull_string;
parse_extent = fields_arr[0]->stptr;
save_FS = dupnode(FS_node->var_value);
getnode(Null_field);
*Null_field = *Nnull_string;
Null_field->flags |= FIELD;
Null_field->flags &= ~(NUMCUR|NUMBER|MAYBE_NUM|PERM);
field0_valid = TRUE;
}
/* grow_fields --- acquire new fields as needed */
static void
grow_fields_arr(long num)
{
register int t;
register NODE *n;
erealloc(fields_arr, NODE **, (num + 1) * sizeof(NODE *), "grow_fields_arr");
for (t = nf_high_water + 1; t <= num; t++) {
getnode(n);
*n = *Null_field;
fields_arr[t] = n;
}
nf_high_water = num;
}
/* set_field --- set the value of a particular field */
/*ARGSUSED*/
static void
set_field(long num,
char *str,
long len,
NODE *dummy ATTRIBUTE_UNUSED) /* just to make interface same as set_element */
{
register NODE *n;
if (num > nf_high_water)
grow_fields_arr(num);
n = fields_arr[num];
n->stptr = str;
n->stlen = len;
n->flags = (STRCUR|STRING|MAYBE_NUM|FIELD);
}
/* rebuild_record --- Someone assigned a value to $(something).
Fix up $0 to be right */
static void
rebuild_record()
{
/*
* use explicit unsigned longs for lengths, in case
* a size_t isn't big enough.
*/
register unsigned long tlen;
register unsigned long ofslen;
register NODE *tmp;
NODE *ofs;
char *ops;
register char *cops;
long i;
assert(NF != -1);
tlen = 0;
ofs = force_string(OFS_node->var_value);
ofslen = ofs->stlen;
for (i = NF; i > 0; i--) {
tmp = fields_arr[i];
tmp = force_string(tmp);
tlen += tmp->stlen;
}
tlen += (NF - 1) * ofslen;
if ((long) tlen < 0)
tlen = 0;
emalloc(ops, char *, tlen + 2, "rebuild_record");
cops = ops;
ops[0] = '\0';
for (i = 1; i <= NF; i++) {
free_wstr(fields_arr[i]);
tmp = fields_arr[i];
/* copy field */
if (tmp->stlen == 1)
*cops++ = tmp->stptr[0];
else if (tmp->stlen != 0) {
memcpy(cops, tmp->stptr, tmp->stlen);
cops += tmp->stlen;
}
/* copy OFS */
if (i != NF) {
if (ofslen == 1)
*cops++ = ofs->stptr[0];
else if (ofslen != 0) {
memcpy(cops, ofs->stptr, ofslen);
cops += ofslen;
}
}
}
tmp = make_str_node(ops, tlen, ALREADY_MALLOCED);
/*
* Since we are about to unref fields_arr[0], we want to find
* any fields that still point into it, and have them point
* into the new field zero. This has to be done intelligently,
* so that unrefing a field doesn't try to unref into the old $0.
*/
for (cops = ops, i = 1; i <= NF; i++) {
if (fields_arr[i]->stlen > 0) {
NODE *n;
getnode(n);
if ((fields_arr[i]->flags & FIELD) == 0) {
*n = *Null_field;
n->stlen = fields_arr[i]->stlen;
if ((fields_arr[i]->flags & (NUMCUR|NUMBER)) != 0) {
n->flags |= (fields_arr[i]->flags & (NUMCUR|NUMBER));
n->numbr = fields_arr[i]->numbr;
}
} else {
*n = *(fields_arr[i]);
n->flags &= ~(MALLOC|TEMP|PERM|STRING);
}
n->stptr = cops;
unref(fields_arr[i]);
fields_arr[i] = n;
#ifdef MBS_SUPPORT
assert((n->flags & WSTRCUR) == 0);
#endif
}
cops += fields_arr[i]->stlen + ofslen;
}
unref(fields_arr[0]);
fields_arr[0] = tmp;
field0_valid = TRUE;
}
/*
* set_record:
* setup $0, but defer parsing rest of line until reference is made to $(>0)
* or to NF. At that point, parse only as much as necessary.
*
* Manage a private buffer for the contents of $0. Doing so keeps us safe
* if `getline var' decides to rearrange the contents of the IOBUF that
* $0 might have been pointing into. The cost is the copying of the buffer;
* but better correct than fast.
*/
void
set_record(const char *buf, int cnt)
{
NODE *n;
static char *databuf;
static unsigned long databuf_size;
#define INITIAL_SIZE 512
#define MAX_SIZE ((unsigned long) ~0) /* maximally portable ... */
reset_record();
/* buffer management: */
if (databuf_size == 0) { /* first time */
emalloc(databuf, char *, INITIAL_SIZE, "set_record");
databuf_size = INITIAL_SIZE;
memset(databuf, '\0', INITIAL_SIZE);
}
/*
* Make sure there's enough room. Since we sometimes need
* to place a sentinel at the end, we make sure
* databuf_size is > cnt after allocation.
*/
if (cnt >= databuf_size) {
while (cnt >= databuf_size && databuf_size <= MAX_SIZE)
databuf_size *= 2;
erealloc(databuf, char *, databuf_size, "set_record");
memset(databuf, '\0', databuf_size);
}
/* copy the data */
memcpy(databuf, buf, cnt);
/* manage field 0: */
unref(fields_arr[0]);
getnode(n);
n->stptr = databuf;
n->stlen = cnt;
n->stref = 1;
n->type = Node_val;
n->stfmt = -1;
n->flags = (STRING|STRCUR|MAYBE_NUM|FIELD);
fields_arr[0] = n;
#undef INITIAL_SIZE
#undef MAX_SIZE
}
/* reset_record --- start over again with current $0 */
void
reset_record()
{
register int i;
NODE *n;
(void) force_string(fields_arr[0]);
NF = -1;
for (i = 1; i <= parse_high_water; i++) {
unref(fields_arr[i]);
getnode(n);
*n = *Null_field;
fields_arr[i] = n;
}
parse_high_water = 0;
/*
* $0 = $0 should resplit using the current value of FS.
*/
if (resave_fs) {
resave_fs = FALSE;
unref(save_FS);
save_FS = dupnode(FS_node->var_value);
}
field0_valid = TRUE;
}
/* set_NF --- handle what happens to $0 and fields when NF is changed */
void
set_NF()
{
register int i;
NODE *n;
assert(NF != -1);
NF = (long) force_number(NF_node->var_value);
if (NF < 0)
fatal(_("NF set to negative value"));
if (NF > nf_high_water)
grow_fields_arr(NF);
if (parse_high_water < NF) {
for (i = parse_high_water + 1; i >= 0 && i <= NF; i++) {
unref(fields_arr[i]);
getnode(n);
*n = *Null_field;
fields_arr[i] = n;
}
} else if (parse_high_water > 0) {
for (i = NF + 1; i >= 0 && i <= parse_high_water; i++) {
unref(fields_arr[i]);
getnode(n);
*n = *Null_field;
fields_arr[i] = n;
}
parse_high_water = NF;
}
field0_valid = FALSE;
}
/*
* re_parse_field --- parse fields using a regexp.
*
* This is called both from get_field() and from do_split()
* via (*parse_field)(). This variation is for when FS is a regular
* expression -- either user-defined or because RS=="" and FS==" "
*/
static long
re_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
int len,
NODE *fs ATTRIBUTE_UNUSED,
Regexp *rp,
Setfunc set, /* routine to set the value of the parsed field */
NODE *n,
int in_middle)
{
register char *scan = *buf;
register long nf = parse_high_water;
register char *field;
register char *end = scan + len;
int regex_flags = RE_NEED_START;
#ifdef MBS_SUPPORT
size_t mbclen = 0;
mbstate_t mbs;
if (gawk_mb_cur_max > 1)
memset(&mbs, 0, sizeof(mbstate_t));
#endif
if (in_middle)
regex_flags |= RE_NO_BOL;
if (up_to == UNLIMITED)
nf = 0;
if (len == 0)
return nf;
if (RS_is_null && default_FS)
while (scan < end && (*scan == ' ' || *scan == '\t' || *scan == '\n'))
scan++;
field = scan;
while (scan < end
&& research(rp, scan, 0, (end - scan), regex_flags) != -1
&& nf < up_to) {
regex_flags |= RE_NO_BOL;
if (REEND(rp, scan) == RESTART(rp, scan)) { /* null match */
#ifdef MBS_SUPPORT
if (gawk_mb_cur_max > 1) {
mbclen = mbrlen(scan, end-scan, &mbs);
if ((mbclen == 1) || (mbclen == (size_t) -1)
|| (mbclen == (size_t) -2) || (mbclen == 0)) {
/* We treat it as a singlebyte character. */
mbclen = 1;
}
scan += mbclen;
} else
#endif
scan++;
if (scan == end) {
(*set)(++nf, field, (long)(scan - field), n);
up_to = nf;
break;
}
continue;
}
(*set)(++nf, field,
(long)(scan + RESTART(rp, scan) - field), n);
scan += REEND(rp, scan);
field = scan;
if (scan == end) /* FS at end of record */
(*set)(++nf, field, 0L, n);
}
if (nf != up_to && scan < end) {
(*set)(++nf, scan, (long)(end - scan), n);
scan = end;
}
*buf = scan;
return nf;
}
/*
* def_parse_field --- default field parsing.
*
* This is called both from get_field() and from do_split()
* via (*parse_field)(). This variation is for when FS is a single space
* character.
*/
static long
def_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
int len,
NODE *fs,
Regexp *rp ATTRIBUTE_UNUSED,
Setfunc set, /* routine to set the value of the parsed field */
NODE *n,
int in_middle ATTRIBUTE_UNUSED)
{
register char *scan = *buf;
register long nf = parse_high_water;
register char *field;
register char *end = scan + len;
char sav;
if (up_to == UNLIMITED)
nf = 0;
if (len == 0)
return nf;
/*
* Nasty special case. If FS set to "", return whole record
* as first field. This is not worth a separate function.
*/
if (fs->stlen == 0) {
(*set)(++nf, *buf, len, n);
*buf += len;
return nf;
}
/* before doing anything save the char at *end */
sav = *end;
/* because it will be destroyed now: */
*end = ' '; /* sentinel character */
for (; nf < up_to; scan++) {
/*
* special case: fs is single space, strip leading whitespace
*/
while (scan < end && (*scan == ' ' || *scan == '\t' || *scan == '\n'))
scan++;
if (scan >= end)
break;
field = scan;
while (*scan != ' ' && *scan != '\t' && *scan != '\n')
scan++;
(*set)(++nf, field, (long)(scan - field), n);
if (scan == end)
break;
}
/* everything done, restore original char at *end */
*end = sav;
*buf = scan;
return nf;
}
/*
* posix_def_parse_field --- default field parsing.
*
* This is called both from get_field() and from do_split()
* via (*parse_field)(). This variation is for when FS is a single space
* character. The only difference between this and def_parse_field()
* is that this one does not allow newlines to separate fields.
*/
static long
posix_def_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
int len,
NODE *fs,
Regexp *rp ATTRIBUTE_UNUSED,
Setfunc set, /* routine to set the value of the parsed field */
NODE *n,
int in_middle ATTRIBUTE_UNUSED)
{
register char *scan = *buf;
register long nf = parse_high_water;
register char *field;
register char *end = scan + len;
char sav;
if (up_to == UNLIMITED)
nf = 0;
if (len == 0)
return nf;
/*
* Nasty special case. If FS set to "", return whole record
* as first field. This is not worth a separate function.
*/
if (fs->stlen == 0) {
(*set)(++nf, *buf, len, n);
*buf += len;
return nf;
}
/* before doing anything save the char at *end */
sav = *end;
/* because it will be destroyed now: */
*end = ' '; /* sentinel character */
for (; nf < up_to; scan++) {
/*
* special case: fs is single space, strip leading whitespace
*/
while (scan < end && (*scan == ' ' || *scan == '\t'))
scan++;
if (scan >= end)
break;
field = scan;
while (*scan != ' ' && *scan != '\t')
scan++;
(*set)(++nf, field, (long)(scan - field), n);
if (scan == end)
break;
}
/* everything done, restore original char at *end */
*end = sav;
*buf = scan;
return nf;
}
/*
* null_parse_field --- each character is a separate field
*
* This is called both from get_field() and from do_split()
* via (*parse_field)(). This variation is for when FS is the null string.
*/
static long
null_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
int len,
NODE *fs ATTRIBUTE_UNUSED,
Regexp *rp ATTRIBUTE_UNUSED,
Setfunc set, /* routine to set the value of the parsed field */
NODE *n,
int in_middle ATTRIBUTE_UNUSED)
{
register char *scan = *buf;
register long nf = parse_high_water;
register char *end = scan + len;
if (up_to == UNLIMITED)
nf = 0;
if (len == 0)
return nf;
#ifdef MBS_SUPPORT
if (gawk_mb_cur_max > 1) {
mbstate_t mbs;
memset(&mbs, 0, sizeof(mbstate_t));
for (; nf < up_to && scan < end;) {
size_t mbclen = mbrlen(scan, end-scan, &mbs);
if ((mbclen == 1) || (mbclen == (size_t) -1)
|| (mbclen == (size_t) -2) || (mbclen == 0)) {
/* We treat it as a singlebyte character. */
mbclen = 1;
}
(*set)(++nf, scan, mbclen, n);
scan += mbclen;
}
} else
#endif
for (; nf < up_to && scan < end; scan++)
(*set)(++nf, scan, 1L, n);
*buf = scan;
return nf;
}
/*
* sc_parse_field --- single character field separator
*
* This is called both from get_field() and from do_split()
* via (*parse_field)(). This variation is for when FS is a single character
* other than space.
*/
static long
sc_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
int len,
NODE *fs,
Regexp *rp ATTRIBUTE_UNUSED,
Setfunc set, /* routine to set the value of the parsed field */
NODE *n,
int in_middle ATTRIBUTE_UNUSED)
{
register char *scan = *buf;
register char fschar;
register long nf = parse_high_water;
register char *field;
register char *end = scan + len;
char sav;
#ifdef MBS_SUPPORT
size_t mbclen = 0;
mbstate_t mbs;
if (gawk_mb_cur_max > 1)
memset(&mbs, 0, sizeof(mbstate_t));
#endif
if (up_to == UNLIMITED)
nf = 0;
if (len == 0)
return nf;
if (RS_is_null && fs->stlen == 0)
fschar = '\n';
else
fschar = fs->stptr[0];
/* before doing anything save the char at *end */
sav = *end;
/* because it will be destroyed now: */
*end = fschar; /* sentinel character */
for (; nf < up_to;) {
field = scan;
#ifdef MBS_SUPPORT
if (gawk_mb_cur_max > 1) {
while (*scan != fschar) {
mbclen = mbrlen(scan, end-scan, &mbs);
if ((mbclen == 1) || (mbclen == (size_t) -1)
|| (mbclen == (size_t) -2) || (mbclen == 0)) {
/* We treat it as a singlebyte character. */
mbclen = 1;
}
scan += mbclen;
}
} else
#endif
while (*scan != fschar)
scan++;
(*set)(++nf, field, (long)(scan - field), n);
if (scan == end)
break;
scan++;
if (scan == end) { /* FS at end of record */
(*set)(++nf, field, 0L, n);
break;
}
}
/* everything done, restore original char at *end */
*end = sav;
*buf = scan;
return nf;
}
/*
* fw_parse_field --- field parsing using FIELDWIDTHS spec
*
* This is called from get_field() via (*parse_field)().
* This variation is for fields are fixed widths.
*/
static long
fw_parse_field(long up_to, /* parse only up to this field number */
char **buf, /* on input: string to parse; on output: point to start next */
int len,
NODE *fs ATTRIBUTE_UNUSED,
Regexp *rp ATTRIBUTE_UNUSED,
Setfunc set, /* routine to set the value of the parsed field */
NODE *n,
int in_middle ATTRIBUTE_UNUSED)
{
register char *scan = *buf;
register long nf = parse_high_water;
register char *end = scan + len;
#ifdef MBS_SUPPORT
int nmbc;
size_t mbclen;
size_t mbslen;
size_t lenrest;
char *mbscan;
mbstate_t mbs;
memset(&mbs, 0, sizeof(mbstate_t));
#endif
if (up_to == UNLIMITED)
nf = 0;
if (len == 0)
return nf;
for (; nf < up_to && (len = FIELDWIDTHS[nf+1]) != -1; ) {
#ifdef MBS_SUPPORT
if (gawk_mb_cur_max > 1) {
nmbc = 0;
mbslen = 0;
mbscan = scan;
lenrest = end - scan;
while (nmbc < len && mbslen < lenrest) {
mbclen = mbrlen(mbscan, end - mbscan, &mbs);
if ( mbclen == 1
|| mbclen == (size_t) -1
|| mbclen == (size_t) -2
|| mbclen == 0) {
/* We treat it as a singlebyte character. */
mbclen = 1;
}
if (mbclen <= end - mbscan) {
mbscan += mbclen;
mbslen += mbclen;
++nmbc;
}
}
(*set)(++nf, scan, (long) mbslen, n);
scan += mbslen;
}
else
#endif
{
if (len > end - scan)
len = end - scan;
(*set)(++nf, scan, (long) len, n);
scan += len;
}
}
if (len == -1)
*buf = end;
else
*buf = scan;
return nf;
}
/* get_field --- return a particular $n */
/* assign is not NULL if this field is on the LHS of an assign */
NODE **
get_field(register long requested, Func_ptr *assign)
{
int in_middle = FALSE;
/*
* if requesting whole line but some other field has been altered,
* then the whole line must be rebuilt
*/
if (requested == 0) {
if (! field0_valid) {
/* first, parse remainder of input record */
if (NF == -1) {
NF = (*parse_field)(UNLIMITED-1, &parse_extent,
fields_arr[0]->stlen -
(parse_extent - fields_arr[0]->stptr),
save_FS, FS_regexp, set_field,
(NODE *) NULL, in_middle);
parse_high_water = NF;
}
rebuild_record();
}
if (assign != NULL)
*assign = reset_record;
return &fields_arr[0];
}
/* assert(requested > 0); */
if (assign != NULL)
field0_valid = FALSE; /* $0 needs reconstruction */
if (requested <= parse_high_water) /* already parsed this field */
return &fields_arr[requested];
if (NF == -1) { /* have not yet parsed to end of record */
/*
* parse up to requested fields, calling set_field() for each,
* saving in parse_extent the point where the parse left off
*/
if (parse_high_water == 0) /* starting at the beginning */
parse_extent = fields_arr[0]->stptr;
else
in_middle = TRUE;
parse_high_water = (*parse_field)(requested, &parse_extent,
fields_arr[0]->stlen - (parse_extent - fields_arr[0]->stptr),
save_FS, FS_regexp, set_field, (NODE *) NULL, in_middle);
/*
* if we reached the end of the record, set NF to the number of
* fields so far. Note that requested might actually refer to
* a field that is beyond the end of the record, but we won't
* set NF to that value at this point, since this is only a
* reference to the field and NF only gets set if the field
* is assigned to -- this case is handled below
*/
if (parse_extent == fields_arr[0]->stptr + fields_arr[0]->stlen)
NF = parse_high_water;
if (requested == UNLIMITED-1) /* UNLIMITED-1 means set NF */
requested = parse_high_water;
}
if (parse_high_water < requested) { /* requested beyond end of record */
if (assign != NULL) { /* expand record */
if (requested > nf_high_water)
grow_fields_arr(requested);
NF = requested;
parse_high_water = requested;
} else
return &Null_field;
}
return &fields_arr[requested];
}
/* set_element --- set an array element, used by do_split() */
static void
set_element(long num, char *s, long len, NODE *n)
{
register NODE *it;
it = make_string(s, len);
it->flags |= MAYBE_NUM;
*assoc_lookup(n, tmp_number((AWKNUM) (num)), FALSE) = it;
}
/* do_split --- implement split(), semantics are same as for field splitting */
NODE *
do_split(NODE *tree)
{
NODE *src, *arr, *sep, *fs, *src2, *fs2, *tmp;
char *s;
long (*parseit) P((long, char **, int, NODE *,
Regexp *, Setfunc, NODE *, int));
Regexp *rp = NULL;
src = force_string(tree_eval(tree->lnode));
arr = get_param(tree->rnode->lnode);
if (arr->type != Node_var_array)
fatal(_("split: second argument is not an array"));
sep = tree->rnode->rnode->lnode;
if (src->stlen == 0) {
/*
* Skip the work if first arg is the null string.
*/
free_temp(src);
/*
* Evaluate sep if it may have side effects.
*/
if ((sep->re_flags & (FS_DFLT|CONSTANT)) == 0)
free_temp(tree_eval(sep->re_exp));
/*
* And now we can safely turn off the array.
*/
assoc_clear(arr);
return tmp_number((AWKNUM) 0);
}
if ((sep->re_flags & FS_DFLT) != 0 && ! using_FIELDWIDTHS() && ! RS_is_null) {
parseit = parse_field;
fs = force_string(FS_node->var_value);
rp = FS_regexp;
} else {
fs = force_string(tree_eval(sep->re_exp));
if (fs->stlen == 0) {
static short warned = FALSE;
parseit = null_parse_field;
if (do_lint && ! warned) {
warned = TRUE;
lintwarn(_("split: null string for third arg is a gawk extension"));
}
} else if (fs->stlen == 1 && (sep->re_flags & CONSTANT) == 0) {
if (fs->stptr[0] == ' ') {
if (do_posix)
parseit = posix_def_parse_field;
else
parseit = def_parse_field;
} else
parseit = sc_parse_field;
} else {
parseit = re_parse_field;
rp = re_update(sep);
}
}
/*
* do dupnode(), to avoid problems like
* x = split(a["LINE"], a, a["FS"])
* since we assoc_clear the array. gack.
* this also gives us complete call by value semantics.
*/
src2 = dupnode(src);
free_temp(src);
fs2 = dupnode(fs);
free_temp(fs);
assoc_clear(arr);
s = src2->stptr;
tmp = tmp_number((AWKNUM) (*parseit)(UNLIMITED, &s, (int) src2->stlen,
fs2, rp, set_element, arr, FALSE));
unref(src2);
unref(fs2);
return tmp;
}
/* set_FIELDWIDTHS --- handle an assignment to FIELDWIDTHS */
void
set_FIELDWIDTHS()
{
register char *scan;
char *end;
register int i;
static int fw_alloc = 4;
static short warned = FALSE;
extern unsigned long strtoul P((const char *, char **endptr, int base));
if (do_lint && ! warned) {
warned = TRUE;
lintwarn(_("`FIELDWIDTHS' is a gawk extension"));
}
if (do_traditional) /* quick and dirty, does the trick */
return;
/*
* If changing the way fields are split, obey least-suprise
* semantics, and force $0 to be split totally.
*/
if (fields_arr != NULL)
(void) get_field(UNLIMITED - 1, 0);
parse_field = fw_parse_field;
scan = force_string(FIELDWIDTHS_node->var_value)->stptr;
if (FIELDWIDTHS == NULL)
emalloc(FIELDWIDTHS, int *, fw_alloc * sizeof(int), "set_FIELDWIDTHS");
FIELDWIDTHS[0] = 0;
for (i = 1; ; i++) {
unsigned long int tmp;
if (i + 1 >= fw_alloc) {
fw_alloc *= 2;
erealloc(FIELDWIDTHS, int *, fw_alloc * sizeof(int), "set_FIELDWIDTHS");
}
/* Ensure that there is no leading `-' sign. Otherwise,
strtoul would accept it and return a bogus result. */
while (is_blank(*scan)) {
++scan;
}
if (*scan == '-')
fatal(_("invalid FIELDWIDTHS value, near `%s'"),
scan);
if (*scan == '\0')
break;
/* Detect an invalid base-10 integer, a valid value that
is followed by something other than a blank or '\0',
or a value that is not in the range [1..INT_MAX]. */
errno = 0;
tmp = strtoul(scan, &end, 10);
if (errno != 0
|| (*end != '\0' && ! is_blank(*end))
|| !(0 < tmp && tmp <= INT_MAX))
fatal(_("invalid FIELDWIDTHS value, near `%s'"),
scan);
FIELDWIDTHS[i] = tmp;
scan = end;
/* Skip past any trailing blanks. */
while (is_blank(*scan)) {
++scan;
}
if (*scan == '\0')
break;
}
if (i == 1) /* empty string! */
i--;
FIELDWIDTHS[i+1] = -1;
update_PROCINFO("FS", "FIELDWIDTHS");
}
/* set_FS --- handle things when FS is assigned to */
void
set_FS()
{
char buf[10];
NODE *fs;
static NODE *save_fs = NULL;
static NODE *save_rs = NULL;
int remake_re = TRUE;
/*
* If changing the way fields are split, obey least-suprise
* semantics, and force $0 to be split totally.
*/
if (fields_arr != NULL)
(void) get_field(UNLIMITED - 1, 0);
/* It's possible that only IGNORECASE changed, or FS = FS */
/*
* This comparison can't use cmp_nodes(), which pays attention
* to IGNORECASE, and that's not what we want.
*/
if (save_fs
&& FS_node->var_value->stlen == save_fs->stlen
&& memcmp(FS_node->var_value->stptr, save_fs->stptr, save_fs->stlen) == 0
&& save_rs
&& RS_node->var_value->stlen == save_rs->stlen
&& memcmp(RS_node->var_value->stptr, save_rs->stptr, save_rs->stlen) == 0) {
if (FS_regexp != NULL)
FS_regexp = (IGNORECASE ? FS_re_no_case : FS_re_yes_case);
/* FS = FS */
if (! using_FIELDWIDTHS()) {
return;
} else {
remake_re = FALSE;
goto choose_fs_function;
}
}
unref(save_fs);
save_fs = dupnode(FS_node->var_value);
unref(save_rs);
save_rs = dupnode(RS_node->var_value);
resave_fs = TRUE;
if (FS_regexp != NULL) {
refree(FS_re_yes_case);
refree(FS_re_no_case);
FS_re_yes_case = FS_re_no_case = FS_regexp = NULL;
}
choose_fs_function:
buf[0] = '\0';
default_FS = FALSE;
fs = force_string(FS_node->var_value);
if (! do_traditional && fs->stlen == 0) {
static short warned = FALSE;
parse_field = null_parse_field;
if (do_lint && ! warned) {
warned = TRUE;
lintwarn(_("null string for `FS' is a gawk extension"));
}
} else if (fs->stlen > 1) {
if (do_lint_old)
warning(_("old awk does not support regexps as value of `FS'"));
parse_field = re_parse_field;
} else if (RS_is_null) {
/* we know that fs->stlen <= 1 */
parse_field = sc_parse_field;
if (fs->stlen == 1) {
if (fs->stptr[0] == ' ') {
default_FS = TRUE;
strcpy(buf, "[ \t\n]+");
} else if (fs->stptr[0] == '\\') {
/* yet another special case */
strcpy(buf, "[\\\\\n]");
} else if (fs->stptr[0] != '\n')
sprintf(buf, "[%c\n]", fs->stptr[0]);
}
} else {
if (do_posix)
parse_field = posix_def_parse_field;
else
parse_field = def_parse_field;
if (fs->stlen == 1) {
if (fs->stptr[0] == ' ')
default_FS = TRUE;
else if (fs->stptr[0] == '\\')
/* same special case */
strcpy(buf, "[\\\\]");
else
parse_field = sc_parse_field;
}
}
if (remake_re) {
if (FS_regexp != NULL) {
refree(FS_re_yes_case);
refree(FS_re_no_case);
FS_re_yes_case = FS_re_no_case = FS_regexp = NULL;
}
if (buf[0] != '\0') {
FS_re_yes_case = make_regexp(buf, strlen(buf), FALSE, TRUE);
FS_re_no_case = make_regexp(buf, strlen(buf), TRUE, TRUE);
FS_regexp = (IGNORECASE ? FS_re_no_case : FS_re_yes_case);
parse_field = re_parse_field;
} else if (parse_field == re_parse_field) {
FS_re_yes_case = make_regexp(fs->stptr, fs->stlen, FALSE, TRUE);
FS_re_no_case = make_regexp(fs->stptr, fs->stlen, TRUE, TRUE);
FS_regexp = (IGNORECASE ? FS_re_no_case : FS_re_yes_case);
} else
FS_re_yes_case = FS_re_no_case = FS_regexp = NULL;
}
/*
* For FS = "c", we don't use IGNORECASE. But we must use
* re_parse_field to get the character and the newline as
* field separators.
*/
if (fs->stlen == 1 && parse_field == re_parse_field)
FS_regexp = FS_re_yes_case;
update_PROCINFO("FS", "FS");
}
/* using_fieldwidths --- is FS or FIELDWIDTHS in use? */
int
using_fieldwidths()
{
return using_FIELDWIDTHS();
}
/* update_PROCINFO --- update PROCINFO[sub] when FS or FIELDWIDTHS set */
static void
update_PROCINFO(char *subscript, char *str)
{
NODE **aptr;
if (PROCINFO_node == NULL)
return;
aptr = assoc_lookup(PROCINFO_node, tmp_string(subscript, strlen(subscript)), FALSE);
assign_val(aptr, tmp_string(str, strlen(str)));
}
|