Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-menulst2cfg.c
bloba39f8693947302fd6f2f47ea1aaba5655a3cf5c4
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <config.h>
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>
29 int
30 main (int argc, char **argv)
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;
40 grub_util_host_init (&argc, &argv);
42 if (argc >= 2 && argv[1][0] == '-')
44 fprintf (stdout, _("Usage: %s [INFILE [OUTFILE]]\n"), argv[0]);
45 return 0;
48 if (argc >= 2)
50 in = grub_util_fopen (argv[1], "r");
51 if (!in)
53 fprintf (stderr, _("cannot open `%s': %s"),
54 argv[1], strerror (errno));
55 return 1;
58 else
59 in = stdin;
61 if (argc >= 3)
63 out = grub_util_fopen (argv[2], "w");
64 if (!out)
66 if (in != stdin)
67 fclose (in);
68 fprintf (stderr, _("cannot open `%s': %s"),
69 argv[2], strerror (errno));
70 return 1;
72 out_fname = argv[2];
74 else
75 out = stdout;
77 while (1)
79 char *parsed;
81 if (getline (&buf, &bufsize, in) < 0)
82 break;
85 char *oldname = NULL;
86 char *newsuffix;
88 oldname = entryname;
89 parsed = grub_legacy_parse (buf, &entryname, &newsuffix);
90 if (newsuffix)
92 suffixlen += strlen (newsuffix);
93 suffix = xrealloc (suffix, suffixlen + 1);
94 strcat (suffix, newsuffix);
96 if (oldname != entryname && oldname)
97 fprintf (out, "}\n\n");
98 if (oldname != entryname)
100 char *escaped = grub_legacy_escape (entryname, strlen (entryname));
101 fprintf (out, "menuentry \'%s\' {\n", escaped);
102 free (escaped);
103 free (oldname);
107 if (parsed)
108 fprintf (out, "%s%s", entryname ? " " : "", parsed);
109 free (parsed);
110 parsed = NULL;
113 if (entryname)
114 fprintf (out, "}\n\n");
116 if (fwrite (suffix, 1, suffixlen, out) != suffixlen)
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));
125 free (buf);
126 free (suffix);
127 free (entryname);
129 if (in != stdin)
130 fclose (in);
131 if (out != stdout)
132 fclose (out);
134 return 0;