summaryrefslogtreecommitdiffstats
path: root/Parser/parser.c
Commit message (Expand)AuthorAgeFilesLines
* Issue #1772673: The type of `char*` arguments now changed to `const char*`.Serhiy Storchaka2013-10-191-2/+2
* Issue #18722: Remove uses of the "register" keyword in C code.Antoine Pitrou2013-08-131-17/+17
* Recorded merge of revisions 81029 via svnmerge fromAntoine Pitrou2010-05-091-270/+270
* The BDFL has retired! Long live the FLUFL (Friendly Language Uncle For Life)!Brett Cannon2009-04-011-0/+8
* Merged revisions 67030-67031 via svnmerge fromBenjamin Peterson2008-10-261-3/+0
* Merged revisions 61952-61953 via svnmerge fromChristian Heimes2008-03-261-4/+12
* Four months of trunk changes (including a few releases...)Thomas Wouters2006-12-131-10/+12
* Merge p3yk branch with the trunk up to revision 45595. This breaks a fairThomas Wouters2006-04-211-3/+3
* Checkpoint. 218 tests are okay; 53 are failing. Done so far:Guido van Rossum2006-03-151-0/+2
* Patch #1440601: Add col_offset attribute to AST nodes.Martin v. Löwis2006-03-011-7/+7
* Make 'as' an actual keyword when with's future statement is used. NotThomas Wouters2006-02-281-10/+12
* from __future__ import with_statement addon for 'with', mostly written byThomas Wouters2006-02-281-13/+24
* Disable the parser hacks that allowed the "yield" keyword to be enabledNeil Schemenauer2002-03-221-0/+10
* The parser doesn't need its own implementation of assert, and having itsTim Peters2001-12-041-1/+1
* Fis SF bug #442647: not all forms of legal future statements wereGuido van Rossum2001-07-191-3/+9
* Preliminary support for "from __future__ import generators" to enableGuido van Rossum2001-07-151-3/+35
* Fix a bug in stack overflow error handling. This fixes half of BugGuido van Rossum2000-10-021-1/+1
* REMOVED all CWI, CNRI and BeOpen copyright markings.Guido van Rossum2000-09-011-9/+0
* Mass ANSIfication.Thomas Wouters2000-07-221-53/+13
* Create two new exceptions: IndentationError and TabError. These areFred Drake2000-07-111-1/+11
* Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.Tim Peters2000-07-091-6/+6
* Change copyright notice - 2nd try.Guido van Rossum2000-06-301-6/+0
* Change copyright notice.Guido van Rossum2000-06-301-22/+7
* Add new parser error code, E_OVERFLOW. This error is returned whenJeremy Hylton2000-06-201-14/+15
* Another directory quickly renamed.Guido van Rossum1997-04-291-28/+29
* Rename DEBUG macro to Py_DEBUGGuido van Rossum1996-12-301-5/+5
* New permission notice, includes CNRI.Guido van Rossum1996-10-251-13/+20
* Added 1995 to copyright message.Guido van Rossum1995-01-041-2/+2
* Parser/tokenizer.c (tok_nextc): zap tok->buf after freeing;Guido van Rossum1994-12-301-4/+2
* Merge back to main trunkGuido van Rossum1994-08-301-1/+1
* * Changed all copyright messages to include 1993.Guido van Rossum1993-03-291-2/+2
* Correct debug printing codeGuido van Rossum1992-09-031-1/+1
* Copyright for 1992 addedGuido van Rossum1992-04-051-1/+1
* Added copyright notice.Guido van Rossum1991-02-191-0/+24
* "Compiling" versionGuido van Rossum1990-12-201-16/+18
* Free parse tree when deleting parser.Guido van Rossum1990-11-181-2/+3
* Initial revisionGuido van Rossum1990-10-141-0/+396
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "cmCTestScriptHandler.h"

#include "cmCTest.h"
#include "cmake.h"
#include "cmFunctionBlocker.h"
#include "cmMakefile.h"
#include "cmLocalGenerator.h"
#include "cmGlobalGenerator.h"
#include "cmGeneratedFileStream.h"

//#include <cmsys/RegularExpression.hxx>
#include <cmsys/Process.h>

// used for sleep
#ifdef _WIN32
#include "windows.h"
#endif

