3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Text.RegularExpressions;
7 namespace bytex64.WebThing {
8 public struct SearchHandlerPair {
9 public string Shortcut;
12 public SearchHandlerPair(string s, string p) {
19 public static List<string> ConfigPath;
20 public static string ConfigPathOut = null;
22 public static Dictionary<string,string> Options;
23 public static Dictionary<string,Dictionary<string,string>> PluginOptions;
24 public static string[] Arguments;
25 public static List<string> Plugins;
26 public static List<SearchHandlerPair> SearchHandlers;
28 private static HashSet<string> CommandLineOptions;
29 private static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
30 private static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
31 private static Regex file_command_re = new Regex(@"^([\w]+)\s*(.*)$");
34 Options = new Dictionary<string,string>();
35 PluginOptions = new Dictionary<string,Dictionary<string,string>>();
36 CommandLineOptions = new HashSet<string>();
37 Plugins = new List<string>();
38 SearchHandlers = new List<SearchHandlerPair>();
41 IDictionary Env = Environment.GetEnvironmentVariables();
42 ConfigPath = new List<string>();
43 ConfigPath.Add("/etc/WebThing");
44 if (Env.Contains("HOME")) {
45 string homepath = Env["HOME"] + "/.config/WebThing";
46 ConfigPath.Add(homepath);
47 if (Directory.Exists(homepath))
48 ConfigPathOut = homepath;
50 string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
51 if (LocalAppData != "") {
52 string homepath = LocalAppData + "/WebThing";
53 ConfigPath.Add(homepath);
54 if (Directory.Exists(homepath))
55 ConfigPathOut = homepath;
59 private static void ParseOption(string key, string val) {
60 Match m = item_re.Match(key);
61 string plugin = m.Groups[1].Value;
62 string name = m.Groups[2].Value;
63 if (plugin.Length > 0) { // Plugin Option
64 if (!PluginOptions.ContainsKey(plugin))
65 PluginOptions[plugin] = new Dictionary<string,string>();
66 PluginOptions[plugin][name] = val;
67 } else { // Global Option
68 switch(key.ToLower()) {
79 public static void DumpOptions() {
80 Console.WriteLine("Options:");
81 foreach (string key in Options.Keys)
82 Console.WriteLine(" {0}\t{1}", key, Options[key]);
84 Console.WriteLine("Plugin Options:");
85 foreach (string plugin in PluginOptions.Keys) {
86 Console.WriteLine(" {0}", plugin);
87 foreach (string key in PluginOptions[plugin].Keys)
88 Console.WriteLine(" {0}\t{1}", key, PluginOptions[plugin][key]);
91 Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
94 public static void ParseCommandLine() {
95 Queue<string> q = new Queue<string>();
96 foreach (string s in System.Environment.GetCommandLineArgs()) {
99 q.Dequeue(); // Remove self argument
101 Regex SimpleOpt = new Regex(@"^-([\w.]+)(?:[:=](.*))?$");
102 Regex LongOpt = new Regex(@"^--([\w.]+)$");
103 List<string> args = new List<string>();
105 while (q.Count > 0) {
106 string opt = q.Dequeue();
109 m = SimpleOpt.Match(opt);
111 string key = m.Groups[1].Value;
112 string val = m.Groups[2].Value;
113 if (val.Length > 0) {
114 ParseOption(key, val);
116 ParseOption(key, null);
118 CommandLineOptions.Add(key);
122 m = LongOpt.Match(opt);
124 string key = m.Groups[1].Value;
126 string val = q.Peek();
127 if (!(SimpleOpt.IsMatch(val) || LongOpt.IsMatch(val))) {
129 ParseOption(key, val);
131 ParseOption(key, null);
134 ParseOption(key, null);
136 CommandLineOptions.Add(key);
144 Arguments = args.ToArray();
147 public static void Load() {
148 foreach (string path in ConfigPath) {
149 if (!Directory.Exists(path)) continue;
150 string[] ConfigFiles = Directory.GetFiles(path, "*.conf");
151 foreach (string file in ConfigFiles) {
155 if (Options.ContainsKey("config"))
156 LoadFile(Options["config"]);
159 public static void LoadFile(string filename) {
162 f = new StreamReader(filename);
163 } catch (FileNotFoundException) {
164 Console.WriteLine("Could not open configuration file {0}", filename);
169 while ((line = f.ReadLine()) != null) {
173 m = file_var_re.Match(line);
175 string key = m.Groups[1].Value;
176 if (!CommandLineOptions.Contains(key))
177 ParseOption(key, m.Groups[2].Value);
181 m = file_command_re.Match(line);
183 string cmd = m.Groups[1].Value.ToLower();
186 Plugins.Add(m.Groups[2].Value);
188 case "searchhandler":
189 string[] args = Regex.Split(m.Groups[2].Value, @"\s+");
190 if (args.Length != 2) {
191 Console.WriteLine("Expecting two arguments for SearchHandler.");
194 SearchHandlers.Add(new SearchHandlerPair(args[0], args[1]));
197 Console.WriteLine("Unknown Command in {0}: {1}", filename, line);
207 public static void SaveFile(string filename) {
210 f = new StreamWriter(filename);
211 } catch (IOException) {
212 Console.WriteLine("Could not save configuration to {0}", filename);
216 foreach (string key in Options.Keys) {
217 if (CommandLineOptions.Contains(key)) continue;
218 f.WriteLine("{0} = {1}", key, Options[key]);
221 foreach (string plugin in PluginOptions.Keys) {
222 foreach (string key in PluginOptions[plugin].Keys) {
223 string pkey = String.Format("{0}.{1}", plugin, key);
224 if (CommandLineOptions.Contains(pkey)) continue;
225 f.WriteLine("{0} = {1}", pkey, PluginOptions[plugin][key]);
232 public static void SaveFile(WebThingPlugin p, string filename) {
233 string plugin_name = p.GetType().FullName;
234 SaveFile(plugin_name, filename);
237 public static void SaveFile(string plugin_name, string filename) {
238 // TODO: Throw exception if plugin does not exist
239 TextWriter f = new StreamWriter(filename);
241 foreach (string key in PluginOptions[plugin_name].Keys) {
242 f.WriteLine("{0}.{1} = {2}", plugin_name, key, PluginOptions[plugin_name][key]);
249 public static bool ParseBool(string v) {
250 switch(v.ToLower()) {