]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/fdisk-menu.c
fdisk: value is never read [clang analyze]
[thirdparty/util-linux.git] / disk-utils / fdisk-menu.c
CommitLineData
161b0d1a
KZ
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <ctype.h>
6#include <stdint.h>
7
8#include "c.h"
9#include "fdisk.h"
9f280903 10#include "pt-sun.h"
f02fecd1
KZ
11#include "pt-mbr.h"
12
161b0d1a 13struct menu_entry {
7e937b77
KZ
14 const char key; /* command key */
15 const char *title; /* help string */
16 unsigned int normal : 1, /* normal mode */
17 expert : 1, /* expert mode */
18 hidden : 1; /* be sensitive for this key,
19 but don't print it in help */
161b0d1a 20
7e937b77
KZ
21 enum fdisk_labeltype label; /* only for this label */
22 enum fdisk_labeltype exclude; /* all labels except this */
f92e1810 23 enum fdisk_labeltype parent; /* for nested PT */
161b0d1a
KZ
24};
25
26#define IS_MENU_SEP(e) ((e)->key == '-')
27#define IS_MENU_HID(e) ((e)->hidden)
28
29struct menu {
30 enum fdisk_labeltype label; /* only for this label */
31 enum fdisk_labeltype exclude; /* all labels except this */
32
f92e1810
KZ
33 unsigned int nonested : 1; /* don't make this menu active in nested PT */
34
a47fec81 35 int (*callback)(struct fdisk_context **,
a410f8df
KZ
36 const struct menu *,
37 const struct menu_entry *);
161b0d1a
KZ
38
39 struct menu_entry entries[]; /* NULL terminated array */
40};
41
42struct menu_context {
43 size_t menu_idx; /* the current menu */
44 size_t entry_idx; /* index with in the current menu */
45};
46
47#define MENU_CXT_EMPTY { 0, 0 }
8e40a677 48#define DECLARE_MENU_CB(x) \
a47fec81 49 static int x(struct fdisk_context **, \
8e40a677
KZ
50 const struct menu *, \
51 const struct menu_entry *)
161b0d1a 52
8e40a677 53DECLARE_MENU_CB(gpt_menu_cb);
9f280903 54DECLARE_MENU_CB(sun_menu_cb);
aae727f2 55DECLARE_MENU_CB(sgi_menu_cb);
8e40a677 56DECLARE_MENU_CB(geo_menu_cb);
f02fecd1 57DECLARE_MENU_CB(dos_menu_cb);
b529ea2a 58DECLARE_MENU_CB(bsd_menu_cb);
b3ac22ef 59DECLARE_MENU_CB(createlabel_menu_cb);
89309968 60DECLARE_MENU_CB(generic_menu_cb);
b9e94cd7 61
161b0d1a
KZ
62/*
63 * Menu entry macros:
64 * MENU_X* expert mode only
65 * MENU_B* both -- expert + normal mode
66 *
7e937b77
KZ
67 * *_E exclude this label
68 * *_H hidden
69 * *_L only for this label
161b0d1a
KZ
70 */
71
72/* separator */
73#define MENU_SEP(t) { .title = t, .key = '-', .normal = 1 }
74#define MENU_XSEP(t) { .title = t, .key = '-', .expert = 1 }
75#define MENU_BSEP(t) { .title = t, .key = '-', .expert = 1, .normal = 1 }
76
77/* entry */
78#define MENU_ENT(k, t) { .title = t, .key = k, .normal = 1 }
79#define MENU_ENT_E(k, t, l) { .title = t, .key = k, .normal = 1, .exclude = l }
7e937b77 80#define MENU_ENT_L(k, t, l) { .title = t, .key = k, .normal = 1, .label = l }
161b0d1a
KZ
81
82#define MENU_XENT(k, t) { .title = t, .key = k, .expert = 1 }
83#define MENU_XENT_H(k, t) { .title = t, .key = k, .expert = 1, .hidden = 1 }
84
85#define MENU_BENT(k, t) { .title = t, .key = k, .expert = 1, .normal = 1 }
27ddd4f1 86#define MENU_BENT_E(k, t, l) { .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l }
161b0d1a 87
f92e1810
KZ
88#define MENU_ENT_NEST(k, t, l, p) { .title = t, .key = k, .normal = 1, .label = l, .parent = p }
89#define MENU_XENT_NEST(k, t, l, p) { .title = t, .key = k, .expert = 1, .label = l, .parent = p }
161b0d1a
KZ
90
91/* Generic menu */
92struct menu menu_generic = {
89309968 93 .callback = generic_menu_cb,
161b0d1a 94 .entries = {
6ae5e1e0 95 MENU_BSEP(N_("Generic")),
161b0d1a 96 MENU_ENT ('d', N_("delete a partition")),
0efd951c 97 MENU_ENT ('F', N_("list free unpartitioned space")),
161b0d1a
KZ
98 MENU_ENT ('l', N_("list known partition types")),
99 MENU_ENT ('n', N_("add a new partition")),
100 MENU_BENT ('p', N_("print the partition table")),
6ae5e1e0 101 MENU_ENT ('t', N_("change a partition type")),
27ddd4f1 102 MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
3e3b51b3 103 MENU_ENT ('i', N_("print information about a partition")),
161b0d1a 104
e916600f
KZ
105 MENU_XENT('d', N_("print the raw data of the first sector from the device")),
106 MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
dd7ba604 107 MENU_XENT('f', N_("fix partitions order")),
6ae5e1e0 108
161b0d1a
KZ
109 MENU_SEP(N_("Misc")),
110 MENU_BENT ('m', N_("print this menu")),
111 MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT),
22ddf547 112 MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD),
161b0d1a 113
a30e4ef4 114 MENU_SEP(N_("Script")),
5cb9918d
KZ
115 MENU_ENT ('I', N_("load disk layout from sfdisk script file")),
116 MENU_ENT ('O', N_("dump disk layout to sfdisk script file")),
a30e4ef4 117
2a1a67df 118 MENU_BSEP(N_("Save & Exit")),
22ddf547
KZ
119 MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_BSD),
120 MENU_ENT_L('w', N_("write table to disk"), FDISK_DISKLABEL_BSD),
161b0d1a
KZ
121 MENU_BENT ('q', N_("quit without saving changes")),
122 MENU_XENT ('r', N_("return to main menu")),
f92e1810
KZ
123
124 MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS),
161b0d1a
KZ
125
126 { 0, NULL }
127 }
128};
129
130struct menu menu_createlabel = {
b3ac22ef 131 .callback = createlabel_menu_cb,
22ddf547 132 .exclude = FDISK_DISKLABEL_BSD,
f92e1810 133 .nonested = 1,
161b0d1a
KZ
134 .entries = {
135 MENU_SEP(N_("Create a new label")),
136 MENU_ENT('g', N_("create a new empty GPT partition table")),
137 MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")),
138 MENU_ENT('o', N_("create a new empty DOS partition table")),
139 MENU_ENT('s', N_("create a new empty Sun partition table")),
140
141 /* backward compatibility -- be sensitive to 'g', but don't
142 * print it in the expert menu */
143 MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")),
144 { 0, NULL }
145 }
146};
147
be9ba6f3 148struct menu menu_geo = {
8e40a677 149 .callback = geo_menu_cb,
22ddf547 150 .exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD,
be9ba6f3
KZ
151 .entries = {
152 MENU_XSEP(N_("Geometry")),
153 MENU_XENT('c', N_("change number of cylinders")),
154 MENU_XENT('h', N_("change number of heads")),
155 MENU_XENT('s', N_("change number of sectors/track")),
156 { 0, NULL }
157 }
158};
159
161b0d1a 160struct menu menu_gpt = {
b9e94cd7 161 .callback = gpt_menu_cb,
161b0d1a
KZ
162 .label = FDISK_DISKLABEL_GPT,
163 .entries = {
164 MENU_XSEP(N_("GPT")),
35b1f0a4 165 MENU_XENT('i', N_("change disk GUID")),
161b0d1a 166 MENU_XENT('n', N_("change partition name")),
35b1f0a4 167 MENU_XENT('u', N_("change partition UUID")),
f92e1810 168 MENU_XENT('M', N_("enter protective/hybrid MBR")),
f362d863
KZ
169
170 MENU_XSEP(""),
171 MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")),
172 MENU_XENT('B', N_("toggle the no block IO protocol flag")),
173 MENU_XENT('R', N_("toggle the required partition flag")),
174 MENU_XENT('S', N_("toggle the GUID specific bits")),
175
161b0d1a
KZ
176 { 0, NULL }
177 }
178};
179
2a1a67df 180struct menu menu_sun = {
9f280903 181 .callback = sun_menu_cb,
2a1a67df
KZ
182 .label = FDISK_DISKLABEL_SUN,
183 .entries = {
184 MENU_BSEP(N_("Sun")),
aaa43826 185 MENU_ENT('a', N_("toggle the read-only flag")),
2a1a67df
KZ
186 MENU_ENT('c', N_("toggle the mountable flag")),
187
188 MENU_XENT('a', N_("change number of alternate cylinders")),
2a1a67df 189 MENU_XENT('e', N_("change number of extra sectors per cylinder")),
2a1a67df
KZ
190 MENU_XENT('i', N_("change interleave factor")),
191 MENU_XENT('o', N_("change rotation speed (rpm)")),
2a1a67df
KZ
192 MENU_XENT('y', N_("change number of physical cylinders")),
193 { 0, NULL }
194 }
195};
196
6ae5e1e0 197struct menu menu_sgi = {
aae727f2 198 .callback = sgi_menu_cb,
6ae5e1e0
KZ
199 .label = FDISK_DISKLABEL_SGI,
200 .entries = {
201 MENU_SEP(N_("SGI")),
202 MENU_ENT('a', N_("select bootable partition")),
203 MENU_ENT('b', N_("edit bootfile entry")),
204 MENU_ENT('c', N_("select sgi swap partition")),
aae727f2 205 MENU_ENT('i', N_("create SGI info")),
6ae5e1e0
KZ
206 { 0, NULL }
207 }
208};
209
210struct menu menu_dos = {
f02fecd1 211 .callback = dos_menu_cb,
6ae5e1e0
KZ
212 .label = FDISK_DISKLABEL_DOS,
213 .entries = {
214 MENU_BSEP(N_("DOS (MBR)")),
215 MENU_ENT('a', N_("toggle a bootable flag")),
216 MENU_ENT('b', N_("edit nested BSD disklabel")),
217 MENU_ENT('c', N_("toggle the dos compatibility flag")),
218
219 MENU_XENT('b', N_("move beginning of data in a partition")),
6ae5e1e0 220 MENU_XENT('i', N_("change the disk identifier")),
f92e1810
KZ
221
222 MENU_XENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
223 FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
6ae5e1e0
KZ
224 { 0, NULL }
225 }
226};
227
228struct menu menu_bsd = {
b529ea2a 229 .callback = bsd_menu_cb,
22ddf547 230 .label = FDISK_DISKLABEL_BSD,
6ae5e1e0
KZ
231 .entries = {
232 MENU_SEP(N_("BSD")),
233 MENU_ENT('e', N_("edit drive data")),
234 MENU_ENT('i', N_("install bootstrap")),
235 MENU_ENT('s', N_("show complete disklabel")),
6ae5e1e0 236 MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
6ae5e1e0
KZ
237 { 0, NULL }
238 }
239};
240
161b0d1a 241static const struct menu *menus[] = {
6ae5e1e0
KZ
242 &menu_gpt,
243 &menu_sun,
244 &menu_sgi,
245 &menu_dos,
246 &menu_bsd,
be9ba6f3 247 &menu_geo,
161b0d1a
KZ
248 &menu_generic,
249 &menu_createlabel,
161b0d1a
KZ
250};
251
252static const struct menu_entry *next_menu_entry(
253 struct fdisk_context *cxt,
254 struct menu_context *mc)
255{
8eccde20
KZ
256 struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
257 struct fdisk_context *parent = fdisk_get_parent(cxt);
258 unsigned int type = 0, pr_type = 0;
259
260 assert(cxt);
261
68ddb136
KZ
262 if (lb)
263 type = fdisk_label_get_type(lb);
8eccde20
KZ
264 if (parent)
265 pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
266
161b0d1a
KZ
267 while (mc->menu_idx < ARRAY_SIZE(menus)) {
268 const struct menu *m = menus[mc->menu_idx];
269 const struct menu_entry *e = &(m->entries[mc->entry_idx]);
270
f92e1810
KZ
271 /*
272 * whole-menu filter
273 */
274
be9ba6f3 275 /* no more entries */
161b0d1a 276 if (e->title == NULL ||
be9ba6f3 277 /* menu wanted for specified labels only */
8eccde20 278 (m->label && lb && !(m->label & type)) ||
f92e1810 279 /* unwanted for nested PT */
8eccde20 280 (m->nonested && parent) ||
be9ba6f3 281 /* menu excluded for specified labels */
8eccde20 282 (m->exclude && lb && (m->exclude & type))) {
161b0d1a
KZ
283 mc->menu_idx++;
284 mc->entry_idx = 0;
285 continue;
286 }
287
f92e1810
KZ
288 /*
289 * per entry filter
290 */
291
7e937b77 292 /* excluded for the current label */
8eccde20 293 if ((e->exclude && lb && e->exclude & type) ||
7e937b77 294 /* entry wanted for specified labels only */
8eccde20 295 (e->label && lb && !(e->label & type)) ||
161b0d1a 296 /* exclude non-expert entries in expect mode */
6a632136 297 (e->expert == 0 && fdisk_is_details(cxt)) ||
f92e1810 298 /* nested only */
8eccde20 299 (e->parent && (!parent || pr_type != e->parent)) ||
161b0d1a 300 /* exclude non-normal entries in normal mode */
6a632136 301 (e->normal == 0 && !fdisk_is_details(cxt))) {
161b0d1a
KZ
302 mc->entry_idx++;
303 continue;
304 }
305 mc->entry_idx++;
306 return e;
307
308 }
309 return NULL;
310}
311
3b4f9f16
KZ
312/* returns @menu and menu entry for then @key */
313static const struct menu_entry *get_fdisk_menu_entry(
314 struct fdisk_context *cxt,
315 int key,
316 const struct menu **menu)
317{
318 struct menu_context mc = MENU_CXT_EMPTY;
319 const struct menu_entry *e;
320
321 while ((e = next_menu_entry(cxt, &mc))) {
322 if (IS_MENU_SEP(e) || e->key != key)
323 continue;
324
325 if (menu)
326 *menu = menus[mc.menu_idx];
327 return e;
328 }
329
330 return NULL;
331}
332
333static int menu_detect_collisions(struct fdisk_context *cxt)
334{
335 struct menu_context mc = MENU_CXT_EMPTY;
336 const struct menu_entry *e, *r;
337
338 while ((e = next_menu_entry(cxt, &mc))) {
339 if (IS_MENU_SEP(e))
340 continue;
341
342 r = get_fdisk_menu_entry(cxt, e->key, NULL);
343 if (!r) {
e6d0c4c1 344 DBG(MENU, ul_debug("warning: not found "
3b4f9f16
KZ
345 "entry for %c", e->key));
346 return -1;
347 }
348 if (r != e) {
e6d0c4c1 349 DBG(MENU, ul_debug("warning: duplicate key '%c'",
3b4f9f16 350 e->key));
e6d0c4c1
KZ
351 DBG(MENU, ul_debug(" : %s", e->title));
352 DBG(MENU, ul_debug(" : %s", r->title));
3b4f9f16
KZ
353 abort();
354 }
355 }
356
357 return 0;
358}
359
1f75d1ce 360static int print_fdisk_menu(struct fdisk_context *cxt)
161b0d1a
KZ
361{
362 struct menu_context mc = MENU_CXT_EMPTY;
363 const struct menu_entry *e;
364
e6d0c4c1 365 ON_DBG(MENU, menu_detect_collisions(cxt));
3b4f9f16 366
6a632136 367 if (fdisk_is_details(cxt))
39f01b7b 368 printf(_("\nHelp (expert commands):\n"));
161b0d1a 369 else
39f01b7b 370 printf(_("\nHelp:\n"));
161b0d1a
KZ
371
372 while ((e = next_menu_entry(cxt, &mc))) {
373 if (IS_MENU_HID(e))
374 continue; /* hidden entry */
f362d863
KZ
375 if (IS_MENU_SEP(e) && (!e->title || !*e->title))
376 printf("\n");
377 else if (IS_MENU_SEP(e)) {
496c979a 378 color_scheme_enable("help-title", UL_COLOR_BOLD);
161b0d1a 379 printf("\n %s\n", _(e->title));
80a1712f
KZ
380 color_disable();
381 } else
161b0d1a
KZ
382 printf(" %c %s\n", e->key, _(e->title));
383 }
384 fputc('\n', stdout);
385
8eccde20
KZ
386 if (fdisk_get_parent(cxt)) {
387 struct fdisk_label *l = fdisk_get_label(cxt, NULL),
388 *p = fdisk_get_label(fdisk_get_parent(cxt), NULL);
389
f92e1810
KZ
390 fdisk_info(cxt, _("You're editing nested '%s' partition table, "
391 "primary partition table is '%s'."),
8eccde20
KZ
392 fdisk_label_get_name(l),
393 fdisk_label_get_name(p));
394 }
f92e1810 395
161b0d1a
KZ
396 return 0;
397}
398
a410f8df
KZ
399/* Asks for command, verify the key and perform the command or
400 * returns the command key if no callback for the command is
401 * implemented.
402 *
a47fec81
KZ
403 * Note that this function might exchange the context pointer to
404 * switch to another (nested) context.
405 *
a410f8df
KZ
406 * Returns: <0 on error
407 * 0 on success (the command performed)
408 * >0 if no callback (then returns the key)
409 */
a47fec81 410int process_fdisk_menu(struct fdisk_context **cxt0)
a410f8df 411{
a47fec81 412 struct fdisk_context *cxt = *cxt0;
a410f8df
KZ
413 const struct menu_entry *ent;
414 const struct menu *menu;
415 int key, rc;
416 const char *prompt;
417 char buf[BUFSIZ];
418
6a632136 419 if (fdisk_is_details(cxt))
a410f8df
KZ
420 prompt = _("Expert command (m for help): ");
421 else
422 prompt = _("Command (m for help): ");
423
424 fputc('\n',stdout);
425 rc = get_user_reply(cxt, prompt, buf, sizeof(buf));
426 if (rc)
427 return rc;
428
429 key = buf[0];
430 ent = get_fdisk_menu_entry(cxt, key, &menu);
431 if (!ent) {
432 fdisk_warnx(cxt, _("%c: unknown command"), key);
433 return -EINVAL;
434 }
435
e6d0c4c1 436 DBG(MENU, ul_debug("selected: key=%c, entry='%s'",
a410f8df 437 key, ent->title));
a410f8df
KZ
438
439 /* menu has implemented callback, use it */
27ddd4f1 440 if (menu->callback)
a47fec81
KZ
441 rc = menu->callback(cxt0, menu, ent);
442 else {
e6d0c4c1 443 DBG(MENU, ul_debug("no callback for key '%c'", key));
27ddd4f1 444 rc = -EINVAL;
a47fec81 445 }
a410f8df 446
e6d0c4c1 447 DBG(MENU, ul_debug("process menu done [rc=%d]", rc));
a47fec81 448 return rc;
a410f8df
KZ
449}
450
a30e4ef4
KZ
451static int script_read(struct fdisk_context *cxt)
452{
453 struct fdisk_script *sc = NULL;
454 char *filename = NULL;
455 int rc;
456
457 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
458 if (rc)
459 return rc;
460
461 errno = 0;
462 sc = fdisk_new_script_from_file(cxt, filename);
463 if (!sc && errno)
54fefa07 464 fdisk_warn(cxt, _("Cannot open %s"), filename);
a30e4ef4
KZ
465 else if (!sc)
466 fdisk_warnx(cxt, _("Failed to parse script file %s"), filename);
467 else if (fdisk_apply_script(cxt, sc) != 0)
468 fdisk_warnx(cxt, _("Failed to apply script %s"), filename);
469 else
470 fdisk_info(cxt, _("Script successfully applied."));
471
472 fdisk_unref_script(sc);
473 free(filename);
474 return rc;
475}
476
477static int script_write(struct fdisk_context *cxt)
478{
479 struct fdisk_script *sc = NULL;
480 char *filename = NULL;
481 FILE *f = NULL;
482 int rc;
483
484 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
485 if (rc)
486 return rc;
487
488 sc = fdisk_new_script(cxt);
489 if (!sc) {
490 fdisk_warn(cxt, _("Failed to allocate script handler"));
491 goto done;
492 }
493
494 rc = fdisk_script_read_context(sc, NULL);
495 if (rc) {
54fefa07 496 fdisk_warnx(cxt, _("Failed to transform disk layout into script"));
a30e4ef4
KZ
497 goto done;
498 }
499
500 f = fopen(filename, "w");
501 if (!f) {
54fefa07 502 fdisk_warn(cxt, _("Cannot open %s"), filename);
a30e4ef4
KZ
503 goto done;
504 }
505
506 rc = fdisk_script_write_file(sc, f);
507 if (rc)
508 fdisk_warn(cxt, _("Failed to write script %s"), filename);
509 else
510 fdisk_info(cxt, _("Script successfully saved."));
511done:
512 if (f)
513 fclose(f);
514 fdisk_unref_script(sc);
515 free(filename);
516 return rc;
517}
518
89309968
KZ
519/*
520 * Basic fdisk actions
521 */
522static int generic_menu_cb(struct fdisk_context **cxt0,
523 const struct menu *menu __attribute__((__unused__)),
524 const struct menu_entry *ent)
525{
526 struct fdisk_context *cxt = *cxt0;
527 int rc = 0;
27ddd4f1 528 size_t n;
89309968 529
27ddd4f1 530 /* actions shared between expert and normal mode */
89309968 531 switch (ent->key) {
89309968
KZ
532 case 'p':
533 list_disk_geometry(cxt);
9f670072 534 list_disklabel(cxt);
89309968 535 break;
df4bfa97 536 case 'w':
6a632136 537 if (fdisk_is_readonly(cxt)) {
e146ae4e
KZ
538 fdisk_warnx(cxt, _("Device open in read-only mode."));
539 break;
540 }
df4bfa97
KZ
541 rc = fdisk_write_disklabel(cxt);
542 if (rc)
fd56121a 543 err(EXIT_FAILURE, _("failed to write disklabel"));
8eccde20 544 if (fdisk_get_parent(cxt))
df4bfa97
KZ
545 break; /* nested PT, don't leave */
546 fdisk_info(cxt, _("The partition table has been altered."));
a57639e1
KZ
547 rc = fdisk_reread_partition_table(cxt);
548 if (!rc)
6a632136 549 rc = fdisk_deassign_device(cxt, 0);
a57639e1 550 /* fallthrough */
89309968 551 case 'q':
c7119037 552 fdisk_unref_context(cxt);
a57639e1
KZ
553 fputc('\n', stdout);
554 exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
27ddd4f1
KZ
555 case 'm':
556 rc = print_fdisk_menu(cxt);
557 break;
89309968
KZ
558 case 'v':
559 rc = fdisk_verify_disklabel(cxt);
560 break;
3e3b51b3
JLB
561 case 'i':
562 rc = print_partition_info(cxt);
563 break;
0efd951c
KZ
564 case 'F':
565 list_freespace(cxt);
566 break;
27ddd4f1
KZ
567 }
568
569 /* expert mode */
570 if (ent->expert) {
571 switch (ent->key) {
572 case 'd':
e916600f
KZ
573 dump_firstsector(cxt);
574 break;
575 case 'D':
576 dump_disklabel(cxt);
27ddd4f1 577 break;
dd7ba604
KZ
578 case 'f':
579 rc = fdisk_reorder_partitions(cxt);
580 break;
27ddd4f1 581 case 'r':
6a632136 582 rc = fdisk_enable_details(cxt, 0);
27ddd4f1
KZ
583 break;
584 }
585 return rc;
586 }
587
588 /* normal mode */
589 switch (ent->key) {
590 case 'd':
591 rc = fdisk_ask_partnum(cxt, &n, FALSE);
592 if (!rc)
593 rc = fdisk_delete_partition(cxt, n);
594 if (rc)
48d4d931 595 fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1);
27ddd4f1 596 else
48d4d931 597 fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1);
27ddd4f1 598 break;
a30e4ef4
KZ
599 case 'I':
600 script_read(cxt);
601 break;
602 case 'O':
603 script_write(cxt);
604 break;
27ddd4f1
KZ
605 case 'l':
606 list_partition_types(cxt);
607 break;
608 case 'n':
c3bc7483 609 rc = fdisk_add_partition(cxt, NULL, NULL);
27ddd4f1
KZ
610 break;
611 case 't':
612 change_partition_type(cxt);
613 break;
614 case 'u':
6a632136
KZ
615 fdisk_set_unit(cxt,
616 fdisk_use_cylinders(cxt) ? "sectors" :
27ddd4f1 617 "cylinders");
6a632136 618 if (fdisk_use_cylinders(cxt))
27ddd4f1
KZ
619 fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
620 else
621 fdisk_info(cxt, _("Changing display/entry units to sectors."));
622 break;
623 case 'x':
6a632136 624 fdisk_enable_details(cxt, 1);
27ddd4f1
KZ
625 break;
626 case 'r':
f92e1810 627 /* return from nested BSD to DOS */
8eccde20
KZ
628 if (fdisk_get_parent(cxt)) {
629 *cxt0 = fdisk_get_parent(cxt);
27ddd4f1 630
fd56121a 631 fdisk_info(cxt, _("Leaving nested disklabel."));
c7119037 632 fdisk_unref_context(cxt);
b5729667 633 cxt = *cxt0;
27ddd4f1
KZ
634 }
635 break;
89309968 636 }
27ddd4f1 637
89309968
KZ
638 return rc;
639}
640
8e40a677 641
b9e94cd7
KZ
642/*
643 * This is fdisk frontend for GPT specific libfdisk functions that
644 * are not expported by generic libfdisk API.
645 */
a47fec81 646static int gpt_menu_cb(struct fdisk_context **cxt0,
b9e94cd7
KZ
647 const struct menu *menu __attribute__((__unused__)),
648 const struct menu_entry *ent)
649{
a47fec81 650 struct fdisk_context *cxt = *cxt0;
f92e1810 651 struct fdisk_context *mbr;
6936c081 652 struct fdisk_partition *pa = NULL;
b9e94cd7 653 size_t n;
f92e1810 654 int rc = 0;
b9e94cd7
KZ
655
656 assert(cxt);
657 assert(ent);
aa36c2cf 658 assert(fdisk_is_label(cxt, GPT));
b9e94cd7 659
e6d0c4c1 660 DBG(MENU, ul_debug("enter GPT menu"));
b9e94cd7 661
f92e1810
KZ
662 if (ent->expert) {
663 switch (ent->key) {
664 case 'i':
665 return fdisk_set_disklabel_id(cxt);
666 case 'M':
667 mbr = fdisk_new_nested_context(cxt, "dos");
668 if (!mbr)
669 return -ENOMEM;
670 *cxt0 = cxt = mbr;
6a632136 671 fdisk_enable_details(cxt, 1); /* keep us in expert mode */
0477369a 672 fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel."));
f92e1810
KZ
673 return 0;
674 }
35b1f0a4 675
f92e1810
KZ
676 /* actions where is necessary partnum */
677 rc = fdisk_ask_partnum(cxt, &n, FALSE);
678 if (rc)
679 return rc;
b9e94cd7 680
f92e1810
KZ
681 switch(ent->key) {
682 case 'u':
6936c081
KZ
683 pa = fdisk_new_partition(); /* new template */
684 if (!pa)
685 rc = -ENOMEM;
686 else {
687 char *str = NULL;
688 rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str);
689 if (!rc)
690 rc = fdisk_partition_set_uuid(pa, str);
691 if (!rc)
692 rc = fdisk_set_partition(cxt, n, pa);
693 free(str);
694 fdisk_unref_partition(pa);
695 }
f92e1810
KZ
696 break;
697 case 'n':
6936c081
KZ
698 pa = fdisk_new_partition(); /* new template */
699 if (!pa)
700 rc = -ENOMEM;
701 else {
702 char *str = NULL;
703 rc = fdisk_ask_string(cxt, _("New name"), &str);
704 if (!rc)
705 rc = fdisk_partition_set_name(pa, str);
706 if (!rc)
707 rc = fdisk_set_partition(cxt, n, pa);
708 free(str);
709 fdisk_unref_partition(pa);
710 }
f92e1810 711 break;
f362d863 712 case 'A':
a1ef792f 713 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
f362d863
KZ
714 break;
715 case 'B':
a1ef792f 716 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
f362d863
KZ
717 break;
718 case 'R':
a1ef792f 719 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
f362d863
KZ
720 break;
721 case 'S':
a1ef792f 722 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
f362d863 723 break;
f92e1810 724 }
b9e94cd7 725 }
6936c081 726
b9e94cd7
KZ
727 return rc;
728}
729
f02fecd1
KZ
730
731/*
732 * This is fdisk frontend for MBR specific libfdisk functions that
733 * are not expported by generic libfdisk API.
734 */
a47fec81 735static int dos_menu_cb(struct fdisk_context **cxt0,
f02fecd1
KZ
736 const struct menu *menu __attribute__((__unused__)),
737 const struct menu_entry *ent)
738{
a47fec81 739 struct fdisk_context *cxt = *cxt0;
f02fecd1
KZ
740 int rc = 0;
741
e6d0c4c1 742 DBG(MENU, ul_debug("enter DOS menu"));
a47fec81 743
f02fecd1
KZ
744 if (!ent->expert) {
745 switch (ent->key) {
746 case 'a':
747 {
748 size_t n;
749 rc = fdisk_ask_partnum(cxt, &n, FALSE);
750 if (!rc)
a1ef792f 751 rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
f02fecd1
KZ
752 break;
753 }
754 case 'b':
755 {
756 struct fdisk_context *bsd
757 = fdisk_new_nested_context(cxt, "bsd");
818d7924
KZ
758 if (!bsd)
759 return -ENOMEM;
aa36c2cf 760 if (!fdisk_has_label(bsd))
818d7924 761 rc = fdisk_create_disklabel(bsd, "bsd");
a47fec81 762 if (rc)
c7119037 763 fdisk_unref_context(bsd);
27ddd4f1 764 else {
a47fec81 765 *cxt0 = cxt = bsd;
0477369a 766 fdisk_info(cxt, _("Entering nested BSD disklabel."));
27ddd4f1 767 }
f02fecd1
KZ
768 break;
769 }
770 case 'c':
771 toggle_dos_compatibility_flag(cxt);
772 break;
773 }
774 return rc;
775 }
776
777 /* expert mode */
778 switch (ent->key) {
779 case 'b':
780 {
781 size_t n;
782 rc = fdisk_ask_partnum(cxt, &n, FALSE);
783 if (!rc)
f8ad3899 784 rc = fdisk_dos_move_begin(cxt, n);
f02fecd1
KZ
785 break;
786 }
f02fecd1
KZ
787 case 'i':
788 rc = fdisk_set_disklabel_id(cxt);
789 break;
f92e1810
KZ
790 case 'M':
791 /* return from nested MBR to GPT */
8eccde20
KZ
792 if (fdisk_get_parent(cxt)) {
793 *cxt0 = fdisk_get_parent(cxt);
f92e1810
KZ
794
795 fdisk_info(cxt, _("Leaving nested disklabel."));
c7119037 796 fdisk_unref_context(cxt);
f92e1810
KZ
797 cxt = *cxt0;
798 }
799 break;
f02fecd1
KZ
800 }
801 return rc;
802}
803
a47fec81 804static int sun_menu_cb(struct fdisk_context **cxt0,
9f280903
KZ
805 const struct menu *menu __attribute__((__unused__)),
806 const struct menu_entry *ent)
807{
a47fec81 808 struct fdisk_context *cxt = *cxt0;
9f280903
KZ
809 int rc = 0;
810
e6d0c4c1 811 DBG(MENU, ul_debug("enter SUN menu"));
a47fec81 812
9f280903
KZ
813 assert(cxt);
814 assert(ent);
aa36c2cf 815 assert(fdisk_is_label(cxt, SUN));
9f280903 816
e6d0c4c1 817 DBG(MENU, ul_debug("enter SUN menu"));
9f280903
KZ
818
819 /* normal mode */
820 if (!ent->expert) {
821 size_t n;
822
823 rc = fdisk_ask_partnum(cxt, &n, FALSE);
824 if (rc)
825 return rc;
826 switch (ent->key) {
827 case 'a':
a1ef792f 828 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
9f280903
KZ
829 break;
830 case 'c':
a1ef792f 831 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
9f280903
KZ
832 break;
833 }
834 return rc;
835 }
836
837 /* expert mode */
838 switch (ent->key) {
839 case 'a':
840 rc = fdisk_sun_set_alt_cyl(cxt);
841 break;
842 case 'e':
843 rc = fdisk_sun_set_xcyl(cxt);
844 break;
845 case 'i':
846 rc = fdisk_sun_set_ilfact(cxt);
847 break;
848 case 'o':
849 rc = fdisk_sun_set_rspeed(cxt);
850 break;
851 case 'y':
852 rc = fdisk_sun_set_pcylcount(cxt);
853 break;
854 }
855 return rc;
856}
857
aae727f2
KZ
858static int sgi_menu_cb(struct fdisk_context **cxt0,
859 const struct menu *menu __attribute__((__unused__)),
860 const struct menu_entry *ent)
861{
862 struct fdisk_context *cxt = *cxt0;
863 int rc = -EINVAL;
864 size_t n = 0;
865
e6d0c4c1 866 DBG(MENU, ul_debug("enter SGI menu"));
aae727f2
KZ
867
868 assert(cxt);
869 assert(ent);
aa36c2cf 870 assert(fdisk_is_label(cxt, SGI));
aae727f2
KZ
871
872 if (ent->expert)
873 return rc;
874
875 switch (ent->key) {
876 case 'a':
877 rc = fdisk_ask_partnum(cxt, &n, FALSE);
878 if (!rc)
a1ef792f 879 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
aae727f2
KZ
880 break;
881 case 'b':
ac84272d 882 fdisk_sgi_set_bootfile(cxt);
aae727f2
KZ
883 break;
884 case 'c':
885 rc = fdisk_ask_partnum(cxt, &n, FALSE);
886 if (!rc)
a1ef792f 887 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
aae727f2
KZ
888 break;
889 case 'i':
ac84272d 890 rc = fdisk_sgi_create_info(cxt);
aae727f2
KZ
891 break;
892 }
893
894 return rc;
895}
896
b529ea2a
KZ
897/*
898 * This is fdisk frontend for BSD specific libfdisk functions that
899 * are not expported by generic libfdisk API.
900 */
901static int bsd_menu_cb(struct fdisk_context **cxt0,
902 const struct menu *menu __attribute__((__unused__)),
903 const struct menu_entry *ent)
904{
905 struct fdisk_context *cxt = *cxt0;
e563f055 906 int rc = 0, org;
b529ea2a
KZ
907
908 assert(cxt);
909 assert(ent);
aa36c2cf 910 assert(fdisk_is_label(cxt, BSD));
b529ea2a 911
e6d0c4c1 912 DBG(MENU, ul_debug("enter BSD menu"));
b529ea2a
KZ
913
914 switch(ent->key) {
915 case 'e':
916 rc = fdisk_bsd_edit_disklabel(cxt);
917 break;
918 case 'i':
919 rc = fdisk_bsd_write_bootstrap(cxt);
920 break;
921 case 's':
6a632136 922 org = fdisk_is_details(cxt);
e563f055 923
6a632136 924 fdisk_enable_details(cxt, 1);
9f670072 925 list_disklabel(cxt);
6a632136 926 fdisk_enable_details(cxt, org);
b529ea2a
KZ
927 break;
928 case 'x':
929 rc = fdisk_bsd_link_partition(cxt);
930 break;
931 }
932 return rc;
933}
934
8e40a677 935/* C/H/S commands */
a47fec81 936static int geo_menu_cb(struct fdisk_context **cxt0,
8e40a677
KZ
937 const struct menu *menu __attribute__((__unused__)),
938 const struct menu_entry *ent)
939{
a47fec81 940 struct fdisk_context *cxt = *cxt0;
8e40a677
KZ
941 int rc = -EINVAL;
942 uintmax_t c = 0, h = 0, s = 0;
943
e6d0c4c1 944 DBG(MENU, ul_debug("enter GEO menu"));
a47fec81 945
8e40a677
KZ
946 assert(cxt);
947 assert(ent);
948
949 switch (ent->key) {
950 case 'c':
8eccde20 951 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_cylinders(cxt),
8e40a677
KZ
952 1048576, _("Number of cylinders"), &c);
953 break;
954 case 'h':
8eccde20 955 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_heads(cxt),
8e40a677
KZ
956 256, _("Number of heads"), &h);
957 break;
958 case 's':
8eccde20 959 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_sectors(cxt),
8e40a677
KZ
960 63, _("Number of sectors"), &s);
961 break;
962 }
963
964 if (!rc)
965 fdisk_override_geometry(cxt, c, h, s);
966 return rc;
967}
968
b3ac22ef
KZ
969static int createlabel_menu_cb(struct fdisk_context **cxt0,
970 const struct menu *menu __attribute__((__unused__)),
971 const struct menu_entry *ent)
972{
973 struct fdisk_context *cxt = *cxt0;
974 int rc = -EINVAL;
975
e6d0c4c1 976 DBG(MENU, ul_debug("enter Create label menu"));
b3ac22ef
KZ
977
978 assert(cxt);
979 assert(ent);
980
aae727f2
KZ
981 if (ent->expert) {
982 switch (ent->key) {
983 case 'g':
984 /* Deprecated, use 'G' in main menu, just for backward
985 * compatibility only. */
986 rc = fdisk_create_disklabel(cxt, "sgi");
987 break;
988 }
989 return rc;
990 }
991
b3ac22ef
KZ
992 switch (ent->key) {
993 case 'g':
994 fdisk_create_disklabel(cxt, "gpt");
995 break;
996 case 'G':
997 fdisk_create_disklabel(cxt, "sgi");
998 break;
999 case 'o':
1000 fdisk_create_disklabel(cxt, "dos");
1001 break;
1002 case 's':
1003 fdisk_create_disklabel(cxt, "sun");
1004 break;
1005 }
1006 return rc;
1007}