#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <float.h>

// needed for sleep
#if !defined(_WIN32)
# include <unistd.h>
#endif

#include "cmCTestBuildCommand.h"
#include "cmCTestConfigureCommand.h"
#include "cmCTestCoverageCommand.h"
#include "cmCTestEmptyBinaryDirectoryCommand.h"
#include "cmCTestMemCheckCommand.h"
#include "cmCTestReadCustomFilesCommand.h"
#include "cmCTestRunScriptCommand.h"
#include "cmCTestSleepCommand.h"
#include "cmCTestStartCommand.h"
#include "cmCTestSubmitCommand.h"
#include "cmCTestTestCommand.h"
#include "cmCTestUpdateCommand.h"

#define CTEST_INITIAL_CMAKE_OUTPUT_FILE_NAME "CTestInitialCMakeOutput.log"

// used to keep elapsed time up to date
class cmCTestScriptFunctionBlocker : public cmFunctionBlocker
{
public:
  cmCTestScriptFunctionBlocker() {}
  virtual ~cmCTestScriptFunctionBlocker() {}
  virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
                                 cmMakefile &mf);
  //virtual bool ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf);
  //virtual void ScopeEnded(cmMakefile &mf);

  cmCTestScriptHandler* CTestScriptHandler;
};

// simply update the time and don't block anything
bool cmCTestScriptFunctionBlocker::
IsFunctionBlocked(const cmListFileFunction& , cmMakefile &)
{
  this->CTestScriptHandler->UpdateElapsedTime();
  return false;
}

//----------------------------------------------------------------------
cmCTestScriptHandler::cmCTestScriptHandler()
{
  this->Backup = false;
  this->EmptyBinDir = false;
  this->EmptyBinDirOnce = false;
  this->Makefile = 0;
  this->LocalGenerator = 0;
  this->CMake = 0;
  this->GlobalGenerator = 0;

  this->ScriptStartTime = 0;

  // the *60 is becuase the settings are in minutes but GetTime is seconds
  this->MinimumInterval = 30*60;
  this->ContinuousDuration = -1;
}

//----------------------------------------------------------------------
void cmCTestScriptHandler::Initialize()
{
  this->Superclass::Initialize();
  this->Backup = false;
  this->EmptyBinDir = false;
  this->EmptyBinDirOnce = false;

  this->SourceDir = "";
  this->BinaryDir = "";
  this->BackupSourceDir = "";
  this->BackupBinaryDir = "";
  this->CTestRoot = "";
  this->CVSCheckOut = "";
  this->CTestCmd = "";
  this->UpdateCmd = "";
  this->CTestEnv = "";
  this->InitCache = "";
  this->CMakeCmd = "";
  this->CMOutFile = "";
  this->ExtraUpdates.clear();

  this->MinimumInterval = 20*60;
  this->ContinuousDuration = -1;

  // what time in seconds did this script start running
  this->ScriptStartTime = 0;

  this->Makefile = 0;
  if (this->LocalGenerator)
    {
    delete this->LocalGenerator;
    }
  this->LocalGenerator = 0;
  if (this->GlobalGenerator)
    {
    delete this->GlobalGenerator;
    }
  this->GlobalGenerator = 0;
  if (this->CMake)
    {
    delete this->CMake;
    }
}

//----------------------------------------------------------------------
cmCTestScriptHandler::~cmCTestScriptHandler()
{
  // local generator owns the makefile
  this->Makefile = 0;
  if (this->LocalGenerator)
    {
    delete this->LocalGenerator;
    }
  this->LocalGenerator = 0;
  if (this->GlobalGenerator)
    {
    delete this->GlobalGenerator;
    }
  this->GlobalGenerator = 0;
  if (this->CMake)
    {
    delete this->CMake;
    }
}


//----------------------------------------------------------------------
// just adds an argument to the vector
void cmCTestScriptHandler::AddConfigurationScript(const char *script, 
                                                  bool pscope)
{
  this->ConfigurationScripts.push_back(script);
  this->ScriptProcessScope.push_back(pscope);
}


