]>
Commit | Line | Data |
---|---|---|
2ddfd9e4 BS |
1 | /* |
2 | * cfdisk.c - Display or manipulate a disk partition table. | |
3 | * | |
4 | * Copyright (C) 2014 Karel Zak <kzak@redhat.com> | |
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> |
baa3b270 | 21 | #include <libsmartcols.h> |
7556c944 | 22 | #include <sys/ioctl.h> |
541e6934 KZ |
23 | |
24 | #ifdef HAVE_SLANG_H | |
3b411726 | 25 | # include <slang.h> |
541e6934 | 26 | #elif defined(HAVE_SLANG_SLANG_H) |
3b411726 | 27 | # include <slang/slang.h> |
541e6934 KZ |
28 | #endif |
29 | ||
48d7b13a | 30 | #ifdef HAVE_SLCURSES_H |
3b411726 | 31 | # include <slcurses.h> |
48d7b13a | 32 | #elif defined(HAVE_SLANG_SLCURSES_H) |
3b411726 | 33 | # include <slang/slcurses.h> |
30c97bb8 | 34 | #elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR) |
3b411726 | 35 | # include <ncursesw/ncurses.h> |
48d7b13a | 36 | #elif defined(HAVE_NCURSES_H) |
3b411726 | 37 | # include <ncurses.h> |
48d7b13a | 38 | #elif defined(HAVE_NCURSES_NCURSES_H) |
3b411726 | 39 | # include <ncurses/ncurses.h> |
2b6fc908 | 40 | #endif |
541e6934 | 41 | |
5f94ca33 | 42 | #ifdef HAVE_WIDECHAR |
3b411726 | 43 | # include <wctype.h> |
f1512be8 | 44 | # include <wchar.h> |
5f94ca33 KZ |
45 | #endif |
46 | ||
8c3a5a44 | 47 | #include "c.h" |
b2d28533 | 48 | #include "closestream.h" |
7eda085c | 49 | #include "nls.h" |
8abcf290 | 50 | #include "strutils.h" |
8c3a5a44 | 51 | #include "xalloc.h" |
5f94ca33 | 52 | #include "mbsalign.h" |
04915c3f | 53 | #include "colors.h" |
6dbe3af9 | 54 | |
8c3a5a44 KZ |
55 | #include "fdiskP.h" |
56 | ||
a4f0a42d KZ |
57 | #ifdef __GNU__ |
58 | # define DEFAULT_DEVICE "/dev/hd0" | |
59 | # define ALTERNATE_DEVICE "/dev/sd0" | |
60 | #elif defined(__FreeBSD__) | |
61 | # define DEFAULT_DEVICE "/dev/ad0" | |
62 | # define ALTERNATE_DEVICE "/dev/da0" | |
63 | #else | |
64 | # define DEFAULT_DEVICE "/dev/sda" | |
65 | # define ALTERNATE_DEVICE "/dev/hda" | |
66 | #endif | |
67 | ||
c743bf74 | 68 | #define ARROW_CURSOR_STRING ">> " |
8c3a5a44 KZ |
69 | #define ARROW_CURSOR_DUMMY " " |
70 | #define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1) | |
71 | ||
72 | #define MENU_PADDING 2 | |
73 | #define TABLE_START_LINE 4 | |
a6f69126 KZ |
74 | #define MENU_START_LINE ((size_t) LINES - 5) |
75 | #define INFO_LINE ((size_t) LINES - 2) | |
76 | #define HINT_LINE ((size_t) LINES - 1) | |
b1f58330 KZ |
77 | |
78 | #define CFDISK_ERR_ESC 5000 | |
79 | ||
80 | #ifndef KEY_ESC | |
81 | # define KEY_ESC '\033' | |
82 | #endif | |
83 | #ifndef KEY_DELETE | |
84 | # define KEY_DELETE '\177' | |
85 | #endif | |
1af8003b KZ |
86 | |
87 | /* colors */ | |
88 | enum { | |
89 | CFDISK_CL_NONE = 0, | |
45333e9d KZ |
90 | CFDISK_CL_WARNING, |
91 | CFDISK_CL_FREESPACE, | |
1af8003b KZ |
92 | }; |
93 | static const int color_pairs[][2] = { | |
94 | /* color foreground, background */ | |
45333e9d KZ |
95 | [CFDISK_CL_WARNING] = { COLOR_RED, -1 }, |
96 | [CFDISK_CL_FREESPACE] = { COLOR_GREEN, -1 } | |
1af8003b | 97 | }; |
8c3a5a44 | 98 | |
8460875d | 99 | struct cfdisk; |
8460875d | 100 | |
3b411726 KZ |
101 | static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx); |
102 | static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf, int key, size_t *idx); | |
103 | static struct cfdisk_menu *menu_push(struct cfdisk *cf, struct cfdisk_menuitem *item); | |
b1f58330 | 104 | static struct cfdisk_menu *menu_pop(struct cfdisk *cf); |
7556c944 | 105 | static void menu_refresh_size(struct cfdisk *cf); |
83fa0f80 | 106 | |
1af8003b | 107 | static int ui_refresh(struct cfdisk *cf); |
83fa0f80 KZ |
108 | static void ui_warnx(const char *fmt, ...); |
109 | static void ui_warn(const char *fmt, ...); | |
110 | static void ui_info(const char *fmt, ...); | |
b1f58330 | 111 | static void ui_draw_menu(struct cfdisk *cf); |
ac27ea5c | 112 | static int ui_menu_move(struct cfdisk *cf, int key); |
7556c944 | 113 | static void ui_menu_resize(struct cfdisk *cf); |
ac27ea5c | 114 | |
b1f58330 KZ |
115 | static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res, |
116 | uintmax_t low, uintmax_t up); | |
8460875d | 117 | |
83fa0f80 | 118 | static int ui_enabled; |
7556c944 | 119 | static int ui_resize; |
8c3a5a44 | 120 | |
88942179 | 121 | /* menu item */ |
3b411726 | 122 | struct cfdisk_menuitem { |
8c3a5a44 KZ |
123 | int key; /* keyboard shortcut */ |
124 | const char *name; /* item name */ | |
88942179 | 125 | const char *desc; /* item description (hint) */ |
6fed9601 | 126 | void *userdata; |
749af4b6 KZ |
127 | }; |
128 | ||
88942179 | 129 | /* menu */ |
8c3a5a44 | 130 | struct cfdisk_menu { |
88942179 KZ |
131 | char *title; /* optional menu title */ |
132 | struct cfdisk_menuitem *items; /* array with menu items */ | |
133 | char *ignore;/* string with keys to ignore */ | |
134 | size_t width; /* maximal width of the menu item */ | |
135 | size_t nitems; /* number of the active menu items */ | |
136 | size_t page_sz;/* when menu longer than screen */ | |
137 | size_t idx; /* the current menu item */ | |
8c3a5a44 | 138 | struct cfdisk_menu *prev; |
88942179 KZ |
139 | |
140 | /* @ignore keys generator */ | |
63128bbf | 141 | int (*ignore_cb) (struct cfdisk *, char *, size_t); |
8a726114 | 142 | |
88942179 | 143 | unsigned int vertical : 1; /* enable vertical mode */ |
749af4b6 KZ |
144 | }; |
145 | ||
88942179 | 146 | /* main menu */ |
3b411726 | 147 | static struct cfdisk_menuitem main_menuitems[] = { |
8c3a5a44 KZ |
148 | { 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") }, |
149 | { 'd', N_("Delete"), N_("Delete the current partition") }, | |
8c3a5a44 | 150 | { 'n', N_("New"), N_("Create new partition from free space") }, |
8c3a5a44 KZ |
151 | { 'q', N_("Quit"), N_("Quit program without writing partition table") }, |
152 | { 't', N_("Type"), N_("Change the partition type") }, | |
314a0dd8 | 153 | { 'h', N_("Help"), N_("Print help screen") }, |
d5314bf5 | 154 | { 's', N_("Sort"), N_("Fix partitions order") }, |
8c3a5a44 KZ |
155 | { 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") }, |
156 | { 0, NULL, NULL } | |
157 | }; | |
6dbe3af9 | 158 | |
88942179 | 159 | /* top level control struct */ |
8c3a5a44 KZ |
160 | struct cfdisk { |
161 | struct fdisk_context *cxt; /* libfdisk context */ | |
162 | struct fdisk_table *table; /* partition table */ | |
a6f69126 | 163 | struct cfdisk_menu *menu; /* the current menu */ |
7eda085c | 164 | |
8c3a5a44 KZ |
165 | int *cols; /* output columns */ |
166 | size_t ncols; /* number of columns */ | |
7eda085c | 167 | |
8c3a5a44 KZ |
168 | char *linesbuf; /* table as string */ |
169 | size_t linesbufsz; /* size of the tb_buf */ | |
7eda085c | 170 | |
8c3a5a44 KZ |
171 | char **lines; /* array with lines */ |
172 | size_t nlines; /* number of lines */ | |
a6f69126 KZ |
173 | size_t lines_idx; /* current line <0..N>, exclude header */ |
174 | size_t page_sz; | |
d5314bf5 | 175 | |
bc787727 KZ |
176 | unsigned int nwrites; /* fdisk_write_disklabel() counter */ |
177 | ||
d121efdb KZ |
178 | unsigned int wrong_order :1, /* PT not in right order */ |
179 | zero_start :1; /* ignore existing partition table */ | |
8c3a5a44 | 180 | }; |
5c36a0eb | 181 | |
88942179 KZ |
182 | /* Initialize output columns -- we follow libcfdisk columns (usually specific |
183 | * to the label type. | |
184 | */ | |
8c3a5a44 KZ |
185 | static int cols_init(struct cfdisk *cf) |
186 | { | |
187 | assert(cf); | |
5c36a0eb | 188 | |
8c3a5a44 KZ |
189 | free(cf->cols); |
190 | cf->cols = NULL; | |
191 | cf->ncols = 0; | |
5c36a0eb | 192 | |
8c3a5a44 | 193 | return fdisk_get_columns(cf->cxt, 0, &cf->cols, &cf->ncols); |
5c36a0eb KZ |
194 | } |
195 | ||
7556c944 KZ |
196 | static void resize(void) |
197 | { | |
198 | struct winsize ws; | |
199 | ||
200 | if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1 | |
201 | && ws.ws_row && ws.ws_col) { | |
202 | LINES = ws.ws_row; | |
203 | COLS = ws.ws_col; | |
204 | #if HAVE_RESIZETERM | |
205 | resizeterm(ws.ws_row, ws.ws_col); | |
206 | #endif | |
207 | clearok(stdscr, TRUE); | |
208 | } | |
209 | touchwin(stdscr); | |
210 | ||
211 | DBG(FRONTEND, ul_debug("ui: resize refresh COLS=%d, LINES=%d", COLS, LINES)); | |
212 | ui_resize = 0; | |
213 | } | |
214 | ||
c743bf74 KZ |
215 | /* Reads partition in tree-like order from scols |
216 | */ | |
217 | static int partition_from_scols(struct fdisk_table *tb, | |
218 | struct libscols_line *ln) | |
219 | { | |
220 | struct fdisk_partition *pa = scols_line_get_userdata(ln); | |
221 | ||
222 | fdisk_table_add_partition(tb, pa); | |
223 | fdisk_unref_partition(pa); | |
224 | ||
225 | if (scols_line_has_children(ln)) { | |
226 | struct libscols_line *chln; | |
227 | struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD); | |
228 | ||
229 | if (!itr) | |
230 | return -EINVAL; | |
231 | while (scols_line_next_child(ln, itr, &chln) == 0) | |
232 | partition_from_scols(tb, chln); | |
a9408274 | 233 | scols_free_iter(itr); |
c743bf74 KZ |
234 | } |
235 | return 0; | |
236 | } | |
237 | ||
8c3a5a44 KZ |
238 | /* It would be possible to use fdisk_table_to_string(), but we want some |
239 | * extension to the output format, so let's do it without libfdisk | |
240 | */ | |
241 | static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb) | |
242 | { | |
8c3a5a44 | 243 | const struct fdisk_column *col; |
8903f7df | 244 | struct fdisk_partition *pa; |
8c3a5a44 KZ |
245 | struct fdisk_label *lb; |
246 | struct fdisk_iter *itr = NULL; | |
baa3b270 OO |
247 | struct libscols_table *table = NULL; |
248 | struct libscols_iter *s_itr = NULL; | |
8c3a5a44 | 249 | char *res = NULL; |
8903f7df | 250 | size_t i; |
45333e9d | 251 | int tree = 0; |
baa3b270 | 252 | struct libscols_line *ln, *ln_cont = NULL; |
8c3a5a44 | 253 | |
88141067 | 254 | DBG(FRONTEND, ul_debug("table: convert to string")); |
8c3a5a44 KZ |
255 | |
256 | assert(cf); | |
257 | assert(cf->cxt); | |
258 | assert(cf->cols); | |
259 | assert(tb); | |
260 | ||
261 | lb = fdisk_context_get_label(cf->cxt, NULL); | |
262 | assert(lb); | |
263 | ||
8c3a5a44 KZ |
264 | itr = fdisk_new_iter(FDISK_ITER_FORWARD); |
265 | if (!itr) | |
266 | goto done; | |
5c36a0eb | 267 | |
45333e9d KZ |
268 | /* get container (e.g. extended partition) */ |
269 | while (fdisk_table_next_partition(tb, itr, &pa) == 0) { | |
270 | if (fdisk_partition_is_nested(pa)) { | |
88141067 | 271 | DBG(FRONTEND, ul_debug("table: nested detected, using tree")); |
baa3b270 | 272 | tree = SCOLS_FL_TREE; |
45333e9d KZ |
273 | break; |
274 | } | |
275 | } | |
45333e9d | 276 | |
0925a9dd | 277 | table = scols_new_table(); |
baa3b270 | 278 | if (!table) |
45333e9d | 279 | goto done; |
0925a9dd | 280 | scols_table_enable_maxout(table, 1); |
45333e9d | 281 | |
8c3a5a44 KZ |
282 | /* headers */ |
283 | for (i = 0; i < cf->ncols; i++) { | |
284 | col = fdisk_label_get_column(lb, cf->cols[i]); | |
45333e9d | 285 | if (col) { |
baa3b270 | 286 | int fl = col->scols_flags; |
45333e9d | 287 | if (tree && col->id == FDISK_COL_DEVICE) |
baa3b270 OO |
288 | fl |= SCOLS_FL_TREE; |
289 | if (!scols_table_new_column(table, col->name, col->width, fl)) | |
290 | goto done; | |
45333e9d | 291 | } |
8c3a5a44 KZ |
292 | } |
293 | ||
294 | /* data */ | |
8903f7df KZ |
295 | fdisk_reset_iter(itr, FDISK_ITER_FORWARD); |
296 | ||
8c3a5a44 | 297 | while (fdisk_table_next_partition(tb, itr, &pa) == 0) { |
baa3b270 | 298 | struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL; |
8903f7df | 299 | |
baa3b270 | 300 | ln = scols_table_new_line(table, parent); |
8c3a5a44 KZ |
301 | if (!ln) |
302 | goto done; | |
303 | for (i = 0; i < cf->ncols; i++) { | |
304 | char *cdata = NULL; | |
8c3a5a44 KZ |
305 | col = fdisk_label_get_column(lb, cf->cols[i]); |
306 | if (!col) | |
307 | continue; | |
308 | if (fdisk_partition_to_string(pa, cf->cxt, col->id, &cdata)) | |
309 | continue; | |
c743bf74 | 310 | scols_line_refer_data(ln, i, cdata); |
8c3a5a44 | 311 | } |
8903f7df | 312 | if (tree && fdisk_partition_is_container(pa)) |
45333e9d | 313 | ln_cont = ln; |
d051ea93 | 314 | |
baa3b270 | 315 | scols_line_set_userdata(ln, (void *) pa); |
d051ea93 | 316 | fdisk_ref_partition(pa); |
8c3a5a44 | 317 | } |
6dbe3af9 | 318 | |
baa3b270 | 319 | if (scols_table_is_empty(table)) |
d051ea93 KZ |
320 | goto done; |
321 | ||
baa3b270 OO |
322 | scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH); |
323 | scols_print_table_to_string(table, &res); | |
d051ea93 | 324 | |
baa3b270 | 325 | /* scols_* code might reorder lines, let's reorder @tb according to the |
d051ea93 KZ |
326 | * final output (it's no problem because partitions are addressed by |
327 | * parno stored within struct fdisk_partition) */ | |
328 | ||
329 | /* remove all */ | |
330 | fdisk_reset_iter(itr, FDISK_ITER_FORWARD); | |
331 | while (fdisk_table_next_partition(tb, itr, &pa) == 0) | |
332 | fdisk_table_remove_partition(tb, pa); | |
333 | ||
baa3b270 OO |
334 | s_itr = scols_new_iter(SCOLS_ITER_FORWARD); |
335 | if (!s_itr) | |
336 | goto done; | |
337 | ||
c743bf74 | 338 | /* add all in the right order (don't forget the output is tree) */ |
baa3b270 | 339 | while (scols_table_next_line(table, s_itr, &ln) == 0) { |
c743bf74 KZ |
340 | if (scols_line_get_parent(ln)) |
341 | continue; | |
342 | if (partition_from_scols(tb, ln)) | |
343 | break; | |
8c3a5a44 KZ |
344 | } |
345 | done: | |
baa3b270 OO |
346 | scols_unref_table(table); |
347 | scols_free_iter(s_itr); | |
8c3a5a44 | 348 | fdisk_free_iter(itr); |
6dbe3af9 | 349 | |
8c3a5a44 | 350 | return res; |
2b6fc908 KZ |
351 | } |
352 | ||
88942179 KZ |
353 | /* |
354 | * Read data about partitions from libfdisk and prepare output lines. | |
355 | */ | |
8460875d | 356 | static int lines_refresh(struct cfdisk *cf) |
8c3a5a44 KZ |
357 | { |
358 | int rc; | |
359 | char *p; | |
360 | size_t i; | |
7eda085c | 361 | |
8c3a5a44 | 362 | assert(cf); |
2b6fc908 | 363 | |
88141067 | 364 | DBG(FRONTEND, ul_debug("refreshing buffer")); |
c64061c9 | 365 | |
8c3a5a44 KZ |
366 | free(cf->linesbuf); |
367 | free(cf->lines); | |
368 | cf->linesbuf = NULL; | |
369 | cf->linesbufsz = 0; | |
370 | cf->lines = NULL; | |
371 | cf->nlines = 0; | |
6dbe3af9 | 372 | |
8c3a5a44 | 373 | fdisk_unref_table(cf->table); |
1af8003b | 374 | cf->table = NULL; |
6dbe3af9 | 375 | |
2cec7949 KZ |
376 | /* read partitions and free spaces into cf->table */ |
377 | rc = fdisk_get_partitions(cf->cxt, &cf->table); | |
378 | if (!rc) | |
379 | rc = fdisk_get_freespaces(cf->cxt, &cf->table); | |
8c3a5a44 KZ |
380 | if (rc) |
381 | return rc; | |
0d8589c5 | 382 | |
8c3a5a44 KZ |
383 | cf->linesbuf = table_to_string(cf, cf->table); |
384 | if (!cf->linesbuf) | |
385 | return -ENOMEM; | |
6dbe3af9 | 386 | |
8c3a5a44 KZ |
387 | cf->linesbufsz = strlen(cf->linesbuf); |
388 | cf->nlines = fdisk_table_get_nents(cf->table) + 1; /* 1 for header line */ | |
a6f69126 | 389 | cf->page_sz = 0; |
d5314bf5 | 390 | cf->wrong_order = fdisk_table_wrong_order(cf->table) ? 1 : 0; |
a6f69126 KZ |
391 | |
392 | if (MENU_START_LINE - TABLE_START_LINE < cf->nlines) | |
393 | cf->page_sz = MENU_START_LINE - TABLE_START_LINE - 1; | |
6dbe3af9 | 394 | |
6d6d9c1a | 395 | cf->lines = xcalloc(cf->nlines, sizeof(char *)); |
6dbe3af9 | 396 | |
8c3a5a44 KZ |
397 | for (p = cf->linesbuf, i = 0; p && i < cf->nlines; i++) { |
398 | cf->lines[i] = p; | |
399 | p = strchr(p, '\n'); | |
400 | if (p) { | |
401 | *p = '\0'; | |
402 | p++; | |
403 | } | |
404 | } | |
6dbe3af9 | 405 | |
8c3a5a44 | 406 | return 0; |
6dbe3af9 KZ |
407 | } |
408 | ||
00b4f26a KZ |
409 | static struct fdisk_partition *get_current_partition(struct cfdisk *cf) |
410 | { | |
411 | assert(cf); | |
412 | assert(cf->table); | |
413 | ||
414 | return fdisk_table_get_partition(cf->table, cf->lines_idx); | |
415 | } | |
416 | ||
45333e9d KZ |
417 | static int is_freespace(struct cfdisk *cf, size_t i) |
418 | { | |
419 | struct fdisk_partition *pa; | |
420 | ||
421 | assert(cf); | |
422 | assert(cf->table); | |
423 | ||
424 | pa = fdisk_table_get_partition(cf->table, i); | |
425 | return fdisk_partition_is_freespace(pa); | |
426 | } | |
427 | ||
b1f58330 KZ |
428 | /* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's |
429 | * responseback to libfdisk | |
430 | */ | |
431 | static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf) | |
432 | { | |
3b411726 | 433 | struct cfdisk_menuitem *d, *cm; |
b1f58330 KZ |
434 | int key; |
435 | size_t i = 0, nitems; | |
436 | const char *name, *desc; | |
437 | ||
438 | assert(ask); | |
439 | assert(cf); | |
440 | ||
441 | /* create cfdisk menu according to libfdisk ask-menu, note that the | |
442 | * last cm[] item has to be empty -- so nitems + 1 */ | |
443 | nitems = fdisk_ask_menu_get_nitems(ask); | |
3b411726 | 444 | cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem)); |
b1f58330 KZ |
445 | |
446 | for (i = 0; i < nitems; i++) { | |
447 | if (fdisk_ask_menu_get_item(ask, i, &key, &name, &desc)) | |
448 | break; | |
449 | cm[i].key = key; | |
450 | cm[i].desc = desc; | |
451 | cm[i].name = name; | |
452 | } | |
453 | ||
454 | /* make the new menu active */ | |
63128bbf | 455 | menu_push(cf, cm); |
b1f58330 KZ |
456 | ui_draw_menu(cf); |
457 | refresh(); | |
458 | ||
459 | /* wait for keys */ | |
460 | do { | |
ac27ea5c KZ |
461 | int key = getch(); |
462 | ||
7556c944 KZ |
463 | if (ui_resize) |
464 | ui_menu_resize(cf); | |
ac27ea5c KZ |
465 | if (ui_menu_move(cf, key) == 0) |
466 | continue; | |
467 | ||
468 | switch (key) { | |
b1f58330 KZ |
469 | case KEY_ENTER: |
470 | case '\n': | |
471 | case '\r': | |
6fed9601 | 472 | d = menu_get_menuitem(cf, cf->menu->idx); |
b1f58330 KZ |
473 | if (d) |
474 | fdisk_ask_menu_set_result(ask, d->key); | |
475 | menu_pop(cf); | |
476 | free(cm); | |
477 | return 0; | |
478 | } | |
479 | } while (1); | |
480 | ||
481 | menu_pop(cf); | |
482 | free(cm); | |
483 | return -1; | |
484 | } | |
485 | ||
88942179 KZ |
486 | /* libfdisk callback |
487 | */ | |
8c3a5a44 KZ |
488 | static int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask, |
489 | void *data __attribute__((__unused__))) | |
490 | { | |
491 | int rc = 0; | |
6dbe3af9 | 492 | |
8c3a5a44 KZ |
493 | assert(cxt); |
494 | assert(ask); | |
6dbe3af9 | 495 | |
8c3a5a44 KZ |
496 | switch(fdisk_ask_get_type(ask)) { |
497 | case FDISK_ASKTYPE_INFO: | |
83fa0f80 | 498 | ui_info(fdisk_ask_print_get_mesg(ask)); |
8c3a5a44 KZ |
499 | break; |
500 | case FDISK_ASKTYPE_WARNX: | |
83fa0f80 | 501 | ui_warnx(fdisk_ask_print_get_mesg(ask)); |
8c3a5a44 KZ |
502 | break; |
503 | case FDISK_ASKTYPE_WARN: | |
83fa0f80 | 504 | ui_warn(fdisk_ask_print_get_mesg(ask)); |
8c3a5a44 | 505 | break; |
b1f58330 KZ |
506 | case FDISK_ASKTYPE_MENU: |
507 | ask_menu(ask, (struct cfdisk *) data); | |
508 | break; | |
8c3a5a44 | 509 | default: |
83fa0f80 KZ |
510 | ui_warnx(_("internal error: unsupported dialog type %d"), |
511 | fdisk_ask_get_type(ask)); | |
8c3a5a44 KZ |
512 | return -EINVAL; |
513 | } | |
514 | return rc; | |
fd6b7a7f | 515 | } |
6dbe3af9 | 516 | |
7aa0d529 | 517 | static int ui_end(void) |
8c3a5a44 | 518 | { |
7aa0d529 | 519 | if (!ui_enabled) |
8c3a5a44 KZ |
520 | return -EINVAL; |
521 | ||
48d7b13a | 522 | #if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H) |
8c3a5a44 KZ |
523 | SLsmg_gotorc(LINES - 1, 0); |
524 | SLsmg_refresh(); | |
2b6fc908 | 525 | #else |
8c3a5a44 | 526 | mvcur(0, COLS - 1, LINES-1, 0); |
2b6fc908 | 527 | #endif |
8c3a5a44 KZ |
528 | nl(); |
529 | endwin(); | |
530 | printf("\n"); | |
7aa0d529 | 531 | ui_enabled = 0; |
8c3a5a44 | 532 | return 0; |
6dbe3af9 KZ |
533 | } |
534 | ||
1af8003b | 535 | static void ui_vprint_center(int line, int attrs, const char *fmt, va_list ap) |
f609e92e | 536 | { |
8c3a5a44 | 537 | size_t width; |
8c3a5a44 | 538 | char *buf = NULL; |
f609e92e | 539 | |
8c3a5a44 KZ |
540 | move(line, 0); |
541 | clrtoeol(); | |
f609e92e | 542 | |
8c3a5a44 | 543 | xvasprintf(&buf, fmt, ap); |
fd6b7a7f | 544 | |
b1f58330 | 545 | width = mbs_safe_width(buf); |
91ba41ca KZ |
546 | if (width > (size_t) COLS) { |
547 | char *p = strrchr(buf + COLS, ' '); | |
548 | if (!p) | |
549 | p = buf + COLS; | |
550 | *p = '\0'; | |
551 | if (line + 1 >= LINES) | |
552 | line--; | |
553 | attron(attrs); | |
554 | mvaddstr(line, 0, buf); | |
555 | mvaddstr(line + 1, 0, p+1); | |
556 | attroff(attrs); | |
557 | } else { | |
558 | attron(attrs); | |
559 | mvaddstr(line, (COLS - width) / 2, buf); | |
560 | attroff(attrs); | |
561 | } | |
8c3a5a44 | 562 | free(buf); |
6dbe3af9 KZ |
563 | } |
564 | ||
1af8003b KZ |
565 | static void ui_center(int line, const char *fmt, ...) |
566 | { | |
567 | va_list ap; | |
568 | va_start(ap, fmt); | |
569 | ui_vprint_center(line, 0, fmt, ap); | |
570 | va_end(ap); | |
571 | } | |
572 | ||
83fa0f80 KZ |
573 | static void ui_warnx(const char *fmt, ...) |
574 | { | |
575 | va_list ap; | |
576 | va_start(ap, fmt); | |
577 | if (ui_enabled) | |
f1512be8 KZ |
578 | ui_vprint_center(INFO_LINE, |
579 | colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0, | |
580 | fmt, ap); | |
8fc37981 | 581 | else { |
83fa0f80 | 582 | vfprintf(stderr, fmt, ap); |
8fc37981 KZ |
583 | fputc('\n', stderr); |
584 | } | |
83fa0f80 KZ |
585 | va_end(ap); |
586 | } | |
587 | ||
588 | static void ui_warn(const char *fmt, ...) | |
1af8003b | 589 | { |
83fa0f80 | 590 | char *fmt_m; |
1af8003b | 591 | va_list ap; |
83fa0f80 KZ |
592 | |
593 | xasprintf(&fmt_m, "%s: %m", fmt); | |
594 | ||
1af8003b | 595 | va_start(ap, fmt); |
83fa0f80 | 596 | if (ui_enabled) |
f1512be8 KZ |
597 | ui_vprint_center(INFO_LINE, |
598 | colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0, | |
599 | fmt_m, ap); | |
8fc37981 | 600 | else { |
83fa0f80 | 601 | vfprintf(stderr, fmt_m, ap); |
8fc37981 KZ |
602 | fputc('\n', stderr); |
603 | } | |
1af8003b | 604 | va_end(ap); |
83fa0f80 | 605 | free(fmt_m); |
1af8003b KZ |
606 | } |
607 | ||
7aa0d529 KZ |
608 | static int __attribute__((__noreturn__)) ui_errx(int rc, const char *fmt, ...) |
609 | { | |
610 | va_list ap; | |
611 | ui_end(); | |
612 | ||
613 | va_start(ap, fmt); | |
614 | fprintf(stderr, "%s: ", program_invocation_short_name); | |
615 | vfprintf(stderr, fmt, ap); | |
8fc37981 | 616 | fputc('\n', stderr); |
7aa0d529 KZ |
617 | va_end(ap); |
618 | ||
619 | exit(rc); | |
620 | } | |
621 | ||
1af8003b KZ |
622 | static void ui_info(const char *fmt, ...) |
623 | { | |
624 | va_list ap; | |
625 | va_start(ap, fmt); | |
83fa0f80 KZ |
626 | if (ui_enabled) |
627 | ui_vprint_center(INFO_LINE, A_BOLD, fmt, ap); | |
8fc37981 | 628 | else { |
83fa0f80 | 629 | vfprintf(stdout, fmt, ap); |
8fc37981 KZ |
630 | fputc('\n', stdout); |
631 | } | |
1af8003b KZ |
632 | va_end(ap); |
633 | } | |
634 | ||
635 | static void ui_clean_info(void) | |
636 | { | |
637 | move(INFO_LINE, 0); | |
638 | clrtoeol(); | |
639 | } | |
6dbe3af9 | 640 | |
b1f58330 KZ |
641 | static void ui_hint(const char *fmt, ...) |
642 | { | |
643 | va_list ap; | |
644 | va_start(ap, fmt); | |
645 | if (ui_enabled) | |
646 | ui_vprint_center(HINT_LINE, A_BOLD, fmt, ap); | |
8fc37981 | 647 | else { |
b1f58330 | 648 | vfprintf(stdout, fmt, ap); |
8fc37981 KZ |
649 | fputc('\n', stdout); |
650 | } | |
b1f58330 KZ |
651 | va_end(ap); |
652 | } | |
653 | ||
654 | static void ui_clean_hint(void) | |
655 | { | |
656 | move(HINT_LINE, 0); | |
657 | clrtoeol(); | |
658 | } | |
659 | ||
8c3a5a44 KZ |
660 | static void die_on_signal(int dummy __attribute__((__unused__))) |
661 | { | |
88141067 | 662 | DBG(FRONTEND, ul_debug("die on signal.")); |
7aa0d529 | 663 | ui_end(); |
8c3a5a44 | 664 | exit(EXIT_FAILURE); |
6dbe3af9 KZ |
665 | } |
666 | ||
7556c944 KZ |
667 | static void resize_on_signal(int dummy __attribute__((__unused__))) |
668 | { | |
669 | DBG(FRONTEND, ul_debug("resize on signal.")); | |
670 | ui_resize = 1; | |
671 | } | |
672 | ||
673 | static void menu_refresh_size(struct cfdisk *cf) | |
674 | { | |
675 | if (cf->menu && cf->menu->nitems) | |
3524452a | 676 | cf->menu->page_sz = (cf->menu->nitems / (LINES - 4)) ? LINES - 4 : 0; |
7556c944 KZ |
677 | } |
678 | ||
8c3a5a44 KZ |
679 | static void menu_update_ignore(struct cfdisk *cf) |
680 | { | |
00b4f26a KZ |
681 | char ignore[128] = { 0 }; |
682 | int i = 0; | |
8c3a5a44 | 683 | struct cfdisk_menu *m; |
3b411726 | 684 | struct cfdisk_menuitem *d, *org; |
83fa0f80 | 685 | size_t idx; |
6dbe3af9 | 686 | |
8c3a5a44 | 687 | assert(cf); |
63128bbf KZ |
688 | assert(cf->menu); |
689 | assert(cf->menu->ignore_cb); | |
6dbe3af9 | 690 | |
8c3a5a44 | 691 | m = cf->menu; |
6fed9601 | 692 | org = menu_get_menuitem(cf, m->idx); |
83fa0f80 | 693 | |
88141067 | 694 | DBG(FRONTEND, ul_debug("menu: update menu ignored keys")); |
6dbe3af9 | 695 | |
63128bbf | 696 | i = m->ignore_cb(cf, ignore, sizeof(ignore)); |
00b4f26a KZ |
697 | ignore[i] = '\0'; |
698 | ||
8c3a5a44 | 699 | /* return if no change */ |
00b4f26a KZ |
700 | if ( (!m->ignore && !*ignore) |
701 | || (m->ignore && *ignore && strcmp(m->ignore, ignore) == 0)) { | |
8c3a5a44 | 702 | return; |
fd6b7a7f | 703 | } |
6dbe3af9 | 704 | |
8c3a5a44 | 705 | free(m->ignore); |
8460875d | 706 | m->ignore = xstrdup(ignore); |
8c3a5a44 | 707 | m->nitems = 0; |
6dbe3af9 | 708 | |
3b411726 | 709 | for (d = m->items; d->name; d++) { |
8c3a5a44 | 710 | if (m->ignore && strchr(m->ignore, d->key)) |
8460875d KZ |
711 | continue; |
712 | m->nitems++; | |
8c3a5a44 | 713 | } |
83fa0f80 KZ |
714 | |
715 | /* refresh menu index to be at the same menuitem or go to the first */ | |
716 | if (org && menu_get_menuitem_by_key(cf, org->key, &idx)) | |
6fed9601 | 717 | m->idx = idx; |
83fa0f80 | 718 | else |
6fed9601 KZ |
719 | m->idx = 0; |
720 | ||
7556c944 | 721 | menu_refresh_size(cf); |
6dbe3af9 KZ |
722 | } |
723 | ||
b1f58330 KZ |
724 | static struct cfdisk_menu *menu_push( |
725 | struct cfdisk *cf, | |
3b411726 | 726 | struct cfdisk_menuitem *items) |
8c3a5a44 KZ |
727 | { |
728 | struct cfdisk_menu *m = xcalloc(1, sizeof(*m)); | |
3b411726 | 729 | struct cfdisk_menuitem *d; |
6dbe3af9 | 730 | |
8c3a5a44 | 731 | assert(cf); |
6dbe3af9 | 732 | |
88141067 | 733 | DBG(FRONTEND, ul_debug("menu: new menu")); |
6dbe3af9 | 734 | |
8c3a5a44 | 735 | m->prev = cf->menu; |
3b411726 | 736 | m->items = items; |
6dbe3af9 | 737 | |
3b411726 | 738 | for (d = m->items; d->name; d++) { |
8c3a5a44 | 739 | const char *name = _(d->name); |
b1f58330 | 740 | size_t len = mbs_safe_width(name); |
8c3a5a44 KZ |
741 | if (len > m->width) |
742 | m->width = len; | |
743 | m->nitems++; | |
744 | } | |
6dbe3af9 | 745 | |
8c3a5a44 | 746 | cf->menu = m; |
7556c944 | 747 | menu_refresh_size(cf); |
8c3a5a44 | 748 | return m; |
6dbe3af9 KZ |
749 | } |
750 | ||
8c3a5a44 | 751 | static struct cfdisk_menu *menu_pop(struct cfdisk *cf) |
6dbe3af9 | 752 | { |
8c3a5a44 | 753 | struct cfdisk_menu *m = NULL; |
6dbe3af9 | 754 | |
8c3a5a44 | 755 | assert(cf); |
7eda085c | 756 | |
88141067 | 757 | DBG(FRONTEND, ul_debug("menu: rem menu")); |
7eda085c | 758 | |
8c3a5a44 KZ |
759 | if (cf->menu) { |
760 | m = cf->menu->prev; | |
761 | free(cf->menu->ignore); | |
8a726114 | 762 | free(cf->menu->title); |
8c3a5a44 | 763 | free(cf->menu); |
c07ebfa1 | 764 | } |
8c3a5a44 KZ |
765 | cf->menu = m; |
766 | return cf->menu; | |
6dbe3af9 KZ |
767 | } |
768 | ||
8a726114 KZ |
769 | static void menu_set_title(struct cfdisk_menu *m, const char *title) |
770 | { | |
771 | char *str = NULL; | |
772 | ||
773 | if (title) { | |
774 | size_t len = mbs_safe_width(title); | |
775 | if (len + 3 > m->width) | |
776 | m->width = len + 3; | |
777 | str = xstrdup(title); | |
778 | } | |
779 | m->title = str; | |
780 | } | |
781 | ||
782 | ||
b1f58330 | 783 | static int ui_init(struct cfdisk *cf __attribute__((__unused__))) |
8c3a5a44 KZ |
784 | { |
785 | struct sigaction sa; | |
6dbe3af9 | 786 | |
88141067 | 787 | DBG(FRONTEND, ul_debug("ui: init")); |
6dbe3af9 | 788 | |
8c3a5a44 KZ |
789 | /* setup SIGCHLD handler */ |
790 | sigemptyset(&sa.sa_mask); | |
791 | sa.sa_flags = 0; | |
792 | sa.sa_handler = die_on_signal; | |
793 | sigaction(SIGINT, &sa, NULL); | |
794 | sigaction(SIGTERM, &sa, NULL); | |
6dbe3af9 | 795 | |
7556c944 KZ |
796 | sa.sa_handler = resize_on_signal; |
797 | sigaction(SIGWINCH, &sa, NULL); | |
798 | ||
83fa0f80 | 799 | ui_enabled = 1; |
8c3a5a44 | 800 | initscr(); |
6dbe3af9 | 801 | |
f1512be8 | 802 | #ifdef HAVE_USE_DEFAULT_COLORS |
04915c3f | 803 | if (colors_wanted() && has_colors()) { |
1af8003b KZ |
804 | size_t i; |
805 | ||
806 | start_color(); | |
807 | use_default_colors(); | |
1af8003b KZ |
808 | for (i = 1; i < ARRAY_SIZE(color_pairs); i++) /* yeah, start from 1! */ |
809 | init_pair(i, color_pairs[i][0], color_pairs[i][1]); | |
810 | } | |
f1512be8 | 811 | #else |
e66a6627 | 812 | colors_off(); |
f1512be8 | 813 | #endif |
1af8003b | 814 | |
8c3a5a44 KZ |
815 | cbreak(); |
816 | noecho(); | |
817 | nonl(); | |
818 | curs_set(0); | |
819 | keypad(stdscr, TRUE); | |
6dbe3af9 | 820 | |
8c3a5a44 | 821 | return 0; |
7eda085c KZ |
822 | } |
823 | ||
8c3a5a44 KZ |
824 | static size_t menuitem_get_line(struct cfdisk *cf, size_t idx) |
825 | { | |
6fed9601 KZ |
826 | struct cfdisk_menu *m = cf->menu; |
827 | ||
828 | if (m->vertical) { | |
829 | if (!m->page_sz) /* small menu */ | |
830 | return (LINES - (cf->menu->nitems + 1)) / 2 + idx; | |
831 | return (idx % m->page_sz) + 1; | |
8a726114 | 832 | } else { |
6fed9601 KZ |
833 | size_t len = m->width + 4 + MENU_PADDING; /* item width */ |
834 | size_t items = COLS / len; /* items per line */ | |
8a726114 | 835 | |
a1da27a8 KZ |
836 | if (items == 0) |
837 | return 0; | |
838 | ||
8a726114 KZ |
839 | return MENU_START_LINE + ((idx / items)); |
840 | } | |
7eda085c KZ |
841 | } |
842 | ||
8c3a5a44 KZ |
843 | static int menuitem_get_column(struct cfdisk *cf, size_t idx) |
844 | { | |
8a726114 KZ |
845 | if (cf->menu->vertical) { |
846 | size_t nc = cf->menu->width + MENU_PADDING; | |
7aa0d529 KZ |
847 | if ((size_t) COLS <= nc) |
848 | return 0; | |
849 | return (COLS - nc) / 2; | |
8a726114 KZ |
850 | } else { |
851 | size_t len = cf->menu->width + 4 + MENU_PADDING; /* item width */ | |
852 | size_t items = COLS / len; /* items per line */ | |
853 | size_t extra = items < cf->menu->nitems ? /* extra space on line */ | |
854 | COLS % len : /* - multi-line menu */ | |
855 | COLS - (cf->menu->nitems * len); /* - one line menu */ | |
856 | ||
a1da27a8 KZ |
857 | if (items == 0) |
858 | return 0; /* hmm... no space */ | |
859 | ||
8a726114 | 860 | extra += MENU_PADDING; /* add padding after last item to extra */ |
e66ac5d3 | 861 | |
8a726114 KZ |
862 | if (idx < items) |
863 | return (idx * len) + (extra / 2); | |
864 | return ((idx % items) * len) + (extra / 2); | |
865 | } | |
df1dddf9 KZ |
866 | } |
867 | ||
6fed9601 KZ |
868 | static int menuitem_on_page(struct cfdisk *cf, size_t idx) |
869 | { | |
870 | struct cfdisk_menu *m = cf->menu; | |
871 | ||
872 | if (m->page_sz == 0 || | |
873 | m->idx / m->page_sz == idx / m->page_sz) | |
874 | return 1; | |
875 | return 0; | |
876 | } | |
877 | ||
3b411726 | 878 | static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx) |
8c3a5a44 | 879 | { |
3b411726 | 880 | struct cfdisk_menuitem *d; |
8c3a5a44 | 881 | size_t i; |
7eda085c | 882 | |
3b411726 | 883 | for (i = 0, d = cf->menu->items; d->name; d++) { |
8c3a5a44 KZ |
884 | if (cf->menu->ignore && strchr(cf->menu->ignore, d->key)) |
885 | continue; | |
886 | if (i++ == idx) | |
887 | return d; | |
d26aa358 | 888 | } |
7eda085c | 889 | |
8c3a5a44 | 890 | return NULL; |
6dbe3af9 KZ |
891 | } |
892 | ||
3b411726 | 893 | static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf, |
83fa0f80 KZ |
894 | int key, size_t *idx) |
895 | { | |
3b411726 | 896 | struct cfdisk_menuitem *d; |
83fa0f80 | 897 | |
3b411726 | 898 | for (*idx = 0, d = cf->menu->items; d->name; d++) { |
83fa0f80 KZ |
899 | if (cf->menu->ignore && strchr(cf->menu->ignore, d->key)) |
900 | continue; | |
901 | if (key == d->key) | |
902 | return d; | |
903 | (*idx)++; | |
904 | } | |
905 | ||
906 | return NULL; | |
907 | } | |
908 | ||
8c3a5a44 | 909 | static void ui_draw_menuitem(struct cfdisk *cf, |
3b411726 | 910 | struct cfdisk_menuitem *d, |
8c3a5a44 KZ |
911 | size_t idx) |
912 | { | |
913 | char buf[80 * MB_CUR_MAX]; | |
914 | const char *name; | |
915 | size_t width = cf->menu->width + 2; /* 2 = blank around string */ | |
8a726114 | 916 | int ln, cl, vert = cf->menu->vertical; |
8c3a5a44 | 917 | |
6fed9601 KZ |
918 | if (!menuitem_on_page(cf, idx)) |
919 | return; /* no visible item */ | |
920 | ln = menuitem_get_line(cf, idx); | |
921 | cl = menuitem_get_column(cf, idx); | |
922 | ||
8c3a5a44 | 923 | name = _(d->name); |
8a726114 KZ |
924 | mbsalign(name, buf, sizeof(buf), &width, |
925 | vert ? MBS_ALIGN_LEFT : MBS_ALIGN_CENTER, | |
926 | 0); | |
8c3a5a44 | 927 | |
88141067 | 928 | DBG(FRONTEND, ul_debug("ui: menuitem: cl=%d, ln=%d, item='%s'", |
8c3a5a44 KZ |
929 | cl, ln, buf)); |
930 | ||
8a726114 KZ |
931 | if (vert) { |
932 | mvaddch(ln, cl - 1, ACS_VLINE); | |
933 | mvaddch(ln, cl + cf->menu->width + 4, ACS_VLINE); | |
934 | } | |
935 | ||
6fed9601 | 936 | if (cf->menu->idx == idx) { |
8c3a5a44 | 937 | standout(); |
8a726114 | 938 | mvprintw(ln, cl, vert ? " %s " : "[%s]", buf); |
8c3a5a44 KZ |
939 | standend(); |
940 | if (d->desc) | |
b1f58330 | 941 | ui_hint(d->desc); |
8c3a5a44 | 942 | } else |
8a726114 | 943 | mvprintw(ln, cl, vert ? " %s " : "[%s]", buf); |
6dbe3af9 KZ |
944 | } |
945 | ||
7556c944 KZ |
946 | static void ui_clean_menu(struct cfdisk *cf) |
947 | { | |
948 | size_t i; | |
949 | size_t nlines; | |
950 | struct cfdisk_menu *m = cf->menu; | |
951 | size_t ln = menuitem_get_line(cf, 0); | |
952 | ||
953 | if (m->vertical) | |
954 | nlines = m->page_sz ? m->page_sz : m->nitems; | |
955 | else | |
956 | nlines = menuitem_get_line(cf, m->nitems); | |
957 | ||
958 | for (i = ln; i <= ln + nlines; i++) { | |
959 | move(i, 0); | |
960 | clrtoeol(); | |
961 | } | |
962 | if (m->vertical) { | |
963 | move(ln - 1, 0); | |
964 | clrtoeol(); | |
965 | } | |
966 | ui_clean_hint(); | |
967 | } | |
968 | ||
8c3a5a44 KZ |
969 | static void ui_draw_menu(struct cfdisk *cf) |
970 | { | |
3b411726 | 971 | struct cfdisk_menuitem *d; |
8a726114 | 972 | struct cfdisk_menu *m; |
6fed9601 KZ |
973 | size_t i = 0; |
974 | size_t ln = menuitem_get_line(cf, 0); | |
975 | size_t nlines; | |
7eda085c | 976 | |
8c3a5a44 KZ |
977 | assert(cf); |
978 | assert(cf->menu); | |
fd6b7a7f | 979 | |
88141067 | 980 | DBG(FRONTEND, ul_debug("ui: menu: draw start")); |
6dbe3af9 | 981 | |
7556c944 | 982 | ui_clean_menu(cf); |
8a726114 KZ |
983 | m = cf->menu; |
984 | ||
6fed9601 KZ |
985 | if (m->vertical) |
986 | nlines = m->page_sz ? m->page_sz : m->nitems; | |
987 | else | |
988 | nlines = menuitem_get_line(cf, m->nitems); | |
989 | ||
63128bbf KZ |
990 | if (m->ignore_cb) |
991 | menu_update_ignore(cf); | |
00b4f26a | 992 | i = 0; |
8c3a5a44 KZ |
993 | while ((d = menu_get_menuitem(cf, i))) |
994 | ui_draw_menuitem(cf, d, i++); | |
6dbe3af9 | 995 | |
8a726114 | 996 | if (m->vertical) { |
8a726114 | 997 | size_t cl = menuitem_get_column(cf, 0); |
6fed9601 | 998 | size_t curpg = m->page_sz ? m->idx / m->page_sz : 0; |
8a726114 | 999 | |
6fed9601 | 1000 | /* corners and horizontal lines */ |
8a726114 KZ |
1001 | mvaddch(ln - 1, cl - 1, ACS_ULCORNER); |
1002 | mvaddch(ln + nlines, cl - 1, ACS_LLCORNER); | |
1003 | ||
1004 | for (i = 0; i < m->width + 4; i++) { | |
1005 | mvaddch(ln - 1, cl + i, ACS_HLINE); | |
1006 | mvaddch(ln + nlines, cl + i, ACS_HLINE); | |
1007 | } | |
6fed9601 | 1008 | |
8a726114 KZ |
1009 | mvaddch(ln - 1, cl + i, ACS_URCORNER); |
1010 | mvaddch(ln + nlines, cl + i, ACS_LRCORNER); | |
1011 | ||
6fed9601 KZ |
1012 | /* draw also lines around empty lines on last page */ |
1013 | if (m->page_sz && | |
1014 | m->nitems / m->page_sz == m->idx / m->page_sz) { | |
1015 | for (i = m->nitems % m->page_sz + 1; i <= m->page_sz; i++) { | |
1016 | mvaddch(i, cl - 1, ACS_VLINE); | |
1017 | mvaddch(i, cl + cf->menu->width + 4, ACS_VLINE); | |
1018 | } | |
1019 | } | |
8a726114 KZ |
1020 | if (m->title) { |
1021 | attron(A_BOLD); | |
1022 | mvprintw(ln - 1, cl, " %s ", m->title); | |
1023 | attroff(A_BOLD); | |
1024 | } | |
6fed9601 KZ |
1025 | if (curpg != 0) |
1026 | mvaddch(ln - 1, cl + m->width + 3, ACS_UARROW); | |
1027 | if (m->page_sz && curpg < m->nitems / m->page_sz) | |
1028 | mvaddch(ln + nlines, cl + m->width + 3, ACS_DARROW); | |
8a726114 KZ |
1029 | } |
1030 | ||
88141067 | 1031 | DBG(FRONTEND, ul_debug("ui: menu: draw end.")); |
6dbe3af9 KZ |
1032 | } |
1033 | ||
8c3a5a44 KZ |
1034 | static void ui_menu_goto(struct cfdisk *cf, int where) |
1035 | { | |
3b411726 | 1036 | struct cfdisk_menuitem *d; |
8c3a5a44 KZ |
1037 | size_t old; |
1038 | ||
f019ce1f KZ |
1039 | /* stop and begin/end for vertical menus */ |
1040 | if (cf->menu->vertical) { | |
1041 | if (where < 0) | |
1042 | where = 0; | |
1043 | else if (where > (int) cf->menu->nitems - 1) | |
1044 | where = cf->menu->nitems - 1; | |
1045 | } else { | |
1046 | /* continue from begin/end */ | |
1047 | if (where < 0) | |
1048 | where = cf->menu->nitems - 1; | |
1049 | else if ((size_t) where > cf->menu->nitems - 1) | |
1050 | where = 0; | |
1051 | } | |
6fed9601 | 1052 | if ((size_t) where == cf->menu->idx) |
8c3a5a44 | 1053 | return; |
6dbe3af9 | 1054 | |
1af8003b KZ |
1055 | ui_clean_info(); |
1056 | ||
6fed9601 KZ |
1057 | old = cf->menu->idx; |
1058 | cf->menu->idx = where; | |
1059 | ||
1060 | if (!menuitem_on_page(cf, old)) { | |
1061 | ui_draw_menu(cf); | |
1062 | return; | |
1063 | } | |
6dbe3af9 | 1064 | |
8c3a5a44 KZ |
1065 | d = menu_get_menuitem(cf, old); |
1066 | ui_draw_menuitem(cf, d, old); | |
6dbe3af9 | 1067 | |
8c3a5a44 KZ |
1068 | d = menu_get_menuitem(cf, where); |
1069 | ui_draw_menuitem(cf, d, where); | |
6dbe3af9 KZ |
1070 | } |
1071 | ||
ac27ea5c KZ |
1072 | static int ui_menu_move(struct cfdisk *cf, int key) |
1073 | { | |
f019ce1f KZ |
1074 | struct cfdisk_menu *m; |
1075 | ||
ac27ea5c KZ |
1076 | assert(cf); |
1077 | assert(cf->menu); | |
1078 | ||
7556c944 KZ |
1079 | if (key == ERR) |
1080 | return 0; /* ignore errors */ | |
1081 | ||
f019ce1f KZ |
1082 | m = cf->menu; |
1083 | ||
88141067 | 1084 | DBG(FRONTEND, ul_debug("ui: menu move key >%c<.", key)); |
f019ce1f KZ |
1085 | |
1086 | if (m->vertical) | |
ac27ea5c KZ |
1087 | { |
1088 | switch (key) { | |
1089 | case KEY_DOWN: | |
1090 | case '\016': /* ^N */ | |
1091 | case 'j': /* Vi-like alternative */ | |
f019ce1f | 1092 | ui_menu_goto(cf, m->idx + 1); |
ac27ea5c KZ |
1093 | return 0; |
1094 | case KEY_UP: | |
1095 | case '\020': /* ^P */ | |
1096 | case 'k': /* Vi-like alternative */ | |
f019ce1f | 1097 | ui_menu_goto(cf, (int) m->idx - 1); |
ac27ea5c | 1098 | return 0; |
f019ce1f KZ |
1099 | case KEY_PPAGE: |
1100 | if (m->page_sz) { | |
1101 | ui_menu_goto(cf, (int) m->idx - m->page_sz); | |
1102 | return 0; | |
1103 | } | |
d02c2035 | 1104 | /* fallthrough */ |
ac27ea5c KZ |
1105 | case KEY_HOME: |
1106 | ui_menu_goto(cf, 0); | |
1107 | return 0; | |
f019ce1f KZ |
1108 | case KEY_NPAGE: |
1109 | if (m->page_sz) { | |
1110 | ui_menu_goto(cf, m->idx + m->page_sz); | |
1111 | return 0; | |
1112 | } | |
d02c2035 | 1113 | /* fallthrough */ |
ac27ea5c | 1114 | case KEY_END: |
f019ce1f | 1115 | ui_menu_goto(cf, m->nitems); |
ac27ea5c KZ |
1116 | return 0; |
1117 | } | |
1118 | } else { | |
1119 | switch (key) { | |
1120 | case KEY_RIGHT: | |
1121 | case '\t': | |
f019ce1f | 1122 | ui_menu_goto(cf, m->idx + 1); |
ac27ea5c KZ |
1123 | return 0; |
1124 | case KEY_LEFT: | |
1125 | #ifdef KEY_BTAB | |
1126 | case KEY_BTAB: | |
1127 | #endif | |
f019ce1f | 1128 | ui_menu_goto(cf, (int) m->idx - 1); |
ac27ea5c KZ |
1129 | return 0; |
1130 | } | |
1131 | } | |
1132 | ||
1133 | return 1; /* key irrelevant for menu move */ | |
1134 | } | |
1135 | ||
7556c944 KZ |
1136 | /* but don't call me from ui_run(), this is for pop-up menus only */ |
1137 | static void ui_menu_resize(struct cfdisk *cf) | |
1138 | { | |
1139 | resize(); | |
1140 | ui_clean_menu(cf); | |
1141 | menu_refresh_size(cf); | |
1142 | ui_draw_menu(cf); | |
1143 | refresh(); | |
1144 | } | |
1145 | ||
a6f69126 KZ |
1146 | static int partition_on_page(struct cfdisk *cf, size_t i) |
1147 | { | |
1148 | if (cf->page_sz == 0 || | |
1149 | cf->lines_idx / cf->page_sz == i / cf->page_sz) | |
1150 | return 1; | |
1151 | return 0; | |
1152 | } | |
1153 | ||
8c3a5a44 KZ |
1154 | static void ui_draw_partition(struct cfdisk *cf, size_t i) |
1155 | { | |
1156 | int ln = TABLE_START_LINE + 1 + i; /* skip table header */ | |
1157 | int cl = ARROW_CURSOR_WIDTH; /* we need extra space for cursor */ | |
a6f69126 KZ |
1158 | int cur = cf->lines_idx == i; |
1159 | size_t curpg = 0; | |
1160 | ||
1161 | if (cf->page_sz) { | |
1162 | if (!partition_on_page(cf, i)) | |
1163 | return; | |
1164 | ln = TABLE_START_LINE + (i % cf->page_sz) + 1; | |
1165 | curpg = cf->lines_idx / cf->page_sz; | |
1166 | } | |
6dbe3af9 | 1167 | |
88141067 | 1168 | DBG(FRONTEND, ul_debug( |
a6f69126 KZ |
1169 | "ui: draw partition %zu [page_sz=%zu, " |
1170 | "line=%d, idx=%zu]", | |
1171 | i, cf->page_sz, ln, cf->lines_idx)); | |
6dbe3af9 | 1172 | |
a6f69126 KZ |
1173 | if (cur) { |
1174 | attron(A_REVERSE); | |
8c3a5a44 KZ |
1175 | mvaddstr(ln, 0, ARROW_CURSOR_STRING); |
1176 | mvaddstr(ln, cl, cf->lines[i + 1]); | |
a6f69126 | 1177 | attroff(A_REVERSE); |
6dbe3af9 | 1178 | } else { |
45333e9d KZ |
1179 | int at = 0; |
1180 | ||
f1512be8 | 1181 | if (colors_wanted() && is_freespace(cf, i)) { |
45333e9d KZ |
1182 | attron(COLOR_PAIR(CFDISK_CL_FREESPACE)); |
1183 | at = 1; | |
1184 | } | |
8c3a5a44 KZ |
1185 | mvaddstr(ln, 0, ARROW_CURSOR_DUMMY); |
1186 | mvaddstr(ln, cl, cf->lines[i + 1]); | |
45333e9d KZ |
1187 | if (at) |
1188 | attroff(COLOR_PAIR(CFDISK_CL_FREESPACE)); | |
6dbe3af9 KZ |
1189 | } |
1190 | ||
a6f69126 KZ |
1191 | if ((size_t) ln == MENU_START_LINE - 1 && |
1192 | cf->page_sz && curpg < cf->nlines / cf->page_sz) { | |
1193 | if (cur) | |
1194 | attron(A_REVERSE); | |
1195 | mvaddch(ln, COLS - 1, ACS_DARROW); | |
1196 | mvaddch(ln, 0, ACS_DARROW); | |
1197 | if (cur) | |
1198 | attroff(A_REVERSE); | |
1199 | } | |
6dbe3af9 KZ |
1200 | } |
1201 | ||
8c3a5a44 KZ |
1202 | static int ui_draw_table(struct cfdisk *cf) |
1203 | { | |
1204 | int cl = ARROW_CURSOR_WIDTH; | |
1205 | size_t i, nparts = fdisk_table_get_nents(cf->table); | |
a6f69126 | 1206 | size_t curpg = cf->page_sz ? cf->lines_idx / cf->page_sz : 0; |
7eda085c | 1207 | |
88141067 | 1208 | DBG(FRONTEND, ul_debug("ui: draw table")); |
6dbe3af9 | 1209 | |
a6f69126 KZ |
1210 | for (i = TABLE_START_LINE; i <= TABLE_START_LINE + cf->page_sz; i++) { |
1211 | move(i, 0); | |
1212 | clrtoeol(); | |
1213 | } | |
6dbe3af9 | 1214 | |
b2301179 KZ |
1215 | if ((size_t) cf->lines_idx > nparts - 1) |
1216 | cf->lines_idx = nparts ? nparts - 1 : 0; | |
1217 | ||
8c3a5a44 KZ |
1218 | /* print header */ |
1219 | attron(A_BOLD); | |
1220 | mvaddstr(TABLE_START_LINE, cl, cf->lines[0]); | |
1221 | attroff(A_BOLD); | |
6dbe3af9 | 1222 | |
8c3a5a44 KZ |
1223 | /* print partitions */ |
1224 | for (i = 0; i < nparts; i++) | |
1225 | ui_draw_partition(cf, i); | |
6dbe3af9 | 1226 | |
a6f69126 KZ |
1227 | if (curpg != 0) { |
1228 | mvaddch(TABLE_START_LINE, COLS - 1, ACS_UARROW); | |
1229 | mvaddch(TABLE_START_LINE, 0, ACS_UARROW); | |
1230 | } | |
1231 | if (cf->page_sz && curpg < cf->nlines / cf->page_sz) { | |
1232 | mvaddch(MENU_START_LINE - 1, COLS - 1, ACS_DARROW); | |
1233 | mvaddch(MENU_START_LINE - 1, 0, ACS_DARROW); | |
1234 | } | |
8c3a5a44 | 1235 | return 0; |
6dbe3af9 KZ |
1236 | } |
1237 | ||
8c3a5a44 KZ |
1238 | static int ui_table_goto(struct cfdisk *cf, int where) |
1239 | { | |
1240 | size_t old; | |
1241 | size_t nparts = fdisk_table_get_nents(cf->table); | |
6dbe3af9 | 1242 | |
88141067 | 1243 | DBG(FRONTEND, ul_debug("ui: goto table %d", where)); |
6dbe3af9 | 1244 | |
8c3a5a44 KZ |
1245 | if (where < 0) |
1246 | where = 0; | |
1247 | else if ((size_t) where > nparts - 1) | |
1248 | where = nparts - 1; | |
6dbe3af9 | 1249 | |
8c3a5a44 KZ |
1250 | if ((size_t) where == cf->lines_idx) |
1251 | return 0; | |
6dbe3af9 | 1252 | |
8c3a5a44 KZ |
1253 | old = cf->lines_idx; |
1254 | cf->lines_idx = where; | |
6dbe3af9 | 1255 | |
a6f69126 KZ |
1256 | if (!partition_on_page(cf, old) ||!partition_on_page(cf, where)) |
1257 | ui_draw_table(cf); | |
1258 | else { | |
1259 | ui_draw_partition(cf, old); /* cleanup old */ | |
1260 | ui_draw_partition(cf, where); /* draw new */ | |
1261 | } | |
1af8003b | 1262 | ui_clean_info(); |
8c3a5a44 KZ |
1263 | ui_draw_menu(cf); |
1264 | refresh(); | |
1265 | return 0; | |
6dbe3af9 KZ |
1266 | } |
1267 | ||
8c3a5a44 KZ |
1268 | static int ui_refresh(struct cfdisk *cf) |
1269 | { | |
1270 | char *id = NULL; | |
1271 | uint64_t bytes = cf->cxt->total_sectors * cf->cxt->sector_size; | |
1272 | char *strsz = size_to_human_string(SIZE_SUFFIX_SPACE | |
1273 | | SIZE_SUFFIX_3LETTER, bytes); | |
1274 | erase(); | |
1275 | ||
83fa0f80 | 1276 | if (!ui_enabled) |
8c3a5a44 KZ |
1277 | return -EINVAL; |
1278 | ||
1279 | /* header */ | |
1280 | attron(A_BOLD); | |
1af8003b | 1281 | ui_center(0, _("Disk: %s"), cf->cxt->dev_path); |
8c3a5a44 | 1282 | attroff(A_BOLD); |
1af8003b | 1283 | ui_center(1, _("Size: %s, %ju bytes, %ju sectors"), |
8c3a5a44 KZ |
1284 | strsz, bytes, (uintmax_t) cf->cxt->total_sectors); |
1285 | if (fdisk_get_disklabel_id(cf->cxt, &id) == 0 && id) | |
1af8003b | 1286 | ui_center(2, _("Label: %s, identifier: %s"), |
8c3a5a44 | 1287 | cf->cxt->label->name, id); |
7eda085c | 1288 | else |
33867d3b | 1289 | ui_center(2, _("Label: %s"), cf->cxt->label->name); |
8c3a5a44 | 1290 | free(strsz); |
6dbe3af9 | 1291 | |
8c3a5a44 KZ |
1292 | ui_draw_table(cf); |
1293 | ui_draw_menu(cf); | |
6dbe3af9 | 1294 | refresh(); |
8c3a5a44 | 1295 | return 0; |
6dbe3af9 KZ |
1296 | } |
1297 | ||
b1f58330 KZ |
1298 | static ssize_t ui_get_string(struct cfdisk *cf, const char *prompt, |
1299 | const char *hint, char *buf, size_t len) | |
1300 | { | |
1301 | size_t cells = 0; | |
1302 | ssize_t i = 0, rc = -1; | |
b1f58330 KZ |
1303 | int ln = MENU_START_LINE, cl = 1; |
1304 | ||
1305 | assert(cf); | |
1306 | assert(buf); | |
1307 | assert(len); | |
1308 | ||
1309 | move(ln, 0); | |
1310 | clrtoeol(); | |
1311 | ||
1312 | if (prompt) { | |
f1512be8 | 1313 | mvaddstr(ln, cl, (char *) prompt); |
b1f58330 KZ |
1314 | cl += mbs_safe_width(prompt); |
1315 | } | |
1316 | ||
1317 | /* default value */ | |
1318 | if (*buf) { | |
1319 | i = strlen(buf); | |
1320 | cells = mbs_safe_width(buf); | |
1321 | mvaddstr(ln, cl, buf); | |
1322 | } | |
1323 | ||
1324 | if (hint) | |
1325 | ui_hint(hint); | |
1326 | else | |
1327 | ui_clean_hint(); | |
1328 | ||
1329 | move(ln, cl + cells); | |
1330 | curs_set(1); | |
1331 | refresh(); | |
1332 | ||
1333 | while (1) { | |
1334 | #if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \ | |
1335 | defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR) | |
a08fa9ab | 1336 | wint_t c; |
b1f58330 KZ |
1337 | if (get_wch(&c) == ERR) { |
1338 | #else | |
a08fa9ab | 1339 | int c; |
b1f58330 KZ |
1340 | if ((c = getch()) == ERR) { |
1341 | #endif | |
7556c944 KZ |
1342 | if (ui_resize) { |
1343 | resize(); | |
1344 | continue; | |
1345 | } | |
b1f58330 KZ |
1346 | if (!isatty(STDIN_FILENO)) |
1347 | exit(2); | |
1348 | else | |
1349 | goto done; | |
1350 | } | |
1351 | if (c == '\r' || c == '\n' || c == KEY_ENTER) | |
1352 | break; | |
1353 | ||
1354 | switch (c) { | |
1355 | case KEY_ESC: | |
1356 | rc = -CFDISK_ERR_ESC; | |
1357 | goto done; | |
1358 | case KEY_DELETE: | |
1359 | case '\b': | |
1360 | case KEY_BACKSPACE: | |
1361 | if (i > 0) { | |
1362 | cells--; | |
1363 | i = mbs_truncate(buf, &cells); | |
1364 | if (i < 0) | |
1365 | goto done; | |
1366 | mvaddch(ln, cl + cells, ' '); | |
1367 | move(ln, cl + cells); | |
1368 | } else | |
1369 | beep(); | |
1370 | break; | |
1371 | default: | |
1372 | #if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR) | |
1373 | if (i + 1 < (ssize_t) len && iswprint(c)) { | |
1374 | wchar_t wc = (wchar_t) c; | |
1375 | char s[MB_CUR_MAX + 1]; | |
1376 | int sz = wctomb(s, wc); | |
1377 | ||
1378 | if (sz > 0 && sz + i < (ssize_t) len) { | |
1379 | s[sz] = '\0'; | |
1380 | mvaddnstr(ln, cl + cells, s, sz); | |
1381 | memcpy(buf + i, s, sz); | |
1382 | i += sz; | |
1383 | buf[i] = '\0'; | |
1384 | cells += wcwidth(wc); | |
1385 | } else | |
1386 | beep(); | |
1387 | } | |
1388 | #else | |
1389 | if (i + 1 < (ssize_t) len && isprint(c)) { | |
1390 | mvaddch(ln, cl + cells, c); | |
f0fb84ab RM |
1391 | buf[i++] = c; |
1392 | buf[i] = '\0'; | |
b1f58330 KZ |
1393 | cells++; |
1394 | } | |
1395 | #endif | |
1396 | else | |
1397 | beep(); | |
1398 | } | |
1399 | refresh(); | |
1400 | } | |
1401 | ||
1402 | rc = i; /* success */ | |
1403 | done: | |
1404 | move(ln, 0); | |
1405 | clrtoeol(); | |
1406 | curs_set(0); | |
1407 | refresh(); | |
1408 | ||
1409 | return rc; | |
1410 | } | |
1411 | ||
1412 | /* @res is default value as well as result in bytes */ | |
1413 | static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res, | |
1414 | uintmax_t low, uintmax_t up) | |
1415 | { | |
1416 | char buf[128]; | |
1417 | uintmax_t user = 0; | |
1418 | ssize_t rc; | |
1419 | char *dflt = size_to_human_string(0, *res); | |
1420 | ||
88141067 | 1421 | DBG(FRONTEND, ul_debug("ui: get_size (default=%ju)", *res)); |
b1f58330 KZ |
1422 | |
1423 | ui_clean_info(); | |
1424 | ||
1425 | do { | |
91ba41ca | 1426 | int pwr = 0, insec = 0; |
b1f58330 KZ |
1427 | |
1428 | snprintf(buf, sizeof(buf), "%s", dflt); | |
1429 | rc = ui_get_string(cf, prompt, | |
5139eca7 | 1430 | _("May be followed by {M,B,G,T}iB " |
91ba41ca | 1431 | "(the \"iB\" is optional) or S for sectors."), |
b1f58330 KZ |
1432 | buf, sizeof(buf)); |
1433 | if (rc == 0) { | |
1434 | ui_warnx(_("Please, specify size.")); | |
1435 | continue; /* nothing specified */ | |
1436 | } else if (rc == -CFDISK_ERR_ESC) | |
1437 | break; /* cancel dialog */ | |
1438 | ||
91ba41ca KZ |
1439 | if (strcmp(buf, dflt) == 0) |
1440 | user = *res, rc = 0; /* no change, use default */ | |
1441 | else { | |
1442 | size_t len = strlen(buf); | |
bfdb3244 | 1443 | if (buf[len - 1] == 'S' || buf[len - 1] == 's') { |
91ba41ca KZ |
1444 | insec = 1; |
1445 | buf[len - 1] = '\0'; | |
1446 | } | |
1447 | rc = parse_size(buf, &user, &pwr); /* parse */ | |
1448 | } | |
1449 | ||
b1f58330 | 1450 | if (rc == 0) { |
88141067 | 1451 | DBG(FRONTEND, ul_debug("ui: get_size user=%ju, power=%d, sectors=%s", |
91ba41ca KZ |
1452 | user, pwr, insec ? "yes" : "no")); |
1453 | if (insec) | |
1454 | user *= cf->cxt->sector_size; | |
b1f58330 KZ |
1455 | if (user < low) { |
1456 | ui_warnx(_("Minimal size is %ju"), low); | |
1457 | rc = -ERANGE; | |
1458 | } | |
1459 | if (user > up && pwr && user < up + (1ULL << pwr * 10)) | |
1460 | /* ignore when the user specified size overflow | |
1461 | * with in range specified by suffix (e.g. MiB) */ | |
1462 | user = up; | |
1463 | ||
1464 | if (user > up) { | |
1465 | ui_warnx(_("Maximal size is %ju bytes."), up); | |
1466 | rc = -ERANGE; | |
1467 | } | |
2cec7949 KZ |
1468 | } else |
1469 | ui_warnx(_("Failed to parse size.")); | |
b1f58330 KZ |
1470 | } while (rc != 0); |
1471 | ||
1472 | if (rc == 0) | |
1473 | *res = user; | |
1474 | free(dflt); | |
1475 | ||
88141067 | 1476 | DBG(FRONTEND, ul_debug("ui: get_size (result=%ju, rc=%zd)", *res, rc)); |
b1f58330 KZ |
1477 | return rc; |
1478 | } | |
1479 | ||
6d6d9c1a KZ |
1480 | static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf, |
1481 | struct fdisk_parttype *cur) | |
6fed9601 | 1482 | { |
3b411726 | 1483 | struct cfdisk_menuitem *d, *cm; |
6d6d9c1a | 1484 | size_t i = 0, nitems, idx = 0; |
6fed9601 | 1485 | struct fdisk_parttype *t = NULL; |
6d6d9c1a | 1486 | int has_typestr = 0; |
6fed9601 | 1487 | |
88141067 | 1488 | DBG(FRONTEND, ul_debug("ui: asking for parttype.")); |
6fed9601 KZ |
1489 | |
1490 | /* create cfdisk menu according to label types, note that the | |
1491 | * last cm[] item has to be empty -- so nitems + 1 */ | |
1492 | nitems = cf->cxt->label->nparttypes; | |
6d6d9c1a KZ |
1493 | if (!nitems) |
1494 | return NULL; | |
3b411726 | 1495 | cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem)); |
6fed9601 KZ |
1496 | if (!cm) |
1497 | return NULL; | |
1498 | ||
6d6d9c1a KZ |
1499 | has_typestr = cf->cxt->label->parttypes[0].typestr && |
1500 | *cf->cxt->label->parttypes[0].typestr; | |
1501 | ||
6fed9601 KZ |
1502 | for (i = 0; i < nitems; i++) { |
1503 | struct fdisk_parttype *x = &cf->cxt->label->parttypes[i]; | |
6d6d9c1a | 1504 | char *name; |
6fed9601 | 1505 | |
6d6d9c1a KZ |
1506 | if (!x || !x->name) |
1507 | continue; | |
6fed9601 | 1508 | cm[i].userdata = x; |
6d6d9c1a | 1509 | if (!has_typestr) |
86407f2c | 1510 | xasprintf(&name, "%2x %s", x->type, _(x->name)); |
6d6d9c1a | 1511 | else { |
86407f2c | 1512 | name = (char *) _(x->name); |
6d6d9c1a KZ |
1513 | cm[i].desc = x->typestr; |
1514 | } | |
1515 | cm[i].name = name; | |
1516 | if (x == cur) | |
1517 | idx = i; | |
6fed9601 KZ |
1518 | } |
1519 | ||
1520 | /* make the new menu active */ | |
1521 | menu_push(cf, cm); | |
1522 | cf->menu->vertical = 1; | |
6d6d9c1a | 1523 | cf->menu->idx = idx; |
6fed9601 KZ |
1524 | menu_set_title(cf->menu, _("Select partition type")); |
1525 | ui_draw_menu(cf); | |
1526 | refresh(); | |
1527 | ||
1528 | do { | |
1529 | int key = getch(); | |
f019ce1f | 1530 | |
7556c944 KZ |
1531 | if (ui_resize) |
1532 | ui_menu_resize(cf); | |
6fed9601 KZ |
1533 | if (ui_menu_move(cf, key) == 0) |
1534 | continue; | |
f019ce1f | 1535 | |
6fed9601 KZ |
1536 | switch (key) { |
1537 | case KEY_ENTER: | |
1538 | case '\n': | |
1539 | case '\r': | |
1540 | d = menu_get_menuitem(cf, cf->menu->idx); | |
1541 | if (d) | |
1542 | t = (struct fdisk_parttype *) d->userdata; | |
1543 | goto done; | |
ad5d45c0 | 1544 | case KEY_ESC: |
6fed9601 KZ |
1545 | case 'q': |
1546 | case 'Q': | |
1547 | goto done; | |
1548 | } | |
1549 | } while (1); | |
1550 | ||
1551 | done: | |
1552 | menu_pop(cf); | |
6d6d9c1a KZ |
1553 | if (!has_typestr) { |
1554 | for (i = 0; i < nitems; i++) | |
1555 | free((char *) cm[i].name); | |
1556 | } | |
6fed9601 | 1557 | free(cm); |
88141067 | 1558 | DBG(FRONTEND, ul_debug("ui: get parrtype done [type=%s] ", t ? t->name : NULL)); |
6fed9601 KZ |
1559 | return t; |
1560 | } | |
1561 | ||
7aa0d529 KZ |
1562 | /* prints menu with libfdisk labels and waits for users response */ |
1563 | static int ui_create_label(struct cfdisk *cf) | |
1564 | { | |
3b411726 | 1565 | struct cfdisk_menuitem *d, *cm; |
7aa0d529 KZ |
1566 | int rc = 1; |
1567 | size_t i = 0, nitems; | |
1568 | struct fdisk_label *lb = NULL; | |
1569 | ||
1570 | assert(cf); | |
1571 | ||
88141067 | 1572 | DBG(FRONTEND, ul_debug("ui: asking for new disklabe.")); |
7aa0d529 KZ |
1573 | |
1574 | /* create cfdisk menu according to libfdisk labels, note that the | |
1575 | * last cm[] item has to be empty -- so nitems + 1 */ | |
1576 | nitems = fdisk_context_get_nlabels(cf->cxt); | |
3b411726 | 1577 | cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem)); |
7aa0d529 | 1578 | |
dd626abd KZ |
1579 | while (fdisk_context_next_label(cf->cxt, &lb) == 0) { |
1580 | if (fdisk_label_is_disabled(lb) || strcmp(lb->name, "bsd") == 0) | |
1581 | continue; | |
1582 | cm[i++].name = lb->name; | |
7aa0d529 KZ |
1583 | } |
1584 | ||
1585 | erase(); | |
1586 | ui_center(LINES - 4, | |
1587 | _("Device does not contain a recognized partition table.")); | |
1588 | ui_center(LINES - 3, | |
1589 | _("Please, select a type to create a new disk label.")); | |
1590 | ||
1591 | /* make the new menu active */ | |
63128bbf | 1592 | menu_push(cf, cm); |
7aa0d529 KZ |
1593 | cf->menu->vertical = 1; |
1594 | menu_set_title(cf->menu, _("Select label type")); | |
1595 | ui_draw_menu(cf); | |
1596 | refresh(); | |
1597 | ||
1598 | do { | |
1599 | int key = getch(); | |
7556c944 KZ |
1600 | |
1601 | if (ui_resize) | |
1602 | ui_menu_resize(cf); | |
7aa0d529 KZ |
1603 | if (ui_menu_move(cf, key) == 0) |
1604 | continue; | |
1605 | switch (key) { | |
1606 | case KEY_ENTER: | |
1607 | case '\n': | |
1608 | case '\r': | |
6fed9601 | 1609 | d = menu_get_menuitem(cf, cf->menu->idx); |
7aa0d529 KZ |
1610 | if (d) |
1611 | rc = fdisk_create_disklabel(cf->cxt, d->name); | |
1612 | goto done; | |
ad5d45c0 | 1613 | case KEY_ESC: |
7aa0d529 KZ |
1614 | case 'q': |
1615 | case 'Q': | |
1616 | goto done; | |
1617 | } | |
1618 | } while (1); | |
1619 | ||
1620 | done: | |
1621 | menu_pop(cf); | |
1622 | free(cm); | |
88141067 | 1623 | DBG(FRONTEND, ul_debug("ui: create label done [rc=%d] ", rc)); |
7aa0d529 KZ |
1624 | return rc; |
1625 | } | |
1626 | ||
314a0dd8 KZ |
1627 | static int ui_help(void) |
1628 | { | |
1629 | size_t i; | |
314a0dd8 KZ |
1630 | static const char *help[] = { |
1631 | N_("Help Screen for cfdisk"), | |
1632 | "", | |
1633 | N_("This is cfdisk, a curses based disk partitioning program, which"), | |
1634 | N_("allows you to create, delete and modify partitions on your hard"), | |
1635 | N_("disk drive."), | |
1636 | "", | |
1637 | N_("Copyright (C) 2014 Karel Zak <kzak@redhat.com> "), | |
1638 | N_("Based on the original cfdisk from Kevin E. Martin & aeb."), | |
1639 | "", | |
1640 | N_("Command Meaning"), | |
1641 | N_("------- -------"), | |
1642 | N_(" b Toggle bootable flag of the current partition"), | |
1643 | N_(" d Delete the current partition"), | |
1644 | N_(" h Print this screen"), | |
1645 | N_(" n Create new partition from free space"), | |
1646 | N_(" q Quit program without writing partition table"), | |
1647 | N_(" t Change the partition type"), | |
33f7c502 | 1648 | N_(" s Fix partitions order (only when in disarray)"), |
314a0dd8 KZ |
1649 | N_(" W Write partition table to disk (must enter upper case W)"), |
1650 | N_(" Since this might destroy data on the disk, you must"), | |
1651 | N_(" either confirm or deny the write by entering `yes' or"), | |
1652 | N_(" `no'"), | |
1653 | N_("Up Arrow Move cursor to the previous partition"), | |
1654 | N_("Down Arrow Move cursor to the next partition"), | |
1655 | N_("Left Arrow Move cursor to the previous menu item"), | |
1656 | N_("Right Arrow Move cursor to the next menu item"), | |
1657 | ||
1658 | "", | |
1659 | N_("Note: All of the commands can be entered with either upper or lower"), | |
1660 | N_("case letters (except for Writes)."), | |
1661 | "", | |
1662 | N_("Use lsblk(8) or partx(8) to see more details about the device.") | |
1663 | }; | |
1664 | ||
1665 | erase(); | |
314a0dd8 KZ |
1666 | for (i = 0; i < ARRAY_SIZE(help); i++) |
1667 | mvaddstr(i, 1, _(help[i])); | |
1668 | ||
1669 | ui_info(_("Press a key to continue.")); | |
7556c944 | 1670 | |
314a0dd8 KZ |
1671 | getch(); |
1672 | return 0; | |
1673 | } | |
1674 | ||
63128bbf KZ |
1675 | /* TODO: use @sz, now 128bytes */ |
1676 | static int main_menu_ignore_keys(struct cfdisk *cf, char *ignore, | |
1677 | size_t sz __attribute__((__unused__))) | |
1678 | { | |
1679 | struct fdisk_partition *pa = get_current_partition(cf); | |
1680 | size_t i = 0; | |
1681 | ||
1682 | if (!pa) | |
1683 | return 0; | |
1684 | if (fdisk_partition_is_freespace(pa)) { | |
1685 | ignore[i++] = 'd'; /* delete */ | |
1686 | ignore[i++] = 't'; /* set type */ | |
1687 | ignore[i++] = 'b'; /* set bootable */ | |
1688 | } else { | |
1689 | ignore[i++] = 'n'; | |
1690 | if (!fdisk_is_disklabel(cf->cxt, DOS) && | |
1691 | !fdisk_is_disklabel(cf->cxt, SGI)) | |
1692 | ignore[i++] = 'b'; | |
1693 | } | |
e146ae4e | 1694 | |
d5314bf5 KZ |
1695 | if (!cf->wrong_order) |
1696 | ignore[i++] = 's'; | |
e146ae4e KZ |
1697 | if (fdisk_context_is_readonly(cf->cxt)) |
1698 | ignore[i++] = 'W'; | |
63128bbf KZ |
1699 | return i; |
1700 | } | |
1701 | ||
1702 | ||
ed8a13e6 KZ |
1703 | /* returns: error: < 0, success: 0, quit: 1 */ |
1704 | static int main_menu_action(struct cfdisk *cf, int key) | |
1705 | { | |
1706 | size_t n; | |
d5314bf5 | 1707 | int ref = 0, rc, org_order = cf->wrong_order; |
ed8a13e6 KZ |
1708 | const char *info = NULL, *warn = NULL; |
1709 | struct fdisk_partition *pa; | |
1710 | ||
1711 | assert(cf); | |
1712 | assert(cf->cxt); | |
1713 | assert(cf->menu); | |
1714 | ||
1715 | if (key == 0) { | |
3b411726 | 1716 | struct cfdisk_menuitem *d = menu_get_menuitem(cf, cf->menu->idx); |
ed8a13e6 KZ |
1717 | if (!d) |
1718 | return 0; | |
1719 | key = d->key; | |
1720 | ||
1721 | } else if (key != 'w' && key != 'W') | |
1722 | key = tolower(key); /* case insensitive except 'W'rite */ | |
1723 | ||
88141067 | 1724 | DBG(FRONTEND, ul_debug("ui: main menu action: key=%c", key)); |
ed8a13e6 KZ |
1725 | |
1726 | if (cf->menu->ignore && strchr(cf->menu->ignore, key)) { | |
88141067 | 1727 | DBG(FRONTEND, ul_debug(" ignore '%c'", key)); |
ed8a13e6 KZ |
1728 | return 0; |
1729 | } | |
1730 | ||
1731 | pa = get_current_partition(cf); | |
1732 | n = fdisk_partition_get_partno(pa); | |
1733 | ||
88141067 | 1734 | DBG(FRONTEND, ul_debug("menu action on %p", pa)); |
7b1ffbc4 KZ |
1735 | ui_clean_hint(); |
1736 | ui_clean_info(); | |
ed8a13e6 KZ |
1737 | |
1738 | switch (key) { | |
1739 | case 'b': /* Bootable flag */ | |
1740 | { | |
1741 | int fl = fdisk_is_disklabel(cf->cxt, DOS) ? DOS_FLAG_ACTIVE : | |
1742 | fdisk_is_disklabel(cf->cxt, SGI) ? SGI_FLAG_BOOT : 0; | |
1743 | ||
1744 | if (fl && fdisk_partition_toggle_flag(cf->cxt, n, fl)) | |
1745 | warn = _("Could not toggle the flag."); | |
1746 | else if (fl) | |
1747 | ref = 1; | |
1748 | break; | |
1749 | } | |
f1512be8 | 1750 | #ifdef KEY_DC |
f019ce1f | 1751 | case KEY_DC: |
f1512be8 | 1752 | #endif |
ed8a13e6 KZ |
1753 | case 'd': /* Delete */ |
1754 | if (fdisk_delete_partition(cf->cxt, n) != 0) | |
1755 | warn = _("Could not delete partition %zu."); | |
1756 | else | |
1757 | info = _("Partition %zu has been deleted."); | |
1758 | ref = 1; | |
1759 | break; | |
314a0dd8 KZ |
1760 | case 'h': /* help */ |
1761 | ui_help(); | |
1762 | ref = 1; | |
1763 | break; | |
ed8a13e6 KZ |
1764 | case 'n': /* New */ |
1765 | { | |
1766 | uint64_t start, size, dflt_size; | |
1767 | struct fdisk_partition *npa; /* the new partition */ | |
1768 | ||
1769 | if (!pa || !fdisk_partition_is_freespace(pa)) | |
1770 | return -EINVAL; | |
1771 | npa = fdisk_new_partition(); | |
1772 | if (!npa) | |
1773 | return -ENOMEM; | |
1774 | /* free space range */ | |
1775 | start = fdisk_partition_get_start(pa); | |
1776 | size = dflt_size = fdisk_partition_get_size(pa) * cf->cxt->sector_size; | |
1777 | ||
1778 | if (ui_get_size(cf, _("Partition size: "), &size, 1, size) | |
1779 | == -CFDISK_ERR_ESC) | |
1780 | break; | |
1781 | ||
1782 | if (dflt_size == size) /* default is to fillin all free space */ | |
1783 | fdisk_partition_end_follow_default(npa, 1); | |
1784 | else /* set relative size of the partition */ | |
1785 | fdisk_partition_set_size(npa, size / cf->cxt->sector_size); | |
1786 | ||
1787 | fdisk_partition_set_start(npa, start); | |
1788 | fdisk_partition_partno_follow_default(npa, 1); | |
1789 | /* add to disk label -- libfdisk will ask for missing details */ | |
1790 | rc = fdisk_add_partition(cf->cxt, npa); | |
1791 | fdisk_unref_partition(npa); | |
1792 | if (rc == 0) | |
1793 | ref = 1; | |
1794 | break; | |
1795 | } | |
1796 | case 'q': /* Quit */ | |
1797 | return 1; | |
1798 | case 't': /* Type */ | |
6fed9601 | 1799 | { |
6fed9601 KZ |
1800 | struct fdisk_parttype *t; |
1801 | ||
1802 | if (!pa || fdisk_partition_is_freespace(pa)) | |
1803 | return -EINVAL; | |
1804 | t = (struct fdisk_parttype *) fdisk_partition_get_type(pa); | |
6d6d9c1a | 1805 | t = ui_get_parttype(cf, t); |
6fed9601 KZ |
1806 | ref = 1; |
1807 | ||
1808 | if (t && fdisk_set_partition_type(cf->cxt, n, t) == 0) | |
09af3db4 | 1809 | info = _("Changed type of partition %zu."); |
6fed9601 | 1810 | else |
09af3db4 | 1811 | info = _("The type of partition %zu is unchanged."); |
ed8a13e6 | 1812 | break; |
6fed9601 | 1813 | } |
d5314bf5 KZ |
1814 | case 's': /* fix order */ |
1815 | if (cf->wrong_order) { | |
1816 | fdisk_reorder_partitions(cf->cxt); | |
1817 | ref = 1; | |
1818 | } | |
1819 | break; | |
ed8a13e6 | 1820 | case 'W': /* Write */ |
7b1ffbc4 KZ |
1821 | { |
1822 | char buf[64] = { 0 }; | |
e146ae4e KZ |
1823 | int rc; |
1824 | ||
1825 | if (fdisk_context_is_readonly(cf->cxt)) { | |
33f7c502 | 1826 | warn = _("Device open in read-only mode"); |
e146ae4e KZ |
1827 | break; |
1828 | } | |
1829 | ||
1830 | rc = ui_get_string(cf, | |
7b1ffbc4 KZ |
1831 | _("Are you sure you want to write the partition " |
1832 | "table to disk? "), | |
09af3db4 | 1833 | _("Type \"yes\" or \"no\", or press ESC to leave this dialog."), |
7b1ffbc4 KZ |
1834 | buf, sizeof(buf)); |
1835 | ||
1836 | ref = 1; | |
1837 | if (rc <= 0 || strcasecmp(buf, "yes") != 0 | |
1838 | || strcasecmp(buf, _("yes")) != 0) { | |
1839 | info = _("Did not write partition table to disk"); | |
1840 | break; | |
1841 | } | |
ed8a13e6 KZ |
1842 | rc = fdisk_write_disklabel(cf->cxt); |
1843 | if (rc) | |
7b1ffbc4 KZ |
1844 | warn = _("Failed to write disklabel"); |
1845 | else { | |
1846 | fdisk_reread_partition_table(cf->cxt); | |
1847 | info = _("The partition table has been altered."); | |
1848 | } | |
bc787727 | 1849 | cf->nwrites++; |
7b1ffbc4 KZ |
1850 | break; |
1851 | } | |
1852 | default: | |
ed8a13e6 KZ |
1853 | break; |
1854 | } | |
1855 | ||
1856 | if (ref) { | |
1857 | lines_refresh(cf); | |
1858 | ui_refresh(cf); | |
1859 | } | |
7b1ffbc4 KZ |
1860 | |
1861 | ui_clean_hint(); | |
ed8a13e6 | 1862 | if (warn) |
7b1ffbc4 | 1863 | ui_warnx(warn, n + 1); |
ed8a13e6 | 1864 | else if (info) |
7b1ffbc4 | 1865 | ui_info(info, n + 1); |
d5314bf5 KZ |
1866 | else if (key == 'n' && cf->wrong_order && org_order == 0) |
1867 | ui_info(_("Note that partition table entries are not in disk order now.")); | |
ed8a13e6 KZ |
1868 | |
1869 | return 0; | |
1870 | } | |
1871 | ||
7556c944 KZ |
1872 | static void ui_resize_refresh(struct cfdisk *cf) |
1873 | { | |
1874 | resize(); | |
1875 | menu_refresh_size(cf); | |
1876 | lines_refresh(cf); | |
1877 | ui_refresh(cf); | |
1878 | } | |
1879 | ||
8c3a5a44 KZ |
1880 | static int ui_run(struct cfdisk *cf) |
1881 | { | |
7aa0d529 | 1882 | int rc = 0; |
6dbe3af9 | 1883 | |
88141067 | 1884 | DBG(FRONTEND, ul_debug("ui: start COLS=%d, LINES=%d", COLS, LINES)); |
6dbe3af9 | 1885 | |
d121efdb | 1886 | if (!fdisk_dev_has_disklabel(cf->cxt) || cf->zero_start) { |
7aa0d529 KZ |
1887 | rc = ui_create_label(cf); |
1888 | if (rc < 0) | |
1889 | ui_errx(EXIT_FAILURE, | |
1890 | _("failed to create a new disklabel")); | |
1891 | if (rc) | |
1892 | return rc; | |
1893 | } | |
1894 | ||
1895 | cols_init(cf); | |
1896 | rc = lines_refresh(cf); | |
1897 | if (rc) | |
1898 | ui_errx(EXIT_FAILURE, _("failed to read partitions")); | |
6dbe3af9 | 1899 | |
3b411726 | 1900 | menu_push(cf, main_menuitems); |
63128bbf KZ |
1901 | cf->menu->ignore_cb = main_menu_ignore_keys; |
1902 | ||
8c3a5a44 KZ |
1903 | rc = ui_refresh(cf); |
1904 | if (rc) | |
1905 | return rc; | |
6dbe3af9 | 1906 | |
e146ae4e KZ |
1907 | if (fdisk_context_is_readonly(cf->cxt)) |
1908 | ui_warnx(_("Device open in read-only mode.")); | |
1909 | ||
8c3a5a44 | 1910 | do { |
8460875d | 1911 | int rc = 0, key = getch(); |
6dbe3af9 | 1912 | |
7556c944 KZ |
1913 | if (ui_resize) |
1914 | /* Note that ncurses getch() returns ERR when interrupted | |
1915 | * by signal, but SLang does not interrupt at all. */ | |
1916 | ui_resize_refresh(cf); | |
1917 | if (key == ERR) | |
1918 | continue; | |
ac27ea5c KZ |
1919 | if (ui_menu_move(cf, key) == 0) |
1920 | continue; | |
1921 | ||
88141067 | 1922 | DBG(FRONTEND, ul_debug("ui: main action key >%c<.", key)); |
f019ce1f | 1923 | |
8c3a5a44 KZ |
1924 | switch (key) { |
1925 | case KEY_DOWN: | |
1926 | case '\016': /* ^N */ | |
1927 | case 'j': /* Vi-like alternative */ | |
1928 | ui_table_goto(cf, cf->lines_idx + 1); | |
1929 | break; | |
1930 | case KEY_UP: | |
1931 | case '\020': /* ^P */ | |
1932 | case 'k': /* Vi-like alternative */ | |
f019ce1f | 1933 | ui_table_goto(cf, (int) cf->lines_idx - 1); |
8c3a5a44 | 1934 | break; |
f019ce1f KZ |
1935 | case KEY_PPAGE: |
1936 | if (cf->page_sz) { | |
1937 | ui_table_goto(cf, (int) cf->lines_idx - cf->page_sz); | |
1938 | break; | |
1939 | } | |
8c3a5a44 KZ |
1940 | case KEY_HOME: |
1941 | ui_table_goto(cf, 0); | |
1942 | break; | |
f019ce1f KZ |
1943 | case KEY_NPAGE: |
1944 | if (cf->page_sz) { | |
1945 | ui_table_goto(cf, cf->lines_idx + cf->page_sz); | |
1946 | break; | |
1947 | } | |
8c3a5a44 | 1948 | case KEY_END: |
f019ce1f | 1949 | ui_table_goto(cf, (int) cf->nlines - 1); |
8c3a5a44 | 1950 | break; |
8c3a5a44 KZ |
1951 | case KEY_ENTER: |
1952 | case '\n': | |
1953 | case '\r': | |
ed8a13e6 | 1954 | rc = main_menu_action(cf, 0); |
8c3a5a44 KZ |
1955 | break; |
1956 | default: | |
ed8a13e6 | 1957 | rc = main_menu_action(cf, key); |
8460875d | 1958 | if (rc < 0) |
8c3a5a44 KZ |
1959 | beep(); |
1960 | break; | |
1961 | } | |
8460875d KZ |
1962 | |
1963 | if (rc == 1) | |
1964 | break; /* quit */ | |
8c3a5a44 | 1965 | } while (1); |
6dbe3af9 | 1966 | |
8c3a5a44 | 1967 | menu_pop(cf); |
6dbe3af9 | 1968 | |
4f4daf8f | 1969 | curs_set(1); |
88141067 | 1970 | DBG(FRONTEND, ul_debug("ui: end")); |
8c3a5a44 KZ |
1971 | |
1972 | return 0; | |
1973 | } | |
6dbe3af9 | 1974 | |
04915c3f KZ |
1975 | static void __attribute__ ((__noreturn__)) usage(FILE *out) |
1976 | { | |
1977 | fputs(USAGE_HEADER, out); | |
1978 | ||
1979 | fprintf(out, | |
1980 | _(" %1$s [options] <disk>\n"), program_invocation_short_name); | |
1981 | ||
1982 | fputs(USAGE_OPTIONS, out); | |
1983 | fputs(_(" -L --color[=<when>] colorize output (auto, always or never)\n"), out); | |
d121efdb | 1984 | fputs(_(" -z --zero start with zeroed partition table\n"), out); |
04915c3f KZ |
1985 | |
1986 | fputs(USAGE_SEPARATOR, out); | |
1987 | fputs(USAGE_HELP, out); | |
1988 | fputs(USAGE_VERSION, out); | |
1989 | ||
1990 | fprintf(out, USAGE_MAN_TAIL("cfdisk(8)")); | |
1991 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
1992 | } | |
1993 | ||
8c3a5a44 KZ |
1994 | int main(int argc, char *argv[]) |
1995 | { | |
a4f0a42d | 1996 | const char *diskpath; |
ad9fe47e | 1997 | int rc, c, colormode = UL_COLORMODE_UNDEF; |
8c3a5a44 KZ |
1998 | struct cfdisk _cf = { .lines_idx = 0 }, |
1999 | *cf = &_cf; | |
6dbe3af9 | 2000 | |
04915c3f KZ |
2001 | static const struct option longopts[] = { |
2002 | { "color", optional_argument, NULL, 'L' }, | |
2003 | { "help", no_argument, NULL, 'h' }, | |
2004 | { "version", no_argument, NULL, 'V' }, | |
d121efdb | 2005 | { "zero", no_argument, NULL, 'z' }, |
04915c3f KZ |
2006 | { NULL, 0, 0, 0 }, |
2007 | }; | |
2008 | ||
8c3a5a44 KZ |
2009 | setlocale(LC_ALL, ""); |
2010 | bindtextdomain(PACKAGE, LOCALEDIR); | |
2011 | textdomain(PACKAGE); | |
2012 | atexit(close_stdout); | |
6dbe3af9 | 2013 | |
d121efdb | 2014 | while((c = getopt_long(argc, argv, "L::hVz", longopts, NULL)) != -1) { |
04915c3f KZ |
2015 | switch(c) { |
2016 | case 'h': | |
2017 | usage(stdout); | |
2018 | break; | |
2019 | case 'L': | |
2020 | colormode = UL_COLORMODE_AUTO; | |
2021 | if (optarg) | |
2022 | colormode = colormode_or_err(optarg, | |
2023 | _("unsupported color mode")); | |
2024 | break; | |
2025 | case 'V': | |
2026 | printf(_("%s from %s\n"), program_invocation_short_name, | |
2027 | PACKAGE_STRING); | |
2028 | return EXIT_SUCCESS; | |
d121efdb KZ |
2029 | case 'z': |
2030 | cf->zero_start = 1; | |
2031 | break; | |
04915c3f KZ |
2032 | } |
2033 | } | |
2034 | ||
210bb492 | 2035 | colors_init(colormode, "cfdisk"); |
04915c3f | 2036 | |
8c3a5a44 | 2037 | fdisk_init_debug(0); |
710ed55d KZ |
2038 | scols_init_debug(0); |
2039 | ||
8c3a5a44 KZ |
2040 | cf->cxt = fdisk_new_context(); |
2041 | if (!cf->cxt) | |
2042 | err(EXIT_FAILURE, _("failed to allocate libfdisk context")); | |
6dbe3af9 | 2043 | |
8c3a5a44 | 2044 | fdisk_context_set_ask(cf->cxt, ask_callback, (void *) cf); |
6dbe3af9 | 2045 | |
04915c3f | 2046 | if (optind == argc) |
a4f0a42d KZ |
2047 | diskpath = access(DEFAULT_DEVICE, F_OK) == 0 ? |
2048 | DEFAULT_DEVICE : ALTERNATE_DEVICE; | |
2049 | else | |
2050 | diskpath = argv[optind]; | |
6dbe3af9 | 2051 | |
a4f0a42d | 2052 | rc = fdisk_context_assign_device(cf->cxt, diskpath, 0); |
e146ae4e | 2053 | if (rc == -EACCES) |
a4f0a42d | 2054 | rc = fdisk_context_assign_device(cf->cxt, diskpath, 1); |
e146ae4e | 2055 | if (rc != 0) |
a4f0a42d KZ |
2056 | err(EXIT_FAILURE, _("cannot open %s"), |
2057 | optind == argc ? DEFAULT_DEVICE : diskpath); | |
6dbe3af9 | 2058 | |
8c3a5a44 KZ |
2059 | /* Don't use err(), warn() from this point */ |
2060 | ui_init(cf); | |
2061 | ui_run(cf); | |
7aa0d529 | 2062 | ui_end(); |
6dbe3af9 | 2063 | |
8c3a5a44 KZ |
2064 | free(cf->lines); |
2065 | free(cf->linesbuf); | |
2066 | fdisk_unref_table(cf->table); | |
ed8a13e6 | 2067 | |
bc787727 | 2068 | rc = fdisk_context_deassign_device(cf->cxt, cf->nwrites == 0); |
8c3a5a44 | 2069 | fdisk_free_context(cf->cxt); |
88141067 | 2070 | DBG(FRONTEND, ul_debug("bye! [rc=%d]", rc)); |
ed8a13e6 | 2071 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
6dbe3af9 | 2072 | } |