home *** CD-ROM | disk | FTP | other *** search
- /*
- * Miscellaneous curses routines.
- */
-
- #include <stdio.h>
- #include <curses.h>
- #include <signal.h>
- #include <setjmp.h>
- #ifdef OLDCURSES
- #include <fcntl.h>
- #else /* OLDCURSES */
- #include <term.h>
- #endif /* OLDCURSES */
- #include "misc.h"
-
- /*
- * Get a string from a window. Similar to wgetstr(), except we limit
- * the length, return a NULL (not pointer to NULL) on ESC key, beep
- * at any character in 'disallow' string, and beep at any character not
- * in 'allow'. (It doesn't make sense to use both 'allow' and 'disallow'
- * at the same time)
- */
-
- char *
- get_str(win, num, allow, disallow)
- WINDOW *win;
- int num;
- char *allow, *disallow;
- {
- int count, x, y;
- char ans, *strchr();
- static char buf[80];
-
- count = 0;
- while ((ans = wgetch(win)) != '\r') {
- /* do our own backspace */
- if (ans == 8) {
- if (!count) {
- beep();
- continue;
- }
- count--;
- buf[count] = NULL;
- getyx(win, y, x);
- x--;
- wmove(win, y, x);
- waddch(win, ' ');
- wmove(win, y, x);
- wrefresh(win);
- continue;
- }
- /* an ESC anywhere in the string */
- if (ans == 27)
- return(NULL);
-
- /* illegal character ? */
- if (disallow != NULL && strchr(disallow, ans)) {
- beep();
- continue;
- }
- if (allow != NULL && !strchr(allow, ans)) {
- beep();
- continue;
- }
- /* exceeded the max ? */
- if (count == num) {
- beep();
- continue;
- }
-
- buf[count] = ans;
- waddch(win, ans);
- wrefresh(win);
- count++;
- }
- buf[count] = NULL;
- return(buf);
- }
-
- /*
- * Get a number from a window. We limit the length and return a -1
- * on ESC key.
- */
-
- int
- get_num(win, num)
- WINDOW *win;
- int num;
- {
- int count, x, y, number;
- char ans, buf[80];
-
- count = 0;
- while ((ans = wgetch(win)) != '\r') {
- /* do our own backspace */
- if (ans == 8) {
- if (!count) {
- beep();
- continue;
- }
- count--;
- buf[count] = NULL;
- getyx(win, y, x);
- x--;
- wmove(win, y, x);
- waddch(win, ' ');
- wmove(win, y, x);
- wrefresh(win);
- continue;
- }
- /* an ESC anywhere in the string */
- if (ans == 27)
- return(-1);
- /* only digits are allowed */
- if (ans < '0' || ans > '9') {
- beep();
- continue;
- }
- /* exceeded the max ? */
- if (count == num) {
- beep();
- continue;
- }
-
- buf[count] = ans;
- waddch(win, ans);
- wrefresh(win);
- count++;
- }
- buf[count] = NULL;
- number = atoi(buf);
- return(number);
- }
-
- /*
- * Change video attributes while printing a string. The use of the
- * pre-processor definition NOPROMOTE (located in misc.h) means that
- * strings will be printed without any special video attribute if the
- * requested capability doesn't exist.
- */
-
- wattrstr(win, attr, str)
- WINDOW *win;
- int attr;
- char *str;
- {
- int do_it;
- /* if nothing, do nothing */
- if (str == NULL || *str == NULL)
- return(0);
-
- #ifdef OLDCURSES
- if (attr)
- wstandout(win);
- waddstr(win, str);
- if (attr)
- wstandend(win);
- #else /* OLDCURSES */
- #ifdef NOPROMOTE
- /* does the capability exist ? */
- do_it = 0;
- if ((attr & A_STANDOUT) && enter_standout_mode)
- do_it++;
- if ((attr & A_UNDERLINE) && enter_underline_mode)
- do_it++;
- if ((attr & A_REVERSE) && (enter_reverse_mode || enter_standout_mode))
- do_it++;
- if ((attr & A_BLINK) && enter_blink_mode)
- do_it++;
- if ((attr & A_BOLD) && enter_bold_mode)
- do_it++;
- if ((attr & A_DIM) && enter_dim_mode)
- do_it++;
- #else /* NOPROMOTE */
- do_it = 1;
- #endif /* NOPROMOTE */
-
- if (do_it)
- wattron(win, attr);
- /* print the string */
- waddstr(win, str);
- if (do_it)
- wattroff(win, attr);
- #endif /* OLDCURSES */
- return(0);
- }
-
- /*
- * Change video attributes while printing a character.
- */
-
- wattrch(win, attr, c)
- WINDOW *win;
- int attr;
- char c;
- {
- int do_it;
-
- if (c == NULL)
- return(0);
- #ifdef OLDCURSES
- if (attr)
- wstandout(win);
- waddch(win, c);
- if (attr)
- wstandend(win);
- #else /* OLDCURSES */
- #ifdef NOPROMOTE
- /* does the capability exist ? */
- do_it = 0;
- if ((attr & A_STANDOUT) && enter_standout_mode)
- do_it++;
- if ((attr & A_UNDERLINE) && enter_underline_mode)
- do_it++;
- if ((attr & A_REVERSE) && (enter_reverse_mode || enter_standout_mode))
- do_it++;
- if ((attr & A_BLINK) && enter_blink_mode)
- do_it++;
- if ((attr & A_BOLD) && enter_bold_mode)
- do_it++;
- if ((attr & A_DIM) && enter_dim_mode)
- do_it++;
- #else /* NOPROMOTE */
- do_it = 1;
- #endif /* NOPROMOTE */
-
- if (do_it)
- wattron(win, attr);
- /* print the character */
- waddch(win, c);
- if (do_it)
- wattroff(win, attr);
- #endif /* OLDCURSES */
- return(0);
- }
-
-
- /*
- * Change video attributes while printing a number.
- */
-
- wattrnum(win, attr, num)
- WINDOW *win;
- int attr, num;
- {
- int do_it;
- char buf[20];
-
- sprintf(buf, "%d", num);
-
- #ifdef OLDCURSES
- if (attr)
- wstandout(win);
- waddstr(win, buf);
- if (attr)
- wstandend(win);
- #else /* OLDCURSES */
- #ifdef NOPROMOTE
- /* does the capability exist ? */
- do_it = 0;
- if ((attr & A_STANDOUT) && enter_standout_mode)
- do_it++;
- if ((attr & A_UNDERLINE) && enter_underline_mode)
- do_it++;
- if ((attr & A_REVERSE) && (enter_reverse_mode || enter_standout_mode))
- do_it++;
- if ((attr & A_BLINK) && enter_blink_mode)
- do_it++;
- if ((attr & A_BOLD) && enter_bold_mode)
- do_it++;
- if ((attr & A_DIM) && enter_dim_mode)
- do_it++;
- #else /* NOPROMOTE */
- do_it = 1;
- #endif /* NOPROMOTE */
-
- if (do_it)
- wattron(win, attr);
- /* print the character */
- waddstr(win, buf);
- if (do_it)
- wattroff(win, attr);
- #endif /* OLDCURSES */
- return(0);
- }
-
- /*
- * Prompt for a Yes or No answer. Echo the single key input as words.
- * Handle the funny cursor movement problems with magic cookie terminals.
- * Returns a 1 on yes.
- */
-
- int
- yes_prompt(win, y, x, attr, str)
- WINDOW *win;
- int y, x, attr;
- char *str;
- {
- int save, ret_code;
- char new_str[80], *strcpy(), *strcat();
- /* build and display the prompt */
- strcpy(new_str, str);
- strcat(new_str, "? (y/n):");
- mvwattrstr(win, y, x, attr, new_str);
- wmove(win, y, strlen(new_str)+x+2);
- wrefresh(win);
-
- ret_code = 0;
- save = wgetch(win);
- if (save == 'y' || save == 'Y') {
- waddstr(win, "Yes");
- ret_code = 1;
- }
- else
- waddstr(win, "No");
-
- wrefresh(win);
- return(ret_code);
- }
-
- /*
- * Handy routine for clear-to-end-of-line. Fixes up the box if requested.
- */
-
- int
- clear_line(win, y, x, re_box)
- WINDOW *win;
- int y, x, re_box;
- {
- if (wmove(win, y, x) == ERR)
- return(ERR);
-
- wclrtoeol(win);
-
- if (re_box) {
- mvwaddch(win, y, win->_maxx-1, '|');
- wmove(win, y, x);
- }
- return(0);
- }
-
- /*
- * Wait for a key or time out. Returns a -1 on timeout.
- */
-
- jmp_buf wk_jmp;
-
- int
- wait_key(win, sec)
- WINDOW *win;
- unsigned int sec;
- {
- int c, force_wk();
- unsigned int alarm();
-
- signal(SIGALRM, force_wk);
- if (setjmp(wk_jmp))
- return(-1);
- alarm(sec);
- c = wgetch(win);
- alarm(0);
- return(c);
- }
- int
- force_wk(dummy)
- int dummy;
- {
- void longjmp();
-
- longjmp(wk_jmp, 1);
- }
-
- /*
- * Here are some routines that are probably missing from the older
- * flavors of curses(3)
- */
-
- #ifdef OLDCURSES
- /*
- * Make the terminal bell go off
- */
-
- int
- beep()
- {
- fputc(7, stderr);
- return(0);
- }
-
- /*
- * Fix the stdin so that a read is satisfied immediately. When read()
- * is called it returns the character in the queue, or an error if no
- * key was pressed. The window argument is not used!
- */
-
- int
- nodelay(win, flag)
- WINDOW *win;
- int flag;
- {
- if (flag)
- fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NDELAY);
- else
- fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NDELAY);
- return(0);
- }
-
- /*
- * Take the terminal out of the "curses mode"
- */
-
- int
- resetterm()
- {
- resetty();
- return(0);
- }
-
- /*
- * Put the terminal back into the "curses mode"
- */
-
- int
- myputchar(c)
- char c;
- {
- putchar(c);
- }
- int
- fixterm()
- {
- tputs(TI, 1, myputchar);
- tputs(VS, 1, myputchar);
- nonl();
- crmode();
- noecho();
- return(0);
- }
- #endif /* OLDCURSES */
-