home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Src / loop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-03  |  5.6 KB  |  304 lines

  1. /*
  2.  * $Id: loop.c,v 2.4 1996/10/15 20:16:35 hzoli Exp $
  3.  *
  4.  * loop.c - loop execution
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * Copyright (c) 1992-1996 Paul Falstad
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and to distribute modified versions of this software for any
  14.  * purpose, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  *
  17.  * In no event shall Paul Falstad or the Zsh Development Group be liable
  18.  * to any party for direct, indirect, special, incidental, or consequential
  19.  * damages arising out of the use of this software and its documentation,
  20.  * even if Paul Falstad and the Zsh Development Group have been advised of
  21.  * the possibility of such damage.
  22.  *
  23.  * Paul Falstad and the Zsh Development Group specifically disclaim any
  24.  * warranties, including, but not limited to, the implied warranties of
  25.  * merchantability and fitness for a particular purpose.  The software
  26.  * provided hereunder is on an "as is" basis, and Paul Falstad and the
  27.  * Zsh Development Group have no obligation to provide maintenance,
  28.  * support, updates, enhancements, or modifications.
  29.  *
  30.  */
  31.  
  32. #include "zsh.h"
  33.  
  34. /**/
  35. int
  36. execfor(Cmd cmd)
  37. {
  38.     List list;
  39.     Forcmd node;
  40.     char *str;
  41.     LinkList args;
  42.  
  43.     loops++;
  44.     lastval = 0;
  45.     node = cmd->u.forcmd;
  46.     args = cmd->args;
  47.     if (!node->inflag) {
  48.     char **x;
  49.  
  50.     args = newlinklist();
  51.     for (x = pparams; *x; x++)
  52.         addlinknode(args, ztrdup(*x));
  53.     }
  54.     pushheap();
  55.     while ((str = (char *)ugetnode(args))) {
  56.     setsparam(node->name, ztrdup(str));
  57.     list = (List) dupstruct(node->list);
  58.     execlist(list, 1, (cmd->flags & CFLAG_EXEC) && empty(args));
  59.     if (breaks) {
  60.         breaks--;
  61.         if (breaks || !contflag)
  62.         break;
  63.         contflag = 0;
  64.     }
  65.     if (errflag) {
  66.         lastval = 1;
  67.         break;
  68.     }
  69.     freeheap();
  70.     }
  71.     popheap();
  72.     loops--;
  73.     return lastval;
  74. }
  75.  
  76. /**/
  77. int
  78. execselect(Cmd cmd)
  79. {
  80.     List list;
  81.     Forcmd node;
  82.     char *str, *s;
  83.     LinkList args;
  84.     LinkNode n;
  85.     int i;
  86.     FILE *inp;
  87.  
  88.     node = cmd->u.forcmd;
  89.     args = cmd->args;
  90.     if (!node->inflag) {
  91.     char **x;
  92.  
  93.     args = newlinklist();
  94.     for (x = pparams; *x; x++)
  95.         addlinknode(args, ztrdup(*x));
  96.     }
  97.     if (empty(args))
  98.     return 1;
  99.     loops++;
  100.     lastval = 0;
  101.     pushheap();
  102.     inp = fdopen(dup((SHTTY == -1) ? 0 : SHTTY), "r");
  103.     for (;;) {
  104.     do {
  105.         selectlist(args);
  106.         if (empty(bufstack)) {
  107.             if (interact && SHTTY != -1 && isset(USEZLE))
  108.             str = (char *)zleread(prompt3, NULL);
  109.             else {
  110.             int pptlen;
  111.             str = putprompt(prompt3, &pptlen, NULL, 1);
  112.             fwrite(str, pptlen, 1, stderr);
  113.             free(str);
  114.             fflush(stderr);
  115.             str = fgets(zalloc(256), 256, inp);
  116.             }
  117.         } else
  118.         str = (char *)getlinknode(bufstack);
  119.         if (!str || errflag) {
  120.         if (breaks)
  121.             breaks--;
  122.         fprintf(stderr, "\n");
  123.         fflush(stderr);
  124.         goto done;
  125.         }
  126.         if ((s = strchr(str, '\n')))
  127.         *s = '\0';
  128.     }
  129.     while (!*str);
  130.     setsparam("REPLY", ztrdup(str));
  131.     i = atoi(str);
  132.     if (!i)
  133.         str = "";
  134.     else {
  135.         for (i--, n = firstnode(args); n && i; incnode(n), i--);
  136.         if (n)
  137.         str = (char *) getdata(n);
  138.         else
  139.         str = "";
  140.     }
  141.     setsparam(node->name, ztrdup(str));
  142.     list = (List) dupstruct(node->list);
  143.     execlist(list, 1, 0);
  144.     freeheap();
  145.     if (breaks) {
  146.         breaks--;
  147.         if (breaks || !contflag)
  148.         break;
  149.         contflag = 0;
  150.     }
  151.     if (errflag)
  152.         break;
  153.     }
  154.   done:
  155.     popheap();
  156.     fclose(inp);
  157.     loops--;
  158.     return lastval;
  159. }
  160.  
  161. /**/
  162. int
  163. execwhile(Cmd cmd)
  164. {
  165.     List list;
  166.     struct whilecmd *node;
  167.     int olderrexit, oldval;
  168.  
  169.     olderrexit = noerrexit;
  170.     node = cmd->u.whilecmd;
  171.     oldval = 0;
  172.     pushheap();
  173.     loops++;
  174.     for (;;) {
  175.     list = (List) dupstruct(node->cont);
  176.     noerrexit = 1;
  177.     execlist(list, 1, 0);
  178.     noerrexit = olderrexit;
  179.     if (!((lastval == 0) ^ node->cond)) {
  180.         if (breaks)
  181.         breaks--;
  182.         lastval = oldval;
  183.         break;
  184.     }
  185.     list = (List) dupstruct(node->loop);
  186.     execlist(list, 1, 0);
  187.     if (breaks) {
  188.         breaks--;
  189.         if (breaks || !contflag)
  190.         break;
  191.         contflag = 0;
  192.     }
  193.     freeheap();
  194.     if (errflag) {
  195.         lastval = 1;
  196.         break;
  197.     }
  198.     oldval = lastval;
  199.     }
  200.     popheap();
  201.     loops--;
  202.     return lastval;
  203. }
  204.  
  205. /**/
  206. int
  207. execrepeat(Cmd cmd)
  208. {
  209.     List list;
  210.     int count;
  211.  
  212.     lastval = 0;
  213.     if (empty(cmd->args) || nextnode(firstnode(cmd->args))) {
  214.     zerr("bad argument for repeat", NULL, 0);
  215.     return 1;
  216.     }
  217.     count = atoi(peekfirst(cmd->args));
  218.     pushheap();
  219.     loops++;
  220.     while (count--) {
  221.     list = (List) dupstruct(cmd->u.list);
  222.     execlist(list, 1, 0);
  223.     freeheap();
  224.     if (breaks) {
  225.         breaks--;
  226.         if (breaks || !contflag)
  227.         break;
  228.         contflag = 0;
  229.     }
  230.     if (errflag) {
  231.         lastval = 1;
  232.         break;
  233.     }
  234.     }
  235.     popheap();
  236.     loops--;
  237.     return lastval;
  238. }
  239.  
  240. /**/
  241. int
  242. execif(Cmd cmd)
  243. {
  244.     struct ifcmd *node;
  245.     int olderrexit;
  246.     List *i, *t;
  247.  
  248.     olderrexit = noerrexit;
  249.     node = cmd->u.ifcmd;
  250.     i = node->ifls;
  251.     t = node->thenls;
  252.  
  253.     if (!noerrexit)
  254.     noerrexit = 1;
  255.     while (*i) {
  256.     execlist(*i, 1, 0);
  257.     if (!lastval)
  258.         break;
  259.     i++;
  260.     t++;
  261.     }
  262.     noerrexit = olderrexit;
  263.  
  264.     if (*t)
  265.     execlist(*t, 1, cmd->flags & CFLAG_EXEC);
  266.     else
  267.     lastval = 0;
  268.  
  269.     return lastval;
  270. }
  271.  
  272. /**/
  273. int
  274. execcase(Cmd cmd)
  275. {
  276.     struct casecmd *node;
  277.     char *word;
  278.     List *l;
  279.     char **p;
  280.  
  281.     node = cmd->u.casecmd;
  282.     l = node->lists;
  283.     p = node->pats;
  284.  
  285.     word = *p++;
  286.     singsub(&word);
  287.     untokenize(word);
  288.     lastval = 0;
  289.  
  290.     if (node) {
  291.     while (*p) {
  292.         singsub(p);
  293.         if (matchpat(word, *p))
  294.         break;
  295.         p++;
  296.         l++;
  297.     }
  298.     if (*l)
  299.         execlist(*l, 1, cmd->flags & CFLAG_EXEC);
  300.     }
  301.     return lastval;
  302. }
  303.  
  304.