home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / install.cmm < prev    next >
Encoding:
Text File  |  1995-10-20  |  8.9 KB  |  263 lines

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