1 using System;
  2 using System.IO;
  3 using System.Collections;
  4 using System.RegularExpressions;
  5 
  6 void PrintHelp()
  7 {
  8     Console.Out() << "Usage: sgrep \"PATTERN\" FILES..." << endl();
  9 }
 10 
 11 int main(int argcconst char** argv)
 12 {
 13     ustring pattern;
 14     List<string> fileNames;
 15     for (int i = 1; i < argc; ++i;)
 16     {
 17         string arg = argv[i];
 18         if (arg.StartsWith("--"))
 19         {
 20             if (arg == "--help")
 21             {
 22                 PrintHelp();
 23                 return 1;
 24             }
 25             else
 26             {
 27                 int errorId = AllocateError("unknown option \'" + arg + "\'");
 28                 Console.Error() << GetErrorMessage(errorId) << endl();
 29                 return 1;
 30             }
 31         }
 32         else if (arg.StartsWith("-"))
 33         {
 34             for (char o : arg)
 35             {
 36                 if (o == 'h')
 37                 {
 38                     PrintHelp();
 39                     return 1;
 40                 }
 41                 else
 42                 {
 43                     int errorId = AllocateError("unknown option \'-" + string(o) + "\'");
 44                     Console.Error() << GetErrorMessage(errorId) << endl();
 45                     return 1;
 46                 }
 47             }
 48         }
 49         else
 50         {
 51             if (pattern.IsEmpty())
 52             {
 53                 auto utfResult = ToUtf32(arg);
 54                 if (utfResult.Error())
 55                 {
 56                     Console.Error() << utfResult.GetErrorMessage() << endl();
 57                     return 1;
 58                 }
 59                 ustring s = Rvalue(utfResult.Value());
 60                 pattern = u".*" + s + u".*";
 61             }
 62             else
 63             {
 64                 fileNames.Add(arg);
 65             }
 66         }
 67     }
 68     Context context;
 69     auto nfaResult = CompileRegularExpressionPattern(contextpattern);
 70     if (nfaResult.Error())
 71     {
 72         Console.Error() << nfaResult.GetErrorMessage() << endl();
 73         return 1;
 74     }
 75     Nfa nfa = nfaResult.Value();
 76     for (const string& fileName : fileNames)
 77     {
 78         Console.Out() << fileName << ":" << endl();
 79         auto readResult = File.ReadAllLines(fileName);
 80         if (readResult.Error())
 81         {
 82             Console.Error() << readResult.GetErrorMessage() << endl();
 83             return 1;
 84         }
 85         List<string> lines = Rvalue(readResult.Value());
 86         long n = lines.Count();
 87         for (long i = 0; i < n; ++i;)
 88         {
 89             auto utfResult = ToUtf32(lines[i]);
 90             if (utfResult.Error())
 91             {
 92                 Console.Error() << utfResult.GetErrorMessage() << endl();
 93                 return 1;
 94             }
 95             ustring line = Rvalue(utfResult.Value());
 96             if (PatternMatch(linenfa))
 97             {
 98                 Console.Out() << ToString(i + 1) << ": " << line << endl();
 99             }
100         }
101     }
102     return 0;
103 }