[Gambas-user] GAMBAS PATH

Tobias Boege taboege at ...626...
Sun Aug 11 16:20:59 CEST 2013


On Sun, 11 Aug 2013, Tobias Boege wrote:
> On Sun, 11 Aug 2013, Antonio S?nchez wrote:
> > Hi, Beno?t, would reconsider the possibility to run GAMBAS from a given
> > path with a environment variable or another method? It would be a great
> > help to run programs made with older versions of GAMBAS in new systems that
> > do not have in its repositories older versions of GAMBAS.
> > 
> > It would be useful to run programs made in GAMBAS in home directory too
> > without root permissions.
> > 
> > And you could play with several versions only copying necessary files in
> > the work folder.
> > 
> > And another advantage is that you can be sure that the user will have just
> > the version your program need =:-)
> > 
> 
> I've hacked around in the interpreter some time ago to allow just that. I'm
> not sure if I succeeded but give me some time and I'll try to dig the stuff
> out again.
> 

Okay, the attached patch seems to work. When applied, gbx3 will first look
at the environment variable GAMBAS_ROOT. If it is defined, non-empty, the
contained path exists and is a directory, then this is taken as the Gambas
installation directory for the duration of the program. All components and
resources are located relative to that directory instead of the normal /usr
then (of course only if the /usr is not hard-coded).

My proof was a gb.ncurses project as simple as:

---
Public Sub Main()
  Window.Print("hello")
End
---

I installed the patched interpreter to /usr as usual. Then I made a simple
modification to gb.ncurses which adds ", man" to each printed string. I then
installed the modified gb.ncurses along with the interpreter like

$ make install DESTDIR=~

which creates a usr/ directory under ~/ with the alternate Gambas
installation.

$ gbx3

leads to the expected output "hello" whereas

$ GAMBAS_ROOT=~/usr gbx3

gives me "hello, man". So this works at least with components. Note that the
interpreter which runs is still the one under /usr. Only the components it
loads are from ~/usr. You may run into compatibility issues of you are not
cautious with this patch. The best thing would be to set your PATH to prefer
the interpreter which belongs to the components you want to run, i.e. in the
above case

# Also take the gbx3 from ~/usr
$ PATH=~/usr/bin:$PATH GAMBAS_ROOT=~/usr gbx3

A quick grep looks also good: the /usr path doesn't seem to hang anywhere
hardcoded:

$ grep /usr -R * --include=*.c --include=*.cpp --include=*.h --exclude=gb_common.h
gb.httpd/src/thttpd.h://#define CGI_PATH "/usr/local/bin:/usr/ucb:/bin:/usr/bin"
gb.httpd/src/thttpd.h:#define CGI_LD_LIBRARY_PATH "/usr/local/lib:/usr/lib"
gb.qt4/src/main.cpp:              && !_translator->load(QString("qt_") + locale, QString("/usr/lib/qt4/translations"))
gb.qt4/src/main.cpp:              && !_translator->load(QString("qt_") + locale, QString("/usr/share/qt4/translations")));
main/gbx/gbx_archive.c:                 if (!path) path = exist_library("/usr/bin", arch->name);
main/share/gb_component.h:#define GAMBAS_LINK_PATH  "/usr/bin/gbx" GAMBAS_VERSION_STRING
main/gbc/gbc_archive.c: fputs("#! /usr/bin/env gbr" GAMBAS_VERSION_STRING "\n", arch_file);

But seriously, I'd be cautious... How likely can a hardcoded path sneak in
and break compatibility - or something that I haven't thought of!

If you change the grep to include also components written in Gambas and the
IDE (--include=*.module --include=*.class), the you'll find quite some
hardcoded /usr paths!

Anyway, have fun with it as long as it doesn't break too much :-) I'll be
writing this up as an article for the adventurous Gambas users... The
patch should apply against the latest revision.

Regards,
Tobi
-------------- next part --------------
Index: main/share/gb_file_temp.h
===================================================================
--- main/share/gb_file_temp.h	(revision 5776)
+++ main/share/gb_file_temp.h	(working copy)
@@ -1130,7 +1130,21 @@
 const char *FILE_find_gambas(void)
 {
 	const char *path;
+	static char buf[sizeof(file_buffer)];
 
+	path = getenv("GAMBAS_ROOT");
+	if (path && *path) {
+		if (!FILE_is_dir(path)) {
+			fprintf(stderr, "WARNING: GAMBAS_ROOT defined but "
+					"not a directory. Ignoring.\n");
+			goto ignore;
+		}
+		path =  FILE_cat(path, "bin/gbx" GAMBAS_VERSION_STRING, NULL);
+		strcpy(buf, file_buffer);
+		return buf;
+	}
+ignore:
+
 	if (FILE_exist(GAMBAS_LINK_PATH))
 	{
 		path = FILE_readlink(GAMBAS_LINK_PATH);


More information about the User mailing list