| /* | |
| ** | |
| ** File Description: | |
| ** | |
| ** basic user interface code... | |
| ** | |
| ** | |
| */ | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include <ctype.h> | |
| #include <string.h> | |
| #include "user_intf.h" | |
| #define CHAR_BS 8 | |
| #define CHAR_DEL 127 | |
| #define CHAR_NEWLINE 0xA | |
| /*****************************************************************************************************/ | |
| int mislic_get_char_fflush() | |
| { | |
| int user_input; | |
| FLUSH_STDOUT; | |
| /* fflush(stdin may not always work.. do this instead */ | |
| do | |
| { | |
| user_input = getchar(); | |
| } | |
| while(iscntrl(user_input)); | |
| return user_input; | |
| } | |
| /*****************************************************************************************************/ | |
| /* print a char X times... */ | |
| void print_chars(unsigned int len, char print_char) | |
| { | |
| unsigned int i; | |
| for(i = 0; i < len; i++) | |
| { | |
| putchar(print_char); | |
| } | |
| } | |
| /*****************************************************************************************************/ | |
| /* Simple banner creator */ | |
| void print_banner(const char *banner) | |
| { | |
| unsigned int len = strlen(banner); | |
| unsigned int banner_len = ((MISLIC_MAX_COL-len-2) >> 1) -1; | |
| putchar(MISLIC_EOL); | |
| print_chars( MISLIC_MAX_COL, '*'); | |
| putchar(MISLIC_EOL); | |
| print_chars( banner_len, '*'); | |
| printf( " %s ", banner); | |
| /* For odd number of chars for a banner, add 1 extra asterisks */ | |
| if(len &1) | |
| { | |
| putchar('*'); | |
| } | |
| print_chars( banner_len, '*'); | |
| putchar(MISLIC_EOL); | |
| print_chars( MISLIC_MAX_COL, '*'); | |
| putchar(MISLIC_EOL); | |
| FLUSH_STDOUT; | |
| } | |
| /*****************************************************************************************************/ | |
| int display_menu(const char *menu_header, const char **menu_items) | |
| { | |
| int i; | |
| printf("\n"); | |
| print_banner( menu_header ); | |
| for(i = 0; menu_items[i]; i++) | |
| { | |
| printf( "%02d) %s%c", i, menu_items[i], MISLIC_EOL); | |
| } | |
| print_chars(MISLIC_MAX_COL, '*'); | |
| putchar(MISLIC_EOL); | |
| putchar(MISLIC_EOL); | |
| FLUSH_STDOUT; | |
| return i; | |
| } | |
| /*****************************************************************************************************/ | |
| /* Returns back a positive number if did get a valid input (0-N), or N+1 if failed */ | |
| int get_menu_selection(int max_count, int current_channel) | |
| { | |
| char buf[80]; | |
| char input; | |
| int i = 0; | |
| int selection; | |
| do | |
| { | |
| display_menu_prompt(current_channel); | |
| FLUSH_STDOUT; | |
| *buf = 'r'; /* initialize value to something safe */ | |
| do | |
| { | |
| input = getchar(); | |
| if( (input == CHAR_BS) || (input == CHAR_DEL) ) | |
| { | |
| i--; | |
| }else if (!iscntrl((int)input)) | |
| { | |
| buf[i++] = input; | |
| } | |
| } | |
| while((input != CHAR_NEWLINE) && (i < 80)); | |
| buf[i] = 0; | |
| if ((*buf == 'q') || (*buf == 'Q')) | |
| { | |
| return QUIT_MENU; | |
| } | |
| if ((*buf == 'r') || (*buf == 'R')) | |
| { | |
| return REDISPLAY_MENU; | |
| } | |
| selection = (int)strtol(buf, NULL, 10); | |
| i = 0; | |
| } | |
| while(iscntrl((int)*buf) || (selection > max_count)); | |
| return selection; | |
| } | |
| /*****************************************************************************************************/ | |
| int get_int(int min_value, int max_value) | |
| { | |
| int user_input; | |
| FLUSH_STDOUT; | |
| do | |
| { | |
| fflush(stdin); | |
| } | |
| while(scanf("%d", &user_input) != 1); | |
| (void)getchar(); /* Eat the \n */ | |
| if( ( user_input < min_value) || (user_input > max_value) ) | |
| { | |
| return (max_value+1); | |
| } | |
| return user_input; | |
| } | |
| /*****************************************************************************************************/ | |
| unsigned int get_hex(unsigned int min_value, unsigned int max_value) | |
| { | |
| unsigned long user_input; | |
| FLUSH_STDOUT; | |
| do | |
| { | |
| fflush(stdin); | |
| } | |
| while(scanf("%lx", &user_input) != 1); | |
| (void)getchar(); /* Eat the \n */ | |
| if( ( user_input < min_value) || (user_input > max_value) ) | |
| { | |
| return (max_value+1); | |
| } | |
| return user_input; | |
| } | |
| /*****************************************************************************************************/ | |
| /* returns 0 if the character is not found... */ | |
| int get_char(char *legal_options, char *user_input) | |
| { | |
| *user_input = mislic_get_char_fflush(); | |
| return (strchr(legal_options, *user_input) != 0); | |
| } | |
| /*****************************************************************************************************/ | |
| /* Look for a particular character range + 'q' for quit */ | |
| int get_char_range(char start_range, char end_range) | |
| { | |
| int user_input; | |
| user_input = mislic_get_char_fflush(); | |
| if( (user_input < start_range) || ((user_input >= (end_range)) | |
| && !(user_input == 'q') ) ) | |
| { | |
| return -1; | |
| } | |
| return user_input; | |
| } | |
| /*****************************************************************************************************/ | |
| /* BUFFER is assumed to be of at least BUFSIZ in allocated space */ | |
| int get_fn(char *prompt, char *buffer) | |
| { | |
| printf("%s %s", prompt, MISLIC_PROMPT); | |
| FLUSH_STDOUT; | |
| fflush(stdin); | |
| scanf("%s", buffer); | |
| (void)getchar(); /* Eat the \n */ | |
| buffer[strlen(buffer)] = 0; | |
| return 0; | |
| } | |
| /*****************************************************************************************************/ | |
| int get_prompted_int(int min_value, int max_value, const char *prompt) | |
| { | |
| int user_input; | |
| do | |
| { | |
| printf("%s %s", prompt, MISLIC_PROMPT); | |
| user_input = get_int(min_value, max_value); | |
| } | |
| while(user_input > max_value); | |
| return user_input; | |
| } | |
| /*****************************************************************************************************/ | |