Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-syslinux2cfg.c
blobf4fda6db9bb5aab3e5ff2adb11f999b6293218fe
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010,2012,2013 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/>.
20 #include <config.h>
22 #include <grub/util/misc.h>
23 #include <grub/i18n.h>
24 #include <grub/term.h>
25 #include <grub/font.h>
26 #include <grub/emu/hostdisk.h>
28 #define _GNU_SOURCE 1
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <grub/err.h>
36 #include <grub/types.h>
37 #include <grub/dl.h>
38 #include <grub/misc.h>
39 #include <grub/mm.h>
40 #include <grub/syslinux_parse.h>
42 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
43 #pragma GCC diagnostic ignored "-Wmissing-declarations"
44 #include <argp.h>
45 #pragma GCC diagnostic error "-Wmissing-prototypes"
46 #pragma GCC diagnostic error "-Wmissing-declarations"
48 #include "progname.h"
50 struct arguments
52 char *input;
53 char *root;
54 char *target_root;
55 char *cwd;
56 char *target_cwd;
57 char *output;
58 int verbosity;
59 grub_syslinux_flavour_t flav;
62 static struct argp_option options[] = {
63 {"target-root", 't', N_("DIR"), 0,
64 N_("root directory as it will be seen on runtime [default=/]."), 0},
65 {"root", 'r', N_("DIR"), 0,
66 N_("root directory of the syslinux disk [default=/]."), 0},
67 {"target-cwd", 'T', N_("DIR"), 0,
68 N_(
69 "current directory of syslinux as it will be seen on runtime [default is parent directory of input file]."
70 ), 0},
71 {"cwd", 'c', N_("DIR"), 0,
72 N_("current directory of syslinux [default is parent directory of input file]."), 0},
74 {"output", 'o', N_("FILE"), 0, N_("write output to FILE [default=stdout]."), 0},
75 {"isolinux", 'i', 0, 0, N_("assume input is an isolinux configuration file."), 0},
76 {"pxelinux", 'p', 0, 0, N_("assume input is a pxelinux configuration file."), 0},
77 {"syslinux", 's', 0, 0, N_("assume input is a syslinux configuration file."), 0},
78 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
79 { 0, 0, 0, 0, 0, 0 }
82 static error_t
83 argp_parser (int key, char *arg, struct argp_state *state)
85 /* Get the input argument from argp_parse, which we
86 know is a pointer to our arguments structure. */
87 struct arguments *arguments = state->input;
89 switch (key)
91 case 't':
92 free (arguments->target_root);
93 arguments->target_root = xstrdup (arg);
94 break;
96 case 'T':
97 free (arguments->target_cwd);
98 arguments->target_cwd = xstrdup (arg);
99 break;
101 case 'c':
102 free (arguments->cwd);
103 arguments->cwd = xstrdup (arg);
104 break;
106 case 'o':
107 free (arguments->output);
108 arguments->output = xstrdup (arg);
109 break;
111 case ARGP_KEY_ARG:
112 if (!arguments->input)
114 arguments->input = xstrdup (arg);
115 return 0;
117 return ARGP_ERR_UNKNOWN;
119 case 'r':
120 free (arguments->root);
121 arguments->root = xstrdup (arg);
122 return 0;
124 case 'i':
125 arguments->flav = GRUB_SYSLINUX_ISOLINUX;
126 break;
128 case 's':
129 arguments->flav = GRUB_SYSLINUX_SYSLINUX;
130 break;
131 case 'p':
132 arguments->flav = GRUB_SYSLINUX_PXELINUX;
133 break;
135 case 'v':
136 arguments->verbosity++;
137 break;
139 default:
140 return ARGP_ERR_UNKNOWN;
143 return 0;
146 static struct argp argp = {
147 options, argp_parser, N_("FILE"),
148 N_("Transform syslinux config into GRUB one."),
149 NULL, NULL, NULL
153 main (int argc, char *argv[])
155 struct arguments arguments;
157 grub_util_host_init (&argc, &argv);
159 /* Check for options. */
160 memset (&arguments, 0, sizeof (struct arguments));
161 if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
163 fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
164 exit(1);
167 if (!arguments.input)
169 fprintf (stderr, "%s", _("Missing arguments\n"));
170 exit(1);
173 grub_init_all ();
174 grub_hostfs_init ();
175 grub_host_init ();
177 char *t, *inpfull, *rootfull, *res;
178 t = canonicalize_file_name (arguments.input);
179 if (!t)
181 grub_util_error (_("cannot open `%s': %s"), arguments.input,
182 strerror (errno));
185 inpfull = xasprintf ("(host)/%s", t);
186 free (t);
188 t = canonicalize_file_name (arguments.root ? : "/");
189 if (!t)
191 grub_util_error (_("cannot open `%s': %s"), arguments.root,
192 strerror (errno));
195 rootfull = xasprintf ("(host)/%s", t);
196 free (t);
198 char *cwd = xstrdup (arguments.input);
199 char *p = strrchr (cwd, '/');
200 char *cwdfull;
201 if (p)
202 *p = '\0';
203 else
205 free (cwd);
206 cwd = xstrdup (".");
209 t = canonicalize_file_name (arguments.cwd ? : cwd);
210 if (!t)
212 grub_util_error (_("cannot open `%s': %s"), arguments.root,
213 strerror (errno));
216 cwdfull = xasprintf ("(host)/%s", t);
217 free (t);
219 res = grub_syslinux_config_file (rootfull, arguments.target_root ? : "/",
220 cwdfull, arguments.target_cwd ? : cwd,
221 inpfull, arguments.flav);
222 if (!res)
223 grub_util_error ("%s", grub_errmsg);
224 if (arguments.output)
226 FILE *f = grub_util_fopen (arguments.output, "wb");
227 if (!f)
228 grub_util_error (_("cannot open `%s': %s"), arguments.output,
229 strerror (errno));
230 fwrite (res, 1, strlen (res), f);
231 fclose (f);
233 else
234 printf ("%s\n", res);
235 free (res);
236 free (rootfull);
237 free (inpfull);
238 free (arguments.root);
239 free (arguments.output);
240 free (arguments.target_root);
241 free (arguments.input);
242 free (cwdfull);
243 free (cwd);
245 return 0;