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