[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/6] * expand.c (variable_name_extract): extract variable name an
From: |
Macpaul Lin |
Subject: |
[PATCH 1/6] * expand.c (variable_name_extract): extract variable name and strip prefix. |
Date: |
Thu, 14 Aug 2014 15:37:34 +0800 |
Variables used in conditional lines usually has '$',
'(', and ')' prefix, and etc.
We can use vairable_name_extract() to extract pure variable
name without these prefix.
Signed-off-by: Macpaul Lin <address@hidden>
---
expand.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/expand.c b/expand.c
index e4b08f1..0ef5c0e 100644
--- a/expand.c
+++ b/expand.c
@@ -181,6 +181,78 @@ reference_variable (char *o, const char *name, unsigned
int length)
return o;
}
+/* Parse P (a null-terminated string) to get the variable name.
+
+ Get rid of $(..) related symbols to get variable name as an
+ a-zA-Z0-9 string. */
+char *
+variable_name_extract (char *p, char **ps)
+{
+ char *p2 = NULL;
+ char *p3 = NULL;
+
+ while (1)
+ {
+ int c = *p++;
+
+ if (!STOP_SET (c, MAP_VARIABLE))
+ return (char *)p2;
+
+ if (c == '$')
+ {
+ /* This begins a variable expansion reference. Make sure we don't
+ treat chars inside the reference as assignment tokens. */
+ char closeparen;
+ int count;
+ c = *p++;
+ if (c == '(')
+ {
+ closeparen = ')';
+ p2 = p;
+ }
+ else if (c == '{')
+ {
+ closeparen = '}';
+ }
+ else
+ /* '$$' or '$X'. Either way, nothing special to do here. */
+ continue;
+
+ /* P now points past the opening paren or brace.
+ Count parens or braces until it is matched. */
+ count = 0;
+ for (; *p != '\0'; ++p)
+ {
+ if (*p == c)
+ {
+ ++count;
+ p2 = p+1;
+ }
+ else if (*p == closeparen)
+ {
+ --count;
+ if (count < 0)
+ {
+ if (!p3)
+ p3 = p;
+ *ps = p3;
+ }
+ else
+ {
+ if (!p3)
+ p3 = p;
+ }
+ ++p;
+ break;
+ }
+ }
+ continue;
+ }
+ }
+
+ return (char *)p2;
+}
+
/* Scan STRING for variable references and expansion-function calls. Only
LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
a null byte is found.
--
1.9.1
- [PATCH 1/6] * expand.c (variable_name_extract): extract variable name and strip prefix.,
Macpaul Lin <=
- [PATCH 2/6] * variable.h: Add support of struct value_records and parent_records, Macpaul Lin, 2014/08/14
- [PATCH 3/6] * variable.c: Add support for constructing value_records and parent_records, Macpaul Lin, 2014/08/14
- [PATCH 4/6] * read.c: Construct the dependency chain between parent and target variable, Macpaul Lin, 2014/08/14
- [PATCH 5/6] * load.c: add support of parent_records, Macpaul Lin, 2014/08/14
- [PATCH 6/6] * main.c: add --dep parameter for print variable dependency, Macpaul Lin, 2014/08/14