home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 4.ddi / C / APYESNO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  3.8 KB  |  135 lines

  1. /**
  2. *
  3. * Name        apyesno -- Display a prompt and accept a YES/NO response
  4. *
  5. * Synopsis    resp = apyesno(pmsg,def_resp);
  6. *
  7. *        int  resp      The value of the response:  0 = NO,
  8. *                  1 = YES
  9. *        char *pmsg      Message to be displayed
  10. *        int  def_resp      Indicator of how the null response is
  11. *                  to be interpreted:
  12. *                  0 = interpret null as NO;
  13. *                  1 = interpret null as YES;
  14. *                  other values reject null responses.
  15. *
  16. * Description    This function displays the message, appends a question
  17. *        mark and a blank ("? "), and then accepts a response.
  18. *        The response must be either "YES", "NO", or the null
  19. *        response (just a carriage return, possibly preceded by
  20. *        spaces and tabs).
  21. *
  22. *        The null response is interpreted according the default
  23. *        setting.  If an unacceptable response is entered, the
  24. *        previous response is erased, the cursor is set to a full
  25. *        box, and the response is requested again.
  26. *
  27. *        Letter case is ignored in the response.  Only the first
  28. *        nonspace character of the response is examined (so, for
  29. *        example, "  yak" is interpreted as "YES").
  30. *
  31. *        See APQUERY for a description of the screen effects of
  32. *        various keystrokes as the response is entered.
  33. *
  34. * Returns    resp          The value of the response:  0 = NO;
  35. *                  1 = YES.
  36. *
  37. * Version    3.0 Copyright (C) Blaise Computing Inc. 1986
  38. *
  39. **/
  40.  
  41. #include <string.h>
  42.  
  43. #include <bapplic.h>
  44. #include <bkeybd.h>
  45. #include <bscreen.h>
  46. #include <bstring.h>
  47.  
  48. #define  LF    ((char) 10)    /* Line feed     (CTRL-J)          */
  49. #define  CR    ((char) 13)    /* Carriage return (CTRL-M)          */
  50.  
  51. #define  YES         1
  52. #define  NO         0
  53.  
  54. int apyesno(pmsg,def_resp)
  55. char *pmsg;
  56. int  def_resp;
  57. {
  58.     int  result;
  59.     int  old_page,mode,columns,act_page;
  60.     int  row,col,end_row,end_col,num_scrolled;
  61.     int  done,scan;
  62.     char response[10];
  63.  
  64.     old_page = b_curpage;          /* Save former display page.    */
  65.     scmode(&mode,&columns,&act_page);
  66.     scpage(act_page);              /* Direct I/O to active page.   */
  67.  
  68.                       /* Write the message.          */
  69.     sccurpos(&row,&col);
  70.     scwrstr(row,col,0,pmsg,-1,-1,CHARS_ONLY + MOVE_CUR + CUR_AFTER);
  71.     sccurpos(&row,&col);
  72.     scwrstr(row,col,2,"? ",-1,-1,CHARS_ONLY + MOVE_CUR + CUR_AFTER);
  73.     sccurpos(&row,&col);
  74.  
  75.                   /* Now "row" and "col" mark the         */
  76.                   /* beginning of the user's response on  */
  77.                   /* the screen.                  */
  78.     do
  79.     {                      /* Get one response.          */
  80.     apquery(response,sizeof(response),&scan,&num_scrolled);
  81.  
  82.     stpcvt(response,RLWHITE + RTWHITE + REDUCE + TOUP);
  83.  
  84.     if (   strlen(response) <= 0
  85.         && (def_resp == YES || def_resp == NO))
  86.     {                  /* Null response is acceptable: */
  87.         result = def_resp;          /* infer default.           */
  88.         done   = 1;
  89.     }
  90.     else
  91.     {                  /* Non-null response:          */
  92.                       /* redisplay and test it.       */
  93.         sccurpos(&end_row,&end_col);
  94.  
  95.         row -= num_scrolled;      /* Compensate for scrolled      */
  96.                       /* screen.              */
  97.  
  98.         sccurset(row,col);          /* Clear response.          */
  99.         scwrite((char) ' ',
  100.               (end_col - col)
  101.             + (columns * (end_row - row)));
  102.  
  103.                       /* Rewrite converted response.  */
  104.         scwrstr(row,col,0,response,-1,-1,  CHARS_ONLY
  105.                          + MOVE_CUR
  106.                          + CUR_AFTER);
  107.         scttywrt(CR,1);
  108.         scttywrt(LF,1);
  109.  
  110.                       /* Validate & extract result.   */
  111.         if (response[0] == 'Y')
  112.         {
  113.         result = YES;
  114.         done   = 1;
  115.         }
  116.         else if (response[0] == 'N')
  117.         {
  118.         result = NO;
  119.         done   = 1;
  120.         }
  121.         else
  122.         {
  123.         sccurset(row,col);    /* Move back to beginning.      */
  124.                       /* Set cursor to full block.    */
  125.         scpgcur(0,0,13,CUR_ADJUST);
  126.         done   = 0;
  127.         }
  128.     }
  129.     } while (!done);              /* Try again.              */
  130.  
  131.     scpage(old_page);              /* Restore current display page */
  132.  
  133.     return result;
  134. }
  135.