changed zfs files naming to (hdX,Y)/filesystem@@file
[grub2/phcoder.git] / commands / parttool.c
blobc807f066b8f9f9e795653f7fc3fdf69310a12112
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>
33 static struct grub_parttool *parts = 0;
34 static int curhandle = 0;
35 static grub_dl_t mymod;
36 static char helpmsg[] =
37 "perform COMMANDS on partition.\n"
38 "Use \"parttool PARTITION help\" for the list "
39 "of available commands";
41 int
42 grub_parttool_register(const char *part_name,
43 const grub_parttool_function_t func,
44 const struct grub_parttool_argdesc *args)
46 struct grub_parttool *cur;
47 int nargs = 0;
49 if (! parts)
50 grub_dl_ref (mymod);
52 cur = (struct grub_parttool *) grub_malloc (sizeof (struct grub_parttool));
53 cur->next = parts;
54 cur->name = grub_strdup (part_name);
55 cur->handle = curhandle++;
56 for (nargs = 0; args[nargs].name != 0; nargs++);
57 cur->nargs = nargs;
58 cur->args = (struct grub_parttool_argdesc *)
59 grub_malloc ((nargs + 1) * sizeof (struct grub_parttool_argdesc));
60 grub_memcpy (cur->args, args,
61 (nargs + 1) * sizeof (struct grub_parttool_argdesc));
63 cur->func = func;
64 parts = cur;
65 return cur->handle;
68 void
69 grub_parttool_unregister (int handle)
71 struct grub_parttool *prev = 0, *cur, *t;
72 for (cur = parts; cur; )
73 if (cur->handle == handle)
75 grub_free (cur->args);
76 grub_free (cur->name);
77 if (prev)
78 prev->next = cur->next;
79 else
80 parts = cur->next;
81 t = cur;
82 cur = cur->next;
83 grub_free (t);
85 else
87 prev = cur;
88 cur = cur->next;
90 if (! parts)
91 grub_dl_unref (mymod);
94 static grub_err_t
95 grub_cmd_parttool (grub_command_t cmd __attribute__ ((unused)),
96 int argc, char **args)
98 grub_device_t dev;
99 struct grub_parttool *cur, *ptool;
100 int *parsed;
101 int i, j;
102 grub_err_t err = GRUB_ERR_NONE;
104 auto grub_err_t show_help (void);
105 grub_err_t show_help (void)
107 int found = 0;
108 for (cur = parts; cur; cur = cur->next)
109 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
111 struct grub_parttool_argdesc *curarg;
112 found = 1;
113 for (curarg = cur->args; curarg->name; curarg++)
115 int spacing = 20;
117 spacing -= grub_strlen (curarg->name);
118 grub_printf ("%s", curarg->name);
120 switch (curarg->type)
122 case GRUB_PARTTOOL_ARG_BOOL:
123 grub_printf ("+/-");
124 spacing -= 3;
125 break;
127 case GRUB_PARTTOOL_ARG_VAL:
128 grub_printf ("=VAL");
129 spacing -= 4;
130 break;
132 case GRUB_PARTTOOL_ARG_END:
133 break;
135 while (spacing-- > 0)
136 grub_printf (" ");
137 grub_printf ("%s\n", curarg->desc);
140 if (! found)
141 grub_printf ("Sorry no parttool is available for %s\n",
142 dev->disk->partition->partmap->name);
143 return GRUB_ERR_NONE;
146 if (argc < 1)
148 grub_printf ("%s\n", helpmsg);
149 return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few arguments");
152 if (args[0][0] == '(' && args[0][grub_strlen (args[0]) - 1] == ')')
154 args[0][grub_strlen (args[0]) - 1] = 0;
155 dev = grub_device_open (args[0] + 1);
156 args[0][grub_strlen (args[0]) - 1] = ')';
158 else
159 dev = grub_device_open (args[0]);
161 if (! dev)
162 return grub_errno;
164 if (! dev->disk)
166 grub_device_close (dev);
167 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a disk");
170 if (! dev->disk->partition)
172 grub_device_close (dev);
173 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not a partition");
176 /* Load modules. */
177 #ifndef GRUB_UTIL
179 const char *prefix;
180 prefix = grub_env_get ("prefix");
181 if (prefix)
183 char *filename;
185 filename = grub_malloc (grub_strlen (prefix) + sizeof ("/parttool.lst"));
186 if (filename)
188 grub_file_t file;
190 grub_sprintf (filename, "%s/parttool.lst", prefix);
191 file = grub_file_open (filename);
192 if (file)
194 char *buf = 0;
195 for (;; grub_free(buf))
197 char *p, *name;
199 buf = grub_file_getline (file);
201 if (! buf)
202 break;
204 name = buf;
206 if (! grub_isgraph (name[0]))
207 continue;
209 p = grub_strchr (name, ':');
210 if (! p)
211 continue;
213 *p = '\0';
214 while (*++p == ' ')
217 if (! grub_isgraph (*p))
218 continue;
220 if (grub_strcmp (name, dev->disk->partition->partmap->name)
221 != 0)
222 continue;
224 grub_dl_load (p);
227 grub_file_close (file);
230 grub_free (filename);
233 /* Ignore errors. */
234 grub_errno = GRUB_ERR_NONE;
236 #endif
238 if (argc == 1)
239 return show_help ();
241 for (i = 1; i < argc; i++)
242 if (grub_strcmp (args[i], "help") == 0)
243 return show_help ();
245 parsed = (int *) grub_malloc (argc * sizeof (int));
246 grub_memset (parsed, 0, argc * sizeof (int));
248 for (i = 1; i < argc; i++)
249 if (! parsed[i])
251 struct grub_parttool_argdesc *curarg;
252 struct grub_parttool_args *pargs;
253 for (cur = parts; cur; cur = cur->next)
254 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
256 for (curarg = cur->args; curarg->name; curarg++)
257 if (grub_strncmp (curarg->name, args[i],
258 grub_strlen (curarg->name)) == 0
259 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
260 && (args[i][grub_strlen (curarg->name)] == '+'
261 || args[i][grub_strlen (curarg->name)] == '-'
262 || args[i][grub_strlen (curarg->name)] == 0))
263 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
264 && args[i][grub_strlen (curarg->name)] == '=')))
266 break;
267 if (curarg->name)
268 break;
270 if (! cur)
271 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
272 args[i]);
273 ptool = cur;
274 pargs = (struct grub_parttool_args *)
275 grub_malloc (ptool->nargs * sizeof (struct grub_parttool_args));
276 grub_memset (pargs, 0,
277 ptool->nargs * sizeof (struct grub_parttool_args));
278 for (j = i; j < argc; j++)
279 if (! parsed[j])
281 for (curarg = ptool->args; curarg->name; curarg++)
282 if (grub_strncmp (curarg->name, args[i],
283 grub_strlen (curarg->name)) == 0
284 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
285 && (args[j][grub_strlen (curarg->name)] == '+'
286 || args[j][grub_strlen (curarg->name)] == '-'
287 || args[j][grub_strlen (curarg->name)] == 0))
288 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
289 && args[j][grub_strlen (curarg->name)] == '=')))
291 parsed[j] = 1;
292 pargs[curarg - ptool->args].set = 1;
293 switch (curarg->type)
295 case GRUB_PARTTOOL_ARG_BOOL:
296 pargs[curarg - ptool->args].bool
297 = (args[j][grub_strlen (curarg->name)] != '-');
298 break;
300 case GRUB_PARTTOOL_ARG_VAL:
301 pargs[curarg - ptool->args].str
302 = (args[j] + grub_strlen (curarg->name) + 1);
303 break;
305 case GRUB_PARTTOOL_ARG_END:
306 break;
311 err = ptool->func (dev, pargs);
312 grub_free (pargs);
313 if (err)
314 break;
317 grub_free (parsed);
318 grub_device_close (dev);
319 return err;
322 static grub_command_t cmd;
324 GRUB_MOD_INIT(parttool)
326 mymod = mod;
327 cmd = grub_register_command ("parttool", grub_cmd_parttool,
328 "parttool PARTITION COMMANDS",
329 helpmsg);
332 GRUB_MOD_FINI(parttool)
334 grub_unregister_command (cmd);