X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2Finput.c;h=ff9604d516751d4cfa12196db5134a10fab45352;hb=63444196dd1edb154f81d9418b3d0bc2367163e5;hp=2e918ee83cf33b8324a0d0c64b6eabe079bcc89f;hpb=09ed626f25fb3e7c57ad7a59e5261ea005aa498f;p=ap-utils.git diff --git a/lib/input.c b/lib/input.c index 2e918ee..ff9604d 100644 --- a/lib/input.c +++ b/lib/input.c @@ -21,15 +21,17 @@ #include #include +#include +#include #include #include +#include #include "ap-utils.h" -#include "ap-curses.h" -extern WINDOW *main_sub, *win_for_help, *main_win; +extern WINDOW *main_sub, *win_for_help; #define GEN_IV_MSG _("Invalid value. Press any key to continue.") -#define OOR_IV_MSG _("Value must be in range %i - %i. Press any key to continue.") +#define OOR_IV_MSG _("Value must be in range %u - %u. Press any key to continue.") #define MAC_LEN 12 #define MAC_BYTES 6 @@ -40,8 +42,9 @@ get_mac(char *mac, int row, int col) char message[MAC_LEN+1], mess[MAC_LEN/MAC_BYTES+1]; int i; - get_value(message, row, col, MAC_LEN+1, HEX_STRING, 0, 0, NULL); - if (strlen(message) < 12) i = 255; + message[0] = '\0'; + while (strlen(message) != 12) + get_value(message, row, col, MAC_LEN+1, HEX_STRING, 0, 0, NULL); for (i = 0; i < MAC_BYTES; i++) { mess[0] = message[2 * i]; @@ -103,14 +106,15 @@ get_mask_again: * value ....... this should keep the value, entered by the user, * in ascii string notation * row - * col ......... the begin coordinate, relative to the current window, + * col ......... the begin coordinates, relative to the current window, * at which the string entered by the user will appear * len ......... maximum length of the entered string, including the - * tailing '\0' character + * tailing '\0' character. Negative value along with + * vt=ANY_STRING means that zero-length string is allowed. * vt .......... desired input value type (types INT_STRING, HEX_STRING, * ANY_STRING) * minv, maxv .. min/max bounds in case of integer value input. Each can be - * of value 0 - maxint. If both are 0, it means no value bounds + * of value 0-0xFFFFFFFF. If both are 0, it means no value bounds * checking will be performed. Used only with vt=INT_STRING. * helpmsg ..... pointer to string that will be printed after OOR_IV_MSG * message if the entered integer value is out of range. @@ -120,17 +124,22 @@ get_mask_again: */ void get_value(char *value, int row, int col, int len, - char vt, int minv, int maxv, char *helpmsg) + char vt, unsigned int minv, unsigned int maxv, char *helpmsg) { - int i; + unsigned int i; unsigned char c, acs; - char iv_msg[128]; + char iv_msg[128], zerolen_perm = 0; + + if (vt == ANY_STRING && len < 0) { + zerolen_perm = 1; + len = -len; + } echo(); - wattrset(main_sub, COLOR_PAIR(2)); + wattrset(main_sub, COLOR_PAIR(12)); get_value_again: wmove(main_sub, row, col); - for (i = 0; i < len - 1; i++) + for (i = 0; i < (unsigned int)len - 1; i++) waddch(main_sub, ' '); curs_set(1); wmove(main_sub, row, col); @@ -158,19 +167,19 @@ get_value_again: value[i--] = 0; wmove(main_sub, row, col + i); waddch(main_sub, ' '); - /* put backspace to move cursor back */ - wechochar(main_sub, 0x08); + wmove(main_sub, row, col + i); + wrefresh(main_sub); } continue; } else if (c == 0x0D) { /* ENTER KEY */ - if (i > 0) + if (i > 0 || zerolen_perm) break; continue; } else if (c == 0x1B) { /* ESCAPE KEY */ continue; - } else if (acs && (i < len - 1)) { + } else if (acs && (i < (unsigned int)len - 1)) { value[i++] = c; wechochar(main_sub, c); } @@ -179,8 +188,9 @@ get_value_again: noecho(); if (vt == INT_STRING && (minv | maxv)) { - i = atoi(value); - if (i < minv || i > maxv) { + errno = 0; + i = strtoul(value, (char **)NULL, 10); + if (i < minv || i > maxv || errno == ERANGE) { if (helpmsg) { snprintf(iv_msg, sizeof(iv_msg) - 1, OOR_IV_MSG, minv, maxv); print_helperr(iv_msg); @@ -194,7 +204,7 @@ get_value_again: curs_set(0); wattrset(main_sub, A_BOLD); wmove(main_sub, row, col); - for (i = 0; i < len - 1; i++) + for (i = 0; i < (unsigned int)len - 1; i++) waddch(main_sub, ' '); mvwaddstr(main_sub, row, col, value); wattrset(main_sub, A_NORMAL); @@ -211,7 +221,7 @@ get_pass(char *value, int row, int col, int len) { int i, j = 0; - wattrset(main_sub, COLOR_PAIR(2)); + wattrset(main_sub, COLOR_PAIR(12)); wmove(main_sub, row, col); for (i = 0; i < len - 1; i++) waddch(main_sub, ' '); @@ -284,7 +294,7 @@ int menu_choose(int brow, int bcol, char **names, unsigned int num) else choose_menu = newwin(nrow, ncol, brow - nrow + 3, bcol + MCOLS); sub_choose_menu = derwin(choose_menu, nrow - 2, ncol - 2, 1, 1); - attrset(COLOR_PAIR(1)); + attrset(COLOR_PAIR(11)); mvwaddch(choose_menu, 0, 0, ACS_ULCORNER); mvwaddch(choose_menu, 0, ncol - 1, ACS_URCORNER); @@ -353,3 +363,59 @@ int menu_choose(int brow, int bcol, char **names, unsigned int num) } } +/* + * Reads key by either getch() (WAIT_FOREVER mode) or select() + * (WAIT_TIMEOUT mode). Returns -1 upon error, 0 for timeout, or + * pressed key code. + */ +int wait_key() +{ + int i = 0; + fd_set rds; + struct timeval timeout; + extern int wait_mode; + + timeout.tv_sec = 1; + timeout.tv_usec = 0; + FD_ZERO(&rds); + FD_SET(0, &rds); + + if (wait_mode == WAIT_TIMEOUT) { + /* + * wait up to timeout until anything is avail. for reading + * on stdin + */ + i = select(1, &rds, NULL, NULL, &timeout); + + /* not timed out => anything avail. for reading in rds */ + if (i > 0) + i = getc(stdin); + + /* error occured */ + if (i == -1) { + print_helperr(ERR_SELECT); + getch(); + } + + /* also happens: i = 0 => timeout => release */ + + } else { /* wait_mode = WAIT_FOREVER */ + i = getch(); + } + + return i; +} + +int help_ysn() +{ + char c; + + print_help (_("Y - Yes; Any other key - No (it's safer to answer No)")); + c = getch(); + clear_main(0); + if (c == 'y' || c == 'Y') + return 0; + + return 1; +} +