home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / INSTALL.CMM < prev    next >
Text File  |  1996-02-12  |  10KB  |  259 lines

  1. /*************************************************************************
  2.  ***                    Install.cmm for OS/2 ver.1                     ***
  3.  *** This is the installation program to set up CEnvi unregistered     ***
  4.  *** shareware on your computer.  Before running this program, CEnvi   ***
  5.  *** must be copied to a hard disk directory.  This Install.cmm        ***
  6.  *** program should be run from CEnvi for OS/2 at the command line.    ***
  7.  *************************************************************************/
  8.  
  9. if defined(_OS2_)
  10.    ExeName = "CEnvi2.exe"
  11. else {
  12.    puts("This INSTALL.CMM is for the OS/2 version of CEnvi.");
  13.    puts("You are not running this version.\a");
  14.    exit(EXIT_FAILURE);
  15. }
  16.  
  17. main(argc,argv)
  18. {
  19.    // There should be no input args.  If there are then the user needs help.
  20.    ScreenClear();
  21.    if ( argc != 1 )
  22.       GiveInstructionsAndExit();
  23.  
  24.    // Get the current directory.  The GetInstallDirectory() function assures
  25.    // that it is not a floppy.
  26.    InstallDirectory = GetInstallDirectory(argv[0]);
  27.  
  28.    // Make the all directory based on the install directory
  29.    strcpy(allDirectory,InstallDirectory);
  30.    while((allDirectory[length = strlen(allDirectory) - 1]!='\\')&&(length!=0))
  31.       allDirectory[length] = '\0';
  32.    strcat(allDirectory,"ALL");
  33.  
  34.    Install(InstallDirectory,allDirectory,"CONFIG.SYS","CONFIG.BAK");
  35.  
  36.    puts("\nFinished installing CEnvi For OS/2!");
  37.    puts("\n<Press any key to continue>\n");
  38.    getch();
  39. }
  40.  
  41.  
  42. GiveInstructionsAndExit()  // Show some info about using this program
  43. {                          // and then exit this program. Do not return.
  44.    printf("Install.cmm - CEnvi installation procedure.  Execute with no parameters\n");
  45.    printf("              from the directory that contains %s and also contains\n",ExeName);
  46.    printf("              the *.cmm and other sample CEnvi files, including Install.cmm\n");
  47.    exit(EXIT_FAILURE);
  48. }
  49.  
  50.  
  51. GetInstallDirectory(Executable)   // return current dir that is not on a floppy
  52. {
  53.    // current source directory from the name of the executable
  54.    CurDir = SplitFileName(FullPath(Executable)).dir;
  55.    assert( CurDir != NULL  &&  CurDir[0] != 0 );
  56.  
  57.    // check that current directory is not floppy A: or B:
  58.    if ( CurDir[0] == 'A'  ||  CurDir[0] == 'B' ) {
  59.       printf("\nCannot install CEnvi from a floppy.\n\a\n");
  60.       GiveInstructionsAndExit();
  61.    }
  62.  
  63.    // Have found the directory Install.cmm is in, and so check that
  64.    // CEnvi.exe is also in the same directory.
  65.    if ( NULL == Directory(strcat(strcpy(temp,CurDir),ExeName)) ) {
  66.       printf("\nThis installation assumes that %s is in the same directory as\n",ExeName);
  67.       printf("Install.cmm.  Could not find %s in the Install.cmm directory.\n\a\n",ExeName);
  68.       GiveInstructionsAndExit();
  69.    }
  70.  
  71.    // remove extra backslash if at end of name (i.e., not root directory)
  72.    if ( strcmp(CurDir+1,":\\") )
  73.       CurDir[strlen(CurDir)-1] = 0;
  74.  
  75.    // return the result
  76.    return(CurDir);
  77. }
  78.  
  79.  
  80. Fatal(Format)  // like printf() but also beeps and exits program
  81. {
  82.    va_start(parameters,Format);
  83.    printf("\a\n");  // beep
  84.    vprintf(Format,parameters);
  85.    exit(EXIT_FAILURE);
  86. }
  87.  
  88.  
  89. GetYesOrNo()   // prompt for Yes or No, and return entered boolean TRUE
  90. {              // if YES else FALSE if NO.
  91.    printf(" (Y/N) ");
  92.    while( TRUE ) { // forever
  93.       key = toupper(getch());
  94.       if ( key != 'Y' && key != 'N' )
  95.          printf("\a");  // beep
  96.       else
  97.          break;
  98.    }
  99.    printf("%c\n",key);
  100.    return( key == 'Y' );
  101. }
  102.  
  103. CopyFile(Source,Destination) // copy file from source to destination
  104. {
  105.    system("copy %s %s > NUL:",Source,Destination);
  106. }
  107.  
  108.  
  109. SkipWhitespace(str)
  110. {
  111.    while( isspace(str[0]) )
  112.       str++;
  113. }
  114.  
  115.  
  116. AlreadyInPath(Dir,Path) // search through path for this Dir, return True if found, else False
  117. {
  118.    len = strlen(Dir)
  119.    p = Path;
  120.    do {
  121.       if ( 0 == strnicmp(p,Dir,len)  && (p[len]==0 || p[len]==';') )
  122.          return(True)
  123.       p = strchr(p,';')
  124.    } while( p++ != NULL )
  125.    return(False)
  126. }
  127.  
  128.  
  129. AlterEVarLine(text,EVar,Dir)
  130.    // If this text is a line setting the EVar environment variable, and if
  131.    // Dir is not already in it, then add Dir.  Return TRUE if this is
  132.    // a line for EVar, and False if it is not.
  133. {
  134.    FoundEVar = FALSE; // assume this is not the line
  135.    // determine if text is of the type "EVAR=" or "set EVAR="
  136.    SkipWhitespace(c = text);
  137.    // if the next statement is "SET" then skip it
  138.    if ( !strncmpi(c,"SET",3) ) {
  139.       // Skip beyond the SET statement
  140.       c += 3;
  141.       SkipWhitespace(c);
  142.    }
  143.    // test if this next part is the variable name
  144.    EVarLen = strlen(EVar);
  145.    if ( !strncmpi(c,EVar,EVarLen) ) {
  146.       c += EVarLen;
  147.       if ( isspace(c[0])  ||  c[0] == '=' ) {
  148.          // THIS IS IT.  This line describes the EVar.
  149.          SkipWhitespace(c);
  150.          if ( c[0] == '=' ) {
  151.             c++;
  152.             SkipWhitespace(c);
  153.          }
  154.          FoundEVar = TRUE;
  155.          // If Dir is not already in this line then add Dir at the end.
  156.          // Check each dir as value between semicolons (;)
  157.          SkipWhitespace(c);
  158.          if ( !AlreadyInPath(Dir,c) ) {
  159.             // add this to the end of the existing path
  160.             if ( c[strlen(c)-1] != ';' )
  161.                strcat(c,";");
  162.             strcat(c,Dir);
  163.          }
  164.       }
  165.    }
  166.    return(FoundEVar);
  167. }
  168.  
  169. Install(Dir,allDirectory,pFileName,pBackupFileName)
  170. {
  171.    // set up the profile string
  172.    sprintf(profileString,"%s;%s",Dir,allDirectory);
  173.  
  174.    // append the PATH statement so that it contains this directory, and
  175.    // also add the CMMPATH environment variable.  But give user a choice
  176.    // of whether to do this first; if they choose not to then tell them
  177.    // what they must do by hand.
  178.    printf("\nIf you choose, install will add:\n\n");
  179.    printf("    \"%s\"\n\n",Dir);
  180.    printf("to the PATH environment variable in your %s file.  Install will also\n",pFileName);
  181.    printf("add %s and %s to the CMMPATH environment variable in\n",Dir,allDirectory);
  182.    printf("that file.  If you select to make these changes to %s, then\n",pFileName);
  183.    printf("install will backup the current version of %s to %s.\n",pFileName,pBackupFileName);
  184.    printf("\n\nShould these changes be made to %s?",pFileName);
  185.    if ( GetYesOrNo() ) {
  186.  
  187.       do
  188.       {
  189.          printf("\nEnter drive letter for the booted %s file: ",pFileName);
  190.          DriveLetter = getche();
  191.          printf("\n");
  192.          if ( !isalpha(DriveLetter) ) 
  193.          {
  194.             printf("\aInvalid drive letter %c!\n",DriveLetter);
  195.          }
  196.          else
  197.          {
  198.             sprintf(lFileName,"%c:\\%s",DriveLetter,pFileName);
  199.             sprintf(lBackupFileName,"%c:\\%s",DriveLetter,pBackupFileName);
  200.             src = fopen(lFileName,"rt");
  201.             if ( src == NULL )
  202.                printf("Could not open source file \"%s\" for reading.\n",lFileName);
  203.             else
  204.                fclose(src);
  205.          }
  206.       } while(!isalpha(DriveLetter) || (src == NULL));
  207.  
  208.       printf("Making changes to %s...",lFileName);
  209.       // user chose to make automatic changes.  Do so now
  210.       CopyFile(lFileName,lBackupFileName);
  211.  
  212.       // will now read the backup file, and if any PATH or CMMPATH line is
  213.       // encountered then will alter it, else just copy line exactly.
  214.       src = fopen(lBackupFileName,"rt");
  215.       if ( src == NULL )
  216.          Fatal("Could not open source file \"%s\" for reading.",lBackupFileName);
  217.       dest = fopen(lFileName,"wt");
  218.       if ( dest == NULL )
  219.          Fatal("Could not open source file \"%s\" for reading.",lBackupFileName);
  220.       SetPath = SetCmmPath = False;
  221.       while ( NULL != (line = fgets(src)) ) {
  222.          // remove the pesky newline if there is one
  223.          if ( PeskyNewLine = (line[strlen(line)-1] == '\n') )
  224.             line[strlen(line)-1] = 0;
  225.          if ( SetPath == FALSE && AlterEVarLine(line,"PATH",Dir) )
  226.             SetPath = True;
  227.          if ( AlterEVarLine(line,"CMMPATH",Dir) )
  228.          {
  229.             sprintf(line,"set CMMPATH=%s;%s",Dir,AllDirectory);
  230.             SetCmmPath = True;
  231.          }
  232.          fputs(line,dest);
  233.          if ( PeskyNewLine )
  234.             fputs("\n",dest);
  235.       }
  236.       fclose(src);
  237.       // if haven't already written PATH or CMMPATH, then do so now
  238.       if ( !SetPath )
  239.          fprintf(dest,"\nSET PATH=%s\n",Dir);
  240.       if ( !SetCmmPath )
  241.          fprintf(dest,"\nSET CMMPATH=%s\n",profileString);
  242.       fclose(dest);
  243.       printf("\n%s has been altered.\n",lFileName);
  244.       printf("Changes will take effect after you reboot.\n");
  245.    } else {
  246.       // user choose not to automatically install, so describe what the user
  247.       // needs to do by hand to make it work.
  248.       ScreenClear();
  249.       printf("\nThe PATH environment variable is used to find executable files, such as\n");
  250.       printf("%s, and batch files, such as those included in this CEnvi\n",ExeName);
  251.       printf("unregistered shareware release.  The CMMPATH environment variable is\n");
  252.       printf("used by CEnvi to find common source code that may be included\n");
  253.       printf("in other CEnvi source files.\n\n");
  254.       printf("You chose not to have Install automatically alter %s.\n\n",pFileName);
  255.       printf("You may wish to try Install.cmm again, or to make these modifications\n");
  256.       printf("to %s by hand.\n",pFileName);
  257.    }
  258. }
  259.