#include <stdlib.h>
#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
#include <unistd.h>
#include <menu.h>
+#include <errno.h>
#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
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];
* 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.
*/
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);
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);
}
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);
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);
{
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, ' ');
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);
}
}
+/*
+ * 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;
+}
+