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

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