Remove nested functions from script reading and parsing.
* grub-core/kern/parser.c (grub_parser_split_cmdline): Add getline_data argument, passed to getline. * grub-core/kern/rescue_parser.c (grub_rescue_parse_line): Add getline_data argument, passed to grub_parser_split_cmdline. * grub-core/script/lexer.c (grub_script_lexer_yywrap): Pass lexerstate->getline_data to lexerstate->getline. (grub_script_lexer_init): Add getline_data argument, saved in lexerstate->getline_data. * grub-core/script/main.c (grub_normal_parse_line): Add getline_data argument, passed to grub_script_parse. * grub-core/script/script.c (grub_script_parse): Add getline_data argument, passed to grub_script_lexer_init. * include/grub/parser.h (grub_parser_split_cmdline): Update prototype. Update all callers to pass appropriate getline data. (struct grub_parser.parse_line): Likewise. (grub_rescue_parse_line): Likewise. * include/grub/reader.h (grub_reader_getline_t): Add void * argument. * include/grub/script_sh.h (struct grub_lexer_param): Add getline_data member. (grub_script_parse): Update prototype. Update all callers to pass appropriate getline data. (grub_script_lexer_init): Likewise. (grub_normal_parse_line): Likewise. * grub-core/commands/legacycfg.c (legacy_file_getline): Add unused data argument. * grub-core/kern/parser.c (grub_parser_execute: getline): Make static instead of nested. Rename to ... (grub_parser_execute_getline): ... this. * grub-core/kern/rescue_reader.c (grub_rescue_read_line): Add unused data argument. * grub-core/normal/main.c (read_config_file: getline): Make static instead of nested. Rename to ... (read_config_file_getline): ... this. (grub_normal_read_line): Add unused data argument. * grub-core/script/execute.c (grub_script_execute_sourcecode: getline): Make static instead of nested. Rename to ... (grub_script_execute_sourcecode_getline): ... this. * util/grub-script-check.c (main: get_config_line): Make static instead of nested.
This commit is contained in:
parent
d0d4b8a063
commit
09fd6d8293
15 changed files with 238 additions and 148 deletions
|
@ -85,81 +85,92 @@ static struct argp argp = {
|
|||
NULL, NULL, NULL
|
||||
};
|
||||
|
||||
/* Context for main. */
|
||||
struct main_ctx
|
||||
{
|
||||
int lineno;
|
||||
FILE *file;
|
||||
struct arguments arguments;
|
||||
};
|
||||
|
||||
/* Helper for main. */
|
||||
static grub_err_t
|
||||
get_config_line (char **line, int cont __attribute__ ((unused)), void *data)
|
||||
{
|
||||
struct main_ctx *ctx = data;
|
||||
int i;
|
||||
char *cmdline = 0;
|
||||
size_t len = 0;
|
||||
ssize_t curread;
|
||||
|
||||
curread = getline (&cmdline, &len, (ctx->file ?: stdin));
|
||||
if (curread == -1)
|
||||
{
|
||||
*line = 0;
|
||||
grub_errno = GRUB_ERR_READ_ERROR;
|
||||
|
||||
if (cmdline)
|
||||
free (cmdline);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
if (ctx->arguments.verbose)
|
||||
grub_printf ("%s", cmdline);
|
||||
|
||||
for (i = 0; cmdline[i] != '\0'; i++)
|
||||
{
|
||||
/* Replace tabs and carriage returns with spaces. */
|
||||
if (cmdline[i] == '\t' || cmdline[i] == '\r')
|
||||
cmdline[i] = ' ';
|
||||
|
||||
/* Replace '\n' with '\0'. */
|
||||
if (cmdline[i] == '\n')
|
||||
cmdline[i] = '\0';
|
||||
}
|
||||
|
||||
ctx->lineno++;
|
||||
*line = grub_strdup (cmdline);
|
||||
|
||||
free (cmdline);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
struct main_ctx ctx = {
|
||||
.lineno = 0,
|
||||
.file = 0
|
||||
};
|
||||
char *input;
|
||||
int lineno = 0;
|
||||
FILE *file = 0;
|
||||
struct arguments arguments;
|
||||
int found_input = 0;
|
||||
struct grub_script *script = NULL;
|
||||
|
||||
auto grub_err_t get_config_line (char **line, int cont);
|
||||
grub_err_t get_config_line (char **line, int cont __attribute__ ((unused)))
|
||||
{
|
||||
int i;
|
||||
char *cmdline = 0;
|
||||
size_t len = 0;
|
||||
ssize_t curread;
|
||||
|
||||
curread = getline(&cmdline, &len, (file ?: stdin));
|
||||
if (curread == -1)
|
||||
{
|
||||
*line = 0;
|
||||
grub_errno = GRUB_ERR_READ_ERROR;
|
||||
|
||||
if (cmdline)
|
||||
free (cmdline);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
if (arguments.verbose)
|
||||
grub_printf("%s", cmdline);
|
||||
|
||||
for (i = 0; cmdline[i] != '\0'; i++)
|
||||
{
|
||||
/* Replace tabs and carriage returns with spaces. */
|
||||
if (cmdline[i] == '\t' || cmdline[i] == '\r')
|
||||
cmdline[i] = ' ';
|
||||
|
||||
/* Replace '\n' with '\0'. */
|
||||
if (cmdline[i] == '\n')
|
||||
cmdline[i] = '\0';
|
||||
}
|
||||
|
||||
lineno++;
|
||||
*line = grub_strdup (cmdline);
|
||||
|
||||
free (cmdline);
|
||||
return 0;
|
||||
}
|
||||
|
||||
set_program_name (argv[0]);
|
||||
grub_util_init_nls ();
|
||||
|
||||
memset (&arguments, 0, sizeof (struct arguments));
|
||||
memset (&ctx.arguments, 0, sizeof (struct arguments));
|
||||
|
||||
/* Check for options. */
|
||||
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
|
||||
if (argp_parse (&argp, argc, argv, 0, 0, &ctx.arguments) != 0)
|
||||
{
|
||||
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Obtain ARGUMENT. */
|
||||
if (!arguments.filename)
|
||||
if (!ctx.arguments.filename)
|
||||
{
|
||||
file = 0; /* read from stdin */
|
||||
ctx.file = 0; /* read from stdin */
|
||||
}
|
||||
else
|
||||
{
|
||||
file = fopen (arguments.filename, "r");
|
||||
if (! file)
|
||||
ctx.file = fopen (ctx.arguments.filename, "r");
|
||||
if (! ctx.file)
|
||||
{
|
||||
char *program = xstrdup(program_name);
|
||||
fprintf (stderr, "%s: %s: %s\n", program_name,
|
||||
arguments.filename, strerror(errno));
|
||||
ctx.arguments.filename, strerror (errno));
|
||||
argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
|
||||
free(program);
|
||||
exit(1);
|
||||
|
@ -169,12 +180,12 @@ main (int argc, char *argv[])
|
|||
do
|
||||
{
|
||||
input = 0;
|
||||
get_config_line(&input, 0);
|
||||
get_config_line (&input, 0, &ctx);
|
||||
if (! input)
|
||||
break;
|
||||
found_input = 1;
|
||||
|
||||
script = grub_script_parse (input, get_config_line);
|
||||
script = grub_script_parse (input, get_config_line, &ctx);
|
||||
if (script)
|
||||
{
|
||||
grub_script_execute (script);
|
||||
|
@ -184,11 +195,11 @@ main (int argc, char *argv[])
|
|||
grub_free (input);
|
||||
} while (script != 0);
|
||||
|
||||
if (file) fclose (file);
|
||||
if (ctx.file) fclose (ctx.file);
|
||||
|
||||
if (found_input && script == 0)
|
||||
{
|
||||
fprintf (stderr, _("Syntax error at line %u\n"), lineno);
|
||||
fprintf (stderr, _("Syntax error at line %u\n"), ctx.lineno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue