📄 ui.c
字号:
}voidfdisk_print_using_dev (PedDevice* dev){ printf (_("Using %s\n"), dev->path);}intfdisk_command_line_get_word_count (){ return str_list_length (command_line);}voidfdisk_command_line_prompt_words (const char* prompt, const char* def, const StrList* possibilities, int multi_word){ char* line; char* real_prompt; char* _def = (char*) def; int _def_needs_free = 0; if (!def && str_list_length (possibilities) == 1) { _def = str_list_convert_node (possibilities); _def_needs_free = 1; } if (fdisk_opt_script_mode) { if (_def) fdisk_command_line_push_line (_def, 0); return; } do { real_prompt = _construct_prompt (prompt, _def, possibilities); line = _readline (real_prompt, possibilities); free (real_prompt); if (!line) break; if (!strlen (line)) { if (_def) fdisk_command_line_push_line (_def, 0); } else { fdisk_command_line_push_line (line, multi_word); } free (line); } while (!fdisk_command_line_get_word_count () && !_def); if (_def_needs_free) free (_def);}voidfdisk_command_line_flush (){ str_list_destroy (command_line); command_line = NULL;}/** * Get a word from command line. * * \param possibilities a StrList of valid strings, NULL if all are valid. * \param multi_word whether multiple words are allowed. * * \return The word(s), or NULL if empty. */char*fdisk_command_line_get_word (const char* prompt, const char* def, const StrList* possibilities, int multi_word){ do { if (fdisk_command_line_get_word_count ()) { char* result = fdisk_command_line_pop_word (); StrList* result_node; if (!possibilities) return result; result_node = str_list_match (possibilities, result); free (result); if (result_node) return str_list_convert_node (result_node); fdisk_command_line_flush (); } fdisk_command_line_prompt_words (prompt, def, possibilities, multi_word); } while (fdisk_command_line_get_word_count ()); return NULL;}/* "multi_word mode" is the "normal" mode... many words can be typed, * delimited by spaces, etc. * In single-word mode, only one word is parsed per line. * Leading and trailing spaces are removed. For example: " a b c " * is a single word "a b c". The motivation for this mode is partition * names, etc. In single-word mode, the empty string is a word. * (but not in multi-word mode). */voidfdisk_command_line_push_line (const char* line, int multi_word){ int quoted = 0; char quote_char = 0; char this_word [256]; int i; do { while (*line == ' ') line++; i = 0; for (; *line; line++) { if (*line == ' ' && !quoted) { if (multi_word) break; /* single word: check for trailing spaces + eol */ if (_str_is_spaces (line)) break; } if (!quoted && strchr ("'\"", *line)) { quoted = 1; quote_char = *line; continue; } if (quoted && *line == quote_char) { quoted = 0; continue; } /* hack: escape characters */ if (quoted && line[0] == '\\' && line[1]) line++; this_word [i++] = *line; } if (i || !multi_word) { this_word [i] = 0; fdisk_command_line_push_word (this_word); } } while (*line && multi_word);}char*fdisk_command_line_pop_word (){ char* result; StrList* next; PED_ASSERT (command_line != NULL, return NULL); result = str_list_convert_node (command_line); next = command_line->next; str_list_destroy_node (command_line); command_line = next; return result;}voidfdisk_command_line_push_word (const char* word){ command_line = str_list_append (command_line, word);}#if 0intfdisk_command_line_get_disk (const char* prompt, PedDisk** value){ PedDevice* dev = *value ? (*value)->dev : NULL; if (!fdisk_command_line_get_device (prompt, &dev)) return 0; if (dev != (*value)->dev) { PedDisk* new_disk = ped_disk_new (dev); if (!new_disk) return 0; *value = new_disk; } return 1;}#endif#if 0intfdisk_command_line_get_partition (const char* prompt, PedDisk* disk, PedPartition** value){ PedPartition* part; int num = (*value) ? (*value)->num : 0; if (!fdisk_command_line_get_integer (prompt, &num)) { ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Expecting a partition number.")); return 0; } part = ped_disk_get_partition (disk, num); if (!part) { ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Partition doesn't exist.")); return 0; } *value = part; return 1;}#endif#if 0intfdisk_command_line_get_fs_type (const char* prompt, const PedFileSystemType*(* value)){ char* fs_type_name; PedFileSystemType* fs_type; fs_type_name = fdisk_command_line_get_word (prompt, *value ? (*value)->name : NULL, fs_type_list, 1); if (!fs_type_name) { ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Expecting a file system type.")); return 0; } fs_type = ped_file_system_type_get (fs_type_name); if (!fs_type) { ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Unknown file system type \"%s\"."), fs_type_name); return 0; } free (fs_type_name); *value = fs_type; return 1;}#endif#if 0intfdisk_command_line_get_disk_type (const char* prompt, const PedDiskType*(* value)){ char* disk_type_name; PedDiskType* disk_type; disk_type_name = fdisk_command_line_get_word (prompt, *value ? (*value)->name : NULL, disk_type_list, 1); if (!disk_type_name) { ped_exception_throw (PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Expecting a disk label type.")); return 0; } *value = ped_disk_type_get (disk_type_name); free (disk_type_name); PED_ASSERT (*value != NULL, return 0); return 1;}#endif#if 0intfdisk_command_line_get_device (const char* prompt, PedDevice** value){ char* def_dev_name = *value ? (*value)->path : NULL; char* dev_name; PedDevice* dev; dev_name = fdisk_command_line_get_word (prompt, def_dev_name, NULL, 1); if (!dev_name) return 0; dev = ped_device_get (dev_name); free (dev_name); if (!dev) return 0; *value = dev; return 1;}#endifintfdisk_command_line_get_integer (const char* prompt, int* value){ char def_str [12]; char* input; int valid; snprintf (def_str, sizeof(def_str), "%d", *value); input = fdisk_command_line_get_word (prompt, *value ? def_str : NULL, NULL, 1); if (!input) return 0; valid = sscanf (input, "%d", value); free (input); return valid;}fdisk_command_line_get_llinteger (const char* prompt, long long* value){ char def_str [22]; char* input; int valid; snprintf (def_str, sizeof(def_str), "%lld", *value); input = fdisk_command_line_get_word (prompt, *value ? def_str : NULL, NULL, 1); if (!input) return 0; valid = sscanf (input, "%lld", value); free (input); return valid;}intfdisk_command_line_get_option (const char* head, const Option* opts){ StrList *possibilities = NULL; char *prompt = strdup(head); char *result; char buf[128]; int i; buf[1] = '\0'; for (i = 0; opts[i].option; i++) { /* Initialise the possibilities StrList */ buf[0] = opts[i].option; buf[1] = '\0'; possibilities = str_list_append (possibilities, buf); /* Initialise the prompt string */ 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 handles it as it should */ prompt = realloc_and_cat (prompt, "\n"); /* Queries the user about the option, we turn off the automatic adding of possibilities to the prompt */ prompt_possibilities = 0; result = fdisk_command_line_get_word (prompt, NULL, possibilities, 1); prompt_possibilities = 1; /* We can use i, not very intuitive to the coder, but who cares */ if (result) { i = result[0]; free(result); } else { i = 0; } free(prompt); return i;} intfdisk_get_partpos (const char* prompt, const void* context, const char *possibilities){ const Option *orig_opts = (Option *) context; Option opts[5]; int i,j; for (i=0, j=0; orig_opts[i].option && j < 5; i++) { if (strchr(possibilities,orig_opts[i].option)) { opts[j].option = orig_opts[i].option; opts[j].description = orig_opts[i].description; j++; } } opts[j].option = 0; return fdisk_command_line_get_option(prompt, opts);}intfdisk_command_line_get_part_type (const char* prompt, const PedDisk *disk, PedPartitionType *type){ Option opts[4]; int i = 0, temp; if (_can_create_extended (disk)) { opts[i].option = 'e'; opts[i].description = _("extended"); i++; } if (_can_create_logical (disk)) { temp = PED_PARTITION_LOGICAL; opts[i].option = 'l'; opts[i].description = _("logical (5 or over)"); i++; } if (_can_create_primary (disk)) { opts[i].option = 'p'; opts[i].description = _("primary partition (1-4)"); i++; } opts[i].option = 0; if (i == 0) return 0; else if (i == 1) temp = opts[0].option; else temp = fdisk_command_line_get_option(prompt, opts); if (temp == 'e') *type = PED_PARTITION_EXTENDED; else if (temp == 'l') *type = PED_PARTITION_LOGICAL; else if (temp == 'p') *type = PED_PARTITION_NORMAL; else return 0; return 1;}#if 0intfdisk_command_line_get_part_type (const char* prompt, const PedDisk* disk, PedPartitionType* type){ StrList* opts = NULL; char* type_name; if (_can_create_primary (disk)) { opts = str_list_append_unique (opts, "primary"); opts = str_list_append_unique (opts, _("primary")); } if (_can_create_extended (disk)) { opts = str_list_append_unique (opts, "extended"); opts = str_list_append_unique (opts, _("extended")); } if (_can_create_logical (disk)) { opts = str_list_append_unique (opts, "logical"); opts = str_list_append_unique (opts, _("logical")); } if (!opts) { ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Can't create any more partitions.")); return 0; } type_name = fdisk_command_line_get_word (prompt, NULL, opts, 1); str_list_destroy (opts); if (!type_name) { ped_exception_throw ( PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL, _("Expecting a partition type.")); return 0; } if (!strcmp (type_name, "primary") || !strcmp (type_name, _("primary"))) { *type = 0; } if (!strcmp (type_name, "extended") || !strcmp (type_name, _("extended"))) { *type = PED_PARTITION_EXTENDED; } if (!strcmp (type_name, "logical") || !strcmp (type_name, _("logical"))) { *type = PED_PARTITION_LOGICAL; } free (type_name); return 1;}#endifchar*fdisk_command_line_peek_word (){ if (command_line) return str_list_convert_node (command_line); else return NULL;}#if 0intfdisk_command_line_get_sector (const char* prompt, PedDevice* dev, PedSector* value, PedGeometry** range){ char* def_str; char* input; int valid; def_str = ped_unit_format (dev, *value); input = fdisk_command_line_get_word (prompt, *value ? def_str : NULL, NULL, 1); /* def_str might have rounded *value a little bit. If the user picked * the default, make sure the selected sector is identical to the * default. */ if (input && *value && !strcmp (input, def_str)) { if (range) { *range = ped_geometry_new (dev, *value, 1); ped_free (def_str); return *range != NULL; } ped_free (def_str); return 1; } ped_free (def_str); if (!input) { *value = 0; if (range) *range = NULL; return 0; } valid = ped_unit_parse (input, dev, value, range); free (input); return valid;}#endifintfdisk_init_ui (){ fdisk_opt_script_mode = !isatty (0); if (!init_ex_opt_str () || !init_state_str () || !init_fs_type_str () || !init_disk_type_str ()) return 0; ped_exception_set_handler (exception_handler);#ifdef HAVE_LIBREADLINE rl_initialize (); rl_attempted_completion_function = (CPPFunction*) fdisk_complete_function; readline_state.in_readline = 0;#endif signal (SIGINT, &interrupt_handler); return 1;}voidfdisk_done_ui (){ ped_exception_set_handler (NULL); done_ex_opt_str (); done_state_str (); str_list_destroy (fs_type_list); str_list_destroy (disk_type_list);}intfdisk_command_line_get_part_flag (const char* prompt, const PedPartition* part, PedPartitionFlag* flag){ StrList* opts = NULL; PedPartitionFlag walk = 0; char* flag_name; while ( (walk = ped_partition_flag_next (walk)) ) { if (ped_partition_is_flag_available (part, walk)) { const char* walk_name; walk_name = ped_partition_flag_get_name (walk); opts = str_list_append (opts, walk_name); opts = str_list_append_unique (opts, _(walk_name)); } } flag_name = fdisk_command_line_get_word (prompt, NULL, opts, 1); str_list_destroy (opts); if (flag_name) { *flag = ped_partition_flag_get_by_name (flag_name); ped_free (flag_name); return 1; } else { return 0; }}intfdisk_command_line_get_state (const char* prompt, int* value){ char* def_word; char* input; if (*value) def_word = str_list_convert_node (on_list); else def_word = str_list_convert_node (off_list); input = fdisk_command_line_get_word (prompt, def_word, on_off_list, 1); free (def_word); if (!input) return 0; if (str_list_match_any (on_list, input)) *value = 1; else *value = 0; free (input); return 1;}intfdisk_command_line_get_unit (const char* prompt, PedUnit* unit){ StrList* opts = NULL; PedUnit walk; char* unit_name; const char* default_unit_name; for (walk = PED_UNIT_FIRST; walk <= PED_UNIT_LAST; walk++) opts = str_list_append (opts, ped_unit_get_name (walk)); default_unit_name = ped_unit_get_name (ped_unit_get_default ()); unit_name = fdisk_command_line_get_word (prompt, default_unit_name, opts, 1); str_list_destroy (opts); if (unit_name) { *unit = ped_unit_get_by_name (unit_name); free (unit_name); return 1; } else { return 0; }}intfdisk_command_line_is_integer (){ char* word; int is_integer; int scratch; word = fdisk_command_line_peek_word (); if (!word) return 0; is_integer = sscanf (word, "%d", &scratch); free (word); return is_integer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -