/* * Wrapper application to set octave env variables and then run octave * compile with gcc -o octave-launch octave-launch.c -Wl,--subsystem,windows -lshlwapi * * Copyright (C) 2020 John Donoghue * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, see . */ #include #include int ParentDir(char *dir) { int len = strlen(dir); while(len > 0 && dir[len] != '\\') len --; dir[len] = 0; return len; } char * FilePart(char *dir) { int len = strlen(dir); while(len > 0 && dir[len-1] != '\\') len --; return &dir[len]; } int main(int argc, char **argv) { PROCESS_INFORMATION pi; char msyspath[1024]; char path[1024]; char binpath[1024]; int gui_mode = -1; int i; /* get path of us as we assume octave will be in same dir */ DWORD nSize = GetModuleFileName(NULL, path, sizeof(path)-1); if (nSize) { while(nSize > 0 && path[nSize] != '\\') nSize --; path[nSize] = '\0'; } nSize = GetShortPathName(path, binpath, sizeof(binpath)-1); if (nSize == 0) { StrCpy(binpath, path); } /* binpath is to the bin dir so get the parent */ StrCpy(path, binpath); ParentDir(path); /* now at the octave home */ SetEnvironmentVariable("OCTAVE_HOME", path); /* qt paths * either %OCT_HOME%\qt5\plugins * or %OCT_HOME\plugins */ nSize = strlen(path); StrCat(path, "\\qt5\\plugins"); if (! PathFileExists(path)) { path[nSize] = '\0'; StrCat(path, "\\plugins"); } SetEnvironmentVariable("QT_PLUGIN_PATH", path); path[nSize] = '\0'; /* get msys path and set env */ StrCpy(msyspath, path); char * msys = FilePart(msyspath); /* * should be mingw64 for msys2 ming64 * should be mingw32 for msys2 ming32 * else msys */ if (StrCmpI(msys, "mingw64") == 0) { SetEnvironmentVariable("MSYSTEM", "MINGW64"); ParentDir(msyspath); StrCat(msyspath, "\\bin"); SetEnvironmentVariable("MSYSPATH", msyspath); } else if (StrCmpI(msys, "mingw32") == 0) { SetEnvironmentVariable("MSYSTEM", "MINGW32"); ParentDir(msyspath); StrCat(msyspath, "\\bin"); SetEnvironmentVariable("MSYSPATH", msyspath); } else { SetEnvironmentVariable("MSYSTEM", "MSYS"); SetEnvironmentVariable("MSYSPATH", msyspath); } /* exe paths */ { char pathbuffer[8096]; char newpathbuffer[8096]; nSize = GetEnvironmentVariable("PATH", pathbuffer, sizeof(pathbuffer)-1); pathbuffer[nSize] = '\0'; /* set our paths first */ StrCpy(newpathbuffer, binpath); StrCat(newpathbuffer, ";"); StrCat(newpathbuffer, msyspath); StrCat(newpathbuffer, ";"); StrCat(newpathbuffer, pathbuffer); SetEnvironmentVariable("PATH", newpathbuffer); } /* other env */ SetEnvironmentVariable("TERM", "cygwin"); SetEnvironmentVariable("GNUTERM", "wxt"); SetEnvironmentVariable("GS", "gs.exe"); /* home set */ nSize = GetEnvironmentVariable("HOME", path, sizeof(path)-1); if (nSize == 0 || path[0] == 0) { char newhome[1024]; nSize = GetEnvironmentVariable("USERPROFILE", path, sizeof(path)-1); if (nSize == 0 || path[0] == 0) { /* build dir from drive and path */ char tmpbuff[1024]; nSize = GetEnvironmentVariable("HOMEDRIVE", tmpbuff, sizeof(tmpbuff)-1); if (nSize) StrCpy (path, tmpbuff); else path[0] = '\0'; nSize = GetEnvironmentVariable("HOMEPATH", tmpbuff, sizeof(tmpbuff)-1); if(nSize) StrCat(path, tmpbuff); } nSize = GetShortPathName(path, newhome, sizeof(newhome)-1); if (nSize == 0) { StrCpy(newhome, path); } SetEnvironmentVariable("HOME", newhome); } /* check for gui mode */ for (i=1; i