Anonymous avatar Anonymous committed a9f9f23

Comments (0)

Files changed (2)

 {
 	PedPartition*	part;
 	int		num; 
-	char*		buf;
+	char		buf[100];
 	if (!disk) 
 		return 0;
 	if (!uiquery->getpart) {
-		sprintf(buf,_("%s number"),prompt);
+		snprintf(buf,sizeof(buf),_("%s number"),prompt);
 		num = (*value) ? (*value)->num : 0;
 		if (!uiquery->getint (buf, &num)) {
-			free(buf);
 			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 					     _("Expecting a partition number."));
 			return 0;
 		}
-		free(buf);
 		part = ped_disk_get_partition (disk, num);
 		if (!part) {
 			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 	char*		disk_type_name;
 	PedDiskType*	disk_type;
 	if (!uiquery->getdisktype) {
-		if (*value) 
+		if (*value) {
+			disk_type_name = (char *) calloc(sizeof((*value)->name),sizeof(char));
 			strcpy(disk_type_name,(*value)->name);
+		}
 		if (!uiquery->getstring (prompt, &disk_type_name, disk_type_list, NULL, -1))
 			return 0;
 		if (!disk_type_name) {
 	PedFileSystemType*	fs_type;
 	if (!uiquery->getfstype) {
 		if(*value) {
+			fs_type_name = (char *) calloc(sizeof((*value)->name),sizeof(char));
 			strcpy(fs_type_name,(*value)->name);
 		}
 		if (!uiquery->getstring(prompt,&fs_type_name,fs_type_list,NULL,-1))
 
 	def_str = ped_unit_format (dev, *value);
 	if (*value) {
+		input = (char *) calloc(sizeof(def_str),sizeof(char));
 		strcpy(input,def_str);
 	}
 	if (custom || !input) {
         PedFileSystemType*      dst_fs_type;
 	if (warn)
 		if (!ask_boolean_question 
-		(_("WARNING: This writes all data to disk automatically, continue"),uiquery)) 
+		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 1;
 
         if (!dst_disk)
         PedFileSystem*          fs;
 	if (warn) {
 		if (!ask_boolean_question 
-		(_("WARNING: This writes all data to disk automatically, continue"),uiquery)) 
+		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 1;
 	}
 
 
 	if (warn) {
 		if (!ask_boolean_question 
-		(_("WARNING: This writes all data to disk automatically, continue"),uiquery)) 
+		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 1;
 	}
 
 
 	if (warn) {
 		if (!ask_boolean_question 
-		(_("WARNING: This writes all data to disk automatically, continue"),uiquery)) 
+		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 1;
 	}
 
 static int
 do_name (PedDisk* disk, PedPartition* part, char *name, UICalls *uiquery)
 {
-
+	const char *temp;
         if (!disk)
                 goto error;
 	if (!part) 
         	if (!get_partition (_("Partition number?"), disk, &part, uiquery))
                 	goto error;
 	if (!name) {
-		strcpy(name, ped_partition_get_name(part));
+		temp = ped_partition_get_name(part);
+		name = (char *) calloc(sizeof(temp),sizeof(char));
+		strcpy(name,temp);
         	if (!uiquery->getstring(_("Partition name"), &name, NULL, NULL, 0))
 			goto error;
 	}
                 goto error;
 	if (warn)
 		if (!ask_boolean_question 
-		(_("WARNING: This writes all data to disk automatically, continue"),uiquery)) 
+		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 1;
 
         if (!get_sector (_("Start"), disk->dev, &start, NULL, custom, uiquery))
                 goto error;
 	if (warn)
 		if (!ask_boolean_question 
-		(_("WARNING: This writes all data to disk automatically, continue"),uiquery)) 
+		(_("WARNING: This writes all data to disk automatically, continue?"),uiquery)) 
 			return 1;
 	if (!part) 
         	if (!get_partition (_("Partition"), disk, &part, uiquery))
         if (!disk)
                 goto error;
 	if (!part) 
-        	if (!get_partition (_("Partition number?"), disk, &part, uiquery))
+        	if (!get_partition (_("Partition"), disk, &part, uiquery))
                 	goto error;
         if (!_partition_warn_busy (part))
                 goto error;
 #define NCH 1
 
 #include "../config.h"
-#include "command.h"
+#include "base.h"
+/*#include "command.h"*/
 /*#include "ui.h"*/
 
 #define N_(String) String
 #else
   #include <curses.h>
 #endif
+#endif
 
 #include <ctype.h>
 #include <stdarg.h>
 
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
-
+#endif
 /* Set the default device, if not set
    Should respect the platform for which cfdisk is compiled */
 #ifndef DEFAULT_DEVICE
 #define DEFAULT_DEVICE "/dev/hda"
 #define ALTERNATE_DEVICE "/dev/sda"
-#endif
+#endif
+
+
+#define K	1000
+
+#define MAX(X,Y) (X > Y ? X : Y)
+#define MIN(X,Y) (X < Y ? X : Y)
+
+/* Keys */
+#define BELL	'\007'	/* ^G */
+#define TAB	'\011'	/* ^I */
+#define CR	'\015'	/* ^M */
+#define ESC	'\033'	/* ^[ */
+#define DEL	'\177'
+#define CTRL_L	'\014'	/* ^L */
+#define CTRL_P	'\020'	/* ^P */
+#define CTRL_N	'\016'	/* ^N */
+#define CTRL_R	'\022'	/* ^R */
+
+#define MENUSIZE 3
+#define MENUDIV 1
+
+
+#define UP_KEY 1
+#define DOWN_KEY 2
+
+#define MENU_TITLE 1  /* Leave one row for menu title */
+#define MENU_LIST 2  /* Display a fullscreen list */
+#define MENU_ANYKEY 4
+#define MENU_BUTTON 8
+
+
+
+struct MenuItem {
+	char key;
+	char *name;
+	char *desc; 
+};
+/*int curses_ready = 0;*/
+
+
+
+void
+clear_status() {
+	move(LINES-1,0);
+	clrtoeol();
+}
+void
+set_status(const char *status) {
+	int x = (COLS-strlen(status))/2;
+	if (x < 0) x = 0; /* We should make sure that this is not negative, for a very long string */
+	clear_status();
+	/*attron(A_REVERSE);*/
+	move(LINES-1,0);
+	mvaddstr(LINES-1, x ,status);
+	/*attroff(A_REVERSE);*/
+}
+
+void
+print_warning(const char *status) {
+	set_status(status);
+	putchar(BELL);
+}
+
+void
+menu_title (const char *title) {
+	move(LINES - 2 - MENUSIZE, 0);
+	clrtoeol();
+	mvaddstr(LINES - 2 - MENUSIZE, MENUDIV, title);	
+}
+
+/* Menu drawing function */
+void
+menu_draw (struct MenuItem *items, int item_len, int type, const char *keys, int selected) {
+	int i, y = LINES - 2 - MENUSIZE + (type & MENU_TITLE ? 1 : 0), x = MENUDIV;
+	int item_size = item_len + (type & MENU_BUTTON ? 2 : 0);
+	const char *desc;
+	move(y,0);
+	clrtoeol();
+	for (i = 0; items[i].name; i++) {
+		/*while (items[i].name && !strchr(keys,tolower(items[i].key))) i++;
+		if (!items[i].name) break;*/
+		char buf[item_size+2];
+		int name_len;
+		const char *name;
+
+		/* Localize the text. Translate empty string to empty string. */
+		name = (items[i].name[0] ? _(items[i].name) : "");
+
+		name_len = strlen(name);
+		/* Enclose the text if the type is a button. If it is too long, cut. */
+		if (name_len >= item_len) {
+			snprintf(buf, item_size+1, (type & MENU_BUTTON) ? "[%s]" : "%s" , name);
+			if (type & MENU_BUTTON) {
+				buf[item_size-1] = ']';
+				buf[item_size] = '\0';
+			}
+		}
+		else {
+			snprintf(buf, item_size+1, (type & MENU_BUTTON) ? "[%*s%s%*s]" : "%*s%s%*s" ,
+				(item_len - name_len)/2, "", name, (item_len - name_len + 1)/2 , "");
+		}
+
+		/* Write the button. Highlight if is selected. */
+		
+		if (i == selected) attron(A_STANDOUT);
+		/*else if (strchr(keys,tolower(items[i].key))) attron(A_DIM);*/
+
+		mvaddstr(y,x,buf);
+
+		if (i == selected) attroff(A_STANDOUT);
+		/*else if (strchr(keys,tolower(items[i].key))) attroff(A_BOLD);*/
+		
+		if (x + 2*item_size + 2*MENUDIV < COLS) {
+			x += item_size + MENUDIV;
+		}
+		else if (y + 3 < LINES) { /* y + 1 < LINES - 2 */
+			y++;
+			x = MENUDIV;
+			move(y,0);
+			clrtoeol();
+		}
+		/* If we can't draw all the menu items, end. */
+		else break;
+	}
+	while (++y < LINES-2) {
+		move(y,0);
+		clrtoeol();
+	}
+	move(LINES-2,0);
+	clrtoeol();
+	move(LINES-1,0);
+	clrtoeol();
+	/* If the selected item exits, print its description */
+	if (selected < i) {
+		desc = _(items[selected].desc);
+		set_status(desc);
+	}
+	refresh;
+}
+
+
+
+/* Menu display function. The selected parameter specifies the item that is selected by
+ * default and the keys string includes the keys that should be accepted by the interface. */
+static int
+do_menu (struct MenuItem *items, int item_len, int type, const char *keys, int selected) {
+	int i, key = 0, count = 0, redraw = 1;
+	
+	/* Count the items and make sure that the selected item is available */
+	for (count = 0; items[count].name; count++);
+	if (selected >= count)
+		selected = 0;
+	while (!key) {
+		if (redraw) {
+			menu_draw (items, item_len, type, keys, selected);
+			redraw = 0;
+		}
+		key = getch();
+		if (key == ESC) {
+			key = getch();
+			if (key == '[' || key == 'O') {
+				key = getch();
+				if (key == 'A') { /* ^[[A = Up arrow */
+					key = DOWN_KEY;
+				}
+				else if (key == 'B') {	/* ^[[B = Down arrow */
+					key = UP_KEY;
+				}
+				else if (key == 'C') {	/* ^[[C = Right arrow */
+					do {
+						selected = (selected + 1) % count;
+					} while (!strchr(keys,tolower(items[selected].key)));
+					key = 0;
+					redraw = 1;
+				}
+				else if (key == 'D') { /* ^[[D = Left arrow */
+					do {
+						selected -= 1;
+						if (selected < 0) selected += count;
+					} while (!strchr(keys,tolower(items[selected].key)));
+					key = 0;
+					redraw = 1;
+				}
+			}
+		}
+		else if (key == CR) {
+			key = items[selected].key;
+		}
+		/* If the pressed key is in the list of accepted keys, print a warning */
+		/* FIXME: If the key of the selected item is not in the accepted keys, *
+		 * it is still valid                                                   */
+		if(!(type & MENU_ANYKEY) && !strchr(keys,tolower(key))) {
+			key = 0;
+			print_warning(_("Invalid key"));
+		}
+	}
+	return key;
+}
+
+UICalls uiquery;
+
+
+int
+getbool(const char* prompt, int* value) {
+	int result;
+	struct MenuItem yes_no[] = {
+		{'y', N_("Yes"), "" },
+		{'n', N_("No"), "" },
+		{0, NULL, NULL }
+	};
+	menu_title(prompt);
+	result = do_menu(yes_no, 5, MENU_BUTTON | MENU_TITLE, "yn", 0);
+	*value = (result == 'y');
+	return 1;
+}
+
+static void
+init_calls() {
+	uiquery.getbool = getbool;
+}
+
+
+static struct MenuItem part_menu[] = {
+	{ 'b', N_("Bootable"), N_("Toggle bootable flag of the current partition") },
+	{ 'n', N_("New"), N_("Create new partition from free space") },
+	{ 's', N_("Rescue"), N_("Look for partitions in the free space") },
+	{ 'f', N_("Format"), N_("Formats the partition with a filesystem") },
+	{ 'c', N_("Check"), N_("Check partition for consistency") },
+	{ 'a', N_("Rename"), N_("Change partition name") },
+	{ 'y', N_("Copy"), N_("Write another partition over this one (requires commit)") },
+	{ 'r', N_("Resize"), N_("Resizes the current partition (requires commit)") },
+	{ 'o', N_("Move"), N_("Moves the current partition (requires commit)") },
+	{ 'd', N_("Delete"), N_("Delete the current partition") },
+	{ 't', N_("Type"), N_("Change the filesystem type") },
+	{ 'u', N_("Units"), N_("Change units of the partition size display") },
+	{ 'w', N_("Commit"), N_("Write the changes to the disk") },
+	{ 'q', N_("Quit"), N_("Quit program without writing partition table") },
+	{ 'p', N_("Print"), N_("Print partition table to the screen or to a file") },
+	{ 'h', N_("Help"), N_("Display help") },
+        { 0, NULL, NULL }
+};
+
+static void
+do_ui (void) {
+	/*curses_ready = 1;*/
+	initscr();
+	//start_color();
+	noecho();
+	nonl();
+	init_calls();
+	int test;
+	uiquery.getbool(N_("Start interface?"),&test);
+	if (test)
+		do_menu(part_menu, 8, MENU_BUTTON, "bfcayrodtuwqph", 0);
+	endwin();
+}
+int
+main (void) {
+	do_ui();
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.