]> git.proxmox.com Git - grub2.git/blob - util/grub-script-check.c
Put recheck back
[grub2.git] / util / grub-script-check.c
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.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
29
30 #define _GNU_SOURCE 1
31
32 #include <ctype.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <getopt.h>
38
39 #include "progname.h"
40
41 static struct option options[] =
42 {
43 {"help", no_argument, 0, 'h'},
44 {"version", no_argument, 0, 'V'},
45 {"verbose", no_argument, 0, 'v'},
46 {0, 0, 0, 0}
47 };
48
49 static void
50 usage (int status)
51 {
52 if (status)
53 fprintf (stderr,
54 _("Try ``%s --help'' for more information.\n"), program_name);
55 else
56 printf (_("\
57 Usage: %s [PATH]\n\
58 \n\
59 Checks GRUB script configuration file for syntax errors.\n\
60 \n\
61 -h, --help display this message and exit\n\
62 -V, --version print version information and exit\n\
63 -v, --verbose print the script as it is being processed\n\
64 \n\
65 Report bugs to <%s>.\n\
66 "), program_name,
67 PACKAGE_BUGREPORT);
68 exit (status);
69 }
70
71 int
72 main (int argc, char *argv[])
73 {
74 char *argument;
75 char *input;
76 int lineno = 0;
77 FILE *file = 0;
78 int verbose = 0;
79 int found_input = 0;
80 struct grub_script *script = NULL;
81
82 auto grub_err_t get_config_line (char **line, int cont);
83 grub_err_t get_config_line (char **line, int cont __attribute__ ((unused)))
84 {
85 int i;
86 char *cmdline = 0;
87 size_t len = 0;
88 ssize_t read;
89
90 read = getline(&cmdline, &len, (file ?: stdin));
91 if (read == -1)
92 {
93 *line = 0;
94 grub_errno = GRUB_ERR_READ_ERROR;
95
96 if (cmdline)
97 free (cmdline);
98 return grub_errno;
99 }
100
101 if (verbose)
102 grub_printf("%s", cmdline);
103
104 for (i = 0; cmdline[i] != '\0'; i++)
105 {
106 /* Replace tabs and carriage returns with spaces. */
107 if (cmdline[i] == '\t' || cmdline[i] == '\r')
108 cmdline[i] = ' ';
109
110 /* Replace '\n' with '\0'. */
111 if (cmdline[i] == '\n')
112 cmdline[i] = '\0';
113 }
114
115 lineno++;
116 *line = grub_strdup (cmdline);
117
118 free (cmdline);
119 return 0;
120 }
121
122 set_program_name (argv[0]);
123 grub_util_init_nls ();
124
125 /* Check for options. */
126 while (1)
127 {
128 int c = getopt_long (argc, argv, "hvV", options, 0);
129
130 if (c == -1)
131 break;
132 else
133 switch (c)
134 {
135 case 'h':
136 usage (0);
137 break;
138
139 case 'V':
140 printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
141 return 0;
142
143 case 'v':
144 verbose = 1;
145 break;
146
147 default:
148 usage (1);
149 break;
150 }
151 }
152
153 /* Obtain ARGUMENT. */
154 if (optind >= argc)
155 {
156 file = 0; /* read from stdin */
157 }
158 else if (optind + 1 != argc)
159 {
160 fprintf (stderr, _("Unknown extra argument `%s'.\n"), argv[optind + 1]);
161 usage (1);
162 }
163 else
164 {
165 argument = argv[optind];
166 file = fopen (argument, "r");
167 if (! file)
168 {
169 fprintf (stderr, "%s: %s: %s\n", program_name, argument, strerror(errno));
170 usage (1);
171 }
172 }
173
174 do
175 {
176 input = 0;
177 get_config_line(&input, 0);
178 if (! input)
179 break;
180 found_input = 1;
181
182 script = grub_script_parse (input, get_config_line);
183 if (script)
184 {
185 grub_script_execute (script);
186 grub_script_free (script);
187 }
188
189 grub_free (input);
190 } while (script != 0);
191
192 if (file) fclose (file);
193
194 if (found_input && script == 0)
195 {
196 fprintf (stderr, _("error: line no: %u\n"), lineno);
197 return 1;
198 }
199
200 return 0;
201 }