xref: /freebsd/usr.sbin/bsdinstall/partedit/diskmenu.c (revision 7ef6e99752db3c0fc7953a039050881c068b43fe)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Alfonso Sabato Siciliano
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <bsddialog.h>
29 #include <libutil.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "diskmenu.h"
35 
36 int
diskmenu_show(const char * title,const char * text,struct partedit_item * items,int nitems,int * focusitem)37 diskmenu_show(const char *title, const char *text, struct partedit_item *items,
38     int nitems, int *focusitem)
39 {
40 	int i, output;
41 	char size[16], *mp;
42 	struct bsddialog_menuitem *bsditems;
43 	struct bsddialog_conf conf;
44 
45 	bsditems = malloc(nitems * sizeof(struct bsddialog_menuitem));
46 	if (bsditems == NULL)
47 		return BSDDIALOG_ERROR;
48 	for (i = 0; i < nitems; i++) {
49 		bsditems[i].prefix = "";
50 		bsditems[i].on = false;
51 		bsditems[i].depth = 2 * items[i].indentation;
52 		/* old menu sets max namelen to 10 */
53 		bsditems[i].name = items[i].name;
54 		humanize_number(size, 7, items[i].size, "B",
55 		    HN_AUTOSCALE, HN_DECIMAL);
56 		mp = items[i].mountpoint != NULL ? items[i].mountpoint : "";
57 		asprintf(__DECONST(char**, &bsditems[i].desc),
58 		    "  %-9s %-15s %s", size, items[i].type, mp);
59 		bsditems[i].bottomdesc = "";
60 	}
61 
62 	bsddialog_initconf(&conf);
63 	conf.title = title;
64 	conf.menu.align_left = true;
65 	conf.text.escape = true;
66 	conf.key.f1_message="[\\Z1\\ZbC\\Znreate]: a new partition.\n"
67 		"[\\Z1\\ZbD\\Znelete]: selected partition(s).\n"
68 		"[\\Z1\\ZbM\\Znodify]: partition type or mountpoint.\n"
69 		"[\\Z1\\ZbR\\Znevert]: changes to disk setup.\n"
70 		"[\\Z1\\ZbA\\Znuto]:   guided partitioning tool.\n"
71 		"[\\Z1\\ZbF\\Zninish]: will ask to apply changes.";
72 	conf.menu.shortcut_buttons = true;
73 	conf.button.ok_label       = "Create";
74 	conf.button.with_extra     = true;
75 	conf.button.extra_label    = "Delete";
76 	conf.button.cancel_label   = "Modify";
77 	conf.button.with_help      = true;
78 	conf.button.help_label     = "Revert";
79 	conf.button.right1_label = "Auto";
80 	conf.button.right2_label = "Finish";
81 	conf.button.default_label  = "Finish";
82 	output = bsddialog_menu(&conf, text, 20, 0, 10, nitems, bsditems,
83 	    focusitem);
84 
85 	for (i = 0; i < nitems; i++)
86 		free((char *)bsditems[i].desc);
87 	free(bsditems);
88 
89 	return (output);
90 }
91