1993-07-08 02:18:43 +04:00
|
|
|
/*
|
|
|
|
* builtin.c - Builtin functions and various utility procedures
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1995-04-05 04:11:36 +04:00
|
|
|
* Copyright (C) 1986, 1988, 1989, 1991-1995 the Free Software Foundation, Inc.
|
1993-07-08 02:18:43 +04:00
|
|
|
*
|
|
|
|
* This file is part of GAWK, the GNU implementation of the
|
|
|
|
* AWK Progamming 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 2 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 GAWK; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "awk.h"
|
|
|
|
|
|
|
|
#ifndef SRANDOM_PROTO
|
1994-05-20 12:03:48 +04:00
|
|
|
extern void srandom P((unsigned int seed));
|
1993-07-08 02:18:43 +04:00
|
|
|
#endif
|
1995-04-05 04:11:36 +04:00
|
|
|
#if defined(RANDOM_MISSING)
|
1993-07-08 02:18:43 +04:00
|
|
|
extern char *initstate P((unsigned seed, char *state, int n));
|
|
|
|
extern char *setstate P((char *state));
|
|
|
|
extern long random P((void));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern NODE **fields_arr;
|
|
|
|
extern int output_is_tty;
|
|
|
|
|
|
|
|
static NODE *sub_common P((NODE *tree, int global));
|
1994-05-20 12:03:48 +04:00
|
|
|
NODE *format_tree P((const char *, int, NODE *));
|
1993-07-08 02:18:43 +04:00
|
|
|
|
|
|
|
#ifdef _CRAY
|
|
|
|
/* Work around a problem in conversion of doubles to exact integers. */
|
|
|
|
#include <float.h>
|
|
|
|
#define Floor(n) floor((n) * (1.0 + DBL_EPSILON))
|
|
|
|
#define Ceil(n) ceil((n) * (1.0 + DBL_EPSILON))
|
|
|
|
|
|
|
|
/* Force the standard C compiler to use the library math functions. */
|
|
|
|
extern double exp(double);
|
|
|
|
double (*Exp)() = exp;
|
|
|
|
#define exp(x) (*Exp)(x)
|
|
|
|
extern double log(double);
|
|
|
|
double (*Log)() = log;
|
|
|
|
#define log(x) (*Log)(x)
|
|
|
|
#else
|
|
|
|
#define Floor(n) floor(n)
|
|
|
|
#define Ceil(n) ceil(n)
|
|
|
|
#endif
|
|
|
|
|
1994-05-20 12:03:48 +04:00
|
|
|
#define DEFAULT_G_PRECISION 6
|
|
|
|
|
|
|
|
#ifdef GFMT_WORKAROUND
|
|
|
|
/* semi-temporary hack, mostly to gracefully handle VMS */
|
|
|
|
static void sgfmt P((char *buf, const char *format, int alt,
|
|
|
|
int fwidth, int precision, double value));
|
|
|
|
#endif /* GFMT_WORKAROUND */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On the alpha, LONG_MAX is too big for doing rand().
|
|
|
|
* On the Cray (Y-MP, anyway), ints and longs are 64 bits, but
|
|
|
|
* random() does things in terms of 32 bits. So we have to chop
|
|
|
|
* LONG_MAX down.
|
|
|
|
*/
|
|
|
|
#if (defined(__alpha) && defined(__osf__)) || defined(_CRAY)
|
|
|
|
#define GAWK_RANDOM_MAX (LONG_MAX & 0x7fffffff)
|
|
|
|
#else
|
|
|
|
#define GAWK_RANDOM_MAX LONG_MAX
|
|
|
|
#endif
|
1994-02-17 04:21:51 +03:00
|
|
|
|
|
|
|
static void efwrite P((const void *ptr, size_t size, size_t count, FILE *fp,
|
|
|
|
const char *from, struct redirect *rp,int flush));
|
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
static void
|
|
|
|
efwrite(ptr, size, count, fp, from, rp, flush)
|
1994-02-17 04:21:51 +03:00
|
|
|
const void *ptr;
|
1993-11-13 05:26:02 +03:00
|
|
|
size_t size, count;
|
1993-07-08 02:18:43 +04:00
|
|
|
FILE *fp;
|
1994-02-17 04:21:51 +03:00
|
|
|
const char *from;
|
1993-07-08 02:18:43 +04:00
|
|
|
struct redirect *rp;
|
|
|
|
int flush;
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
if (fwrite(ptr, size, count, fp) != count)
|
|
|
|
goto wrerror;
|
|
|
|
if (flush
|
|
|
|
&& ((fp == stdout && output_is_tty)
|
|
|
|
|| (rp && (rp->flag & RED_NOBUF)))) {
|
|
|
|
fflush(fp);
|
|
|
|
if (ferror(fp))
|
|
|
|
goto wrerror;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
wrerror:
|
|
|
|
fatal("%s to \"%s\" failed (%s)", from,
|
|
|
|
rp ? rp->value : "standard output",
|
|
|
|
errno ? strerror(errno) : "reason unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Builtin functions */
|
|
|
|
NODE *
|
|
|
|
do_exp(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
double d, res;
|
|
|
|
#ifndef exp
|
|
|
|
double exp P((double));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tmp= tree_eval(tree->lnode);
|
|
|
|
d = force_number(tmp);
|
|
|
|
free_temp(tmp);
|
|
|
|
errno = 0;
|
|
|
|
res = exp(d);
|
|
|
|
if (errno == ERANGE)
|
|
|
|
warning("exp argument %g is out of range", d);
|
|
|
|
return tmp_number((AWKNUM) res);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_index(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *s1, *s2;
|
|
|
|
register char *p1, *p2;
|
1993-11-13 05:26:02 +03:00
|
|
|
register size_t l1, l2;
|
1993-07-08 02:18:43 +04:00
|
|
|
long ret;
|
|
|
|
|
|
|
|
|
|
|
|
s1 = tree_eval(tree->lnode);
|
|
|
|
s2 = tree_eval(tree->rnode->lnode);
|
|
|
|
force_string(s1);
|
|
|
|
force_string(s2);
|
|
|
|
p1 = s1->stptr;
|
|
|
|
p2 = s2->stptr;
|
|
|
|
l1 = s1->stlen;
|
|
|
|
l2 = s2->stlen;
|
|
|
|
ret = 0;
|
|
|
|
if (IGNORECASE) {
|
|
|
|
while (l1) {
|
|
|
|
if (l2 > l1)
|
|
|
|
break;
|
|
|
|
if (casetable[(int)*p1] == casetable[(int)*p2]
|
|
|
|
&& (l2 == 1 || strncasecmp(p1, p2, l2) == 0)) {
|
|
|
|
ret = 1 + s1->stlen - l1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
l1--;
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (l1) {
|
|
|
|
if (l2 > l1)
|
|
|
|
break;
|
|
|
|
if (*p1 == *p2
|
|
|
|
&& (l2 == 1 || STREQN(p1, p2, l2))) {
|
|
|
|
ret = 1 + s1->stlen - l1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
l1--;
|
|
|
|
p1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_temp(s1);
|
|
|
|
free_temp(s2);
|
|
|
|
return tmp_number((AWKNUM) ret);
|
|
|
|
}
|
|
|
|
|
1994-05-20 12:03:48 +04:00
|
|
|
double
|
|
|
|
double_to_int(d)
|
|
|
|
double d;
|
|
|
|
{
|
|
|
|
double floor P((double));
|
|
|
|
double ceil P((double));
|
|
|
|
|
|
|
|
if (d >= 0)
|
|
|
|
d = Floor(d);
|
|
|
|
else
|
|
|
|
d = Ceil(d);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
NODE *
|
|
|
|
do_int(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
d = force_number(tmp);
|
1994-05-20 12:03:48 +04:00
|
|
|
d = double_to_int(d);
|
1993-07-08 02:18:43 +04:00
|
|
|
free_temp(tmp);
|
|
|
|
return tmp_number((AWKNUM) d);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_length(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
1993-11-13 05:26:02 +03:00
|
|
|
size_t len;
|
1993-07-08 02:18:43 +04:00
|
|
|
|
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
len = force_string(tmp)->stlen;
|
|
|
|
free_temp(tmp);
|
|
|
|
return tmp_number((AWKNUM) len);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_log(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
#ifndef log
|
|
|
|
double log P((double));
|
|
|
|
#endif
|
|
|
|
double d, arg;
|
|
|
|
|
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
arg = (double) force_number(tmp);
|
|
|
|
if (arg < 0.0)
|
|
|
|
warning("log called with negative argument %g", arg);
|
|
|
|
d = log(arg);
|
|
|
|
free_temp(tmp);
|
|
|
|
return tmp_number((AWKNUM) d);
|
|
|
|
}
|
|
|
|
|
1994-02-17 04:21:51 +03:00
|
|
|
/*
|
1994-05-20 12:03:48 +04:00
|
|
|
* format_tree() formats nodes of a tree, starting with a left node,
|
|
|
|
* and accordingly to a fmt_string providing a format like in
|
|
|
|
* printf family from C library. Returns a string node which value
|
|
|
|
* is a formatted string. Called by sprintf function.
|
|
|
|
*
|
|
|
|
* It is one of the uglier parts of gawk. Thanks to Michal Jaegermann
|
|
|
|
* for taming this beast and making it compatible with ANSI C.
|
1994-02-17 04:21:51 +03:00
|
|
|
*/
|
1993-07-08 02:18:43 +04:00
|
|
|
|
|
|
|
NODE *
|
1994-05-20 12:03:48 +04:00
|
|
|
format_tree(fmt_string, n0, carg)
|
|
|
|
const char *fmt_string;
|
|
|
|
int n0;
|
|
|
|
register NODE *carg;
|
1993-07-08 02:18:43 +04:00
|
|
|
{
|
1994-02-17 04:21:51 +03:00
|
|
|
/* copy 'l' bytes from 's' to 'obufout' checking for space in the process */
|
|
|
|
/* difference of pointers should be of ptrdiff_t type, but let us be kind */
|
1993-07-08 02:18:43 +04:00
|
|
|
#define bchunk(s,l) if(l) {\
|
|
|
|
while((l)>ofre) {\
|
1994-02-17 04:21:51 +03:00
|
|
|
long olen = obufout - obuf;\
|
1994-05-20 12:03:48 +04:00
|
|
|
erealloc(obuf, char *, osiz*2, "format_tree");\
|
1993-07-08 02:18:43 +04:00
|
|
|
ofre+=osiz;\
|
|
|
|
osiz*=2;\
|
1994-02-17 04:21:51 +03:00
|
|
|
obufout = obuf + olen;\
|
1993-07-08 02:18:43 +04:00
|
|
|
}\
|
1994-02-17 04:21:51 +03:00
|
|
|
memcpy(obufout,s,(size_t)(l));\
|
|
|
|
obufout+=(l);\
|
1993-07-08 02:18:43 +04:00
|
|
|
ofre-=(l);\
|
1994-02-17 04:21:51 +03:00
|
|
|
}
|
|
|
|
/* copy one byte from 's' to 'obufout' checking for space in the process */
|
|
|
|
#define bchunk_one(s) {\
|
|
|
|
if(ofre <= 0) {\
|
|
|
|
long olen = obufout - obuf;\
|
1994-05-20 12:03:48 +04:00
|
|
|
erealloc(obuf, char *, osiz*2, "format_tree");\
|
1994-02-17 04:21:51 +03:00
|
|
|
ofre+=osiz;\
|
|
|
|
osiz*=2;\
|
|
|
|
obufout = obuf + olen;\
|
|
|
|
}\
|
|
|
|
*obufout++ = *s;\
|
|
|
|
--ofre;\
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is there space for something L big in the buffer? */
|
|
|
|
#define chksize(l) if((l)>ofre) {\
|
1994-05-20 12:03:48 +04:00
|
|
|
long olen = obufout - obuf;\
|
|
|
|
erealloc(obuf, char *, osiz*2, "format_tree");\
|
|
|
|
obufout = obuf + olen;\
|
1993-07-08 02:18:43 +04:00
|
|
|
ofre+=osiz;\
|
|
|
|
osiz*=2;\
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next arg to be formatted. If we've run out of args,
|
|
|
|
* return "" (Null string)
|
|
|
|
*/
|
|
|
|
#define parse_next_arg() {\
|
|
|
|
if(!carg) { toofew = 1; break; }\
|
|
|
|
else {\
|
|
|
|
arg=tree_eval(carg->lnode);\
|
|
|
|
carg=carg->rnode;\
|
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *r;
|
|
|
|
int toofew = 0;
|
1994-02-17 04:21:51 +03:00
|
|
|
char *obuf, *obufout;
|
|
|
|
size_t osiz, ofre;
|
|
|
|
char *chbuf;
|
1994-05-20 12:03:48 +04:00
|
|
|
const char *s0, *s1;
|
1994-02-17 04:21:51 +03:00
|
|
|
int cs1;
|
1994-05-20 12:03:48 +04:00
|
|
|
NODE *arg;
|
1994-02-17 04:21:51 +03:00
|
|
|
long fw, prec;
|
1994-05-20 12:03:48 +04:00
|
|
|
int lj, alt, big, have_prec;
|
1993-07-08 02:18:43 +04:00
|
|
|
long *cur;
|
|
|
|
long val;
|
|
|
|
#ifdef sun386 /* Can't cast unsigned (int/long) from ptr->value */
|
|
|
|
long tmp_uval; /* on 386i 4.0.1 C compiler -- it just hangs */
|
|
|
|
#endif
|
|
|
|
unsigned long uval;
|
|
|
|
int sgn;
|
1994-05-20 12:03:48 +04:00
|
|
|
int base = 0;
|
1993-07-08 02:18:43 +04:00
|
|
|
char cpbuf[30]; /* if we have numbers bigger than 30 */
|
|
|
|
char *cend = &cpbuf[30];/* chars, we lose, but seems unlikely */
|
|
|
|
char *cp;
|
|
|
|
char *fill;
|
|
|
|
double tmpval;
|
|
|
|
char signchar = 0;
|
1993-11-13 05:26:02 +03:00
|
|
|
size_t len;
|
1994-02-17 04:21:51 +03:00
|
|
|
static char sp[] = " ";
|
|
|
|
static char zero_string[] = "0";
|
1994-05-20 12:03:48 +04:00
|
|
|
static char lchbuf[] = "0123456789abcdef";
|
|
|
|
static char Uchbuf[] = "0123456789ABCDEF";
|
1993-07-08 02:18:43 +04:00
|
|
|
|
1994-05-20 12:03:48 +04:00
|
|
|
emalloc(obuf, char *, 120, "format_tree");
|
1994-02-17 04:21:51 +03:00
|
|
|
obufout = obuf;
|
1993-07-08 02:18:43 +04:00
|
|
|
osiz = 120;
|
|
|
|
ofre = osiz - 1;
|
1994-05-20 12:03:48 +04:00
|
|
|
|
|
|
|
s0 = s1 = fmt_string;
|
|
|
|
while (n0-- > 0) {
|
1993-07-08 02:18:43 +04:00
|
|
|
if (*s1 != '%') {
|
|
|
|
s1++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bchunk(s0, s1 - s0);
|
|
|
|
s0 = s1;
|
|
|
|
cur = &fw;
|
|
|
|
fw = 0;
|
|
|
|
prec = 0;
|
1994-05-20 12:03:48 +04:00
|
|
|
have_prec = 0;
|
1993-07-08 02:18:43 +04:00
|
|
|
lj = alt = big = 0;
|
|
|
|
fill = sp;
|
|
|
|
cp = cend;
|
1994-05-20 12:03:48 +04:00
|
|
|
chbuf = lchbuf;
|
1993-07-08 02:18:43 +04:00
|
|
|
s1++;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
--n0;
|
1994-02-17 04:21:51 +03:00
|
|
|
switch (cs1 = *s1++) {
|
1994-05-20 12:03:48 +04:00
|
|
|
case (-1): /* dummy case to allow for checking */
|
|
|
|
check_pos:
|
|
|
|
if (cur != &fw)
|
|
|
|
break; /* reject as a valid format */
|
|
|
|
goto retry;
|
1993-07-08 02:18:43 +04:00
|
|
|
case '%':
|
1994-02-17 04:21:51 +03:00
|
|
|
bchunk_one("%");
|
1993-07-08 02:18:43 +04:00
|
|
|
s0 = s1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '0':
|
1994-02-17 04:21:51 +03:00
|
|
|
if (lj)
|
|
|
|
goto retry;
|
1993-07-08 02:18:43 +04:00
|
|
|
if (cur == &fw)
|
1994-02-17 04:21:51 +03:00
|
|
|
fill = zero_string; /* FALL through */
|
1993-07-08 02:18:43 +04:00
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
if (cur == 0)
|
1994-02-17 04:21:51 +03:00
|
|
|
/* goto lose; */
|
|
|
|
break;
|
1994-05-20 12:03:48 +04:00
|
|
|
if (prec >= 0)
|
1994-02-17 04:21:51 +03:00
|
|
|
*cur = cs1 - '0';
|
1994-05-20 12:03:48 +04:00
|
|
|
/* with a negative precision *cur is already set */
|
|
|
|
/* to -1, so it will remain negative, but we have */
|
|
|
|
/* to "eat" precision digits in any case */
|
1993-07-08 02:18:43 +04:00
|
|
|
while (n0 > 0 && *s1 >= '0' && *s1 <= '9') {
|
|
|
|
--n0;
|
|
|
|
*cur = *cur * 10 + *s1++ - '0';
|
|
|
|
}
|
1994-05-20 12:03:48 +04:00
|
|
|
if (prec < 0) /* negative precision is discarded */
|
|
|
|
have_prec = 0;
|
|
|
|
if (cur == &prec)
|
1994-02-17 04:21:51 +03:00
|
|
|
cur = 0;
|
1993-07-08 02:18:43 +04:00
|
|
|
goto retry;
|
|
|
|
case '*':
|
|
|
|
if (cur == 0)
|
1994-02-17 04:21:51 +03:00
|
|
|
/* goto lose; */
|
|
|
|
break;
|
1993-07-08 02:18:43 +04:00
|
|
|
parse_next_arg();
|
|
|
|
*cur = force_number(arg);
|
|
|
|
free_temp(arg);
|
1994-05-20 12:03:48 +04:00
|
|
|
if (cur == &prec)
|
|
|
|
cur = 0;
|
1993-07-08 02:18:43 +04:00
|
|
|
goto retry;
|
|
|
|
case ' ': /* print ' ' or '-' */
|
1994-02-17 04:21:51 +03:00
|
|
|
/* 'space' flag is ignored */
|
|
|
|
/* if '+' already present */
|
|
|
|
if (signchar != 0)
|
1994-05-20 12:03:48 +04:00
|
|
|
goto check_pos;
|
1994-02-17 04:21:51 +03:00
|
|
|
/* FALL THROUGH */
|
1993-07-08 02:18:43 +04:00
|
|
|
case '+': /* print '+' or '-' */
|
1994-02-17 04:21:51 +03:00
|
|
|
signchar = cs1;
|
1994-05-20 12:03:48 +04:00
|
|
|
goto check_pos;
|
1993-07-08 02:18:43 +04:00
|
|
|
case '-':
|
1994-05-20 12:03:48 +04:00
|
|
|
if (prec < 0)
|
|
|
|
break;
|
1994-02-17 04:21:51 +03:00
|
|
|
if (cur == &prec) {
|
|
|
|
prec = -1;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
fill = sp; /* if left justified then other */
|
|
|
|
lj++; /* filling is ignored */
|
1994-05-20 12:03:48 +04:00
|
|
|
goto check_pos;
|
1993-07-08 02:18:43 +04:00
|
|
|
case '.':
|
|
|
|
if (cur != &fw)
|
1994-02-17 04:21:51 +03:00
|
|
|
break;
|
1993-07-08 02:18:43 +04:00
|
|
|
cur = ≺
|
1994-05-20 12:03:48 +04:00
|
|
|
have_prec++;
|
1993-07-08 02:18:43 +04:00
|
|
|
goto retry;
|
|
|
|
case '#':
|
|
|
|
alt++;
|
1994-05-20 12:03:48 +04:00
|
|
|
goto check_pos;
|
1993-07-08 02:18:43 +04:00
|
|
|
case 'l':
|
|
|
|
if (big)
|
1994-02-17 04:21:51 +03:00
|
|
|
break;
|
1995-04-05 04:11:36 +04:00
|
|
|
else {
|
|
|
|
static int warned = 0;
|
|
|
|
|
|
|
|
if (do_lint && ! warned) {
|
|
|
|
warning("`l' is meaningless in awk formats; ignored");
|
|
|
|
warned++;
|
|
|
|
}
|
|
|
|
if (do_posix)
|
|
|
|
fatal("'l' is not permitted in POSIX awk formats");
|
|
|
|
}
|
1993-07-08 02:18:43 +04:00
|
|
|
big++;
|
1995-04-05 04:11:36 +04:00
|
|
|
goto retry;
|
1993-07-08 02:18:43 +04:00
|
|
|
case 'c':
|
|
|
|
parse_next_arg();
|
|
|
|
if (arg->flags & NUMBER) {
|
|
|
|
#ifdef sun386
|
|
|
|
tmp_uval = arg->numbr;
|
|
|
|
uval= (unsigned long) tmp_uval;
|
|
|
|
#else
|
|
|
|
uval = (unsigned long) arg->numbr;
|
|
|
|
#endif
|
|
|
|
cpbuf[0] = uval;
|
|
|
|
prec = 1;
|
1994-02-17 04:21:51 +03:00
|
|
|
cp = cpbuf;
|
|
|
|
goto pr_tail;
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
1994-05-20 12:03:48 +04:00
|
|
|
if (have_prec == 0)
|
1993-07-08 02:18:43 +04:00
|
|
|
prec = 1;
|
|
|
|
else if (prec > arg->stlen)
|
|
|
|
prec = arg->stlen;
|
1994-02-17 04:21:51 +03:00
|
|
|
cp = arg->stptr;
|
|
|
|
goto pr_tail;
|
1993-07-08 02:18:43 +04:00
|
|
|
case 's':
|
|
|
|
parse_next_arg();
|
|
|
|
arg = force_string(arg);
|
1994-05-20 12:03:48 +04:00
|
|
|
if (have_prec == 0 || prec > arg->stlen)
|
1993-07-08 02:18:43 +04:00
|
|
|
prec = arg->stlen;
|
1994-02-17 04:21:51 +03:00
|
|
|
cp = arg->stptr;
|
|
|
|
goto pr_tail;
|
1993-07-08 02:18:43 +04:00
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
parse_next_arg();
|
1994-05-20 12:03:48 +04:00
|
|
|
tmpval = force_number(arg);
|
|
|
|
if (tmpval > LONG_MAX || tmpval < LONG_MIN) {
|
|
|
|
/* out of range - emergency use of %g format */
|
|
|
|
cs1 = 'g';
|
|
|
|
goto format_float;
|
|
|
|
}
|
|
|
|
val = (long) tmpval;
|
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
if (val < 0) {
|
|
|
|
sgn = 1;
|
1994-05-20 12:03:48 +04:00
|
|
|
if (val > LONG_MIN)
|
|
|
|
uval = (unsigned long) -val;
|
|
|
|
else
|
|
|
|
uval = (unsigned long)(-(LONG_MIN + 1))
|
|
|
|
+ (unsigned long)1;
|
|
|
|
} else {
|
1993-07-08 02:18:43 +04:00
|
|
|
sgn = 0;
|
1994-05-20 12:03:48 +04:00
|
|
|
uval = (unsigned long) val;
|
|
|
|
}
|
1993-07-08 02:18:43 +04:00
|
|
|
do {
|
1994-05-20 12:03:48 +04:00
|
|
|
*--cp = (char) ('0' + uval % 10);
|
|
|
|
uval /= 10;
|
|
|
|
} while (uval);
|
1993-07-08 02:18:43 +04:00
|
|
|
if (sgn)
|
|
|
|
*--cp = '-';
|
|
|
|
else if (signchar)
|
|
|
|
*--cp = signchar;
|
1994-05-20 12:03:48 +04:00
|
|
|
if (have_prec != 0) /* ignore '0' flag if */
|
1994-02-17 04:21:51 +03:00
|
|
|
fill = sp; /* precision given */
|
1993-07-08 02:18:43 +04:00
|
|
|
if (prec > fw)
|
|
|
|
fw = prec;
|
|
|
|
prec = cend - cp;
|
1994-02-17 04:21:51 +03:00
|
|
|
if (fw > prec && ! lj && fill != sp
|
|
|
|
&& (*cp == '-' || signchar)) {
|
|
|
|
bchunk_one(cp);
|
|
|
|
cp++;
|
|
|
|
prec--;
|
|
|
|
fw--;
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
1994-02-17 04:21:51 +03:00
|
|
|
goto pr_tail;
|
1993-07-08 02:18:43 +04:00
|
|
|
case 'X':
|
1994-05-20 12:03:48 +04:00
|
|
|
chbuf = Uchbuf; /* FALL THROUGH */
|
1993-07-08 02:18:43 +04:00
|
|
|
case 'x':
|
1994-05-20 12:03:48 +04:00
|
|
|
base += 6; /* FALL THROUGH */
|
|
|
|
case 'u':
|
|
|
|
base += 2; /* FALL THROUGH */
|
|
|
|
case 'o':
|
|
|
|
base += 8;
|
1993-07-08 02:18:43 +04:00
|
|
|
parse_next_arg();
|
1994-05-20 12:03:48 +04:00
|
|
|
tmpval = force_number(arg);
|
|
|
|
if (tmpval > ULONG_MAX || tmpval < LONG_MIN) {
|
|
|
|
/* out of range - emergency use of %g format */
|
|
|
|
cs1 = 'g';
|
|
|
|
goto format_float;
|
|
|
|
}
|
|
|
|
uval = (unsigned long)tmpval;
|
|
|
|
if (have_prec != 0) /* ignore '0' flag if */
|
|
|
|
fill = sp; /* precision given */
|
1993-07-08 02:18:43 +04:00
|
|
|
do {
|
|
|
|
*--cp = chbuf[uval % base];
|
|
|
|
uval /= base;
|
|
|
|
} while (uval);
|
1994-02-17 04:21:51 +03:00
|
|
|
if (alt) {
|
1993-07-08 02:18:43 +04:00
|
|
|
if (base == 16) {
|
1994-02-17 04:21:51 +03:00
|
|
|
*--cp = cs1;
|
|
|
|
*--cp = '0';
|
|
|
|
if (fill != sp) {
|
|
|
|
bchunk(cp, 2);
|
|
|
|
cp += 2;
|
|
|
|
fw -= 2;
|
|
|
|
}
|
|
|
|
} else if (base == 8)
|
|
|
|
*--cp = '0';
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
1994-05-20 12:03:48 +04:00
|
|
|
base = 0;
|
1993-07-08 02:18:43 +04:00
|
|
|
prec = cend - cp;
|
1994-02-17 04:21:51 +03:00
|
|
|
pr_tail:
|
|
|
|
if (! lj) {
|
1993-07-08 02:18:43 +04:00
|
|
|
while (fw > prec) {
|
1994-02-17 04:21:51 +03:00
|
|
|
bchunk_one(fill);
|
1993-07-08 02:18:43 +04:00
|
|
|
fw--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bchunk(cp, (int) prec);
|
1994-02-17 04:21:51 +03:00
|
|
|
while (fw > prec) {
|
|
|
|
bchunk_one(fill);
|
|
|
|
fw--;
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
s0 = s1;
|
1994-02-17 04:21:51 +03:00
|
|
|
free_temp(arg);
|
1993-07-08 02:18:43 +04:00
|
|
|
break;
|
1994-05-20 12:03:48 +04:00
|
|
|
case 'g':
|
|
|
|
case 'G':
|
1994-02-17 04:21:51 +03:00
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
case 'E':
|
1993-07-08 02:18:43 +04:00
|
|
|
parse_next_arg();
|
|
|
|
tmpval = force_number(arg);
|
1994-05-20 12:03:48 +04:00
|
|
|
format_float:
|
1993-07-08 02:18:43 +04:00
|
|
|
free_temp(arg);
|
1994-05-20 12:03:48 +04:00
|
|
|
if (have_prec == 0)
|
|
|
|
prec = DEFAULT_G_PRECISION;
|
1993-07-08 02:18:43 +04:00
|
|
|
chksize(fw + prec + 9); /* 9==slop */
|
|
|
|
|
|
|
|
cp = cpbuf;
|
|
|
|
*cp++ = '%';
|
|
|
|
if (lj)
|
|
|
|
*cp++ = '-';
|
1994-02-17 04:21:51 +03:00
|
|
|
if (signchar)
|
|
|
|
*cp++ = signchar;
|
|
|
|
if (alt)
|
|
|
|
*cp++ = '#';
|
1993-07-08 02:18:43 +04:00
|
|
|
if (fill != sp)
|
|
|
|
*cp++ = '0';
|
1994-02-17 04:21:51 +03:00
|
|
|
cp = strcpy(cp, "*.*") + 3;
|
|
|
|
*cp++ = cs1;
|
|
|
|
*cp = '\0';
|
1993-07-08 02:18:43 +04:00
|
|
|
#ifndef GFMT_WORKAROUND
|
1994-02-17 04:21:51 +03:00
|
|
|
(void) sprintf(obufout, cpbuf,
|
|
|
|
(int) fw, (int) prec, (double) tmpval);
|
1993-07-08 02:18:43 +04:00
|
|
|
#else /* GFMT_WORKAROUND */
|
1994-02-17 04:21:51 +03:00
|
|
|
if (cs1 == 'g' || cs1 == 'G')
|
1994-05-20 12:03:48 +04:00
|
|
|
sgfmt(obufout, cpbuf, (int) alt,
|
1994-02-17 04:21:51 +03:00
|
|
|
(int) fw, (int) prec, (double) tmpval);
|
|
|
|
else
|
|
|
|
(void) sprintf(obufout, cpbuf,
|
|
|
|
(int) fw, (int) prec, (double) tmpval);
|
1993-07-08 02:18:43 +04:00
|
|
|
#endif /* GFMT_WORKAROUND */
|
1994-02-17 04:21:51 +03:00
|
|
|
len = strlen(obufout);
|
1993-07-08 02:18:43 +04:00
|
|
|
ofre -= len;
|
1994-02-17 04:21:51 +03:00
|
|
|
obufout += len;
|
1993-07-08 02:18:43 +04:00
|
|
|
s0 = s1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (toofew)
|
|
|
|
fatal("%s\n\t%s\n\t%*s%s",
|
|
|
|
"not enough arguments to satisfy format string",
|
1994-05-20 12:03:48 +04:00
|
|
|
fmt_string, s1 - fmt_string - 2, "",
|
1993-07-08 02:18:43 +04:00
|
|
|
"^ ran out for this one"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (do_lint && carg != NULL)
|
|
|
|
warning("too many arguments supplied for format string");
|
|
|
|
bchunk(s0, s1 - s0);
|
1994-02-17 04:21:51 +03:00
|
|
|
r = make_str_node(obuf, obufout - obuf, ALREADY_MALLOCED);
|
1993-07-08 02:18:43 +04:00
|
|
|
r->flags |= TEMP;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1994-05-20 12:03:48 +04:00
|
|
|
NODE *
|
|
|
|
do_sprintf(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *r;
|
|
|
|
NODE *sfmt = force_string(tree_eval(tree->lnode));
|
|
|
|
|
|
|
|
r = format_tree(sfmt->stptr, sfmt->stlen, tree->rnode);
|
|
|
|
free_temp(sfmt);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
void
|
|
|
|
do_printf(tree)
|
|
|
|
register NODE *tree;
|
|
|
|
{
|
|
|
|
struct redirect *rp = NULL;
|
|
|
|
register FILE *fp;
|
|
|
|
|
|
|
|
if (tree->rnode) {
|
|
|
|
int errflg; /* not used, sigh */
|
|
|
|
|
|
|
|
rp = redirect(tree->rnode, &errflg);
|
|
|
|
if (rp) {
|
|
|
|
fp = rp->fp;
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
fp = stdout;
|
|
|
|
tree = do_sprintf(tree->lnode);
|
|
|
|
efwrite(tree->stptr, sizeof(char), tree->stlen, fp, "printf", rp , 1);
|
|
|
|
free_temp(tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_sqrt(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
double arg;
|
|
|
|
extern double sqrt P((double));
|
|
|
|
|
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
arg = (double) force_number(tmp);
|
|
|
|
free_temp(tmp);
|
|
|
|
if (arg < 0.0)
|
|
|
|
warning("sqrt called with negative argument %g", arg);
|
|
|
|
return tmp_number((AWKNUM) sqrt(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_substr(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *t1, *t2, *t3;
|
|
|
|
NODE *r;
|
|
|
|
register int indx;
|
|
|
|
size_t length;
|
1993-11-13 05:26:02 +03:00
|
|
|
int is_long;
|
1993-07-08 02:18:43 +04:00
|
|
|
|
|
|
|
t1 = tree_eval(tree->lnode);
|
|
|
|
t2 = tree_eval(tree->rnode->lnode);
|
|
|
|
if (tree->rnode->rnode == NULL) /* third arg. missing */
|
|
|
|
length = t1->stlen;
|
|
|
|
else {
|
|
|
|
t3 = tree_eval(tree->rnode->rnode->lnode);
|
|
|
|
length = (size_t) force_number(t3);
|
|
|
|
free_temp(t3);
|
|
|
|
}
|
|
|
|
indx = (int) force_number(t2) - 1;
|
|
|
|
free_temp(t2);
|
|
|
|
t1 = force_string(t1);
|
|
|
|
if (indx < 0)
|
|
|
|
indx = 0;
|
1993-11-13 05:26:02 +03:00
|
|
|
if (indx >= t1->stlen || (long) length <= 0) {
|
1993-07-08 02:18:43 +04:00
|
|
|
free_temp(t1);
|
|
|
|
return Nnull_string;
|
|
|
|
}
|
1993-11-13 05:26:02 +03:00
|
|
|
if ((is_long = (indx + length > t1->stlen)) || LONG_MAX - indx < length) {
|
1993-07-08 02:18:43 +04:00
|
|
|
length = t1->stlen - indx;
|
1993-11-13 05:26:02 +03:00
|
|
|
if (do_lint && is_long)
|
|
|
|
warning("substr: length %d at position %d exceeds length of first argument",
|
|
|
|
length, indx+1);
|
|
|
|
}
|
1993-07-08 02:18:43 +04:00
|
|
|
r = tmp_string(t1->stptr + indx, length);
|
|
|
|
free_temp(t1);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_strftime(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *t1, *t2;
|
|
|
|
struct tm *tm;
|
|
|
|
time_t fclock;
|
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
t1 = force_string(tree_eval(tree->lnode));
|
|
|
|
|
|
|
|
if (tree->rnode == NULL) /* second arg. missing, default */
|
|
|
|
(void) time(&fclock);
|
|
|
|
else {
|
|
|
|
t2 = tree_eval(tree->rnode->lnode);
|
|
|
|
fclock = (time_t) force_number(t2);
|
|
|
|
free_temp(t2);
|
|
|
|
}
|
|
|
|
tm = localtime(&fclock);
|
|
|
|
|
1993-11-13 05:26:02 +03:00
|
|
|
return tmp_string(buf, strftime(buf, 100, t1->stptr, tm));
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_systime(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
time_t lclock;
|
|
|
|
|
|
|
|
(void) time(&lclock);
|
|
|
|
return tmp_number((AWKNUM) lclock);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_system(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
int ret = 0;
|
|
|
|
char *cmd;
|
1993-11-13 05:26:02 +03:00
|
|
|
char save;
|
1993-07-08 02:18:43 +04:00
|
|
|
|
1993-11-13 05:26:02 +03:00
|
|
|
(void) flush_io (); /* so output is synchronous with gawk's */
|
1993-07-08 02:18:43 +04:00
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
cmd = force_string(tmp)->stptr;
|
1993-11-13 05:26:02 +03:00
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
if (cmd && *cmd) {
|
1993-11-13 05:26:02 +03:00
|
|
|
/* insure arg to system is zero-terminated */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* From: David Trueman <emory!cs.dal.ca!david>
|
|
|
|
* To: arnold@cc.gatech.edu (Arnold Robbins)
|
|
|
|
* Date: Wed, 3 Nov 1993 12:49:41 -0400
|
|
|
|
*
|
|
|
|
* It may not be necessary to save the character, but
|
|
|
|
* I'm not sure. It would normally be the field
|
|
|
|
* separator. If the parse has not yet gone beyond
|
|
|
|
* that, it could mess up (although I doubt it). If
|
|
|
|
* FIELDWIDTHS is being used, it might be the first
|
|
|
|
* character of the next field. Unless someone wants
|
|
|
|
* to check it out exhaustively, I suggest saving it
|
|
|
|
* for now...
|
|
|
|
*/
|
|
|
|
save = cmd[tmp->stlen];
|
|
|
|
cmd[tmp->stlen] = '\0';
|
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
ret = system(cmd);
|
|
|
|
ret = (ret >> 8) & 0xff;
|
1993-11-13 05:26:02 +03:00
|
|
|
|
|
|
|
cmd[tmp->stlen] = save;
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
free_temp(tmp);
|
|
|
|
return tmp_number((AWKNUM) ret);
|
|
|
|
}
|
|
|
|
|
1994-05-20 12:03:48 +04:00
|
|
|
extern NODE **fmt_list; /* declared in eval.c */
|
|
|
|
|
1993-07-08 02:18:43 +04:00
|
|
|
void
|
|
|
|
do_print(tree)
|
|
|
|
register NODE *tree;
|
|
|
|
{
|
|
|
|
register NODE *t1;
|
|
|
|
struct redirect *rp = NULL;
|
|
|
|
register FILE *fp;
|
|
|
|
register char *s;
|
|
|
|
|
|
|
|
if (tree->rnode) {
|
|
|
|
int errflg; /* not used, sigh */
|
|
|
|
|
|
|
|
rp = redirect(tree->rnode, &errflg);
|
|
|
|
if (rp) {
|
|
|
|
fp = rp->fp;
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
fp = stdout;
|
|
|
|
tree = tree->lnode;
|
|
|
|
while (tree) {
|
|
|
|
t1 = tree_eval(tree->lnode);
|
|
|
|
if (t1->flags & NUMBER) {
|
|
|
|
if (OFMTidx == CONVFMTidx)
|
|
|
|
(void) force_string(t1);
|
|
|
|
else {
|
1994-05-20 12:03:48 +04:00
|
|
|
#ifndef GFMT_WORKAROUND
|
1993-07-08 02:18:43 +04:00
|
|
|
char buf[100];
|
|
|
|
|
1994-05-20 12:03:48 +04:00
|
|
|
(void) sprintf(buf, OFMT, t1->numbr);
|
1994-02-17 04:21:51 +03:00
|
|
|
free_temp(t1);
|
1993-07-08 02:18:43 +04:00
|
|
|
t1 = tmp_string(buf, strlen(buf));
|
1994-05-20 12:03:48 +04:00
|
|
|
#else /* GFMT_WORKAROUND */
|
|
|
|
free_temp(t1);
|
|
|
|
t1 = format_tree(OFMT,
|
|
|
|
fmt_list[OFMTidx]->stlen,
|
|
|
|
tree);
|
|
|
|
#endif /* GFMT_WORKAROUND */
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
efwrite(t1->stptr, sizeof(char), t1->stlen, fp, "print", rp, 0);
|
|
|
|
free_temp(t1);
|
|
|
|
tree = tree->rnode;
|
|
|
|
if (tree) {
|
|
|
|
s = OFS;
|
|
|
|
if (OFSlen)
|
1994-05-20 12:03:48 +04:00
|
|
|
efwrite(s, sizeof(char), (size_t)OFSlen,
|
|
|
|
fp, "print", rp, 0);
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s = ORS;
|
|
|
|
if (ORSlen)
|
1993-11-13 05:26:02 +03:00
|
|
|
efwrite(s, sizeof(char), (size_t)ORSlen, fp, "print", rp, 1);
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_tolower(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *t1, *t2;
|
|
|
|
register char *cp, *cp2;
|
|
|
|
|
|
|
|
t1 = tree_eval(tree->lnode);
|
|
|
|
t1 = force_string(t1);
|
|
|
|
t2 = tmp_string(t1->stptr, t1->stlen);
|
|
|
|
for (cp = t2->stptr, cp2 = t2->stptr + t2->stlen; cp < cp2; cp++)
|
|
|
|
if (isupper(*cp))
|
|
|
|
*cp = tolower(*cp);
|
|
|
|
free_temp(t1);
|
|
|
|
return t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_toupper(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *t1, *t2;
|
|
|
|
register char *cp;
|
|
|
|
|
|
|
|
t1 = tree_eval(tree->lnode);
|
|
|
|
t1 = force_string(t1);
|
|
|
|
t2 = tmp_string(t1->stptr, t1->stlen);
|
|
|
|
for (cp = t2->stptr; cp < t2->stptr + t2->stlen; cp++)
|
|
|
|
if (islower(*cp))
|
|
|
|
*cp = toupper(*cp);
|
|
|
|
free_temp(t1);
|
|
|
|
return t2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_atan2(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *t1, *t2;
|
|
|
|
extern double atan2 P((double, double));
|
|
|
|
double d1, d2;
|
|
|
|
|
|
|
|
t1 = tree_eval(tree->lnode);
|
|
|
|
t2 = tree_eval(tree->rnode->lnode);
|
|
|
|
d1 = force_number(t1);
|
|
|
|
d2 = force_number(t2);
|
|
|
|
free_temp(t1);
|
|
|
|
free_temp(t2);
|
|
|
|
return tmp_number((AWKNUM) atan2(d1, d2));
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_sin(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
extern double sin P((double));
|
|
|
|
double d;
|
|
|
|
|
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
d = sin((double)force_number(tmp));
|
|
|
|
free_temp(tmp);
|
|
|
|
return tmp_number((AWKNUM) d);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_cos(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
extern double cos P((double));
|
|
|
|
double d;
|
|
|
|
|
|
|
|
tmp = tree_eval(tree->lnode);
|
|
|
|
d = cos((double)force_number(tmp));
|
|
|
|
free_temp(tmp);
|
|
|
|
return tmp_number((AWKNUM) d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int firstrand = 1;
|
1994-05-20 12:03:48 +04:00
|
|
|
static char state[512];
|
1993-07-08 02:18:43 +04:00
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
NODE *
|
|
|
|
do_rand(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
if (firstrand) {
|
|
|
|
(void) initstate((unsigned) 1, state, sizeof state);
|
|
|
|
srandom(1);
|
|
|
|
firstrand = 0;
|
|
|
|
}
|
1994-05-20 12:03:48 +04:00
|
|
|
return tmp_number((AWKNUM) random() / GAWK_RANDOM_MAX);
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_srand(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *tmp;
|
|
|
|
static long save_seed = 0;
|
|
|
|
long ret = save_seed; /* SVR4 awk srand returns previous seed */
|
|
|
|
|
|
|
|
if (firstrand)
|
|
|
|
(void) initstate((unsigned) 1, state, sizeof state);
|
|
|
|
else
|
|
|
|
(void) setstate(state);
|
|
|
|
|
|
|
|
if (!tree)
|
1994-05-20 12:03:48 +04:00
|
|
|
srandom((unsigned int) (save_seed = (long) time((time_t *) 0)));
|
1993-07-08 02:18:43 +04:00
|
|
|
else {
|
|
|
|
tmp = tree_eval(tree->lnode);
|
1994-05-20 12:03:48 +04:00
|
|
|
srandom((unsigned int) (save_seed = (long) force_number(tmp)));
|
1993-07-08 02:18:43 +04:00
|
|
|
free_temp(tmp);
|
|
|
|
}
|
|
|
|
firstrand = 0;
|
|
|
|
return tmp_number((AWKNUM) ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_match(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
NODE *t1;
|
|
|
|
int rstart;
|
|
|
|
AWKNUM rlength;
|
|
|
|
Regexp *rp;
|
|
|
|
|
|
|
|
t1 = force_string(tree_eval(tree->lnode));
|
|
|
|
tree = tree->rnode->lnode;
|
|
|
|
rp = re_update(tree);
|
|
|
|
rstart = research(rp, t1->stptr, 0, t1->stlen, 1);
|
|
|
|
if (rstart >= 0) { /* match succeded */
|
|
|
|
rstart++; /* 1-based indexing */
|
|
|
|
rlength = REEND(rp, t1->stptr) - RESTART(rp, t1->stptr);
|
|
|
|
} else { /* match failed */
|
|
|
|
rstart = 0;
|
|
|
|
rlength = -1.0;
|
|
|
|
}
|
|
|
|
free_temp(t1);
|
|
|
|
unref(RSTART_node->var_value);
|
|
|
|
RSTART_node->var_value = make_number((AWKNUM) rstart);
|
|
|
|
unref(RLENGTH_node->var_value);
|
|
|
|
RLENGTH_node->var_value = make_number(rlength);
|
|
|
|
return tmp_number((AWKNUM) rstart);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NODE *
|
|
|
|
sub_common(tree, global)
|
|
|
|
NODE *tree;
|
|
|
|
int global;
|
|
|
|
{
|
|
|
|
register char *scan;
|
|
|
|
register char *bp, *cp;
|
|
|
|
char *buf;
|
1993-11-13 05:26:02 +03:00
|
|
|
size_t buflen;
|
1993-07-08 02:18:43 +04:00
|
|
|
register char *matchend;
|
1993-11-13 05:26:02 +03:00
|
|
|
register size_t len;
|
1993-07-08 02:18:43 +04:00
|
|
|
char *matchstart;
|
|
|
|
char *text;
|
1993-11-13 05:26:02 +03:00
|
|
|
size_t textlen;
|
1993-07-08 02:18:43 +04:00
|
|
|
char *repl;
|
|
|
|
char *replend;
|
1993-11-13 05:26:02 +03:00
|
|
|
size_t repllen;
|
1993-07-08 02:18:43 +04:00
|
|
|
int sofar;
|
|
|
|
int ampersands;
|
|
|
|
int matches = 0;
|
|
|
|
Regexp *rp;
|
|
|
|
NODE *s; /* subst. pattern */
|
|
|
|
NODE *t; /* string to make sub. in; $0 if none given */
|
|
|
|
NODE *tmp;
|
|
|
|
NODE **lhs = &tree; /* value not used -- just different from NULL */
|
|
|
|
int priv = 0;
|
|
|
|
Func_ptr after_assign = NULL;
|
|
|
|
|
|
|
|
tmp = tree->lnode;
|
|
|
|
rp = re_update(tmp);
|
|
|
|
|
|
|
|
tree = tree->rnode;
|
|
|
|
s = tree->lnode;
|
|
|
|
|
|
|
|
tree = tree->rnode;
|
|
|
|
tmp = tree->lnode;
|
|
|
|
t = force_string(tree_eval(tmp));
|
|
|
|
|
|
|
|
/* do the search early to avoid work on non-match */
|
|
|
|
if (research(rp, t->stptr, 0, t->stlen, 1) == -1 ||
|
1994-05-20 12:03:48 +04:00
|
|
|
RESTART(rp, t->stptr) > t->stlen) {
|
1993-07-08 02:18:43 +04:00
|
|
|
free_temp(t);
|
1994-05-20 12:03:48 +04:00
|
|
|
return tmp_number((AWKNUM) 0.0);
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->type == Node_val)
|
|
|
|
lhs = NULL;
|
|
|
|
else
|
|
|
|
lhs = get_lhs(tmp, &after_assign);
|
|
|
|
t->flags |= STRING;
|
|
|
|
/*
|
|
|
|
* create a private copy of the string
|
|
|
|
*/
|
|
|
|
if (t->stref > 1 || (t->flags & PERM)) {
|
|
|
|
unsigned int saveflags;
|
|
|
|
|
|
|
|
saveflags = t->flags;
|
|
|
|
t->flags &= ~MALLOC;
|
|
|
|
tmp = dupnode(t);
|
|
|
|
t->flags = saveflags;
|
|
|
|
t = tmp;
|
|
|
|
priv = 1;
|
|
|
|
}
|
|
|
|
text = t->stptr;
|
|
|
|
textlen = t->stlen;
|
|
|
|
buflen = textlen + 2;
|
|
|
|
|
|
|
|
s = force_string(tree_eval(s));
|
|
|
|
repl = s->stptr;
|
|
|
|
replend = repl + s->stlen;
|
|
|
|
repllen = replend - repl;
|
1994-05-20 12:03:48 +04:00
|
|
|
emalloc(buf, char *, buflen + 2, "do_sub");
|
|
|
|
buf[buflen] = '\0';
|
|
|
|
buf[buflen + 1] = '\0';
|
1993-07-08 02:18:43 +04:00
|
|
|
ampersands = 0;
|
|
|
|
for (scan = repl; scan < replend; scan++) {
|
|
|
|
if (*scan == '&') {
|
|
|
|
repllen--;
|
|
|
|
ampersands++;
|
1995-04-05 04:11:36 +04:00
|
|
|
} else if (*scan == '\\'
|
|
|
|
&& (*(scan+1) == '&' || *(scan+1) == '\\')) {
|
1993-07-08 02:18:43 +04:00
|
|
|
repllen--;
|
|
|
|
scan++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bp = buf;
|
|
|
|
for (;;) {
|
|
|
|
matches++;
|
|
|
|
matchstart = t->stptr + RESTART(rp, t->stptr);
|
|
|
|
matchend = t->stptr + REEND(rp, t->stptr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create the result, copying in parts of the original
|
|
|
|
* string
|
|
|
|
*/
|
|
|
|
len = matchstart - text + repllen
|
|
|
|
+ ampersands * (matchend - matchstart);
|
|
|
|
sofar = bp - buf;
|
1995-04-05 04:11:36 +04:00
|
|
|
while (buflen < (sofar + len + 1)) {
|
1993-07-08 02:18:43 +04:00
|
|
|
buflen *= 2;
|
|
|
|
erealloc(buf, char *, buflen, "do_sub");
|
|
|
|
bp = buf + sofar;
|
|
|
|
}
|
|
|
|
for (scan = text; scan < matchstart; scan++)
|
|
|
|
*bp++ = *scan;
|
|
|
|
for (scan = repl; scan < replend; scan++)
|
|
|
|
if (*scan == '&')
|
|
|
|
for (cp = matchstart; cp < matchend; cp++)
|
|
|
|
*bp++ = *cp;
|
1995-04-05 04:11:36 +04:00
|
|
|
else if (*scan == '\\'
|
|
|
|
&& (*(scan+1) == '&' || *(scan+1) == '\\')) {
|
1993-07-08 02:18:43 +04:00
|
|
|
scan++;
|
|
|
|
*bp++ = *scan;
|
|
|
|
} else
|
|
|
|
*bp++ = *scan;
|
1994-05-20 12:03:48 +04:00
|
|
|
|
|
|
|
/* catch the case of gsub(//, "blah", whatever), i.e. empty regexp */
|
1993-07-08 02:18:43 +04:00
|
|
|
if (global && matchstart == matchend && matchend < text + textlen) {
|
|
|
|
*bp++ = *matchend;
|
|
|
|
matchend++;
|
|
|
|
}
|
|
|
|
textlen = text + textlen - matchend;
|
|
|
|
text = matchend;
|
1993-11-13 05:26:02 +03:00
|
|
|
if (!global || (long)textlen <= 0 ||
|
1993-07-08 02:18:43 +04:00
|
|
|
research(rp, t->stptr, text-t->stptr, textlen, 1) == -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sofar = bp - buf;
|
|
|
|
if (buflen - sofar - textlen - 1) {
|
|
|
|
buflen = sofar + textlen + 2;
|
|
|
|
erealloc(buf, char *, buflen, "do_sub");
|
|
|
|
bp = buf + sofar;
|
|
|
|
}
|
|
|
|
for (scan = matchend; scan < text + textlen; scan++)
|
|
|
|
*bp++ = *scan;
|
1994-05-20 12:03:48 +04:00
|
|
|
*bp = '\0';
|
1993-07-08 02:18:43 +04:00
|
|
|
textlen = bp - buf;
|
|
|
|
free(t->stptr);
|
|
|
|
t->stptr = buf;
|
|
|
|
t->stlen = textlen;
|
|
|
|
|
|
|
|
free_temp(s);
|
|
|
|
if (matches > 0 && lhs) {
|
|
|
|
if (priv) {
|
|
|
|
unref(*lhs);
|
|
|
|
*lhs = t;
|
|
|
|
}
|
|
|
|
if (after_assign)
|
|
|
|
(*after_assign)();
|
|
|
|
t->flags &= ~(NUM|NUMBER);
|
|
|
|
}
|
|
|
|
return tmp_number((AWKNUM) matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_gsub(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
return sub_common(tree, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE *
|
|
|
|
do_sub(tree)
|
|
|
|
NODE *tree;
|
|
|
|
{
|
|
|
|
return sub_common(tree, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef GFMT_WORKAROUND
|
1994-02-17 04:21:51 +03:00
|
|
|
/*
|
|
|
|
* printf's %g format [can't rely on gcvt()]
|
|
|
|
* caveat: don't use as argument to *printf()!
|
|
|
|
* 'format' string HAS to be of "<flags>*.*g" kind, or we bomb!
|
|
|
|
*/
|
1994-05-20 12:03:48 +04:00
|
|
|
static void
|
1994-02-17 04:21:51 +03:00
|
|
|
sgfmt(buf, format, alt, fwidth, prec, g)
|
1993-07-08 02:18:43 +04:00
|
|
|
char *buf; /* return buffer; assumed big enough to hold result */
|
1994-02-17 04:21:51 +03:00
|
|
|
const char *format;
|
|
|
|
int alt; /* use alternate form flag */
|
|
|
|
int fwidth; /* field width in a format */
|
|
|
|
int prec; /* indicates desired significant digits, not decimal places */
|
|
|
|
double g; /* value to format */
|
1993-07-08 02:18:43 +04:00
|
|
|
{
|
1994-02-17 04:21:51 +03:00
|
|
|
char dform[40];
|
|
|
|
register char *gpos;
|
|
|
|
register char *d, *e, *p;
|
|
|
|
int again = 0;
|
|
|
|
|
|
|
|
strncpy(dform, format, sizeof dform - 1);
|
|
|
|
dform[sizeof dform - 1] = '\0';
|
|
|
|
gpos = strrchr(dform, '.');
|
|
|
|
|
|
|
|
if (g == 0.0 && alt == 0) { /* easy special case */
|
|
|
|
*gpos++ = 'd';
|
|
|
|
*gpos = '\0';
|
|
|
|
(void) sprintf(buf, dform, fwidth, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gpos += 2; /* advance to location of 'g' in the format */
|
|
|
|
|
|
|
|
if (prec <= 0) /* negative precision is ignored */
|
|
|
|
prec = (prec < 0 ? DEFAULT_G_PRECISION : 1);
|
|
|
|
|
|
|
|
if (*gpos == 'G')
|
|
|
|
again = 1;
|
|
|
|
/* start with 'e' format (it'll provide nice exponent) */
|
|
|
|
*gpos = 'e';
|
|
|
|
prec -= 1;
|
|
|
|
(void) sprintf(buf, dform, fwidth, prec, g);
|
|
|
|
if ((e = strrchr(buf, 'e')) != NULL) { /* find exponent */
|
|
|
|
int exp = atoi(e+1); /* fetch exponent */
|
|
|
|
if (exp >= -4 && exp <= prec) { /* per K&R2, B1.2 */
|
|
|
|
/* switch to 'f' format and re-do */
|
|
|
|
*gpos = 'f';
|
|
|
|
prec -= exp; /* decimal precision */
|
|
|
|
(void) sprintf(buf, dform, fwidth, prec, g);
|
|
|
|
e = buf + strlen(buf);
|
|
|
|
while (*--e == ' ')
|
|
|
|
continue;
|
|
|
|
e += 1;
|
|
|
|
}
|
|
|
|
else if (again != 0)
|
|
|
|
*gpos = 'E';
|
|
|
|
|
|
|
|
/* if 'alt' in force, then trailing zeros are not removed */
|
|
|
|
if (alt == 0 && (d = strrchr(buf, '.')) != NULL) {
|
|
|
|
/* throw away an excess of precision */
|
|
|
|
for (p = e; p > d && *--p == '0'; )
|
|
|
|
prec -= 1;
|
|
|
|
if (d == p)
|
|
|
|
prec -= 1;
|
|
|
|
if (prec < 0)
|
|
|
|
prec = 0;
|
|
|
|
/* and do that once again */
|
|
|
|
again = 1;
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
1994-02-17 04:21:51 +03:00
|
|
|
if (again != 0)
|
|
|
|
(void) sprintf(buf, dform, fwidth, prec, g);
|
1993-07-08 02:18:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* GFMT_WORKAROUND */
|