AIX仿真SCO UNIX的MENU驱动
王光红
AIX有CURSES库,但没有MENU驱动,这样给用户这样的问题:一需要自己写MENU程序,二是代码从SCO UNIX移植到AIX,如果用到MENU就无法移植。本人经过对SCO UNIX的MENU研究,成功地编写了AIX仿真SCO UNIX的MENU驱动代码:
#include <curses.h>
#include "menu.h" //将SCO UNIX的menu.h照搬到AIX上
#include <math.h>
static int index=0;
ITEM *new_item(char *name, char *desc)
{
ITEM *i;
i=(ITEM *) malloc(sizeof(ITEM));
i->name.str=name;
i->name.length=strlen(name);
i->description.str=desc;
i->description.length=strlen(desc);
i->index=index++;
i->imenu=(MENU *)0;
i->value=0;
i->userptr=(char *)0;
i->opt=0x00;
i->status=0;
i->y=0;
i->x=0;
i->left=(ITEM *)0;
i->right=(ITEM *)0;
i->up=(ITEM *)0;
i->down=(ITEM *)0;
return i;
}
MENU *new_menu(ITEM **item)
{
MENU *m;
int n=0;
index=0;
m=(MENU *) malloc(sizeof(MENU));
memset(m, 0L, sizeof(MENU));
m->items=item;
m->curitem=item[0];
for(n=m->nitems=0;item[n];m->nitems++, n++){
if(m->namelen < item[n]->name.length)
m->namelen = item[n]->name.length;
if(m->desclen < item[n]->description.length)
m->desclen = item[n]->description.length;
}
m->marklen=1;
m->width=m->namelen+m->desclen+1+m->marklen;
m->rows=m->height=m->nitems;
m->cols=1;
m->frows=16;
m->fcols=1;
m->itemlen=m->width;
m->pad=' ';
m->toprow=0;
return m;
}
int strncmpi(char *s1, char *s2, int len)
{
int i;
char t1[512], t2[512];
strcpy(t1, s1);
strcpy(t2, s2);
for(i=0; i<len && t1[i]; i++)
if(isupper(t1[i])) t1[i]=tolower(t1[i]);
for(i=0; i<len && t2[i]; i++)
if(isupper(t2[i])) t2[i]=tolower(t2[i]);
return strncmp(t1, t2, len);
}
int set_menu_pattern(MENU *m, char *pattern)
{
int len, n, s;
ITEM *ci;
len=strlen(pattern);
n=m->curitem->index;
ci=m->curitem;
for(s=0;s < m->nitems; n++, s++){
n %=m->nitems;
if(strncmpi(m->items[n]->name.str, pattern, len)==0){
m->pattern=pattern;
m->curitem=m->items[n];
return E_OK;
}
}
return E_NO_MATCH;
}
int set_menu_mark(MENU *m, char *mark)
{
m->mark=mark;
m->marklen=strlen(mark);
m->width +=m->marklen;
m->itemlen +=m->marklen;
m->width=m->itemlen;
return 0;
}
int set_menu_pad(MENU *m, int pad)
{
m->pad=pad;
return 0;
}
int set_menu_format(MENU *m, int y, int x)
{
m->frows=y;
m->fcols=x;
if(x > m->cols){
if(x>=m->nitems){
m->cols=m->nitems;
m->rows=1;
}
else{
m->cols=x;
m->rows=(int)(m->nitems/x)+ (m->nitems%x>0);
}
}
m->height=y<m->rows ? y:m->rows;
m->width=(x<m->cols ? x:m->cols) * m->itemlen - 1;
return 0;
}
int scale_menu(MENU *m, int *rows, int *cols)
{
*rows=m->height;
*cols=m->width;
return 0;
}
int set_menu_win(MENU *m, WINDOW *w)
{
m->win=w;
}
int set_menu_sub(MENU *m, WINDOW *w)
{
m->sub=w;
}
static char *menu_lable(int n, MENU *m)
{
static char lable[81];
char tag[2];
int s, t;
tag[0]=m->pad;
tag[1]=0;
memset(lable, 0L, sizeof(lable));
if(n>=m->nitems) return lable;
s=m->itemlen - m->items[n]->name.length - m->items[n]->description.length - m->marklen -1;
sprintf(lable, "%s", m->items[n]->name.str);
for(t=0;t<s;t++) strcat(lable, tag);
strcat(lable, m->items[n]->description.str);
return lable;
}
(待续)