1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 31*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" 36*7c478bd9Sstevel@tonic-gate #define DEBUG 37*7c478bd9Sstevel@tonic-gate #define tempfree(a) {if (istemp(a)) {xfree(a->sval); a->tval = 0; }} 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate #include "awk.def" 40*7c478bd9Sstevel@tonic-gate #include "math.h" 41*7c478bd9Sstevel@tonic-gate #include "awk.h" 42*7c478bd9Sstevel@tonic-gate #include "stdio.h" 43*7c478bd9Sstevel@tonic-gate #include "ctype.h" 44*7c478bd9Sstevel@tonic-gate #include "wctype.h" 45*7c478bd9Sstevel@tonic-gate #include "awktype.h" 46*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #define RECSIZE BUFSIZ 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #define FILENUM 10 51*7c478bd9Sstevel@tonic-gate struct 52*7c478bd9Sstevel@tonic-gate { 53*7c478bd9Sstevel@tonic-gate FILE *fp; 54*7c478bd9Sstevel@tonic-gate int type; 55*7c478bd9Sstevel@tonic-gate wchar_t *fname; 56*7c478bd9Sstevel@tonic-gate } files[FILENUM]; 57*7c478bd9Sstevel@tonic-gate FILE *popen(); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate extern CELL *execute(), *nodetoobj(), *fieldel(), *dopa2(), *gettemp(); 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate #define PA2NUM 29 62*7c478bd9Sstevel@tonic-gate int pairstack[PA2NUM], paircnt; 63*7c478bd9Sstevel@tonic-gate NODE *winner = NULL; 64*7c478bd9Sstevel@tonic-gate #define MAXTMP 20 65*7c478bd9Sstevel@tonic-gate CELL tmps[MAXTMP]; 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate static CELL truecell ={ OBOOL, BTRUE, 0, 0, 0.0, NUM, 0 }; 68*7c478bd9Sstevel@tonic-gate CELL *true = &truecell; 69*7c478bd9Sstevel@tonic-gate static CELL falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM, 0 }; 70*7c478bd9Sstevel@tonic-gate CELL *false = &falsecell; 71*7c478bd9Sstevel@tonic-gate static CELL breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM, 0 }; 72*7c478bd9Sstevel@tonic-gate CELL *jbreak = &breakcell; 73*7c478bd9Sstevel@tonic-gate static CELL contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM, 0 }; 74*7c478bd9Sstevel@tonic-gate CELL *jcont = &contcell; 75*7c478bd9Sstevel@tonic-gate static CELL nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM, 0 }; 76*7c478bd9Sstevel@tonic-gate CELL *jnext = &nextcell; 77*7c478bd9Sstevel@tonic-gate static CELL exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, 0 }; 78*7c478bd9Sstevel@tonic-gate CELL *jexit = &exitcell; 79*7c478bd9Sstevel@tonic-gate static CELL tempcell ={ OCELL, CTEMP, 0, 0, 0.0, NUM, 0 }; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate run(a) NODE *a; 82*7c478bd9Sstevel@tonic-gate { 83*7c478bd9Sstevel@tonic-gate register int i; 84*7c478bd9Sstevel@tonic-gate 85*7c478bd9Sstevel@tonic-gate execute(a); 86*7c478bd9Sstevel@tonic-gate /* Wait for children to complete if output to a pipe. */ 87*7c478bd9Sstevel@tonic-gate for (i=0; i<FILENUM; i++) 88*7c478bd9Sstevel@tonic-gate if (files[i].fp && files[i].type == '|') 89*7c478bd9Sstevel@tonic-gate pclose(files[i].fp); 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate CELL *execute(u) NODE *u; 94*7c478bd9Sstevel@tonic-gate { 95*7c478bd9Sstevel@tonic-gate register CELL *(*proc)(); 96*7c478bd9Sstevel@tonic-gate register CELL *x; 97*7c478bd9Sstevel@tonic-gate register NODE *a; 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate if (u == NULL) 100*7c478bd9Sstevel@tonic-gate return (true); 101*7c478bd9Sstevel@tonic-gate for (a = u; /* dummy */; a = a->nnext) { 102*7c478bd9Sstevel@tonic-gate if (cantexec(a)) 103*7c478bd9Sstevel@tonic-gate return (nodetoobj(a)); 104*7c478bd9Sstevel@tonic-gate if (notlegal(a->nobj)) 105*7c478bd9Sstevel@tonic-gate error(FATAL, "illegal statement %o", a); 106*7c478bd9Sstevel@tonic-gate proc = proctab[a->nobj-FIRSTTOKEN]; 107*7c478bd9Sstevel@tonic-gate x = (*proc)(a->narg, a->nobj); 108*7c478bd9Sstevel@tonic-gate if (isfld(x)) 109*7c478bd9Sstevel@tonic-gate fldbld(); 110*7c478bd9Sstevel@tonic-gate if (isexpr(a)) 111*7c478bd9Sstevel@tonic-gate return (x); 112*7c478bd9Sstevel@tonic-gate /* a statement, goto next statement */ 113*7c478bd9Sstevel@tonic-gate if (isjump(x)) 114*7c478bd9Sstevel@tonic-gate return (x); 115*7c478bd9Sstevel@tonic-gate if (a->nnext == (NODE *)NULL) 116*7c478bd9Sstevel@tonic-gate return (x); 117*7c478bd9Sstevel@tonic-gate tempfree(x); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate CELL *program(a, n) register NODE **a; 125*7c478bd9Sstevel@tonic-gate { 126*7c478bd9Sstevel@tonic-gate register CELL *x; 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate if (a[0] != NULL) { 129*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 130*7c478bd9Sstevel@tonic-gate if (isexit(x)) 131*7c478bd9Sstevel@tonic-gate return (true); 132*7c478bd9Sstevel@tonic-gate if (isjump(x)) 133*7c478bd9Sstevel@tonic-gate error(FATAL, "unexpected break, continue or next"); 134*7c478bd9Sstevel@tonic-gate tempfree(x); 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate while (getrec()) { 137*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 138*7c478bd9Sstevel@tonic-gate if (isexit(x)) { 139*7c478bd9Sstevel@tonic-gate tempfree(x); 140*7c478bd9Sstevel@tonic-gate break; 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate tempfree(x); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate if (a[2] != NULL) { 145*7c478bd9Sstevel@tonic-gate x = execute(a[2]); 146*7c478bd9Sstevel@tonic-gate if (isbreak(x) || isnext(x) || iscont(x)) 147*7c478bd9Sstevel@tonic-gate error(FATAL, "unexpected break, continue or next"); 148*7c478bd9Sstevel@tonic-gate tempfree(x); 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate return (true); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate CELL *getline() 157*7c478bd9Sstevel@tonic-gate { 158*7c478bd9Sstevel@tonic-gate register CELL *x; 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate x = gettemp(); 161*7c478bd9Sstevel@tonic-gate setfval(x, (awkfloat) getrec()); 162*7c478bd9Sstevel@tonic-gate return (x); 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate CELL *array(a, n) register NODE **a; 169*7c478bd9Sstevel@tonic-gate { 170*7c478bd9Sstevel@tonic-gate register CELL *x, *y; 171*7c478bd9Sstevel@tonic-gate extern CELL *arrayel(); 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 174*7c478bd9Sstevel@tonic-gate y = arrayel(a[0], x); 175*7c478bd9Sstevel@tonic-gate tempfree(x); 176*7c478bd9Sstevel@tonic-gate return (y); 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate CELL *arrayel(a, b) NODE *a; CELL *b; 183*7c478bd9Sstevel@tonic-gate { 184*7c478bd9Sstevel@tonic-gate register wchar_t *s; 185*7c478bd9Sstevel@tonic-gate register CELL *x; 186*7c478bd9Sstevel@tonic-gate register int i; 187*7c478bd9Sstevel@tonic-gate register CELL *y; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate s = getsval(b); 190*7c478bd9Sstevel@tonic-gate x = (CELL *) a; 191*7c478bd9Sstevel@tonic-gate if (!(x->tval&ARR)) { 192*7c478bd9Sstevel@tonic-gate xfree(x->sval); 193*7c478bd9Sstevel@tonic-gate x->tval &= ~STR; 194*7c478bd9Sstevel@tonic-gate x->tval |= ARR; 195*7c478bd9Sstevel@tonic-gate x->sval = (wchar_t *) makesymtab(); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate y = setsymtab(s, tostring(L_NULL), 0.0, STR|NUM, x->sval); 198*7c478bd9Sstevel@tonic-gate y->ctype = OCELL; 199*7c478bd9Sstevel@tonic-gate y->csub = CVAR; 200*7c478bd9Sstevel@tonic-gate return (y); 201*7c478bd9Sstevel@tonic-gate } 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gate CELL *matchop(a, n) NODE **a; 204*7c478bd9Sstevel@tonic-gate { 205*7c478bd9Sstevel@tonic-gate register CELL *x; 206*7c478bd9Sstevel@tonic-gate register wchar_t *s; 207*7c478bd9Sstevel@tonic-gate register int i; 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 210*7c478bd9Sstevel@tonic-gate s = getsval(x); 211*7c478bd9Sstevel@tonic-gate tempfree(x); 212*7c478bd9Sstevel@tonic-gate i = match(a[1], s); 213*7c478bd9Sstevel@tonic-gate if (n == MATCH && i == 1 || n == NOTMATCH && i == 0) 214*7c478bd9Sstevel@tonic-gate return (true); 215*7c478bd9Sstevel@tonic-gate else 216*7c478bd9Sstevel@tonic-gate return (false); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate CELL *boolop(a, n) NODE **a; 223*7c478bd9Sstevel@tonic-gate { 224*7c478bd9Sstevel@tonic-gate register CELL *x, *y; 225*7c478bd9Sstevel@tonic-gate register int i; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate 230*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 231*7c478bd9Sstevel@tonic-gate i = istrue(x); 232*7c478bd9Sstevel@tonic-gate tempfree(x); 233*7c478bd9Sstevel@tonic-gate switch (n) { 234*7c478bd9Sstevel@tonic-gate case BOR: 235*7c478bd9Sstevel@tonic-gate if (i) return (true); 236*7c478bd9Sstevel@tonic-gate y = execute(a[1]); 237*7c478bd9Sstevel@tonic-gate i = istrue(y); 238*7c478bd9Sstevel@tonic-gate tempfree(y); 239*7c478bd9Sstevel@tonic-gate if (i) return (true); 240*7c478bd9Sstevel@tonic-gate else return (false); 241*7c478bd9Sstevel@tonic-gate case AND: 242*7c478bd9Sstevel@tonic-gate if (!i) return (false); 243*7c478bd9Sstevel@tonic-gate y = execute(a[1]); 244*7c478bd9Sstevel@tonic-gate i = istrue(y); 245*7c478bd9Sstevel@tonic-gate tempfree(y); 246*7c478bd9Sstevel@tonic-gate if (i) return (true); 247*7c478bd9Sstevel@tonic-gate else return (false); 248*7c478bd9Sstevel@tonic-gate case NOT: 249*7c478bd9Sstevel@tonic-gate if (i) return (false); 250*7c478bd9Sstevel@tonic-gate else return (true); 251*7c478bd9Sstevel@tonic-gate default: 252*7c478bd9Sstevel@tonic-gate error(FATAL, "unknown boolean operator %d", n); 253*7c478bd9Sstevel@tonic-gate } 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate 259*7c478bd9Sstevel@tonic-gate CELL *relop(a, n) NODE **a; 260*7c478bd9Sstevel@tonic-gate { 261*7c478bd9Sstevel@tonic-gate register int i; 262*7c478bd9Sstevel@tonic-gate register CELL *x, *y; 263*7c478bd9Sstevel@tonic-gate awkfloat j; 264*7c478bd9Sstevel@tonic-gate register wchar_t *xs, *ys; 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate 269*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 270*7c478bd9Sstevel@tonic-gate y = execute(a[1]); 271*7c478bd9Sstevel@tonic-gate if (x->tval&NUM && y->tval&NUM) { 272*7c478bd9Sstevel@tonic-gate j = x->fval - y->fval; 273*7c478bd9Sstevel@tonic-gate i = j<0? -1: (j>0? 1: 0); 274*7c478bd9Sstevel@tonic-gate } else { 275*7c478bd9Sstevel@tonic-gate xs = getsval(x); 276*7c478bd9Sstevel@tonic-gate ys = getsval(y); 277*7c478bd9Sstevel@tonic-gate if (xs && ys) 278*7c478bd9Sstevel@tonic-gate i = wscoll(xs, ys); 279*7c478bd9Sstevel@tonic-gate else 280*7c478bd9Sstevel@tonic-gate return (false); 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate tempfree(x); 283*7c478bd9Sstevel@tonic-gate tempfree(y); 284*7c478bd9Sstevel@tonic-gate switch (n) { 285*7c478bd9Sstevel@tonic-gate case LT: if (i<0) return (true); 286*7c478bd9Sstevel@tonic-gate else return (false); 287*7c478bd9Sstevel@tonic-gate case LE: if (i<=0) return (true); 288*7c478bd9Sstevel@tonic-gate else return (false); 289*7c478bd9Sstevel@tonic-gate case NE: if (i!=0) return (true); 290*7c478bd9Sstevel@tonic-gate else return (false); 291*7c478bd9Sstevel@tonic-gate case EQ: if (i == 0) return (true); 292*7c478bd9Sstevel@tonic-gate else return (false); 293*7c478bd9Sstevel@tonic-gate case GE: if (i>=0) return (true); 294*7c478bd9Sstevel@tonic-gate else return (false); 295*7c478bd9Sstevel@tonic-gate case GT: if (i>0) return (true); 296*7c478bd9Sstevel@tonic-gate else return (false); 297*7c478bd9Sstevel@tonic-gate default: 298*7c478bd9Sstevel@tonic-gate error(FATAL, "unknown relational operator %d", n); 299*7c478bd9Sstevel@tonic-gate } 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate 303*7c478bd9Sstevel@tonic-gate 304*7c478bd9Sstevel@tonic-gate 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate 307*7c478bd9Sstevel@tonic-gate 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate CELL *gettemp() 310*7c478bd9Sstevel@tonic-gate { 311*7c478bd9Sstevel@tonic-gate register int i; 312*7c478bd9Sstevel@tonic-gate register CELL *x; 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate 317*7c478bd9Sstevel@tonic-gate for (i=0; i<MAXTMP; i++) 318*7c478bd9Sstevel@tonic-gate if (tmps[i].tval == 0) 319*7c478bd9Sstevel@tonic-gate break; 320*7c478bd9Sstevel@tonic-gate if (i == MAXTMP) 321*7c478bd9Sstevel@tonic-gate error(FATAL, "out of temporaries in gettemp"); 322*7c478bd9Sstevel@tonic-gate tmps[i] = tempcell; 323*7c478bd9Sstevel@tonic-gate x = &tmps[i]; 324*7c478bd9Sstevel@tonic-gate return (x); 325*7c478bd9Sstevel@tonic-gate } 326*7c478bd9Sstevel@tonic-gate 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate CELL *indirect(a, n) NODE **a; 331*7c478bd9Sstevel@tonic-gate { 332*7c478bd9Sstevel@tonic-gate register CELL *x; 333*7c478bd9Sstevel@tonic-gate register int m; 334*7c478bd9Sstevel@tonic-gate CELL *fieldadr(); 335*7c478bd9Sstevel@tonic-gate 336*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 337*7c478bd9Sstevel@tonic-gate m = getfval(x); 338*7c478bd9Sstevel@tonic-gate tempfree(x); 339*7c478bd9Sstevel@tonic-gate x = fieldadr(m); 340*7c478bd9Sstevel@tonic-gate x->ctype = OCELL; 341*7c478bd9Sstevel@tonic-gate x->csub = CFLD; 342*7c478bd9Sstevel@tonic-gate return (x); 343*7c478bd9Sstevel@tonic-gate } 344*7c478bd9Sstevel@tonic-gate 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate CELL *substr(a, nnn) NODE **a; 349*7c478bd9Sstevel@tonic-gate { 350*7c478bd9Sstevel@tonic-gate register int k, m, n; 351*7c478bd9Sstevel@tonic-gate register wchar_t *s, temp; 352*7c478bd9Sstevel@tonic-gate register CELL *x, *y; 353*7c478bd9Sstevel@tonic-gate 354*7c478bd9Sstevel@tonic-gate y = execute(a[0]); 355*7c478bd9Sstevel@tonic-gate s = getsval(y); 356*7c478bd9Sstevel@tonic-gate k = wslen(s) + 1; 357*7c478bd9Sstevel@tonic-gate if (k <= 1) { 358*7c478bd9Sstevel@tonic-gate x = gettemp(); 359*7c478bd9Sstevel@tonic-gate setsval(x, L_NULL); 360*7c478bd9Sstevel@tonic-gate return (x); 361*7c478bd9Sstevel@tonic-gate } 362*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 363*7c478bd9Sstevel@tonic-gate m = getfval(x); 364*7c478bd9Sstevel@tonic-gate if (m <= 0) 365*7c478bd9Sstevel@tonic-gate m = 1; 366*7c478bd9Sstevel@tonic-gate else if (m > k) 367*7c478bd9Sstevel@tonic-gate m = k; 368*7c478bd9Sstevel@tonic-gate tempfree(x); 369*7c478bd9Sstevel@tonic-gate if (a[2] != 0) { 370*7c478bd9Sstevel@tonic-gate x = execute(a[2]); 371*7c478bd9Sstevel@tonic-gate n = getfval(x); 372*7c478bd9Sstevel@tonic-gate tempfree(x); 373*7c478bd9Sstevel@tonic-gate } 374*7c478bd9Sstevel@tonic-gate else 375*7c478bd9Sstevel@tonic-gate n = k - 1; 376*7c478bd9Sstevel@tonic-gate if (n < 0) 377*7c478bd9Sstevel@tonic-gate n = 0; 378*7c478bd9Sstevel@tonic-gate else if (n > k - m) 379*7c478bd9Sstevel@tonic-gate n = k - m; 380*7c478bd9Sstevel@tonic-gate dprintf("substr: m=%d, n=%d, s=%ws\n", m, n, s); 381*7c478bd9Sstevel@tonic-gate x = gettemp(); 382*7c478bd9Sstevel@tonic-gate temp = s[n+m-1]; 383*7c478bd9Sstevel@tonic-gate s[n+m-1] = (wchar_t)0x0; 384*7c478bd9Sstevel@tonic-gate setsval(x, s + m - 1); 385*7c478bd9Sstevel@tonic-gate s[n+m-1] = temp; 386*7c478bd9Sstevel@tonic-gate tempfree(y); 387*7c478bd9Sstevel@tonic-gate return (x); 388*7c478bd9Sstevel@tonic-gate } 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate 392*7c478bd9Sstevel@tonic-gate 393*7c478bd9Sstevel@tonic-gate CELL *sindex(a, nnn) NODE **a; 394*7c478bd9Sstevel@tonic-gate { 395*7c478bd9Sstevel@tonic-gate register CELL *x; 396*7c478bd9Sstevel@tonic-gate register wchar_t *s1, *s2, *p1, *p2, *q; 397*7c478bd9Sstevel@tonic-gate 398*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 399*7c478bd9Sstevel@tonic-gate s1 = getsval(x); 400*7c478bd9Sstevel@tonic-gate tempfree(x); 401*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 402*7c478bd9Sstevel@tonic-gate s2 = getsval(x); 403*7c478bd9Sstevel@tonic-gate tempfree(x); 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate x = gettemp(); 406*7c478bd9Sstevel@tonic-gate for (p1 = s1; *p1 != (wchar_t)0x0; p1++) { 407*7c478bd9Sstevel@tonic-gate for (q=p1, p2=s2; *p2 != (wchar_t)0x0 && *q == *p2; q++, p2++) 408*7c478bd9Sstevel@tonic-gate ; 409*7c478bd9Sstevel@tonic-gate if (*p2 == (wchar_t)0x0) { 410*7c478bd9Sstevel@tonic-gate setfval(x, (awkfloat) (p1 - s1 + 1)); /* origin 1 */ 411*7c478bd9Sstevel@tonic-gate return (x); 412*7c478bd9Sstevel@tonic-gate } 413*7c478bd9Sstevel@tonic-gate } 414*7c478bd9Sstevel@tonic-gate setfval(x, 0.0); 415*7c478bd9Sstevel@tonic-gate return (x); 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate 418*7c478bd9Sstevel@tonic-gate 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate 421*7c478bd9Sstevel@tonic-gate wchar_t *format(s, a) register wchar_t *s; NODE *a; 422*7c478bd9Sstevel@tonic-gate { 423*7c478bd9Sstevel@tonic-gate register wchar_t *buf, *ep, *str; 424*7c478bd9Sstevel@tonic-gate register wchar_t *p; 425*7c478bd9Sstevel@tonic-gate register char *t; 426*7c478bd9Sstevel@tonic-gate wchar_t *os; 427*7c478bd9Sstevel@tonic-gate wchar_t tbuf[2*RECSIZE]; 428*7c478bd9Sstevel@tonic-gate char fmt[200]; 429*7c478bd9Sstevel@tonic-gate register CELL *x; 430*7c478bd9Sstevel@tonic-gate int flag = 0; 431*7c478bd9Sstevel@tonic-gate awkfloat xf; 432*7c478bd9Sstevel@tonic-gate 433*7c478bd9Sstevel@tonic-gate os = s; 434*7c478bd9Sstevel@tonic-gate p = buf= (wchar_t *)malloc(RECSIZE * sizeof (wchar_t)); 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate if (p == NULL) 437*7c478bd9Sstevel@tonic-gate error(FATAL, "out of space in format"); 438*7c478bd9Sstevel@tonic-gate ep = p + RECSIZE; 439*7c478bd9Sstevel@tonic-gate while (*s) { 440*7c478bd9Sstevel@tonic-gate if (*s != '%') { 441*7c478bd9Sstevel@tonic-gate *p++ = *s++; 442*7c478bd9Sstevel@tonic-gate continue; 443*7c478bd9Sstevel@tonic-gate } 444*7c478bd9Sstevel@tonic-gate if (*(s+1) == '%') { 445*7c478bd9Sstevel@tonic-gate *p++ = '%'; 446*7c478bd9Sstevel@tonic-gate s += 2; 447*7c478bd9Sstevel@tonic-gate continue; 448*7c478bd9Sstevel@tonic-gate } 449*7c478bd9Sstevel@tonic-gate for (t=fmt; *s != '\0'; s++) 450*7c478bd9Sstevel@tonic-gate { 451*7c478bd9Sstevel@tonic-gate if (*s == 's' || *s == 'c') 452*7c478bd9Sstevel@tonic-gate *t++ = 'w'; 453*7c478bd9Sstevel@tonic-gate *t++ = *s; 454*7c478bd9Sstevel@tonic-gate if (*s >= 'a' && *s <= 'z' && *s != 'l') 455*7c478bd9Sstevel@tonic-gate break; 456*7c478bd9Sstevel@tonic-gate if (*s == '*') { 457*7c478bd9Sstevel@tonic-gate if (a == NULL) { 458*7c478bd9Sstevel@tonic-gate error(FATAL, 459*7c478bd9Sstevel@tonic-gate "not enough arguments in printf(%ws) or sprintf(%ws)", 460*7c478bd9Sstevel@tonic-gate os, os); 461*7c478bd9Sstevel@tonic-gate } 462*7c478bd9Sstevel@tonic-gate x = execute(a); 463*7c478bd9Sstevel@tonic-gate a = a->nnext; 464*7c478bd9Sstevel@tonic-gate sprintf(t-1, "%d", (int) getfval(x)); 465*7c478bd9Sstevel@tonic-gate t = fmt + strlen(fmt); 466*7c478bd9Sstevel@tonic-gate tempfree(x); 467*7c478bd9Sstevel@tonic-gate } 468*7c478bd9Sstevel@tonic-gate 469*7c478bd9Sstevel@tonic-gate } 470*7c478bd9Sstevel@tonic-gate *t = '\0'; 471*7c478bd9Sstevel@tonic-gate if (t >= fmt + sizeof (fmt)) 472*7c478bd9Sstevel@tonic-gate error(FATAL, "format item %.20ws... too long", os); 473*7c478bd9Sstevel@tonic-gate switch (*s) { 474*7c478bd9Sstevel@tonic-gate case 'f': case 'e': case 'g': 475*7c478bd9Sstevel@tonic-gate flag = 1; 476*7c478bd9Sstevel@tonic-gate break; 477*7c478bd9Sstevel@tonic-gate case 'd': 478*7c478bd9Sstevel@tonic-gate flag = 2; 479*7c478bd9Sstevel@tonic-gate if (*(s-1) == 'l') break; 480*7c478bd9Sstevel@tonic-gate *(t-1) = 'l'; 481*7c478bd9Sstevel@tonic-gate *t = 'd'; 482*7c478bd9Sstevel@tonic-gate *++t = '\0'; 483*7c478bd9Sstevel@tonic-gate break; 484*7c478bd9Sstevel@tonic-gate case 'o': case 'x': 485*7c478bd9Sstevel@tonic-gate flag = *(s-1) == 'l' ? 2 : 3; 486*7c478bd9Sstevel@tonic-gate break; 487*7c478bd9Sstevel@tonic-gate case 'c': 488*7c478bd9Sstevel@tonic-gate flag = 3; 489*7c478bd9Sstevel@tonic-gate break; 490*7c478bd9Sstevel@tonic-gate case 's': 491*7c478bd9Sstevel@tonic-gate flag = 4; 492*7c478bd9Sstevel@tonic-gate break; 493*7c478bd9Sstevel@tonic-gate default: 494*7c478bd9Sstevel@tonic-gate flag = 0; 495*7c478bd9Sstevel@tonic-gate break; 496*7c478bd9Sstevel@tonic-gate } 497*7c478bd9Sstevel@tonic-gate if (flag == 0) { 498*7c478bd9Sstevel@tonic-gate wsprintf(p, "%s", fmt); 499*7c478bd9Sstevel@tonic-gate p += wslen(p); 500*7c478bd9Sstevel@tonic-gate continue; 501*7c478bd9Sstevel@tonic-gate } 502*7c478bd9Sstevel@tonic-gate if (a == NULL) { 503*7c478bd9Sstevel@tonic-gate error(FATAL, 504*7c478bd9Sstevel@tonic-gate "not enough arguments in printf(%ws) or sprintf(%ws)", os, os); 505*7c478bd9Sstevel@tonic-gate } 506*7c478bd9Sstevel@tonic-gate x = execute(a); 507*7c478bd9Sstevel@tonic-gate a = a->nnext; 508*7c478bd9Sstevel@tonic-gate 509*7c478bd9Sstevel@tonic-gate /* 510*7c478bd9Sstevel@tonic-gate * Get the string to check length; %s is the usual problem; 511*7c478bd9Sstevel@tonic-gate * other conversions can cause overrun if they occur when 512*7c478bd9Sstevel@tonic-gate * the buffer is almost filled. 513*7c478bd9Sstevel@tonic-gate */ 514*7c478bd9Sstevel@tonic-gate if (flag == 4) { /* watch out for converting to numbers! */ 515*7c478bd9Sstevel@tonic-gate str = getsval(x); 516*7c478bd9Sstevel@tonic-gate } 517*7c478bd9Sstevel@tonic-gate else { 518*7c478bd9Sstevel@tonic-gate xf = getfval(x); 519*7c478bd9Sstevel@tonic-gate if (flag == 1) wsprintf(tbuf, fmt, xf); 520*7c478bd9Sstevel@tonic-gate else if (flag == 2) wsprintf(tbuf, fmt, (long)xf); 521*7c478bd9Sstevel@tonic-gate else if (flag == 3) wsprintf(tbuf, fmt, (int)xf); 522*7c478bd9Sstevel@tonic-gate if (wslen(tbuf) >= RECSIZE) 523*7c478bd9Sstevel@tonic-gate error(FATAL, "formatted item %s... too long", 524*7c478bd9Sstevel@tonic-gate tbuf); 525*7c478bd9Sstevel@tonic-gate str = tbuf; 526*7c478bd9Sstevel@tonic-gate } 527*7c478bd9Sstevel@tonic-gate /* 528*7c478bd9Sstevel@tonic-gate * If string overruns the buffer, reallocate; 529*7c478bd9Sstevel@tonic-gate * consider length of format string 530*7c478bd9Sstevel@tonic-gate */ 531*7c478bd9Sstevel@tonic-gate if (p + wslen(str) + wslen(s) + 1 >= ep) { 532*7c478bd9Sstevel@tonic-gate int newlen, oldlen; 533*7c478bd9Sstevel@tonic-gate 534*7c478bd9Sstevel@tonic-gate oldlen = p - buf; 535*7c478bd9Sstevel@tonic-gate /* Add RECSIZE for additional space */ 536*7c478bd9Sstevel@tonic-gate newlen = oldlen + wslen(str) + RECSIZE; 537*7c478bd9Sstevel@tonic-gate buf = realloc(buf, (unsigned) newlen * sizeof(wchar_t)); 538*7c478bd9Sstevel@tonic-gate if (buf == NULL) 539*7c478bd9Sstevel@tonic-gate error(FATAL, "out of format space"); 540*7c478bd9Sstevel@tonic-gate p = buf + oldlen; 541*7c478bd9Sstevel@tonic-gate ep = buf + newlen; 542*7c478bd9Sstevel@tonic-gate } 543*7c478bd9Sstevel@tonic-gate /* Transfer string to buffer */ 544*7c478bd9Sstevel@tonic-gate if (flag == 4) 545*7c478bd9Sstevel@tonic-gate wsprintf(p, fmt, str); 546*7c478bd9Sstevel@tonic-gate else 547*7c478bd9Sstevel@tonic-gate wscpy(p, str); 548*7c478bd9Sstevel@tonic-gate 549*7c478bd9Sstevel@tonic-gate tempfree(x); 550*7c478bd9Sstevel@tonic-gate p += wslen(p); 551*7c478bd9Sstevel@tonic-gate if (p >= ep) 552*7c478bd9Sstevel@tonic-gate error(FATAL, "formatted string too long"); 553*7c478bd9Sstevel@tonic-gate s++; 554*7c478bd9Sstevel@tonic-gate } 555*7c478bd9Sstevel@tonic-gate *p = '\0'; 556*7c478bd9Sstevel@tonic-gate return (buf); 557*7c478bd9Sstevel@tonic-gate } 558*7c478bd9Sstevel@tonic-gate 559*7c478bd9Sstevel@tonic-gate 560*7c478bd9Sstevel@tonic-gate CELL *asprintf(a, n) NODE **a; 561*7c478bd9Sstevel@tonic-gate { 562*7c478bd9Sstevel@tonic-gate register CELL *x; 563*7c478bd9Sstevel@tonic-gate register NODE *y; 564*7c478bd9Sstevel@tonic-gate register wchar_t *s; 565*7c478bd9Sstevel@tonic-gate 566*7c478bd9Sstevel@tonic-gate y = a[0]->nnext; 567*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 568*7c478bd9Sstevel@tonic-gate s = format(getsval(x), y); 569*7c478bd9Sstevel@tonic-gate tempfree(x); 570*7c478bd9Sstevel@tonic-gate x = gettemp(); 571*7c478bd9Sstevel@tonic-gate x->sval = s; 572*7c478bd9Sstevel@tonic-gate x->tval = STR; 573*7c478bd9Sstevel@tonic-gate return (x); 574*7c478bd9Sstevel@tonic-gate } 575*7c478bd9Sstevel@tonic-gate 576*7c478bd9Sstevel@tonic-gate 577*7c478bd9Sstevel@tonic-gate CELL *arith(a, n) NODE **a; 578*7c478bd9Sstevel@tonic-gate { 579*7c478bd9Sstevel@tonic-gate awkfloat i, j; 580*7c478bd9Sstevel@tonic-gate register CELL *x, *y, *z; 581*7c478bd9Sstevel@tonic-gate 582*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 583*7c478bd9Sstevel@tonic-gate i = getfval(x); 584*7c478bd9Sstevel@tonic-gate tempfree(x); 585*7c478bd9Sstevel@tonic-gate if (n != UMINUS) { 586*7c478bd9Sstevel@tonic-gate y = execute(a[1]); 587*7c478bd9Sstevel@tonic-gate j = getfval(y); 588*7c478bd9Sstevel@tonic-gate tempfree(y); 589*7c478bd9Sstevel@tonic-gate } 590*7c478bd9Sstevel@tonic-gate z = gettemp(); 591*7c478bd9Sstevel@tonic-gate switch (n) { 592*7c478bd9Sstevel@tonic-gate case ADD: 593*7c478bd9Sstevel@tonic-gate i += j; 594*7c478bd9Sstevel@tonic-gate break; 595*7c478bd9Sstevel@tonic-gate case MINUS: 596*7c478bd9Sstevel@tonic-gate i -= j; 597*7c478bd9Sstevel@tonic-gate break; 598*7c478bd9Sstevel@tonic-gate case MULT: 599*7c478bd9Sstevel@tonic-gate i *= j; 600*7c478bd9Sstevel@tonic-gate break; 601*7c478bd9Sstevel@tonic-gate case DIVIDE: 602*7c478bd9Sstevel@tonic-gate if (j == 0) 603*7c478bd9Sstevel@tonic-gate error(FATAL, "division by zero"); 604*7c478bd9Sstevel@tonic-gate i /= j; 605*7c478bd9Sstevel@tonic-gate break; 606*7c478bd9Sstevel@tonic-gate case MOD: 607*7c478bd9Sstevel@tonic-gate if (j == 0) 608*7c478bd9Sstevel@tonic-gate error(FATAL, "division by zero"); 609*7c478bd9Sstevel@tonic-gate i = i - j*(long)(i/j); 610*7c478bd9Sstevel@tonic-gate break; 611*7c478bd9Sstevel@tonic-gate case UMINUS: 612*7c478bd9Sstevel@tonic-gate i = -i; 613*7c478bd9Sstevel@tonic-gate break; 614*7c478bd9Sstevel@tonic-gate default: 615*7c478bd9Sstevel@tonic-gate error(FATAL, "illegal arithmetic operator %d", n); 616*7c478bd9Sstevel@tonic-gate } 617*7c478bd9Sstevel@tonic-gate setfval(z, i); 618*7c478bd9Sstevel@tonic-gate return (z); 619*7c478bd9Sstevel@tonic-gate } 620*7c478bd9Sstevel@tonic-gate 621*7c478bd9Sstevel@tonic-gate 622*7c478bd9Sstevel@tonic-gate 623*7c478bd9Sstevel@tonic-gate 624*7c478bd9Sstevel@tonic-gate CELL *incrdecr(a, n) NODE **a; 625*7c478bd9Sstevel@tonic-gate { 626*7c478bd9Sstevel@tonic-gate register CELL *x, *z; 627*7c478bd9Sstevel@tonic-gate register int k; 628*7c478bd9Sstevel@tonic-gate awkfloat xf; 629*7c478bd9Sstevel@tonic-gate 630*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 631*7c478bd9Sstevel@tonic-gate xf = getfval(x); 632*7c478bd9Sstevel@tonic-gate k = (n == PREINCR || n == POSTINCR) ? 1 : -1; 633*7c478bd9Sstevel@tonic-gate if (n == PREINCR || n == PREDECR) { 634*7c478bd9Sstevel@tonic-gate setfval(x, xf + k); 635*7c478bd9Sstevel@tonic-gate return (x); 636*7c478bd9Sstevel@tonic-gate } 637*7c478bd9Sstevel@tonic-gate z = gettemp(); 638*7c478bd9Sstevel@tonic-gate setfval(z, xf); 639*7c478bd9Sstevel@tonic-gate setfval(x, xf + k); 640*7c478bd9Sstevel@tonic-gate tempfree(x); 641*7c478bd9Sstevel@tonic-gate return (z); 642*7c478bd9Sstevel@tonic-gate } 643*7c478bd9Sstevel@tonic-gate 644*7c478bd9Sstevel@tonic-gate 645*7c478bd9Sstevel@tonic-gate 646*7c478bd9Sstevel@tonic-gate CELL *assign(a, n) NODE **a; 647*7c478bd9Sstevel@tonic-gate { 648*7c478bd9Sstevel@tonic-gate register CELL *x, *y; 649*7c478bd9Sstevel@tonic-gate awkfloat xf, yf; 650*7c478bd9Sstevel@tonic-gate 651*7c478bd9Sstevel@tonic-gate 652*7c478bd9Sstevel@tonic-gate 653*7c478bd9Sstevel@tonic-gate 654*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 655*7c478bd9Sstevel@tonic-gate y = execute(a[1]); 656*7c478bd9Sstevel@tonic-gate if (n == ASSIGN) { /* ordinary assignment */ 657*7c478bd9Sstevel@tonic-gate if ((y->tval & (STR|NUM)) == (STR|NUM)) { 658*7c478bd9Sstevel@tonic-gate setsval(x, y->sval); 659*7c478bd9Sstevel@tonic-gate x->fval = y->fval; 660*7c478bd9Sstevel@tonic-gate x->tval |= NUM; 661*7c478bd9Sstevel@tonic-gate 662*7c478bd9Sstevel@tonic-gate } else if (y->tval & STR) 663*7c478bd9Sstevel@tonic-gate setsval(x, y->sval); 664*7c478bd9Sstevel@tonic-gate else if (y->tval & NUM) 665*7c478bd9Sstevel@tonic-gate setfval(x, y->fval); 666*7c478bd9Sstevel@tonic-gate tempfree(y); 667*7c478bd9Sstevel@tonic-gate return (x); 668*7c478bd9Sstevel@tonic-gate } 669*7c478bd9Sstevel@tonic-gate xf = getfval(x); 670*7c478bd9Sstevel@tonic-gate yf = getfval(y); 671*7c478bd9Sstevel@tonic-gate switch (n) { 672*7c478bd9Sstevel@tonic-gate case ADDEQ: 673*7c478bd9Sstevel@tonic-gate xf += yf; 674*7c478bd9Sstevel@tonic-gate break; 675*7c478bd9Sstevel@tonic-gate case SUBEQ: 676*7c478bd9Sstevel@tonic-gate xf -= yf; 677*7c478bd9Sstevel@tonic-gate break; 678*7c478bd9Sstevel@tonic-gate case MULTEQ: 679*7c478bd9Sstevel@tonic-gate xf *= yf; 680*7c478bd9Sstevel@tonic-gate break; 681*7c478bd9Sstevel@tonic-gate case DIVEQ: 682*7c478bd9Sstevel@tonic-gate if (yf == 0) 683*7c478bd9Sstevel@tonic-gate error(FATAL, "division by zero"); 684*7c478bd9Sstevel@tonic-gate xf /= yf; 685*7c478bd9Sstevel@tonic-gate break; 686*7c478bd9Sstevel@tonic-gate case MODEQ: 687*7c478bd9Sstevel@tonic-gate if (yf == 0) 688*7c478bd9Sstevel@tonic-gate error(FATAL, "division by zero"); 689*7c478bd9Sstevel@tonic-gate xf = xf - yf*(long)(xf/yf); 690*7c478bd9Sstevel@tonic-gate break; 691*7c478bd9Sstevel@tonic-gate default: 692*7c478bd9Sstevel@tonic-gate error(FATAL, "illegal assignment operator %d", n); 693*7c478bd9Sstevel@tonic-gate break; 694*7c478bd9Sstevel@tonic-gate } 695*7c478bd9Sstevel@tonic-gate tempfree(y); 696*7c478bd9Sstevel@tonic-gate setfval(x, xf); 697*7c478bd9Sstevel@tonic-gate return (x); 698*7c478bd9Sstevel@tonic-gate } 699*7c478bd9Sstevel@tonic-gate 700*7c478bd9Sstevel@tonic-gate 701*7c478bd9Sstevel@tonic-gate 702*7c478bd9Sstevel@tonic-gate 703*7c478bd9Sstevel@tonic-gate CELL *cat(a, q) NODE **a; 704*7c478bd9Sstevel@tonic-gate { 705*7c478bd9Sstevel@tonic-gate register CELL *x, *y, *z; 706*7c478bd9Sstevel@tonic-gate register int n1, n2; 707*7c478bd9Sstevel@tonic-gate register wchar_t *s; 708*7c478bd9Sstevel@tonic-gate 709*7c478bd9Sstevel@tonic-gate 710*7c478bd9Sstevel@tonic-gate 711*7c478bd9Sstevel@tonic-gate 712*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 713*7c478bd9Sstevel@tonic-gate y = execute(a[1]); 714*7c478bd9Sstevel@tonic-gate getsval(x); 715*7c478bd9Sstevel@tonic-gate getsval(y); 716*7c478bd9Sstevel@tonic-gate n1 = wslen(x->sval); 717*7c478bd9Sstevel@tonic-gate n2 = wslen(y->sval); 718*7c478bd9Sstevel@tonic-gate if ((s = (wchar_t *) malloc((n1 + n2 + 1) * sizeof (wchar_t))) == NULL) 719*7c478bd9Sstevel@tonic-gate error(FATAL, "out of space in cat"); 720*7c478bd9Sstevel@tonic-gate wscpy(s, x->sval); 721*7c478bd9Sstevel@tonic-gate wscpy(s+n1, y->sval); 722*7c478bd9Sstevel@tonic-gate tempfree(y); 723*7c478bd9Sstevel@tonic-gate z = gettemp(); 724*7c478bd9Sstevel@tonic-gate z->sval = s; 725*7c478bd9Sstevel@tonic-gate z->tval = STR; 726*7c478bd9Sstevel@tonic-gate tempfree(x); 727*7c478bd9Sstevel@tonic-gate return (z); 728*7c478bd9Sstevel@tonic-gate } 729*7c478bd9Sstevel@tonic-gate 730*7c478bd9Sstevel@tonic-gate 731*7c478bd9Sstevel@tonic-gate 732*7c478bd9Sstevel@tonic-gate 733*7c478bd9Sstevel@tonic-gate CELL *pastat(a, n) NODE **a; 734*7c478bd9Sstevel@tonic-gate { 735*7c478bd9Sstevel@tonic-gate register CELL *x; 736*7c478bd9Sstevel@tonic-gate 737*7c478bd9Sstevel@tonic-gate 738*7c478bd9Sstevel@tonic-gate 739*7c478bd9Sstevel@tonic-gate 740*7c478bd9Sstevel@tonic-gate if (a[0] == 0) 741*7c478bd9Sstevel@tonic-gate x = true; 742*7c478bd9Sstevel@tonic-gate else 743*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 744*7c478bd9Sstevel@tonic-gate if (istrue(x)) { 745*7c478bd9Sstevel@tonic-gate tempfree(x); 746*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 747*7c478bd9Sstevel@tonic-gate } 748*7c478bd9Sstevel@tonic-gate return (x); 749*7c478bd9Sstevel@tonic-gate } 750*7c478bd9Sstevel@tonic-gate 751*7c478bd9Sstevel@tonic-gate 752*7c478bd9Sstevel@tonic-gate 753*7c478bd9Sstevel@tonic-gate 754*7c478bd9Sstevel@tonic-gate CELL *dopa2(a, n) NODE **a; 755*7c478bd9Sstevel@tonic-gate { 756*7c478bd9Sstevel@tonic-gate register CELL *x; 757*7c478bd9Sstevel@tonic-gate register int pair; 758*7c478bd9Sstevel@tonic-gate 759*7c478bd9Sstevel@tonic-gate 760*7c478bd9Sstevel@tonic-gate 761*7c478bd9Sstevel@tonic-gate 762*7c478bd9Sstevel@tonic-gate pair = (int) a[3]; 763*7c478bd9Sstevel@tonic-gate if (pairstack[pair] == 0) { 764*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 765*7c478bd9Sstevel@tonic-gate if (istrue(x)) 766*7c478bd9Sstevel@tonic-gate pairstack[pair] = 1; 767*7c478bd9Sstevel@tonic-gate tempfree(x); 768*7c478bd9Sstevel@tonic-gate } 769*7c478bd9Sstevel@tonic-gate if (pairstack[pair] == 1) { 770*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 771*7c478bd9Sstevel@tonic-gate if (istrue(x)) 772*7c478bd9Sstevel@tonic-gate pairstack[pair] = 0; 773*7c478bd9Sstevel@tonic-gate tempfree(x); 774*7c478bd9Sstevel@tonic-gate x = execute(a[2]); 775*7c478bd9Sstevel@tonic-gate return (x); 776*7c478bd9Sstevel@tonic-gate } 777*7c478bd9Sstevel@tonic-gate return (false); 778*7c478bd9Sstevel@tonic-gate } 779*7c478bd9Sstevel@tonic-gate 780*7c478bd9Sstevel@tonic-gate 781*7c478bd9Sstevel@tonic-gate 782*7c478bd9Sstevel@tonic-gate 783*7c478bd9Sstevel@tonic-gate CELL *aprintf(a, n) NODE **a; 784*7c478bd9Sstevel@tonic-gate { 785*7c478bd9Sstevel@tonic-gate register CELL *x; 786*7c478bd9Sstevel@tonic-gate 787*7c478bd9Sstevel@tonic-gate 788*7c478bd9Sstevel@tonic-gate 789*7c478bd9Sstevel@tonic-gate 790*7c478bd9Sstevel@tonic-gate x = asprintf(a, n); 791*7c478bd9Sstevel@tonic-gate if (a[1] == NULL) { 792*7c478bd9Sstevel@tonic-gate printf("%ws", x->sval); 793*7c478bd9Sstevel@tonic-gate tempfree(x); 794*7c478bd9Sstevel@tonic-gate return (true); 795*7c478bd9Sstevel@tonic-gate } 796*7c478bd9Sstevel@tonic-gate redirprint(x->sval, (int)a[1], a[2]); 797*7c478bd9Sstevel@tonic-gate return (x); 798*7c478bd9Sstevel@tonic-gate } 799*7c478bd9Sstevel@tonic-gate 800*7c478bd9Sstevel@tonic-gate 801*7c478bd9Sstevel@tonic-gate 802*7c478bd9Sstevel@tonic-gate 803*7c478bd9Sstevel@tonic-gate CELL *split(a, nnn) NODE **a; 804*7c478bd9Sstevel@tonic-gate { 805*7c478bd9Sstevel@tonic-gate register CELL *x; 806*7c478bd9Sstevel@tonic-gate register CELL *ap; 807*7c478bd9Sstevel@tonic-gate register wchar_t *s, *p, c; 808*7c478bd9Sstevel@tonic-gate wchar_t *t, temp, num[5]; 809*7c478bd9Sstevel@tonic-gate register wchar_t sep; 810*7c478bd9Sstevel@tonic-gate int n, flag; 811*7c478bd9Sstevel@tonic-gate 812*7c478bd9Sstevel@tonic-gate 813*7c478bd9Sstevel@tonic-gate 814*7c478bd9Sstevel@tonic-gate 815*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 816*7c478bd9Sstevel@tonic-gate s = getsval(x); 817*7c478bd9Sstevel@tonic-gate tempfree(x); 818*7c478bd9Sstevel@tonic-gate if (a[2] == 0) 819*7c478bd9Sstevel@tonic-gate sep = **FS; 820*7c478bd9Sstevel@tonic-gate else { 821*7c478bd9Sstevel@tonic-gate x = execute(a[2]); 822*7c478bd9Sstevel@tonic-gate sep = getsval(x)[0]; 823*7c478bd9Sstevel@tonic-gate tempfree(x); 824*7c478bd9Sstevel@tonic-gate } 825*7c478bd9Sstevel@tonic-gate ap = (CELL *) a[1]; 826*7c478bd9Sstevel@tonic-gate freesymtab(ap); 827*7c478bd9Sstevel@tonic-gate dprintf("split: s=|%ws|, a=%ws, sep=|%wc|\n", s, ap->nval, sep); 828*7c478bd9Sstevel@tonic-gate ap->tval &= ~STR; 829*7c478bd9Sstevel@tonic-gate ap->tval |= ARR; 830*7c478bd9Sstevel@tonic-gate ap->sval = (wchar_t *) makesymtab(); 831*7c478bd9Sstevel@tonic-gate 832*7c478bd9Sstevel@tonic-gate 833*7c478bd9Sstevel@tonic-gate 834*7c478bd9Sstevel@tonic-gate 835*7c478bd9Sstevel@tonic-gate n = 0; 836*7c478bd9Sstevel@tonic-gate if (sep == ' ') 837*7c478bd9Sstevel@tonic-gate for (n = 0; /* dummy */; /* dummy */) { 838*7c478bd9Sstevel@tonic-gate c = *s; 839*7c478bd9Sstevel@tonic-gate while (iswblank(c) || c == '\t' || c == '\n') 840*7c478bd9Sstevel@tonic-gate c = *(++s); 841*7c478bd9Sstevel@tonic-gate if (*s == 0) 842*7c478bd9Sstevel@tonic-gate break; 843*7c478bd9Sstevel@tonic-gate n++; 844*7c478bd9Sstevel@tonic-gate t = s; 845*7c478bd9Sstevel@tonic-gate do 846*7c478bd9Sstevel@tonic-gate c = *(++s); 847*7c478bd9Sstevel@tonic-gate while (! iswblank(c) && c != '\t' && 848*7c478bd9Sstevel@tonic-gate c != '\n' && c != '\0'); 849*7c478bd9Sstevel@tonic-gate temp = c; 850*7c478bd9Sstevel@tonic-gate *s = (wchar_t)0x0; 851*7c478bd9Sstevel@tonic-gate wsprintf(num, "%d", n); 852*7c478bd9Sstevel@tonic-gate if (isanumber(t)) 853*7c478bd9Sstevel@tonic-gate setsymtab(num, tostring(t), 854*7c478bd9Sstevel@tonic-gate watof(t), STR|NUM, ap->sval); 855*7c478bd9Sstevel@tonic-gate else 856*7c478bd9Sstevel@tonic-gate setsymtab(num, tostring(t), 0.0, STR, ap->sval); 857*7c478bd9Sstevel@tonic-gate *s = temp; 858*7c478bd9Sstevel@tonic-gate if (*s != 0) 859*7c478bd9Sstevel@tonic-gate s++; 860*7c478bd9Sstevel@tonic-gate 861*7c478bd9Sstevel@tonic-gate } else if (*s != 0) 862*7c478bd9Sstevel@tonic-gate for (;;) { 863*7c478bd9Sstevel@tonic-gate n++; 864*7c478bd9Sstevel@tonic-gate t = s; 865*7c478bd9Sstevel@tonic-gate while ((c = *s) != sep && c != '\n' && c != '\0') 866*7c478bd9Sstevel@tonic-gate s++; 867*7c478bd9Sstevel@tonic-gate temp = c; 868*7c478bd9Sstevel@tonic-gate *s = (wchar_t)0x0; 869*7c478bd9Sstevel@tonic-gate wsprintf(num, "%d", n); 870*7c478bd9Sstevel@tonic-gate if (isanumber(t)) 871*7c478bd9Sstevel@tonic-gate setsymtab(num, tostring(t), 872*7c478bd9Sstevel@tonic-gate watof(t), STR|NUM, ap->sval); 873*7c478bd9Sstevel@tonic-gate else 874*7c478bd9Sstevel@tonic-gate setsymtab(num, tostring(t), 0.0, STR, ap->sval); 875*7c478bd9Sstevel@tonic-gate *s = temp; 876*7c478bd9Sstevel@tonic-gate if (*s++ == 0) 877*7c478bd9Sstevel@tonic-gate break; 878*7c478bd9Sstevel@tonic-gate } 879*7c478bd9Sstevel@tonic-gate x = gettemp(); 880*7c478bd9Sstevel@tonic-gate x->tval = NUM; 881*7c478bd9Sstevel@tonic-gate x->fval = n; 882*7c478bd9Sstevel@tonic-gate return (x); 883*7c478bd9Sstevel@tonic-gate } 884*7c478bd9Sstevel@tonic-gate 885*7c478bd9Sstevel@tonic-gate 886*7c478bd9Sstevel@tonic-gate 887*7c478bd9Sstevel@tonic-gate 888*7c478bd9Sstevel@tonic-gate CELL *ifstat(a, n) NODE **a; 889*7c478bd9Sstevel@tonic-gate { 890*7c478bd9Sstevel@tonic-gate register CELL *x; 891*7c478bd9Sstevel@tonic-gate 892*7c478bd9Sstevel@tonic-gate 893*7c478bd9Sstevel@tonic-gate 894*7c478bd9Sstevel@tonic-gate 895*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 896*7c478bd9Sstevel@tonic-gate if (istrue(x)) { 897*7c478bd9Sstevel@tonic-gate tempfree(x); 898*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 899*7c478bd9Sstevel@tonic-gate 900*7c478bd9Sstevel@tonic-gate } else if (a[2] != 0) { 901*7c478bd9Sstevel@tonic-gate tempfree(x); 902*7c478bd9Sstevel@tonic-gate x = execute(a[2]); 903*7c478bd9Sstevel@tonic-gate } 904*7c478bd9Sstevel@tonic-gate return (x); 905*7c478bd9Sstevel@tonic-gate } 906*7c478bd9Sstevel@tonic-gate 907*7c478bd9Sstevel@tonic-gate 908*7c478bd9Sstevel@tonic-gate 909*7c478bd9Sstevel@tonic-gate 910*7c478bd9Sstevel@tonic-gate CELL *whilestat(a, n) NODE **a; 911*7c478bd9Sstevel@tonic-gate { 912*7c478bd9Sstevel@tonic-gate register CELL *x; 913*7c478bd9Sstevel@tonic-gate 914*7c478bd9Sstevel@tonic-gate 915*7c478bd9Sstevel@tonic-gate 916*7c478bd9Sstevel@tonic-gate 917*7c478bd9Sstevel@tonic-gate for (;;) { 918*7c478bd9Sstevel@tonic-gate x = execute(a[0]); 919*7c478bd9Sstevel@tonic-gate if (!istrue(x)) return (x); 920*7c478bd9Sstevel@tonic-gate tempfree(x); 921*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 922*7c478bd9Sstevel@tonic-gate if (isbreak(x)) { 923*7c478bd9Sstevel@tonic-gate x = true; 924*7c478bd9Sstevel@tonic-gate return (x); 925*7c478bd9Sstevel@tonic-gate } 926*7c478bd9Sstevel@tonic-gate if (isnext(x) || isexit(x)) 927*7c478bd9Sstevel@tonic-gate return (x); 928*7c478bd9Sstevel@tonic-gate tempfree(x); 929*7c478bd9Sstevel@tonic-gate } 930*7c478bd9Sstevel@tonic-gate } 931*7c478bd9Sstevel@tonic-gate 932*7c478bd9Sstevel@tonic-gate 933*7c478bd9Sstevel@tonic-gate 934*7c478bd9Sstevel@tonic-gate 935*7c478bd9Sstevel@tonic-gate CELL *forstat(a, n) NODE **a; 936*7c478bd9Sstevel@tonic-gate { 937*7c478bd9Sstevel@tonic-gate register CELL *x; 938*7c478bd9Sstevel@tonic-gate register CELL *z; 939*7c478bd9Sstevel@tonic-gate 940*7c478bd9Sstevel@tonic-gate 941*7c478bd9Sstevel@tonic-gate 942*7c478bd9Sstevel@tonic-gate 943*7c478bd9Sstevel@tonic-gate z = execute(a[0]); 944*7c478bd9Sstevel@tonic-gate tempfree(z); 945*7c478bd9Sstevel@tonic-gate for (;;) { 946*7c478bd9Sstevel@tonic-gate if (a[1]!=0) { 947*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 948*7c478bd9Sstevel@tonic-gate if (!istrue(x)) return (x); 949*7c478bd9Sstevel@tonic-gate else tempfree(x); 950*7c478bd9Sstevel@tonic-gate } 951*7c478bd9Sstevel@tonic-gate x = execute(a[3]); 952*7c478bd9Sstevel@tonic-gate if (isbreak(x)) { /* turn off break */ 953*7c478bd9Sstevel@tonic-gate x = true; 954*7c478bd9Sstevel@tonic-gate return (x); 955*7c478bd9Sstevel@tonic-gate } 956*7c478bd9Sstevel@tonic-gate if (isnext(x) || isexit(x)) 957*7c478bd9Sstevel@tonic-gate return (x); 958*7c478bd9Sstevel@tonic-gate tempfree(x); 959*7c478bd9Sstevel@tonic-gate z = execute(a[2]); 960*7c478bd9Sstevel@tonic-gate tempfree(z); 961*7c478bd9Sstevel@tonic-gate } 962*7c478bd9Sstevel@tonic-gate } 963*7c478bd9Sstevel@tonic-gate 964*7c478bd9Sstevel@tonic-gate 965*7c478bd9Sstevel@tonic-gate 966*7c478bd9Sstevel@tonic-gate 967*7c478bd9Sstevel@tonic-gate CELL *instat(a, n) NODE **a; 968*7c478bd9Sstevel@tonic-gate { 969*7c478bd9Sstevel@tonic-gate register CELL *vp, *arrayp, *cp, **tp; 970*7c478bd9Sstevel@tonic-gate register CELL *x; 971*7c478bd9Sstevel@tonic-gate int i; 972*7c478bd9Sstevel@tonic-gate 973*7c478bd9Sstevel@tonic-gate 974*7c478bd9Sstevel@tonic-gate 975*7c478bd9Sstevel@tonic-gate 976*7c478bd9Sstevel@tonic-gate vp = (CELL *) a[0]; 977*7c478bd9Sstevel@tonic-gate arrayp = (CELL *) a[1]; 978*7c478bd9Sstevel@tonic-gate if (!(arrayp->tval & ARR)) 979*7c478bd9Sstevel@tonic-gate error(FATAL, "%ws is not an array", arrayp->nval); 980*7c478bd9Sstevel@tonic-gate tp = (CELL **) arrayp->sval; 981*7c478bd9Sstevel@tonic-gate for (i = 0; i < MAXSYM; i++) { /* this routine knows too much */ 982*7c478bd9Sstevel@tonic-gate for (cp = tp[i]; cp != NULL; cp = cp->nextval) { 983*7c478bd9Sstevel@tonic-gate setsval(vp, cp->nval); 984*7c478bd9Sstevel@tonic-gate x = execute(a[2]); 985*7c478bd9Sstevel@tonic-gate if (isbreak(x)) { 986*7c478bd9Sstevel@tonic-gate x = true; 987*7c478bd9Sstevel@tonic-gate return (x); 988*7c478bd9Sstevel@tonic-gate } 989*7c478bd9Sstevel@tonic-gate if (isnext(x) || isexit(x)) 990*7c478bd9Sstevel@tonic-gate return (x); 991*7c478bd9Sstevel@tonic-gate tempfree(x); 992*7c478bd9Sstevel@tonic-gate } 993*7c478bd9Sstevel@tonic-gate } 994*7c478bd9Sstevel@tonic-gate return (true); 995*7c478bd9Sstevel@tonic-gate } 996*7c478bd9Sstevel@tonic-gate 997*7c478bd9Sstevel@tonic-gate 998*7c478bd9Sstevel@tonic-gate 999*7c478bd9Sstevel@tonic-gate 1000*7c478bd9Sstevel@tonic-gate CELL *jump(a, n) NODE **a; 1001*7c478bd9Sstevel@tonic-gate { 1002*7c478bd9Sstevel@tonic-gate register CELL *y; 1003*7c478bd9Sstevel@tonic-gate 1004*7c478bd9Sstevel@tonic-gate 1005*7c478bd9Sstevel@tonic-gate 1006*7c478bd9Sstevel@tonic-gate 1007*7c478bd9Sstevel@tonic-gate switch (n) { 1008*7c478bd9Sstevel@tonic-gate case EXIT: 1009*7c478bd9Sstevel@tonic-gate if (a[0] != 0) { 1010*7c478bd9Sstevel@tonic-gate y = execute(a[0]); 1011*7c478bd9Sstevel@tonic-gate errorflag = getfval(y); 1012*7c478bd9Sstevel@tonic-gate } 1013*7c478bd9Sstevel@tonic-gate return (jexit); 1014*7c478bd9Sstevel@tonic-gate case NEXT: 1015*7c478bd9Sstevel@tonic-gate return (jnext); 1016*7c478bd9Sstevel@tonic-gate case BREAK: 1017*7c478bd9Sstevel@tonic-gate return (jbreak); 1018*7c478bd9Sstevel@tonic-gate case CONTINUE: 1019*7c478bd9Sstevel@tonic-gate return (jcont); 1020*7c478bd9Sstevel@tonic-gate default: 1021*7c478bd9Sstevel@tonic-gate error(FATAL, "illegal jump type %d", n); 1022*7c478bd9Sstevel@tonic-gate } 1023*7c478bd9Sstevel@tonic-gate } 1024*7c478bd9Sstevel@tonic-gate 1025*7c478bd9Sstevel@tonic-gate 1026*7c478bd9Sstevel@tonic-gate 1027*7c478bd9Sstevel@tonic-gate 1028*7c478bd9Sstevel@tonic-gate CELL *fncn(a, n) NODE **a; 1029*7c478bd9Sstevel@tonic-gate { 1030*7c478bd9Sstevel@tonic-gate register CELL *x; 1031*7c478bd9Sstevel@tonic-gate awkfloat u; 1032*7c478bd9Sstevel@tonic-gate register int t; 1033*7c478bd9Sstevel@tonic-gate register wchar_t *wp; 1034*7c478bd9Sstevel@tonic-gate 1035*7c478bd9Sstevel@tonic-gate t = (int) a[0]; 1036*7c478bd9Sstevel@tonic-gate x = execute(a[1]); 1037*7c478bd9Sstevel@tonic-gate if (t == FLENGTH) 1038*7c478bd9Sstevel@tonic-gate u = (awkfloat) wslen(getsval(x)); 1039*7c478bd9Sstevel@tonic-gate else if (t == FLOG) 1040*7c478bd9Sstevel@tonic-gate u = log(getfval(x)); 1041*7c478bd9Sstevel@tonic-gate else if (t == FINT) 1042*7c478bd9Sstevel@tonic-gate u = (awkfloat) (long) getfval(x); 1043*7c478bd9Sstevel@tonic-gate else if (t == FEXP) 1044*7c478bd9Sstevel@tonic-gate u = exp(getfval(x)); 1045*7c478bd9Sstevel@tonic-gate else if (t == FSQRT) 1046*7c478bd9Sstevel@tonic-gate u = sqrt(getfval(x)); 1047*7c478bd9Sstevel@tonic-gate else 1048*7c478bd9Sstevel@tonic-gate error(FATAL, "illegal function type %d", t); 1049*7c478bd9Sstevel@tonic-gate tempfree(x); 1050*7c478bd9Sstevel@tonic-gate x = gettemp(); 1051*7c478bd9Sstevel@tonic-gate setfval(x, u); 1052*7c478bd9Sstevel@tonic-gate return (x); 1053*7c478bd9Sstevel@tonic-gate } 1054*7c478bd9Sstevel@tonic-gate 1055*7c478bd9Sstevel@tonic-gate 1056*7c478bd9Sstevel@tonic-gate 1057*7c478bd9Sstevel@tonic-gate 1058*7c478bd9Sstevel@tonic-gate CELL *print(a, n) NODE **a; 1059*7c478bd9Sstevel@tonic-gate { 1060*7c478bd9Sstevel@tonic-gate register NODE *x; 1061*7c478bd9Sstevel@tonic-gate register CELL *y; 1062*7c478bd9Sstevel@tonic-gate wchar_t s[RECSIZE]; 1063*7c478bd9Sstevel@tonic-gate wchar_t *ss, *bp, *ep; 1064*7c478bd9Sstevel@tonic-gate 1065*7c478bd9Sstevel@tonic-gate s[0] = '\0'; 1066*7c478bd9Sstevel@tonic-gate bp = s; 1067*7c478bd9Sstevel@tonic-gate ep = s + RECSIZE; 1068*7c478bd9Sstevel@tonic-gate for (x=a[0]; x!=NULL; x=x->nnext) { 1069*7c478bd9Sstevel@tonic-gate y = execute(x); 1070*7c478bd9Sstevel@tonic-gate ss = getsval(y); 1071*7c478bd9Sstevel@tonic-gate /* allocate larger buffer if needed */ 1072*7c478bd9Sstevel@tonic-gate if (ep < bp + wslen(bp) + wslen(ss)) { 1073*7c478bd9Sstevel@tonic-gate int newlen; 1074*7c478bd9Sstevel@tonic-gate wchar_t *oldbp; 1075*7c478bd9Sstevel@tonic-gate 1076*7c478bd9Sstevel@tonic-gate newlen = wslen(bp) + wslen(ss) + 1 + 1; 1077*7c478bd9Sstevel@tonic-gate oldbp = bp; 1078*7c478bd9Sstevel@tonic-gate bp = malloc(newlen * sizeof(wchar_t)); 1079*7c478bd9Sstevel@tonic-gate if (bp == NULL) 1080*7c478bd9Sstevel@tonic-gate error(FATAL, "out of space in print"); 1081*7c478bd9Sstevel@tonic-gate ep = bp + newlen; 1082*7c478bd9Sstevel@tonic-gate wscpy(bp, oldbp); 1083*7c478bd9Sstevel@tonic-gate if (oldbp != s) 1084*7c478bd9Sstevel@tonic-gate free(oldbp); 1085*7c478bd9Sstevel@tonic-gate } 1086*7c478bd9Sstevel@tonic-gate if (ss) 1087*7c478bd9Sstevel@tonic-gate wscat(bp, ss); 1088*7c478bd9Sstevel@tonic-gate tempfree(y); 1089*7c478bd9Sstevel@tonic-gate if (x->nnext == NULL) 1090*7c478bd9Sstevel@tonic-gate wscat(bp, *ORS); 1091*7c478bd9Sstevel@tonic-gate else 1092*7c478bd9Sstevel@tonic-gate wscat(bp, *OFS); 1093*7c478bd9Sstevel@tonic-gate } 1094*7c478bd9Sstevel@tonic-gate if (a[1] == 0) { 1095*7c478bd9Sstevel@tonic-gate printf("%ws", bp); 1096*7c478bd9Sstevel@tonic-gate if (bp != s) 1097*7c478bd9Sstevel@tonic-gate free(bp); 1098*7c478bd9Sstevel@tonic-gate return (true); 1099*7c478bd9Sstevel@tonic-gate } 1100*7c478bd9Sstevel@tonic-gate 1101*7c478bd9Sstevel@tonic-gate redirprint(bp, (int)a[1], a[2]); 1102*7c478bd9Sstevel@tonic-gate if (bp != s) 1103*7c478bd9Sstevel@tonic-gate free(bp); 1104*7c478bd9Sstevel@tonic-gate return (false); 1105*7c478bd9Sstevel@tonic-gate } 1106*7c478bd9Sstevel@tonic-gate 1107*7c478bd9Sstevel@tonic-gate 1108*7c478bd9Sstevel@tonic-gate 1109*7c478bd9Sstevel@tonic-gate 1110*7c478bd9Sstevel@tonic-gate CELL *nullproc() {} 1111*7c478bd9Sstevel@tonic-gate 1112*7c478bd9Sstevel@tonic-gate 1113*7c478bd9Sstevel@tonic-gate 1114*7c478bd9Sstevel@tonic-gate 1115*7c478bd9Sstevel@tonic-gate CELL *nodetoobj(a) NODE *a; 1116*7c478bd9Sstevel@tonic-gate { 1117*7c478bd9Sstevel@tonic-gate register CELL *x; 1118*7c478bd9Sstevel@tonic-gate 1119*7c478bd9Sstevel@tonic-gate x= (CELL *) a->nobj; 1120*7c478bd9Sstevel@tonic-gate x->ctype = OCELL; 1121*7c478bd9Sstevel@tonic-gate x->csub = a->subtype; 1122*7c478bd9Sstevel@tonic-gate if (isfld(x)) 1123*7c478bd9Sstevel@tonic-gate fldbld(); 1124*7c478bd9Sstevel@tonic-gate return (x); 1125*7c478bd9Sstevel@tonic-gate } 1126*7c478bd9Sstevel@tonic-gate 1127*7c478bd9Sstevel@tonic-gate redirprint(s, a, b) wchar_t *s; NODE *b; 1128*7c478bd9Sstevel@tonic-gate { 1129*7c478bd9Sstevel@tonic-gate register int i; 1130*7c478bd9Sstevel@tonic-gate register CELL *x; 1131*7c478bd9Sstevel@tonic-gate 1132*7c478bd9Sstevel@tonic-gate x = execute(b); 1133*7c478bd9Sstevel@tonic-gate getsval(x); 1134*7c478bd9Sstevel@tonic-gate for (i=0; i<FILENUM; i++) 1135*7c478bd9Sstevel@tonic-gate if (files[i].fname && wscmp(x->sval, files[i].fname) == 0) 1136*7c478bd9Sstevel@tonic-gate goto doit; 1137*7c478bd9Sstevel@tonic-gate for (i=0; i<FILENUM; i++) 1138*7c478bd9Sstevel@tonic-gate if (files[i].fp == 0) 1139*7c478bd9Sstevel@tonic-gate break; 1140*7c478bd9Sstevel@tonic-gate if (i >= FILENUM) 1141*7c478bd9Sstevel@tonic-gate error(FATAL, "too many output files %d", i); 1142*7c478bd9Sstevel@tonic-gate if (a == '|') /* a pipe! */ 1143*7c478bd9Sstevel@tonic-gate files[i].fp = popen(toeuccode(x->sval), "w"); 1144*7c478bd9Sstevel@tonic-gate else if (a == APPEND) 1145*7c478bd9Sstevel@tonic-gate files[i].fp = fopen(toeuccode(x->sval), "a"); 1146*7c478bd9Sstevel@tonic-gate else if (a == GT) 1147*7c478bd9Sstevel@tonic-gate files[i].fp = fopen(toeuccode(x->sval), "w"); 1148*7c478bd9Sstevel@tonic-gate else 1149*7c478bd9Sstevel@tonic-gate error(FATAL, "illegal redirection near line %lld", lineno); 1150*7c478bd9Sstevel@tonic-gate if (files[i].fp == NULL) 1151*7c478bd9Sstevel@tonic-gate error(FATAL, "can't open file %ws", x->sval); 1152*7c478bd9Sstevel@tonic-gate files[i].fname = tostring(x->sval); 1153*7c478bd9Sstevel@tonic-gate files[i].type = a; 1154*7c478bd9Sstevel@tonic-gate doit: 1155*7c478bd9Sstevel@tonic-gate fprintf(files[i].fp, "%ws", s); 1156*7c478bd9Sstevel@tonic-gate #ifndef gcos 1157*7c478bd9Sstevel@tonic-gate fflush(files[i].fp); /* in case someone is waiting for the output */ 1158*7c478bd9Sstevel@tonic-gate #endif 1159*7c478bd9Sstevel@tonic-gate tempfree(x); 1160*7c478bd9Sstevel@tonic-gate } 1161