home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / DESQVIEW / API_EXAM.ZIP / REQUEST.C < prev    next >
Text File  |  1988-04-28  |  8KB  |  314 lines

  1. /****************************************************************
  2. *
  3. *  Name:          REQUEST
  4. *
  5. *  Function:      Provides a front-end for the SERVER program.  
  6. *                 Takes a string entered by the user and sends
  7. *                 requests to the SERVER to either add the string 
  8. *                 to the database or find all records that contain 
  9. *                 the string.
  10. *
  11. *  Shows how to:  1. find a named mailbox.
  12. *                 2. read a key-at-a-time from the keyboard.
  13. *                 3. use an asynchronous notify function to 
  14. *                    detect attempts to close the window.
  15. *
  16. ****************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include "dvapi.h"
  20.  
  21. /* minimum API level required */
  22. #define required 0x201
  23.  
  24. /* public name of file server mailbox */
  25. char mbname[] = "Example File Server";
  26. int lmbname = sizeof(mbname);
  27.  
  28. /* status values for messages to the server */
  29. #define LOGON           1
  30. #define LOGOFF          2
  31. #define ADDREC          3
  32. #define QUERY           4
  33.  
  34. /* status values for messages from the server */
  35. #define LOGGED_ON       1
  36. #define LOGGED_OFF      2
  37. #define READY           3
  38. #define FOUND           4
  39.  
  40. /* keyboard codes returned by key_getc() */
  41. #define BACKSPACE       8
  42. #define ENTER_KEY       13
  43. #define F1_KEY          0x3B+256
  44. #define F2_KEY          0x3C+256
  45.  
  46. /* object handles */
  47. ulong win,kbd,mal,server,obj;
  48.  
  49. /* variables used to receive mail */
  50. char *mptr;
  51. int mlng,status;
  52.  
  53. /* user input buffer and character counter */
  54. char inbuf[80],*inptr;
  55. int nchars;
  56.  
  57. /* message displayed if the server is not present */
  58. char need_server_msg[] = "Please load the SERVER program.\n";
  59.  
  60. /* message displayed once the server acknowledges logon */
  61. char logged_on_msg[] = "\
  62. File server is ready.\n\
  63. To query:        type a search string followed by ENTER.\n\
  64. To add a record: type the record followed by F1.\n\
  65. To auto-query:   type F2.\n";  
  66.  
  67. /* other variables */
  68. int  version,auto_query = 0;
  69.  
  70. int notify_function();
  71.  
  72.  
  73. /**********************************************************************
  74. *  main  -  check for DESQview present and enable required extensions.
  75. ***********************************************************************/
  76.  
  77. main () {
  78.   /* initialize C interfaces and get API version number */
  79.   version = api_init();
  80.  
  81.   /* if DESQview is not running or version is too low, display a message */ 
  82.   if (version < required) {
  83.     printf ("This program requires DESQview version %d.02%d or later.\n",
  84.              required/256,required%256);
  85.     }
  86.  
  87.   /* tell DESQview what extensions to enable and start application */
  88.   else {
  89.     api_level (required);
  90.     program_body();
  91.     }
  92.  
  93.   /* disable C interfaces and return from program */
  94.   api_exit();
  95.   }
  96.  
  97.  
  98. /******************************************************************** 
  99. /*  program_body
  100. /*
  101. /*  Locate the file server.  Sit in a loop sending requests and
  102. /*  processing responses.  Ask for notification when the user tries
  103. /*  to close the window and notify the server before actually closing.
  104. /********************************************************************/ 
  105.  
  106. program_body () {
  107.  
  108.   /* get handles of default objects */
  109.   win = win_me();
  110.   kbd = key_me();
  111.   mal = mal_me();
  112.  
  113.   /* find the server's named mailbox.  If it is not present, prompt the
  114.   /* user to load SERVER.  Loop until found. */
  115.   server = mal_find (mbname,lmbname);
  116.   if (server == 0) win_swrite (win,need_server_msg);
  117.   while (server == 0) {
  118.     api_pause();
  119.     server = mal_find (mbname,lmbname);
  120.     }
  121.  
  122.   /* ask for asynchronous notification on CLOSE requests */
  123.   win_async (win,notify_function);
  124.   win_notify (win,NTF_CLOSE);
  125.  
  126.   /* ask server for logon, wait for reply, and display instructions */
  127.   mal_addto (server,NULL,0,LOGON);
  128.   status = mal_read (mal,&mptr,&mlng);
  129.   win_swrite (win,logged_on_msg);
  130.  
  131.   /* send a request to the server.  Display each message received from the
  132.   /* server until one is received with the READY status.  Loop. */
  133.   while (1) {
  134.     send_request();
  135.     while (1) {
  136.       status = mal_read (mal,&mptr,&mlng);
  137.       if (status == READY) break;
  138.       win_write (win,mptr,mlng);
  139.       win_swrite (win,"\n");
  140.       }
  141.     }
  142.   }
  143.  
  144. /******************************************************************** 
  145. /*  send_request
  146. /*
  147. /*  This function writes a prompt and processes user input.  The easiest
  148. /*  way to take user input is to use an input field and let DESQview
  149. /*  handle entry editing.  We do it manually here to show how keystroke 
  150. /*  mode can be used in situations where fields are inappropriate (such
  151. /*  as when writing a text editor).
  152. /********************************************************************/ 
  153.  
  154. send_request () {
  155.   int k,req,row,col;
  156.  
  157.   /* write the prompt and display the cursor */
  158.   win_swrite (win,"\n? ");
  159.   win_hcur (win);
  160.  
  161.   /* initialize the input buffer variables */
  162.   inptr = inbuf;
  163.   nchars = 0;
  164.  
  165.   /* gather keys until "break" or until 80 characters have been typed */
  166.   while (nchars < 80) {
  167.  
  168.     /* if in auto_query mode and there is no pending keyboard input,
  169.     /* fake a query for the string "time" */
  170.     if (auto_query) {
  171.       if (key_sizeof (kbd) == 0) {
  172.         win_swrite (win,"time");
  173.         strcpy (inbuf,"time\0");
  174.         req = QUERY;
  175.         break;
  176.         }
  177.  
  178.       /* any key typed while in auto_query mode terminates the mode
  179.       /* and is dropped */
  180.       else {
  181.         auto_query = 0;
  182.         k = key_getc (kbd);
  183.         }
  184.       }
  185.  
  186.     /* wait for next key */
  187.     k = key_getc(kbd);
  188.  
  189.     /* if normal key - add to buffer, display, and update cursor */
  190.     if ((k>31) && (k<256)) {
  191.       *inptr++ = k;
  192.       nchars += 1;
  193.       win_putc (win,k,7);
  194.       win_hcur (win);
  195.       }
  196.  
  197.     /* if ENTER - terminate input string and setup to send QUERY message */
  198.     else if (k == ENTER_KEY) {
  199.       *inptr = 0;
  200.       req = QUERY;
  201.       break;
  202.       }                  
  203.  
  204.     /* if F2 - enter auto_query mode */
  205.     else if (k == F2_KEY) {
  206.       auto_query = 1;
  207.       }
  208.  
  209.     /* if F1 - terminate input string and setup to send ADDREC message */
  210.     else if (k == F1_KEY) {
  211.       *inptr = 0;
  212.       req = ADDREC;
  213.       break;
  214.       }
  215.  
  216.     /* if BACKSPACE - backup one char in input buffer and in the window */
  217.     else if (k == BACKSPACE) {
  218.       qry_cursor (win,&row,&col);
  219.       if (col > 2) {
  220.         nchars -= 1;
  221.         inptr -= 1;
  222.         win_cursor (win,row,col-1);
  223.         win_blanks (win,1);
  224.         win_cursor (win,row,col-1);
  225.         win_hcur (win);
  226.         }
  227.       }
  228.     }
  229.  
  230.   /* send the resulting request to the server and write a carriage return
  231.   /* to the window */
  232.   mal_addto (server,inbuf,strlen(inbuf)+1,req);
  233.   win_swrite (win,"\n");
  234.   }
  235.  
  236.  
  237. /******************************************************************** 
  238. /*  notify_function
  239. /*
  240. /*  This function is called as a "software interrupt" whenever the 
  241. /*  window manager sends a notify message.  Since we have only requested
  242. /*  notification on CLOSE requests, we do not need to check the type
  243. /*  of notify.  All we do is send a message to the server requesting 
  244. /*  a logoff and wait for confirmation.  Any mail received in the 
  245. /*  meantime is ignored.  When confirmation arrives, we free our task 
  246. /*  window.
  247. /********************************************************************/ 
  248.  
  249. notify_function () {
  250.   mal_addto (server,NULL,0,LOGOFF);
  251.   while (mal_read (mal,&mptr,&mlng) != LOGGED_OFF) {};
  252.   api_exit();
  253.   win_free (win);
  254.   }
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.