Browse Source

esh: parser fix up for single-character variables

K. Lange 2 years ago
parent
commit
92e0203075
3 changed files with 38 additions and 8 deletions
  1. 17 4
      apps/bim.c
  2. 6 2
      apps/sh.c
  3. 15 2
      lib/rline_exp.c

+ 17 - 4
apps/bim.c

@@ -955,10 +955,14 @@ static char * syn_sh_keywords[] = {
 };
 
 static int variable_char(uint8_t c) {
-	if (c >= 'A' && c <= 'Z')  return 1;
+	if (c >= 'A' && c <= 'Z') return 1;
 	if (c >= 'a' && c <= 'z') return 1;
-	if (c >= '0' && c <= '9')  return 1;
+	if (c >= '0' && c <= '9') return 1;
 	if (c == '_') return 1;
+	return 0;
+}
+
+int variable_char_first(uint8_t c) {
 	if (c == '?') return 1;
 	if (c == '$') return 1;
 	if (c == '#') return 1;
@@ -1000,8 +1004,17 @@ static int syn_sh_extended(line_t * line, int i, int c, int last, int * out_left
 			return FLAG_NUMERAL;
 		}
 		int j = i + 1;
-		for (; j < line->actual + 1; ++j) {
-			if (!variable_char(line->text[j].codepoint)) break;
+		int col = 0;
+		for (; j < line->actual + 1; ++j, ++col) {
+			if (col == 0) {
+				if (variable_char_first(line->text[j].codepoint) || isdigit(line->text[j].codepoint)) {
+					j++;
+					break;
+				}
+				if (!variable_char(line->text[j].codepoint)) break;
+			} else {
+				if (!variable_char(line->text[j].codepoint)) break;
+			}
 		}
 		*out_left = (j - i) - 1;
 		return FLAG_NUMERAL;

+ 6 - 2
apps/sh.c

@@ -725,6 +725,10 @@ int variable_char(uint8_t c) {
 	if (c >= 'a' && c <= 'z') return 1;
 	if (c >= '0' && c <= '9')  return 1;
 	if (c == '_') return 1;
+	return 0;
+}
+
+int variable_char_first(uint8_t c) {
 	if (c == '?') return 1;
 	if (c == '$') return 1;
 	if (c == '#') return 1;
@@ -939,11 +943,11 @@ int shell_exec(char * buffer, size_t size, FILE * file, char ** out_buffer) {
 								p++;
 							}
 						} else {
-							while (*p != '\0' && variable_char(*p) && (coll < 100)) {
+							while (*p != '\0' && (variable_char(*p) || (coll == 0 && variable_char_first(*p)))  && (coll < 100)) {
 								var[coll] = *p;
 								coll++;
 								var[coll] = '\0';
-								if (coll == 0 && (isdigit(*p) || *p == '?')) {
+								if (coll == 1 && (isdigit(*p) || *p == '?' || *p == '$' || *p == '#')) {
 									p++;
 									break; /* Don't let these keep going */
 								}

+ 15 - 2
lib/rline_exp.c

@@ -316,6 +316,10 @@ static int variable_char(uint8_t c) {
 	if (c >= 'a' && c <= 'z') return 1;
 	if (c >= '0' && c <= '9')  return 1;
 	if (c == '_') return 1;
+	return 0;
+}
+
+int variable_char_first(uint8_t c) {
 	if (c == '?') return 1;
 	if (c == '$') return 1;
 	if (c == '#') return 1;
@@ -357,8 +361,17 @@ static int syn_sh_extended(line_t * line, int i, int c, int last, int * out_left
 			return FLAG_NUMERAL;
 		}
 		int j = i + 1;
-		for (; j < line->actual + 1; ++j) {
-			if (!variable_char(line->text[j].codepoint)) break;
+		int col = 0;
+		for (; j < line->actual + 1; ++j, ++col) {
+			if (col == 0) {
+				if (variable_char_first(line->text[j].codepoint) || isdigit(line->text[j].codepoint)) {
+					j++;
+					break;
+				}
+				if (!variable_char(line->text[j].codepoint)) break;
+			} else {
+				if (!variable_char(line->text[j].codepoint)) break;
+			}
 		}
 		*out_left = (j - i) - 1;
 		return FLAG_NUMERAL;