6d29bd2002-02-23Martin Nilsson #pike __REAL_VERSION__
8201502002-08-28Mikael Brandström // $Id: Local.pmod,v 1.6 2002/08/28 13:40:24 mikael Exp $
6d29bd2002-02-23Martin Nilsson  //! @[Local] gives a local module namespace used for locally //! installed pike modules. Modules are searched for in //! the directory @tt{pike_modules@} which can be located in //! the user's home directory or profile directory, or in any //! of the system directories @tt{/opt/share, /usr/local/share, //! /opt@} or @tt{/usr/local/@}. The user's home directory is //! determined by examining the environment variable HOME, and
6dffbe2002-08-28Mikael Brandström //! if that fails the environment variable USERPROFILE. If the //! environment variable PIKE_LOCAL_PATH is set, the paths specified //! there will be searched first.
6d29bd2002-02-23Martin Nilsson //! @seealso
a5c9e32002-02-25Martin Nilsson //! @[Local.add_path()], @[Local.remove_path()]
6d29bd2002-02-23Martin Nilsson //! static array(string) local_path; mixed `[](string name) {
5e22182002-02-23Mikael Brandström  // Make sure dot syntax Local.add_path works. if(name=="add_path" || name=="remove_path"){ if(name=="add_path") return add_path; return remove_path; }
8201502002-08-28Mikael Brandström  // FIXME: Should .pike och .pmod files have priority? // currently .pike files has it here, but .pmod in the // resolver. // mikael
6d29bd2002-02-23Martin Nilsson  foreach(local_path,string lp){ program r=(program)combine_path(lp,name); if(r) return r; Stdio.Stat st; if(st=file_stat(lp+name+".pmod")){ if(st->isdir) return master()->dirnode(lp+name+".pmod"); return (object)(lp+name+".pmod"); } catch{ return (object)(lp+name+".so"); }; // This is not a proper handling of .so-files, it does not // handle when there are conflicting .so and .pmod files. } return UNDEFINED; } array(string) _indices() { array(string) tmp = ({ }); foreach(local_path,string lp) tmp+=get_dir(lp);
8201502002-08-28Mikael Brandström  return Array.uniq(map(glob("*.pike",tmp)+glob("*.pmod",tmp)+glob("*.so",tmp),
6d29bd2002-02-23Martin Nilsson  lambda(string in){ if(glob("*.so",in)) return in[..sizeof(in)-4];
8201502002-08-28Mikael Brandström  return in[..sizeof(in)-6]; }));
6d29bd2002-02-23Martin Nilsson } // _values intentionally not overloaded static void create() { string tmp; local_path=({ }); // FIXME $prefix/pike_modules // FIXME All this should be controllable from .pikerc, when such a file is implemented...
5e22182002-02-23Mikael Brandström  add_path("/usr/local/pike_modules"); add_path("/opt/pike_modules"); add_path("/opt/share/pike_modules"); add_path("/usr/local/share/pike_modules");
6dffbe2002-08-28Mikael Brandström 
6d29bd2002-02-23Martin Nilsson  if( (tmp=getenv("HOME")) || (tmp=getenv("USERPROFILE")) ) { tmp = (tmp[-1]=='/'?tmp:tmp+"/")+"pike_modules/";
5e22182002-02-23Mikael Brandström  add_path(tmp);
6d29bd2002-02-23Martin Nilsson  }
6dffbe2002-08-28Mikael Brandström  if(tmp = getenv("PIKE_LOCAL_PATH") ) {
7751eb2002-08-28Mikael Brandström  array to_add=reverse(tmp/":"); // preserve order
6dffbe2002-08-28Mikael Brandström  add_path( to_add[*] ); }
6d29bd2002-02-23Martin Nilsson } //! This function prepends @[path] to the @[Local] module //! searchpath. //! //! @param path //! The path to the directory to be added. //! //! @returns //! Returns 1 on success, otherwise 0.
5e22182002-02-23Mikael Brandström int(0..1) add_path(string path){
6d29bd2002-02-23Martin Nilsson  if(!file_stat(path)) return 0; if(path[sizeof(path)-1]!='/') path+="/";
5e22182002-02-23Mikael Brandström  remove_path(path); // Avoid duplicate entries.
6d29bd2002-02-23Martin Nilsson  local_path=({path})+local_path; return 1; } //! This function removes @[path] from the @[Local] module //! searchpath. If @[path] is not in the search path, this is //! a noop. //! //! @param path //! The path to be removed. //!
5e22182002-02-23Mikael Brandström void remove_path(string path){
6d29bd2002-02-23Martin Nilsson  if(local_path) local_path-=({ path }); }