2010-01-09 14:57:58 +00:00
|
|
|
/* grub-script-check.c - check grub script file for syntax errors */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-01-22 13:37:27 +00:00
|
|
|
* Copyright (C) 2009,2010 Free Software Foundation, Inc.
|
2010-01-09 14:57:58 +00:00
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
2010-05-06 06:04:04 +00:00
|
|
|
#include <grub/emu/misc.h>
|
2010-01-18 11:28:03 +00:00
|
|
|
#include <grub/util/misc.h>
|
2010-01-09 14:57:58 +00:00
|
|
|
#include <grub/i18n.h>
|
|
|
|
#include <grub/parser.h>
|
|
|
|
#include <grub/script_sh.h>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-02-03 20:45:43 +00:00
|
|
|
#include <argp.h>
|
2010-01-09 14:57:58 +00:00
|
|
|
|
|
|
|
#include "progname.h"
|
|
|
|
|
2012-02-03 20:45:43 +00:00
|
|
|
struct arguments
|
2010-01-09 14:57:58 +00:00
|
|
|
{
|
2012-02-03 20:45:43 +00:00
|
|
|
int verbose;
|
|
|
|
char *filename;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct argp_option options[] = {
|
2012-02-05 10:23:47 +00:00
|
|
|
{"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
|
2012-02-03 20:45:43 +00:00
|
|
|
{ 0, 0, 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static error_t
|
|
|
|
argp_parser (int key, char *arg, struct argp_state *state)
|
|
|
|
{
|
|
|
|
/* Get the input argument from argp_parse, which we
|
|
|
|
know is a pointer to our arguments structure. */
|
|
|
|
struct arguments *arguments = state->input;
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case 'v':
|
|
|
|
arguments->verbose = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARGP_KEY_ARG:
|
|
|
|
if (state->arg_num == 0)
|
|
|
|
arguments->filename = xstrdup (arg);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Too many arguments. */
|
|
|
|
fprintf (stderr, _("Unknown extra argument `%s'.\n"), arg);
|
|
|
|
argp_usage (state);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ARGP_ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
return 0;
|
2010-01-09 14:57:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 20:45:43 +00:00
|
|
|
static struct argp argp = {
|
|
|
|
options, argp_parser, N_("[PATH]"),
|
|
|
|
N_("Checks GRUB script configuration file for syntax errors."),
|
|
|
|
NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
2010-01-09 14:57:58 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *input;
|
2010-12-03 03:25:57 +00:00
|
|
|
int lineno = 0;
|
2010-01-09 14:57:58 +00:00
|
|
|
FILE *file = 0;
|
2012-02-03 20:45:43 +00:00
|
|
|
struct arguments arguments;
|
2010-05-27 14:54:18 +00:00
|
|
|
int found_input = 0;
|
|
|
|
struct grub_script *script = NULL;
|
2010-01-09 14:57:58 +00:00
|
|
|
|
|
|
|
auto grub_err_t get_config_line (char **line, int cont);
|
|
|
|
grub_err_t get_config_line (char **line, int cont __attribute__ ((unused)))
|
|
|
|
{
|
2010-01-28 16:13:28 +00:00
|
|
|
int i;
|
2010-01-09 14:57:58 +00:00
|
|
|
char *cmdline = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
ssize_t read;
|
|
|
|
|
|
|
|
read = getline(&cmdline, &len, (file ?: stdin));
|
|
|
|
if (read == -1)
|
|
|
|
{
|
|
|
|
*line = 0;
|
|
|
|
grub_errno = GRUB_ERR_READ_ERROR;
|
|
|
|
|
|
|
|
if (cmdline)
|
|
|
|
free (cmdline);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
|
2012-02-03 20:45:43 +00:00
|
|
|
if (arguments.verbose)
|
2010-01-09 14:57:58 +00:00
|
|
|
grub_printf("%s", cmdline);
|
|
|
|
|
2010-01-28 16:13:28 +00:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
2010-12-03 03:25:57 +00:00
|
|
|
lineno++;
|
2010-01-09 14:57:58 +00:00
|
|
|
*line = grub_strdup (cmdline);
|
|
|
|
|
|
|
|
free (cmdline);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_program_name (argv[0]);
|
2010-01-18 11:28:03 +00:00
|
|
|
grub_util_init_nls ();
|
2010-01-09 14:57:58 +00:00
|
|
|
|
2012-02-03 20:45:43 +00:00
|
|
|
memset (&arguments, 0, sizeof (struct arguments));
|
|
|
|
|
2010-01-09 14:57:58 +00:00
|
|
|
/* Check for options. */
|
2012-02-03 20:45:43 +00:00
|
|
|
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
|
2010-01-09 14:57:58 +00:00
|
|
|
{
|
2012-02-03 20:45:43 +00:00
|
|
|
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
|
|
|
|
exit(1);
|
2010-01-09 14:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Obtain ARGUMENT. */
|
2012-02-03 20:45:43 +00:00
|
|
|
if (!arguments.filename)
|
2010-01-09 14:57:58 +00:00
|
|
|
{
|
2010-01-18 11:28:03 +00:00
|
|
|
file = 0; /* read from stdin */
|
2010-01-09 14:57:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-03 20:45:43 +00:00
|
|
|
file = fopen (arguments.filename, "r");
|
2010-01-09 14:57:58 +00:00
|
|
|
if (! file)
|
|
|
|
{
|
2012-02-03 20:45:43 +00:00
|
|
|
char *program = xstrdup(program_name);
|
|
|
|
fprintf (stderr, "%s: %s: %s\n", program_name,
|
|
|
|
arguments.filename, strerror(errno));
|
|
|
|
argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
|
|
|
|
free(program);
|
|
|
|
exit(1);
|
2010-01-09 14:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
input = 0;
|
|
|
|
get_config_line(&input, 0);
|
|
|
|
if (! input)
|
|
|
|
break;
|
2010-05-27 14:54:18 +00:00
|
|
|
found_input = 1;
|
2010-01-09 14:57:58 +00:00
|
|
|
|
|
|
|
script = grub_script_parse (input, get_config_line);
|
|
|
|
if (script)
|
|
|
|
{
|
|
|
|
grub_script_execute (script);
|
|
|
|
grub_script_free (script);
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (input);
|
|
|
|
} while (script != 0);
|
|
|
|
|
|
|
|
if (file) fclose (file);
|
|
|
|
|
2010-12-03 03:25:57 +00:00
|
|
|
if (found_input && script == 0)
|
|
|
|
{
|
2011-11-11 20:44:56 +00:00
|
|
|
fprintf (stderr, _("error: line no: %u\n"), lineno);
|
2010-12-03 03:25:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-01-09 14:57:58 +00:00
|
|
|
}
|