Logo Search packages:      
Sourcecode: highlight version File versions  Download package

int HLCmdLineApp::run ( const int  argc,
const char *  argv[] 
)

Start application

Parameters:
argc Number of command line arguments
argv values of command line arguments
Returns:
EXIT_SUCCESS or EXIT_FAILURE

Definition at line 344 of file main.cpp.

References CmdLineOptions::attachLineAnchors(), CmdLineOptions::disableBabelShorthands(), CmdLineOptions::enableBatchMode(), CmdLineOptions::enclosePreTag(), CmdLineOptions::fillLineNrZeroes(), CmdLineOptions::forceOutput(), CmdLineOptions::formatSupportsExtStyle(), CmdLineOptions::fragmentOutput(), CmdLineOptions::getAdditionalConfDir(), CmdLineOptions::getAdditionalDataDir(), CmdLineOptions::getAnchorPrefix(), CmdLineOptions::getBaseFont(), CmdLineOptions::getBaseFontSize(), CmdLineOptions::getClassName(), CmdLineOptions::getConfigFilePath(), CmdLineOptions::getDataDir(), CmdLineOptions::getDocumentTitle(), CmdLineOptions::getEncoding(), CmdLineOptions::getIndentScheme(), CmdLineOptions::getInputFileNames(), highlight::CodeGenerator::getInstance(), CmdLineOptions::getKeywordCase(), DataDir::getLangPath(), CmdLineOptions::getLanguage(), CmdLineOptions::getLineLength(), CmdLineOptions::getMarkLines(), CmdLineOptions::getNumberSpaces(), CmdLineOptions::getNumberStart(), CmdLineOptions::getNumberWidth(), CmdLineOptions::getOutDirectory(), CmdLineOptions::getOutFileSuffix(), CmdLineOptions::getOutputType(), CmdLineOptions::getPageSize(), CmdLineOptions::getSingleInFilename(), CmdLineOptions::getSingleOutFilename(), CmdLineOptions::getStyleInFilename(), CmdLineOptions::getStyleOutFilename(), CmdLineOptions::getSVGHeight(), CmdLineOptions::getSVGWidth(), CmdLineOptions::getTagsFile(), CmdLineOptions::getThemeName(), DataDir::getThemePath(), CmdLineOptions::getWrappingStyle(), CmdLineOptions::includeCharStyles(), CmdLineOptions::includeStyleDef(), CmdLineOptions::inlineCSS(), CmdLineOptions::isSkippedExt(), CmdLineOptions::orderedList(), CmdLineOptions::prettySymbols(), printBadInstallationInfo(), printConfigInfo(), CmdLineOptions::printConfigInfo(), printDebugInfo(), CmdLineOptions::printDebugInfo(), Help::printHelp(), CmdLineOptions::printHelp(), CmdLineOptions::printIndexFile(), printInstalledLanguages(), printInstalledThemes(), printIOErrorReport(), CmdLineOptions::printLineNumbers(), CmdLineOptions::printProgress(), CmdLineOptions::printVersion(), printVersionInfo(), CmdLineOptions::quietMode(), CmdLineOptions::replaceQuotes(), DataDir::searchDataDir(), DataDir::setAdditionalConfDir(), DataDir::setAdditionalDataDir(), CmdLineOptions::showLangdefs(), CmdLineOptions::showThemes(), CmdLineOptions::styleOutPathDefined(), CmdLineOptions::syntaxGiven(), CmdLineOptions::useFNamesAsAnchors(), and CmdLineOptions::validateInput().

