home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / CLIPBRD.CMD < prev    next >
OS/2 REXX Batch file  |  1994-10-03  |  8KB  |  238 lines

  1. EXTPROC CEnvi2
  2. //******************************************
  3. //*** ClipBrd - Command-line access to   ***
  4. //*** ver.1     clipboard text functions ***
  5. //******************************************
  6.  
  7. #include <ClipBrd.lib>
  8.  
  9. main(argc,argv)
  10. {
  11.    Cmd = argv[1];
  12.    Text = argv[2];
  13.    success = False;  // assume failure
  14.    if ( argc == 2 ) {
  15.       if      ( !stricmp(Cmd,"DELETE") )     success = DeleteClipBrd();
  16.       else if ( !stricmp(Cmd,"GET") )        success = GetClipBrd();
  17.       else if ( !stricmp(Cmd,"ISORT") )      success = SortClipBrd(False);
  18.       else if ( !stricmp(Cmd,"LOWER") )      success = LowercaseClipBrd();
  19.       else if ( !stricmp(Cmd,"QUERY") )      success = QueryClipBrd();
  20.       else if ( !stricmp(Cmd,"SORT") )       success = SortClipBrd(True);
  21.       else if ( !stricmp(Cmd,"TEXT") )       success = TextOnlyInClipBrd();
  22.       else if ( !stricmp(Cmd,"UPPER") )      success = UppercaseClipBrd();
  23.       else Instructions();
  24.    } else if ( argc == 3 ) {
  25.       if ( !stricmp(Cmd,"GET") && Text[0] == '@' )
  26.          success = GetClipBrd(Text+1);
  27.       else {
  28.          if ( Text[0] == '@' ) {
  29.             // Get text from FileSpec
  30.             Text = ReadTextFromFile(Text+1);
  31.          }
  32.          if      ( !stricmp(Cmd,"APPEND") )     success = AppendToClipBrd(Text);
  33.          else if ( !stricmp(Cmd,"FIND") )       success = FindInClipboard(Text,True);
  34.          else if ( !stricmp(Cmd,"IFIND") )      success = FindInClipboard(Text,False);
  35.          else if ( !stricmp(Cmd,"PREPEND") )    success = PrependToClipBrd(Text);
  36.          else if ( !stricmp(Cmd,"PUT") )        success = PutInClipBrd(Text);
  37.          else Instructions();
  38.       }
  39.    } else
  40.       Instructions();
  41.    return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
  42. }
  43.  
  44. Instructions()
  45. {
  46.    puts("\a")
  47.    puts("SYNTAX: ClipBrd [ DELETE | GET | ISORT | LOWER | QUERY | SORT | TEXT | UPPER ]")
  48.    puts("    or  ClipBrd [ APPEND | FIND | IFIND | PREPEND | PUT ] <text | @<file>>")
  49.    puts("    or  ClipBrd GET @<file>")
  50.    puts("")
  51.    puts("Where: APPEND - Append new text to end of existing clipboard")
  52.    puts("       DELETE - Delete contents of the clipboard")
  53.    puts("       FIND - Find case-sensitive string in clipboard")
  54.    puts("       GET - Display contents of clipboard; or write to @<file>")
  55.    puts("       IFIND - Find case-insensitive string in clipboard")
  56.    puts("       ISORT - Case-insenstive, alphabetic sort of clipboard contents")
  57.    puts("       PREPEND - Prepend new text to beginning of existing clipboard")
  58.    puts("       PUT - Put text in clipboard")
  59.    puts("       QUERY - Does clipboard contain text?")
  60.    puts("       SORT - Case-sensitive, alphabetic sort of clipboard contents")
  61.    puts("       TEXT - Remove all clipboard types except for text")
  62.    puts("       @<FILE> - file specification to get text from/copy-to")
  63.    puts("")
  64.    printf("Return: Return ERRORLEVEL %d if success, and %d if error. QUERY, FIND, and\n",EXIT_SUCCESS,EXIT_FAILURE)
  65.    printf("        IFIND return %d if found, and %d if not found\n",EXIT_SUCCESS,EXIT_FAILURE)
  66.    puts("");
  67.    puts(`Examples: ClipBrd DELETE                   Delete clipboard contents`)
  68.    puts(`          ClipBrd GET @CLIP.TXT            Save to file CLIP.TXT`)
  69.    puts(`          ClipBrd Append "Little bunny."   Add text to clipboard`)
  70.    puts(`          ClipBrd GET @\\DEV\\PRN          Send clipboard to printer`)
  71. }
  72.  
  73. GetClipboardText(PrintError)   // return clipboard text; message and NULL if none
  74. {
  75.    lText = GetClipboardData(CF_TEXT);
  76.    if ( !lText && PrintError )
  77.       puts("No text in the clipboard.\n");
  78.    return lText;
  79. }
  80.  
  81. PutClipboardText(pText) // put text in clipboard; message and False if none
  82. {
  83.    success = PutClipboardData(pText,1+strlen(pText),CF_TEXT);
  84.    if ( !success )
  85.       puts("Erroring copying text to the clipboard.\n");
  86.    return success;
  87. }
  88.  
  89. ReadTextFromFile(pFileSpec)   // return string; else exit
  90. {
  91.    if ( !(fp = fopen(pFileSpec,"rb")) ) {
  92.       printf("Unable to open file \%s\" for reading.\n",pFileSpec);
  93.       exit(EXIT_FAILURE);
  94.    }
  95.    // read in file CHUNK_SIZE bytes at a time
  96.    #define CHUNK_SIZE 1000
  97.    lText = "", lTextLen = 0;
  98.    do {
  99.       lRead = fread(lChunk,CHUNK_SIZE,fp);
  100.       memcpy(lText+lTextLen,lChunk,lRead);
  101.       lTextLen += lRead;
  102.    } while (CHUNK_SIZE == lRead);
  103.    fclose(fp);
  104.    lText[lTextLen] = '\0';
  105.    return lText;
  106. }
  107.  
  108. AppendToClipBrd(pText)
  109. {
  110.    if ( !(data = GetClipboardText(False)) )
  111.       data = "";
  112.    strcat(data,pText);
  113.    return PutClipboardText(data);
  114. }
  115.  
  116. DeleteClipBrd()
  117. {
  118.    return ( PutClipboardData(NULL,0,CF_TEXT) );
  119. }
  120.  
  121. FindInClipboard(pText,pCaseSensitive)
  122. {
  123.    lTextLen = strlen(pText);
  124.    if ( pCaseSensitive ) {
  125.       lFindChars[0] = pText[0], lFindChars[1] = '\0';
  126.       lFindFunction = "memcmp";
  127.    } else {
  128.       lFindChars[2] = '\0';
  129.       lFindChars[1] = tolower(pText[0]);
  130.       lFindChars[0] = toupper(pText[0]);
  131.       lFindFunction = "memicmp";
  132.    }
  133.    if ( data = GetClipboardText() ) {
  134.       while ( data = strpbrk(data,lFindChars) ) {
  135.          if ( !function(lFindFunction,data,pText,lTextLen) ) {
  136.             printf("Text found.\n");
  137.             return True;
  138.          }
  139.          data++;
  140.       }
  141.    }
  142.    printf("Text not found.\n");
  143.    return False;
  144. }
  145.  
  146. GetClipBrd(OptionalFileSpec)
  147. {
  148.    if ( data = GetClipboardText(True) ) {
  149.       if ( va_arg() ) {
  150.          if ( !(fp = fopen(OptionalFileSpec,"wb")) ) {
  151.             printf("Error opening file \"%s\" for writing.\n",OptionalFileSpec);
  152.             data = NULL;
  153.          } else {
  154.             fwrite(data,strlen(data),fp);
  155.             fclose(fp);
  156.          }
  157.       } else
  158.          fwrite(data,strlen(data),stdout);
  159.    }
  160.    return(data != NULL);
  161. }
  162.  
  163. LowercaseClipBrd()
  164. {
  165.    if ( data = GetClipboardText(True) )
  166.       strlwr(data);
  167.    return( data != NULL && PutClipboardText(data));
  168. }
  169.  
  170. PrependToClipBrd(pText)
  171. {
  172.    if ( !(data = GetClipboardText(False)) )
  173.       data = "";
  174.    strcat(pText,data);
  175.    return PutClipboardText(pText);
  176. }
  177.  
  178. PutInClipBrd(pText)
  179. {
  180.    return PutClipboardText(pText);
  181. }
  182.  
  183. QueryClipBrd()
  184. {
  185.    return ( NULL != GetClipboardText(True) );
  186. }
  187.  
  188. SortClipBrd(CaseSensitive)
  189. {
  190.    if ( data = GetClipboardText(True) ) {
  191.  
  192.       // determine if the last character is line-feed, so we'll
  193.       // know whether to add line-feed in the sorted list
  194.       AddFinalLineFeed = ( NULL != strchr("\r\n",data[strlen(data)-1]) );
  195.  
  196.       // build array of each line of text in the clipboard
  197.       LineCount = 0;
  198.       Line = strtok(data,"\r\n");
  199.       while ( Line ) {
  200.          strcpy(List[LineCount++],Line);
  201.          Line = strtok(NULL,"\r\n");
  202.       }
  203.  
  204.       // sort this array of text alphabetically, case-(in)sensitive
  205.       qsort(List,LineCount,CaseSensitive ? "strcmp" : "stricmp" );
  206.  
  207.       // build one long string to put data back into the clipboard
  208.       data[0] = '\0';
  209.       for ( i = 0; i < LineCount; i++ ) {
  210.          strcat(data,List[i]);
  211.          strcat(data,"\r\n");
  212.       }
  213.  
  214.       // if there wasn't supposed to be a line-feed at the end, then remove it
  215.       if ( !AddFinalLineFeed )
  216.          data[strlen(data)-2] = '\0';
  217.  
  218.       // put this new data string in clipboard, replacing old
  219.       if ( !PutClipboardText(data) )
  220.          data = NULL;
  221.    }
  222.    return(data != NULL);
  223. }
  224.  
  225. TextOnlyInClipBrd()
  226. {
  227.    return ( NULL != (data=GetClipboardText(True))
  228.          && PutClipboardText(data) );
  229. }
  230.  
  231. UppercaseClipBrd()
  232. {
  233.    if ( data = GetClipboardText(True) )
  234.       strupr(data);
  235.    return( data != NULL && PutClipboardText(data));
  236. }
  237.  
  238.