]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/cfdisk.c
include/c.h: add errtryhelp()
[thirdparty/util-linux.git] / disk-utils / cfdisk.c
CommitLineData
2ddfd9e4
BS
1/*
2 * cfdisk.c - Display or manipulate a disk partition table.
3 *
dda7fe12 4 * Copyright (C) 2014-2015 Karel Zak <kzak@redhat.com>
2ddfd9e4
BS
5 * Copyright (C) 1994 Kevin E. Martin (martin@cs.unc.edu)
6 *
7 * The original cfdisk was inspired by the fdisk program
8 * by A. V. Le Blanc (leblanc@mcc.ac.uk.
9 *
10 * cfdisk is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
6dbe3af9
KZ
15#include <stdlib.h>
16#include <stdio.h>
6dbe3af9 17#include <errno.h>
8c3a5a44 18#include <signal.h>
00b4f26a 19#include <ctype.h>
04915c3f 20#include <getopt.h>
57c83d63 21#include <assert.h>
baa3b270 22#include <libsmartcols.h>
7556c944 23#include <sys/ioctl.h>
57c83d63 24#include <libfdisk.h>
541e6934 25
dda7fe12
OO
26#ifdef HAVE_LIBMOUNT
27# include <libmount.h> /* keep it optional for non-linux systems */
28#endif
29
541e6934 30#ifdef HAVE_SLANG_H
3b411726 31# include <slang.h>
541e6934 32#elif defined(HAVE_SLANG_SLANG_H)
3b411726 33# include <slang/slang.h>
541e6934
KZ
34#endif
35
48d7b13a 36#ifdef HAVE_SLCURSES_H
3b411726 37# include <slcurses.h>
48d7b13a 38#elif defined(HAVE_SLANG_SLCURSES_H)
3b411726 39# include <slang/slcurses.h>
30c97bb8 40#elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
3b411726 41# include <ncursesw/ncurses.h>
48d7b13a 42#elif defined(HAVE_NCURSES_H)
3b411726 43# include <ncurses.h>
48d7b13a 44#elif defined(HAVE_NCURSES_NCURSES_H)
3b411726 45# include <ncurses/ncurses.h>
2b6fc908 46#endif
541e6934 47
5f94ca33 48#ifdef HAVE_WIDECHAR
3b411726 49# include <wctype.h>
f1512be8 50# include <wchar.h>
5f94ca33
KZ
51#endif
52
8c3a5a44 53#include "c.h"
b2d28533 54#include "closestream.h"
7eda085c 55#include "nls.h"
8abcf290 56#include "strutils.h"
8c3a5a44 57#include "xalloc.h"
5f94ca33 58#include "mbsalign.h"
04915c3f 59#include "colors.h"
57c83d63 60#include "debug.h"
dda7fe12 61#include "list.h"
8c3a5a44 62
6e51ab0c 63static const char *default_disks[] = {
a4f0a42d 64#ifdef __GNU__
6e51ab0c
KZ
65 "/dev/hd0",
66 "/dev/sd0",
a4f0a42d 67#elif defined(__FreeBSD__)
6e51ab0c
KZ
68 "/dev/ad0",
69 "/dev/da0",
a4f0a42d 70#else
6e51ab0c
KZ
71 "/dev/sda",
72 "/dev/vda",
73 "/dev/hda",
a4f0a42d 74#endif
6e51ab0c 75};
a4f0a42d 76
c743bf74 77#define ARROW_CURSOR_STRING ">> "
8c3a5a44
KZ
78#define ARROW_CURSOR_DUMMY " "
79#define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
80
5c04b408
KZ
81/* vertical menu */
82#define MENU_V_SPADDING 1 /* space around menu item string */
83
84/* horizontal menu */
85#define MENU_H_SPADDING 0 /* space around menu item string */
86#define MENU_H_BETWEEN 2 /* space between menu items */
87#define MENU_H_PRESTR "["
88#define MENU_H_POSTSTR "]"
89
90#define MENU_TITLE_PADDING 3
91
92#define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
93#define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
94
8c3a5a44 95#define TABLE_START_LINE 4
6e07b904 96#define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
eef63970 97#define INFO_LINE (ui_lines - 2)
702d0e1c 98#define WARN_LINE INFO_LINE
eef63970 99#define HINT_LINE (ui_lines - 1)
b1f58330
KZ
100
101#define CFDISK_ERR_ESC 5000
102
103#ifndef KEY_ESC
104# define KEY_ESC '\033'
105#endif
106#ifndef KEY_DELETE
107# define KEY_DELETE '\177'
108#endif
1af8003b
KZ
109
110/* colors */
111enum {
112 CFDISK_CL_NONE = 0,
45333e9d
KZ
113 CFDISK_CL_WARNING,
114 CFDISK_CL_FREESPACE,
f9fb8290 115 CFDISK_CL_INFO
1af8003b
KZ
116};
117static const int color_pairs[][2] = {
118 /* color foreground, background */
45333e9d 119 [CFDISK_CL_WARNING] = { COLOR_RED, -1 },
f9fb8290
KZ
120 [CFDISK_CL_FREESPACE] = { COLOR_GREEN, -1 },
121 [CFDISK_CL_INFO] = { COLOR_BLUE, -1 }
1af8003b 122};
8c3a5a44 123
8460875d 124struct cfdisk;
8460875d 125
3b411726
KZ
126static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx);
127static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf, int key, size_t *idx);
128static struct cfdisk_menu *menu_push(struct cfdisk *cf, struct cfdisk_menuitem *item);
b1f58330 129static struct cfdisk_menu *menu_pop(struct cfdisk *cf);
7556c944 130static void menu_refresh_size(struct cfdisk *cf);
83fa0f80 131
1af8003b 132static int ui_refresh(struct cfdisk *cf);
83fa0f80
KZ
133static void ui_warnx(const char *fmt, ...);
134static void ui_warn(const char *fmt, ...);
135static void ui_info(const char *fmt, ...);
b1f58330 136static void ui_draw_menu(struct cfdisk *cf);
ac27ea5c 137static int ui_menu_move(struct cfdisk *cf, int key);
7556c944 138static void ui_menu_resize(struct cfdisk *cf);
ac27ea5c 139
b1f58330 140static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
05af8bd4 141 uintmax_t low, uintmax_t up, int *expsize);
8460875d 142
83fa0f80 143static int ui_enabled;
7556c944 144static int ui_resize;
8c3a5a44 145
eef63970
KZ
146/* ncurses LINES and COLS may be actual variables or *macros*, but we need
147 * something portable and writable */
148size_t ui_lines;
149size_t ui_cols;
150
88942179 151/* menu item */
3b411726 152struct cfdisk_menuitem {
8c3a5a44
KZ
153 int key; /* keyboard shortcut */
154 const char *name; /* item name */
88942179 155 const char *desc; /* item description (hint) */
6fed9601 156 void *userdata;
749af4b6
KZ
157};
158
88942179 159/* menu */
8c3a5a44 160struct cfdisk_menu {
88942179
KZ
161 char *title; /* optional menu title */
162 struct cfdisk_menuitem *items; /* array with menu items */
163 char *ignore;/* string with keys to ignore */
164 size_t width; /* maximal width of the menu item */
165 size_t nitems; /* number of the active menu items */
166 size_t page_sz;/* when menu longer than screen */
167 size_t idx; /* the current menu item */
b5ef65a9 168 int prefkey;/* preferred menu item */
8c3a5a44 169 struct cfdisk_menu *prev;
88942179
KZ
170
171 /* @ignore keys generator */
63128bbf 172 int (*ignore_cb) (struct cfdisk *, char *, size_t);
8a726114 173
88942179 174 unsigned int vertical : 1; /* enable vertical mode */
749af4b6
KZ
175};
176
88942179 177/* main menu */
3b411726 178static struct cfdisk_menuitem main_menuitems[] = {
8c3a5a44
KZ
179 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
180 { 'd', N_("Delete"), N_("Delete the current partition") },
8c3a5a44 181 { 'n', N_("New"), N_("Create new partition from free space") },
3d6db3fd 182 { 'q', N_("Quit"), N_("Quit program without writing changes") },
8c3a5a44 183 { 't', N_("Type"), N_("Change the partition type") },
314a0dd8 184 { 'h', N_("Help"), N_("Print help screen") },
d5314bf5 185 { 's', N_("Sort"), N_("Fix partitions order") },
8c3a5a44 186 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
a89eafed 187 { 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
8c3a5a44
KZ
188 { 0, NULL, NULL }
189};
6dbe3af9 190
dda7fe12
OO
191/* extra partinfo in name:value pairs */
192struct cfdisk_extra {
193 char *name;
194 char *data;
195
196 struct list_head exs;
197};
198
199/* line and extra partinfo list_head */
200struct cfdisk_line {
201 char *data; /* line data */
202 struct libscols_table *extra; /* extra info ('X') */
203 WINDOW *w; /* window with extra info */
204};
205
88942179 206/* top level control struct */
8c3a5a44
KZ
207struct cfdisk {
208 struct fdisk_context *cxt; /* libfdisk context */
209 struct fdisk_table *table; /* partition table */
dda7fe12 210
a6f69126 211 struct cfdisk_menu *menu; /* the current menu */
7eda085c 212
bd85d11f
KZ
213 int *fields; /* output columns IDs */
214 size_t nfields; /* number of columns IDs */
7eda085c 215
8c3a5a44
KZ
216 char *linesbuf; /* table as string */
217 size_t linesbufsz; /* size of the tb_buf */
7eda085c 218
dda7fe12
OO
219 struct cfdisk_line *lines; /* list of lines */
220
8c3a5a44 221 size_t nlines; /* number of lines */
a6f69126
KZ
222 size_t lines_idx; /* current line <0..N>, exclude header */
223 size_t page_sz;
d5314bf5 224
bc787727
KZ
225 unsigned int nwrites; /* fdisk_write_disklabel() counter */
226
dda7fe12
OO
227 WINDOW *act_win; /* the window currently on the screen */
228
229#ifdef HAVE_LIBMOUNT
230 struct libmnt_table *mtab;
231 struct libmnt_table *fstab;
232 struct libmnt_cache *mntcache;
233#endif
d121efdb 234 unsigned int wrong_order :1, /* PT not in right order */
dda7fe12
OO
235 zero_start :1, /* ignore existing partition table */
236 show_extra :1; /* show extra partinfo */
8c3a5a44 237};
5c36a0eb 238
14620822
KZ
239
240/*
241 * let's use include/debug.h stuff for cfdisk too
242 */
243UL_DEBUG_DEFINE_MASK(cfdisk);
819d9a29 244UL_DEBUG_DEFINE_MASKNAMES(cfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
14620822
KZ
245
246#define CFDISK_DEBUG_INIT (1 << 1)
247#define CFDISK_DEBUG_UI (1 << 2)
248#define CFDISK_DEBUG_MENU (1 << 3)
249#define CFDISK_DEBUG_MISC (1 << 4)
250#define CFDISK_DEBUG_TABLE (1 << 5)
251#define CFDISK_DEBUG_ALL 0xFFFF
252
14620822
KZ
253#define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
254
255static void cfdisk_init_debug(void)
256{
257 __UL_INIT_DEBUG(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
258}
259
bd85d11f 260/* Initialize output columns -- we follow libfdisk fields (usually specific
88942179
KZ
261 * to the label type.
262 */
8c3a5a44
KZ
263static int cols_init(struct cfdisk *cf)
264{
265 assert(cf);
5c36a0eb 266
bd85d11f
KZ
267 free(cf->fields);
268 cf->fields = NULL;
269 cf->nfields = 0;
5c36a0eb 270
11ee1177 271 return fdisk_label_get_fields_ids(NULL, cf->cxt, &cf->fields, &cf->nfields);
5c36a0eb
KZ
272}
273
7556c944
KZ
274static void resize(void)
275{
276 struct winsize ws;
277
278 if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1
279 && ws.ws_row && ws.ws_col) {
eef63970
KZ
280 ui_lines = ws.ws_row;
281 ui_cols = ws.ws_col;
7556c944
KZ
282#if HAVE_RESIZETERM
283 resizeterm(ws.ws_row, ws.ws_col);
284#endif
285 clearok(stdscr, TRUE);
286 }
287 touchwin(stdscr);
288
eef63970
KZ
289 DBG(UI, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
290 ui_cols, ui_lines));
7556c944
KZ
291 ui_resize = 0;
292}
293
c743bf74
KZ
294/* Reads partition in tree-like order from scols
295 */
296static int partition_from_scols(struct fdisk_table *tb,
297 struct libscols_line *ln)
298{
299 struct fdisk_partition *pa = scols_line_get_userdata(ln);
300
301 fdisk_table_add_partition(tb, pa);
302 fdisk_unref_partition(pa);
303
304 if (scols_line_has_children(ln)) {
305 struct libscols_line *chln;
306 struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
307
308 if (!itr)
309 return -EINVAL;
310 while (scols_line_next_child(ln, itr, &chln) == 0)
311 partition_from_scols(tb, chln);
a9408274 312 scols_free_iter(itr);
c743bf74
KZ
313 }
314 return 0;
315}
316
8c3a5a44
KZ
317static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb)
318{
8903f7df 319 struct fdisk_partition *pa;
8c3a5a44
KZ
320 struct fdisk_label *lb;
321 struct fdisk_iter *itr = NULL;
baa3b270
OO
322 struct libscols_table *table = NULL;
323 struct libscols_iter *s_itr = NULL;
8c3a5a44 324 char *res = NULL;
8903f7df 325 size_t i;
45333e9d 326 int tree = 0;
baa3b270 327 struct libscols_line *ln, *ln_cont = NULL;
8c3a5a44 328
14620822 329 DBG(TABLE, ul_debug("convert to string"));
8c3a5a44
KZ
330
331 assert(cf);
332 assert(cf->cxt);
bd85d11f 333 assert(cf->fields);
8c3a5a44
KZ
334 assert(tb);
335
6a632136 336 lb = fdisk_get_label(cf->cxt, NULL);
8c3a5a44
KZ
337 assert(lb);
338
8c3a5a44
KZ
339 itr = fdisk_new_iter(FDISK_ITER_FORWARD);
340 if (!itr)
341 goto done;
5c36a0eb 342
45333e9d
KZ
343 /* get container (e.g. extended partition) */
344 while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
345 if (fdisk_partition_is_nested(pa)) {
14620822 346 DBG(TABLE, ul_debug("nested detected, using tree"));
baa3b270 347 tree = SCOLS_FL_TREE;
45333e9d
KZ
348 break;
349 }
350 }
45333e9d 351
0925a9dd 352 table = scols_new_table();
baa3b270 353 if (!table)
45333e9d 354 goto done;
0925a9dd 355 scols_table_enable_maxout(table, 1);
717fff86 356 scols_table_enable_nowrap(table, 1);
45333e9d 357
9907a95a
KZ
358#if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
359 scols_table_enable_ascii(table, 1);
360#endif
361
8c3a5a44 362 /* headers */
bd85d11f
KZ
363 for (i = 0; i < cf->nfields; i++) {
364 int fl = 0;
365 const struct fdisk_field *field =
366 fdisk_label_get_field(lb, cf->fields[i]);
367 if (!field)
368 continue;
369
370 if (fdisk_field_is_number(field))
371 fl |= SCOLS_FL_RIGHT;
372 if (fdisk_field_get_id(field) == FDISK_FIELD_TYPE)
373 fl |= SCOLS_FL_TRUNC;
374 if (tree && fdisk_field_get_id(field) == FDISK_FIELD_DEVICE)
375 fl |= SCOLS_FL_TREE;
376
377 if (!scols_table_new_column(table,
b6e3c614 378 _(fdisk_field_get_name(field)),
bd85d11f
KZ
379 fdisk_field_get_width(field), fl))
380 goto done;
8c3a5a44
KZ
381 }
382
383 /* data */
8903f7df
KZ
384 fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
385
8c3a5a44 386 while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
baa3b270 387 struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL;
8903f7df 388
baa3b270 389 ln = scols_table_new_line(table, parent);
8c3a5a44
KZ
390 if (!ln)
391 goto done;
bd85d11f 392 for (i = 0; i < cf->nfields; i++) {
8c3a5a44 393 char *cdata = NULL;
bd85d11f 394
d44115f3 395 if (fdisk_partition_to_string(pa, cf->cxt,
bd85d11f 396 cf->fields[i], &cdata))
8c3a5a44 397 continue;
c743bf74 398 scols_line_refer_data(ln, i, cdata);
8c3a5a44 399 }
8903f7df 400 if (tree && fdisk_partition_is_container(pa))
45333e9d 401 ln_cont = ln;
d051ea93 402
baa3b270 403 scols_line_set_userdata(ln, (void *) pa);
d051ea93 404 fdisk_ref_partition(pa);
8c3a5a44 405 }
6dbe3af9 406
baa3b270 407 if (scols_table_is_empty(table))
d051ea93
KZ
408 goto done;
409
baa3b270
OO
410 scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH);
411 scols_print_table_to_string(table, &res);
d051ea93 412
baa3b270 413 /* scols_* code might reorder lines, let's reorder @tb according to the
d051ea93
KZ
414 * final output (it's no problem because partitions are addressed by
415 * parno stored within struct fdisk_partition) */
416
417 /* remove all */
418 fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
419 while (fdisk_table_next_partition(tb, itr, &pa) == 0)
420 fdisk_table_remove_partition(tb, pa);
421
baa3b270
OO
422 s_itr = scols_new_iter(SCOLS_ITER_FORWARD);
423 if (!s_itr)
424 goto done;
425
c743bf74 426 /* add all in the right order (don't forget the output is tree) */
baa3b270 427 while (scols_table_next_line(table, s_itr, &ln) == 0) {
c743bf74
KZ
428 if (scols_line_get_parent(ln))
429 continue;
430 if (partition_from_scols(tb, ln))
431 break;
8c3a5a44
KZ
432 }
433done:
baa3b270
OO
434 scols_unref_table(table);
435 scols_free_iter(s_itr);
8c3a5a44 436 fdisk_free_iter(itr);
6dbe3af9 437
8c3a5a44 438 return res;
2b6fc908
KZ
439}
440
dda7fe12
OO
441static void cfdisk_free_lines(struct cfdisk *cf)
442{
443 size_t i = 0;
444 while(i < cf->nlines) {
445 scols_unref_table(cf->lines[i].extra);
446
447 DBG(UI, ul_debug("delete window: %p",
448 cf->lines[i].w));
449
a2d0dbb4
KZ
450 if (cf->lines[i].w)
451 delwin(cf->lines[i].w);
452 cf->lines[i].w = NULL;
dda7fe12
OO
453 ++i;
454 }
455 cf->act_win = NULL;
456 free(cf->lines);
a2d0dbb4 457 cf->lines = NULL;
dda7fe12 458}
88942179
KZ
459/*
460 * Read data about partitions from libfdisk and prepare output lines.
461 */
8460875d 462static int lines_refresh(struct cfdisk *cf)
8c3a5a44
KZ
463{
464 int rc;
465 char *p;
466 size_t i;
7eda085c 467
8c3a5a44 468 assert(cf);
2b6fc908 469
14620822 470 DBG(TABLE, ul_debug("refreshing buffer"));
c64061c9 471
8c3a5a44 472 free(cf->linesbuf);
dda7fe12 473 cfdisk_free_lines(cf);
8c3a5a44
KZ
474 cf->linesbuf = NULL;
475 cf->linesbufsz = 0;
476 cf->lines = NULL;
477 cf->nlines = 0;
6dbe3af9 478
8c3a5a44 479 fdisk_unref_table(cf->table);
1af8003b 480 cf->table = NULL;
6dbe3af9 481
2cec7949
KZ
482 /* read partitions and free spaces into cf->table */
483 rc = fdisk_get_partitions(cf->cxt, &cf->table);
484 if (!rc)
485 rc = fdisk_get_freespaces(cf->cxt, &cf->table);
8c3a5a44
KZ
486 if (rc)
487 return rc;
0d8589c5 488
8c3a5a44
KZ
489 cf->linesbuf = table_to_string(cf, cf->table);
490 if (!cf->linesbuf)
491 return -ENOMEM;
6dbe3af9 492
8c3a5a44
KZ
493 cf->linesbufsz = strlen(cf->linesbuf);
494 cf->nlines = fdisk_table_get_nents(cf->table) + 1; /* 1 for header line */
a6f69126 495 cf->page_sz = 0;
d5314bf5 496 cf->wrong_order = fdisk_table_wrong_order(cf->table) ? 1 : 0;
a6f69126
KZ
497
498 if (MENU_START_LINE - TABLE_START_LINE < cf->nlines)
499 cf->page_sz = MENU_START_LINE - TABLE_START_LINE - 1;
6dbe3af9 500
dda7fe12 501 cf->lines = xcalloc(cf->nlines, sizeof(struct cfdisk_line));
6dbe3af9 502
8c3a5a44 503 for (p = cf->linesbuf, i = 0; p && i < cf->nlines; i++) {
86688c37
KZ
504 cf->lines[i].data = p;
505 p = strchr(p, '\n');
8c3a5a44
KZ
506 if (p) {
507 *p = '\0';
508 p++;
509 }
dda7fe12
OO
510 cf->lines[i].extra = scols_new_table();
511 scols_table_enable_noheadings(cf->lines[i].extra, 1);
512 scols_table_new_column(cf->lines[i].extra, NULL, 0, SCOLS_FL_RIGHT);
513 scols_table_new_column(cf->lines[i].extra, NULL, 0, SCOLS_FL_TRUNC);
8c3a5a44 514 }
6dbe3af9 515
8c3a5a44 516 return 0;
6dbe3af9
KZ
517}
518
00b4f26a
KZ
519static struct fdisk_partition *get_current_partition(struct cfdisk *cf)
520{
521 assert(cf);
522 assert(cf->table);
523
524 return fdisk_table_get_partition(cf->table, cf->lines_idx);
525}
526
45333e9d
KZ
527static int is_freespace(struct cfdisk *cf, size_t i)
528{
529 struct fdisk_partition *pa;
530
531 assert(cf);
532 assert(cf->table);
533
534 pa = fdisk_table_get_partition(cf->table, i);
535 return fdisk_partition_is_freespace(pa);
536}
537
b1f58330 538/* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
9e930041 539 * response back to libfdisk
b1f58330
KZ
540 */
541static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
542{
3b411726 543 struct cfdisk_menuitem *d, *cm;
b1f58330
KZ
544 int key;
545 size_t i = 0, nitems;
546 const char *name, *desc;
547
548 assert(ask);
549 assert(cf);
550
551 /* create cfdisk menu according to libfdisk ask-menu, note that the
552 * last cm[] item has to be empty -- so nitems + 1 */
553 nitems = fdisk_ask_menu_get_nitems(ask);
3b411726 554 cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
b1f58330
KZ
555
556 for (i = 0; i < nitems; i++) {
557 if (fdisk_ask_menu_get_item(ask, i, &key, &name, &desc))
558 break;
559 cm[i].key = key;
560 cm[i].desc = desc;
561 cm[i].name = name;
562 }
563
564 /* make the new menu active */
63128bbf 565 menu_push(cf, cm);
b1f58330
KZ
566 ui_draw_menu(cf);
567 refresh();
568
569 /* wait for keys */
570 do {
7ee26cbf 571 key = getch();
ac27ea5c 572
7556c944
KZ
573 if (ui_resize)
574 ui_menu_resize(cf);
ac27ea5c
KZ
575 if (ui_menu_move(cf, key) == 0)
576 continue;
577
578 switch (key) {
b1f58330
KZ
579 case KEY_ENTER:
580 case '\n':
581 case '\r':
6fed9601 582 d = menu_get_menuitem(cf, cf->menu->idx);
b1f58330
KZ
583 if (d)
584 fdisk_ask_menu_set_result(ask, d->key);
585 menu_pop(cf);
586 free(cm);
587 return 0;
588 }
589 } while (1);
590
591 menu_pop(cf);
592 free(cm);
593 return -1;
594}
595
88942179
KZ
596/* libfdisk callback
597 */
ffab025d
KZ
598static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
599 struct fdisk_ask *ask,
600 void *data __attribute__((__unused__)))
8c3a5a44
KZ
601{
602 int rc = 0;
6dbe3af9 603
8c3a5a44 604 assert(ask);
6dbe3af9 605
8c3a5a44
KZ
606 switch(fdisk_ask_get_type(ask)) {
607 case FDISK_ASKTYPE_INFO:
83fa0f80 608 ui_info(fdisk_ask_print_get_mesg(ask));
8c3a5a44
KZ
609 break;
610 case FDISK_ASKTYPE_WARNX:
83fa0f80 611 ui_warnx(fdisk_ask_print_get_mesg(ask));
8c3a5a44
KZ
612 break;
613 case FDISK_ASKTYPE_WARN:
83fa0f80 614 ui_warn(fdisk_ask_print_get_mesg(ask));
8c3a5a44 615 break;
b1f58330
KZ
616 case FDISK_ASKTYPE_MENU:
617 ask_menu(ask, (struct cfdisk *) data);
618 break;
8c3a5a44 619 default:
83fa0f80
KZ
620 ui_warnx(_("internal error: unsupported dialog type %d"),
621 fdisk_ask_get_type(ask));
8c3a5a44
KZ
622 return -EINVAL;
623 }
624 return rc;
fd6b7a7f 625}
6dbe3af9 626
7aa0d529 627static int ui_end(void)
8c3a5a44 628{
7aa0d529 629 if (!ui_enabled)
8c3a5a44
KZ
630 return -EINVAL;
631
48d7b13a 632#if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
eef63970 633 SLsmg_gotorc(ui_lines - 1, 0);
8c3a5a44 634 SLsmg_refresh();
2b6fc908 635#else
eef63970 636 mvcur(0, ui_cols - 1, ui_lines-1, 0);
2b6fc908 637#endif
cd8414f7 638 curs_set(1);
8c3a5a44
KZ
639 nl();
640 endwin();
641 printf("\n");
7aa0d529 642 ui_enabled = 0;
8c3a5a44 643 return 0;
6dbe3af9
KZ
644}
645
eef63970 646static void ui_vprint_center(size_t line, int attrs, const char *fmt, va_list ap)
f609e92e 647{
8c3a5a44 648 size_t width;
8c3a5a44 649 char *buf = NULL;
f609e92e 650
8c3a5a44
KZ
651 move(line, 0);
652 clrtoeol();
f609e92e 653
8c3a5a44 654 xvasprintf(&buf, fmt, ap);
fd6b7a7f 655
b1f58330 656 width = mbs_safe_width(buf);
eef63970
KZ
657 if (width > (size_t) ui_cols) {
658 char *p = strrchr(buf + ui_cols, ' ');
91ba41ca 659 if (!p)
eef63970 660 p = buf + ui_cols;
91ba41ca 661 *p = '\0';
eef63970 662 if (line + 1 >= ui_lines)
91ba41ca
KZ
663 line--;
664 attron(attrs);
665 mvaddstr(line, 0, buf);
666 mvaddstr(line + 1, 0, p+1);
667 attroff(attrs);
668 } else {
669 attron(attrs);
eef63970 670 mvaddstr(line, (ui_cols - width) / 2, buf);
91ba41ca
KZ
671 attroff(attrs);
672 }
8c3a5a44 673 free(buf);
6dbe3af9
KZ
674}
675
eef63970 676static void ui_center(size_t line, const char *fmt, ...)
1af8003b
KZ
677{
678 va_list ap;
679 va_start(ap, fmt);
680 ui_vprint_center(line, 0, fmt, ap);
681 va_end(ap);
682}
683
83fa0f80
KZ
684static void ui_warnx(const char *fmt, ...)
685{
686 va_list ap;
687 va_start(ap, fmt);
688 if (ui_enabled)
702d0e1c 689 ui_vprint_center(WARN_LINE,
f1512be8
KZ
690 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
691 fmt, ap);
8fc37981 692 else {
83fa0f80 693 vfprintf(stderr, fmt, ap);
8fc37981
KZ
694 fputc('\n', stderr);
695 }
83fa0f80
KZ
696 va_end(ap);
697}
698
699static void ui_warn(const char *fmt, ...)
1af8003b 700{
83fa0f80 701 char *fmt_m;
1af8003b 702 va_list ap;
83fa0f80
KZ
703
704 xasprintf(&fmt_m, "%s: %m", fmt);
705
1af8003b 706 va_start(ap, fmt);
83fa0f80 707 if (ui_enabled)
702d0e1c 708 ui_vprint_center(WARN_LINE,
f1512be8
KZ
709 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
710 fmt_m, ap);
8fc37981 711 else {
83fa0f80 712 vfprintf(stderr, fmt_m, ap);
8fc37981
KZ
713 fputc('\n', stderr);
714 }
1af8003b 715 va_end(ap);
83fa0f80 716 free(fmt_m);
1af8003b
KZ
717}
718
29e9b787
KZ
719static void ui_clean_warn(void)
720{
721 move(WARN_LINE, 0);
722 clrtoeol();
723}
724
7aa0d529
KZ
725static int __attribute__((__noreturn__)) ui_errx(int rc, const char *fmt, ...)
726 {
727 va_list ap;
728 ui_end();
729
730 va_start(ap, fmt);
731 fprintf(stderr, "%s: ", program_invocation_short_name);
732 vfprintf(stderr, fmt, ap);
8fc37981 733 fputc('\n', stderr);
7aa0d529
KZ
734 va_end(ap);
735
736 exit(rc);
737}
738
1af8003b
KZ
739static void ui_info(const char *fmt, ...)
740{
741 va_list ap;
742 va_start(ap, fmt);
83fa0f80 743 if (ui_enabled)
f9fb8290
KZ
744 ui_vprint_center(INFO_LINE,
745 colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO) : 0,
746 fmt, ap);
8fc37981 747 else {
83fa0f80 748 vfprintf(stdout, fmt, ap);
8fc37981
KZ
749 fputc('\n', stdout);
750 }
1af8003b
KZ
751 va_end(ap);
752}
753
754static void ui_clean_info(void)
755{
756 move(INFO_LINE, 0);
757 clrtoeol();
758}
6dbe3af9 759
b1f58330
KZ
760static void ui_hint(const char *fmt, ...)
761{
762 va_list ap;
763 va_start(ap, fmt);
764 if (ui_enabled)
765 ui_vprint_center(HINT_LINE, A_BOLD, fmt, ap);
8fc37981 766 else {
b1f58330 767 vfprintf(stdout, fmt, ap);
8fc37981
KZ
768 fputc('\n', stdout);
769 }
b1f58330
KZ
770 va_end(ap);
771}
772
773static void ui_clean_hint(void)
774{
775 move(HINT_LINE, 0);
776 clrtoeol();
777}
778
8c3a5a44
KZ
779static void die_on_signal(int dummy __attribute__((__unused__)))
780{
14620822 781 DBG(MISC, ul_debug("die on signal."));
7aa0d529 782 ui_end();
8c3a5a44 783 exit(EXIT_FAILURE);
6dbe3af9
KZ
784}
785
7556c944
KZ
786static void resize_on_signal(int dummy __attribute__((__unused__)))
787{
14620822 788 DBG(MISC, ul_debug("resize on signal."));
7556c944
KZ
789 ui_resize = 1;
790}
791
792static void menu_refresh_size(struct cfdisk *cf)
793{
794 if (cf->menu && cf->menu->nitems)
eef63970 795 cf->menu->page_sz = (cf->menu->nitems / (ui_lines - 4)) ? ui_lines - 4 : 0;
7556c944
KZ
796}
797
8c3a5a44
KZ
798static void menu_update_ignore(struct cfdisk *cf)
799{
00b4f26a
KZ
800 char ignore[128] = { 0 };
801 int i = 0;
8c3a5a44 802 struct cfdisk_menu *m;
b5ef65a9 803 struct cfdisk_menuitem *d, *org = NULL;
83fa0f80 804 size_t idx;
6dbe3af9 805
8c3a5a44 806 assert(cf);
63128bbf
KZ
807 assert(cf->menu);
808 assert(cf->menu->ignore_cb);
6dbe3af9 809
8c3a5a44 810 m = cf->menu;
14620822 811 DBG(MENU, ul_debug("update menu ignored keys"));
6dbe3af9 812
63128bbf 813 i = m->ignore_cb(cf, ignore, sizeof(ignore));
00b4f26a
KZ
814 ignore[i] = '\0';
815
8c3a5a44 816 /* return if no change */
b5ef65a9 817 if ((!m->ignore && !*ignore)
00b4f26a 818 || (m->ignore && *ignore && strcmp(m->ignore, ignore) == 0)) {
8c3a5a44 819 return;
fd6b7a7f 820 }
6dbe3af9 821
b5ef65a9
KZ
822 if (!m->prefkey)
823 org = menu_get_menuitem(cf, m->idx);
824
8c3a5a44 825 free(m->ignore);
8460875d 826 m->ignore = xstrdup(ignore);
8c3a5a44 827 m->nitems = 0;
6dbe3af9 828
3b411726 829 for (d = m->items; d->name; d++) {
8c3a5a44 830 if (m->ignore && strchr(m->ignore, d->key))
8460875d
KZ
831 continue;
832 m->nitems++;
8c3a5a44 833 }
83fa0f80 834
b5ef65a9
KZ
835 DBG(MENU, ul_debug("update menu preferred keys"));
836
83fa0f80
KZ
837 /* refresh menu index to be at the same menuitem or go to the first */
838 if (org && menu_get_menuitem_by_key(cf, org->key, &idx))
6fed9601 839 m->idx = idx;
b5ef65a9
KZ
840 else if (m->prefkey && menu_get_menuitem_by_key(cf, m->prefkey, &idx))
841 m->idx = idx;
83fa0f80 842 else
6fed9601
KZ
843 m->idx = 0;
844
7556c944 845 menu_refresh_size(cf);
6dbe3af9
KZ
846}
847
b1f58330
KZ
848static struct cfdisk_menu *menu_push(
849 struct cfdisk *cf,
3b411726 850 struct cfdisk_menuitem *items)
8c3a5a44
KZ
851{
852 struct cfdisk_menu *m = xcalloc(1, sizeof(*m));
3b411726 853 struct cfdisk_menuitem *d;
6dbe3af9 854
8c3a5a44 855 assert(cf);
6dbe3af9 856
14620822 857 DBG(MENU, ul_debug("new menu"));
6dbe3af9 858
8c3a5a44 859 m->prev = cf->menu;
3b411726 860 m->items = items;
6dbe3af9 861
3b411726 862 for (d = m->items; d->name; d++) {
8c3a5a44 863 const char *name = _(d->name);
b1f58330 864 size_t len = mbs_safe_width(name);
8c3a5a44
KZ
865 if (len > m->width)
866 m->width = len;
867 m->nitems++;
868 }
6dbe3af9 869
8c3a5a44 870 cf->menu = m;
b5ef65a9 871
7556c944 872 menu_refresh_size(cf);
8c3a5a44 873 return m;
6dbe3af9
KZ
874}
875
8c3a5a44 876static struct cfdisk_menu *menu_pop(struct cfdisk *cf)
6dbe3af9 877{
8c3a5a44 878 struct cfdisk_menu *m = NULL;
6dbe3af9 879
8c3a5a44 880 assert(cf);
7eda085c 881
14620822 882 DBG(MENU, ul_debug("pop menu"));
7eda085c 883
8c3a5a44
KZ
884 if (cf->menu) {
885 m = cf->menu->prev;
886 free(cf->menu->ignore);
8a726114 887 free(cf->menu->title);
8c3a5a44 888 free(cf->menu);
c07ebfa1 889 }
8c3a5a44
KZ
890 cf->menu = m;
891 return cf->menu;
6dbe3af9
KZ
892}
893
8a726114
KZ
894static void menu_set_title(struct cfdisk_menu *m, const char *title)
895{
896 char *str = NULL;
897
898 if (title) {
899 size_t len = mbs_safe_width(title);
5c04b408
KZ
900 if (len + MENU_TITLE_PADDING > m->width)
901 m->width = len + MENU_TITLE_PADDING;
8a726114
KZ
902 str = xstrdup(title);
903 }
904 m->title = str;
905}
906
907
b1f58330 908static int ui_init(struct cfdisk *cf __attribute__((__unused__)))
8c3a5a44
KZ
909{
910 struct sigaction sa;
6dbe3af9 911
14620822 912 DBG(UI, ul_debug("init"));
6dbe3af9 913
8c3a5a44
KZ
914 /* setup SIGCHLD handler */
915 sigemptyset(&sa.sa_mask);
916 sa.sa_flags = 0;
917 sa.sa_handler = die_on_signal;
918 sigaction(SIGINT, &sa, NULL);
919 sigaction(SIGTERM, &sa, NULL);
6dbe3af9 920
7556c944
KZ
921 sa.sa_handler = resize_on_signal;
922 sigaction(SIGWINCH, &sa, NULL);
923
83fa0f80 924 ui_enabled = 1;
8c3a5a44 925 initscr();
6dbe3af9 926
f1512be8 927#ifdef HAVE_USE_DEFAULT_COLORS
04915c3f 928 if (colors_wanted() && has_colors()) {
1af8003b
KZ
929 size_t i;
930
931 start_color();
932 use_default_colors();
1af8003b
KZ
933 for (i = 1; i < ARRAY_SIZE(color_pairs); i++) /* yeah, start from 1! */
934 init_pair(i, color_pairs[i][0], color_pairs[i][1]);
935 }
f1512be8 936#else
e66a6627 937 colors_off();
f1512be8 938#endif
1af8003b 939
8c3a5a44
KZ
940 cbreak();
941 noecho();
942 nonl();
943 curs_set(0);
944 keypad(stdscr, TRUE);
6dbe3af9 945
8c3a5a44 946 return 0;
7eda085c
KZ
947}
948
5c04b408
KZ
949/* "[ string ]" */
950#define MENU_H_ITEMWIDTH(m) ( MENU_H_PRESTR_SZ \
951 + MENU_H_SPADDING \
952 + (m)->width \
953 + MENU_H_SPADDING \
954 + MENU_H_POSTSTR_SZ)
955
956#define MENU_V_ITEMWIDTH(m) (MENU_V_SPADDING + (m)->width + MENU_V_SPADDING)
957
958
8c3a5a44
KZ
959static size_t menuitem_get_line(struct cfdisk *cf, size_t idx)
960{
6fed9601
KZ
961 struct cfdisk_menu *m = cf->menu;
962
963 if (m->vertical) {
964 if (!m->page_sz) /* small menu */
eef63970 965 return (ui_lines - (cf->menu->nitems + 1)) / 2 + idx;
6fed9601 966 return (idx % m->page_sz) + 1;
8a726114 967 } else {
5c04b408 968 size_t len = MENU_H_ITEMWIDTH(m) + MENU_H_BETWEEN; /** item width */
eef63970 969 size_t items = ui_cols / len; /* items per line */
8a726114 970
a1da27a8
KZ
971 if (items == 0)
972 return 0;
8a726114
KZ
973 return MENU_START_LINE + ((idx / items));
974 }
7eda085c
KZ
975}
976
8c3a5a44
KZ
977static int menuitem_get_column(struct cfdisk *cf, size_t idx)
978{
8a726114 979 if (cf->menu->vertical) {
5c04b408 980 size_t nc = MENU_V_ITEMWIDTH(cf->menu);
eef63970 981 if ((size_t) ui_cols <= nc)
7aa0d529 982 return 0;
eef63970 983 return (ui_cols - nc) / 2;
8a726114 984 } else {
5c04b408 985 size_t len = MENU_H_ITEMWIDTH(cf->menu) + MENU_H_BETWEEN; /* item width */
eef63970 986 size_t items = ui_cols / len; /* items per line */
8a726114 987 size_t extra = items < cf->menu->nitems ? /* extra space on line */
eef63970
KZ
988 ui_cols % len : /* - multi-line menu */
989 ui_cols - (cf->menu->nitems * len); /* - one line menu */
8a726114 990
a1da27a8
KZ
991 if (items == 0)
992 return 0; /* hmm... no space */
993
5c04b408 994 extra += MENU_H_BETWEEN; /* add padding after last item to extra */
e66ac5d3 995
8a726114
KZ
996 if (idx < items)
997 return (idx * len) + (extra / 2);
998 return ((idx % items) * len) + (extra / 2);
999 }
df1dddf9
KZ
1000}
1001
6fed9601
KZ
1002static int menuitem_on_page(struct cfdisk *cf, size_t idx)
1003{
1004 struct cfdisk_menu *m = cf->menu;
1005
1006 if (m->page_sz == 0 ||
1007 m->idx / m->page_sz == idx / m->page_sz)
1008 return 1;
1009 return 0;
1010}
1011
3b411726 1012static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx)
8c3a5a44 1013{
3b411726 1014 struct cfdisk_menuitem *d;
8c3a5a44 1015 size_t i;
7eda085c 1016
3b411726 1017 for (i = 0, d = cf->menu->items; d->name; d++) {
8c3a5a44
KZ
1018 if (cf->menu->ignore && strchr(cf->menu->ignore, d->key))
1019 continue;
1020 if (i++ == idx)
1021 return d;
d26aa358 1022 }
7eda085c 1023
8c3a5a44 1024 return NULL;
6dbe3af9
KZ
1025}
1026
3b411726 1027static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf,
83fa0f80
KZ
1028 int key, size_t *idx)
1029{
3b411726 1030 struct cfdisk_menuitem *d;
83fa0f80 1031
3b411726 1032 for (*idx = 0, d = cf->menu->items; d->name; d++) {
83fa0f80
KZ
1033 if (cf->menu->ignore && strchr(cf->menu->ignore, d->key))
1034 continue;
1035 if (key == d->key)
1036 return d;
1037 (*idx)++;
1038 }
1039
1040 return NULL;
1041}
1042
8c3a5a44 1043static void ui_draw_menuitem(struct cfdisk *cf,
3b411726 1044 struct cfdisk_menuitem *d,
8c3a5a44
KZ
1045 size_t idx)
1046{
5c04b408 1047 char buf[80 * MB_CUR_MAX], *ptr = buf;
8c3a5a44 1048 const char *name;
5c04b408 1049 size_t width;
8a726114 1050 int ln, cl, vert = cf->menu->vertical;
8c3a5a44 1051
6fed9601
KZ
1052 if (!menuitem_on_page(cf, idx))
1053 return; /* no visible item */
1054 ln = menuitem_get_line(cf, idx);
1055 cl = menuitem_get_column(cf, idx);
1056
5c04b408
KZ
1057 /* string width */
1058 if (vert) {
1059 width = cf->menu->width + MENU_V_SPADDING;
1060 memset(ptr, ' ', MENU_V_SPADDING);
1061 ptr += MENU_V_SPADDING;
1062 } else
1063 width = MENU_H_SPADDING + cf->menu->width + MENU_H_SPADDING;
1064
8c3a5a44 1065 name = _(d->name);
5c04b408 1066 mbsalign(name, ptr, sizeof(buf), &width,
8a726114
KZ
1067 vert ? MBS_ALIGN_LEFT : MBS_ALIGN_CENTER,
1068 0);
8c3a5a44 1069
14620822 1070 DBG(MENU, ul_debug("menuitem: cl=%d, ln=%d, item='%s'",
8c3a5a44
KZ
1071 cl, ln, buf));
1072
8a726114
KZ
1073 if (vert) {
1074 mvaddch(ln, cl - 1, ACS_VLINE);
5c04b408 1075 mvaddch(ln, cl + MENU_V_ITEMWIDTH(cf->menu), ACS_VLINE);
8a726114
KZ
1076 }
1077
5c04b408 1078 if (cf->menu->idx == idx)
8c3a5a44 1079 standout();
5c04b408
KZ
1080
1081 if (vert)
1082 mvprintw(ln, cl, "%s", buf);
1083 else
1084 mvprintw(ln, cl, "%s%s%s", MENU_H_PRESTR, buf, MENU_H_POSTSTR);
1085
1086 if (cf->menu->idx == idx) {
8c3a5a44
KZ
1087 standend();
1088 if (d->desc)
b6e3c614 1089 ui_hint(_(d->desc));
5c04b408 1090 }
6dbe3af9
KZ
1091}
1092
7556c944
KZ
1093static void ui_clean_menu(struct cfdisk *cf)
1094{
1095 size_t i;
702d0e1c 1096 size_t lastline;
7556c944
KZ
1097 struct cfdisk_menu *m = cf->menu;
1098 size_t ln = menuitem_get_line(cf, 0);
1099
1100 if (m->vertical)
702d0e1c 1101 lastline = ln + (m->page_sz ? m->page_sz : m->nitems);
7556c944 1102 else
702d0e1c 1103 lastline = menuitem_get_line(cf, m->nitems);
7556c944 1104
702d0e1c 1105 for (i = ln; i <= lastline; i++) {
7556c944
KZ
1106 move(i, 0);
1107 clrtoeol();
702d0e1c 1108 DBG(MENU, ul_debug("clean_menu: line %zu", i));
7556c944
KZ
1109 }
1110 if (m->vertical) {
1111 move(ln - 1, 0);
1112 clrtoeol();
1113 }
1114 ui_clean_hint();
1115}
1116
8c3a5a44
KZ
1117static void ui_draw_menu(struct cfdisk *cf)
1118{
3b411726 1119 struct cfdisk_menuitem *d;
8a726114 1120 struct cfdisk_menu *m;
6fed9601
KZ
1121 size_t i = 0;
1122 size_t ln = menuitem_get_line(cf, 0);
1123 size_t nlines;
7eda085c 1124
8c3a5a44
KZ
1125 assert(cf);
1126 assert(cf->menu);
fd6b7a7f 1127
14620822 1128 DBG(MENU, ul_debug("draw start"));
6dbe3af9 1129
7556c944 1130 ui_clean_menu(cf);
8a726114
KZ
1131 m = cf->menu;
1132
6fed9601
KZ
1133 if (m->vertical)
1134 nlines = m->page_sz ? m->page_sz : m->nitems;
1135 else
1136 nlines = menuitem_get_line(cf, m->nitems);
1137
63128bbf
KZ
1138 if (m->ignore_cb)
1139 menu_update_ignore(cf);
00b4f26a 1140 i = 0;
8c3a5a44
KZ
1141 while ((d = menu_get_menuitem(cf, i)))
1142 ui_draw_menuitem(cf, d, i++);
6dbe3af9 1143
8a726114 1144 if (m->vertical) {
8a726114 1145 size_t cl = menuitem_get_column(cf, 0);
6fed9601 1146 size_t curpg = m->page_sz ? m->idx / m->page_sz : 0;
8a726114 1147
6fed9601 1148 /* corners and horizontal lines */
8a726114
KZ
1149 mvaddch(ln - 1, cl - 1, ACS_ULCORNER);
1150 mvaddch(ln + nlines, cl - 1, ACS_LLCORNER);
1151
5c04b408 1152 for (i = 0; i < MENU_V_ITEMWIDTH(m); i++) {
8a726114
KZ
1153 mvaddch(ln - 1, cl + i, ACS_HLINE);
1154 mvaddch(ln + nlines, cl + i, ACS_HLINE);
1155 }
6fed9601 1156
8a726114
KZ
1157 mvaddch(ln - 1, cl + i, ACS_URCORNER);
1158 mvaddch(ln + nlines, cl + i, ACS_LRCORNER);
1159
6fed9601
KZ
1160 /* draw also lines around empty lines on last page */
1161 if (m->page_sz &&
1162 m->nitems / m->page_sz == m->idx / m->page_sz) {
1163 for (i = m->nitems % m->page_sz + 1; i <= m->page_sz; i++) {
1164 mvaddch(i, cl - 1, ACS_VLINE);
5c04b408 1165 mvaddch(i, cl + MENU_V_ITEMWIDTH(m), ACS_VLINE);
6fed9601
KZ
1166 }
1167 }
8a726114
KZ
1168 if (m->title) {
1169 attron(A_BOLD);
1170 mvprintw(ln - 1, cl, " %s ", m->title);
1171 attroff(A_BOLD);
1172 }
6fed9601 1173 if (curpg != 0)
5c04b408 1174 mvaddch(ln - 1, cl + MENU_V_ITEMWIDTH(m) - 2, ACS_UARROW);
6fed9601 1175 if (m->page_sz && curpg < m->nitems / m->page_sz)
5c04b408 1176 mvaddch(ln + nlines, cl + MENU_V_ITEMWIDTH(m) - 2, ACS_DARROW);
8a726114
KZ
1177 }
1178
14620822 1179 DBG(MENU, ul_debug("draw end."));
6dbe3af9
KZ
1180}
1181
dda7fe12
OO
1182inline static int extra_insert_pair(struct cfdisk_line *l, const char *name, const char *data)
1183{
1184 struct libscols_line *lsl;
4c166c22 1185 int rc;
dda7fe12
OO
1186
1187 assert(l);
a2d0dbb4 1188 assert(l->extra);
dda7fe12 1189
13c551f2 1190 if (!data || !*data)
dda7fe12
OO
1191 return 0;
1192
1193 lsl = scols_table_new_line(l->extra, NULL);
1194 if (!lsl)
1195 return -ENOMEM;
1196
4c166c22
KZ
1197 rc = scols_line_set_data(lsl, 0, name);
1198 if (!rc)
1199 rc = scols_line_set_data(lsl, 1, data);
dda7fe12 1200
4c166c22 1201 return rc;
dda7fe12
OO
1202}
1203
cf9b302d
KZ
1204#ifndef HAVE_LIBMOUNT
1205static char *get_mountpoint( struct cfdisk *cf __attribute__((unused)),
1206 const char *tagname __attribute__((unused)),
1207 const char *tagdata __attribute__((unused)))
1208{
1209 return NULL;
1210}
1211#else
1212static char *get_mountpoint(struct cfdisk *cf, const char *tagname, const char *tagdata)
dda7fe12
OO
1213{
1214 struct libmnt_fs *fs = NULL;
1215 char *target = NULL;
1216 int mounted = 0;
1217
cf9b302d
KZ
1218 assert(tagname);
1219 assert(tagdata);
dda7fe12 1220
cf9b302d 1221 DBG(UI, ul_debug("asking for mountpoint [%s=%s]", tagname, tagdata));
dda7fe12
OO
1222
1223 if (!cf->mntcache)
1224 cf->mntcache = mnt_new_cache();
1225
1226 /* 1st try between mounted filesystems */
1227 if (!cf->mtab) {
1228 cf->mtab = mnt_new_table();
1229 if (cf->mtab) {
1230 mnt_table_set_cache(cf->mtab, cf->mntcache);
1231 mnt_table_parse_mtab(cf->mtab, NULL);
1232 }
1233 }
1234
1235 if (cf->mtab)
cf9b302d 1236 fs = mnt_table_find_tag(cf->mtab, tagname, tagdata, MNT_ITER_FORWARD);
dda7fe12
OO
1237
1238 /* 2nd try fstab */
1239 if (!fs) {
1240 if (!cf->fstab) {
1241 cf->fstab = mnt_new_table();
1242 if (cf->fstab) {
1243 mnt_table_set_cache(cf->fstab, cf->mntcache);
1244 mnt_table_parse_fstab(cf->fstab, NULL);
1245 }
1246 }
1247 if (cf->fstab)
cf9b302d 1248 fs = mnt_table_find_tag(cf->fstab, tagname, tagdata, MNT_ITER_FORWARD);
dda7fe12
OO
1249 } else
1250 mounted = 1;
1251
1252 if (fs) {
1253 if (mounted)
1254 xasprintf(&target, _("%s (mounted)"), mnt_fs_get_target(fs));
1255 else
1256 target = xstrdup(mnt_fs_get_target(fs));
1257 }
1258
1259 return target;
1260}
1261#endif /* HAVE_LIBMOUNT */
1262
1263static void extra_prepare_data(struct cfdisk *cf)
1264{
1265 struct fdisk_partition *pa = get_current_partition(cf);
1266 struct cfdisk_line *l = &cf->lines[cf->lines_idx];
1267 char *data = NULL;
cf9b302d 1268 char *mountpoint = NULL;
dda7fe12
OO
1269
1270 DBG(UI, ul_debug("preparing extra data"));
1271
1272 /* string data should not equal an empty string */
1273 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_NAME, &data) && data) {
1274 extra_insert_pair(l, _("Partition name:"), data);
cf9b302d
KZ
1275 if (!mountpoint)
1276 mountpoint = get_mountpoint(cf, "PARTLABEL", data);
dda7fe12
OO
1277 free(data);
1278 }
1279
1280 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_UUID, &data) && data) {
1281 extra_insert_pair(l, _("Partition UUID:"), data);
cf9b302d
KZ
1282 if (!mountpoint)
1283 mountpoint = get_mountpoint(cf, "PARTUUID", data);
dda7fe12
OO
1284 free(data);
1285 }
1286
1287 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_TYPE, &data) && data) {
1288 char *code = NULL, *type = NULL;
1289
1290 fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_TYPEID, &code);
1291 xasprintf(&type, "%s (%s)", data, code);
1292
1293 extra_insert_pair(l, _("Partition type:"), type);
1294 free(data);
1295 free(code);
1296 free(type);
1297 }
1298
1299 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_ATTR, &data) && data) {
1300 extra_insert_pair(l, _("Attributes:"), data);
1301 free(data);
1302 }
1303
1304 /* for numeric data, only show non-zero rows */
1305 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_BSIZE, &data) && data) {
1306 if (atoi(data))
1307 extra_insert_pair(l, "BSIZE:", data);
1308 free(data);
1309 }
1310
1311 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_CPG, &data) && data) {
1312 if (atoi(data))
1313 extra_insert_pair(l, "CPG:", data);
1314 free(data);
1315 }
1316
1317 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSIZE, &data) && data) {
1318 if (atoi(data))
1319 extra_insert_pair(l, "FSIZE:", data);
1320 free(data);
1321 }
1322
3d6db3fd
KZ
1323 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSUUID, &data) && data) {
1324 extra_insert_pair(l, _("Filesystem UUID:"), data);
cf9b302d
KZ
1325 if (!mountpoint)
1326 mountpoint = get_mountpoint(cf, "UUID", data);
3d6db3fd
KZ
1327 free(data);
1328 }
dda7fe12 1329
3d6db3fd
KZ
1330 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSLABEL, &data) && data) {
1331 extra_insert_pair(l, _("Filesystem LABEL:"), data);
cf9b302d
KZ
1332 if (!mountpoint)
1333 mountpoint = get_mountpoint(cf, "LABEL", data);
3d6db3fd
KZ
1334 free(data);
1335 }
1336 if (!fdisk_partition_to_string(pa, cf->cxt, FDISK_FIELD_FSTYPE, &data) && data) {
1337 extra_insert_pair(l, _("Filesystem:"), data);
1338 free(data);
dda7fe12 1339 }
dda7fe12 1340
cf9b302d
KZ
1341 if (mountpoint) {
1342 extra_insert_pair(l, _("Mountpoint:"), mountpoint);
1343 free(mountpoint);
dda7fe12 1344 }
dda7fe12
OO
1345}
1346
dda7fe12
OO
1347static int ui_draw_extra(struct cfdisk *cf)
1348{
1349 WINDOW *win_ex;
1350 int wline = 1;
1351 struct cfdisk_line *ln = &cf->lines[cf->lines_idx];
1352 char *tbstr = NULL, *end;
1353 int win_ex_start_line, win_height, tblen;
1354 int ndatalines;
1355
3542fc51
KZ
1356 if (!cf->show_extra)
1357 return 0;
1358
1359 DBG(UI, ul_debug("draw extra"));
1360
a2d0dbb4
KZ
1361 assert(ln->extra);
1362
cbcafba2 1363 if (cf->act_win) {
3542fc51 1364 wclear(cf->act_win);
cbcafba2
KZ
1365 touchwin(stdscr);
1366 }
3542fc51 1367
a2d0dbb4 1368 if (scols_table_is_empty(ln->extra)) {
dda7fe12 1369 extra_prepare_data(cf);
a2d0dbb4
KZ
1370 if (scols_table_is_empty(ln->extra))
1371 return 0;
1372 }
dda7fe12
OO
1373
1374 ndatalines = fdisk_table_get_nents(cf->table) + 1;
1375
1376 /* nents + header + one free line */
1377 win_ex_start_line = TABLE_START_LINE + ndatalines;
1378 win_height = MENU_START_LINE - win_ex_start_line;
1379 tblen = scols_table_get_nlines(ln->extra);
1380
1381 /* we can't get a single line of data under the partlist*/
1382 if (win_height < 3)
1383 return 1;
1384
1385 /* number of data lines + 2 for top/bottom lines */
1386 win_height = win_height < tblen + 2 ? win_height : tblen + 2;
1387
1388 if ((size_t) win_ex_start_line + win_height + 1 < MENU_START_LINE)
1389 win_ex_start_line = MENU_START_LINE - win_height;
1390
3542fc51 1391 win_ex = subwin(stdscr, win_height, ui_cols - 2, win_ex_start_line, 1);
dda7fe12
OO
1392
1393 scols_table_reduce_termwidth(ln->extra, 4);
1394 scols_print_table_to_string(ln->extra, &tbstr);
1395
1396 end = tbstr;
1397 while ((end = strchr(end, '\n')))
1398 *end++ = '\0';
1399
1400 box(win_ex, 0, 0);
1401
1402 end = tbstr;
1403 while (--win_height > 1) {
1404 mvwaddstr(win_ex, wline++, 1 /* window column*/, tbstr);
1405 tbstr += strlen(tbstr) + 1;
1406 }
1407 free(end);
1408
a2d0dbb4
KZ
1409 if (ln->w)
1410 delwin(ln->w);
dda7fe12
OO
1411
1412 DBG(UI, ul_debug("draw window: %p", win_ex));
3542fc51 1413 touchwin(stdscr);
dda7fe12
OO
1414 wrefresh(win_ex);
1415
1416 cf->act_win = ln->w = win_ex;
1417 return 0;
1418}
1419
8c3a5a44
KZ
1420static void ui_menu_goto(struct cfdisk *cf, int where)
1421{
3b411726 1422 struct cfdisk_menuitem *d;
8c3a5a44
KZ
1423 size_t old;
1424
f019ce1f
KZ
1425 /* stop and begin/end for vertical menus */
1426 if (cf->menu->vertical) {
1427 if (where < 0)
1428 where = 0;
1429 else if (where > (int) cf->menu->nitems - 1)
1430 where = cf->menu->nitems - 1;
1431 } else {
1432 /* continue from begin/end */
1433 if (where < 0)
1434 where = cf->menu->nitems - 1;
1435 else if ((size_t) where > cf->menu->nitems - 1)
1436 where = 0;
1437 }
6fed9601 1438 if ((size_t) where == cf->menu->idx)
8c3a5a44 1439 return;
6dbe3af9 1440
1af8003b
KZ
1441 ui_clean_info();
1442
6fed9601
KZ
1443 old = cf->menu->idx;
1444 cf->menu->idx = where;
1445
1446 if (!menuitem_on_page(cf, old)) {
1447 ui_draw_menu(cf);
1448 return;
1449 }
6dbe3af9 1450
8c3a5a44
KZ
1451 d = menu_get_menuitem(cf, old);
1452 ui_draw_menuitem(cf, d, old);
6dbe3af9 1453
8c3a5a44
KZ
1454 d = menu_get_menuitem(cf, where);
1455 ui_draw_menuitem(cf, d, where);
dda7fe12 1456
6dbe3af9
KZ
1457}
1458
ac27ea5c
KZ
1459static int ui_menu_move(struct cfdisk *cf, int key)
1460{
f019ce1f
KZ
1461 struct cfdisk_menu *m;
1462
ac27ea5c
KZ
1463 assert(cf);
1464 assert(cf->menu);
1465
7556c944
KZ
1466 if (key == ERR)
1467 return 0; /* ignore errors */
1468
f019ce1f
KZ
1469 m = cf->menu;
1470
14620822 1471 DBG(MENU, ul_debug("menu move key >%c<.", key));
f019ce1f
KZ
1472
1473 if (m->vertical)
ac27ea5c
KZ
1474 {
1475 switch (key) {
1476 case KEY_DOWN:
1477 case '\016': /* ^N */
1478 case 'j': /* Vi-like alternative */
f019ce1f 1479 ui_menu_goto(cf, m->idx + 1);
ac27ea5c
KZ
1480 return 0;
1481 case KEY_UP:
1482 case '\020': /* ^P */
1483 case 'k': /* Vi-like alternative */
f019ce1f 1484 ui_menu_goto(cf, (int) m->idx - 1);
ac27ea5c 1485 return 0;
f019ce1f
KZ
1486 case KEY_PPAGE:
1487 if (m->page_sz) {
1488 ui_menu_goto(cf, (int) m->idx - m->page_sz);
1489 return 0;
1490 }
d02c2035 1491 /* fallthrough */
ac27ea5c
KZ
1492 case KEY_HOME:
1493 ui_menu_goto(cf, 0);
1494 return 0;
f019ce1f
KZ
1495 case KEY_NPAGE:
1496 if (m->page_sz) {
1497 ui_menu_goto(cf, m->idx + m->page_sz);
1498 return 0;
1499 }
d02c2035 1500 /* fallthrough */
ac27ea5c 1501 case KEY_END:
f019ce1f 1502 ui_menu_goto(cf, m->nitems);
ac27ea5c
KZ
1503 return 0;
1504 }
1505 } else {
1506 switch (key) {
1507 case KEY_RIGHT:
1508 case '\t':
f019ce1f 1509 ui_menu_goto(cf, m->idx + 1);
ac27ea5c
KZ
1510 return 0;
1511 case KEY_LEFT:
1512#ifdef KEY_BTAB
1513 case KEY_BTAB:
1514#endif
f019ce1f 1515 ui_menu_goto(cf, (int) m->idx - 1);
ac27ea5c
KZ
1516 return 0;
1517 }
1518 }
1519
1520 return 1; /* key irrelevant for menu move */
1521}
1522
7556c944
KZ
1523/* but don't call me from ui_run(), this is for pop-up menus only */
1524static void ui_menu_resize(struct cfdisk *cf)
1525{
1526 resize();
1527 ui_clean_menu(cf);
1528 menu_refresh_size(cf);
1529 ui_draw_menu(cf);
1530 refresh();
1531}
1532
a6f69126
KZ
1533static int partition_on_page(struct cfdisk *cf, size_t i)
1534{
1535 if (cf->page_sz == 0 ||
1536 cf->lines_idx / cf->page_sz == i / cf->page_sz)
1537 return 1;
1538 return 0;
1539}
1540
8c3a5a44
KZ
1541static void ui_draw_partition(struct cfdisk *cf, size_t i)
1542{
1543 int ln = TABLE_START_LINE + 1 + i; /* skip table header */
1544 int cl = ARROW_CURSOR_WIDTH; /* we need extra space for cursor */
a6f69126
KZ
1545 int cur = cf->lines_idx == i;
1546 size_t curpg = 0;
1547
1548 if (cf->page_sz) {
1549 if (!partition_on_page(cf, i))
1550 return;
1551 ln = TABLE_START_LINE + (i % cf->page_sz) + 1;
1552 curpg = cf->lines_idx / cf->page_sz;
1553 }
6dbe3af9 1554
14620822
KZ
1555 DBG(UI, ul_debug(
1556 "draw partition %zu [page_sz=%zu, "
a6f69126
KZ
1557 "line=%d, idx=%zu]",
1558 i, cf->page_sz, ln, cf->lines_idx));
6dbe3af9 1559
a6f69126
KZ
1560 if (cur) {
1561 attron(A_REVERSE);
8c3a5a44 1562 mvaddstr(ln, 0, ARROW_CURSOR_STRING);
dda7fe12 1563 mvaddstr(ln, cl, cf->lines[i + 1].data);
a6f69126 1564 attroff(A_REVERSE);
6dbe3af9 1565 } else {
45333e9d
KZ
1566 int at = 0;
1567
f1512be8 1568 if (colors_wanted() && is_freespace(cf, i)) {
45333e9d
KZ
1569 attron(COLOR_PAIR(CFDISK_CL_FREESPACE));
1570 at = 1;
1571 }
8c3a5a44 1572 mvaddstr(ln, 0, ARROW_CURSOR_DUMMY);
dda7fe12 1573 mvaddstr(ln, cl, cf->lines[i + 1].data);
45333e9d 1574 if (at)
dda7fe12 1575 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE));
6dbe3af9
KZ
1576 }
1577
a6f69126
KZ
1578 if ((size_t) ln == MENU_START_LINE - 1 &&
1579 cf->page_sz && curpg < cf->nlines / cf->page_sz) {
1580 if (cur)
1581 attron(A_REVERSE);
eef63970 1582 mvaddch(ln, ui_cols - 1, ACS_DARROW);
a6f69126
KZ
1583 mvaddch(ln, 0, ACS_DARROW);
1584 if (cur)
1585 attroff(A_REVERSE);
1586 }
dda7fe12 1587
6dbe3af9
KZ
1588}
1589
8c3a5a44
KZ
1590static int ui_draw_table(struct cfdisk *cf)
1591{
1592 int cl = ARROW_CURSOR_WIDTH;
1593 size_t i, nparts = fdisk_table_get_nents(cf->table);
a6f69126 1594 size_t curpg = cf->page_sz ? cf->lines_idx / cf->page_sz : 0;
7eda085c 1595
14620822 1596 DBG(UI, ul_debug("draw table"));
6dbe3af9 1597
a6f69126
KZ
1598 for (i = TABLE_START_LINE; i <= TABLE_START_LINE + cf->page_sz; i++) {
1599 move(i, 0);
1600 clrtoeol();
1601 }
6dbe3af9 1602
a2d0dbb4 1603 if (nparts == 0 || (size_t) cf->lines_idx > nparts - 1)
b2301179
KZ
1604 cf->lines_idx = nparts ? nparts - 1 : 0;
1605
8c3a5a44
KZ
1606 /* print header */
1607 attron(A_BOLD);
dda7fe12 1608 mvaddstr(TABLE_START_LINE, cl, cf->lines[0].data);
8c3a5a44 1609 attroff(A_BOLD);
6dbe3af9 1610
8c3a5a44
KZ
1611 /* print partitions */
1612 for (i = 0; i < nparts; i++)
1613 ui_draw_partition(cf, i);
6dbe3af9 1614
a6f69126 1615 if (curpg != 0) {
eef63970 1616 mvaddch(TABLE_START_LINE, ui_cols - 1, ACS_UARROW);
a6f69126
KZ
1617 mvaddch(TABLE_START_LINE, 0, ACS_UARROW);
1618 }
1619 if (cf->page_sz && curpg < cf->nlines / cf->page_sz) {
eef63970 1620 mvaddch(MENU_START_LINE - 1, ui_cols - 1, ACS_DARROW);
a6f69126
KZ
1621 mvaddch(MENU_START_LINE - 1, 0, ACS_DARROW);
1622 }
8c3a5a44 1623 return 0;
6dbe3af9
KZ
1624}
1625
8c3a5a44
KZ
1626static int ui_table_goto(struct cfdisk *cf, int where)
1627{
1628 size_t old;
1629 size_t nparts = fdisk_table_get_nents(cf->table);
6dbe3af9 1630
14620822 1631 DBG(UI, ul_debug("goto table %d", where));
6dbe3af9 1632
8c3a5a44
KZ
1633 if (where < 0)
1634 where = 0;
1635 else if ((size_t) where > nparts - 1)
1636 where = nparts - 1;
6dbe3af9 1637
8c3a5a44
KZ
1638 if ((size_t) where == cf->lines_idx)
1639 return 0;
6dbe3af9 1640
8c3a5a44
KZ
1641 old = cf->lines_idx;
1642 cf->lines_idx = where;
6dbe3af9 1643
a6f69126
KZ
1644 if (!partition_on_page(cf, old) ||!partition_on_page(cf, where))
1645 ui_draw_table(cf);
1646 else {
1647 ui_draw_partition(cf, old); /* cleanup old */
1648 ui_draw_partition(cf, where); /* draw new */
1649 }
1af8003b 1650 ui_clean_info();
8c3a5a44 1651 ui_draw_menu(cf);
3542fc51 1652 ui_draw_extra(cf);
8c3a5a44 1653 refresh();
dda7fe12 1654
8c3a5a44 1655 return 0;
6dbe3af9
KZ
1656}
1657
8c3a5a44
KZ
1658static int ui_refresh(struct cfdisk *cf)
1659{
57c83d63 1660 struct fdisk_label *lb;
8c3a5a44 1661 char *id = NULL;
57c83d63 1662 uint64_t bytes = fdisk_get_nsectors(cf->cxt) * fdisk_get_sector_size(cf->cxt);
ec8a728a 1663 char *strsz;
8c3a5a44 1664
83fa0f80 1665 if (!ui_enabled)
8c3a5a44
KZ
1666 return -EINVAL;
1667
ec8a728a
KZ
1668 strsz = size_to_human_string(SIZE_SUFFIX_SPACE
1669 | SIZE_SUFFIX_3LETTER, bytes);
1670
57c83d63
KZ
1671 lb = fdisk_get_label(cf->cxt, NULL);
1672 assert(lb);
1673
a2d0dbb4
KZ
1674 clear();
1675
8c3a5a44
KZ
1676 /* header */
1677 attron(A_BOLD);
57c83d63 1678 ui_center(0, _("Disk: %s"), fdisk_get_devname(cf->cxt));
8c3a5a44 1679 attroff(A_BOLD);
1af8003b 1680 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
57c83d63 1681 strsz, bytes, (uintmax_t) fdisk_get_nsectors(cf->cxt));
8c3a5a44 1682 if (fdisk_get_disklabel_id(cf->cxt, &id) == 0 && id)
1af8003b 1683 ui_center(2, _("Label: %s, identifier: %s"),
57c83d63 1684 fdisk_label_get_name(lb), id);
7eda085c 1685 else
57c83d63 1686 ui_center(2, _("Label: %s"), fdisk_label_get_name(lb));
8c3a5a44 1687 free(strsz);
6dbe3af9 1688
8c3a5a44
KZ
1689 ui_draw_table(cf);
1690 ui_draw_menu(cf);
6dbe3af9 1691 refresh();
8c3a5a44 1692 return 0;
6dbe3af9
KZ
1693}
1694
ffab025d 1695static ssize_t ui_get_string(const char *prompt,
b1f58330
KZ
1696 const char *hint, char *buf, size_t len)
1697{
1698 size_t cells = 0;
1699 ssize_t i = 0, rc = -1;
b1f58330
KZ
1700 int ln = MENU_START_LINE, cl = 1;
1701
b1f58330
KZ
1702 assert(buf);
1703 assert(len);
1704
1705 move(ln, 0);
1706 clrtoeol();
1707
a89eafed
KZ
1708 move(ln + 1, 0);
1709 clrtoeol();
1710
b1f58330 1711 if (prompt) {
f1512be8 1712 mvaddstr(ln, cl, (char *) prompt);
b1f58330
KZ
1713 cl += mbs_safe_width(prompt);
1714 }
1715
1716 /* default value */
1717 if (*buf) {
1718 i = strlen(buf);
1719 cells = mbs_safe_width(buf);
1720 mvaddstr(ln, cl, buf);
1721 }
1722
1723 if (hint)
1724 ui_hint(hint);
1725 else
1726 ui_clean_hint();
1727
1728 move(ln, cl + cells);
1729 curs_set(1);
1730 refresh();
1731
1732 while (1) {
1733#if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1734 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
a08fa9ab 1735 wint_t c;
b1f58330
KZ
1736 if (get_wch(&c) == ERR) {
1737#else
a08fa9ab 1738 int c;
b1f58330
KZ
1739 if ((c = getch()) == ERR) {
1740#endif
7556c944
KZ
1741 if (ui_resize) {
1742 resize();
1743 continue;
1744 }
b1f58330
KZ
1745 if (!isatty(STDIN_FILENO))
1746 exit(2);
1747 else
1748 goto done;
1749 }
1750 if (c == '\r' || c == '\n' || c == KEY_ENTER)
1751 break;
1752
1753 switch (c) {
1754 case KEY_ESC:
1755 rc = -CFDISK_ERR_ESC;
1756 goto done;
635a8151
KZ
1757 case KEY_LEFT: /* TODO: implement full buffer editor */
1758 case KEY_RIGHT:
1759 case KEY_END:
1760 case KEY_HOME:
1761 case KEY_UP:
1762 case KEY_DOWN:
1763 beep();
1764 break;
b1f58330
KZ
1765 case KEY_DELETE:
1766 case '\b':
1767 case KEY_BACKSPACE:
1768 if (i > 0) {
1769 cells--;
1770 i = mbs_truncate(buf, &cells);
1771 if (i < 0)
1772 goto done;
1773 mvaddch(ln, cl + cells, ' ');
1774 move(ln, cl + cells);
1775 } else
1776 beep();
1777 break;
635a8151 1778
b1f58330
KZ
1779 default:
1780#if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1781 if (i + 1 < (ssize_t) len && iswprint(c)) {
1782 wchar_t wc = (wchar_t) c;
1783 char s[MB_CUR_MAX + 1];
1784 int sz = wctomb(s, wc);
1785
1786 if (sz > 0 && sz + i < (ssize_t) len) {
1787 s[sz] = '\0';
1788 mvaddnstr(ln, cl + cells, s, sz);
1789 memcpy(buf + i, s, sz);
1790 i += sz;
1791 buf[i] = '\0';
1792 cells += wcwidth(wc);
1793 } else
1794 beep();
1795 }
1796#else
1797 if (i + 1 < (ssize_t) len && isprint(c)) {
1798 mvaddch(ln, cl + cells, c);
f0fb84ab
RM
1799 buf[i++] = c;
1800 buf[i] = '\0';
b1f58330
KZ
1801 cells++;
1802 }
1803#endif
1804 else
1805 beep();
1806 }
1807 refresh();
1808 }
1809
1810 rc = i; /* success */
1811done:
1812 move(ln, 0);
1813 clrtoeol();
1814 curs_set(0);
1815 refresh();
1816
1817 return rc;
1818}
1819
1820/* @res is default value as well as result in bytes */
1821static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
9b0b9fb1 1822 uintmax_t low, uintmax_t up, int *expsize)
b1f58330
KZ
1823{
1824 char buf[128];
1825 uintmax_t user = 0;
1826 ssize_t rc;
1827 char *dflt = size_to_human_string(0, *res);
1828
14620822 1829 DBG(UI, ul_debug("get_size (default=%ju)", *res));
b1f58330
KZ
1830
1831 ui_clean_info();
1832
29e9b787
KZ
1833 snprintf(buf, sizeof(buf), "%s", dflt);
1834
b1f58330 1835 do {
91ba41ca 1836 int pwr = 0, insec = 0;
b1f58330 1837
ffab025d 1838 rc = ui_get_string(prompt,
a4fbbbd4
BS
1839 _("May be followed by M for MiB, G for GiB, "
1840 "T for TiB, or S for sectors."),
b1f58330 1841 buf, sizeof(buf));
29e9b787
KZ
1842 ui_clean_warn();
1843
b1f58330
KZ
1844 if (rc == 0) {
1845 ui_warnx(_("Please, specify size."));
1846 continue; /* nothing specified */
1847 } else if (rc == -CFDISK_ERR_ESC)
1848 break; /* cancel dialog */
1849
91ba41ca
KZ
1850 if (strcmp(buf, dflt) == 0)
1851 user = *res, rc = 0; /* no change, use default */
1852 else {
1853 size_t len = strlen(buf);
bfdb3244 1854 if (buf[len - 1] == 'S' || buf[len - 1] == 's') {
91ba41ca
KZ
1855 insec = 1;
1856 buf[len - 1] = '\0';
1857 }
1858 rc = parse_size(buf, &user, &pwr); /* parse */
1859 }
1860
b1f58330 1861 if (rc == 0) {
14620822 1862 DBG(UI, ul_debug("get_size user=%ju, power=%d, sectors=%s",
91ba41ca
KZ
1863 user, pwr, insec ? "yes" : "no"));
1864 if (insec)
57c83d63 1865 user *= fdisk_get_sector_size(cf->cxt);
b1f58330 1866 if (user < low) {
143d9e3e 1867 ui_warnx(_("Minimum size is %ju bytes."), low);
b1f58330
KZ
1868 rc = -ERANGE;
1869 }
1870 if (user > up && pwr && user < up + (1ULL << pwr * 10))
1871 /* ignore when the user specified size overflow
1872 * with in range specified by suffix (e.g. MiB) */
1873 user = up;
1874
1875 if (user > up) {
143d9e3e 1876 ui_warnx(_("Maximum size is %ju bytes."), up);
b1f58330
KZ
1877 rc = -ERANGE;
1878 }
9b0b9fb1
KZ
1879 if (rc == 0 && insec && expsize)
1880 *expsize = 1;
1881
2cec7949
KZ
1882 } else
1883 ui_warnx(_("Failed to parse size."));
b1f58330
KZ
1884 } while (rc != 0);
1885
1886 if (rc == 0)
1887 *res = user;
1888 free(dflt);
1889
14620822 1890 DBG(UI, ul_debug("get_size (result=%ju, rc=%zd)", *res, rc));
b1f58330
KZ
1891 return rc;
1892}
1893
6d6d9c1a
KZ
1894static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
1895 struct fdisk_parttype *cur)
6fed9601 1896{
3b411726 1897 struct cfdisk_menuitem *d, *cm;
6d6d9c1a 1898 size_t i = 0, nitems, idx = 0;
a745611d 1899 struct fdisk_parttype *t = NULL;
5ab37600 1900 struct fdisk_label *lb;
a745611d 1901 int codetypes = 0;
6fed9601 1902
14620822 1903 DBG(UI, ul_debug("asking for parttype."));
6fed9601 1904
5ab37600
KZ
1905 lb = fdisk_get_label(cf->cxt, NULL);
1906
6fed9601
KZ
1907 /* create cfdisk menu according to label types, note that the
1908 * last cm[] item has to be empty -- so nitems + 1 */
a745611d
KZ
1909 nitems = fdisk_label_get_nparttypes(lb);
1910 if (!nitems)
6d6d9c1a 1911 return NULL;
5ab37600 1912
3b411726 1913 cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
6fed9601
KZ
1914 if (!cm)
1915 return NULL;
1916
a745611d 1917 codetypes = fdisk_label_has_code_parttypes(lb);
6d6d9c1a 1918
6fed9601 1919 for (i = 0; i < nitems; i++) {
a745611d 1920 const struct fdisk_parttype *x = fdisk_label_get_parttype(lb, i);
6d6d9c1a 1921 char *name;
6fed9601 1922
a745611d
KZ
1923 cm[i].userdata = (void *) x;
1924 if (codetypes)
1925 xasprintf(&name, "%2x %s",
1926 fdisk_parttype_get_code(x),
1927 _(fdisk_parttype_get_name(x)));
6d6d9c1a 1928 else {
a745611d
KZ
1929 name = (char *) _(fdisk_parttype_get_name(x));
1930 cm[i].desc = fdisk_parttype_get_string(x);
6d6d9c1a
KZ
1931 }
1932 cm[i].name = name;
1933 if (x == cur)
1934 idx = i;
6fed9601
KZ
1935 }
1936
1937 /* make the new menu active */
1938 menu_push(cf, cm);
1939 cf->menu->vertical = 1;
6d6d9c1a 1940 cf->menu->idx = idx;
6fed9601
KZ
1941 menu_set_title(cf->menu, _("Select partition type"));
1942 ui_draw_menu(cf);
1943 refresh();
1944
1945 do {
1946 int key = getch();
f019ce1f 1947
7556c944
KZ
1948 if (ui_resize)
1949 ui_menu_resize(cf);
6fed9601
KZ
1950 if (ui_menu_move(cf, key) == 0)
1951 continue;
f019ce1f 1952
6fed9601
KZ
1953 switch (key) {
1954 case KEY_ENTER:
1955 case '\n':
1956 case '\r':
1957 d = menu_get_menuitem(cf, cf->menu->idx);
1958 if (d)
1959 t = (struct fdisk_parttype *) d->userdata;
1960 goto done;
ad5d45c0 1961 case KEY_ESC:
6fed9601
KZ
1962 case 'q':
1963 case 'Q':
1964 goto done;
1965 }
1966 } while (1);
1967
1968done:
1969 menu_pop(cf);
a745611d 1970 if (codetypes) {
6d6d9c1a
KZ
1971 for (i = 0; i < nitems; i++)
1972 free((char *) cm[i].name);
1973 }
6fed9601 1974 free(cm);
57c83d63
KZ
1975 DBG(UI, ul_debug("get parrtype done [type=%s] ", t ?
1976 fdisk_parttype_get_name(t) : NULL));
6fed9601
KZ
1977 return t;
1978}
1979
a89eafed
KZ
1980static int ui_script_read(struct cfdisk *cf)
1981{
1982 struct fdisk_script *sc = NULL;
1983 char buf[PATH_MAX] = { 0 };
1984 int rc;
1985
941409c0 1986 erase();
ffab025d 1987 rc = ui_get_string( _("Enter script file name: "),
a89eafed
KZ
1988 _("The script file will be applied to in-memory partition table."),
1989 buf, sizeof(buf));
1990 if (rc <= 0)
1991 return rc;
1992
1993 rc = -1;
1994 errno = 0;
1995 sc = fdisk_new_script_from_file(cf->cxt, buf);
1996 if (!sc && errno)
54fefa07 1997 ui_warn(_("Cannot open %s"), buf);
a89eafed
KZ
1998 else if (!sc)
1999 ui_warnx(_("Failed to parse script file %s"), buf);
2000 else if (fdisk_apply_script(cf->cxt, sc) != 0)
2001 ui_warnx(_("Failed to apply script %s"), buf);
2002 else
2003 rc = 0;
2004
941409c0 2005 ui_clean_hint();
a89eafed
KZ
2006 fdisk_unref_script(sc);
2007 return rc;
2008}
2009
2010static int ui_script_write(struct cfdisk *cf)
2011{
2012 struct fdisk_script *sc = NULL;
2013 char buf[PATH_MAX] = { 0 };
2014 FILE *f = NULL;
2015 int rc;
2016
ffab025d 2017 rc = ui_get_string( _("Enter script file name: "),
a89eafed
KZ
2018 _("The current in-memory partition table will be dumped to the file."),
2019 buf, sizeof(buf));
2020 if (rc <= 0)
2021 return rc;
2022
2023 rc = 0;
2024 sc = fdisk_new_script(cf->cxt);
2025 if (!sc) {
2026 ui_warn(_("Failed to allocate script handler"));
2027 goto done;
2028 }
2029
2030 rc = fdisk_script_read_context(sc, NULL);
2031 if (rc) {
2032 ui_warnx(_("Failed to read disk layout into script."));
2033 goto done;
2034 }
2035
8c9615a9 2036 DBG(UI, ul_debug("writing dump into: '%s'", buf));
a89eafed
KZ
2037 f = fopen(buf, "w");
2038 if (!f) {
54fefa07 2039 ui_warn(_("Cannot open %s"), buf);
8c9615a9 2040 rc = -errno;
a89eafed
KZ
2041 goto done;
2042 }
2043
2044 rc = fdisk_script_write_file(sc, f);
8c9615a9
KZ
2045 if (!rc)
2046 ui_info(_("Disk layout successfully dumped."));
2047done:
a89eafed
KZ
2048 if (rc)
2049 ui_warn(_("Failed to write script %s"), buf);
a89eafed
KZ
2050 if (f)
2051 fclose(f);
2052 fdisk_unref_script(sc);
2053 return rc;
2054}
2055
7aa0d529
KZ
2056/* prints menu with libfdisk labels and waits for users response */
2057static int ui_create_label(struct cfdisk *cf)
2058{
3b411726 2059 struct cfdisk_menuitem *d, *cm;
941409c0 2060 int rc = 1, refresh_menu = 1;
7aa0d529
KZ
2061 size_t i = 0, nitems;
2062 struct fdisk_label *lb = NULL;
2063
2064 assert(cf);
2065
14620822 2066 DBG(UI, ul_debug("asking for new disklabe."));
7aa0d529
KZ
2067
2068 /* create cfdisk menu according to libfdisk labels, note that the
2069 * last cm[] item has to be empty -- so nitems + 1 */
6a632136 2070 nitems = fdisk_get_nlabels(cf->cxt);
3b411726 2071 cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
7aa0d529 2072
6a632136 2073 while (fdisk_next_label(cf->cxt, &lb) == 0) {
57c83d63 2074 if (fdisk_label_is_disabled(lb) ||
8eccde20 2075 fdisk_label_get_type(lb) == FDISK_DISKLABEL_BSD)
dd626abd 2076 continue;
57c83d63 2077 cm[i++].name = fdisk_label_get_name(lb);
7aa0d529
KZ
2078 }
2079
2080 erase();
7aa0d529
KZ
2081
2082 /* make the new menu active */
63128bbf 2083 menu_push(cf, cm);
7aa0d529
KZ
2084 cf->menu->vertical = 1;
2085 menu_set_title(cf->menu, _("Select label type"));
941409c0
KZ
2086
2087 if (!cf->zero_start)
2088 ui_info(_("Device does not contain a recognized partition table."));
2089
7aa0d529
KZ
2090
2091 do {
941409c0
KZ
2092 if (refresh_menu) {
2093 ui_draw_menu(cf);
2094 ui_hint(_("Select a type to create a new label or press 'L' to load script file."));
2095 refresh();
2096 refresh_menu = 0;
2097 }
2098
7aa0d529 2099 int key = getch();
7556c944
KZ
2100
2101 if (ui_resize)
2102 ui_menu_resize(cf);
7aa0d529
KZ
2103 if (ui_menu_move(cf, key) == 0)
2104 continue;
2105 switch (key) {
2106 case KEY_ENTER:
2107 case '\n':
2108 case '\r':
6fed9601 2109 d = menu_get_menuitem(cf, cf->menu->idx);
7aa0d529
KZ
2110 if (d)
2111 rc = fdisk_create_disklabel(cf->cxt, d->name);
2112 goto done;
ad5d45c0 2113 case KEY_ESC:
7aa0d529
KZ
2114 case 'q':
2115 case 'Q':
2116 goto done;
f16c37f4 2117 case 'l':
a89eafed 2118 case 'L':
a89eafed
KZ
2119 rc = ui_script_read(cf);
2120 if (rc == 0)
2121 goto done;
941409c0 2122 refresh_menu = 1;
a89eafed 2123 break;
7aa0d529
KZ
2124 }
2125 } while (1);
2126
2127done:
2128 menu_pop(cf);
2129 free(cm);
14620822 2130 DBG(UI, ul_debug("create label done [rc=%d] ", rc));
7aa0d529
KZ
2131 return rc;
2132}
2133
a89eafed 2134
314a0dd8
KZ
2135static int ui_help(void)
2136{
2137 size_t i;
314a0dd8 2138 static const char *help[] = {
e993b9c8
BS
2139 N_("This is cfdisk, a curses-based disk partitioning program."),
2140 N_("It lets you create, delete, and modify partitions on a block device."),
fd4484a7 2141 " ",
314a0dd8
KZ
2142 N_("Command Meaning"),
2143 N_("------- -------"),
2144 N_(" b Toggle bootable flag of the current partition"),
2145 N_(" d Delete the current partition"),
2146 N_(" h Print this screen"),
2147 N_(" n Create new partition from free space"),
2148 N_(" q Quit program without writing partition table"),
33f7c502 2149 N_(" s Fix partitions order (only when in disarray)"),
e993b9c8 2150 N_(" t Change the partition type"),
a89eafed 2151 N_(" u Dump disk layout to sfdisk compatible script file"),
e993b9c8
BS
2152 N_(" W Write partition table to disk (you must enter uppercase W);"),
2153 N_(" since this might destroy data on the disk, you must either"),
2154 N_(" confirm or deny the write by entering 'yes' or 'no'"),
dda7fe12 2155 N_(" x Display/hide extra information about a partition"),
314a0dd8
KZ
2156 N_("Up Arrow Move cursor to the previous partition"),
2157 N_("Down Arrow Move cursor to the next partition"),
2158 N_("Left Arrow Move cursor to the previous menu item"),
2159 N_("Right Arrow Move cursor to the next menu item"),
fd4484a7 2160 " ",
314a0dd8 2161 N_("Note: All of the commands can be entered with either upper or lower"),
e993b9c8 2162 N_("case letters (except for Write)."),
fd4484a7 2163 " ",
314a0dd8
KZ
2164 N_("Use lsblk(8) or partx(8) to see more details about the device.")
2165 };
2166
2167 erase();
314a0dd8
KZ
2168 for (i = 0; i < ARRAY_SIZE(help); i++)
2169 mvaddstr(i, 1, _(help[i]));
2170
2171 ui_info(_("Press a key to continue."));
7556c944 2172
314a0dd8
KZ
2173 getch();
2174 return 0;
2175}
2176
63128bbf
KZ
2177/* TODO: use @sz, now 128bytes */
2178static int main_menu_ignore_keys(struct cfdisk *cf, char *ignore,
2179 size_t sz __attribute__((__unused__)))
2180{
2181 struct fdisk_partition *pa = get_current_partition(cf);
2182 size_t i = 0;
2183
2184 if (!pa)
2185 return 0;
2186 if (fdisk_partition_is_freespace(pa)) {
2187 ignore[i++] = 'd'; /* delete */
2188 ignore[i++] = 't'; /* set type */
2189 ignore[i++] = 'b'; /* set bootable */
b5ef65a9 2190 cf->menu->prefkey = 'n';
63128bbf 2191 } else {
b5ef65a9 2192 cf->menu->prefkey = 'q';
63128bbf 2193 ignore[i++] = 'n';
aa36c2cf
KZ
2194 if (!fdisk_is_label(cf->cxt, DOS) &&
2195 !fdisk_is_label(cf->cxt, SGI))
63128bbf
KZ
2196 ignore[i++] = 'b';
2197 }
e146ae4e 2198
d5314bf5
KZ
2199 if (!cf->wrong_order)
2200 ignore[i++] = 's';
f9fb8290 2201
6a632136 2202 if (fdisk_is_readonly(cf->cxt))
e146ae4e 2203 ignore[i++] = 'W';
dda7fe12 2204
63128bbf
KZ
2205 return i;
2206}
2207
2208
ed8a13e6
KZ
2209/* returns: error: < 0, success: 0, quit: 1 */
2210static int main_menu_action(struct cfdisk *cf, int key)
2211{
2212 size_t n;
d5314bf5 2213 int ref = 0, rc, org_order = cf->wrong_order;
ed8a13e6
KZ
2214 const char *info = NULL, *warn = NULL;
2215 struct fdisk_partition *pa;
2216
2217 assert(cf);
2218 assert(cf->cxt);
2219 assert(cf->menu);
2220
2221 if (key == 0) {
3b411726 2222 struct cfdisk_menuitem *d = menu_get_menuitem(cf, cf->menu->idx);
ed8a13e6
KZ
2223 if (!d)
2224 return 0;
2225 key = d->key;
2226
2227 } else if (key != 'w' && key != 'W')
2228 key = tolower(key); /* case insensitive except 'W'rite */
2229
14620822 2230 DBG(MENU, ul_debug("main menu action: key=%c", key));
ed8a13e6
KZ
2231
2232 if (cf->menu->ignore && strchr(cf->menu->ignore, key)) {
14620822 2233 DBG(MENU, ul_debug(" ignore '%c'", key));
ed8a13e6
KZ
2234 return 0;
2235 }
2236
2237 pa = get_current_partition(cf);
ec8a728a
KZ
2238 if (!pa)
2239 return -EINVAL;
ed8a13e6
KZ
2240 n = fdisk_partition_get_partno(pa);
2241
14620822 2242 DBG(MENU, ul_debug("menu action on %p", pa));
7b1ffbc4
KZ
2243 ui_clean_hint();
2244 ui_clean_info();
ed8a13e6
KZ
2245
2246 switch (key) {
2247 case 'b': /* Bootable flag */
2248 {
aa36c2cf
KZ
2249 int fl = fdisk_is_label(cf->cxt, DOS) ? DOS_FLAG_ACTIVE :
2250 fdisk_is_label(cf->cxt, SGI) ? SGI_FLAG_BOOT : 0;
ed8a13e6 2251
a1ef792f 2252 if (fl && fdisk_toggle_partition_flag(cf->cxt, n, fl))
ed8a13e6
KZ
2253 warn = _("Could not toggle the flag.");
2254 else if (fl)
2255 ref = 1;
2256 break;
2257 }
f1512be8 2258#ifdef KEY_DC
f019ce1f 2259 case KEY_DC:
f1512be8 2260#endif
ed8a13e6
KZ
2261 case 'd': /* Delete */
2262 if (fdisk_delete_partition(cf->cxt, n) != 0)
2263 warn = _("Could not delete partition %zu.");
2264 else
2265 info = _("Partition %zu has been deleted.");
2266 ref = 1;
2267 break;
d4640320 2268 case 'h': /* Help */
2a0ffa31 2269 case '?':
314a0dd8
KZ
2270 ui_help();
2271 ref = 1;
2272 break;
ed8a13e6
KZ
2273 case 'n': /* New */
2274 {
75090201 2275 uint64_t start, size, dflt_size, secs;
ed8a13e6 2276 struct fdisk_partition *npa; /* the new partition */
9b0b9fb1 2277 int expsize = 0; /* size specified explicitly in sectors */
ed8a13e6 2278
ec8a728a 2279 if (!fdisk_partition_is_freespace(pa) || !fdisk_partition_has_start(pa))
ed8a13e6 2280 return -EINVAL;
75090201 2281
ed8a13e6
KZ
2282 /* free space range */
2283 start = fdisk_partition_get_start(pa);
57c83d63 2284 size = dflt_size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cf->cxt);
ed8a13e6 2285
1bb387bd
KZ
2286 if (ui_get_size(cf, _("Partition size: "), &size,
2287 fdisk_get_sector_size(cf->cxt),
2288 size, &expsize) == -CFDISK_ERR_ESC)
ed8a13e6
KZ
2289 break;
2290
75090201 2291 secs = size / fdisk_get_sector_size(cf->cxt);
75090201
KZ
2292
2293 npa = fdisk_new_partition();
2294 if (!npa)
2295 return -ENOMEM;
2296
ed8a13e6
KZ
2297 if (dflt_size == size) /* default is to fillin all free space */
2298 fdisk_partition_end_follow_default(npa, 1);
75090201
KZ
2299 else
2300 fdisk_partition_set_size(npa, secs);
ed8a13e6 2301
9b0b9fb1
KZ
2302 if (expsize)
2303 fdisk_partition_size_explicit(pa, 1);
2304
ed8a13e6 2305 fdisk_partition_set_start(npa, start);
9b0b9fb1 2306 fdisk_partition_partno_follow_default(npa, 1);
ed8a13e6 2307 /* add to disk label -- libfdisk will ask for missing details */
c3bc7483 2308 rc = fdisk_add_partition(cf->cxt, npa, NULL);
ed8a13e6
KZ
2309 fdisk_unref_partition(npa);
2310 if (rc == 0)
2311 ref = 1;
2312 break;
2313 }
2314 case 'q': /* Quit */
2315 return 1;
2316 case 't': /* Type */
6fed9601 2317 {
6fed9601
KZ
2318 struct fdisk_parttype *t;
2319
ec8a728a 2320 if (fdisk_partition_is_freespace(pa))
6fed9601
KZ
2321 return -EINVAL;
2322 t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
6d6d9c1a 2323 t = ui_get_parttype(cf, t);
6fed9601
KZ
2324 ref = 1;
2325
2326 if (t && fdisk_set_partition_type(cf->cxt, n, t) == 0)
09af3db4 2327 info = _("Changed type of partition %zu.");
6fed9601 2328 else
09af3db4 2329 info = _("The type of partition %zu is unchanged.");
ed8a13e6 2330 break;
6fed9601 2331 }
d4640320 2332 case 's': /* Sort */
d5314bf5
KZ
2333 if (cf->wrong_order) {
2334 fdisk_reorder_partitions(cf->cxt);
2335 ref = 1;
2336 }
2337 break;
d4640320 2338 case 'u': /* dUmp */
8c9615a9 2339 ui_script_write(cf);
a89eafed 2340 break;
ed8a13e6 2341 case 'W': /* Write */
7b1ffbc4
KZ
2342 {
2343 char buf[64] = { 0 };
e146ae4e 2344
6a632136 2345 if (fdisk_is_readonly(cf->cxt)) {
d4640320 2346 warn = _("Device is open in read-only mode.");
e146ae4e
KZ
2347 break;
2348 }
2349
ffab025d 2350 rc = ui_get_string(
7b1ffbc4
KZ
2351 _("Are you sure you want to write the partition "
2352 "table to disk? "),
09af3db4 2353 _("Type \"yes\" or \"no\", or press ESC to leave this dialog."),
7b1ffbc4
KZ
2354 buf, sizeof(buf));
2355
2356 ref = 1;
59af21c3
KZ
2357 if (rc <= 0 || (strcasecmp(buf, "yes") != 0 &&
2358 strcasecmp(buf, _("yes")) != 0)) {
d4640320 2359 info = _("Did not write partition table to disk.");
7b1ffbc4
KZ
2360 break;
2361 }
ed8a13e6
KZ
2362 rc = fdisk_write_disklabel(cf->cxt);
2363 if (rc)
d4640320 2364 warn = _("Failed to write disklabel.");
7b1ffbc4
KZ
2365 else {
2366 fdisk_reread_partition_table(cf->cxt);
2367 info = _("The partition table has been altered.");
2368 }
bc787727 2369 cf->nwrites++;
7b1ffbc4
KZ
2370 break;
2371 }
2372 default:
ed8a13e6
KZ
2373 break;
2374 }
2375
2376 if (ref) {
2377 lines_refresh(cf);
2378 ui_refresh(cf);
3542fc51 2379 ui_draw_extra(cf);
a89eafed
KZ
2380 } else
2381 ui_draw_menu(cf);
7b1ffbc4
KZ
2382
2383 ui_clean_hint();
75090201 2384
ed8a13e6 2385 if (warn)
7b1ffbc4 2386 ui_warnx(warn, n + 1);
ed8a13e6 2387 else if (info)
7b1ffbc4 2388 ui_info(info, n + 1);
d5314bf5
KZ
2389 else if (key == 'n' && cf->wrong_order && org_order == 0)
2390 ui_info(_("Note that partition table entries are not in disk order now."));
ed8a13e6
KZ
2391
2392 return 0;
2393}
2394
7556c944
KZ
2395static void ui_resize_refresh(struct cfdisk *cf)
2396{
2397 resize();
2398 menu_refresh_size(cf);
2399 lines_refresh(cf);
2400 ui_refresh(cf);
3542fc51 2401 ui_draw_extra(cf);
7556c944
KZ
2402}
2403
a2d0dbb4
KZ
2404static void toggle_show_extra(struct cfdisk *cf)
2405{
2406 if (cf->show_extra && cf->act_win) {
2407 wclear(cf->act_win);
2408 touchwin(stdscr);
2409 }
2410 cf->show_extra = cf->show_extra ? 0 : 1;
2411
2412 if (cf->show_extra)
2413 ui_draw_extra(cf);
2414 DBG(MENU, ul_debug("extra: %s", cf->show_extra ? "ENABLED" : "DISABLED" ));
2415}
2416
8c3a5a44
KZ
2417static int ui_run(struct cfdisk *cf)
2418{
7aa0d529 2419 int rc = 0;
6dbe3af9 2420
eef63970
KZ
2421 ui_lines = LINES;
2422 ui_cols = COLS;
2423 DBG(UI, ul_debug("start cols=%zu, lines=%zu", ui_cols, ui_lines));
6dbe3af9 2424
97ffbd43 2425 if (fdisk_get_collision(cf->cxt)) {
cb19f547 2426 ui_warnx(_("Device already contains a %s signature; it will be removed by a write command."),
97ffbd43
KZ
2427 fdisk_get_collision(cf->cxt));
2428 fdisk_enable_wipe(cf->cxt, 1);
2429 ui_hint(_("Press a key to continue."));
2430 getch();
2431 }
2432
aa36c2cf 2433 if (!fdisk_has_label(cf->cxt) || cf->zero_start) {
7aa0d529
KZ
2434 rc = ui_create_label(cf);
2435 if (rc < 0)
2436 ui_errx(EXIT_FAILURE,
2437 _("failed to create a new disklabel"));
2438 if (rc)
2439 return rc;
2440 }
2441
2442 cols_init(cf);
2443 rc = lines_refresh(cf);
2444 if (rc)
2445 ui_errx(EXIT_FAILURE, _("failed to read partitions"));
6dbe3af9 2446
3b411726 2447 menu_push(cf, main_menuitems);
63128bbf
KZ
2448 cf->menu->ignore_cb = main_menu_ignore_keys;
2449
8c3a5a44
KZ
2450 rc = ui_refresh(cf);
2451 if (rc)
2452 return rc;
6dbe3af9 2453
dda7fe12 2454 cf->show_extra = 1;
3542fc51 2455 ui_draw_extra(cf);
dda7fe12 2456
6a632136 2457 if (fdisk_is_readonly(cf->cxt))
62eea9ce 2458 ui_warnx(_("Device is open in read-only mode."));
f9fb8290 2459 else if (cf->wrong_order)
a2d0dbb4 2460 ui_info(_("Note that partition table entries are not in disk order now."));
e146ae4e 2461
8c3a5a44 2462 do {
7ee26cbf 2463 int key = getch();
6dbe3af9 2464
7ee26cbf 2465 rc = 0;
7556c944
KZ
2466 if (ui_resize)
2467 /* Note that ncurses getch() returns ERR when interrupted
2468 * by signal, but SLang does not interrupt at all. */
2469 ui_resize_refresh(cf);
2470 if (key == ERR)
2471 continue;
ac27ea5c
KZ
2472 if (ui_menu_move(cf, key) == 0)
2473 continue;
2474
14620822 2475 DBG(UI, ul_debug("main action key >%c<.", key));
f019ce1f 2476
8c3a5a44
KZ
2477 switch (key) {
2478 case KEY_DOWN:
2479 case '\016': /* ^N */
2480 case 'j': /* Vi-like alternative */
2481 ui_table_goto(cf, cf->lines_idx + 1);
2482 break;
2483 case KEY_UP:
2484 case '\020': /* ^P */
2485 case 'k': /* Vi-like alternative */
f019ce1f 2486 ui_table_goto(cf, (int) cf->lines_idx - 1);
8c3a5a44 2487 break;
f019ce1f
KZ
2488 case KEY_PPAGE:
2489 if (cf->page_sz) {
2490 ui_table_goto(cf, (int) cf->lines_idx - cf->page_sz);
2491 break;
2492 }
8c3a5a44
KZ
2493 case KEY_HOME:
2494 ui_table_goto(cf, 0);
2495 break;
f019ce1f
KZ
2496 case KEY_NPAGE:
2497 if (cf->page_sz) {
2498 ui_table_goto(cf, cf->lines_idx + cf->page_sz);
2499 break;
2500 }
8c3a5a44 2501 case KEY_END:
f019ce1f 2502 ui_table_goto(cf, (int) cf->nlines - 1);
8c3a5a44 2503 break;
8c3a5a44
KZ
2504 case KEY_ENTER:
2505 case '\n':
2506 case '\r':
ed8a13e6 2507 rc = main_menu_action(cf, 0);
8c3a5a44 2508 break;
80351b1f 2509 case 'X':
3542fc51 2510 case 'x': /* Extra */
a2d0dbb4 2511 toggle_show_extra(cf);
3542fc51 2512 break;
8c3a5a44 2513 default:
ed8a13e6 2514 rc = main_menu_action(cf, key);
8460875d 2515 if (rc < 0)
8c3a5a44
KZ
2516 beep();
2517 break;
2518 }
8460875d
KZ
2519
2520 if (rc == 1)
2521 break; /* quit */
8c3a5a44 2522 } while (1);
6dbe3af9 2523
8c3a5a44 2524 menu_pop(cf);
6dbe3af9 2525
14620822 2526 DBG(UI, ul_debug("end"));
8c3a5a44
KZ
2527 return 0;
2528}
6dbe3af9 2529
04915c3f
KZ
2530static void __attribute__ ((__noreturn__)) usage(FILE *out)
2531{
2532 fputs(USAGE_HEADER, out);
04915c3f
KZ
2533 fprintf(out,
2534 _(" %1$s [options] <disk>\n"), program_invocation_short_name);
2535
451dbcfa
BS
2536 fputs(USAGE_SEPARATOR, out);
2537 fputs(_("Display or manipulate a disk partition table.\n"), out);
2538
04915c3f 2539 fputs(USAGE_OPTIONS, out);
5d51dc2a
KZ
2540 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out);
2541 fprintf(out,
2542 " %s\n", USAGE_COLORS_DEFAULT);
2543 fputs(_(" -z, --zero start with zeroed partition table\n"), out);
04915c3f
KZ
2544
2545 fputs(USAGE_SEPARATOR, out);
2546 fputs(USAGE_HELP, out);
2547 fputs(USAGE_VERSION, out);
2548
2549 fprintf(out, USAGE_MAN_TAIL("cfdisk(8)"));
2550 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
2551}
2552
8c3a5a44
KZ
2553int main(int argc, char *argv[])
2554{
6e51ab0c 2555 const char *diskpath = NULL;
ad9fe47e 2556 int rc, c, colormode = UL_COLORMODE_UNDEF;
8c3a5a44
KZ
2557 struct cfdisk _cf = { .lines_idx = 0 },
2558 *cf = &_cf;
6dbe3af9 2559
04915c3f
KZ
2560 static const struct option longopts[] = {
2561 { "color", optional_argument, NULL, 'L' },
2562 { "help", no_argument, NULL, 'h' },
2563 { "version", no_argument, NULL, 'V' },
d121efdb 2564 { "zero", no_argument, NULL, 'z' },
04915c3f
KZ
2565 { NULL, 0, 0, 0 },
2566 };
2567
8c3a5a44
KZ
2568 setlocale(LC_ALL, "");
2569 bindtextdomain(PACKAGE, LOCALEDIR);
2570 textdomain(PACKAGE);
2571 atexit(close_stdout);
6dbe3af9 2572
d121efdb 2573 while((c = getopt_long(argc, argv, "L::hVz", longopts, NULL)) != -1) {
04915c3f
KZ
2574 switch(c) {
2575 case 'h':
2576 usage(stdout);
2577 break;
2578 case 'L':
2579 colormode = UL_COLORMODE_AUTO;
2580 if (optarg)
2581 colormode = colormode_or_err(optarg,
2582 _("unsupported color mode"));
2583 break;
2584 case 'V':
f6277500 2585 printf(UTIL_LINUX_VERSION);
04915c3f 2586 return EXIT_SUCCESS;
d121efdb
KZ
2587 case 'z':
2588 cf->zero_start = 1;
2589 break;
04915c3f
KZ
2590 }
2591 }
2592
210bb492 2593 colors_init(colormode, "cfdisk");
04915c3f 2594
8c3a5a44 2595 fdisk_init_debug(0);
710ed55d 2596 scols_init_debug(0);
14620822 2597 cfdisk_init_debug();
8c3a5a44
KZ
2598 cf->cxt = fdisk_new_context();
2599 if (!cf->cxt)
2600 err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
6dbe3af9 2601
6a632136 2602 fdisk_set_ask(cf->cxt, ask_callback, (void *) cf);
6dbe3af9 2603
6e51ab0c
KZ
2604 if (optind == argc) {
2605 size_t i;
2606
2607 for (i = 0; i < ARRAY_SIZE(default_disks); i++) {
2608 if (access(default_disks[i], F_OK) == 0) {
2609 diskpath = default_disks[i];
2610 break;
2611 }
2612 }
2613 if (!diskpath)
2614 diskpath = default_disks[0]; /* default, used for "cannot open" */
2615 } else
a4f0a42d 2616 diskpath = argv[optind];
6dbe3af9 2617
6a632136 2618 rc = fdisk_assign_device(cf->cxt, diskpath, 0);
e146ae4e 2619 if (rc == -EACCES)
6a632136 2620 rc = fdisk_assign_device(cf->cxt, diskpath, 1);
e146ae4e 2621 if (rc != 0)
6e51ab0c 2622 err(EXIT_FAILURE, _("cannot open %s"), diskpath);
6dbe3af9 2623
8c3a5a44
KZ
2624 /* Don't use err(), warn() from this point */
2625 ui_init(cf);
2626 ui_run(cf);
7aa0d529 2627 ui_end();
6dbe3af9 2628
dda7fe12 2629 cfdisk_free_lines(cf);
8c3a5a44 2630 free(cf->linesbuf);
ed8a13e6 2631
dda7fe12
OO
2632 fdisk_unref_table(cf->table);
2633#ifdef HAVE_LIBMOUNT
2634 mnt_unref_table(cf->fstab);
2635 mnt_unref_table(cf->mtab);
2636 mnt_unref_cache(cf->mntcache);
2637#endif
6a632136 2638 rc = fdisk_deassign_device(cf->cxt, cf->nwrites == 0);
c7119037 2639 fdisk_unref_context(cf->cxt);
14620822 2640 DBG(MISC, ul_debug("bye! [rc=%d]", rc));
ed8a13e6 2641 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
6dbe3af9 2642}