{

      CmdLineOptions options ( argc, argv );

      // set data directory path, where /langDefs and /themes reside
      string dataDirPath = ( options.getDataDir().empty() ) ?  Platform::getAppPath() :options.getDataDir();

      if ( options.printVersion() )
      {
            printVersionInfo();
            return EXIT_SUCCESS;
      }




      dataDir.setAdditionalDataDir ( options.getAdditionalDataDir() );
      dataDir.setAdditionalConfDir ( options.getAdditionalConfDir() );

      if ( ! dataDir.searchDataDir ( dataDirPath ) )
      {
            printBadInstallationInfo();
            return EXIT_FAILURE;
      }

      if ( options.printHelp() )
      {
            Help::printHelp();
            return EXIT_SUCCESS;
      }

      if ( options.printConfigInfo() )
      {
            printConfigInfo ( options.getConfigFilePath() );
            return EXIT_SUCCESS;
      }

      if ( options.showThemes() )
      {
            return printInstalledThemes() ?EXIT_SUCCESS:EXIT_FAILURE;
      }
      if ( options.showLangdefs() )
      {
            return printInstalledLanguages() ?EXIT_SUCCESS:EXIT_FAILURE;
      }

      loadFileTypeConfig ( "filetypes", &extensions, &scriptShebangs );

      const  vector <string> inFileList=options.getInputFileNames();

      if ( options.enableBatchMode() && inFileList[0].empty() )
      {
            return EXIT_FAILURE;
      }

      string themePath=dataDir.getThemePath ( options.getThemeName() );

      auto_ptr<highlight::CodeGenerator> generator ( highlight::CodeGenerator::getInstance ( options.getOutputType() ) );


      generator->setHTMLAttachAnchors ( options.attachLineAnchors() );
      generator->setHTMLOrderedList ( options.orderedList() );
      generator->setHTMLInlineCSS ( options.inlineCSS() );
      generator->setHTMLEnclosePreTag ( options.enclosePreTag() );
      generator->setHTMLAnchorPrefix ( options.getAnchorPrefix() );
      generator->setHTMLClassName ( options.getClassName() );

      generator->setLATEXReplaceQuotes ( options.replaceQuotes() );
      generator->setLATEXNoShorthands ( options.disableBabelShorthands() );
      generator->setLATEXPrettySymbols ( options.prettySymbols() );

      generator->setRTFPageSize ( options.getPageSize() );
      generator->setRTFCharStyles ( options.includeCharStyles() );

      generator->setSVGSize ( options.getSVGWidth(),  options.getSVGHeight() );

      generator->setValidateInput ( options.validateInput() );
      generator->setStyleInputPath ( options.getStyleInFilename() );
      generator->setStyleOutputPath ( options.getStyleOutFilename() );
      generator->setIncludeStyle ( options.includeStyleDef() );
      generator->setPrintLineNumbers ( options.printLineNumbers(), options.getNumberStart() );
      generator->setPrintZeroes ( options.fillLineNrZeroes() );
      generator->setFragmentCode ( options.fragmentOutput() );
      generator->setPreformatting ( options.getWrappingStyle(),
                                    ( generator->getPrintLineNumbers() ) ?
                                    options.getLineLength() - options.getNumberWidth() : options.getLineLength(),
                                    options.getNumberSpaces() );

      generator->setEncoding ( options.getEncoding() );
      generator->setBaseFont ( options.getBaseFont() ) ;
      generator->setBaseFontSize ( options.getBaseFontSize() ) ;
      generator->setLineNumberWidth ( options.getNumberWidth() );

      map <int,string> markedLines = options.getMarkLines();
      if ( !markedLines.empty() )
      {
            map<int, string>::iterator it;
            for ( it=markedLines.begin(); it!=markedLines.end();it++ )
            {
                  generator->addMarkedLine ( it->first, it->second );
            }
      }

      bool styleFileWanted = !options.fragmentOutput() || options.styleOutPathDefined();


      if ( !generator->initTheme ( themePath ) )
      {
            cerr << "highlight: Could not find style "
            << themePath
            << ".\n";
            return EXIT_FAILURE;
      }
      /*
        if (!options.getIndentScheme().empty()){
          string indentSchemePath =
                    dataDir.getIndentSchemesPath(options.getIndentScheme()+".indent");
          if (!generator->initIndentationScheme(indentSchemePath)){
              cerr << "highlight: Could not find indentation scheme "
                   << indentSchemePath
                   << ".\n";
             return EXIT_FAILURE;
          }
        }*/
      bool formattingEnabled = generator->initIndentationScheme ( options.getIndentScheme() );

      if ( !formattingEnabled && !options.getIndentScheme().empty() )
      {
            cerr << "highlight: Undefined indentation scheme "
            << options.getIndentScheme()
            << ".\n";
            return EXIT_FAILURE;
      }

      if ( !options.getTagsFile().empty() )
      {
            if ( !generator->initTagInformation ( options.getTagsFile() ) )
            {
                  cerr << "highlight: Could not load ctags file "
                  << options.getTagsFile()
                  << ".\n";
                  return EXIT_FAILURE;
            }
      }

      string outDirectory = options.getOutDirectory();
#ifndef WIN32
      ifstream dirTest ( outDirectory.c_str() );
      if ( !outDirectory.empty() && !options.quietMode() && !dirTest )
      {
            cerr << "highlight: Output directory \""
            << outDirectory
            << "\" does not exist.\n";
            return EXIT_FAILURE;
      }
      dirTest.close();
#endif

      bool initError=false, IOError=false;
      unsigned int fileCount=inFileList.size(),
                             fileCountWidth=getNumDigits ( fileCount ),
                                            i=0,
                                              numBadFormatting=0,
                                                               numBadInput=0,
                                                                           numBadOutput=0;

      vector<string> badFormattedFiles, badInputFiles, badOutputFiles;
      string inFileName, outFilePath;
      string suffix, lastSuffix;

      if ( options.syntaxGiven() )  // user defined language definition, valid for all files
      {
            suffix = guessFileType ( options.getLanguage() );
      }

      while ( i < fileCount && !initError )
      {
            if ( !options.syntaxGiven() )  // determine file type for each file
            {
                  suffix = guessFileType ( getFileSuffix ( inFileList[i] ), inFileList[i] );
            }
            if ( suffix.empty() )
            {
                  if ( !options.enableBatchMode() )
                        cerr << "highlight: Undefined language definition. Use --"
                        << OPT_SYNTAX << " option.\n";
                  if ( !options.forceOutput() )
                  {
                        initError = true;
                        break;
                  }
            }

            if ( suffix != lastSuffix )
            {
                  string langDefPath=dataDir.getLangPath ( suffix+".lang" );
                  highlight::LoadResult loadRes= generator->loadLanguage ( langDefPath );
                  if ( loadRes==highlight::LOAD_FAILED_REGEX )
                  {
                        cerr << "highlight: Regex error ( "
                        << generator->getLanguage().getFailedRegex()
                        << " ) in "<<suffix<<".lang\n";
                        initError = true;
                        break;
                  }
                  else if ( loadRes==highlight::LOAD_FAILED )
                  {
                        // do also ignore error msg if --syntax parameter should be skipped
                        if ( !options.isSkippedExt ( suffix ) )
                        {
                              cerr << "highlight: Unknown source file extension \""
                              << suffix
                              << "\".\n";
                        }
                        if ( !options.forceOutput() )
                        {
                              initError = true;
                              break;
                        }
                  }
                  if ( options.printDebugInfo() && loadRes==highlight::LOAD_NEW )
                  {
                        printDebugInfo ( generator->getLanguage(), langDefPath );
                  }
                  lastSuffix = suffix;
            }

            string::size_type pos= ( inFileList[i] ).find_last_of ( Platform::pathSeparator );
            inFileName = inFileList[i].substr ( pos+1 );

            if ( options.enableBatchMode() )
            {

                  outFilePath = outDirectory;
                  outFilePath += inFileName;
                  outFilePath += options.getOutFileSuffix();

                  if ( !options.quietMode() )
                  {
                        if ( options.printProgress() )
                        {
                              printProgressBar ( fileCount, i+1 );
                        }
                        else
                        {
                              printCurrentAction ( outFilePath, fileCount, i+1, fileCountWidth );
                        }
                  }
            }
            else
            {
                  outFilePath = options.getSingleOutFilename();
                  if ( outFilePath.size() && outFilePath==options.getSingleInFilename() )
                  {
                        cerr  << "highlight: Output path equals input path: \""
                        << outFilePath << "\".\n";
                        initError = true;
                        break;
                  }

            }

            if ( options.useFNamesAsAnchors() )
            {
                  generator->setHTMLAnchorPrefix ( inFileName );
            }

            generator->setTitle ( options.getDocumentTitle().empty() ?
                                  inFileList[i]:options.getDocumentTitle() );

            generator->setKeyWordCase ( options.getKeywordCase() );

            highlight::ParseError error = generator->generateFile ( inFileList[i], outFilePath );

            if ( error==highlight::BAD_INPUT )
            {
                  if ( numBadInput++ < IO_ERROR_REPORT_LENGTH || options.printDebugInfo() )
                  {
                        badInputFiles.push_back ( inFileList[i] );
                  }
            }
            else if ( error==highlight::BAD_OUTPUT )
            {
                  if ( numBadOutput++ < IO_ERROR_REPORT_LENGTH || options.printDebugInfo() )
                  {
                        badOutputFiles.push_back ( outFilePath );
                  }
            }
            if ( formattingEnabled && !generator->formattingIsPossible() )
            {
                  if ( numBadFormatting++ < IO_ERROR_REPORT_LENGTH || options.printDebugInfo() )
                  {
                        badFormattedFiles.push_back ( outFilePath );
                  }
            }
            ++i;
      }


      if ( i &&   !options.includeStyleDef()
              && styleFileWanted
              && options.formatSupportsExtStyle() )
      {
            bool useStdout =  options.getStyleOutFilename() =="stdout";
            string cssOutFile=outDirectory  + options.getStyleOutFilename();
            bool success=generator->printExternalStyle ( useStdout?"":cssOutFile );
            if ( !success )
            {
                  cerr << "highlight: Could not write " << cssOutFile <<".\n";
                  IOError = true;
            }
            if ( useStdout )
            {
                  return EXIT_SUCCESS;
            }
      }

      if ( i && options.printIndexFile() )
      {
            bool success=generator -> printIndexFile ( inFileList, outDirectory );
            if ( !success )
            {
                  cerr << "highlight: Could not write index file.\n";
                  IOError = true;
            }
      }

      if ( numBadInput )
      {
            printIOErrorReport ( numBadInput, badInputFiles, "read input" );
            IOError = true;
      }
      if ( numBadOutput )
      {
            printIOErrorReport ( numBadOutput, badOutputFiles, "write output" );
            IOError = true;
      }
      if ( numBadFormatting )
      {
            printIOErrorReport ( numBadFormatting, badFormattedFiles, "reformat" );
      }
      return ( initError || IOError ) ? EXIT_FAILURE : EXIT_SUCCESS;
}


Generated by  Doxygen 1.6.0   Back to index