ieee1275: split up grub_machine_get_bootlocation
[grub.git] / util / grub-menulst2cfg.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2010  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 #include <config.h>
20
21 #include <grub/legacy_parse.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <grub/util/misc.h>
26 #include <grub/misc.h>
27 #include <grub/i18n.h>
28
29 int
30 main (int argc, char **argv)
31 {
32   FILE *in, *out;
33   char *entryname = NULL;
34   char *buf = NULL;
35   size_t bufsize = 0;
36   char *suffix = xstrdup ("");
37   int suffixlen = 0;
38   const char *out_fname = 0;
39
40   grub_util_host_init (&argc, &argv);
41
42   if (argc >= 2 && argv[1][0] == '-')
43     {
44       fprintf (stdout, _("Usage: %s [INFILE [OUTFILE]]\n"), argv[0]);
45       return 0;
46     }
47
48   if (argc >= 2)
49     {
50       in = grub_util_fopen (argv[1], "r");
51       if (!in)
52         {
53           fprintf (stderr, _("cannot open `%s': %s"),
54                    argv[1], strerror (errno));
55           return 1;
56         }
57     }
58   else
59     in = stdin;
60
61   if (argc >= 3)
62     {
63       out = grub_util_fopen (argv[2], "w");
64       if (!out)
65         {                                       
66           if (in != stdin)
67             fclose (in);
68           fprintf (stderr, _("cannot open `%s': %s"),
69                    argv[2], strerror (errno));
70           return 1;
71         }
72       out_fname = argv[2];
73     }
74   else
75     out = stdout;
76
77   while (1)
78     {
79       char *parsed;
80
81       if (getline (&buf, &bufsize, in) < 0)
82         break;
83
84       {
85         char *oldname = NULL;
86         char *newsuffix;
87
88         oldname = entryname;
89         parsed = grub_legacy_parse (buf, &entryname, &newsuffix);
90         if (newsuffix)
91           {
92             suffixlen += strlen (newsuffix);
93             suffix = xrealloc (suffix, suffixlen + 1);
94             strcat (suffix, newsuffix);
95           }
96         if (oldname != entryname && oldname)
97           fprintf (out, "}\n\n");
98         if (oldname != entryname)
99           {
100             char *escaped = grub_legacy_escape (entryname, strlen (entryname));
101             fprintf (out, "menuentry \'%s\' {\n", escaped);
102             free (escaped);
103             free (oldname);
104           }
105       }
106
107       if (parsed)
108         fprintf (out, "%s%s", entryname ? "  " : "", parsed);
109       free (parsed);
110       parsed = NULL;
111     }
112
113   if (entryname)
114     fprintf (out, "}\n\n");
115
116   if (fwrite (suffix, 1, suffixlen, out) != suffixlen)
117     {
118       if (out_fname)
119         grub_util_error ("cannot write to `%s': %s",
120                          out_fname, strerror (errno));
121       else
122         grub_util_error ("cannot write to the stdout: %s", strerror (errno));
123     }
124
125   free (buf);
126   free (suffix);
127   free (entryname);
128
129   if (in != stdin)
130     fclose (in);
131   if (out != stdout)
132     fclose (out);
133
134   return 0;
135 }