home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / ASH / ASH-LINU.2 / ASH-LINU / ash-linux-0.2 / var.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-04  |  13.0 KB  |  653 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. /*static char sccsid[] = "from: @(#)var.c    5.3 (Berkeley) 4/12/91";*/
  39. static char rcsid[] = "var.c,v 1.4 1993/08/01 18:57:58 mycroft Exp";
  40. #endif /* not lint */
  41.  
  42. /*
  43.  * Shell variables.
  44.  */
  45.  
  46. #include "shell.h"
  47. #include "output.h"
  48. #include "expand.h"
  49. #include "nodes.h"    /* for other headers */
  50. #include "eval.h"    /* defines cmdenviron */
  51. #include "exec.h"
  52. #include "syntax.h"
  53. #include "options.h"
  54. #include "mail.h"
  55. #include "var.h"
  56. #include "memalloc.h"
  57. #include "error.h"
  58. #include "mystring.h"
  59.  
  60.  
  61. #define VTABSIZE 39
  62.  
  63.  
  64. struct varinit {
  65.     struct var *var;
  66.     int flags;
  67.     char *text;
  68. };
  69.  
  70.  
  71. #if ATTY
  72. struct var vatty;
  73. #endif
  74. struct var vifs;
  75. struct var vmail;
  76. struct var vmpath;
  77. struct var vpath;
  78. struct var vps1;
  79. struct var vps2;
  80. struct var vvers;
  81. #if ATTY
  82. struct var vterm;
  83. #endif
  84.  
  85. const struct varinit varinit[] = {
  86. #if ATTY
  87.     {&vatty,    VSTRFIXED|VTEXTFIXED|VUNSET,    "ATTY="},
  88. #endif
  89.     {&vifs,    VSTRFIXED|VTEXTFIXED,        "IFS= \t\n"},
  90.     {&vmail,    VSTRFIXED|VTEXTFIXED|VUNSET,    "MAIL="},
  91.     {&vmpath,    VSTRFIXED|VTEXTFIXED|VUNSET,    "MAILPATH="},
  92.     {&vpath,    VSTRFIXED|VTEXTFIXED,        "PATH=:/bin:/usr/bin"},
  93.     /* 
  94.      * vps1 depends on uid
  95.      */
  96.     {&vps2,    VSTRFIXED|VTEXTFIXED,        "PS2=> "},
  97. #if ATTY
  98.     {&vterm,    VSTRFIXED|VTEXTFIXED|VUNSET,    "TERM="},
  99. #endif
  100.     {NULL,    0,                NULL}
  101. };
  102.  
  103. struct var *vartab[VTABSIZE];
  104.  
  105. STATIC void unsetvar __P((char *));
  106. STATIC struct var **hashvar __P((char *));
  107. STATIC int varequal __P((char *, char *));
  108.  
  109. /*
  110.  * Initialize the varable symbol tables and import the environment
  111.  */
  112.  
  113. #ifdef mkinit
  114. INCLUDE "var.h"
  115. INIT {
  116.     char **envp;
  117.     extern char **environ;
  118.  
  119.     initvar();
  120.     for (envp = environ ; *envp ; envp++) {
  121.         if (strchr(*envp, '=')) {
  122.             setvareq(*envp, VEXPORT|VTEXTFIXED);
  123.         }
  124.     }
  125. }
  126. #endif
  127.  
  128.  
  129. /*
  130.  * This routine initializes the builtin variables.  It is called when the
  131.  * shell is initialized and again when a shell procedure is spawned.
  132.  */
  133.  
  134. void
  135. initvar() {
  136.     const struct varinit *ip;
  137.     struct var *vp;
  138.     struct var **vpp;
  139.  
  140.     for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
  141.         if ((vp->flags & VEXPORT) == 0) {
  142.             vpp = hashvar(ip->text);
  143.             vp->next = *vpp;
  144.             *vpp = vp;
  145.             vp->text = ip->text;
  146.             vp->flags = ip->flags;
  147.         }
  148.     }
  149.     /*
  150.      * PS1 depends on uid
  151.      */
  152.     if ((vps1.flags & VEXPORT) == 0) {
  153.         vpp = hashvar("PS1=");
  154.         vps1.next = *vpp;
  155.         *vpp = &vps1;
  156.         vps1.text = getuid() ? "PS1=$ " : "PS1=# ";
  157.         vps1.flags = VSTRFIXED|VTEXTFIXED;
  158.     }
  159. }
  160.  
  161. /*
  162.  * Set the value of a variable.  The flags argument is ored with the
  163.  * flags of the variable.  If val is NULL, the variable is unset.
  164.  */
  165.  
  166. void
  167. setvar(name, val, flags)
  168.     char *name, *val;
  169.     {
  170.     char *p, *q;
  171.     int len;
  172.     int namelen;
  173.     char *nameeq;
  174.     int isbad;
  175.  
  176.     isbad = 0;
  177.     p = name;
  178.     if (! is_name(*p++))
  179.         isbad = 1;
  180.     for (;;) {
  181.         if (! is_in_name(*p)) {
  182.             if (*p == '\0' || *p == '=')
  183.                 break;
  184.             isbad = 1;
  185.         }
  186.         p++;
  187.     }
  188.     namelen = p - name;
  189.     if (isbad)
  190.         error("%.*s: is read only", namelen, name);
  191.     len = namelen + 2;        /* 2 is space for '=' and '\0' */
  192.     if (val == NULL) {
  193.         flags |= VUNSET;
  194.     } else {
  195.         len += strlen(val);
  196.     }
  197.     p = nameeq = ckmalloc(len);
  198.     q = name;
  199.     while (--namelen >= 0)
  200.         *p++ = *q++;
  201.     *p++ = '=';
  202.     *p = '\0';
  203.     if (val)
  204.         scopy(val, p);
  205.     setvareq(nameeq, flags);
  206. }
  207.  
  208.  
  209.  
  210. /*
  211.  * Same as setvar except that the variable and value are passed in
  212.  * the first argument as name=value.  Since the first argument will
  213.  * be actually stored in the table, it should not be a string that
  214.  * will go away.
  215.  */
  216.  
  217. void
  218. setvareq(s, flags)
  219.     char *s;
  220.     {
  221.     struct var *vp, **vpp;
  222.  
  223.     vpp = hashvar(s);
  224.     for (vp = *vpp ; vp ; vp = vp->next) {
  225.         if (varequal(s, vp->text)) {
  226.             if (vp->flags & VREADONLY) {
  227.                 int len = strchr(s, '=') - s;
  228.                 error("%.*s: is read only", len, s);
  229.             }
  230.             INTOFF;
  231.             if (vp == &vpath)
  232.                 changepath(s + 5);    /* 5 = strlen("PATH=") */
  233.             if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
  234.                 ckfree(vp->text);
  235.             vp->flags &=~ (VTEXTFIXED|VSTACK|VUNSET);
  236.             vp->flags |= flags;
  237.             vp->text = s;
  238.             if (iflag && (vp == &vmpath || (vp == &vmail && ! mpathset())))
  239.                 chkmail(1);
  240.             INTON;
  241.             return;
  242.         }
  243.     }
  244.     /* not found */
  245.     vp = ckmalloc(sizeof (*vp));
  246.     vp->flags = flags;
  247.     vp->text = s;
  248.     vp->next = *vpp;
  249.     *vpp = vp;
  250. }
  251.  
  252.  
  253.  
  254. /*
  255.  * Process a linked list of variable assignments.
  256.  */
  257.  
  258. void
  259. listsetvar(list)
  260.     struct strlist *list;
  261.     {
  262.     struct strlist *lp;
  263.  
  264.     INTOFF;
  265.     for (lp = list ; lp ; lp = lp->next) {
  266.         setvareq(savestr(lp->text), 0);
  267.     }
  268.     INTON;
  269. }
  270.  
  271.  
  272.  
  273. /*
  274.  * Find the value of a variable.  Returns NULL if not set.
  275.  */
  276.  
  277. char *
  278. lookupvar(name)
  279.     char *name;
  280.     {
  281.     struct var *v;
  282.  
  283.     for (v = *hashvar(name) ; v ; v = v->next) {
  284.         if (varequal(v->text, name)) {
  285.             if (v->flags & VUNSET)
  286.                 break;
  287.             return strchr(v->text, '=') + 1;
  288.         }
  289.     }
  290.     if (uflag)
  291.         error("%.*s: variable not set", strchr(name, '=') - name, name);
  292.     return NULL;
  293. }
  294.  
  295.  
  296.  
  297. /*
  298.  * Search the environment of a builtin command.  If the second argument
  299.  * is nonzero, return the value of a variable even if it hasn't been
  300.  * exported.
  301.  */
  302.  
  303. char *
  304. bltinlookup(name, doall)
  305.     char *name;
  306.     {
  307.     struct strlist *sp;
  308.     struct var *v;
  309.  
  310.     for (sp = cmdenviron ; sp ; sp = sp->next) {
  311.         if (varequal(sp->text, name))
  312.             return strchr(sp->text, '=') + 1;
  313.     }
  314.     for (v = *hashvar(name) ; v ; v = v->next) {
  315.         if (varequal(v->text, name)) {
  316.             if (v->flags & VUNSET
  317.              || ! doall && (v->flags & VEXPORT) == 0)
  318.                 return NULL;
  319.             return strchr(v->text, '=') + 1;
  320.         }
  321.     }
  322.     return NULL;
  323. }
  324.  
  325.  
  326.  
  327. /*
  328.  * Generate a list of exported variables.  This routine is used to construct
  329.  * the third argument to execve when executing a program.
  330.  */
  331.  
  332. char **
  333. environment() {
  334.     int nenv;
  335.     struct var **vpp;
  336.     struct var *vp;
  337.     char **env, **ep;
  338.  
  339.     nenv = 0;
  340.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  341.         for (vp = *vpp ; vp ; vp = vp->next)
  342.             if (vp->flags & VEXPORT)
  343.                 nenv++;
  344.     }
  345.     ep = env = stalloc((nenv + 1) * sizeof *env);
  346.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  347.         for (vp = *vpp ; vp ; vp = vp->next)
  348.             if (vp->flags & VEXPORT)
  349.                 *ep++ = vp->text;
  350.     }
  351.     *ep = NULL;
  352.     return env;
  353. }
  354.  
  355.  
  356. /*
  357.  * Called when a shell procedure is invoked to clear out nonexported
  358.  * variables.  It is also necessary to reallocate variables of with
  359.  * VSTACK set since these are currently allocated on the stack.
  360.  */
  361.  
  362. #ifdef mkinit
  363. MKINIT void shprocvar();
  364.  
  365. SHELLPROC {
  366.     shprocvar();
  367. }
  368. #endif
  369.  
  370. void
  371. shprocvar() {
  372.     struct var **vpp;
  373.     struct var *vp, **prev;
  374.  
  375.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  376.         for (prev = vpp ; (vp = *prev) != NULL ; ) {
  377.             if ((vp->flags & VEXPORT) == 0) {
  378.                 *prev = vp->next;
  379.                 if ((vp->flags & VTEXTFIXED) == 0)
  380.                     ckfree(vp->text);
  381.                 if ((vp->flags & VSTRFIXED) == 0)
  382.                     ckfree(vp);
  383.             } else {
  384.                 if (vp->flags & VSTACK) {
  385.                     vp->text = savestr(vp->text);
  386.                     vp->flags &=~ VSTACK;
  387.                 }
  388.                 prev = &vp->next;
  389.             }
  390.         }
  391.     }
  392.     initvar();
  393. }
  394.  
  395.  
  396.  
  397. /*
  398.  * Command to list all variables which are set.  Currently this command
  399.  * is invoked from the set command when the set command is called without
  400.  * any variables.
  401.  */
  402.  
  403. int
  404. showvarscmd(argc, argv)  char **argv; {
  405.     struct var **vpp;
  406.     struct var *vp;
  407.  
  408.     for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  409.         for (vp = *vpp ; vp ; vp = vp->next) {
  410.             if ((vp->flags & VUNSET) == 0)
  411.                 out1fmt("%s\n", vp->text);
  412.         }
  413.     }
  414.     return 0;
  415. }
  416.  
  417.  
  418.  
  419. /*
  420.  * The export and readonly commands.
  421.  */
  422.  
  423. int
  424. exportcmd(argc, argv)  char **argv; {
  425.     struct var **vpp;
  426.     struct var *vp;
  427.     char *name;
  428.     char *p;
  429.     int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
  430.  
  431.     listsetvar(cmdenviron);
  432.     if (argc > 1) {
  433.         while ((name = *argptr++) != NULL) {
  434.             if ((p = strchr(name, '=')) != NULL) {
  435.                 p++;
  436.             } else {
  437.                 vpp = hashvar(name);
  438.                 for (vp = *vpp ; vp ; vp = vp->next) {
  439.                     if (varequal(vp->text, name)) {
  440.                         vp->flags |= flag;
  441.                         goto found;
  442.                     }
  443.                 }
  444.             }
  445.             setvar(name, p, flag);
  446. found:;
  447.         }
  448.     } else {
  449.         for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
  450.             for (vp = *vpp ; vp ; vp = vp->next) {
  451.                 if (vp->flags & flag) {
  452.                     for (p = vp->text ; *p != '=' ; p++)
  453.                         out1c(*p);
  454.                     out1c('\n');
  455.                 }
  456.             }
  457.         }
  458.     }
  459.     return 0;
  460. }
  461.  
  462.  
  463. /*
  464.  * The "local" command.
  465.  */
  466.  
  467. localcmd(argc, argv)  char **argv; {
  468.     char *name;
  469.  
  470.     if (! in_function())
  471.         error("Not in a function");
  472.     while ((name = *argptr++) != NULL) {
  473.         mklocal(name);
  474.     }
  475.     return 0;
  476. }
  477.  
  478.  
  479. /*
  480.  * Make a variable a local variable.  When a variable is made local, it's
  481.  * value and flags are saved in a localvar structure.  The saved values
  482.  * will be restored when the shell function returns.  We handle the name
  483.  * "-" as a special case.
  484.  */
  485.  
  486. void
  487. mklocal(name)
  488.     char *name;
  489.     {
  490.     struct localvar *lvp;
  491.     struct var **vpp;
  492.     struct var *vp;
  493.  
  494.     INTOFF;
  495.     lvp = ckmalloc(sizeof (struct localvar));
  496.     if (name[0] == '-' && name[1] == '\0') {
  497.         lvp->text = ckmalloc(sizeof optval);
  498.         bcopy(optval, lvp->text, sizeof optval);
  499.         vp = NULL;
  500.     } else {
  501.         vpp = hashvar(name);
  502.         for (vp = *vpp ; vp && ! varequal(vp->text, name) ; vp = vp->next);
  503.         if (vp == NULL) {
  504.             if (strchr(name, '='))
  505.                 setvareq(savestr(name), VSTRFIXED);
  506.             else
  507.                 setvar(name, NULL, VSTRFIXED);
  508.             vp = *vpp;    /* the new variable */
  509.             lvp->text = NULL;
  510.             lvp->flags = VUNSET;
  511.         } else {
  512.             lvp->text = vp->text;
  513.             lvp->flags = vp->flags;
  514.             vp->flags |= VSTRFIXED|VTEXTFIXED;
  515.             if (strchr(name, '='))
  516.                 setvareq(savestr(name), 0);
  517.         }
  518.     }
  519.     lvp->vp = vp;
  520.     lvp->next = localvars;
  521.     localvars = lvp;
  522.     INTON;
  523. }
  524.  
  525.  
  526. /*
  527.  * Called after a function returns.
  528.  */
  529.  
  530. void
  531. poplocalvars() {
  532.     struct localvar *lvp;
  533.     struct var *vp;
  534.  
  535.     while ((lvp = localvars) != NULL) {
  536.         localvars = lvp->next;
  537.         vp = lvp->vp;
  538.         if (vp == NULL) {    /* $- saved */
  539.             bcopy(lvp->text, optval, sizeof optval);
  540.             ckfree(lvp->text);
  541.         } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
  542.             unsetvar(vp->text);
  543.         } else {
  544.             if ((vp->flags & VTEXTFIXED) == 0)
  545.                 ckfree(vp->text);
  546.             vp->flags = lvp->flags;
  547.             vp->text = lvp->text;
  548.         }
  549.         ckfree(lvp);
  550.     }
  551. }
  552.  
  553.  
  554. setvarcmd(argc, argv)  char **argv; {
  555.     if (argc <= 2)
  556.         return unsetcmd(argc, argv);
  557.     else if (argc == 3)
  558.         setvar(argv[1], argv[2], 0);
  559.     else
  560.         error("List assignment not implemented");
  561.     return 0;
  562. }
  563.  
  564.  
  565. /*
  566.  * The unset builtin command.  We unset the function before we unset the
  567.  * variable to allow a function to be unset when there is a readonly variable
  568.  * with the same name.
  569.  */
  570.  
  571. unsetcmd(argc, argv)  char **argv; {
  572.     char **ap;
  573.  
  574.     for (ap = argv + 1 ; *ap ; ap++) {
  575.         unsetfunc(*ap);
  576.         unsetvar(*ap);
  577.     }
  578.     return 0;
  579. }
  580.  
  581.  
  582. /*
  583.  * Unset the specified variable.
  584.  */
  585.  
  586. STATIC void
  587. unsetvar(s)
  588.     char *s;
  589.     {
  590.     struct var **vpp;
  591.     struct var *vp;
  592.  
  593.     vpp = hashvar(s);
  594.     for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
  595.         if (varequal(vp->text, s)) {
  596.             INTOFF;
  597.             if (*(strchr(vp->text, '=') + 1) != '\0'
  598.              || vp->flags & VREADONLY) {
  599.                 setvar(s, nullstr, 0);
  600.             }
  601.             vp->flags &=~ VEXPORT;
  602.             vp->flags |= VUNSET;
  603.             if ((vp->flags & VSTRFIXED) == 0) {
  604.                 if ((vp->flags & VTEXTFIXED) == 0)
  605.                     ckfree(vp->text);
  606.                 *vpp = vp->next;
  607.                 ckfree(vp);
  608.             }
  609.             INTON;
  610.             return;
  611.         }
  612.     }
  613. }
  614.  
  615.  
  616.  
  617. /*
  618.  * Find the appropriate entry in the hash table from the name.
  619.  */
  620.  
  621. STATIC struct var **
  622. hashvar(p)
  623.     register char *p;
  624.     {
  625.     unsigned int hashval;
  626.  
  627.     hashval = *p << 4;
  628.     while (*p && *p != '=')
  629.         hashval += *p++;
  630.     return &vartab[hashval % VTABSIZE];
  631. }
  632.  
  633.  
  634.  
  635. /*
  636.  * Returns true if the two strings specify the same varable.  The first
  637.  * variable name is terminated by '='; the second may be terminated by
  638.  * either '=' or '\0'.
  639.  */
  640.  
  641. STATIC int
  642. varequal(p, q)
  643.     register char *p, *q;
  644.     {
  645.     while (*p == *q++) {
  646.         if (*p++ == '=')
  647.             return 1;
  648.     }
  649.     if (*p == '=' && *(q - 1) == '\0')
  650.         return 1;
  651.     return 0;
  652. }
  653.