bug-make
[Top][All Lists]
Advanced

[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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]