3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Text.RegularExpressions;
7 namespace bytex64.WebThing {
9 public static List<string> ConfigPath;
11 public static Dictionary<string,string> Options;
12 public static Dictionary<string,Dictionary<string,string>> PluginOptions;
13 public static string[] Arguments;
14 public static List<string> Plugins;
16 private static HashSet<string> CommandLineOptions;
17 private static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
18 private static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
19 private static Regex file_command_re = new Regex(@"^([\w]+)\s*(.*)$");
22 Options = new Dictionary<string,string>();
23 PluginOptions = new Dictionary<string,Dictionary<string,string>>();
24 CommandLineOptions = new HashSet<string>();
25 Plugins = new List<string>();
28 IDictionary Env = Environment.GetEnvironmentVariables();
29 ConfigPath = new List<string>();
30 ConfigPath.Add("/etc/WebThing");
31 if (Env.Contains("HOME")) {
32 ConfigPath.Add(Env["HOME"] + "/.config/WebThing");
33 ConfigPath.Add(Env["HOME"] + "/.WebThing");
35 string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
36 if (LocalAppData != "") {
37 ConfigPath.Add(LocalAppData + "/WebThing");
41 private static void ParseOption(string key, string val) {
42 Match m = item_re.Match(key);
43 string plugin = m.Groups[1].Value;
44 string name = m.Groups[2].Value;
45 if (plugin.Length > 0) { // Plugin Option
46 if (!PluginOptions.ContainsKey(plugin))
47 PluginOptions[plugin] = new Dictionary<string,string>();
48 PluginOptions[plugin][name] = val;
49 } else { // Global Option
62 public static void DumpOptions() {
63 Console.WriteLine("Options:");
64 foreach (string key in Options.Keys)
65 Console.WriteLine(" {0}\t{1}", key, Options[key]);
67 Console.WriteLine("Plugin Options:");
68 foreach (string plugin in PluginOptions.Keys) {
69 Console.WriteLine(" {0}", plugin);
70 foreach (string key in PluginOptions[plugin].Keys)
71 Console.WriteLine(" {0}\t{1}", key, PluginOptions[plugin][key]);
74 Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
77 public static void ParseCommandLine() {
78 Queue<string> q = new Queue<string>();
79 foreach (string s in System.Environment.GetCommandLineArgs()) {
82 q.Dequeue(); // Remove self argument
84 Regex SimpleOpt = new Regex(@"^-([\w.]+)(?:[:=](.*))?$");
85 Regex LongOpt = new Regex(@"^--([\w.]+)$");
86 List<string> args = new List<string>();
89 string opt = q.Dequeue();
92 m = SimpleOpt.Match(opt);
94 string key = m.Groups[1].Value;
95 string val = m.Groups[2].Value;
97 ParseOption(key, val);
99 ParseOption(key, null);
101 CommandLineOptions.Add(key);
105 m = LongOpt.Match(opt);
107 string key = m.Groups[1].Value;
109 string val = q.Peek();
110 if (!(SimpleOpt.IsMatch(val) || LongOpt.IsMatch(val))) {
112 ParseOption(key, val);
114 ParseOption(key, null);
117 ParseOption(key, null);
119 CommandLineOptions.Add(key);
127 Arguments = args.ToArray();
130 public static void Load() {
131 foreach (string path in ConfigPath) {
132 if (!Directory.Exists(path)) continue;
133 string[] ConfigFiles = Directory.GetFiles(path, "*.conf");
134 foreach (string file in ConfigFiles) {
138 if (Options.ContainsKey("config"))
139 LoadFile(Options["config"]);
142 public static void LoadFile(string filename) {
145 f = new StreamReader(File.OpenRead(filename));
146 } catch (FileNotFoundException) {
147 Console.WriteLine("Could not open configuration file {0}", filename);
152 while ((line = f.ReadLine()) != null) {
156 m = file_var_re.Match(line);
158 string key = m.Groups[1].Value;
159 if (!CommandLineOptions.Contains(key))
160 ParseOption(key, m.Groups[2].Value);
164 m = file_command_re.Match(line);
166 string cmd = m.Groups[1].Value;
170 Plugins.Add(m.Groups[2].Value);
173 Console.WriteLine("Unknown Command in {0}: {1}", filename, line);