2 * scr.c from Access Point SNMP Utils for Linux
3 * program input & screen related functions
5 * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2 from
9 * June 1991 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "ap-curses.h"
29 extern WINDOW *main_sub, *win_for_help, *main_win;
31 #define GEN_IV_MSG _("Invalid value. Press any key to continue.")
32 #define OOR_IV_MSG _("Value must be in range %i - %i. Press any key to continue.")
38 get_mac(char *mac, int row, int col)
40 char message[MAC_LEN+1], mess[MAC_LEN/MAC_BYTES+1];
43 get_value(message, row, col, MAC_LEN+1, HEX_STRING, 0, 0, NULL);
44 if (strlen(message) < 12) i = 255;
46 for (i = 0; i < MAC_BYTES; i++) {
47 mess[0] = message[2 * i];
48 mess[1] = message[2 * i + 1];
50 mac[i] = strtol(mess, NULL, 16);
57 get_ip(struct in_addr *ip, int row, int col, char *helpmsg)
59 char message[IP_LEN+1], *cp;
63 get_value(message, row, col, IP_LEN+1, ANY_STRING, 0, 0, NULL);
64 for (cp = message, dotc = 0; *cp && (cp = index(cp, '.')); cp++, dotc++);
65 if (dotc < 3 || !(inet_aton(message, ip))) {
67 print_helperr(GEN_IV_MSG);
76 get_mask(struct in_addr *ip, int row, int col, char *helpmsg)
78 int i, bit, hmask, trans_count = 0;
81 get_ip(ip, row, col, helpmsg);
82 hmask = ntohl(ip->s_addr);
83 bit = hmask & 0x00000001;
84 for (i = 1; i < 32; i++)
85 if (((hmask >> i) & 0x00000001) != bit) {
90 if (trans_count > 1) {
91 print_helperr(GEN_IV_MSG);
99 * This is the main input function for all cases when user needs to enter
100 * a visible string. It also performs syntax correctness checks
101 * as well as bounds checks if required.
103 * value ....... this should keep the value, entered by the user,
104 * in ascii string notation
106 * col ......... the begin coordinate, relative to the current window,
107 * at which the string entered by the user will appear
108 * len ......... maximum length of the entered string, including the
109 * tailing '\0' character
110 * vt .......... desired input value type (types INT_STRING, HEX_STRING,
112 * minv, maxv .. min/max bounds in case of integer value input. Each can be
113 * of value 0 - maxint. If both are 0, it means no value bounds
114 * checking will be performed. Used only with vt=INT_STRING.
115 * helpmsg ..... pointer to string that will be printed after OOR_IV_MSG
116 * message if the entered integer value is out of range.
117 * If NULL, then no 'invalid value' and subsequent status line
118 * message will be printed, and whole input process will be
119 * repeated. Used only with vt=INT_STRING.
122 get_value(char *value, int row, int col, int len,
123 char vt, int minv, int maxv, char *helpmsg)
126 unsigned char c, acs;
130 wattrset(main_sub, COLOR_PAIR(2));
132 wmove(main_sub, row, col);
133 for (i = 0; i < len - 1; i++)
134 waddch(main_sub, ' ');
136 wmove(main_sub, row, col);
145 if (c >= '0' && c <= '9') acs = 1;
148 if ((c >= '0' && c <= '9') ||
149 (c >= 'a' && c <= 'f') ||
150 (c >= 'A' && c <= 'F')) acs = 1;
159 wmove(main_sub, row, col + i);
160 waddch(main_sub, ' ');
161 /* put backspace to move cursor back */
162 wechochar(main_sub, 0x08);
165 } else if (c == 0x0D) {
170 } else if (c == 0x1B) {
173 } else if (acs && (i < len - 1)) {
175 wechochar(main_sub, c);
181 if (vt == INT_STRING && (minv | maxv)) {
183 if (i < minv || i > maxv) {
185 snprintf(iv_msg, sizeof(iv_msg) - 1, OOR_IV_MSG, minv, maxv);
186 print_helperr(iv_msg);
190 goto get_value_again;
195 wattrset(main_sub, A_BOLD);
196 wmove(main_sub, row, col);
197 for (i = 0; i < len - 1; i++)
198 waddch(main_sub, ' ');
199 mvwaddstr(main_sub, row, col, value);
200 wattrset(main_sub, A_NORMAL);
205 * Note: indeed we should use get_value() for this action, but for the moment,
206 * we'll settle with this one.
210 get_pass(char *value, int row, int col, int len)
214 wattrset(main_sub, COLOR_PAIR(2));
215 wmove(main_sub, row, col);
216 for (i = 0; i < len - 1; i++)
217 waddch(main_sub, ' ');
221 value[j] = mvwgetch(main_sub, row, col + j);
222 if (value[j] == 0x0A) /* NEWLINE */
224 if (value[j] == 0x1B) /* ESCAPE */
226 if (value[j] != 0x7F) { /* BACKSPACE */
228 mvwaddch(main_sub, row, col + j++, '*');
231 mvwaddch(main_sub, row, col + --j, ' ');
237 wattrset(main_sub, A_BOLD);
238 wmove(main_sub, row, col);
239 for (i = 0; i < len - 1; i++)
240 waddch(main_sub, ' ');
241 wmove(main_sub, row, col);
242 for (i = 0; i < j; i++)
243 waddch(main_sub, '*');
244 wattrset(main_sub, A_NORMAL);
249 yes_no(int brow, int bcol)
251 char *names[2] = { YES, NO };
253 return menu_choose(brow, bcol, names, 2) + 1;
257 on_off(int brow, int bcol)
259 char *names[2] = { ON, OFF };
261 return menu_choose(brow, bcol, names, 2) + 1;
264 int menu_choose(int brow, int bcol, char **names, unsigned int num)
266 unsigned short int c;
267 ITEM **menu_item = calloc(num, sizeof(ITEM)), **ip = menu_item;
269 unsigned int ncol = 2, i, nrow;
270 WINDOW *choose_menu, *sub_choose_menu;
271 extern WINDOW *main_sub;
273 for (i = 0; i < num; i++) {
274 *ip++ = new_item(names[i], "");
275 if (ncol < strlen(names[i]) + 3)
276 ncol = strlen(names[i]) + 3;
282 if (brow + 2 + nrow <= (unsigned int) LINES - 2)
283 choose_menu = newwin(nrow, ncol, brow + 2, bcol + MCOLS);
285 choose_menu = newwin(nrow, ncol, brow - nrow + 3, bcol + MCOLS);
286 sub_choose_menu = derwin(choose_menu, nrow - 2, ncol - 2, 1, 1);
287 attrset(COLOR_PAIR(1));
289 mvwaddch(choose_menu, 0, 0, ACS_ULCORNER);
290 mvwaddch(choose_menu, 0, ncol - 1, ACS_URCORNER);
291 mvwaddch(choose_menu, nrow - 1, 0, ACS_LLCORNER);
292 mvwaddch(choose_menu, nrow - 1, ncol - 1, ACS_LRCORNER);
293 for (i = 1; i < ncol - 1; i++) {
294 mvwaddch(choose_menu, 0, i, ACS_HLINE);
295 mvwaddch(choose_menu, nrow - 1, i, ACS_HLINE);
297 for (i = 1; i < nrow - 1; i++) {
298 mvwaddch(choose_menu, i, 0, ACS_VLINE);
299 mvwaddch(choose_menu, i, ncol - 1, ACS_VLINE);
301 wrefresh(choose_menu);
304 menu1 = new_menu(menu_item);
305 set_menu_win(menu1, choose_menu);
306 set_menu_sub(menu1, sub_choose_menu);
308 set_menu_opts(menu1, O_ONEVALUE);
312 wrefresh(sub_choose_menu);
319 menu_driver(menu1, REQ_NEXT_ITEM);
320 wrefresh(sub_choose_menu);
325 menu_driver(menu1, REQ_LAST_ITEM);
326 wrefresh(sub_choose_menu);
331 menu_driver(menu1, REQ_FIRST_ITEM);
332 wrefresh(sub_choose_menu);
337 menu_driver(menu1, REQ_PREV_ITEM);
338 wrefresh(sub_choose_menu);
341 i = item_index(current_item(menu1));
344 for (c = 0; c < nrow - 2; c++)
345 free_item(menu_item[c]);
346 delwin(sub_choose_menu);