//----------------------------------------------------------------------
// the generic entry point for handling scripts, this routine will run all
// the scripts provides a -S arguments
int cmCTestScriptHandler::ProcessHandler()
{
  int res = 0;
  for (size_t i=0; i <  this->ConfigurationScripts.size(); ++i)
    {
    // for each script run it
    res += this->RunConfigurationScript
      (cmSystemTools::CollapseFullPath(this->ConfigurationScripts[i].c_str()),
       this->ScriptProcessScope[i]);
    }
  if ( res )
    {
    return -1;
    }
  return 0;
}

void cmCTestScriptHandler::UpdateElapsedTime()
{
  if (this->LocalGenerator)
    {
    // set the current elapsed time
    char timeString[20];
    int itime = static_cast<unsigned int>(cmSystemTools::GetTime()
                                          - this->ScriptStartTime);
    sprintf(timeString,"%i",itime);
    this->LocalGenerator->GetMakefile()->AddDefinition("CTEST_ELAPSED_TIME",
                                                   timeString);
    }
}

//----------------------------------------------------------------------
void cmCTestScriptHandler::AddCTestCommand(cmCTestCommand* command)
{
  cmCTestCommand* newCom = command;
  newCom->CTest = this->CTest;
  newCom->CTestScriptHandler = this;
  this->CMake->AddCommand(newCom);
}

int cmCTestScriptHandler::ExecuteScript(const std::string& total_script_arg)
{
  // execute the script passing in the arguments to the script as well as the
  // arguments from this invocation of cmake
  std::vector<const char*> argv;
  argv.push_back(this->CTest->GetCTestExecutable());
  argv.push_back("-SR");
  argv.push_back(total_script_arg.c_str());

  cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, 
             "Executable for CTest is: " << 
             this->CTest->GetCTestExecutable() << "\n");

  // now pass through all the other arguments
  std::vector<cmStdString> &initArgs = 
    this->CTest->GetInitialCommandLineArguments();
  for(size_t i=1; i < initArgs.size(); ++i)
    {
    argv.push_back(initArgs[i].c_str());
    }
  argv.push_back(0);

  // Now create process object
  cmsysProcess* cp = cmsysProcess_New();
  cmsysProcess_SetCommand(cp, &*argv.begin());
  //cmsysProcess_SetWorkingDirectory(cp, dir);
  cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  //cmsysProcess_SetTimeout(cp, timeout);
  cmsysProcess_Execute(cp);

  std::vector<char> out;
  std::vector<char> err;
  std::string line;
  int pipe = cmSystemTools::WaitForLine(cp, line, 100.0, out, err);
  while(pipe != cmsysProcess_Pipe_None)
    {
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Output: " 
               << line << "\n");
    if(pipe == cmsysProcess_Pipe_STDERR)
      {
      cmCTestLog(this->CTest, ERROR_MESSAGE, line << "\n");
      }
    else if(pipe == cmsysProcess_Pipe_STDOUT)
      {
      cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, line << "\n");
      }
    pipe = cmSystemTools::WaitForLine(cp, line, 100, out, err);
    }
  
  // Properly handle output of the build command
  cmsysProcess_WaitForExit(cp, 0);
  int result = cmsysProcess_GetState(cp);

  int retVal = 0;
  if(result == cmsysProcess_State_Exited)
    {
    retVal = cmsysProcess_GetExitValue(cp);
    }
  else
    {
    abort();
    }
  return retVal;
}


