home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / zsh / Source / src / loop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-07  |  5.1 KB  |  292 lines

  1. /*
  2.  *
  3.  * loop.c - loop execution
  4.  *
  5.  * This file is part of zsh, the Z shell.
  6.  *
  7.  * This software is Copyright 1992 by Paul Falstad
  8.  *
  9.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  10.  * use this software as long as: there is no monetary profit gained
  11.  * specifically from the use or reproduction of this software, it is not
  12.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  13.  * included prominently in any copy made.
  14.  *
  15.  * The author make no claims as to the fitness or correctness of this software
  16.  * for any use whatsoever, and it is provided as is. Any use of this software
  17.  * is at the user's own risk.
  18.  *
  19.  */
  20.  
  21. #include "zsh.h"
  22.  
  23. int execfor(cmd)        /**/
  24. Cmd cmd;
  25. {
  26.     List list;
  27.     struct forcmd *node;
  28.     char *str;
  29.     Lklist args;
  30.     int cj = thisjob;
  31.  
  32.     loops++;
  33.     exiting = 0;
  34.     node = cmd->u.forcmd;
  35.     args = cmd->args;
  36.     if (!node->inflag) {
  37.     char **x;
  38.  
  39.     args = newlist();
  40.     for (x = pparams; *x; x++)
  41.         addnode(args, ztrdup(*x));
  42.     }
  43.     pushheap();
  44.     while ((str = (char *)ugetnode(args))) {
  45.     setsparam(node->name, ztrdup(str));
  46.     list = (List) dupstruct(node->list);
  47.     execlist(list);
  48.     if (breaks) {
  49.         breaks--;
  50.         if (breaks || !contflag)
  51.         break;
  52.         contflag = 0;
  53.     }
  54.     if (errflag) {
  55.         lastval = 1;
  56.         break;
  57.     }
  58.     freeheap();
  59.     }
  60.     popheap();
  61.     thisjob = cj;
  62.     loops--;
  63.     return lastval;
  64. }
  65.  
  66. int execselect(cmd)        /**/
  67. Cmd cmd;
  68. {
  69.     List list;
  70.     struct forcmd *node;
  71.     char *str, *s;
  72.     Lklist args;
  73.     Lknode n;
  74.     int cj = thisjob, t0;
  75.     FILE *inp;
  76.  
  77.     node = cmd->u.forcmd;
  78.     args = cmd->args;
  79.     if (!node->inflag) {
  80.     char **x;
  81.  
  82.     args = newlist();
  83.     for (x = pparams; *x; x++)
  84.         addnode(args, ztrdup(*x));
  85.     }
  86.     if (empty(args))
  87.     return 1;
  88.     loops++;
  89.     exiting = 0;
  90.     pushheap();
  91.     inp = fdopen(dup((SHTTY == -1) ? 0 : SHTTY), "r");
  92.     for (;;) {
  93.     do {
  94.         int pl;
  95.  
  96.         selectlist(args);
  97.         str = putprompt(prompt3, &pl, 0);
  98.         if (full(bufstack))
  99.         str = (char *)getnode(bufstack);
  100.         else if (interact && SHTTY != -1 && isset(USEZLE)) {
  101.         str = (char *)zleread((unsigned char *)str, NULL, pl, 0);
  102.         } else {
  103.         fprintf(stderr, "%s", str);
  104.         fflush(stderr);
  105.         str = fgets(zalloc(256), 256, inp);
  106.         }
  107.         if (!str || errflag) {
  108.         if (breaks)
  109.             breaks--;
  110.         fprintf(stderr, "\n");
  111.         fflush(stderr);
  112.         goto done;
  113.         }
  114.         if ((s = strchr(str, '\n')))
  115.         *s = '\0';
  116.     }
  117.     while (!*str);
  118.     setsparam("REPLY", ztrdup(str));
  119.     t0 = atoi(str);
  120.     if (!t0)
  121.         str = "";
  122.     else {
  123.         for (t0--, n = firstnode(args); n && t0; incnode(n), t0--);
  124.         if (n)
  125.         str = (char *)getdata(n);
  126.         else
  127.         str = "";
  128.     }
  129.     setsparam(node->name, ztrdup(str));
  130.     list = (List) dupstruct(node->list);
  131.     execlist(list);
  132.     freeheap();
  133.     if (breaks) {
  134.         breaks--;
  135.         if (breaks || !contflag)
  136.         break;
  137.         contflag = 0;
  138.     }
  139.     if (errflag)
  140.         break;
  141.     }
  142.   done:
  143.     popheap();
  144.     fclose(inp);
  145.     thisjob = cj;
  146.     loops--;
  147.     return lastval;
  148. }
  149.  
  150. int execwhile(cmd)        /**/
  151. Cmd cmd;
  152. {
  153.     List list;
  154.     struct whilecmd *node;
  155.     int cj = thisjob;
  156.  
  157.     node = cmd->u.whilecmd;
  158.     exiting = 0;
  159.     pushheap();
  160.     loops++;
  161.     for (;;) {
  162.     int oldval = lastval;
  163.     list = (List) dupstruct(node->cont);
  164.     execlist(list);
  165.     if (!((lastval == 0) ^ node->cond)) {
  166.         if (breaks)
  167.         breaks--;
  168.         lastval = oldval;
  169.         break;
  170.     }
  171.     list = (List) dupstruct(node->loop);
  172.     execlist(list);
  173.     if (breaks) {
  174.         breaks--;
  175.         if (breaks || !contflag)
  176.         break;
  177.         contflag = 0;
  178.     }
  179.     freeheap();
  180.     if (errflag) {
  181.         lastval = 1;
  182.         break;
  183.     }
  184.     }
  185.     popheap();
  186.     thisjob = cj;
  187.     loops--;
  188.     return lastval;
  189. }
  190.  
  191. int execrepeat(cmd)        /**/
  192. Cmd cmd;
  193. {
  194.     List list;
  195.     int cj = thisjob, count;
  196.  
  197.     exiting = 0;
  198.     if (empty(cmd->args) || nextnode(firstnode(cmd->args))) {
  199.     zerr("bad argument for repeat", NULL, 0);
  200.     return 1;
  201.     }
  202.     count = atoi(peekfirst(cmd->args));
  203.     pushheap();
  204.     loops++;
  205.     while (count--) {
  206.     list = (List) dupstruct(cmd->u.list);
  207.     execlist(list);
  208.     freeheap();
  209.     if (breaks) {
  210.         breaks--;
  211.         if (breaks || !contflag)
  212.         break;
  213.         contflag = 0;
  214.     }
  215.     if (errflag) {
  216.         lastval = 1;
  217.         break;
  218.     }
  219.     }
  220.     popheap();
  221.     thisjob = cj;
  222.     loops--;
  223.     return lastval;
  224. }
  225.  
  226. int execif(cmd)            /**/
  227. Cmd cmd;
  228. {
  229.     struct ifcmd *node;
  230.     int cj = thisjob;
  231.     List *i, *t;
  232.  
  233.     node = cmd->u.ifcmd;
  234.     exiting = 0;
  235.     i = node->ifls;
  236.     t = node->thenls;
  237.  
  238.     while (*i) {
  239.     execlist(*i);
  240.     if (!lastval)
  241.         break;
  242.     i++;
  243.     t++;
  244.     }
  245.  
  246.     if (*t)
  247.     execlist(*t);
  248.  
  249.     thisjob = cj;
  250.     return lastval;
  251. }
  252.  
  253. int execcase(cmd)        /**/
  254. Cmd cmd;
  255. {
  256.     struct casecmd *node;
  257.     char *word;
  258.     Lklist args;
  259.     int cj = thisjob;
  260.     List *l;
  261.     char **p;
  262.  
  263.     node = cmd->u.casecmd;
  264.     args = cmd->args;
  265.     l = node->lists;
  266.     p = node->pats;
  267.  
  268.     exiting = 0;
  269.     if (firstnode(args) && nextnode(firstnode(args))) {
  270.     zerr("too many arguments to case", NULL, 0);
  271.     return 1;
  272.     }
  273.     if (empty(args))
  274.     word = dupstring("");
  275.     else
  276.     word = (char *)peekfirst(args);
  277.  
  278.     if (node) {
  279.     while (*p) {
  280.         singsub(p);
  281.         if (matchpat(word, *p))
  282.         break;
  283.         p++;
  284.         l++;
  285.     }
  286.     if (*l)
  287.         execlist(*l);
  288.     }
  289.     thisjob = cj;
  290.     return lastval;
  291. }
  292.