source: icGREP/icgrep-devel/icgrep/icgrep.cpp @ 5944

Last change on this file since 5944 was 5944, checked in by cameron, 13 months ago

Common command line file selection utility for icgrep, wc ...

File size: 4.8 KB
Line 
1/*
2 *  Copyright (c) 2014-8 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <cstdio>
8#include <vector>
9#include <llvm/Support/CommandLine.h>
10#include <llvm/Support/ErrorHandling.h>
11#include <llvm/Support/Signals.h>
12#include <llvm/Support/raw_ostream.h>
13#include <re/re_alt.h>
14#include <re/re_seq.h>
15#include <re/re_start.h>
16#include <re/re_end.h>
17#include <re/parsers/parser.h>
18#include <re/re_utility.h>
19#include <grep/grep_engine.h>
20#include <grep_interface.h>
21#include <fstream>
22#include <string>
23#include <toolchain/toolchain.h>
24#include <re/re_toolchain.h>
25#include <pablo/pablo_toolchain.h>
26#include <util/file_select.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29
30using namespace llvm;
31
32static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
33
34static cl::opt<bool> ByteMode("enable-byte-mode", cl::desc("Process regular expressions in byte mode"));
35
36static cl::opt<int> REsPerGroup("re-num", cl::desc("Number of regular expressions processed by each kernel."), cl::init(0));
37
38static re::ModeFlagSet globalFlags = re::MULTILINE_MODE_FLAG;
39
40std::vector<re::RE *> readExpressions() {
41 
42    if (grep::FileFlag != "") {
43        std::ifstream regexFile(grep::FileFlag.c_str());
44        std::string r;
45        if (regexFile.is_open()) {
46            while (std::getline(regexFile, r)) {
47                grep::RegexpVector.push_back(r);
48            }
49            regexFile.close();
50        }
51    }
52   
53    // if there are no regexes specified through -e or -f, the first positional argument
54    // must be a regex, not an input file.
55   
56    if (grep::RegexpVector.size() == 0) {
57        grep::RegexpVector.push_back(inputFiles[0]);
58        inputFiles.erase(inputFiles.begin());
59    }
60    if (grep::IgnoreCaseFlag) {
61        globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
62    }
63
64    std::vector<re::RE *> REs;
65    for (unsigned i = 0; i < grep::RegexpVector.size(); i++) {
66        re::RE * re_ast = re::RE_Parser::parse(grep::RegexpVector[i], globalFlags, grep::RegexpSyntax, ByteMode);
67        REs.push_back(re_ast);
68    }
69
70   
71    // If there are multiple REs, combine them into groups.
72    // A separate kernel will be created for each group.
73    if (REs.size() > 1) {
74        codegen::SegmentPipelineParallel = true;
75        if (REsPerGroup == 0) {
76            // If no grouping factor is specified, we use a default formula.
77            REsPerGroup = (REs.size() + codegen::ThreadNum) / (codegen::ThreadNum + 1);
78        }
79        std::vector<re::RE *> groups;
80        auto start = REs.begin();
81        auto end = start + REsPerGroup;
82        while (end < REs.end()) {
83            groups.push_back(re::makeAlt(start, end));
84            start = end;
85            end += REsPerGroup;
86        }
87        if ((REs.end() - start) > 1) {
88            groups.push_back(re::makeAlt(start, REs.end()));
89        } else {
90            groups.push_back(*start);
91        }
92    REs.swap(groups);
93    }
94    for (re::RE *& re_ast : REs) {
95        assert (re_ast);
96        if (grep::WordRegexpFlag) {
97            re_ast = re::makeSeq({re::makeWordBoundary(), re_ast, re::makeWordBoundary()});
98        }
99        if (grep::LineRegexpFlag) {
100            re_ast = re::makeSeq({re::makeStart(), re_ast, re::makeEnd()});
101        }
102    }
103
104    return REs;
105}
106
107
108int main(int argc, char *argv[]) {
109
110    grep::InitializeCommandLineInterface(argc, argv);
111   
112    auto REs = readExpressions();
113
114    std::vector<std::string> allFiles = argv::getFullFileList(inputFiles);
115    if (allFiles.empty()) {
116        allFiles = { "-" };
117    }
118    else if ((allFiles.size() > 1) && !grep::NoFilenameFlag) {
119        grep::WithFilenameFlag = true;
120    }
121
122    grep::GrepEngine * grepEngine = nullptr;
123   
124    switch (grep::Mode) {
125        case grep::NormalMode:
126            grepEngine = new grep::EmitMatchesEngine(); break;
127        case grep::CountOnly:
128            grepEngine = new grep::CountOnlyEngine(); break;
129        case grep::FilesWithMatch:
130        case grep::FilesWithoutMatch:
131            grepEngine = new grep::MatchOnlyEngine(grep::Mode == grep::FilesWithoutMatch); break;
132        case grep::QuietMode:
133            grepEngine = new grep::QuietModeEngine(); break;
134        default: llvm_unreachable("Invalid grep mode!");
135    }
136               
137    if (grep::UnicodeLinesFlag) {
138        grepEngine->setRecordBreak(grep::GrepRecordBreakKind::Unicode);
139    } else if (grep::NullDataFlag) {
140        grepEngine->setRecordBreak(grep::GrepRecordBreakKind::Null);
141    } else {
142        grepEngine->setRecordBreak(grep::GrepRecordBreakKind::LF);
143    }
144    grepEngine->initREs(REs);
145    grepEngine->grepCodeGen();
146    grepEngine->initFileResult(allFiles);
147    bool matchFound = grepEngine->searchAllFiles();
148    delete(grepEngine);
149   
150    return matchFound ? grep::MatchFoundExitCode : grep::MatchNotFoundExitCode;
151}
Note: See TracBrowser for help on using the repository browser.