]>
Commit | Line | Data |
---|---|---|
161b0d1a KZ |
1 | |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include <string.h> | |
5 | #include <ctype.h> | |
6 | #include <stdint.h> | |
7 | ||
8 | #include "c.h" | |
9 | #include "fdisk.h" | |
10 | ||
11 | struct menu_entry { | |
12 | const char key; | |
13 | const char *title; | |
14 | unsigned int normal : 1, | |
15 | expert : 1, | |
16 | hidden : 1; | |
17 | ||
18 | enum fdisk_labeltype exclude; | |
19 | }; | |
20 | ||
21 | #define IS_MENU_SEP(e) ((e)->key == '-') | |
22 | #define IS_MENU_HID(e) ((e)->hidden) | |
23 | ||
24 | struct menu { | |
25 | enum fdisk_labeltype label; /* only for this label */ | |
26 | enum fdisk_labeltype exclude; /* all labels except this */ | |
27 | ||
28 | int (*callback)(struct fdisk_context *, struct menu *, int); | |
29 | ||
30 | struct menu_entry entries[]; /* NULL terminated array */ | |
31 | }; | |
32 | ||
33 | struct menu_context { | |
34 | size_t menu_idx; /* the current menu */ | |
35 | size_t entry_idx; /* index with in the current menu */ | |
36 | }; | |
37 | ||
38 | #define MENU_CXT_EMPTY { 0, 0 } | |
39 | ||
40 | /* | |
41 | * Menu entry macros: | |
42 | * MENU_X* expert mode only | |
43 | * MENU_B* both -- expert + normal mode | |
44 | * | |
45 | * *_E exclude | |
46 | * *_H hidden | |
47 | */ | |
48 | ||
49 | /* separator */ | |
50 | #define MENU_SEP(t) { .title = t, .key = '-', .normal = 1 } | |
51 | #define MENU_XSEP(t) { .title = t, .key = '-', .expert = 1 } | |
52 | #define MENU_BSEP(t) { .title = t, .key = '-', .expert = 1, .normal = 1 } | |
53 | ||
54 | /* entry */ | |
55 | #define MENU_ENT(k, t) { .title = t, .key = k, .normal = 1 } | |
56 | #define MENU_ENT_E(k, t, l) { .title = t, .key = k, .normal = 1, .exclude = l } | |
57 | ||
58 | #define MENU_XENT(k, t) { .title = t, .key = k, .expert = 1 } | |
59 | #define MENU_XENT_H(k, t) { .title = t, .key = k, .expert = 1, .hidden = 1 } | |
60 | ||
61 | #define MENU_BENT(k, t) { .title = t, .key = k, .expert = 1, .normal = 1 } | |
62 | ||
63 | ||
64 | /* Generic menu */ | |
65 | struct menu menu_generic = { | |
66 | /* .callback = generic_menu_cb,*/ | |
67 | .entries = { | |
68 | MENU_XENT('d', N_("print the raw data of the first sector")), | |
69 | ||
70 | MENU_SEP(N_("Alter partition table")), | |
71 | MENU_ENT ('d', N_("delete a partition")), | |
72 | MENU_ENT ('l', N_("list known partition types")), | |
73 | MENU_ENT ('n', N_("add a new partition")), | |
74 | MENU_BENT ('p', N_("print the partition table")), | |
75 | MENU_ENT ('t', N_("change a partition's system id")), | |
76 | MENU_ENT ('v', N_("verify the partition table")), | |
77 | ||
78 | MENU_SEP(N_("Misc")), | |
79 | MENU_BENT ('m', N_("print this menu")), | |
80 | MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT), | |
81 | MENU_ENT ('x', N_("extra functionality (experts only)")), | |
82 | ||
2a1a67df | 83 | MENU_BSEP(N_("Save & Exit")), |
161b0d1a KZ |
84 | MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_OSF), |
85 | MENU_BENT ('q', N_("quit without saving changes")), | |
86 | MENU_XENT ('r', N_("return to main menu")), | |
87 | ||
88 | { 0, NULL } | |
89 | } | |
90 | }; | |
91 | ||
92 | struct menu menu_createlabel = { | |
93 | /* .callback = createlabel_menu_cb, */ | |
94 | .exclude = FDISK_DISKLABEL_OSF, | |
95 | .entries = { | |
96 | MENU_SEP(N_("Create a new label")), | |
97 | MENU_ENT('g', N_("create a new empty GPT partition table")), | |
98 | MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")), | |
99 | MENU_ENT('o', N_("create a new empty DOS partition table")), | |
100 | MENU_ENT('s', N_("create a new empty Sun partition table")), | |
101 | ||
102 | /* backward compatibility -- be sensitive to 'g', but don't | |
103 | * print it in the expert menu */ | |
104 | MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")), | |
105 | { 0, NULL } | |
106 | } | |
107 | }; | |
108 | ||
109 | struct menu menu_gpt = { | |
110 | /* .callback = gpt_menu_cb, */ | |
111 | .label = FDISK_DISKLABEL_GPT, | |
112 | .entries = { | |
113 | MENU_XSEP(N_("GPT")), | |
114 | MENU_XENT('u', N_("change partition UUID")), | |
115 | MENU_XENT('n', N_("change partition name")), | |
116 | { 0, NULL } | |
117 | } | |
118 | }; | |
119 | ||
2a1a67df KZ |
120 | struct menu menu_sun = { |
121 | /* .callback = sun_menu_cb, */ | |
122 | .label = FDISK_DISKLABEL_SUN, | |
123 | .entries = { | |
124 | MENU_BSEP(N_("Sun")), | |
125 | MENU_ENT('a', N_("toggle a read only flag")), | |
126 | MENU_ENT('c', N_("toggle the mountable flag")), | |
127 | ||
128 | MENU_XENT('a', N_("change number of alternate cylinders")), | |
129 | MENU_XENT('c', N_("change number of cylinders")), | |
130 | MENU_XENT('e', N_("change number of extra sectors per cylinder")), | |
131 | MENU_XENT('h', N_("change number of heads")), | |
132 | MENU_XENT('i', N_("change interleave factor")), | |
133 | MENU_XENT('o', N_("change rotation speed (rpm)")), | |
134 | MENU_XENT('s', N_("change number of sectors/track")), | |
135 | MENU_XENT('y', N_("change number of physical cylinders")), | |
136 | { 0, NULL } | |
137 | } | |
138 | }; | |
139 | ||
161b0d1a KZ |
140 | static const struct menu *menus[] = { |
141 | &menu_generic, | |
142 | &menu_createlabel, | |
2a1a67df KZ |
143 | &menu_gpt, |
144 | &menu_sun | |
161b0d1a KZ |
145 | }; |
146 | ||
147 | static const struct menu_entry *next_menu_entry( | |
148 | struct fdisk_context *cxt, | |
149 | struct menu_context *mc) | |
150 | { | |
151 | while (mc->menu_idx < ARRAY_SIZE(menus)) { | |
152 | const struct menu *m = menus[mc->menu_idx]; | |
153 | const struct menu_entry *e = &(m->entries[mc->entry_idx]); | |
154 | ||
155 | /* move to the next submenu if there is no more entries */ | |
156 | if (e->title == NULL || | |
157 | (m->label && cxt->label && !(m->label & cxt->label->id))) { | |
158 | mc->menu_idx++; | |
159 | mc->entry_idx = 0; | |
160 | continue; | |
161 | } | |
162 | ||
163 | /* is the entry excluded for the current label? */ | |
164 | if ((e->exclude && cxt->label && | |
165 | e->exclude & cxt->label->id) || | |
166 | /* exclude non-expert entries in expect mode */ | |
167 | (e->expert == 0 && fdisk_context_display_details(cxt)) || | |
168 | /* exclude non-normal entries in normal mode */ | |
169 | (e->normal == 0 && !fdisk_context_display_details(cxt))) { | |
170 | ||
171 | mc->entry_idx++; | |
172 | continue; | |
173 | } | |
174 | mc->entry_idx++; | |
175 | return e; | |
176 | ||
177 | } | |
178 | return NULL; | |
179 | } | |
180 | ||
181 | static int print_fdisk_menu(struct fdisk_context *cxt) | |
182 | { | |
183 | struct menu_context mc = MENU_CXT_EMPTY; | |
184 | const struct menu_entry *e; | |
185 | ||
186 | if (fdisk_context_display_details(cxt)) | |
187 | printf(_("\nExpert commands:\n")); | |
188 | else | |
189 | printf(_("\nCommands:\n")); | |
190 | ||
191 | while ((e = next_menu_entry(cxt, &mc))) { | |
192 | if (IS_MENU_HID(e)) | |
193 | continue; /* hidden entry */ | |
194 | if (IS_MENU_SEP(e)) | |
195 | printf("\n %s\n", _(e->title)); | |
196 | else | |
197 | printf(" %c %s\n", e->key, _(e->title)); | |
198 | } | |
199 | fputc('\n', stdout); | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | #ifdef TEST_PROGRAM | |
205 | struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt) { return NULL; } | |
206 | struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt) { return NULL; } | |
207 | struct fdisk_label *fdisk_new_mac_label(struct fdisk_context *cxt) { return NULL; } | |
208 | struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt) { return NULL; } | |
209 | ||
210 | int main(int argc, char *argv[]) | |
211 | { | |
212 | struct fdisk_context *cxt; | |
213 | int idx = 1; | |
214 | ||
215 | fdisk_init_debug(0); | |
216 | cxt = fdisk_new_context(); | |
217 | ||
218 | if (argc > idx && strcmp(argv[idx], "--expert") == 0) { | |
219 | fdisk_context_enable_details(cxt, 1); | |
220 | idx++; | |
221 | } | |
222 | fdisk_context_switch_label(cxt, argc > idx ? argv[idx] : "gpt"); | |
223 | ||
224 | print_fdisk_menu(cxt); | |
225 | return 0; | |
226 | } | |
227 | #endif |