[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 1/6] * expand.c (variable_name_extract): extract variable name
From: |
Macpaul Lin |
Subject: |
[PATCH v2 1/6] * expand.c (variable_name_extract): extract variable name and strip prefix. |
Date: |
Mon, 18 Aug 2014 21:27:10 +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>
---
Changes for v2:
- no changes.
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 v2 1/6] * expand.c (variable_name_extract): extract variable name and strip prefix.,
Macpaul Lin <=
- [PATCH v2 2/6] * variable.h: Add support of struct value_records and parent_records, Macpaul Lin, 2014/08/18
- [PATCH v2 3/6] * variable.c: Add support for constructing value_records and parent_records, Macpaul Lin, 2014/08/18
- [PATCH v2 4/6] * read.c: Construct the dependency chain between parent and target variable, Macpaul Lin, 2014/08/18
- [PATCH v2 5/6] * load.c: add support of parent_records, Macpaul Lin, 2014/08/18
- [PATCH v2 6/6] * main.c: add --dep parameter for print variable dependency, Macpaul Lin, 2014/08/18
- Re: [PATCH v2 1/6] * expand.c (variable_name_extract): extract variable name and strip prefix., Paul Smith, 2014/08/18