home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name wnquery -- Return a string from the operator.
- *
- * Synopsis final = wnquery(presponse,resp_size,pscan);
- *
- * char final Character that terminated input, or
- * NUL ('\0') if an extended key
- * sequence terminated the input.
- * char *presponse Pointer to character buffer in which
- * to put operator's input.
- * int resp_size Size of input buffer in bytes (including
- * one byte for the trailing NUL ('\0')).
- * int *pscan If the terminal character was an ASCII
- * character, this is its value;
- * if it was an extended key
- * sequence, this is its scan code.
- *
- * Description This function returns a string of keystrokes from the
- * user, echoing keystrokes and confining all I/O to the
- * current window.
- *
- * Previous pending output to the window is completed and
- * all keystrokes are echoed to the window even though the
- * window may be designated "delayed". If the window was
- * "delayed" before WNQUERY is called, this status is
- * restored on exit.
- *
- * The user may type characters as usual, terminating the
- * input by one of the following: ENTER, CTRL-M, CTRL-J,
- * or a function key or other extended key sequence. The
- * characters are echoed and the cursor moves across the
- * window. When the rightmost column of the window is
- * reached, the cursor moves to the next row, except that
- * input stops when the lower right corner of the window is
- * reached. The input also stops when the buffer is full.
- *
- * (The cursor is only visible if the current window is the
- * one (among all the windows on this display page) that is
- * selected to have an active cursor (via WNCURSOR) and if
- * this window's own cursor is on.)
- *
- * Once the input has stopped (because the end of the
- * window is reached or the buffer is full), the cursor
- * remains just after the last response character. Further
- * keystrokes (except for backspace and terminating keys)
- * cause beeps and are discarded. Backspace works as
- * usual. The function does not return until ENTER or
- * another terminating key has been pressed (even if the
- * input has stopped).
- *
- * The terminating character is not returned as part of the
- * input. If the terminating character is an ASCII
- * character, its value is returned in *pscan and as the
- * value of the function; if it was an extended key
- * sequence, its scan code is returned in *pscan and the
- * value of the function is zero.
- *
- * An error occurs if no window is designated current, if
- * any portion of the current window's data area is covered
- * by another window, or if the current window is not
- * currently displayed. In event of error, the function
- * returns immediately without displaying the message, 0 is
- * returned in *pscan and as the value of the function, and
- * *presponse is made the null string ("").
- *
- * Special The following keystrokes have special effects when
- * characters entered as part of the response:
- *
- * Backspace or CTRL-H ('\b') deletes the previous
- * character entered and moves the cursor to that
- * character's location. A backspace at the beginning of
- * the response causes a beep.
- *
- * NULs (ASCII 0) are ignored and discarded.
- *
- * CTRL-G causes a beep but is not returned as part of the
- * response.
- *
- * ENTER or CTRL-M ('\15') terminates the response.
- *
- * CTRL-J ('\12') terminates the response.
- *
- * Special non-ASCII IBM keys (such as function keys and
- * some ALT keys) terminate the response.
- *
- * All other ASCII characters, including control
- * characters, are echoed. (Control characters are echoed
- * as themselves, that is, without an uparrow ('^')
- * prefix.)
- *
- * Returns final Character that terminated input, or
- * NUL ('\0') if an extended key
- * sequence terminated the input.
- * *pscan ASCII code or extended code of
- * keystroke that terminated input.
- * *presponse User's response as a string.
- * b_wnerr Possible values:
- * (No change) Success.
- * WN_BAD_WIN No window designated
- * as current.
- * WN_NOT_SHOWN Window not displayed.
- * WN_COVERED Data area covered.
- * WN_ILL_DIM Internal error.
- * WN_BAD_DEV Internal error.
- * WN_NULL_PTR Internal error.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1986
- *
- * Version 3.02 March 24, 1987
- * Forced WNUPDATE in every case (previously, we would call
- * WNUPDATE only if the "delayed" flag was set; but
- * that would be inproper if the flag was cleared while
- * the window was dirty).
- *
- **/
-
- #include <string.h>
-
- #include <bkeybd.h>
- #include <bwindow.h>
-
- #define BEL ((int) '\7' ) /* Bell, CTRL-G */
- #define BS ((int) '\b' ) /* Backspace, CTRL-H */
- #define LF ((int) '\12') /* Linefeed, CTRL-J */
- #define ENTER ((int) '\15') /* ENTER, CTRL-M */
-
- char wnquery(presponse,resp_size,pscan)
- char *presponse;
- int resp_size,*pscan;
- {
- int was_delayed;
- char ch;
- int is_char;
- int max_row,max_col,row,col;
- int num_have;
- static char blank = ' ';
-
- *presponse = '\0'; /* Zeros in case of error. */
- *pscan = 0;
-
- if (wnvalwin(b_pcurwin) == NIL) /* Validate window. */
- {
- wnerror(WN_BAD_WIN);
- return '\0';
- }
- /* Make sure it's visible. */
- if ( b_pcurwin->where_shown.dev != MONO
- && b_pcurwin->where_shown.dev != COLOR)
- {
- wnerror(WN_NOT_SHOWN);
- return '\0';
- }
- /* Make sure we can write to it.*/
- if (b_pcurwin->internals.frozen)
- {
- wnerror(WN_COVERED);
- return '\0';
- }
-
- was_delayed = b_pcurwin->options.delayed;
-
- if (wnupdate(b_pcurwin) == NIL) /* Complete pending writes*/
- return '\0';
- b_pcurwin->options.delayed = 0; /* Allow immediate I/O. */
-
- max_row = b_pcurwin->img.dim.h - 1;
- max_col = b_pcurwin->img.dim.w - 1;
- num_have = 0;
-
- do /* Collect the response. */
- {
- /* Await a keystroke. */
- if (is_char = kbin(pscan))
- { /* Got an ASCII character. */
- wncurpos(&row,&col);
-
- switch (ch = (char) *pscan)
- {
- case LF:
- case ENTER:
- break; /* This is a terminating */
- /* character: handle it at */
- /* loop exit. */
-
- case '\0': /* Ignore NULs. */
- break;
-
- case BEL: /* BEL: just beep. */
- wnwrtty((char) BEL,-1,-1);
- break;
-
- default:
- if ( num_have >= resp_size - 1 /* Buffer full */
- || ( row >= max_row
- && col >= max_col)) /* End of window */
- {
- wnwrtty((char) BEL,-1,-1);
- }
- else
- { /* Echo char & advance cursor. */
- wnwrtty(ch,-1,-1);
- /* Update response buffer. */
- num_have++;
- *presponse++ = ch;
- }
- break;
-
- case BS:
- if (num_have)
- { /* Move cursor to last char */
- /* entered. */
-
- if (col) /* Back up one column. */
- wncurmov(row,--col);
- else /* Back up to previous row. */
- wncurmov(--row,col = max_col);
-
- /* Erase previous character. */
- wnwrbuf(row,col,1,&blank,-1,-1,
- CUR_BEG | CHARS_ONLY | MOVE_CUR);
- num_have--;
- presponse--;
- }
- else
- wnwrtty((char) BEL,-1,-1); /* Can't delete */
- /* past beginning. */
- break;
- }
- }
- } while (is_char && (ch != (char) ENTER) && (ch != (char) LF));
-
- *presponse = '\0'; /* Terminate response string. */
-
- /* Restore "delayed" state. */
- b_pcurwin->options.delayed = was_delayed;
-
- return is_char ? ch : (char) '\0';/* Successful exit. */
- }