Terminal Programming

Ubuntu Korea Community Wiki
imported>Drake님의 2014년 6월 18일 (수) 12:05 판 (새 문서: == 메뉴 == === Basic === <source lang="c"> →‎menu1.c: →‎getchoice 원형 정리: #include <stdio.h> char *menu[] = { "a - add new record", "d - delete record", "q - qui...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)
둘러보기로 이동 검색으로 이동

메뉴

Basic

<source lang="c"> /* menu1.c */ /* getchoice 원형 정리 */

  1. include <stdio.h>

char *menu[] = {

 "a - add new record",
 "d - delete record",
 "q - quit",
 NULL,

};

  • menu를 출력하고 사용자의 입력을 받는다.
  • main은 menu를 사용하여 getchoice를 호출

int getchoice(char *greet, char *choices[]) {

 int chosen = 0;
 int selected;
 char **option;
 do {
   printf("Choice: %s\n", greet);
   option = choices;
   while(*option) {
     printf("%s\n", *option);
     option++;
   }
   selected = getchar();
   option = choices;
   while(*option) {
     if(selected == *option[0]) {
       chosen =1;
       break;
     }
     option++;
   }
   if(!chosen) {
     printf("Incorrect choice, select again\n");
   }
 } while(!chosen);
 return selected;

}

/* main 함수는 menu를 사용하여 getchoice를 호출한다. */ int main() {

 int choice = 0;
 do
 {
   choice = getchoice("Please select an action", menu);
   printf("You have chosen: %c\n", choice);
 } while (choice != 'q');
 return 0;

} </source>

  • 기본적인 메뉴 프로그램이다.문제가 많다

isatty

<source lang="c">

  1. include <unistd.h>

int isatty(int fildes); </source>

  • isatty는 fildes가 터미널이면 1, 아니면 0을 반환한다.

<source lang="c"> /* menu2.c */ /* getchoice 원형 정리 */

  1. include <unistd.h>
  2. include <stdio.h>

char *menu[] = {

 "a - add new record",
 "d - delete record",
 "q - quit",
 NULL,

};

  • menu를 출력하고 사용자의 입력을 받는다.
  • main은 menu를 사용하여 getchoice를 호출

int getchoice(char *greet, char *choices[]) {

 int chosen = 0;
 int selected;
 char **option;
 do {
   printf("Choice: %s\n", greet);
   option = choices;
   while(*option) {
     printf("%s\n", *option);
     option++;
   }
   selected = getchar();
   option = choices;
   while(*option) {
     if(selected == *option[0]) {
       chosen =1;
       break;
     }
     option++;
   }
   if(!chosen) {
     printf("Incorrect choice, select again\n");
   }
 } while(!chosen);
 return selected;

}

int main() {

 int choice = 0;
 if(!isatty(fileno(stdout))) {
   fprintf(stderr, "You are not a terminal!\n");
   return 1;
 }
 do {
   choice = getchoice("Please select an action", menu);
   printf("You have chosen: %c\n", choice);
 } while (choice != 'q');
 return 0;

} </source>

  • 콘솔이 아닐때 에러를 뱉어준다.

<source lang="c">

  1. include <stdio.h>
  2. include <unistd.h>

char *menu[] = {

 "a - add new record",
 "d - delete record",
 "q - quit",
 NULL,

};

int getchoice(char *greet, char *choices[], FILE *in, FILE *out); {

 int chosen = 0;
 int selected;
 char **option;
 do {
   fprintf(out, "Choice: %s\n", greet);
   option = choices;
   while(*option) {
     fprintf(out, "%s\n", *option);
     option++;
   }
   do {
     selected = fgetc(in);
   } while (selected == '\n');
   option = choices;
   while(*option) {
     if(selected == *option[0]) {
       chosen = 1;
       break;
     }
     option++;
   }
   if(!chosen) {
     fprintf(out, "Incorrect choice, select again\n");
   }
 } while(!chosen);
 return selected;

}

int main() {

 int choice = 0;
 FILE *input;
 FILE *output;
 if(!isatty(fileno(stdout))) {
   fprintf(stderr, "You are not a terminal, OK.\n");
 }
 input = fopen("/dev/tty", "r");
 output = fopen("/dev/tty", "w");
 if(!input || !output) {
   fprintf(stderr, "Unable to open /dev/tty\n");
   return 1;
 }
 do {
   choice = getchoice("Please select an action", menu, input, output);
   printf("You have chosen: %c\n", choice);
 } while(choice != 'q');
 return 0;

} </source>

  • 이제 리다이렉션해도 터미널 출력으로만 나오게 된다.