home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 06user / getreply.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.2 KB  |  139 lines

  1. /*
  2.  *    getreply -- display a message and wait for a reply
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <memory.h>
  8. #include <ctype.h>
  9. #include <local\std.h>
  10. #include <local\keydefs.h>
  11. #include "linebuf.h"
  12.  
  13. char *
  14. getreply(row, col, width, mesg, lp, size, attr, pg)
  15. short row, col, width;    /* window location and width */
  16. char *mesg;        /* message text */
  17. LINEBUF *lp;        /* line pointer */
  18. short size;        /* size of line buffer */
  19. short attr;        /* video attribute for response field */
  20. short pg;        /* active display page */
  21. {
  22.     int n, k, len;
  23.     short mfw;    /* message field width */
  24.     short rfw;    /* response field width */
  25.     short ccol;    /* visible cursor column */
  26.     int msgflag;    /* non-zero after a message is displayed */
  27.     char *cp;    /* character pointer */
  28.     char *wp;    /* pointer to window start */
  29.     char *tmp;    /* temporary char pointer */
  30.  
  31.     extern int writemsg(short, short, short, char *, char *, short);
  32.  
  33.     /* display the prompt string and calculate response field width */
  34.     putcur(row, col, pg);
  35.     mfw = writemsg(row, col, width, mesg, NULL, pg);
  36.     rfw = width - mfw;
  37.     writea(attr, rfw, pg);
  38.  
  39.     /* collect the user's response */
  40.     memset(lp->l_buf, '\0', size);
  41.     wp = cp = lp->l_buf;
  42.     putcur(row, col + mfw, pg);
  43.     msgflag = 0;
  44.     while ((k = getkey()) != K_RETURN) {
  45.         if (msgflag) {
  46.             /* clear old messages */
  47.             errmsg("");
  48.             putcur(row, ccol, pg);
  49.             msgflag = 0;
  50.         }
  51.         if (isascii(k) && isprint(k)) {
  52.             len = strlen(cp);
  53.             if (cp + len - lp->l_buf < size - 1) {
  54.                 memcpy(cp + 1, cp, len);
  55.                 *cp = k;
  56.                 ++cp;
  57.             }
  58.             else {
  59.                 errmsg("input buffer full");
  60.                 ++msgflag;
  61.             }
  62.         }
  63.         else
  64.             switch (k) {
  65.             case K_LEFT:
  66.                 /* move left one character */
  67.                 if (cp > lp->l_buf)
  68.                     --cp;
  69.                 break;
  70.             case K_RIGHT:
  71.                 /* move right one character */
  72.                 if (*cp != '\0')
  73.                     ++cp;
  74.                 break;
  75.             case K_UP:
  76.                 /* pop a line off the stack */
  77.                 if (lp->l_prev != NULL) {
  78.                     lp = lp->l_prev;
  79.                     wp = cp = lp->l_buf;
  80.                 }
  81.                 break;
  82.             case K_DOWN:
  83.                 /* push a line onto the stack */
  84.                 if (lp->l_next != NULL) {
  85.                     lp = lp->l_next;
  86.                     wp = cp = lp->l_buf;
  87.                 }
  88.                 break;
  89.             case K_HOME:
  90.                 /* beginning of buffer */
  91.                 cp = lp->l_buf;
  92.                 break;
  93.             case K_END:
  94.                 /* end of buffer */
  95.                 while (*cp != '\0')
  96.                     ++cp;
  97.                 break;
  98.             case K_CTRLH:
  99.                 if (cp > lp->l_buf) {
  100.                     tmp = cp - 1;
  101.                     memcpy(tmp, cp, strlen(tmp));
  102.                     --cp;
  103.                 }
  104.                 break;
  105.             case K_DEL:
  106.                 /* delete character at cursor */
  107.                 memcpy(cp, cp + 1, strlen(cp));
  108.                 break;
  109.             case K_ESC:
  110.                 /* cancel current input */
  111.                 lp->l_buf[0] = '\0';
  112.                 putcur(row, col, pg);
  113.                 writec(' ', width, pg);
  114.                 return (NULL);
  115.             default:
  116.                 errmsg("unknown command");
  117.                 ++msgflag;
  118.                 break;
  119.             }
  120.  
  121.         /* adjust the window pointer if necessary */
  122.         if (cp < wp)
  123.             wp = cp;
  124.         else if (cp >= wp + rfw)
  125.             wp = cp + 1 - rfw;
  126.  
  127.         /* display the reply window */
  128.         ccol = col + mfw;
  129.         writemsg(row, ccol, rfw, wp, NULL, pg);
  130.  
  131.         /* reposition the cursor */
  132.         ccol = col + mfw + (cp - wp);
  133.         putcur(row, ccol, pg);
  134.     }
  135.     putcur(row, col, pg);
  136.     writec(' ', width, pg);    /* blank message area */
  137.     return (lp->l_buf);
  138. }
  139.