From 0703ce71f36bcf479605f036546cadac7d5ec5e1 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Sat, 6 Jul 2019 05:07:43 +0000
Subject: [PATCH] unexport variable using '!' at the begin of a value intead of
 the begin of the key

---
 Makefile                        |  9 ---------
 doc/html/66-env.html            | 15 ++++-----------
 doc/html/execl-envfile.html     | 14 +++++++-------
 doc/man/66-env.1.scd            | 22 ++++++++--------------
 doc/man/execl-envfile.1.scd     | 18 +++++++++---------
 doc/man/execl-envfile.5.scd     |  4 ++--
 src/extra-tools/execl-envfile.c |  7 ++++---
 src/include/66/constants.h      |  2 ++
 src/lib66/environ.c             | 21 ++++++++++++++-------
 src/lib66/ssexec_env.c          |  8 +++++---
 10 files changed, 55 insertions(+), 65 deletions(-)

diff --git a/Makefile b/Makefile
index 415123cd..eb603f88 100644
--- a/Makefile
+++ b/Makefile
@@ -182,15 +182,6 @@ install-man:
 		install -m755 -d $(DESTDIR)$(mandir)/man$$i; \
 		install -m644 man/*.$$i $(DESTDIR)$(mandir)/man$$i/ ; \
 	done
-	
-# %.1: %.1.scd
-# 	scdoc < $@.scd > $@
-
-# %.5: %.5.scd
-# 	scdoc < $@.scd > $@
-	
-# %.8: %.8.scd
-# 	scdoc < $@.scd > $@
 
 .PHONY: it all clean distclean tgz strip install install-dynlib install-bin install-lib install-include man install-man
 
diff --git a/doc/html/66-env.html b/doc/html/66-env.html
index a373f84d..d363a7d5 100644
--- a/doc/html/66-env.html
+++ b/doc/html/66-env.html
@@ -72,7 +72,7 @@
 			<tt><b>-L</b></tt> : list defined environment variables for <em>service</em>.
 		</li>
 		<li> 
-			<tt><b>-r&nbsp;<em>key=value</em></b></tt> : override an existing <em>key=value</em> pair with the given one. The <em>key=value</em> needs to be double-quoted in case of multiple arguments. Backslash '\' should be used to properly parse a double-quoted element.
+			<tt><b>-r&nbsp;<em>key=value</em></b></tt> : override an existing <em>key=value</em> pair with the given one. The <em>key=value</em> needs to be double-quoted or single quoted in case of multiple arguments. In case of double quote backslash '\' should be used to properly parse a double-quoted element.
 		</li>
 	</ul>
 	
@@ -81,24 +81,17 @@
 		<ul>
 			<pre>66-env -L ntpd</pre>
 			<pre>66-env -r CMD_ARGS=-d </pre>
-			<pre>66-env -r "CMD_ARGS=-c /etc/nginx/nginx.conf -g \"daemon off;\""</pre>
+			<pre>66-env -r CMD_ARGS='-c /etc/nginx/nginx.conf -g "daemon off;"'</pre>
 		</ul>
 
 <h2>Notes</h2>
 
 		<p>
-			Keys which are preceded by an exclamation mark "!" in the configuration <em>service</em> file can be accessed as if they had none. For example, a <tt>key=value</tt> corresponding to:
+			Unexport a <em>key</em> from the environment can be handled by using an exclamation mark "!" at the begin of the <em>value</em>. For example, :
 		</p>
 		<ul>
-			<pre>!CMD_ARGS=--print-pid=4</pre>
+			<pre>66-env -r CMD_ARGS='!-c /etc/nginx/nginx.conf -g "daemon off;"'</pre>
 		</ul>
-		<p>can be replaced by the command:</p>
-		<ul>
-			<pre>66-env -r CMD_ARGS=--print-pid=3</pre>
-		</ul>
-		<p>
-			In this case the operator will be kept even if it was not included in the overwriting command.
-		</p>
 	</ul>
 </body>
 </html>
diff --git a/doc/html/execl-envfile.html b/doc/html/execl-envfile.html
index e0995808..296b0e9f 100644
--- a/doc/html/execl-envfile.html
+++ b/doc/html/execl-envfile.html
@@ -24,11 +24,11 @@
 <h2>Interface</h2>
 
 	<pre>
-	execl-envfile [ -h ] [ -f <em>file</em> ] [ -l ] <em>conf</em> <em>prog</em>
+	execl-envfile [ -h ] [ -f <em>file</em> ] [ -l ] <em>src</em> <em>prog</em>
 	</pre>
 
 	<p>
-		This tool expects to find a regular file or a directory in <em>conf</em> containing one or multiple <tt>key=value</tt> pair(s). It will parse that file, import the <tt>key=value</tt> and then exec the given <em>prog</em> with the modified environment. In case of directory for each file found it apply the same process.
+		This tool expects to find a regular file or a directory in <em>src</em> containing one or multiple <tt>key=value</tt> pair(s). It will parse that file, import the <tt>key=value</tt> and then exec the given <em>prog</em> with the modified environment. In case of directory for each file found it apply the same process.
 	</p>
 
 	<ul>
@@ -65,18 +65,18 @@
 		</li>
 
 		<li>
-			<tt><b>-f <em>file</em></b></tt> : only parse <em>file</em> found in <em>conf</em>. <b>deprecated option</b>
+			<tt><b>-f <em>file</em></b></tt> : only parse <em>file</em> found in <em>src</em>. <b>deprecated option</b>
 		</li>
 
 		<li>
-			<tt><b>-l</b></tt> : loose; do nothing and execute <em>prog</em> directly if <em>conf</em> does not contain any regular file(s) or <em>conf</em> does not exist.
+			<tt><b>-l</b></tt> : loose; do nothing and execute <em>prog</em> directly if <em>src</em> does not contain any regular file(s) or <em>src</em> does not exist.
 		</li>
 	</ul>
 
 <h2>File syntax</h2>
 
 	<p>
-		<em>conf</em> is a text file or a directory containing lines of pairs with the syntax being:<br><tt>key = value</tt><br>Whitespace is permitted before and after <em>key</em>, and before or after <em>value</em>. Quoting is also possible.
+		<em>src</em> is a text file or a directory containing lines of pairs with the syntax being:<br><tt>key = value</tt><br>Whitespace is permitted before and after <em>key</em>, and before or after <em>value</em>. Quoting is also possible.
 	</p>
 
 	<p>
@@ -88,7 +88,7 @@
 	</p>
 
 	<p>
-		If <tt>key</tt> begin by a <tt>'!'</tt> character: <tt>!key=value</tt> the <tt>key</tt> will be removed from the environment after the substitution.
+		If <tt>val</tt> begin by a <tt>'!'</tt> character: <tt>key=!value</tt> the <tt>key</tt> will be removed from the environment after the substitution.
 	</p>
 
 <h2>Example of use</h2>
@@ -120,7 +120,7 @@
 <h2>Limits</h2>
 
 	<p>
-		<em>conf</em> can not exceed more than 100 files. Each file can not contain more than 4095 bytes or more than 50 <em>key=value</em> pairs.
+		<em>src</em> can not exceed more than 100 files. Each file can not contain more than 4095 bytes or more than 50 <em>key=value</em> pairs.
 	</p>
 
 </body>
diff --git a/doc/man/66-env.1.scd b/doc/man/66-env.1.scd
index 1f33706b..13cd7cc4 100644
--- a/doc/man/66-env.1.scd
+++ b/doc/man/66-env.1.scd
@@ -2,7 +2,8 @@
 
 # NAME
 
-66-env - List or replace an environment variables of a service
+66-env - List or replaces an environment variables of a service depending
+on the options passed.
 
 # SYNOPSIS
 
@@ -39,7 +40,8 @@ It display the contain of the file or replace a _key=value_ pair if requested.
 *-r* _key=value_
 	Override an existing _key=value_ pair with the given one. The
 	_key=value_ needs to be double-quoted in case of multiple arguments.
-	Backslash *\\* should be used to properly parse a double-quoted element.
+	In case of double quote backslash *\\* should be used to properly 
+	parse a double-quoted element.
 
 # EXIT STATUS
 
@@ -52,20 +54,12 @@ It display the contain of the file or replace a _key=value_ pair if requested.
 ```
 66-env -L ntpd
 66-env -r CMD_ARGS=-d 
-66-env -r "CMD_ARGS=-c /etc/nginx/nginx.conf -g \"daemon off;\""
+66-env -r CMD_ARGS='-c /etc/nginx/nginx.conf -g "daemon off;"'
 ```
 
 # NOTES
 
-Keys which are preceded by an exclamation mark *!* in the configuration
-_service_ file can be accessed as if they had none. For example, a _key=value_
-corresponding to:
+Unexport a _key_ from the environment can be handled by using an exclamation
+mark *!* at the begin of the _value_. For example:
 
-	!CMD_ARGS=--print-pid=4
-
-can be replaced by the command:
-
-	66-env -r CMD_ARGS=--print-pid=3
-
-In this case the operator will be kept even if it was not included in the
-overwriting command.
+	66-env -r CMD_ARGS='!-c /etc/nginx/nginx.conf -g "daemon off;"'
diff --git a/doc/man/execl-envfile.1.scd b/doc/man/execl-envfile.1.scd
index 432257b2..93d8334b 100644
--- a/doc/man/execl-envfile.1.scd
+++ b/doc/man/execl-envfile.1.scd
@@ -6,17 +6,17 @@ execl-envfile - A blend of *s6-envdir* and *importas* program.
 
 # SYNOPSIS
 
-execl-envfile [ *-h* ] [ *-l* ] _dir_ _prog_
+execl-envfile [ *-h* ] [ *-l* ] _src_ _prog_
 
 # DESCRIPTION
 
-It reads files containing variable assignments on the given directory, adds the
+It reads files containing variable assignments on the given file/directory, adds the
 variables to the environment, then executes a program.
 
-This tool expects to find one or more regular file(s) in _dir_ containing
-one or multiple _key=value_ pair. For each file found: it will parse the file,
+This tool expects to find a regular file or a directory in _src_ containing
+one or multiple _key=value_ pair(s). It will parse that file,
 import the _key=value_ and then exec the given _prog_ with the modified
-environment.
+environment. In case of directory for each file found it apply the same process.
 
 - It opens and reads the file.
 - It parses the file.
@@ -31,18 +31,18 @@ environment.
 	Prints this help.
 
 *-f* _file_
-	Only parses the _file_ found in _dir_.
+	Only parses the _file_ found in _src_. *deprecated option*
 
 *-l*
-	Loose. Do nothing and executes _prog_ directly if _dir_ does not contain
-	any regular file(s) or _dir_ doesn't exist.
+	Loose. Do nothing and executes _prog_ directly if _src_ does not contain
+	any regular file(s) or _src_ doesn't exist.
 
 # EXAMPLE
 
 ```
 	#!/usr/bin/execlineb -P
 	fdmove -c 2 1
-	execl-envfile -f ntpd /etc/66/conf
+	execl-envfile /etc/66/conf/ntpd
 	foreground { mkdir -p  -m 0755 ${RUNDIR} } 
 	execl-cmdline -s { ntpd ${CMD_ARGS} }
 ```
diff --git a/doc/man/execl-envfile.5.scd b/doc/man/execl-envfile.5.scd
index 2ae2be44..091540d4 100644
--- a/doc/man/execl-envfile.5.scd
+++ b/doc/man/execl-envfile.5.scd
@@ -23,8 +23,8 @@ key = value # comment not valid
 ```
 
 Empty _value_ is *not* permitted.
-If _key_ begin by a *!* character:
-	*!*_key=value_
+If _val_ begin by a *!* character:
+	_key=*!*value_
 the _key_ will be removed from the environment after the substitution.
 
 # LIMITS
diff --git a/src/extra-tools/execl-envfile.c b/src/extra-tools/execl-envfile.c
index 169e41f2..ede12498 100644
--- a/src/extra-tools/execl-envfile.c
+++ b/src/extra-tools/execl-envfile.c
@@ -38,6 +38,7 @@
 
 #include <66/parser.h>
 #include <66/environ.h>
+#include <66/constants.h>
 
 static stralloc SAENV = STRALLOC_ZERO ;
 static genalloc GAENV = GENALLOC_ZERO ; //diuint32, pos in senv
@@ -193,7 +194,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
 	stralloc_free(&src) ;
 	
 	/** be able to freed the stralloc before existing */
