/*
- * scr.c from Access Point SNMP Utils for Linux
+ * input.c from Access Point SNMP Utils for Linux
* program input & screen related functions
*
* Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
+ * Copyright (c) 2005 Jan Rafaj <jr-aputils at cedric dot unob dot cz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 from
#include <stdlib.h>
#include <string.h>
-#include <sys/time.h>
-#include <sys/types.h>
#include <unistd.h>
#include <menu.h>
#include <errno.h>
void
get_mask(struct in_addr *ip, int row, int col, char *helpmsg)
{
- int i, bit, hmask, trans_count = 0;
+ int i, bit, hmask, trans_count;
get_mask_again:
+ trans_count = 0;
get_ip(ip, row, col, helpmsg);
hmask = ntohl(ip->s_addr);
bit = hmask & 0x00000001;
get_value(char *value, int row, int col, int len,
char vt, unsigned int minv, unsigned int maxv, char *helpmsg)
{
- unsigned int i;
- unsigned char c, acs;
+ int c, i, x, y;
+ unsigned char acs;
char iv_msg[128], zerolen_perm = 0;
+ enum { INSERT, OVERWRITE } mode;
+ WINDOW *gv_win;
if (vt == ANY_STRING && len < 0) {
zerolen_perm = 1;
len = -len;
}
- echo();
- wattrset(main_sub, COLOR_PAIR(12));
+ gv_win = derwin(main_sub, 1, len - 1, row, col);
+ wbkgdset(gv_win, A_REVERSE);
+
get_value_again:
- wmove(main_sub, row, col);
- for (i = 0; i < (unsigned int)len - 1; i++)
- waddch(main_sub, ' ');
+ mode = INSERT;
curs_set(1);
- wmove(main_sub, row, col);
- wrefresh(main_sub);
+ werase(gv_win);
+ wrefresh(gv_win);
- i = 0;
+ i = 0; /* means 'character count' here */
while (1) {
- c = getchar();
+ c = getch();
acs = 0;
switch (vt) {
case INT_STRING:
case ANY_STRING:
acs = 1;
}
- if (c == 0x7F) {
- /* DELETE KEY */
- if (i > 0) {
- value[i--] = 0;
- wmove(main_sub, row, col + i);
- waddch(main_sub, ' ');
- wmove(main_sub, row, col + i);
- wrefresh(main_sub);
- }
- continue;
- } else if (c == 0x0D) {
- /* ENTER KEY */
- if (i > 0 || zerolen_perm)
+ /* rather do not ask, how this works... */
+ getyx(gv_win, y, x);
+ switch (c) {
+ case KEY_BACKSPACE:
+ /* lefthandside DELETE */
+ if (x > 0) {
+ /*
+ * do not step cursor back only if window is filled AND
+ * cursor is totally on RHS
+ */
+ if ((i < len - 1) || (x < len - 2))
+ x--;
+ mvwdelch(gv_win, 0, x);
+ wrefresh(gv_win);
+ i--;
+ }
break;
- continue;
- } else if (c == 0x1B) {
- /* ESCAPE KEY */
- continue;
- } else if (acs && (i < (unsigned int)len - 1)) {
- value[i++] = c;
- wechochar(main_sub, c);
+ case KEY_DC:
+ /* righthandside DELETE */
+ if (i > 0) {
+ if (x == i)
+ x--;
+ mvwdelch(gv_win, 0, x);
+ wrefresh(gv_win);
+ i--;
+ }
+ break;
+ case KEY_LEFT:
+ if (x > 0) {
+ wmove(gv_win, 0, --x);
+ wrefresh(gv_win);
+ }
+ break;
+ case KEY_RIGHT:
+ if (x < i) {
+ wmove(gv_win, 0, ++x);
+ wrefresh(gv_win);
+ }
+ break;
+ case KEY_HOME:
+ wmove(gv_win, 0, 0);
+ wrefresh(gv_win);
+ break;
+ case KEY_END:
+ wmove(gv_win, 0, (i == len - 1 ? i - 1 : i));
+ wrefresh(gv_win);
+ break;
+ case KEY_IC:
+ mode = (mode == INSERT ? OVERWRITE : INSERT);
+ curs_set(mode == INSERT ? 1 : 2);
+ break;
+ case 0x0A:
+ /* ENTER KEY */
+ if (i > 0 || zerolen_perm)
+ goto away;
+
+ break;
+ default:
+ if (acs && (c < 0x100)) {
+ if (mode == INSERT) {
+ if (i < len - 1) {
+ winsch(gv_win, c);
+ i++;
+ wmove(gv_win, 0, ++x);
+ wrefresh(gv_win);
+ }
+ } else {
+ if (i <= len - 1) {
+// wechochar(gv_win, c);
+ waddch(gv_win, c);
+ if (x == i)
+ i++;
+ wmove(gv_win, 0, ++x);
+ wrefresh(gv_win);
+ }
+ }
+ }
}
}
+
+away:
+ wmove(gv_win, 0, 0);
+ winnstr(gv_win, value, i);
value[i] = 0;
+ curs_set(0);
- noecho();
- if (vt == INT_STRING && (minv | maxv)) {
- errno = 0;
+ if (vt == INT_STRING) {
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);
- getch();
- print_help(helpmsg);
+ sprintf(value, "%i", i); /* eat leading zeros */
+ if (minv | maxv) {
+ errno = 0;
+ if ((unsigned)i < minv || (unsigned)i > maxv || errno == ERANGE) {
+ if (helpmsg) {
+ snprintf(iv_msg, sizeof(iv_msg) -1, OOR_IV_MSG, minv, maxv);
+ print_helperr(iv_msg);
+ getch();
+ print_help(helpmsg);
+ }
+ goto get_value_again;
}
- goto get_value_again;
}
}
- curs_set(0);
- wattrset(main_sub, A_BOLD);
- wmove(main_sub, row, col);
- for (i = 0; i < (unsigned int)len - 1; i++)
- waddch(main_sub, ' ');
- mvwaddstr(main_sub, row, col, value);
- wattrset(main_sub, A_NORMAL);
- wrefresh(main_sub);
+ wbkgdset(gv_win, A_BOLD);
+ werase(gv_win);
+ waddstr(gv_win, value);
+ wrefresh(gv_win);
+// mvwprintw(main_sub, 18, 1, "length: %i", i);
+// wrefresh(main_sub);
+ delwin(gv_win);
}
/*
}
/*
- * Reads key by either getch() (WAIT_FOREVER mode) or select()
- * (WAIT_TIMEOUT mode). Returns -1 upon error, 0 for timeout, or
- * pressed key code.
+ * Reads key by either no-delay getch() (WAIT_FOREVER mode) or using
+ * getch() with tval tenths of second timeout (WAIT_TIMEOUT mode).
+ * Returns 0 for timeout, or pressed key code.
*/
-int wait_key()
+int wait_key(int tval)
{
- int i = 0;
- fd_set rds;
- struct timeval timeout;
+ int i;
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();
- }
+ if (wait_mode == WAIT_TIMEOUT)
+ timeout(tval * 100);
- /* also happens: i = 0 => timeout => release */
+ i = getch();
- } else { /* wait_mode = WAIT_FOREVER */
- i = getch();
+ if (wait_mode == WAIT_TIMEOUT) {
+ timeout(-1);
+// nocbreak();
+// cbreak();
}
+ if (i == ERR)
+ i = 0;
+
return i;
}