From f866fe808bc574a9304a2ed184ab5c2f0c4b2eee Mon Sep 17 00:00:00 2001 From: BVK Chaitanya Date: Fri, 19 Nov 2010 19:08:44 +0530 Subject: [PATCH] reuse code from legacy parser --- ChangeLog | 7 +++++++ grub-core/commands/menuentry.c | 24 ++++++++---------------- grub-core/lib/legacy_parse.c | 25 ++++++------------------- grub-core/script/script.c | 21 +++++++++++++++++++++ include/grub/script_sh.h | 3 +++ 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/ChangeLog b/ChangeLog index b53b396b5..eb8064188 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,13 @@ * grub-core/commands/menuentry.c (setparams_prefix): Use single quotes for arguments. + * grub-core/lib/legacy_parse.c (grub_legacy_escape): Use + grub_script_escape_squotes function instead. + + * include/grub/script_sh.h (grub_script_escape_squotes): New + prototype. + * grub-core/script/script.c (grub_script_escape_squotes): New + function. 2010-11-18 Vladimir Serbinenko diff --git a/grub-core/commands/menuentry.c b/grub-core/commands/menuentry.c index 7a07698d8..f64378724 100644 --- a/grub-core/commands/menuentry.c +++ b/grub-core/commands/menuentry.c @@ -24,6 +24,7 @@ #include #include #include +#include static const struct grub_arg_option options[] = { @@ -221,26 +222,17 @@ setparams_prefix (int argc, char **args) return 0; grub_strcpy (result, "setparams"); - i = 9; + p = result + 9; for (j = 0; j < argc; j++) { - result[i++] = ' '; - result[i++] = '\''; - p = args[j]; - while (*p) { - result[i++] = *p; - if (*p == '\'') { - result[i++] = '\\'; - result[i++] = '\''; - result[i++] = '\''; - } - p++; - } - result[i++] = '\''; + *p++ = ' '; + *p++ = '\''; + p = grub_script_escape_squotes (p, args[j], grub_strlen (args[j])); + *p++ = '\''; } - result[i++] = '\n'; - result[i] = '\0'; + *p++ = '\n'; + *p = '\0'; return result; } diff --git a/grub-core/lib/legacy_parse.c b/grub-core/lib/legacy_parse.c index 5a359ff1c..8fe60b03d 100644 --- a/grub-core/lib/legacy_parse.c +++ b/grub-core/lib/legacy_parse.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -323,30 +324,16 @@ char * grub_legacy_escape (const char *in, grub_size_t len) { const char *ptr; - char *ret, *outptr; + char *outptr; int overhead = 0; for (ptr = in; ptr < in + len && *ptr; ptr++) if (*ptr == '\'') overhead += 3; - ret = grub_malloc (ptr - in + overhead + 1); - if (!ret) + outptr = grub_malloc (ptr - in + overhead + 1); + if (!outptr) return NULL; - outptr = ret; - for (ptr = in; ptr < in + len && *ptr; ptr++) - { - if (*ptr == '\'') - { - *outptr++ = '\''; - *outptr++ = '\\'; - *outptr++ = '\''; - *outptr++ = '\''; - continue; - } - - *outptr++ = *ptr; - } - *outptr++ = 0; - return ret; + grub_script_escape_squotes (outptr, in, len); + return outptr; } static char * diff --git a/grub-core/script/script.c b/grub-core/script/script.c index ad3018357..45c3222b2 100644 --- a/grub-core/script/script.c +++ b/grub-core/script/script.c @@ -22,6 +22,27 @@ #include #include +/* Escape single quotes in first `len' characters of `in' into a GRUB + script argument form into `out'; return address of the end in + `out'. */ +char * +grub_script_escape_squotes (char *out, const char *in, grub_size_t len) +{ + while (*in && len--) + { + *out++ = *in; + if (*in == '\'') + { + *out++ = '\\'; + *out++ = '\''; + *out++ = '\''; + } + in++; + } + *out = '\0'; + return out; +} + /* It is not possible to deallocate the memory when a syntax error was found. Because of that it is required to keep track of all memory allocations. The memory is freed in case of an error, or assigned diff --git a/include/grub/script_sh.h b/include/grub/script_sh.h index 6d31fca5a..f5dcd881e 100644 --- a/include/grub/script_sh.h +++ b/include/grub/script_sh.h @@ -382,6 +382,9 @@ grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *c grub_err_t grub_normal_parse_line (char *line, grub_reader_getline_t getline); +char * +grub_script_escape_squotes (char *out, const char *in, grub_size_t len); + static inline struct grub_script * grub_script_ref (struct grub_script *script) {