Terminal Programming
메뉴
Basic
<source lang="c"> /* menu1.c */ /* getchoice 원형 정리 */
- 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">
- include <unistd.h>
int isatty(int fildes); </source>
- isatty는 fildes가 터미널이면 1, 아니면 0을 반환한다.
<source lang="c"> /* menu2.c */ /* getchoice 원형 정리 */
- include <unistd.h>
- 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">
- include <stdio.h>
- 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>
- 이제 리다이렉션해도 터미널 출력으로만 나오게 된다.