2014-07-14 15:53:36 -05:00
|
|
|
/*
|
|
|
|
* cfdisk.c - Display or manipulate a disk partition table.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Karel Zak <kzak@redhat.com>
|
|
|
|
* Copyright (C) 1994 Kevin E. Martin (martin@cs.unc.edu)
|
|
|
|
*
|
|
|
|
* The original cfdisk was inspired by the fdisk program
|
|
|
|
* by A. V. Le Blanc (leblanc@mcc.ac.uk.
|
|
|
|
*
|
|
|
|
* cfdisk is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
2006-12-06 17:25:32 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2014-01-09 07:49:45 -06:00
|
|
|
#include <signal.h>
|
2014-01-22 04:53:34 -06:00
|
|
|
#include <ctype.h>
|
2014-02-27 06:28:40 -06:00
|
|
|
#include <getopt.h>
|
2014-03-27 08:58:51 -05:00
|
|
|
#include <libsmartcols.h>
|
2014-06-05 07:32:51 -05:00
|
|
|
#include <sys/ioctl.h>
|
2010-07-01 03:56:41 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_SLANG_H
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <slang.h>
|
2010-07-01 03:56:41 -05:00
|
|
|
#elif defined(HAVE_SLANG_SLANG_H)
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <slang/slang.h>
|
2010-07-01 03:56:41 -05:00
|
|
|
#endif
|
|
|
|
|
2006-12-06 17:26:54 -06:00
|
|
|
#ifdef HAVE_SLCURSES_H
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <slcurses.h>
|
2006-12-06 17:26:54 -06:00
|
|
|
#elif defined(HAVE_SLANG_SLCURSES_H)
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <slang/slcurses.h>
|
2010-07-01 12:41:40 -05:00
|
|
|
#elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <ncursesw/ncurses.h>
|
2006-12-06 17:26:54 -06:00
|
|
|
#elif defined(HAVE_NCURSES_H)
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <ncurses.h>
|
2006-12-06 17:26:54 -06:00
|
|
|
#elif defined(HAVE_NCURSES_NCURSES_H)
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <ncurses/ncurses.h>
|
2006-12-06 17:25:35 -06:00
|
|
|
#endif
|
2010-07-01 03:56:41 -05:00
|
|
|
|
2010-05-07 07:12:26 -05:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2014-03-07 06:01:22 -06:00
|
|
|
# include <wctype.h>
|
2014-03-10 08:44:50 -05:00
|
|
|
# include <wchar.h>
|
2010-05-07 07:12:26 -05:00
|
|
|
#endif
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
#include "c.h"
|
2012-04-04 13:02:04 -05:00
|
|
|
#include "closestream.h"
|
2006-12-06 17:25:39 -06:00
|
|
|
#include "nls.h"
|
2010-11-16 07:47:35 -06:00
|
|
|
#include "strutils.h"
|
2014-01-09 07:49:45 -06:00
|
|
|
#include "xalloc.h"
|
2010-05-07 07:12:26 -05:00
|
|
|
#include "mbsalign.h"
|
2014-02-27 06:28:40 -06:00
|
|
|
#include "colors.h"
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
#include "fdiskP.h"
|
|
|
|
|
2014-04-03 04:58:48 -05:00
|
|
|
#define ARROW_CURSOR_STRING ">> "
|
2014-01-09 07:49:45 -06:00
|
|
|
#define ARROW_CURSOR_DUMMY " "
|
|
|
|
#define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
|
|
|
|
|
|
|
|
#define MENU_PADDING 2
|
|
|
|
#define TABLE_START_LINE 4
|
2014-02-27 05:24:50 -06:00
|
|
|
#define MENU_START_LINE ((size_t) LINES - 5)
|
|
|
|
#define INFO_LINE ((size_t) LINES - 2)
|
|
|
|
#define HINT_LINE ((size_t) LINES - 1)
|
2014-01-29 07:22:12 -06:00
|
|
|
|
|
|
|
#define CFDISK_ERR_ESC 5000
|
|
|
|
|
|
|
|
#ifndef KEY_ESC
|
|
|
|
# define KEY_ESC '\033'
|
|
|
|
#endif
|
|
|
|
#ifndef KEY_DELETE
|
|
|
|
# define KEY_DELETE '\177'
|
|
|
|
#endif
|
2014-01-22 06:05:19 -06:00
|
|
|
|
|
|
|
/* colors */
|
|
|
|
enum {
|
|
|
|
CFDISK_CL_NONE = 0,
|
2014-02-03 07:40:35 -06:00
|
|
|
CFDISK_CL_WARNING,
|
|
|
|
CFDISK_CL_FREESPACE,
|
2014-01-22 06:05:19 -06:00
|
|
|
};
|
|
|
|
static const int color_pairs[][2] = {
|
|
|
|
/* color foreground, background */
|
2014-02-03 07:40:35 -06:00
|
|
|
[CFDISK_CL_WARNING] = { COLOR_RED, -1 },
|
|
|
|
[CFDISK_CL_FREESPACE] = { COLOR_GREEN, -1 }
|
2014-01-22 06:05:19 -06:00
|
|
|
};
|
2014-01-09 07:49:45 -06:00
|
|
|
|
2014-01-22 04:10:54 -06:00
|
|
|
struct cfdisk;
|
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx);
|
|
|
|
static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf, int key, size_t *idx);
|
|
|
|
static struct cfdisk_menu *menu_push(struct cfdisk *cf, struct cfdisk_menuitem *item);
|
2014-01-29 07:22:12 -06:00
|
|
|
static struct cfdisk_menu *menu_pop(struct cfdisk *cf);
|
2014-06-05 07:32:51 -05:00
|
|
|
static void menu_refresh_size(struct cfdisk *cf);
|
2014-01-22 07:22:35 -06:00
|
|
|
|
2014-01-22 06:05:19 -06:00
|
|
|
static int ui_refresh(struct cfdisk *cf);
|
2014-01-22 07:22:35 -06:00
|
|
|
static void ui_warnx(const char *fmt, ...);
|
|
|
|
static void ui_warn(const char *fmt, ...);
|
|
|
|
static void ui_info(const char *fmt, ...);
|
2014-01-29 07:22:12 -06:00
|
|
|
static void ui_draw_menu(struct cfdisk *cf);
|
2014-02-24 07:27:49 -06:00
|
|
|
static int ui_menu_move(struct cfdisk *cf, int key);
|
2014-06-05 07:32:51 -05:00
|
|
|
static void ui_menu_resize(struct cfdisk *cf);
|
2014-02-24 07:27:49 -06:00
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
|
|
|
|
uintmax_t low, uintmax_t up);
|
2014-01-22 04:10:54 -06:00
|
|
|
|
2014-01-22 07:22:35 -06:00
|
|
|
static int ui_enabled;
|
2014-06-05 07:32:51 -05:00
|
|
|
static int ui_resize;
|
2014-01-09 07:49:45 -06:00
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/* menu item */
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem {
|
2014-01-09 07:49:45 -06:00
|
|
|
int key; /* keyboard shortcut */
|
|
|
|
const char *name; /* item name */
|
2014-03-07 06:14:51 -06:00
|
|
|
const char *desc; /* item description (hint) */
|
2014-02-26 06:02:30 -06:00
|
|
|
void *userdata;
|
2012-09-24 04:30:26 -05:00
|
|
|
};
|
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/* menu */
|
2014-01-09 07:49:45 -06:00
|
|
|
struct cfdisk_menu {
|
2014-03-07 06:14:51 -06:00
|
|
|
char *title; /* optional menu title */
|
|
|
|
struct cfdisk_menuitem *items; /* array with menu items */
|
|
|
|
char *ignore;/* string with keys to ignore */
|
|
|
|
size_t width; /* maximal width of the menu item */
|
|
|
|
size_t nitems; /* number of the active menu items */
|
|
|
|
size_t page_sz;/* when menu longer than screen */
|
|
|
|
size_t idx; /* the current menu item */
|
2014-01-09 07:49:45 -06:00
|
|
|
struct cfdisk_menu *prev;
|
2014-03-07 06:14:51 -06:00
|
|
|
|
|
|
|
/* @ignore keys generator */
|
2014-02-25 06:22:26 -06:00
|
|
|
int (*ignore_cb) (struct cfdisk *, char *, size_t);
|
2014-02-24 06:57:08 -06:00
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
unsigned int vertical : 1; /* enable vertical mode */
|
2012-09-24 04:30:26 -05:00
|
|
|
};
|
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/* main menu */
|
2014-03-07 06:01:22 -06:00
|
|
|
static struct cfdisk_menuitem main_menuitems[] = {
|
2014-01-09 07:49:45 -06:00
|
|
|
{ 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
|
|
|
|
{ 'd', N_("Delete"), N_("Delete the current partition") },
|
|
|
|
{ 'n', N_("New"), N_("Create new partition from free space") },
|
|
|
|
{ 'q', N_("Quit"), N_("Quit program without writing partition table") },
|
|
|
|
{ 't', N_("Type"), N_("Change the partition type") },
|
2014-02-26 07:22:04 -06:00
|
|
|
{ 'h', N_("Help"), N_("Print help screen") },
|
2014-06-05 05:13:41 -05:00
|
|
|
{ 's', N_("Sort"), N_("Fix partitions order") },
|
2014-01-09 07:49:45 -06:00
|
|
|
{ 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
|
|
|
|
{ 0, NULL, NULL }
|
|
|
|
};
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/* top level control struct */
|
2014-01-09 07:49:45 -06:00
|
|
|
struct cfdisk {
|
|
|
|
struct fdisk_context *cxt; /* libfdisk context */
|
|
|
|
struct fdisk_table *table; /* partition table */
|
2014-02-27 05:24:50 -06:00
|
|
|
struct cfdisk_menu *menu; /* the current menu */
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
int *cols; /* output columns */
|
|
|
|
size_t ncols; /* number of columns */
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
char *linesbuf; /* table as string */
|
|
|
|
size_t linesbufsz; /* size of the tb_buf */
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
char **lines; /* array with lines */
|
|
|
|
size_t nlines; /* number of lines */
|
2014-02-27 05:24:50 -06:00
|
|
|
size_t lines_idx; /* current line <0..N>, exclude header */
|
|
|
|
size_t page_sz;
|
2014-06-05 05:13:41 -05:00
|
|
|
|
2014-07-15 05:28:16 -05:00
|
|
|
unsigned int nwrites; /* fdisk_write_disklabel() counter */
|
|
|
|
|
2014-06-20 05:17:53 -05:00
|
|
|
unsigned int wrong_order :1, /* PT not in right order */
|
|
|
|
zero_start :1; /* ignore existing partition table */
|
2014-01-09 07:49:45 -06:00
|
|
|
};
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/* Initialize output columns -- we follow libcfdisk columns (usually specific
|
|
|
|
* to the label type.
|
|
|
|
*/
|
2014-01-09 07:49:45 -06:00
|
|
|
static int cols_init(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
assert(cf);
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
free(cf->cols);
|
|
|
|
cf->cols = NULL;
|
|
|
|
cf->ncols = 0;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
return fdisk_get_columns(cf->cxt, 0, &cf->cols, &cf->ncols);
|
2006-12-06 17:25:37 -06:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
static void resize(void)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
|
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1
|
|
|
|
&& ws.ws_row && ws.ws_col) {
|
|
|
|
LINES = ws.ws_row;
|
|
|
|
COLS = ws.ws_col;
|
|
|
|
#if HAVE_RESIZETERM
|
|
|
|
resizeterm(ws.ws_row, ws.ws_col);
|
|
|
|
#endif
|
|
|
|
clearok(stdscr, TRUE);
|
|
|
|
}
|
|
|
|
touchwin(stdscr);
|
|
|
|
|
|
|
|
DBG(FRONTEND, ul_debug("ui: resize refresh COLS=%d, LINES=%d", COLS, LINES));
|
|
|
|
ui_resize = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-03 04:58:48 -05:00
|
|
|
/* Reads partition in tree-like order from scols
|
|
|
|
*/
|
|
|
|
static int partition_from_scols(struct fdisk_table *tb,
|
|
|
|
struct libscols_line *ln)
|
|
|
|
{
|
|
|
|
struct fdisk_partition *pa = scols_line_get_userdata(ln);
|
|
|
|
|
|
|
|
fdisk_table_add_partition(tb, pa);
|
|
|
|
fdisk_unref_partition(pa);
|
|
|
|
|
|
|
|
if (scols_line_has_children(ln)) {
|
|
|
|
struct libscols_line *chln;
|
|
|
|
struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
|
|
|
|
|
|
|
|
if (!itr)
|
|
|
|
return -EINVAL;
|
|
|
|
while (scols_line_next_child(ln, itr, &chln) == 0)
|
|
|
|
partition_from_scols(tb, chln);
|
2014-07-17 06:46:27 -05:00
|
|
|
scols_free_iter(itr);
|
2014-04-03 04:58:48 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* It would be possible to use fdisk_table_to_string(), but we want some
|
|
|
|
* extension to the output format, so let's do it without libfdisk
|
|
|
|
*/
|
|
|
|
static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb)
|
|
|
|
{
|
|
|
|
const struct fdisk_column *col;
|
2014-02-04 13:51:30 -06:00
|
|
|
struct fdisk_partition *pa;
|
2014-01-09 07:49:45 -06:00
|
|
|
struct fdisk_label *lb;
|
|
|
|
struct fdisk_iter *itr = NULL;
|
2014-03-27 08:58:51 -05:00
|
|
|
struct libscols_table *table = NULL;
|
|
|
|
struct libscols_iter *s_itr = NULL;
|
2014-01-09 07:49:45 -06:00
|
|
|
char *res = NULL;
|
2014-02-04 13:51:30 -06:00
|
|
|
size_t i;
|
2014-02-03 07:40:35 -06:00
|
|
|
int tree = 0;
|
2014-03-27 08:58:51 -05:00
|
|
|
struct libscols_line *ln, *ln_cont = NULL;
|
2014-01-09 07:49:45 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("table: convert to string"));
|
2014-01-09 07:49:45 -06:00
|
|
|
|
|
|
|
assert(cf);
|
|
|
|
assert(cf->cxt);
|
|
|
|
assert(cf->cols);
|
|
|
|
assert(tb);
|
|
|
|
|
|
|
|
lb = fdisk_context_get_label(cf->cxt, NULL);
|
|
|
|
assert(lb);
|
|
|
|
|
|
|
|
itr = fdisk_new_iter(FDISK_ITER_FORWARD);
|
|
|
|
if (!itr)
|
|
|
|
goto done;
|
2006-12-06 17:25:37 -06:00
|
|
|
|
2014-02-03 07:40:35 -06:00
|
|
|
/* get container (e.g. extended partition) */
|
|
|
|
while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
|
|
|
|
if (fdisk_partition_is_nested(pa)) {
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("table: nested detected, using tree"));
|
2014-03-27 08:58:51 -05:00
|
|
|
tree = SCOLS_FL_TREE;
|
2014-02-03 07:40:35 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 09:09:57 -05:00
|
|
|
table = scols_new_table();
|
2014-03-27 08:58:51 -05:00
|
|
|
if (!table)
|
2014-02-03 07:40:35 -06:00
|
|
|
goto done;
|
2014-04-03 09:09:57 -05:00
|
|
|
scols_table_enable_maxout(table, 1);
|
2014-02-03 07:40:35 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* headers */
|
|
|
|
for (i = 0; i < cf->ncols; i++) {
|
|
|
|
col = fdisk_label_get_column(lb, cf->cols[i]);
|
2014-02-03 07:40:35 -06:00
|
|
|
if (col) {
|
2014-03-27 08:58:51 -05:00
|
|
|
int fl = col->scols_flags;
|
2014-02-03 07:40:35 -06:00
|
|
|
if (tree && col->id == FDISK_COL_DEVICE)
|
2014-03-27 08:58:51 -05:00
|
|
|
fl |= SCOLS_FL_TREE;
|
|
|
|
if (!scols_table_new_column(table, col->name, col->width, fl))
|
|
|
|
goto done;
|
2014-02-03 07:40:35 -06:00
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* data */
|
2014-02-04 13:51:30 -06:00
|
|
|
fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
|
2014-03-27 08:58:51 -05:00
|
|
|
struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL;
|
2014-02-04 13:51:30 -06:00
|
|
|
|
2014-03-27 08:58:51 -05:00
|
|
|
ln = scols_table_new_line(table, parent);
|
2014-01-09 07:49:45 -06:00
|
|
|
if (!ln)
|
|
|
|
goto done;
|
|
|
|
for (i = 0; i < cf->ncols; i++) {
|
|
|
|
char *cdata = NULL;
|
|
|
|
col = fdisk_label_get_column(lb, cf->cols[i]);
|
|
|
|
if (!col)
|
|
|
|
continue;
|
|
|
|
if (fdisk_partition_to_string(pa, cf->cxt, col->id, &cdata))
|
|
|
|
continue;
|
2014-04-03 04:58:48 -05:00
|
|
|
scols_line_refer_data(ln, i, cdata);
|
2014-01-09 07:49:45 -06:00
|
|
|
}
|
2014-02-04 13:51:30 -06:00
|
|
|
if (tree && fdisk_partition_is_container(pa))
|
2014-02-03 07:40:35 -06:00
|
|
|
ln_cont = ln;
|
2014-02-04 16:53:03 -06:00
|
|
|
|
2014-03-27 08:58:51 -05:00
|
|
|
scols_line_set_userdata(ln, (void *) pa);
|
2014-02-04 16:53:03 -06:00
|
|
|
fdisk_ref_partition(pa);
|
2014-01-09 07:49:45 -06:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-27 08:58:51 -05:00
|
|
|
if (scols_table_is_empty(table))
|
2014-02-04 16:53:03 -06:00
|
|
|
goto done;
|
|
|
|
|
2014-03-27 08:58:51 -05:00
|
|
|
scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH);
|
|
|
|
scols_print_table_to_string(table, &res);
|
2014-02-04 16:53:03 -06:00
|
|
|
|
2014-03-27 08:58:51 -05:00
|
|
|
/* scols_* code might reorder lines, let's reorder @tb according to the
|
2014-02-04 16:53:03 -06:00
|
|
|
* final output (it's no problem because partitions are addressed by
|
|
|
|
* parno stored within struct fdisk_partition) */
|
|
|
|
|
|
|
|
/* remove all */
|
|
|
|
fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
|
|
|
|
while (fdisk_table_next_partition(tb, itr, &pa) == 0)
|
|
|
|
fdisk_table_remove_partition(tb, pa);
|
|
|
|
|
2014-03-27 08:58:51 -05:00
|
|
|
s_itr = scols_new_iter(SCOLS_ITER_FORWARD);
|
|
|
|
if (!s_itr)
|
|
|
|
goto done;
|
|
|
|
|
2014-04-03 04:58:48 -05:00
|
|
|
/* add all in the right order (don't forget the output is tree) */
|
2014-03-27 08:58:51 -05:00
|
|
|
while (scols_table_next_line(table, s_itr, &ln) == 0) {
|
2014-04-03 04:58:48 -05:00
|
|
|
if (scols_line_get_parent(ln))
|
|
|
|
continue;
|
|
|
|
if (partition_from_scols(tb, ln))
|
|
|
|
break;
|
2014-01-09 07:49:45 -06:00
|
|
|
}
|
|
|
|
done:
|
2014-03-27 08:58:51 -05:00
|
|
|
scols_unref_table(table);
|
|
|
|
scols_free_iter(s_itr);
|
2014-01-09 07:49:45 -06:00
|
|
|
fdisk_free_iter(itr);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
return res;
|
2006-12-06 17:25:35 -06:00
|
|
|
}
|
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/*
|
|
|
|
* Read data about partitions from libfdisk and prepare output lines.
|
|
|
|
*/
|
2014-01-22 04:10:54 -06:00
|
|
|
static int lines_refresh(struct cfdisk *cf)
|
2014-01-09 07:49:45 -06:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
char *p;
|
|
|
|
size_t i;
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
assert(cf);
|
2006-12-06 17:25:35 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("refreshing buffer"));
|
2008-09-16 06:08:12 -05:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
free(cf->linesbuf);
|
|
|
|
free(cf->lines);
|
|
|
|
cf->linesbuf = NULL;
|
|
|
|
cf->linesbufsz = 0;
|
|
|
|
cf->lines = NULL;
|
|
|
|
cf->nlines = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
fdisk_unref_table(cf->table);
|
2014-01-22 06:05:19 -06:00
|
|
|
cf->table = NULL;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-30 05:21:11 -06:00
|
|
|
/* read partitions and free spaces into cf->table */
|
|
|
|
rc = fdisk_get_partitions(cf->cxt, &cf->table);
|
|
|
|
if (!rc)
|
|
|
|
rc = fdisk_get_freespaces(cf->cxt, &cf->table);
|
2014-01-09 07:49:45 -06:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
2010-12-18 12:24:03 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
cf->linesbuf = table_to_string(cf, cf->table);
|
|
|
|
if (!cf->linesbuf)
|
|
|
|
return -ENOMEM;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
cf->linesbufsz = strlen(cf->linesbuf);
|
|
|
|
cf->nlines = fdisk_table_get_nents(cf->table) + 1; /* 1 for header line */
|
2014-02-27 05:24:50 -06:00
|
|
|
cf->page_sz = 0;
|
2014-06-05 05:13:41 -05:00
|
|
|
cf->wrong_order = fdisk_table_wrong_order(cf->table) ? 1 : 0;
|
2014-02-27 05:24:50 -06:00
|
|
|
|
|
|
|
if (MENU_START_LINE - TABLE_START_LINE < cf->nlines)
|
|
|
|
cf->page_sz = MENU_START_LINE - TABLE_START_LINE - 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-26 06:58:31 -06:00
|
|
|
cf->lines = xcalloc(cf->nlines, sizeof(char *));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
for (p = cf->linesbuf, i = 0; p && i < cf->nlines; i++) {
|
|
|
|
cf->lines[i] = p;
|
|
|
|
p = strchr(p, '\n');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-22 04:53:34 -06:00
|
|
|
static struct fdisk_partition *get_current_partition(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
assert(cf);
|
|
|
|
assert(cf->table);
|
|
|
|
|
|
|
|
return fdisk_table_get_partition(cf->table, cf->lines_idx);
|
|
|
|
}
|
|
|
|
|
2014-02-03 07:40:35 -06:00
|
|
|
static int is_freespace(struct cfdisk *cf, size_t i)
|
|
|
|
{
|
|
|
|
struct fdisk_partition *pa;
|
|
|
|
|
|
|
|
assert(cf);
|
|
|
|
assert(cf->table);
|
|
|
|
|
|
|
|
pa = fdisk_table_get_partition(cf->table, i);
|
|
|
|
return fdisk_partition_is_freespace(pa);
|
|
|
|
}
|
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
/* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
|
|
|
|
* responseback to libfdisk
|
|
|
|
*/
|
|
|
|
static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
|
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d, *cm;
|
2014-01-29 07:22:12 -06:00
|
|
|
int key;
|
|
|
|
size_t i = 0, nitems;
|
|
|
|
const char *name, *desc;
|
|
|
|
|
|
|
|
assert(ask);
|
|
|
|
assert(cf);
|
|
|
|
|
|
|
|
/* create cfdisk menu according to libfdisk ask-menu, note that the
|
|
|
|
* last cm[] item has to be empty -- so nitems + 1 */
|
|
|
|
nitems = fdisk_ask_menu_get_nitems(ask);
|
2014-03-07 06:01:22 -06:00
|
|
|
cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
|
2014-01-29 07:22:12 -06:00
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
if (fdisk_ask_menu_get_item(ask, i, &key, &name, &desc))
|
|
|
|
break;
|
|
|
|
cm[i].key = key;
|
|
|
|
cm[i].desc = desc;
|
|
|
|
cm[i].name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make the new menu active */
|
2014-02-25 06:22:26 -06:00
|
|
|
menu_push(cf, cm);
|
2014-01-29 07:22:12 -06:00
|
|
|
ui_draw_menu(cf);
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
/* wait for keys */
|
|
|
|
do {
|
2014-02-24 07:27:49 -06:00
|
|
|
int key = getch();
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
if (ui_resize)
|
|
|
|
ui_menu_resize(cf);
|
2014-02-24 07:27:49 -06:00
|
|
|
if (ui_menu_move(cf, key) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (key) {
|
2014-01-29 07:22:12 -06:00
|
|
|
case KEY_ENTER:
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2014-02-26 06:02:30 -06:00
|
|
|
d = menu_get_menuitem(cf, cf->menu->idx);
|
2014-01-29 07:22:12 -06:00
|
|
|
if (d)
|
|
|
|
fdisk_ask_menu_set_result(ask, d->key);
|
|
|
|
menu_pop(cf);
|
|
|
|
free(cm);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
menu_pop(cf);
|
|
|
|
free(cm);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-07 06:14:51 -06:00
|
|
|
/* libfdisk callback
|
|
|
|
*/
|
2014-01-09 07:49:45 -06:00
|
|
|
static int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
|
|
|
|
void *data __attribute__((__unused__)))
|
|
|
|
{
|
|
|
|
int rc = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
assert(cxt);
|
|
|
|
assert(ask);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
switch(fdisk_ask_get_type(ask)) {
|
|
|
|
case FDISK_ASKTYPE_INFO:
|
2014-01-22 07:22:35 -06:00
|
|
|
ui_info(fdisk_ask_print_get_mesg(ask));
|
2014-01-09 07:49:45 -06:00
|
|
|
break;
|
|
|
|
case FDISK_ASKTYPE_WARNX:
|
2014-01-22 07:22:35 -06:00
|
|
|
ui_warnx(fdisk_ask_print_get_mesg(ask));
|
2014-01-09 07:49:45 -06:00
|
|
|
break;
|
|
|
|
case FDISK_ASKTYPE_WARN:
|
2014-01-22 07:22:35 -06:00
|
|
|
ui_warn(fdisk_ask_print_get_mesg(ask));
|
2014-01-09 07:49:45 -06:00
|
|
|
break;
|
2014-01-29 07:22:12 -06:00
|
|
|
case FDISK_ASKTYPE_MENU:
|
|
|
|
ask_menu(ask, (struct cfdisk *) data);
|
|
|
|
break;
|
2014-01-09 07:49:45 -06:00
|
|
|
default:
|
2014-01-22 07:22:35 -06:00
|
|
|
ui_warnx(_("internal error: unsupported dialog type %d"),
|
|
|
|
fdisk_ask_get_type(ask));
|
2014-01-09 07:49:45 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return rc;
|
2006-12-06 17:25:34 -06:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-25 05:47:22 -06:00
|
|
|
static int ui_end(void)
|
2014-01-09 07:49:45 -06:00
|
|
|
{
|
2014-02-25 05:47:22 -06:00
|
|
|
if (!ui_enabled)
|
2014-01-09 07:49:45 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
2006-12-06 17:26:54 -06:00
|
|
|
#if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
|
2014-01-09 07:49:45 -06:00
|
|
|
SLsmg_gotorc(LINES - 1, 0);
|
|
|
|
SLsmg_refresh();
|
2006-12-06 17:25:35 -06:00
|
|
|
#else
|
2014-01-09 07:49:45 -06:00
|
|
|
mvcur(0, COLS - 1, LINES-1, 0);
|
2006-12-06 17:25:35 -06:00
|
|
|
#endif
|
2014-01-09 07:49:45 -06:00
|
|
|
nl();
|
|
|
|
endwin();
|
|
|
|
printf("\n");
|
2014-02-25 05:47:22 -06:00
|
|
|
ui_enabled = 0;
|
2014-01-09 07:49:45 -06:00
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-22 06:05:19 -06:00
|
|
|
static void ui_vprint_center(int line, int attrs, const char *fmt, va_list ap)
|
2010-01-12 06:58:38 -06:00
|
|
|
{
|
2014-01-09 07:49:45 -06:00
|
|
|
size_t width;
|
|
|
|
char *buf = NULL;
|
2010-01-12 06:58:38 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
move(line, 0);
|
|
|
|
clrtoeol();
|
2010-01-12 06:58:38 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
xvasprintf(&buf, fmt, ap);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
width = mbs_safe_width(buf);
|
2014-01-31 03:31:08 -06:00
|
|
|
if (width > (size_t) COLS) {
|
|
|
|
char *p = strrchr(buf + COLS, ' ');
|
|
|
|
if (!p)
|
|
|
|
p = buf + COLS;
|
|
|
|
*p = '\0';
|
|
|
|
if (line + 1 >= LINES)
|
|
|
|
line--;
|
|
|
|
attron(attrs);
|
|
|
|
mvaddstr(line, 0, buf);
|
|
|
|
mvaddstr(line + 1, 0, p+1);
|
|
|
|
attroff(attrs);
|
|
|
|
} else {
|
|
|
|
attron(attrs);
|
|
|
|
mvaddstr(line, (COLS - width) / 2, buf);
|
|
|
|
attroff(attrs);
|
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
free(buf);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-22 06:05:19 -06:00
|
|
|
static void ui_center(int line, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ui_vprint_center(line, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2014-01-22 07:22:35 -06:00
|
|
|
static void ui_warnx(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (ui_enabled)
|
2014-03-10 08:44:50 -05:00
|
|
|
ui_vprint_center(INFO_LINE,
|
|
|
|
colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
|
|
|
|
fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
else {
|
2014-01-22 07:22:35 -06:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
2014-01-22 07:22:35 -06:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_warn(const char *fmt, ...)
|
2014-01-22 06:05:19 -06:00
|
|
|
{
|
2014-01-22 07:22:35 -06:00
|
|
|
char *fmt_m;
|
2014-01-22 06:05:19 -06:00
|
|
|
va_list ap;
|
2014-01-22 07:22:35 -06:00
|
|
|
|
|
|
|
xasprintf(&fmt_m, "%s: %m", fmt);
|
|
|
|
|
2014-01-22 06:05:19 -06:00
|
|
|
va_start(ap, fmt);
|
2014-01-22 07:22:35 -06:00
|
|
|
if (ui_enabled)
|
2014-03-10 08:44:50 -05:00
|
|
|
ui_vprint_center(INFO_LINE,
|
|
|
|
colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
|
|
|
|
fmt_m, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
else {
|
2014-01-22 07:22:35 -06:00
|
|
|
vfprintf(stderr, fmt_m, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
2014-01-22 06:05:19 -06:00
|
|
|
va_end(ap);
|
2014-01-22 07:22:35 -06:00
|
|
|
free(fmt_m);
|
2014-01-22 06:05:19 -06:00
|
|
|
}
|
|
|
|
|
2014-02-25 05:47:22 -06:00
|
|
|
static int __attribute__((__noreturn__)) ui_errx(int rc, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
ui_end();
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "%s: ", program_invocation_short_name);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
fputc('\n', stderr);
|
2014-02-25 05:47:22 -06:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
2014-01-22 06:05:19 -06:00
|
|
|
static void ui_info(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2014-01-22 07:22:35 -06:00
|
|
|
if (ui_enabled)
|
|
|
|
ui_vprint_center(INFO_LINE, A_BOLD, fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
else {
|
2014-01-22 07:22:35 -06:00
|
|
|
vfprintf(stdout, fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
2014-01-22 06:05:19 -06:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_clean_info(void)
|
|
|
|
{
|
|
|
|
move(INFO_LINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
static void ui_hint(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (ui_enabled)
|
|
|
|
ui_vprint_center(HINT_LINE, A_BOLD, fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
else {
|
2014-01-29 07:22:12 -06:00
|
|
|
vfprintf(stdout, fmt, ap);
|
2014-07-15 05:32:38 -05:00
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
2014-01-29 07:22:12 -06:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ui_clean_hint(void)
|
|
|
|
{
|
|
|
|
move(HINT_LINE, 0);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static void die_on_signal(int dummy __attribute__((__unused__)))
|
|
|
|
{
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("die on signal."));
|
2014-02-25 05:47:22 -06:00
|
|
|
ui_end();
|
2014-01-09 07:49:45 -06:00
|
|
|
exit(EXIT_FAILURE);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
static void resize_on_signal(int dummy __attribute__((__unused__)))
|
|
|
|
{
|
|
|
|
DBG(FRONTEND, ul_debug("resize on signal."));
|
|
|
|
ui_resize = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menu_refresh_size(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
if (cf->menu && cf->menu->nitems)
|
2014-07-13 11:31:11 -05:00
|
|
|
cf->menu->page_sz = (cf->menu->nitems / (LINES - 4)) ? LINES - 4 : 0;
|
2014-06-05 07:32:51 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static void menu_update_ignore(struct cfdisk *cf)
|
|
|
|
{
|
2014-01-22 04:53:34 -06:00
|
|
|
char ignore[128] = { 0 };
|
|
|
|
int i = 0;
|
2014-01-09 07:49:45 -06:00
|
|
|
struct cfdisk_menu *m;
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d, *org;
|
2014-01-22 07:22:35 -06:00
|
|
|
size_t idx;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
assert(cf);
|
2014-02-25 06:22:26 -06:00
|
|
|
assert(cf->menu);
|
|
|
|
assert(cf->menu->ignore_cb);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
m = cf->menu;
|
2014-02-26 06:02:30 -06:00
|
|
|
org = menu_get_menuitem(cf, m->idx);
|
2014-01-22 07:22:35 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("menu: update menu ignored keys"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-25 06:22:26 -06:00
|
|
|
i = m->ignore_cb(cf, ignore, sizeof(ignore));
|
2014-01-22 04:53:34 -06:00
|
|
|
ignore[i] = '\0';
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* return if no change */
|
2014-01-22 04:53:34 -06:00
|
|
|
if ( (!m->ignore && !*ignore)
|
|
|
|
|| (m->ignore && *ignore && strcmp(m->ignore, ignore) == 0)) {
|
2014-01-09 07:49:45 -06:00
|
|
|
return;
|
2006-12-06 17:25:34 -06:00
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
free(m->ignore);
|
2014-01-22 04:10:54 -06:00
|
|
|
m->ignore = xstrdup(ignore);
|
2014-01-09 07:49:45 -06:00
|
|
|
m->nitems = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
for (d = m->items; d->name; d++) {
|
2014-01-09 07:49:45 -06:00
|
|
|
if (m->ignore && strchr(m->ignore, d->key))
|
2014-01-22 04:10:54 -06:00
|
|
|
continue;
|
|
|
|
m->nitems++;
|
2014-01-09 07:49:45 -06:00
|
|
|
}
|
2014-01-22 07:22:35 -06:00
|
|
|
|
|
|
|
/* refresh menu index to be at the same menuitem or go to the first */
|
|
|
|
if (org && menu_get_menuitem_by_key(cf, org->key, &idx))
|
2014-02-26 06:02:30 -06:00
|
|
|
m->idx = idx;
|
2014-01-22 07:22:35 -06:00
|
|
|
else
|
2014-02-26 06:02:30 -06:00
|
|
|
m->idx = 0;
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
menu_refresh_size(cf);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
static struct cfdisk_menu *menu_push(
|
|
|
|
struct cfdisk *cf,
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *items)
|
2014-01-09 07:49:45 -06:00
|
|
|
{
|
|
|
|
struct cfdisk_menu *m = xcalloc(1, sizeof(*m));
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
assert(cf);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("menu: new menu"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
m->prev = cf->menu;
|
2014-03-07 06:01:22 -06:00
|
|
|
m->items = items;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
for (d = m->items; d->name; d++) {
|
2014-01-09 07:49:45 -06:00
|
|
|
const char *name = _(d->name);
|
2014-01-29 07:22:12 -06:00
|
|
|
size_t len = mbs_safe_width(name);
|
2014-01-09 07:49:45 -06:00
|
|
|
if (len > m->width)
|
|
|
|
m->width = len;
|
|
|
|
m->nitems++;
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
cf->menu = m;
|
2014-06-05 07:32:51 -05:00
|
|
|
menu_refresh_size(cf);
|
2014-01-09 07:49:45 -06:00
|
|
|
return m;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static struct cfdisk_menu *menu_pop(struct cfdisk *cf)
|
2006-12-06 17:25:32 -06:00
|
|
|
{
|
2014-01-09 07:49:45 -06:00
|
|
|
struct cfdisk_menu *m = NULL;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
assert(cf);
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("menu: rem menu"));
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
if (cf->menu) {
|
|
|
|
m = cf->menu->prev;
|
|
|
|
free(cf->menu->ignore);
|
2014-02-24 06:57:08 -06:00
|
|
|
free(cf->menu->title);
|
2014-01-09 07:49:45 -06:00
|
|
|
free(cf->menu);
|
2006-12-06 17:25:46 -06:00
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
cf->menu = m;
|
|
|
|
return cf->menu;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
static void menu_set_title(struct cfdisk_menu *m, const char *title)
|
|
|
|
{
|
|
|
|
char *str = NULL;
|
|
|
|
|
|
|
|
if (title) {
|
|
|
|
size_t len = mbs_safe_width(title);
|
|
|
|
if (len + 3 > m->width)
|
|
|
|
m->width = len + 3;
|
|
|
|
str = xstrdup(title);
|
|
|
|
}
|
|
|
|
m->title = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
static int ui_init(struct cfdisk *cf __attribute__((__unused__)))
|
2014-01-09 07:49:45 -06:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: init"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* setup SIGCHLD handler */
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sa.sa_handler = die_on_signal;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
sa.sa_handler = resize_on_signal;
|
|
|
|
sigaction(SIGWINCH, &sa, NULL);
|
|
|
|
|
2014-01-22 07:22:35 -06:00
|
|
|
ui_enabled = 1;
|
2014-01-09 07:49:45 -06:00
|
|
|
initscr();
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-10 08:44:50 -05:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2014-02-27 06:28:40 -06:00
|
|
|
if (colors_wanted() && has_colors()) {
|
2014-01-22 06:05:19 -06:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
|
|
|
for (i = 1; i < ARRAY_SIZE(color_pairs); i++) /* yeah, start from 1! */
|
|
|
|
init_pair(i, color_pairs[i][0], color_pairs[i][1]);
|
|
|
|
}
|
2014-03-10 08:44:50 -05:00
|
|
|
#else
|
2014-04-30 07:51:46 -05:00
|
|
|
colors_off();
|
2014-03-10 08:44:50 -05:00
|
|
|
#endif
|
2014-01-22 06:05:19 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
nonl();
|
|
|
|
curs_set(0);
|
|
|
|
keypad(stdscr, TRUE);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
return 0;
|
2006-12-06 17:25:39 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static size_t menuitem_get_line(struct cfdisk *cf, size_t idx)
|
|
|
|
{
|
2014-02-26 06:02:30 -06:00
|
|
|
struct cfdisk_menu *m = cf->menu;
|
|
|
|
|
|
|
|
if (m->vertical) {
|
|
|
|
if (!m->page_sz) /* small menu */
|
|
|
|
return (LINES - (cf->menu->nitems + 1)) / 2 + idx;
|
|
|
|
return (idx % m->page_sz) + 1;
|
2014-02-24 06:57:08 -06:00
|
|
|
} else {
|
2014-02-26 06:02:30 -06:00
|
|
|
size_t len = m->width + 4 + MENU_PADDING; /* item width */
|
|
|
|
size_t items = COLS / len; /* items per line */
|
2014-02-24 06:57:08 -06:00
|
|
|
|
2014-07-01 06:25:46 -05:00
|
|
|
if (items == 0)
|
|
|
|
return 0;
|
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
return MENU_START_LINE + ((idx / items));
|
|
|
|
}
|
2006-12-06 17:25:39 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static int menuitem_get_column(struct cfdisk *cf, size_t idx)
|
|
|
|
{
|
2014-02-24 06:57:08 -06:00
|
|
|
if (cf->menu->vertical) {
|
|
|
|
size_t nc = cf->menu->width + MENU_PADDING;
|
2014-02-25 05:47:22 -06:00
|
|
|
if ((size_t) COLS <= nc)
|
|
|
|
return 0;
|
|
|
|
return (COLS - nc) / 2;
|
2014-02-24 06:57:08 -06:00
|
|
|
} else {
|
|
|
|
size_t len = cf->menu->width + 4 + MENU_PADDING; /* item width */
|
|
|
|
size_t items = COLS / len; /* items per line */
|
|
|
|
size_t extra = items < cf->menu->nitems ? /* extra space on line */
|
|
|
|
COLS % len : /* - multi-line menu */
|
|
|
|
COLS - (cf->menu->nitems * len); /* - one line menu */
|
|
|
|
|
2014-07-01 06:25:46 -05:00
|
|
|
if (items == 0)
|
|
|
|
return 0; /* hmm... no space */
|
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
extra += MENU_PADDING; /* add padding after last item to extra */
|
2011-01-02 15:58:13 -06:00
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
if (idx < items)
|
|
|
|
return (idx * len) + (extra / 2);
|
|
|
|
return ((idx % items) * len) + (extra / 2);
|
|
|
|
}
|
2006-12-06 17:26:12 -06:00
|
|
|
}
|
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
static int menuitem_on_page(struct cfdisk *cf, size_t idx)
|
|
|
|
{
|
|
|
|
struct cfdisk_menu *m = cf->menu;
|
|
|
|
|
|
|
|
if (m->page_sz == 0 ||
|
|
|
|
m->idx / m->page_sz == idx / m->page_sz)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
static struct cfdisk_menuitem *menu_get_menuitem(struct cfdisk *cf, size_t idx)
|
2014-01-09 07:49:45 -06:00
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d;
|
2014-01-09 07:49:45 -06:00
|
|
|
size_t i;
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
for (i = 0, d = cf->menu->items; d->name; d++) {
|
2014-01-09 07:49:45 -06:00
|
|
|
if (cf->menu->ignore && strchr(cf->menu->ignore, d->key))
|
|
|
|
continue;
|
|
|
|
if (i++ == idx)
|
|
|
|
return d;
|
2006-12-06 17:26:19 -06:00
|
|
|
}
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
return NULL;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
static struct cfdisk_menuitem *menu_get_menuitem_by_key(struct cfdisk *cf,
|
2014-01-22 07:22:35 -06:00
|
|
|
int key, size_t *idx)
|
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d;
|
2014-01-22 07:22:35 -06:00
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
for (*idx = 0, d = cf->menu->items; d->name; d++) {
|
2014-01-22 07:22:35 -06:00
|
|
|
if (cf->menu->ignore && strchr(cf->menu->ignore, d->key))
|
|
|
|
continue;
|
|
|
|
if (key == d->key)
|
|
|
|
return d;
|
|
|
|
(*idx)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static void ui_draw_menuitem(struct cfdisk *cf,
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d,
|
2014-01-09 07:49:45 -06:00
|
|
|
size_t idx)
|
|
|
|
{
|
|
|
|
char buf[80 * MB_CUR_MAX];
|
|
|
|
const char *name;
|
|
|
|
size_t width = cf->menu->width + 2; /* 2 = blank around string */
|
2014-02-24 06:57:08 -06:00
|
|
|
int ln, cl, vert = cf->menu->vertical;
|
2014-01-09 07:49:45 -06:00
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
if (!menuitem_on_page(cf, idx))
|
|
|
|
return; /* no visible item */
|
|
|
|
ln = menuitem_get_line(cf, idx);
|
|
|
|
cl = menuitem_get_column(cf, idx);
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
name = _(d->name);
|
2014-02-24 06:57:08 -06:00
|
|
|
mbsalign(name, buf, sizeof(buf), &width,
|
|
|
|
vert ? MBS_ALIGN_LEFT : MBS_ALIGN_CENTER,
|
|
|
|
0);
|
2014-01-09 07:49:45 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: menuitem: cl=%d, ln=%d, item='%s'",
|
2014-01-09 07:49:45 -06:00
|
|
|
cl, ln, buf));
|
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
if (vert) {
|
|
|
|
mvaddch(ln, cl - 1, ACS_VLINE);
|
|
|
|
mvaddch(ln, cl + cf->menu->width + 4, ACS_VLINE);
|
|
|
|
}
|
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
if (cf->menu->idx == idx) {
|
2014-01-09 07:49:45 -06:00
|
|
|
standout();
|
2014-02-24 06:57:08 -06:00
|
|
|
mvprintw(ln, cl, vert ? " %s " : "[%s]", buf);
|
2014-01-09 07:49:45 -06:00
|
|
|
standend();
|
|
|
|
if (d->desc)
|
2014-01-29 07:22:12 -06:00
|
|
|
ui_hint(d->desc);
|
2014-01-09 07:49:45 -06:00
|
|
|
} else
|
2014-02-24 06:57:08 -06:00
|
|
|
mvprintw(ln, cl, vert ? " %s " : "[%s]", buf);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
static void ui_clean_menu(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t nlines;
|
|
|
|
struct cfdisk_menu *m = cf->menu;
|
|
|
|
size_t ln = menuitem_get_line(cf, 0);
|
|
|
|
|
|
|
|
if (m->vertical)
|
|
|
|
nlines = m->page_sz ? m->page_sz : m->nitems;
|
|
|
|
else
|
|
|
|
nlines = menuitem_get_line(cf, m->nitems);
|
|
|
|
|
|
|
|
for (i = ln; i <= ln + nlines; i++) {
|
|
|
|
move(i, 0);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
if (m->vertical) {
|
|
|
|
move(ln - 1, 0);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
ui_clean_hint();
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static void ui_draw_menu(struct cfdisk *cf)
|
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d;
|
2014-02-24 06:57:08 -06:00
|
|
|
struct cfdisk_menu *m;
|
2014-02-26 06:02:30 -06:00
|
|
|
size_t i = 0;
|
|
|
|
size_t ln = menuitem_get_line(cf, 0);
|
|
|
|
size_t nlines;
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
assert(cf);
|
|
|
|
assert(cf->menu);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: menu: draw start"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
ui_clean_menu(cf);
|
2014-02-24 06:57:08 -06:00
|
|
|
m = cf->menu;
|
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
if (m->vertical)
|
|
|
|
nlines = m->page_sz ? m->page_sz : m->nitems;
|
|
|
|
else
|
|
|
|
nlines = menuitem_get_line(cf, m->nitems);
|
|
|
|
|
2014-02-25 06:22:26 -06:00
|
|
|
if (m->ignore_cb)
|
|
|
|
menu_update_ignore(cf);
|
2014-01-22 04:53:34 -06:00
|
|
|
i = 0;
|
2014-01-09 07:49:45 -06:00
|
|
|
while ((d = menu_get_menuitem(cf, i)))
|
|
|
|
ui_draw_menuitem(cf, d, i++);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
if (m->vertical) {
|
|
|
|
size_t cl = menuitem_get_column(cf, 0);
|
2014-02-26 06:02:30 -06:00
|
|
|
size_t curpg = m->page_sz ? m->idx / m->page_sz : 0;
|
2014-02-24 06:57:08 -06:00
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
/* corners and horizontal lines */
|
2014-02-24 06:57:08 -06:00
|
|
|
mvaddch(ln - 1, cl - 1, ACS_ULCORNER);
|
|
|
|
mvaddch(ln + nlines, cl - 1, ACS_LLCORNER);
|
|
|
|
|
|
|
|
for (i = 0; i < m->width + 4; i++) {
|
|
|
|
mvaddch(ln - 1, cl + i, ACS_HLINE);
|
|
|
|
mvaddch(ln + nlines, cl + i, ACS_HLINE);
|
|
|
|
}
|
2014-02-26 06:02:30 -06:00
|
|
|
|
2014-02-24 06:57:08 -06:00
|
|
|
mvaddch(ln - 1, cl + i, ACS_URCORNER);
|
|
|
|
mvaddch(ln + nlines, cl + i, ACS_LRCORNER);
|
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
/* draw also lines around empty lines on last page */
|
|
|
|
if (m->page_sz &&
|
|
|
|
m->nitems / m->page_sz == m->idx / m->page_sz) {
|
|
|
|
for (i = m->nitems % m->page_sz + 1; i <= m->page_sz; i++) {
|
|
|
|
mvaddch(i, cl - 1, ACS_VLINE);
|
|
|
|
mvaddch(i, cl + cf->menu->width + 4, ACS_VLINE);
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 06:57:08 -06:00
|
|
|
if (m->title) {
|
|
|
|
attron(A_BOLD);
|
|
|
|
mvprintw(ln - 1, cl, " %s ", m->title);
|
|
|
|
attroff(A_BOLD);
|
|
|
|
}
|
2014-02-26 06:02:30 -06:00
|
|
|
if (curpg != 0)
|
|
|
|
mvaddch(ln - 1, cl + m->width + 3, ACS_UARROW);
|
|
|
|
if (m->page_sz && curpg < m->nitems / m->page_sz)
|
|
|
|
mvaddch(ln + nlines, cl + m->width + 3, ACS_DARROW);
|
2014-02-24 06:57:08 -06:00
|
|
|
}
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: menu: draw end."));
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static void ui_menu_goto(struct cfdisk *cf, int where)
|
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d;
|
2014-01-09 07:49:45 -06:00
|
|
|
size_t old;
|
|
|
|
|
2014-02-27 06:05:19 -06:00
|
|
|
/* stop and begin/end for vertical menus */
|
|
|
|
if (cf->menu->vertical) {
|
|
|
|
if (where < 0)
|
|
|
|
where = 0;
|
|
|
|
else if (where > (int) cf->menu->nitems - 1)
|
|
|
|
where = cf->menu->nitems - 1;
|
|
|
|
} else {
|
|
|
|
/* continue from begin/end */
|
|
|
|
if (where < 0)
|
|
|
|
where = cf->menu->nitems - 1;
|
|
|
|
else if ((size_t) where > cf->menu->nitems - 1)
|
|
|
|
where = 0;
|
|
|
|
}
|
2014-02-26 06:02:30 -06:00
|
|
|
if ((size_t) where == cf->menu->idx)
|
2014-01-09 07:49:45 -06:00
|
|
|
return;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-22 06:05:19 -06:00
|
|
|
ui_clean_info();
|
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
old = cf->menu->idx;
|
|
|
|
cf->menu->idx = where;
|
|
|
|
|
|
|
|
if (!menuitem_on_page(cf, old)) {
|
|
|
|
ui_draw_menu(cf);
|
|
|
|
return;
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
d = menu_get_menuitem(cf, old);
|
|
|
|
ui_draw_menuitem(cf, d, old);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
d = menu_get_menuitem(cf, where);
|
|
|
|
ui_draw_menuitem(cf, d, where);
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-02-24 07:27:49 -06:00
|
|
|
static int ui_menu_move(struct cfdisk *cf, int key)
|
|
|
|
{
|
2014-02-27 06:05:19 -06:00
|
|
|
struct cfdisk_menu *m;
|
|
|
|
|
2014-02-24 07:27:49 -06:00
|
|
|
assert(cf);
|
|
|
|
assert(cf->menu);
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
if (key == ERR)
|
|
|
|
return 0; /* ignore errors */
|
|
|
|
|
2014-02-27 06:05:19 -06:00
|
|
|
m = cf->menu;
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: menu move key >%c<.", key));
|
2014-02-27 06:05:19 -06:00
|
|
|
|
|
|
|
if (m->vertical)
|
2014-02-24 07:27:49 -06:00
|
|
|
{
|
|
|
|
switch (key) {
|
|
|
|
case KEY_DOWN:
|
|
|
|
case '\016': /* ^N */
|
|
|
|
case 'j': /* Vi-like alternative */
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_menu_goto(cf, m->idx + 1);
|
2014-02-24 07:27:49 -06:00
|
|
|
return 0;
|
|
|
|
case KEY_UP:
|
|
|
|
case '\020': /* ^P */
|
|
|
|
case 'k': /* Vi-like alternative */
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_menu_goto(cf, (int) m->idx - 1);
|
2014-02-24 07:27:49 -06:00
|
|
|
return 0;
|
2014-02-27 06:05:19 -06:00
|
|
|
case KEY_PPAGE:
|
|
|
|
if (m->page_sz) {
|
|
|
|
ui_menu_goto(cf, (int) m->idx - m->page_sz);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-17 06:59:01 -05:00
|
|
|
/* fallthrough */
|
2014-02-24 07:27:49 -06:00
|
|
|
case KEY_HOME:
|
|
|
|
ui_menu_goto(cf, 0);
|
|
|
|
return 0;
|
2014-02-27 06:05:19 -06:00
|
|
|
case KEY_NPAGE:
|
|
|
|
if (m->page_sz) {
|
|
|
|
ui_menu_goto(cf, m->idx + m->page_sz);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-17 06:59:01 -05:00
|
|
|
/* fallthrough */
|
2014-02-24 07:27:49 -06:00
|
|
|
case KEY_END:
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_menu_goto(cf, m->nitems);
|
2014-02-24 07:27:49 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (key) {
|
|
|
|
case KEY_RIGHT:
|
|
|
|
case '\t':
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_menu_goto(cf, m->idx + 1);
|
2014-02-24 07:27:49 -06:00
|
|
|
return 0;
|
|
|
|
case KEY_LEFT:
|
|
|
|
#ifdef KEY_BTAB
|
|
|
|
case KEY_BTAB:
|
|
|
|
#endif
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_menu_goto(cf, (int) m->idx - 1);
|
2014-02-24 07:27:49 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* key irrelevant for menu move */
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
/* but don't call me from ui_run(), this is for pop-up menus only */
|
|
|
|
static void ui_menu_resize(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
resize();
|
|
|
|
ui_clean_menu(cf);
|
|
|
|
menu_refresh_size(cf);
|
|
|
|
ui_draw_menu(cf);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2014-02-27 05:24:50 -06:00
|
|
|
static int partition_on_page(struct cfdisk *cf, size_t i)
|
|
|
|
{
|
|
|
|
if (cf->page_sz == 0 ||
|
|
|
|
cf->lines_idx / cf->page_sz == i / cf->page_sz)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static void ui_draw_partition(struct cfdisk *cf, size_t i)
|
|
|
|
{
|
|
|
|
int ln = TABLE_START_LINE + 1 + i; /* skip table header */
|
|
|
|
int cl = ARROW_CURSOR_WIDTH; /* we need extra space for cursor */
|
2014-02-27 05:24:50 -06:00
|
|
|
int cur = cf->lines_idx == i;
|
|
|
|
size_t curpg = 0;
|
|
|
|
|
|
|
|
if (cf->page_sz) {
|
|
|
|
if (!partition_on_page(cf, i))
|
|
|
|
return;
|
|
|
|
ln = TABLE_START_LINE + (i % cf->page_sz) + 1;
|
|
|
|
curpg = cf->lines_idx / cf->page_sz;
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug(
|
2014-02-27 05:24:50 -06:00
|
|
|
"ui: draw partition %zu [page_sz=%zu, "
|
|
|
|
"line=%d, idx=%zu]",
|
|
|
|
i, cf->page_sz, ln, cf->lines_idx));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 05:24:50 -06:00
|
|
|
if (cur) {
|
|
|
|
attron(A_REVERSE);
|
2014-01-09 07:49:45 -06:00
|
|
|
mvaddstr(ln, 0, ARROW_CURSOR_STRING);
|
|
|
|
mvaddstr(ln, cl, cf->lines[i + 1]);
|
2014-02-27 05:24:50 -06:00
|
|
|
attroff(A_REVERSE);
|
2006-12-06 17:25:32 -06:00
|
|
|
} else {
|
2014-02-03 07:40:35 -06:00
|
|
|
int at = 0;
|
|
|
|
|
2014-03-10 08:44:50 -05:00
|
|
|
if (colors_wanted() && is_freespace(cf, i)) {
|
2014-02-03 07:40:35 -06:00
|
|
|
attron(COLOR_PAIR(CFDISK_CL_FREESPACE));
|
|
|
|
at = 1;
|
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
mvaddstr(ln, 0, ARROW_CURSOR_DUMMY);
|
|
|
|
mvaddstr(ln, cl, cf->lines[i + 1]);
|
2014-02-03 07:40:35 -06:00
|
|
|
if (at)
|
|
|
|
attroff(COLOR_PAIR(CFDISK_CL_FREESPACE));
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-02-27 05:24:50 -06:00
|
|
|
if ((size_t) ln == MENU_START_LINE - 1 &&
|
|
|
|
cf->page_sz && curpg < cf->nlines / cf->page_sz) {
|
|
|
|
if (cur)
|
|
|
|
attron(A_REVERSE);
|
|
|
|
mvaddch(ln, COLS - 1, ACS_DARROW);
|
|
|
|
mvaddch(ln, 0, ACS_DARROW);
|
|
|
|
if (cur)
|
|
|
|
attroff(A_REVERSE);
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static int ui_draw_table(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
int cl = ARROW_CURSOR_WIDTH;
|
|
|
|
size_t i, nparts = fdisk_table_get_nents(cf->table);
|
2014-02-27 05:24:50 -06:00
|
|
|
size_t curpg = cf->page_sz ? cf->lines_idx / cf->page_sz : 0;
|
2006-12-06 17:25:39 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: draw table"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 05:24:50 -06:00
|
|
|
for (i = TABLE_START_LINE; i <= TABLE_START_LINE + cf->page_sz; i++) {
|
|
|
|
move(i, 0);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-10 05:57:26 -05:00
|
|
|
if ((size_t) cf->lines_idx > nparts - 1)
|
|
|
|
cf->lines_idx = nparts ? nparts - 1 : 0;
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* print header */
|
|
|
|
attron(A_BOLD);
|
|
|
|
mvaddstr(TABLE_START_LINE, cl, cf->lines[0]);
|
|
|
|
attroff(A_BOLD);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* print partitions */
|
|
|
|
for (i = 0; i < nparts; i++)
|
|
|
|
ui_draw_partition(cf, i);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 05:24:50 -06:00
|
|
|
if (curpg != 0) {
|
|
|
|
mvaddch(TABLE_START_LINE, COLS - 1, ACS_UARROW);
|
|
|
|
mvaddch(TABLE_START_LINE, 0, ACS_UARROW);
|
|
|
|
}
|
|
|
|
if (cf->page_sz && curpg < cf->nlines / cf->page_sz) {
|
|
|
|
mvaddch(MENU_START_LINE - 1, COLS - 1, ACS_DARROW);
|
|
|
|
mvaddch(MENU_START_LINE - 1, 0, ACS_DARROW);
|
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static int ui_table_goto(struct cfdisk *cf, int where)
|
|
|
|
{
|
|
|
|
size_t old;
|
|
|
|
size_t nparts = fdisk_table_get_nents(cf->table);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: goto table %d", where));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
if (where < 0)
|
|
|
|
where = 0;
|
|
|
|
else if ((size_t) where > nparts - 1)
|
|
|
|
where = nparts - 1;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
if ((size_t) where == cf->lines_idx)
|
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
old = cf->lines_idx;
|
|
|
|
cf->lines_idx = where;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 05:24:50 -06:00
|
|
|
if (!partition_on_page(cf, old) ||!partition_on_page(cf, where))
|
|
|
|
ui_draw_table(cf);
|
|
|
|
else {
|
|
|
|
ui_draw_partition(cf, old); /* cleanup old */
|
|
|
|
ui_draw_partition(cf, where); /* draw new */
|
|
|
|
}
|
2014-01-22 06:05:19 -06:00
|
|
|
ui_clean_info();
|
2014-01-09 07:49:45 -06:00
|
|
|
ui_draw_menu(cf);
|
|
|
|
refresh();
|
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static int ui_refresh(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
char *id = NULL;
|
|
|
|
uint64_t bytes = cf->cxt->total_sectors * cf->cxt->sector_size;
|
|
|
|
char *strsz = size_to_human_string(SIZE_SUFFIX_SPACE
|
|
|
|
| SIZE_SUFFIX_3LETTER, bytes);
|
|
|
|
erase();
|
|
|
|
|
2014-01-22 07:22:35 -06:00
|
|
|
if (!ui_enabled)
|
2014-01-09 07:49:45 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* header */
|
|
|
|
attron(A_BOLD);
|
2014-01-22 06:05:19 -06:00
|
|
|
ui_center(0, _("Disk: %s"), cf->cxt->dev_path);
|
2014-01-09 07:49:45 -06:00
|
|
|
attroff(A_BOLD);
|
2014-01-22 06:05:19 -06:00
|
|
|
ui_center(1, _("Size: %s, %ju bytes, %ju sectors"),
|
2014-01-09 07:49:45 -06:00
|
|
|
strsz, bytes, (uintmax_t) cf->cxt->total_sectors);
|
|
|
|
if (fdisk_get_disklabel_id(cf->cxt, &id) == 0 && id)
|
2014-01-22 06:05:19 -06:00
|
|
|
ui_center(2, _("Label: %s, identifier: %s"),
|
2014-01-09 07:49:45 -06:00
|
|
|
cf->cxt->label->name, id);
|
2006-12-06 17:25:39 -06:00
|
|
|
else
|
2014-03-07 06:44:31 -06:00
|
|
|
ui_center(2, _("Label: %s"), cf->cxt->label->name);
|
2014-01-09 07:49:45 -06:00
|
|
|
free(strsz);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
ui_draw_table(cf);
|
|
|
|
ui_draw_menu(cf);
|
2006-12-06 17:25:32 -06:00
|
|
|
refresh();
|
2014-01-09 07:49:45 -06:00
|
|
|
return 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
static ssize_t ui_get_string(struct cfdisk *cf, const char *prompt,
|
|
|
|
const char *hint, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
size_t cells = 0;
|
|
|
|
ssize_t i = 0, rc = -1;
|
|
|
|
int ln = MENU_START_LINE, cl = 1;
|
|
|
|
|
|
|
|
assert(cf);
|
|
|
|
assert(buf);
|
|
|
|
assert(len);
|
|
|
|
|
|
|
|
move(ln, 0);
|
|
|
|
clrtoeol();
|
|
|
|
|
|
|
|
if (prompt) {
|
2014-03-10 08:44:50 -05:00
|
|
|
mvaddstr(ln, cl, (char *) prompt);
|
2014-01-29 07:22:12 -06:00
|
|
|
cl += mbs_safe_width(prompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* default value */
|
|
|
|
if (*buf) {
|
|
|
|
i = strlen(buf);
|
|
|
|
cells = mbs_safe_width(buf);
|
|
|
|
mvaddstr(ln, cl, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hint)
|
|
|
|
ui_hint(hint);
|
|
|
|
else
|
|
|
|
ui_clean_hint();
|
|
|
|
|
|
|
|
move(ln, cl + cells);
|
|
|
|
curs_set(1);
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
#if !defined(HAVE_SLCURSES_H) && !defined(HAVE_SLANG_SLCURSES_H) && \
|
|
|
|
defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
|
2014-05-13 02:58:58 -05:00
|
|
|
wint_t c;
|
2014-01-29 07:22:12 -06:00
|
|
|
if (get_wch(&c) == ERR) {
|
|
|
|
#else
|
2014-05-13 02:58:58 -05:00
|
|
|
int c;
|
2014-01-29 07:22:12 -06:00
|
|
|
if ((c = getch()) == ERR) {
|
|
|
|
#endif
|
2014-06-05 07:32:51 -05:00
|
|
|
if (ui_resize) {
|
|
|
|
resize();
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-29 07:22:12 -06:00
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
exit(2);
|
|
|
|
else
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (c == '\r' || c == '\n' || c == KEY_ENTER)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case KEY_ESC:
|
|
|
|
rc = -CFDISK_ERR_ESC;
|
|
|
|
goto done;
|
|
|
|
case KEY_DELETE:
|
|
|
|
case '\b':
|
|
|
|
case KEY_BACKSPACE:
|
|
|
|
if (i > 0) {
|
|
|
|
cells--;
|
|
|
|
i = mbs_truncate(buf, &cells);
|
|
|
|
if (i < 0)
|
|
|
|
goto done;
|
|
|
|
mvaddch(ln, cl + cells, ' ');
|
|
|
|
move(ln, cl + cells);
|
|
|
|
} else
|
|
|
|
beep();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
#if defined(HAVE_LIBNCURSESW) && defined(HAVE_WIDECHAR)
|
|
|
|
if (i + 1 < (ssize_t) len && iswprint(c)) {
|
|
|
|
wchar_t wc = (wchar_t) c;
|
|
|
|
char s[MB_CUR_MAX + 1];
|
|
|
|
int sz = wctomb(s, wc);
|
|
|
|
|
|
|
|
if (sz > 0 && sz + i < (ssize_t) len) {
|
|
|
|
s[sz] = '\0';
|
|
|
|
mvaddnstr(ln, cl + cells, s, sz);
|
|
|
|
memcpy(buf + i, s, sz);
|
|
|
|
i += sz;
|
|
|
|
buf[i] = '\0';
|
|
|
|
cells += wcwidth(wc);
|
|
|
|
} else
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (i + 1 < (ssize_t) len && isprint(c)) {
|
|
|
|
mvaddch(ln, cl + cells, c);
|
2014-03-14 07:00:01 -05:00
|
|
|
buf[i++] = c;
|
|
|
|
buf[i] = '\0';
|
2014-01-29 07:22:12 -06:00
|
|
|
cells++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = i; /* success */
|
|
|
|
done:
|
|
|
|
move(ln, 0);
|
|
|
|
clrtoeol();
|
|
|
|
curs_set(0);
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @res is default value as well as result in bytes */
|
|
|
|
static int ui_get_size(struct cfdisk *cf, const char *prompt, uintmax_t *res,
|
|
|
|
uintmax_t low, uintmax_t up)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
uintmax_t user = 0;
|
|
|
|
ssize_t rc;
|
|
|
|
char *dflt = size_to_human_string(0, *res);
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: get_size (default=%ju)", *res));
|
2014-01-29 07:22:12 -06:00
|
|
|
|
|
|
|
ui_clean_info();
|
|
|
|
|
|
|
|
do {
|
2014-01-31 03:31:08 -06:00
|
|
|
int pwr = 0, insec = 0;
|
2014-01-29 07:22:12 -06:00
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s", dflt);
|
|
|
|
rc = ui_get_string(cf, prompt,
|
2014-02-04 16:09:26 -06:00
|
|
|
_("May be followed by {M,B,G,T}iB "
|
2014-01-31 03:31:08 -06:00
|
|
|
"(the \"iB\" is optional) or S for sectors."),
|
2014-01-29 07:22:12 -06:00
|
|
|
buf, sizeof(buf));
|
|
|
|
if (rc == 0) {
|
|
|
|
ui_warnx(_("Please, specify size."));
|
|
|
|
continue; /* nothing specified */
|
|
|
|
} else if (rc == -CFDISK_ERR_ESC)
|
|
|
|
break; /* cancel dialog */
|
|
|
|
|
2014-01-31 03:31:08 -06:00
|
|
|
if (strcmp(buf, dflt) == 0)
|
|
|
|
user = *res, rc = 0; /* no change, use default */
|
|
|
|
else {
|
|
|
|
size_t len = strlen(buf);
|
2014-07-15 06:01:56 -05:00
|
|
|
if (buf[len - 1] == 'S' || buf[len - 1] == 's') {
|
2014-01-31 03:31:08 -06:00
|
|
|
insec = 1;
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
rc = parse_size(buf, &user, &pwr); /* parse */
|
|
|
|
}
|
|
|
|
|
2014-01-29 07:22:12 -06:00
|
|
|
if (rc == 0) {
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: get_size user=%ju, power=%d, sectors=%s",
|
2014-01-31 03:31:08 -06:00
|
|
|
user, pwr, insec ? "yes" : "no"));
|
|
|
|
if (insec)
|
|
|
|
user *= cf->cxt->sector_size;
|
2014-01-29 07:22:12 -06:00
|
|
|
if (user < low) {
|
|
|
|
ui_warnx(_("Minimal size is %ju"), low);
|
|
|
|
rc = -ERANGE;
|
|
|
|
}
|
|
|
|
if (user > up && pwr && user < up + (1ULL << pwr * 10))
|
|
|
|
/* ignore when the user specified size overflow
|
|
|
|
* with in range specified by suffix (e.g. MiB) */
|
|
|
|
user = up;
|
|
|
|
|
|
|
|
if (user > up) {
|
|
|
|
ui_warnx(_("Maximal size is %ju bytes."), up);
|
|
|
|
rc = -ERANGE;
|
|
|
|
}
|
2014-01-30 05:21:11 -06:00
|
|
|
} else
|
|
|
|
ui_warnx(_("Failed to parse size."));
|
2014-01-29 07:22:12 -06:00
|
|
|
} while (rc != 0);
|
|
|
|
|
|
|
|
if (rc == 0)
|
|
|
|
*res = user;
|
|
|
|
free(dflt);
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: get_size (result=%ju, rc=%zd)", *res, rc));
|
2014-01-29 07:22:12 -06:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-02-26 06:58:31 -06:00
|
|
|
static struct fdisk_parttype *ui_get_parttype(struct cfdisk *cf,
|
|
|
|
struct fdisk_parttype *cur)
|
2014-02-26 06:02:30 -06:00
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d, *cm;
|
2014-02-26 06:58:31 -06:00
|
|
|
size_t i = 0, nitems, idx = 0;
|
2014-02-26 06:02:30 -06:00
|
|
|
struct fdisk_parttype *t = NULL;
|
2014-02-26 06:58:31 -06:00
|
|
|
int has_typestr = 0;
|
2014-02-26 06:02:30 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: asking for parttype."));
|
2014-02-26 06:02:30 -06:00
|
|
|
|
|
|
|
/* create cfdisk menu according to label types, note that the
|
|
|
|
* last cm[] item has to be empty -- so nitems + 1 */
|
|
|
|
nitems = cf->cxt->label->nparttypes;
|
2014-02-26 06:58:31 -06:00
|
|
|
if (!nitems)
|
|
|
|
return NULL;
|
2014-03-07 06:01:22 -06:00
|
|
|
cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
|
2014-02-26 06:02:30 -06:00
|
|
|
if (!cm)
|
|
|
|
return NULL;
|
|
|
|
|
2014-02-26 06:58:31 -06:00
|
|
|
has_typestr = cf->cxt->label->parttypes[0].typestr &&
|
|
|
|
*cf->cxt->label->parttypes[0].typestr;
|
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
for (i = 0; i < nitems; i++) {
|
|
|
|
struct fdisk_parttype *x = &cf->cxt->label->parttypes[i];
|
2014-02-26 06:58:31 -06:00
|
|
|
char *name;
|
2014-02-26 06:02:30 -06:00
|
|
|
|
2014-02-26 06:58:31 -06:00
|
|
|
if (!x || !x->name)
|
|
|
|
continue;
|
2014-02-26 06:02:30 -06:00
|
|
|
cm[i].userdata = x;
|
2014-02-26 06:58:31 -06:00
|
|
|
if (!has_typestr)
|
|
|
|
xasprintf(&name, "%2x %s", x->type, x->name);
|
|
|
|
else {
|
|
|
|
name = (char *) x->name;
|
|
|
|
cm[i].desc = x->typestr;
|
|
|
|
}
|
|
|
|
cm[i].name = name;
|
|
|
|
if (x == cur)
|
|
|
|
idx = i;
|
2014-02-26 06:02:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make the new menu active */
|
|
|
|
menu_push(cf, cm);
|
|
|
|
cf->menu->vertical = 1;
|
2014-02-26 06:58:31 -06:00
|
|
|
cf->menu->idx = idx;
|
2014-02-26 06:02:30 -06:00
|
|
|
menu_set_title(cf->menu, _("Select partition type"));
|
|
|
|
ui_draw_menu(cf);
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
do {
|
|
|
|
int key = getch();
|
2014-02-27 06:05:19 -06:00
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
if (ui_resize)
|
|
|
|
ui_menu_resize(cf);
|
2014-02-26 06:02:30 -06:00
|
|
|
if (ui_menu_move(cf, key) == 0)
|
|
|
|
continue;
|
2014-02-27 06:05:19 -06:00
|
|
|
|
2014-02-26 06:02:30 -06:00
|
|
|
switch (key) {
|
|
|
|
case KEY_ENTER:
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
d = menu_get_menuitem(cf, cf->menu->idx);
|
|
|
|
if (d)
|
|
|
|
t = (struct fdisk_parttype *) d->userdata;
|
|
|
|
goto done;
|
2014-07-15 05:41:30 -05:00
|
|
|
case KEY_ESC:
|
2014-02-26 06:02:30 -06:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
done:
|
|
|
|
menu_pop(cf);
|
2014-02-26 06:58:31 -06:00
|
|
|
if (!has_typestr) {
|
|
|
|
for (i = 0; i < nitems; i++)
|
|
|
|
free((char *) cm[i].name);
|
|
|
|
}
|
2014-02-26 06:02:30 -06:00
|
|
|
free(cm);
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: get parrtype done [type=%s] ", t ? t->name : NULL));
|
2014-02-26 06:02:30 -06:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2014-02-25 05:47:22 -06:00
|
|
|
/* prints menu with libfdisk labels and waits for users response */
|
|
|
|
static int ui_create_label(struct cfdisk *cf)
|
|
|
|
{
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d, *cm;
|
2014-02-25 05:47:22 -06:00
|
|
|
int rc = 1;
|
|
|
|
size_t i = 0, nitems;
|
|
|
|
struct fdisk_label *lb = NULL;
|
|
|
|
|
|
|
|
assert(cf);
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: asking for new disklabe."));
|
2014-02-25 05:47:22 -06:00
|
|
|
|
|
|
|
/* create cfdisk menu according to libfdisk labels, note that the
|
|
|
|
* last cm[] item has to be empty -- so nitems + 1 */
|
|
|
|
nitems = fdisk_context_get_nlabels(cf->cxt);
|
2014-03-07 06:01:22 -06:00
|
|
|
cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
|
2014-02-25 05:47:22 -06:00
|
|
|
|
2014-06-20 05:07:30 -05:00
|
|
|
while (fdisk_context_next_label(cf->cxt, &lb) == 0) {
|
|
|
|
if (fdisk_label_is_disabled(lb) || strcmp(lb->name, "bsd") == 0)
|
|
|
|
continue;
|
|
|
|
cm[i++].name = lb->name;
|
2014-02-25 05:47:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
erase();
|
|
|
|
ui_center(LINES - 4,
|
|
|
|
_("Device does not contain a recognized partition table."));
|
|
|
|
ui_center(LINES - 3,
|
|
|
|
_("Please, select a type to create a new disk label."));
|
|
|
|
|
|
|
|
/* make the new menu active */
|
2014-02-25 06:22:26 -06:00
|
|
|
menu_push(cf, cm);
|
2014-02-25 05:47:22 -06:00
|
|
|
cf->menu->vertical = 1;
|
|
|
|
menu_set_title(cf->menu, _("Select label type"));
|
|
|
|
ui_draw_menu(cf);
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
do {
|
|
|
|
int key = getch();
|
2014-06-05 07:32:51 -05:00
|
|
|
|
|
|
|
if (ui_resize)
|
|
|
|
ui_menu_resize(cf);
|
2014-02-25 05:47:22 -06:00
|
|
|
if (ui_menu_move(cf, key) == 0)
|
|
|
|
continue;
|
|
|
|
switch (key) {
|
|
|
|
case KEY_ENTER:
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2014-02-26 06:02:30 -06:00
|
|
|
d = menu_get_menuitem(cf, cf->menu->idx);
|
2014-02-25 05:47:22 -06:00
|
|
|
if (d)
|
|
|
|
rc = fdisk_create_disklabel(cf->cxt, d->name);
|
|
|
|
goto done;
|
2014-07-15 05:41:30 -05:00
|
|
|
case KEY_ESC:
|
2014-02-25 05:47:22 -06:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
done:
|
|
|
|
menu_pop(cf);
|
|
|
|
free(cm);
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: create label done [rc=%d] ", rc));
|
2014-02-25 05:47:22 -06:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-02-26 07:22:04 -06:00
|
|
|
static int ui_help(void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
static const char *help[] = {
|
|
|
|
N_("Help Screen for cfdisk"),
|
|
|
|
"",
|
|
|
|
N_("This is cfdisk, a curses based disk partitioning program, which"),
|
|
|
|
N_("allows you to create, delete and modify partitions on your hard"),
|
|
|
|
N_("disk drive."),
|
|
|
|
"",
|
|
|
|
N_("Copyright (C) 2014 Karel Zak <kzak@redhat.com> "),
|
|
|
|
N_("Based on the original cfdisk from Kevin E. Martin & aeb."),
|
|
|
|
"",
|
|
|
|
N_("Command Meaning"),
|
|
|
|
N_("------- -------"),
|
|
|
|
N_(" b Toggle bootable flag of the current partition"),
|
|
|
|
N_(" d Delete the current partition"),
|
|
|
|
N_(" h Print this screen"),
|
|
|
|
N_(" n Create new partition from free space"),
|
|
|
|
N_(" q Quit program without writing partition table"),
|
|
|
|
N_(" t Change the partition type"),
|
2014-07-15 06:12:23 -05:00
|
|
|
N_(" s Fix partitions order (only when in disarray)"),
|
2014-02-26 07:22:04 -06:00
|
|
|
N_(" W Write partition table to disk (must enter upper case W)"),
|
|
|
|
N_(" Since this might destroy data on the disk, you must"),
|
|
|
|
N_(" either confirm or deny the write by entering `yes' or"),
|
|
|
|
N_(" `no'"),
|
|
|
|
N_("Up Arrow Move cursor to the previous partition"),
|
|
|
|
N_("Down Arrow Move cursor to the next partition"),
|
|
|
|
N_("Left Arrow Move cursor to the previous menu item"),
|
|
|
|
N_("Right Arrow Move cursor to the next menu item"),
|
|
|
|
|
|
|
|
"",
|
|
|
|
N_("Note: All of the commands can be entered with either upper or lower"),
|
|
|
|
N_("case letters (except for Writes)."),
|
|
|
|
"",
|
|
|
|
N_("Use lsblk(8) or partx(8) to see more details about the device.")
|
|
|
|
};
|
|
|
|
|
|
|
|
erase();
|
|
|
|
for (i = 0; i < ARRAY_SIZE(help); i++)
|
|
|
|
mvaddstr(i, 1, _(help[i]));
|
|
|
|
|
|
|
|
ui_info(_("Press a key to continue."));
|
2014-06-05 07:32:51 -05:00
|
|
|
|
2014-02-26 07:22:04 -06:00
|
|
|
getch();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-25 06:22:26 -06:00
|
|
|
/* TODO: use @sz, now 128bytes */
|
|
|
|
static int main_menu_ignore_keys(struct cfdisk *cf, char *ignore,
|
|
|
|
size_t sz __attribute__((__unused__)))
|
|
|
|
{
|
|
|
|
struct fdisk_partition *pa = get_current_partition(cf);
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if (!pa)
|
|
|
|
return 0;
|
|
|
|
if (fdisk_partition_is_freespace(pa)) {
|
|
|
|
ignore[i++] = 'd'; /* delete */
|
|
|
|
ignore[i++] = 't'; /* set type */
|
|
|
|
ignore[i++] = 'b'; /* set bootable */
|
|
|
|
} else {
|
|
|
|
ignore[i++] = 'n';
|
|
|
|
if (!fdisk_is_disklabel(cf->cxt, DOS) &&
|
|
|
|
!fdisk_is_disklabel(cf->cxt, SGI))
|
|
|
|
ignore[i++] = 'b';
|
|
|
|
}
|
2014-03-21 08:04:59 -05:00
|
|
|
|
2014-06-05 05:13:41 -05:00
|
|
|
if (!cf->wrong_order)
|
|
|
|
ignore[i++] = 's';
|
2014-03-21 08:04:59 -05:00
|
|
|
if (fdisk_context_is_readonly(cf->cxt))
|
|
|
|
ignore[i++] = 'W';
|
2014-02-25 06:22:26 -06:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-25 06:05:09 -06:00
|
|
|
/* returns: error: < 0, success: 0, quit: 1 */
|
|
|
|
static int main_menu_action(struct cfdisk *cf, int key)
|
|
|
|
{
|
|
|
|
size_t n;
|
2014-06-05 05:13:41 -05:00
|
|
|
int ref = 0, rc, org_order = cf->wrong_order;
|
2014-02-25 06:05:09 -06:00
|
|
|
const char *info = NULL, *warn = NULL;
|
|
|
|
struct fdisk_partition *pa;
|
|
|
|
|
|
|
|
assert(cf);
|
|
|
|
assert(cf->cxt);
|
|
|
|
assert(cf->menu);
|
|
|
|
|
|
|
|
if (key == 0) {
|
2014-03-07 06:01:22 -06:00
|
|
|
struct cfdisk_menuitem *d = menu_get_menuitem(cf, cf->menu->idx);
|
2014-02-25 06:05:09 -06:00
|
|
|
if (!d)
|
|
|
|
return 0;
|
|
|
|
key = d->key;
|
|
|
|
|
|
|
|
} else if (key != 'w' && key != 'W')
|
|
|
|
key = tolower(key); /* case insensitive except 'W'rite */
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: main menu action: key=%c", key));
|
2014-02-25 06:05:09 -06:00
|
|
|
|
|
|
|
if (cf->menu->ignore && strchr(cf->menu->ignore, key)) {
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug(" ignore '%c'", key));
|
2014-02-25 06:05:09 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pa = get_current_partition(cf);
|
|
|
|
n = fdisk_partition_get_partno(pa);
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("menu action on %p", pa));
|
2014-02-26 07:49:13 -06:00
|
|
|
ui_clean_hint();
|
|
|
|
ui_clean_info();
|
2014-02-25 06:05:09 -06:00
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'b': /* Bootable flag */
|
|
|
|
{
|
|
|
|
int fl = fdisk_is_disklabel(cf->cxt, DOS) ? DOS_FLAG_ACTIVE :
|
|
|
|
fdisk_is_disklabel(cf->cxt, SGI) ? SGI_FLAG_BOOT : 0;
|
|
|
|
|
|
|
|
if (fl && fdisk_partition_toggle_flag(cf->cxt, n, fl))
|
|
|
|
warn = _("Could not toggle the flag.");
|
|
|
|
else if (fl)
|
|
|
|
ref = 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-10 08:44:50 -05:00
|
|
|
#ifdef KEY_DC
|
2014-02-27 06:05:19 -06:00
|
|
|
case KEY_DC:
|
2014-03-10 08:44:50 -05:00
|
|
|
#endif
|
2014-02-25 06:05:09 -06:00
|
|
|
case 'd': /* Delete */
|
|
|
|
if (fdisk_delete_partition(cf->cxt, n) != 0)
|
|
|
|
warn = _("Could not delete partition %zu.");
|
|
|
|
else
|
|
|
|
info = _("Partition %zu has been deleted.");
|
|
|
|
ref = 1;
|
|
|
|
break;
|
2014-02-26 07:22:04 -06:00
|
|
|
case 'h': /* help */
|
|
|
|
ui_help();
|
|
|
|
ref = 1;
|
|
|
|
break;
|
2014-02-25 06:05:09 -06:00
|
|
|
case 'n': /* New */
|
|
|
|
{
|
|
|
|
uint64_t start, size, dflt_size;
|
|
|
|
struct fdisk_partition *npa; /* the new partition */
|
|
|
|
|
|
|
|
if (!pa || !fdisk_partition_is_freespace(pa))
|
|
|
|
return -EINVAL;
|
|
|
|
npa = fdisk_new_partition();
|
|
|
|
if (!npa)
|
|
|
|
return -ENOMEM;
|
|
|
|
/* free space range */
|
|
|
|
start = fdisk_partition_get_start(pa);
|
|
|
|
size = dflt_size = fdisk_partition_get_size(pa) * cf->cxt->sector_size;
|
|
|
|
|
|
|
|
if (ui_get_size(cf, _("Partition size: "), &size, 1, size)
|
|
|
|
== -CFDISK_ERR_ESC)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (dflt_size == size) /* default is to fillin all free space */
|
|
|
|
fdisk_partition_end_follow_default(npa, 1);
|
|
|
|
else /* set relative size of the partition */
|
|
|
|
fdisk_partition_set_size(npa, size / cf->cxt->sector_size);
|
|
|
|
|
|
|
|
fdisk_partition_set_start(npa, start);
|
|
|
|
fdisk_partition_partno_follow_default(npa, 1);
|
|
|
|
/* add to disk label -- libfdisk will ask for missing details */
|
|
|
|
rc = fdisk_add_partition(cf->cxt, npa);
|
|
|
|
fdisk_unref_partition(npa);
|
|
|
|
if (rc == 0)
|
|
|
|
ref = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'q': /* Quit */
|
|
|
|
return 1;
|
|
|
|
case 't': /* Type */
|
2014-02-26 06:02:30 -06:00
|
|
|
{
|
|
|
|
struct fdisk_parttype *t;
|
|
|
|
|
|
|
|
if (!pa || fdisk_partition_is_freespace(pa))
|
|
|
|
return -EINVAL;
|
|
|
|
t = (struct fdisk_parttype *) fdisk_partition_get_type(pa);
|
2014-02-26 06:58:31 -06:00
|
|
|
t = ui_get_parttype(cf, t);
|
2014-02-26 06:02:30 -06:00
|
|
|
ref = 1;
|
|
|
|
|
|
|
|
if (t && fdisk_set_partition_type(cf->cxt, n, t) == 0)
|
2014-02-26 07:49:13 -06:00
|
|
|
info = _("Changed type of the partition %zu.");
|
2014-02-26 06:02:30 -06:00
|
|
|
else
|
2014-02-26 07:49:13 -06:00
|
|
|
info = _("Type of the partition %zu is unchanged.");
|
2014-02-25 06:05:09 -06:00
|
|
|
break;
|
2014-02-26 06:02:30 -06:00
|
|
|
}
|
2014-06-05 05:13:41 -05:00
|
|
|
case 's': /* fix order */
|
|
|
|
if (cf->wrong_order) {
|
|
|
|
fdisk_reorder_partitions(cf->cxt);
|
|
|
|
ref = 1;
|
|
|
|
}
|
|
|
|
break;
|
2014-02-25 06:05:09 -06:00
|
|
|
case 'W': /* Write */
|
2014-02-26 07:49:13 -06:00
|
|
|
{
|
|
|
|
char buf[64] = { 0 };
|
2014-03-21 08:04:59 -05:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (fdisk_context_is_readonly(cf->cxt)) {
|
2014-07-15 06:12:23 -05:00
|
|
|
warn = _("Device open in read-only mode");
|
2014-03-21 08:04:59 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ui_get_string(cf,
|
2014-02-26 07:49:13 -06:00
|
|
|
_("Are you sure you want to write the partition "
|
|
|
|
"table to disk? "),
|
|
|
|
_("Type \"yes\" or \"no\" or press ESC to left dialog."),
|
|
|
|
buf, sizeof(buf));
|
|
|
|
|
|
|
|
ref = 1;
|
|
|
|
if (rc <= 0 || strcasecmp(buf, "yes") != 0
|
|
|
|
|| strcasecmp(buf, _("yes")) != 0) {
|
|
|
|
info = _("Did not write partition table to disk");
|
|
|
|
break;
|
|
|
|
}
|
2014-02-25 06:05:09 -06:00
|
|
|
rc = fdisk_write_disklabel(cf->cxt);
|
|
|
|
if (rc)
|
2014-02-26 07:49:13 -06:00
|
|
|
warn = _("Failed to write disklabel");
|
|
|
|
else {
|
|
|
|
fdisk_reread_partition_table(cf->cxt);
|
|
|
|
info = _("The partition table has been altered.");
|
|
|
|
}
|
2014-07-15 05:28:16 -05:00
|
|
|
cf->nwrites++;
|
2014-02-26 07:49:13 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2014-02-25 06:05:09 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref) {
|
|
|
|
lines_refresh(cf);
|
|
|
|
ui_refresh(cf);
|
|
|
|
}
|
2014-02-26 07:49:13 -06:00
|
|
|
|
|
|
|
ui_clean_hint();
|
2014-02-25 06:05:09 -06:00
|
|
|
if (warn)
|
2014-02-26 07:49:13 -06:00
|
|
|
ui_warnx(warn, n + 1);
|
2014-02-25 06:05:09 -06:00
|
|
|
else if (info)
|
2014-02-26 07:49:13 -06:00
|
|
|
ui_info(info, n + 1);
|
2014-06-05 05:13:41 -05:00
|
|
|
else if (key == 'n' && cf->wrong_order && org_order == 0)
|
|
|
|
ui_info(_("Note that partition table entries are not in disk order now."));
|
2014-02-25 06:05:09 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
static void ui_resize_refresh(struct cfdisk *cf)
|
|
|
|
{
|
|
|
|
resize();
|
|
|
|
menu_refresh_size(cf);
|
|
|
|
lines_refresh(cf);
|
|
|
|
ui_refresh(cf);
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
static int ui_run(struct cfdisk *cf)
|
|
|
|
{
|
2014-02-25 05:47:22 -06:00
|
|
|
int rc = 0;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: start COLS=%d, LINES=%d", COLS, LINES));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-06-20 05:17:53 -05:00
|
|
|
if (!fdisk_dev_has_disklabel(cf->cxt) || cf->zero_start) {
|
2014-02-25 05:47:22 -06:00
|
|
|
rc = ui_create_label(cf);
|
|
|
|
if (rc < 0)
|
|
|
|
ui_errx(EXIT_FAILURE,
|
|
|
|
_("failed to create a new disklabel"));
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cols_init(cf);
|
|
|
|
rc = lines_refresh(cf);
|
|
|
|
if (rc)
|
|
|
|
ui_errx(EXIT_FAILURE, _("failed to read partitions"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-07 06:01:22 -06:00
|
|
|
menu_push(cf, main_menuitems);
|
2014-02-25 06:22:26 -06:00
|
|
|
cf->menu->ignore_cb = main_menu_ignore_keys;
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
rc = ui_refresh(cf);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 08:04:59 -05:00
|
|
|
if (fdisk_context_is_readonly(cf->cxt))
|
|
|
|
ui_warnx(_("Device open in read-only mode."));
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
do {
|
2014-01-22 04:10:54 -06:00
|
|
|
int rc = 0, key = getch();
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-06-05 07:32:51 -05:00
|
|
|
if (ui_resize)
|
|
|
|
/* Note that ncurses getch() returns ERR when interrupted
|
|
|
|
* by signal, but SLang does not interrupt at all. */
|
|
|
|
ui_resize_refresh(cf);
|
|
|
|
if (key == ERR)
|
|
|
|
continue;
|
2014-02-24 07:27:49 -06:00
|
|
|
if (ui_menu_move(cf, key) == 0)
|
|
|
|
continue;
|
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: main action key >%c<.", key));
|
2014-02-27 06:05:19 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
switch (key) {
|
|
|
|
case KEY_DOWN:
|
|
|
|
case '\016': /* ^N */
|
|
|
|
case 'j': /* Vi-like alternative */
|
|
|
|
ui_table_goto(cf, cf->lines_idx + 1);
|
|
|
|
break;
|
|
|
|
case KEY_UP:
|
|
|
|
case '\020': /* ^P */
|
|
|
|
case 'k': /* Vi-like alternative */
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_table_goto(cf, (int) cf->lines_idx - 1);
|
2014-01-09 07:49:45 -06:00
|
|
|
break;
|
2014-02-27 06:05:19 -06:00
|
|
|
case KEY_PPAGE:
|
|
|
|
if (cf->page_sz) {
|
|
|
|
ui_table_goto(cf, (int) cf->lines_idx - cf->page_sz);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
case KEY_HOME:
|
|
|
|
ui_table_goto(cf, 0);
|
|
|
|
break;
|
2014-02-27 06:05:19 -06:00
|
|
|
case KEY_NPAGE:
|
|
|
|
if (cf->page_sz) {
|
|
|
|
ui_table_goto(cf, cf->lines_idx + cf->page_sz);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-09 07:49:45 -06:00
|
|
|
case KEY_END:
|
2014-02-27 06:05:19 -06:00
|
|
|
ui_table_goto(cf, (int) cf->nlines - 1);
|
2014-01-09 07:49:45 -06:00
|
|
|
break;
|
|
|
|
case KEY_ENTER:
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2014-02-25 06:05:09 -06:00
|
|
|
rc = main_menu_action(cf, 0);
|
2014-01-09 07:49:45 -06:00
|
|
|
break;
|
|
|
|
default:
|
2014-02-25 06:05:09 -06:00
|
|
|
rc = main_menu_action(cf, key);
|
2014-01-22 04:10:54 -06:00
|
|
|
if (rc < 0)
|
2014-01-09 07:49:45 -06:00
|
|
|
beep();
|
|
|
|
break;
|
|
|
|
}
|
2014-01-22 04:10:54 -06:00
|
|
|
|
|
|
|
if (rc == 1)
|
|
|
|
break; /* quit */
|
2014-01-09 07:49:45 -06:00
|
|
|
} while (1);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
menu_pop(cf);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("ui: end"));
|
2014-01-09 07:49:45 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 06:28:40 -06:00
|
|
|
static void __attribute__ ((__noreturn__)) usage(FILE *out)
|
|
|
|
{
|
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
|
|
|
|
fprintf(out,
|
|
|
|
_(" %1$s [options] <disk>\n"), program_invocation_short_name);
|
|
|
|
|
|
|
|
fputs(USAGE_OPTIONS, out);
|
|
|
|
fputs(_(" -L --color[=<when>] colorize output (auto, always or never)\n"), out);
|
2014-06-20 05:17:53 -05:00
|
|
|
fputs(_(" -z --zero start with zeroed partition table\n"), out);
|
2014-02-27 06:28:40 -06:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
|
|
|
|
|
|
|
fprintf(out, USAGE_MAN_TAIL("cfdisk(8)"));
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-03-11 06:22:35 -05:00
|
|
|
int rc, c, colormode = UL_COLORMODE_UNDEF;
|
2014-01-09 07:49:45 -06:00
|
|
|
struct cfdisk _cf = { .lines_idx = 0 },
|
|
|
|
*cf = &_cf;
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 06:28:40 -06:00
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "color", optional_argument, NULL, 'L' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2014-06-20 05:17:53 -05:00
|
|
|
{ "zero", no_argument, NULL, 'z' },
|
2014-02-27 06:28:40 -06:00
|
|
|
{ NULL, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
atexit(close_stdout);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-06-20 05:17:53 -05:00
|
|
|
while((c = getopt_long(argc, argv, "L::hVz", longopts, NULL)) != -1) {
|
2014-02-27 06:28:40 -06:00
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
colormode = UL_COLORMODE_AUTO;
|
|
|
|
if (optarg)
|
|
|
|
colormode = colormode_or_err(optarg,
|
|
|
|
_("unsupported color mode"));
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf(_("%s from %s\n"), program_invocation_short_name,
|
|
|
|
PACKAGE_STRING);
|
|
|
|
return EXIT_SUCCESS;
|
2014-06-20 05:17:53 -05:00
|
|
|
case 'z':
|
|
|
|
cf->zero_start = 1;
|
|
|
|
break;
|
2014-02-27 06:28:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 08:44:50 -05:00
|
|
|
|
|
|
|
|
2014-03-11 05:49:52 -05:00
|
|
|
colors_init(colormode, "cfdisk");
|
2014-02-27 06:28:40 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
fdisk_init_debug(0);
|
2014-06-06 06:47:06 -05:00
|
|
|
scols_init_debug(0);
|
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
cf->cxt = fdisk_new_context();
|
|
|
|
if (!cf->cxt)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
fdisk_context_set_ask(cf->cxt, ask_callback, (void *) cf);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-02-27 06:28:40 -06:00
|
|
|
if (optind == argc)
|
|
|
|
usage(stderr);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-03-21 08:04:59 -05:00
|
|
|
rc = fdisk_context_assign_device(cf->cxt, argv[optind], 0);
|
|
|
|
if (rc == -EACCES)
|
|
|
|
rc = fdisk_context_assign_device(cf->cxt, argv[optind], 1);
|
|
|
|
if (rc != 0)
|
2014-01-09 07:49:45 -06:00
|
|
|
err(EXIT_FAILURE, _("cannot open %s"), argv[optind]);
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
/* Don't use err(), warn() from this point */
|
|
|
|
ui_init(cf);
|
|
|
|
ui_run(cf);
|
2014-02-25 05:47:22 -06:00
|
|
|
ui_end();
|
2006-12-06 17:25:32 -06:00
|
|
|
|
2014-01-09 07:49:45 -06:00
|
|
|
free(cf->lines);
|
|
|
|
free(cf->linesbuf);
|
|
|
|
fdisk_unref_table(cf->table);
|
2014-02-25 06:05:09 -06:00
|
|
|
|
2014-07-15 05:28:16 -05:00
|
|
|
rc = fdisk_context_deassign_device(cf->cxt, cf->nwrites == 0);
|
2014-01-09 07:49:45 -06:00
|
|
|
fdisk_free_context(cf->cxt);
|
2014-03-21 06:35:21 -05:00
|
|
|
DBG(FRONTEND, ul_debug("bye! [rc=%d]", rc));
|
2014-02-25 06:05:09 -06:00
|
|
|
return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2006-12-06 17:25:32 -06:00
|
|
|
}
|