1 // =================================
  2 // Copyright (c) 2022 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Collections;
  8 using System.IO;
  9 using paths;
 10 
 11 int main(int argcconst char** argv)
 12 {
 13     try
 14     {
 15         int mode = -1;
 16         List<string> files;
 17         for (int i = 1; i < argc; ++i;)
 18         {
 19             string arg = argv[i];
 20             if (arg.StartsWith("--"))
 21             {
 22                 if (arg == "--help")
 23                 {
 24                     PrintHelp();
 25                     return 1;
 26                 }
 27                 else
 28                 {
 29                     throw Exception("unknown option '" + arg + "'");
 30                 }
 31             }
 32             else if (arg.StartsWith("-"))
 33             {
 34                 string options = arg.Substring(1);
 35                 if (options.IsEmpty())
 36                 {
 37                     throw Exception("unknown option '" + arg + "'");
 38                 }
 39                 else
 40                 {
 41                     bool unknown = false;
 42                     string uo;
 43                     for (char o : options)
 44                     {
 45                         switch (o)
 46                         {
 47                             case 'h':
 48                             {
 49                                 PrintHelp();
 50                                 return 1;
 51                             }
 52                             default:
 53                             {
 54                                 unknown = true;
 55                                 uo.Append(o);
 56                                 break;
 57                             }
 58                         }
 59                         if (unknown)
 60                         {
 61                             throw Exception("unknown option '-" + uo + "'");
 62                         }
 63                     }
 64                 }
 65             }
 66             else
 67             {
 68                 if (mode == -1)
 69                 {
 70                     if (arg.Length() < 3)
 71                     {
 72                         throw Exception("invalid mode argument: three or more octal digits expected");
 73                     }
 74                     for (char o : arg)
 75                     {
 76                         if (o < '0' || o > '7')
 77                         {
 78                             throw Exception("invalid mode argument: octal digits expected");
 79                         }
 80                     }
 81                     mode = cast<int>(ParseOctal(arg));
 82                 }
 83                 else
 84                 {
 85                     files.Add(GetFullPath(arg));
 86                 }
 87             }
 88         }
 89         if (files.IsEmpty())
 90         {
 91             throw Exception("no files specified");
 92         }
 93         if (!HasEnv("SHELL"))
 94         {
 95             files = Expand(files);
 96         }
 97         ChangeMode(modefiles);
 98     }
 99     catch (const Exception& ex)
100     {
101         Console.Error() << ex.ToString() << endl();
102         return 1;
103     }
104     return 0;
105 }