home *** CD-ROM | disk | FTP | other *** search
- /*
- * Example program --
- * A possible replacement for curses' wgetstr(). Allows
- * limited editing. Recognizes the erase and kill chars.
- *
- * By Hung Le (mott@ucscb.ucsc.edu ...!ucbvax!ucscc!ucscb!mott)
- * History:
- * 01/18/89 - Initial version
- * 03/20/89 - add control-w to delete word
- *
- * Release to Public Domain
- */
-
- #include <curses.h>
- #include <signal.h>
- /* REMEMBER to include "my_wgetstr.h" */
- #include "my_wgetstr.h"
-
- /* test my_wgetstr() */
- main()
- {
- char buffer[30];
- static char *header[] =
- {
- "INPUT WINDOW",
- "Look Ma ... Line editing in curses",
- "Erase char to erase, Kill char to delete the whole line",
- "control-w to delete a word, empty input line to quit",
- };
- char ender[81], *name, *getlogin(), *getenv();
- int clean_up(), n;
- int x_pos, y_pos;
- WINDOW *inwin;
-
- initscr();
- clear();
- refresh();
-
- /* set up interupt handler */
- signal(SIGINT, clean_up);
-
- /* set up input window */
- inwin = newwin(8, COLS, 3, 0);
- if (inwin == (WINDOW *) NULL)
- clean_up();
-
- /* print out header */
- box(inwin, '|', '-');
- for (n = 0; n < 4; n++)
- mvwaddstr(inwin, n + 1, COLS / 2 - strlen(header[n]) / 2, header[n]);
- mvwaddstr(inwin, 6, 15, "Enter input --> ");
- /* get the prompt positions so we can return at later time */
- getyx(inwin, y_pos, x_pos);
- wrefresh(inwin);
-
- /* now get string */
- do
- {
- /* clear last input */
- wmove(inwin, y_pos, x_pos);
- wclrtoeol(inwin);
- box(inwin, '|', '-');
- wrefresh(inwin);
-
- /*
- * get string. NO_RET=0 and RET=1 are defined in
- * "my_wgetstr.h"
- */
- n = my_wgetstr(inwin, buffer, sizeof(buffer), NO_RET);
-
- /* print out the result of my_wgetstr */
- move(15, 15);
- clrtoeol();
- mvprintw(15, 15, "Received %d chars -- \"%s\"", n, buffer);
- refresh();
-
- /* move back to the input prompt */
- wmove(inwin, y_pos, x_pos);
- wrefresh(inwin);
- }
- while (n != 0); /* while input is not empty */
-
- name = getenv("NAME");
- if (name == (char *) NULL) /* use login if NAME is not set */
- name = getlogin();
- /* say good bye */
- move(15, 15);
- clrtoeol();
- sprintf(ender, "GOOD BYE, \"%s\"", name);
- mvprintw(15, COLS / 2 - strlen(ender) / 2, "%s", ender);
-
- /* move to last line ... looks cleaner this way */
- move(LINES - 1, 0);
- refresh();
- clean_up();
- }
-
- clean_up()
- {
- endwin();
- exit();
- }
-