forked from Surachai-kent/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfdisk.c
More file actions
2861 lines (2387 loc) · 64.3 KB
/
cfdisk.c
File metadata and controls
2861 lines (2387 loc) · 64.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cfdisk.c - Display or manipulate a disk partition table.
*
* Copyright (C) 2014-2023 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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <ctype.h>
#include <getopt.h>
#include <assert.h>
#include <libsmartcols.h>
#include <sys/ioctl.h>
#include <rpmatch.h>
#include <libfdisk.h>
#ifdef HAVE_LIBMOUNT
# include <libmount.h> /* keep it optional for non-linux systems */
#endif
#ifdef HAVE_SLANG_H
# include <slang.h>
#elif defined(HAVE_SLANG_SLANG_H)
# include <slang/slang.h>
#endif
#ifndef _XOPEN_SOURCE
# define _XOPEN_SOURCE 500 /* for inclusion of get_wch */
#endif
#ifdef HAVE_SLCURSES_H
# include <slcurses.h>
#elif defined(HAVE_SLANG_SLCURSES_H)
# include <slang/slcurses.h>
#elif defined(HAVE_NCURSESW_NCURSES_H) && defined(HAVE_WIDECHAR)
# include <ncursesw/ncurses.h>
#elif defined(HAVE_NCURSES_H)
# include <ncurses.h>
#elif defined(HAVE_NCURSES_NCURSES_H)
# include <ncurses/ncurses.h>
#endif
#ifdef HAVE_WIDECHAR
# include <wctype.h>
# include <wchar.h>
#endif
#include "c.h"
#include "closestream.h"
#include "nls.h"
#include "strutils.h"
#include "xalloc.h"
#include "mbsalign.h"
#include "mbsedit.h"
#include "colors.h"
#include "debug.h"
#include "list.h"
#include "blkdev.h"
static const char *default_disks[] = {
#ifdef __GNU__
"/dev/hd0",
"/dev/sd0",
#elif defined(__FreeBSD__)
"/dev/ad0",
"/dev/da0",
#else
"/dev/sda",
"/dev/vda",
"/dev/hda",
#endif
};
#define ARROW_CURSOR_STRING ">> "
#define ARROW_CURSOR_DUMMY " "
#define ARROW_CURSOR_WIDTH (sizeof(ARROW_CURSOR_STRING) - 1)
/* vertical menu */
#define MENU_V_SPADDING 1 /* space around menu item string */
/* horizontal menu */
#define MENU_H_SPADDING 0 /* space around menu item string */
#define MENU_H_BETWEEN 2 /* space between menu items */
#define MENU_H_PRESTR "["
#define MENU_H_POSTSTR "]"
#define MENU_TITLE_PADDING 3
#define MENU_H_PRESTR_SZ (sizeof(MENU_H_PRESTR) - 1)
#define MENU_H_POSTSTR_SZ (sizeof(MENU_H_POSTSTR) - 1)
#define TABLE_START_LINE 4
#define MENU_START_LINE (ui_lines - 4) /* The menu maybe use two lines */
#define INFO_LINE (ui_lines - 2)
#define WARN_LINE INFO_LINE
#define HINT_LINE (ui_lines - 1)
#define CFDISK_ERR_ESC 5000
#ifndef KEY_ESC
# define KEY_ESC '\033'
#endif
#ifndef KEY_DELETE
# define KEY_DELETE '\177'
#endif
#ifndef KEY_DC
# define KEY_DC 0423
#endif
/* colors */
enum {
CFDISK_CL_NONE = 0,
CFDISK_CL_WARNING,
CFDISK_CL_FREESPACE,
CFDISK_CL_INFO
};
#ifdef HAVE_USE_DEFAULT_COLORS
static const int color_pairs[][2] = {
/* color foreground, background */
[CFDISK_CL_WARNING] = { COLOR_RED, -1 },
[CFDISK_CL_FREESPACE] = { COLOR_GREEN, -1 },
[CFDISK_CL_INFO] = { COLOR_BLUE, -1 }
};
#endif
struct cfdisk;
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);
static struct cfdisk_menu *menu_pop(struct cfdisk *cf);
static void menu_refresh_size(struct cfdisk *cf);
static int ui_end(void);
static int ui_refresh(struct cfdisk *cf);
static void ui_warnx(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
static void ui_warn(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
static void ui_info(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
static void ui_draw_menu(struct cfdisk *cf);
static int ui_menu_move(struct cfdisk *cf, int key);
static void ui_menu_resize(struct cfdisk *cf);
static int ui_get_size(struct cfdisk *cf, const char *prompt, uint64_t *res,
uint64_t low, uint64_t up, int *expsize);
static int ui_enabled;
static volatile sig_atomic_t sig_resize;
static volatile sig_atomic_t sig_die;
/* ncurses LINES and COLS may be actual variables or *macros*, but we need
* something portable and writable */
static size_t ui_lines;
static size_t ui_cols;
/* menu item */
struct cfdisk_menuitem {
int key; /* keyboard shortcut */
const char *name; /* item name */
const char *desc; /* item description (hint) */
void *userdata;
};
/* menu */
struct cfdisk_menu {
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 */
int prefkey;/* preferred menu item */
struct cfdisk_menu *prev;
/* @ignore keys generator */
int (*ignore_cb) (struct cfdisk *, char *, size_t);
unsigned int vertical : 1; /* enable vertical mode */
};
/* main menu */
static struct cfdisk_menuitem main_menuitems[] = {
{ 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
{ 'd', N_("Delete"), N_("Delete the current partition") },
{ 'r', N_("Resize"), N_("Reduce or enlarge the current partition") },
{ 'n', N_("New"), N_("Create new partition from free space") },
{ 'q', N_("Quit"), N_("Quit program without writing changes") },
{ 't', N_("Type"), N_("Change the partition type") },
{ 'h', N_("Help"), N_("Print help screen") },
{ 's', N_("Sort"), N_("Fix partitions order") },
{ 'W', N_("Write"), N_("Write partition table to disk (this might destroy data)") },
{ 'u', N_("Dump"), N_("Dump partition table to sfdisk compatible script file") },
{ 0, NULL, NULL }
};
/* extra partinfo in name:value pairs */
struct cfdisk_extra {
char *name;
char *data;
struct list_head exs;
};
/* line and extra partinfo list_head */
struct cfdisk_line {
char *data; /* line data */
struct libscols_table *extra; /* extra info ('X') */
WINDOW *w; /* window with extra info */
};
/* top level control struct */
struct cfdisk {
struct fdisk_context *cxt; /* libfdisk context */
struct fdisk_table *table; /* partition table */
struct fdisk_table *original_layout; /* original on-disk PT */
struct cfdisk_menu *menu; /* the current menu */
int *fields; /* output columns IDs */
size_t nfields; /* number of columns IDs */
char *linesbuf; /* table as string */
size_t linesbufsz; /* size of the tb_buf */
struct cfdisk_line *lines; /* list of lines */
size_t nlines; /* number of lines */
size_t lines_idx; /* current line <0..N>, exclude header */
size_t page_sz;
unsigned int nwrites; /* fdisk_write_disklabel() counter */
WINDOW *act_win; /* the window currently on the screen */
#ifdef HAVE_LIBMOUNT
struct libmnt_table *mtab;
struct libmnt_table *fstab;
struct libmnt_cache *mntcache;
#endif
unsigned int wrong_order :1, /* PT not in right order */
zero_start :1, /* ignore existing partition table */
device_is_used : 1, /* don't use re-read ioctl */
show_extra :1; /* show extra partinfo */
};
/*
* let's use include/debug.h stuff for cfdisk too
*/
static UL_DEBUG_DEFINE_MASK(cfdisk);
UL_DEBUG_DEFINE_MASKNAMES(cfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
#define CFDISK_DEBUG_INIT (1 << 1)
#define CFDISK_DEBUG_UI (1 << 2)
#define CFDISK_DEBUG_MENU (1 << 3)
#define CFDISK_DEBUG_MISC (1 << 4)
#define CFDISK_DEBUG_TABLE (1 << 5)
#define CFDISK_DEBUG_ALL 0xFFFF
#define DBG(m, x) __UL_DBG(cfdisk, CFDISK_DEBUG_, m, x)
static void cfdisk_init_debug(void)
{
__UL_INIT_DEBUG_FROM_ENV(cfdisk, CFDISK_DEBUG_, 0, CFDISK_DEBUG);
}
/* Initialize output columns -- we follow libfdisk fields (usually specific
* to the label type.
*/
static int cols_init(struct cfdisk *cf)
{
assert(cf);
free(cf->fields);
cf->fields = NULL;
cf->nfields = 0;
return fdisk_label_get_fields_ids(NULL, cf->cxt, &cf->fields, &cf->nfields);
}
static void die_on_signal(void)
{
DBG(MISC, ul_debug("die on signal."));
ui_end();
exit(EXIT_FAILURE);
}
static void resize(void)
{
struct winsize ws;
if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) != -1
&& ws.ws_row && ws.ws_col) {
ui_lines = ws.ws_row;
ui_cols = ws.ws_col;
#if HAVE_RESIZETERM
resizeterm(ws.ws_row, ws.ws_col);
#endif
clearok(stdscr, TRUE);
}
touchwin(stdscr);
DBG(UI, ul_debug("ui: resize refresh ui_cols=%zu, ui_lines=%zu",
ui_cols, ui_lines));
sig_resize = 0;
}
/* 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);
scols_free_iter(itr);
}
return 0;
}
static char *table_to_string(struct cfdisk *cf, struct fdisk_table *tb)
{
struct fdisk_partition *pa;
struct fdisk_label *lb;
struct fdisk_iter *itr;
struct libscols_table *table = NULL;
struct libscols_iter *s_itr = NULL;
char *res = NULL;
size_t i;
int tree = 0;
struct libscols_line *ln, *ln_cont = NULL;
DBG(TABLE, ul_debug("convert to string"));
assert(cf);
assert(cf->cxt);
assert(cf->fields);
assert(tb);
lb = fdisk_get_label(cf->cxt, NULL);
assert(lb);
itr = fdisk_new_iter(FDISK_ITER_FORWARD);
if (!itr)
goto done;
/* get container (e.g. extended partition) */
while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
if (fdisk_partition_is_nested(pa)) {
DBG(TABLE, ul_debug("nested detected, using tree"));
tree = SCOLS_FL_TREE;
break;
}
}
table = scols_new_table();
if (!table)
goto done;
scols_table_enable_maxout(table, 1);
scols_table_enable_nowrap(table, 1);
#if !defined(HAVE_LIBNCURSESW) || !defined(HAVE_WIDECHAR)
scols_table_enable_ascii(table, 1);
#endif
/* headers */
for (i = 0; i < cf->nfields; i++) {
int fl = 0;
const struct fdisk_field *field =
fdisk_label_get_field(lb, cf->fields[i]);
if (!field)
continue;
if (fdisk_field_is_number(field))
fl |= SCOLS_FL_RIGHT;
if (fdisk_field_get_id(field) == FDISK_FIELD_TYPE)
fl |= SCOLS_FL_TRUNC;
if (tree && fdisk_field_get_id(field) == FDISK_FIELD_DEVICE)
fl |= SCOLS_FL_TREE;
if (!scols_table_new_column(table,
_(fdisk_field_get_name(field)),
fdisk_field_get_width(field), fl))
goto done;
}
/* data */
fdisk_reset_iter(itr, FDISK_ITER_FORWARD);
while (fdisk_table_next_partition(tb, itr, &pa) == 0) {
struct libscols_line *parent = fdisk_partition_is_nested(pa) ? ln_cont : NULL;
ln = scols_table_new_line(table, parent);
if (!ln)
goto done;
for (i = 0; i < cf->nfields; i++) {
char *cdata = NULL;
if (fdisk_partition_to_string(pa, cf->cxt,
cf->fields[i], &cdata))
continue;
scols_line_refer_data(ln, i, cdata);
}
if (tree && fdisk_partition_is_container(pa))
ln_cont = ln;
scols_line_set_userdata(ln, (void *) pa);
fdisk_ref_partition(pa);
}
if (scols_table_is_empty(table))
goto done;
scols_table_reduce_termwidth(table, ARROW_CURSOR_WIDTH);
scols_print_table_to_string(table, &res);
/* scols_* code might reorder lines, let's reorder @tb according to the
* final output (it's no problem because partitions are addressed by
* parno stored within struct fdisk_partition) */
/* remove all */
fdisk_reset_table(tb);
s_itr = scols_new_iter(SCOLS_ITER_FORWARD);
if (!s_itr)
goto done;
/* add all in the right order (don't forget the output is tree) */
while (scols_table_next_line(table, s_itr, &ln) == 0) {
if (scols_line_get_parent(ln))
continue;
if (partition_from_scols(tb, ln))
break;
}
done:
scols_unref_table(table);
scols_free_iter(s_itr);
fdisk_free_iter(itr);
return res;
}
static void cfdisk_free_lines(struct cfdisk *cf)
{
size_t i = 0;
while(i < cf->nlines) {
scols_unref_table(cf->lines[i].extra);
DBG(UI, ul_debug("delete window: %p",
cf->lines[i].w));
if (cf->lines[i].w)
delwin(cf->lines[i].w);
cf->lines[i].w = NULL;
++i;
}
cf->act_win = NULL;
free(cf->lines);
cf->lines = NULL;
}
/*
* Read data about partitions from libfdisk and prepare output lines.
*/
static int lines_refresh(struct cfdisk *cf)
{
int rc;
char *p;
size_t i;
assert(cf);
DBG(TABLE, ul_debug("refreshing buffer"));
free(cf->linesbuf);
cfdisk_free_lines(cf);
cf->linesbuf = NULL;
cf->linesbufsz = 0;
cf->lines = NULL;
cf->nlines = 0;
fdisk_unref_table(cf->table);
cf->table = NULL;
/* 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);
if (rc)
return rc;
cf->linesbuf = table_to_string(cf, cf->table);
if (!cf->linesbuf)
return -ENOMEM;
cf->linesbufsz = strlen(cf->linesbuf);
cf->nlines = fdisk_table_get_nents(cf->table) + 1; /* 1 for header line */
cf->page_sz = 0;
cf->wrong_order = fdisk_table_wrong_order(cf->table) ? 1 : 0;
if (MENU_START_LINE - TABLE_START_LINE < cf->nlines)
cf->page_sz = MENU_START_LINE - TABLE_START_LINE - 1;
cf->lines = xcalloc(cf->nlines, sizeof(struct cfdisk_line));
for (p = cf->linesbuf, i = 0; p && i < cf->nlines; i++) {
cf->lines[i].data = p;
p = strchr(p, '\n');
if (p) {
*p = '\0';
p++;
}
cf->lines[i].extra = scols_new_table();
scols_table_enable_noheadings(cf->lines[i].extra, 1);
scols_table_new_column(cf->lines[i].extra, NULL, 0, SCOLS_FL_RIGHT);
scols_table_new_column(cf->lines[i].extra, NULL, 0, SCOLS_FL_TRUNC);
}
return 0;
}
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);
}
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);
}
/* converts libfdisk FDISK_ASKTYPE_MENU to cfdisk menu and returns user's
* response back to libfdisk
*/
static int ask_menu(struct fdisk_ask *ask, struct cfdisk *cf)
{
struct cfdisk_menuitem *d, *cm;
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);
cm = xcalloc(nitems + 1, sizeof(struct cfdisk_menuitem));
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 */
menu_push(cf, cm);
ui_draw_menu(cf);
refresh();
/* wait for keys */
while (!sig_die) {
key = getch();
if (sig_die)
break;
if (sig_resize)
ui_menu_resize(cf);
if (ui_menu_move(cf, key) == 0)
continue;
switch (key) {
case KEY_ENTER:
case '\n':
case '\r':
d = menu_get_menuitem(cf, cf->menu->idx);
if (d)
fdisk_ask_menu_set_result(ask, d->key);
menu_pop(cf);
free(cm);
return 0;
}
}
if (sig_die)
die_on_signal();
menu_pop(cf);
free(cm);
return -1;
}
/* libfdisk callback
*/
static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
struct fdisk_ask *ask,
void *data __attribute__((__unused__)))
{
int rc = 0;
assert(ask);
switch(fdisk_ask_get_type(ask)) {
case FDISK_ASKTYPE_INFO:
ui_info("%s", fdisk_ask_print_get_mesg(ask));
break;
case FDISK_ASKTYPE_WARNX:
ui_warnx("%s", fdisk_ask_print_get_mesg(ask));
break;
case FDISK_ASKTYPE_WARN:
ui_warn("%s", fdisk_ask_print_get_mesg(ask));
break;
case FDISK_ASKTYPE_MENU:
ask_menu(ask, (struct cfdisk *) data);
break;
default:
ui_warnx(_("internal error: unsupported dialog type %d"),
fdisk_ask_get_type(ask));
return -EINVAL;
}
return rc;
}
static int ui_end(void)
{
if (!ui_enabled)
return -EINVAL;
#if defined(HAVE_SLCURSES_H) || defined(HAVE_SLANG_SLCURSES_H)
SLsmg_gotorc(ui_lines - 1, 0);
SLsmg_refresh();
#else
mvcur(0, ui_cols - 1, ui_lines-1, 0);
#endif
curs_set(1);
nl();
endwin();
printf("\n");
ui_enabled = 0;
return 0;
}
static void __attribute__((__format__ (__printf__, 3, 0)))
ui_vprint_center(size_t line, int attrs, const char *fmt, va_list ap)
{
size_t width;
char *buf = NULL;
move(line, 0);
clrtoeol();
xvasprintf(&buf, fmt, ap);
width = mbs_safe_width(buf);
if (width > (size_t) ui_cols) {
char *p = strrchr(buf + ui_cols, ' ');
if (!p)
p = buf + ui_cols;
*p = '\0';
if (line + 1 >= ui_lines)
line--;
attron(attrs);
mvaddstr(line, 0, buf);
mvaddstr(line + 1, 0, p+1);
attroff(attrs);
} else {
attron(attrs);
mvaddstr(line, (ui_cols - width) / 2, buf);
attroff(attrs);
}
free(buf);
}
static void __attribute__((__format__ (__printf__, 2, 3)))
ui_center(size_t line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
ui_vprint_center(line, 0, fmt, ap);
va_end(ap);
}
static void __attribute__((__format__ (__printf__, 1, 2)))
ui_warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (ui_enabled)
ui_vprint_center(WARN_LINE,
colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
fmt, ap);
else {
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}
va_end(ap);
}
static void __attribute__((__format__ (__printf__, 1, 2)))
ui_warn(const char *fmt, ...)
{
char *fmt_m;
va_list ap;
xasprintf(&fmt_m, "%s: %m", fmt);
va_start(ap, fmt);
if (ui_enabled)
ui_vprint_center(WARN_LINE,
colors_wanted() ? COLOR_PAIR(CFDISK_CL_WARNING) : 0,
fmt_m, ap);
else {
vfprintf(stderr, fmt_m, ap);
fputc('\n', stderr);
}
va_end(ap);
free(fmt_m);
}
static void ui_clean_warn(void)
{
move(WARN_LINE, 0);
clrtoeol();
}
static int __attribute__((__noreturn__))
__attribute__((__format__ (__printf__, 2, 3)))
ui_err(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);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(ap);
exit(rc);
}
static int __attribute__((__noreturn__))
__attribute__((__format__ (__printf__, 2, 3)))
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);
fputc('\n', stderr);
va_end(ap);
exit(rc);
}
static void __attribute__((__format__ (__printf__, 1, 2)))
ui_info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (ui_enabled)
ui_vprint_center(INFO_LINE,
colors_wanted() ? COLOR_PAIR(CFDISK_CL_INFO) : 0,
fmt, ap);
else {
vfprintf(stdout, fmt, ap);
fputc('\n', stdout);
}
va_end(ap);
}
static void ui_clean_info(void)
{
move(INFO_LINE, 0);
clrtoeol();
}
static void __attribute__((__format__ (__printf__, 1, 2)))
ui_hint(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (ui_enabled)
ui_vprint_center(HINT_LINE, A_BOLD, fmt, ap);
else {
vfprintf(stdout, fmt, ap);
fputc('\n', stdout);
}
va_end(ap);
}
static void ui_clean_hint(void)
{
move(HINT_LINE, 0);
clrtoeol();
}
static void sig_handler_die(int dummy __attribute__((__unused__)))
{
sig_die = 1;
}
static void sig_handler_resize(int dummy __attribute__((__unused__)))
{
sig_resize = 1;
}
static void menu_refresh_size(struct cfdisk *cf)
{
if (cf->menu && cf->menu->nitems)
cf->menu->page_sz = (cf->menu->nitems / (ui_lines - 4)) ? ui_lines - 4 : 0;
}
static void menu_update_ignore(struct cfdisk *cf)
{
char ignore[128] = { 0 };
int i = 0;
struct cfdisk_menu *m;
struct cfdisk_menuitem *d, *org = NULL;
size_t idx;
assert(cf);
assert(cf->menu);
assert(cf->menu->ignore_cb);
m = cf->menu;
DBG(MENU, ul_debug("update menu ignored keys"));
i = m->ignore_cb(cf, ignore, sizeof(ignore));
ignore[i] = '\0';
/* return if no change */
if ((!m->ignore && !*ignore)
|| (m->ignore && *ignore && strcmp(m->ignore, ignore) == 0)) {
return;
}
if (!m->prefkey)
org = menu_get_menuitem(cf, m->idx);
free(m->ignore);
m->ignore = xstrdup(ignore);
m->nitems = 0;
for (d = m->items; d->name; d++) {
if (m->ignore && strchr(m->ignore, d->key))
continue;
m->nitems++;
}
DBG(MENU, ul_debug("update menu preferred keys"));
/* 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))
m->idx = idx;
else if (m->prefkey && menu_get_menuitem_by_key(cf, m->prefkey, &idx))
m->idx = idx;
else
m->idx = 0;
menu_refresh_size(cf);
}
static struct cfdisk_menu *menu_push(
struct cfdisk *cf,
struct cfdisk_menuitem *items)
{
struct cfdisk_menu *m = xcalloc(1, sizeof(*m));
struct cfdisk_menuitem *d;
assert(cf);
DBG(MENU, ul_debug("new menu"));
m->prev = cf->menu;
m->items = items;
for (d = m->items; d->name; d++) {
const char *name = _(d->name);
size_t len = mbs_safe_width(name);
if (len > m->width)
m->width = len;
m->nitems++;
}
cf->menu = m;
menu_refresh_size(cf);
return m;
}
static struct cfdisk_menu *menu_pop(struct cfdisk *cf)
{
struct cfdisk_menu *m = NULL;
assert(cf);
DBG(MENU, ul_debug("pop menu"));
if (cf->menu) {
m = cf->menu->prev;
free(cf->menu->ignore);
free(cf->menu->title);
free(cf->menu);
}
cf->menu = m;
return cf->menu;
}
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 + MENU_TITLE_PADDING > m->width)
m->width = len + MENU_TITLE_PADDING;
str = xstrdup(title);
}
m->title = str;
}
static int ui_init(struct cfdisk *cf __attribute__((__unused__)))
{
struct sigaction sa;
DBG(UI, ul_debug("init"));
/* setup SIGCHLD handler */
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sig_handler_die;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = sig_handler_resize;
sigaction(SIGWINCH, &sa, NULL);
ui_enabled = 1;
initscr();
#ifdef HAVE_USE_DEFAULT_COLORS
if (colors_wanted() && has_colors()) {
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]);
}
#else
colors_off();
#endif
cbreak();
noecho();
nonl();
curs_set(0);
keypad(stdscr, TRUE);