-	char tmp[modifs.len] ;
+	char tmp[modifs.len+1] ;
 	memcpy(tmp,modifs.s,modifs.len) ;
 	tmp[modifs.len] = 0 ;
 	
@@ -207,9 +208,9 @@ int main (int argc, char const *const *argv, char const *const *envp)
 			unexport = 0 ;
 			int key = genalloc_s(diuint32,&GAENV)[i].left ;
 			int val = genalloc_s(diuint32,&GAENV)[i].right ;
-			if ((SAENV.s+key)[0] == '!')
+			if ((SAENV.s+val)[0] == SS_VAR_UNEXPORT)
 			{
-				key++ ;
+				val++ ;
 				unexport = 1 ;
 			}
 			if(!loop_stra(&info.vars,SAENV.s + key))
diff --git a/src/include/66/constants.h b/src/include/66/constants.h
index d08dbdff..60451e51 100644
--- a/src/include/66/constants.h
+++ b/src/include/66/constants.h
@@ -33,6 +33,7 @@
 #define SS_NOTIFICATION_LEN (sizeof SS_NOTIFICATION - 1)
 #define SS_MAXDEATHTALLY "max-death-tally"
 #define SS_MAXDEATHTALLY_LEN (sizeof SS_MAXDEATHTALLY - 1)
