Ignore:
Timestamp:
Dec 8, 2014, 1:17:21 PM (5 years ago)
Author:
cameron
Message:

New icgrep option parser based on LLVM CommandLine? system.

File:
1 edited

Legend:

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

    r4324 r4325  
    2424#include <sys/stat.h>
    2525
    26 int main(int argc, char *argv[])
    27 {
    28     char * inregex, * fileregex, * infilename, * outfilename;
    29     FILE *infile, *outfile, *regexfile;
    30 
    31     int fdSrc;
    32     struct stat infile_sb;
    33     char * infile_buffer;
    34 
    35     int opt_code;
    36     bool count_only_option = 0;
    37     bool print_version_option = 0;
    38     bool regex_from_file_option = 0;
    39     bool ascii_only_option = 0;
    40     bool compile_time_option = 0;
    41     bool enable_multiplexing = 0;
    42     bool print_usage = 0;
     26#include "llvm/Support/CommandLine.h"
    4327
    4428
    45     long lSize = 0;
     29   
     30int main(int argc, char *argv[]) {
     31    static cl::opt<std::string> regexp1(cl::Positional, cl::Required, cl::desc("<regexp>"));
     32    static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
     33   
     34    static cl::opt<bool>CountOnly("c", cl::desc("Count and display the matching lines per file only."));
     35    cl::alias CountOnlyLong("-count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
     36   
     37    static cl::opt<bool>ShowFileNames("H", cl::desc("Show the file name with each matching line."));
     38    cl::alias ShowFileNamesLong("-with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
     39   
     40    static cl::opt<bool>ShowLineNumbers("n", cl::desc("Show the line number with each matching line."));
     41    cl::alias ShowLineNumbersLong("-line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
     42   
     43    //cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("<filename>"));
     44 
     45    // Does -b mean the byte offset of the line, or the byte offset of the match start within the line?
     46    //static cl::opt<bool>ShowByteOffsets("b", cl::desc("Show the byte offset with each matching line."));
     47    //cl::alias ShowByteOffsetsLong("-byte-offset", cl::desc("Alias for -b"), cl::aliasopt(ShowByteOffsets));
    4648
    47     size_t result;
     49    Encoding encoding(Encoding::Type::UTF_8, 8);
    4850
    49     while ((opt_code = getopt(argc, argv, "cvftam")) != -1)
    50     {
    51         switch (opt_code)
    52         {
    53         case 'c':
    54             count_only_option = 1;
    55             break;
    56         case 'v':
    57             print_version_option = 1;
    58             break;
    59         case 'f':
    60             regex_from_file_option = 1;
    61             break;
    62         case 't':
    63             compile_time_option = 1;
    64             break;
    65         case 'a':
    66             ascii_only_option = 1;
    67             break;
    68         case 'm':
    69             enable_multiplexing = 1;
    70             break;
    71         case '?':
    72             break;
    73         default:
    74             printf ("Invalid option: %c\n", opt_code);
    75             print_usage = 1;
    76         }
    77     }
     51    cl::ParseCommandLineOptions(argc, argv);
    7852
    79     if (optind >= argc)
    80     {
    81         printf ("Too few arguments\n");
    82         print_usage = 1;
    83     }
    84 
    85     if (print_usage) {
    86         printf("Usage: %s [-a] [-c] [-f] [-m] [-t] [-v] <regex|regexfile> <inputfile> [<outputfile>]\n", argv[0]);
    87         exit(-1);
    88     }
    89 
    90     inregex = argv[optind++];
    91     if (inregex == 0)
    92     {
    93         fprintf(stderr, "Error: cannot read the regular expression.\n");
    94         exit(-1);
    95     }
    96 
    97     if (regex_from_file_option)
    98     {
    99         regexfile = fopen(inregex, "rb");
    100         if (!regexfile){
    101             fprintf(stderr, "Error: cannot open %s for processing.\n", inregex);
    102             exit(-1);
    103         }
    104 
    105         fseek (regexfile , 0 , SEEK_END);
    106         lSize = ftell (regexfile);
    107         rewind (regexfile);
    108 
    109         fileregex = (char*) malloc (sizeof(char)*lSize);
    110         if (fileregex == NULL) {fputs ("Memory error",stderr); exit (2);}
    111 
    112         result = fread (fileregex, 1, lSize, regexfile);
    113         if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
    114         fclose(regexfile);
    115 
    116         if (fileregex[lSize - 1] == '\n') fileregex[lSize - 1] = '\0';
    117     }
    118 
    119     infilename = argv[optind++];
    120 
    121 
    122     if (print_version_option)
    123     {
    124         fprintf(outfile, "Parabix icgrep implementation: August 2014\n");
    125     }
    126 
    127     Encoding encoding(ascii_only_option ? Encoding::Type::ASCII : Encoding::Type::UTF_8, 8);
    128     const auto llvm_codegen = icgrep::compile(encoding, (regex_from_file_option ? fileregex : inregex), false, enable_multiplexing);
     53    const auto llvm_codegen = icgrep::compile(encoding, regexp1, false, false);
    12954
    13055    if (llvm_codegen.process_block_fptr != 0)
     
    13459           (void (*)(const Basis_bits &basis_bits, BitBlock carry_q[], BitBlock advance_q[], Output &output))(void*)llvm_codegen.process_block_fptr;
    13560        GrepExecutor grepEngine = GrepExecutor(llvm_codegen.carry_q_size, llvm_codegen.advance_q_size, FP);
    136         grepEngine.setCountOnlyOption(count_only_option);
    137         grepEngine.doGrep(infilename);
     61        grepEngine.setCountOnlyOption(CountOnly);
     62        if (inputFiles.size() > 1) grepEngine.setShowFileNameOption();
     63        for (unsigned i = 0; i != inputFiles.size(); ++i) {
     64            grepEngine.doGrep(inputFiles[i].c_str());
     65        }
    13866    }
    139 
    140     if (regex_from_file_option) free(fileregex);
    14167
    14268    return 0;
Note: See TracChangeset for help on using the changeset viewer.