]>
git.proxmox.com Git - grub2.git/blob - util/grub-script-check.c
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>
41 static struct option options
[] =
43 {"help", no_argument
, 0, 'h'},
44 {"version", no_argument
, 0, 'V'},
45 {"verbose", no_argument
, 0, 'v'},
54 _("Try ``%s --help'' for more information.\n"), program_name
);
59 Checks GRUB script configuration file for syntax errors.\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\
65 Report bugs to <%s>.\n\
72 main (int argc
, char *argv
[])
80 struct grub_script
*script
= NULL
;
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
)))
90 read
= getline(&cmdline
, &len
, (file
?: stdin
));
94 grub_errno
= GRUB_ERR_READ_ERROR
;
102 grub_printf("%s", cmdline
);
104 for (i
= 0; cmdline
[i
] != '\0'; i
++)
106 /* Replace tabs and carriage returns with spaces. */
107 if (cmdline
[i
] == '\t' || cmdline
[i
] == '\r')
110 /* Replace '\n' with '\0'. */
111 if (cmdline
[i
] == '\n')
116 *line
= grub_strdup (cmdline
);
122 set_program_name (argv
[0]);
123 grub_util_init_nls ();
125 /* Check for options. */
128 int c
= getopt_long (argc
, argv
, "hvV", options
, 0);
140 printf ("%s (%s) %s\n", program_name
, PACKAGE_NAME
, PACKAGE_VERSION
);
153 /* Obtain ARGUMENT. */
156 file
= 0; /* read from stdin */
158 else if (optind
+ 1 != argc
)
160 fprintf (stderr
, _("Unknown extra argument `%s'.\n"), argv
[optind
+ 1]);
165 argument
= argv
[optind
];
166 file
= fopen (argument
, "r");
169 fprintf (stderr
, "%s: %s: %s\n", program_name
, argument
, strerror(errno
));
177 get_config_line(&input
, 0);
182 script
= grub_script_parse (input
, get_config_line
);
185 grub_script_execute (script
);
186 grub_script_free (script
);
190 } while (script
!= 0);
192 if (file
) fclose (file
);
194 if (found_input
&& script
== 0)
196 fprintf (stderr
, _("error: line no: %u\n"), lineno
);