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

Last change on this file since 4415 was 4412, checked in by cameron, 4 years ago

Command line parameter -i for case-insensitive matching

File size: 5.4 KB
Line 
1/*
2 *  Copyright (c) 2014 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 <string>
8#include <iostream>
9#include <fstream>
10
11#include "icgrep.h"
12#include "utf_encoding.h"
13#include "compiler.h"
14#include "pablo/pablo_compiler.h"
15#include "do_grep.h"
16
17#include "llvm/Support/CommandLine.h"
18
19int main(int argc, char *argv[]) {
20    StringMap<cl::Option*> Map;
21    cl::getRegisteredOptions(Map);
22    Map["time-passes"]->setHiddenFlag(cl::Hidden);
23    Map["disable-spill-fusing"]->setHiddenFlag(cl::Hidden);
24    Map["enable-misched"]->setHiddenFlag(cl::Hidden);
25    Map["enable-tbaa"]->setHiddenFlag(cl::Hidden);
26    Map["exhaustive-register-search"]->setHiddenFlag(cl::Hidden);
27    Map["fatal-assembler-warnings"]->setHiddenFlag(cl::Hidden);
28    Map["join-liveintervals"]->setHiddenFlag(cl::Hidden);
29    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
30    Map["mc-x86-disable-arith-relaxation"]->setHiddenFlag(cl::Hidden);
31    Map["limit-float-precision"]->setHiddenFlag(cl::Hidden);
32    Map["print-after-all"]->setHiddenFlag(cl::Hidden);
33    Map["print-before-all"]->setHiddenFlag(cl::Hidden);
34    Map["print-machineinstrs"]->setHiddenFlag(cl::Hidden);
35    Map["regalloc"]->setHiddenFlag(cl::Hidden);
36    Map["rng-seed"]->setHiddenFlag(cl::Hidden);
37    Map["spiller"]->setHiddenFlag(cl::Hidden);
38    Map["stackmap-version"]->setHiddenFlag(cl::Hidden);
39    Map["x86-asm-syntax"]->setHiddenFlag(cl::Hidden);
40    Map["verify-debug-info"]->setHiddenFlag(cl::Hidden);
41    Map["verify-dom-info"]->setHiddenFlag(cl::Hidden);
42    Map["verify-loop-info"]->setHiddenFlag(cl::Hidden);
43    Map["verify-regalloc"]->setHiddenFlag(cl::Hidden);
44    Map["verify-scev"]->setHiddenFlag(cl::Hidden);
45   
46   
47    cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
48                                          "These options control the regular expression source.");
49   
50    cl::OptionCategory bGrepOutputOptions("Output Options",
51                                         "These options control the output.");
52   
53    int firstInputFile = 1;  // Normal case when first positional arg is a regex.
54    cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
55   
56    cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."), cl::cat(bGrepOutputOptions));
57    cl::alias CountOnlyLong("count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
58   
59    cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."), cl::cat(bGrepOutputOptions));
60    cl::alias ShowFileNamesLong("with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
61   
62    cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
63    cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."), cl::cat(bGrepOutputOptions));
64    cl::alias ShowLineNumbersLong("line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
65   
66    cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
67   
68    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(aRegexSourceOptions));
69 
70    // Does -b mean the byte offset of the line, or the byte offset of the match start within the line?
71    //static cl::opt<bool>ShowByteOffsets("b", cl::desc("Show the byte offset with each matching line."));
72    //cl::alias ShowByteOffsetsLong("-byte-offset", cl::desc("Alias for -b"), cl::aliasopt(ShowByteOffsets));
73
74    Encoding encoding(Encoding::Type::UTF_8, 8);
75
76    cl::ParseCommandLineOptions(argc, argv);
77   
78    //std::vector<std::string> regexVector;
79    if (RegexFilename != "") {
80        std::ifstream regexFile(RegexFilename.c_str());
81        std::string r;
82        if (regexFile.is_open()) {
83            while (std::getline(regexFile, r)) {
84                regexVector.push_back(r);
85            }
86            regexFile.close();
87        }
88    }
89   
90    // if there are no regexes specified through -e or -f, the first positional argument
91    // must be a regex, not an input file.
92   
93    if (regexVector.size() == 0) {
94        regexVector.push_back(inputFiles[0]);
95        firstInputFile = 1;
96    }
97    else {
98        firstInputFile = 0;
99    }
100   
101    re::ModeFlagSet globalFlags = 0;
102    if (CaseInsensitive) globalFlags |= re::CASE_INSENSITIVE_MODE_FLAG;
103   
104    const auto llvm_codegen = icgrep::compile(encoding, regexVector, globalFlags);
105
106    if (llvm_codegen.process_block_fptr != 0) {
107        void (*FP)(const Basis_bits &basis_bits, BitBlock carry_q[], BitBlock advance_q[], Output &output) = 
108           (void (*)(const Basis_bits &basis_bits, BitBlock carry_q[], BitBlock advance_q[], Output &output))(void*)llvm_codegen.process_block_fptr;
109        GrepExecutor grepEngine = GrepExecutor(llvm_codegen.carry_q_size, llvm_codegen.advance_q_size, FP);
110        grepEngine.setCountOnlyOption(CountOnly);
111        grepEngine.setShowLineNumberOption(ShowLineNumbers);
112            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
113            grepEngine.setShowFileNameOption();
114        }
115        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
116            grepEngine.doGrep(inputFiles[i].c_str());
117        }
118    }
119   
120    return 0;
121}
122
Note: See TracBrowser for help on using the repository browser.