mirror of
https://github.com/ipxe/ipxe
synced 2026-03-16 03:02:07 +03:00
[script] Allow for backslash continuation of script lines
Allow long script lines to be broken up using backslash continuation.
For example:
choose --default linux --timeout 3000 os \
&& goto boot_${os} || goto cancelled
Requested-by: Robin Smidsrød <robin@smidsrod.no>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -57,9 +57,11 @@ static size_t script_offset;
|
|||||||
static int process_script ( struct image *image,
|
static int process_script ( struct image *image,
|
||||||
int ( * process_line ) ( const char *line ),
|
int ( * process_line ) ( const char *line ),
|
||||||
int ( * terminate ) ( int rc ) ) {
|
int ( * terminate ) ( int rc ) ) {
|
||||||
|
size_t len = 0;
|
||||||
|
char *line = NULL;
|
||||||
off_t eol;
|
off_t eol;
|
||||||
size_t len;
|
size_t frag_len;
|
||||||
char *line;
|
char *tmp;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
script_offset = 0;
|
script_offset = 0;
|
||||||
@@ -71,28 +73,54 @@ static int process_script ( struct image *image,
|
|||||||
( image->len - script_offset ) );
|
( image->len - script_offset ) );
|
||||||
if ( eol < 0 )
|
if ( eol < 0 )
|
||||||
eol = image->len;
|
eol = image->len;
|
||||||
len = ( eol - script_offset );
|
frag_len = ( eol - script_offset );
|
||||||
|
|
||||||
/* Allocate buffer for line */
|
/* Allocate buffer for line */
|
||||||
line = zalloc ( len + 1 /* NUL */ );
|
tmp = realloc ( line, ( len + frag_len + 1 /* NUL */ ) );
|
||||||
if ( ! line )
|
if ( ! tmp ) {
|
||||||
return -ENOMEM;
|
rc = -ENOMEM;
|
||||||
|
goto err_alloc;
|
||||||
|
}
|
||||||
|
line = tmp;
|
||||||
|
|
||||||
/* Copy line */
|
/* Copy line */
|
||||||
copy_from_user ( line, image->data, script_offset, len );
|
copy_from_user ( ( line + len ), image->data, script_offset,
|
||||||
|
frag_len );
|
||||||
|
len += frag_len;
|
||||||
|
|
||||||
|
/* Move to next line in script */
|
||||||
|
script_offset += ( frag_len + 1 );
|
||||||
|
|
||||||
|
/* Strip trailing CR, if present */
|
||||||
|
if ( line[ len - 1 ] == '\r' )
|
||||||
|
len--;
|
||||||
|
|
||||||
|
/* Handle backslash continuations */
|
||||||
|
if ( line[ len - 1 ] == '\\' ) {
|
||||||
|
len--;
|
||||||
|
rc = -EINVAL;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Terminate line */
|
||||||
|
line[len] = '\0';
|
||||||
DBG ( "$ %s\n", line );
|
DBG ( "$ %s\n", line );
|
||||||
|
|
||||||
/* Move to next line */
|
/* Process line */
|
||||||
script_offset += ( len + 1 );
|
|
||||||
|
|
||||||
/* Process and free line */
|
|
||||||
rc = process_line ( line );
|
rc = process_line ( line );
|
||||||
free ( line );
|
|
||||||
if ( terminate ( rc ) )
|
if ( terminate ( rc ) )
|
||||||
return rc;
|
goto err_process;
|
||||||
|
|
||||||
|
/* Free line */
|
||||||
|
free ( line );
|
||||||
|
line = NULL;
|
||||||
|
len = 0;
|
||||||
|
|
||||||
} while ( script_offset < image->len );
|
} while ( script_offset < image->len );
|
||||||
|
|
||||||
|
err_process:
|
||||||
|
err_alloc:
|
||||||
|
free ( line );
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user