7 #include <libsmartcols.h>
11 #elif defined(HAVE_SLANG_SLANG_H)
12 # include <slang/slang.h>
15 #ifdef HAVE_SLCURSES_H
16 # include <slcurses.h>
17 #elif defined(HAVE_SLANG_SLCURSES_H)
18 # include <slang/slcurses.h>
19 #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
20 # include <ncursesw/ncurses.h>
21 #elif defined(HAVE_NCURSES_H)
23 #elif defined(HAVE_NCURSES_NCURSES_H)
24 # include <ncurses/ncurses.h>
33 #include "closestream.h"
42 #define ARROW_CURSOR_STRING ">> "
43 #define ARROW_CURSOR_DUMMY " "
44 #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
46 #define MENU_PADDING 2
47 #define TABLE_START_LINE 4
48 #define MENU_START_LINE ((size_t) LINES - 5)
49 #define INFO_LINE ((size_t) LINES - 2)
50 #define HINT_LINE ((size_t) LINES - 1)
52 #define CFDISK_ERR_ESC 5000
55 # define KEY_ESC '\033'
58 # define KEY_DELETE '\177'
67 static const int color_pairs
[][2] = {
68 /* color foreground, background */
69 [CFDISK_CL_WARNING
] = { COLOR_RED
, -1 },
70 [CFDISK_CL_FREESPACE
] = { COLOR_GREEN
, -1 }
75 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
);
76 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
, int key
, size_t *idx
);
77 static struct cfdisk_menu
*menu_push(struct cfdisk
*cf
, struct cfdisk_menuitem
*item
);
78 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
);
80 static int ui_refresh(struct cfdisk
*cf
);
81 static void ui_warnx(const char *fmt
, ...);
82 static void ui_warn(const char *fmt
, ...);
83 static void ui_info(const char *fmt
, ...);
84 static void ui_draw_menu(struct cfdisk
*cf
);
85 static int ui_menu_move(struct cfdisk
*cf
, int key
);
87 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
88 uintmax_t low
, uintmax_t up
);
90 static int ui_enabled
;
93 struct cfdisk_menuitem
{
94 int key
; /* keyboard shortcut */
95 const char *name
; /* item name */
96 const char *desc
; /* item description (hint) */
102 char *title
; /* optional menu title */
103 struct cfdisk_menuitem
*items
; /* array with menu items */
104 char *ignore
;/* string with keys to ignore */
105 size_t width
; /* maximal width of the menu item */
106 size_t nitems
; /* number of the active menu items */
107 size_t page_sz
;/* when menu longer than screen */
108 size_t idx
; /* the current menu item */
109 struct cfdisk_menu
*prev
;
111 /* @ignore keys generator */
112 int (*ignore_cb
) (struct cfdisk
*, char *, size_t);
114 unsigned int vertical
: 1; /* enable vertical mode */
118 static struct cfdisk_menuitem main_menuitems
[] = {
119 { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
120 { 'd', N_("Delete"), N_("Delete the current partition") },
121 { 'n', N_("New"), N_("Create new partition from free space") },
122 { 'q', N_("Quit"), N_("Quit program without writing partition table") },
123 { 't', N_("Type"), N_("Change the partition type") },
124 { 'h', N_("Help"), N_("Print help screen") },
125 { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
129 /* top level control struct */
131 struct fdisk_context
*cxt
; /* libfdisk context */
132 struct fdisk_table
*table
; /* partition table */
133 struct cfdisk_menu
*menu
; /* the current menu */
135 int *cols
; /* output columns */
136 size_t ncols
; /* number of columns */
138 char *linesbuf
; /* table as string */
139 size_t linesbufsz
; /* size of the tb_buf */
141 char **lines
; /* array with lines */
142 size_t nlines
; /* number of lines */
143 size_t lines_idx
; /* current line <0..N>, exclude header */
147 /* Initialize output columns -- we follow libcfdisk columns (usually specific
150 static int cols_init(struct cfdisk
*cf
)
158 return fdisk_get_columns(cf
->cxt
, 0, &cf
->cols
, &cf
->ncols
);
161 /* Reads partition in tree-like order from scols
163 static int partition_from_scols(struct fdisk_table
*tb
,
164 struct libscols_line
*ln
)
166 struct fdisk_partition
*pa
= scols_line_get_userdata(ln
);
168 fdisk_table_add_partition(tb
, pa
);
169 fdisk_unref_partition(pa
);
171 if (scols_line_has_children(ln
)) {
172 struct libscols_line
*chln
;
173 struct libscols_iter
*itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
177 while (scols_line_next_child(ln
, itr
, &chln
) == 0)
178 partition_from_scols(tb
, chln
);
183 /* It would be possible to use fdisk_table_to_string(), but we want some
184 * extension to the output format, so let's do it without libfdisk
186 static char *table_to_string(struct cfdisk
*cf
, struct fdisk_table
*tb
)
188 const struct fdisk_column
*col
;
189 struct fdisk_partition
*pa
;
190 struct fdisk_label
*lb
;
191 struct fdisk_iter
*itr
= NULL
;
192 struct libscols_table
*table
= NULL
;
193 struct libscols_iter
*s_itr
= NULL
;
197 struct libscols_line
*ln
, *ln_cont
= NULL
;
199 DBG(FRONTEND
, ul_debug("table: convert to string"));
206 lb
= fdisk_context_get_label(cf
->cxt
, NULL
);
209 itr
= fdisk_new_iter(FDISK_ITER_FORWARD
);
213 /* get container (e.g. extended partition) */
214 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
215 if (fdisk_partition_is_nested(pa
)) {
216 DBG(FRONTEND
, ul_debug("table: nested detected, using tree"));
217 tree
= SCOLS_FL_TREE
;
222 table
= scols_new_table(NULL
);
225 scols_table_set_max(table
, 1);
226 scols_table_set_tree(table
, tree
);
229 for (i
= 0; i
< cf
->ncols
; i
++) {
230 col
= fdisk_label_get_column(lb
, cf
->cols
[i
]);
232 int fl
= col
->scols_flags
;
233 if (tree
&& col
->id
== FDISK_COL_DEVICE
)
235 if (!scols_table_new_column(table
, col
->name
, col
->width
, fl
))
241 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
243 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0) {
244 struct libscols_line
*parent
= fdisk_partition_is_nested(pa
) ? ln_cont
: NULL
;
246 ln
= scols_table_new_line(table
, parent
);
249 for (i
= 0; i
< cf
->ncols
; i
++) {
251 col
= fdisk_label_get_column(lb
, cf
->cols
[i
]);
254 if (fdisk_partition_to_string(pa
, cf
->cxt
, col
->id
, &cdata
))
256 scols_line_refer_data(ln
, i
, cdata
);
258 if (tree
&& fdisk_partition_is_container(pa
))
261 scols_line_set_userdata(ln
, (void *) pa
);
262 fdisk_ref_partition(pa
);
265 if (scols_table_is_empty(table
))
268 scols_table_reduce_termwidth(table
, ARROW_CURSOR_WIDTH
);
269 scols_print_table_to_string(table
, &res
);
271 /* scols_* code might reorder lines, let's reorder @tb according to the
272 * final output (it's no problem because partitions are addressed by
273 * parno stored within struct fdisk_partition) */
276 fdisk_reset_iter(itr
, FDISK_ITER_FORWARD
);
277 while (fdisk_table_next_partition(tb
, itr
, &pa
) == 0)
278 fdisk_table_remove_partition(tb
, pa
);
280 s_itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
284 /* add all in the right order (don't forget the output is tree) */
285 while (scols_table_next_line(table
, s_itr
, &ln
) == 0) {
286 if (scols_line_get_parent(ln
))
288 if (partition_from_scols(tb
, ln
))
292 scols_unref_table(table
);
293 scols_free_iter(s_itr
);
294 fdisk_free_iter(itr
);
300 * Read data about partitions from libfdisk and prepare output lines.
302 static int lines_refresh(struct cfdisk
*cf
)
310 DBG(FRONTEND
, ul_debug("refreshing buffer"));
319 fdisk_unref_table(cf
->table
);
322 /* read partitions and free spaces into cf->table */
323 rc
= fdisk_get_partitions(cf
->cxt
, &cf
->table
);
325 rc
= fdisk_get_freespaces(cf
->cxt
, &cf
->table
);
329 cf
->linesbuf
= table_to_string(cf
, cf
->table
);
333 cf
->linesbufsz
= strlen(cf
->linesbuf
);
334 cf
->nlines
= fdisk_table_get_nents(cf
->table
) + 1; /* 1 for header line */
337 if (MENU_START_LINE
- TABLE_START_LINE
< cf
->nlines
)
338 cf
->page_sz
= MENU_START_LINE
- TABLE_START_LINE
- 1;
340 cf
->lines
= xcalloc(cf
->nlines
, sizeof(char *));
342 for (p
= cf
->linesbuf
, i
= 0; p
&& i
< cf
->nlines
; i
++) {
354 static struct fdisk_partition
*get_current_partition(struct cfdisk
*cf
)
359 return fdisk_table_get_partition(cf
->table
, cf
->lines_idx
);
362 static int is_freespace(struct cfdisk
*cf
, size_t i
)
364 struct fdisk_partition
*pa
;
369 pa
= fdisk_table_get_partition(cf
->table
, i
);
370 return fdisk_partition_is_freespace(pa
);
373 /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
374 * responseback to libfdisk
376 static int ask_menu(struct fdisk_ask
*ask
, struct cfdisk
*cf
)
378 struct cfdisk_menuitem
*d
, *cm
;
380 size_t i
= 0, nitems
;
381 const char *name
, *desc
;
386 /* create cfdisk menu according to libfdisk ask-menu, note that the
387 * last cm[] item has to be empty -- so nitems + 1 */
388 nitems
= fdisk_ask_menu_get_nitems(ask
);
389 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
391 for (i
= 0; i
< nitems
; i
++) {
392 if (fdisk_ask_menu_get_item(ask
, i
, &key
, &name
, &desc
))
399 /* make the new menu active */
408 if (ui_menu_move(cf
, key
) == 0)
415 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
417 fdisk_ask_menu_set_result(ask
, d
->key
);
431 static int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
432 void *data
__attribute__((__unused__
)))
439 switch(fdisk_ask_get_type(ask
)) {
440 case FDISK_ASKTYPE_INFO
:
441 ui_info(fdisk_ask_print_get_mesg(ask
));
443 case FDISK_ASKTYPE_WARNX
:
444 ui_warnx(fdisk_ask_print_get_mesg(ask
));
446 case FDISK_ASKTYPE_WARN
:
447 ui_warn(fdisk_ask_print_get_mesg(ask
));
449 case FDISK_ASKTYPE_MENU
:
450 ask_menu(ask
, (struct cfdisk
*) data
);
453 ui_warnx(_("internal error: unsupported dialog type %d"),
454 fdisk_ask_get_type(ask
));
460 static int ui_end(void)
465 #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
466 SLsmg_gotorc(LINES
- 1, 0);
469 mvcur(0, COLS
- 1, LINES
-1, 0);
478 static void ui_vprint_center(int line
, int attrs
, const char *fmt
, va_list ap
)
486 xvasprintf(&buf
, fmt
, ap
);
488 width
= mbs_safe_width(buf
);
489 if (width
> (size_t) COLS
) {
490 char *p
= strrchr(buf
+ COLS
, ' ');
494 if (line
+ 1 >= LINES
)
497 mvaddstr(line
, 0, buf
);
498 mvaddstr(line
+ 1, 0, p
+1);
502 mvaddstr(line
, (COLS
- width
) / 2, buf
);
508 static void ui_center(int line
, const char *fmt
, ...)
512 ui_vprint_center(line
, 0, fmt
, ap
);
516 static void ui_warnx(const char *fmt
, ...)
521 ui_vprint_center(INFO_LINE
,
522 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
525 vfprintf(stderr
, fmt
, ap
);
529 static void ui_warn(const char *fmt
, ...)
534 xasprintf(&fmt_m
, "%s: %m", fmt
);
538 ui_vprint_center(INFO_LINE
,
539 colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING
) : 0,
542 vfprintf(stderr
, fmt_m
, ap
);
547 static int __attribute__((__noreturn__
)) ui_errx(int rc
, const char *fmt
, ...)
553 fprintf(stderr
, "%s: ", program_invocation_short_name
);
554 vfprintf(stderr
, fmt
, ap
);
560 static void ui_info(const char *fmt
, ...)
565 ui_vprint_center(INFO_LINE
, A_BOLD
, fmt
, ap
);
567 vfprintf(stdout
, fmt
, ap
);
571 static void ui_clean_info(void)
577 static void ui_hint(const char *fmt
, ...)
582 ui_vprint_center(HINT_LINE
, A_BOLD
, fmt
, ap
);
584 vfprintf(stdout
, fmt
, ap
);
588 static void ui_clean_hint(void)
594 static void die_on_signal(int dummy
__attribute__((__unused__
)))
596 DBG(FRONTEND
, ul_debug("die on signal."));
601 static void menu_update_ignore(struct cfdisk
*cf
)
603 char ignore
[128] = { 0 };
605 struct cfdisk_menu
*m
;
606 struct cfdisk_menuitem
*d
, *org
;
611 assert(cf
->menu
->ignore_cb
);
614 org
= menu_get_menuitem(cf
, m
->idx
);
616 DBG(FRONTEND
, ul_debug("menu: update menu ignored keys"));
618 i
= m
->ignore_cb(cf
, ignore
, sizeof(ignore
));
621 /* return if no change */
622 if ( (!m
->ignore
&& !*ignore
)
623 || (m
->ignore
&& *ignore
&& strcmp(m
->ignore
, ignore
) == 0)) {
628 m
->ignore
= xstrdup(ignore
);
631 for (d
= m
->items
; d
->name
; d
++) {
632 if (m
->ignore
&& strchr(m
->ignore
, d
->key
))
637 /* refresh menu index to be at the same menuitem or go to the first */
638 if (org
&& menu_get_menuitem_by_key(cf
, org
->key
, &idx
))
643 m
->page_sz
= m
->nitems
/ (LINES
- 4) ? LINES
- 4 : 0;
646 static struct cfdisk_menu
*menu_push(
648 struct cfdisk_menuitem
*items
)
650 struct cfdisk_menu
*m
= xcalloc(1, sizeof(*m
));
651 struct cfdisk_menuitem
*d
;
655 DBG(FRONTEND
, ul_debug("menu: new menu"));
660 for (d
= m
->items
; d
->name
; d
++) {
661 const char *name
= _(d
->name
);
662 size_t len
= mbs_safe_width(name
);
669 m
->page_sz
= m
->nitems
/ (LINES
- 4) ? LINES
- 4 : 0;
673 static struct cfdisk_menu
*menu_pop(struct cfdisk
*cf
)
675 struct cfdisk_menu
*m
= NULL
;
679 DBG(FRONTEND
, ul_debug("menu: rem menu"));
683 free(cf
->menu
->ignore
);
684 free(cf
->menu
->title
);
691 static void menu_set_title(struct cfdisk_menu
*m
, const char *title
)
696 size_t len
= mbs_safe_width(title
);
697 if (len
+ 3 > m
->width
)
699 str
= xstrdup(title
);
705 static int ui_init(struct cfdisk
*cf
__attribute__((__unused__
)))
709 DBG(FRONTEND
, ul_debug("ui: init"));
711 /* setup SIGCHLD handler */
712 sigemptyset(&sa
.sa_mask
);
714 sa
.sa_handler
= die_on_signal
;
715 sigaction(SIGINT
, &sa
, NULL
);
716 sigaction(SIGTERM
, &sa
, NULL
);
721 #ifdef HAVE_USE_DEFAULT_COLORS
722 if (colors_wanted() && has_colors()) {
726 use_default_colors();
727 for (i
= 1; i
< ARRAY_SIZE(color_pairs
); i
++) /* yeah, start from 1! */
728 init_pair(i
, color_pairs
[i
][0], color_pairs
[i
][1]);
731 colors_init(UL_COLORMODE_NEVER
, "cfdisk");
738 keypad(stdscr
, TRUE
);
743 static size_t menuitem_get_line(struct cfdisk
*cf
, size_t idx
)
745 struct cfdisk_menu
*m
= cf
->menu
;
748 if (!m
->page_sz
) /* small menu */
749 return (LINES
- (cf
->menu
->nitems
+ 1)) / 2 + idx
;
750 return (idx
% m
->page_sz
) + 1;
752 size_t len
= m
->width
+ 4 + MENU_PADDING
; /* item width */
753 size_t items
= COLS
/ len
; /* items per line */
755 return MENU_START_LINE
+ ((idx
/ items
));
759 static int menuitem_get_column(struct cfdisk
*cf
, size_t idx
)
761 if (cf
->menu
->vertical
) {
762 size_t nc
= cf
->menu
->width
+ MENU_PADDING
;
763 if ((size_t) COLS
<= nc
)
765 return (COLS
- nc
) / 2;
767 size_t len
= cf
->menu
->width
+ 4 + MENU_PADDING
; /* item width */
768 size_t items
= COLS
/ len
; /* items per line */
769 size_t extra
= items
< cf
->menu
->nitems
? /* extra space on line */
770 COLS
% len
: /* - multi-line menu */
771 COLS
- (cf
->menu
->nitems
* len
); /* - one line menu */
773 extra
+= MENU_PADDING
; /* add padding after last item to extra */
776 return (idx
* len
) + (extra
/ 2);
777 return ((idx
% items
) * len
) + (extra
/ 2);
781 static int menuitem_on_page(struct cfdisk
*cf
, size_t idx
)
783 struct cfdisk_menu
*m
= cf
->menu
;
785 if (m
->page_sz
== 0 ||
786 m
->idx
/ m
->page_sz
== idx
/ m
->page_sz
)
791 static struct cfdisk_menuitem
*menu_get_menuitem(struct cfdisk
*cf
, size_t idx
)
793 struct cfdisk_menuitem
*d
;
796 for (i
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
797 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
806 static struct cfdisk_menuitem
*menu_get_menuitem_by_key(struct cfdisk
*cf
,
807 int key
, size_t *idx
)
809 struct cfdisk_menuitem
*d
;
811 for (*idx
= 0, d
= cf
->menu
->items
; d
->name
; d
++) {
812 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, d
->key
))
822 static void ui_draw_menuitem(struct cfdisk
*cf
,
823 struct cfdisk_menuitem
*d
,
826 char buf
[80 * MB_CUR_MAX
];
828 size_t width
= cf
->menu
->width
+ 2; /* 2 = blank around string */
829 int ln
, cl
, vert
= cf
->menu
->vertical
;
831 if (!menuitem_on_page(cf
, idx
))
832 return; /* no visible item */
833 ln
= menuitem_get_line(cf
, idx
);
834 cl
= menuitem_get_column(cf
, idx
);
837 mbsalign(name
, buf
, sizeof(buf
), &width
,
838 vert
? MBS_ALIGN_LEFT
: MBS_ALIGN_CENTER
,
841 DBG(FRONTEND
, ul_debug("ui: menuitem: cl=%d, ln=%d, item='%s'",
845 mvaddch(ln
, cl
- 1, ACS_VLINE
);
846 mvaddch(ln
, cl
+ cf
->menu
->width
+ 4, ACS_VLINE
);
849 if (cf
->menu
->idx
== idx
) {
851 mvprintw(ln
, cl
, vert
? " %s " : "[%s]", buf
);
856 mvprintw(ln
, cl
, vert
? " %s " : "[%s]", buf
);
859 static void ui_draw_menu(struct cfdisk
*cf
)
861 struct cfdisk_menuitem
*d
;
862 struct cfdisk_menu
*m
;
864 size_t ln
= menuitem_get_line(cf
, 0);
870 DBG(FRONTEND
, ul_debug("ui: menu: draw start"));
875 nlines
= m
->page_sz
? m
->page_sz
: m
->nitems
;
877 nlines
= menuitem_get_line(cf
, m
->nitems
);
879 for (i
= ln
; i
<= ln
+ nlines
; i
++) {
885 menu_update_ignore(cf
);
887 while ((d
= menu_get_menuitem(cf
, i
)))
888 ui_draw_menuitem(cf
, d
, i
++);
891 size_t cl
= menuitem_get_column(cf
, 0);
892 size_t curpg
= m
->page_sz
? m
->idx
/ m
->page_sz
: 0;
894 /* corners and horizontal lines */
895 mvaddch(ln
- 1, cl
- 1, ACS_ULCORNER
);
896 mvaddch(ln
+ nlines
, cl
- 1, ACS_LLCORNER
);
898 for (i
= 0; i
< m
->width
+ 4; i
++) {
899 mvaddch(ln
- 1, cl
+ i
, ACS_HLINE
);
900 mvaddch(ln
+ nlines
, cl
+ i
, ACS_HLINE
);
903 mvaddch(ln
- 1, cl
+ i
, ACS_URCORNER
);
904 mvaddch(ln
+ nlines
, cl
+ i
, ACS_LRCORNER
);
906 /* draw also lines around empty lines on last page */
908 m
->nitems
/ m
->page_sz
== m
->idx
/ m
->page_sz
) {
909 for (i
= m
->nitems
% m
->page_sz
+ 1; i
<= m
->page_sz
; i
++) {
910 mvaddch(i
, cl
- 1, ACS_VLINE
);
911 mvaddch(i
, cl
+ cf
->menu
->width
+ 4, ACS_VLINE
);
916 mvprintw(ln
- 1, cl
, " %s ", m
->title
);
920 mvaddch(ln
- 1, cl
+ m
->width
+ 3, ACS_UARROW
);
921 if (m
->page_sz
&& curpg
< m
->nitems
/ m
->page_sz
)
922 mvaddch(ln
+ nlines
, cl
+ m
->width
+ 3, ACS_DARROW
);
925 DBG(FRONTEND
, ul_debug("ui: menu: draw end."));
928 static void ui_menu_goto(struct cfdisk
*cf
, int where
)
930 struct cfdisk_menuitem
*d
;
933 /* stop and begin/end for vertical menus */
934 if (cf
->menu
->vertical
) {
937 else if (where
> (int) cf
->menu
->nitems
- 1)
938 where
= cf
->menu
->nitems
- 1;
940 /* continue from begin/end */
942 where
= cf
->menu
->nitems
- 1;
943 else if ((size_t) where
> cf
->menu
->nitems
- 1)
946 if ((size_t) where
== cf
->menu
->idx
)
952 cf
->menu
->idx
= where
;
954 if (!menuitem_on_page(cf
, old
)) {
959 d
= menu_get_menuitem(cf
, old
);
960 ui_draw_menuitem(cf
, d
, old
);
962 d
= menu_get_menuitem(cf
, where
);
963 ui_draw_menuitem(cf
, d
, where
);
966 static int ui_menu_move(struct cfdisk
*cf
, int key
)
968 struct cfdisk_menu
*m
;
975 DBG(FRONTEND
, ul_debug("ui: menu move key >%c<.", key
));
981 case '\016': /* ^N */
982 case 'j': /* Vi-like alternative */
983 ui_menu_goto(cf
, m
->idx
+ 1);
986 case '\020': /* ^P */
987 case 'k': /* Vi-like alternative */
988 ui_menu_goto(cf
, (int) m
->idx
- 1);
992 ui_menu_goto(cf
, (int) m
->idx
- m
->page_sz
);
1000 ui_menu_goto(cf
, m
->idx
+ m
->page_sz
);
1004 ui_menu_goto(cf
, m
->nitems
);
1011 ui_menu_goto(cf
, m
->idx
+ 1);
1017 ui_menu_goto(cf
, (int) m
->idx
- 1);
1022 return 1; /* key irrelevant for menu move */
1025 static int partition_on_page(struct cfdisk
*cf
, size_t i
)
1027 if (cf
->page_sz
== 0 ||
1028 cf
->lines_idx
/ cf
->page_sz
== i
/ cf
->page_sz
)
1033 static void ui_draw_partition(struct cfdisk
*cf
, size_t i
)
1035 int ln
= TABLE_START_LINE
+ 1 + i
; /* skip table header */
1036 int cl
= ARROW_CURSOR_WIDTH
; /* we need extra space for cursor */
1037 int cur
= cf
->lines_idx
== i
;
1041 if (!partition_on_page(cf
, i
))
1043 ln
= TABLE_START_LINE
+ (i
% cf
->page_sz
) + 1;
1044 curpg
= cf
->lines_idx
/ cf
->page_sz
;
1047 DBG(FRONTEND
, ul_debug(
1048 "ui: draw partition %zu [page_sz=%zu, "
1049 "line=%d, idx=%zu]",
1050 i
, cf
->page_sz
, ln
, cf
->lines_idx
));
1054 mvaddstr(ln
, 0, ARROW_CURSOR_STRING
);
1055 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1060 if (colors_wanted() && is_freespace(cf
, i
)) {
1061 attron(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1064 mvaddstr(ln
, 0, ARROW_CURSOR_DUMMY
);
1065 mvaddstr(ln
, cl
, cf
->lines
[i
+ 1]);
1067 attroff(COLOR_PAIR(CFDISK_CL_FREESPACE
));
1070 if ((size_t) ln
== MENU_START_LINE
- 1 &&
1071 cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1074 mvaddch(ln
, COLS
- 1, ACS_DARROW
);
1075 mvaddch(ln
, 0, ACS_DARROW
);
1081 static int ui_draw_table(struct cfdisk
*cf
)
1083 int cl
= ARROW_CURSOR_WIDTH
;
1084 size_t i
, nparts
= fdisk_table_get_nents(cf
->table
);
1085 size_t curpg
= cf
->page_sz
? cf
->lines_idx
/ cf
->page_sz
: 0;
1087 DBG(FRONTEND
, ul_debug("ui: draw table"));
1089 for (i
= TABLE_START_LINE
; i
<= TABLE_START_LINE
+ cf
->page_sz
; i
++) {
1094 if ((size_t) cf
->lines_idx
> nparts
- 1)
1095 cf
->lines_idx
= nparts
? nparts
- 1 : 0;
1099 mvaddstr(TABLE_START_LINE
, cl
, cf
->lines
[0]);
1102 /* print partitions */
1103 for (i
= 0; i
< nparts
; i
++)
1104 ui_draw_partition(cf
, i
);
1107 mvaddch(TABLE_START_LINE
, COLS
- 1, ACS_UARROW
);
1108 mvaddch(TABLE_START_LINE
, 0, ACS_UARROW
);
1110 if (cf
->page_sz
&& curpg
< cf
->nlines
/ cf
->page_sz
) {
1111 mvaddch(MENU_START_LINE
- 1, COLS
- 1, ACS_DARROW
);
1112 mvaddch(MENU_START_LINE
- 1, 0, ACS_DARROW
);
1117 static int ui_table_goto(struct cfdisk
*cf
, int where
)
1120 size_t nparts
= fdisk_table_get_nents(cf
->table
);
1122 DBG(FRONTEND
, ul_debug("ui: goto table %d", where
));
1126 else if ((size_t) where
> nparts
- 1)
1129 if ((size_t) where
== cf
->lines_idx
)
1132 old
= cf
->lines_idx
;
1133 cf
->lines_idx
= where
;
1135 if (!partition_on_page(cf
, old
) ||!partition_on_page(cf
, where
))
1138 ui_draw_partition(cf
, old
); /* cleanup old */
1139 ui_draw_partition(cf
, where
); /* draw new */
1147 static int ui_refresh(struct cfdisk
*cf
)
1150 uint64_t bytes
= cf
->cxt
->total_sectors
* cf
->cxt
->sector_size
;
1151 char *strsz
= size_to_human_string(SIZE_SUFFIX_SPACE
1152 | SIZE_SUFFIX_3LETTER
, bytes
);
1160 ui_center(0, _("Disk: %s"), cf
->cxt
->dev_path
);
1162 ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
1163 strsz
, bytes
, (uintmax_t) cf
->cxt
->total_sectors
);
1164 if (fdisk_get_disklabel_id(cf
->cxt
, &id
) == 0 && id
)
1165 ui_center(2, _("Label: %s, identifier: %s"),
1166 cf
->cxt
->label
->name
, id
);
1168 ui_center(2, _("Label: %s"), cf
->cxt
->label
->name
);
1177 static ssize_t
ui_get_string(struct cfdisk
*cf
, const char *prompt
,
1178 const char *hint
, char *buf
, size_t len
)
1181 ssize_t i
= 0, rc
= -1;
1183 int ln
= MENU_START_LINE
, cl
= 1;
1193 mvaddstr(ln
, cl
, (char *) prompt
);
1194 cl
+= mbs_safe_width(prompt
);
1200 cells
= mbs_safe_width(buf
);
1201 mvaddstr(ln
, cl
, buf
);
1209 move(ln
, cl
+ cells
);
1214 #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
1215 defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1216 if (get_wch(&c
) == ERR
) {
1218 if ((c
= getch()) == ERR
) {
1220 if (!isatty(STDIN_FILENO
))
1225 if (c
== '\r' || c
== '\n' || c
== KEY_ENTER
)
1230 rc
= -CFDISK_ERR_ESC
;
1237 i
= mbs_truncate(buf
, &cells
);
1240 mvaddch(ln
, cl
+ cells
, ' ');
1241 move(ln
, cl
+ cells
);
1246 #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
1247 if (i
+ 1 < (ssize_t
) len
&& iswprint(c
)) {
1248 wchar_t wc
= (wchar_t) c
;
1249 char s
[MB_CUR_MAX
+ 1];
1250 int sz
= wctomb(s
, wc
);
1252 if (sz
> 0 && sz
+ i
< (ssize_t
) len
) {
1254 mvaddnstr(ln
, cl
+ cells
, s
, sz
);
1255 memcpy(buf
+ i
, s
, sz
);
1258 cells
+= wcwidth(wc
);
1263 if (i
+ 1 < (ssize_t
) len
&& isprint(c
)) {
1264 mvaddch(ln
, cl
+ cells
, c
);
1276 rc
= i
; /* success */
1286 /* @res is default value as well as result in bytes */
1287 static int ui_get_size(struct cfdisk
*cf
, const char *prompt
, uintmax_t *res
,
1288 uintmax_t low
, uintmax_t up
)
1293 char *dflt
= size_to_human_string(0, *res
);
1295 DBG(FRONTEND
, ul_debug("ui: get_size (default=%ju)", *res
));
1300 int pwr
= 0, insec
= 0;
1302 snprintf(buf
, sizeof(buf
), "%s", dflt
);
1303 rc
= ui_get_string(cf
, prompt
,
1304 _("May be followed by {M,B,G,T}iB "
1305 "(the \"iB\" is optional) or S for sectors."),
1308 ui_warnx(_("Please, specify size."));
1309 continue; /* nothing specified */
1310 } else if (rc
== -CFDISK_ERR_ESC
)
1311 break; /* cancel dialog */
1313 if (strcmp(buf
, dflt
) == 0)
1314 user
= *res
, rc
= 0; /* no change, use default */
1316 size_t len
= strlen(buf
);
1317 if (buf
[len
- 1] == 'S') {
1319 buf
[len
- 1] = '\0';
1321 rc
= parse_size(buf
, &user
, &pwr
); /* parse */
1325 DBG(FRONTEND
, ul_debug("ui: get_size user=%ju, power=%d, sectors=%s",
1326 user
, pwr
, insec
? "yes" : "no"));
1328 user
*= cf
->cxt
->sector_size
;
1330 ui_warnx(_("Minimal size is %ju"), low
);
1333 if (user
> up
&& pwr
&& user
< up
+ (1ULL << pwr
* 10))
1334 /* ignore when the user specified size overflow
1335 * with in range specified by suffix (e.g. MiB) */
1339 ui_warnx(_("Maximal size is %ju bytes."), up
);
1343 ui_warnx(_("Failed to parse size."));
1350 DBG(FRONTEND
, ul_debug("ui: get_size (result=%ju, rc=%zd)", *res
, rc
));
1354 static struct fdisk_parttype
*ui_get_parttype(struct cfdisk
*cf
,
1355 struct fdisk_parttype
*cur
)
1357 struct cfdisk_menuitem
*d
, *cm
;
1358 size_t i
= 0, nitems
, idx
= 0;
1359 struct fdisk_parttype
*t
= NULL
;
1360 int has_typestr
= 0;
1362 DBG(FRONTEND
, ul_debug("ui: asking for parttype."));
1364 /* create cfdisk menu according to label types, note that the
1365 * last cm[] item has to be empty -- so nitems + 1 */
1366 nitems
= cf
->cxt
->label
->nparttypes
;
1369 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1373 has_typestr
= cf
->cxt
->label
->parttypes
[0].typestr
&&
1374 *cf
->cxt
->label
->parttypes
[0].typestr
;
1376 for (i
= 0; i
< nitems
; i
++) {
1377 struct fdisk_parttype
*x
= &cf
->cxt
->label
->parttypes
[i
];
1384 xasprintf(&name
, "%2x %s", x
->type
, x
->name
);
1386 name
= (char *) x
->name
;
1387 cm
[i
].desc
= x
->typestr
;
1394 /* make the new menu active */
1396 cf
->menu
->vertical
= 1;
1397 cf
->menu
->idx
= idx
;
1398 menu_set_title(cf
->menu
, _("Select partition type"));
1405 if (ui_menu_move(cf
, key
) == 0)
1412 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1414 t
= (struct fdisk_parttype
*) d
->userdata
;
1425 for (i
= 0; i
< nitems
; i
++)
1426 free((char *) cm
[i
].name
);
1429 DBG(FRONTEND
, ul_debug("ui: get parrtype done [type=%s] ", t
? t
->name
: NULL
));
1433 /* prints menu with libfdisk labels and waits for users response */
1434 static int ui_create_label(struct cfdisk
*cf
)
1436 struct cfdisk_menuitem
*d
, *cm
;
1438 size_t i
= 0, nitems
;
1439 struct fdisk_label
*lb
= NULL
;
1443 DBG(FRONTEND
, ul_debug("ui: asking for new disklabe."));
1445 /* create cfdisk menu according to libfdisk labels, note that the
1446 * last cm[] item has to be empty -- so nitems + 1 */
1447 nitems
= fdisk_context_get_nlabels(cf
->cxt
);
1448 cm
= xcalloc(nitems
+ 1, sizeof(struct cfdisk_menuitem
));
1450 for (i
= 0; i
< nitems
; i
++) {
1451 if (fdisk_context_next_label(cf
->cxt
, &lb
))
1453 cm
[i
].name
= lb
->name
;
1457 ui_center(LINES
- 4,
1458 _("Device does not contain a recognized partition table."));
1459 ui_center(LINES
- 3,
1460 _("Please, select a type to create a new disk label."));
1462 /* make the new menu active */
1464 cf
->menu
->vertical
= 1;
1465 menu_set_title(cf
->menu
, _("Select label type"));
1471 if (ui_menu_move(cf
, key
) == 0)
1477 d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1479 rc
= fdisk_create_disklabel(cf
->cxt
, d
->name
);
1490 DBG(FRONTEND
, ul_debug("ui: create label done [rc=%d] ", rc
));
1494 static int ui_help(void)
1497 static const char *help
[] = {
1498 N_("Help Screen for cfdisk"),
1500 N_("This is cfdisk, a curses based disk partitioning program, which"),
1501 N_("allows you to create, delete and modify partitions on your hard"),
1504 N_("Copyright (C) 2014 Karel Zak <kzak@redhat.com> "),
1505 N_("Based on the original cfdisk from Kevin E. Martin & aeb."),
1507 N_("Command Meaning"),
1508 N_("------- -------"),
1509 N_(" b Toggle bootable flag of the current partition"),
1510 N_(" d Delete the current partition"),
1511 N_(" h Print this screen"),
1512 N_(" n Create new partition from free space"),
1513 N_(" q Quit program without writing partition table"),
1514 N_(" t Change the partition type"),
1515 N_(" W Write partition table to disk (must enter upper case W)"),
1516 N_(" Since this might destroy data on the disk, you must"),
1517 N_(" either confirm or deny the write by entering `yes' or"),
1519 N_("Up Arrow Move cursor to the previous partition"),
1520 N_("Down Arrow Move cursor to the next partition"),
1521 N_("Left Arrow Move cursor to the previous menu item"),
1522 N_("Right Arrow Move cursor to the next menu item"),
1525 N_("Note: All of the commands can be entered with either upper or lower"),
1526 N_("case letters (except for Writes)."),
1528 N_("Use lsblk(8) or partx(8) to see more details about the device.")
1532 for (i
= 0; i
< ARRAY_SIZE(help
); i
++)
1533 mvaddstr(i
, 1, _(help
[i
]));
1535 ui_info(_("Press a key to continue."));
1540 /* TODO: use @sz, now 128bytes */
1541 static int main_menu_ignore_keys(struct cfdisk
*cf
, char *ignore
,
1542 size_t sz
__attribute__((__unused__
)))
1544 struct fdisk_partition
*pa
= get_current_partition(cf
);
1549 if (fdisk_partition_is_freespace(pa
)) {
1550 ignore
[i
++] = 'd'; /* delete */
1551 ignore
[i
++] = 't'; /* set type */
1552 ignore
[i
++] = 'b'; /* set bootable */
1555 if (!fdisk_is_disklabel(cf
->cxt
, DOS
) &&
1556 !fdisk_is_disklabel(cf
->cxt
, SGI
))
1561 if (fdisk_context_is_readonly(cf
->cxt
))
1567 /* returns: error: < 0, success: 0, quit: 1 */
1568 static int main_menu_action(struct cfdisk
*cf
, int key
)
1572 const char *info
= NULL
, *warn
= NULL
;
1573 struct fdisk_partition
*pa
;
1580 struct cfdisk_menuitem
*d
= menu_get_menuitem(cf
, cf
->menu
->idx
);
1585 } else if (key
!= 'w' && key
!= 'W')
1586 key
= tolower(key
); /* case insensitive except 'W'rite */
1588 DBG(FRONTEND
, ul_debug("ui: main menu action: key=%c", key
));
1590 if (cf
->menu
->ignore
&& strchr(cf
->menu
->ignore
, key
)) {
1591 DBG(FRONTEND
, ul_debug(" ignore '%c'", key
));
1595 pa
= get_current_partition(cf
);
1596 n
= fdisk_partition_get_partno(pa
);
1598 DBG(FRONTEND
, ul_debug("menu action on %p", pa
));
1603 case 'b': /* Bootable flag */
1605 int fl
= fdisk_is_disklabel(cf
->cxt
, DOS
) ? DOS_FLAG_ACTIVE
:
1606 fdisk_is_disklabel(cf
->cxt
, SGI
) ? SGI_FLAG_BOOT
: 0;
1608 if (fl
&& fdisk_partition_toggle_flag(cf
->cxt
, n
, fl
))
1609 warn
= _("Could not toggle the flag.");
1617 case 'd': /* Delete */
1618 if (fdisk_delete_partition(cf
->cxt
, n
) != 0)
1619 warn
= _("Could not delete partition %zu.");
1621 info
= _("Partition %zu has been deleted.");
1624 case 'h': /* help */
1630 uint64_t start
, size
, dflt_size
;
1631 struct fdisk_partition
*npa
; /* the new partition */
1633 if (!pa
|| !fdisk_partition_is_freespace(pa
))
1635 npa
= fdisk_new_partition();
1638 /* free space range */
1639 start
= fdisk_partition_get_start(pa
);
1640 size
= dflt_size
= fdisk_partition_get_size(pa
) * cf
->cxt
->sector_size
;
1642 if (ui_get_size(cf
, _("Partition size: "), &size
, 1, size
)
1646 if (dflt_size
== size
) /* default is to fillin all free space */
1647 fdisk_partition_end_follow_default(npa
, 1);
1648 else /* set relative size of the partition */
1649 fdisk_partition_set_size(npa
, size
/ cf
->cxt
->sector_size
);
1651 fdisk_partition_set_start(npa
, start
);
1652 fdisk_partition_partno_follow_default(npa
, 1);
1653 /* add to disk label -- libfdisk will ask for missing details */
1654 rc
= fdisk_add_partition(cf
->cxt
, npa
);
1655 fdisk_unref_partition(npa
);
1660 case 'q': /* Quit */
1662 case 't': /* Type */
1664 struct fdisk_parttype
*t
;
1666 if (!pa
|| fdisk_partition_is_freespace(pa
))
1668 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
1669 t
= ui_get_parttype(cf
, t
);
1672 if (t
&& fdisk_set_partition_type(cf
->cxt
, n
, t
) == 0)
1673 info
= _("Changed type of the partition %zu.");
1675 info
= _("Type of the partition %zu is unchanged.");
1678 case 'W': /* Write */
1680 char buf
[64] = { 0 };
1683 if (fdisk_context_is_readonly(cf
->cxt
)) {
1684 ui_warnx(_("Device open in read-only mode"));
1688 rc
= ui_get_string(cf
,
1689 _("Are you sure you want to write the partition "
1691 _("Type \"yes\" or \"no\" or press ESC to left dialog."),
1695 if (rc
<= 0 || strcasecmp(buf
, "yes") != 0
1696 || strcasecmp(buf
, _("yes")) != 0) {
1697 info
= _("Did not write partition table to disk");
1700 rc
= fdisk_write_disklabel(cf
->cxt
);
1702 warn
= _("Failed to write disklabel");
1704 fdisk_reread_partition_table(cf
->cxt
);
1705 info
= _("The partition table has been altered.");
1720 ui_warnx(warn
, n
+ 1);
1722 ui_info(info
, n
+ 1);
1727 static int ui_run(struct cfdisk
*cf
)
1731 DBG(FRONTEND
, ul_debug("ui: start COLS=%d, LINES=%d", COLS
, LINES
));
1733 if (!fdisk_dev_has_disklabel(cf
->cxt
)) {
1734 rc
= ui_create_label(cf
);
1736 ui_errx(EXIT_FAILURE
,
1737 _("failed to create a new disklabel"));
1743 rc
= lines_refresh(cf
);
1745 ui_errx(EXIT_FAILURE
, _("failed to read partitions"));
1747 menu_push(cf
, main_menuitems
);
1748 cf
->menu
->ignore_cb
= main_menu_ignore_keys
;
1750 rc
= ui_refresh(cf
);
1754 if (fdisk_context_is_readonly(cf
->cxt
))
1755 ui_warnx(_("Device open in read-only mode."));
1758 int rc
= 0, key
= getch();
1760 if (ui_menu_move(cf
, key
) == 0)
1763 DBG(FRONTEND
, ul_debug("ui: main action key >%c<.", key
));
1767 case '\016': /* ^N */
1768 case 'j': /* Vi-like alternative */
1769 ui_table_goto(cf
, cf
->lines_idx
+ 1);
1772 case '\020': /* ^P */
1773 case 'k': /* Vi-like alternative */
1774 ui_table_goto(cf
, (int) cf
->lines_idx
- 1);
1778 ui_table_goto(cf
, (int) cf
->lines_idx
- cf
->page_sz
);
1782 ui_table_goto(cf
, 0);
1786 ui_table_goto(cf
, cf
->lines_idx
+ cf
->page_sz
);
1790 ui_table_goto(cf
, (int) cf
->nlines
- 1);
1795 rc
= main_menu_action(cf
, 0);
1798 rc
= main_menu_action(cf
, key
);
1810 DBG(FRONTEND
, ul_debug("ui: end"));
1815 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
1817 fputs(USAGE_HEADER
, out
);
1820 _(" %1$s [options] <disk>\n"), program_invocation_short_name
);
1822 fputs(USAGE_OPTIONS
, out
);
1823 fputs(_(" -L --color[=<when>] colorize output (auto, always or never)\n"), out
);
1825 fputs(USAGE_SEPARATOR
, out
);
1826 fputs(USAGE_HELP
, out
);
1827 fputs(USAGE_VERSION
, out
);
1829 fprintf(out
, USAGE_MAN_TAIL("cfdisk(8)"));
1830 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1833 int main(int argc
, char *argv
[])
1835 int rc
, c
, colormode
= UL_COLORMODE_UNDEF
;
1836 struct cfdisk _cf
= { .lines_idx
= 0 },
1839 static const struct option longopts
[] = {
1840 { "color", optional_argument
, NULL
, 'L' },
1841 { "help", no_argument
, NULL
, 'h' },
1842 { "version", no_argument
, NULL
, 'V' },
1846 setlocale(LC_ALL
, "");
1847 bindtextdomain(PACKAGE
, LOCALEDIR
);
1848 textdomain(PACKAGE
);
1849 atexit(close_stdout
);
1851 while((c
= getopt_long(argc
, argv
, "L::hV", longopts
, NULL
)) != -1) {
1857 colormode
= UL_COLORMODE_AUTO
;
1859 colormode
= colormode_or_err(optarg
,
1860 _("unsupported color mode"));
1863 printf(_("%s from %s\n"), program_invocation_short_name
,
1865 return EXIT_SUCCESS
;
1871 colors_init(colormode
, "cfdisk");
1873 fdisk_init_debug(0);
1874 cf
->cxt
= fdisk_new_context();
1876 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
1878 fdisk_context_set_ask(cf
->cxt
, ask_callback
, (void *) cf
);
1883 rc
= fdisk_context_assign_device(cf
->cxt
, argv
[optind
], 0);
1885 rc
= fdisk_context_assign_device(cf
->cxt
, argv
[optind
], 1);
1887 err(EXIT_FAILURE
, _("cannot open %s"), argv
[optind
]);
1889 /* Don't use err(), warn() from this point */
1896 fdisk_unref_table(cf
->table
);
1898 rc
= fdisk_context_deassign_device(cf
->cxt
);
1899 fdisk_free_context(cf
->cxt
);
1900 DBG(FRONTEND
, ul_debug("bye! [rc=%d]", rc
));
1901 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;