run.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. /****************************************************************
  2. Copyright (C) Lucent Technologies 1997
  3. All Rights Reserved
  4. Permission to use, copy, modify, and distribute this software and
  5. its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the name Lucent Technologies or any of
  10. its entities not be used in advertising or publicity pertaining
  11. to distribution of the software without specific, written prior
  12. permission.
  13. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  14. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  15. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  16. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  18. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  20. THIS SOFTWARE.
  21. ****************************************************************/
  22. #define DEBUG
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <setjmp.h>
  26. #include <math.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <time.h>
  30. #include <utf.h>
  31. #include "awk.h"
  32. #include "y.tab.h"
  33. #define tempfree(x) if (istemp(x)) tfree(x); else
  34. /*
  35. #undef tempfree
  36. void tempfree(Cell *p) {
  37. if (p->ctype == OCELL && (p->csub < CUNK || p->csub > CFREE)) {
  38. WARNING("bad csub %d in Cell %d %s",
  39. p->csub, p->ctype, p->sval);
  40. }
  41. if (istemp(p))
  42. tfree(p);
  43. }
  44. */
  45. #ifdef _NFILE
  46. #ifndef FOPEN_MAX
  47. #define FOPEN_MAX _NFILE
  48. #endif
  49. #endif
  50. #ifndef FOPEN_MAX
  51. #define FOPEN_MAX 40 /* max number of open files */
  52. #endif
  53. #ifndef RAND_MAX
  54. #define RAND_MAX 32767 /* all that ansi guarantees */
  55. #endif
  56. jmp_buf env;
  57. extern int pairstack[];
  58. Node *winner = NULL; /* root of parse tree */
  59. Cell *tmps; /* free temporary cells for execution */
  60. static Cell truecell ={ OBOOL, BTRUE, 0, 0, 1.0, NUM };
  61. Cell *True = &truecell;
  62. static Cell falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM };
  63. Cell *False = &falsecell;
  64. static Cell breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM };
  65. Cell *jbreak = &breakcell;
  66. static Cell contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM };
  67. Cell *jcont = &contcell;
  68. static Cell nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM };
  69. Cell *jnext = &nextcell;
  70. static Cell nextfilecell ={ OJUMP, JNEXTFILE, 0, 0, 0.0, NUM };
  71. Cell *jnextfile = &nextfilecell;
  72. static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM };
  73. Cell *jexit = &exitcell;
  74. static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM };
  75. Cell *jret = &retcell;
  76. static Cell tempcell ={ OCELL, CTEMP, 0, "", 0.0, NUM|STR|DONTFREE };
  77. Node *curnode = NULL; /* the node being executed, for debugging */
  78. /* buffer memory management */
  79. int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
  80. char *whatrtn)
  81. /* pbuf: address of pointer to buffer being managed
  82. * psiz: address of buffer size variable
  83. * minlen: minimum length of buffer needed
  84. * quantum: buffer size quantum
  85. * pbptr: address of movable pointer into buffer, or 0 if none
  86. * whatrtn: name of the calling routine if failure should cause fatal error
  87. *
  88. * return 0 for realloc failure, !=0 for success
  89. */
  90. {
  91. if (minlen > *psiz) {
  92. char *tbuf;
  93. int rminlen = quantum ? minlen % quantum : 0;
  94. int boff = pbptr ? *pbptr - *pbuf : 0;
  95. /* round up to next multiple of quantum */
  96. if (rminlen)
  97. minlen += quantum - rminlen;
  98. tbuf = (char *) realloc(*pbuf, minlen);
  99. if (tbuf == NULL) {
  100. if (whatrtn)
  101. FATAL("out of memory in %s", whatrtn);
  102. return 0;
  103. }
  104. *pbuf = tbuf;
  105. *psiz = minlen;
  106. if (pbptr)
  107. *pbptr = tbuf + boff;
  108. }
  109. return 1;
  110. }
  111. void run(Node *a) /* execution of parse tree starts here */
  112. {
  113. extern void stdinit(void);
  114. stdinit();
  115. execute(a);
  116. closeall();
  117. }
  118. Cell *execute(Node *u) /* execute a node of the parse tree */
  119. {
  120. int nobj;
  121. Cell *(*proc)(Node **, int);
  122. Cell *x;
  123. Node *a;
  124. if (u == NULL)
  125. return(True);
  126. for (a = u; ; a = a->nnext) {
  127. curnode = a;
  128. if (isvalue(a)) {
  129. x = (Cell *) (a->narg[0]);
  130. if (isfld(x) && !donefld)
  131. fldbld();
  132. else if (isrec(x) && !donerec)
  133. recbld();
  134. return(x);
  135. }
  136. nobj = a->nobj;
  137. if (notlegal(nobj)) /* probably a Cell* but too risky to print */
  138. FATAL("illegal statement");
  139. proc = proctab[nobj-FIRSTTOKEN];
  140. x = (*proc)(a->narg, nobj);
  141. if (isfld(x) && !donefld)
  142. fldbld();
  143. else if (isrec(x) && !donerec)
  144. recbld();
  145. if (isexpr(a))
  146. return(x);
  147. if (isjump(x))
  148. return(x);
  149. if (a->nnext == NULL)
  150. return(x);
  151. tempfree(x);
  152. }
  153. }
  154. Cell *program(Node **a, int n) /* execute an awk program */
  155. { /* a[0] = BEGIN, a[1] = body, a[2] = END */
  156. Cell *x;
  157. if (setjmp(env) != 0)
  158. goto ex;
  159. if (a[0]) { /* BEGIN */
  160. x = execute(a[0]);
  161. if (isexit(x))
  162. return(True);
  163. if (isjump(x))
  164. FATAL("illegal break, continue, next or nextfile from BEGIN");
  165. tempfree(x);
  166. }
  167. if (a[1] || a[2])
  168. while (getrec(&record, &recsize, 1) > 0) {
  169. x = execute(a[1]);
  170. if (isexit(x))
  171. break;
  172. tempfree(x);
  173. }
  174. ex:
  175. if (setjmp(env) != 0) /* handles exit within END */
  176. goto ex1;
  177. if (a[2]) { /* END */
  178. x = execute(a[2]);
  179. if (isbreak(x) || isnext(x) || iscont(x))
  180. FATAL("illegal break, continue, next or nextfile from END");
  181. tempfree(x);
  182. }
  183. ex1:
  184. return(True);
  185. }
  186. struct Frame { /* stack frame for awk function calls */
  187. int nargs; /* number of arguments in this call */
  188. Cell *fcncell; /* pointer to Cell for function */
  189. Cell **args; /* pointer to array of arguments after execute */
  190. Cell *retval; /* return value */
  191. };
  192. #define NARGS 50 /* max args in a call */
  193. struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */
  194. int nframe = 0; /* number of frames allocated */
  195. struct Frame *fp = NULL; /* frame pointer. bottom level unused */
  196. Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
  197. {
  198. static Cell newcopycell = { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE };
  199. int i, ncall, ndef;
  200. Node *x;
  201. Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */
  202. Cell *y, *z, *fcn;
  203. char *s;
  204. fcn = execute(a[0]); /* the function itself */
  205. s = fcn->nval;
  206. if (!isfcn(fcn))
  207. FATAL("calling undefined function %s", s);
  208. if (frame == NULL) {
  209. fp = frame = (struct Frame *) calloc(nframe += 100, sizeof(struct Frame));
  210. if (frame == NULL)
  211. FATAL("out of space for stack frames calling %s", s);
  212. }
  213. for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */
  214. ncall++;
  215. ndef = (int) fcn->fval; /* args in defn */
  216. dprintf( ("calling %s, %d args (%d in defn), fp=%d\n", s, ncall, ndef, (int) (fp-frame)) );
  217. if (ncall > ndef)
  218. WARNING("function %s called with %d args, uses only %d",
  219. s, ncall, ndef);
  220. if (ncall + ndef > NARGS)
  221. FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS);
  222. for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */
  223. dprintf( ("evaluate args[%d], fp=%d:\n", i, (int) (fp-frame)) );
  224. y = execute(x);
  225. oargs[i] = y;
  226. dprintf( ("args[%d]: %s %f <%s>, t=%o\n",
  227. i, y->nval, y->fval, isarr(y) ? "(array)" : y->sval, y->tval) );
  228. if (isfcn(y))
  229. FATAL("can't use function %s as argument in %s", y->nval, s);
  230. if (isarr(y))
  231. args[i] = y; /* arrays by ref */
  232. else
  233. args[i] = copycell(y);
  234. tempfree(y);
  235. }
  236. for ( ; i < ndef; i++) { /* add null args for ones not provided */
  237. args[i] = gettemp();
  238. *args[i] = newcopycell;
  239. }
  240. fp++; /* now ok to up frame */
  241. if (fp >= frame + nframe) {
  242. int dfp = fp - frame; /* old index */
  243. frame = (struct Frame *)
  244. realloc((char *) frame, (nframe += 100) * sizeof(struct Frame));
  245. if (frame == NULL)
  246. FATAL("out of space for stack frames in %s", s);
  247. fp = frame + dfp;
  248. }
  249. fp->fcncell = fcn;
  250. fp->args = args;
  251. fp->nargs = ndef; /* number defined with (excess are locals) */
  252. fp->retval = gettemp();
  253. dprintf( ("start exec of %s, fp=%d\n", s, (int) (fp-frame)) );
  254. y = execute((Node *)(fcn->sval)); /* execute body */
  255. dprintf( ("finished exec of %s, fp=%d\n", s, (int) (fp-frame)) );
  256. for (i = 0; i < ndef; i++) {
  257. Cell *t = fp->args[i];
  258. if (isarr(t)) {
  259. if (t->csub == CCOPY) {
  260. if (i >= ncall) {
  261. freesymtab(t);
  262. t->csub = CTEMP;
  263. tempfree(t);
  264. } else {
  265. oargs[i]->tval = t->tval;
  266. oargs[i]->tval &= ~(STR|NUM|DONTFREE);
  267. oargs[i]->sval = t->sval;
  268. tempfree(t);
  269. }
  270. }
  271. } else if (t != y) { /* kludge to prevent freeing twice */
  272. t->csub = CTEMP;
  273. tempfree(t);
  274. }
  275. }
  276. tempfree(fcn);
  277. if (isexit(y) || isnext(y) || isnextfile(y))
  278. return y;
  279. tempfree(y); /* this can free twice! */
  280. z = fp->retval; /* return value */
  281. dprintf( ("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval) );
  282. fp--;
  283. return(z);
  284. }
  285. Cell *copycell(Cell *x) /* make a copy of a cell in a temp */
  286. {
  287. Cell *y;
  288. y = gettemp();
  289. y->csub = CCOPY; /* prevents freeing until call is over */
  290. y->nval = x->nval; /* BUG? */
  291. y->sval = x->sval ? tostring(x->sval) : NULL;
  292. y->fval = x->fval;
  293. y->tval = x->tval & ~(CON|FLD|REC|DONTFREE); /* copy is not constant or field */
  294. /* is DONTFREE right? */
  295. return y;
  296. }
  297. Cell *arg(Node **a, int n) /* nth argument of a function */
  298. {
  299. n = ptoi(a[0]); /* argument number, counting from 0 */
  300. dprintf( ("arg(%d), fp->nargs=%d\n", n, fp->nargs) );
  301. if (n+1 > fp->nargs)
  302. FATAL("argument #%d of function %s was not supplied",
  303. n+1, fp->fcncell->nval);
  304. return fp->args[n];
  305. }
  306. Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */
  307. {
  308. Cell *y;
  309. switch (n) {
  310. case EXIT:
  311. if (a[0] != NULL) {
  312. y = execute(a[0]);
  313. errorflag = (int) getfval(y);
  314. tempfree(y);
  315. }
  316. longjmp(env, 1);
  317. case RETURN:
  318. if (a[0] != NULL) {
  319. y = execute(a[0]);
  320. if ((y->tval & (STR|NUM)) == (STR|NUM)) {
  321. setsval(fp->retval, getsval(y));
  322. fp->retval->fval = getfval(y);
  323. fp->retval->tval |= NUM;
  324. }
  325. else if (y->tval & STR)
  326. setsval(fp->retval, getsval(y));
  327. else if (y->tval & NUM)
  328. setfval(fp->retval, getfval(y));
  329. else /* can't happen */
  330. FATAL("bad type variable %d", y->tval);
  331. tempfree(y);
  332. }
  333. return(jret);
  334. case NEXT:
  335. return(jnext);
  336. case NEXTFILE:
  337. nextfile();
  338. return(jnextfile);
  339. case BREAK:
  340. return(jbreak);
  341. case CONTINUE:
  342. return(jcont);
  343. default: /* can't happen */
  344. FATAL("illegal jump type %d", n);
  345. }
  346. return 0; /* not reached */
  347. }
  348. Cell *getline(Node **a, int n) /* get next line from specific input */
  349. { /* a[0] is variable, a[1] is operator, a[2] is filename */
  350. Cell *r, *x;
  351. extern Cell **fldtab;
  352. FILE *fp;
  353. char *buf;
  354. int bufsize = recsize;
  355. int mode;
  356. if ((buf = (char *) malloc(bufsize)) == NULL)
  357. FATAL("out of memory in getline");
  358. fflush(stdout); /* in case someone is waiting for a prompt */
  359. r = gettemp();
  360. if (a[1] != NULL) { /* getline < file */
  361. x = execute(a[2]); /* filename */
  362. mode = ptoi(a[1]);
  363. if (mode == '|') /* input pipe */
  364. mode = LE; /* arbitrary flag */
  365. fp = openfile(mode, getsval(x));
  366. tempfree(x);
  367. if (fp == NULL)
  368. n = -1;
  369. else
  370. n = readrec(&buf, &bufsize, fp);
  371. if (n <= 0) {
  372. ;
  373. } else if (a[0] != NULL) { /* getline var <file */
  374. x = execute(a[0]);
  375. setsval(x, buf);
  376. tempfree(x);
  377. } else { /* getline <file */
  378. setsval(fldtab[0], buf);
  379. if (is_number(fldtab[0]->sval)) {
  380. fldtab[0]->fval = atof(fldtab[0]->sval);
  381. fldtab[0]->tval |= NUM;
  382. }
  383. }
  384. } else { /* bare getline; use current input */
  385. if (a[0] == NULL) /* getline */
  386. n = getrec(&record, &recsize, 1);
  387. else { /* getline var */
  388. n = getrec(&buf, &bufsize, 0);
  389. x = execute(a[0]);
  390. setsval(x, buf);
  391. tempfree(x);
  392. }
  393. }
  394. setfval(r, (Awkfloat) n);
  395. free(buf);
  396. return r;
  397. }
  398. Cell *getnf(Node **a, int n) /* get NF */
  399. {
  400. if (donefld == 0)
  401. fldbld();
  402. return (Cell *) a[0];
  403. }
  404. Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
  405. {
  406. Cell *x, *y, *z;
  407. char *s;
  408. Node *np;
  409. char *buf;
  410. int bufsz = recsize;
  411. int nsub = strlen(*SUBSEP);
  412. if ((buf = (char *) malloc(bufsz)) == NULL)
  413. FATAL("out of memory in array");
  414. x = execute(a[0]); /* Cell* for symbol table */
  415. buf[0] = 0;
  416. for (np = a[1]; np; np = np->nnext) {
  417. y = execute(np); /* subscript */
  418. s = getsval(y);
  419. if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, 0))
  420. FATAL("out of memory for %s[%s...]", x->nval, buf);
  421. strcat(buf, s);
  422. if (np->nnext)
  423. strcat(buf, *SUBSEP);
  424. tempfree(y);
  425. }
  426. if (!isarr(x)) {
  427. dprintf( ("making %s into an array\n", x->nval) );
  428. if (freeable(x))
  429. xfree(x->sval);
  430. x->tval &= ~(STR|NUM|DONTFREE);
  431. x->tval |= ARR;
  432. x->sval = (char *) makesymtab(NSYMTAB);
  433. }
  434. z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval);
  435. z->ctype = OCELL;
  436. z->csub = CVAR;
  437. tempfree(x);
  438. free(buf);
  439. return(z);
  440. }
  441. Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
  442. {
  443. Cell *x, *y;
  444. Node *np;
  445. char *s;
  446. int nsub = strlen(*SUBSEP);
  447. x = execute(a[0]); /* Cell* for symbol table */
  448. if (!isarr(x))
  449. return True;
  450. if (a[1] == 0) { /* delete the elements, not the table */
  451. freesymtab(x);
  452. x->tval &= ~STR;
  453. x->tval |= ARR;
  454. x->sval = (char *) makesymtab(NSYMTAB);
  455. } else {
  456. int bufsz = recsize;
  457. char *buf;
  458. if ((buf = (char *) malloc(bufsz)) == NULL)
  459. FATAL("out of memory in adelete");
  460. buf[0] = 0;
  461. for (np = a[1]; np; np = np->nnext) {
  462. y = execute(np); /* subscript */
  463. s = getsval(y);
  464. if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, 0))
  465. FATAL("out of memory deleting %s[%s...]", x->nval, buf);
  466. strcat(buf, s);
  467. if (np->nnext)
  468. strcat(buf, *SUBSEP);
  469. tempfree(y);
  470. }
  471. freeelem(x, buf);
  472. free(buf);
  473. }
  474. tempfree(x);
  475. return True;
  476. }
  477. Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */
  478. {
  479. Cell *x, *ap, *k;
  480. Node *p;
  481. char *buf;
  482. char *s;
  483. int bufsz = recsize;
  484. int nsub = strlen(*SUBSEP);
  485. ap = execute(a[1]); /* array name */
  486. if (!isarr(ap)) {
  487. dprintf( ("making %s into an array\n", ap->nval) );
  488. if (freeable(ap))
  489. xfree(ap->sval);
  490. ap->tval &= ~(STR|NUM|DONTFREE);
  491. ap->tval |= ARR;
  492. ap->sval = (char *) makesymtab(NSYMTAB);
  493. }
  494. if ((buf = (char *) malloc(bufsz)) == NULL) {
  495. FATAL("out of memory in intest");
  496. }
  497. buf[0] = 0;
  498. for (p = a[0]; p; p = p->nnext) {
  499. x = execute(p); /* expr */
  500. s = getsval(x);
  501. if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, 0))
  502. FATAL("out of memory deleting %s[%s...]", x->nval, buf);
  503. strcat(buf, s);
  504. tempfree(x);
  505. if (p->nnext)
  506. strcat(buf, *SUBSEP);
  507. }
  508. k = lookup(buf, (Array *) ap->sval);
  509. tempfree(ap);
  510. free(buf);
  511. if (k == NULL)
  512. return(False);
  513. else
  514. return(True);
  515. }
  516. Cell *matchop(Node **a, int n) /* ~ and match() */
  517. {
  518. Cell *x, *y;
  519. char *s, *t;
  520. int i;
  521. void *p;
  522. x = execute(a[1]); /* a[1] = target text */
  523. s = getsval(x);
  524. if (a[0] == 0) /* a[1] == 0: already-compiled reg expr */
  525. p = (void *) a[2];
  526. else {
  527. y = execute(a[2]); /* a[2] = regular expr */
  528. t = getsval(y);
  529. p = compre(t);
  530. tempfree(y);
  531. }
  532. if (n == MATCHFCN)
  533. i = pmatch(p, s, s);
  534. else
  535. i = match(p, s, s);
  536. tempfree(x);
  537. if (n == MATCHFCN) {
  538. int start = countposn(s, patbeg-s)+1;
  539. if (patlen < 0)
  540. start = 0;
  541. setfval(rstartloc, (Awkfloat) start);
  542. setfval(rlengthloc, (Awkfloat) countposn(patbeg, patlen));
  543. x = gettemp();
  544. x->tval = NUM;
  545. x->fval = start;
  546. return x;
  547. } else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0))
  548. return(True);
  549. else
  550. return(False);
  551. }
  552. Cell *boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */
  553. {
  554. Cell *x, *y;
  555. int i;
  556. x = execute(a[0]);
  557. i = istrue(x);
  558. tempfree(x);
  559. switch (n) {
  560. case BOR:
  561. if (i) return(True);
  562. y = execute(a[1]);
  563. i = istrue(y);
  564. tempfree(y);
  565. if (i) return(True);
  566. else return(False);
  567. case AND:
  568. if ( !i ) return(False);
  569. y = execute(a[1]);
  570. i = istrue(y);
  571. tempfree(y);
  572. if (i) return(True);
  573. else return(False);
  574. case NOT:
  575. if (i) return(False);
  576. else return(True);
  577. default: /* can't happen */
  578. FATAL("unknown boolean operator %d", n);
  579. }
  580. return 0; /*NOTREACHED*/
  581. }
  582. Cell *relop(Node **a, int n) /* a[0 < a[1], etc. */
  583. {
  584. int i;
  585. Cell *x, *y;
  586. Awkfloat j;
  587. x = execute(a[0]);
  588. y = execute(a[1]);
  589. if (x->tval&NUM && y->tval&NUM) {
  590. j = x->fval - y->fval;
  591. i = j<0? -1: (j>0? 1: 0);
  592. } else {
  593. i = strcmp(getsval(x), getsval(y));
  594. }
  595. tempfree(x);
  596. tempfree(y);
  597. switch (n) {
  598. case LT: if (i<0) return(True);
  599. else return(False);
  600. case LE: if (i<=0) return(True);
  601. else return(False);
  602. case NE: if (i!=0) return(True);
  603. else return(False);
  604. case EQ: if (i == 0) return(True);
  605. else return(False);
  606. case GE: if (i>=0) return(True);
  607. else return(False);
  608. case GT: if (i>0) return(True);
  609. else return(False);
  610. default: /* can't happen */
  611. FATAL("unknown relational operator %d", n);
  612. }
  613. return 0; /*NOTREACHED*/
  614. }
  615. void tfree(Cell *a) /* free a tempcell */
  616. {
  617. if (freeable(a)) {
  618. dprintf( ("freeing %s %s %o\n", a->nval, a->sval, a->tval) );
  619. xfree(a->sval);
  620. }
  621. if (a == tmps)
  622. FATAL("tempcell list is curdled");
  623. a->cnext = tmps;
  624. tmps = a;
  625. }
  626. Cell *gettemp(void) /* get a tempcell */
  627. { int i;
  628. Cell *x;
  629. if (!tmps) {
  630. tmps = (Cell *) calloc(100, sizeof(Cell));
  631. if (!tmps)
  632. FATAL("out of space for temporaries");
  633. for(i = 1; i < 100; i++)
  634. tmps[i-1].cnext = &tmps[i];
  635. tmps[i-1].cnext = 0;
  636. }
  637. x = tmps;
  638. tmps = x->cnext;
  639. *x = tempcell;
  640. return(x);
  641. }
  642. Cell *indirect(Node **a, int n) /* $( a[0] ) */
  643. {
  644. Cell *x;
  645. int m;
  646. char *s;
  647. x = execute(a[0]);
  648. m = (int) getfval(x);
  649. if (m == 0 && !is_number(s = getsval(x))) /* suspicion! */
  650. FATAL("illegal field $(%s), name \"%s\"", s, x->nval);
  651. /* BUG: can x->nval ever be null??? */
  652. tempfree(x);
  653. x = fieldadr(m);
  654. x->ctype = OCELL; /* BUG? why are these needed? */
  655. x->csub = CFLD;
  656. return(x);
  657. }
  658. Cell *substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */
  659. {
  660. int k, m, n;
  661. char *s, *p;
  662. int temp;
  663. Cell *x, *y, *z = 0;
  664. x = execute(a[0]);
  665. y = execute(a[1]);
  666. if (a[2] != 0)
  667. z = execute(a[2]);
  668. s = getsval(x);
  669. k = countposn(s, strlen(s)) + 1;
  670. if (k <= 1) {
  671. tempfree(x);
  672. tempfree(y);
  673. if (a[2] != 0)
  674. tempfree(z);
  675. x = gettemp();
  676. setsval(x, "");
  677. return(x);
  678. }
  679. m = (int) getfval(y);
  680. if (m <= 0)
  681. m = 1;
  682. else if (m > k)
  683. m = k;
  684. tempfree(y);
  685. if (a[2] != 0) {
  686. n = (int) getfval(z);
  687. tempfree(z);
  688. } else
  689. n = k - 1;
  690. if (n < 0)
  691. n = 0;
  692. else if (n > k - m)
  693. n = k - m;
  694. dprintf( ("substr: m=%d, n=%d, s=%s\n", m, n, s) );
  695. y = gettemp();
  696. while (*s && --m)
  697. s += mblen(s, k);
  698. for (p = s; *p && n--; p += mblen(p, k))
  699. ;
  700. temp = *p; /* with thanks to John Linderman */
  701. *p = '\0';
  702. setsval(y, s);
  703. *p = temp;
  704. tempfree(x);
  705. return(y);
  706. }
  707. Cell *sindex(Node **a, int nnn) /* index(a[0], a[1]) */
  708. {
  709. Cell *x, *y, *z;
  710. char *s1, *s2, *p1, *p2, *q;
  711. Awkfloat v = 0.0;
  712. x = execute(a[0]);
  713. s1 = getsval(x);
  714. y = execute(a[1]);
  715. s2 = getsval(y);
  716. z = gettemp();
  717. for (p1 = s1; *p1 != '\0'; p1++) {
  718. for (q=p1, p2=s2; *p2 != '\0' && *q == *p2; q++, p2++)
  719. ;
  720. if (*p2 == '\0') {
  721. v = (Awkfloat) countposn(s1, p1-s1) + 1; /* origin 1 */
  722. break;
  723. }
  724. }
  725. tempfree(x);
  726. tempfree(y);
  727. setfval(z, v);
  728. return(z);
  729. }
  730. #define MAXNUMSIZE 50
  731. int format(char **pbuf, int *pbufsize, char *s, Node *a) /* printf-like conversions */
  732. {
  733. char *fmt;
  734. char *p, *t, *os;
  735. Cell *x;
  736. int flag = 0, n;
  737. int fmtwd; /* format width */
  738. int fmtsz = recsize;
  739. char *buf = *pbuf;
  740. int bufsize = *pbufsize;
  741. os = s;
  742. p = buf;
  743. if ((fmt = (char *) malloc(fmtsz)) == NULL)
  744. FATAL("out of memory in format()");
  745. while (*s) {
  746. adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format");
  747. if (*s != '%') {
  748. *p++ = *s++;
  749. continue;
  750. }
  751. if (*(s+1) == '%') {
  752. *p++ = '%';
  753. s += 2;
  754. continue;
  755. }
  756. /* have to be real careful in case this is a huge number, eg, %100000d */
  757. fmtwd = atoi(s+1);
  758. if (fmtwd < 0)
  759. fmtwd = -fmtwd;
  760. adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
  761. for (t = fmt; (*t++ = *s) != '\0'; s++) {
  762. if (!adjbuf(&fmt, &fmtsz, MAXNUMSIZE+1+t-fmt, recsize, &t, 0))
  763. FATAL("format item %.30s... ran format() out of memory", os);
  764. if (isalpha(*s) && *s != 'l' && *s != 'h' && *s != 'L')
  765. break; /* the ansi panoply */
  766. if (*s == '*') {
  767. x = execute(a);
  768. a = a->nnext;
  769. sprintf(t-1, "%d", fmtwd=(int) getfval(x));
  770. if (fmtwd < 0)
  771. fmtwd = -fmtwd;
  772. adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
  773. t = fmt + strlen(fmt);
  774. tempfree(x);
  775. }
  776. }
  777. *t = '\0';
  778. if (fmtwd < 0)
  779. fmtwd = -fmtwd;
  780. adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
  781. switch (*s) {
  782. case 'f': case 'e': case 'g': case 'E': case 'G':
  783. flag = 1;
  784. break;
  785. case 'd': case 'i':
  786. flag = 2;
  787. if(*(s-1) == 'l') break;
  788. *(t-1) = 'l';
  789. *t = 'd';
  790. *++t = '\0';
  791. break;
  792. case 'o': case 'x': case 'X': case 'u':
  793. flag = *(s-1) == 'l' ? 2 : 3;
  794. break;
  795. case 's':
  796. flag = 4;
  797. break;
  798. case 'c':
  799. flag = 5;
  800. break;
  801. default:
  802. WARNING("weird printf conversion %s", fmt);
  803. flag = 0;
  804. break;
  805. }
  806. if (a == NULL)
  807. FATAL("not enough args in printf(%s)", os);
  808. x = execute(a);
  809. a = a->nnext;
  810. n = MAXNUMSIZE;
  811. if (fmtwd > n)
  812. n = fmtwd;
  813. adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format");
  814. switch (flag) {
  815. case 0: sprintf(p, "%s", fmt); /* unknown, so dump it too */
  816. t = getsval(x);
  817. n = strlen(t);
  818. if (fmtwd > n)
  819. n = fmtwd;
  820. adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format");
  821. p += strlen(p);
  822. sprintf(p, "%s", t);
  823. break;
  824. case 1: sprintf(p, fmt, getfval(x)); break;
  825. case 2: sprintf(p, fmt, (long) getfval(x)); break;
  826. case 3: sprintf(p, fmt, (int) getfval(x)); break;
  827. case 4:
  828. t = getsval(x);
  829. n = strlen(t);
  830. if (fmtwd > n)
  831. n = fmtwd;
  832. if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, 0))
  833. FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
  834. sprintf(p, fmt, t);
  835. break;
  836. case 5:
  837. if (isnum(x)) {
  838. if (getfval(x))
  839. sprintf(p, fmt, (int) getfval(x));
  840. else{
  841. *p++ = '\0';
  842. *p = '\0';
  843. }
  844. } else
  845. sprintf(p, fmt, getsval(x)[0]);
  846. break;
  847. }
  848. tempfree(x);
  849. p += strlen(p);
  850. s++;
  851. }
  852. *p = '\0';
  853. free(fmt);
  854. for ( ; a; a = a->nnext) /* evaluate any remaining args */
  855. execute(a);
  856. *pbuf = buf;
  857. *pbufsize = bufsize;
  858. return p - buf;
  859. }
  860. Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */
  861. {
  862. Cell *x;
  863. Node *y;
  864. char *buf;
  865. int bufsz=3*recsize;
  866. if ((buf = (char *) malloc(bufsz)) == NULL)
  867. FATAL("out of memory in awksprintf");
  868. y = a[0]->nnext;
  869. x = execute(a[0]);
  870. if (format(&buf, &bufsz, getsval(x), y) == -1)
  871. FATAL("sprintf string %.30s... too long. can't happen.", buf);
  872. tempfree(x);
  873. x = gettemp();
  874. x->sval = buf;
  875. x->tval = STR;
  876. return(x);
  877. }
  878. Cell *awkprintf(Node **a, int n) /* printf */
  879. { /* a[0] is list of args, starting with format string */
  880. /* a[1] is redirection operator, a[2] is redirection file */
  881. FILE *fp;
  882. Cell *x;
  883. Node *y;
  884. char *buf;
  885. int len;
  886. int bufsz=3*recsize;
  887. if ((buf = (char *) malloc(bufsz)) == NULL)
  888. FATAL("out of memory in awkprintf");
  889. y = a[0]->nnext;
  890. x = execute(a[0]);
  891. if ((len = format(&buf, &bufsz, getsval(x), y)) == -1)
  892. FATAL("printf string %.30s... too long. can't happen.", buf);
  893. tempfree(x);
  894. if (a[1] == NULL) {
  895. /* fputs(buf, stdout); */
  896. fwrite(buf, len, 1, stdout);
  897. if (ferror(stdout))
  898. FATAL("write error on stdout");
  899. } else {
  900. fp = redirect(ptoi(a[1]), a[2]);
  901. /* fputs(buf, fp); */
  902. fwrite(buf, len, 1, fp);
  903. fflush(fp);
  904. if (ferror(fp))
  905. FATAL("write error on %s", filename(fp));
  906. }
  907. free(buf);
  908. return(True);
  909. }
  910. Cell *arith(Node **a, int n) /* a[0] + a[1], etc. also -a[0] */
  911. {
  912. Awkfloat i, j = 0;
  913. double v;
  914. Cell *x, *y, *z;
  915. x = execute(a[0]);
  916. i = getfval(x);
  917. tempfree(x);
  918. if (n != UMINUS) {
  919. y = execute(a[1]);
  920. j = getfval(y);
  921. tempfree(y);
  922. }
  923. z = gettemp();
  924. switch (n) {
  925. case ADD:
  926. i += j;
  927. break;
  928. case MINUS:
  929. i -= j;
  930. break;
  931. case MULT:
  932. i *= j;
  933. break;
  934. case DIVIDE:
  935. if (j == 0)
  936. FATAL("division by zero");
  937. i /= j;
  938. break;
  939. case MOD:
  940. if (j == 0)
  941. FATAL("division by zero in mod");
  942. modf(i/j, &v);
  943. i = i - j * v;
  944. break;
  945. case UMINUS:
  946. i = -i;
  947. break;
  948. case POWER:
  949. if (j >= 0 && modf(j, &v) == 0.0) /* pos integer exponent */
  950. i = ipow(i, (int) j);
  951. else
  952. i = errcheck(pow(i, j), "pow");
  953. break;
  954. default: /* can't happen */
  955. FATAL("illegal arithmetic operator %d", n);
  956. }
  957. setfval(z, i);
  958. return(z);
  959. }
  960. double ipow(double x, int n) /* x**n. ought to be done by pow, but isn't always */
  961. {
  962. double v;
  963. if (n <= 0)
  964. return 1;
  965. v = ipow(x, n/2);
  966. if (n % 2 == 0)
  967. return v * v;
  968. else
  969. return x * v * v;
  970. }
  971. Cell *incrdecr(Node **a, int n) /* a[0]++, etc. */
  972. {
  973. Cell *x, *z;
  974. int k;
  975. Awkfloat xf;
  976. x = execute(a[0]);
  977. xf = getfval(x);
  978. k = (n == PREINCR || n == POSTINCR) ? 1 : -1;
  979. if (n == PREINCR || n == PREDECR) {
  980. setfval(x, xf + k);
  981. return(x);
  982. }
  983. z = gettemp();
  984. setfval(z, xf);
  985. setfval(x, xf + k);
  986. tempfree(x);
  987. return(z);
  988. }
  989. Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */
  990. { /* this is subtle; don't muck with it. */
  991. Cell *x, *y;
  992. Awkfloat xf, yf;
  993. double v;
  994. y = execute(a[1]);
  995. x = execute(a[0]);
  996. if (n == ASSIGN) { /* ordinary assignment */
  997. if (x == y && !(x->tval & (FLD|REC))) /* self-assignment: */
  998. ; /* leave alone unless it's a field */
  999. else if ((y->tval & (STR|NUM)) == (STR|NUM)) {
  1000. setsval(x, getsval(y));
  1001. x->fval = getfval(y);
  1002. x->tval |= NUM;
  1003. }
  1004. else if (isstr(y))
  1005. setsval(x, getsval(y));
  1006. else if (isnum(y))
  1007. setfval(x, getfval(y));
  1008. else
  1009. funnyvar(y, "read value of");
  1010. tempfree(y);
  1011. return(x);
  1012. }
  1013. xf = getfval(x);
  1014. yf = getfval(y);
  1015. switch (n) {
  1016. case ADDEQ:
  1017. xf += yf;
  1018. break;
  1019. case SUBEQ:
  1020. xf -= yf;
  1021. break;
  1022. case MULTEQ:
  1023. xf *= yf;
  1024. break;
  1025. case DIVEQ:
  1026. if (yf == 0)
  1027. FATAL("division by zero in /=");
  1028. xf /= yf;
  1029. break;
  1030. case MODEQ:
  1031. if (yf == 0)
  1032. FATAL("division by zero in %%=");
  1033. modf(xf/yf, &v);
  1034. xf = xf - yf * v;
  1035. break;
  1036. case POWEQ:
  1037. if (yf >= 0 && modf(yf, &v) == 0.0) /* pos integer exponent */
  1038. xf = ipow(xf, (int) yf);
  1039. else
  1040. xf = errcheck(pow(xf, yf), "pow");
  1041. break;
  1042. default:
  1043. FATAL("illegal assignment operator %d", n);
  1044. break;
  1045. }
  1046. tempfree(y);
  1047. setfval(x, xf);
  1048. return(x);
  1049. }
  1050. Cell *cat(Node **a, int q) /* a[0] cat a[1] */
  1051. {
  1052. Cell *x, *y, *z;
  1053. int n1, n2;
  1054. char *s;
  1055. x = execute(a[0]);
  1056. y = execute(a[1]);
  1057. getsval(x);
  1058. getsval(y);
  1059. n1 = strlen(x->sval);
  1060. n2 = strlen(y->sval);
  1061. s = (char *) malloc(n1 + n2 + 1);
  1062. if (s == NULL)
  1063. FATAL("out of space concatenating %.15s... and %.15s...",
  1064. x->sval, y->sval);
  1065. strcpy(s, x->sval);
  1066. strcpy(s+n1, y->sval);
  1067. tempfree(y);
  1068. z = gettemp();
  1069. z->sval = s;
  1070. z->tval = STR;
  1071. tempfree(x);
  1072. return(z);
  1073. }
  1074. Cell *pastat(Node **a, int n) /* a[0] { a[1] } */
  1075. {
  1076. Cell *x;
  1077. if (a[0] == 0)
  1078. x = execute(a[1]);
  1079. else {
  1080. x = execute(a[0]);
  1081. if (istrue(x)) {
  1082. tempfree(x);
  1083. x = execute(a[1]);
  1084. }
  1085. }
  1086. return x;
  1087. }
  1088. Cell *dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */
  1089. {
  1090. Cell *x;
  1091. int pair;
  1092. pair = ptoi(a[3]);
  1093. if (pairstack[pair] == 0) {
  1094. x = execute(a[0]);
  1095. if (istrue(x))
  1096. pairstack[pair] = 1;
  1097. tempfree(x);
  1098. }
  1099. if (pairstack[pair] == 1) {
  1100. x = execute(a[1]);
  1101. if (istrue(x))
  1102. pairstack[pair] = 0;
  1103. tempfree(x);
  1104. x = execute(a[2]);
  1105. return(x);
  1106. }
  1107. return(False);
  1108. }
  1109. Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
  1110. {
  1111. Cell *x = 0, *y, *ap;
  1112. char *s, *t, *fs = 0;
  1113. char temp, num[50];
  1114. int n, nb, sep, tempstat, arg3type;
  1115. y = execute(a[0]); /* source string */
  1116. s = getsval(y);
  1117. arg3type = ptoi(a[3]);
  1118. if (a[2] == 0) /* fs string */
  1119. fs = *FS;
  1120. else if (arg3type == STRING) { /* split(str,arr,"string") */
  1121. x = execute(a[2]);
  1122. fs = getsval(x);
  1123. } else if (arg3type == REGEXPR)
  1124. fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
  1125. else
  1126. FATAL("illegal type of split");
  1127. sep = *fs;
  1128. ap = execute(a[1]); /* array name */
  1129. freesymtab(ap);
  1130. dprintf( ("split: s=|%s|, a=%s, sep=|%s|\n", s, ap->nval, fs) );
  1131. ap->tval &= ~STR;
  1132. ap->tval |= ARR;
  1133. ap->sval = (char *) makesymtab(NSYMTAB);
  1134. n = 0;
  1135. if ((*s != '\0' && strlen(fs) > 1) || arg3type == REGEXPR) { /* reg expr */
  1136. void *p;
  1137. if (arg3type == REGEXPR) { /* it's ready already */
  1138. p = (void *) a[2];
  1139. } else {
  1140. p = compre(fs);
  1141. }
  1142. t = s;
  1143. if (nematch(p,s,t)) {
  1144. do {
  1145. n++;
  1146. sprintf(num, "%d", n);
  1147. temp = *patbeg;
  1148. *patbeg = '\0';
  1149. if (is_number(t))
  1150. setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
  1151. else
  1152. setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
  1153. *patbeg = temp;
  1154. t = patbeg + patlen;
  1155. if (t[-1] == 0 || *t == 0) {
  1156. n++;
  1157. sprintf(num, "%d", n);
  1158. setsymtab(num, "", 0.0, STR, (Array *) ap->sval);
  1159. goto spdone;
  1160. }
  1161. } while (nematch(p,s,t));
  1162. }
  1163. n++;
  1164. sprintf(num, "%d", n);
  1165. if (is_number(t))
  1166. setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
  1167. else
  1168. setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
  1169. spdone:
  1170. p = NULL;
  1171. } else if (sep == ' ') {
  1172. for (n = 0; ; ) {
  1173. while (*s == ' ' || *s == '\t' || *s == '\n')
  1174. s++;
  1175. if (*s == 0)
  1176. break;
  1177. n++;
  1178. t = s;
  1179. do
  1180. s++;
  1181. while (*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0');
  1182. temp = *s;
  1183. *s = '\0';
  1184. sprintf(num, "%d", n);
  1185. if (is_number(t))
  1186. setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
  1187. else
  1188. setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
  1189. *s = temp;
  1190. if (*s != 0)
  1191. s++;
  1192. }
  1193. } else if (sep == 0) { /* new: split(s, a, "") => 1 char/elem */
  1194. for (n = 0; *s != 0; s += nb) {
  1195. Rune r;
  1196. char buf[UTFmax+1];
  1197. n++;
  1198. snprintf(num, sizeof num, "%d", n);
  1199. nb = chartorune(&r, s);
  1200. memmove(buf, s, nb);
  1201. buf[nb] = '\0';
  1202. if (isdigit(buf[0]))
  1203. setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval);
  1204. else
  1205. setsymtab(num, buf, 0.0, STR, (Array *) ap->sval);
  1206. }
  1207. } else if (*s != 0) {
  1208. for (;;) {
  1209. n++;
  1210. t = s;
  1211. while (*s != sep && *s != '\n' && *s != '\0')
  1212. s++;
  1213. temp = *s;
  1214. *s = '\0';
  1215. sprintf(num, "%d", n);
  1216. if (is_number(t))
  1217. setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
  1218. else
  1219. setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
  1220. *s = temp;
  1221. if (*s++ == 0)
  1222. break;
  1223. }
  1224. }
  1225. tempfree(ap);
  1226. tempfree(y);
  1227. if (a[2] != 0 && arg3type == STRING)
  1228. tempfree(x);
  1229. x = gettemp();
  1230. x->tval = NUM;
  1231. x->fval = n;
  1232. return(x);
  1233. }
  1234. Cell *condexpr(Node **a, int n) /* a[0] ? a[1] : a[2] */
  1235. {
  1236. Cell *x;
  1237. x = execute(a[0]);
  1238. if (istrue(x)) {
  1239. tempfree(x);
  1240. x = execute(a[1]);
  1241. } else {
  1242. tempfree(x);
  1243. x = execute(a[2]);
  1244. }
  1245. return(x);
  1246. }
  1247. Cell *ifstat(Node **a, int n) /* if (a[0]) a[1]; else a[2] */
  1248. {
  1249. Cell *x;
  1250. x = execute(a[0]);
  1251. if (istrue(x)) {
  1252. tempfree(x);
  1253. x = execute(a[1]);
  1254. } else if (a[2] != 0) {
  1255. tempfree(x);
  1256. x = execute(a[2]);
  1257. }
  1258. return(x);
  1259. }
  1260. Cell *whilestat(Node **a, int n) /* while (a[0]) a[1] */
  1261. {
  1262. Cell *x;
  1263. for (;;) {
  1264. x = execute(a[0]);
  1265. if (!istrue(x))
  1266. return(x);
  1267. tempfree(x);
  1268. x = execute(a[1]);
  1269. if (isbreak(x)) {
  1270. x = True;
  1271. return(x);
  1272. }
  1273. if (isnext(x) || isexit(x) || isret(x))
  1274. return(x);
  1275. tempfree(x);
  1276. }
  1277. }
  1278. Cell *dostat(Node **a, int n) /* do a[0]; while(a[1]) */
  1279. {
  1280. Cell *x;
  1281. for (;;) {
  1282. x = execute(a[0]);
  1283. if (isbreak(x))
  1284. return True;
  1285. if (isnext(x) || isnextfile(x) || isexit(x) || isret(x))
  1286. return(x);
  1287. tempfree(x);
  1288. x = execute(a[1]);
  1289. if (!istrue(x))
  1290. return(x);
  1291. tempfree(x);
  1292. }
  1293. }
  1294. Cell *forstat(Node **a, int n) /* for (a[0]; a[1]; a[2]) a[3] */
  1295. {
  1296. Cell *x;
  1297. x = execute(a[0]);
  1298. tempfree(x);
  1299. for (;;) {
  1300. if (a[1]!=0) {
  1301. x = execute(a[1]);
  1302. if (!istrue(x)) return(x);
  1303. else tempfree(x);
  1304. }
  1305. x = execute(a[3]);
  1306. if (isbreak(x)) /* turn off break */
  1307. return True;
  1308. if (isnext(x) || isexit(x) || isret(x))
  1309. return(x);
  1310. tempfree(x);
  1311. x = execute(a[2]);
  1312. tempfree(x);
  1313. }
  1314. }
  1315. Cell *instat(Node **a, int n) /* for (a[0] in a[1]) a[2] */
  1316. {
  1317. Cell *x, *vp, *arrayp, *cp, *ncp;
  1318. Array *tp;
  1319. int i;
  1320. vp = execute(a[0]);
  1321. arrayp = execute(a[1]);
  1322. if (!isarr(arrayp)) {
  1323. return True;
  1324. }
  1325. tp = (Array *) arrayp->sval;
  1326. tempfree(arrayp);
  1327. for (i = 0; i < tp->size; i++) { /* this routine knows too much */
  1328. for (cp = tp->tab[i]; cp != NULL; cp = ncp) {
  1329. setsval(vp, cp->nval);
  1330. ncp = cp->cnext;
  1331. x = execute(a[2]);
  1332. if (isbreak(x)) {
  1333. tempfree(vp);
  1334. return True;
  1335. }
  1336. if (isnext(x) || isexit(x) || isret(x)) {
  1337. tempfree(vp);
  1338. return(x);
  1339. }
  1340. tempfree(x);
  1341. }
  1342. }
  1343. return True;
  1344. }
  1345. Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg list */
  1346. {
  1347. Cell *x, *y;
  1348. Awkfloat u;
  1349. int t;
  1350. wchar_t wc;
  1351. char *p, *buf;
  1352. char mbc[50];
  1353. Node *nextarg;
  1354. FILE *fp;
  1355. t = ptoi(a[0]);
  1356. x = execute(a[1]);
  1357. nextarg = a[1]->nnext;
  1358. switch (t) {
  1359. case FLENGTH:
  1360. p = getsval(x);
  1361. u = (Awkfloat) countposn(p, strlen(p)); break;
  1362. case FLOG:
  1363. u = errcheck(log(getfval(x)), "log"); break;
  1364. case FINT:
  1365. modf(getfval(x), &u); break;
  1366. case FEXP:
  1367. u = errcheck(exp(getfval(x)), "exp"); break;
  1368. case FSQRT:
  1369. u = errcheck(sqrt(getfval(x)), "sqrt"); break;
  1370. case FSIN:
  1371. u = sin(getfval(x)); break;
  1372. case FCOS:
  1373. u = cos(getfval(x)); break;
  1374. case FATAN:
  1375. if (nextarg == 0) {
  1376. WARNING("atan2 requires two arguments; returning 1.0");
  1377. u = 1.0;
  1378. } else {
  1379. y = execute(a[1]->nnext);
  1380. u = atan2(getfval(x), getfval(y));
  1381. tempfree(y);
  1382. nextarg = nextarg->nnext;
  1383. }
  1384. break;
  1385. case FSYSTEM:
  1386. fflush(stdout); /* in case something is buffered already */
  1387. u = (Awkfloat) system(getsval(x)) / 256; /* 256 is unix-dep */
  1388. break;
  1389. case FRAND:
  1390. /* in principle, rand() returns something in 0..RAND_MAX */
  1391. u = (Awkfloat) (rand() % RAND_MAX) / RAND_MAX;
  1392. break;
  1393. case FSRAND:
  1394. if (isrec(x)) /* no argument provided */
  1395. u = time((time_t *)0);
  1396. else
  1397. u = getfval(x);
  1398. srand((unsigned int) u);
  1399. break;
  1400. case FTOUPPER:
  1401. case FTOLOWER:
  1402. buf = tostring(getsval(x));
  1403. if (t == FTOUPPER) {
  1404. for (p = buf; *p; p++)
  1405. if (islower(*p))
  1406. *p = toupper(*p);
  1407. } else {
  1408. for (p = buf; *p; p++)
  1409. if (isupper(*p))
  1410. *p = tolower(*p);
  1411. }
  1412. tempfree(x);
  1413. x = gettemp();
  1414. setsval(x, buf);
  1415. free(buf);
  1416. return x;
  1417. case FFLUSH:
  1418. if ((fp = openfile(FFLUSH, getsval(x))) == NULL)
  1419. u = EOF;
  1420. else
  1421. u = fflush(fp);
  1422. break;
  1423. case FUTF:
  1424. wc = (int)getfval(x);
  1425. mbc[wctomb(mbc, wc)] = 0;
  1426. tempfree(x);
  1427. x = gettemp();
  1428. setsval(x, mbc);
  1429. return x;
  1430. default: /* can't happen */
  1431. FATAL("illegal function type %d", t);
  1432. break;
  1433. }
  1434. tempfree(x);
  1435. x = gettemp();
  1436. setfval(x, u);
  1437. if (nextarg != 0) {
  1438. WARNING("warning: function has too many arguments");
  1439. for ( ; nextarg; nextarg = nextarg->nnext)
  1440. execute(nextarg);
  1441. }
  1442. return(x);
  1443. }
  1444. Cell *printstat(Node **a, int n) /* print a[0] */
  1445. {
  1446. int r;
  1447. Node *x;
  1448. Cell *y;
  1449. FILE *fp;
  1450. if (a[1] == 0) /* a[1] is redirection operator, a[2] is file */
  1451. fp = stdout;
  1452. else
  1453. fp = redirect(ptoi(a[1]), a[2]);
  1454. for (x = a[0]; x != NULL; x = x->nnext) {
  1455. y = execute(x);
  1456. fputs(getsval(y), fp);
  1457. tempfree(y);
  1458. if (x->nnext == NULL)
  1459. r = fputs(*ORS, fp);
  1460. else
  1461. r = fputs(*OFS, fp);
  1462. if (r == EOF)
  1463. FATAL("write error on %s", filename(fp));
  1464. }
  1465. if (a[1] != 0)
  1466. if (fflush(fp) == EOF)
  1467. FATAL("write error on %s", filename(fp));
  1468. return(True);
  1469. }
  1470. Cell *nullproc(Node **a, int n)
  1471. {
  1472. n = n;
  1473. a = a;
  1474. return 0;
  1475. }
  1476. FILE *redirect(int a, Node *b) /* set up all i/o redirections */
  1477. {
  1478. FILE *fp;
  1479. Cell *x;
  1480. char *fname;
  1481. x = execute(b);
  1482. fname = getsval(x);
  1483. fp = openfile(a, fname);
  1484. if (fp == NULL)
  1485. FATAL("can't open file %s", fname);
  1486. tempfree(x);
  1487. return fp;
  1488. }
  1489. struct files {
  1490. FILE *fp;
  1491. char *fname;
  1492. int mode; /* '|', 'a', 'w' => LE/LT, GT */
  1493. } files[FOPEN_MAX] ={
  1494. { NULL, "/dev/stdin", LT }, /* watch out: don't free this! */
  1495. { NULL, "/dev/stdout", GT },
  1496. { NULL, "/dev/stderr", GT }
  1497. };
  1498. void stdinit(void) /* in case stdin, etc., are not constants */
  1499. {
  1500. files[0].fp = stdin;
  1501. files[1].fp = stdout;
  1502. files[2].fp = stderr;
  1503. }
  1504. FILE *openfile(int a, char *us)
  1505. {
  1506. char *s = us;
  1507. int i, m;
  1508. FILE *fp = 0;
  1509. if (*s == '\0')
  1510. FATAL("null file name in print or getline");
  1511. for (i=0; i < FOPEN_MAX; i++)
  1512. if (files[i].fname && strcmp(s, files[i].fname) == 0) {
  1513. if (a == files[i].mode || (a==APPEND && files[i].mode==GT))
  1514. return files[i].fp;
  1515. if (a == FFLUSH)
  1516. return files[i].fp;
  1517. }
  1518. if (a == FFLUSH) /* didn't find it, so don't create it! */
  1519. return NULL;
  1520. for (i=0; i < FOPEN_MAX; i++)
  1521. if (files[i].fp == 0)
  1522. break;
  1523. if (i >= FOPEN_MAX)
  1524. FATAL("%s makes too many open files", s);
  1525. fflush(stdout); /* force a semblance of order */
  1526. m = a;
  1527. if (a == GT) {
  1528. fp = fopen(s, "w");
  1529. } else if (a == APPEND) {
  1530. fp = fopen(s, "a");
  1531. m = GT; /* so can mix > and >> */
  1532. } else if (a == '|') { /* output pipe */
  1533. fp = popen(s, "w");
  1534. } else if (a == LE) { /* input pipe */
  1535. fp = popen(s, "r");
  1536. } else if (a == LT) { /* getline <file */
  1537. fp = strcmp(s, "-") == 0 ? stdin : fopen(s, "r"); /* "-" is stdin */
  1538. } else /* can't happen */
  1539. FATAL("illegal redirection %d", a);
  1540. if (fp != NULL) {
  1541. files[i].fname = tostring(s);
  1542. files[i].fp = fp;
  1543. files[i].mode = m;
  1544. }
  1545. return fp;
  1546. }
  1547. char *filename(FILE *fp)
  1548. {
  1549. int i;
  1550. for (i = 0; i < FOPEN_MAX; i++)
  1551. if (fp == files[i].fp)
  1552. return files[i].fname;
  1553. return "???";
  1554. }
  1555. Cell *closefile(Node **a, int n)
  1556. {
  1557. Cell *x;
  1558. int i, stat;
  1559. n = n;
  1560. x = execute(a[0]);
  1561. getsval(x);
  1562. for (i = 0; i < FOPEN_MAX; i++)
  1563. if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) {
  1564. if (ferror(files[i].fp))
  1565. WARNING( "i/o error occurred on %s", files[i].fname );
  1566. if (files[i].mode == '|' || files[i].mode == LE)
  1567. stat = pclose(files[i].fp);
  1568. else
  1569. stat = fclose(files[i].fp);
  1570. if (stat == EOF)
  1571. WARNING( "i/o error occurred closing %s", files[i].fname );
  1572. if (i > 2) /* don't do /dev/std... */
  1573. xfree(files[i].fname);
  1574. files[i].fname = NULL; /* watch out for ref thru this */
  1575. files[i].fp = NULL;
  1576. }
  1577. tempfree(x);
  1578. return(True);
  1579. }
  1580. void closeall(void)
  1581. {
  1582. int i, stat;
  1583. for (i = 0; i < FOPEN_MAX; i++)
  1584. if (files[i].fp) {
  1585. if (ferror(files[i].fp))
  1586. WARNING( "i/o error occurred on %s", files[i].fname );
  1587. if (files[i].mode == '|' || files[i].mode == LE)
  1588. stat = pclose(files[i].fp);
  1589. else
  1590. stat = fclose(files[i].fp);
  1591. if (stat == EOF)
  1592. WARNING( "i/o error occurred while closing %s", files[i].fname );
  1593. }
  1594. }
  1595. void backsub(char **pb_ptr, char **sptr_ptr);
  1596. Cell *sub(Node **a, int nnn) /* substitute command */
  1597. {
  1598. char *sptr, *pb, *q;
  1599. Cell *x, *y, *result;
  1600. char *t, *buf;
  1601. void *p;
  1602. int bufsz = recsize;
  1603. if ((buf = (char *) malloc(bufsz)) == NULL)
  1604. FATAL("out of memory in sub");
  1605. x = execute(a[3]); /* target string */
  1606. t = getsval(x);
  1607. if (a[0] == 0) /* 0 => a[1] is already-compiled regexpr */
  1608. p = (void *) a[1]; /* regular expression */
  1609. else {
  1610. y = execute(a[1]);
  1611. p = compre(getsval(y));
  1612. tempfree(y);
  1613. }
  1614. y = execute(a[2]); /* replacement string */
  1615. result = False;
  1616. if (pmatch(p, t, t)) {
  1617. sptr = t;
  1618. adjbuf(&buf, &bufsz, 1+patbeg-sptr, recsize, 0, "sub");
  1619. pb = buf;
  1620. while (sptr < patbeg)
  1621. *pb++ = *sptr++;
  1622. sptr = getsval(y);
  1623. while (*sptr != 0) {
  1624. adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "sub");
  1625. if (*sptr == '\\') {
  1626. backsub(&pb, &sptr);
  1627. } else if (*sptr == '&') {
  1628. sptr++;
  1629. adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "sub");
  1630. for (q = patbeg; q < patbeg+patlen; )
  1631. *pb++ = *q++;
  1632. } else
  1633. *pb++ = *sptr++;
  1634. }
  1635. *pb = '\0';
  1636. if (pb > buf + bufsz)
  1637. FATAL("sub result1 %.30s too big; can't happen", buf);
  1638. sptr = patbeg + patlen;
  1639. if ((patlen == 0 && *patbeg) || (patlen && *(sptr-1))) {
  1640. adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "sub");
  1641. while ((*pb++ = *sptr++) != 0)
  1642. ;
  1643. }
  1644. if (pb > buf + bufsz)
  1645. FATAL("sub result2 %.30s too big; can't happen", buf);
  1646. setsval(x, buf); /* BUG: should be able to avoid copy */
  1647. result = True;;
  1648. }
  1649. tempfree(x);
  1650. tempfree(y);
  1651. free(buf);
  1652. return result;
  1653. }
  1654. Cell *gsub(Node **a, int nnn) /* global substitute */
  1655. {
  1656. Cell *x, *y;
  1657. char *rptr, *sptr, *t, *pb, *c;
  1658. char *buf;
  1659. void *p;
  1660. int mflag, num;
  1661. int bufsz = recsize;
  1662. if ((buf = (char *)malloc(bufsz)) == NULL)
  1663. FATAL("out of memory in gsub");
  1664. mflag = 0; /* if mflag == 0, can replace empty string */
  1665. num = 0;
  1666. x = execute(a[3]); /* target string */
  1667. c = t = getsval(x);
  1668. if (a[0] == 0) /* 0 => a[1] is already-compiled regexpr */
  1669. p = (void *) a[1]; /* regular expression */
  1670. else {
  1671. y = execute(a[1]);
  1672. p = compre(getsval(y));
  1673. tempfree(y);
  1674. }
  1675. y = execute(a[2]); /* replacement string */
  1676. if (pmatch(p, t, c)) {
  1677. pb = buf;
  1678. rptr = getsval(y);
  1679. do {
  1680. if (patlen == 0 && *patbeg != 0) { /* matched empty string */
  1681. if (mflag == 0) { /* can replace empty */
  1682. num++;
  1683. sptr = rptr;
  1684. while (*sptr != 0) {
  1685. adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
  1686. if (*sptr == '\\') {
  1687. backsub(&pb, &sptr);
  1688. } else if (*sptr == '&') {
  1689. char *q;
  1690. sptr++;
  1691. adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
  1692. for (q = patbeg; q < patbeg+patlen; )
  1693. *pb++ = *q++;
  1694. } else
  1695. *pb++ = *sptr++;
  1696. }
  1697. }
  1698. if (*c == 0) /* at end */
  1699. goto done;
  1700. adjbuf(&buf, &bufsz, 2+pb-buf, recsize, &pb, "gsub");
  1701. *pb++ = *c++;
  1702. if (pb > buf + bufsz) /* BUG: not sure of this test */
  1703. FATAL("gsub result0 %.30s too big; can't happen", buf);
  1704. mflag = 0;
  1705. }
  1706. else { /* matched nonempty string */
  1707. num++;
  1708. sptr = c;
  1709. adjbuf(&buf, &bufsz, 1+(patbeg-sptr)+pb-buf, recsize, &pb, "gsub");
  1710. while (sptr < patbeg)
  1711. *pb++ = *sptr++;
  1712. sptr = rptr;
  1713. while (*sptr != 0) {
  1714. adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gsub");
  1715. if (*sptr == '\\') {
  1716. backsub(&pb, &sptr);
  1717. } else if (*sptr == '&') {
  1718. char *q;
  1719. sptr++;
  1720. adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gsub");
  1721. for (q = patbeg; q < patbeg+patlen; )
  1722. *pb++ = *q++;
  1723. } else
  1724. *pb++ = *sptr++;
  1725. }
  1726. c = patbeg + patlen;
  1727. if ((c[-1] == 0) || (*c == 0))
  1728. goto done;
  1729. if (pb > buf + bufsz)
  1730. FATAL("gsub result1 %.30s too big; can't happen", buf);
  1731. mflag = 1;
  1732. }
  1733. } while (pmatch(p, t, c));
  1734. sptr = c;
  1735. adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "gsub");
  1736. while ((*pb++ = *sptr++) != 0)
  1737. ;
  1738. done: if (pb > buf + bufsz)
  1739. FATAL("gsub result2 %.30s too big; can't happen", buf);
  1740. *pb = '\0';
  1741. setsval(x, buf); /* BUG: should be able to avoid copy + free */
  1742. }
  1743. tempfree(x);
  1744. tempfree(y);
  1745. x = gettemp();
  1746. x->tval = NUM;
  1747. x->fval = num;
  1748. free(buf);
  1749. return(x);
  1750. }
  1751. void backsub(char **pb_ptr, char **sptr_ptr) /* handle \\& variations */
  1752. { /* sptr[0] == '\\' */
  1753. char *pb = *pb_ptr, *sptr = *sptr_ptr;
  1754. if (sptr[1] == '\\') {
  1755. if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */
  1756. *pb++ = '\\';
  1757. *pb++ = '&';
  1758. sptr += 4;
  1759. } else if (sptr[2] == '&') { /* \\& -> \ + matched */
  1760. *pb++ = '\\';
  1761. sptr += 2;
  1762. } else { /* \\x -> \\x */
  1763. *pb++ = *sptr++;
  1764. *pb++ = *sptr++;
  1765. }
  1766. } else if (sptr[1] == '&') { /* literal & */
  1767. sptr++;
  1768. *pb++ = *sptr++;
  1769. } else /* literal \ */
  1770. *pb++ = *sptr++;
  1771. *pb_ptr = pb;
  1772. *sptr_ptr = sptr;
  1773. }