Ignore:
Timestamp:
May 29, 2017, 11:01:22 AM (2 years ago)
Author:
cameron
Message:

Recognize all standard Posix and GNU grep flags; support in progress

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r5476 r5480  
    3636using namespace llvm;
    3737
    38 static cl::OptionCategory LegacyGrepOptions("A. Standard Grep Options",
    39                                        "These are standard grep options intended for compatibility with typical grep usage.");
    40 
    41 
    42 static cl::opt<bool> UTF_16("UTF-16", cl::desc("Regular expressions over the UTF-16 representation of Unicode."), cl::cat(LegacyGrepOptions));
    43 static cl::OptionCategory EnhancedGrepOptions("B. Enhanced Grep Options",
    44                                        "These are additional options for icgrep functionality and performance.");
    45 
     38static cl::opt<bool> UTF_16("UTF-16", cl::desc("Regular expressions over the UTF-16 representation of Unicode."));
    4639
    4740static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
    4841
    49 static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(LegacyGrepOptions));
    50 static cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""), cl::cat(LegacyGrepOptions));
    51 
    5242static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(1));
    5343
    54 static cl::opt<bool> MultiGrepKernels("enable-multigrep-kernels", cl::desc("Construct separated kernels for each regular expression"), cl::cat(EnhancedGrepOptions));
     44static cl::opt<bool> MultiGrepKernels("enable-multigrep-kernels", cl::desc("Construct separated kernels for each regular expression"));
    5545static cl::opt<int> REsPerGroup("re-num", cl::desc("Number of regular expressions processed by each kernel."), cl::init(1));
    5646static std::vector<std::string> allFiles;
     
    6050std::vector<re::RE *> readExpressions() {
    6151 
    62     if (RegexFilename != "") {
    63         std::ifstream regexFile(RegexFilename.c_str());
     52    if (grep::FileFlag != "") {
     53        std::ifstream regexFile(grep::FileFlag.c_str());
    6454        std::string r;
    6555        if (regexFile.is_open()) {
    6656            while (std::getline(regexFile, r)) {
    67                 regexVector.push_back(r);
     57                grep::RegexpVector.push_back(r);
    6858            }
    6959            regexFile.close();
     
    7464    // must be a regex, not an input file.
    7565   
    76     if (regexVector.size() == 0) {
    77         regexVector.push_back(inputFiles[0]);
     66    if (grep::RegexpVector.size() == 0) {
     67        grep::RegexpVector.push_back(inputFiles[0]);
    7868        inputFiles.erase(inputFiles.begin());
    7969    }
     
    8373
    8474    std::vector<re::RE *> REs;
    85     for (unsigned i = 0; i < regexVector.size(); i++) {
    86         re::RE * re_ast = re::RE_Parser::parse(regexVector[i], globalFlags, grep::RegexpSyntax);
     75    for (unsigned i = 0; i < grep::RegexpVector.size(); i++) {
     76        re::RE * re_ast = re::RE_Parser::parse(grep::RegexpVector[i], globalFlags, grep::RegexpSyntax);
    8777        REs.push_back(re_ast);
    8878    }
     
    149139bool excludeDirectory(boost::filesystem::path dirpath) { return dirpath.filename() == ".svn";}
    150140
     141// Determine whether to skip a path based on -D skip or -d skip settings.
     142bool skip_path(boost::filesystem::path p) {
     143    using namespace boost::filesystem;
     144    switch (status(p).type()) {
     145        case directory_file: return grep::DirectoriesFlag == grep::Skip;
     146        case block_file:
     147        case character_file:
     148        case fifo_file:
     149        case socket_file:
     150            return grep::DevicesFlag == grep::Skip;
     151        default:
     152            return false;
     153    }
     154}
     155
    151156std::vector<std::string> getFullFileList(cl::list<std::string> & inputFiles) {
    152157    using namespace boost::filesystem;
     
    154159    std::vector<std::string> expanded_paths;
    155160    boost::system::error_code errc;
    156     if (grep::DereferenceRecursiveFlag) {
    157         grep::RecursiveFlag = true;
    158     }
    159161    for (const std::string & f : inputFiles) {
    160 //        if (f == "-") {
    161 //            continue;
    162 //        }
     162        //        if (f == "-") {
     163        //            continue;
     164        //        }
    163165        path p(f);
    164         if (LLVM_UNLIKELY(grep::RecursiveFlag && is_directory(p))) {
     166        if (skip_path(p)) {
     167            continue;
     168        }
     169        if (LLVM_UNLIKELY((grep::DirectoriesFlag == grep::Recurse) && is_directory(p))) {
    165170            if (!excludeDirectory(p)) {
    166171                recursive_directory_iterator di(p, follow_symlink, errc), end;
    167172                if (errc) {
    168173                    // If we cannot enter the directory, keep it in the list of files.
    169                     expanded_paths.push_back(f); 
     174                    expanded_paths.push_back(f);
    170175                    continue;
    171176                }
     
    177182                        }
    178183                    } else {
    179                         expanded_paths.push_back(e.string());
     184                        if (!skip_path(e)) expanded_paths.push_back(e.string());
    180185                    }
    181186                    di.increment(errc);
    182187                    if (errc) {
    183                         expanded_paths.push_back(e.string()); 
     188                        expanded_paths.push_back(e.string());
    184189                    }
    185190                }
     
    191196    return expanded_paths;
    192197}
    193 
    194198
    195199int main(int argc, char *argv[]) {
Note: See TracChangeset for help on using the changeset viewer.