//----------------------------------------------------------------------
// this sets up some variables for the script to use, creates the required
// cmake instance and generators, and then reads in the script
int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
{
  // if the argument has a , in it then it needs to be broken into the fist
  // argument (which is the script) and the second argument which will be
  // passed into the scripts as S_ARG
  std::string script = total_script_arg;
  std::string script_arg;
  if (total_script_arg.find(",") != std::string::npos)
    {
    script = total_script_arg.substr(0,total_script_arg.find(","));
    script_arg = total_script_arg.substr(total_script_arg.find(",")+1);
    }

  // make sure the file exists
  if (!cmSystemTools::FileExists(script.c_str()))
    {
    cmSystemTools::Error("Cannot find file: ", script.c_str());
    return 1;
    }

  // create a cmake instance to read the configuration script
  // read in the list file to fill the cache
  if (this->CMake)
    {
    delete this->CMake;
    delete this->GlobalGenerator;
    delete this->LocalGenerator;
    }
  this->CMake = new cmake;
  this->CMake->AddCMakePaths(this->CTest->GetCTestExecutable());
  this->GlobalGenerator = new cmGlobalGenerator;
  this->GlobalGenerator->SetCMakeInstance(this->CMake);

  this->LocalGenerator = this->GlobalGenerator->CreateLocalGenerator();
  this->LocalGenerator->SetGlobalGenerator(this->GlobalGenerator);
  this->Makefile = this->LocalGenerator->GetMakefile();

  // set a variable with the path to the current script
  this->Makefile->AddDefinition("CTEST_SCRIPT_DIRECTORY",
                            cmSystemTools::GetFilenamePath(script).c_str());
  this->Makefile->AddDefinition("CTEST_SCRIPT_NAME",
                            cmSystemTools::GetFilenameName(script).c_str());
  this->Makefile->AddDefinition("CTEST_EXECUTABLE_NAME",
                            this->CTest->GetCTestExecutable());
  this->Makefile->AddDefinition("CMAKE_EXECUTABLE_NAME",
                            this->CTest->GetCMakeExecutable());
  this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", true);
  this->UpdateElapsedTime();

  // add any ctest specific commands, probably should have common superclass
  // for ctest commands to clean this up. If a couple more commands are
  // created with the same format lets do that - ken
  this->AddCTestCommand(new cmCTestBuildCommand);
  this->AddCTestCommand(new cmCTestConfigureCommand);
  this->AddCTestCommand(new cmCTestCoverageCommand);
  this->AddCTestCommand(new cmCTestEmptyBinaryDirectoryCommand);
  this->AddCTestCommand(new cmCTestMemCheckCommand);
  this->AddCTestCommand(new cmCTestReadCustomFilesCommand);
  this->AddCTestCommand(new cmCTestRunScriptCommand);
  this->AddCTestCommand(new cmCTestSleepCommand);
  this->AddCTestCommand(new cmCTestStartCommand);
  this->AddCTestCommand(new cmCTestSubmitCommand);
  this->AddCTestCommand(new cmCTestTestCommand);
  this->AddCTestCommand(new cmCTestUpdateCommand);

  // add the script arg if defined
  if (script_arg.size())
    {
    this->Makefile->AddDefinition("CTEST_SCRIPT_ARG", script_arg.c_str());
    }

  // always add a function blocker to update the elapsed time
  cmCTestScriptFunctionBlocker *f = new cmCTestScriptFunctionBlocker();
  f->CTestScriptHandler = this;
  this->Makefile->AddFunctionBlocker(f);

  // finally read in the script
  if (!this->Makefile->ReadListFile(0, script.c_str()) ||
    cmSystemTools::GetErrorOccuredFlag())
    {
    return 2;
    }

  return 0;
}


