Anonymous avatar Anonymous committed 3489d2e

Make more Linux fdisk-like exception handler. Try to improve localisation issues. (fdisk@sv.gnu.org/fdisk--main--0--patch-55)
fdisk@sv.gnu.org/fdisk--main--0--patch-55
Keywords:

Comments (0)

Files changed (5)

 #  include <locale.h>
 /* TODO: Fix some localizing issues */
 #  define _(String) dgettext (PACKAGE, String)
+/* FIXME: This doesn't seem to work */
 #  define P_(String) dgettext ("parted", String)
 #else
 #  define _(String) (String)
 getstring (const char* prompt, char **value, const StrList* words, const StrList* locwords, int mword) {
 	/* TODO: Implement multiword, etc. */
 
-	const StrList *selected;
+	const StrList *selected, *walk, *locwalk;
 	if (words || locwords) {
-		if(locwords)
+		if(locwords) {
 			selected = do_strlist(prompt,locwords,0);
+			if (words) {
+				for (walk = words, locwalk = locwords;
+				     walk && locwalk;
+				     walk = walk->next, locwalk = locwalk->next)
+				{
+					if (selected == locwalk) {
+						selected = walk;
+						break;
+					}
+				}
+			}
+		}
 		else
 			selected = do_strlist(prompt,words,0);
 		if (*value) ped_free(*value);
 /* Here we begin with flag editing */
 static void
 printaflag (int *y, const PedPartition *part, PedPartitionFlag flag, PedPartitionFlag selected) {
-	/* Yeah, I know it shouldn't be this way, but ... */
+
+
+
+	/* TODO: I decided we should not have the names here. */
+#if 0
 	const struct { const PedPartitionFlag flag; const char* text; } labels[] = {
 		{ PED_PARTITION_BOOT, _("Bootable") },
 		{ PED_PARTITION_ROOT, _("Root") },
 		{ PED_PARTITION_MSFT_RESERVED, _("MSFT Reserved") }, /* Please, play with this on any partition you happen to find */
 		{ 0, NULL }
 	};
+#endif
 	int i;
-	const char *text = NULL;
+
 	if (!ped_partition_is_flag_available(part,flag))
 		return;
 	move(*y,0); clrtoeol();
 			attron(A_STANDOUT);
 	}
 	/* Find suitable text */
+#if 0
 	for (i = 0; labels[i].flag; i++) {
 		if (flag == labels[i].flag) {
 			text = labels[i].text;
 	}
 	if (!text)
 		text = ped_partition_flag_get_name(flag);
+#endif
+	
 	mvaddch((*y),7,'[');
 	mvaddch((*y),8,ped_partition_get_flag (part, flag) ? 'X' : ' ');
 	mvaddstr((*y),9,"] ");
-	mvaddstr((*y)++,11,text);
+	mvaddstr((*y)++,11,P_(ped_partition_flag_get_name(flag)));
 	if (selected == flag && !arrow_cursor)
 		attroff(A_STANDOUT);
 	
 		return 0;
 	}
 
-	if (!strcmp (type_name, "primary")
-			|| !strcmp (type_name, _("primary"))) {
+	if (!strcmp (type_name, "primary")) {
 		*type = 0;
 	}
-	if (!strcmp (type_name, "extended")
-			|| !strcmp (type_name, _("extended"))) {
+	if (!strcmp (type_name, "extended")) {
 		*type = PED_PARTITION_EXTENDED;
 	}
-	if (!strcmp (type_name, "logical")
-			|| !strcmp (type_name, _("logical"))) {
+	if (!strcmp (type_name, "logical")) {
 		*type = PED_PARTITION_LOGICAL;
 	}
 
 			fs_type_name = (char *) calloc(n,sizeof(char));
 			strncpy(fs_type_name,(*value)->name,n);
 		}
-		if (!uiquery->getstring(prompt,&fs_type_name,mkfs ? fs_type_mkfs : fs_type_list,NULL,-1))
+		if (!uiquery->getstring(prompt, &fs_type_name,
+		                        mkfs ? fs_type_mkfs : fs_type_list,
+		                        NULL, -1))
 			return 0;
 		if (!fs_type_name) {
 			ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
 
 			walk_name = ped_partition_flag_get_name (flag);
 			opts = str_list_append (opts, walk_name);
-			/* FIXME: If we do things like this, we should change the behaviour of getstring */
-			/*locopts = str_list_append (locopts, P_(walk_name));*/
+			locopts = str_list_append (locopts, P_(walk_name));
 		}
 	}
 	if(*value)
 	/* All functions should return 1 on success, 0 otherwise */
 	/* getstring: 	The second parameter is the string that is read. *
 	 * There are two valid string lists, the second is localized     *
+	 * The function *MUST* return the string from the non-localized  *
+	 * list corresponding to the localized one.                      *
 	 * The integer specified how to treat multiwords, TODO: explain  */
 	int (*getstring)(const char* prompt, char** value, const StrList*,
 	                 const StrList* loc, int multi_word);
 	}
 }
 
-/* We might as well get rid of these  two */
+
 
 static int
 getstring (const char* prompt, char** value, const StrList* words,
 	char*		def_str = NULL;
 	char*		input;
 	StrList*	valid = NULL;
+	const StrList*	walk;
+	const StrList*	locwalk;
+	char*		fix_result = NULL;
+
 	/* TODO: Add a function that does just this to strlist.c */
 	valid = str_list_join (str_list_duplicate(words),
 	                       str_list_duplicate(locwords));
 	if (*value) def_str = strdup(*value);
 	input = fdisk_command_line_get_word (prompt, def_str,
 				             valid, multi_word);
+
 	str_list_destroy(valid);
 	if (def_str) free(def_str);
-	*value = input;
+
+	/* If the user has choosen a localised string, we should return the
+	   non-localized one, corresponding to it */
+	for (walk = words, locwalk = locwords; walk && locwalk;
+	     walk = walk->next, locwalk = locwalk->next)
+	{
+		/* If it matches a non-localised string, we are happy */
+		if (str_list_match_node(walk, input)) {
+			if (fix_result)
+				free(fix_result);
+			fix_result = NULL;
+			break;
+		}
+		/* If it matches a localised string, we save the non-localised
+		   one, but we don't break */
+		if (!fix_result && str_list_match_node(locwalk, input)) {
+			fix_result = str_list_convert_node(walk);
+		}
+	}
+	if (fix_result) {
+		free(input);
+		*value = fix_result;
+	}
+	else
+		*value = input;
 	return 1;
 }
 
+/* We might as well get rid of this one */
 static int
 getbool (const char* prompt, int* value)
 {
 	pos.start.range = NULL;
 	pos.end.range = NULL;
 
-	if (!fdisk_compatibility_mode && part_type != PED_PARTITION_EXTENDED &&
+	if (!fdisk_compatibility_mode && fs_type->ops->create &&
+	    part_type != PED_PARTITION_EXTENDED &&
 	    command_line_prompt_boolean_question(
 		_("Do you want to create the filesystem on the partition?"))) 
 	{
 	
 }
 
+#if 0
 static int
 do_mkpartfs (PedDisk** disk)
 {
 	                         UI_CUSTOM_VALUES | UI_WARN_COMMIT | 
 	                         UI_SPECIFY_PART_TYPE);
 }
+#endif
 
 static int
 do_move (PedDisk** disk)
 	);
 }
 
