home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol11n16.zip / CMDCLIP.C < prev    next >
C/C++ Source or Header  |  1992-08-14  |  3KB  |  123 lines

  1. /* 
  2. CMDCLIP.C -- put commands on Windows clipboard, for CLIPSERV
  3.  
  4. Copyright (c) 1992 Ziff Davis Communications
  5. PC Magazine * Andrew Schulman (June 1992)
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <dos.h>
  12. #include "winclip.h"
  13.  
  14. /* Save away contents of clipboard before changing it */
  15. static char *save = 0;
  16.  
  17. /* Before exiting, make sure we restore the saved clip contents */
  18. void cleanup(char *s, int ret) 
  19.     if (s)
  20.         puts(s); 
  21.     if (save)
  22.     {
  23.         PutClipString(save);    /* put it back on clipboard */
  24.         FreeClipString(save);
  25.     }
  26.     exit(ret);
  27. }
  28.  
  29. int Clipserv(void)
  30. {
  31.     char *s;
  32.     int i;
  33.     PutClipString("CMDCLIP INSTCHECK");
  34.     for (i=0; i<5; i++)
  35.     {
  36.         Yield();
  37.         s = GetClipString();
  38.         if (strcmp(s, "CLIPREPLY INSTOK") == 0)
  39.         {
  40.             FreeClipString(s);
  41.             return 1;
  42.         }
  43.         else
  44.             FreeClipString(s);
  45.     }
  46.     /* still here */
  47.     return 0;
  48. }
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.     char buf[256], *s;
  53.     char far *cmdtail;
  54.     int len;
  55.     int i;
  56.  
  57.     if (argc < 2)
  58.         cleanup("usage: cmdclip <clipserv command> [args...]", 1);
  59.     
  60.     /* Make sure we're running under Windows */
  61.     if (! WindowsClipboard())
  62.         cleanup("This program requires Windows Enhanced mode", 1);
  63.     
  64.     /* Save away current contents of clipboard, except if
  65.        it's just an old CMDCLIP request */
  66.     save = GetClipString();
  67.     if (strncmp(save, "CMDCLIP ", 8) == 0)
  68.     {
  69.         FreeClipString(save);
  70.         save = 0;
  71.     }
  72.     
  73.     /* Make sure that CLIPSERV is running:  Clipserv() install check
  74.        works by putting a request into the clipboard and seeing if it
  75.        gets changed in the right away.  If it doesn't, CLIPSERV must
  76.        not be running.  But this test bashes the clipboard, so it
  77.        should only be done AFTER we've saved the clipboard's contents */
  78.     if (! Clipserv())
  79.         cleanup("This program requires Clipserv", 1);
  80.     
  81.     /* Assemble the command/request for CLIPSERV */
  82.     strcpy(buf, "CMDCLIP ");
  83.     cmdtail = MK_FP(_psp, 0x82);
  84.     len = *((unsigned char far *) MK_FP(_psp, 0x80)) - 1;
  85.     _fstrncat(buf, cmdtail, len);
  86.     
  87.     /* Send the request to CLIPSERV */
  88.     PutClipString(buf);
  89.     
  90.     /* if asked server to exit, not going to be a reply! */
  91.     if (strcmp(argv[1], "EXIT") != 0)
  92.     {
  93.         /* Wait for CLIPSERV for reply.  As long as the clipboard 
  94.            contents still match the request we put in there, we know 
  95.            that CLIPSERV hasn't responded yet. Yield() to give
  96.            CLIPSERV a chance. Because 2F/1680 Yield doesn't work so
  97.            good, especially in 3.1 Enhanced mode, Yield (in WINCLIP.C)
  98.            does a few INT 28h Idle calls instead */
  99.         while (strcmp(buf, s = GetClipString()) == 0)
  100.         {
  101.             Yield();
  102.             if (i++ > 10)
  103.                 cleanup("CLIPSERV went down", 1);
  104.             FreeClipString(s);
  105.         }
  106.  
  107.         /* Finally, the clipboard contents have been changed, hopefully
  108.            by CLIPSERV.  (We could check for the case of other programs
  109.            changing the clipboard at just the wrong moment, but this 
  110.            works fine in practice.)  So we display what is hopefully
  111.            CLIPSERV's reply. */
  112.         puts(s);
  113.         
  114.         FreeClipString(s);
  115.     }
  116.  
  117.     /* Free up memory, restore the old clipboard contents, and leave */
  118.     cleanup(NULL, 0);
  119.     return 0;
  120. }
  121.  
  122.