home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Games / NetHack 3.1.3 / source / win / tty / getline.c next >
Encoding:
C/C++ Source or Header  |  1993-08-01  |  5.1 KB  |  229 lines  |  [TEXT/R*ch]

  1. /*    SCCS Id: @(#)getline.c    3.1    92/01/05    */
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6.  
  7. #ifdef TTY_GRAPHICS
  8.  
  9. #include "wintty.h"
  10. #include "func_tab.h"
  11.  
  12. #ifdef OVL1
  13.  
  14. char morc = 0;    /* tell the outside world what char you chose */
  15.  
  16. #endif /* OVL1 */
  17.  
  18. extern char erase_char, kill_char;    /* from appropriate tty.c file */
  19.  
  20. #ifdef OVL1
  21.  
  22. /*
  23.  * Read a line closed with '\n' into the array char bufp[BUFSZ].
  24.  * (The '\n' is not stored. The string is closed with a '\0'.)
  25.  * Reading can be interrupted by an escape ('\033') - now the
  26.  * resulting string is "\033".
  27.  */
  28. void
  29. tty_getlin(query, bufp)
  30. const char *query;
  31. register char *bufp;
  32. {
  33.     register char *obufp = bufp;
  34.     register int c;
  35.     struct WinDesc *cw = wins[WIN_MESSAGE];
  36.     boolean doprev = 0;
  37.  
  38.     if(ttyDisplay->toplin == 1 && !(cw->flags & WIN_STOP)) more();
  39.     cw->flags &= ~WIN_STOP;
  40.     ttyDisplay->toplin = 3; /* special prompt state */
  41.     ttyDisplay->inread++;
  42.     pline("%s ", query);
  43.     for(;;) {
  44.         (void) fflush(stdout);
  45.         if((c = Getchar()) == EOF) {
  46.             *bufp = 0;
  47.             break;
  48.         }
  49.         if(c == '\033') {
  50.             *obufp = c;
  51.             obufp[1] = 0;
  52.             break;
  53.         }
  54.         if (ttyDisplay->intr) {
  55.             ttyDisplay->intr--;
  56.             *bufp = 0;
  57.             putsyms(obufp);
  58.         }
  59.         if(c == '\020') { /* ctrl-P */
  60.             if(!doprev)
  61.             (void) tty_doprev_message(); /* need two initially */
  62.             (void) tty_doprev_message();
  63.             doprev = 1;
  64.             continue;
  65.         } else if(doprev) {
  66.             tty_clear_nhwindow(WIN_MESSAGE);
  67.             cw->maxcol = cw->maxrow;
  68.             doprev = 0;
  69.             addtopl(query);
  70.             addtopl(" ");
  71.             *bufp = 0;
  72.             addtopl(obufp);
  73.         }
  74.         if(c == erase_char || c == '\b') {
  75.             if(bufp != obufp) {
  76.                 bufp--;
  77.                 putsyms("\b \b");/* putsym converts \b */
  78.             } else    tty_nhbell();
  79. #if defined(apollo)
  80.         } else if(c == '\n' || c == '\r') {
  81. #else
  82.         } else if(c == '\n') {
  83. #endif
  84.             *bufp = 0;
  85.             break;
  86.         } else if(' ' <= c && c < '\177' && 
  87.                 (bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)) {
  88.                 /* avoid isprint() - some people don't have it
  89.                    ' ' is not always a printing char */
  90.             *bufp = c;
  91.             bufp[1] = 0;
  92.             putsyms(bufp);
  93.             bufp++;
  94.         } else if(c == kill_char || c == '\177') { /* Robert Viduya */
  95.                 /* this test last - @ might be the kill_char */
  96.             while(bufp != obufp) {
  97.                 bufp--;
  98.                 putsyms("\b \b");
  99.             }
  100.         } else
  101.             tty_nhbell();
  102.     }
  103.     ttyDisplay->toplin = 2;        /* nonempty, no --More-- required */
  104.     ttyDisplay->inread--;
  105.     clear_nhwindow(WIN_MESSAGE);    /* clean up after ourselves */
  106. }
  107.  
  108. void
  109. xwaitforspace(s)
  110. register const char *s;    /* chars allowed besides space or return */
  111. {
  112.     register int c;
  113.  
  114.     morc = 0;
  115.  
  116.     while((c = tty_nhgetch()) != '\n') {
  117.     if(flags.cbreak) {
  118.         if(c == ' ') break;
  119.         if(s && index(s,c)) {
  120.         morc = c;
  121.         break;
  122.         }
  123.         tty_nhbell();
  124.     }
  125.     }
  126.  
  127. }
  128.  
  129. #endif /* OVL1 */
  130. #ifdef OVL2
  131.  
  132. #ifdef COM_COMPL
  133. /* Read in an extended command - doing command line completion for
  134.  * when enough characters have been entered to make a unique command.
  135.  * This is just a modified getlin().   -jsb
  136.  */
  137. void
  138. tty_get_ext_cmd(bufp)
  139. register char *bufp;
  140. {
  141.     register char *obufp = bufp;
  142.     register int c;
  143.     int com_index, oindex;
  144.  
  145.     ttyDisplay->toplin = 3; /* special prompt state */
  146.     ttyDisplay->inread++;
  147.     pline("# ");
  148.  
  149.     for(;;) {
  150.         (void) fflush(stdout);
  151.         if((c = readchar()) == EOF) {
  152.             *bufp = 0;
  153.             break;
  154.         }
  155.         if(c == '\033') {
  156.             *obufp = c;
  157.             obufp[1] = 0;
  158.             break;
  159.         }
  160.         if (ttyDisplay->intr) {
  161.             ttyDisplay->intr--;
  162.             *bufp = 0;
  163.             putsyms(obufp);
  164.         }
  165.         if(c == erase_char || c == '\b') {
  166.             if(bufp != obufp) {
  167.                 bufp--;
  168.                 putsyms("\b \b"); /* putsym converts \b */
  169.             } else    tty_nhbell();
  170. #if defined(apollo)
  171.         } else if(c == '\n' || c == '\r') {
  172. #else
  173.         } else if(c == '\n') {
  174. #endif
  175.             *bufp = 0;
  176.             break;
  177.         } else if(' ' <= c && c < '\177' &&
  178.                 (bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)) {
  179.                 /* avoid isprint() - some people don't have it
  180.                    ' ' is not always a printing char */
  181.             *bufp = c;
  182.             bufp[1] = 0;
  183.             oindex = 0;
  184.             com_index = -1;
  185.  
  186.             while(extcmdlist[oindex].ef_txt != NULL){
  187.                 if(!strncmpi(obufp, extcmdlist[oindex].ef_txt,
  188.                     strlen(obufp)))
  189.                     if(com_index == -1) /* No matches yet*/
  190.                         com_index = oindex;
  191.                     else /* More than 1 match */
  192.                         com_index = -2;
  193.                 oindex++;
  194.             }
  195.             if(com_index >= 0){
  196.                 Strcpy(obufp, extcmdlist[com_index].ef_txt);
  197.                 /* finish printing our string */
  198.                 putsyms(bufp);
  199.                 bufp = obufp; /* reset it */
  200.                 if((int)strlen(obufp) < BUFSZ-1 &&
  201.                         (int)strlen(obufp) < COLNO)
  202.                     /* set bufp at the end of our string */
  203.                     bufp += strlen(obufp);
  204.             } else {
  205.                 putsyms(bufp);
  206.                 if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
  207.                     bufp++;
  208.             }
  209.         } else if(c == kill_char || c == '\177') { /* Robert Viduya */
  210.                 /* this test last - @ might be the kill_char */
  211.             while(bufp != obufp) {
  212.                 bufp--;
  213.                 putsyms("\b \b");
  214.             }
  215.         } else
  216.             tty_nhbell();
  217.     }
  218.     ttyDisplay->toplin = 2;        /* nonempty, no --More-- required */
  219.     ttyDisplay->inread--;
  220.     clear_nhwindow(WIN_MESSAGE);    /* clean up after ourselves */
  221. }
  222. #endif /* COM_COMPL */
  223.  
  224. #endif /* OVL2 */
  225.  
  226. #endif /* TTY_GRAPHICS */
  227.  
  228. /*getline.c*/
  229.