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/>.
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>
36 #include <grub/types.h>
38 #include <grub/misc.h>
40 #include <grub/syslinux_parse.h>
42 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
43 #pragma GCC diagnostic ignored "-Wmissing-declarations"
45 #pragma GCC diagnostic error "-Wmissing-prototypes"
46 #pragma GCC diagnostic error "-Wmissing-declarations"
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,
69 "current directory of syslinux as it will be seen on runtime [default is parent directory of input file]."
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},
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
;
92 free (arguments
->target_root
);
93 arguments
->target_root
= xstrdup (arg
);
97 free (arguments
->target_cwd
);
98 arguments
->target_cwd
= xstrdup (arg
);
102 free (arguments
->cwd
);
103 arguments
->cwd
= xstrdup (arg
);
107 free (arguments
->output
);
108 arguments
->output
= xstrdup (arg
);
112 if (!arguments
->input
)
114 arguments
->input
= xstrdup (arg
);
117 return ARGP_ERR_UNKNOWN
;
120 free (arguments
->root
);
121 arguments
->root
= xstrdup (arg
);
125 arguments
->flav
= GRUB_SYSLINUX_ISOLINUX
;
129 arguments
->flav
= GRUB_SYSLINUX_SYSLINUX
;
132 arguments
->flav
= GRUB_SYSLINUX_PXELINUX
;
136 arguments
->verbosity
++;
140 return ARGP_ERR_UNKNOWN
;
146 static struct argp argp
= {
147 options
, argp_parser
, N_("FILE"),
148 N_("Transform syslinux config into GRUB one."),
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"));
167 if (!arguments
.input
)
169 fprintf (stderr
, "%s", _("Missing arguments\n"));
177 char *t
, *inpfull
, *rootfull
, *res
;
178 t
= canonicalize_file_name (arguments
.input
);
181 grub_util_error (_("cannot open `%s': %s"), arguments
.input
,
185 inpfull
= xasprintf ("(host)/%s", t
);
188 t
= canonicalize_file_name (arguments
.root
? : "/");
191 grub_util_error (_("cannot open `%s': %s"), arguments
.root
,
195 rootfull
= xasprintf ("(host)/%s", t
);
198 char *cwd
= xstrdup (arguments
.input
);
199 char *p
= strrchr (cwd
, '/');
209 t
= canonicalize_file_name (arguments
.cwd
? : cwd
);
212 grub_util_error (_("cannot open `%s': %s"), arguments
.root
,
216 cwdfull
= xasprintf ("(host)/%s", t
);
219 res
= grub_syslinux_config_file (rootfull
, arguments
.target_root
? : "/",
220 cwdfull
, arguments
.target_cwd
? : cwd
,
221 inpfull
, arguments
.flav
);
223 grub_util_error ("%s", grub_errmsg
);
224 if (arguments
.output
)
226 FILE *f
= grub_util_fopen (arguments
.output
, "wb");
228 grub_util_error (_("cannot open `%s': %s"), arguments
.output
,
230 fwrite (res
, 1, strlen (res
), f
);
234 printf ("%s\n", res
);
238 free (arguments
.root
);
239 free (arguments
.output
);
240 free (arguments
.target_root
);
241 free (arguments
.input
);