+
 PedExceptionOption
 fdisk_command_line_get_ex_opt (const char* prompt, PedExceptionOption options)
 {
-	StrList*		options_strlist = NULL;
+	Option			optlist[10];
 	PedExceptionOption	opt;
-	char*			opt_name;
+	const char*		opt_name;
+	int			i = 0, result;
+
+	for (opt = option_get_next (options, 0); opt && i < sizeof(optlist) - 1; 
+	     opt = option_get_next (options, opt), i++) {
+		opt_name = ped_exception_get_option_string (opt);
+		optlist[i].option = tolower(opt_name[0]);
+		/* FIXME: Localization here won't work this way */
+		optlist[i].description = _(ped_exception_get_option_string (opt));
+	}
+	optlist[i].option = 0;
+	result = fdisk_command_line_get_option (prompt, optlist);
+	if (!result)
+		return PED_EXCEPTION_UNHANDLED;
 
 	for (opt = option_get_next (options, 0); opt; 
 	     opt = option_get_next (options, opt)) {
-		options_strlist = str_list_append_unique (options_strlist,
-				     _(ped_exception_get_option_string (opt)));
-		options_strlist = str_list_append_unique (options_strlist,
-				     ped_exception_get_option_string (opt));
-	}
-
-	opt_name = fdisk_command_line_get_word (prompt, NULL, options_strlist, 1);
-	if (!opt_name)
-		return PED_EXCEPTION_UNHANDLED;
-	str_list_destroy (options_strlist);
-
-	opt = PED_EXCEPTION_OPTION_FIRST;
-	while (1) {
-		opt = option_get_next (options, opt);
-		if (strcmp (opt_name,
-			    ped_exception_get_option_string (opt)) == 0)
-			break;
-		if (strcmp (opt_name,
-			    _(ped_exception_get_option_string (opt))) == 0)
+		opt_name = ped_exception_get_option_string (opt);
+		if (result == tolower(opt_name[0])) 
 			break;
 	}
-	free (opt_name);
 	return opt;
 }
 
 		possibilities = str_list_append (possibilities, buf);
 
 		/* Initialise the prompt string */
-		snprintf(buf,sizeof(buf),"\n   %c   %s",
-		         opts[i].option, opts[i].description);
+		if (prompt[0] == '\0')
+			snprintf(buf,sizeof(buf),"   %c   %s",
+		        	 opts[i].option, opts[i].description);
+		else
+			snprintf(buf,sizeof(buf),"\n   %c   %s",
+		        	 opts[i].option, opts[i].description);
 		prompt = realloc_and_cat (prompt, buf);
 	}
 	/* We add a newline at the end, so _construct_prompt
 	prompt_possibilities = 1;
 	
 	/* We can use i, not very intuitive to the coder, but who cares */
-	i = result[0];
+	if (result) {
+		i = result[0];
+		free(result);
+	}
+	else {
+		i = 0;
+	}
 	free(prompt);
-	free(result);
 	return i;
 } 
 
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.