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