Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-script-check.c
blob73d51f0e42d2a57da9e6731e4a6dd32b45c1b492
1 /* grub-script-check.c - check grub script file for syntax errors */
2 /*
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/>.
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/mm.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>
30 #define _GNU_SOURCE 1
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <argp.h>
39 #include "progname.h"
41 struct arguments
43 int verbose;
44 char *filename;
47 static struct argp_option options[] = {
48 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
49 { 0, 0, 0, 0, 0, 0 }
52 static error_t
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;
59 switch (key)
61 case 'v':
62 arguments->verbose = 1;
63 break;
65 case ARGP_KEY_ARG:
66 if (state->arg_num == 0)
67 arguments->filename = xstrdup (arg);
68 else
70 /* Too many arguments. */
71 fprintf (stderr, _("Unknown extra argument `%s'."), arg);
72 fprintf (stderr, "\n");
73 argp_usage (state);
75 break;
76 default:
77 return ARGP_ERR_UNKNOWN;
79 return 0;
82 static struct argp argp = {
83 options, argp_parser, N_("[PATH]"),
84 N_("Checks GRUB script configuration file for syntax errors."),
85 NULL, NULL, NULL
88 int
89 main (int argc, char *argv[])
91 char *input;
92 int lineno = 0;
93 FILE *file = 0;
94 struct arguments arguments;
95 int found_input = 0;
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)))
101 int i;
102 char *cmdline = 0;
103 size_t len = 0;
104 ssize_t curread;
106 curread = getline(&cmdline, &len, (file ?: stdin));
107 if (curread == -1)
109 *line = 0;
110 grub_errno = GRUB_ERR_READ_ERROR;
112 if (cmdline)
113 free (cmdline);
114 return grub_errno;
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')
124 cmdline[i] = ' ';
126 /* Replace '\n' with '\0'. */
127 if (cmdline[i] == '\n')
128 cmdline[i] = '\0';
131 lineno++;
132 *line = grub_strdup (cmdline);
134 free (cmdline);
135 return 0;
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"));
147 exit(1);
150 /* Obtain ARGUMENT. */
151 if (!arguments.filename)
153 file = 0; /* read from stdin */
155 else
157 file = fopen (arguments.filename, "r");
158 if (! file)
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);
164 free(program);
165 exit(1);
171 input = 0;
172 get_config_line(&input, 0);
173 if (! input)
174 break;
175 found_input = 1;
177 script = grub_script_parse (input, get_config_line);
178 if (script)
180 grub_script_execute (script);
181 grub_script_free (script);
184 grub_free (input);
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);
192 return 1;
195 return 0;