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