[问题]大家帮忙看看 谢谢

软件和网站开发以及相关技术探讨
回复
lpzgbd
帖子: 52
注册时间: 2007-02-14 13:56
来自: swust

[问题]大家帮忙看看 谢谢

#1

帖子 lpzgbd » 2007-06-13 16:35

这是在<<linux程序设计上>>的程序 :
#include<stdio.h>
#include<unistd.h>
#include<curses.h>
#include<term.h>
#include<termios.h>
static FILE *output_stream=(FILE*)0;
int char_to_terminal(int char_to_write);
char *menu[]={"a -add new record","d -delete record","q -quit",NULL,};
int char_to_terminal(int char_to_write){
if(output_stream) putc(char_to_write,output_stream);
return 0;
}
int getchoice(char *greet ,char *choices[],FILE *in ,FILE *out){
int chosen=0;
int screenrow,screencol=10;
int selected;
char **option;
char *cursor,*clear;
output_stream=out;
setupterm(NULL,fileno(out),(int *)0);
cursor=tigetstr("cup");
clear=tigetstr("clear");
screenrow=4;
tputs(clear,1,char_to_terminal);
tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);
fprintf(out,"choice:%s",greet);
screenrow+=2;
option=choices;
while(*option){
tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);
fprintf(out,"%s\n",*option);
screenrow++;
option++;
}
fprintf(out,"\n");
do
{
fflush(out);
selected =fgetc(in);
option=choices;
while(*option){
if(selected==*option[0]){
chosen=1;
break;
}
option++;
}
if(!chosen){
tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);
fprintf(out,"incorect choice \n");
}
}while(!chosen);
tputs(clear,1,char_to_terminal);
return selected;
}

int main(){
int choice=0;
FILE *input;
FILE *output;
struct termios init, new;

if(!isatty(fileno(stdout) )){
fprintf(stderr,"you are not a terminal!\n");
}
input=fopen("/dev/tty","r");
output=fopen("/dev/tty","w");
if(!input||!output){
fprintf(stderr,"unable to open /dev/tty\n");
}
tcgetattr(fileno(input),&init);
new=init;
new.c_lflag&=~ICANON;
new.c_lflag&~ECHO;
new.c_lflag&=~ISIG;
new.c_cc[VMIN]=1;
new.c_cc[VTIME]=0;
if(tcsetattr(fileno(input),TCSANOW,&new)!=0){
fprintf(stderr,"coundt set attributes\n");
}
do {
choice=getchoice("please select an acton",menu,input,output);
printf("you have chosen:%c\n",choice);
sleep(1);
}while(choice!='q');
tcsetattr(fileno(input),TCSANOW,&init);
exit(0);
}
主要问题是 对tputs函数不理解,比如说tputs(tparm(cursor,screenrow,screencol),1,char_to_terminal);这句话是什么意思呢?
谢谢!
回复