Hi

On Thu, Aug 02, 2007 at 07:41:49AM +0200, Marc Haber wrote:
> On Thu, Aug 02, 2007 at 07:18:54AM +0200, Ola Lundqvist wrote:
> > On Wed, Aug 01, 2007 at 11:49:19PM +0200, Marc Haber wrote:
> > > I am afraid that your way of parsing the environment won't work. One
> > > of the difficulties is that one cannot easily distinguish between
> > > "unset variable" and "variable set with a null value", which would be
> > > needed to find out whether a variable is not meant to be overridden
> > > (unset), or meant to be overridden with a null value (empty).
> > 
> > I do not agree. Look at this:
> > 
> > [EMAIL PROTECTED]:~/minirc$ set | grep "^XX"
> > [EMAIL PROTECTED]:~/minirc$ XX=1
> > [EMAIL PROTECTED]:~/minirc$ set | grep "^XX"
> > XX=1
> > [EMAIL PROTECTED]:~/minirc$ XX=
> > [EMAIL PROTECTED]:~/minirc$ set | grep "^XX"
> > XX=
> > [EMAIL PROTECTED]:~/minirc$ unset XX
> > [EMAIL PROTECTED]:~/minirc$ set | grep "^XX"
> 
> That'll work, but is mucho ugly. I think that my way is much more
> elegant.

Maybe so. But not that easy to maintain (I think).

> > > I am therefore afraid that we won't get along without an explicit list
> > > of Variables needed for the override mechanism (since we need to know
> > > which variables to copy into the _THIS variables). In a first step,
> > > $ACTIONF is sourced to initiate the global overrides of the plain
> > > variables. We then copy the contents of FOO to FOO_THIS (which is the
> > > variable that will actually be used by this iteration's code) and
> > > source $ACTIONF again. This time, the FOO_THIS definitions inside
> > > $ACTIONF get in effect.
> > 
> > Well copy things from the normal variable to a this variable is
> > an other way. Then we do not need to restore...
> 
> Yes, and with the double source, we get both the temporaray and the
> permanent overrides.

Yes, that is what we get from my was as well. :)

> > > It was a long day for me, so I might be wrong, but you'll point me
> > > into the right direction.
> > 
> > I hope you have had a good night sleep by now. :)
> 
> Not quite. Looks like I'm sick today - my temperature is raised, my
> tonsils are swollen, I had a bad shiver last night when going to bed,
> and I need to go to Berlin today for a business trip.

Ouch! Not good. I really hope you will get better soon.

> > I can accept your patch below, if you can convince that the way
> > I get the list of variables do not work. I'm not right now. :)
> 
> Assuming that we stay with the "use _THIS variables in the code"
> approach, how would you get the list of unsuffixed variables? Would
> you rather copy the entire environment to _THIS variabls? Or how do
> you want to select which variables to copy and which not?

I do the other way around. I have prepared a patch that you can look at
actually. I have not tested it yet though, but it should work in
principle.

What it does is this:
In the beginning of the action loop just after the action config has
been sourced it stores variables.

In the end of the loop it restore it.

Storing of variables look like this:
* Get the list of *_HERE variables by using the 'set' command and grep for it.
* Calculate the name and the name for storing the old value.
* Store the old value.
* Set the new value.

Restoring is the same, but without storing.

The main difference between your and my way is:
* Your patch have a fixed set of variables
* Your patch copy that fixed set to a here name.
* My patch only copy the values that are temporary set from
  user. On the other hand I need to restore it.

Please take a look at the patch below and see what you think about it.
I have tested the functions and it seems to work well.

--- cron-apt    (revision 2437)
+++ cron-apt    (arbetskopia)
@@ -412,6 +412,37 @@
        done;
 }
 
+general_varmove() {
+    PREFIX=$1
+    STORE=$2
+    SIFS=$IFS
+    IFS="
+"
+    LIST=$(set | grep "^[a-zA-Z][a-zA-Z0-9_]*_$PREFIX" | sed -e "s|=.*||;")
+    for PVAR in $* ; do
+        # Var name
+       VVAR=$(echo $PVAR | sed -e "s|_$PREFIX$||g;")
+       if [ -n "$STORE" ] ; then
+            # Stored var name
+           eval "SVAR=${VVAR}_$STORE"
+            # Store the value
+           eval "$SVAR=\$$VAR"
+       fi
+        # Set variable to here variable
+       eval "$VVAR=\$$PVAR"
+       eval "unset $PVAR"
+    done
+    IFS=$SIFS
+}
+
+herevariables_store() {
+    general_varmove HERE STORED
+}
+
+herevariables_restore() {
+    general_varmove STORED ""
+}
+
 ############################### check 
######################################### 
 if ! [ -d "$LIBDIR/$CONFIGDIRNAME" ]; then
@@ -489,6 +520,7 @@
        echo "CRON-APT ACTION: $ACTIONF" > "$ACTIONMAIL"
        echo "CRON-APT ACTION: $ACTIONF" > "$ACTIONLOG"
        echo "CRON-APT ACTION: $ACTIONF" > "$ACTIONSYSLOG"
+       herevariables_store
        cat "$ACTIONDIR/$ACTIONF" | \
            sed -e "s/#.*$//;" | \
            grep -v "^[[:space:]]*$" | {
@@ -623,6 +655,7 @@
            fi
            rm -f "$STATUS"
        fi
+       herevariables_restore
 done
 
 onexit


Best regards,

// Ola

> Greetings
> Marc
> 
> -- 
> -----------------------------------------------------------------------------
> Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
> Mannheim, Germany  |  lose things."    Winona Ryder | Fon: *49 621 72739834
> Nordisch by Nature |  How to make an American Quilt | Fax: *49 3221 2323190
> 
> 

-- 
 --------------------- Ola Lundqvist ---------------------------
/  [EMAIL PROTECTED]                     Annebergsslingan 37      \
|  [EMAIL PROTECTED]                     654 65 KARLSTAD          |
|  http://opalsys.net/                 +46 (0)70-332 1551       |
\  gpg/f.p.: 7090 A92B 18FE 7994 0C36  4FE4 18A1 B1CF 0FE5 3DD9 /
 ---------------------------------------------------------------


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to