+
 /**tree dir*/
 #define SS_RULES "/rules"
 #define SS_RULES_LEN (sizeof SS_RULES - 1)
@@ -85,6 +86,7 @@
 #define SS_STATE "/state"
 #define SS_STATE_LEN (sizeof SS_STATE - 1)
 
+#define SS_VAR_UNEXPORT '!'
 /** boot */
 #define SS_BOOT_CONF "init.conf"
 #define SS_BOOT_CONF_LEN (sizeof SS_BOOT_CONF - 1)
diff --git a/src/lib66/environ.c b/src/lib66/environ.c
index 0dcf9cb9..66bbe8ad 100644
--- a/src/lib66/environ.c
+++ b/src/lib66/environ.c
@@ -32,6 +32,7 @@
 #include <66/parser.h>
 #include <66/environ.h>
 #include <66/utils.h>
+#include <66/constants.h>
 
 #include <execline/execline.h>
 /* @Return 1 on success
@@ -175,10 +176,16 @@ int env_parsenclean(stralloc *modifs,stralloc *src)
 		if (!parse_env(&tmp,&pos)) goto err ;
 		if (!env_clean(&tmp)) goto err ;
 		tmp.len--;//remove '0'
-		int u = 0 ;
-		if (tmp.s[0] == '!') u++ ;
-		if (!stralloc_catb(modifs,tmp.s + u ,(tmp.len - u) + 1)) goto err ;// ||
-//		!stralloc_0(modifs)) goto err ;
+		int r = get_len_until(tmp.s,'=') ;
+		if (tmp.s[r+1] == SS_VAR_UNEXPORT){
+			r++ ;
+			char t[tmp.len+1] ;
+			memcpy(t,tmp.s,r) ;
+			memcpy(t+r,tmp.s+r+1,tmp.len-(r+1)) ;
+			tmp.len--;
+			t[tmp.len] = 0 ;
+			if (!stralloc_catb(modifs,t,tmp.len + 1)) goto err ;
+		}else if (!stralloc_catb(modifs,tmp.s,tmp.len + 1)) goto err ;// ||
 	}
 	
 	genalloc_deepfree(stralist,&gatmp,stra_free) ;
@@ -225,13 +232,13 @@ int env_substitute(char const *key, char const *val,exlsn_t *info, char const *c
 	blah.var = info->vars.len ;
 	blah.value = info->values.len ;
 	
-	if (el_vardupl(key, info->vars.s, info->vars.len)) { strerr_warnw1x("bad substitution key") ; goto err ; }
-	if (!stralloc_catb(&info->vars,key, strlen(key) + 1)) { strerr_warnw1x("env_substitute") ; goto err ; }
+	if (el_vardupl(key, info->vars.s, info->vars.len)) { strerr_warnwu1x("el_vardupl") ; goto err ; }
+	if (!stralloc_catb(&info->vars,key, strlen(key) + 1)) { strerr_warnwu1x("append stralloc of env_substitute") ; goto err ; }
 	
 	x = env_get2(envp, key) ;
 	if (!x)
 	{
-		if (insist) { strerr_warnw2x(val,": is not set") ; goto err ; }
+		if (insist) { strerr_warnw2x(key,": is not set") ; goto err ; }
 		x = defaultval ;
 	}
 	else if (unexport)
diff --git a/src/lib66/ssexec_env.c b/src/lib66/ssexec_env.c
index 512e5b0c..6557d24e 100644
--- a/src/lib66/ssexec_env.c
+++ b/src/lib66/ssexec_env.c
@@ -83,9 +83,11 @@ int ssexec_env(int argc, char const *const *argv,char const *const *envp,ssexec_
 	else if (replace)
 	{
 		stralloc newlist = STRALLOC_ZERO ;
-		genalloc mga = GENALLOC_ZERO ;
+		// key=value from cmdline
+		genalloc mga = GENALLOC_ZERO ;// diuint32
 		stralloc msa = STRALLOC_ZERO ;
-		genalloc ga = GENALLOC_ZERO ;
+		// key=value from file
+		genalloc ga = GENALLOC_ZERO ;// diuint32
 		stralloc sa = STRALLOC_ZERO ;
 		char *key, *val, *mkey, *mval ;
 		if (!env_clean(&var)) strerr_diefu1x(111,"clean key=value pair") ;
@@ -100,7 +102,7 @@ int ssexec_env(int argc, char const *const *argv,char const *const *envp,ssexec_
 			if (!stralloc_cats(&newlist,key) ||
 			!stralloc_cats(&newlist,"=")) retstralloc(111,"replace") ;
 			
-			if (obstr_equal((key[0] == '!') ? key+1:key,mkey)) {
+			if (obstr_equal(key,mkey)) {
 				if (!stralloc_cats(&newlist,mval)) retstralloc(111,"replace") ;
 			}else if (!stralloc_cats(&newlist,val)) retstralloc(111,"replace") ;
 			if (!stralloc_cats(&newlist,"\n")) retstralloc(111,"replace") ;
-- 
GitLab