LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 634|回复: 1

关于“菜单函数以及终端控制”代码中函数调用疑问

[复制链接]
发表于 2010-7-1 18:31:29 | 显示全部楼层 |阅读模式
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <termios.h>
  5. #include <term.h>
  6. #include <curses.h>
  7. static FILE *output_stream = (FILE *)0;
  8. char *menu[] = {
  9.         "a - add new record",
  10.         "d - delete record",
  11.         "q - quit",
  12.         NULL,
  13.         };
  14. int getchoice(char *greet, char *choices[], FILE *in, FILE *out);
  15. int char_to_terminal(int char_to_write);
  16. int main()
  17. {
  18.         int choice = 0;
  19.         FILE *input;
  20.         FILE *output;
  21.         if(!isatty(fileno(stdout))) {
  22.                 fprintf(stderr, "you are not a terminal, OK.\n");
  23.         }
  24.         input = fopen("/dev/tty", "r");
  25.         output = fopen("/dev/tty", "w");
  26.         if(!input || !output) {
  27.                 fprintf(stderr, "Unable to open /dev/tty\n");
  28.                 exit(1);
  29.         }
  30.         do {
  31.                 choice = getchoice("Please select an action", menu, input, output);
  32.                 printf("You have chosen: %c\n", choice);
  33.                 sleep(2);
  34.         } while(choice != 'q');
  35.         exit(0);
  36. }
  37. int getchoice(char *greet, char *choices[], FILE *in, FILE *out)
  38. {
  39.         int chosen = 0;
  40.         int selected;
  41.         int screenrow, screencol = 10;
  42.         char **option;
  43.         char *cursor, *clear;
  44.         output_stream = out;
  45.         setupterm(NULL, fileno(out), (int *)0);/*设置终端*/
  46.         cursor = tigetstr("cup"); /*得到光标位置转义序列*/
  47.         clear = tigetstr("clear"); /*得到clear清屏转义序列*/
  48.         screenrow = 4;
  49.         tputs(clear, 1, char_to_terminal);/*清屏*/
  50.         tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);/*定位光标*/
  51.         fprintf(out, "choice: %s", greet);
  52.         screenrow += 2;
  53.         option = choices;
  54.         while(*option) { /*打印菜单,每次换行都需设置一次*/
  55.                 tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);                  
  56.                 fprintf(out, "%s", *option);
  57.                 screenrow++;
  58.                 option++;
  59.         }
  60.         fprintf(out, "\n");
  61.         do {
  62.                 fflush(out); /*清缓冲区*/
  63.                 selected = fgetc(in);
  64.                 option = choices;
  65.                 while(*option) {  /*检查输入选项*/
  66.                         if(selected == *option[0]) {
  67.                                 chosen = 1;
  68.                                 break;
  69.                         }
  70.                         option++;
  71.         }
  72.         if(!chosen && selected != '\n') { /*遇到回车符不打印出错信息*/
  73.                 tputs(tparm(cursor, screenrow, screencol), 1, char_to_terminal);
  74.                 fprintf(out, "Incorrtct choice, select again\n");
  75.         }
  76.         } while(!chosen);
  77.         tputs(clear, 1, char_to_terminal);
  78.         return selected;
  79. }
  80. /*构造 putchar() 型函数,putc() 相当于 putchar()*/
  81. int char_to_terminal(int char_to_write)
  82. {
  83.         if(output_stream) putc(char_to_write, output_stream);
  84.         return 0;
  85. }
复制代码

对于函数“char_to_terminal”的调用一直有点疑问,定义char_to_terminal的时候是int char_to_terminal (int char_to_write),但是在调用的时候并没有给赋值给char_to_write这个形参。
发表于 2010-7-2 09:03:49 | 显示全部楼层
tputs函数原型:
int tputs(const char *str, int affcnt, int (*putc)(int));
可见第三个参数要求一个函数指针,对char_to_terminal函数形参数值的传递是在函数tputs内部进行的.楼主有太多的基础功课要补......
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表