]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[settings] Generalise expand_command() to expand_settings()
authorMichael Brown <mcb30@ipxe.org>
Fri, 28 Jan 2011 00:16:18 +0000 (00:16 +0000)
committerMichael Brown <mcb30@ipxe.org>
Fri, 28 Jan 2011 00:16:18 +0000 (00:16 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/exec.c
src/core/settings.c
src/include/ipxe/settings.h

index bb3b343d8de653a16d56aace49362daccca2ee81..57b2df581f722cb6d5254076e72042e54da53bb8 100644 (file)
@@ -86,80 +86,6 @@ int execv ( const char *command, char * const argv[] ) {
        return -ENOEXEC;
 }
 
-/**
- * Expand variables within command line
- *
- * @v command          Command line
- * @ret expcmd         Expanded command line
- *
- * The expanded command line is allocated with malloc() and the caller
- * must eventually free() it.
- */
-static char * expand_command ( const char *command ) {
-       char *expcmd;
-       char *start;
-       char *end;
-       char *head;
-       char *name;
-       char *tail;
-       int setting_len;
-       int new_len;
-       char *tmp;
-
-       /* Obtain temporary modifiable copy of command line */
-       expcmd = strdup ( command );
-       if ( ! expcmd )
-               return NULL;
-
-       /* Expand while expansions remain */
-       while ( 1 ) {
-
-               head = expcmd;
-
-               /* Locate setting to be expanded */
-               start = NULL;
-               end = NULL;
-               for ( tmp = expcmd ; *tmp ; tmp++ ) {
-                       if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
-                               start = tmp;
-                       if ( start && ( tmp[0] == '}' ) ) {
-                               end = tmp;
-                               break;
-                       }
-               }
-               if ( ! end )
-                       break;
-               *start = '\0';
-               name = ( start + 2 );
-               *end = '\0';
-               tail = ( end + 1 );
-
-               /* Determine setting length */
-               setting_len = fetchf_named_setting ( name, NULL, 0 );
-               if ( setting_len < 0 )
-                       setting_len = 0; /* Treat error as empty setting */
-
-               /* Read setting into temporary buffer */
-               {
-                       char setting_buf[ setting_len + 1 ];
-
-                       setting_buf[0] = '\0';
-                       fetchf_named_setting ( name, setting_buf,
-                                              sizeof ( setting_buf ) );
-
-                       /* Construct expanded string and discard old string */
-                       tmp = expcmd;
-                       new_len = asprintf ( &expcmd, "%s%s%s",
-                                            head, setting_buf, tail );
-                       free ( tmp );
-                       if ( new_len < 0 )
-                               return NULL;
-               }
-       }
-
-       return expcmd;
-}
-
 /**
  * Split command line into tokens
  *
@@ -294,7 +220,7 @@ int system ( const char *command ) {
        int rc = 0;
 
        /* Perform variable expansion */
-       expcmd = expand_command ( command );
+       expcmd = expand_settings ( command );
        if ( ! expcmd )
                return -ENOMEM;
 
index e2b4826309b8479bc059a4f8089271036ec5043f..da662e6168899ff6ddb17356b00d5eda149377a7 100644 (file)
@@ -1467,6 +1467,87 @@ struct setting_type setting_type_uuid __setting_type = {
        .fetchf = fetchf_uuid,
 };
 
+/******************************************************************************
+ *
+ * Setting expansion
+ *
+ ******************************************************************************
+ */
+
+/**
+ * Expand variables within string
+ *
+ * @v string           String
+ * @ret expstr         Expanded string
+ *
+ * The expanded string is allocated with malloc() and the caller must
+ * eventually free() it.
+ */
+char * expand_settings ( const char *string ) {
+       char *expstr;
+       char *start;
+       char *end;
+       char *head;
+       char *name;
+       char *tail;
+       int setting_len;
+       int new_len;
+       char *tmp;
+
+       /* Obtain temporary modifiable copy of string */
+       expstr = strdup ( string );
+       if ( ! expstr )
+               return NULL;
+
+       /* Expand while expansions remain */
+       while ( 1 ) {
+
+               head = expstr;
+
+               /* Locate setting to be expanded */
+               start = NULL;
+               end = NULL;
+               for ( tmp = expstr ; *tmp ; tmp++ ) {
+                       if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
+                               start = tmp;
+                       if ( start && ( tmp[0] == '}' ) ) {
+                               end = tmp;
+                               break;
+                       }
+               }
+               if ( ! end )
+                       break;
+               *start = '\0';
+               name = ( start + 2 );
+               *end = '\0';
+               tail = ( end + 1 );
+
+               /* Determine setting length */
+               setting_len = fetchf_named_setting ( name, NULL, 0 );
+               if ( setting_len < 0 )
+                       setting_len = 0; /* Treat error as empty setting */
+
+               /* Read setting into temporary buffer */
+               {
+                       char setting_buf[ setting_len + 1 ];
+
+                       setting_buf[0] = '\0';
+                       fetchf_named_setting ( name, setting_buf,
+                                              sizeof ( setting_buf ) );
+
+                       /* Construct expanded string and discard old string */
+                       tmp = expstr;
+                       new_len = asprintf ( &expstr, "%s%s%s",
+                                            head, setting_buf, tail );
+                       free ( tmp );
+                       if ( new_len < 0 )
+                               return NULL;
+               }
+       }
+
+       return expstr;
+}
+
 /******************************************************************************
  *
  * Settings
index a764bf0e5ce3170e78c2d5e65b112b7499f7b9e1..d99e5ec0b611eaafd58458cd9293c3d5609f8cf6 100644 (file)
@@ -221,6 +221,7 @@ extern int storef_setting ( struct settings *settings,
                            const char *value );
 extern int storef_named_setting ( const char *name, const char *value );
 extern int fetchf_named_setting ( const char *name, char *buf, size_t len );
+extern char * expand_settings ( const char *string );
 
 extern struct setting_type setting_type_string __setting_type;
 extern struct setting_type setting_type_ipv4 __setting_type;