1 /*
2 * Implementation of GPTData class derivative with curses-based text-mode
3 * interaction
4 * Copyright (C) 2011-2018 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 #ifdef __APPLE__
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 uint 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(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(BytesToIeee((space->lastLBA - space->firstLBA + 1), blockSize).c_str());
250 move(lineNum, 24);
251 printw(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(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(theLine.c_str());
275 move(lineNum++, 0);
276 theLine = "----------------------------------------------------------------";
277 printw(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: %016x\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;
434 int partNum;
435 char inLine[80];
436
437 move(LINES - 4, 0);
438 clrtobot();
439 while ((newFirstLBA < currentSpace->firstLBA) || (newFirstLBA > currentSpace->lastLBA)) {
440 newFirstLBA = currentSpace->firstLBA;
441 move(LINES - 4, 0);
442 clrtoeol();
443 newFirstLBA = currentSpace->firstLBA;
444 Align(&newFirstLBA);
445 printw("First sector (%lld-%lld, default = %lld): ", newFirstLBA, currentSpace->lastLBA, newFirstLBA);
446 echo();
447 getnstr(inLine, 79);
448 noecho();
449 newFirstLBA = IeeeToInt(inLine, blockSize, currentSpace->firstLBA, currentSpace->lastLBA, newFirstLBA);
450 Align(&newFirstLBA);
451 } // while
452 size = currentSpace->lastLBA - newFirstLBA + 1;
453 while ((newLastLBA > currentSpace->lastLBA) || (newLastLBA < newFirstLBA)) {
454 move(LINES - 3, 0);
455 clrtoeol();
456 printw("Size in sectors or {KMGTP} (default = %lld): ", size);
457 echo();
458 getnstr(inLine, 79);
459 noecho();
460 newLastLBA = newFirstLBA + IeeeToInt(inLine, blockSize, 1, size, size) - 1;
461 } // while
462 partNum = FindFirstFreePart();
463 if (CreatePartition(partNum, newFirstLBA, newLastLBA)) { // created OK; set type code & name....
464 ChangeType(partNum);
465 ChangeName(partNum);
466 } else {
467 Report("Error creating partition!");
468 } // if/else
469 } // GPTDataCurses::MakeNewPart()
470
471 // Prompt user for permission to save data and, if it's given, do so!
SaveData(void)472 void GPTDataCurses::SaveData(void) {
473 string answer = "";
474 char inLine[80];
475
476 move(LINES - 4, 0);
477 clrtobot();
478 move (LINES - 2, 14);
479 printw("Warning!! This may destroy data on your disk!");
480 echo();
481 while ((answer != "yes") && (answer != "no")) {
482 move (LINES - 4, 2);
483 printw("Are you sure you want to write the partition table to disk? (yes or no): ");
484 getnstr(inLine, 79);
485 answer = inLine;
486 if ((answer != "yes") && (answer != "no")) {
487 move(LINES - 2, 0);
488 clrtoeol();
489 move(LINES - 2, 14);
490 printw("Please enter 'yes' or 'no'");
491 } // if
492 } // while()
493 noecho();
494 if (answer == "yes") {
495 if (SaveGPTData(1)) {
496 if (!myDisk.DiskSync())
497 Report("The kernel may be using the old partition table. Reboot to use the new\npartition table!");
498 } else {
499 Report("Problem saving data! Your partition table may be damaged!");
500 }
501 }
502 } // GPTDataCurses::SaveData()
503
504 // Back up the partition table, prompting user for a filename....
Backup(void)505 void GPTDataCurses::Backup(void) {
506 char inLine[80];
507
508 ClearBottom();
509 move(LINES - 3, 0);
510 printw("Enter backup filename to save: ");
511 echo();
512 getnstr(inLine, 79);
513 noecho();
514 SaveGPTBackup(inLine);
515 } // GPTDataCurses::Backup()
516
517 // Load a GPT backup from a file
LoadBackup(void)518 void GPTDataCurses::LoadBackup(void) {
519 char inLine[80];
520
521 ClearBottom();
522 move(LINES - 3, 0);
523 printw("Enter backup filename to load: ");
524 echo();
525 getnstr(inLine, 79);
526 noecho();
527 if (!LoadGPTBackup(inLine))
528 Report("Restoration failed!");
529 IdentifySpaces();
530 } // GPTDataCurses::LoadBackup()
531
532 // Display some basic help information
ShowHelp(void)533 void GPTDataCurses::ShowHelp(void) {
534 int i = 0;
535
536 clear();
537 move(0, (COLS - 22) / 2);
538 printw("Help screen for cgdisk");
539 move(2, 0);
540 printw("This is cgdisk, a curses-based disk partitioning program. You can use it\n");
541 printw("to create, delete, and modify partitions on your hard disk.\n\n");
542 attron(A_BOLD);
543 printw("Use cgdisk only on GUID Partition Table (GPT) disks!\n");
544 attroff(A_BOLD);
545 printw("Use cfdisk on Master Boot Record (MBR) disks.\n\n");
546 printw("Command Meaning\n");
547 printw("------- -------\n");
548 while (menuMain[i].key != 0) {
549 printw(" %c %s\n", menuMain[i].key, menuMain[i].desc.c_str());
550 i++;
551 } // while()
552 PromptToContinue();
553 } // GPTDataCurses::ShowHelp()
554
555 /************************************
556 * *
557 * User input and menuing functions *
558 * *
559 ************************************/
560
561 // Change the currently-selected space....
ChangeSpaceSelection(int delta)562 void GPTDataCurses::ChangeSpaceSelection(int delta) {
563 if (currentSpace != NULL) {
564 while ((delta > 0) && (currentSpace->nextSpace != NULL)) {
565 currentSpace = currentSpace->nextSpace;
566 delta--;
567 currentSpaceNum++;
568 } // while
569 while ((delta < 0) && (currentSpace->prevSpace != NULL)) {
570 currentSpace = currentSpace->prevSpace;
571 delta++;
572 currentSpaceNum--;
573 } // while
574 } // if
575 // Below will hopefully never be true; bad counting error (bug), so reset to
576 // the first Space as a failsafe....
577 if (DisplayParts(currentSpaceNum) != currentSpaceNum) {
578 currentSpaceNum = 0;
579 currentSpace = firstSpace;
580 DisplayParts(currentSpaceNum);
581 } // if
582 } // GPTDataCurses
583
584 // Move option selection left or right....
MoveSelection(int delta)585 void GPTDataCurses::MoveSelection(int delta) {
586 int newKeyNum;
587
588 // Begin with a sanity check to ensure a valid key is selected....
589 if (whichOptions.find(currentKey) == string::npos)
590 currentKey = 'n';
591 newKeyNum = whichOptions.find(currentKey);
592 newKeyNum += delta;
593 if (newKeyNum < 0)
594 newKeyNum = whichOptions.length() - 1;
595 newKeyNum %= whichOptions.length();
596 currentKey = whichOptions[newKeyNum];
597 DisplayOptions(currentKey);
598 } // GPTDataCurses::MoveSelection()
599
600 // Show user's options. Refers to currentSpace to determine which options to show.
601 // Highlights the option with the key selectedKey; or a default if that's invalid.
DisplayOptions(char selectedKey)602 void GPTDataCurses::DisplayOptions(char selectedKey) {
603 uint i, j = 0, firstLine, numPerLine;
604 string optionName, optionDesc = "";
605
606 if (currentSpace != NULL) {
607 if (currentSpace->partNum == -1) { // empty space is selected
608 whichOptions = EMPTY_SPACE_OPTIONS;
609 if (whichOptions.find(selectedKey) == string::npos)
610 selectedKey = 'n';
611 } else { // a partition is selected
612 whichOptions = PARTITION_OPTIONS;
613 if (whichOptions.find(selectedKey) == string::npos)
614 selectedKey = 't';
615 } // if/else
616
617 firstLine = LINES - 4;
618 numPerLine = (COLS - 8) / 12;
619 ClearBottom();
620 move(firstLine, 0);
621 for (i = 0; i < whichOptions.length(); i++) {
622 optionName = "";
623 for (j = 0; menuMain[j].key; j++) {
624 if (menuMain[j].key == whichOptions[i]) {
625 optionName = menuMain[j].name;
626 if (whichOptions[i] == selectedKey)
627 optionDesc = menuMain[j].desc;
628 } // if
629 } // for
630 move(firstLine + i / numPerLine, (i % numPerLine) * 12 + 4);
631 if (whichOptions[i] == selectedKey) {
632 attron(A_REVERSE);
633 printw("[ %s ]", optionName.c_str());
634 attroff(A_REVERSE);
635 } else {
636 printw("[ %s ]", optionName.c_str());
637 } // if/else
638 } // for
639 move(LINES - 1, (COLS - optionDesc.length()) / 2);
640 printw(optionDesc.c_str());
641 currentKey = selectedKey;
642 } // if
643 } // GPTDataCurses::DisplayOptions()
644
645 // Accept user input and process it. Returns when the program should terminate.
AcceptInput()646 void GPTDataCurses::AcceptInput() {
647 int inputKey, exitNow = 0;
648
649 do {
650 refresh();
651 inputKey = getch();
652 switch (inputKey) {
653 case KEY_UP:
654 ChangeSpaceSelection(-1);
655 break;
656 case KEY_DOWN:
657 ChangeSpaceSelection(+1);
658 break;
659 case 339: // page up key
660 ChangeSpaceSelection(RESERVED_TOP + RESERVED_BOTTOM - LINES);
661 break;
662 case 338: // page down key
663 ChangeSpaceSelection(LINES - RESERVED_TOP - RESERVED_BOTTOM);
664 break;
665 case KEY_LEFT:
666 MoveSelection(-1);
667 break;
668 case KEY_RIGHT:
669 MoveSelection(+1);
670 break;
671 case KEY_ENTER: case 13:
672 exitNow = Dispatch(currentKey);
673 break;
674 case 27: // escape key
675 exitNow = 1;
676 break;
677 default:
678 exitNow = Dispatch(inputKey);
679 break;
680 } // switch()
681 } while (!exitNow);
682 } // GPTDataCurses::AcceptInput()
683
684 // Operation has been selected, so do it. Returns 1 if the program should
685 // terminate on return from this program, 0 otherwise.
Dispatch(char operation)686 int GPTDataCurses::Dispatch(char operation) {
687 int exitNow = 0;
688
689 switch (operation) {
690 case 'a': case 'A':
691 SetAlignment();
692 break;
693 case 'b': case 'B':
694 Backup();
695 break;
696 case 'd': case 'D':
697 if (ValidPartNum(currentSpace->partNum))
698 DeletePartition(currentSpace->partNum);
699 break;
700 case 'h': case 'H':
701 ShowHelp();
702 break;
703 case 'i': case 'I':
704 if (ValidPartNum(currentSpace->partNum))
705 ShowInfo(currentSpace->partNum);
706 break;
707 case 'l': case 'L':
708 LoadBackup();
709 break;
710 case 'm': case 'M':
711 if (ValidPartNum(currentSpace->partNum))
712 ChangeName(currentSpace->partNum);
713 break;
714 case 'n': case 'N':
715 if (currentSpace->partNum < 0) {
716 MakeNewPart();
717 IdentifySpaces();
718 } // if
719 break;
720 case 'q': case 'Q':
721 exitNow = 1;
722 break;
723 case 't': case 'T':
724 if (ValidPartNum(currentSpace->partNum))
725 ChangeType(currentSpace->partNum);
726 break;
727 case 'v': case 'V':
728 Verify();
729 break;
730 case 'w': case 'W':
731 SaveData();
732 break;
733 default:
734 break;
735 } // switch()
736 DrawMenu();
737 return exitNow;
738 } // GPTDataCurses::Dispatch()
739
740 // Draws the main menu
DrawMenu(void)741 void GPTDataCurses::DrawMenu(void) {
742 string title="cgdisk ";
743 title += GPTFDISK_VERSION;
744 string drive="Disk Drive: ";
745 drive += device;
746 ostringstream size;
747
748 size << "Size: " << diskSize << ", " << BytesToIeee(diskSize, blockSize);
749
750 clear();
751 move(0, (COLS - title.length()) / 2);
752 printw(title.c_str());
753 move(2, (COLS - drive.length()) / 2);
754 printw(drive.c_str());
755 move(3, (COLS - size.str().length()) / 2);
756 printw(size.str().c_str());
757 DisplayParts(currentSpaceNum);
758 } // DrawMenu
759
MainMenu(void)760 int GPTDataCurses::MainMenu(void) {
761 if (((LINES - RESERVED_TOP - RESERVED_BOTTOM) < 2) || (COLS < 80)) {
762 Report("Display is too small; it must be at least 80 x 14 characters!");
763 } else {
764 if (GPTData::Verify() > 0)
765 Report("Warning! Problems found on disk! Use the Verify function to learn more.\n"
766 "Using gdisk or some other program may be necessary to repair the problems.");
767 IdentifySpaces();
768 currentSpaceNum = 0;
769 DrawMenu();
770 AcceptInput();
771 } // if/else
772 endwin();
773 return 0;
774 } // GPTDataCurses::MainMenu
775
776 /***********************************************************
777 * *
778 * Non-class support functions (mostly related to ncurses) *
779 * *
780 ***********************************************************/
781
782 // Clears the specified line of all data....
ClearLine(int lineNum)783 void ClearLine(int lineNum) {
784 move(lineNum, 0);
785 clrtoeol();
786 } // ClearLine()
787
788 // Clear the last few lines of the display
ClearBottom(void)789 void ClearBottom(void) {
790 move(LINES - RESERVED_BOTTOM, 0);
791 clrtobot();
792 } // ClearBottom()
793
PromptToContinue(void)794 void PromptToContinue(void) {
795 ClearBottom();
796 move(LINES - 2, (COLS - 29) / 2);
797 printw("Press any key to continue....");
798 cbreak();
799 getch();
800 } // PromptToContinue()
801
802 // Display one line of text on the screen and prompt to press any key to continue.
Report(string theText)803 void Report(string theText) {
804 clear();
805 move(0, 0);
806 printw(theText.c_str());
807 move(LINES - 2, (COLS - 29) / 2);
808 printw("Press any key to continue....");
809 cbreak();
810 getch();
811 } // Report()
812
813 // Displays all the partition type codes and then prompts to continue....
814 // NOTE: This function temporarily exits curses mode as a matter of
815 // convenience.
ShowTypes(void)816 void ShowTypes(void) {
817 PartType tempType;
818 char junk;
819
820 def_prog_mode();
821 endwin();
822 tempType.ShowAllTypes(LINES - 3);
823 cout << "\nPress the <Enter> key to continue: ";
824 cin.get(junk);
825 reset_prog_mode();
826 refresh();
827 } // ShowTypes()
828