⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfdisk.c

📁 fdisk 实现源码,可以查询Linux下系统的分区信息
💻 C
📖 第 1 页 / 共 5 页
字号:
		clrtoeol();	}	move(LINES-1,0);	clrtoeol();	/* If the selected item exits, print its description */	if (selected < i) {		desc = _(items[selected].desc);		set_status(desc,0);	}	refresh();}/* TODO: Not perfect */static is_key_ok (int key, const char* ok_keys, MenuOptions opts) {	if (opts & MENU_ARROWS) {		switch (key) {			case KEY_UP:			case KEY_DOWN:			case KEY_LEFT:			case KEY_RIGHT:				return 1;		}	}#if USE_KEYPAD	if (key > 255)		return 0;#endif	return strchr(ok_keys,key) != NULL;}/* 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 intdo_menu (const MenuItem *items, int item_len, MenuOptions opts, const char *keys, int *sel) {	int i, key = 0, count = 0, redraw = 1, save_selection = 0, selected = 0;	if (sel) {		selected = *sel;	}	/* Count the items and make sure that the selected item is available */	for (count = 0; items[count].name; count++);	if (selected >= count)		selected = 0;	i = 0;	/* NOTE: Was tolower(items[selected].key) */	while (!strchr(keys,items[selected].key)) {		selected = (selected + 1) % count;		if (i++ > count) {			selected = 0;			break;		}	}	/* If the selected menu item was not not available, we save the pointer to change it back */	/* FIXME: The menu acts funny. Re-order the items so that it works better :) */	//if (selected == saved_selection) save_selection = 1;	while (!key) {		if (redraw) {			menu_draw (items, item_len, opts, keys, selected);			opts &= ~MENU_LEAVE_WARNING; /* Clear the warning next time */			redraw = 0;		}		key = getch();#if USE_KEYPAD == 0		if (key == ESC) {			key = getch();			if (key == '[' || key == 'O') {				key = getch();				if (key == 'A') { /* ^[[A = Up arrow */					key = KEY_UP;				}				else if (key == 'B') {	/* ^[[B = Down arrow */					key = KEY_DOWN;				}				else if (key == 'C') {	/* ^[[C = Right arrow */					key = KEY_RIGHT;				}				else if (key == 'D') { /* ^[[D = Left arrow */					key = KEY_LEFT;				}			}		}#endif		if (key == KEY_RIGHT) {			do {				selected = (selected + 1) % count;			/* NOTE: Was tolower(items[selected].key) */			} while (!strchr(keys,items[selected].key));			key = 0;			redraw = 1;			save_selection = 1;		}		else if (key == KEY_LEFT) {			do {				selected -= 1;				if (selected < 0) selected += count;			/* NOTE: Was tolower(items[selected].key) */			} while (!strchr(keys,items[selected].key));			key = 0;			redraw = 1;			save_selection = 1;		}		else if (key == CR) {			key = items[selected].key;		}		/* If the pressed key is in the list of accepted keys, print a warning */		/* NOTE: Was tolower(key) */		if(!(opts & MENU_ANYKEY) && !is_key_ok(key,keys,opts)) {			key = 0;			print_warning(_("Invalid key"),0);		}	}	if (sel && save_selection)		*sel = selected;	return key;}/* Lines at the top of the string list drawer */#define SLD_HEAD 5/* String list choice *//* The last two parameters specify the number of the selected element and the first element displayed    last time. If opts & 1, we assume it is a fullscreen text, not choice*/static voidstrlist_draw (const char *prompt, const StrList *strlist, int opts, const StrList* selected,               int selnum, int *start) {	char *temp;	int n,x,y,i;	const StrList *current;	if (opts & 1) {		*start = selnum - (LINES - 2 - SLD_HEAD);		*start = MAX(0,*start);		y = 0;		n = LINES - 2;	}	else {		n = LINES-MENUSIZE-3;		y = INFOSIZE;		if (selnum - *start < 0)			*start = selnum;		else if (selnum - *start > n - INFOSIZE - SLD_HEAD)			*start = selnum - n + INFOSIZE + SLD_HEAD;	}	move(y++,0); clrtoeol();	move(y,0); clrtoeol(); mvaddstr(y++,5,prompt);	mvaddch(y,0,' ');	for (x = 1; x < COLS-1; x++) {		mvaddch(y,x,'-');        }	mvaddch(y++,x,' ');	move(y++,0); clrtoeol();	x = (opts & 1 ? 1 : 7);	for (current = strlist, i = 0; current && y < n; current = current->next, i++) {		move(y,0); clrtoeol();		if (i < *start)			continue;		if (!(opts & 1) && current == selected) {			if (arrow_cursor)				mvaddstr(y,3,"-->");			else				attron(A_STANDOUT);		}				temp = str_list_convert_node(current);		mvaddstr(y,x,temp);		if (temp) free(temp);		if (!(opts & 1) && current == selected && !arrow_cursor)			attroff(A_STANDOUT);		y++;	}	/* We clear the next line. No need to clean all lines, unless we implement page up and down */	while (y < n) {		move(y,0); clrtoeol();		y++;	} }static void show_info(Context *c);static void plist_draw (Context *c, PedPartition *selected, int selnum, int *start);static int do_plist (Context *c, PedPartition **part, PedPartitionType have, PedPartitionType havent);static const StrList*do_strlist (const char *prompt, const StrList *strlist, int opts) {	const StrList *selected = strlist, *temp, *next;	int redraw = 1, key = 0, done = 0, start = 0, selnum = 0, i;	int move_down_by;	const char* end_warning = opts & 1 ? 	                              _("No more text") : _("No more choices");	if (opts & 1) {		for (selnum = 0, selected = strlist; 		     selnum < LINES-2-SLD_HEAD && selected->next;		     selnum++, selected = selected->next);	}	while (!done) {		if (redraw) {			clear_menu();			strlist_draw(prompt, strlist, opts, selected, selnum,&start);			redraw = 0;		}		refresh();		key = getch();#if USE_KEYPAD == 0		if (key == ESC) {			key = getch();			if (key == '[' || key == 'O') {			key = getch();				if (key == 'B' && !(opts & 2)) { /*Down arrow*/					key = KEY_UP;				}				else				if (key == 'A' && !(opts & 2)) { /*Up arrow*/					key = KEY_DOWN;				}				else if (opts & 1)					done = 1;				else					print_warning(_("Invalid key"),0);			}			else if (opts & 1)				done = 1;			else				print_warning(_("Invalid key"),0);		}#endif		/* We want to move 1 element when an arrow is pressed */		move_down_by = 1;		switch (key) {#if USE_KEYPAD			case KEY_NPAGE:				if (opts & 1)					move_down_by = LINES - SLD_HEAD - 4;				else 					move_down_by = LINES - MENUSIZE -					               INFOSIZE - SLD_HEAD - 5;#endif			case KEY_DOWN:				if (!selected->next) {					print_warning(end_warning,0);					break;				}				for (i = 0; i < move_down_by && selected->next; i++) {						selected = selected->next;				}				selnum += i;				redraw = 1;				break;#if USE_KEYPAD			case KEY_PPAGE:				if (opts & 1)					move_down_by = LINES - SLD_HEAD - 4;				else 					move_down_by = LINES - MENUSIZE -					               INFOSIZE - SLD_HEAD - 5;#endif			case KEY_UP:				if (strlist == selected || 				    ((opts & 1) && selnum <= LINES-2-SLD_HEAD)) {					print_warning(end_warning,0);					break;				}								/* FIXME: This might be improved by editing strlist */				/* (next represents temp->next->next->...->next) */				/* We set temp and next move_down_by elements apart, and move				   them both, until next reaches the current element,				   then temp points to the element n elements before it */				temp = strlist;				/* We set temp and next to n elements apart */				for (i = 0, next = strlist;				     i < move_down_by && next && next != selected;				     i++, next = next->next);				/* Move them together until next is our				   current element, which means that temp				   points to the element n elements before				   it */				while (temp && next && next != selected) {					temp = temp->next;					next = next->next;				}				/* This point should be never happen */				if (next != selected) {					print_warning(_("Bug in the program. "							"The programmer is an idiot."),0);				}				else {					selnum -= i;					selected = temp;					redraw = 1;				}				break;			case CR:				done = 1;				break;			default:				if (opts & 1)					done = 1;				else					print_warning(_("Invalid key"),0);		}	}	/* We redraw partition info */	show_info(NULL);	plist_draw(NULL, NULL, 0, 0);	return selected;}static intread_char (const char* prompt, char **value) {	char buf[SMALLBUF];	int y = LINES - 2 - MENUSIZE, i, key = 0, done = 0,x;	while (y < LINES) {		move(y,0);		clrtoeol();		y++;	}	y = LINES - 1 - MENUSIZE;	int n = SMALLBUF;	if (*value) {		i = strlen(*value);		i = MIN(i,n);		strncpy(buf,*value,i);	}	else i = 0;	buf[i] = '\0';	mvaddstr(y,MENUDIV,prompt); addstr(": ");	getyx(stdscr,y,x);	n = MIN(n,COLS-x);	addstr(buf);	getyx(stdscr,y,x);		refresh();	while(!done) {		key = getch();		if (key == ESC) {#if USE_KEYPAD == 0			if (getch() == ESC) 				return 0;			else getch();#else			return 0;#endif		}		else if (key == KEY_BACKSPACE || 		         key == KEY_DC ||		         /* Fix for misconfigured terminals */		         key == '\b' ||		         key == '\177') {			if (i > 0) {				buf[--i] = '\0';				move(y,--x);				delch();			}			else				print_warning(_("No more characters to delete"),1);		}		else if (key == CR) {			done = 1;		}		else if (isprint(key)) {			if (i < n-1) {				buf[i++] = key;				buf[i] = '\0';				mvaddch(y,x++,key);			}			else				print_warning(_("String too long"),1);		}		refresh();	}	if (*value) ped_free (*value);	*value = strdup(buf);	return 1;	}static UICalls uiquery;static intgetbool(const char* prompt, int* value) {	int result;	int menudef = !*value;	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", &menudef);	*value = (result == 'y');	return 1;}static intgetstring (const char* prompt, char **value, const StrList* words, const StrList* locwords, int mword) {	const StrList *selected, *walk, *locwalk;	if (words || 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);		*value = str_list_convert_node(selected);		return (*value != NULL);	}	else		return read_char(prompt,value);}static intgetpart (const char* prompt, PedDisk **disk, PedPartition **value) {	/* NOTE: We should prompt the user for a disk in this case,	   but this will never happen in cfdisk. Also note that *disk	   is untouched in this function. */	if (!*disk) {				return 0;	}	Context choice_context;	choice_context.disk = *disk;	menu_title(prompt);	return do_plist(&choice_context, value, 0, 	                PED_PARTITION_EXTENDED | PED_PARTITION_FREESPACE);}static intgetpartpos (const char* prompt, const void* context, const char *possibilities){	menu_title(prompt);	return do_menu((MenuItem*) context, 11, MENU_BUTTON | MENU_TITLE,	               possibilities, NULL);}static PedExceptionOptionexception_handler (PedException* ex) {	static MenuItem ex_choices[] = {		{ 'f', N_("Fix"), "" },		{ 'y', N_("Yes"), "" },		{ 'n', N_("No"), "" },		{ 'o', N_("OK"), "" },		{ 'r', N_("Retry"), "" },		{ 'i', N_("Ignore"), "" },		{ 'c', N_("Cancel"), "" },		{ 0, NULL, NULL }	};	/* Adding new elements to ex_key may lead to segfaults. Double check. */	static const struct { char key; PedExceptionOption option; }  ex_keys [] = {		{ 'f', PED_EXCEPTION_FIX },		{ 'y', PED_EXCEPTION_YES },		{ 'n', PED_EXCEPTION_NO },		{ 'o', PED_EXCEPTION_OK },		{ 'r', PED_EXCEPTION_RETRY },		{ 'i', PED_EXCEPTION_IGNORE },		{ 'c', PED_EXCEPTION_CANCEL },		{ 0, 0 }	};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -