| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | /*
|
| 2 | **
|
| 3 | ** File Description:
|
| 4 | **
|
| 5 | ** basic user interface code...
|
| 6 | **
|
| 7 | **
|
| 8 | */
|
| 9 |
|
| 10 | #include <string.h>
|
| 11 | #include <stdlib.h>
|
| 12 | #include <ctype.h>
|
| 13 |
|
| 14 | #include <string.h>
|
| 15 | #include "user_intf.h"
|
| 16 |
|
| 17 | #define CHAR_BS 8
|
| 18 | #define CHAR_DEL 127
|
| 19 | #define CHAR_NEWLINE 0xA
|
| 20 |
|
| 21 | /*****************************************************************************************************/
|
| 22 | int mislic_get_char_fflush()
|
| 23 | {
|
| 24 | int user_input;
|
| 25 |
|
| 26 | FLUSH_STDOUT;
|
| 27 |
|
| 28 | /* fflush(stdin may not always work.. do this instead */
|
| 29 | do
|
| 30 | {
|
| 31 | user_input = getchar();
|
| 32 | }
|
| 33 | while(iscntrl(user_input));
|
| 34 |
|
| 35 | return user_input;
|
| 36 | }
|
| 37 |
|
| 38 | /*****************************************************************************************************/
|
| 39 |
|
| 40 | /* print a char X times... */
|
| 41 | void print_chars(unsigned int len, char print_char)
|
| 42 | {
|
| 43 | unsigned int i;
|
| 44 | for(i = 0; i < len; i++)
|
| 45 | {
|
| 46 | putchar(print_char);
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | /*****************************************************************************************************/
|
| 51 |
|
| 52 | /* Simple banner creator */
|
| 53 | void print_banner(const char *banner)
|
| 54 | {
|
| 55 | unsigned int len = strlen(banner);
|
| 56 | unsigned int banner_len = ((MISLIC_MAX_COL-len-2) >> 1) -1;
|
| 57 |
|
| 58 | putchar(MISLIC_EOL);
|
| 59 | print_chars( MISLIC_MAX_COL, '*');
|
| 60 | putchar(MISLIC_EOL);
|
| 61 |
|
| 62 | print_chars( banner_len, '*');
|
| 63 | printf( " %s ", banner);
|
| 64 |
|
| 65 | /* For odd number of chars for a banner, add 1 extra asterisks */
|
| 66 | if(len &1)
|
| 67 | {
|
| 68 | putchar('*');
|
| 69 | }
|
| 70 | print_chars( banner_len, '*');
|
| 71 | putchar(MISLIC_EOL);
|
| 72 |
|
| 73 | print_chars( MISLIC_MAX_COL, '*');
|
| 74 | putchar(MISLIC_EOL);
|
| 75 | FLUSH_STDOUT;
|
| 76 | }
|
| 77 |
|
| 78 | /*****************************************************************************************************/
|
| 79 |
|
| 80 | int display_menu(const char *menu_header, const char **menu_items)
|
| 81 | {
|
| 82 | int i;
|
| 83 |
|
| 84 | printf("\n");
|
| 85 | print_banner( menu_header );
|
| 86 |
|
| 87 | for(i = 0; menu_items[i]; i++)
|
| 88 | {
|
| 89 | printf( "%02d) %s%c", i, menu_items[i], MISLIC_EOL);
|
| 90 | }
|
| 91 |
|
| 92 | print_chars(MISLIC_MAX_COL, '*');
|
| 93 | putchar(MISLIC_EOL);
|
| 94 | putchar(MISLIC_EOL);
|
| 95 | FLUSH_STDOUT;
|
| 96 |
|
| 97 | return i;
|
| 98 | }
|
| 99 |
|
| 100 | /*****************************************************************************************************/
|
| 101 | /* Returns back a positive number if did get a valid input (0-N), or N+1 if failed */
|
| 102 | int get_menu_selection(int max_count, int current_channel)
|
| 103 | {
|
| 104 | char buf[80];
|
| 105 | char input;
|
| 106 | int i = 0;
|
| 107 | int selection;
|
| 108 |
|
| 109 | do
|
| 110 | {
|
| 111 | display_menu_prompt(current_channel);
|
| 112 | FLUSH_STDOUT;
|
| 113 |
|
| 114 | *buf = 'r'; /* initialize value to something safe */
|
| 115 |
|
| 116 | do
|
| 117 | {
|
| 118 | input = getchar();
|
| 119 | if( (input == CHAR_BS) || (input == CHAR_DEL) )
|
| 120 | {
|
| 121 | i--;
|
| 122 | }else if (!iscntrl((int)input))
|
| 123 | {
|
| 124 | buf[i++] = input;
|
| 125 | }
|
| 126 | }
|
| 127 | while((input != CHAR_NEWLINE) && (i < 80));
|
| 128 |
|
| 129 | buf[i] = 0;
|
| 130 |
|
| 131 | if ((*buf == 'q') || (*buf == 'Q'))
|
| 132 | {
|
| 133 | return QUIT_MENU;
|
| 134 | }
|
| 135 |
|
| 136 | if ((*buf == 'r') || (*buf == 'R'))
|
| 137 | {
|
| 138 | return REDISPLAY_MENU;
|
| 139 | }
|
| 140 |
|
| 141 | selection = (int)strtol(buf, NULL, 10);
|
| 142 | i = 0;
|
| 143 | }
|
| 144 | while(iscntrl((int)*buf) || (selection > max_count));
|
| 145 |
|
| 146 | return selection;
|
| 147 | }
|
| 148 |
|
| 149 | /*****************************************************************************************************/
|
| 150 |
|
| 151 | int get_int(int min_value, int max_value)
|
| 152 | {
|
| 153 | int user_input;
|
| 154 |
|
| 155 | FLUSH_STDOUT;
|
| 156 |
|
| 157 | do
|
| 158 | {
|
| 159 | fflush(stdin);
|
| 160 | }
|
| 161 | while(scanf("%d", &user_input) != 1);
|
| 162 |
|
| 163 | (void)getchar(); /* Eat the \n */
|
| 164 |
|
| 165 | if( ( user_input < min_value) || (user_input > max_value) )
|
| 166 | {
|
| 167 | return (max_value+1);
|
| 168 | }
|
| 169 |
|
| 170 | return user_input;
|
| 171 |
|
| 172 | }
|
| 173 | /*****************************************************************************************************/
|
| 174 | unsigned int get_hex(unsigned int min_value, unsigned int max_value)
|
| 175 | {
|
| 176 | unsigned long user_input;
|
| 177 |
|
| 178 | FLUSH_STDOUT;
|
| 179 |
|
| 180 | do
|
| 181 | {
|
| 182 | fflush(stdin);
|
| 183 | }
|
| 184 | while(scanf("%lx", &user_input) != 1);
|
| 185 |
|
| 186 | (void)getchar(); /* Eat the \n */
|
| 187 |
|
| 188 | if( ( user_input < min_value) || (user_input > max_value) )
|
| 189 | {
|
| 190 | return (max_value+1);
|
| 191 | }
|
| 192 |
|
| 193 | return user_input;
|
| 194 | }
|
| 195 |
|
| 196 | /*****************************************************************************************************/
|
| 197 |
|
| 198 | /* returns 0 if the character is not found... */
|
| 199 |
|
| 200 | int get_char(char *legal_options, char *user_input)
|
| 201 | {
|
| 202 | *user_input = mislic_get_char_fflush();
|
| 203 | return (strchr(legal_options, *user_input) != 0);
|
| 204 | }
|
| 205 |
|
| 206 | /*****************************************************************************************************/
|
| 207 | /* Look for a particular character range + 'q' for quit */
|
| 208 |
|
| 209 | int get_char_range(char start_range, char end_range)
|
| 210 | {
|
| 211 | int user_input;
|
| 212 |
|
| 213 | user_input = mislic_get_char_fflush();
|
| 214 |
|
| 215 | if( (user_input < start_range) || ((user_input >= (end_range))
|
| 216 | && !(user_input == 'q') ) )
|
| 217 | {
|
| 218 | return -1;
|
| 219 | }
|
| 220 |
|
| 221 | return user_input;
|
| 222 | }
|
| 223 |
|
| 224 | /*****************************************************************************************************/
|
| 225 | /* BUFFER is assumed to be of at least BUFSIZ in allocated space */
|
| 226 | int get_fn(char *prompt, char *buffer)
|
| 227 | {
|
| 228 | printf("%s %s", prompt, MISLIC_PROMPT);
|
| 229 | FLUSH_STDOUT;
|
| 230 | fflush(stdin);
|
| 231 | scanf("%s", buffer);
|
| 232 | (void)getchar(); /* Eat the \n */
|
| 233 | buffer[strlen(buffer)] = 0;
|
| 234 | return 0;
|
| 235 | }
|
| 236 |
|
| 237 | /*****************************************************************************************************/
|
| 238 | int get_prompted_int(int min_value, int max_value, const char *prompt)
|
| 239 | {
|
| 240 | int user_input;
|
| 241 | do
|
| 242 | {
|
| 243 | printf("%s %s", prompt, MISLIC_PROMPT);
|
| 244 | user_input = get_int(min_value, max_value);
|
| 245 | }
|
| 246 | while(user_input > max_value);
|
| 247 | return user_input;
|
| 248 | }
|
| 249 |
|
| 250 | /*****************************************************************************************************/
|
| 251 |
|