//----------------------------------------------------------------------
// extract variabels from the script to set ivars
int cmCTestScriptHandler::ExtractVariables()
{
  // Temporary variables
  const char* minInterval;
  const char* contDuration;

  this->SourceDir
    = this->Makefile->GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
  this->BinaryDir
    = this->Makefile->GetSafeDefinition("CTEST_BINARY_DIRECTORY");

  // add in translations for src and bin
  cmSystemTools::AddKeepPath(this->SourceDir.c_str());
  cmSystemTools::AddKeepPath(this->BinaryDir.c_str());

  this->CTestCmd
    = this->Makefile->GetSafeDefinition("CTEST_COMMAND");
  this->CVSCheckOut
    = this->Makefile->GetSafeDefinition("CTEST_CVS_CHECKOUT");
  this->CTestRoot
    = this->Makefile->GetSafeDefinition("CTEST_DASHBOARD_ROOT");
  this->UpdateCmd
    = this->Makefile->GetSafeDefinition("CTEST_UPDATE_COMMAND");
  if ( this->UpdateCmd.empty() )
    {
    this->UpdateCmd
      = this->Makefile->GetSafeDefinition("CTEST_CVS_COMMAND");
    }
  this->CTestEnv
    = this->Makefile->GetSafeDefinition("CTEST_ENVIRONMENT");
  this->InitCache
    = this->Makefile->GetSafeDefinition("CTEST_INITIAL_CACHE");
  this->CMakeCmd
    = this->Makefile->GetSafeDefinition("CTEST_CMAKE_COMMAND");
  this->CMOutFile
    = this->Makefile->GetSafeDefinition("CTEST_CMAKE_OUTPUT_FILE_NAME");

  this->Backup
    = this->Makefile->IsOn("CTEST_BACKUP_AND_RESTORE");
  this->EmptyBinDir
    = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY");
  this->EmptyBinDirOnce
    = this->Makefile->IsOn("CTEST_START_WITH_EMPTY_BINARY_DIRECTORY_ONCE");

  minInterval
    = this->Makefile->GetDefinition("CTEST_CONTINUOUS_MINIMUM_INTERVAL");
  contDuration
    = this->Makefile->GetDefinition("CTEST_CONTINUOUS_DURATION");

  char updateVar[40];
  int i;
  for (i = 1; i < 10; ++i)
    {
    sprintf(updateVar,"CTEST_EXTRA_UPDATES_%i",i);
    const char *updateVal = this->Makefile->GetDefinition(updateVar);
    if ( updateVal )
      {
      if ( this->UpdateCmd.empty() )
        {
        cmSystemTools::Error(updateVar,
          " specified without specifying CTEST_CVS_COMMAND.");
        return 12;
        }
      this->ExtraUpdates.push_back(updateVal);
      }
    }

  // in order to backup and restore we also must have the cvs root
  if (this->Backup && this->CVSCheckOut.empty())
    {
    cmSystemTools::Error(
      "Backup was requested without specifying CTEST_CVS_CHECKOUT.");
    return 3;
    }

  // make sure the required info is here
  if (this->SourceDir.empty() ||
      this->BinaryDir.empty() ||
      this->CTestCmd.empty())
    {
    std::string msg = "CTEST_SOURCE_DIRECTORY = ";
    msg += (!this->SourceDir.empty()) ? this->SourceDir.c_str() : "(Null)";
    msg += "\nCTEST_BINARY_DIRECTORY = ";
    msg += (!this->BinaryDir.empty()) ? this->BinaryDir.c_str() : "(Null)";
    msg += "\nCTEST_COMMAND = ";
    msg += (!this->CTestCmd.empty()) ? this->CTestCmd.c_str() : "(Null)";
    cmSystemTools::Error(
      "Some required settings in the configuration file were missing:\n",
      msg.c_str());
    return 4;
    }

  // if the dashboard root isn't specified then we can compute it from the
  // this->SourceDir
  if (this->CTestRoot.empty() )
    {
    this->CTestRoot = cmSystemTools::GetFilenamePath(this->SourceDir).c_str();
    }

  // the script may override the minimum continuous interval
  if (minInterval)
    {
    this->MinimumInterval = 60 * atof(minInterval);
    }
  if (contDuration)
    {
    this->ContinuousDuration = 60.0 * atof(contDuration);
    }


  this->UpdateElapsedTime();

  return 0;
}

//----------------------------------------------------------------------
void cmCTestScriptHandler::SleepInSeconds(unsigned int secondsToWait)
{
#if defined(_WIN32)
        Sleep(1000*secondsToWait);
#else
        sleep(secondsToWait);
#endif
}

//----------------------------------------------------------------------
// run a specific script
int cmCTestScriptHandler::RunConfigurationScript
(const std::string& total_script_arg, bool pscope)
{
  int result;

  this->ScriptStartTime =
    cmSystemTools::GetTime();

  // read in the script
  if (pscope)
    {
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
      "Reading Script: " << total_script_arg << std::endl);
    result = this->ReadInScript(total_script_arg);
    }
  else
    {
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
      "Executing Script: " << total_script_arg << std::endl);
    result = this->ExecuteScript(total_script_arg);
    }
  if (result)
    {
    return result;
    }

  // only run the curent script if we should
  if (this->Makefile && this->Makefile->IsOn("CTEST_RUN_CURRENT_SCRIPT"))
    {
    return this->RunCurrentScript();
    }
  return result;
}

