svn commit: [26167] trunk/busybox/shell: hush_test/hush-misc

vda at busybox.net vda at busybox.net
Mon Apr 20 22:04:21 UTC 2009


Author: vda
Date: 2009-04-20 22:04:21 +0000 (Mon, 20 Apr 2009)
New Revision: 26167

Log:
hush: export -n support

function                                             old     new   delta
builtin_export                                       206     256     +50
set_local_var                                        248     265     +17
expand_variables                                    2204    2203      -1



Added:
   trunk/busybox/shell/hush_test/hush-misc/export-n.right
   trunk/busybox/shell/hush_test/hush-misc/export-n.tests

Modified:
   trunk/busybox/shell/Config.in
   trunk/busybox/shell/hush.c


Changeset:
Modified: trunk/busybox/shell/Config.in
===================================================================
--- trunk/busybox/shell/Config.in	2009-04-20 10:52:31 UTC (rev 26166)
+++ trunk/busybox/shell/Config.in	2009-04-20 22:04:21 UTC (rev 26167)
@@ -232,6 +232,13 @@
 	help
 	  Enable support for shell functions in hush. +800 bytes.
 
+config HUSH_EXPORT_N
+	bool "Support export '-n' option"
+	default n
+	depends on HUSH
+	help
+	  Enable support for export '-n' option in hush. It is a bash extension.
+
 config LASH
 	bool "lash (deprecated: aliased to hush)"
 	default n

Modified: trunk/busybox/shell/hush.c
===================================================================
--- trunk/busybox/shell/hush.c	2009-04-20 10:52:31 UTC (rev 26166)
+++ trunk/busybox/shell/hush.c	2009-04-20 22:04:21 UTC (rev 26167)
@@ -1253,10 +1253,10 @@
 /* str holds "NAME=VAL" and is expected to be malloced.
  * We take ownership of it.
  * flg_export:
- *  0: do not export
- *  1: export
- * -1: if NAME is set, leave export status alone
- *     if NAME is not set, do not export
+ *  0: do not change export flag
+ *     (if creating new variable, flag will be 0)
+ *  1: set export flag and putenv the variable
+ * -1: clear export flag and unsetenv the variable
  * flg_read_only is set only when we handle -R var=val
  */
 #if BB_MMU
@@ -1297,6 +1297,7 @@
 			free(str);
 			return -1;
 		}
+//TODO: optimize out redundant unsetenv/putenv's?
 		debug_printf_env("%s: unsetenv '%s'\n", __func__, str);
 		unsetenv(str); /* just in case */
 		*value = '=';
@@ -1331,8 +1332,13 @@
 	if (flg_export == 1)
 		cur->flg_export = 1;
 	if (cur->flg_export) {
-		debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr);
-		return putenv(cur->varstr);
+		if (flg_export == -1) {
+			cur->flg_export = 0;
+			/* unsetenv was already done */
+		} else {
+			debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr);
+			return putenv(cur->varstr);
+		}
 	}
 	return 0;
 }
@@ -2214,7 +2220,7 @@
 								val = NULL;
 							} else {
 								char *new_var = xasprintf("%s=%s", var, val);
-								set_local_var(new_var, -1, 0);
+								set_local_var(new_var, 0, 0);
 							}
 						}
 					}
@@ -6400,7 +6406,9 @@
 
 static int builtin_export(char **argv)
 {
-	if (*++argv == NULL) {
+	unsigned opt_unexport;
+
+	if (argv[1] == NULL) {
 		char **e = environ;
 		if (e) {
 			while (*e) {
@@ -6426,15 +6434,33 @@
 		return EXIT_SUCCESS;
 	}
 
+#if ENABLE_HUSH_EXPORT_N
+	opt_unexport = getopt32(argv, "+n"); /* "+": stop at 1st non-option */
+	argv += optind;
+#else
+	opt_unexport = 0;
+	argv++;
+#endif
+
 	do {
 		char *name = *argv;
 
-		/* So far we do not check that name is valid */
+		/* So far we do not check that name is valid (TODO?) */
+
 		if (strchr(name, '=') == NULL) {
-			/* Exporting a name without a =VALUE */
 			struct variable *var;
 
 			var = get_local_var(name);
+			if (opt_unexport) {
+				/* export -n NAME (without =VALUE) */
+				if (var) {
+					var->flg_export = 0;
+					debug_printf_env("%s: unsetenv '%s'\n", __func__, name);
+					unsetenv(name);
+				} /* else: export -n NOT_EXISTING_VAR: no-op */
+				continue;
+			}
+			/* export NAME (without =VALUE) */
 			if (var) {
 				var->flg_export = 1;
 				debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
@@ -6448,10 +6474,13 @@
 			 * We just set it to "" and export. */
 			name = xasprintf("%s=", name);
 		} else {
-			/* Exporting VAR=VALUE */
+			/* (Un)exporting NAME=VALUE */
 			name = xstrdup(name);
 		}
-		set_local_var(name, 1, 0);
+		set_local_var(name,
+			/*export:*/ (opt_unexport ? -1 : 1),
+			/*readonly:*/ 0
+		);
 	} while (*++argv);
 
 	return EXIT_SUCCESS;

Added: trunk/busybox/shell/hush_test/hush-misc/export-n.right
===================================================================
--- trunk/busybox/shell/hush_test/hush-misc/export-n.right	                        (rev 0)
+++ trunk/busybox/shell/hush_test/hush-misc/export-n.right	2009-04-20 22:04:21 UTC (rev 26167)
@@ -0,0 +1,10 @@
+export aaa1="'''"
+export aaa2=''
+export aaa3="'''"'abc'
+export aaa8='8'
+aaa9=9
+aaa10=10
+Nothing:
+Nothing:
+Nothing:
+Done

Added: trunk/busybox/shell/hush_test/hush-misc/export-n.tests
===================================================================
--- trunk/busybox/shell/hush_test/hush-misc/export-n.tests	                        (rev 0)
+++ trunk/busybox/shell/hush_test/hush-misc/export-n.tests	2009-04-20 22:04:21 UTC (rev 26167)
@@ -0,0 +1,37 @@
+export aaa1="'''"
+export aaa2=""
+export aaa3="'''abc"
+export | grep aaa.=
+
+export -n aaa1
+unset aaa2; export -n aaa2="ghi"
+export -n aaa3="klm"
+export | grep aaa.=
+
+export aaa4=4 aaa5=5
+export -n aaa4=4n
+export -n aaa5
+export | grep aaa.=
+
+export aaa5=5 aaa6=6 aaa7=7 aaa8=8
+export -n aaa5 aaa6=6n aaa7
+export | grep aaa.=
+
+aaa9=9
+export -n aaa9
+set | grep ^aaa9=
+
+export aaa10=10
+export -n aaa10
+set | grep ^aaa10=
+
+
+export EXPORTED=qwe
+export -nnnnnn nnnnnn; echo "Nothing:"; env | grep nnnnnn
+
+export -n EXPORTED=123; echo "Nothing:"; env | grep ^EXPORTED
+
+export EXPORTED=qwe
+export -n EXPORTED; EXPORTED=123; echo "Nothing:"; env | grep ^EXPORTED
+
+echo Done


Property changes on: trunk/busybox/shell/hush_test/hush-misc/export-n.tests
___________________________________________________________________
Added: svn:executable
   + *



More information about the busybox-cvs mailing list