Skip to content

Commit e863e97

Browse files
committed
Fixed loading files using relative names (closes #829, #830).
1 parent 1aa6af4 commit e863e97

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

build/win32_starter.c

+21-7
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,26 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
149149

150150
if (!GetFullPathName(argv[0],MAX_PATH,buffer,&file)) {
151151
MessageBox(NULL,
152-
TEXT("Couldn't find the correct working directory"),
152+
TEXT("Couldn't find the executable path"),
153153
TEXT("Failed to start editor"),
154154
MB_OK|MB_ICONERROR);
155155
return 0;
156156
}
157157
if (file!=NULL) *file = 0; // finish the string, don't need the appname
158158

159+
LPWSTR path = (LPWSTR)GlobalAlloc(GMEM_FIXED, MAX_PATH+1);
160+
if (GetCurrentDirectoryW(MAX_PATH, path) == 0) {
161+
MessageBox(NULL,
162+
TEXT("Couldn't find the current working directory"),
163+
TEXT("Failed to start editor"),
164+
MB_OK|MB_ICONERROR);
165+
return 0;
166+
}
167+
159168
SetCurrentDirectory(buffer);
160169

161170
hinstLib = LoadLibrary(".\\bin\\lua51.dll");
162-
if (hinstLib != NULL)
163-
{
171+
if (hinstLib != NULL) {
164172
luaL_newstate = (voidfunc*) GetProcAddress(hinstLib, "luaL_newstate");
165173
luaL_loadbuffer = (varfunc*) GetProcAddress(hinstLib, "luaL_loadbuffer");
166174
luaL_openlibs = (varfunc*) GetProcAddress(hinstLib, "luaL_openlibs");
@@ -182,13 +190,19 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
182190
// of the whole process, it SHOULD be pretty unlikely to fail here
183191
// but don't come back on me if it does...
184192
void *L = luaL_newstate();
185-
int i;
186193

187194
if (L!=NULL) {
188-
lua_createtable(L,argc,0);
189-
for (i=0;i<argc;i++) {
195+
int i;
196+
lua_createtable(L,argc+2,0);
197+
lua_pushstring(L,argv[0]); // executable name goes first
198+
lua_rawseti(L,-2,1);
199+
lua_pushstring(L,"-cwd");
200+
lua_rawseti(L,-2,2);
201+
lua_pushstring(L,WideCharToUTF8(path));
202+
lua_rawseti(L,-2,3);
203+
for (i=1;i<argc;i++) {
190204
lua_pushstring(L,argv[i]);
191-
lua_rawseti(L,-2,i+1);
205+
lua_rawseti(L,-2,i+3);
192206
}
193207
lua_setfield(L,LUA_GLOBALSINDEX,"_ARG");
194208
luaL_openlibs(L);

src/editor/singleinstance.lua

+7-12
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,16 @@ else -- something different is running on our port
7777
ide:Print(("Another instance is running under user '%s' and can't be activated. This instance will continue running, which may cause interference with the debugger."):format(username))
7878
else
7979
local failed = false
80-
for index = 2, #arg do
81-
local fileName = arg[index]
82-
if fileName ~= "--"
83-
-- on OSX, the command line includes -psn parameter, so ignore it
84-
and (ide.osname ~= 'Macintosh' or not fileName:find("^-psn")) then
85-
cln:send(protocol.client.requestloading:format(fileName))
80+
for _, filename in ipairs(ide.filenames) do
81+
cln:send(protocol.client.requestloading:format(ide:MergePath(ide.cwd or "", filename)))
8682

87-
local msg, err = cln:receive()
88-
if msg ~= protocol.server.answerok then
89-
failed = true
90-
ide:Print(err,msg)
91-
end
83+
local msg, err = cln:receive()
84+
if msg ~= protocol.server.answerok then
85+
failed = true
86+
ide:Print(err,msg)
9287
end
9388
end
94-
if #arg == 1 then -- no files are being loaded; just active the IDE
89+
if #ide.filenames == 0 then -- no files are being loaded; just active the IDE
9590
cln:send(protocol.client.show)
9691
if cln:receive() ~= protocol.server.answerok then failed = true end
9792
end

src/main.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ide = {
7171
onidle = {},
7272

7373
proto = {}, -- prototypes for various classes
74+
filenames = {}, -- names for files to load
7475

7576
app = nil, -- application engine
7677
interpreter = nil, -- current Lua interpreter
@@ -335,7 +336,6 @@ ide.test.setLuaPaths = setLuaPaths
335336

336337
---------------
337338
-- process args
338-
local filenames = {}
339339
local configs = {}
340340
do
341341
-- application parameters are passed as script parameters on Windows
@@ -365,10 +365,12 @@ do
365365
for index = 2, #arg do
366366
if (arg[index] == "-cfg" and index+1 <= #arg) then
367367
table.insert(configs,arg[index+1])
368-
elseif arg[index-1] ~= "-cfg"
368+
elseif (arg[index] == "-cwd" and index+1 <= #arg) then
369+
ide.cwd = arg[index+1]
370+
elseif arg[index-1] ~= "-cfg" and arg[index-1] ~= "-cwd"
369371
-- on OSX command line includes -psn... parameter, don't include these
370372
and (ide.osname ~= 'Macintosh' or not arg[index]:find("^-psn")) then
371-
table.insert(filenames,arg[index])
373+
table.insert(ide.filenames,arg[index])
372374
end
373375
end
374376

@@ -598,8 +600,8 @@ SettingsRestoreView()
598600
-- Load the filenames
599601

600602
do
601-
for _, filename in ipairs(filenames) do
602-
if filename ~= "--" then ide:ActivateFile(filename) end
603+
for _, filename in ipairs(ide.filenames) do
604+
ide:ActivateFile(ide:MergePath(ide.cwd or "", filename))
603605
end
604606
if ide:GetEditorNotebook():GetPageCount() == 0 then NewFile() end
605607
end

zbstudio.exe

0 Bytes
Binary file not shown.

zbstudio.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/bin/bash
22

3-
# Thanks to StackOverflow wiki (http://stackoverflow.com/a/246128)
4-
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # per http://stackoverflow.com/a/246128
4+
CWD="$PWD" # save the current directory, as it's going to change
55

66
if [[ $(uname) == "Darwin" ]]; then
77
# MacOS Sierra throws `error -10810` running with quarantined files even when explicitly allowed
88
ATTR="com.apple.quarantine"
99
(cd "$DIR"; \
1010
if [[ $( xattr -pl $ATTR zbstudio 2>&1 ) == $ATTR:* ]]; then xattr -rd $ATTR zbstudio bin; fi; \
11-
open zbstudio/ZeroBraneStudio.app --args "$@")
11+
open zbstudio/ZeroBraneStudio.app --args -cwd "$CWD" "$@")
1212
else
1313
case "$(uname -m)" in
1414
x86_64) ARCH=x64;;
1515
armv7l) ARCH=armhf;;
1616
aarch64) ARCH=aarch64;;
1717
*) ARCH=x86;;
1818
esac
19-
(cd "$DIR"; bin/linux/$ARCH/lua src/main.lua zbstudio "$@") &
19+
(cd "$DIR"; bin/linux/$ARCH/lua src/main.lua zbstudio -cwd "$CWD" "$@") &
2020
fi

0 commit comments

Comments
 (0)