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