grub-script-check.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/emu/misc.h>
  24. #include <grub/util/misc.h>
  25. #include <grub/i18n.h>
  26. #include <grub/parser.h>
  27. #include <grub/script_sh.h>
  28. #define _GNU_SOURCE 1
  29. #include <ctype.h>
  30. #include <errno.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  35. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  36. #include <argp.h>
  37. #pragma GCC diagnostic error "-Wmissing-prototypes"
  38. #pragma GCC diagnostic error "-Wmissing-declarations"
  39. #include "progname.h"
  40. struct arguments
  41. {
  42. int verbose;
  43. char *filename;
  44. };
  45. static struct argp_option options[] = {
  46. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  47. { 0, 0, 0, 0, 0, 0 }
  48. };
  49. static error_t
  50. argp_parser (int key, char *arg, struct argp_state *state)
  51. {
  52. /* Get the input argument from argp_parse, which we
  53. know is a pointer to our arguments structure. */
  54. struct arguments *arguments = state->input;
  55. switch (key)
  56. {
  57. case 'v':
  58. arguments->verbose = 1;
  59. break;
  60. case ARGP_KEY_ARG:
  61. if (state->arg_num == 0)
  62. arguments->filename = xstrdup (arg);
  63. else
  64. {
  65. /* Too many arguments. */
  66. fprintf (stderr, _("Unknown extra argument `%s'."), arg);
  67. fprintf (stderr, "\n");
  68. argp_usage (state);
  69. }
  70. break;
  71. default:
  72. return ARGP_ERR_UNKNOWN;
  73. }
  74. return 0;
  75. }
  76. static struct argp argp = {
  77. options, argp_parser, N_("[PATH]"),
  78. N_("Checks GRUB script configuration file for syntax errors."),
  79. NULL, NULL, NULL
  80. };
  81. /* Context for main. */
  82. struct main_ctx
  83. {
  84. int lineno;
  85. FILE *file;
  86. struct arguments arguments;
  87. };
  88. /* Helper for main. */
  89. static grub_err_t
  90. get_config_line (char **line, int cont __attribute__ ((unused)), void *data)
  91. {
  92. struct main_ctx *ctx = data;
  93. int i;
  94. char *cmdline = 0;
  95. size_t len = 0;
  96. ssize_t curread;
  97. curread = getline (&cmdline, &len, (ctx->file ?: stdin));
  98. if (curread == -1)
  99. {
  100. *line = 0;
  101. grub_errno = GRUB_ERR_READ_ERROR;
  102. if (cmdline)
  103. free (cmdline);
  104. return grub_errno;
  105. }
  106. if (ctx->arguments.verbose)
  107. grub_printf ("%s", cmdline);
  108. for (i = 0; cmdline[i] != '\0'; i++)
  109. {
  110. /* Replace tabs and carriage returns with spaces. */
  111. if (cmdline[i] == '\t' || cmdline[i] == '\r')
  112. cmdline[i] = ' ';
  113. /* Replace '\n' with '\0'. */
  114. if (cmdline[i] == '\n')
  115. cmdline[i] = '\0';
  116. }
  117. ctx->lineno++;
  118. *line = grub_strdup (cmdline);
  119. free (cmdline);
  120. return 0;
  121. }
  122. int
  123. main (int argc, char *argv[])
  124. {
  125. struct main_ctx ctx = {
  126. .lineno = 0,
  127. .file = 0
  128. };
  129. char *input;
  130. int found_input = 0, found_cmd = 0;
  131. struct grub_script *script = NULL;
  132. grub_util_host_init (&argc, &argv);
  133. memset (&ctx.arguments, 0, sizeof (struct arguments));
  134. /* Check for options. */
  135. if (argp_parse (&argp, argc, argv, 0, 0, &ctx.arguments) != 0)
  136. {
  137. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  138. exit(1);
  139. }
  140. /* Obtain ARGUMENT. */
  141. if (!ctx.arguments.filename)
  142. {
  143. ctx.file = 0; /* read from stdin */
  144. }
  145. else
  146. {
  147. ctx.file = grub_util_fopen (ctx.arguments.filename, "r");
  148. if (! ctx.file)
  149. {
  150. char *program = xstrdup(program_name);
  151. fprintf (stderr, _("cannot open `%s': %s"),
  152. ctx.arguments.filename, strerror (errno));
  153. argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
  154. free(program);
  155. exit(1);
  156. }
  157. }
  158. do
  159. {
  160. input = 0;
  161. get_config_line (&input, 0, &ctx);
  162. if (! input)
  163. break;
  164. found_input = 1;
  165. script = grub_script_parse (input, get_config_line, &ctx);
  166. if (script)
  167. {
  168. if (script->cmd)
  169. found_cmd = 1;
  170. grub_script_execute (script);
  171. grub_script_free (script);
  172. }
  173. grub_free (input);
  174. } while (script != 0);
  175. if (ctx.file) fclose (ctx.file);
  176. if (found_input && script == 0)
  177. {
  178. fprintf (stderr, _("Syntax error at line %u\n"), ctx.lineno);
  179. return 1;
  180. }
  181. if (! found_cmd)
  182. {
  183. fprintf (stderr, _("Script `%s' contains no commands and will do nothing\n"),
  184. ctx.arguments.filename);
  185. return 1;
  186. }
  187. return 0;
  188. }