home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / CENVIVER.CMM < prev    next >
Text File  |  1996-01-24  |  6KB  |  151 lines

  1. //*************************************************************************
  2. //*** CEnviVer.cmm - CEnvi script use to change all references to one   ***
  3. //*** ver.1          version of CEnvi to references to another version. ***
  4. //***                For example, in upgrading from version 1.090 to    ***
  5. //***                1.10 you may need to change all versions strings   ***
  6. //***                from CENVI to CENVID, or in changing from CEnvi    ***
  7. //***                for DOS to CEnvi for 32-bit dos you may need to    ***
  8. //***                change CENVID to CENVID32.                         ***
  9. //*************************************************************************
  10.  
  11. Instructions()
  12. {
  13.    puts("\nCEnviVer - Change version of CEnvi executable name in a file\a")
  14.    puts("")
  15.    puts("USAGE: CEnviX CEnviVer <filespec> <NewCEnviSpec> [/PROMPT]")
  16.    puts("")
  17.    puts("WHERE: filespec - Name of source file with references to CEnvi???")
  18.    puts("       NewCEnviSpec - New CEnvi executable (e.g. CEnvi2, CEnviD32, etc...)")
  19.    puts("OPTION: /PROMPT - Prompt before changing any text (abbrev. /P)")
  20.    puts("")
  21.    puts("EXAMPLES: CEnvi2 CEnviVer BatLoops.cmd CEnvi2")
  22.    puts("          ALLFILES *.cmm CEnviD CEnviVer $FILE CEnviD /PROMPT")
  23.    puts("          ALLFILES *.bat CEnviD32 CEnviVer $FILE CEnviD32 /P")
  24.    puts("")
  25.    if !defined(_SHELL_) {
  26.       printf("Press any key to exit...");
  27.       getch();
  28.    }
  29.    exit(EXIT_FAILURE);
  30. }
  31.  
  32. #include <optparms.lib>
  33. #define TEMP_FILE_NAME  "CENVIVER.TMP"
  34.  
  35. main(argc,argv)
  36. {
  37.    // Get input parameters
  38.    Prompt = OptionalParameter(argc,argv,"PROMPT")
  39.          || OptionalParameter(argc,argv,"P")
  40.    if ( argc != 3 )
  41.       Instructions();
  42.    FileSpec = argv[1];
  43.    NewCEnvi = argv[2];
  44.  
  45.    if ( memicmp(NewCEnvi,"CEnvi",5) ) {
  46.       printf("New name must be of the form \"CEnviXXX\" where XXX is version.\n\a",FileSpec);
  47.       exit(EXIT_FAILURE);
  48.    }
  49.    if ( 8 < strlen(NewCEnvi) ) {
  50.       printf("New name must 8 characters or less.\n\a",FileSpec);
  51.       exit(EXIT_FAILURE);
  52.    }
  53.  
  54.    // Open old source file
  55.    SrcFP = fopen(FileSpec,"rt");
  56.    if ( !SrcFP ) {
  57.       printf("Unable to open source file \"%s\" for reading.\n\a",FileSpec);
  58.       exit(EXIT_FAILURE);
  59.    }
  60.  
  61.    // open temporary file in same directory as source file
  62.    FileNameParts = SplitFileName(FileSpec);
  63.    sprintf(TemporaryFileSpec,"%s%s",FileNameParts.dir,TEMP_FILE_NAME);
  64.    DstFP = fopen(TemporaryFileSpec,"wt");
  65.    if ( !DstFP ) {
  66.       printf("Unable to open temporary file \"%s\" for reading.\n\a",TemporaryFileSpec);
  67.       fclose(SrcFP);
  68.       exit(EXIT_FAILURE);
  69.    }
  70.  
  71.    // replace all text in source
  72.    TextReplaced = RewriteCEnviReferences(SrcFP,DstFP,NewCEnvi,Prompt);
  73.  
  74.    // not reading or writing any more, so close files
  75.    SrcError = ferror(SrcFP) & fclose(SrcFP);
  76.    DstError = ferror(DstFP) & fclose(DstFP);
  77.  
  78.    // if error in either file then say so
  79.    if ( SrcError ) {
  80.       printf("Error reading source file \"%s\".\a\n",FileSpec);
  81.    } else if ( TextReplaced ) {
  82.       // text was replaced so remove old file and replace with new one
  83.       if ( DstError )
  84.          printf("Error writing temporary file \"%s\".\a\n",TemporaryFileSpec);
  85.       else if ( remove(FileSpec) )
  86.          printf("Unable to remove old file \"%s\" for replacing.\n\a",FileSpec);
  87.       else if ( rename(TemporaryFileSpec,FileSpec) )
  88.          printf("Unable to replace old file with new file. Old file \"%s\"\n"
  89.                 "may be lost!\a\n",FileSpec);
  90.       else
  91.          printf("\nReplace %d CEnvi??? with \"%s\".\n",TextReplaced,NewCEnvi);
  92.    } else {
  93.       puts("\nNo text replaced.");
  94.    }
  95.  
  96.    // whatever else may have happened, delete temporary file if there
  97.    remove(TemporaryFileSpec);
  98. }
  99.  
  100. RewriteCEnviReferences(src,dst,NewName,Prompt)
  101.    // find old references to CEnvi execuable and replace with new
  102. {
  103.    lCount = 0;
  104.    ReplaceLen = strlen(NewName);
  105.    while( lFound = lLine = fgets(src) ) {
  106.       while ( lFound = strpbrk(lFound,"Cc") ) {
  107.          if ( !memicmp(lFound,"CEnvi",5)
  108.            && (lFound == lLine || strchr(" \t@",lFound[-1]) ) ) {
  109.             // find up to end of CEnvi??? string
  110.             if ( end = strpbrk(lFound,". \t\r\n") ) {
  111.                FindLen = end - lFound;
  112.                if ( FindLen <= 8
  113.                  && ( FindLen != ReplaceLen || memicmp(lFound,NewName,FindLen) ) ) {
  114.                   // make sure that old name doesn't have "weird" character
  115.                   for ( i = 0; i < FindLen; i++ ) {
  116.                      if ( !isalnum(lFound[i]) )
  117.                         break;
  118.                   }
  119.                   if ( i == FindLen ) {
  120.                      // name has changed, so replace old name with new if user wants it
  121.                      strcpy(SaveOldLine,lLine);
  122.                      strcpy(lFound+ReplaceLen,lFound+FindLen);
  123.                      memcpy(lFound,NewName,ReplaceLen);
  124.                      printf("\nOLD: %s",SaveOldLine);
  125.                      printf("NEW: %s",lLine);
  126.                      if ( Prompt && !ReplaceWanted() )
  127.                         strcpy(lLine,SaveOldLine);
  128.                      else
  129.                         lCount++;
  130.                   }
  131.                }
  132.             }
  133.          }
  134.          lFound++;
  135.       }
  136.       fputs(lLine,dst);
  137.    }
  138.    return lCount;
  139. }
  140.  
  141. ReplaceWanted(lOld,lNew) // want to replace old with new
  142. {
  143.    printf("REPLACE (Y/N) ? ");
  144.    while( kbhit() ) getch();  // flush keyboard
  145.    do {
  146.       key = getch();
  147.    } while ( !strchr("YyNn",key) );
  148.    printf("%c\n",key);
  149.    return( 'Y' == toupper(key) );
  150. }
  151.