ieee1275: split up grub_machine_get_bootlocation
[grub.git] / util / grub-file.c
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
19
20 #include <config.h>
21
22 #include <grub/util/misc.h>
23 #include <grub/i18n.h>
24 #include <grub/term.h>
25 #include <grub/font.h>
26 #include <grub/gfxmenu_view.h>
27 #include <grub/color.h>
28 #include <grub/util/install.h>
29 #include <grub/command.h>
30 #include <grub/env.h>
31
32 #define _GNU_SOURCE     1
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39
40 #include "progname.h"
41
42 void grub_file_init (void);
43 void grub_host_init (void);
44 void grub_hostfs_init (void);
45
46 int
47 main (int argc, char *argv[])
48 {
49   char **argv2;
50   int i;
51   int had_file = 0, had_separator = 0;
52   grub_command_t cmd;
53   grub_err_t err;
54
55   grub_util_host_init (&argc, &argv);
56
57   argv2 = xmalloc (argc * sizeof (argv2[0]));
58
59   if (argc == 2 && strcmp (argv[1], "--version") == 0)
60     {
61       printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
62     }
63
64   for (i = 1; i < argc; i++)
65     {
66       if (argv[i][0] == '-' && argv[i][1] == '-'
67           && argv[i][2] == '\0' && !had_separator)
68         {
69           had_separator = 1;
70           argv2[i - 1] = xstrdup (argv[i]);
71           continue;
72         }
73       if (argv[i][0] == '-' && !had_separator)
74         {
75           argv2[i - 1] = xstrdup (argv[i]);
76           continue;
77         }
78       if (had_file)
79         grub_util_error ("one argument expected");
80       argv2[i - 1] = grub_canonicalize_file_name (argv[i]);
81       if (!argv2[i - 1])
82         {
83           grub_util_error (_("cannot open `%s': %s"), argv[i],
84                            strerror (errno));
85         }
86       had_file = 1;
87     }
88   argv2[i - 1] = NULL;
89
90   /* Initialize all modules. */
91   grub_init_all ();
92   grub_file_init ();
93   grub_hostfs_init ();
94   grub_host_init ();
95
96   grub_env_set ("root", "host");
97
98   cmd = grub_command_find ("file");
99   if (! cmd)
100     grub_util_error (_("can't find command `%s'"), "file");
101
102   err = (cmd->func) (cmd, argc - 1, argv2);
103   if (err && err != GRUB_ERR_TEST_FAILURE)
104     grub_print_error ();
105   return err;
106 }