//----------------------------------------------------------------------
int cmCTestScriptHandler::RunCurrentScript()
{
  int result;

  // do not run twice
  this->Makefile->AddDefinition("CTEST_RUN_CURRENT_SCRIPT", false);

  // no popup widows
  cmSystemTools::SetRunCommandHideConsole(true);

  // extract the vars from the cache and store in ivars
  result = this->ExtractVariables();
  if (result)
    {
    return result;
    }

  // set any environment variables
  if (!this->CTestEnv.empty())
    {
    std::vector<std::string> envArgs;
    cmSystemTools::ExpandListArgument(this->CTestEnv.c_str(),envArgs);
    // for each variable/argument do a putenv
    for (unsigned i = 0; i < envArgs.size(); ++i)
      {
      cmSystemTools::PutEnv(envArgs[i].c_str());
      }
    }

  // now that we have done most of the error checking finally run the
  // dashboard, we may be asked to repeatedly run this dashboard, such as
  // for a continuous, do we ned to run it more than once?
  if ( this->ContinuousDuration >= 0 )
    {
    this->UpdateElapsedTime();
    double ending_time  = cmSystemTools::GetTime() + this->ContinuousDuration;
    if (this->EmptyBinDirOnce)
      {
      this->EmptyBinDir = true;
      }
    do
      {
      double interval = cmSystemTools::GetTime();
      result = this->RunConfigurationDashboard();
      interval = cmSystemTools::GetTime() - interval;
      if (interval < this->MinimumInterval)
        {
        this->SleepInSeconds(
          static_cast<unsigned int>(this->MinimumInterval - interval));
        }
      if (this->EmptyBinDirOnce)
        {
        this->EmptyBinDir = false;
        }
      }
    while (cmSystemTools::GetTime() < ending_time);
    }
  // otherwise just run it once
  else
    {
    result = this->RunConfigurationDashboard();
    }

  return result;
}

//----------------------------------------------------------------------
int cmCTestScriptHandler::CheckOutSourceDir()
{
  std::string command;
  std::string output;
  int retVal;
  bool res;

  if (!cmSystemTools::FileExists(this->SourceDir.c_str()) &&
      !this->CVSCheckOut.empty())
    {
    // we must now checkout the src dir
    output = "";
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
      "Run cvs: " << this->CVSCheckOut << std::endl);
    res = cmSystemTools::RunSingleCommand(this->CVSCheckOut.c_str(), &output,
      &retVal, this->CTestRoot.c_str(), this->HandlerVerbose,
      0 /*this->TimeOut*/);
    if (!res || retVal != 0)
      {
      cmSystemTools::Error("Unable to perform cvs checkout:\n",
                           output.c_str());
      return 6;
      }
    }
  return 0;
}

//----------------------------------------------------------------------
int cmCTestScriptHandler::BackupDirectories()
{
  int retVal;

  // compute the backup names
  this->BackupSourceDir = this->SourceDir;
  this->BackupSourceDir += "_CMakeBackup";
  this->BackupBinaryDir = this->BinaryDir;
  this->BackupBinaryDir += "_CMakeBackup";

  // backup the binary and src directories if requested
  if (this->Backup)
    {
    // if for some reason those directories exist then first delete them
    if (cmSystemTools::FileExists(this->BackupSourceDir.c_str()))
      {
      cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
      }
    if (cmSystemTools::FileExists(this->BackupBinaryDir.c_str()))
      {
      cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
      }

    // first rename the src and binary directories
    rename(this->SourceDir.c_str(), this->BackupSourceDir.c_str());
    rename(this->BinaryDir.c_str(), this->BackupBinaryDir.c_str());

    // we must now checkout the src dir
    retVal = this->CheckOutSourceDir();
    if (retVal)
      {
      this->RestoreBackupDirectories();
      return retVal;
      }
    }

  return 0;
}


//----------------------------------------------------------------------
int cmCTestScriptHandler::PerformExtraUpdates()
{
  std::string command;
  std::string output;
  int retVal;
  bool res;

  // do an initial cvs update as required
  command = this->UpdateCmd;
  std::vector<cmStdString>::iterator it;
  for (it = this->ExtraUpdates.begin();
    it != this->ExtraUpdates.end();
    ++ it )
    {
    std::vector<std::string> cvsArgs;
    cmSystemTools::ExpandListArgument(it->c_str(),cvsArgs);
    if (cvsArgs.size() == 2)
      {
      std::string fullCommand = command;
      fullCommand += " update ";
      fullCommand += cvsArgs[1];
      output = "";
      retVal = 0;
      cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run Update: "
        << fullCommand.c_str() << std::endl);
      res = cmSystemTools::RunSingleCommand(fullCommand.c_str(), &output,
        &retVal, cvsArgs[0].c_str(),
        this->HandlerVerbose, 0 /*this->TimeOut*/);
      if (!res || retVal != 0)
        {
        cmSystemTools::Error("Unable to perform extra updates:\n",
          it->c_str(), "\nWith output:\n",
          output.c_str());
        return 0;
        }
      }
    }
  return 0;
}


