home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / win / tty / getline.c next >
Encoding:
C/C++ Source or Header  |  1992-09-18  |  4.6 KB  |  209 lines

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