Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / parttool.c
blob42c590e1a897c2af772c862a1aedf5d658f2181e
1 /* parttool.c - common dispatcher and parser for partition operations */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <grub/types.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/err.h>
25 #include <grub/dl.h>
26 #include <grub/normal.h>
27 #include <grub/device.h>
28 #include <grub/disk.h>
29 #include <grub/partition.h>
30 #include <grub/parttool.h>
31 #include <grub/command.h>
32 #include <grub/i18n.h>
34 GRUB_MOD_LICENSE ("GPLv2+");
36 static struct grub_parttool *parts = 0;
37 static int curhandle = 0;
38 static grub_dl_t mymod;
39 static char helpmsg[] =
40 N_("Perform COMMANDS on partition.\n"
41 "Use `parttool PARTITION help' for the list "
42 "of available commands.");
44 int
45 grub_parttool_register(const char *part_name,
46 const grub_parttool_function_t func,
47 const struct grub_parttool_argdesc *args)
49 struct grub_parttool *cur;
50 int nargs = 0;
52 if (! parts)
53 grub_dl_ref (mymod);
55 cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
56 cur->next = parts;
57 cur->name = grub_strdup (part_name);
58 cur->handle = curhandle++;
59 for (nargs = 0; args[nargs].name != 0; nargs++);
60 cur->nargs = nargs;
61 cur->args = (struct grub_parttool_argdesc *)
62 grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
63 grub_memcpy (cur->args, args,
64 (nargs + 1) * sizeof (struct grub_parttool_argdesc));
66 cur->func = func;
67 parts = cur;
68 return cur->handle;
71 void
72 grub_parttool_unregister (int handle)
74 struct grub_parttool *prev = 0, *cur, *t;
75 for (cur = parts; cur; )
76 if (cur->handle == handle)
78 grub_free (cur->args);
79 grub_free (cur->name);
80 if (prev)
81 prev->next = cur->next;
82 else
83 parts = cur->next;
84 t = cur;
85 cur = cur->next;
86 grub_free (t);
88 else
90 prev = cur;
91 cur = cur->next;
93 if (! parts)
94 grub_dl_unref (mymod);
97 static grub_err_t
98 show_help (grub_device_t dev)
100 int found = 0;
101 struct grub_parttool *cur;
103 for (cur = parts; cur; cur = cur->next)
104 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
106 struct grub_parttool_argdesc *curarg;
107 found = 1;
108 for (curarg = cur->args; curarg->name; curarg++)
110 int spacing = 20;
112 spacing -= grub_strlen (curarg->name);
113 grub_printf ("%s", curarg->name);
115 switch (curarg->type)
117 case GRUB_PARTTOOL_ARG_BOOL:
118 grub_printf ("+/-");
119 spacing -= 3;
120 break;
122 case GRUB_PARTTOOL_ARG_VAL:
123 grub_xputs (_("=VAL"));
124 spacing -= 4;
125 break;
127 case GRUB_PARTTOOL_ARG_END:
128 break;
130 while (spacing-- > 0)
131 grub_printf (" ");
132 grub_puts_ (curarg->desc);
135 if (! found)
136 grub_printf_ (N_("Sorry, no parttool is available for %s\n"),
137 dev->disk->partition->partmap->name);
138 return GRUB_ERR_NONE;
141 static grub_err_t
142 grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
143 int argc, char **args)
145 grub_device_t dev;
146 struct grub_parttool *cur, *ptool;
147 int *parsed;
148 int i, j;
149 grub_err_t err = GRUB_ERR_NONE;
151 if (argc < 1)
153 grub_puts_ (helpmsg);
154 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
157 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
159 args[0][grub_strlen (args[0]) - 1] = 0;
160 dev = grub_device_open (args[0] + 1);
161 args[0][grub_strlen (args[0]) - 1] = ')';
163 else
164 dev = grub_device_open (args[0]);
166 if (! dev)
167 return grub_errno;
169 if (! dev->disk)
171 grub_device_close (dev);
172 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
175 if (! dev->disk->partition)
177 grub_device_close (dev);
178 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
181 /* Load modules. */
182 if (! grub_no_modules)
184 const char *prefix;
185 prefix = grub_env_get ("prefix");
186 if (prefix)
188 char *filename;
190 filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
191 "/parttool.lst", prefix);
192 if (filename)
194 grub_file_t file;
196 file = grub_file_open (filename);
197 if (file)
199 char *buf = 0;
200 for (;; grub_free(buf))
202 char *p, *name;
204 buf = grub_file_getline (file);
206 if (! buf)
207 break;
209 name = buf;
210 while (grub_isspace (name[0]))
211 name++;
213 if (! grub_isgraph (name[0]))
214 continue;
216 p = grub_strchr (name, ':');
217 if (! p)
218 continue;
220 *p = '\0';
221 p++;
222 while (*p == ' ' || *p == '\t')
223 p++;
225 if (! grub_isgraph (*p))
226 continue;
228 if (grub_strcmp (name, dev->disk->partition->partmap->name)
229 != 0)
230 continue;
232 grub_dl_load (p);
235 grub_file_close (file);
238 grub_free (filename);
241 /* Ignore errors. */
242 grub_errno = GRUB_ERR_NONE;
245 if (argc == 1)
247 err = show_help (dev);
248 grub_device_close (dev);
249 return err;
252 for (i = 1; i < argc; i++)
253 if (grub_strcmp (args[i], "help") == 0)
255 err = show_help (dev);
256 grub_device_close (dev);
257 return err;
260 parsed = (int *) grub_zalloc (argc * sizeof (int));
262 for (i = 1; i < argc; i++)
263 if (! parsed[i])
265 struct grub_parttool_argdesc *curarg;
266 struct grub_parttool_args *pargs;
267 for (cur = parts; cur; cur = cur->next)
268 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
270 for (curarg = cur->args; curarg->name; curarg++)
271 if (grub_strncmp (curarg->name, args[i],
272 grub_strlen (curarg->name)) == 0
273 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
274 && (args[i][grub_strlen (curarg->name)] == '+'
275 || args[i][grub_strlen (curarg->name)] == '-'
276 || args[i][grub_strlen (curarg->name)] == 0))
277 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
278 && args[i][grub_strlen (curarg->name)] == '=')))
280 break;
281 if (curarg->name)
282 break;
284 if (! cur)
286 grub_device_close (dev);
287 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unknown argument `%s'"),
288 args[i]);
290 ptool = cur;
291 pargs = (struct grub_parttool_args *)
292 grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
293 for (j = i; j < argc; j++)
294 if (! parsed[j])
296 for (curarg = ptool->args; curarg->name; curarg++)
297 if (grub_strncmp (curarg->name, args[j],
298 grub_strlen (curarg->name)) == 0
299 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
300 && (args[j][grub_strlen (curarg->name)] == '+'
301 || args[j][grub_strlen (curarg->name)] == '-'
302 || args[j][grub_strlen (curarg->name)] == 0))
303 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
304 && args[j][grub_strlen (curarg->name)] == '=')))
306 parsed[j] = 1;
307 pargs[curarg - ptool->args].set = 1;
308 switch (curarg->type)
310 case GRUB_PARTTOOL_ARG_BOOL:
311 pargs[curarg - ptool->args].bool
312 = (args[j][grub_strlen (curarg->name)] != '-');
313 break;
315 case GRUB_PARTTOOL_ARG_VAL:
316 pargs[curarg - ptool->args].str
317 = (args[j] + grub_strlen (curarg->name) + 1);
318 break;
320 case GRUB_PARTTOOL_ARG_END:
321 break;
326 err = ptool->func (dev, pargs);
327 grub_free (pargs);
328 if (err)
329 break;
332 grub_free (parsed);
333 grub_device_close (dev);
334 return err;
337 static grub_command_t cmd;
339 GRUB_MOD_INIT(parttool)
341 mymod = mod;
342 cmd = grub_register_command ("parttool", grub_cmd_parttool,
343 N_("PARTITION COMMANDS"),
344 helpmsg);
347 GRUB_MOD_FINI(parttool)
349 grub_unregister_command (cmd);