1 /* grub-script-check.c - check grub script file for syntax errors */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009,2010 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/types.h>
23 #include <grub/misc.h>
24 #include <grub/emu/misc.h>
25 #include <grub/util/misc.h>
26 #include <grub/i18n.h>
27 #include <grub/parser.h>
28 #include <grub/script_sh.h>
47 static struct argp_option options
[] = {
48 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
53 argp_parser (int key
, char *arg
, struct argp_state
*state
)
55 /* Get the input argument from argp_parse, which we
56 know is a pointer to our arguments structure. */
57 struct arguments
*arguments
= state
->input
;
62 arguments
->verbose
= 1;
66 if (state
->arg_num
== 0)
67 arguments
->filename
= xstrdup (arg
);
70 /* Too many arguments. */
71 fprintf (stderr
, _("Unknown extra argument `%s'."), arg
);
72 fprintf (stderr
, "\n");
77 return ARGP_ERR_UNKNOWN
;
82 static struct argp argp
= {
83 options
, argp_parser
, N_("[PATH]"),
84 N_("Checks GRUB script configuration file for syntax errors."),
89 main (int argc
, char *argv
[])
94 struct arguments arguments
;
96 struct grub_script
*script
= NULL
;
98 auto grub_err_t
get_config_line (char **line
, int cont
);
99 grub_err_t
get_config_line (char **line
, int cont
__attribute__ ((unused
)))
106 curread
= getline(&cmdline
, &len
, (file
?: stdin
));
110 grub_errno
= GRUB_ERR_READ_ERROR
;
117 if (arguments
.verbose
)
118 grub_printf("%s", cmdline
);
120 for (i
= 0; cmdline
[i
] != '\0'; i
++)
122 /* Replace tabs and carriage returns with spaces. */
123 if (cmdline
[i
] == '\t' || cmdline
[i
] == '\r')
126 /* Replace '\n' with '\0'. */
127 if (cmdline
[i
] == '\n')
132 *line
= grub_strdup (cmdline
);
138 set_program_name (argv
[0]);
139 grub_util_init_nls ();
141 memset (&arguments
, 0, sizeof (struct arguments
));
143 /* Check for options. */
144 if (argp_parse (&argp
, argc
, argv
, 0, 0, &arguments
) != 0)
146 fprintf (stderr
, "%s", _("Error in parsing command line arguments\n"));
150 /* Obtain ARGUMENT. */
151 if (!arguments
.filename
)
153 file
= 0; /* read from stdin */
157 file
= fopen (arguments
.filename
, "r");
160 char *program
= xstrdup(program_name
);
161 fprintf (stderr
, "%s: %s: %s\n", program_name
,
162 arguments
.filename
, strerror(errno
));
163 argp_help (&argp
, stderr
, ARGP_HELP_STD_USAGE
, program
);
172 get_config_line(&input
, 0);
177 script
= grub_script_parse (input
, get_config_line
);
180 grub_script_execute (script
);
181 grub_script_free (script
);
185 } while (script
!= 0);
187 if (file
) fclose (file
);
189 if (found_input
&& script
== 0)
191 fprintf (stderr
, _("Syntax error at line %u\n"), lineno
);