grub-file.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <grub/util/misc.h>
  20. #include <grub/i18n.h>
  21. #include <grub/term.h>
  22. #include <grub/font.h>
  23. #include <grub/gfxmenu_view.h>
  24. #include <grub/color.h>
  25. #include <grub/util/install.h>
  26. #include <grub/command.h>
  27. #include <grub/env.h>
  28. #define _GNU_SOURCE 1
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #include "progname.h"
  35. void grub_file_init (void);
  36. void grub_host_init (void);
  37. void grub_hostfs_init (void);
  38. int
  39. main (int argc, char *argv[])
  40. {
  41. char **argv2;
  42. int i;
  43. int had_file = 0, had_separator = 0;
  44. grub_command_t cmd;
  45. grub_err_t err;
  46. grub_util_host_init (&argc, &argv);
  47. argv2 = xcalloc (argc, sizeof (argv2[0]));
  48. if (argc == 2 && strcmp (argv[1], "--version") == 0)
  49. {
  50. printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  51. }
  52. for (i = 1; i < argc; i++)
  53. {
  54. if (argv[i][0] == '-' && argv[i][1] == '-'
  55. && argv[i][2] == '\0' && !had_separator)
  56. {
  57. had_separator = 1;
  58. argv2[i - 1] = xstrdup (argv[i]);
  59. continue;
  60. }
  61. if (argv[i][0] == '-' && !had_separator)
  62. {
  63. argv2[i - 1] = xstrdup (argv[i]);
  64. continue;
  65. }
  66. if (had_file)
  67. grub_util_error ("one argument expected");
  68. argv2[i - 1] = grub_canonicalize_file_name (argv[i]);
  69. if (!argv2[i - 1])
  70. {
  71. grub_util_error (_("cannot open `%s': %s"), argv[i],
  72. strerror (errno));
  73. }
  74. had_file = 1;
  75. }
  76. argv2[i - 1] = NULL;
  77. /* Initialize all modules. */
  78. grub_init_all ();
  79. grub_file_init ();
  80. grub_hostfs_init ();
  81. grub_host_init ();
  82. grub_env_set ("root", "host");
  83. cmd = grub_command_find ("file");
  84. if (! cmd)
  85. grub_util_error (_("can't find command `%s'"), "file");
  86. err = (cmd->func) (cmd, argc - 1, argv2);
  87. if (err && err != GRUB_ERR_TEST_FAILURE)
  88. grub_print_error ();
  89. return err;
  90. }