ec94462002-05-01Martin Stjernholm // -*- Pike -*-
2f520e2002-04-28Martin Nilsson // // Master Control Program for Pike. // // This file is part of Pike. For copyright information see COPYRIGHT.
59e32d2002-06-01Martin Nilsson // Pike is distributed under GPL, LGPL and MPL. See the file COPYING // for more information.
2f520e2002-04-28Martin Nilsson //
695d742004-12-08Martin Stjernholm // $Id: master.pike.in,v 1.259 2004/12/08 15:02:08 mast Exp $
c896b41998-04-29Henrik Grubbström (Grubba) 
a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
2f520e2002-04-28Martin Nilsson 
77e4f22002-07-09Martin Nilsson // Some programs destroys character pairs beginning with the currency // symbol when running in chinese locale.
47b4322002-06-28Martin Nilsson #if "¤/" != "\244/" #error "master.pike.in is corrupted." #endif
2f520e2002-04-28Martin Nilsson // --- Some configurable parameters
bec57d1999-09-06Fredrik Hübinette (Hubbe) #define PIKE_AUTORELOAD
2f520e2002-04-28Martin Nilsson #define GETCWD_CACHE #define FILE_STAT_CACHE
77e4f22002-07-09Martin Nilsson // This define is searched and replaced by bin/install.pike.
0edc122004-05-03Henrik Grubbström (Grubba) // The PIKE_MODULE_RELOC stuff is curently broken. //#undef PIKE_MODULE_RELOC
2f520e2002-04-28Martin Nilsson  #ifndef PIKE_WARNINGS
51a8f42002-05-15Henrik Grubbström (Grubba) #define PIKE_WARNINGS 1
2f520e2002-04-28Martin Nilsson #endif /* PIKE_WARNINGS */ // --- Global constants and variables
bec57d1999-09-06Fredrik Hübinette (Hubbe) 
c896b41998-04-29Henrik Grubbström (Grubba) // Used by describe_backtrace() et al. #if !defined(BT_MAX_STRING_LEN) || (BT_MAX_STRING_LEN <= 0) #undef BT_MAX_STRING_LEN
a858201999-10-15Fredrik Hübinette (Hubbe) #define BT_MAX_STRING_LEN 200
c896b41998-04-29Henrik Grubbström (Grubba) #endif /* !defined(BT_MAX_STRING_LEN) || (BT_MAX_STRING_LEN <= 0) */
d4fd0a1999-12-06Henrik Grubbström (Grubba) constant bt_max_string_len = BT_MAX_STRING_LEN;
6df5a52001-11-07Martin Nilsson //! @decl constant bt_max_string_len = 200
8a49852002-03-10Martin Stjernholm //! This constant contains the maximum length of a function entry in a //! backtrace. Defaults to 200 if no BT_MAX_STRING_LEN define has been
6df5a52001-11-07Martin Nilsson //! given.
97e5c72001-07-28Martin Nilsson 
401fa02002-12-02Martin Stjernholm // Enables the out of date warning in low_findprog().
e115cb1999-12-09Henrik Grubbström (Grubba) #ifndef OUT_OF_DATE_WARNING #define OUT_OF_DATE_WARNING 1 #endif /* OUT_OF_DATE_WARNING */ constant out_of_date_warning = OUT_OF_DATE_WARNING;
3310402004-03-16Henrik Grubbström (Grubba) #ifdef __NT__ #define PATH_SEPARATOR ";" #else #define PATH_SEPARATOR ":" #endif
6df5a52001-11-07Martin Nilsson //! @decl constant out_of_date_warning = 1 //! Should Pike complain about out of date compiled files. //! 1 means yes and 0 means no. Controlled by the OUT_OF_DATE_WARNING //! define.
97e5c72001-07-28Martin Nilsson 
2f520e2002-04-28Martin Nilsson //! If not zero compilation warnings will be written out on stderr. int want_warnings = PIKE_WARNINGS;
df2c632000-08-02Henrik Grubbström (Grubba) 
2f520e2002-04-28Martin Nilsson //! int compat_major=-1;
4839dd2001-09-02Marcus Comstedt 
2f520e2002-04-28Martin Nilsson //! int compat_minor=-1; // --- Functions begin here. #define Stat _static_modules.files.Stat #define capitalize(X) (upper_case((X)[..0])+(X)[1..])
3310402004-03-16Henrik Grubbström (Grubba) #define write(X ...) _static_modules.files()->_stdout->write(X) #ifdef RESOLV_DEBUG #if constant (thread_local) static object resolv_msg_depth = thread_local(); #define GET_RESOLV_MSG_DEPTH (resolv_msg_depth->get()) #define INC_RESOLV_MSG_DEPTH() (resolv_msg_depth->set (resolv_msg_depth->get() + 1)) #define DEC_RESOLV_MSG_DEPTH() (resolv_msg_depth->set (resolv_msg_depth->get() - 1)) #else static int resolv_msg_depth; #define GET_RESOLV_MSG_DEPTH resolv_msg_depth #define INC_RESOLV_MSG_DEPTH() (++resolv_msg_depth) #define DEC_RESOLV_MSG_DEPTH() (--resolv_msg_depth) #endif void resolv_debug (string fmt, mixed... args) { string pad = " " * GET_RESOLV_MSG_DEPTH; if (sizeof (args)) fmt = sprintf (fmt, @args); if (fmt[-1] == '\n') fmt = pad + replace (fmt[..sizeof (fmt) - 2], "\n", "\n" + pad) + "\n"; else fmt = pad + replace (fmt, "\n", "\n" + pad); werror (fmt); } #else // !RESOLV_DEBUG #define INC_RESOLV_MSG_DEPTH() 0 #define DEC_RESOLV_MSG_DEPTH() 0 #define resolv_debug(X...) do {} while (0) #endif // !RESOLV_DEBUG
c896b41998-04-29Henrik Grubbström (Grubba) 
4f64a52002-05-22Martin Nilsson //! @appears error
01f7f32002-05-22Johan Sundström //! Throws an error. A more readable version of the code
3310402004-03-16Henrik Grubbström (Grubba) //! @expr{throw( ({ sprintf(f, @@args), backtrace() }) )@}.
4f64a52002-05-22Martin Nilsson void error(string f, mixed ... args) {
da24482002-05-24Martin Nilsson  array b = backtrace();
4f64a52002-05-22Martin Nilsson  if (sizeof(args)) f = sprintf(f, @args); throw( ({ f, b[..sizeof(b)-2] }) ); }
6df5a52001-11-07Martin Nilsson // FIXME: Should the pikeroot-things be private?
26f3da2000-07-11Fredrik Hübinette (Hubbe) #ifdef PIKE_FAKEROOT object o; string fakeroot(string s) { string tmp1=combine_path_with_cwd(s); #ifdef PIKE_FAKEROOT_OMIT
3310402004-03-16Henrik Grubbström (Grubba)  foreach(PIKE_FAKEROOT_OMIT/PATH_SEPARATOR, string x)
26f3da2000-07-11Fredrik Hübinette (Hubbe)  if(glob(x,tmp1)) return s; #endif return PIKE_FAKEROOT+tmp1; } #else #define fakeroot(X) X
2f520e2002-04-28Martin Nilsson #endif // PIKE_FAKEROOT
26f3da2000-07-11Fredrik Hübinette (Hubbe) 
4839dd2001-09-02Marcus Comstedt #ifdef PIKE_MODULE_RELOC string relocate_module(string s) {
3310402004-03-16Henrik Grubbström (Grubba)  if(s == "/${PIKE_MODULE_PATH}" || has_prefix (s, "/${PIKE_MODULE_PATH}/")) {
4839dd2001-09-02Marcus Comstedt  string tmp = s[21..]; foreach(pike_module_path, string path) { string s2 = fakeroot(sizeof(tmp)? combine_path(path, tmp) : path); if(master_file_stat(s2)) return s2; } } return fakeroot(s); } string unrelocate_module(string s) {
3310402004-03-16Henrik Grubbström (Grubba)  if(s == "/${PIKE_MODULE_PATH}" || has_prefix (s, "/${PIKE_MODULE_PATH}/"))
4839dd2001-09-02Marcus Comstedt  return s; foreach(pike_module_path, string path) if(s == path) return "/${PIKE_MODULE_PATH}"; else {
3310402004-03-16Henrik Grubbström (Grubba)  path = combine_path(path, ""); if(has_prefix (s, path)) return "/${PIKE_MODULE_PATH}/"+s[sizeof(path)..];
4839dd2001-09-02Marcus Comstedt  }
3310402004-03-16Henrik Grubbström (Grubba)  /* This is necessary to find compat modules... */ foreach(pike_module_path, string path) { path = combine_path(path, "..", ""); if(has_prefix (s, path)) return "/${PIKE_MODULE_PATH}/../"+s[sizeof(path)..]; }
4839dd2001-09-02Marcus Comstedt  return s; }
3310402004-03-16Henrik Grubbström (Grubba) 
4839dd2001-09-02Marcus Comstedt #ifdef fakeroot #undef fakeroot #endif #define fakeroot relocate_module
2f520e2002-04-28Martin Nilsson #endif // PIKE_MODULE_RELOC
4839dd2001-09-02Marcus Comstedt 
26f3da2000-07-11Fredrik Hübinette (Hubbe) 
b07e962001-07-27Martin Nilsson //! @appears is_absolute_path
8f45692001-01-14Henrik Grubbström (Grubba) //! Check if a path @[p] is fully qualified (ie not relative). //! //! @returns //! Returns 1 if the path is absolute, 0 otherwise.
ca2b071998-03-28Henrik Grubbström (Grubba) int is_absolute_path(string p) { #ifdef __NT__ p=replace(p,"\\","/");
e49e542001-01-22Fredrik Hübinette (Hubbe)  if(sscanf(p,"%[a-zA-Z]:%*c",string s)==2 && sizeof(s)==1)
ca2b071998-03-28Henrik Grubbström (Grubba)  return 1;
471ed91998-04-24Fredrik Hübinette (Hubbe) #define IS_ABSOLUTE_PATH is_absolute_path #else
e178b72003-04-10Martin Stjernholm #define IS_ABSOLUTE_PATH(X) has_prefix((X),"/")
ca2b071998-03-28Henrik Grubbström (Grubba) #endif
e178b72003-04-10Martin Stjernholm  return has_prefix(p,"/");
ca2b071998-03-28Henrik Grubbström (Grubba) }
4619bd2002-07-29Martin Nilsson #ifdef __NT__ #define EXPLODE_PATH(X) (replace((X),"\\","/")/"/") #else #define EXPLODE_PATH(X) ((X)/"/") #endif
b07e962001-07-27Martin Nilsson //! @appears explode_path
8f45692001-01-14Henrik Grubbström (Grubba) //! Split a path @[p] into its components. //! //! This function divides a path into its components. This might seem like //! it could be done by dividing the string on <tt>"/"</tt>, but that will //! not work on some operating systems. //!
bbf4702000-04-10Henrik Grubbström (Grubba) array(string) explode_path(string p)
ca2b071998-03-28Henrik Grubbström (Grubba) {
4619bd2002-07-29Martin Nilsson  return EXPLODE_PATH(p);
ca2b071998-03-28Henrik Grubbström (Grubba) }
b07e962001-07-27Martin Nilsson //! @appears dirname
77e4f22002-07-09Martin Nilsson //! Returns all but the last segment of a path. Some example inputs and //! outputs: //! //! @xml{<matrix> //! <r><c><b>Expression</b></c><c><b>Value</b></c></r> //! <r><c>dirname("/a/b")</c><c>"/a/"</c></r> //! <r><c>dirname("/a/")</c><c>"/a/"</c></r> //! <r><c>dirname("/a")</c><c>"/"</c></r> //! <r><c>dirname("/")</c><c>"/"</c></r> //! <r><c>dirname("")</c><c>""</c></r> //! </matrix>@}
8f45692001-01-14Henrik Grubbström (Grubba) //! //! @seealso //! @[basename()], @[explode_path()]
ca2b071998-03-28Henrik Grubbström (Grubba) string dirname(string x) {
77e4f22002-07-09Martin Nilsson  if(x=="") return "";
afa1292000-02-19Martin Nilsson  array(string) tmp=EXPLODE_PATH(x);
bd22082002-07-29Martin Nilsson  if(x[0]=='/' && sizeof(tmp)<3) return "/";
ca2b071998-03-28Henrik Grubbström (Grubba)  return tmp[..sizeof(tmp)-2]*"/"; }
b07e962001-07-27Martin Nilsson //! @appears basename
8f45692001-01-14Henrik Grubbström (Grubba) //! Returns the last segment of a path. //! //! @seealso //! @[dirname()], @[explode_path()]
ca2b071998-03-28Henrik Grubbström (Grubba) string basename(string x) {
afa1292000-02-19Martin Nilsson  array(string) tmp=EXPLODE_PATH(x);
ca2b071998-03-28Henrik Grubbström (Grubba)  return tmp[-1]; }
471ed91998-04-24Fredrik Hübinette (Hubbe) #define BASENAME(X) (EXPLODE_PATH(X)[-1])
ca2b071998-03-28Henrik Grubbström (Grubba) 
bec57d1999-09-06Fredrik Hübinette (Hubbe) #ifdef PIKE_AUTORELOAD int autoreload_on; int newest; #define AUTORELOAD_CHECK_FILE(X) \
da24482002-05-24Martin Nilsson  if(autoreload_on) if(Stat s=master_file_stat(X)) if(s->mtime>newest) newest=[int]s->mtime;
bec57d1999-09-06Fredrik Hübinette (Hubbe)  #define AUTORELOAD_BEGIN() \ int ___newest=newest; \ newest=0
3310402004-03-16Henrik Grubbström (Grubba) #define AUTORELOAD_FINISH(VAR, CACHE, FILE) \ if(autoreload_on) { \ mixed val = CACHE[FILE]; \ if(!zero_type (val) && val != no_value && newest <= load_time[FILE]) { \ VAR = val; \ } \ } \ load_time[FILE]=time(); \ if(___newest > newest) newest=___newest;
bec57d1999-09-06Fredrik Hübinette (Hubbe)  mapping(string:int) load_time=([]); #else #define AUTORELOAD_CHECK_FILE(X) #define AUTORELOAD_BEGIN() #define AUTORELOAD_FINISH(VAR,CACHE,FILE)
2f520e2002-04-28Martin Nilsson #endif // PIKE_AUTORELOAD
a20af62000-09-26Fredrik Hübinette (Hubbe) 
b07e962001-07-27Martin Nilsson //! @appears compile_string
56cd002001-10-28Martin Nilsson //! Compile the Pike code in the string @[source] into a program.
3310402004-03-16Henrik Grubbström (Grubba) //! If @[filename] is not specified, it will default to @expr{"-"@}.
8f45692001-01-14Henrik Grubbström (Grubba) //!
3310402004-03-16Henrik Grubbström (Grubba) //! Functionally equal to @expr{@[compile](@[cpp](@[source], @[filename]))@}.
8f45692001-01-14Henrik Grubbström (Grubba) //! //! @seealso //! @[compile()], @[cpp()], @[compile_file()] //! program compile_string(string source, void|string filename, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) {
d2777c2001-03-26Henrik Grubbström (Grubba)  return compile(cpp(source, filename||"-", 1, handler,
8f45692001-01-14Henrik Grubbström (Grubba)  compat_major, compat_minor),
a20af62000-09-26Fredrik Hübinette (Hubbe)  handler, compat_major, compat_minor);
ca2b071998-03-28Henrik Grubbström (Grubba) }
97e5c72001-07-28Martin Nilsson //!
d8a6fc2000-03-25Fredrik Hübinette (Hubbe) string master_read_file(string file) { object o=_static_modules.files()->Fd();
da24482002-05-24Martin Nilsson  if( ([function(string, string : int)]o->open)(fakeroot(file),"r") ) return ([function(void : string)]o->read)();
d8a6fc2000-03-25Fredrik Hübinette (Hubbe)  return 0; }
ca2b071998-03-28Henrik Grubbström (Grubba) #ifdef GETCWD_CACHE
3310402004-03-16Henrik Grubbström (Grubba) static string current_path;
ca2b071998-03-28Henrik Grubbström (Grubba) int cd(string s) { current_path=0; return predef::cd(s); } string getcwd() { return current_path || (current_path=predef::getcwd()); }
2f520e2002-04-28Martin Nilsson #endif // GETCWD_CACHE
ca2b071998-03-28Henrik Grubbström (Grubba)  string combine_path_with_cwd(string path) {
471ed91998-04-24Fredrik Hübinette (Hubbe)  return combine_path(IS_ABSOLUTE_PATH(path)?"/":getcwd(),path);
ca2b071998-03-28Henrik Grubbström (Grubba) } #ifdef FILE_STAT_CACHE #define FILE_STAT_CACHE_TIME 20 int invalidate_time; mapping(string:multiset(string)) dir_cache = ([]);
61a4242000-08-27Mirar (Pontus Hagland) Stat master_file_stat(string x)
ca2b071998-03-28Henrik Grubbström (Grubba) {
da24482002-05-24Martin Nilsson  string dir = combine_path_with_cwd(x); string file = BASENAME(dir); dir = dirname(dir);
ca2b071998-03-28Henrik Grubbström (Grubba)  if(time() > invalidate_time) {
da24482002-05-24Martin Nilsson  dir_cache = ([]); invalidate_time = time()+FILE_STAT_CACHE_TIME;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
da24482002-05-24Martin Nilsson  multiset(string) d = dir_cache[dir]; if( zero_type(d) )
ca2b071998-03-28Henrik Grubbström (Grubba)  {
da24482002-05-24Martin Nilsson  array(string) tmp = get_dir(dir); if(tmp)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
fc398b2001-05-31Fredrik Hübinette (Hubbe) #ifdef __NT__
da24482002-05-24Martin Nilsson  tmp = map(tmp, lower_case);
fc398b2001-05-31Fredrik Hübinette (Hubbe) #endif
da24482002-05-24Martin Nilsson  d = dir_cache[dir] = aggregate_multiset(@tmp);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
da24482002-05-24Martin Nilsson  else dir_cache[dir]=0;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
fc398b2001-05-31Fredrik Hübinette (Hubbe) #ifdef __NT__
da24482002-05-24Martin Nilsson  file = lower_case(file);
fc398b2001-05-31Fredrik Hübinette (Hubbe) #endif
ca2b071998-03-28Henrik Grubbström (Grubba)  if(d && !d[file]) return 0; return predef::file_stat(x); } #else #define master_file_stat file_stat
2f520e2002-04-28Martin Nilsson #endif // FILE_STAT_CACHE
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba) //! Mapping containing the environment variables. //! //! The mapping currently has the following structure: //! @mapping //! @member array(string) index //! Note that the index is @[lower_case()]'d on NT. //! @array //! @elem string varname //! Variable name with case intact. //! @elem string value //! Variable value. //! @endarray //! @endmapping //! //! @note //! This mapping should not be accessed directly; use @[getenv()] //! and @[putenv()] instead. //! //! @note //! This mapping is not compatible with @[Process.create_process()]; //! use the mapping returned from calling @[getenv()] without arguments //! instead. mapping(string:array(string)) environment=([]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
fc398b2001-05-31Fredrik Hübinette (Hubbe) 
8f45692001-01-14Henrik Grubbström (Grubba) //! @decl string getenv(string varname) //! @decl mapping(string:string) getenv()
b07e962001-07-27Martin Nilsson //! @appears getenv
8f45692001-01-14Henrik Grubbström (Grubba) //!
a71f832001-11-13Tomas Nilsson //! When called with no arguments, a mapping with all current environment
f324812003-01-09Henrik Grubbström (Grubba) //! variables will be returned. Destructive opreations on the mapping //! will not affect the internal environment representation.
8f45692001-01-14Henrik Grubbström (Grubba) //! //! If the @[varname] argument has been given, the value of the environment //! variable with the name @[varname] will be returned. If no such
3310402004-03-16Henrik Grubbström (Grubba) //! environment variable exists, @expr{0@} (zero) will be returned.
8f45692001-01-14Henrik Grubbström (Grubba) //!
a71f832001-11-13Tomas Nilsson //! On NT the environment variable name is case insensitive. //!
944d5b1999-12-05Henrik Grubbström (Grubba) string|mapping(string:string) getenv(string|void s)
ca2b071998-03-28Henrik Grubbström (Grubba) {
da24482002-05-24Martin Nilsson  if(!s) return [mapping(string:string)]aggregate_mapping( @(values(environment)*({}) ) );
a71f832001-11-13Tomas Nilsson #ifdef __NT__ s = lower_case(s); #endif return environment[s] && environment[s][1];
df2c632000-08-02Henrik Grubbström (Grubba) }
fc398b2001-05-31Fredrik Hübinette (Hubbe) 
b07e962001-07-27Martin Nilsson //! @appears compile_file
fc398b2001-05-31Fredrik Hübinette (Hubbe) //! Compile the Pike code contained in the file @[filename] into a program. //! //! This function will compile the file @[filename] to a Pike program that can //! later be instantiated. It is the same as doing
3310402004-03-16Henrik Grubbström (Grubba) //! @expr{@[compile_string](@[Stdio.read_file](@[filename]), @[filename])@}.
fc398b2001-05-31Fredrik Hübinette (Hubbe) //! //! @seealso //! @[compile()], @[compile_string()], @[cpp()] //! program compile_file(string filename, object|void handler, void|program p, void|object o) { AUTORELOAD_CHECK_FILE(filename); return compile(cpp(master_read_file(filename), filename, 1, handler, compat_major, compat_minor), handler, compat_major, compat_minor, p, o); }
df2c632000-08-02Henrik Grubbström (Grubba) #if 0 variant mapping(string:string) getenv() { return environment + ([]); } variant string getenv(string s) { return environment[s]; } function(:mapping(string:string))|function(string:string) getenv(s) { if(!s) return environment + ([]); return environment[s]; }
b07e962001-07-27Martin Nilsson // mapping(string:string) getenv() |
df2c632000-08-02Henrik Grubbström (Grubba) string getenv(string s) { if(!s) return environment + ([]);
ca2b071998-03-28Henrik Grubbström (Grubba)  return environment[s]; }
df2c632000-08-02Henrik Grubbström (Grubba) #endif /* 0 */
ca2b071998-03-28Henrik Grubbström (Grubba) 
b07e962001-07-27Martin Nilsson //! @appears putenv
8f45692001-01-14Henrik Grubbström (Grubba) //! Sets the environment variable @[varname] to @[value]. //!
a71f832001-11-13Tomas Nilsson //! On NT the environment variable name is case insensitive. //!
8f45692001-01-14Henrik Grubbström (Grubba) //! @seealso //! @[getenv()] //! void putenv(string varname, string value)
ca2b071998-03-28Henrik Grubbström (Grubba) {
a71f832001-11-13Tomas Nilsson  string index = varname; #ifdef __NT__ index = lower_case(varname); if (environment[index] && environment[index][0]) varname = environment[index][0]; #endif environment[index] = ({ varname, value });
ca2b071998-03-28Henrik Grubbström (Grubba) }
b07e962001-07-27Martin Nilsson //! @appears normalize_path
c57eda2001-08-29Martin Nilsson //! Replaces "\" with "/" if runing on MS Windows. It is
c2d49b2002-02-14Martin Nilsson //! adviced to use @[System.normalize_path] instead.
c57eda2001-08-29Martin Nilsson string normalize_path( string path )
4c3f7d2000-04-13Per Hedbor { #ifndef __NT__
c57eda2001-08-29Martin Nilsson  return path;
4c3f7d2000-04-13Per Hedbor #else
c57eda2001-08-29Martin Nilsson  return replace(path,"\\","/");
4c3f7d2000-04-13Per Hedbor #endif }
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba) //! Mapping containing the cache of currently compiled files. //! //! This mapping currently has the following structure: //! @mapping //! @member program filename //! @endmapping //! //! @note //! As a special case the current master program is available //! under the name @expr{"/master"@}. mapping(string:program|NoValue) programs=(["/master":object_program(this_object())]); mapping (program:object|NoValue) objects=([ object_program(this_object()):this_object(), object_program(_static_modules): _static_modules ]); mapping(string:object|NoValue) fc=([]); // Note: It's assumed that the mappings above never decrease in size // except in *_reverse_lookup(). no_value is used for entries that // should be considered removed. constant no_value = (<>); constant NoValue = typeof (no_value); // The reverse mapping for objects isn't only for speed; search() // doesn't work reliably there since it calls `==. static mapping(program:string) rev_programs = ([]); static mapping(object:program) rev_objects = ([]); static mapping(mixed:string) rev_fc = ([]); string programs_reverse_lookup (program prog) //! Returns the path for @[prog] in @[programs], if it got any. { if (sizeof (rev_programs) < sizeof (programs)) { foreach (programs; string path; program|NoValue prog) if (prog == no_value) m_delete (programs, path); else rev_programs[prog] = path; } return rev_programs[prog]; } program objects_reverse_lookup (object obj) //! Returns the program for @[obj] in @[objects], if it got any. { if (sizeof (rev_objects) < sizeof (objects)) { foreach (objects; program prog; object|NoValue obj) if (obj == no_value) m_delete (objects, obj); else rev_objects[obj] = prog; } return rev_objects[obj]; } string fc_reverse_lookup (object obj) //! Returns the path for @[obj] in @[fc], if it got any. { if (sizeof (rev_fc) < sizeof (fc)) { foreach (fc; string path; mixed obj) if (obj == no_value) m_delete (fc, obj); else rev_fc[obj] = path; } return rev_fc[obj]; }
ca2b071998-03-28Henrik Grubbström (Grubba) 
6d926c1999-11-26Henrik Grubbström (Grubba) array(string) query_precompiled_names(string fname) { // Filenames of potential precompiled files in priority order.
8a49852002-03-10Martin Stjernholm #ifdef PRECOMPILED_SEARCH_MORE // Search for precompiled files in all module directories, not just // in the one where the source file is. This is useful when running // pike directly from the build directory. fname = fakeroot (fname); // FIXME: Not sure if this works correctly with the fakeroot and // module relocation stuff. foreach (pike_module_path, string path) if (has_prefix (fname, path)) return map (pike_module_path, `+, "/", fname[sizeof (path)..], ".o"); #endif
6d926c1999-11-26Henrik Grubbström (Grubba)  return ({ fname + ".o" }); }
6330192000-02-10Fredrik Hübinette (Hubbe) #if constant(_static_modules.Builtin.mutex) #define THREADED
da24482002-05-24Martin Nilsson _static_modules.Builtin.mutex compilation_mutex = _static_modules.Builtin()->mutex();
6330192000-02-10Fredrik Hübinette (Hubbe) #endif
aa68b12001-03-19Fredrik Hübinette (Hubbe) static program low_findprog(string pname, string ext, object|void handler, void|int mkobj)
ca2b071998-03-28Henrik Grubbström (Grubba) { program ret;
61a4242000-08-27Mirar (Pontus Hagland)  Stat s;
ca2b071998-03-28Henrik Grubbström (Grubba)  string fname=pname+ext;
bec57d1999-09-06Fredrik Hübinette (Hubbe) 
6330192000-02-10Fredrik Hübinette (Hubbe) #ifdef THREADED
3b11422000-02-13Henrik Grubbström (Grubba)  object key; // FIXME: The catch is needed, since we might be called in // a context when threads are disabled. // (compile() disables threads).
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed err = catch {
3b11422000-02-13Henrik Grubbström (Grubba)  key=compilation_mutex->lock(2); };
098c8a2000-03-30Henrik Grubbström (Grubba)  if (err) {
2f520e2002-04-28Martin Nilsson  werror( "low_findprog: Caught spurious error:\n" "%s\n", describe_backtrace(err) );
098c8a2000-03-30Henrik Grubbström (Grubba)  }
6330192000-02-10Fredrik Hübinette (Hubbe) #endif
4839dd2001-09-02Marcus Comstedt #ifdef PIKE_MODULE_RELOC fname = unrelocate_module(fname); #endif
bec57d1999-09-06Fredrik Hübinette (Hubbe) #ifdef PIKE_AUTORELOAD if(!autoreload_on || load_time[fname]>=time()) #endif {
3310402004-03-16Henrik Grubbström (Grubba)  if(!zero_type (ret=programs[fname]) && ret != no_value) { resolv_debug ("low_findprog %s: returning cached (no autoreload)\n", fname);
401fa02002-12-02Martin Stjernholm  return ret; }
bec57d1999-09-06Fredrik Hübinette (Hubbe)  }
41ee482000-02-28Fredrik Hübinette (Hubbe) #ifdef __NT__ // Ugly kluge to work better with cygwin32 if(getenv("OSTYPE")=="cygwin32") { string tmp=fname[..1]; if((tmp=="//" || tmp=="\\\\") && (fname[3]=='/' || fname[3]=='\\')) { if(!master_file_stat(fname)) { fname=fname[2..2]+":"+fname[3..]; } } } #endif
3310402004-03-16Henrik Grubbström (Grubba)  if( (s=master_file_stat(fakeroot(fname))) && s->isreg )
ca2b071998-03-28Henrik Grubbström (Grubba)  {
bec57d1999-09-06Fredrik Hübinette (Hubbe)  AUTORELOAD_BEGIN();
14fc641999-11-26Henrik Grubbström (Grubba) 
6e2ebb1999-12-28Martin Stjernholm #ifdef PIKE_AUTORELOAD
3310402004-03-16Henrik Grubbström (Grubba)  if (load_time[fname] > s->mtime) if (!zero_type (ret=programs[fname]) && ret != no_value) { resolv_debug ("low_findprog %s: returning cached (autoreload)\n", fname);
401fa02002-12-02Martin Stjernholm  return ret; }
6e2ebb1999-12-28Martin Stjernholm #endif
14fc641999-11-26Henrik Grubbström (Grubba) 
ca2b071998-03-28Henrik Grubbström (Grubba)  switch(ext) { case "": case ".pike":
6d926c1999-11-26Henrik Grubbström (Grubba)  foreach(query_precompiled_names(fname), string oname) {
61a4242000-08-27Mirar (Pontus Hagland)  if(Stat s2=master_file_stat(fakeroot(oname)))
3310402004-03-16Henrik Grubbström (Grubba)  { if(s2->isreg && s2->mtime >= s->mtime)
6d926c1999-11-26Henrik Grubbström (Grubba)  { mixed err=catch { AUTORELOAD_CHECK_FILE(oname);
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("low_findprog %s: decoding dumped\n", fname); INC_RESOLV_MSG_DEPTH();
401fa02002-12-02Martin Stjernholm  ret = decode_value(master_read_file(oname),
3310402004-03-16Henrik Grubbström (Grubba)  (handler && handler->get_codec || get_codec)(fname, mkobj)); DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: dump decode ok\n", fname);
401fa02002-12-02Martin Stjernholm  return programs[fname] = ret;
6d926c1999-11-26Henrik Grubbström (Grubba)  };
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: dump decode failed\n", fname); programs[fname] = no_value;
5d6a562001-11-08Fredrik Hübinette (Hubbe)  if (handler && handler->compile_warning) {
6d926c1999-11-26Henrik Grubbström (Grubba)  handler->compile_warning(oname, 0, sprintf("Decode failed:\n"
88a2fa2001-08-16Martin Stjernholm  "\t%s", describe_error(err)));
6d926c1999-11-26Henrik Grubbström (Grubba)  } else { compile_warning(oname, 0, sprintf("Decode failed:\n"
88a2fa2001-08-16Martin Stjernholm  "\t%s", describe_error(err)));
6d926c1999-11-26Henrik Grubbström (Grubba)  }
e115cb1999-12-09Henrik Grubbström (Grubba)  } else if (out_of_date_warning) {
280de72002-03-08Martin Stjernholm  if (handler && handler->compile_warning) {
6d926c1999-11-26Henrik Grubbström (Grubba)  handler->compile_warning(oname, 0, "Compiled file is out of date\n"); } else { compile_warning(oname, 0, "Compiled file is out of date\n"); }
10e16f1999-11-04Henrik Grubbström (Grubba)  }
b085961998-05-19Fredrik Hübinette (Hubbe)  }
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
bec57d1999-09-06Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("low_findprog %s: compiling, mkobj: %O\n", fname, mkobj); INC_RESOLV_MSG_DEPTH();
401fa02002-12-02Martin Stjernholm  programs[fname]=ret=__empty_program(0, fname);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  if ( mixed e=catch { ret=compile_file(fname,
34241f2001-05-24Henrik Grubbström (Grubba)  handler,
aa68b12001-03-19Fredrik Hübinette (Hubbe)  ret, mkobj? (objects[ret]=__null_program()) : 0); } )
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: compilation failed\n", fname); objects[ret] = no_value; ret=programs[fname]=0; // Negative cache.
c46d682000-01-05Martin Stjernholm  throw(e);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: compilation ok\n", fname);
ca2b071998-03-28Henrik Grubbström (Grubba)  break;
d5a9da2004-03-22Martin Stjernholm 
ca2b071998-03-28Henrik Grubbström (Grubba) #if constant(load_module) case ".so":
caa3221998-04-15Henrik Grubbström (Grubba)  if (fname == "") {
2f520e2002-04-28Martin Nilsson  werror( "low_findprog(\"%s\", \"%s\") => load_module(\"\")\n" "%s\n", pname, ext, describe_backtrace(backtrace()) );
caa3221998-04-15Henrik Grubbström (Grubba)  }
26f3da2000-07-11Fredrik Hübinette (Hubbe) 
d5a9da2004-03-22Martin Stjernholm  if (array|object err = catch (ret=load_module(fakeroot(fname)))) { if (objectp (err) && err->is_dlopen_error) { // Do not treat errors from dlopen(3) as exceptions since in // a dist we can have .so files that are dynamically linked // against libraries that don't exist on the system, and in // that case we should just treat the module as nonexisting. // // What we really want is to do this only for errors that // are due to nonexisting files, but the error reporting // from dlerror(3) doesn't allow us to tell those from other // errors. (handler && handler->compile_warning || compile_warning) (fname, 0, "Failed to load library: " + err->__dlerror); ret = 0; } else throw (err); }
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("low_findprog %s: loaded binary\n", fname);
ca2b071998-03-28Henrik Grubbström (Grubba) #endif /* load_module */ }
bec57d1999-09-06Fredrik Hübinette (Hubbe)  AUTORELOAD_FINISH(ret,programs,fname);
6e2ebb1999-12-28Martin Stjernholm 
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("low_findprog %s: returning %O\n", fname, ret);
ca2b071998-03-28Henrik Grubbström (Grubba)  return programs[fname]=ret; }
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("low_findprog %s: file not found\n", fname);
6e2ebb1999-12-28Martin Stjernholm  return 0;
ca2b071998-03-28Henrik Grubbström (Grubba) }
eaa4da2001-10-04Fredrik Hübinette (Hubbe) //
a002d32002-03-01Martin Stjernholm // This function is called by the compiler when a delayed compilation // error occurs in the given program. It should remove all references // to the program so that it can be freed.
eaa4da2001-10-04Fredrik Hübinette (Hubbe) // void unregister(program p) {
a002d32002-03-01Martin Stjernholm  if(string fname=search(programs,p)) {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("unregister %s\n", fname); programs[fname] = no_value;
a002d32002-03-01Martin Stjernholm  // FIXME: The following assumes that programs are always stored // with '/' as path separators, even on NT. Haven't checked if // that always is the case. fname = dirname (fname);
da24482002-05-24Martin Nilsson  object n;
3310402004-03-16Henrik Grubbström (Grubba)  if ( fname!="" && objectp (n = fc[fname]) )
a002d32002-03-01Martin Stjernholm  if (n->is_resolv_dirnode || n->is_resolv_joinnode) n->delete_value (p); }
3310402004-03-16Henrik Grubbström (Grubba)  if (objectp (objects[p])) objects[p] = no_value;
c6cf602001-12-13Martin Stjernholm  foreach (fc; string name; mixed mod) if (objectp(mod) && object_program(mod) == p)
3310402004-03-16Henrik Grubbström (Grubba)  fc[name] = no_value;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) }
aa68b12001-03-19Fredrik Hübinette (Hubbe) static program findprog(string pname, string ext, object|void handler, void|int mkobj)
ca2b071998-03-28Henrik Grubbström (Grubba) { switch(ext) { case ".pike": case ".so":
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return low_findprog(pname,ext,handler, mkobj);
ca2b071998-03-28Henrik Grubbström (Grubba)  default: pname+=ext; return
aa68b12001-03-19Fredrik Hübinette (Hubbe)  low_findprog(pname,"", handler, mkobj) || low_findprog(pname,".pike", handler, mkobj) || low_findprog(pname,".so", handler, mkobj);
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
aa68b12001-03-19Fredrik Hübinette (Hubbe) program low_cast_to_program(string pname, string current_file, object|void handler, void|int mkobj)
ca2b071998-03-28Henrik Grubbström (Grubba) { string ext; string nname;
471ed91998-04-24Fredrik Hübinette (Hubbe)  array(string) tmp=EXPLODE_PATH(pname);
ca2b071998-03-28Henrik Grubbström (Grubba) 
34241f2001-05-24Henrik Grubbström (Grubba)  // werror("low_cast_to_program(%O, %O, %O, %O)\n", // pname, current_file, handler, mkobj);
ca2b071998-03-28Henrik Grubbström (Grubba)  if(sscanf(reverse(tmp[-1]),"%s.%s",ext, nname)) { ext="."+reverse(ext); tmp[-1]=reverse(nname); pname=tmp*"/";
2f520e2002-04-28Martin Nilsson  } else {
ca2b071998-03-28Henrik Grubbström (Grubba)  ext=""; }
2f520e2002-04-28Martin Nilsson 
471ed91998-04-24Fredrik Hübinette (Hubbe)  if(IS_ABSOLUTE_PATH(pname))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  program|NoValue prog = programs[pname]; if (programp (prog)) return prog;
ca2b071998-03-28Henrik Grubbström (Grubba)  pname=combine_path("/",pname);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return findprog(pname,ext,handler,mkobj);
2f520e2002-04-28Martin Nilsson  } else {
ca2b071998-03-28Henrik Grubbström (Grubba)  string cwd; if(current_file) {
2f520e2002-04-28Martin Nilsson  cwd=dirname(current_file); } else {
ca2b071998-03-28Henrik Grubbström (Grubba)  cwd=getcwd(); }
aa68b12001-03-19Fredrik Hübinette (Hubbe)  if(program ret=findprog(combine_path(cwd,pname),ext,handler,mkobj))
ca2b071998-03-28Henrik Grubbström (Grubba)  return ret; foreach(pike_program_path, string path)
aa68b12001-03-19Fredrik Hübinette (Hubbe)  if(program ret=findprog(combine_path(path,pname),ext,handler,mkobj))
ca2b071998-03-28Henrik Grubbström (Grubba)  return ret; return 0; } }
aa68b12001-03-19Fredrik Hübinette (Hubbe) 
97e5c72001-07-28Martin Nilsson //! This function is called when the driver wants to cast a string //! to a program, this might be because of an explicit cast, an inherit //! or a implict cast. In the future it might receive more arguments, //! to aid the master finding the right program.
aa68b12001-03-19Fredrik Hübinette (Hubbe) program cast_to_program(string pname, string current_file, object|void handler) {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("cast_to_program(%O, %O)\n", pname, current_file); INC_RESOLV_MSG_DEPTH(); program ret = low_cast_to_program(pname, current_file, handler); DEC_RESOLV_MSG_DEPTH(); resolv_debug ("cast_to_program(%O, %O) => %O\n", pname, current_file, ret); return ret;
aa68b12001-03-19Fredrik Hübinette (Hubbe) }
97e5c72001-07-28Martin Nilsson //! This function is called when an error occurs that is not caught //! with catch().
06d6d31999-12-13Henrik Grubbström (Grubba) void handle_error(array(mixed)|object trace)
ca2b071998-03-28Henrik Grubbström (Grubba) {
62e87b1998-04-14Fredrik Hübinette (Hubbe)  if(mixed x=catch { werror(describe_backtrace(trace));
d28a712001-07-02Henrik Grubbström (Grubba)  }) {
9f71092000-11-20Martin Stjernholm  // One reason for this might be too little stack space, which // easily can occur for "out of stack" errors. It should help to // tune up the STACK_MARGIN values in interpret.c then.
62e87b1998-04-14Fredrik Hübinette (Hubbe)  werror("Error in handle_error in master object:\n");
9f71092000-11-20Martin Stjernholm  if(catch {
d28a712001-07-02Henrik Grubbström (Grubba)  catch { if (catch {
da24482002-05-24Martin Nilsson  string msg = [string]x[0]; array bt = [array]x[1];
d28a712001-07-02Henrik Grubbström (Grubba)  werror("%s%O\n", msg, bt); }) { werror("%O\n", x); } }; werror("Original error:\n" "%O\n", trace);
aa73fc1999-10-21Fredrik Hübinette (Hubbe)  }) { werror("sprintf() failed to write error.\n"); }
62e87b1998-04-14Fredrik Hübinette (Hubbe)  }
ca2b071998-03-28Henrik Grubbström (Grubba) }
c5cc2d1998-04-29Henrik Grubbström (Grubba) /* This array contains the names of the functions * that a replacing master-object may want to override. */ constant master_efuns = ({
4f64a52002-05-22Martin Nilsson  "error",
c5cc2d1998-04-29Henrik Grubbström (Grubba)  "basename", "dirname", "is_absolute_path", "explode_path", "compile_string", "compile_file", "add_include_path", "remove_include_path", "add_module_path", "remove_module_path", "add_program_path", "remove_program_path", "describe_backtrace",
a66ff22000-01-11Martin Stjernholm  "describe_error",
4c3f7d2000-04-13Per Hedbor  "normalize_path",
c5cc2d1998-04-29Henrik Grubbström (Grubba)  "getenv", "putenv", #ifdef GETCWD_CACHE "cd", "getcwd", #endif });
28c5382002-09-09Marcus Comstedt string include_prefix;
3310402004-03-16Henrik Grubbström (Grubba)  //! @decl int strlen(string|multiset|array|mapping|object thing) //! @appears strlen //! Alias for @[sizeof]. //! @deprecated sizeof //! @decl int write(string fmt, mixed ... args) //! @appears write //! Writes a string on stdout. Works just like @[Stdio.File.write] //! on @[Stdio.stdout].
ca2b071998-03-28Henrik Grubbström (Grubba) /* Note that create is called before add_precompiled_program */ void create() {
c5cc2d1998-04-29Henrik Grubbström (Grubba)  object o = this_object();
ca2b071998-03-28Henrik Grubbström (Grubba) 
9066122002-11-28Martin Nilsson  foreach(master_efuns, string e) if (o[e])
c5cc2d1998-04-29Henrik Grubbström (Grubba)  add_constant(e, o[e]);
9066122002-11-28Martin Nilsson  else error("Function %O is missing from master.pike.\n", e);
c5cc2d1998-04-29Henrik Grubbström (Grubba)  add_constant("strlen", sizeof); add_constant("write", _static_modules.files()->_stdout->write);
ca2b071998-03-28Henrik Grubbström (Grubba) 
87f9082001-03-12Fredrik Hübinette (Hubbe) #define CO(X) add_constant(#X,_static_modules.Builtin.__backend->X) CO(call_out); CO(_do_call_outs); CO(find_call_out); CO(remove_call_out); CO(call_out_info);
e37a3e1999-10-09Fredrik Hübinette (Hubbe) #if "¤share_prefix¤"[0]!='¤'
3310402004-03-16Henrik Grubbström (Grubba)  // add path for architecture-independant files
e37a3e1999-10-09Fredrik Hübinette (Hubbe)  add_include_path("¤share_prefix¤/include"); add_module_path("¤share_prefix¤/modules"); #endif #if "¤lib_prefix¤"[0]!='¤' // add path for architecture-dependant files add_include_path("¤lib_prefix¤/include"); add_module_path("¤lib_prefix¤/modules"); #endif
28c5382002-09-09Marcus Comstedt  #if "¤include_prefix¤"[0]!='¤' include_prefix = "¤include_prefix¤"; #endif
ca2b071998-03-28Henrik Grubbström (Grubba) }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever a inherit is called for. //! It is supposed to return the program to inherit. //! The first argument is the argument given to inherit, and the second //! is the file name of the program currently compiling. Note that the //! file name can be changed with #line, or set by compile_string, so //! it can not be 100% trusted to be a filename. //! previous_object(), can be virtually anything in this function, as it //! is called from the compiler.
10e16f1999-11-04Henrik Grubbström (Grubba) program handle_inherit(string pname, string current_file, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("handle_inherit(%O, %O)\n", pname, current_file); INC_RESOLV_MSG_DEPTH(); program ret = low_cast_to_program(pname, current_file, handler); DEC_RESOLV_MSG_DEPTH(); resolv_debug ("handle_inherit(%O, %O) => %O\n", pname, current_file, ret); return ret;
ca2b071998-03-28Henrik Grubbström (Grubba) }
34241f2001-05-24Henrik Grubbström (Grubba) object low_cast_to_object(string oname, string current_file, object|void current_handler)
ca2b071998-03-28Henrik Grubbström (Grubba) { program p; object o;
34241f2001-05-24Henrik Grubbström (Grubba)  p = low_cast_to_program(oname, current_file, current_handler, 1);
ca2b071998-03-28Henrik Grubbström (Grubba)  if(!p) return 0;
3310402004-03-16Henrik Grubbström (Grubba)  // NB: p might be a function in a fake_object...
401fa02002-12-02Martin Stjernholm  if(!objectp (o=objects[p])) o=objects[p]=p();
ca2b071998-03-28Henrik Grubbström (Grubba)  return o; }
97e5c72001-07-28Martin Nilsson //! This function is called when the drivers wants to cast a string //! to an object because of an implict or explicit cast. This function //! may also receive more arguments in the future.
ca2b071998-03-28Henrik Grubbström (Grubba) object cast_to_object(string oname, string current_file) {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("cast_to_object(%O, %O)\n", oname, current_file); INC_RESOLV_MSG_DEPTH();
401fa02002-12-02Martin Stjernholm  object o = low_cast_to_object(oname, current_file);
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("cast_to_object(%O, %O) => %O\n", oname, current_file, o);
401fa02002-12-02Martin Stjernholm  if (objectp (o)) return o;
ac0fed2000-03-25Fredrik Hübinette (Hubbe)  error("Cast '"+oname+"' to object failed"+
b1693a2000-04-07Fredrik Hübinette (Hubbe)  ((current_file && current_file!="-")?sprintf(" for '%s'",current_file):"")+".\n");
ac0fed2000-03-25Fredrik Hübinette (Hubbe)  return 0;
ca2b071998-03-28Henrik Grubbström (Grubba) }
eaa4da2001-10-04Fredrik Hübinette (Hubbe) class dirnode
ca2b071998-03-28Henrik Grubbström (Grubba) {
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  string dirname; object|void handler;
4817482000-05-11Martin Stjernholm  constant is_resolv_dirnode = 1;
401fa02002-12-02Martin Stjernholm  // objectp() is intentionally not used on the module object, to // allow a module to deny its own existence with `!.
14bb592000-05-06Fredrik Hübinette (Hubbe)  mixed module=module_checker();
098c8a2000-03-30Henrik Grubbström (Grubba)  mapping(string:mixed) cache=([]); array(string) files;
ca2b071998-03-28Henrik Grubbström (Grubba) 
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  void create(string d, object|void h) {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("dirnode(%O,%O) created\n",d,h);
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  dirname=d; handler=h; fc[dirname]=this_object();
4a6f0d2002-12-09Martin Stjernholm  if(has_suffix(dirname,".pmod"))
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  fc[dirname[..sizeof(dirname)-6]]=this_object(); }
3310402004-03-16Henrik Grubbström (Grubba)  void get_files() { if (!(files = get_dir(fakeroot(dirname)))) { werror ("Error listing module directory %O: %s\n", dirname, strerror (errno())); files = ({}); } }
14bb592000-05-06Fredrik Hübinette (Hubbe)  class module_checker { int `!() {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("dirnode(%O)->module_checker()->`!()\n",dirname); INC_RESOLV_MSG_DEPTH(); if (mixed err = catch { if(module=findmodule(dirname+"/module", handler)) { if(mixed tmp=module->_module_value) module=tmp; /* This allows for `[] to have side effects first time * it is called. (Specifically, the Calendar module uses * this */ cache=([]); _cache_full=0; } DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->module_checker()->`!() => %s\n", dirname, !module ? "doesn't exist" : "exists"); return !module; }) { //werror ("findmodule error: " + describe_backtrace (err)); // findmodule() failed. This can occur due to circularities // between encode_value()'ed programs. // The error will then typically be: // "Cannot call functions in unfinished objects." // Pretend not to exist for now... DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->module_checker()->`!() => failure, doesn't exist\n", dirname); return 1;
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  }
14bb592000-05-06Fredrik Hübinette (Hubbe)  }
52c5792000-05-23Fredrik Hübinette (Hubbe) 
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  mixed `[](string index)
cd99712001-11-09Martin Stjernholm  {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("dirnode(%O)->module_checker()[%O] => %O\n", dirname, index, module && module[index]); return module && module[index];
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  }
52c5792000-05-23Fredrik Hübinette (Hubbe)  array(string) _indices() { if(module) return indices(module); } array _values() { if(module) return values(module); }
ca2b071998-03-28Henrik Grubbström (Grubba)  }
098c8a2000-03-30Henrik Grubbström (Grubba)  static mixed ind(string index)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("dirnode(%O)->ind(%O)\n", dirname, index); INC_RESOLV_MSG_DEPTH(); if(module)
e118831999-12-22Per Hedbor  {
cd99712001-11-09Martin Stjernholm  mixed o;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) // _describe(module);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  if(!zero_type(o=module[index])) {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->ind(%O) => found %O\n", dirname, index, o);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return o; }
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("dirnode(%O)->ind(%O) => not found in module\n", dirname, index);
a2faf61999-12-01Martin Stjernholm  }
3310402004-03-16Henrik Grubbström (Grubba)  else resolv_debug ("dirnode(%O)->ind(%O) => no module\n", dirname, index);
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  if(!files) get_files();
e118831999-12-22Per Hedbor 
3310402004-03-16Henrik Grubbström (Grubba)  int(0..1) ret;
e118831999-12-22Per Hedbor  foreach( files, string s )
8c03142002-11-15Martin Nilsson  if( has_value(s, index) || has_value(index,s) )
e118831999-12-22Per Hedbor  { ret=1; break; }
3310402004-03-16Henrik Grubbström (Grubba)  if(!ret) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->ind(%O) => no file match\n", dirname, index);
e118831999-12-22Per Hedbor  return UNDEFINED;
3310402004-03-16Henrik Grubbström (Grubba)  } string fullname = dirname+"/"+index; object o; if(objectp(o=findmodule(fullname, handler)))
ca2b071998-03-28Henrik Grubbström (Grubba)  { if(mixed tmp=o->_module_value) o=tmp;
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->ind(%O) => found submodule %O\n", dirname, index, o);
ca2b071998-03-28Henrik Grubbström (Grubba)  return o; }
3310402004-03-16Henrik Grubbström (Grubba)  if (program p=cast_to_program( fullname, 0, handler ))
aa68b12001-03-19Fredrik Hübinette (Hubbe)  {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->ind(%O) => found subprogram %O\n", dirname, index, p);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return p; }
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->ind(%O) => not found\n", dirname, index);
73e9551999-12-28Martin Stjernholm  return UNDEFINED;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed `[](string index)
ca2b071998-03-28Henrik Grubbström (Grubba)  { mixed ret;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE
3310402004-03-16Henrik Grubbström (Grubba)  werror("%*nDirnode(%O) cache[%O] ?????\n", sizeof(backtrace()),dirname,index);
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #endif if(!zero_type(ret=cache[index]))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE
3310402004-03-16Henrik Grubbström (Grubba)  werror("%*nDirnode(%O) cache[%O] => %O%s\n", sizeof(backtrace()),dirname,index, ret, (ret != ZERO_TYPE)?"":" (zero_type)");
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #endif
cd99712001-11-09Martin Stjernholm  if (ret != ZERO_TYPE) return ret;
3310402004-03-16Henrik Grubbström (Grubba) #ifdef MODULE_TRACE werror("%*nDirnode(%O) ZERO_TYPE!\n", sizeof(backtrace()),dirname); #endif
ca2b071998-03-28Henrik Grubbström (Grubba)  return UNDEFINED; }
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  ret=ind(index);
4a6f0d2002-12-09Martin Stjernholm  // We might have gotten placeholder objects in the first pass // which must not be cached to the second.
3310402004-03-16Henrik Grubbström (Grubba)  if(ret == predef::__placeholder_object) { #ifdef MODULE_TRACE werror("%*nDirnode(%O) PLACE_HOLDER.\n", sizeof(backtrace()),dirname); #endif return ret; }
4a6f0d2002-12-09Martin Stjernholm 
cd99712001-11-09Martin Stjernholm  cache[index] = zero_type(ret) ? ZERO_TYPE : ret; return ret;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3e4b811998-10-16Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  static int(0..1) _cache_full;
3e4b811998-10-16Henrik Grubbström (Grubba)  void fill_cache() {
3310402004-03-16Henrik Grubbström (Grubba) #if 0
f4f08d1999-02-26Henrik Grubbström (Grubba)  werror(describe_backtrace(({ sprintf("Filling cache in dirnode %O\n", dirname), backtrace() })));
3310402004-03-16Henrik Grubbström (Grubba) #endif
3e4b811998-10-16Henrik Grubbström (Grubba)  if (_cache_full) { return; } if (module) { foreach(indices(module), string index) { cache[index] = module[index]; } }
3310402004-03-16Henrik Grubbström (Grubba)  if(!files) get_files();
5be4aa2001-11-12Martin Stjernholm 
e118831999-12-22Per Hedbor  foreach(files, string fname) {
0e68e62000-02-04Henrik Grubbström (Grubba)  mixed err = catch {
3e4b811998-10-16Henrik Grubbström (Grubba)  if (((< ".pike", ".pmod" >)[fname[sizeof(fname)-5..]]) && !zero_type(`[](fname[..sizeof(fname)-6]))) { continue;
0e68e62000-02-04Henrik Grubbström (Grubba)  } else if ((fname[sizeof(fname)-3..] == ".so") &&
3e4b811998-10-16Henrik Grubbström (Grubba)  !zero_type(`[](fname[..sizeof(fname)-4]))) { continue; } };
0e68e62000-02-04Henrik Grubbström (Grubba)  if (err) { compile_warning(dirname+"."+fname, 0, sprintf("Compilation failed:\n" "%s\n", describe_backtrace(err))); }
3e4b811998-10-16Henrik Grubbström (Grubba)  } _cache_full = 1; }
3310402004-03-16Henrik Grubbström (Grubba)  static array(string) _indices()
3e4b811998-10-16Henrik Grubbström (Grubba)  { fill_cache();
3310402004-03-16Henrik Grubbström (Grubba)  // Note: Cannot index cache at all here to filter out the // ZERO_TYPE values since that can change the order in the // mapping, and _indices() has to return the elements in the same // order as a nearby _values() call. return filter (indices (cache), map (values (cache), `!=, ZERO_TYPE));
3e4b811998-10-16Henrik Grubbström (Grubba)  }
3310402004-03-16Henrik Grubbström (Grubba)  static array(mixed) _values()
3e4b811998-10-16Henrik Grubbström (Grubba)  { fill_cache();
cd99712001-11-09Martin Stjernholm  return values(cache) - ({ZERO_TYPE});
3e4b811998-10-16Henrik Grubbström (Grubba)  }
52c5792000-05-23Fredrik Hübinette (Hubbe) 
a002d32002-03-01Martin Stjernholm  void delete_value (mixed val) { if (string name = search (cache, val)) m_delete (cache, name); }
3310402004-03-16Henrik Grubbström (Grubba)  static int(0..) _sizeof() { return sizeof(_values()); } static string _sprintf(int as) { return as=='O' && sprintf("master()->dirnode(%O)",dirname); } }
ca2b071998-03-28Henrik Grubbström (Grubba) 
6e2ebb1999-12-28Martin Stjernholm static class ZERO_TYPE {};
94257b1998-10-16Henrik Grubbström (Grubba) class joinnode {
4817482000-05-11Martin Stjernholm  constant is_resolv_joinnode = 1;
94257b1998-10-16Henrik Grubbström (Grubba)  array(object|mapping) joined_modules;
098c8a2000-03-30Henrik Grubbström (Grubba)  mapping(string:mixed) cache=([]);
94257b1998-10-16Henrik Grubbström (Grubba) 
52c5792000-05-23Fredrik Hübinette (Hubbe)  string _sprintf(int as) {
9852f72002-12-03Martin Nilsson  return as=='O' && sprintf("master()->joinnode(%O)",joined_modules);
52c5792000-05-23Fredrik Hübinette (Hubbe)  }
94257b1998-10-16Henrik Grubbström (Grubba)  void create(array(object|mapping) _joined_modules) { joined_modules = _joined_modules;
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("joinnode(%O) created\n", joined_modules);
94257b1998-10-16Henrik Grubbström (Grubba)  }
098c8a2000-03-30Henrik Grubbström (Grubba)  static mixed ind(string index)
94257b1998-10-16Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug ("joinnode(%O)->ind(%O)\n", joined_modules, index); INC_RESOLV_MSG_DEPTH();
94257b1998-10-16Henrik Grubbström (Grubba)  array(mixed) res = ({});
ca051c1999-11-23Per Hedbor  foreach(joined_modules, object|mapping o) {
94257b1998-10-16Henrik Grubbström (Grubba)  mixed ret;
ca051c1999-11-23Per Hedbor  if (!zero_type(ret = o[index])) {
8b307e1998-10-17Henrik Grubbström (Grubba)  if (objectp(ret = o[index]) &&
3ad5d62000-05-13Martin Stjernholm  (ret->is_resolv_dirnode || ret->is_resolv_joinnode))
ca051c1999-11-23Per Hedbor  {
8b307e1998-10-17Henrik Grubbström (Grubba)  // Only join directorynodes (or joinnodes).
3310402004-03-16Henrik Grubbström (Grubba)  if (ret->is_resolv_joinnode) res += ret->joined_modules; else res += ({ ret });
8cdeed2000-09-15Mirar (Pontus Hagland)  } else if ( !zero_type(ret) ) {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("joinnode(%O)->ind(%O) => found %O\n", joined_modules, index, ret);
ca051c1999-11-23Per Hedbor  return (ret);
8b307e1998-10-17Henrik Grubbström (Grubba)  } else { // Ignore continue; }
94257b1998-10-16Henrik Grubbström (Grubba)  } }
3310402004-03-16Henrik Grubbström (Grubba)  if (sizeof(res) > 1) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("joinnode(%O)->ind(%O) => new joinnode\n", joined_modules, index); return joinnode(res); } else if (sizeof(res)) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("joinnode(%O)->ind(%O) => found %O\n", joined_modules, index, res[0]);
94257b1998-10-16Henrik Grubbström (Grubba)  return res[0];
3310402004-03-16Henrik Grubbström (Grubba)  } DEC_RESOLV_MSG_DEPTH(); resolv_debug ("joinnode(%O)->ind(%O) => not found\n", joined_modules, index);
94257b1998-10-16Henrik Grubbström (Grubba)  return UNDEFINED; }
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed `[](string index)
94257b1998-10-16Henrik Grubbström (Grubba)  {
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed ret;
94257b1998-10-16Henrik Grubbström (Grubba)  if (!zero_type(ret = cache[index])) { if (ret != ZERO_TYPE) { return ret; } return UNDEFINED; } ret = ind(index);
4a6f0d2002-12-09Martin Stjernholm  // We might have gotten placeholder objects in the first pass // which must not be cached to the second. if(ret == predef::__placeholder_object) return ret;
94257b1998-10-16Henrik Grubbström (Grubba)  if (zero_type(ret)) { cache[index] = ZERO_TYPE; } else { cache[index] = ret; } return ret; }
3310402004-03-16Henrik Grubbström (Grubba) 
098c8a2000-03-30Henrik Grubbström (Grubba)  static int _cache_full;
3310402004-03-16Henrik Grubbström (Grubba) 
3e4b811998-10-16Henrik Grubbström (Grubba)  void fill_cache() {
3310402004-03-16Henrik Grubbström (Grubba) #if 0
f4f08d1999-02-26Henrik Grubbström (Grubba)  werror(describe_backtrace(({ "Filling cache in joinnode\n", backtrace() })));
3310402004-03-16Henrik Grubbström (Grubba) #endif
3e4b811998-10-16Henrik Grubbström (Grubba)  if (_cache_full) { return; }
c084262000-02-04Henrik Grubbström (Grubba)  foreach(joined_modules, object|mapping|program o) {
3e4b811998-10-16Henrik Grubbström (Grubba)  foreach(indices(o), string index) { if (zero_type(cache[index])) { `[](index); } } } _cache_full = 1; }
3310402004-03-16Henrik Grubbström (Grubba) 
3e4b811998-10-16Henrik Grubbström (Grubba)  array(string) _indices() { fill_cache();
3310402004-03-16Henrik Grubbström (Grubba)  // Note: Cannot index cache at all here to filter out the // ZERO_TYPE values since that can change the order in the // mapping, and _indices() has to return the elements in the same // order as a nearby _values() call. return filter (indices (cache), map (values (cache), `!=, ZERO_TYPE));
3e4b811998-10-16Henrik Grubbström (Grubba)  }
3310402004-03-16Henrik Grubbström (Grubba) 
3e4b811998-10-16Henrik Grubbström (Grubba)  array(mixed) _values() { fill_cache();
cd99712001-11-09Martin Stjernholm  return values(cache) - ({ZERO_TYPE});
3e4b811998-10-16Henrik Grubbström (Grubba)  }
a002d32002-03-01Martin Stjernholm  void delete_value (mixed val) { if (string name = search (cache, val)) m_delete (cache, name); for (int i = 0; i < sizeof (joined_modules); i++) { object|mapping|program o = joined_modules[i]; if (o == val) { joined_modules = joined_modules[..i - 1] + joined_modules[i + 1..]; i--; } else if (objectp (o) && (o->is_resolv_dirnode || o->is_resolv_joinnode)) o->delete_value (val); else if (string name = mappingp (o) && search (o, val)) m_delete (o, name); } }
94257b1998-10-16Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  int `== (mixed other) { return objectp (other) && other->is_resolv_joinnode && equal (mkmultiset (joined_modules), mkmultiset (other->joined_modules)); } array(object) _encode() { return joined_modules; } void _decode (array(object) joined_modules) { this_program::joined_modules = joined_modules; } };
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba) program|object findmodule(string fullname, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) {
3310402004-03-16Henrik Grubbström (Grubba)  program|object o; resolv_debug ("findmodule(%O)\n", fullname); if(!zero_type(o=fc[fullname]) && o != no_value)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  if (objectp(o) || programp(o) || o != 0) { resolv_debug ("findmodule(%O) => found %O (cached)\n", fullname, o); return o; } resolv_debug ("findmodule(%O) => not found (cached)\n", fullname); return UNDEFINED;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
61a4242000-08-27Mirar (Pontus Hagland)  if(Stat stat=master_file_stat(fakeroot(fullname+".pmod")))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  if(stat->isdir) { resolv_debug ("findmodule(%O) => new dirnode\n", fullname);
dbef7a2003-12-03Henrik Grubbström (Grubba)  return fc[fullname] = dirnode(fullname+".pmod", handler);
3310402004-03-16Henrik Grubbström (Grubba)  }
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3310402004-03-16Henrik Grubbström (Grubba)  INC_RESOLV_MSG_DEPTH(); if(objectp (o = low_cast_to_object(fullname+".pmod", "/.", handler))) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("findmodule(%O) => got object %O\n", fullname, o);
ca2b071998-03-28Henrik Grubbström (Grubba)  return fc[fullname]=o;
3310402004-03-16Henrik Grubbström (Grubba)  }
ca2b071998-03-28Henrik Grubbström (Grubba)  #if constant(load_module)
acdeca2004-04-29Henrik Grubbström (Grubba)  if(objectp (o = low_cast_to_object(fullname+".so", "/.", handler))) {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("findmodule(%O) => got .so object %O\n", fullname, o);
acdeca2004-04-29Henrik Grubbström (Grubba)  return fc[fullname] = o;
3310402004-03-16Henrik Grubbström (Grubba)  }
ca2b071998-03-28Henrik Grubbström (Grubba) #endif
3310402004-03-16Henrik Grubbström (Grubba)  if (programp (o = cast_to_program(fullname, "/.", handler))) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("findmodule(%O) => got .pike program %O\n", fullname, o); return fc[fullname] = o; } DEC_RESOLV_MSG_DEPTH(); resolv_debug ("findmodule(%O) => not found\n", fullname);
401fa02002-12-02Martin Stjernholm  return fc[fullname] = 0;
ca2b071998-03-28Henrik Grubbström (Grubba) }
10e16f1999-11-04Henrik Grubbström (Grubba) mixed handle_import(string what, string|void current_file, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) {
afa1292000-02-19Martin Nilsson  array(string) tmp; string path;
ca2b071998-03-28Henrik Grubbström (Grubba)  if(current_file) {
471ed91998-04-24Fredrik Hübinette (Hubbe)  tmp=EXPLODE_PATH(current_file);
cc2c071998-04-19Fredrik Hübinette (Hubbe)  tmp[-1]=what;
ca2b071998-03-28Henrik Grubbström (Grubba)  path=combine_path_with_cwd( tmp*"/");
57b1af2001-12-13Henrik Grubbström (Grubba)  } else { path = combine_path_with_cwd(what);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3310402004-03-16Henrik Grubbström (Grubba)  #if 0 // If we can't cache the dirnode when we got a handler, then // findmodule has to be broken too. Good caching is necessary for // module dumping. /mast if (handler) { resolv_debug ("handle_import(%O, %O, %O) => new dirnode with handler\n", what, current_file, handler); return dirnode(path, handler); } #endif if(objectp (fc[path])) { resolv_debug ("handle_import(%O, %O) => found %O (cached)\n", what, current_file, fc[path]); return fc[path]; } resolv_debug ("handle_import(%O, %O) => new dirnode\n", what, current_file);
6d93862003-08-18Tomas Nilsson #ifdef PIKE_MODULE_RELOC
3310402004-03-16Henrik Grubbström (Grubba)  // If we have PIKE_MODULE_RELOC enabled, // we might need to map to a join node. // FIXME: Ought to use the non-relocate_module() fakeroot(). if(path == "/${PIKE_MODULE_PATH}" || has_prefix(path, "/${PIKE_MODULE_PATH}/")) {
6d93862003-08-18Tomas Nilsson  string tmp = path[21..];
3310402004-03-16Henrik Grubbström (Grubba)  array(dirnode) dirnodes = ({}); foreach(pike_module_path, string prefix) { string s2 = fakeroot(sizeof(tmp)? combine_path(prefix, tmp) : prefix); if(master_file_stat(s2)) dirnodes += ({ dirnode(s2, handler) });
6d93862003-08-18Tomas Nilsson  }
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("handle_import(%O, %O) => Found %d dirnodes\n", what, current_file, sizeof(dirnodes)); if (sizeof(dirnodes) > 1) return fc[path] = joinnode(dirnodes); if (sizeof(dirnodes)) return fc[path] = dirnodes[0];
6d93862003-08-18Tomas Nilsson  return UNDEFINED; }
3310402004-03-16Henrik Grubbström (Grubba) #endif /* PIKE_MODULE_RELOC */ return fc[path] = dirnode(fakeroot(path), handler);
cc2c071998-04-19Fredrik Hübinette (Hubbe) }
a20af62000-09-26Fredrik Hübinette (Hubbe)  multiset no_resolv = (<>);
b07e962001-07-27Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe) class CompatResolver
cc2c071998-04-19Fredrik Hübinette (Hubbe) {
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(string) pike_include_path=({}); array(string) pike_module_path=({}); array(string) pike_program_path=({});
374c902001-12-20Martin Stjernholm  mapping(string:string) predefines = ([]);
a20af62000-09-26Fredrik Hübinette (Hubbe)  string ver;
3310402004-03-16Henrik Grubbström (Grubba)  //! If we fail to resolv, try our parent. //! //! Typical configuration: //! @pre{0.6->7.0->7.2->7.4->master@} CompatResolver parent_resolver;
4619bd2002-07-29Martin Nilsson  //! The CompatResolver is initialized with a value that can be
6df5a52001-11-07Martin Nilsson  //! casted into a "%d.%d" string, e.g. a version object. void create(mixed version)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
6df5a52001-11-07Martin Nilsson  ver=(string)version;
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Add a directory to search for include files. //! //! This is the same as the command line option @tt{-I@}. //! //! @note //! Note that the added directory will only be searched when using //! < > to quote the included file. //! //! @seealso //! @[remove_include_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void add_include_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_include_path-=({tmp}); pike_include_path=({tmp})+pike_include_path; }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Remove a directory to search for include files. //! //! This function performs the reverse operation of @[add_include_path()]. //! //! @seealso //! @[add_include_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void remove_include_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_include_path-=({tmp}); }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Add a directory to search for modules. //! //! This is the same as the command line option @tt{-M@}. //! //! @seealso //! @[remove_module_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void add_module_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_module_path-=({tmp}); pike_module_path=({tmp})+pike_module_path; }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Remove a directory to search for modules. //! //! This function performs the reverse operation of @[add_module_path()]. //! //! @seealso //! @[add_module_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void remove_module_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_module_path-=({tmp}); }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Add a directory to search for programs. //! //! This is the same as the command line option @tt{-P@}. //! //! @seealso //! @[remove_program_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void add_program_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_program_path-=({tmp}); pike_program_path=({tmp})+pike_program_path; }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Remove a directory to search for programs. //! //! This function performs the reverse operation of @[add_program_path()]. //! //! @seealso //! @[add_program_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void remove_program_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_program_path-=({tmp}); }
374c902001-12-20Martin Stjernholm  //! Add a define (without arguments) which will be implicitly //! defined in @[cpp] calls. void add_predefine (string name, string value) { predefines[name] = value; } //! Remove a define from the set that are implicitly defined in //! @[cpp] calls. void remove_predefine (string name) { m_delete (predefines, name); }
f324812003-01-09Henrik Grubbström (Grubba)  //! Returns a mapping with the current predefines.
374c902001-12-20Martin Stjernholm  mapping get_predefines() { return predefines; }
6df5a52001-11-07Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  mapping get_default_module() { /* This is an ugly kluge to avoid an infinite recursion. * The infinite recursion occurs because this function is * called for every file when the compat_major/minor is set. * This kluge could cause problems with threads if the * compiler was threaded. -Hubbe */ int saved_compat_minor=compat_minor; int saved_compat_major=compat_major; compat_minor=-1; compat_major=-1; mixed x; mixed err =catch {
acd1632001-12-17Henrik Grubbström (Grubba)  if(resolv("__default") && (x=resolv("__default.all_constants"))) x=x();
a20af62000-09-26Fredrik Hübinette (Hubbe)  }; compat_major=saved_compat_major; compat_minor=saved_compat_minor; if(err) throw(err); return x; }
6df5a52001-11-07Martin Nilsson  //!
34241f2001-05-24Henrik Grubbström (Grubba)  mixed resolv_base(string identifier, string|void current_file, object|void current_handler)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) // werror("Resolv_base(%O)\n",identifier);
3310402004-03-16Henrik Grubbström (Grubba)  mapping(string:mixed)|object static_modules = _static_modules; if (current_handler && current_handler->get_default_module) { mapping(string:mixed) default_module = current_handler->get_default_module(); if (default_module) { static_modules = (default_module->_static_modules || ([])); } }
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(mixed) tmp = ({});
3310402004-03-16Henrik Grubbström (Grubba)  #if 0 if (static_modules[identifier]) { tmp += ({ static_modules[identifier] }); } #endif if (identifier == "_static_modules") { tmp += ({ static_modules }); }
b8cd052000-09-26Fredrik Hübinette (Hubbe)  string dir=current_file ? dirname(current_file) : "/";
34241f2001-05-24Henrik Grubbström (Grubba) 
a20af62000-09-26Fredrik Hübinette (Hubbe)  foreach(pike_module_path, string path) {
34241f2001-05-24Henrik Grubbström (Grubba)  string file=combine_path(dir, path, identifier);
401fa02002-12-02Martin Stjernholm  mixed ret = findmodule(file, current_handler);
3310402004-03-16Henrik Grubbström (Grubba)  if(objectp (ret) || programp(ret)) { // ret is either zero or an object or a program here.
401fa02002-12-02Martin Stjernholm  if (ret->is_resolv_dirnode || ret->is_resolv_joinnode) {
a20af62000-09-26Fredrik Hübinette (Hubbe)  if (mixed new_ret = ret->_module_value) { ret = new_ret; }
3310402004-03-16Henrik Grubbström (Grubba)  if (ret->is_resolv_joinnode) tmp += ret->joined_modules; else tmp += ({ ret });
a20af62000-09-26Fredrik Hübinette (Hubbe)  } else { if (mixed new_ret = ret->_module_value) { ret = new_ret; } if (!sizeof(tmp)) { return ret; } else { // Ignore
3310402004-03-16Henrik Grubbström (Grubba) #if 1
2f520e2002-04-28Martin Nilsson  werror( "Ignoring file %O: %t for identifier %O\n", file, ret, identifier );
3310402004-03-16Henrik Grubbström (Grubba) #endif
a20af62000-09-26Fredrik Hübinette (Hubbe)  continue; } } }
8b307e1998-10-17Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  if (sizeof(tmp)) { if (sizeof(tmp) == 1) {
3310402004-03-16Henrik Grubbström (Grubba)  return tmp[0];
8b307e1998-10-17Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  return joinnode(tmp);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3310402004-03-16Henrik Grubbström (Grubba)  if (parent_resolver) { return parent_resolver->resolv(identifier, current_file, current_handler); }
a20af62000-09-26Fredrik Hübinette (Hubbe)  return UNDEFINED;
94257b1998-10-16Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  mapping resolv_cache = set_weak_flag( ([]), 1 );
6df5a52001-11-07Martin Nilsson  //!
34241f2001-05-24Henrik Grubbström (Grubba)  mixed resolv(string identifier, string|void current_file, object|void current_handler)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("resolv(%O, %O)\n",identifier, current_file); INC_RESOLV_MSG_DEPTH();
eaa4da2001-10-04Fredrik Hübinette (Hubbe) 
34241f2001-05-24Henrik Grubbström (Grubba)  // FIXME: Support having the cache in the handler?
3c6f582002-05-24Martin Stjernholm  if( no_resolv[ identifier ] ) {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug("resolv(%O, %O) => excluded\n",identifier, current_file);
a20af62000-09-26Fredrik Hübinette (Hubbe)  return UNDEFINED;
3c6f582002-05-24Martin Stjernholm  }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
26ece22001-06-18Henrik Grubbström (Grubba)  if (current_file && !stringp(current_file)) {
4619bd2002-07-29Martin Nilsson  error( "resolv(%O, %O, %O): current_file is not a string!\n", identifier, current_file, current_handler, backtrace() );
26ece22001-06-18Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  mixed ret; string id=identifier+":"+(current_file ? dirname(current_file) : "-");
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  if( zero_type (ret = resolv_cache[id]) != 1 ) {
3c6f582002-05-24Martin Stjernholm #ifdef RESOLV_DEBUG
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH();
3c6f582002-05-24Martin Stjernholm  if (ret == ZERO_TYPE)
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("resolv(%O, %O) => not found (cached)\n",identifier, current_file);
3c6f582002-05-24Martin Stjernholm  else
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("resolv(%O, %O) => found %O (cached)\n",identifier, current_file, ret);
3c6f582002-05-24Martin Stjernholm #endif /* RESOLV_DEBUG */
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  return ret == ZERO_TYPE ? UNDEFINED : ret; }
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(string) tmp=identifier/".";
34241f2001-05-24Henrik Grubbström (Grubba)  ret=resolv_base(tmp[0], current_file, current_handler);
179eb12002-11-29Henrik Grubbström (Grubba)  foreach(tmp[1..],string index) { if (zero_type(ret)) break; ret=ret[index]; }
a20af62000-09-26Fredrik Hübinette (Hubbe)  resolv_cache[id] = zero_type (ret) ? ZERO_TYPE : ret;
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH();
3c6f582002-05-24Martin Stjernholm #ifdef RESOLV_DEBUG if (zero_type (ret))
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("resolv(%O, %O) => not found\n",identifier, current_file);
3c6f582002-05-24Martin Stjernholm  else
3310402004-03-16Henrik Grubbström (Grubba)  resolv_debug("resolv(%O, %O) => found %O\n",identifier, current_file, ret);
3c6f582002-05-24Martin Stjernholm #endif /* RESOLV_DEBUG */
a20af62000-09-26Fredrik Hübinette (Hubbe)  return ret; }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever an #include directive is encountered //! it receives the argument for #include and should return the file name //! of the file to include
a20af62000-09-26Fredrik Hübinette (Hubbe)  string handle_include(string f, string current_file, int local_include) { if(local_include) {
3310402004-03-16Henrik Grubbström (Grubba)  array(string) tmp=EXPLODE_PATH(current_file);
a20af62000-09-26Fredrik Hübinette (Hubbe)  tmp[-1]=f;
3310402004-03-16Henrik Grubbström (Grubba)  return combine_path_with_cwd(tmp*"/");
a20af62000-09-26Fredrik Hübinette (Hubbe)  } else {
3310402004-03-16Henrik Grubbström (Grubba)  foreach(pike_include_path, string path) { path=combine_path(path,f); if(master_file_stat(fakeroot(path))) return path; } if (parent_resolver) { return parent_resolver->handle_include(f, current_file, local_include); }
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  // Failed. return 0;
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
6df5a52001-11-07Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  string read_include(string f) { AUTORELOAD_CHECK_FILE(f) return master_read_file(f); }
9852f72002-12-03Martin Nilsson  string _sprintf(int t)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
9852f72002-12-03Martin Nilsson  return t=='O' && sprintf("CompatResolver(%O)",ver);
94257b1998-10-16Henrik Grubbström (Grubba)  }
7c11f41999-10-04Fredrik Hübinette (Hubbe) }
a20af62000-09-26Fredrik Hübinette (Hubbe) inherit CompatResolver;
97e5c72001-07-28Martin Nilsson //! These are useful if you want to start other Pike processes //! with the same options as this one was started with.
bfa62b1998-04-30Henrik Grubbström (Grubba) string _pike_file_name;
ac51841998-04-29Henrik Grubbström (Grubba) string _master_file_name;
b1d7d92002-01-07Martin Nilsson // Gets set to 1 if we're in async-mode (script->main() returned <0) private int(0..1) _async=0;
53a32a2000-06-04Francesco Chemolli 
b1d7d92002-01-07Martin Nilsson //! Returns 1 if we´re in async-mode, e.g. if the main method has //! returned a negative number. int(0..1) asyncp() {
53a32a2000-06-04Francesco Chemolli  return _async; }
45c7262001-04-17Mirar (Pontus Hagland) #if constant(thread_create) // this must be done in __init if someone inherits the master static object _backend_thread=this_thread();
97e5c72001-07-28Martin Nilsson  //! The backend_thread() function is useful to determine if you are //! the backend thread - important when doing async/sync protocols. //! This method is only available if thread_create is present.
45c7262001-04-17Mirar (Pontus Hagland) object backend_thread() { return _backend_thread; } #endif
3310402004-03-16Henrik Grubbström (Grubba) 
97e5c72001-07-28Martin Nilsson //! This function is called when all the driver is done with all setup //! of modules, efuns, tables etc. etc. and is ready to start executing //! _real_ programs. It receives the arguments not meant for the driver //! and an array containing the environment variables on the same form as //! a C program receives them.
afa1292000-02-19Martin Nilsson void _main(array(string) orig_argv, array(string) env)
ca2b071998-03-28Henrik Grubbström (Grubba) {
afa1292000-02-19Martin Nilsson  array(string) argv=copy_value(orig_argv);
3310402004-03-16Henrik Grubbström (Grubba)  int debug,trace,run_tool;
ca2b071998-03-28Henrik Grubbström (Grubba)  object tmp;
c05dc42002-09-05Marcus Comstedt  string postparseaction=0;
ca2b071998-03-28Henrik Grubbström (Grubba) 
ddd2af2001-12-29Martin Nilsson  predefines = _static_modules.Builtin()->_take_over_initial_predefines();
bfa62b1998-04-30Henrik Grubbström (Grubba)  _pike_file_name = orig_argv[0];
45c7262001-04-17Mirar (Pontus Hagland) #if constant(thread_create) _backend_thread = this_thread(); #endif
bfa62b1998-04-30Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  foreach(env, string a) if( sscanf(a, "%s=%s", a, string b)==2 ) { #ifdef __NT__ if(a=="") { sscanf(b, "%s=%s", a, b); a="="+a; } #endif putenv(a, b);
d854161999-04-08Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  else werror("Broken environment var %s\n",a);
d854161999-04-08Fredrik Hübinette (Hubbe) 
da2ddc2002-08-11Martin Nilsson  void _error(string a, mixed ... b) {
4619bd2002-07-29Martin Nilsson  werror(a, @b); exit(1); };
624e851999-06-02Marcus Comstedt 
e90a8b1999-06-01Mirar (Pontus Hagland) #ifndef NOT_INSTALLED
3310402004-03-16Henrik Grubbström (Grubba)  { array parts = (getenv("PIKE_INCLUDE_PATH")||"")/PATH_SEPARATOR-({""}); int i = sizeof(parts); while(i) add_include_path(parts[--i]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  parts = (getenv("PIKE_PROGRAM_PATH")||"")/PATH_SEPARATOR-({""}); i = sizeof(parts); while(i) add_program_path(parts[--i]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  parts = (getenv("PIKE_MODULE_PATH")||"")/PATH_SEPARATOR-({""}); i = sizeof(parts); while(i) add_module_path(parts[--i]); }
e90a8b1999-06-01Mirar (Pontus Hagland) #endif
da2ddc2002-08-11Martin Nilsson  // Some configure scripts depends on this format.
4619bd2002-07-29Martin Nilsson  string format_paths() {
25c0a82002-08-11Per Hedbor  return ("master.pike...: " + (_master_file_name || __FILE__) + "\n" "Module path...: " + pike_module_path*"\n"
020d002002-08-15Per Hedbor  " " + "\n"
25c0a82002-08-11Per Hedbor  "Include path..: " + pike_include_path*"\n"
020d002002-08-15Per Hedbor  " " + "\n"
25c0a82002-08-11Per Hedbor  "Program path..: " + pike_program_path*"\n"
020d002002-08-15Per Hedbor  " " + "\n");
4619bd2002-07-29Martin Nilsson  };
8ab5272003-02-21Henrik Grubbström (Grubba)  mixed main_resolv(string ... syms) { mixed v = resolv(syms[0]); foreach(syms[1..], string sym) if(v) v = v[sym]; if(!v)
7268aa2003-05-19Martin Stjernholm  _error("Could not resolv %s. (Perhaps the installed pike tree has been moved.)\n", syms*".");
8ab5272003-02-21Henrik Grubbström (Grubba)  return v; };
ca2b071998-03-28Henrik Grubbström (Grubba)  if(sizeof(argv)>1 && sizeof(argv[1]) && argv[1][0]=='-') {
3310402004-03-16Henrik Grubbström (Grubba)  array q; tmp = main_resolv( "Getopt" );
4619bd2002-07-29Martin Nilsson 
747f0a2003-05-17Rene van Rüschen  int NO_ARG = tmp->NO_ARG; int MAY_HAVE_ARG = tmp->MAY_HAVE_ARG; int HAS_ARG = tmp->HAS_ARG;
ca2b071998-03-28Henrik Grubbström (Grubba)  q=tmp->find_all_options(argv,({
8c03142002-11-15Martin Nilsson  ({"compat_version", HAS_ARG, ({"-V", "--compat"}), 0, 0}), ({"version", NO_ARG, ({"-v", "--version"}), 0, 0}),
3310402004-03-16Henrik Grubbström (Grubba)  ({"dumpversion", NO_ARG, ({"--dumpversion"}), 0, 0}), ({"help", MAY_HAVE_ARG, ({"-h", "--help"}), 0, 0}),
8c03142002-11-15Martin Nilsson  ({"features", NO_ARG, ({"--features"}), 0, 0}), ({"info", NO_ARG, ({"--info"}), 0, 0}), ({"execute", HAS_ARG, ({"-e", "--execute"}), 0, 0}), ({"debug_without", HAS_ARG, ({"--debug-without"}), 0, 0}), ({"preprocess", HAS_ARG, ({"-E", "--preprocess"}), 0, 0}), ({"modpath", HAS_ARG, ({"-M", "--module-path"}), 0, 0}), ({"ipath", HAS_ARG, ({"-I", "--include-path"}), 0, 0}), ({"ppath", HAS_ARG, ({"-P", "--program-path"}), 0, 0}), ({"showpaths", NO_ARG, ({"--show-paths"}), 0, 0}), ({"warnings", NO_ARG, ({"-w", "--warnings"}), 0, 0}), ({"nowarnings", NO_ARG, ({"-W", "--woff", "--no-warnings"}), 0, 0}), ({"autoreload", NO_ARG, ({"--autoreload"}), 0, 0}), ({"master", HAS_ARG, ({"-m"}), 0, 0}), ({"compiler_trace", NO_ARG, ({"--compiler-trace"}), 0, 0}), ({"assembler_debug",MAY_HAVE_ARG, ({"--assembler-debug"}), 0, 0}), ({"optimizer_debug",MAY_HAVE_ARG, ({"--optimizer-debug"}), 0, 0}), ({"debug", MAY_HAVE_ARG, ({"--debug"}), 0, 1}), ({"trace", MAY_HAVE_ARG, ({"--trace"}), 0, 1}), ({"ignore", MAY_HAVE_ARG, ({"-Dqdatplr"}), 0, 1}), ({"ignore", HAS_ARG, ({"-s"}), 0, 0}), ({"run_tool", NO_ARG, ({"-x"}), 0, 0}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  }), 1);
3310402004-03-16Henrik Grubbström (Grubba) 
ca2b071998-03-28Henrik Grubbström (Grubba)  /* Parse -M and -I backwards */
3310402004-03-16Henrik Grubbström (Grubba)  for(int i=sizeof(q)-1; i>=0; i--)
ca2b071998-03-28Henrik Grubbström (Grubba)  { switch(q[i][0]) {
a20af62000-09-26Fredrik Hübinette (Hubbe)  case "compat_version": sscanf(q[i][1],"%d.%d",compat_major,compat_minor); break;
8c03142002-11-15Martin Nilsson 
bec57d1999-09-06Fredrik Hübinette (Hubbe) #ifdef PIKE_AUTORELOAD case "autoreload": autoreload_on++;
3310402004-03-16Henrik Grubbström (Grubba)  break;
bec57d1999-09-06Fredrik Hübinette (Hubbe) #endif
8c03142002-11-15Martin Nilsson 
3d14462000-09-24Per Hedbor  case "debug_without":
3310402004-03-16Henrik Grubbström (Grubba)  // FIXME: Disable loading of dumped modules?
3d14462000-09-24Per Hedbor  foreach( q[i][1]/",", string feature ) { switch( feature ) { case "ttf": no_resolv[ "_Image_TTF" ] = 1; break; case "zlib": no_resolv[ "Gz" ] = 1; break; case "unisys": no_resolv[ "_Image_GIF" ] = 1; no_resolv[ "_Image_TIFF" ] = 1; break; case "threads": // not really 100% correct, but good enough for most things. no_resolv[ "Thread" ] = 1;
f324812003-01-09Henrik Grubbström (Grubba)  add_constant( "thread_create" );
3d14462000-09-24Per Hedbor  break; default: no_resolv[ feature ] = 1; break; } } break;
8c03142002-11-15Martin Nilsson 
c5cc2d1998-04-29Henrik Grubbström (Grubba)  case "debug": debug+=(int)q[i][1]; break;
862fb41998-04-05Fredrik Hübinette (Hubbe) 
c5cc2d1998-04-29Henrik Grubbström (Grubba) #if constant(_compiler_trace) case "compiler_trace": _compiler_trace(1); break; #endif /* constant(_compiler_trace) */
8f45692001-01-14Henrik Grubbström (Grubba) #if constant(_assembler_debug) case "assembler_debug": _assembler_debug((int)q[i][1]); break; #endif /* constant(_assembler_debug) */
e27a5c1999-11-19Henrik Grubbström (Grubba) #if constant(_optimizer_debug) case "optimizer_debug": _optimizer_debug((int)q[i][1]); break; #endif /* constant(_optimizer_debug) */
c5cc2d1998-04-29Henrik Grubbström (Grubba)  case "trace": trace+=(int)q[i][1]; break;
862fb41998-04-05Fredrik Hübinette (Hubbe) 
ca2b071998-03-28Henrik Grubbström (Grubba)  case "modpath": add_module_path(q[i][1]); break; case "ipath": add_include_path(q[i][1]); break; case "ppath": add_program_path(q[i][1]); break;
c5cc2d1998-04-29Henrik Grubbström (Grubba)  case "warnings":
ca2b071998-03-28Henrik Grubbström (Grubba)  want_warnings++; break;
ac51841998-04-29Henrik Grubbström (Grubba) 
b714e12002-09-05Marcus Comstedt  case "nowarnings":
098c8a2000-03-30Henrik Grubbström (Grubba)  want_warnings--; break;
ac51841998-04-29Henrik Grubbström (Grubba)  case "master": _master_file_name = q[i][1]; break;
66dca02002-09-05Marcus Comstedt  case "run_tool":
c05dc42002-09-05Marcus Comstedt  run_tool = 1;
66dca02002-09-05Marcus Comstedt  break;
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
afa1292000-02-19Martin Nilsson  foreach(q, array opts)
ca2b071998-03-28Henrik Grubbström (Grubba)  { switch(opts[0]) {
3310402004-03-16Henrik Grubbström (Grubba)  case "dumpversion": write("%d.%d.%d\n", __REAL_MAJOR__, __REAL_MINOR__, __REAL_BUILD__); exit(0);
ca2b071998-03-28Henrik Grubbström (Grubba)  case "version":
e078562004-02-20Martin Nilsson  werror(version() + " Copyright © 1994-2004 Linköping University\n"
ca2b071998-03-28Henrik Grubbström (Grubba)  "Pike comes with ABSOLUTELY NO WARRANTY; This is free software and you are\n" "welcome to redistribute it under certain conditions; Read the files\n"
b38f142002-04-30Martin Nilsson  "COPYING and COPYRIGHT in the Pike distribution for more details.\n");
ca2b071998-03-28Henrik Grubbström (Grubba)  exit(0); case "help":
3310402004-03-16Henrik Grubbström (Grubba)  werror( main_resolv("Tools","MasterHelp")->do_help(opts[1]) );
ca2b071998-03-28Henrik Grubbström (Grubba)  exit(0);
94c8552001-01-19Mirar (Pontus Hagland)  case "features":
a6b6df2001-01-19Mirar (Pontus Hagland)  postparseaction="features"; break; case "info": postparseaction="info";
94c8552001-01-19Mirar (Pontus Hagland)  break;
2ea8261998-05-12Martin Stjernholm  case "showpaths":
4619bd2002-07-29Martin Nilsson  werror(format_paths());
2ea8261998-05-12Martin Stjernholm  exit(0);
ca2b071998-03-28Henrik Grubbström (Grubba)  case "execute":
5c8a5e2003-02-26Martin Stjernholm #ifdef __AUTO_BIGNUM__ main_resolv( "Gmp", "bignum" ); #endif /* __AUTO_BIGNUM__ */
040eb02003-03-20Martin Stjernholm 
3310402004-03-16Henrik Grubbström (Grubba)  random_seed((time() ^ (getpid()<<8)));
7d69132003-05-15Henrik Grubbström (Grubba)  argv = tmp->get_args(argv,1);
3310402004-03-16Henrik Grubbström (Grubba)  program prog; if(Version(compat_major,compat_minor) <= Version(7,4)) prog = compile_string( "mixed create(int argc, array(string) argv,array(string) env){"+ opts[1]+";}"); else prog = compile_string( "#define NOT(X) !(X)\n" "#define CHAR(X) 'X'\n" "mixed run(int argc, array(string) argv," "mapping(string:string) env){"+ opts[1]+";}");
040eb02003-03-20Martin Stjernholm  #if constant(_debug) if(debug) _debug(debug); #endif
f1a1142001-02-23Fredrik Hübinette (Hubbe)  if(trace) predef::trace(trace);
3310402004-03-16Henrik Grubbström (Grubba)  mixed ret;
040eb02003-03-20Martin Stjernholm  mixed err = catch { // One reason for this catch is to get a new call to // eval_instruction in interpret.c so that the debug and // trace levels set above take effect in the bytecode // evaluator.
3310402004-03-16Henrik Grubbström (Grubba)  if(currentversion <= Version(7,4)) prog (sizeof(argv),argv,env); else ret = prog()->run(sizeof(argv),argv,getenv());
040eb02003-03-20Martin Stjernholm  }; if(trace) predef::trace(0); if (err) { handle_error (err);
3310402004-03-16Henrik Grubbström (Grubba)  ret = 10;
040eb02003-03-20Martin Stjernholm  }
3310402004-03-16Henrik Grubbström (Grubba)  if(stringp(ret)) { write(ret); if(ret[-1]!='\n') write("\n"); } if(!intp(ret) || ret<0) ret=0; exit(ret);
fcf7281998-04-28Fredrik Hübinette (Hubbe) 
c5cc2d1998-04-29Henrik Grubbström (Grubba)  case "preprocess":
5c8a5e2003-02-26Martin Stjernholm #ifdef __AUTO_BIGNUM__ main_resolv( "Gmp", "bignum" ); #endif /* __AUTO_BIGNUM__ */
3310402004-03-16Henrik Grubbström (Grubba)  write(cpp(master_read_file(opts[1]),opts[1]));
c5cc2d1998-04-29Henrik Grubbström (Grubba)  exit(0);
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
caa3221998-04-15Henrik Grubbström (Grubba)  argv = tmp->get_args(argv,1);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
a6b6df2001-01-19Mirar (Pontus Hagland)  switch (postparseaction)
94c8552001-01-19Mirar (Pontus Hagland)  {
a6b6df2001-01-19Mirar (Pontus Hagland)  case "features":
3310402004-03-16Henrik Grubbström (Grubba)  write( main_resolv( "Tools", "Install", "features" )()*"\n"+"\n" ); exit(0);
a6b6df2001-01-19Mirar (Pontus Hagland)  case "info":
3310402004-03-16Henrik Grubbström (Grubba)  write("Software......Pike\n" "Version......."+version()+"\n" "WWW...........http://pike.ida.liu.se/\n" "\n" "pike binary..."+_pike_file_name+"\n"+ format_paths() + "\n" "Features......"+ main_resolv( "Tools","Install","features" )()*"\n "+ "\n");
a6b6df2001-01-19Mirar (Pontus Hagland)  exit(0);
94c8552001-01-19Mirar (Pontus Hagland)  }
3310402004-03-16Henrik Grubbström (Grubba) #ifdef __AUTO_BIGNUM__ main_resolv( "Gmp", "bignum" ); #endif /* __AUTO_BIGNUM__ */
1808722001-08-31Fredrik Hübinette (Hubbe)  random_seed(time() ^ (getpid()<<8));
9339fc2000-01-09Fredrik Hübinette (Hubbe) 
c05dc42002-09-05Marcus Comstedt  if(sizeof(argv)==1)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  if(run_tool) { werror("Pike -x specificed without tool name.\n" "Available tools:\n"); mapping t = ([]); int i; object ts = main_resolv( "Tools", "Standalone" ); foreach(indices(ts)-indices(ts->module), string s) { object o = ts[s](); if(!o->main) continue; t[s] = o->description || ""; i = max(i, sizeof(s)); } foreach(sort(indices(t)), string s) werror(" %-"+i+"s %s\n", s, t[s]); exit(1); } main_resolv( "Tools", "Hilfe" )->StdinHilfe();
caa3221998-04-15Henrik Grubbström (Grubba)  exit(0);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
4619bd2002-07-29Martin Nilsson  else argv=argv[1..];
ca2b071998-03-28Henrik Grubbström (Grubba) 
586d1c2000-09-04Fredrik Hübinette (Hubbe)  program prog;
34fb3d1998-10-31Henrik Grubbström (Grubba) 
66dca02002-09-05Marcus Comstedt  if(run_tool) { mixed err = catch {
3310402004-03-16Henrik Grubbström (Grubba)  prog=main_resolv( "Tools", "Standalone", argv[0] );
66dca02002-09-05Marcus Comstedt  }; if (err) _error( "Pike: Failed to load tool %s:\n"
c05dc42002-09-05Marcus Comstedt  "%s\n", argv[0],
66dca02002-09-05Marcus Comstedt  stringp(err[0])?err[0]:describe_backtrace(err) );
3310402004-03-16Henrik Grubbström (Grubba)  argv[0] = search(programs, prog) || argv[0];
66dca02002-09-05Marcus Comstedt  } else { argv[0]=combine_path_with_cwd(argv[0]); mixed err = catch { prog=(program)argv[0]; }; if (err) _error( "Pike: Failed to compile script:\n" "%s\n", stringp(err[0])?err[0]:describe_backtrace(err) ); }
34fb3d1998-10-31Henrik Grubbström (Grubba) 
586d1c2000-09-04Fredrik Hübinette (Hubbe)  if(!prog)
4619bd2002-07-29Martin Nilsson  _error("Pike: Couldn't find script to execute\n(%O)\n", argv[0]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
862fb41998-04-05Fredrik Hübinette (Hubbe) #if constant(_debug) if(debug) _debug(debug); #endif
3d5cba2002-07-23Martin Stjernholm  if(trace) predef::trace(trace);
3310402004-03-16Henrik Grubbström (Grubba)  mixed ret;
040eb02003-03-20Martin Stjernholm  mixed err = catch { // The main reason for this catch is actually to get a new call // to eval_instruction in interpret.c so that the debug and // trace levels set above take effect in the bytecode evaluator.
3310402004-03-16Henrik Grubbström (Grubba)  object script; if(Version(compat_major,compat_minor) <= Version(7,4)) { script=prog(); } else { script=prog(argv); }
040eb02003-03-20Martin Stjernholm  if(!script->main) _error("Error: %s has no main().\n", argv[0]);
3310402004-03-16Henrik Grubbström (Grubba)  ret=script->main(sizeof(argv),argv,env);
040eb02003-03-20Martin Stjernholm  }; if(trace) predef::trace(0); if (err) { handle_error (err);
3310402004-03-16Henrik Grubbström (Grubba)  ret = 10; } if(!intp(ret)) { werror("Error: Non-integer value %O returned from main.\n", ret); exit(10);
040eb02003-03-20Martin Stjernholm  }
3310402004-03-16Henrik Grubbström (Grubba)  if(ret >=0) exit([int]ret);
53a32a2000-06-04Francesco Chemolli  _async=1;
87f9082001-03-12Fredrik Hübinette (Hubbe)  while(1) { mixed err=catch { while(1) _static_modules.Builtin.__backend(3600.0); }; master()->handle_error(err); }
ca2b071998-03-28Henrik Grubbström (Grubba) }
01d3c91999-11-04Henrik Grubbström (Grubba) #if constant(thread_local) object inhibit_compile_errors = thread_local(); void set_inhibit_compile_errors(mixed f) { inhibit_compile_errors->set(f); }
71d9951999-11-29Henrik Grubbström (Grubba)  mixed get_inhibit_compile_errors() {
c2eb4a1999-11-29David Hedbor  return inhibit_compile_errors->get();
71d9951999-11-29Henrik Grubbström (Grubba) }
01d3c91999-11-04Henrik Grubbström (Grubba) #else /* !constant(thread_local) */
ca2b071998-03-28Henrik Grubbström (Grubba) mixed inhibit_compile_errors; void set_inhibit_compile_errors(mixed f) { inhibit_compile_errors=f; }
71d9951999-11-29Henrik Grubbström (Grubba)  mixed get_inhibit_compile_errors() {
c2eb4a1999-11-29David Hedbor  return inhibit_compile_errors;
71d9951999-11-29Henrik Grubbström (Grubba) }
01d3c91999-11-04Henrik Grubbström (Grubba) #endif /* constant(thread_local) */
ca2b071998-03-28Henrik Grubbström (Grubba) 
0024f92001-06-06Mirar (Pontus Hagland) static private function(string:string) _trim_file_name_cb=0;
ca2b071998-03-28Henrik Grubbström (Grubba) string trim_file_name(string s) {
4839dd2001-09-02Marcus Comstedt #ifdef PIKE_MODULE_RELOC s = relocate_module(s); #endif
e37a3e1999-10-09Fredrik Hübinette (Hubbe)  if(getenv("LONG_PIKE_ERRORS")) return s;
471ed91998-04-24Fredrik Hübinette (Hubbe)  if(getenv("SHORT_PIKE_ERRORS")) return BASENAME(s);
0024f92001-06-06Mirar (Pontus Hagland)  if (_trim_file_name_cb) return _trim_file_name_cb(s);
df2c632000-08-02Henrik Grubbström (Grubba)  /* getcwd() can fail, but since this is called from handle_error(), * we don't want to fail, so we don't care about that. */ catch { string cwd=getcwd(); if (sizeof(cwd) && (cwd[-1] != '/')) { cwd += "/"; }
3310402004-03-16Henrik Grubbström (Grubba)  if(has_prefix (s, cwd)) return s[sizeof(cwd)..];
df2c632000-08-02Henrik Grubbström (Grubba)  };
ca2b071998-03-28Henrik Grubbström (Grubba)  return s; }
0024f92001-06-06Mirar (Pontus Hagland) function(string:string) set_trim_file_name_callback(function(string:string) s) { function(string:string) f=_trim_file_name_cb; _trim_file_name_cb=s; return f; }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever a compiling error occurs. //! Nothing strange about it. //! Note that previous_object cannot be trusted in ths function, because //! the compiler calls this function.
ca2b071998-03-28Henrik Grubbström (Grubba) void compile_error(string file,int line,string err) {
01d3c91999-11-04Henrik Grubbström (Grubba)  mixed val;
3112422000-03-27Per Hedbor  if(! (val = get_inhibit_compile_errors() ))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
2f520e2002-04-28Martin Nilsson  werror( "%s:%s:%s\n",trim_file_name(file), line?(string)line:"-",err );
ca2b071998-03-28Henrik Grubbström (Grubba)  }
01d3c91999-11-04Henrik Grubbström (Grubba)  else if(objectp(val) || programp(val) || functionp(val))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
01d3c91999-11-04Henrik Grubbström (Grubba)  if (objectp(val) && val->compile_error) { val->compile_error(file, line, err); } else {
3112422000-03-27Per Hedbor  val(file, line, err);
01d3c91999-11-04Henrik Grubbström (Grubba)  }
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever a compiling warning occurs. //! Nothing strange about it. //! Note that previous_object cannot be trusted in ths function, because //! the compiler calls this function.
ca2b071998-03-28Henrik Grubbström (Grubba) void compile_warning(string file,int line,string err) {
01d3c91999-11-04Henrik Grubbström (Grubba)  mixed val;
3112422000-03-27Per Hedbor  if(!(val = get_inhibit_compile_errors() ))
ca2b071998-03-28Henrik Grubbström (Grubba)  { if(want_warnings)
2f520e2002-04-28Martin Nilsson  werror( "%s:%s: Warning: %s\n",trim_file_name(file), line?(string)line:"-",err ); } else if (objectp(val) && val->compile_warning) {
3310402004-03-16Henrik Grubbström (Grubba)  ([function(string,int,string:void)]([object]val) ->compile_warning)(file, line, err);
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
97e5c72001-07-28Martin Nilsson 
8594c82001-08-15Martin Stjernholm //! This function is called when an exception is catched during //! compilation. Its message is also reported to @[compile_error] if //! this function returns zero. int compile_exception (array|object trace) {
ad53042001-08-16Martin Stjernholm  if (objectp (trace) &&
3310402004-03-16Henrik Grubbström (Grubba)  ( ([object]trace)->is_cpp_error || ([object]trace)->is_compilation_error))
ad53042001-08-16Martin Stjernholm  // Errors thrown directly by cpp() and compile() are normally not // interesting; they've already been reported to compile_error.
5802de2001-08-16Martin Stjernholm  return 1;
8594c82001-08-15Martin Stjernholm  if (mixed val = get_inhibit_compile_errors()) {
3310402004-03-16Henrik Grubbström (Grubba)  if (objectp(val) && ([object]val)->compile_exception) return ([function(object:int)]([object]val) ->compile_exception)([object]trace);
8594c82001-08-15Martin Stjernholm  } else { handle_error (trace); return 1; } return 0; }
97e5c72001-07-28Martin Nilsson //! Called for every runtime warning. The first argument identifies //! where the warning comes from, the second identifies the specific //! message, and the rest depends on that. See code below for currently //! implemented warnings.
8f23062000-06-09Martin Stjernholm void runtime_warning (string where, string what, mixed... args) { if (want_warnings) switch (where + "." + what) { case "gc.bad_cycle": // args[0] is an array containing the objects in the cycle // which aren't destructed and have destroy() functions.
3c6f582002-05-24Martin Stjernholm #if 0 // Ignore this warning for now since we do not yet have a weak // modifier, so it can't be avoided in a reasonable way.
8f23062000-06-09Martin Stjernholm  werror ("GC warning: Garbing cycle where destroy() will be called " "in arbitrary order:\n%{ %s\n%}",
d3a3322002-11-23Marcus Comstedt  sprintf("%O", args[0][*]));
3c6f582002-05-24Martin Stjernholm #endif
8f23062000-06-09Martin Stjernholm  break; default: werror ("%s warning: %s %O\n", capitalize (where), what, args); } }
ca2b071998-03-28Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba) static object _charset_mod;
97e5c72001-07-28Martin Nilsson  //! This function is called by cpp() when it wants to do //! character code conversion.
f4f08d1999-02-26Henrik Grubbström (Grubba) string decode_charset(string data, string charset) {
db25e61999-02-26Henrik Grubbström (Grubba)  // werror(sprintf("decode_charset(%O, %O)\n", data, charset));
f4f08d1999-02-26Henrik Grubbström (Grubba)  if (!_charset_mod) {
3310402004-03-16Henrik Grubbström (Grubba)  object mod = [object]resolv("Locale");
f4f08d1999-02-26Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  _charset_mod = [object](mod && mod["Charset"]);
f4f08d1999-02-26Henrik Grubbström (Grubba)  if (!_charset_mod) { compile_warning("-", 0, "No Locale.Charset module!"); return 0; } } object decoder; catch {
3310402004-03-16Henrik Grubbström (Grubba)  decoder = ([function(string:object)]_charset_mod->decoder)(charset);
f4f08d1999-02-26Henrik Grubbström (Grubba)  }; if (!decoder) { compile_warning("-", 0, sprintf("Unknown charset %O!", charset)); return 0; }
3310402004-03-16Henrik Grubbström (Grubba)  return ([function(void:string)]([function(string:object)]decoder-> feed)(data)->drain)();
f4f08d1999-02-26Henrik Grubbström (Grubba) }
ca2b071998-03-28Henrik Grubbström (Grubba) 
da0a822000-08-29Martin Stjernholm class Describer
ca2b071998-03-28Henrik Grubbström (Grubba) {
da0a822000-08-29Martin Stjernholm  int clipped=0; int canclip=0; mapping(mixed:int|string) ident = ([]); int identcount = 0; void identify_parts (mixed stuff) {
9f71092000-11-20Martin Stjernholm  // Use an array as stack here instead of recursing directly; we // might be pressed for stack space if the backtrace being // described is a stack overflow. array identify_stack = ({stuff}); while (sizeof (identify_stack)) { stuff = identify_stack[-1]; identify_stack = identify_stack[..sizeof (identify_stack) - 2];
26ece22001-06-18Henrik Grubbström (Grubba)  if (objectp (stuff) || functionp (stuff) || programp (stuff)) ident[stuff]++; else if (arrayp (stuff)) {
9f71092000-11-20Martin Stjernholm  if (!ident[stuff]++) identify_stack += stuff; } else if (multisetp (stuff)) { if (!ident[stuff]++)
3310402004-03-16Henrik Grubbström (Grubba)  identify_stack += indices([multiset]stuff);
9f71092000-11-20Martin Stjernholm  } else if (mappingp (stuff)) { if (!ident[stuff]++)
3310402004-03-16Henrik Grubbström (Grubba)  identify_stack += indices([mapping]stuff) + values([mapping]stuff);
9f71092000-11-20Martin Stjernholm  }
26ece22001-06-18Henrik Grubbström (Grubba) 
da0a822000-08-29Martin Stjernholm  } }
2602b82000-08-29Martin Stjernholm  string describe_string (string m, int maxlen) { canclip++; if(sizeof(m) < maxlen) { string t = sprintf("%O", m); if (sizeof(t) < (maxlen + 2)) { return t; } t = 0; } clipped++; if(maxlen>10) { return sprintf("%O+[%d]",m[..maxlen-5],sizeof(m)-(maxlen-5)); }else{ return "string["+sizeof(m)+"]"; } } string describe_array (array m, int maxlen) { if(!sizeof(m)) return "({})"; else { if(maxlen<5) { clipped++; return "array["+sizeof(m)+"]"; } else { canclip++; return "({" + describe_comma_list(m,maxlen-2) +"})"; } } } string describe_mapping (mapping m, int maxlen) { if(!sizeof(m)) return "([])"; else return "mapping["+sizeof(m)+"]"; } string describe_multiset (multiset m, int maxlen) { if(!sizeof(m)) return "(<>)"; else return "multiset["+sizeof(m)+"]"; }
da0a822000-08-29Martin Stjernholm  string describe (mixed m, int maxlen)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  if (stringp (ident[m])) return [string]ident[m];
da0a822000-08-29Martin Stjernholm  else if (intp (ident[m]) && ident[m] > 1) ident[m] = "@" + identcount++;
0a064e2002-11-24Henrik Grubbström (Grubba)  string res; if (catch (res=sprintf("%t",m))) res = "object"; // Object with a broken _sprintf(), probably. switch(res) { case "int":
695d742004-12-08Martin Stjernholm  if (!m && zero_type (m) == 1) return "UNDEFINED";
0a064e2002-11-24Henrik Grubbström (Grubba)  case "float": return (string)m; case "string":
3310402004-03-16Henrik Grubbström (Grubba)  return describe_string ([string]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  case "array":
3310402004-03-16Henrik Grubbström (Grubba)  res = describe_array ([array]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "mapping":
3310402004-03-16Henrik Grubbström (Grubba)  res = describe_mapping ([mapping]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "multiset":
3310402004-03-16Henrik Grubbström (Grubba)  res = describe_multiset ([multiset]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "function":
3310402004-03-16Henrik Grubbström (Grubba)  if (string tmp=describe_function([function]m)) res = tmp;
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "program":
3310402004-03-16Henrik Grubbström (Grubba)  if(string tmp=describe_program([program]m)) res = tmp;
0a064e2002-11-24Henrik Grubbström (Grubba)  break; default: /* object or type. */ if (catch { if(string tmp=sprintf("%O", m)) res = tmp; }) { // Extra paranoia case. res = sprintf("Instance of %O", _typeof(m));
f645f02001-10-26Henrik Grubbström (Grubba)  }
0a064e2002-11-24Henrik Grubbström (Grubba)  break;
da0a822000-08-29Martin Stjernholm  }
0a064e2002-11-24Henrik Grubbström (Grubba)  if (stringp(ident[m]))
da0a822000-08-29Martin Stjernholm  return ident[m] + "=" + res; return res; }
e8ebc01999-08-27Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  string describe_comma_list(array x, int maxlen) { string ret="";
e8ebc01999-08-27Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  if(!sizeof(x)) return ""; if(maxlen<0) return ",,,"+sizeof(x);
5e09161999-10-15Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  int clip=min(maxlen/2,sizeof(x)); int len=maxlen; int done=0;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  while(1) {
da0a822000-08-29Martin Stjernholm  array(string) z=allocate(clip); array(int) isclipped=allocate(clip); array(int) clippable=allocate(clip); for(int e=0;e<clip;e++)
ba20b11999-10-15Fredrik Hübinette (Hubbe)  {
da0a822000-08-29Martin Stjernholm  clipped=0; canclip=0; z[e]=describe(x[e],len); isclipped[e]=clipped; clippable[e]=canclip;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  } while(1) {
da0a822000-08-29Martin Stjernholm  string ret = z[..clip-1]*","; if(done || sizeof(ret)<=maxlen+1) { int tmp=sizeof(x)-clip-1; clipped=`+(0,@isclipped); if(tmp>=0) { clipped++; ret+=",,,"+tmp; } canclip++; return ret; } int last_newlen=len; int newlen; int clipsuggest; while(1) { int smallsize=0; int num_large=0; clipsuggest=0;
ba20b11999-10-15Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  for(int e=0;e<clip;e++)
ba20b11999-10-15Fredrik Hübinette (Hubbe)  {
5e09161999-10-15Fredrik Hübinette (Hubbe)  if((sizeof(z[e])>=last_newlen || isclipped[e]) && clippable[e])
ba20b11999-10-15Fredrik Hübinette (Hubbe)  num_large++; else smallsize+=sizeof(z[e]); if(num_large * 15 + smallsize < maxlen) clipsuggest=e+1; }
da0a822000-08-29Martin Stjernholm  newlen=num_large ? (maxlen-smallsize)/num_large : 0;
ba20b11999-10-15Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  if(newlen<8 || newlen >= last_newlen) break; last_newlen=newlen; }
e8ebc01999-08-27Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  if(newlen < 8 && clip) { clip-= (clip/4) || 1; if(clip > clipsuggest) clip=clipsuggest; }else{ len=newlen; done++; break; }
ba20b11999-10-15Fredrik Hübinette (Hubbe)  } }
da0a822000-08-29Martin Stjernholm  return ret; }
e8ebc01999-08-27Fredrik Hübinette (Hubbe) }
be242d2002-11-23Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba) string program_path_to_name ( string path, void|string module_prefix, void|string module_suffix, void|string object_suffix ) //! Converts a module path on the form @expr{"Foo.pmod/Bar.pmod"@} or //! @expr{"/path/to/pike/lib/modules/Foo.pmod/Bar.pmod"@} to a module //! identifier on the form @expr{"Foo.Bar"@}. //! //! If @[module_prefix] or @[module_suffix] are given, they are //! prepended and appended, respectively, to the returned string if //! it's a module file (i.e. ends with @expr{".pmod"@} or //! @expr{".so"@}). If @[object_suffix] is given, it's appended to the //! returned string if it's an object file (i.e. ends with //! @expr{".pike"@}).
d3a3322002-11-23Marcus Comstedt { array(string) sort_paths_by_length(array(string) paths) { sort(map(paths, sizeof), paths); return reverse(paths); };
3310402004-03-16Henrik Grubbström (Grubba)  if (path == "/master") return "master" + (object_suffix || ""); foreach(sort_paths_by_length(map(pike_module_path - ({""}), lambda(string s) { if (s[-1] == '/') return s; return s+"/"; })), string prefix) { if (has_prefix(path, prefix)) { path = path[sizeof(prefix)..]; break;
d3a3322002-11-23Marcus Comstedt  } }
3310402004-03-16Henrik Grubbström (Grubba)  #if 0 // This seems broken. Why should the current directory or the // setting of SHORT_PIKE_ERRORS etc affect the module identifiers? // /mast path = trim_file_name(path); #endif string modname = replace(path, ".pmod/", "."); if(search(modname, "/")<0) path=modname; if (has_suffix(path, ".module.pmod")) { return (module_prefix || "") + path[..sizeof(path)-13] + (module_suffix || ""); } if (has_suffix(path, ".pmod")) { return (module_prefix || "") + path[..sizeof(path)-6] + (module_suffix || ""); } if (has_suffix(path, ".so")) { return (module_prefix || "") + path[..sizeof(path)-4] + (module_suffix || ""); } if (has_suffix(path, ".pike")) { return path[..sizeof(path)-6] + (object_suffix || ""); } return path + (object_suffix || "");
d3a3322002-11-23Marcus Comstedt }
be242d2002-11-23Henrik Grubbström (Grubba) //! Describe the path to the module @[mod]. //! //! @param mod //! If @[mod] is a program, attempt to describe the path //! to a clone of @[mod]. //! //! @param ret_obj //! If an instance of @[mod] is found, it will be returned
3310402004-03-16Henrik Grubbström (Grubba) //! by changing element @expr{0@} of @[ret_obj].
be242d2002-11-23Henrik Grubbström (Grubba) //! //! @returns //! The a description of the path. //! //! @note //! The returned description will end with a proper indexing method
3310402004-03-16Henrik Grubbström (Grubba) //! currently either @expr{"."@} or @expr{"->"@}.
be242d2002-11-23Henrik Grubbström (Grubba) string describe_module(object|program mod, array(object)|void ret_obj) {
fceb272003-09-09Martin Stjernholm  // Note: mod might be a bignum object; objectp won't work right for // our purposes. object_program returns zero for non-objects, so we // use it instead. program parent_fun = object_program(mod); if (parent_fun) {
be242d2002-11-23Henrik Grubbström (Grubba)  if (ret_obj) ret_obj[0] = mod;
fceb272003-09-09Martin Stjernholm  } else if (programp (mod)) {
be242d2002-11-23Henrik Grubbström (Grubba)  parent_fun = mod;
401fa02002-12-02Martin Stjernholm  if (objectp (mod = objects[parent_fun]) && ret_obj) ret_obj[0] = mod;
be242d2002-11-23Henrik Grubbström (Grubba)  }
fceb272003-09-09Martin Stjernholm  else return ""; // efun
be242d2002-11-23Henrik Grubbström (Grubba)  if (mod) { catch { string res = sprintf("%O", mod);
d3a3322002-11-23Marcus Comstedt  if (res != "object" && res != "")
401fa02002-12-02Martin Stjernholm  return (objectp (objects[parent_fun]) && programs["/master"] != parent_fun?
a632422002-11-23Marcus Comstedt  res+".":res+"->");
be242d2002-11-23Henrik Grubbström (Grubba)  }; } if (!object_program(parent_fun)) { // We might be a top-level entity.
3310402004-03-16Henrik Grubbström (Grubba)  if (string path = programs_reverse_lookup (parent_fun)) return program_path_to_name(path, "", ".", "()->");
be242d2002-11-23Henrik Grubbström (Grubba)  } // Begin by describing our parent. array(object) parent_obj = ({ 0 }); string res = describe_module(function_object(parent_fun)||
f324812003-01-09Henrik Grubbström (Grubba)  function_program(parent_fun)||
be242d2002-11-23Henrik Grubbström (Grubba)  object_program(parent_fun), parent_obj); // werror("So far: %O parent_obj:%O\n", res, parent_obj);
401fa02002-12-02Martin Stjernholm  object|program parent =
fceb272003-09-09Martin Stjernholm  object_program (parent_obj[0]) ? parent_obj[0] : object_program(parent_fun); if (mod && (object_program (parent) || parent)) {
be242d2002-11-23Henrik Grubbström (Grubba)  // Object identified. catch { // Check if we're an object in parent. int i = search(values(parent), mod); if (i >= 0) {
3310402004-03-16Henrik Grubbström (Grubba)  return res + [string]indices(parent)[i] + ".";
be242d2002-11-23Henrik Grubbström (Grubba)  } }; } // We're cloned from something in parent. if (string fun_name = function_name(parent_fun)) { return res + fun_name + "()->"; } // No such luck. // Try identifying a clone of ourselves.
fceb272003-09-09Martin Stjernholm  if (!mod && (object_program (parent) || parent)) {
be242d2002-11-23Henrik Grubbström (Grubba)  catch { // Check if there's a clone of parent_fun in parent_obj. int i; array(mixed) val = values(parent);
3310402004-03-16Henrik Grubbström (Grubba)  array(string) ind = [array(string)]indices(parent);
be242d2002-11-23Henrik Grubbström (Grubba)  for (i=0; i < sizeof(val); i++) {
fceb272003-09-09Martin Stjernholm  if (object_program(val[i]) && object_program(val[i]) == parent_fun) {
be242d2002-11-23Henrik Grubbström (Grubba)  return res + ind[i] + "."; } } }; } // We're really out of luck here...
d3a3322002-11-23Marcus Comstedt  return res + (describe_program(parent_fun)||"unknown_program") + "()->";
be242d2002-11-23Henrik Grubbström (Grubba) }
97e5c72001-07-28Martin Nilsson //!
3310402004-03-16Henrik Grubbström (Grubba) string describe_object(object o)
bec57d1999-09-06Fredrik Hübinette (Hubbe) { string s;
38f4122003-12-18Martin Stjernholm  if(zero_type (o)) return 0; // Destructed.
be242d2002-11-23Henrik Grubbström (Grubba) 
3310402004-03-16Henrik Grubbström (Grubba)  if (o == _static_modules) return "_static_modules"; program|function(mixed...:void|object) parent_fun = object_program(o);
be242d2002-11-23Henrik Grubbström (Grubba)  /* Constant object? */ catch {
f324812003-01-09Henrik Grubbström (Grubba)  object|program parent_obj = (function_object(parent_fun) || function_program(parent_fun));
be242d2002-11-23Henrik Grubbström (Grubba) 
10ab772002-12-04Marcus Comstedt  if (objectp (parent_obj) || parent_obj) {
be242d2002-11-23Henrik Grubbström (Grubba)  /* Check if we have a constant object. */ object tmp = objects[parent_obj];
401fa02002-12-02Martin Stjernholm  if (objectp (tmp)) parent_obj = tmp;
be242d2002-11-23Henrik Grubbström (Grubba)  /* Try finding ourselves in parent_obj. */
3310402004-03-16Henrik Grubbström (Grubba)  int i = search(values(parent_obj), o);
be242d2002-11-23Henrik Grubbström (Grubba)  if (i >= 0) {
3310402004-03-16Henrik Grubbström (Grubba)  s = [string]indices(parent_obj)[i];
d3a3322002-11-23Marcus Comstedt  return describe_module(parent_obj) + s;
be242d2002-11-23Henrik Grubbström (Grubba)  } } };
401fa02002-12-02Martin Stjernholm  if(objectp (objects[parent_fun]))
3310402004-03-16Henrik Grubbström (Grubba)  if ((s = programs_reverse_lookup (parent_fun)) && (s=program_path_to_name(s, "", "", "()")))
a632422002-11-23Marcus Comstedt  return s;
be242d2002-11-23Henrik Grubbström (Grubba)  /* Try identifying the program. */ if(( s=describe_program(parent_fun) ))
a632422002-11-23Marcus Comstedt  return s+"()";
be242d2002-11-23Henrik Grubbström (Grubba) 
bec57d1999-09-06Fredrik Hübinette (Hubbe)  return 0; }
97e5c72001-07-28Martin Nilsson //!
3310402004-03-16Henrik Grubbström (Grubba) string describe_program(program|function p)
62e87b1998-04-14Fredrik Hübinette (Hubbe) { string s; if(!p) return 0;
d3a3322002-11-23Marcus Comstedt 
3310402004-03-16Henrik Grubbström (Grubba)  if (p == object_program (_static_modules)) return "object_program(_static_modules)"; if(programp(p) && (s = programs_reverse_lookup ([program] p)) && (s=program_path_to_name(s, "object_program(", ")", "")))
d3a3322002-11-23Marcus Comstedt  return s;
62e87b1998-04-14Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba)  if(object|program tmp=(function_object(p) || function_program(p))) {
10ab772002-12-04Marcus Comstedt  if(s = function_name(p))
3310402004-03-16Henrik Grubbström (Grubba)  { return describe_module(tmp) + s; } }
e8ce5e2001-04-09Fredrik Hübinette (Hubbe) 
bec57d1999-09-06Fredrik Hübinette (Hubbe)  if(s=_static_modules.Builtin()->program_defined(p)) return EXPLODE_PATH(s)[-1];
62e87b1998-04-14Fredrik Hübinette (Hubbe)  return 0; }
97e5c72001-07-28Martin Nilsson //!
2602b82000-08-29Martin Stjernholm string describe_function (function f) { if (!f) return 0; string name;
3310402004-03-16Henrik Grubbström (Grubba)  if(string s = programs_reverse_lookup (f))
2602b82000-08-29Martin Stjernholm  {
01f0272001-11-19Martin Nilsson  if(has_suffix(s, ".pmod")) name = EXPLODE_PATH(s[..sizeof(s)-6])[-1];
2602b82000-08-29Martin Stjernholm  else name = trim_file_name(s); } else if (catch (name = function_name (f))) name = "function";
3310402004-03-16Henrik Grubbström (Grubba)  object o = function_object([function(mixed...:void|mixed)]f);
fceb272003-09-09Martin Stjernholm  if(object_program (o)) { // Check if it's an object in a way that // (hopefully) doesn't call any functions // in it (neither `== nor `!).
2602b82000-08-29Martin Stjernholm  string s; if (!catch (s = sprintf("%O",o)) && s != "object") return s+"->"+name; } return name; }
ca2b071998-03-28Henrik Grubbström (Grubba) /* It is possible that this should be a real efun, * it is currently used by handle_error to convert a backtrace to a * readable message. */
8f45692001-01-14Henrik Grubbström (Grubba) 
b07e962001-07-27Martin Nilsson //! @appears describe_backtrace
8f45692001-01-14Henrik Grubbström (Grubba) //! Returns a string containing a readable message that describes where //! the backtrace was made. //! //! The argument @[trace] should normally be the return value from a call //! to @[backtrace()], or a caught error. //! //! @seealso //! @[backtrace()], @[describe_error()], @[catch()], @[throw()] //!
ba20b11999-10-15Fredrik Hübinette (Hubbe) string describe_backtrace(mixed trace, void|int linewidth)
ca2b071998-03-28Henrik Grubbström (Grubba) { int e;
5be4aa2001-11-12Martin Stjernholm  string ret = "";
d4fd0a1999-12-06Henrik Grubbström (Grubba)  int backtrace_len=((int)getenv("PIKE_BACKTRACE_LEN")) || bt_max_string_len;
ca2b071998-03-28Henrik Grubbström (Grubba) 
ba20b11999-10-15Fredrik Hübinette (Hubbe)  if(!linewidth) { linewidth=99999; catch {
3310402004-03-16Henrik Grubbström (Grubba)  linewidth=[int]_static_modules.files()->_stdin->tcgetattr()->columns;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  };
fe8fb32000-01-15Fredrik Hübinette (Hubbe)  if(linewidth<10) linewidth=99999;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  if((arrayp(trace) && sizeof([array]trace)==2 && stringp(([array]trace)[0]))|| (objectp(trace) && ([object]trace)->is_generic_error))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
a232721999-10-06Henrik Grubbström (Grubba)  if (catch {
3310402004-03-16Henrik Grubbström (Grubba)  ret = ([array(string)]trace)[0]; if(!stringp(ret)) ret = "No error message!\n"; trace = ([array]trace)[1];
a232721999-10-06Henrik Grubbström (Grubba)  }) { return "Error indexing backtrace!\n"; }
5be4aa2001-11-12Martin Stjernholm  if(!arrayp(trace)) return ret + "No backtrace.\n"; }else if (!arrayp (trace)) return sprintf ("Unrecognized backtrace format: %O\n", trace);
ca2b071998-03-28Henrik Grubbström (Grubba)  {
da0a822000-08-29Martin Stjernholm  Describer desc = Describer(); desc->identify_parts (trace);
3310402004-03-16Henrik Grubbström (Grubba)  array trace = [array]trace;
da0a822000-08-29Martin Stjernholm 
9f71092000-11-20Martin Stjernholm  int end = 0; if( (sizeof(trace)>1) && arrayp(trace[0]) &&
3310402004-03-16Henrik Grubbström (Grubba)  (sizeof([array]trace[0]) > 2) && (([array]trace[0])[2] == _main))
9f71092000-11-20Martin Stjernholm  end = 1; mapping(string:int) prev_pos = ([]); array(string) frames = ({}); int loop_start = 0, loop_next, loops; for(e = sizeof(trace)-1; e>=end; e--)
ca2b071998-03-28Henrik Grubbström (Grubba)  { mixed tmp; string row;
3310402004-03-16Henrik Grubbström (Grubba)  if (array err=[array]catch {
a232721999-10-06Henrik Grubbström (Grubba)  tmp = trace[e]; if(stringp(tmp))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  row=[string]tmp;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
a232721999-10-06Henrik Grubbström (Grubba)  else if(arrayp(tmp))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3310402004-03-16Henrik Grubbström (Grubba)  array tmp = [array]tmp;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  string pos;
dbe79a1999-12-07Henrik Grubbström (Grubba)  if(sizeof(tmp)>=2 && stringp(tmp[0])) { if (intp(tmp[1])) {
3310402004-03-16Henrik Grubbström (Grubba)  pos=trim_file_name([string]tmp[0])+":"+(string)tmp[1];
dbe79a1999-12-07Henrik Grubbström (Grubba)  } else {
3310402004-03-16Henrik Grubbström (Grubba)  pos = sprintf("%s:Bad line %t", trim_file_name([string]tmp[0]), tmp[1]);
dbe79a1999-12-07Henrik Grubbström (Grubba)  }
a232721999-10-06Henrik Grubbström (Grubba)  }else{
3310402004-03-16Henrik Grubbström (Grubba)  string desc="Unknown program";
a232721999-10-06Henrik Grubbström (Grubba)  if(sizeof(tmp)>=3 && functionp(tmp[2])) {
e118831999-12-22Per Hedbor  catch {
3310402004-03-16Henrik Grubbström (Grubba)  if(mixed tmp=function_object([function(mixed...: void|mixed)]tmp[2]))
a232721999-10-06Henrik Grubbström (Grubba)  if(tmp=object_program(tmp))
3310402004-03-16Henrik Grubbström (Grubba)  if(tmp=describe_program([program]tmp)) desc=[string]tmp;
a232721999-10-06Henrik Grubbström (Grubba)  }; }
ba20b11999-10-15Fredrik Hübinette (Hubbe)  pos=desc; }
9f71092000-11-20Martin Stjernholm 
ba20b11999-10-15Fredrik Hübinette (Hubbe)  string data; if(sizeof(tmp)>=3) {
dfca1f2000-08-22Martin Stjernholm  if(functionp(tmp[2])) {
3310402004-03-16Henrik Grubbström (Grubba)  data = describe_function ([function]tmp[2]);
dfca1f2000-08-22Martin Stjernholm  }
ba20b11999-10-15Fredrik Hübinette (Hubbe)  else if (stringp(tmp[2])) {
3310402004-03-16Henrik Grubbström (Grubba)  data = [string]tmp[2];
ba20b11999-10-15Fredrik Hübinette (Hubbe)  } else data ="unknown function"; data+="("+
da0a822000-08-29Martin Stjernholm  desc->describe_comma_list(tmp[3..], backtrace_len)+
ba20b11999-10-15Fredrik Hübinette (Hubbe)  ")"; if(sizeof(pos)+sizeof(data) < linewidth-4) { row=sprintf("%s: %s",pos,data); }else{ row=sprintf("%s:\n%s",pos,sprintf(" %*-/s",linewidth-6,data)); }
dbe79a1999-12-07Henrik Grubbström (Grubba)  } else { row = pos;
62e87b1998-04-14Fredrik Hübinette (Hubbe)  }
ca2b071998-03-28Henrik Grubbström (Grubba)  }
a232721999-10-06Henrik Grubbström (Grubba)  else {
9798481999-12-06Henrik Grubbström (Grubba)  if (tmp) {
e0e05f2000-03-23Martin Stjernholm  if (catch (row = sprintf("%O", tmp))) row = describe_program(object_program(tmp)) + " with broken _sprintf()";
9798481999-12-06Henrik Grubbström (Grubba)  } else { row = "Destructed object"; }
a232721999-10-06Henrik Grubbström (Grubba)  }
6e343b2001-03-14Martin Stjernholm  }) { row = sprintf("Error indexing backtrace line %d: %s (%O)!", e, err[0], err[1]); }
5d6a562001-11-08Fredrik Hübinette (Hubbe)  int dup_frame; if (!zero_type(dup_frame = prev_pos[row])) { dup_frame -= sizeof(frames); if (!loop_start) { loop_start = dup_frame; loop_next = dup_frame + 1; loops = 0; continue; } else { int new_loop = 0; if (!loop_next) loop_next = loop_start, new_loop = 1; if (dup_frame == loop_next++) { loops += new_loop; continue; } } } prev_pos[row] = sizeof(frames); if (loop_start) { array(string) tail; if (!loop_next) tail = ({}), loops++; else tail = frames[loop_start + sizeof(frames) .. loop_next - 1 + sizeof(frames)]; if (loops) frames += ({sprintf ("... last %d frames above repeated %d times ...\n", -loop_start, loops)}); frames += tail; prev_pos = ([]); loop_start = 0; }
9f71092000-11-20Martin Stjernholm  frames += ({row + "\n"}); } if (loop_start) { // Want tail to contain a full loop rather than being empty; it // looks odd when the repeat message ends the backtrace.
5d6a562001-11-08Fredrik Hübinette (Hubbe)  array(string) tail = frames[loop_start + sizeof(frames) .. loop_next - 1 + sizeof(frames)];
d48c1f2001-12-18Martin Stjernholm  if (loops) frames += ({sprintf("... last %d frames above repeated %d times ...\n", -loop_start, loops)});
9f71092000-11-20Martin Stjernholm  frames += tail;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
9f71092000-11-20Martin Stjernholm  ret += frames * "";
ca2b071998-03-28Henrik Grubbström (Grubba)  }
5be4aa2001-11-12Martin Stjernholm  return ret;
ca2b071998-03-28Henrik Grubbström (Grubba) }
471ed91998-04-24Fredrik Hübinette (Hubbe) 
b07e962001-07-27Martin Nilsson //! @appears describe_error
8f45692001-01-14Henrik Grubbström (Grubba) //! Returns only the error message from a backtrace. //! //! If there is no error message in the backtrace, a fallback message //! will be returned. //! //! @seealso //! @[backtrace()], @[describe_backtrace()] //!
a66ff22000-01-11Martin Stjernholm string describe_error (mixed trace) {
3310402004-03-16Henrik Grubbström (Grubba)  if((arrayp(trace) && sizeof([array]trace)==2 && stringp(([array]trace)[0])) || (objectp(trace) && ([object]trace)->is_generic_error))
a66ff22000-01-11Martin Stjernholm  { if (catch {
3310402004-03-16Henrik Grubbström (Grubba)  return ([array(string)]trace)[0] || "No error message.\n";
a66ff22000-01-11Martin Stjernholm  }) { return "Error indexing backtrace!\n"; } } return sprintf ("Backtrace is of unknown type %t!\n", trace); }
471ed91998-04-24Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba) #ifdef ENCODE_DEBUG # define ENC_MSG(X...) do werror (X); while (0) # define ENC_RETURN(val) do { \ mixed _v__ = (val); \ werror (" returned %s\n", \ zero_type (_v__) ? "UNDEFINED" : \ sprintf ("%O", _v__)); \ return _v__; \ } while (0) #else # define ENC_MSG(X...) do {} while (0) # define ENC_RETURN(val) do return (val); while (0) #endif #ifdef DECODE_DEBUG # define DEC_MSG(X...) do werror (X); while (0) # define DEC_RETURN(val) do { \ mixed _v__ = (val); \ werror (" returned %s\n", \ zero_type (_v__) ? "UNDEFINED" : \ sprintf ("%O", _v__)); \ return _v__; \ } while (0) #else # define DEC_MSG(X...) do {} while (0) # define DEC_RETURN(val) do return (val); while (0) #endif class Encoder //! Codec for use with @[encode_value]. It understands all the //! standard references to builtin functions and pike modules. //! //! The format of the produced identifiers are documented here to //! allow extension of this class: //! //! The produced names are either strings or arrays. The string //! variant specifies the thing to look up according to the first //! character: //! //! 'c' Look up in all_constants(). //! 's' Look up in _static_modules. //! 'r' Look up with resolv(). //! 'p' Look up in programs. //! 'o' Look up in programs, then look up the result in objects. //! 'f' Look up in fc. //! //! In the array format, the first element is a string as above and //! the rest specify a series of things to do with the result: //! //! A string Look up this string in the result. //! 'm' Get module object in dirnode. //! 'p' Do object_program(result). //! //! All lowercase letters and the symbols ':', '/' and '.' are //! reserved for internal use in both cases where characters are used //! above.
471ed91998-04-24Fredrik Hübinette (Hubbe) {
3310402004-03-16Henrik Grubbström (Grubba)  mixed encoded;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba)  static mapping(mixed:string) rev_constants = ([]); static mapping(mixed:string) rev_static_modules = ([]); static array find_index (object|program parent, mixed child, array(object) module_object) { array id; find_id: { array inds = indices (parent), vals = values (parent); int i = search (vals, child); if (i >= 0 && parent[inds[i]] == child) { id = ({inds[i]}); ENC_MSG (" found as parent value with index %O\n", id[0]); } else { // Try again with the programs of the objects in parent, since // it's common that only objects and not their programs are // accessible in modules. foreach (vals; i; mixed val) if (objectp (val) && child == object_program (val) && val == parent[inds[i]]) { if (module_object) { module_object[0] = val; id = ({inds[i]}); } else id = ({inds[i], 'p'}); ENC_MSG (" found as program of parent value object %O with index %O\n", val, id[0]); break find_id; } error ("Cannot find %O in %O.\n", child, parent);
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  } }
3310402004-03-16Henrik Grubbström (Grubba)  if (!stringp (id[0])) error ("Got nonstring index %O for %O in %O.\n", id[0], child, parent);
471ed91998-04-24Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba)  return id; }
471ed91998-04-24Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba)  static string|array compare_resolved (string name, mixed what, mixed resolved, array(object) module_object) { array append; compare: { if (resolved == what) { ENC_MSG (" compare_resolved: %O is %O\n", what, resolved); break compare;
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  if (objectp (resolved)) { if (object_program (resolved) == what) { ENC_MSG (" compare_resolved: %O is program of %O\n", what, resolved); append = ({'p'}); break compare; } if (resolved->is_resolv_dirnode) if (resolved->module == what) { ENC_MSG (" compare_resolved: %O is dirnode module of %O\n", what, resolved); append = ({'m'}); resolved = resolved->module; break compare; } else if (object_program (resolved->module) == what) { ENC_MSG (" compare_resolved: %O is program of dirnode module of %O\n", what, resolved); append = ({'m', 'p'}); break compare; } else ENC_MSG (" compare_resolved: %O is different from dirnode module %O\n", what, resolved->module); #if 0 // This is only safe if the joinnode modules don't conflict, // and we don't know that. if (resolved->is_resolv_joinnode) { ENC_MSG (" compare_resolved: searching for %O in joinnode %O\n", what, resolved); foreach (resolved->joined_modules, mixed part) if (string|array name = compare_resolved (name, what, part, module_object)) { if (module_object) module_object[0] = resolved; return name; } } #endif } ENC_MSG (" compare_resolved: %O is different from %O\n", what, resolved);
471ed91998-04-24Fredrik Hübinette (Hubbe)  return 0; }
3310402004-03-16Henrik Grubbström (Grubba)  name = "r" + name; string|array res = has_value (name, ".") ? name / "." : name; if (append) if (module_object) { // The caller is going to do subindexing. In both the 'p' and // 'm' cases it's better to do that from the original // object/dirnode, so just drop the suffixes. module_object[0] = resolved; return res; } else return (arrayp (res) ? res : ({res})) + append; else return res; } string|array nameof (mixed what, void|array(object) module_object) //! When @[module_object] is set and the name would end with an //! @expr{object_program@} step (i.e. @expr{'p'@}), then drop that //! step so that the name corresponds to the object instead. //! @expr{@[module_object][0]@} will receive the found object. { ENC_MSG ("nameof (%t %O)\n", what, what); if (what == encoded) { ENC_MSG (" got the thing to encode - encoding recursively\n"); return UNDEFINED; } if (string id = rev_constants[what]) ENC_RETURN (id); if (string id = rev_static_modules[what]) ENC_RETURN (id); if (objectp (what)) { if (what->is_resolv_dirnode) { ENC_MSG (" is a dirnode\n"); string name = program_path_to_name (what->dirname); if (string|array ref = compare_resolved (name, what, resolv (name), module_object)) ENC_RETURN (ref); } else if (what->is_resolv_joinnode) { ENC_MSG (" is a joinnode\n"); object modules = _static_modules.Builtin.array_iterator (what->joined_modules); object|mapping value; check_dirnode: if (modules && objectp (value = modules->value()) && value->is_resolv_dirnode) { string name = program_path_to_name (value->dirname); modules += 1; foreach (modules;; value) if (!objectp (value) || !value->is_resolv_dirnode || program_path_to_name (value->dirname) != name) break check_dirnode; ENC_MSG (" joinnode has consistent name %O\n", name); if (string|array ref = compare_resolved (name, what, resolv (name), module_object)) ENC_RETURN (ref); } } program prog; if ((prog = objects_reverse_lookup (what))) ENC_MSG (" found program in objects: %O\n", prog); #if 0 else if ((prog = object_program (what))) ENC_MSG (" got program of object: %O\n", prog); #endif if (prog) { if (prog == encoded) ENC_RETURN ("o"); if (string path = programs_reverse_lookup (prog)) { ENC_MSG (" found path in programs: %O\n", path); string name = program_path_to_name (path); if (string|array ref = compare_resolved (name, what->_module_value || what, resolv (name), module_object)) ENC_RETURN (ref); else { ENC_MSG (" Warning: Failed to resolve; encoding path\n"); #ifdef PIKE_MODULE_RELOC ENC_RETURN ("o" + unrelocate_module (path)); #else ENC_RETURN ("o" + path); #endif }
401fa02002-12-02Martin Stjernholm  }
3310402004-03-16Henrik Grubbström (Grubba)  } if (string path = fc_reverse_lookup (what)) { ENC_MSG (" found path in fc: %O\n", path); string name = program_path_to_name (path); if (string|array ref = compare_resolved (name, what, resolv (name), module_object)) ENC_RETURN (ref); else { ENC_MSG (" Warning: Failed to resolve; encoding path\n"); #ifdef PIKE_MODULE_RELOC ENC_RETURN ("f" + unrelocate_module (path)); #else ENC_RETURN ("f" + path); #endif } } if (what->_encode) { ENC_MSG (" object got _encode function - encoding recursively\n"); return UNDEFINED; } if (function|program prog = object_program (what)) { ENC_MSG (" got program of object: %O\n", prog); if (object|program parent = function_object (prog) || function_program (prog)) { ENC_MSG (" got parent of program: %O\n", parent); // We're going to subindex the parent so we ask for the // module object and not the program. That since we'll // always be able to do a better job if we base the indexing // on objects. array parent_object = ({0}); string|array parent_name = nameof (parent, parent_object); if (!parent_name) { ENC_MSG (" inside the thing to encode - encoding recursively\n"); return UNDEFINED; } else { if (objectp (parent_object[0])) parent = parent_object[0]; array id = find_index (parent, what, module_object); if (equal (id, ({"_module_value"}))) ENC_RETURN (parent_name); else ENC_RETURN ((arrayp (parent_name) ? parent_name : ({parent_name})) + id); } } } error ("Failed to find name of unencodable object %O.\n", what);
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  if (programp (what) || functionp (what)) { if (string path = programs_reverse_lookup (what)) { ENC_MSG (" found path in programs: %O\n", path); string name = program_path_to_name (path); if (string|array ref = compare_resolved (name, what, resolv (name), module_object)) ENC_RETURN (ref); else { ENC_MSG (" Warning: Failed to resolve; encoding path\n"); #ifdef PIKE_MODULE_RELOC ENC_RETURN ("p" + unrelocate_module (path)); #else ENC_RETURN ("p" + path); #endif } } if (object|program parent = function_object (what) || function_program (what)) { ENC_MSG (" got parent: %O\n", parent); if (!objectp (parent)) { object parent_obj = objects[parent]; if (objectp (parent_obj)) { ENC_MSG (" found object for parent program in objects: %O\n", parent_obj); parent = parent_obj; } } array parent_object = ({0}); string|array parent_name = nameof (parent, parent_object); if (!parent_name) { ENC_MSG (" inside the thing to encode - encoding recursively\n"); return UNDEFINED; } else { if (objectp (parent_object[0])) parent = parent_object[0]; if (parent["_module_value"] == what && objects_reverse_lookup (parent)) { ENC_MSG (" found as _module_value of parent module\n"); ENC_RETURN (parent_name); } else { string|array id = function_name (what); if (stringp (id) && parent[id] == what) { ENC_MSG (" found function name in parent: %O\n", id); id = ({id}); } else id = find_index (parent, what, module_object); if (equal (id, ({"_module_value"}))) ENC_RETURN (parent_name); else ENC_RETURN ((arrayp (parent_name) ? parent_name : ({parent_name})) + id); } } } error ("Failed to find name of %t %O.\n", what, what);
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  // FIXME: Should have a reverse mapping of constants in modules; // it can potentially be large mappings and stuff that we encode // here. They can go stale too. ENC_MSG (" encoding recursively\n"); return ([])[0]; }
471ed91998-04-24Fredrik Hübinette (Hubbe)  mixed encode_object(object x)
3310402004-03-16Henrik Grubbström (Grubba)  { DEC_MSG ("encode_object (%O)\n", x); if(!x->_encode) error ("Cannot encode object %O without _encode function.\n", x); DEC_RETURN (([function]x->_encode)()); } void create (void|mixed encoded) //! Creates an encoder instance. If @[encoded] is specified, it's //! encoded instead of being reverse resolved to a name. That's //! necessary to encode programs. { this_program::encoded = encoded; foreach (all_constants(); string var; mixed val) rev_constants[val] = "c" + var; rev_static_modules = mkmapping (values (_static_modules), map (indices (_static_modules), lambda (string name) {return "s" + name;})); #if 0 // This looks flawed; when the decoder looks it up, it'll get the // module and not its program. /mast foreach (rev_static_modules; mixed module; string name) { if (objectp(module)) { program p = object_program(module); if (!rev_static_modules[p]) { // Some people inherit modules... rev_static_modules[p] = "s" + name; } } } #endif } } class Decoder (void|string fname, void|int mkobj) //! Codec for use with @[decode_value]. This is the decoder //! corresponding to @[Encoder]. See that one for more details. { static int unregistered = 1; object __register_new_program(program p) { DEC_MSG ("__register_new_program (%O)\n", p); if(unregistered && fname)
471ed91998-04-24Fredrik Hübinette (Hubbe)  {
3310402004-03-16Henrik Grubbström (Grubba)  unregistered = 0; resolv_debug("register %s\n", fname); programs[fname]=p; if (mkobj) DEC_RETURN (objectp (objects[p]) ? objects[p] : (objects[p]=__null_program()));
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RETURN (0); } static mixed thingof (string|array what) { mixed res; array sublist; if (arrayp (what)) sublist = what, what = sublist[0]; switch (what[0]) { case 'c': if (zero_type (res = all_constants()[what[1..]])) error ("Cannot find global constant %O.\n", what[1..]); break; case 's': if (zero_type (res = _static_modules[what[1..]])) error ("Cannot find %O in _static_modules.\n", what[1..]); break; case 'r': if (zero_type (res = resolv (what[1..]))) error ("Cannot resolve %O.\n", what[1..]); break; case 'p': if (!(res = low_cast_to_program (what[1..], fname, this))) error ("Cannot find program for %O.\n", what[1..]); break; case 'o': if (!objectp (res = low_cast_to_object (what[1..], fname, this))) error ("Cannot find object for %O.\n", what[1..]); break; case 'f': if (!objectp (res = findmodule (what[1..], this))) error ("Cannot find module for %O.\n", what[1..]); break; } DEC_MSG (" got %O\n", res); if (sublist) { mixed subres = res; for (int i = 1; i < sizeof (sublist); i++) { mixed op = sublist[i]; if (stringp (op)) { if (!programp (subres) && !objectp (subres) && !mappingp (subres)) error ("Cannot subindex %O%{[%O]%} since it's a %t.\n", res, sublist[1..i-1], subres); if (zero_type (subres = subres[op])) error ("Cannot find %O in %O%{[%O]%}.\n", op, res, sublist[1..i-1]); DEC_MSG (" indexed with %O: %O\n", op, subres); } else switch (op) { case 'm': if (objectp (subres) && subres->is_resolv_joinnode) { dirnode found; foreach (subres->joined_modules, object|mapping part) if (objectp (part) && part->is_resolv_dirnode && part->module) { if (found) error ("There are ambiguous module objects in %O.\n", subres); else found = part; } if (found) subres = found; } if (objectp (subres) && subres->is_resolv_dirnode) { if (subres->module) { subres = subres->module; DEC_MSG (" got dirnode module %O\n", subres); } else error ("Cannot find module object in dirnode %O.\n", subres); } else error ("Cannot get module object in thing that isn't " "a dirnode or unambiguous joinnode: %O\n", subres); break; case 'p': subres = object_program (subres); DEC_MSG (" got object_program %O\n", subres); break; default: error ("Unknown sublist operation %O in %O\n", op, what); } } res = subres; } return res; } object objectof (string|array what) { DEC_MSG ("objectof (%O)\n", what); if (!what) { // This is necessary for compatibility with 7.2 encoded values: // If an object was fed to encode_value there and there was no // codec then a zero would be encoded silently since the failed // call to nameof was ignored. decode_value would likewise // silently ignore the failed call objectof(0) and a zero would // be decoded. Now we supply a fairly capable codec which is // used by default and we therefore get these objectof(0) calls // here. So if we throw an error we can't decode values which // 7.2 would encode and decode without a codec (albeit partly // incorrectly). So just print a sulky warning and continue.. :P
f2da672004-03-24Anders Johansson  DEC_MSG ("Warning: Decoded broken object identifier to zero.\n");
3310402004-03-16Henrik Grubbström (Grubba)  DEC_RETURN (0); } DEC_RETURN ([object] thingof (what)); } function functionof (string|array what) { DEC_MSG ("functionof (%O)\n", what); DEC_RETURN ([function] thingof (what)); } program programof (string|array what) { DEC_MSG ("programof (%O)\n", what); DEC_RETURN ([program] thingof (what)); }
d28a712001-07-02Henrik Grubbström (Grubba)  void decode_object(object o, mixed data)
471ed91998-04-24Fredrik Hübinette (Hubbe)  {
3310402004-03-16Henrik Grubbström (Grubba)  DEC_MSG ("decode_object (object(%O), %O)\n", object_program (o), data); if(!o->_decode) error ("Cannot decode object(%O) without _decode function.\n", object_program (o)); ([function(mixed:void)]o->_decode)(data);
471ed91998-04-24Fredrik Hübinette (Hubbe)  } }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba) mapping(string:Codec) codecs = set_weak_flag(([]),1); Codec get_codec(string|void fname, int|void mkobj) { string key = fname + "\0" + mkobj; if (codecs[key]) return codecs[key]; return codecs[key] = Decoder(fname, mkobj); } class Codec //! @[Encoder] and @[Decoder] rolled into one. This is for mainly //! compatibility; there's typically no use combining encoding and //! decoding into the same object. { inherit Encoder; inherit Decoder; void create (void|mixed encoded) //! The optional argument is the thing to encode; it's passed on to //! @[Encoder]. { Encoder::create (encoded); } } // The master acts as the default codec. inherit Codec;
5d6a562001-11-08Fredrik Hübinette (Hubbe) 
6df5a52001-11-07Martin Nilsson //! Contains version information about a Pike version.
a20af62000-09-26Fredrik Hübinette (Hubbe) class Version {
6df5a52001-11-07Martin Nilsson  //! The major and minor parts of the version.
a20af62000-09-26Fredrik Hübinette (Hubbe)  int major; int minor;
6df5a52001-11-07Martin Nilsson  //! @decl void create(int major, int minor) //! Set the version in the object.
a20af62000-09-26Fredrik Hübinette (Hubbe)  void create(int maj, int min)
6df5a52001-11-07Martin Nilsson  {
3310402004-03-16Henrik Grubbström (Grubba)  if(maj==-1) maj = __REAL_MAJOR__; if(min==-1) min = __REAL_MINOR__;
6df5a52001-11-07Martin Nilsson  major = maj; minor = min; }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
3310402004-03-16Henrik Grubbström (Grubba) #define CMP(X) ((major - ([object]X)->major) || (minor - ([object]X)->minor))
a20af62000-09-26Fredrik Hübinette (Hubbe) 
6df5a52001-11-07Martin Nilsson  //! Methods define so that version objects //! can be compared and ordered.
3310402004-03-16Henrik Grubbström (Grubba)  int `<(mixed v) { return objectp(v) && CMP(v) < 0; } int `>(mixed v) { return objectp(v) && CMP(v) > 0; } int `==(mixed v) { return objectp(v) && CMP(v)== 0; }
a20af62000-09-26Fredrik Hübinette (Hubbe)  int _hash() { return major * 4711 + minor ; }
9852f72002-12-03Martin Nilsson  string _sprintf(int t) { switch(t) { case 's': return sprintf("%d.%d",major,minor); case 'O': return sprintf("%O(%s)", this_program, this_object()); } }
6df5a52001-11-07Martin Nilsson  //! The version object can be casted into a string.
a20af62000-09-26Fredrik Hübinette (Hubbe)  mixed cast(string type) { switch(type) { case "string": return sprintf("%d.%d",major,minor); } } }
6df5a52001-11-07Martin Nilsson //! Version information about the current Pike version.
a20af62000-09-26Fredrik Hübinette (Hubbe) Version currentversion=Version(__MAJOR__,__MINOR__);
6df5a52001-11-07Martin Nilsson 
a20af62000-09-26Fredrik Hübinette (Hubbe) mapping(Version:CompatResolver) compat_handler_cache=set_weak_flag( ([]), 1); CompatResolver get_compilation_handler(int major, int minor) { Version v=Version(major,minor); if(v > currentversion) { /* do we want to make an error if major.minor > __MAJOR__.__MINOR ? */ return 0; }
3310402004-03-16Henrik Grubbström (Grubba)  CompatResolver ret;
a20af62000-09-26Fredrik Hübinette (Hubbe)  if(!zero_type(ret=compat_handler_cache[v])) return ret;
3310402004-03-16Henrik Grubbström (Grubba)  array(string) files;
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(Version) available=({}); #if "¤share_prefix¤"[0]!='¤'
5be4aa2001-11-12Martin Stjernholm  if (!(files = get_dir("¤share_prefix¤"))) { werror ("Error listing directory %O: %s\n", "¤share_prefix¤", strerror (errno())); files = ({}); } foreach(files, string ver)
a20af62000-09-26Fredrik Hübinette (Hubbe)  { if(sscanf(ver,"%d.%d",int maj, int min)) { Version x=Version(maj, min) ; if(x >= v) available|=({ x }); } } #endif #if "¤lib_prefix¤"[0]!='¤'
5be4aa2001-11-12Martin Stjernholm  if (!(files = get_dir("¤lib_prefix¤"))) { werror ("Error listing directory %O: %s\n", "¤lib_prefix¤", strerror (errno())); files = ({}); } foreach(files, string ver)
a20af62000-09-26Fredrik Hübinette (Hubbe)  { if(sscanf(ver,"%d.%d",int maj, int min)) { Version x=Version(maj, min) ; if(x >= v) available|=({ x }); } } #endif sort(available); #ifndef RESOLVER_HACK /* We need to define RESOLVER_HACK when we add * version-specific stuff in the CompatResolver. * As long as all the compatibility is done in the * module dierctories, RESOLVER_HACK can be undefined */ /* No compat needed */ if(!sizeof(available)) { compat_handler_cache[v]=0; return 0; } /* Same as available[0] */ if(ret=compat_handler_cache[available[0]]) return compat_handler_cache[v]=ret; #endif
3310402004-03-16Henrik Grubbström (Grubba)  // The root resolver is this object. ret = this_object();
a20af62000-09-26Fredrik Hübinette (Hubbe) 
b8cd052000-09-26Fredrik Hübinette (Hubbe)  foreach(reverse(available), Version tmp)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
3310402004-03-16Henrik Grubbström (Grubba)  CompatResolver compat_handler = compat_handler_cache[tmp]; if (!compat_handler) { compat_handler = CompatResolver(tmp); // Fall back to the successor version. compat_handler->parent_resolver = ret; string base;
a20af62000-09-26Fredrik Hübinette (Hubbe) #if "¤lib_prefix¤"[0]!='¤'
3310402004-03-16Henrik Grubbström (Grubba)  base=combine_path("¤lib_prefix¤",sprintf("%s",tmp)); compat_handler->add_module_path(combine_path(base,"modules")); compat_handler->add_include_path(combine_path(base,"include"));
a20af62000-09-26Fredrik Hübinette (Hubbe) #endif #if "¤share_prefix¤"[0]!='¤'
3310402004-03-16Henrik Grubbström (Grubba)  base=combine_path("¤share_prefix¤",sprintf("%s",tmp)); compat_handler->add_module_path(combine_path(base,"modules")); compat_handler->add_include_path(combine_path(base,"include"));
a20af62000-09-26Fredrik Hübinette (Hubbe) #endif
3310402004-03-16Henrik Grubbström (Grubba)  if( tmp <= Version(0,6)) ret->pike_module_path+=({"."});
a20af62000-09-26Fredrik Hübinette (Hubbe)  #ifndef RESOLVER_HACK
3310402004-03-16Henrik Grubbström (Grubba)  ret = compat_handler_cache[tmp] = compat_handler;
a20af62000-09-26Fredrik Hübinette (Hubbe) #endif
3310402004-03-16Henrik Grubbström (Grubba)  } } // Note: May duplicate the assignment above. compat_handler_cache[v] = ret;
a20af62000-09-26Fredrik Hübinette (Hubbe)  return ret; }
9852f72002-12-03Martin Nilsson string _sprintf(int t)
a20af62000-09-26Fredrik Hübinette (Hubbe) {
9852f72002-12-03Martin Nilsson  switch(t) { case 't': return "master"; case 'O': return "master()"; }
a20af62000-09-26Fredrik Hübinette (Hubbe) }