2 * builtin.c - Builtin functions and various utility procedures
6 * Copyright (C) 1986, 1988, 1989, 1991-2000 the Free Software Foundation, Inc.
8 * This file is part of GAWK, the GNU implementation of the
9 * AWK Programming Language.
11 * GAWK is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GAWK is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 * $FreeBSD: src/contrib/awk/builtin.c,v 1.7.2.1 2001/01/23 22:08:30 asmodai Exp $
37 /* can declare these, since we always use the random shipped with gawk */
38 extern char *initstate P((unsigned seed, char *state, int n));
39 extern char *setstate P((char *state));
40 extern long random P((void));
41 extern void srandom P((unsigned int seed));
44 extern NODE **fields_arr;
45 extern int output_is_tty;
47 static NODE *sub_common P((NODE *tree, int how_many, int backdigs));
48 NODE *format_tree P((const char *, int, NODE *));
51 /* Work around a problem in conversion of doubles to exact integers. */
53 #define Floor(n) floor((n) * (1.0 + DBL_EPSILON))
54 #define Ceil(n) ceil((n) * (1.0 + DBL_EPSILON))
56 /* Force the standard C compiler to use the library math functions. */
57 extern double exp(double);
58 double (*Exp)() = exp;
59 #define exp(x) (*Exp)(x)
60 extern double log(double);
61 double (*Log)() = log;
62 #define log(x) (*Log)(x)
64 #define Floor(n) floor(n)
65 #define Ceil(n) ceil(n)
68 #define DEFAULT_G_PRECISION 6
70 #ifdef GFMT_WORKAROUND
71 /* semi-temporary hack, mostly to gracefully handle VMS */
72 static void sgfmt P((char *buf, const char *format, int alt,
73 int fwidth, int precision, double value));
74 #endif /* GFMT_WORKAROUND */
77 * Since we supply the version of random(), we know what
80 #define GAWK_RANDOM_MAX 0x7fffffffL
82 static void efwrite P((const void *ptr, size_t size, size_t count, FILE *fp,
83 const char *from, struct redirect *rp, int flush));
85 /* efwrite --- like fwrite, but with error checking */
88 efwrite(ptr, size, count, fp, from, rp, flush)
97 if (fwrite(ptr, size, count, fp) != count)
100 && ((fp == stdout && output_is_tty)
101 || (rp && (rp->flag & RED_NOBUF)))) {
109 fatal("%s to \"%s\" failed (%s)", from,
110 rp ? rp->value : "standard output",
111 errno ? strerror(errno) : "reason unknown");
114 /* do_exp --- exponential function */
123 tmp = tree_eval(tree->lnode);
124 d = force_number(tmp);
129 warning("exp argument %g is out of range", d);
130 return tmp_number((AWKNUM) res);
133 /* stdfile --- return fp for a standard file */
136 * This function allows `fflush("/dev/stdout")' to work.
137 * The other files will be available via getredirect().
138 * /dev/stdin is not included, since fflush is only for output.
147 if (STREQN(name, "/dev/stderr", 11))
149 else if (STREQN(name, "/dev/stdout", 11))
156 /* do_fflush --- flush output, either named file or pipe or everything */
168 /* fflush() --- flush stdout */
170 status = fflush(stdout);
171 return tmp_number((AWKNUM) status);
174 tmp = tree_eval(tree->lnode);
175 tmp = force_string(tmp);
178 /* fflush("") --- flush all */
179 if (tmp->stlen == 0) {
182 return tmp_number((AWKNUM) status);
185 rp = getredirect(tmp->stptr, tmp->stlen);
188 if ((rp->flag & (RED_WRITE|RED_APPEND)) == 0) {
191 "fflush: cannot flush: %s `%s' opened for reading, not writing",
192 (rp->flag & RED_PIPE) ? "pipe" : "file",
195 return tmp_number((AWKNUM) status);
200 } else if ((fp = stdfile(tmp->stptr, tmp->stlen)) != NULL) {
203 warning("fflush: `%s' is not an open file or pipe", file);
205 return tmp_number((AWKNUM) status);
208 /* do_index --- find index of a string */
215 register char *p1, *p2;
216 register size_t l1, l2;
220 s1 = tree_eval(tree->lnode);
221 s2 = tree_eval(tree->rnode->lnode);
230 /* IGNORECASE will already be false if posix */
235 if (casetable[(int)*p1] == casetable[(int)*p2]
236 && (l2 == 1 || strncasecmp(p1, p2, l2) == 0)) {
237 ret = 1 + s1->stlen - l1;
248 && (l2 == 1 || STREQN(p1, p2, l2))) {
249 ret = 1 + s1->stlen - l1;
258 return tmp_number((AWKNUM) ret);
261 /* double_to_int --- convert double to int, used several places */
274 /* do_int --- convert double to int for awk */
283 tmp = tree_eval(tree->lnode);
284 d = force_number(tmp);
285 d = double_to_int(d);
287 return tmp_number((AWKNUM) d);
290 /* do_length --- length of a string or $0 */
299 tmp = tree_eval(tree->lnode);
300 len = force_string(tmp)->stlen;
302 return tmp_number((AWKNUM) len);
305 /* do_log --- the log function */
314 tmp = tree_eval(tree->lnode);
315 arg = (double) force_number(tmp);
317 warning("log called with negative argument %g", arg);
320 return tmp_number((AWKNUM) d);
324 * format_tree() formats nodes of a tree, starting with a left node,
325 * and accordingly to a fmt_string providing a format like in
326 * printf family from C library. Returns a string node which value
327 * is a formatted string. Called by sprintf function.
329 * It is one of the uglier parts of gawk. Thanks to Michal Jaegermann
330 * for taming this beast and making it compatible with ANSI C.
334 format_tree(fmt_string, n0, carg)
335 const char *fmt_string;
339 /* copy 'l' bytes from 's' to 'obufout' checking for space in the process */
340 /* difference of pointers should be of ptrdiff_t type, but let us be kind */
341 #define bchunk(s, l) if (l) { \
342 while ((l) > ofre) { \
343 long olen = obufout - obuf; \
344 erealloc(obuf, char *, osiz * 2, "format_tree"); \
347 obufout = obuf + olen; \
349 memcpy(obufout, s, (size_t) (l)); \
354 /* copy one byte from 's' to 'obufout' checking for space in the process */
355 #define bchunk_one(s) { \
357 long olen = obufout - obuf; \
358 erealloc(obuf, char *, osiz * 2, "format_tree"); \
361 obufout = obuf + olen; \
367 /* Is there space for something L big in the buffer? */
368 #define chksize(l) if ((l) > ofre) { \
369 long olen = obufout - obuf; \
370 erealloc(obuf, char *, osiz * 2, "format_tree"); \
371 obufout = obuf + olen; \
377 * Get the next arg to be formatted. If we've run out of args,
378 * return "" (Null string)
380 #define parse_next_arg() { \
381 if (carg == NULL) { \
385 arg = tree_eval(carg->lnode); \
386 carg = carg->rnode; \
392 char *obuf, *obufout;
399 int lj, alt, big, bigbig, small, have_prec, need_format;
401 #ifdef sun386 /* Can't cast unsigned (int/long) from ptr->value */
402 long tmp_uval; /* on 386i 4.0.1 C compiler -- it just hangs */
407 char cpbuf[30]; /* if we have numbers bigger than 30 */
408 char *cend = &cpbuf[30];/* chars, we lose, but seems unlikely */
412 char signchar = FALSE;
414 int zero_flag = FALSE;
415 static char sp[] = " ";
416 static char zero_string[] = "0";
417 static char lchbuf[] = "0123456789abcdef";
418 static char Uchbuf[] = "0123456789ABCDEF";
420 #define INITIAL_OUT_SIZE 512
421 emalloc(obuf, char *, INITIAL_OUT_SIZE, "format_tree");
423 osiz = INITIAL_OUT_SIZE;
428 s0 = s1 = fmt_string;
443 lj = alt = big = bigbig = small = FALSE;
450 if (n0-- <= 0) /* ran out early! */
453 switch (cs1 = *s1++) {
454 case (-1): /* dummy case to allow for checking */
457 break; /* reject as a valid format */
467 * Only turn on zero_flag if we haven't seen
468 * the field width or precision yet. Otherwise,
469 * screws up floating point formatting.
490 * with a negative precision *cur is already set
491 * to -1, so it will remain negative, but we have
492 * to "eat" precision digits in any case
494 while (n0 > 0 && *s1 >= '0' && *s1 <= '9') {
496 *cur = *cur * 10 + *s1++ - '0';
498 if (prec < 0) /* negative precision is discarded */
502 if (n0 == 0) /* badly formatted control string */
509 *cur = force_number(arg);
511 if (*cur < 0 && cur == &fw) {
523 case ' ': /* print ' ' or '-' */
524 /* 'space' flag is ignored */
525 /* if '+' already present */
526 if (signchar != FALSE)
529 case '+': /* print '+' or '-' */
539 fill = sp; /* if left justified then other */
540 lj++; /* filling is ignored */
555 static int warned = FALSE;
557 if (do_lint && ! warned) {
558 warning("`l' is meaningless in awk formats; ignored");
562 fatal("'l' is not permitted in POSIX awk formats");
570 static int warned = FALSE;
572 if (do_lint && ! warned) {
573 warning("`L' is meaningless in awk formats; ignored");
577 fatal("'L' is not permitted in POSIX awk formats");
585 static int warned = FALSE;
587 if (do_lint && ! warned) {
588 warning("`h' is meaningless in awk formats; ignored");
592 fatal("'h' is not permitted in POSIX awk formats");
598 if (zero_flag && ! lj)
601 /* user input that looks numeric is numeric */
602 if ((arg->flags & (MAYBE_NUM|NUMBER)) == MAYBE_NUM)
603 (void) force_number(arg);
604 if (arg->flags & NUMBER) {
606 tmp_uval = arg->numbr;
607 uval = (unsigned long) tmp_uval;
609 uval = (unsigned long) arg->numbr;
616 if (have_prec == FALSE)
618 else if (prec > arg->stlen)
624 if (zero_flag && ! lj)
627 arg = force_string(arg);
628 if (! have_prec || prec > arg->stlen)
636 tmpval = force_number(arg);
639 * ``The result of converting a zero value with a
640 * precision of zero is no characters.''
642 if (have_prec && prec == 0 && tmpval == 0)
646 if (tmpval < LONG_MIN)
649 uval = - (unsigned long) (long) tmpval;
651 /* Use !, so that NaNs are out of range.
652 The cast avoids a SunOS 4.1.x cc bug. */
653 if (! (tmpval <= (unsigned long) ULONG_MAX))
656 uval = (unsigned long) tmpval;
659 *--cp = (char) ('0' + uval % 10);
663 /* add more output digits to match the precision */
665 while (cend - cp < prec)
674 * When to fill with zeroes is of course not simple.
675 * First: No zero fill if left-justifying.
676 * Next: There seem to be two cases:
677 * A '0' without a precision, e.g. %06d
678 * A precision with no field width, e.g. %.10d
679 * Any other case, we don't want to fill with zeroes.
682 && ((zero_flag && ! have_prec)
683 || (fw == 0 && have_prec)))
688 if (fw > prec && ! lj && fill != sp
689 && (*cp == '-' || signchar)) {
697 chbuf = Uchbuf; /* FALL THROUGH */
699 base += 6; /* FALL THROUGH */
701 base += 2; /* FALL THROUGH */
706 tmpval = force_number(arg);
709 * ``The result of converting a zero value with a
710 * precision of zero is no characters.''
712 * If I remember the ANSI C standard, though,
713 * it says that for octal conversions
714 * the precision is artificially increased
715 * to add an extra 0 if # is supplied.
717 * printf("%#.0o\n", 0);
720 if (! alt && have_prec && prec == 0 && tmpval == 0)
724 if (tmpval < LONG_MIN)
726 uval = (unsigned long) (long) tmpval;
728 /* Use !, so that NaNs are out of range.
729 The cast avoids a SunOS 4.1.x cc bug. */
730 if (! (tmpval <= (unsigned long) ULONG_MAX))
732 uval = (unsigned long) tmpval;
735 * When to fill with zeroes is of course not simple.
736 * First: No zero fill if left-justifying.
737 * Next: There seem to be two cases:
738 * A '0' without a precision, e.g. %06d
739 * A precision with no field width, e.g. %.10d
740 * Any other case, we don't want to fill with zeroes.
743 && ((zero_flag && ! have_prec)
744 || (fw == 0 && have_prec)))
747 *--cp = chbuf[uval % base];
751 /* add more output digits to match the precision */
753 while (cend - cp < prec)
757 if (alt && tmpval != 0) {
766 } else if (base == 8)
780 bchunk(cp, (int) prec);
790 /* out of range - emergency use of %g format */
801 tmpval = force_number(arg);
805 prec = DEFAULT_G_PRECISION;
806 chksize(fw + prec + 9); /* 9 == slop */
822 #ifndef GFMT_WORKAROUND
823 (void) sprintf(obufout, cpbuf,
824 (int) fw, (int) prec, (double) tmpval);
825 #else /* GFMT_WORKAROUND */
826 if (cs1 == 'g' || cs1 == 'G')
827 sgfmt(obufout, cpbuf, (int) alt,
828 (int) fw, (int) prec, (double) tmpval);
830 (void) sprintf(obufout, cpbuf,
831 (int) fw, (int) prec, (double) tmpval);
832 #endif /* GFMT_WORKAROUND */
833 len = strlen(obufout);
842 fatal("%s\n\t`%s'\n\t%*s%s",
843 "not enough arguments to satisfy format string",
844 fmt_string, s1 - fmt_string - 2, "",
845 "^ ran out for this one"
851 "printf format specifier does not have control letter");
854 "too many arguments supplied for format string");
857 r = make_str_node(obuf, obufout - obuf, ALREADY_MALLOCED);
862 /* do_sprintf --- perform sprintf */
869 NODE *sfmt = force_string(tree_eval(tree->lnode));
871 r = format_tree(sfmt->stptr, sfmt->stlen, tree->rnode);
876 /* do_printf --- perform printf, including redirection */
882 struct redirect *rp = NULL;
885 if (tree->lnode == NULL) {
886 if (do_traditional) {
888 warning("printf: no arguments");
889 return; /* bwk accepts it silently */
891 fatal("printf: no arguments");
894 if (tree->rnode != NULL) {
895 int errflg; /* not used, sigh */
897 rp = redirect(tree->rnode, &errflg);
906 tree = do_sprintf(tree->lnode);
907 efwrite(tree->stptr, sizeof(char), tree->stlen, fp, "printf", rp, TRUE);
911 /* do_sqrt --- do the sqrt function */
920 tmp = tree_eval(tree->lnode);
921 arg = (double) force_number(tmp);
924 warning("sqrt called with negative argument %g", arg);
925 return tmp_number((AWKNUM) sqrt(arg));
928 /* do_substr --- do the substr function */
936 register size_t indx;
938 double d_index, d_length;
940 t1 = force_string(tree_eval(tree->lnode));
941 t2 = tree_eval(tree->rnode->lnode);
942 d_index = force_number(t2);
947 warning("substr: start index %g invalid, using 1",
951 if (do_lint && double_to_int(d_index) != d_index)
952 warning("substr: non-integer start index %g will be truncated",
955 indx = d_index - 1; /* awk indices are from 1, C's are from 0 */
957 if (tree->rnode->rnode == NULL) { /* third arg. missing */
958 /* use remainder of string */
959 length = t1->stlen - indx;
961 t3 = tree_eval(tree->rnode->rnode->lnode);
962 d_length = force_number(t3);
964 if (d_length <= 0.0) {
966 warning("substr: length %g is <= 0", d_length);
970 if (do_lint && double_to_int(d_length) != d_length)
972 "substr: non-integer length %g will be truncated",
977 if (t1->stlen == 0) {
979 warning("substr: source string is zero length");
983 if ((indx + length) > t1->stlen) {
986 "substr: length %d at position %d exceeds length of first argument (%d)",
987 length, indx+1, t1->stlen);
988 length = t1->stlen - indx;
990 if (indx >= t1->stlen) {
992 warning("substr: start index %d is past end of string",
997 r = tmp_string(t1->stptr + indx, length);
1002 /* do_strftime --- format a time stamp */
1008 NODE *t1, *t2, *ret;
1012 size_t buflen, bufsize;
1014 static char def_format[] = "%a %b %d %H:%M:%S %Z %Y";
1018 /* set defaults first */
1019 format = def_format; /* traditional date format */
1020 formatlen = strlen(format);
1021 (void) time(&fclock); /* current time of day */
1024 if (tree != NULL) { /* have args */
1025 if (tree->lnode != NULL) {
1026 t1 = force_string(tree_eval(tree->lnode));
1028 formatlen = t1->stlen;
1029 if (formatlen == 0) {
1031 warning("strftime called with empty format string");
1033 return tmp_string("", 0);
1037 if (tree->rnode != NULL) {
1038 t2 = tree_eval(tree->rnode->lnode);
1039 fclock = (time_t) force_number(t2);
1044 tm = localtime(&fclock);
1047 bufsize = sizeof(buf);
1050 buflen = strftime(bufp, bufsize, format, tm);
1052 * buflen can be zero EITHER because there's not enough
1053 * room in the string, or because the control command
1054 * goes to the empty string. Make a reasonable guess that
1055 * if the buffer is 1024 times bigger than the length of the
1056 * format string, it's not failing for lack of room.
1057 * Thanks to Paul Eggert for pointing out this issue.
1059 if (buflen > 0 || bufsize >= 1024 * formatlen)
1063 emalloc(bufp, char *, bufsize, "do_strftime");
1065 erealloc(bufp, char *, bufsize, "do_strftime");
1067 ret = tmp_string(bufp, buflen);
1075 /* do_systime --- get the time of day */
1083 (void) time(&lclock);
1084 return tmp_number((AWKNUM) lclock);
1089 /* do_system --- run an external command */
1100 (void) flush_io(); /* so output is synchronous with gawk's */
1101 tmp = tree_eval(tree->lnode);
1102 cmd = force_string(tmp)->stptr;
1105 /* insure arg to system is zero-terminated */
1108 * From: David Trueman <david@cs.dal.ca>
1109 * To: arnold@cc.gatech.edu (Arnold Robbins)
1110 * Date: Wed, 3 Nov 1993 12:49:41 -0400
1112 * It may not be necessary to save the character, but
1113 * I'm not sure. It would normally be the field
1114 * separator. If the parse has not yet gone beyond
1115 * that, it could mess up (although I doubt it). If
1116 * FIELDWIDTHS is being used, it might be the first
1117 * character of the next field. Unless someone wants
1118 * to check it out exhaustively, I suggest saving it
1121 save = cmd[tmp->stlen];
1122 cmd[tmp->stlen] = '\0';
1125 ret = (ret >> 8) & 0xff;
1127 cmd[tmp->stlen] = save;
1130 return tmp_number((AWKNUM) ret);
1133 extern NODE **fmt_list; /* declared in eval.c */
1135 /* do_print --- print items, separated by OFS, terminated with ORS */
1139 register NODE *tree;
1142 struct redirect *rp = NULL;
1149 int errflg; /* not used, sigh */
1151 rp = redirect(tree->rnode, &errflg);
1162 * General idea is to evaluate all the expressions first and
1163 * then print them, otherwise you get suprising behavior.
1164 * See test/prtoeval.awk for an example program.
1166 save = tree = tree->lnode;
1167 for (numnodes = 0; tree != NULL; tree = tree->rnode)
1169 emalloc(t, NODE **, numnodes * sizeof(NODE *), "do_print");
1172 for (i = 0; tree != NULL; i++, tree = tree->rnode) {
1175 /* Here lies the wumpus. R.I.P. */
1176 n = tree_eval(tree->lnode);
1180 if ((t[i]->flags & (NUMBER|STRING)) == NUMBER) {
1181 if (OFMTidx == CONVFMTidx)
1182 (void) force_string(t[i]);
1184 tval = tmp_number(t[i]->numbr);
1186 t[i] = format_val(OFMT, OFMTidx, tval);
1191 for (i = 0; i < numnodes; i++) {
1192 efwrite(t[i]->stptr, sizeof(char), t[i]->stlen, fp, "print", rp, FALSE);
1195 if (i != numnodes - 1 && OFSlen > 0)
1196 efwrite(OFS, sizeof(char), (size_t) OFSlen,
1197 fp, "print", rp, FALSE);
1201 efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp, TRUE);
1206 /* do_tolower --- lower case a string */
1213 register unsigned char *cp, *cp2;
1215 t1 = tree_eval(tree->lnode);
1216 t1 = force_string(t1);
1217 t2 = tmp_string(t1->stptr, t1->stlen);
1218 for (cp = (unsigned char *)t2->stptr,
1219 cp2 = (unsigned char *)(t2->stptr + t2->stlen); cp < cp2; cp++)
1226 /* do_toupper --- upper case a string */
1233 register unsigned char *cp, *cp2;
1235 t1 = tree_eval(tree->lnode);
1236 t1 = force_string(t1);
1237 t2 = tmp_string(t1->stptr, t1->stlen);
1238 for (cp = (unsigned char *)t2->stptr,
1239 cp2 = (unsigned char *)(t2->stptr + t2->stlen); cp < cp2; cp++)
1246 /* do_atan2 --- do the atan2 function */
1255 t1 = tree_eval(tree->lnode);
1256 t2 = tree_eval(tree->rnode->lnode);
1257 d1 = force_number(t1);
1258 d2 = force_number(t2);
1261 return tmp_number((AWKNUM) atan2(d1, d2));
1264 /* do_sin --- do the sin function */
1273 tmp = tree_eval(tree->lnode);
1274 d = sin((double) force_number(tmp));
1276 return tmp_number((AWKNUM) d);
1279 /* do_cos --- do the cos function */
1288 tmp = tree_eval(tree->lnode);
1289 d = cos((double) force_number(tmp));
1291 return tmp_number((AWKNUM) d);
1294 /* do_rand --- do the rand function */
1296 static int firstrand = TRUE;
1297 static char state[512];
1305 (void) initstate((unsigned) 1, state, sizeof state);
1309 return tmp_number((AWKNUM) random() / GAWK_RANDOM_MAX);
1312 /* do_srand --- seed the random number generator */
1319 static long save_seed = 1;
1320 long ret = save_seed; /* SVR4 awk srand returns previous seed */
1323 (void) initstate((unsigned) 1, state, sizeof state);
1324 /* don't need to srandom(1), we're changing the seed below */
1327 (void) setstate(state);
1331 srandom((unsigned int) (save_seed = (long) time((time_t *) 0) ^ getpid()));
1333 srandom((unsigned int) (save_seed = (long) time((time_t *) 0)));
1336 tmp = tree_eval(tree->lnode);
1337 srandom((unsigned int) (save_seed = (long) force_number(tmp)));
1340 return tmp_number((AWKNUM) ret);
1343 /* do_match --- match a regexp, set RSTART and RLENGTH */
1354 t1 = force_string(tree_eval(tree->lnode));
1355 tree = tree->rnode->lnode;
1356 rp = re_update(tree);
1357 rstart = research(rp, t1->stptr, 0, t1->stlen, TRUE);
1358 if (rstart >= 0) { /* match succeded */
1359 rstart++; /* 1-based indexing */
1360 rlength = REEND(rp, t1->stptr) - RESTART(rp, t1->stptr);
1361 } else { /* match failed */
1366 unref(RSTART_node->var_value);
1367 RSTART_node->var_value = make_number((AWKNUM) rstart);
1368 unref(RLENGTH_node->var_value);
1369 RLENGTH_node->var_value = make_number(rlength);
1370 return tmp_number((AWKNUM) rstart);
1373 /* sub_common --- the common code (does the work) for sub, gsub, and gensub */
1376 * Gsub can be tricksy; particularly when handling the case of null strings.
1377 * The following awk code was useful in debugging problems. It is too bad
1378 * that it does not readily translate directly into the C code, below.
1380 * #! /usr/local/bin/mawk -f
1383 * TRUE = 1; FALSE = 0
1384 * print "--->", mygsub("abc", "b+", "FOO")
1385 * print "--->", mygsub("abc", "x*", "X")
1386 * print "--->", mygsub("abc", "b*", "X")
1387 * print "--->", mygsub("abc", "c", "X")
1388 * print "--->", mygsub("abc", "c+", "X")
1389 * print "--->", mygsub("abc", "x*$", "X")
1392 * function mygsub(str, regex, replace, origstr, newstr, eosflag, nonzeroflag)
1395 * eosflag = nonzeroflag = FALSE
1396 * while (match(str, regex)) {
1397 * if (RLENGTH > 0) { # easy case
1398 * nonzeroflag = TRUE
1399 * if (RSTART == 1) { # match at front of string
1400 * newstr = newstr replace
1402 * newstr = newstr substr(str, 1, RSTART-1) replace
1404 * str = substr(str, RSTART+RLENGTH)
1405 * } else if (nonzeroflag) {
1406 * # last match was non-zero in length, and at the
1407 * # current character, we get a zero length match,
1408 * # which we don't really want, so skip over it
1409 * newstr = newstr substr(str, 1, 1)
1410 * str = substr(str, 2)
1411 * nonzeroflag = FALSE
1414 * if (RSTART == 1) {
1415 * newstr = newstr replace substr(str, 1, 1)
1416 * str = substr(str, 2)
1418 * return newstr str replace
1421 * if (length(str) == 0)
1427 * if (length(str) > 0)
1428 * newstr = newstr str # rest of string
1435 * NB: `howmany' conflicts with a SunOS macro in <sys/param.h>.
1439 sub_common(tree, how_many, backdigs)
1441 int how_many, backdigs;
1443 register char *scan;
1444 register char *bp, *cp;
1447 register char *matchend;
1448 register size_t len;
1459 NODE *s; /* subst. pattern */
1460 NODE *t; /* string to make sub. in; $0 if none given */
1462 NODE **lhs = &tree; /* value not used -- just different from NULL */
1464 Func_ptr after_assign = NULL;
1466 int global = (how_many == -1);
1468 int lastmatchnonzero;
1471 rp = re_update(tmp);
1478 t = force_string(tree_eval(tmp));
1480 /* do the search early to avoid work on non-match */
1481 if (research(rp, t->stptr, 0, t->stlen, TRUE) == -1 ||
1482 RESTART(rp, t->stptr) > t->stlen) {
1484 return tmp_number((AWKNUM) 0.0);
1487 if (tmp->type == Node_val)
1490 lhs = get_lhs(tmp, &after_assign);
1493 * create a private copy of the string
1495 if (t->stref > 1 || (t->flags & (PERM|FIELD)) != 0) {
1496 unsigned int saveflags;
1498 saveflags = t->flags;
1499 t->flags &= ~MALLOC;
1501 t->flags = saveflags;
1507 buflen = textlen + 2;
1509 s = force_string(tree_eval(s));
1511 replend = repl + s->stlen;
1512 repllen = replend - repl;
1513 emalloc(buf, char *, buflen + 2, "sub_common");
1515 buf[buflen + 1] = '\0';
1517 for (scan = repl; scan < replend; scan++) {
1521 } else if (*scan == '\\') {
1522 if (backdigs) { /* gensub, behave sanely */
1523 if (ISDIGIT(scan[1])) {
1526 } else { /* \q for any q --> q */
1530 } else { /* (proposed) posix '96 mode */
1531 if (strncmp(scan, "\\\\\\&", 4) == 0) {
1535 } else if (strncmp(scan, "\\\\&", 3) == 0) {
1536 /* \\& --> \<string> */
1540 } else if (scan[1] == '&') {
1545 leave alone, it goes into the output */
1550 lastmatchnonzero = FALSE;
1552 for (current = 1;; current++) {
1554 matchstart = t->stptr + RESTART(rp, t->stptr);
1555 matchend = t->stptr + REEND(rp, t->stptr);
1558 * create the result, copying in parts of the original
1561 len = matchstart - text + repllen
1562 + ampersands * (matchend - matchstart);
1564 while (buflen < (sofar + len + 1)) {
1566 erealloc(buf, char *, buflen, "sub_common");
1569 for (scan = text; scan < matchstart; scan++)
1571 if (global || current == how_many) {
1573 * If the current match matched the null string,
1574 * and the last match didn't and did a replacement,
1575 * then skip this one.
1577 if (lastmatchnonzero && matchstart == matchend) {
1578 lastmatchnonzero = FALSE;
1583 * If replacing all occurrences, or this is the
1584 * match we want, copy in the replacement text,
1585 * making substitutions as we go.
1587 for (scan = repl; scan < replend; scan++)
1589 for (cp = matchstart; cp < matchend; cp++)
1591 else if (*scan == '\\') {
1592 if (backdigs) { /* gensub, behave sanely */
1593 if (ISDIGIT(scan[1])) {
1594 int dig = scan[1] - '0';
1598 + SUBPATSTART(rp, t->stptr, dig);
1600 + SUBPATEND(rp, t->stptr, dig);
1602 for (cp = start; cp < end; cp++)
1605 } else /* \q for any q --> q */
1607 } else { /* posix '96 mode, bleah */
1608 if (strncmp(scan, "\\\\\\&", 4) == 0) {
1613 } else if (strncmp(scan, "\\\\&", 3) == 0) {
1614 /* \\& --> \<string> */
1616 for (cp = matchstart; cp < matchend; cp++)
1619 } else if (scan[1] == '&') {
1628 if (matchstart != matchend)
1629 lastmatchnonzero = TRUE;
1632 * don't want this match, skip over it by copying
1635 for (cp = matchstart; cp < matchend; cp++)
1639 /* catch the case of gsub(//, "blah", whatever), i.e. empty regexp */
1640 if (matchstart == matchend && matchend < text + textlen) {
1644 textlen = text + textlen - matchend;
1647 if ((current >= how_many && !global)
1648 || ((long) textlen <= 0 && matchstart == matchend)
1649 || research(rp, t->stptr, text - t->stptr, textlen, TRUE) == -1)
1654 if (buflen - sofar - textlen - 1) {
1655 buflen = sofar + textlen + 2;
1656 erealloc(buf, char *, buflen, "sub_common");
1659 for (scan = matchend; scan < text + textlen; scan++)
1668 if (matches > 0 && lhs) {
1673 if (after_assign != NULL)
1675 t->flags &= ~(NUM|NUMBER);
1677 return tmp_number((AWKNUM) matches);
1680 /* do_gsub --- global substitution */
1686 return sub_common(tree, -1, FALSE);
1689 /* do_sub --- single substitution */
1695 return sub_common(tree, 1, FALSE);
1698 /* do_gensub --- fix up the tree for sub_common for the gensub function */
1704 NODE n1, n2, n3, *t, *tmp, *target, *ret;
1705 long how_many = 1; /* default is one substitution */
1709 * We have to pull out the value of the global flag, and
1710 * build up a tree without the flag in it, turning it into the
1711 * kind of tree that sub_common() expects. It helps to draw
1712 * a picture of this ...
1715 n2 = *(tree->rnode);
1718 t = tree_eval(n2.rnode->lnode); /* value of global flag */
1720 tmp = force_string(tree_eval(n2.rnode->rnode->lnode)); /* target */
1723 * We make copy of the original target string, and pass that
1724 * in to sub_common() as the target to make the substitution in.
1725 * We will then return the result string as the return value of
1728 target = make_string(tmp->stptr, tmp->stlen);
1731 n3 = *(n2.rnode->rnode);
1735 if ((t->flags & (STR|STRING)) != 0) {
1736 if (t->stlen > 0 && (t->stptr[0] == 'g' || t->stptr[0] == 'G'))
1741 d = force_number(t);
1750 ret = sub_common(&n1, how_many, TRUE);
1754 * Note that we don't care what sub_common() returns, since the
1755 * easiest thing for the programmer is to return the string, even
1756 * if no substitutions were done.
1758 target->flags |= TEMP;
1762 #ifdef GFMT_WORKAROUND
1764 * printf's %g format [can't rely on gcvt()]
1765 * caveat: don't use as argument to *printf()!
1766 * 'format' string HAS to be of "<flags>*.*g" kind, or we bomb!
1769 sgfmt(buf, format, alt, fwidth, prec, g)
1770 char *buf; /* return buffer; assumed big enough to hold result */
1772 int alt; /* use alternate form flag */
1773 int fwidth; /* field width in a format */
1774 int prec; /* indicates desired significant digits, not decimal places */
1775 double g; /* value to format */
1778 register char *gpos;
1779 register char *d, *e, *p;
1782 strncpy(dform, format, sizeof dform - 1);
1783 dform[sizeof dform - 1] = '\0';
1784 gpos = strrchr(dform, '.');
1786 if (g == 0.0 && ! alt) { /* easy special case */
1789 (void) sprintf(buf, dform, fwidth, 0);
1793 /* advance to location of 'g' in the format */
1794 while (*gpos && *gpos != 'g' && *gpos != 'G')
1797 if (prec <= 0) /* negative precision is ignored */
1798 prec = (prec < 0 ? DEFAULT_G_PRECISION : 1);
1802 /* start with 'e' format (it'll provide nice exponent) */
1805 (void) sprintf(buf, dform, fwidth, prec, g);
1806 if ((e = strrchr(buf, 'e')) != NULL) { /* find exponent */
1807 int expn = atoi(e+1); /* fetch exponent */
1808 if (expn >= -4 && expn <= prec) { /* per K&R2, B1.2 */
1809 /* switch to 'f' format and re-do */
1811 prec -= expn; /* decimal precision */
1812 (void) sprintf(buf, dform, fwidth, prec, g);
1813 e = buf + strlen(buf);
1821 /* if 'alt' in force, then trailing zeros are not removed */
1822 if (! alt && (d = strrchr(buf, '.')) != NULL) {
1823 /* throw away an excess of precision */
1824 for (p = e; p > d && *--p == '0'; )
1830 /* and do that once again */
1834 (void) sprintf(buf, dform, fwidth, prec, g);
1837 #endif /* GFMT_WORKAROUND */
1840 #define BITS_PER_BYTE 8 /* if not true, you lose. too bad. */
1842 /* do_lshift --- perform a << operation */
1849 unsigned long uval, ushift, result;
1852 s1 = tree_eval(tree->lnode);
1853 s2 = tree_eval(tree->rnode->lnode);
1854 val = force_number(s1);
1855 shift = force_number(s2);
1860 if (val < 0 || shift < 0)
1861 warning("lshift(%lf, %lf): negative values will give strange results", val, shift);
1862 if (double_to_int(val) != val || double_to_int(shift) != shift)
1863 warning("lshift(%lf, %lf): fractional values will be truncated", val, shift);
1864 if (shift > (sizeof(unsigned long) * BITS_PER_BYTE))
1865 warning("lshift(%lf, %lf): too large shift value will give strange results", val, shift);
1868 uval = (unsigned long) val;
1869 ushift = (unsigned long) shift;
1871 result = uval << ushift;
1872 return tmp_number((AWKNUM) result);
1875 /* do_rshift --- perform a >> operation */
1882 unsigned long uval, ushift, result;
1885 s1 = tree_eval(tree->lnode);
1886 s2 = tree_eval(tree->rnode->lnode);
1887 val = force_number(s1);
1888 shift = force_number(s2);
1893 if (val < 0 || shift < 0)
1894 warning("rshift(%lf, %lf): negative values will give strange results", val, shift);
1895 if (double_to_int(val) != val || double_to_int(shift) != shift)
1896 warning("rshift(%lf, %lf): fractional values will be truncated", val, shift);
1897 if (shift > (sizeof(unsigned long) * BITS_PER_BYTE))
1898 warning("rshift(%lf, %lf): too large shift value will give strange results", val, shift);
1901 uval = (unsigned long) val;
1902 ushift = (unsigned long) shift;
1904 result = uval >> ushift;
1905 return tmp_number((AWKNUM) result);
1908 /* do_and --- perform an & operation */
1915 unsigned long uleft, uright, result;
1918 s1 = tree_eval(tree->lnode);
1919 s2 = tree_eval(tree->rnode->lnode);
1920 left = force_number(s1);
1921 right = force_number(s2);
1926 if (left < 0 || right < 0)
1927 warning("and(%lf, %lf): negative values will give strange results", left, right);
1928 if (double_to_int(left) != left || double_to_int(right) != right)
1929 warning("and(%lf, %lf): fractional values will be truncated", left, right);
1932 uleft = (unsigned long) left;
1933 uright = (unsigned long) right;
1935 result = uleft & uright;
1936 return tmp_number((AWKNUM) result);
1939 /* do_or --- perform an | operation */
1946 unsigned long uleft, uright, result;
1949 s1 = tree_eval(tree->lnode);
1950 s2 = tree_eval(tree->rnode->lnode);
1951 left = force_number(s1);
1952 right = force_number(s2);
1957 if (left < 0 || right < 0)
1958 warning("or(%lf, %lf): negative values will give strange results", left, right);
1959 if (double_to_int(left) != left || double_to_int(right) != right)
1960 warning("or(%lf, %lf): fractional values will be truncated", left, right);
1963 uleft = (unsigned long) left;
1964 uright = (unsigned long) right;
1966 result = uleft | uright;
1967 return tmp_number((AWKNUM) result);
1970 /* do_xor --- perform an ^ operation */
1977 unsigned long uleft, uright, result;
1980 s1 = tree_eval(tree->lnode);
1981 s2 = tree_eval(tree->rnode->lnode);
1982 left = force_number(s1);
1983 right = force_number(s2);
1988 if (left < 0 || right < 0)
1989 warning("xor(%lf, %lf): negative values will give strange results", left, right);
1990 if (double_to_int(left) != left || double_to_int(right) != right)
1991 warning("xor(%lf, %lf): fractional values will be truncated", left, right);
1994 uleft = (unsigned long) left;
1995 uright = (unsigned long) right;
1997 result = uleft ^ uright;
1998 return tmp_number((AWKNUM) result);
2001 /* do_compl --- perform a ~ operation */
2011 tmp = tree_eval(tree->lnode);
2012 d = force_number(tmp);
2017 warning("compl(%lf): negative value will give strange results", d);
2018 if (double_to_int(d) != d)
2019 warning("compl(%lf): fractional value will be truncated", d);
2022 uval = (unsigned long) d;
2024 return tmp_number((AWKNUM) uval);
2027 /* do_strtonum --- the strtonum function */
2036 tmp = tree_eval(tree->lnode);
2038 if ((tmp->flags & (NUM|NUMBER)) != 0)
2039 d = (double) force_number(tmp);
2040 else if (isnondecimal(tmp->stptr))
2041 d = nondec2awknum(tmp->stptr, tmp->stlen);
2043 d = (double) force_number(tmp);
2046 return tmp_number((AWKNUM) d);
2050 #if defined(BITOPS) || defined(NONDECDATA)
2051 /* nondec2awknum --- convert octal or hex value to double */
2054 * Because of awk's concatenation rules and the way awk.y:yylex()
2055 * collects a number, this routine has to be willing to stop on the
2056 * first invalid character.
2060 nondec2awknum(str, len)
2064 AWKNUM retval = 0.0;
2068 if (*str == '0' && (str[1] == 'x' || str[1] == 'X')) {
2071 for (str += 2, len -= 2; len > 0; len--, str++) {
2091 val = *str - 'a' + 10;
2099 val = *str - 'A' + 10;
2104 retval = (retval * 16) + val;
2106 } else if (*str == '0') {
2107 if (strchr(str, '8') != NULL || strchr(str, '9') != NULL)
2109 for (; len > 0; len--) {
2110 if (! isdigit(*str))
2112 retval = (retval * 8) + (*str - '0');
2124 #endif /* defined(BITOPS) || defined(NONDECDATA) */