You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmd.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* $Header: cmd.c,v 1.0.1.1 88/01/21 21:24:16 root Exp $
  2. *
  3. * $Log: cmd.c,v $
  4. * Revision 1.0.1.1 88/01/21 21:24:16 root
  5. * The redo cmd got a segmentation fault because trace context stack overflowed.
  6. *
  7. * Revision 1.0 87/12/18 13:04:51 root
  8. * Initial revision
  9. *
  10. */
  11. #include "handy.h"
  12. #include "EXTERN.h"
  13. #include "search.h"
  14. #include "util.h"
  15. #include "perl.h"
  16. static STR str_chop;
  17. /* This is the main command loop. We try to spend as much time in this loop
  18. * as possible, so lots of optimizations do their activities in here. This
  19. * means things get a little sloppy.
  20. */
  21. STR *
  22. cmd_exec(cmd)
  23. register CMD *cmd;
  24. {
  25. SPAT *oldspat;
  26. #ifdef DEBUGGING
  27. int olddlevel;
  28. int entdlevel;
  29. #endif
  30. register STR *retstr;
  31. register char *tmps;
  32. register int cmdflags;
  33. register bool match;
  34. register char *go_to = goto_targ;
  35. ARG *arg;
  36. FILE *fp;
  37. retstr = &str_no;
  38. #ifdef DEBUGGING
  39. entdlevel = dlevel;
  40. #endif
  41. tail_recursion_entry:
  42. #ifdef DEBUGGING
  43. dlevel = entdlevel;
  44. #endif
  45. if (cmd == Nullcmd)
  46. return retstr;
  47. cmdflags = cmd->c_flags; /* hopefully load register */
  48. if (go_to) {
  49. if (cmd->c_label && strEQ(go_to,cmd->c_label))
  50. goto_targ = go_to = Nullch; /* here at last */
  51. else {
  52. switch (cmd->c_type) {
  53. case C_IF:
  54. oldspat = curspat;
  55. #ifdef DEBUGGING
  56. olddlevel = dlevel;
  57. #endif
  58. retstr = &str_yes;
  59. if (cmd->ucmd.ccmd.cc_true) {
  60. #ifdef DEBUGGING
  61. debname[dlevel] = 't';
  62. debdelim[dlevel++] = '_';
  63. #endif
  64. retstr = cmd_exec(cmd->ucmd.ccmd.cc_true);
  65. }
  66. if (!goto_targ) {
  67. go_to = Nullch;
  68. } else {
  69. retstr = &str_no;
  70. if (cmd->ucmd.ccmd.cc_alt) {
  71. #ifdef DEBUGGING
  72. debname[dlevel] = 'e';
  73. debdelim[dlevel++] = '_';
  74. #endif
  75. retstr = cmd_exec(cmd->ucmd.ccmd.cc_alt);
  76. }
  77. }
  78. if (!goto_targ)
  79. go_to = Nullch;
  80. curspat = oldspat;
  81. #ifdef DEBUGGING
  82. dlevel = olddlevel;
  83. #endif
  84. break;
  85. case C_BLOCK:
  86. case C_WHILE:
  87. if (!(cmdflags & CF_ONCE)) {
  88. cmdflags |= CF_ONCE;
  89. loop_ptr++;
  90. loop_stack[loop_ptr].loop_label = cmd->c_label;
  91. #ifdef DEBUGGING
  92. if (debug & 4) {
  93. deb("(Pushing label #%d %s)\n",
  94. loop_ptr,cmd->c_label);
  95. }
  96. #endif
  97. }
  98. switch (setjmp(loop_stack[loop_ptr].loop_env)) {
  99. case O_LAST: /* not done unless go_to found */
  100. go_to = Nullch;
  101. retstr = &str_no;
  102. #ifdef DEBUGGING
  103. olddlevel = dlevel;
  104. #endif
  105. curspat = oldspat;
  106. #ifdef DEBUGGING
  107. if (debug & 4) {
  108. deb("(Popping label #%d %s)\n",loop_ptr,
  109. loop_stack[loop_ptr].loop_label);
  110. }
  111. #endif
  112. loop_ptr--;
  113. cmd = cmd->c_next;
  114. goto tail_recursion_entry;
  115. case O_NEXT: /* not done unless go_to found */
  116. go_to = Nullch;
  117. goto next_iter;
  118. case O_REDO: /* not done unless go_to found */
  119. go_to = Nullch;
  120. goto doit;
  121. }
  122. oldspat = curspat;
  123. #ifdef DEBUGGING
  124. olddlevel = dlevel;
  125. #endif
  126. if (cmd->ucmd.ccmd.cc_true) {
  127. #ifdef DEBUGGING
  128. debname[dlevel] = 't';
  129. debdelim[dlevel++] = '_';
  130. #endif
  131. cmd_exec(cmd->ucmd.ccmd.cc_true);
  132. }
  133. if (!goto_targ) {
  134. go_to = Nullch;
  135. goto next_iter;
  136. }
  137. #ifdef DEBUGGING
  138. dlevel = olddlevel;
  139. #endif
  140. if (cmd->ucmd.ccmd.cc_alt) {
  141. #ifdef DEBUGGING
  142. debname[dlevel] = 'a';
  143. debdelim[dlevel++] = '_';
  144. #endif
  145. cmd_exec(cmd->ucmd.ccmd.cc_alt);
  146. }
  147. if (goto_targ)
  148. break;
  149. go_to = Nullch;
  150. goto finish_while;
  151. }
  152. cmd = cmd->c_next;
  153. if (cmd && cmd->c_head == cmd) /* reached end of while loop */
  154. return retstr; /* targ isn't in this block */
  155. goto tail_recursion_entry;
  156. }
  157. }
  158. until_loop:
  159. #ifdef DEBUGGING
  160. if (debug & 2) {
  161. deb("%s (%lx) r%lx t%lx a%lx n%lx cs%lx\n",
  162. cmdname[cmd->c_type],cmd,cmd->c_expr,
  163. cmd->ucmd.ccmd.cc_true,cmd->ucmd.ccmd.cc_alt,cmd->c_next,curspat);
  164. }
  165. debname[dlevel] = cmdname[cmd->c_type][0];
  166. debdelim[dlevel++] = '!';
  167. #endif
  168. while (tmps_max >= 0) /* clean up after last eval */
  169. str_free(tmps_list[tmps_max--]);
  170. /* Here is some common optimization */
  171. if (cmdflags & CF_COND) {
  172. switch (cmdflags & CF_OPTIMIZE) {
  173. case CFT_FALSE:
  174. retstr = cmd->c_first;
  175. match = FALSE;
  176. if (cmdflags & CF_NESURE)
  177. goto maybe;
  178. break;
  179. case CFT_TRUE:
  180. retstr = cmd->c_first;
  181. match = TRUE;
  182. if (cmdflags & CF_EQSURE)
  183. goto flipmaybe;
  184. break;
  185. case CFT_REG:
  186. retstr = STAB_STR(cmd->c_stab);
  187. match = str_true(retstr); /* => retstr = retstr, c2 should fix */
  188. if (cmdflags & (match ? CF_EQSURE : CF_NESURE))
  189. goto flipmaybe;
  190. break;
  191. case CFT_ANCHOR: /* /^pat/ optimization */
  192. if (multiline) {
  193. if (*cmd->c_first->str_ptr && !(cmdflags & CF_EQSURE))
  194. goto scanner; /* just unanchor it */
  195. else
  196. break; /* must evaluate */
  197. }
  198. /* FALL THROUGH */
  199. case CFT_STROP: /* string op optimization */
  200. retstr = STAB_STR(cmd->c_stab);
  201. if (*cmd->c_first->str_ptr == *str_get(retstr) &&
  202. strnEQ(cmd->c_first->str_ptr, str_get(retstr),
  203. cmd->c_flen) ) {
  204. if (cmdflags & CF_EQSURE) {
  205. match = !(cmdflags & CF_FIRSTNEG);
  206. retstr = &str_yes;
  207. goto flipmaybe;
  208. }
  209. }
  210. else if (cmdflags & CF_NESURE) {
  211. match = cmdflags & CF_FIRSTNEG;
  212. retstr = &str_no;
  213. goto flipmaybe;
  214. }
  215. break; /* must evaluate */
  216. case CFT_SCAN: /* non-anchored search */
  217. scanner:
  218. retstr = STAB_STR(cmd->c_stab);
  219. if (instr(str_get(retstr),cmd->c_first->str_ptr)) {
  220. if (cmdflags & CF_EQSURE) {
  221. match = !(cmdflags & CF_FIRSTNEG);
  222. retstr = &str_yes;
  223. goto flipmaybe;
  224. }
  225. }
  226. else if (cmdflags & CF_NESURE) {
  227. match = cmdflags & CF_FIRSTNEG;
  228. retstr = &str_no;
  229. goto flipmaybe;
  230. }
  231. break; /* must evaluate */
  232. case CFT_GETS: /* really a while (<file>) */
  233. last_in_stab = cmd->c_stab;
  234. fp = last_in_stab->stab_io->fp;
  235. retstr = defstab->stab_val;
  236. if (fp && str_gets(retstr, fp)) {
  237. last_in_stab->stab_io->lines++;
  238. match = TRUE;
  239. }
  240. else if (last_in_stab->stab_io->flags & IOF_ARGV)
  241. goto doeval; /* doesn't necessarily count as EOF yet */
  242. else {
  243. retstr = &str_no;
  244. match = FALSE;
  245. }
  246. goto flipmaybe;
  247. case CFT_EVAL:
  248. break;
  249. case CFT_UNFLIP:
  250. retstr = eval(cmd->c_expr,Null(char***));
  251. match = str_true(retstr);
  252. if (cmd->c_expr->arg_type == O_FLIP) /* undid itself? */
  253. cmdflags = copyopt(cmd,cmd->c_expr[3].arg_ptr.arg_cmd);
  254. goto maybe;
  255. case CFT_CHOP:
  256. retstr = cmd->c_stab->stab_val;
  257. match = (retstr->str_cur != 0);
  258. tmps = str_get(retstr);
  259. tmps += retstr->str_cur - match;
  260. str_set(&str_chop,tmps);
  261. *tmps = '\0';
  262. retstr->str_nok = 0;
  263. retstr->str_cur = tmps - retstr->str_ptr;
  264. retstr = &str_chop;
  265. goto flipmaybe;
  266. }
  267. /* we have tried to make this normal case as abnormal as possible */
  268. doeval:
  269. retstr = eval(cmd->c_expr,Null(char***));
  270. match = str_true(retstr);
  271. goto maybe;
  272. /* if flipflop was true, flop it */
  273. flipmaybe:
  274. if (match && cmdflags & CF_FLIP) {
  275. if (cmd->c_expr->arg_type == O_FLOP) { /* currently toggled? */
  276. retstr = eval(cmd->c_expr,Null(char***)); /* let eval undo it */
  277. cmdflags = copyopt(cmd,cmd->c_expr[3].arg_ptr.arg_cmd);
  278. }
  279. else {
  280. retstr = eval(cmd->c_expr,Null(char***)); /* let eval do it */
  281. if (cmd->c_expr->arg_type == O_FLOP) /* still toggled? */
  282. cmdflags = copyopt(cmd,cmd->c_expr[4].arg_ptr.arg_cmd);
  283. }
  284. }
  285. else if (cmdflags & CF_FLIP) {
  286. if (cmd->c_expr->arg_type == O_FLOP) { /* currently toggled? */
  287. match = TRUE; /* force on */
  288. }
  289. }
  290. /* at this point, match says whether our expression was true */
  291. maybe:
  292. if (cmdflags & CF_INVERT)
  293. match = !match;
  294. if (!match && cmd->c_type != C_IF) {
  295. cmd = cmd->c_next;
  296. goto tail_recursion_entry;
  297. }
  298. }
  299. /* now to do the actual command, if any */
  300. switch (cmd->c_type) {
  301. case C_NULL:
  302. fatal("panic: cmd_exec\n");
  303. case C_EXPR: /* evaluated for side effects */
  304. if (cmd->ucmd.acmd.ac_expr) { /* more to do? */
  305. retstr = eval(cmd->ucmd.acmd.ac_expr,Null(char***));
  306. }
  307. break;
  308. case C_IF:
  309. oldspat = curspat;
  310. #ifdef DEBUGGING
  311. olddlevel = dlevel;
  312. #endif
  313. if (match) {
  314. retstr = &str_yes;
  315. if (cmd->ucmd.ccmd.cc_true) {
  316. #ifdef DEBUGGING
  317. debname[dlevel] = 't';
  318. debdelim[dlevel++] = '_';
  319. #endif
  320. retstr = cmd_exec(cmd->ucmd.ccmd.cc_true);
  321. }
  322. }
  323. else {
  324. retstr = &str_no;
  325. if (cmd->ucmd.ccmd.cc_alt) {
  326. #ifdef DEBUGGING
  327. debname[dlevel] = 'e';
  328. debdelim[dlevel++] = '_';
  329. #endif
  330. retstr = cmd_exec(cmd->ucmd.ccmd.cc_alt);
  331. }
  332. }
  333. curspat = oldspat;
  334. #ifdef DEBUGGING
  335. dlevel = olddlevel;
  336. #endif
  337. break;
  338. case C_BLOCK:
  339. case C_WHILE:
  340. if (!(cmdflags & CF_ONCE)) { /* first time through here? */
  341. cmdflags |= CF_ONCE;
  342. loop_ptr++;
  343. loop_stack[loop_ptr].loop_label = cmd->c_label;
  344. #ifdef DEBUGGING
  345. if (debug & 4) {
  346. deb("(Pushing label #%d %s)\n",
  347. loop_ptr,cmd->c_label);
  348. }
  349. #endif
  350. }
  351. switch (setjmp(loop_stack[loop_ptr].loop_env)) {
  352. case O_LAST:
  353. retstr = &str_no;
  354. curspat = oldspat;
  355. #ifdef DEBUGGING
  356. if (debug & 4) {
  357. deb("(Popping label #%d %s)\n",loop_ptr,
  358. loop_stack[loop_ptr].loop_label);
  359. }
  360. #endif
  361. loop_ptr--;
  362. cmd = cmd->c_next;
  363. goto tail_recursion_entry;
  364. case O_NEXT:
  365. goto next_iter;
  366. case O_REDO:
  367. #ifdef DEBUGGING
  368. dlevel = olddlevel;
  369. #endif
  370. goto doit;
  371. }
  372. oldspat = curspat;
  373. #ifdef DEBUGGING
  374. olddlevel = dlevel;
  375. #endif
  376. doit:
  377. if (cmd->ucmd.ccmd.cc_true) {
  378. #ifdef DEBUGGING
  379. debname[dlevel] = 't';
  380. debdelim[dlevel++] = '_';
  381. #endif
  382. cmd_exec(cmd->ucmd.ccmd.cc_true);
  383. }
  384. /* actually, this spot is never reached anymore since the above
  385. * cmd_exec() returns through longjmp(). Hooray for structure.
  386. */
  387. next_iter:
  388. #ifdef DEBUGGING
  389. dlevel = olddlevel;
  390. #endif
  391. if (cmd->ucmd.ccmd.cc_alt) {
  392. #ifdef DEBUGGING
  393. debname[dlevel] = 'a';
  394. debdelim[dlevel++] = '_';
  395. #endif
  396. cmd_exec(cmd->ucmd.ccmd.cc_alt);
  397. }
  398. finish_while:
  399. curspat = oldspat;
  400. #ifdef DEBUGGING
  401. dlevel = olddlevel - 1;
  402. #endif
  403. if (cmd->c_type != C_BLOCK)
  404. goto until_loop; /* go back and evaluate conditional again */
  405. }
  406. if (cmdflags & CF_LOOP) {
  407. cmdflags |= CF_COND; /* now test the condition */
  408. goto until_loop;
  409. }
  410. cmd = cmd->c_next;
  411. goto tail_recursion_entry;
  412. }
  413. #ifdef DEBUGGING
  414. /*VARARGS1*/
  415. deb(pat,a1,a2,a3,a4,a5,a6,a7,a8)
  416. char *pat;
  417. {
  418. register int i;
  419. for (i=0; i<dlevel; i++)
  420. fprintf(stderr,"%c%c ",debname[i],debdelim[i]);
  421. fprintf(stderr,pat,a1,a2,a3,a4,a5,a6,a7,a8);
  422. }
  423. #endif
  424. copyopt(cmd,which)
  425. register CMD *cmd;
  426. register CMD *which;
  427. {
  428. cmd->c_flags &= CF_ONCE|CF_COND|CF_LOOP;
  429. cmd->c_flags |= which->c_flags;
  430. cmd->c_first = which->c_first;
  431. cmd->c_flen = which->c_flen;
  432. cmd->c_stab = which->c_stab;
  433. return cmd->c_flags;
  434. }