9ad80956a8fae57d085c914fc6342b7f0463df17
[WebThing.git] / Config.cs
1 using System;
2 using System.IO;
3 using System.Collections.Generic;
4 using System.Text.RegularExpressions;
5
6 namespace bytex64.WebThing {
7     public class Config {
8         public static string ConfigurationPrefix = "/etc";
9
10         public static Dictionary<string,string> Options;
11         public static Dictionary<string,Dictionary<string,string>> PluginOptions;
12         public static string[] Arguments;
13         private static HashSet<string> CommandLineOptions;
14
15         public static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
16         public static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
17
18         static Config() {
19             Options = new Dictionary<string,string>();
20             PluginOptions = new Dictionary<string,Dictionary<string,string>>();
21             CommandLineOptions = new HashSet<string>();
22         }
23
24         private static void ParseOption(string key, string val) {
25             Match m = item_re.Match(key);
26             string plugin = m.Groups[1].Value;
27             string name = m.Groups[2].Value;
28             if (plugin.Length > 0) {   // Plugin Option
29                 if (!PluginOptions.ContainsKey(plugin))
30                     PluginOptions[plugin] = new Dictionary<string,string>();
31                 PluginOptions[plugin][name] = val;
32             } else {                   // Global Option
33                 Options[name] = val;
34             }
35         }
36
37         public static void DumpOptions() {
38             Console.WriteLine("Options:");
39             foreach (string key in Options.Keys)
40                 Console.WriteLine("   {0}\t{1}", key, Options[key]);
41
42             Console.WriteLine("Plugin Options:");
43             foreach (string plugin in PluginOptions.Keys) {
44                 Console.WriteLine("    {0}", plugin);
45                 foreach (string key in PluginOptions[plugin].Keys)
46                     Console.WriteLine("        {0}\t{1}", key, PluginOptions[plugin][key]);
47             }
48
49             Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
50         }
51
52         public static void ParseCommandLine() {
53             Queue<string> q = new Queue<string>();
54             foreach (string s in System.Environment.GetCommandLineArgs()) {
55                 q.Enqueue(s);
56             }
57             q.Dequeue();   // Remove self argument
58
59             Regex SimpleOpt = new Regex(@"^-([\w.]+)(?:[:=](.*))?$");
60             Regex LongOpt   = new Regex(@"^--([\w.]+)$");
61             List<string> args = new List<string>();
62
63             while (q.Count > 0) {
64                 string opt = q.Dequeue();
65                 Match m;
66                    
67                 m = SimpleOpt.Match(opt);
68                 if (m.Success) {
69                     string key = m.Groups[1].Value;
70                     string val = m.Groups[2].Value;
71                     if (val.Length > 0) {
72                         ParseOption(key, val);
73                     } else {
74                         ParseOption(key, null);
75                     }
76                     CommandLineOptions.Add(key);
77                     continue;
78                 }
79
80                 m = LongOpt.Match(opt);
81                 if (m.Success) {
82                     string key = m.Groups[1].Value;
83                     if (q.Count > 0) {
84                         string val = q.Peek();
85                         if (!(SimpleOpt.IsMatch(val) || LongOpt.IsMatch(val))) {
86                             q.Dequeue();
87                             ParseOption(key, val);
88                         } else {
89                             ParseOption(key, null);
90                         }
91                     } else {
92                         ParseOption(key, null);
93                     }
94                     CommandLineOptions.Add(key);
95                     continue;
96                 }
97
98                 // else
99
100                 args.Add(opt);
101             }
102             Arguments = args.ToArray();
103         }
104
105         public static void Load() {
106             LoadFile(ConfigurationPrefix + "/WebThing.conf");
107             LoadFile(Environment.GetEnvironmentVariable("HOME") + "/.config/WebThing/WebThing.conf");
108             if (Options.ContainsKey("config"))
109                 LoadFile(Options["config"]);
110         }
111
112         public static void LoadFile(string filename) {
113             TextReader f;
114             try {
115                 f = new StreamReader(File.OpenRead(filename));
116             } catch (FileNotFoundException) {
117                 Console.WriteLine("Could not open configuration file {0}", filename);
118                 return;
119             }
120
121             string line;
122             while ((line = f.ReadLine()) != null) {
123                 Match m = file_var_re.Match(line.Trim());
124                 if (m.Success) {
125                     string key = m.Groups[1].Value;
126                     if (!CommandLineOptions.Contains(key))
127                         ParseOption(key, m.Groups[2].Value);
128                 }
129             }
130
131             f.Close();
132         }
133     }
134 }