home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cenvi23.zip
/
CENVIVER.CMM
< prev
next >
Wrap
Text File
|
1996-01-24
|
6KB
|
151 lines
//*************************************************************************
//*** CEnviVer.cmm - CEnvi script use to change all references to one ***
//*** ver.1 version of CEnvi to references to another version. ***
//*** For example, in upgrading from version 1.090 to ***
//*** 1.10 you may need to change all versions strings ***
//*** from CENVI to CENVID, or in changing from CEnvi ***
//*** for DOS to CEnvi for 32-bit dos you may need to ***
//*** change CENVID to CENVID32. ***
//*************************************************************************
Instructions()
{
puts("\nCEnviVer - Change version of CEnvi executable name in a file\a")
puts("")
puts("USAGE: CEnviX CEnviVer <filespec> <NewCEnviSpec> [/PROMPT]")
puts("")
puts("WHERE: filespec - Name of source file with references to CEnvi???")
puts(" NewCEnviSpec - New CEnvi executable (e.g. CEnvi2, CEnviD32, etc...)")
puts("OPTION: /PROMPT - Prompt before changing any text (abbrev. /P)")
puts("")
puts("EXAMPLES: CEnvi2 CEnviVer BatLoops.cmd CEnvi2")
puts(" ALLFILES *.cmm CEnviD CEnviVer $FILE CEnviD /PROMPT")
puts(" ALLFILES *.bat CEnviD32 CEnviVer $FILE CEnviD32 /P")
puts("")
if !defined(_SHELL_) {
printf("Press any key to exit...");
getch();
}
exit(EXIT_FAILURE);
}
#include <optparms.lib>
#define TEMP_FILE_NAME "CENVIVER.TMP"
main(argc,argv)
{
// Get input parameters
Prompt = OptionalParameter(argc,argv,"PROMPT")
|| OptionalParameter(argc,argv,"P")
if ( argc != 3 )
Instructions();
FileSpec = argv[1];
NewCEnvi = argv[2];
if ( memicmp(NewCEnvi,"CEnvi",5) ) {
printf("New name must be of the form \"CEnviXXX\" where XXX is version.\n\a",FileSpec);
exit(EXIT_FAILURE);
}
if ( 8 < strlen(NewCEnvi) ) {
printf("New name must 8 characters or less.\n\a",FileSpec);
exit(EXIT_FAILURE);
}
// Open old source file
SrcFP = fopen(FileSpec,"rt");
if ( !SrcFP ) {
printf("Unable to open source file \"%s\" for reading.\n\a",FileSpec);
exit(EXIT_FAILURE);
}
// open temporary file in same directory as source file
FileNameParts = SplitFileName(FileSpec);
sprintf(TemporaryFileSpec,"%s%s",FileNameParts.dir,TEMP_FILE_NAME);
DstFP = fopen(TemporaryFileSpec,"wt");
if ( !DstFP ) {
printf("Unable to open temporary file \"%s\" for reading.\n\a",TemporaryFileSpec);
fclose(SrcFP);
exit(EXIT_FAILURE);
}
// replace all text in source
TextReplaced = RewriteCEnviReferences(SrcFP,DstFP,NewCEnvi,Prompt);
// not reading or writing any more, so close files
SrcError = ferror(SrcFP) & fclose(SrcFP);
DstError = ferror(DstFP) & fclose(DstFP);
// if error in either file then say so
if ( SrcError ) {
printf("Error reading source file \"%s\".\a\n",FileSpec);
} else if ( TextReplaced ) {
// text was replaced so remove old file and replace with new one
if ( DstError )
printf("Error writing temporary file \"%s\".\a\n",TemporaryFileSpec);
else if ( remove(FileSpec) )
printf("Unable to remove old file \"%s\" for replacing.\n\a",FileSpec);
else if ( rename(TemporaryFileSpec,FileSpec) )
printf("Unable to replace old file with new file. Old file \"%s\"\n"
"may be lost!\a\n",FileSpec);
else
printf("\nReplace %d CEnvi??? with \"%s\".\n",TextReplaced,NewCEnvi);
} else {
puts("\nNo text replaced.");
}
// whatever else may have happened, delete temporary file if there
remove(TemporaryFileSpec);
}
RewriteCEnviReferences(src,dst,NewName,Prompt)
// find old references to CEnvi execuable and replace with new
{
lCount = 0;
ReplaceLen = strlen(NewName);
while( lFound = lLine = fgets(src) ) {
while ( lFound = strpbrk(lFound,"Cc") ) {
if ( !memicmp(lFound,"CEnvi",5)
&& (lFound == lLine || strchr(" \t@",lFound[-1]) ) ) {
// find up to end of CEnvi??? string
if ( end = strpbrk(lFound,". \t\r\n") ) {
FindLen = end - lFound;
if ( FindLen <= 8
&& ( FindLen != ReplaceLen || memicmp(lFound,NewName,FindLen) ) ) {
// make sure that old name doesn't have "weird" character
for ( i = 0; i < FindLen; i++ ) {
if ( !isalnum(lFound[i]) )
break;
}
if ( i == FindLen ) {
// name has changed, so replace old name with new if user wants it
strcpy(SaveOldLine,lLine);
strcpy(lFound+ReplaceLen,lFound+FindLen);
memcpy(lFound,NewName,ReplaceLen);
printf("\nOLD: %s",SaveOldLine);
printf("NEW: %s",lLine);
if ( Prompt && !ReplaceWanted() )
strcpy(lLine,SaveOldLine);
else
lCount++;
}
}
}
}
lFound++;
}
fputs(lLine,dst);
}
return lCount;
}
ReplaceWanted(lOld,lNew) // want to replace old with new
{
printf("REPLACE (Y/N) ? ");
while( kbhit() ) getch(); // flush keyboard
do {
key = getch();
} while ( !strchr("YyNn",key) );
printf("%c\n",key);
return( 'Y' == toupper(key) );
}