Changeset 5016


Ignore:
Timestamp:
Apr 29, 2016, 9:49:55 AM (19 months ago)
Author:
aambarts
Message:

Adding -gs flag that pipes icgrep output to grep, thus giving colorized output and possibly backtracking capability in the future.

File:
1 edited

Legend:

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

    r5015 r5016  
    1818#include <mutex>
    1919
     20#include <iostream> // MEEE
     21
    2022static cl::OptionCategory aRegexSourceOptions("Regular Expression Options",
    2123                                       "These options control the regular expression source.");
     
    2426
    2527static cl::opt<bool> CaseInsensitive("i", cl::desc("Ignore case distinctions in the pattern and the file."), cl::cat(aRegexSourceOptions));
     28
    2629
    2730static cl::list<std::string> regexVector("e", cl::desc("Regular expression"), cl::ZeroOrMore, cl::cat(aRegexSourceOptions));
     
    3033
    3134static cl::opt<int> Threads("t", cl::desc("Total number of threads."), cl::init(1));
     35
     36static cl::opt<bool> GrepSupport("gs", cl::desc("Grep support. Pipe the output of icgrep into grep. \
     37         Gives you colored output + back-referencing capability."), cl::cat(aRegexSourceOptions));
     38
    3239
    3340static std::string allREs;
     
    107114    pthread_exit(NULL);
    108115}
     116
     117
     118// Returns true if the command line argument shouldn't be passed to icGrep or Grep.
     119bool isArgUnwantedForAll(char *argument) {
     120
     121    bool isUnwated = false;
     122    std::vector<std::string> unwantedFlags = {"-gs"};
     123
     124    for (int i=0; i<unwantedFlags.size(); i++){   
     125        if (strcmp(argument, unwantedFlags[i].c_str()) == 0) {
     126            isUnwated = true;
     127        }
     128    }
     129
     130    return isUnwated;
     131}
     132// Filters out the command line strings that shouldn't be passed on to Grep
     133bool isArgUnwantedForGrep(char *argument) {
     134    bool isUnwated = false;
     135    std::vector<std::string> unwantedFlags = {"-n"};
     136
     137    for (int i = 0; i < unwantedFlags.size(); i++){
     138        if (strcmp(argument, unwantedFlags[i].c_str()) == 0) {
     139            isUnwated = true;
     140        }
     141    }
     142
     143    for (int i = 0; i < inputFiles.size(); i++){    // filter out input content files.
     144        if (strcmp(argument, inputFiles[i].c_str()) == 0) {
     145            isUnwated = true;
     146        }
     147    }
     148
     149    return isUnwated;
     150}
     151// Filters out the command line strings that shouldn't be passed on to IcGrep
     152bool isArgUnwantedForIcGrep(char *argument) {
     153    bool isUnwated = false;
     154    std::vector<std::string> unwantedFlags = {"-c"};
     155
     156    for (int i=0; i<unwantedFlags.size(); i++){
     157        if (strcmp(argument, unwantedFlags[i].c_str()) == 0) {
     158            isUnwated = true;
     159        }
     160    }
     161
     162    return isUnwated;
     163}
     164
     165/*
     166* Constructs a shell command that calls icgrep and then pipes the output to grep.
     167* Then executs this shell command using the "system()" function.
     168* This allows the output to be colored since all output is piped to grep.
     169*/
     170void pipeIcGrepOutputToGrep(int argc, char *argv[]) {
     171    std::string icGrepArguments = "";
     172    std::string grepArguments = "";
     173
     174    // Construct the shell arguments for icgrep and grep
     175    // by filtering out the command line arguments passed into this process.
     176    for (int i=1; i< argc; i++) {
     177        if (!isArgUnwantedForAll(argv[i])) {
     178
     179            if (!isArgUnwantedForIcGrep(argv[i])) {
     180                icGrepArguments.append(argv[i]);
     181                icGrepArguments.append(" ");
     182            }
     183
     184            if (!isArgUnwantedForGrep(argv[i])) {
     185                grepArguments.append(argv[i]);
     186                grepArguments.append(" ");
     187            }
     188        }
     189    }
     190
     191    std::string systemCall = "./icgrep ";
     192    systemCall.append(icGrepArguments);
     193    systemCall.append(" ");
     194    systemCall.append(" | grep --color=always -P ");
     195    systemCall.append(grepArguments);
     196    system(systemCall.c_str());
     197}
     198
    109199
    110200int main(int argc, char *argv[]) {
     
    139229    re::RE * re_ast = get_icgrep_RE();
    140230    std::string module_name = "grepcode:" + sha1sum(allREs) + ":" + std::to_string(globalFlags);
     231
     232    if (GrepSupport) {  // Calls icgrep again on command line and passes output to grep.
     233        pipeIcGrepOutputToGrep(argc, argv);
     234        return 0;   // icgrep is called again, so we need to end this process.
     235    }
    141236   
    142237    GrepEngine grepEngine;
Note: See TracChangeset for help on using the changeset viewer.