1 /*
2 * Implementation of GPTData class derivative with curses-based text-mode
3 * interaction
4 * Copyright (C) 2011-2022 Roderick W. Smith
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22 #include <clocale>
23 #include <iostream>
24 #include <string>
25 #include <sstream>
26 #if defined (__APPLE__) || (__FreeBSD__)
27 #include <ncurses.h>
28 #else
29 #include <ncursesw/ncurses.h>
30 #endif
31 #include "gptcurses.h"
32 #include "support.h"
33
34 using namespace std;
35
36 // # of lines to reserve for general information and headers (RESERVED_TOP)
37 // and for options and messages (RESERVED_BOTTOM)
38 #define RESERVED_TOP 7
39 #define RESERVED_BOTTOM 5
40
41 int GPTDataCurses::numInstances = 0;
42
GPTDataCurses(void)43 GPTDataCurses::GPTDataCurses(void) {
44 if (numInstances > 0) {
45 refresh();
46 } else {
47 setlocale( LC_ALL , "" );
48 initscr();
49 cbreak();
50 noecho();
51 intrflush(stdscr, false);
52 keypad(stdscr, true);
53 nonl();
54 numInstances++;
55 } // if/else
56 firstSpace = NULL;
57 lastSpace = NULL;
58 currentSpace = NULL;
59 currentSpaceNum = -1;
60 whichOptions = ""; // current set of options
61 currentKey = 'b'; // currently selected option
62 displayType = USE_CURSES;
63 } // GPTDataCurses constructor
64
~GPTDataCurses(void)65 GPTDataCurses::~GPTDataCurses(void) {
66 numInstances--;
67 if ((numInstances == 0) && !isendwin())
68 endwin();
69 } // GPTDataCurses destructor
70
71 /************************************************
72 * *
73 * Functions relating to Spaces data structures *
74 * *
75 ************************************************/
76
EmptySpaces(void)77 void GPTDataCurses::EmptySpaces(void) {
78 Space *trash;
79
80 while (firstSpace != NULL) {
81 trash = firstSpace;
82 firstSpace = firstSpace->nextSpace;
83 delete trash;
84 } // if
85 numSpaces = 0;
86 lastSpace = NULL;
87 } // GPTDataCurses::EmptySpaces()
88
89 // Create Spaces from partitions. Does NOT creates Spaces to represent
90 // unpartitioned space on the disk.
91 // Returns the number of Spaces created.
MakeSpacesFromParts(void)92 int GPTDataCurses::MakeSpacesFromParts(void) {
93 uint32_t i;
94 Space *tempSpace;
95
96 EmptySpaces();
97 for (i = 0; i < numParts; i++) {
98 if (partitions[i].IsUsed()) {
99 tempSpace = new Space;
100 tempSpace->firstLBA = partitions[i].GetFirstLBA();
101 tempSpace->lastLBA = partitions[i].GetLastLBA();
102 tempSpace->origPart = &partitions[i];
103 tempSpace->partNum = (int) i;
104 LinkToEnd(tempSpace);
105 } // if
106 } // for
107 return numSpaces;
108 } // GPTDataCurses::MakeSpacesFromParts()
109
110 // Add a single empty Space to the current Spaces linked list and sort the result....
AddEmptySpace(uint64_t firstLBA,uint64_t lastLBA)111 void GPTDataCurses::AddEmptySpace(uint64_t firstLBA, uint64_t lastLBA) {
112 Space *tempSpace;
113
114 tempSpace = new Space;
115 tempSpace->firstLBA = firstLBA;
116 tempSpace->lastLBA = lastLBA;
117 tempSpace->origPart = &emptySpace;
118 tempSpace->partNum = -1;
119 LinkToEnd(tempSpace);
120 SortSpaces();
121 } // GPTDataCurses::AddEmptySpace();
122
123 // Add Spaces to represent the unallocated parts of the partition table.
124 // Returns the number of Spaces added.
AddEmptySpaces(void)125 int GPTDataCurses::AddEmptySpaces(void) {
126 int numAdded = 0;
127 Space *current;
128
129 SortSpaces();
130 if (firstSpace == NULL) {
131 AddEmptySpace(GetFirstUsableLBA(), GetLastUsableLBA());
132 numAdded++;
133 } else {
134 current = firstSpace;
135 while ((current != NULL) /* && (current->partNum != -1) */ ) {
136 if ((current == firstSpace) && (current->firstLBA > GetFirstUsableLBA())) {
137 AddEmptySpace(GetFirstUsableLBA(), current->firstLBA - 1);
138 numAdded++;
139 } // if
140 if ((current == lastSpace) && (current->lastLBA < GetLastUsableLBA())) {
141 AddEmptySpace(current->lastLBA + 1, GetLastUsableLBA());
142 numAdded++;
143 } // if
144 if ((current->prevSpace != NULL) && (current->prevSpace->lastLBA < (current->firstLBA - 1))) {
145 AddEmptySpace(current->prevSpace->lastLBA + 1, current->firstLBA - 1);
146 numAdded++;
147 } // if
148 current = current->nextSpace;
149 } // while
150 } // if/else
151 return numAdded;
152 } // GPTDataCurses::AddEmptySpaces()
153
154 // Remove the specified Space from the linked list and set its previous and
155 // next pointers to NULL.
UnlinkSpace(Space * theSpace)156 void GPTDataCurses::UnlinkSpace(Space *theSpace) {
157 if (theSpace != NULL) {
158 if (theSpace->prevSpace != NULL)
159 theSpace->prevSpace->nextSpace = theSpace->nextSpace;
160 if (theSpace->nextSpace != NULL)
161 theSpace->nextSpace->prevSpace = theSpace->prevSpace;
162 if (theSpace == firstSpace)
163 firstSpace = theSpace->nextSpace;
164 if (theSpace == lastSpace)
165 lastSpace = theSpace->prevSpace;
166 theSpace->nextSpace = NULL;
167 theSpace->prevSpace = NULL;
168 numSpaces--;
169 } // if
170 } // GPTDataCurses::UnlinkSpace
171
172 // Link theSpace to the end of the current linked list.
LinkToEnd(Space * theSpace)173 void GPTDataCurses::LinkToEnd(Space *theSpace) {
174 if (lastSpace == NULL) {
175 firstSpace = lastSpace = theSpace;
176 theSpace->nextSpace = NULL;
177 theSpace->prevSpace = NULL;
178 } else {
179 theSpace->prevSpace = lastSpace;
180 theSpace->nextSpace = NULL;
181 lastSpace->nextSpace = theSpace;
182 lastSpace = theSpace;
183 } // if/else
184 numSpaces++;
185 } // GPTDataCurses::LinkToEnd()
186
187 // Sort spaces into ascending order by on-disk position.
SortSpaces(void)188 void GPTDataCurses::SortSpaces(void) {
189 Space *oldFirst, *oldLast, *earliest = NULL, *current = NULL;
190
191 oldFirst = firstSpace;
192 oldLast = lastSpace;
193 firstSpace = lastSpace = NULL;
194 while (oldFirst != NULL) {
195 current = earliest = oldFirst;
196 while (current != NULL) {
197 if (current->firstLBA < earliest->firstLBA)
198 earliest = current;
199 current = current->nextSpace;
200 } // while
201 if (oldFirst == earliest)
202 oldFirst = earliest->nextSpace;
203 if (oldLast == earliest)
204 oldLast = earliest->prevSpace;
205 UnlinkSpace(earliest);
206 LinkToEnd(earliest);
207 } // while
208 } // GPTDataCurses::SortSpaces()
209
210 // Identify the spaces on the disk, a "space" being defined as a partition
211 // or an empty gap between, before, or after partitions. The spaces are
212 // presented to users in the main menu display.
IdentifySpaces(void)213 void GPTDataCurses::IdentifySpaces(void) {
214 MakeSpacesFromParts();
215 AddEmptySpaces();
216 } // GPTDataCurses::IdentifySpaces()
217
218 /**************************
219 * *
220 * Data display functions *
221 * *
222 **************************/
223
224 // Display a single Space on line # lineNum.
225 // Returns a pointer to the space being displayed
ShowSpace(int spaceNum,int lineNum)226 Space* GPTDataCurses::ShowSpace(int spaceNum, int lineNum) {
227 Space *space;
228 int i = 0;
229 #ifdef USE_UTF16
230 char temp[40];
231 #endif
232
233 space = firstSpace;
234 while ((space != NULL) && (i < spaceNum)) {
235 space = space->nextSpace;
236 i++;
237 } // while
238 if ((space != NULL) && (lineNum < (LINES - 5))) {
239 ClearLine(lineNum);
240 if (space->partNum == -1) { // space is empty
241 move(lineNum, 12);
242 printw("%s", BytesToIeee((space->lastLBA - space->firstLBA + 1), blockSize).c_str());
243 move(lineNum, 24);
244 printw("free space");
245 } else { // space holds a partition
246 move(lineNum, 3);
247 printw("%d", space->partNum + 1);
248 move(lineNum, 12);
249 printw("%s", BytesToIeee((space->lastLBA - space->firstLBA + 1), blockSize).c_str());
250 move(lineNum, 24);
251 printw("%s", space->origPart->GetTypeName().c_str());
252 move(lineNum, 50);
253 #ifdef USE_UTF16
254 space->origPart->GetDescription().extract(0, 39, temp, 39);
255 printw(temp);
256 #else
257 printw("%s", space->origPart->GetDescription().c_str());
258 #endif
259 } // if/else
260 } // if
261 return space;
262 } // GPTDataCurses::ShowSpace
263
264 // Display the partitions, being sure that the space #selected is displayed
265 // and highlighting that space.
266 // Returns the number of the space being shown (should be selected, but will
267 // be -1 if something weird happens)
DisplayParts(int selected)268 int GPTDataCurses::DisplayParts(int selected) {
269 int lineNum = 5, i = 0, retval = -1, numToShow, pageNum;
270 string theLine;
271
272 move(lineNum++, 0);
273 theLine = "Part. # Size Partition Type Partition Name";
274 printw("%s", theLine.c_str());
275 move(lineNum++, 0);
276 theLine = "----------------------------------------------------------------";
277 printw("%s", theLine.c_str());
278 numToShow = LINES - RESERVED_TOP - RESERVED_BOTTOM;
279 pageNum = selected / numToShow;
280 for (i = pageNum * numToShow; i <= (pageNum + 1) * numToShow - 1; i++) {
281 if (i < numSpaces) { // real space; show it
282 if (i == selected) {
283 currentSpaceNum = i;
284 if (displayType == USE_CURSES) {
285 attron(A_REVERSE);
286 currentSpace = ShowSpace(i, lineNum++);
287 attroff(A_REVERSE);
288 } else {
289 currentSpace = ShowSpace(i, lineNum);
290 move(lineNum++, 0);
291 printw(">");
292 }
293 DisplayOptions(i);
294 retval = selected;
295 } else {
296 ShowSpace(i, lineNum++);
297 }
298 } else { // blank in display
299 ClearLine(lineNum++);
300 } // if/else
301 } // for
302 refresh();
303 return retval;
304 } // GPTDataCurses::DisplayParts()
305
306 /**********************************************
307 * *
308 * Functions corresponding to main menu items *
309 * *
310 **********************************************/
311
312 // Delete the specified partition and re-detect partitions and spaces....
DeletePartition(int partNum)313 void GPTDataCurses::DeletePartition(int partNum) {
314 if (!GPTData::DeletePartition(partNum))
315 Report("Could not delete partition!");
316 IdentifySpaces();
317 if (currentSpaceNum >= numSpaces) {
318 currentSpaceNum = numSpaces - 1;
319 currentSpace = lastSpace;
320 } // if
321 } // GPTDataCurses::DeletePartition()
322
323 // Displays information on the specified partition
ShowInfo(int partNum)324 void GPTDataCurses::ShowInfo(int partNum) {
325 uint64_t size;
326 #ifdef USE_UTF16
327 char temp[NAME_SIZE + 1];
328 #endif
329
330 clear();
331 move(2, (COLS - 29) / 2);
332 printw("Information for partition #%d\n\n", partNum + 1);
333 printw("Partition GUID code: %s (%s)\n", partitions[partNum].GetType().AsString().c_str(),
334 partitions[partNum].GetTypeName().c_str());
335 printw("Partition unique GUID: %s\n", partitions[partNum].GetUniqueGUID().AsString().c_str());
336 printw("First sector: %lld (at %s)\n", partitions[partNum].GetFirstLBA(),
337 BytesToIeee(partitions[partNum].GetFirstLBA(), blockSize).c_str());
338 printw("Last sector: %lld (at %s)\n", partitions[partNum].GetLastLBA(),
339 BytesToIeee(partitions[partNum].GetLastLBA(), blockSize).c_str());
340 size = partitions[partNum].GetLastLBA() - partitions[partNum].GetFirstLBA() + 1;
341 printw("Partition size: %lld sectors (%s)\n", size, BytesToIeee(size, blockSize).c_str());
342 printw("Attribute flags: %016llx\n", partitions[partNum].GetAttributes().GetAttributes());
343 #ifdef USE_UTF16
344 partitions[partNum].GetDescription().extract(0, NAME_SIZE , temp, NAME_SIZE );
345 printw("Partition name: '%s'\n", temp);
346 #else
347 printw("Partition name: '%s'\n", partitions[partNum].GetDescription().c_str());
348 #endif
349 PromptToContinue();
350 } // GPTDataCurses::ShowInfo()
351
352 // Prompt for and change a partition's name....
ChangeName(int partNum)353 void GPTDataCurses::ChangeName(int partNum) {
354 char temp[NAME_SIZE + 1];
355
356 if (ValidPartNum(partNum)) {
357 move(LINES - 4, 0);
358 clrtobot();
359 move(LINES - 4, 0);
360 #ifdef USE_UTF16
361 partitions[partNum].GetDescription().extract(0, NAME_SIZE , temp, NAME_SIZE );
362 printw("Current partition name is '%s'\n", temp);
363 #else
364 printw("Current partition name is '%s'\n", partitions[partNum].GetDescription().c_str());
365 #endif
366 printw("Enter new partition name, or <Enter> to use the current name:\n");
367 echo();
368 getnstr(temp, NAME_SIZE );
369 partitions[partNum].SetName((string) temp);
370 noecho();
371 } // if
372 } // GPTDataCurses::ChangeName()
373
374 // Change the partition's type code....
ChangeType(int partNum)375 void GPTDataCurses::ChangeType(int partNum) {
376 char temp[80] = "L\0";
377 PartType tempType;
378
379 echo();
380 do {
381 move(LINES - 4, 0);
382 clrtobot();
383 move(LINES - 4, 0);
384 printw("Current type is %04x (%s)\n", partitions[partNum].GetType().GetHexType(), partitions[partNum].GetTypeName().c_str());
385 printw("Hex code or GUID (L to show codes, Enter = %04x): ", partitions[partNum].GetType().GetHexType());
386 getnstr(temp, 79);
387 if ((temp[0] == 'L') || (temp[0] == 'l')) {
388 ShowTypes();
389 } else {
390 if (temp[0] == '\0')
391 tempType = partitions[partNum].GetType().GetHexType();
392 tempType = temp;
393 partitions[partNum].SetType(tempType);
394 } // if
395 } while ((temp[0] == 'L') || (temp[0] == 'l') || (partitions[partNum].GetType() == (GUIDData) "0x0000"));
396 noecho();
397 } // GPTDataCurses::ChangeType
398
399 // Sets the partition alignment value
SetAlignment(void)400 void GPTDataCurses::SetAlignment(void) {
401 int alignment;
402 char conversion_specifier[] = "%d";
403
404 move(LINES - 4, 0);
405 clrtobot();
406 printw("Current partition alignment, in sectors, is %d.", GetAlignment());
407 do {
408 move(LINES - 3, 0);
409 printw("Type new alignment value, in sectors: ");
410 echo();
411 scanw(conversion_specifier, &alignment);
412 noecho();
413 } while ((alignment == 0) || (alignment > MAX_ALIGNMENT));
414 GPTData::SetAlignment(alignment);
415 } // GPTDataCurses::SetAlignment()
416
417 // Verify the data structures. Note that this function leaves curses mode and
418 // relies on the underlying GPTData::Verify() function to report on problems
Verify(void)419 void GPTDataCurses::Verify(void) {
420 char junk;
421
422 def_prog_mode();
423 endwin();
424 GPTData::Verify();
425 cout << "\nPress the <Enter> key to continue: ";
426 cin.get(junk);
427 reset_prog_mode();
428 refresh();
429 } // GPTDataCurses::Verify()
430
431 // Create a new partition in the space pointed to by currentSpace.
MakeNewPart(void)432 void GPTDataCurses::MakeNewPart(void) {
433 uint64_t size, newFirstLBA = 0, newLastLBA = 0, lastAligned;
434 int partNum;
435 char inLine[80];
436
437 move(LINES - 4, 0);
438 clrtobot();
439 lastAligned = currentSpace->lastLBA + 1;
440 Align(&lastAligned);
441 lastAligned--;
442 // Discard end-alignment attempt if it's giving us an invalid end point....
443 if (!IsFree(lastAligned))
444 lastAligned = currentSpace->lastLBA;
445 while ((newFirstLBA < currentSpace->firstLBA) || (newFirstLBA > currentSpace->lastLBA)) {
446 move(LINES - 4, 0);
447 clrtoeol();
448 newFirstLBA = currentSpace->firstLBA;
449 Align(&newFirstLBA);
450 printw("First sector (%lld-%lld, default = %lld): ", newFirstLBA, currentSpace->lastLBA, newFirstLBA);
451 echo();
452 getnstr(inLine, 79);
453 noecho();
454 newFirstLBA = IeeeToInt(inLine, blockSize, currentSpace->firstLBA, currentSpace->lastLBA, sectorAlignment, newFirstLBA);
455 Align(&newFirstLBA);
456 } // while
457 if (newFirstLBA > lastAligned)
458 size = currentSpace->lastLBA - newFirstLBA + 1;
459 else
460 size = lastAligned - newFirstLBA + 1;
461 while ((newLastLBA > currentSpace->lastLBA) || (newLastLBA < newFirstLBA)) {
462 move(LINES - 3, 0);
463 clrtoeol();
464 printw("Size in sectors or {KMGTP} (default = %lld): ", size);
465 echo();
466 getnstr(inLine, 79);
467 noecho();
468 newLastLBA = newFirstLBA + IeeeToInt(inLine, blockSize, 1, size, sectorAlignment, size) - 1;
469 } // while
470 partNum = FindFirstFreePart();
471 if (CreatePartition(partNum, newFirstLBA, newLastLBA)) { // created OK; set type code & name....
472 ChangeType(partNum);
473 ChangeName(partNum);
474 } else {
475 Report("Error creating partition!");
476 } // if/else
477 } // GPTDataCurses::MakeNewPart()
478
479 // Prompt user for permission to save data and, if it's given, do so!
SaveData(void)480 void GPTDataCurses::SaveData(void) {
481 string answer = "";
482 char inLine[80];
483
484 move(LINES - 4, 0);
485 clrtobot();
486 move (LINES - 2, 14);
487 printw("Warning!! This may destroy data on your disk!");
488 echo();
489 while ((answer != "yes") && (answer != "no")) {
490 move (LINES - 4, 2);
491 printw("Are you sure you want to write the partition table to disk? (yes or no): ");
492 getnstr(inLine, 79);
493 answer = inLine;
494 if ((answer != "yes") && (answer != "no")) {
495 move(LINES - 2, 0);
496 clrtoeol();
497 move(LINES - 2, 14);
498 printw("Please enter 'yes' or 'no'");
499 } // if
500 } // while()
501 noecho();
502 if (answer == "yes") {
503 if (SaveGPTData(1)) {
504 if (!myDisk.DiskSync())
505 Report("The kernel may be using the old partition table. Reboot to use the new\npartition table!");
506 } else {
507 Report("Problem saving data! Your partition table may be damaged!");
508 }
509 }
510 } // GPTDataCurses::SaveData()
511
512 // Back up the partition table, prompting user for a filename....
Backup(void)513 void GPTDataCurses::Backup(void) {
514 char inLine[80];
515
516 ClearBottom();
517 move(LINES - 3, 0);
518 printw("Enter backup filename to save: ");
519 echo();
520 getnstr(inLine, 79);
521 noecho();
522 SaveGPTBackup(inLine);
523 } // GPTDataCurses::Backup()
524
525 // Load a GPT backup from a file
LoadBackup(void)526 void GPTDataCurses::LoadBackup(void) {
527 char inLine[80];
528
529 ClearBottom();
530 move(LINES - 3, 0);
531 printw("Enter backup filename to load: ");
532 echo();
533 getnstr(inLine, 79);
534 noecho();
535 if (!LoadGPTBackup(inLine))
536 Report("Restoration failed!");
537 IdentifySpaces();
538 } // GPTDataCurses::LoadBackup()
539
540 // Display some basic help information
ShowHelp(void)541 void GPTDataCurses::ShowHelp(void) {
542 int i = 0;
543
544 clear();
545 move(0, (COLS - 22) / 2);
546 printw("Help screen for cgdisk");
547 move(2, 0);
548 printw("This is cgdisk, a curses-based disk partitioning program. You can use it\n");
549 printw("to create, delete, and modify partitions on your hard disk.\n\n");
550 attron(A_BOLD);
551 printw("Use cgdisk only on GUID Partition Table (GPT) disks!\n");
552 attroff(A_BOLD);
553 printw("Use cfdisk on Master Boot Record (MBR) disks.\n\n");
554 printw("Command Meaning\n");
555 printw("------- -------\n");
556 while (menuMain[i].key != 0) {
557 printw(" %c %s\n", menuMain[i].key, menuMain[i].desc.c_str());
558 i++;
559 } // while()
560 PromptToContinue();
561 } // GPTDataCurses::ShowHelp()
562
563 /************************************
564 * *
565 * User input and menuing functions *
566 * *
567 ************************************/
568
569 // Change the currently-selected space....
ChangeSpaceSelection(int delta)570 void GPTDataCurses::ChangeSpaceSelection(int delta) {
571 if (currentSpace != NULL) {
572 while ((delta > 0) && (currentSpace->nextSpace != NULL)) {
573 currentSpace = currentSpace->nextSpace;
574 delta--;
575 currentSpaceNum++;
576 } // while
577 while ((delta < 0) && (currentSpace->prevSpace != NULL)) {
578 currentSpace = currentSpace->prevSpace;
579 delta++;
580 currentSpaceNum--;
581 } // while
582 } // if
583 // Below will hopefully never be true; bad counting error (bug), so reset to
584 // the first Space as a failsafe....
585 if (DisplayParts(currentSpaceNum) != currentSpaceNum) {
586 currentSpaceNum = 0;
587 currentSpace = firstSpace;
588 DisplayParts(currentSpaceNum);
589 } // if
590 } // GPTDataCurses
591
592 // Move option selection left or right....
MoveSelection(int delta)593 void GPTDataCurses::MoveSelection(int delta) {
594 int newKeyNum;
595
596 // Begin with a sanity check to ensure a valid key is selected....
597 if (whichOptions.find(currentKey) == string::npos)
598 currentKey = 'n';
599 newKeyNum = whichOptions.find(currentKey);
600 newKeyNum += delta;
601 if (newKeyNum < 0)
602 newKeyNum = whichOptions.length() - 1;
603 newKeyNum %= whichOptions.length();
604 currentKey = whichOptions[newKeyNum];
605 DisplayOptions(currentKey);
606 } // GPTDataCurses::MoveSelection()
607
608 // Show user's options. Refers to currentSpace to determine which options to show.
609 // Highlights the option with the key selectedKey; or a default if that's invalid.
DisplayOptions(char selectedKey)610 void GPTDataCurses::DisplayOptions(char selectedKey) {
611 uint64_t i, j = 0, firstLine, numPerLine;
612 string optionName, optionDesc = "";
613
614 if (currentSpace != NULL) {
615 if (currentSpace->partNum == -1) { // empty space is selected
616 whichOptions = EMPTY_SPACE_OPTIONS;
617 if (whichOptions.find(selectedKey) == string::npos)
618 selectedKey = 'n';
619 } else { // a partition is selected
620 whichOptions = PARTITION_OPTIONS;
621 if (whichOptions.find(selectedKey) == string::npos)
622 selectedKey = 't';
623 } // if/else
624
625 firstLine = LINES - 4;
626 numPerLine = (COLS - 8) / 12;
627 ClearBottom();
628 move(firstLine, 0);
629 for (i = 0; i < whichOptions.length(); i++) {
630 optionName = "";
631 for (j = 0; menuMain[j].key; j++) {
632 if (menuMain[j].key == whichOptions[i]) {
633 optionName = menuMain[j].name;
634 if (whichOptions[i] == selectedKey)
635 optionDesc = menuMain[j].desc;
636 } // if
637 } // for
638 move(firstLine + i / numPerLine, (i % numPerLine) * 12 + 4);
639 if (whichOptions[i] == selectedKey) {
640 attron(A_REVERSE);
641 printw("[ %s ]", optionName.c_str());
642 attroff(A_REVERSE);
643 } else {
644 printw("[ %s ]", optionName.c_str());
645 } // if/else
646 } // for
647 move(LINES - 1, (COLS - optionDesc.length()) / 2);
648 printw("%s", optionDesc.c_str());
649 currentKey = selectedKey;
650 } // if
651 } // GPTDataCurses::DisplayOptions()
652
653 // Accept user input and process it. Returns when the program should terminate.
AcceptInput()654 void GPTDataCurses::AcceptInput() {
655 int inputKey, exitNow = 0;
656
657 do {
658 refresh();
659 inputKey = getch();
660 switch (inputKey) {
661 case KEY_UP:
662 ChangeSpaceSelection(-1);
663 break;
664 case KEY_DOWN:
665 ChangeSpaceSelection(+1);
666 break;
667 case 339: // page up key
668 ChangeSpaceSelection(RESERVED_TOP + RESERVED_BOTTOM - LINES);
669 break;
670 case 338: // page down key
671 ChangeSpaceSelection(LINES - RESERVED_TOP - RESERVED_BOTTOM);
672 break;
673 case KEY_LEFT:
674 MoveSelection(-1);
675 break;
676 case KEY_RIGHT:
677 MoveSelection(+1);
678 break;
679 case KEY_ENTER: case 13:
680 exitNow = Dispatch(currentKey);
681 break;
682 case 27: // escape key
683 exitNow = 1;
684 break;
685 default:
686 exitNow = Dispatch(inputKey);
687 break;
688 } // switch()
689 } while (!exitNow);
690 } // GPTDataCurses::AcceptInput()
691
692 // Operation has been selected, so do it. Returns 1 if the program should
693 // terminate on return from this program, 0 otherwise.
Dispatch(char operation)694 int GPTDataCurses::Dispatch(char operation) {
695 int exitNow = 0;
696
697 switch (operation) {
698 case 'a': case 'A':
699 SetAlignment();
700 break;
701 case 'b': case 'B':
702 Backup();
703 break;
704 case 'd': case 'D':
705 if (ValidPartNum(currentSpace->partNum))
706 DeletePartition(currentSpace->partNum);
707 break;
708 case 'h': case 'H':
709 ShowHelp();
710 break;
711 case 'i': case 'I':
712 if (ValidPartNum(currentSpace->partNum))
713 ShowInfo(currentSpace->partNum);
714 break;
715 case 'l': case 'L':
716 LoadBackup();
717 break;
718 case 'm': case 'M':
719 if (ValidPartNum(currentSpace->partNum))
720 ChangeName(currentSpace->partNum);
721 break;
722 case 'n': case 'N':
723 if (currentSpace->partNum < 0) {
724 MakeNewPart();
725 IdentifySpaces();
726 } // if
727 break;
728 case 'q': case 'Q':
729 exitNow = 1;
730 break;
731 case 't': case 'T':
732 if (ValidPartNum(currentSpace->partNum))
733 ChangeType(currentSpace->partNum);
734 break;
735 case 'v': case 'V':
736 Verify();
737 break;
738 case 'w': case 'W':
739 SaveData();
740 break;
741 default:
742 break;
743 } // switch()
744 DrawMenu();
745 return exitNow;
746 } // GPTDataCurses::Dispatch()
747
748 // Draws the main menu
DrawMenu(void)749 void GPTDataCurses::DrawMenu(void) {
750 string title="cgdisk ";
751 title += GPTFDISK_VERSION;
752 string drive="Disk Drive: ";
753 drive += device;
754 ostringstream size;
755
756 size << "Size: " << diskSize << ", " << BytesToIeee(diskSize, blockSize);
757
758 clear();
759 move(0, (COLS - title.length()) / 2);
760 printw("%s", title.c_str());
761 move(2, (COLS - drive.length()) / 2);
762 printw("%s", drive.c_str());
763 move(3, (COLS - size.str().length()) / 2);
764 printw("%s", size.str().c_str());
765 DisplayParts(currentSpaceNum);
766 } // DrawMenu
767
MainMenu(void)768 int GPTDataCurses::MainMenu(void) {
769 if (((LINES - RESERVED_TOP - RESERVED_BOTTOM) < 2) || (COLS < 80)) {
770 Report("Display is too small; it must be at least 80 x 14 characters!");
771 } else {
772 if (GPTData::Verify() > 0)
773 Report("Warning! Problems found on disk! Use the Verify function to learn more.\n"
774 "Using gdisk or some other program may be necessary to repair the problems.");
775 IdentifySpaces();
776 currentSpaceNum = 0;
777 DrawMenu();
778 AcceptInput();
779 } // if/else
780 endwin();
781 return 0;
782 } // GPTDataCurses::MainMenu
783
784 /***********************************************************
785 * *
786 * Non-class support functions (mostly related to ncurses) *
787 * *
788 ***********************************************************/
789
790 // Clears the specified line of all data....
ClearLine(int lineNum)791 void ClearLine(int lineNum) {
792 move(lineNum, 0);
793 clrtoeol();
794 } // ClearLine()
795
796 // Clear the last few lines of the display
ClearBottom(void)797 void ClearBottom(void) {
798 move(LINES - RESERVED_BOTTOM, 0);
799 clrtobot();
800 } // ClearBottom()
801
PromptToContinue(void)802 void PromptToContinue(void) {
803 ClearBottom();
804 move(LINES - 2, (COLS - 29) / 2);
805 printw("Press any key to continue....");
806 cbreak();
807 getch();
808 } // PromptToContinue()
809
810 // Display one line of text on the screen and prompt to press any key to continue.
Report(string theText)811 void Report(string theText) {
812 clear();
813 move(0, 0);
814 printw("%s", theText.c_str());
815 move(LINES - 2, (COLS - 29) / 2);
816 printw("Press any key to continue....");
817 cbreak();
818 getch();
819 } // Report()
820
821 // Displays all the partition type codes and then prompts to continue....
822 // NOTE: This function temporarily exits curses mode as a matter of
823 // convenience.
ShowTypes(void)824 void ShowTypes(void) {
825 PartType tempType;
826 char junk;
827
828 def_prog_mode();
829 endwin();
830 tempType.ShowAllTypes(LINES - 3);
831 cout << "\nPress the <Enter> key to continue: ";
832 cin.get(junk);
833 reset_prog_mode();
834 refresh();
835 } // ShowTypes()
836