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