version 1.28, 2020/06/10 21:02:19 |
version 1.29, 2020/06/10 21:02:33 |
|
|
infile = stdin; /* no filenames, so use stdin */ |
infile = stdin; /* no filenames, so use stdin */ |
} |
} |
|
|
|
/* |
|
* POSIX specifies that fields are supposed to be evaluated as if they were |
|
* split using the value of FS at the time that the record's value ($0) was |
|
* read. |
|
* |
|
* Since field-splitting is done lazily, we save the current value of FS |
|
* whenever a new record is read in (implicitly or via getline), or when |
|
* a new value is assigned to $0. |
|
*/ |
|
void savefs(void) |
|
{ |
|
if (strlen(getsval(fsloc)) >= sizeof (inputFS)) |
|
FATAL("field separator %.10s... is too long", *FS); |
|
strlcpy(inputFS, *FS, sizeof(inputFS)); |
|
} |
|
|
static int firsttime = 1; |
static int firsttime = 1; |
|
|
int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ |
int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */ |
|
|
if (isrecord) { |
if (isrecord) { |
donefld = 0; |
donefld = 0; |
donerec = 1; |
donerec = 1; |
|
savefs(); |
} |
} |
saveb0 = buf[0]; |
saveb0 = buf[0]; |
buf[0] = 0; |
buf[0] = 0; |
|
|
int bufsize = *pbufsize; |
int bufsize = *pbufsize; |
char *rs = getsval(rsloc); |
char *rs = getsval(rsloc); |
|
|
if (strlen(getsval(fsloc)) >= sizeof (inputFS)) |
|
FATAL("field separator %.10s... is too long", *FS); |
|
/*fflush(stdout); avoids some buffering problem but makes it 25% slower*/ |
|
strlcpy(inputFS, *FS, sizeof inputFS); /* for subsequent field splitting */ |
|
if ((sep = *rs) == 0) { |
if ((sep = *rs) == 0) { |
sep = '\n'; |
sep = '\n'; |
while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */ |
while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */ |
|
|
} |
} |
fr = fields; |
fr = fields; |
i = 0; /* number of fields accumulated here */ |
i = 0; /* number of fields accumulated here */ |
if (strlen(getsval(fsloc)) >= sizeof (inputFS)) |
|
FATAL("field separator %.10s... is too long", *FS); |
|
strlcpy(inputFS, *FS, sizeof(inputFS)); |
|
if (strlen(inputFS) > 1) { /* it's a regular expression */ |
if (strlen(inputFS) > 1) { /* it's a regular expression */ |
i = refldbld(r, inputFS); |
i = refldbld(r, inputFS); |
} else if ((sep = *inputFS) == ' ') { /* default whitespace */ |
} else if ((sep = *inputFS) == ' ') { /* default whitespace */ |
|
|
break; |
break; |
} |
} |
} |
} |
return i; |
return i; |
} |
} |
|
|
void recbld(void) /* create $0 from $1..$NF if necessary */ |
void recbld(void) /* create $0 from $1..$NF if necessary */ |
|
|
static int been_here = 0; |
static int been_here = 0; |
extern char ebuf[], *ep; |
extern char ebuf[], *ep; |
|
|
if (compile_time == 2 || compile_time == 0 || been_here++ > 0 || |
if (compile_time == 2 || compile_time == 0 || been_here++ > 0 || ebuf == ep) |
ebuf == ep) |
|
return; |
return; |
p = ep - 1; |
p = ep - 1; |
if (p > ebuf && *p == '\n') |
if (p > ebuf && *p == '\n') |