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