//----------------------------------------------------------------------
// run a single dashboard entry
int cmCTestScriptHandler::RunConfigurationDashboard()
{
  // local variables
  std::string command;
  std::string output;
  int retVal;
  bool res;

  // make sure the src directory is there, if it isn't then we might be able
  // to check it out from cvs
  retVal = this->CheckOutSourceDir();
  if (retVal)
    {
    return retVal;
    }

  // backup the dirs if requested
  retVal = this->BackupDirectories();
  if (retVal)
    {
    return retVal;
    }

  // clear the binary directory?
  if (this->EmptyBinDir)
    {
    if ( !cmCTestScriptHandler::EmptyBinaryDirectory(
        this->BinaryDir.c_str()) )
      {
      cmCTestLog(this->CTest, ERROR_MESSAGE,
        "Problem removing the binary directory" << std::endl);
      }
    }

  // make sure the binary directory exists if it isn't the srcdir
  if (!cmSystemTools::FileExists(this->BinaryDir.c_str()) &&
      this->SourceDir != this->BinaryDir)
    {
    if (!cmSystemTools::MakeDirectory(this->BinaryDir.c_str()))
      {
      cmSystemTools::Error("Unable to create the binary directory:\n",
                           this->BinaryDir.c_str());
      this->RestoreBackupDirectories();
      return 7;
      }
    }

  // if the binary directory and the source directory are the same,
  // and we are starting with an empty binary directory, then that means
  // we must check out the source tree
  if (this->EmptyBinDir && this->SourceDir == this->BinaryDir)
    {
    // make sure we have the required info
    if (this->CVSCheckOut.empty())
      {
      cmSystemTools::Error("You have specified the source and binary "
        "directories to be the same (an in source build). You have also "
        "specified that the binary directory is to be erased. This means "
        "that the source will have to be checked out from CVS. But you have "
        "not specified CTEST_CVS_CHECKOUT");
      return 8;
      }

    // we must now checkout the src dir
    retVal = this->CheckOutSourceDir();
    if (retVal)
      {
      this->RestoreBackupDirectories();
      return retVal;
      }
    }

  // backup the dirs if requested
  retVal = this->PerformExtraUpdates();
  if (retVal)
    {
    return retVal;
    }

  // put the initial cache into the bin dir
  if (!this->InitCache.empty())
    {
    std::string cacheFile = this->BinaryDir;
    cacheFile += "/CMakeCache.txt";
    cmGeneratedFileStream fout(cacheFile.c_str());
    if(!fout)
      {
      this->RestoreBackupDirectories();
      return 9;
      }

    fout.write(this->InitCache.c_str(), this->InitCache.size());

    // Make sure the operating system has finished writing the file
    // before closing it.  This will ensure the file is finished before
    // the check below.
    fout.flush();
    fout.close();
    }

  // do an initial cmake to setup the DartConfig file
  int cmakeFailed = 0;
  std::string cmakeFailedOuput;
  if (!this->CMakeCmd.empty())
    {
    command = this->CMakeCmd;
    command += " \"";
    command += this->SourceDir;
    output = "";
    command += "\"";
    retVal = 0;
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run cmake command: "
      << command.c_str() << std::endl);
    res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
      &retVal, this->BinaryDir.c_str(),
      this->HandlerVerbose, 0 /*this->TimeOut*/);

    if ( !this->CMOutFile.empty() )
      {
      std::string cmakeOutputFile = this->CMOutFile;
      if ( !cmSystemTools::FileIsFullPath(cmakeOutputFile.c_str()) )
        {
        cmakeOutputFile = this->BinaryDir + "/" + cmakeOutputFile;
        }

      cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
        "Write CMake output to file: " << cmakeOutputFile.c_str()
        << std::endl);
      cmGeneratedFileStream fout(cmakeOutputFile.c_str());
      if ( fout )
        {
        fout << output.c_str();
        }
      else
        {
        cmCTestLog(this->CTest, ERROR_MESSAGE,
          "Cannot open CMake output file: "
          << cmakeOutputFile.c_str() << " for writing" << std::endl);
        }
      }
    if (!res || retVal != 0)
      {
      // even if this fails continue to the next step
      cmakeFailed = 1;
      cmakeFailedOuput = output;
      }
    }

  // run ctest, it may be more than one command in here
  std::vector<std::string> ctestCommands;
  cmSystemTools::ExpandListArgument(this->CTestCmd,ctestCommands);
  // for each variable/argument do a putenv
  for (unsigned i = 0; i < ctestCommands.size(); ++i)
    {
    command = ctestCommands[i];
    output = "";
    retVal = 0;
    cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Run ctest command: "
      << command.c_str() << std::endl);
    res = cmSystemTools::RunSingleCommand(command.c_str(), &output,
      &retVal, this->BinaryDir.c_str(), this->HandlerVerbose,
      0 /*this->TimeOut*/);

    // did something critical fail in ctest
    if (!res || cmakeFailed ||
        retVal & cmCTest::BUILD_ERRORS)
      {
      this->RestoreBackupDirectories();
      if (cmakeFailed)
        {
        cmCTestLog(this->CTest, ERROR_MESSAGE,
          "Unable to run cmake:" << std::endl
          << cmakeFailedOuput.c_str() << std::endl);
        return 10;
        }
      cmCTestLog(this->CTest, ERROR_MESSAGE,
        "Unable to run ctest:" << std::endl
        << output.c_str() << std::endl);
      if (!res)
        {
        return 11;
        }
      return retVal * 100;
      }
    }

  // if all was succesful, delete the backup dirs to free up disk space
  if (this->Backup)
    {
    cmSystemTools::RemoveADirectory(this->BackupSourceDir.c_str());
    cmSystemTools::RemoveADirectory(this->BackupBinaryDir.c_str());
    }

  return 0;
}


