diff --git a/client.d b/client.d index c60c0b6..38b4233 100644 --- a/client.d +++ b/client.d @@ -28,48 +28,11 @@ import std.path; import std.file; import std.conv; import std.string; -//version(Windows) -//{ -// import core.runtime; -// import core.sys.windows.windows; -// import std.string; -//} + import msgpack; import messages; -//version(Windows) -//{ -// extern(Windows) int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) -// { -// int result; -// void exceptionHandler(Throwable e) { -// throw e; -// } -// try -// { -// Runtime. -// Runtime.initialize(&exceptionHandler); -// result = _main(["dcd-client"] ~ to!string(lpCmdLine).split(" ").array()); -// Runtime.terminate(&exceptionHandler); -// } -// catch (Throwable e) // catch any uncaught exceptions -// { -// MessageBoxA(null, e.toString().toStringz(), "Error", -// MB_OK | MB_ICONEXCLAMATION); -// result = 0; // failed -// } -// return result; -// } -//} -//else -//{ -// int main(string[] args) -// { -// return _main(args); -// } -//} - -int /*_*/main(string[] args) +int main(string[] args) { size_t cursorPos = size_t.max; string[] importPaths; @@ -110,7 +73,7 @@ int /*_*/main(string[] args) { AutocompleteRequest request; request.kind = RequestKind.addImport; - request.importPaths = importPaths.map!(a => isRooted(a) ? a : absolutePath(a)).array; + request.importPaths = importPaths.map!(a => absolutePath(a)).array; TcpSocket socket = createSocket(port); scope (exit) { socket.shutdown(SocketShutdown.BOTH); socket.close(); } return sendRequest(socket, request) ? 0 : 1; diff --git a/server.d b/server.d index 689dcbf..637f597 100644 --- a/server.d +++ b/server.d @@ -25,6 +25,7 @@ import std.algorithm; import std.path; import std.file; import std.array; +import std.process; import msgpack; @@ -32,17 +33,17 @@ import messages; import autocomplete; import modulecache; -version(Posix) -{ - enum CONFIG_FILE_PATH = "~/.config/dcd"; -} -else version(Windows) -{ - enum CONFIG_FILE_PATH = "dcd.conf"; -} +enum CONFIG_FILE_NAME = "dcd.conf"; + +version(linux) version = useXDG; +version(BSD) version = useXDG; +version(FreeBSD) version = useXDG; int main(string[] args) { + // No relative paths + version (Posix) chdir("/"); + ushort port = 9166; bool help; string[] importPaths; @@ -141,21 +142,50 @@ int main(string[] args) return 0; } +string getConfigurationLocation() +{ + version (useXDG) + { + string configDir = environment.get("XDG_CONFIG_HOME", null); + if (configDir is null) + { + configDir = environment.get("HOME", null); + if (configDir is null) + throw new Exception("Both $XDG_CONFIG_HOME and $HOME are unset"); + configDir = buildPath(configDir, ".config", "dcd", CONFIG_FILE_NAME); + } + else + { + configDir = buildPath(configDir, "dcd", CONFIG_FILE_NAME); + } + return configDir; + } + else version(Windows) + { + return CONFIG_FILE_NAME; + } +} + +void warnAboutOldConfigLocation() +{ + version (linux) if ("~/.config/dcd".expandTilde().isFile()) + { + writeln("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + writeln("!! Upgrade warning:"); + writeln("!! '~/.config/dcd' should be moved to '$XDG_CONFIG_HOME/dcd/dcd.conf'"); + writeln("!! or '$HOME/.config/dcd/dcd.conf'"); + writeln("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } +} + string[] loadConfiguredImportDirs() { - version(Windows) - { - string fullPath = buildPath(getcwd(), CONFIG_FILE_PATH); - } - else version(Posix) - { - string fullPath = expandTilde(CONFIG_FILE_PATH); - } - - if (!exists(fullPath)) + warnAboutOldConfigLocation(); + immutable string configLocation = getConfigurationLocation(); + if (!configLocation.exists()) return []; - - File f = File(fullPath, "rt"); + writeln("Loading configuration from ", configLocation); + File f = File(configLocation, "rt"); return f.byLine(KeepTerminator.no).map!(a => a.idup).filter!(a => a.exists()).array(); }