Mercurial > projects > dil
annotate trunk/src/dil/Settings.d @ 445:294353fe2514
Added functions for finding the executables path.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Wed, 17 Oct 2007 17:38:54 +0200 |
parents | 3751db263679 |
children | 325714d8aa6c |
rev | line source |
---|---|
309 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
326 | 5 module dil.Settings; |
327 | 6 import dil.Messages; |
7 import dil.Parser, dil.SyntaxTree, dil.Declarations, dil.Expressions; | |
358
0faf57d99c1c
- Replaced calls to std.file.read() with dil.loadFile().
aziz
parents:
349
diff
changeset
|
8 import dil.File; |
428
3751db263679
Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
399
diff
changeset
|
9 import tango.io.FilePath; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
10 import std.metastrings : FormatT = Format, ToString; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
11 import common; |
309 | 12 |
331 | 13 template Pad(char[] str, uint amount) |
14 { | |
15 static if (str.length >= amount) | |
16 const char[] Pad = str; | |
17 else | |
18 const char[] Pad = "0" ~ Pad!(str, amount-1); | |
19 } | |
20 | |
21 template Pad(int num, uint amount) | |
22 { | |
23 const char[] Pad = Pad!(ToString!(num), amount); | |
24 } | |
25 | |
333 | 26 version(D2) |
27 { | |
28 const VERSION_MAJOR = 2; | |
29 const VERSION_MINOR = 0; | |
30 } | |
31 else | |
32 { | |
33 const VERSION_MAJOR = 1; | |
34 const VERSION_MINOR = 0; | |
35 } | |
36 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
37 const string VERSION = FormatT!("%s.%s", VERSION_MAJOR, Pad!(VERSION_MINOR, 3)); |
343
95f1b6e43214
- Removed TOK.Special and added an own entry for each special token.
aziz
parents:
339
diff
changeset
|
38 const VENDOR = "dil"; |
309 | 39 |
339
9954367bcd4b
- Removed usageGenerate from Settings.d. Added some comments.
aziz
parents:
333
diff
changeset
|
40 /// Used in main help message. |
309 | 41 const COMPILED_WITH = __VENDOR__; |
339
9954367bcd4b
- Removed usageGenerate from Settings.d. Added some comments.
aziz
parents:
333
diff
changeset
|
42 /// ditto |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
43 const COMPILED_VERSION = FormatT!("%s.%s", __VERSION__/1000, Pad!(__VERSION__%1000, 3)); |
339
9954367bcd4b
- Removed usageGenerate from Settings.d. Added some comments.
aziz
parents:
333
diff
changeset
|
44 /// ditto |
309 | 45 const COMPILED_DATE = __TIMESTAMP__; |
46 | |
47 struct GlobalSettings | |
48 { | |
49 static: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
50 string language; /// Language of loaded messages catalogue. |
309 | 51 string[] messages; /// Table of localized compiler messages. |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
52 string[] importPaths; /// Array of import paths to look for modules. |
445
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
53 void load() |
309 | 54 { |
445
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
55 scope execPath = new FilePath(GetExecutableFilePath()); |
428
3751db263679
Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
399
diff
changeset
|
56 auto fileName = execPath.file("config.d").toUtf8(); |
358
0faf57d99c1c
- Replaced calls to std.file.read() with dil.loadFile().
aziz
parents:
349
diff
changeset
|
57 auto sourceText = loadFile(fileName); |
309 | 58 auto parser = new Parser(sourceText, fileName); |
368 | 59 auto root = parser.start(); |
309 | 60 |
61 if (parser.errors.length || parser.lx.errors.length) | |
62 { | |
63 throw new Exception("There are errors in " ~ fileName ~ "."); | |
64 } | |
65 | |
66 foreach (decl; root.children) | |
67 { | |
68 auto v = Cast!(VariableDeclaration)(decl); | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
69 if (v is null) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
70 continue; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
71 auto vname = v.idents[0].srcText; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
72 if (vname == "langfile") |
309 | 73 { |
74 auto e = v.values[0]; | |
75 if (!e) | |
349 | 76 throw new Exception("langfile variable has no value set."); |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
77 auto val = Cast!(StringExpression)(e); |
309 | 78 if (val) |
349 | 79 // Set fileName to d-file with messages table. |
80 fileName = val.getString(); | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
81 } |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
82 else if (vname == "import_paths") |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
83 { |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
84 auto e = v.values[0]; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
85 if (e is null) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
86 throw new Exception("import_paths variable has no variable set."); |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
87 if (auto array = Cast!(ArrayInitializer)(e)) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
88 { |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
89 foreach (value; array.values) |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
90 if (auto str = Cast!(StringExpression)(value)) |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
91 GlobalSettings.importPaths ~= str.getString(); |
309 | 92 } |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
93 else |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
94 throw new Exception("import_paths variable is set to "~e.classinfo.name~" instead of an ArrayInitializer."); |
309 | 95 } |
96 } | |
97 | |
98 // Load messages | |
428
3751db263679
Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
399
diff
changeset
|
99 sourceText = loadFile(execPath.file(fileName).toUtf8()); |
349 | 100 parser = new Parser(sourceText, fileName); |
368 | 101 root = parser.start(); |
349 | 102 |
103 if (parser.errors.length || parser.lx.errors.length) | |
309 | 104 { |
349 | 105 throw new Exception("There are errors in "~fileName~"."); |
106 } | |
309 | 107 |
349 | 108 char[][] messages; |
109 foreach (decl; root.children) | |
110 { | |
111 auto v = Cast!(VariableDeclaration)(decl); | |
112 if (v is null) | |
113 continue; | |
114 if (v.idents[0].srcText == "messages") | |
309 | 115 { |
349 | 116 auto e = v.values[0]; |
117 if (!e) | |
118 throw new Exception("messages variable in "~fileName~" has no value set."); | |
119 if (auto array = Cast!(ArrayInitializer)(e)) | |
309 | 120 { |
349 | 121 foreach (value; array.values) |
309 | 122 { |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
123 if (auto str = Cast!(StringExpression)(value)) |
349 | 124 messages ~= str.getString(); |
309 | 125 } |
126 } | |
349 | 127 else |
128 throw new Exception("messages variable is set to "~e.classinfo.name~" instead of an ArrayInitializer."); | |
309 | 129 } |
349 | 130 else if(v.idents[0].srcText == "lang_code") |
131 { | |
132 auto e = v.values[0]; | |
133 if (!e) | |
134 throw new Exception("lang_code variable in "~fileName~" has no value set."); | |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
135 if (auto str = Cast!(StringExpression)(e)) |
349 | 136 GlobalSettings.language = str.getString(); |
137 } | |
309 | 138 } |
349 | 139 if (messages.length != MID.max+1) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
140 throw new Exception(Format("messages table in {0} must exactly have {1} entries, but {2} were found.", fileName, MID.max+1, messages.length)); |
349 | 141 GlobalSettings.messages = messages; |
394
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
142 dil.Messages.SetMessages(messages); |
309 | 143 } |
144 } | |
445
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
145 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
146 version(Windows) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
147 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
148 private extern(Windows) uint GetModuleFileNameA(void*, char*, uint); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
149 /++ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
150 Get the fully qualified path to this executable. |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
151 +/ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
152 char[] GetExecutableFilePath() |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
153 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
154 alias GetModuleFileNameA GetModuleFileName; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
155 char[] buffer = new char[256]; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
156 uint count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
157 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
158 while (1) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
159 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
160 if (buffer is null) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
161 return null; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
162 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
163 count = GetModuleFileName(null, buffer.ptr, buffer.length); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
164 if (count == 0) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
165 return null; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
166 if (buffer.length != count && buffer[count] == 0) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
167 break; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
168 // Increase size of buffer |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
169 buffer.length = buffer.length * 2; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
170 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
171 assert(buffer[count] == 0); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
172 // Reduce buffer to the actual length of the string (excluding '\0'.) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
173 if (count < buffer.length) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
174 buffer.length = count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
175 return buffer; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
176 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
177 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
178 else version(linux) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
179 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
180 private extern(C) size_t readlink(char* path, char* buf, size_t bufsize); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
181 /++ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
182 Get the fully qualified path to this executable. |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
183 +/ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
184 char[] GetExecutableFilePath() |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
185 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
186 char[] buffer = new char[256]; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
187 size_t count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
188 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
189 while (1) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
190 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
191 // This won't work on very old Linux systems. |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
192 count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
193 if (count == -1) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
194 return null; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
195 if (count < buffer.length) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
196 break; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
197 buffer.length = buffer.length * 2; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
198 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
199 buffer.length = count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
200 return buffer; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
201 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
202 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
203 else |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
204 static assert(0, "GetExecutableFilePath() is not implemented on your platform."); |