//-------------------------------------------------------------------------
void cmCTestScriptHandler::RestoreBackupDirectories()
{
  // if we backed up the dirs and the build failed, then restore
  // the backed up dirs
  if (this->Backup)
    {
    // if for some reason those directories exist then first delete them
    if (cmSystemTools::FileExists(this->SourceDir.c_str()))
      {
      cmSystemTools::RemoveADirectory(this->SourceDir.c_str());
      }
    if (cmSystemTools::FileExists(this->BinaryDir.c_str()))
      {
      cmSystemTools::RemoveADirectory(this->BinaryDir.c_str());
      }
    // rename the src and binary directories
    rename(this->BackupSourceDir.c_str(), this->SourceDir.c_str());
    rename(this->BackupBinaryDir.c_str(), this->BinaryDir.c_str());
    }
}

bool cmCTestScriptHandler::RunScript(cmCTest* ctest, const char *sname, 
                                     bool InProcess)
{
  cmCTestScriptHandler* sh = new cmCTestScriptHandler();
  sh->SetCTestInstance(ctest);
  sh->AddConfigurationScript(sname,InProcess);
  sh->ProcessHandler();
  delete sh;
  return true;
}

bool cmCTestScriptHandler::EmptyBinaryDirectory(const char *sname)
{
  // try to avoid deleting root
  if (!sname || strlen(sname) < 2)
    {
    return false;
    }

  // try to avoid deleting directories that we shouldn't
  std::string check = sname;
  check += "/CMakeCache.txt";
  if(cmSystemTools::FileExists(check.c_str()) &&
     !cmSystemTools::RemoveADirectory(sname))
    {
    return false;
    }
  return true;
}

//-------------------------------------------------------------------------
double cmCTestScriptHandler::GetRemainingTimeAllowed()
{
  if (!this->Makefile)
    {
    return 1.0e7;
    }

  const char *timelimitS
    = this->Makefile->GetDefinition("CTEST_TIME_LIMIT");
  
  if (!timelimitS)
    {
    return 1.0e7;
    }

  double timelimit = atof(timelimitS);
  
  return timelimit - cmSystemTools::GetTime() + this->ScriptStartTime;
}