2009-07-21 Pavel Roskin <proski@gnu.org>
[grub2/phcoder.git] / commands / parttool.c
blob8c985fc4b172a163a49f157802abe66c35af47cf
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_zalloc (argc * sizeof (int));
247 for (i = 1; i < argc; i++)
248 if (! parsed[i])
250 struct grub_parttool_argdesc *curarg;
251 struct grub_parttool_args *pargs;
252 for (cur = parts; cur; cur = cur->next)
253 if (grub_strcmp (dev->disk->partition->partmap->name, cur->name) == 0)
255 for (curarg = cur->args; curarg->name; curarg++)
256 if (grub_strncmp (curarg->name, args[i],
257 grub_strlen (curarg->name)) == 0
258 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
259 && (args[i][grub_strlen (curarg->name)] == '+'
260 || args[i][grub_strlen (curarg->name)] == '-'
261 || args[i][grub_strlen (curarg->name)] == 0))
262 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
263 && args[i][grub_strlen (curarg->name)] == '=')))
265 break;
266 if (curarg->name)
267 break;
269 if (! cur)
270 return grub_error (GRUB_ERR_BAD_ARGUMENT, "unrecognised argument %s",
271 args[i]);
272 ptool = cur;
273 pargs = (struct grub_parttool_args *)
274 grub_zalloc (ptool->nargs * sizeof (struct grub_parttool_args));
275 for (j = i; j < argc; j++)
276 if (! parsed[j])
278 for (curarg = ptool->args; curarg->name; curarg++)
279 if (grub_strncmp (curarg->name, args[i],
280 grub_strlen (curarg->name)) == 0
281 && ((curarg->type == GRUB_PARTTOOL_ARG_BOOL
282 && (args[j][grub_strlen (curarg->name)] == '+'
283 || args[j][grub_strlen (curarg->name)] == '-'
284 || args[j][grub_strlen (curarg->name)] == 0))
285 || (curarg->type == GRUB_PARTTOOL_ARG_VAL
286 && args[j][grub_strlen (curarg->name)] == '=')))
288 parsed[j] = 1;
289 pargs[curarg - ptool->args].set = 1;
290 switch (curarg->type)
292 case GRUB_PARTTOOL_ARG_BOOL:
293 pargs[curarg - ptool->args].bool
294 = (args[j][grub_strlen (curarg->name)] != '-');
295 break;
297 case GRUB_PARTTOOL_ARG_VAL:
298 pargs[curarg - ptool->args].str
299 = (args[j] + grub_strlen (curarg->name) + 1);
300 break;
302 case GRUB_PARTTOOL_ARG_END:
303 break;
308 err = ptool->func (dev, pargs);
309 grub_free (pargs);
310 if (err)
311 break;
314 grub_free (parsed);
315 grub_device_close (dev);
316 return err;
319 static grub_command_t cmd;
321 GRUB_MOD_INIT(parttool)
323 mymod = mod;
324 cmd = grub_register_command ("parttool", grub_cmd_parttool,
325 "parttool PARTITION COMMANDS",
326 helpmsg);
329 GRUB_MOD_FINI(parttool)
331 grub_unregister_command (cmd);