[BusyBox] sh valid char for variable

Vladimir N. Oleynik dzo at simtreas.ru
Sat Mar 10 12:56:15 UTC 2001


Larry and Erik.

I see good work with sh.c
But:
strpbrk(dst+1, " \t~`!$^&*()=|\\[];\"'<>?./"); is VERY strange.
See patch to sh.c base from real sh parser.


--w
vodz
-------------- next part --------------
--- sh.c~	Sat Mar 10 15:24:24 2001
+++ sh.c	Sat Mar 10 15:49:32 2001
@@ -613,10 +613,7 @@
  */
 static int run_command_predicate(char *cmd)
 {
-	int n=strlen(cmd);
-	local_pending_command = xmalloc(n+1);
-	strncpy(local_pending_command, cmd, n); 
-	local_pending_command[n]='\0';
+	local_pending_command = xstrdup(cmd);
 	return( busy_loop(NULL));
 }
 #endif
@@ -916,7 +913,7 @@
 	expand_t expand_result;
 	char *src, *dst, *var;
 	int i=0, length, total_length=0, retval;
-	const char *out_of_space = "out of space during expansion"; 
+	static const char out_of_space[] = "out of space during expansion";
 #endif
 
 	/* get rid of the terminating \n */
@@ -1058,25 +1055,30 @@
 		} else {
 			/* Looks like an environment variable */
 			char delim_hold;
-			int num_skip_chars=1;
+			int num_skip_chars=0;
 			int dstlen = strlen(dst);
 			/* Is this a ${foo} type variable? */
 			if (dstlen >=2 && *(dst+1) == '{') {
 				src=strchr(dst+1, '}');
-				num_skip_chars=2;
+				num_skip_chars++;
 			} else {
-				src=strpbrk(dst+1, " \t~`!$^&*()=|\\[];\"'<>?./");
+				src = dst+1;
+				if(isascii(*src) && (isalpha(*src) || *src == '_')) {
+					src++;
+					while(isascii(*src) && (isalpha(*src)
+						|| *src == '_'
+						|| isdigit(*src)))
+							src++;
+				} else {
+					src=NULL;
 			}
 			if (src == NULL) {
 				src = dst+dstlen;
 			}
-			delim_hold=*src;
+			delim_hold = *src;
 			*src='\0';  /* temporary */
-			var = getenv(dst + num_skip_chars);
-			*src=delim_hold;
-			if (num_skip_chars==2) {
-				src++;
-			}
+			var = getenv(dst + num_skip_chars + 1);
+			*src = delim_hold + num_skip_chars;
 		}
 		if (var == NULL) {
 			/* Seems we got an un-expandable variable.  So delete it. */


More information about the busybox mailing list