ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÓÃUnixϵÄC±àд½çÃæ¿Éµ÷¡¢¹¦ÄÜÀ©³ä·½±ãµÄ²Ëµ¥

·¢²¼Ê±¼ä:2006-08-28 10:07:54À´Ô´:ºìÁª×÷Õß:yinzelei
[code]ºÓÄÏÊ¡ÁÖÖÝÊÐÐÅÓÃÁªÉç¿Æ¼¼²¿ ÀîÁ¬Çä
ÔÚ¿ª·¢³ÌÐòʱ£¬³ÌÐòµÄ½çÃæ¡¢¿ª·ÅÐÔÊÇÁ½¸ö·Ç³£ÖØÒªµÄ·½Ã棬Ŀǰ£¬ÔÚUnixϵͳÏÂÔËÐеijÌÐòµÄ½çÃæ´ó¶¼
±È½ÏËÀ°å£¬¶øÇÒ£¬ÔÚ½øÐй¦ÄÜÀ©³äʱҲ²»ÊǺܷ½±ã¡£ÄÇô£¬Äܲ»ÄÜÉè¼ÆÒ»¸öÏó WindowsÄÇÑùÄܹ»°´ÕÕÓû§ÒªÇó
ËæÒâµ÷Õû½çÃ棬¹¦ÄÜÀ©³ä·½±ãµÄ³ÌÐòÄØ£¿´ð°¸Êǿ϶¨µÄ¡£±ÊÕßͨ¹ýʵ¼ù£¬Éè¼ÆÁËÒ»¸ö²Ëµ¥³ÌÐò£¬Ê¹Óû§ÔÚ¶Ô²Ë
µ¥µÄÏÔʽÑùʽ²»ÂúÒâʱ£¬Ö»Ðèͨ¹ý¶Ô²Ëµ¥²ÎÊýÎļþ½øÐвÙ×÷¼´¿ÉÍê³É¶Ô²Ëµ¥Î»Öᢿí¶È¡¢³¤¶È¡¢ÊÇ·ñÓб߿òµÈ
½øÐе÷Õû£»ÔÚÓû§ÐèÒª½øÐй¦ÄÜÀ©³äʱ£¬Ò²ÎÞÐë¸Ä¶¯Ô´³ÌÐò£¬Ö»Ðë¶Ô²ÎÊýÎļþ½øÐвÙ×÷¾Í¿É½«ÐÂÔö¹¦Äܹҵ½²Ë
µ¥ÉÏ¡£
Ò» ²ÎÊýÎļþ˵Ã÷
±¾³ÌÐòÐèÒª½èÀßÁ½¸ö²ÎÊýÎļþÀ´ÊµÏÖ:
£¨1£©¡¢¶Ô²Ëµ¥ÖÐÿһÏî²ÎÊý½øÐÐ˵Ã÷µÄÎļþ£¨menu.def)£¬Ëü¸ñʽÈçÏÂËùÊö£º
£¡ËùÊô²Ëµ¥´úºÅ£¡Ïî˳ÐòºÅ£¡²Ëµ¥ÏîÃû³Æ£¡Íâ¹Ò³ÌÐòÃû³Æ£¡Ï¼¶²Ëµ¥´úºÅ£¡
˵Ã÷£º
1¡¢Èç²Ëµ¥´úºÅΪ"0"£¬Ôò±íʾ´ËÏîÊôÓÚÖ÷²Ëµ¥£»
2¡¢ÈçÍâ¹Ò³ÌÐòÃû³ÆΪ"0"£¬Ôò±íʾ´Ë²Ëµ¥Ïî¶ÔÓ¦µÄ¹ý³ÌÔڲ˵¥³ÌÐòÄÚ²¿»ò¶ÔÓ¦ÓÚÒ»¸ö×Ӳ˵¥£»
3¡¢Èçϼ¶²Ëµ¥´úºÅΪ"0"£¬Ôò±íʾ´Ë²Ëµ¥ÏîÎÞϼ¶×Ӳ˵¥£»
4¡¢Ïî˳ÐòºÅͬʱ¿É×÷Ϊ²Ëµ¥ÈȼüʹÓá£
¼ÙÈçÎļþmenu.defÖÐÓÐÏÂÃæÕâÒ»ÐУº
£¡0£¡3£¡¸ñʽ»¯´ÅÅÌ£¡format /dev/rfd0135ds18£¡0£¡
Ëü±íʾÖ÷²Ëµ¥µÄµÚÈýÏîΪ¸ñʽ»¯´ÅÅÌ£¬Ëü¶ÔÓ¦µÄÖ´Ðйý³ÌΪ format /dev/rfd0135ds18£¬±¾ÏîÎÞ×Ӳ˵¥¡£
Èç¹ûÓû§Ïë°Ñ×Ô¼º±àµÄʵÏÖ²éѯ¹¦ÄܳÌÐòXXX¹Òµ½±¾³ÌÐòÖ÷²Ëµ¥µÚ4ÏîÉÏ£¬Ôò¿ÉÔÚmenu.defÖÐÔö¼ÓÏÂÃæ
ÕâÒ»ÐУº
£¡0£¡4£¡²éѯ£¡XXX£¡0£¡
£¡²Ëµ¥´úºÅ£¡ÉÏÒ»¼¶²Ëµ¥´úºÅ£¡±ß¿ò±êÖ¾£¡²Ëµ¥¿í¶È£¡²Ëµ¥ÐÐÊý£¡²Ëµ¥ÁÐÊý£¡Æðʼºá×ø±ê£¡Æðʼ×Ý×ø±ê£¡
˵Ã÷£º
1¡¢±ß¿ò±ê־Ϊ"0"±íʾÎÞ¿ò£¬Îª"1"±íʾÓб߿ò£»
2¡¢Éϼ¶²Ëµ¥´úºÅΪ"-1"£¬±íʾÎÞÉϼ¶²Ëµ¥£»
3¡¢Èç²Ëµ¥´úºÅΪ"0"£¬±íʾÖ÷²Ëµ¥¡£
µ±Óû§¶Ô²Ëµ¥ÏÔʾÑùʽ²»ÂúÒâʱ£¬¿Éͨ¹ýµ÷Õû´ËÎļþÉè¼Æ¸öÐÔ»¯µÄ½çÃæ¡£
¶þ ±à³ÌʵÏÖ
±¾³ÌÐòÎļþΪmenu.c£¬²¿·Ö´úÂëÈçÏ£º
#include
#define ESC 27
#define ENT 13
#define REFRESH 12
#define MAX_M 10 /* ²Ëµ¥×î´ó²ãÊý */
void initial(),nomlastpos(),revcurpos(),disponepage(),dispprevline();
void dispnextline(),domenu(),getmenuconf(),keycont();
void getitem(), get_m_conf(), get_m_item(),clearwin(),execprog();
/* ±êʶÿһ²Ëµ¥ÏîµÄ½á¹¹ */
struct menu {
short menu_code; /* ËùÊô²Ëµ¥´úºÅ */
short item_order; /* Ïî˳ÐòºÅ */
char item[20]; /* ²Ëµ¥ÏîÃû³Æ */
char prog[80]; /* ±¾Ïî²Ëµ¥Ö´ÐгÌÐò */
short submenu_code; /* ÏÂÒ»¼¶²Ëµ¥±àºÅ */
struct menu *next; /* Ö¸ÏòÉÏÒ»ÏîµÄÖ¸Õë */
struct menu *prev; /* Ö¸ÏòÏÂÒ»ÏîµÄÖ¸Õë */
} m_item,*head,*this,*new,*last,*scrpos,*lastscrpos,*begin,*lastbegin,*lastscr[MAX_M];
/* ±êʶÿһ²Ëµ¥ÄÚÈݵĽṹ */
struct menuconf {
short menu_code; /* ²Ëµ¥´úºÅ */
short last_code; /* ÉÏÒ»¼¶²Ëµ¥´úºÅ */
short bord_flag; /* ±ß¿ò±êÖ¾ 0--Îޱ߿ò 1--Óб߿ò **/
short m_wight; /* ²Ëµ¥ÏÔʾ¿í¶È */
short m_lengh; /* ÿһÐÐÏîÊý */
short m_col; /* ²Ëµ¥ÁÐÊý */
short m_bx; /* ²Ëµ¥Æðʼºá×ø±ê */
short m_by; /* ²Ëµ¥Æðʼ×Ý×ø±ê */
} m_conf;
WINDOW *menuwin, *boxwin, *curw, *lastw[MAX_M], *workwin;
long curpos, lastcurpos, lastscrcurpos, lastmenucur[MAX_M];
short menu_no = 0, wno = 0;
/* Ö÷º¯Êý */
main()
{
initial();
getmenuconf(0); /* È¡µÚ0ºÅ²Ëµ¥²ÎÊý */
/* ´´½¨Ö÷´°¿Ú */
menuwin=newwin(m_conf.m_lengh, m_conf.m_wight, m_conf.m_bx+1, m_conf.m_by+1);
curw=menuwin; lastw[wno]=menuwin;
getitem(); /* È¡µ±Ç°²Ëµ¥¸÷ÏîÄÚÈÝ */
domenu(head, 0);
endwin();
}
/* È¡²Ëµ¥¸÷Ïî²ÎÊýº¯Êý */
void getitem()
{
FILE *fp;
char buff[0x100];
/* ½¨±ß¿ò´°¿Ú */
boxwin=newwin(m_conf.m_lengh+2,m_conf.m_wight+2,m_conf.m_bx,m_conf.m_by);
keypad(curw, TRUE);
if (m_conf.bord_flag==1) {
box(boxwin, 0,0 );
wrefresh(boxwin);
}
head=NULL;
if ((fp = fopen("./menu.def","r")) == NULL) {
fprintf(stderr, "\n²»ÄÜ´ò¿ª²Ëµ¥¶¨ÒåÎļþ\n");
return;
}
while( fgets(buff, 0x100, fp)!=NULL) {
get_m_item(buff);
continue;
new=(struct menu*)malloc(sizeof(struct menu));
if (head == NULL) {
last = head; head = new;
}
else {
this->;next = new; last = this;
}
this = new;
this->;item_order=m_item.item_order;
strcpy(this->;item,m_item.item);
strcpy(this->;prog,m_item.prog);
this->;submenu_code=m_item.submenu_code;
this->;next=NULL;
this->;prev = last;
}
fclose(fp);
}
/* ²Ëµ¥´¦Àíº¯Êý */
void domenu(curscrp, curp)
struct menu *curscrp;
int curp;
{
int i, x, y;
struct menu *mpos;
this = head;
disponepage(this);
curpos = curp; scrpos = curscrp;
lastcurpos = lastscrcurpos = 0;
revcurpos();
for(;;) {
switch (wgetch(curw)) {
case ENT:
/* ÓÐÏÂÒ»¼¶²Ëµ¥ */
if ((!strcmp(scrpos->;prog, "0")) && (scrpos->;submenu_code != 0)) {
lastbegin = begin->;next;
getmenuconf(scrpos->;submenu_code);
menu_no = scrpos->;submenu_code;
wno++;
lastmenucur[wno]=curpos;
lastscr[wno] = scrpos;
lastw[wno]=curw;
workwin=newwin(m_conf.m_lengh,m_conf.m_wight,m_conf.m_bx+1,m_conf.m_by+1);
curw=workwin;
getitem();
domenu(head, 0);
}
/* ÊÇÄÚ²¿º¯Êý */
/* ÊÇÍⲿ¿ÉÖ´ÐгÌÐò */
else {
endwin();
execprog();
}
break;
case ESC:
case 'q':
case 'Q':
case '0':
/* ÎÞÉϼ¶²Ëµ¥ */
clearwin(); endwin(); exit(0);
}
/* ÓÐÉϼ¶²Ëµ¥ */
else {
clearwin();
getmenuconf(menu_no);
getitem();
touchwin(lastw[wno]);
curw=lastw[wno];
curpos = lastmenucur[wno];
scrpos = lastscr[wno];
wno--;
wrefresh(curw);
}
break;
case 'r':
case 'R':
case REFRESH: /* ÖØÏÔÆÁÄ» */
wrefresh(curscr);
break;
case KEY_RIGHT: /* ÓÒ¹â±ê¼ü */
if ( scrpos->;next != NULL ) {
lastcurpos = curpos; lastscrpos = scrpos;
scrpos=scrpos->;next;
getyx(curw, x, y);
if((x==m_conf.m_lengh-1)&&(curpos%m_conf.m_col==m_conf.m_col-1)){
curpos-=(m_conf.m_col-1); lastcurpos = curpos - 1;
/* ʵÏÖÏòÉϾíÆÁ */
wmove(curw, 0, 0); wdeleteln(curw); dispnextline("R");
}
else
curpos++;
if ((curpos%m_conf.m_col == 0) && (m_conf.m_lengh == 1)) {
revcurpos(); break;
}
else {
nomlastpos(); revcurpos();
}
}
break;
case KEY_LEFT: /* ×ó¹â±ê¼ü */
if ( scrpos->;prev != NULL ) {
lastcurpos = curpos; lastscrpos = scrpos;
scrpos=scrpos->;prev;
getyx(curw, x, y);
if ((x==0) && (curpos%m_conf.m_col ==0)) {
curpos+=m_conf.m_col-1; lastcurpos = curpos + 1;
/* ʵÏÖÏòϾíÆÁ */
winsertln(curw); dispprevline("L");
}
else
curpos--;
if ((curpos%m_conf.m_col==m_conf.m_col-1)&&(m_conf.m_lengh==1)) {
revcurpos(); break;
}
else {
nomlastpos(); revcurpos();
}
}
break;
case KEY_UP: /* ÉϹâ±ê¼ü */
lastcurpos = curpos; lastscrpos = scrpos;
mpos = scrpos;
; for(i=0; i/td>;
if ( mpos->;prev != NULL ) mpos=mpos->;prev;
else break;
}
if ( i==m_conf.m_col ) {
getyx(curw, x, y);
if (x==0) {
lastcurpos += m_conf.m_col;
/* ʵÏÖÏòϾíÆÁ */
winsertln(curw); dispprevline("U");
}
else {
curpos-=m_conf.m_col;
}
scrpos = mpos;
if ( m_conf.m_lengh!=1)
nomlastpos();
revcurpos();
}
break;
case KEY_DOWN: /* Ϲâ±ê¼ü */
lastcurpos = curpos; lastscrpos = scrpos;
mpos = scrpos;
; for(i=0; i/td>;
if ( mpos->;next != NULL )
mpos=mpos->;next;
else
break;
}
if ( i==m_conf.m_col ) {
getyx(curw, x, y);
if (x==m_conf.m_lengh-1) {
lastcurpos -= m_conf.m_col;
/* ʵÏÖÏòÉϾíÆÁ */
wmove(curw, 0, 0); wdeleteln(curw); dispnextline("D");
}
else
curpos+=m_conf.m_col;
scrpos = mpos;
if ( m_conf.m_lengh!=1)
nomlastpos();
revcurpos();
}
break;
default:
beep();
break;
}
}
}
/* ·´ÏÔµ±Ç°ÏÊý */
void revcurpos()
{
wattrset(curw, A_STANDOUT);
wmove(curw, curpos/m_conf.m_col,
(curpos%m_conf.m_col)*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", scrpos->;item);
wattrset(curw, A_NORMAL);
wrefresh(boxwin);
}
/* Õý³£ÏÔʾÉÏÒ»ÏÊý */
void nomlastpos() {
wmove(curw, lastcurpos/m_conf.m_col, (lastcurpos%m_conf.m_col)
*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", lastscrpos->;item);
}
/* ÏÔʾһҳº¯Êý */
void disponepage(first)
struct menu *first;
{
short col, row;
begin=first; /* begin Ϊ±¾Ò³Ê×Ö¸Õë */
; for(row=0; row/td>;
; for(col=0; col/td>;
/* m_conf.m_wight/m_colΪÿһ²Ëµ¥ÏîÓ¦Õ¼ÁÐÊý*/
wmove(curw,row,col*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", first->;item);
wrefresh(curw);
last = first;
first = first->;next;
if (first == NULL) {
break;
}
}
}
}
/* ÏÔʾÉÏÒ»Ðк¯Êý */
void dispprevline(flag)
char flag[2]; /* L-×ó¹â±êÒýÆð U-ÉϹâ±êÒýÆð */
{
struct menu *tmppos;
int tmpcurpos;
tmpcurpos = curpos;
tmppos = scrpos;
if ( flag[0] == 'U') {
while ( tmpcurpos % m_conf.m_col != 0) {
tmppos = tmppos->;prev;
tmpcurpos--;
}
tmppos = tmppos->;prev;
}
for (tmpcurpos = m_conf.m_col-1; tmpcurpos >;= 0; tmpcurpos--) {
wmove(curw, 0, (tmpcurpos%m_conf.m_col)
*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", tmppos->;item);
begin = tmppos; /*begin Ϊ±¾Ò³Ê×Ö¸Õë*/
last = tmppos;
tmppos = tmppos->;prev;
if (tmppos == NULL)
break;
}
wrefresh(curw);
}
/* ÏÔʾÏÂÒ»Ðк¯Êý */
void dispnextline(flag)
char flag[2];/* R-ÓÒ¹â±êÒýÆð D-Ϲâ±êÒýÆð */
{
struct menu *tmppos;
int tmpcurpos;
tmpcurpos = curpos;
tmppos = scrpos;
if ( flag[0] == 'D') {
while ( tmpcurpos % m_conf.m_col != m_conf.m_col-1) {
tmppos = tmppos->;next; tmpcurpos++;
}
tmppos = tmppos->;next;
}
for (tmpcurpos = 0; tmpcurpos < m_conf.m_col; tmpcurpos++) {
wmove(curw, m_conf.m_lengh-1, (tmpcurpos%m_conf.m_col)
*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", tmppos->;item);
last=tmppos;/* last Ϊ±¾Ò³×îºóÒ»¸ö½áµãÖ¸Õë */
begin=tmppos; tmppos = tmppos->;next;
if (tmppos == NULL)
break;
}
}
/* È¡Ö¸¶¨²Ëµ¥²ÎÊýº¯Êý */
void getmenuconf(menu_code)
short menu_code;
{
FILE *fp;
char menu_buff[0x100];
fprintf(stderr, "can not open menu config file");
return;
}
while( fgets(menu_buff, 0x100, fp)!=NULL ) {
get_m_conf(menu_buff);
break;
}
return ;
}
/* È¡Ö¸¶¨²Ëµ¥²ÎÊý´¦Àíº¯Êý */
void get_m_conf(menu_conf)
char *menu_conf;
{
register i, j, k;
char buff[20];
j = k = 0;
for (i = 0; i < strlen(menu_conf); i++) {
if ( menu_conf[i] == '!' ) {
j++;
if ( j == 1) {
k = i+1;
continue;
}
switch(j) {
case 2:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
k=i+1;
break;
case 3:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
k=i+1;
break;
case 4:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.bord_flag = atoi(buff);
k=i+1;
break;
case 5:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_wight = atoi(buff);
k=i+1;
break;
case 6:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_lengh = atoi(buff);
k=i+1;
break;
case 7:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_col = atoi(buff);
k=i+1;
break;
case 8:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_bx = atoi(buff);
k=i+1;
break;
case 9:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_by = atoi(buff);
k=i+1;
break;
default:
break;
}
}
}
}
/* È¡Ö¸¶¨Ïî²ÎÊý´¦Àíº¯Êý */
void get_m_item(menu_item)
char *menu_item;
{
register i, j, k;
char buff[80];
j = k = 0;
for (i = 0; i < strlen(menu_item); i++) {
if ( menu_item[i] == '!' ) {
j++;
if ( j == 1) {
k = i+1;
continue;
}
switch(j) {
case 2:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
k=i+1;
break;
case 3:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
m_item.item_order = atoi(buff);
k=i+1;
break;
case 4:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
strcpy(m_item.item,buff);
k=i+1;
break;
case 5:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
strcpy(m_item.prog,buff);
k=i+1;
break;
case 6:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
m_item.submenu_code = atoi(buff);
k=i+1;
break;
default:
break;
}
}
}
}
void initial() /* ×Ô¶¨¿ªÆô curses º¯Ê½ */
{
initscr();
cbreak(); nonl(); noecho();
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
refresh();
}
/* °´¼üµÈ´ýº¯Êý */
void keycont()
{
fprintf(stderr, "°´¼ü¼ÌÐø..."); getchar();
}
/* ÔËÐпÉÖ´ÐгÌÐòº¯Êý */
void execprog()
{
system("clear");
fprintf(stderr, "%s: \n", scrpos->;item);
system(scrpos->;prog);
keycont(); initial();
touchwin(boxwin); touchwin(curw); keypad(curw, TRUE);
wrefresh(boxwin); wrefresh(curw);
}
/* Çå³ý´°¿Úº¯Êý */
void clearwin()
{
wmove(boxwin, 0, 0);
wclrtobot(boxwin); wrefresh(boxwin); delwin(curw); delwin(boxwin);
}
 Èý ±àÒë˵Ã÷
¸Ã³ÌÐò¿ÉÓÃÈçÏÂÃüÁî±àÒ루ÔÚSCO OpenServer 5.05ϱàÒëͨ¹ý£©£º
cc -o menu menu.c -lcurses[/code]
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