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 //
100c3e2003-12-01Martin Nilsson // $Id: master.pike.in,v 1.312 2003/12/01 18:03:10 nilsson 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.
2f520e2002-04-28Martin Nilsson #undef PIKE_MODULE_RELOC #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;
929cf72003-08-19Martin Nilsson #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.
677d262003-11-14Martin Stjernholm // Have to access some stuff without going through the resolver. private constant Builtin = _static_modules.Builtin; private constant Files = _static_modules.___files; #define Stat Files.Stat
2f520e2002-04-28Martin Nilsson #define capitalize(X) (upper_case((X)[..0])+(X)[1..])
677d262003-11-14Martin Stjernholm #define write(X ...) (Files()->_stdout->write(X)) #define trim_all_whites(X) (Builtin()->string_trim_all_whites (X))
c896b41998-04-29Henrik Grubbström (Grubba) 
3d21a62003-03-27Martin Stjernholm #ifdef RESOLV_DEBUG #if constant (thread_local) static object resolv_msg_depth = thread_local();
97f8142003-09-18Henrik Grubbström (Grubba) // NOTE: May be used before __INIT has completed. #define GET_RESOLV_MSG_DEPTH (resolv_msg_depth && resolv_msg_depth->get()) #define INC_RESOLV_MSG_DEPTH() (resolv_msg_depth && resolv_msg_depth->set (resolv_msg_depth->get() + 1)) #define DEC_RESOLV_MSG_DEPTH() (resolv_msg_depth && resolv_msg_depth->set (resolv_msg_depth->get() - 1))
3d21a62003-03-27Martin Stjernholm #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
4f64a52002-05-22Martin Nilsson //! @appears error
01f7f32002-05-22Johan Sundström //! Throws an error. A more readable version of the code
e5ef062003-04-01Martin Nilsson //! @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
929cf72003-08-19Martin Nilsson  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) {
57b52d2003-05-31Martin Stjernholm  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) {
57b52d2003-05-31Martin Stjernholm  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 {
57b52d2003-05-31Martin Stjernholm  path = combine_path(path, ""); if(has_prefix (s, path)) return "/${PIKE_MODULE_PATH}/"+s[sizeof(path)..];
4839dd2001-09-02Marcus Comstedt  }
57b52d2003-05-31Martin Stjernholm  /* This is necessary to find compat modules... */ foreach(pike_module_path, string path) { path = combine_path(path, "..", ""); if(has_prefix (s, path))
138a732003-08-18Tomas Nilsson  return "/${PIKE_MODULE_PATH}/../"+s[sizeof(path)..];
57b52d2003-05-31Martin Stjernholm  }
4839dd2001-09-02Marcus Comstedt  return s; }
57b52d2003-05-31Martin Stjernholm 
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
39033d2003-04-10Martin Stjernholm #define IS_ABSOLUTE_PATH(X) has_prefix((X),"/")
ca2b071998-03-28Henrik Grubbström (Grubba) #endif
39033d2003-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
57b52d2003-05-31Martin Stjernholm #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.
e5ef062003-04-01Martin Nilsson //! If @[filename] is not specified, it will default to @expr{"-"@}.
8f45692001-01-14Henrik Grubbström (Grubba) //!
e5ef062003-04-01Martin Nilsson //! 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,
c2e10c2003-11-18Martin Stjernholm  object|void handler, void|program p, void|object o)
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,
c2e10c2003-11-18Martin Stjernholm  compat_minor, p, o);
ca2b071998-03-28Henrik Grubbström (Grubba) }
97e5c72001-07-28Martin Nilsson //!
d8a6fc2000-03-25Fredrik Hübinette (Hubbe) string master_read_file(string file) {
677d262003-11-14Martin Stjernholm  object o=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
43293b2002-12-30Henrik 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) 
43293b2002-12-30Henrik 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
df9dcd2003-01-07Martin Nilsson //! 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
e5ef062003-04-01Martin Nilsson //! 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
e5ef062003-04-01Martin Nilsson //! @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) 
43293b2002-12-30Henrik Grubbström (Grubba) //! Mapping containing the cache of currently compiled files. //! //! This mapping currently has the following structure: //! @mapping //! @member program filename //! @endmapping
36bf082003-09-19Martin Nilsson //! The filename path seperator is / on both NT and UNIX.
43293b2002-12-30Henrik Grubbström (Grubba) //! //! @note //! As a special case the current master program is available
e5ef062003-04-01Martin Nilsson //! under the name @expr{"/master"@}.
57b52d2003-05-31Martin Stjernholm 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. {
97f8142003-09-18Henrik Grubbström (Grubba)  // When running with trace, this function can get called // before __INIT has completed. if (!rev_programs) return UNDEFINED;
57b52d2003-05-31Martin Stjernholm  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" }); }
677d262003-11-14Martin Stjernholm static class CompileCallbackError (string error_message, array error_backtrace) { constant is_generic_error = 1; constant is_compile_callback_error = 1; } static void compile_cb_error (string msg, mixed ... args) // Use this to throw errors that should be converted to plain compile // error messages, without backtraces being reported by // compile_exception. { if (sizeof (args)) msg = sprintf (msg, @args); array bt = backtrace(); bt = bt[..sizeof (bt) - 2]; throw (CompileCallbackError (msg, bt)); } static void compile_cb_rethrow (object|array err) // Use this to rethrow errors that should be converted to plain // compile error messages, without backtraces being reported by // compile_exception. { array bt; if (array|object e = catch (bt = get_backtrace (err))) handle_error (e); throw (CompileCallbackError (describe_error (err), bt)); } static void call_compile_warning (object handler, string file, string msg, mixed ... args) { if (sizeof (args)) msg = sprintf (msg, @args); msg = trim_all_whites (msg); if (handler && handler->compile_warning) handler->compile_warning (file, 0, msg); else compile_warning (file, 0, msg); }
6330192000-02-10Fredrik Hübinette (Hubbe) #if constant(_static_modules.Builtin.mutex) #define THREADED
677d262003-11-14Martin Stjernholm Builtin.mutex compilation_mutex = 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) 
68b7a22003-09-19Henrik Grubbström (Grubba)  resolv_debug("low_findprog(%O, %O, %O, %O)\n", pname, ext, handler, mkobj);
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 {
57b52d2003-05-31Martin Stjernholm  if(!zero_type (ret=programs[fname]) && ret != no_value) {
3d21a62003-03-27Martin Stjernholm  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
68065f2003-05-28Johan Sundström  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
68065f2003-05-28Johan Sundström  if (load_time[fname] > s->mtime)
57b52d2003-05-31Martin Stjernholm  if (!zero_type (ret=programs[fname]) && ret != no_value) {
3d21a62003-03-27Martin Stjernholm  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)))
68065f2003-05-28Johan Sundström  { if(s2->isreg && s2->mtime >= s->mtime)
6d926c1999-11-26Henrik Grubbström (Grubba)  { mixed err=catch { AUTORELOAD_CHECK_FILE(oname);
3d21a62003-03-27Martin Stjernholm  resolv_debug ("low_findprog %s: decoding dumped\n", fname); INC_RESOLV_MSG_DEPTH();
401fa02002-12-02Martin Stjernholm  ret = decode_value(master_read_file(oname),
f2d24d2003-06-05Martin Stjernholm  (handler && handler->get_codec || get_codec)(fname, mkobj));
3d21a62003-03-27Martin Stjernholm  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)  };
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: dump decode failed\n", fname);
57b52d2003-05-31Martin Stjernholm  programs[fname] = no_value;
677d262003-11-14Martin Stjernholm  call_compile_warning (handler, oname, "Decode failed: " + describe_error(err));
e115cb1999-12-09Henrik Grubbström (Grubba)  } else if (out_of_date_warning) {
677d262003-11-14Martin Stjernholm  call_compile_warning (handler, oname, "Compiled file is out of date");
10e16f1999-11-04Henrik Grubbström (Grubba)  }
b085961998-05-19Fredrik Hübinette (Hubbe)  }
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
bec57d1999-09-06Fredrik Hübinette (Hubbe) 
3d21a62003-03-27Martin Stjernholm  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);
c2e10c2003-11-18Martin Stjernholm  AUTORELOAD_CHECK_FILE (fname); string src; if (array|object err = catch (src = master_read_file (fname))) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: failed to read file\n", fname); objects[ret] = no_value; ret=programs[fname]=0; // Negative cache. compile_cb_rethrow (err); }
aa68b12001-03-19Fredrik Hübinette (Hubbe)  if ( mixed e=catch {
c2e10c2003-11-18Martin Stjernholm  ret=compile_string(src, fname, handler, ret, mkobj? (objects[ret]=__null_program()) : 0); } )
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: compilation failed\n", fname);
57b52d2003-05-31Martin Stjernholm  objects[ret] = no_value;
ab9d672003-03-08Henrik Grubbström (Grubba)  ret=programs[fname]=0; // Negative cache.
c46d682000-01-05Martin Stjernholm  throw(e);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: compilation ok\n", fname);
ca2b071998-03-28Henrik Grubbström (Grubba)  break; #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) 
c2e10c2003-11-18Martin Stjernholm  if (array|object err = catch (ret = load_module(fakeroot(fname)))) { DEC_RESOLV_MSG_DEPTH(); resolv_debug ("low_findprog %s: failed to load binary\n", fname); objects[ret] = no_value; ret=programs[fname]=0; // Negative cache.
677d262003-11-14Martin Stjernholm  compile_cb_rethrow (err);
c2e10c2003-11-18Martin Stjernholm  }
3d21a62003-03-27Martin Stjernholm  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 
3593292003-09-08Henrik Grubbström (Grubba)  resolv_debug("low_findprog %s: returning %O\n", fname, ret);
ca2b071998-03-28Henrik Grubbström (Grubba)  return programs[fname]=ret; }
3d21a62003-03-27Martin Stjernholm  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)) {
3593292003-09-08Henrik Grubbström (Grubba)  resolv_debug("unregister %s\n", fname);
57b52d2003-05-31Martin Stjernholm  programs[fname] = no_value;
a002d32002-03-01Martin Stjernholm  fname = dirname (fname);
da24482002-05-24Martin Nilsson  object n;
57b52d2003-05-31Martin Stjernholm  if ( fname!="" && objectp (n = fc[fname]) )
a002d32002-03-01Martin Stjernholm  if (n->is_resolv_dirnode || n->is_resolv_joinnode) n->delete_value (p); }
57b52d2003-05-31Martin Stjernholm  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)
57b52d2003-05-31Martin Stjernholm  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)  {
57b52d2003-05-31Martin Stjernholm  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) {
3d21a62003-03-27Martin Stjernholm  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);
677d262003-11-14Martin Stjernholm  if (programp (ret)) return ret; error("Cast %O to program failed%s.\n", pname, (current_file && current_file!="-") ? sprintf(" in %O",current_file) : "");
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",
677d262003-11-14Martin Stjernholm  "get_backtrace",
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;
7a90f12003-01-26Martin Nilsson  //! @decl int strlen(string|multiset|array|mapping|object thing)
e378192003-01-20Martin Nilsson //! @appears strlen //! Alias for @[sizeof]. //! @deprecated sizeof
7a90f12003-01-26Martin Nilsson //! @decl int write(string fmt, mixed ... args)
e378192003-01-20Martin Nilsson //! @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 */
da494f2003-11-12Henrik Grubbström (Grubba) static void create()
ca2b071998-03-28Henrik Grubbström (Grubba) {
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);
677d262003-11-14Martin Stjernholm  add_constant("write", Files()->_stdout->write);
ca2b071998-03-28Henrik Grubbström (Grubba) 
677d262003-11-14Martin Stjernholm #define CO(X) add_constant(#X,Builtin.__backend->X)
87f9082001-03-12Fredrik Hübinette (Hubbe)  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]!='¤'
2d8c832003-08-11Henrik 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
0418252003-10-30H. William Welliver III  system_module_path=pike_module_path;
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) {
3d21a62003-03-27Martin Stjernholm  resolv_debug ("handle_inherit(%O, %O)\n", pname, current_file); INC_RESOLV_MSG_DEPTH();
677d262003-11-14Martin Stjernholm  program ret = cast_to_program(pname, current_file, handler);
3d21a62003-03-27Martin Stjernholm  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;
80f7142003-02-27Henrik 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) {
3d21a62003-03-27Martin Stjernholm  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);
3d21a62003-03-27Martin Stjernholm  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;
677d262003-11-14Martin Stjernholm  error("Cast %O to object failed%s.\n", oname, (current_file && current_file!="-") ? sprintf(" in %O",current_file) : "");
ca2b071998-03-28Henrik Grubbström (Grubba) }
97f8142003-09-18Henrik Grubbström (Grubba) // Marker used for negative caching in module caches. // FIXME: Won't this cause problems when inheriting "/master"? static class ZERO_TYPE {}; //! Module node representing a single directory. //! //! @seealso //! @[joinnode]
eaa4da2001-10-04Fredrik Hübinette (Hubbe) class dirnode
ca2b071998-03-28Henrik Grubbström (Grubba) {
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  string dirname;
97f8142003-09-18Henrik Grubbström (Grubba)  object|void compilation_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 `!.
97f8142003-09-18Henrik Grubbström (Grubba)  mixed module;
098c8a2000-03-30Henrik Grubbström (Grubba)  mapping(string:mixed) cache=([]);
97f8142003-09-18Henrik Grubbström (Grubba)  mapping(string:array(string)) file_paths = ([]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba) #ifdef __NT__ #define FIX_CASE(X) lower_case(X) #else #define FIX_CASE(X) (X) #endif /* __NT__ */ static string base_from_filename(string fname) {
4885f12003-09-19Henrik Grubbström (Grubba)  string low_name = FIX_CASE(fname); if (has_prefix(low_name, ".#")) return 0; if (has_suffix(low_name, ".pike") || has_suffix(low_name, ".pmod")) {
97f8142003-09-18Henrik Grubbström (Grubba)  return fname[..sizeof(fname)-6];
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  }
4885f12003-09-19Henrik Grubbström (Grubba)  if (has_suffix(low_name, ".so")) {
97f8142003-09-18Henrik Grubbström (Grubba)  return fname[..sizeof(fname)-4]; } return 0; } static int prio_from_filename(string fname) { fname = FIX_CASE(fname); if (has_suffix(fname, ".pmod")) return 3; if (has_suffix(fname, ".so")) return 2; if (has_suffix(fname, ".pike")) return 1;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) 
97f8142003-09-18Henrik Grubbström (Grubba)  // FIXME: Warn here? return 0; } static void create(string d, object|void h) { resolv_debug ("dirnode(%O,%O) created\n",d,h); dirname=d; compilation_handler=h; fc[dirname]=this_object();
4885f12003-09-19Henrik Grubbström (Grubba)  if(has_suffix(FIX_CASE(dirname),".pmod")) {
97f8142003-09-18Henrik Grubbström (Grubba)  fc[dirname[..sizeof(dirname)-6]]=this_object();
4885f12003-09-19Henrik Grubbström (Grubba)  }
97f8142003-09-18Henrik Grubbström (Grubba)  array(string) files = sort(get_dir(d)||({})); if (!sizeof(d)) return; array(string) bases = map(files, base_from_filename); files = filter(files, bases); bases = filter(bases, bases); resolv_debug("dirnode(%O,%O) got %d files.\n", d, h, sizeof(bases)); if (!sizeof(files)) return; foreach(files; int no; string fname) { fname = combine_path(dirname, fname); string base = bases[no]; if (base == "module") { // We need a module_checker. module = module_checker(); } array(string) paths = file_paths[base]; if (!paths) { // New entry. file_paths[base] = ({ fname }); continue; } // Multiple files. Order according to prio_from_filename(). // Insert sort. Worst case is 3 filenames. int prio = prio_from_filename(fname); int index; foreach(paths; index; string other_fname) { if (prio_from_filename(other_fname) <= prio) break; } file_paths[base] = paths[..index-1] + ({ fname }) + paths[index..];
e378192003-01-20Martin Nilsson  } }
14bb592000-05-06Fredrik Hübinette (Hubbe)  class module_checker { int `!() {
3d21a62003-03-27Martin Stjernholm  resolv_debug ("dirnode(%O)->module_checker()->`!()\n",dirname); INC_RESOLV_MSG_DEPTH();
80f7142003-02-27Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba)  if (mixed err = catch { // Look up module. if (module = cache["module"] || low_ind("module", 1)) {
80f7142003-02-27Henrik Grubbström (Grubba)  /* This allows for `[] to have side effects first time * it is called. (Specifically, the Calendar module uses
97f8142003-09-18Henrik Grubbström (Grubba)  * this).
80f7142003-02-27Henrik Grubbström (Grubba)  */ cache=([]); _cache_full=0; }
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH();
97f8142003-09-18Henrik Grubbström (Grubba)  resolv_debug("dirnode(%O)->module_checker()->`!() => %s\n", dirname, !module ? "doesn't exist" : "exists");
3d21a62003-03-27Martin Stjernholm  return !module;
80f7142003-02-27Henrik Grubbström (Grubba)  }) {
4ce9832003-06-05Martin Stjernholm  //werror ("findmodule error: " + describe_backtrace (err));
80f7142003-02-27Henrik Grubbström (Grubba)  // 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...
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH();
97f8142003-09-18Henrik Grubbström (Grubba)  resolv_debug("dirnode(%O)->module_checker()->`!() => failure, doesn't exist\n", dirname);
80f7142003-02-27Henrik Grubbström (Grubba)  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  {
3d21a62003-03-27Martin Stjernholm  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)  }
97f8142003-09-18Henrik Grubbström (Grubba)  static mixed low_ind(string index, int(0..1)|void set_module) { array(string) paths; if (!(paths = file_paths[index])) { DEC_RESOLV_MSG_DEPTH(); resolv_debug("dirnode(%O)->ind(%O) => no file match\n", dirname, index); return UNDEFINED; } foreach(paths, string fname) { resolv_debug("dirnode(%O)->ind(%O) Trying file %O...\n", dirname, index, fname); Stat stat = master_file_stat(fakeroot(fname)); if (!stat) { resolv_debug("dirnode(%O)->ind(%O) file %O disappeared!\n", dirname, index, fname); continue; } if (has_suffix(fname, ".pmod")) { if (stat->isdir) { resolv_debug("dirnode(%O)->ind(%O) => found subdirectory %O\n", dirname, index, fname); return dirnode(fname, compilation_handler); } resolv_debug("dirnode(%O)->ind(%O) casting (object)%O\n", dirname, index, fname); // FIXME: cast_to_program() and cast_to_object() // have lots of overhead to guess the proper // filename. This overhead isn't needed in // our cases, so we could make do with // low_findprog() and the caches. mixed ret;
677d262003-11-14Martin Stjernholm  if (objectp(ret = low_cast_to_object(fname, 0, compilation_handler))) {
97f8142003-09-18Henrik Grubbström (Grubba)  // This assignment is needed for eg the Calendar module. if (set_module) module = ret; if(mixed tmp=ret->_module_value) ret=tmp; DEC_RESOLV_MSG_DEPTH(); resolv_debug("dirnode(%O)->ind(%O) => found submodule %O:%O\n", dirname, index, fname, ret); return ret; } } else { resolv_debug("dirnode(%O)->ind(%O) casting (program)%O\n", dirname, index, fname); program ret;
677d262003-11-14Martin Stjernholm  if (ret = low_cast_to_program(fname, 0, compilation_handler)) {
97f8142003-09-18Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug("dirnode(%O)->ind(%O) => found subprogram %O:%O\n", dirname, index, fname, ret); return ret; } } resolv_debug("dirnode(%O)->ind(%O) => failure for file %O\n", dirname, index, fname); } resolv_debug("dirnode(%O)->ind(%O) => UNDEFINED\n", dirname, index); return UNDEFINED; }
098c8a2000-03-30Henrik Grubbström (Grubba)  static mixed ind(string index)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3d21a62003-03-27Martin Stjernholm  resolv_debug ("dirnode(%O)->ind(%O)\n", dirname, index); INC_RESOLV_MSG_DEPTH();
97f8142003-09-18Henrik Grubbström (Grubba)  if (_cache_full) { DEC_RESOLV_MSG_DEPTH(); resolv_debug("dirnode(%O)->ind(%O) => cache_full %O\n", dirname, index, cache[index]); return cache[index]; }
3d21a62003-03-27Martin Stjernholm  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])) {
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("dirnode(%O)->ind(%O) => found %O\n", dirname, index, o);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return o; }
97f8142003-09-18Henrik Grubbström (Grubba)  resolv_debug ("dirnode(%O)->ind(%O) => not found in module\n", dirname, index);
a2faf61999-12-01Martin Stjernholm  }
4ce9832003-06-05Martin Stjernholm  else resolv_debug ("dirnode(%O)->ind(%O) => no module\n", dirname, index);
ca2b071998-03-28Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba)  return low_ind(index);
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
e378192003-01-20Martin Nilsson  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
3d21a62003-03-27Martin Stjernholm  werror("%*nDirnode(%O) cache[%O] => %O%s\n",
ab9d672003-03-08Henrik Grubbström (Grubba)  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;
ab9d672003-03-08Henrik 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);
dd7d452002-12-09Martin Stjernholm  // We might have gotten placeholder objects in the first pass // which must not be cached to the second.
ab9d672003-03-08Henrik Grubbström (Grubba)  if(ret == predef::__placeholder_object) { #ifdef MODULE_TRACE werror("%*nDirnode(%O) PLACE_HOLDER.\n", sizeof(backtrace()),dirname); #endif return ret; }
dd7d452002-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) 
97f8142003-09-18Henrik Grubbström (Grubba)  mixed safe_index(string index) { mixed err; if (err = catch { return `[](index); }) {
677d262003-11-14Martin Stjernholm  call_compile_warning (compilation_handler, dirname+"."+fname, "Compilation failed: " + describe_error(err));
97f8142003-09-18Henrik Grubbström (Grubba)  } return UNDEFINED; }
e378192003-01-20Martin Nilsson  static int(0..1) _cache_full;
3e4b811998-10-16Henrik Grubbström (Grubba)  void fill_cache() {
3d21a62003-03-27Martin Stjernholm #if 0
f4f08d1999-02-26Henrik Grubbström (Grubba)  werror(describe_backtrace(({ sprintf("Filling cache in dirnode %O\n", dirname), backtrace() })));
3d21a62003-03-27Martin Stjernholm #endif
3e4b811998-10-16Henrik Grubbström (Grubba)  if (_cache_full) { return; }
97f8142003-09-18Henrik Grubbström (Grubba)  // NOTE: We rely on side effects in `[]() and safe_index() // to fill the cache.
677d262003-11-14Martin Stjernholm  // Why shouldn't thrown errors be propagated here? /mast
3e4b811998-10-16Henrik Grubbström (Grubba)  if (module) {
97f8142003-09-18Henrik Grubbström (Grubba)  map(indices(module), safe_index);
3e4b811998-10-16Henrik Grubbström (Grubba)  }
97f8142003-09-18Henrik Grubbström (Grubba)  map(indices(file_paths), safe_index);
3e4b811998-10-16Henrik Grubbström (Grubba)  _cache_full = 1; }
e378192003-01-20Martin Nilsson  static array(string) _indices()
3e4b811998-10-16Henrik Grubbström (Grubba)  { fill_cache();
eb801d2003-06-05Martin Stjernholm  // 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)  }
e378192003-01-20Martin Nilsson  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) {
97f8142003-09-18Henrik Grubbström (Grubba)  if (string name = search (cache, val)) {
a002d32002-03-01Martin Stjernholm  m_delete (cache, name);
97f8142003-09-18Henrik Grubbström (Grubba)  _cache_full = 0; }
a002d32002-03-01Martin Stjernholm  }
e378192003-01-20Martin Nilsson  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) 
97f8142003-09-18Henrik Grubbström (Grubba) //! Module node holding possibly multiple directories, //! and optionally falling back to another level. //! //! @seealso //! @[dirnode]
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) 
97f8142003-09-18Henrik Grubbström (Grubba)  object compilation_handler; // NOTE: Uses the empty mapping as the default fallback // for simplified code. joinnode|mapping(mixed:int(0..0)) fallback_module = ([]);
52c5792000-05-23Fredrik Hübinette (Hubbe)  string _sprintf(int as)
97f8142003-09-18Henrik Grubbström (Grubba)  { return as=='O' && sprintf("master()->joinnode(%O)",joined_modules); }
52c5792000-05-23Fredrik Hübinette (Hubbe) 
da494f2003-11-12Henrik Grubbström (Grubba)  static void create(array(object|mapping) _joined_modules, object|void _compilation_handler, joinnode|void _fallback_module)
94257b1998-10-16Henrik Grubbström (Grubba)  { joined_modules = _joined_modules;
97f8142003-09-18Henrik Grubbström (Grubba)  compilation_handler = _compilation_handler; fallback_module = _fallback_module || ([]);
3d21a62003-03-27Martin Stjernholm  resolv_debug ("joinnode(%O) created\n", joined_modules);
94257b1998-10-16Henrik Grubbström (Grubba)  }
97f8142003-09-18Henrik Grubbström (Grubba)  void add_path(string path) { path = combine_path(getcwd(), path); dirnode node = fc[path] || (fc[path] = dirnode(path, compilation_handler)); if (sizeof(joined_modules) && joined_modules[0] == node) return; joined_modules = ({ node }) + (joined_modules - ({ node }));
da494f2003-11-12Henrik Grubbström (Grubba)  // FIXME: Propagate!
97f8142003-09-18Henrik Grubbström (Grubba)  } void rem_path(string path) { path = combine_path(getcwd(), path); joined_modules = filter(joined_modules, lambda(dirnode node) { return !objectp(node) || !node->is_resolv_dirnode || (node->dirname != path); });
da494f2003-11-12Henrik Grubbström (Grubba)  // FIXME: What about the cache? // FIXME: Propagate!
97f8142003-09-18Henrik Grubbström (Grubba)  }
098c8a2000-03-30Henrik Grubbström (Grubba)  static mixed ind(string index)
94257b1998-10-16Henrik Grubbström (Grubba)  {
3d21a62003-03-27Martin Stjernholm  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])) {
97f8142003-09-18Henrik Grubbström (Grubba)  if (objectp(ret) &&
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).
97f8142003-09-18Henrik Grubbström (Grubba)  res += ({ ret }); } else {
3d21a62003-03-27Martin Stjernholm  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)  }
94257b1998-10-16Henrik Grubbström (Grubba)  } }
3d21a62003-03-27Martin Stjernholm 
97f8142003-09-18Henrik Grubbström (Grubba)  if (sizeof(res)) {
3d21a62003-03-27Martin Stjernholm  DEC_RESOLV_MSG_DEPTH();
97f8142003-09-18Henrik Grubbström (Grubba)  resolv_debug("joinnode(%O)->ind(%O) => new joinnode, fallback: %O\n", joined_modules, index, fallback_module[index]); return joinnode(res, compilation_handler, fallback_module[index]);
3d21a62003-03-27Martin Stjernholm  } DEC_RESOLV_MSG_DEPTH();
97f8142003-09-18Henrik Grubbström (Grubba)  resolv_debug ("joinnode(%O)->ind(%O) => not found. Trying fallback %O\n", joined_modules, index, fallback_module); return fallback_module[index];
94257b1998-10-16Henrik Grubbström (Grubba)  }
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);
dd7d452002-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; }
eb801d2003-06-05Martin Stjernholm 
098c8a2000-03-30Henrik Grubbström (Grubba)  static int _cache_full;
eb801d2003-06-05Martin Stjernholm 
3e4b811998-10-16Henrik Grubbström (Grubba)  void fill_cache() {
3d21a62003-03-27Martin Stjernholm #if 0
f4f08d1999-02-26Henrik Grubbström (Grubba)  werror(describe_backtrace(({ "Filling cache in joinnode\n", backtrace() })));
3d21a62003-03-27Martin Stjernholm #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); } } }
a165032003-09-19Henrik Grubbström (Grubba)  foreach(indices(fallback_module), string index) { `[](index); }
3e4b811998-10-16Henrik Grubbström (Grubba)  _cache_full = 1; }
eb801d2003-06-05Martin Stjernholm 
3e4b811998-10-16Henrik Grubbström (Grubba)  array(string) _indices() { fill_cache();
eb801d2003-06-05Martin Stjernholm  // 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)  }
eb801d2003-06-05Martin Stjernholm 
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) 
4ce9832003-06-05Martin Stjernholm  int `== (mixed other) { return objectp (other) && other->is_resolv_joinnode && equal (mkmultiset (joined_modules), mkmultiset (other->joined_modules)); }
57b52d2003-05-31Martin Stjernholm  array(object) _encode() { return joined_modules; } void _decode (array(object) joined_modules) { this_program::joined_modules = joined_modules; } };
ca2b071998-03-28Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba) joinnode handle_import(string path, string|void current_file, object|void current_handler) { array(string) tmp; if(current_file) { tmp=EXPLODE_PATH(current_file); tmp[-1] = path; path=combine_path_with_cwd( tmp*"/"); } else { path = combine_path_with_cwd(path); } // FIXME: Need caching!!! #if 0 // FIXME: This caching strategy could be improved, // since it ignores module_nodes from the // ordinary module tree. if (module_node_cache[current_handler]) { if (module_node_cache[current_handler][path]) { return module_node_cache[current_handler][path]; } } else { module_node_cache[current_handler] = ([]); } module_node node = module_node_cache[current_handler][path] = module_node("import::"+path, 0, current_handler); #endif /* 0 */ joinnode node = joinnode(({}), current_handler); #ifdef PIKE_MODULE_RELOC // If we have PIKE_MODULE_RELOC enabled, // we might need to map to multiple directories. if(path == "/${PIKE_MODULE_PATH}" || has_prefix(path, "/${PIKE_MODULE_PATH}/")) { string tmp = path[21..]; foreach(pike_module_path, string prefix) { node->add_path(sizeof(tmp)? combine_path(prefix, tmp) : prefix); } } else #endif /* PIKE_MODULE_RELOC */ node->add_path(path); return node; } // Probably not used anymore.
3593292003-09-08Henrik Grubbström (Grubba) program|object findmodule(string fullname, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) {
3593292003-09-08Henrik Grubbström (Grubba)  program|object o;
57b52d2003-05-31Martin Stjernholm 
3d21a62003-03-27Martin Stjernholm  resolv_debug ("findmodule(%O)\n", fullname);
57b52d2003-05-31Martin Stjernholm  if(!zero_type(o=fc[fullname]) && o != no_value)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
3593292003-09-08Henrik Grubbström (Grubba)  if (objectp(o) || programp(o) || o != 0) {
3d21a62003-03-27Martin Stjernholm  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)  {
68065f2003-05-28Johan Sundström  if(stat->isdir) {
3d21a62003-03-27Martin Stjernholm  resolv_debug ("findmodule(%O) => new dirnode\n", fullname);
57b52d2003-05-31Martin Stjernholm  return fc[fullname] = dirnode(fullname+".pmod", handler);
3d21a62003-03-27Martin Stjernholm  }
ca2b071998-03-28Henrik Grubbström (Grubba)  }
3d21a62003-03-27Martin Stjernholm  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;
3d21a62003-03-27Martin Stjernholm  }
ca2b071998-03-28Henrik Grubbström (Grubba)  #if constant(load_module)
3d21a62003-03-27Martin Stjernholm  if(master_file_stat(fakeroot(fullname+".so"))) { o = fc[fullname] = low_cast_to_object(fullname, "/.", handler); DEC_RESOLV_MSG_DEPTH(); resolv_debug ("findmodule(%O) => got .so object %O\n", fullname, o); return o; }
ca2b071998-03-28Henrik Grubbström (Grubba) #endif
677d262003-11-14Martin Stjernholm  if (programp (o = low_cast_to_program(fullname, "/.", handler))) {
3593292003-09-08Henrik Grubbström (Grubba)  DEC_RESOLV_MSG_DEPTH(); resolv_debug ("findmodule(%O) => got .pike program %O\n", fullname, o); return fc[fullname] = o; }
3d21a62003-03-27Martin Stjernholm  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) }
97f8142003-09-18Henrik Grubbström (Grubba) #if 0
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)  }
57b52d2003-05-31Martin Stjernholm  #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
34241f2001-05-24Henrik Grubbström (Grubba)  if (handler) {
3d21a62003-03-27Martin Stjernholm  resolv_debug ("handle_import(%O, %O, %O) => new dirnode with handler\n", what, current_file, handler);
34241f2001-05-24Henrik Grubbström (Grubba)  return dirnode(path, handler); }
57b52d2003-05-31Martin Stjernholm #endif if(objectp (fc[path])) {
3d21a62003-03-27Martin Stjernholm  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);
2d8c832003-08-11Henrik Grubbström (Grubba) #ifdef PIKE_MODULE_RELOC // 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}/")) { string tmp = path[21..]; 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) }); } 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]; return UNDEFINED; } #endif /* PIKE_MODULE_RELOC */ return fc[path] = dirnode(fakeroot(path), handler);
cc2c071998-04-19Fredrik Hübinette (Hubbe) }
97f8142003-09-18Henrik Grubbström (Grubba) #endif /* 0 */
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) {
97f8142003-09-18Henrik Grubbström (Grubba)  //! Actual resolver joinnode root_module = joinnode(({predef::_static_modules})); //! Lookup from handler module to corresponding root_module. mapping(object:joinnode) handler_root_modules = ([]);
94dc1b2003-10-30H. William Welliver III  //! The pike system module path, not including any set by the user.
0418252003-10-30H. William Welliver III  array(string) system_module_path=({}); //! The complete module search path
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(string) pike_module_path=({});
0418252003-10-30H. William Welliver III  //! The complete include search path
97f8142003-09-18Henrik Grubbström (Grubba)  array(string) pike_include_path=({});
0418252003-10-30H. William Welliver III  //! The complete program search path
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(string) pike_program_path=({});
0418252003-10-30H. William Welliver III 
da494f2003-11-12Henrik Grubbström (Grubba)  mapping(string:string) predefines = master()->initial_predefines;
a20af62000-09-26Fredrik Hübinette (Hubbe)  string ver;
97f8142003-09-18Henrik Grubbström (Grubba)  //! If we fail to resolv, try the fallback.
3593292003-09-08Henrik Grubbström (Grubba)  //! //! Typical configuration: //! @pre{0.6->7.0->7.2->7.4->master@}
97f8142003-09-18Henrik Grubbström (Grubba)  CompatResolver fallback_resolver;
3593292003-09-08Henrik Grubbström (Grubba) 
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.
97f8142003-09-18Henrik Grubbström (Grubba)  //! //! It can also optionally be initialized with a fallback resolver.
da494f2003-11-12Henrik Grubbström (Grubba)  static void create(mixed version, CompatResolver|void fallback_resolver)
97f8142003-09-18Henrik Grubbström (Grubba)  { resolv_debug("CompatResolver(%O, %O)\n", version, fallback_resolver); ver=(string)version; #if 0 if (version) { root_module->symbol = ver + "::"; } #endif if (CompatResolver::fallback_resolver = fallback_resolver) { root_module->fallback_module = fallback_resolver->root_module;
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
97f8142003-09-18Henrik Grubbström (Grubba)  predefines = initial_predefines; }
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));
97f8142003-09-18Henrik Grubbström (Grubba)  root_module->add_path(tmp); pike_module_path = ({ tmp }) + (pike_module_path - ({ tmp }));
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
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));
97f8142003-09-18Henrik Grubbström (Grubba)  root_module->rem_path(tmp); pike_module_path -= ({ tmp });
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
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); }
db77fa2002-12-14Martin Nilsson  //! 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; }
97f8142003-09-18Henrik Grubbström (Grubba)  // _static_modules -- default for global:: // current_handler->get_default_module()->_static_modules
3215c92003-05-15Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba)  joinnode get_root_module(object|void current_handler) { if (!root_module) { error("get_root_module(%O): No default root module!\n", current_handler); } if (!current_handler) return root_module; joinnode node = handler_root_modules[current_handler]; if (node) return node;
34241f2001-05-24Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba)  // Check for _static_modules. mixed static_modules = _static_modules; if (current_handler->get_default_module) { mapping(string:mixed) default_module = current_handler->get_default_module(); if (default_module) { static_modules = default_module["_static_modules"] || ([]);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
94257b1998-10-16Henrik Grubbström (Grubba)  }
97f8142003-09-18Henrik Grubbström (Grubba)  node = joinnode(({ static_modules, // Copy relevant stuff from the root module. @filter(root_module->joined_modules, lambda(mixed x) { return objectp(x) && x->is_resolv_dirnode; }) }), current_handler, root_module->fallback_module); // FIXME: Is this needed? // Kluge to get _static_modules to work at top level. node->cache->_static_modules = static_modules; return node; } //! mixed resolv_base(string identifier, string|void current_file, object|void current_handler) { // werror("Resolv_base(%O)\n",identifier); return get_root_module(current_handler)[identifier]; }
6df5a52001-11-07Martin Nilsson 
100c3e2003-12-01Martin Nilsson  //! Same as @[resolv], but throws an error instead of returning //! @[UNDEFINED] if the resolv failed. mixed resolv_or_error(string identifier, string|void current_file, void|object current_handler) { mixed res = resolv(identifier, current_file, current_handler); if(zero_type(res)) error("Could not resolv %s.\n", identifier); return res; }
6df5a52001-11-07Martin Nilsson  //!
34241f2001-05-24Henrik Grubbström (Grubba)  mixed resolv(string identifier, string|void current_file, object|void current_handler)
97f8142003-09-18Henrik Grubbström (Grubba)  { resolv_debug("resolv(%O, %O)\n",identifier, current_file); INC_RESOLV_MSG_DEPTH();
a20af62000-09-26Fredrik Hübinette (Hubbe) 
97f8142003-09-18Henrik Grubbström (Grubba)  // FIXME: Support having the cache in the handler? if( no_resolv[ identifier ] ) { DEC_RESOLV_MSG_DEPTH(); resolv_debug("resolv(%O, %O) => excluded\n",identifier, current_file); return UNDEFINED; }
26ece22001-06-18Henrik Grubbström (Grubba) 
97f8142003-09-18Henrik Grubbström (Grubba)  if (current_file && !stringp(current_file)) { error( "resolv(%O, %O, %O): current_file is not a string!\n", identifier, current_file, current_handler, backtrace() ); }
eaa4da2001-10-04Fredrik Hübinette (Hubbe) 
97f8142003-09-18Henrik Grubbström (Grubba)  array(string) tmp=identifier/"."; mixed ret = resolv_base(tmp[0], current_file, current_handler); foreach(tmp[1..],string index) { if (zero_type(ret)) break; ret = ret[index]; } DEC_RESOLV_MSG_DEPTH();
3c6f582002-05-24Martin Stjernholm #ifdef RESOLV_DEBUG
97f8142003-09-18Henrik Grubbström (Grubba)  if (zero_type (ret)) resolv_debug("resolv(%O, %O) => not found\n",identifier, current_file); else resolv_debug("resolv(%O, %O) => found %O\n",identifier, current_file, ret);
3c6f582002-05-24Martin Stjernholm #endif /* RESOLV_DEBUG */
97f8142003-09-18Henrik Grubbström (Grubba)  return ret; }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
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)
97f8142003-09-18Henrik Grubbström (Grubba)  { if(local_include)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
97f8142003-09-18Henrik Grubbström (Grubba)  array(string) tmp=EXPLODE_PATH(current_file); tmp[-1]=f; return combine_path_with_cwd(tmp*"/"); } else { foreach(pike_include_path, string path)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
97f8142003-09-18Henrik Grubbström (Grubba)  path=combine_path(path,f); if(master_file_stat(fakeroot(path))) return path;
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
97f8142003-09-18Henrik Grubbström (Grubba)  if (fallback_resolver) { return fallback_resolver->handle_include(f, current_file,
3593292003-09-08Henrik Grubbström (Grubba)  local_include);
a20af62000-09-26Fredrik Hübinette (Hubbe)  } }
97f8142003-09-18Henrik Grubbström (Grubba)  // Failed. return 0; }
6df5a52001-11-07Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  string read_include(string f)
97f8142003-09-18Henrik Grubbström (Grubba)  {
c2e10c2003-11-18Martin Stjernholm  AUTORELOAD_CHECK_FILE(f); if (array|object err = catch { return master_read_file (f); }) compile_cb_rethrow (err);
97f8142003-09-18Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
9852f72002-12-03Martin Nilsson  string _sprintf(int t)
97f8142003-09-18Henrik Grubbström (Grubba)  { return t=='O' && sprintf("CompatResolver(%O)",ver); }
7c11f41999-10-04Fredrik Hübinette (Hubbe) }
a20af62000-09-26Fredrik Hübinette (Hubbe) inherit CompatResolver;
0e66132003-09-20Martin Nilsson //!
97f8142003-09-18Henrik Grubbström (Grubba) class Pike06Resolver { inherit CompatResolver; //! In Pike 0.6 the current directory was implicitly searched. mixed resolv_base(string identifier, string|void current_file, object|void current_handler) { if (current_file) { joinnode node = handle_import(".", current_file, current_handler); return node[identifier] || ::resolv_base(identifier, current_file, current_handler); } return ::resolv_base(identifier, current_file, current_handler); } }
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
70e2ca2003-01-01Martin Nilsson 
97f8142003-09-18Henrik Grubbström (Grubba) mapping(string:string) initial_predefines = ([]);
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);
5048622003-04-10Martin Nilsson  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) 
97f8142003-09-18Henrik Grubbström (Grubba)  predefines = initial_predefines =
677d262003-11-14Martin Stjernholm  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) 
f595e92003-01-01Martin Nilsson  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)  }
f595e92003-01-01Martin Nilsson  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 
0418252003-10-30H. William Welliver III 
e90a8b1999-06-01Mirar (Pontus Hagland) #ifndef NOT_INSTALLED
5048622003-04-10Martin Nilsson  {
929cf72003-08-19Martin Nilsson  array parts = (getenv("PIKE_INCLUDE_PATH")||"")/PATH_SEPARATOR-({""});
5048622003-04-10Martin Nilsson  int i = sizeof(parts); while(i) add_include_path(parts[--i]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
929cf72003-08-19Martin Nilsson  parts = (getenv("PIKE_PROGRAM_PATH")||"")/PATH_SEPARATOR-({""});
5048622003-04-10Martin Nilsson  i = sizeof(parts); while(i) add_program_path(parts[--i]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
929cf72003-08-19Martin Nilsson  parts = (getenv("PIKE_MODULE_PATH")||"")/PATH_SEPARATOR-({""});
5048622003-04-10Martin Nilsson  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  };
27a23d2003-01-27Martin Nilsson  mixed main_resolv(string ... syms) { mixed v = resolv(syms[0]); foreach(syms[1..], string sym) if(v) v = v[sym]; if(!v)
4ca24f2003-05-19Martin Stjernholm  _error("Could not resolv %s. (Perhaps the installed pike tree has been moved.)\n",
27a23d2003-01-27Martin Nilsson  syms*"."); return v; };
ca2b071998-03-28Henrik Grubbström (Grubba)  if(sizeof(argv)>1 && sizeof(argv[1]) && argv[1][0]=='-') {
5048622003-04-10Martin Nilsson  array q;
27a23d2003-01-27Martin Nilsson  tmp = main_resolv( "Getopt" );
8c03142002-11-15Martin Nilsson  int NO_ARG = tmp->NO_ARG; int MAY_HAVE_ARG = tmp->MAY_HAVE_ARG; int HAS_ARG = tmp->HAS_ARG;
a7a45a1998-05-11Henrik Grubbström (Grubba) 
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}),
64ff312003-04-03Martin Nilsson  ({"dumpversion", NO_ARG, ({"--dumpversion"}), 0, 0}),
d62eab2003-09-18Martin Nilsson  ({"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);
ca2b071998-03-28Henrik Grubbström (Grubba)  /* Parse -M and -I backwards */
5048622003-04-10Martin Nilsson  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++;
631e282003-05-28Johan Sundström  break;
bec57d1999-09-06Fredrik Hübinette (Hubbe) #endif
8c03142002-11-15Martin Nilsson 
3d14462000-09-24Per Hedbor  case "debug_without":
1a67ea2003-07-22Martin Nilsson  // 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;
f595e92003-01-01Martin Nilsson  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]) {
64ff312003-04-03Martin Nilsson  case "dumpversion": write("%d.%d.%d\n", __REAL_MAJOR__, __REAL_MINOR__, __REAL_BUILD__); exit(0);
ca2b071998-03-28Henrik Grubbström (Grubba)  case "version":
9529202003-03-27Martin Nilsson  werror(version() + " Copyright © 1994-2003 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":
d62eab2003-09-18Martin Nilsson  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":
dd75be2003-02-26Martin Stjernholm #ifdef __AUTO_BIGNUM__ main_resolv( "Gmp", "bignum" ); #endif /* __AUTO_BIGNUM__ */
f9a0962003-03-20Martin Stjernholm 
3215c92003-05-15Henrik Grubbström (Grubba)  random_seed((time() ^ (getpid()<<8))); argv = tmp->get_args(argv,1);
f9a0962003-03-20Martin Stjernholm  program prog;
41d0af2003-04-10Martin Nilsson  if(Version(compat_major,compat_minor) <= Version(7,4))
f9a0962003-03-20Martin Stjernholm  prog = compile_string( "mixed create(int argc, array(string) argv,array(string) env){"+ opts[1]+";}");
2ac59e2002-12-29Martin Nilsson  else
f9a0962003-03-20Martin Stjernholm  prog = compile_string(
70e2ca2003-01-01Martin Nilsson  "#define NOT(X) !(X)\n" "#define CHAR(X) 'X'\n"
f9a0962003-03-20Martin Stjernholm  "mixed run(int argc, array(string) argv," "mapping(string:string) env){"+ opts[1]+";}"); #if constant(_debug) if(debug) _debug(debug); #endif
97f8142003-09-18Henrik Grubbström (Grubba)  if(trace) trace = predef::trace(trace);
f9a0962003-03-20Martin Stjernholm  mixed ret; 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. if(currentversion <= Version(7,4)) prog (sizeof(argv),argv,env); else ret = prog()->run(sizeof(argv),argv,getenv()); };
97f8142003-09-18Henrik Grubbström (Grubba)  predef::trace(trace);
f9a0962003-03-20Martin Stjernholm  if (err) { handle_error (err); ret = 10; }
70e2ca2003-01-01Martin Nilsson  if(stringp(ret)) { write(ret); if(ret[-1]!='\n') write("\n"); }
2ac59e2002-12-29Martin Nilsson  if(!intp(ret) || ret<0) ret=0; exit(ret);
fcf7281998-04-28Fredrik Hübinette (Hubbe) 
c5cc2d1998-04-29Henrik Grubbström (Grubba)  case "preprocess":
dd75be2003-02-26Martin Stjernholm #ifdef __AUTO_BIGNUM__ main_resolv( "Gmp", "bignum" ); #endif /* __AUTO_BIGNUM__ */
70e2ca2003-01-01Martin Nilsson  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":
27a23d2003-01-27Martin Nilsson  write( main_resolv( "Tools", "Install", "features" )()*"\n"+"\n" );
70e2ca2003-01-01Martin Nilsson  exit(0);
a6b6df2001-01-19Mirar (Pontus Hagland)  case "info":
70e2ca2003-01-01Martin Nilsson  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......"+
27a23d2003-01-27Martin Nilsson  main_resolv( "Tools","Install","features" )()*"\n "+
70e2ca2003-01-01Martin Nilsson  "\n");
a6b6df2001-01-19Mirar (Pontus Hagland)  exit(0);
94c8552001-01-19Mirar (Pontus Hagland)  }
b4682a2003-02-26Martin Nilsson #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)  {
db77fa2002-12-14Martin Nilsson  if(run_tool) { werror("Pike -x specificed without tool name.\n" "Available tools:\n"); mapping t = ([]); int i;
27a23d2003-01-27Martin Nilsson  object ts = main_resolv( "Tools", "Standalone" );
6e2cfd2003-10-08Martin Stjernholm  foreach (indices(ts), string s) { mixed val = ts[s]; if (programp (val)) { object o = val(); if(!o->main) continue; t[s] = o->description || ""; i = max(i, sizeof(s)); }
db77fa2002-12-14Martin Nilsson  } foreach(sort(indices(t)), string s) werror(" %-"+i+"s %s\n", s, t[s]); exit(1); }
27a23d2003-01-27Martin Nilsson  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 {
27a23d2003-01-27Martin Nilsson  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) );
57b52d2003-05-31Martin Stjernholm  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
97f8142003-09-18Henrik Grubbström (Grubba)  if(trace) trace = predef::trace(trace);
5048622003-04-10Martin Nilsson  mixed ret;
f9a0962003-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.
41d0af2003-04-10Martin Nilsson  object script; if(Version(compat_major,compat_minor) <= Version(7,4)) { script=prog(); } else { script=prog(argv); }
f9a0962003-03-20Martin Stjernholm  if(!script->main) _error("Error: %s has no main().\n", argv[0]);
5048622003-04-10Martin Nilsson  ret=script->main(sizeof(argv),argv,env);
f9a0962003-03-20Martin Stjernholm  };
97f8142003-09-18Henrik Grubbström (Grubba)  predef::trace(trace);
f9a0962003-03-20Martin Stjernholm  if (err) { handle_error (err);
5048622003-04-10Martin Nilsson  ret = 10;
f9a0962003-03-20Martin Stjernholm  }
84d89c2003-04-15Martin Nilsson  if(!intp(ret)) { werror("Error: Non-integer value %O returned from main.\n", ret); exit(10); }
5048622003-04-10Martin Nilsson  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)
677d262003-11-14Martin Stjernholm  Builtin.__backend(3600.0);
87f9082001-03-12Fredrik Hübinette (Hubbe)  }; 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 += "/"; }
28351b2003-05-31Martin Stjernholm  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 
677d262003-11-14Martin Stjernholm //! This function is called whenever a compile error occurs. @[line] //! is zero for errors that aren't associated with any specific line. //! @[err] is not newline terminated.
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 
677d262003-11-14Martin Stjernholm //! This function is called whenever a compile warning occurs. @[line] //! is zero for warnings that aren't associated with any specific //! line. @[err] is not newline terminated.
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) {
23184f2003-04-30Martin Nilsson  ([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) &&
23184f2003-04-30Martin Nilsson  ( ([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;
677d262003-11-14Martin Stjernholm  if (objectp (trace) && ([object] trace)->is_compile_callback_error) // Errors thrown by a compile callback that we should report as a // normal compile error, so let the caller do just that. return 0;
8594c82001-08-15Martin Stjernholm  if (mixed val = get_inhibit_compile_errors()) {
23184f2003-04-30Martin Nilsson  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) 
23184f2003-04-30Martin Nilsson 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) {
23184f2003-04-30Martin Nilsson  object mod = [object]resolv("Locale");
f4f08d1999-02-26Henrik Grubbström (Grubba) 
23184f2003-04-30Martin Nilsson  _charset_mod = [object](mod && mod["Charset"]);
677d262003-11-14Martin Stjernholm  if (!_charset_mod) compile_cb_error ("Cannot handle charset - no Locale.Charset module found.");
f4f08d1999-02-26Henrik Grubbström (Grubba)  }
677d262003-11-14Martin Stjernholm  if (mixed err = catch { object decoder = ([function(string:object)]_charset_mod->decoder)(charset); return ([function(void:string)]([function(string:object)]decoder-> feed)(data)->drain)(); }) compile_cb_rethrow (err);
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]++)
23184f2003-04-30Martin Nilsson  identify_stack += indices([multiset]stuff);
9f71092000-11-20Martin Stjernholm  } else if (mappingp (stuff)) { if (!ident[stuff]++)
23184f2003-04-30Martin Nilsson  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)  {
23184f2003-04-30Martin Nilsson  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": case "float": return (string)m; case "string":
23184f2003-04-30Martin Nilsson  return describe_string ([string]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  case "array":
23184f2003-04-30Martin Nilsson  res = describe_array ([array]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "mapping":
23184f2003-04-30Martin Nilsson  res = describe_mapping ([mapping]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "multiset":
23184f2003-04-30Martin Nilsson  res = describe_multiset ([multiset]m, maxlen);
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "function":
23184f2003-04-30Martin Nilsson  if (string tmp=describe_function([function]m)) res = tmp;
0a064e2002-11-24Henrik Grubbström (Grubba)  break; case "program":
23184f2003-04-30Martin Nilsson  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) 
28351b2003-05-31Martin Stjernholm string program_path_to_name ( string path,
4ce9832003-06-05Martin Stjernholm  void|string module_prefix, void|string module_suffix, void|string object_suffix )
28351b2003-05-31Martin Stjernholm //! 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); };
28351b2003-05-31Martin Stjernholm  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  } }
28351b2003-05-31Martin Stjernholm  #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
e5ef062003-04-01Martin Nilsson //! 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
e5ef062003-04-01Martin Nilsson //! currently either @expr{"."@} or @expr{"->"@}.
be242d2002-11-23Henrik Grubbström (Grubba) string describe_module(object|program mod, array(object)|void ret_obj) {
2d01ff2003-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) { if (ret_obj) ret_obj[0] = mod; } else if (programp (mod)) { parent_fun = mod; if (objectp (mod = objects[parent_fun]) && ret_obj) ret_obj[0] = mod;
be242d2002-11-23Henrik Grubbström (Grubba)  }
2d01ff2003-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.
28351b2003-05-31Martin Stjernholm  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)||
c3b5642003-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 =
2d01ff2003-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) {
23184f2003-04-30Martin Nilsson  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.
2d01ff2003-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);
23184f2003-04-30Martin Nilsson  array(string) ind = [array(string)]indices(parent);
be242d2002-11-23Henrik Grubbström (Grubba)  for (i=0; i < sizeof(val); i++) {
2d01ff2003-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 //!
dd75be2003-02-26Martin Stjernholm string describe_object(object o)
bec57d1999-09-06Fredrik Hübinette (Hubbe) { string s;
90b03d2003-01-16Martin Stjernholm  if(zero_type (o)) return 0; // Destructed.
be242d2002-11-23Henrik Grubbström (Grubba) 
42825a2003-05-07Martin Stjernholm  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 {
c3b5642003-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. */
b38cb32003-09-09Martin Stjernholm  int i = search(values(parent_obj), o);
be242d2002-11-23Henrik Grubbström (Grubba)  if (i >= 0) {
23184f2003-04-30Martin Nilsson  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]))
28351b2003-05-31Martin Stjernholm  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 //!
23184f2003-04-30Martin Nilsson string describe_program(program|function p)
62e87b1998-04-14Fredrik Hübinette (Hubbe) { string s; if(!p) return 0;
d3a3322002-11-23Marcus Comstedt 
42825a2003-05-07Martin Stjernholm  if (p == object_program (_static_modules)) return "object_program(_static_modules)";
23184f2003-04-30Martin Nilsson  if(programp(p) &&
28351b2003-05-31Martin Stjernholm  (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) 
23184f2003-04-30Martin Nilsson  if(object|program tmp=(function_object(p) || function_program(p))) {
10ab772002-12-04Marcus Comstedt  if(s = function_name(p))
c84bd12003-01-13Henrik Grubbström (Grubba)  { return describe_module(tmp) + s; } }
e8ce5e2001-04-09Fredrik Hübinette (Hubbe) 
677d262003-11-14Martin Stjernholm  if(s=Builtin()->program_defined(p))
bec57d1999-09-06Fredrik Hübinette (Hubbe)  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;
57b52d2003-05-31Martin Stjernholm  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";
23184f2003-04-30Martin Nilsson  object o = function_object([function(mixed...:void|mixed)]f);
2d01ff2003-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
677d262003-11-14Martin Stjernholm //! Return a readable message that describes where the backtrace //! @[trace] was made (by @[backtrace]).
8f45692001-01-14Henrik Grubbström (Grubba) //!
677d262003-11-14Martin Stjernholm //! It may also be an error object or array (typically caught by a //! @[catch]), in which case the error message also is included in the //! description.
8f45692001-01-14Henrik Grubbström (Grubba) //! //! @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;
677d262003-11-14Martin 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 {
677d262003-11-14Martin Stjernholm  linewidth=[int]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)  }
677d262003-11-14Martin Stjernholm  // Note: Partial code duplication in describe_error and get_backtrace. if (objectp(trace) && ([object]trace)->is_generic_error) { object err_obj = [object] trace; if (mixed err = catch { if (functionp (err_obj->message)) ret = err_obj->message(); else if (zero_type (ret = err_obj->error_message)) // For compatibility with error objects trying to behave // like arrays. ret = err_obj[0]; if (!ret) ret = ""; else if (!stringp (ret)) ret = sprintf ("<Message in %O is %t, expected string>\n", err_obj, ret); if (functionp (err_obj->backtrace)) trace = err_obj->backtrace(); else if (zero_type (trace = err_obj->error_backtrace)) // For compatibility with error objects trying to behave // like arrays. trace = err_obj[1]; if (!trace) return ret + "<No backtrace>\n"; else if (!arrayp (trace)) return sprintf ("%s<Backtrace in %O is %t, expected array>\n", ret, err_obj, trace); }) return sprintf ("<Failed to index backtrace object %O: %s>\n", err_obj, trim_all_whites (describe_error (err))); } else if (arrayp(trace)) { if (sizeof([array]trace)==2 && stringp(ret = ([array]trace)[0])) { trace = ([array] trace)[1]; if(!trace) return ret + "<No backtrace>\n"; else if (!arrayp (trace)) return sprintf ("%s<Backtrace in error array is %t, expected array>\n", ret, trace);
a232721999-10-06Henrik Grubbström (Grubba)  }
677d262003-11-14Martin Stjernholm  } else return sprintf ("<Invalid backtrace/error container: %O>\n", trace);
ca2b071998-03-28Henrik Grubbström (Grubba)  {
da0a822000-08-29Martin Stjernholm  Describer desc = Describer(); desc->identify_parts (trace);
23184f2003-04-30Martin Nilsson  array trace = [array]trace;
da0a822000-08-29Martin Stjernholm 
9f71092000-11-20Martin Stjernholm  int end = 0; if( (sizeof(trace)>1) && arrayp(trace[0]) &&
23184f2003-04-30Martin Nilsson  (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;
513cbc2003-01-27Martin Nilsson  if (array err=[array]catch {
23184f2003-04-30Martin Nilsson  tmp = trace[e];
a232721999-10-06Henrik Grubbström (Grubba)  if(stringp(tmp))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
855b722003-04-29Martin Nilsson  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)  {
855b722003-04-29Martin Nilsson  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])) {
855b722003-04-29Martin Nilsson  pos=trim_file_name([string]tmp[0])+":"+(string)tmp[1];
dbe79a1999-12-07Henrik Grubbström (Grubba)  } else {
855b722003-04-29Martin Nilsson  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{
855b722003-04-29Martin Nilsson  string desc="Unknown program";
a232721999-10-06Henrik Grubbström (Grubba)  if(sizeof(tmp)>=3 && functionp(tmp[2])) {
e118831999-12-22Per Hedbor  catch {
23184f2003-04-30Martin Nilsson  if(mixed tmp=function_object([function(mixed...: void|mixed)]tmp[2]))
a232721999-10-06Henrik Grubbström (Grubba)  if(tmp=object_program(tmp))
855b722003-04-29Martin Nilsson  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])) {
855b722003-04-29Martin Nilsson  data = describe_function ([function]tmp[2]);
dfca1f2000-08-22Martin Stjernholm  }
ba20b11999-10-15Fredrik Hübinette (Hubbe)  else if (stringp(tmp[2])) {
855b722003-04-29Martin Nilsson  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) //!
677d262003-11-14Martin Stjernholm //! Return the error message from an error object or array (typically //! caught by a @[catch]). //! //! If an error message couldn't be obtained, a fallback message //! describing the failure is returned. No errors due to incorrectness //! in @[err] are thrown.
8f45692001-01-14Henrik Grubbström (Grubba) //! //! @seealso
677d262003-11-14Martin Stjernholm //! @[describe_backtrace()], @[get_backtrace]
8f45692001-01-14Henrik Grubbström (Grubba) //!
677d262003-11-14Martin Stjernholm string describe_error (object|array err)
a66ff22000-01-11Martin Stjernholm {
677d262003-11-14Martin Stjernholm  mixed msg; // Note: Partial code duplication in describe_backtrace and get_backtrace. if (objectp(err) && ([object]err)->is_generic_error) { object err_obj = [object] err; if (mixed err = catch { if (functionp (err_obj->message)) msg = err_obj->message(); else if (zero_type (msg = err_obj->error_message)) // For compatibility with error objects trying to behave // like arrays. msg = err_obj[0]; if (stringp (msg)) return msg; else if (!msg) return "<No error message>\n"; else return sprintf ("<Message in %O is %t, expected string>\n", err_obj, msg); }) return sprintf ("<Failed to index error object %O: %s>\n", err_obj, trim_all_whites (describe_error (err)));
a66ff22000-01-11Martin Stjernholm  }
677d262003-11-14Martin Stjernholm  else if (arrayp(err) && sizeof([array]err)==2 && (!(msg = ([array]err)[0]) || stringp (msg))) return [string] msg || "<No error message>\n"; else return sprintf ("<Invalid error container: %O>\n", err); } //! @appears get_backtrace //! //! Return the backtrace array from an error object or array //! (typically caught by a @[catch]), or zero if there is none. Errors //! are thrown on if there are problems retrieving the backtrace. //! //! @seealso //! @[describe_backtrace()], @[describe_error()] //! array get_backtrace (object|array err) { array bt; // Note: Partial code duplication in describe_backtrace and describe_error. if (objectp(err) && ([object]err)->is_generic_error) { object err_obj = [object] err; if (functionp (err_obj->backtrace)) bt = err_obj->backtrace(); else if (zero_type (bt = err_obj->error_backtrace)) // For compatibility with error objects trying to behave like // arrays. bt = err_obj[1]; if (bt && !arrayp (bt)) error ("Backtrace in %O is %t, expected array.\n", err_obj, bt); } else if (arrayp(err) && sizeof([array]err)==2 && (!(bt = ([array]err)[1]) || arrayp (bt))) {} else if (err) error ("Invalid error container: %O\n", err); return bt;
a66ff22000-01-11Martin Stjernholm }
471ed91998-04-24Fredrik Hübinette (Hubbe) 
57b52d2003-05-31Martin Stjernholm #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.
4ce9832003-06-05Martin Stjernholm //! 'r' Look up with resolv().
57b52d2003-05-31Martin Stjernholm //! '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.
4ce9832003-06-05Martin Stjernholm //! 'm' Get module object in dirnode.
57b52d2003-05-31Martin Stjernholm //! '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) {
57b52d2003-05-31Martin Stjernholm  mixed encoded;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) 
57b52d2003-05-31Martin Stjernholm  static mapping(mixed:string) rev_constants = ([]); static mapping(mixed:string) rev_static_modules = ([]);
4ce9832003-06-05Martin Stjernholm  static array find_index (object|program parent, mixed child, array(object) module_object)
57b52d2003-05-31Martin Stjernholm  { array id; find_id: {
4ce9832003-06-05Martin Stjernholm  array inds = indices (parent), vals = values (parent);
57b52d2003-05-31Martin Stjernholm  int i = search (vals, child);
4ce9832003-06-05Martin Stjernholm  if (i >= 0 && parent[inds[i]] == child) { id = ({inds[i]}); ENC_MSG (" found as parent value with index %O\n", id[0]); }
57b52d2003-05-31Martin Stjernholm  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)
4ce9832003-06-05Martin Stjernholm  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]);
57b52d2003-05-31Martin Stjernholm  break find_id; }
4ce9832003-06-05Martin Stjernholm 
57b52d2003-05-31Martin Stjernholm  error ("Cannot find %O in %O.\n", child, parent);
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  } }
57b52d2003-05-31Martin Stjernholm  if (!stringp (id[0])) error ("Got nonstring index %O for %O in %O.\n", id[0], child, parent);
471ed91998-04-24Fredrik Hübinette (Hubbe) 
57b52d2003-05-31Martin Stjernholm  return id; }
471ed91998-04-24Fredrik Hübinette (Hubbe) 
4ce9832003-06-05Martin Stjernholm  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; } 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); return 0; } 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.
57b52d2003-05-31Martin Stjernholm  { ENC_MSG ("nameof (%t %O)\n", what, what); if (what == encoded) { ENC_MSG (" got the thing to encode - encoding recursively\n");
ff38752002-02-14Henrik Grubbström (Grubba)  return UNDEFINED;
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
57b52d2003-05-31Martin Stjernholm  if (string id = rev_constants[what]) ENC_RETURN (id); if (string id = rev_static_modules[what]) ENC_RETURN (id); if (objectp (what)) {
4ce9832003-06-05Martin Stjernholm  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");
677d262003-11-14Martin Stjernholm  object modules = Builtin.array_iterator (what->joined_modules);
4ce9832003-06-05Martin Stjernholm  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) {
57b52d2003-05-31Martin Stjernholm  if (prog == encoded) ENC_RETURN ("o"); if (string path = programs_reverse_lookup (prog)) {
4ce9832003-06-05Martin Stjernholm  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");
57b52d2003-05-31Martin Stjernholm #ifdef PIKE_MODULE_RELOC
4ce9832003-06-05Martin Stjernholm  ENC_RETURN ("o" + unrelocate_module (path));
57b52d2003-05-31Martin Stjernholm #else
4ce9832003-06-05Martin Stjernholm  ENC_RETURN ("o" + path);
57b52d2003-05-31Martin Stjernholm #endif
4ce9832003-06-05Martin Stjernholm  }
57b52d2003-05-31Martin Stjernholm  } } if (string path = fc_reverse_lookup (what)) {
4ce9832003-06-05Martin Stjernholm  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");
57b52d2003-05-31Martin Stjernholm #ifdef PIKE_MODULE_RELOC
4ce9832003-06-05Martin Stjernholm  ENC_RETURN ("f" + unrelocate_module (path));
57b52d2003-05-31Martin Stjernholm #else
4ce9832003-06-05Martin Stjernholm  ENC_RETURN ("f" + path);
57b52d2003-05-31Martin Stjernholm #endif
4ce9832003-06-05Martin Stjernholm  }
57b52d2003-05-31Martin Stjernholm  } if (what->_encode) { ENC_MSG (" object got _encode function - encoding recursively\n"); return UNDEFINED; }
4ce9832003-06-05Martin Stjernholm  if (function|program prog = object_program (what)) { ENC_MSG (" got program of object: %O\n", prog);
57b52d2003-05-31Martin Stjernholm  if (object|program parent = function_object (prog) || function_program (prog)) {
4ce9832003-06-05Martin Stjernholm  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);
57b52d2003-05-31Martin Stjernholm  if (!parent_name) { ENC_MSG (" inside the thing to encode - encoding recursively\n"); return UNDEFINED; } else {
4ce9832003-06-05Martin Stjernholm  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);
57b52d2003-05-31Martin Stjernholm  }
401fa02002-12-02Martin Stjernholm  }
4ce9832003-06-05Martin Stjernholm  }
57b52d2003-05-31Martin Stjernholm  error ("Failed to find name of unencodable object %O.\n", what);
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
57b52d2003-05-31Martin Stjernholm  if (programp (what) || functionp (what)) { if (string path = programs_reverse_lookup (what)) {
4ce9832003-06-05Martin Stjernholm  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");
57b52d2003-05-31Martin Stjernholm #ifdef PIKE_MODULE_RELOC
4ce9832003-06-05Martin Stjernholm  ENC_RETURN ("p" + unrelocate_module (path));
57b52d2003-05-31Martin Stjernholm #else
4ce9832003-06-05Martin Stjernholm  ENC_RETURN ("p" + path);
57b52d2003-05-31Martin Stjernholm #endif
4ce9832003-06-05Martin Stjernholm  }
57b52d2003-05-31Martin Stjernholm  } if (object|program parent = function_object (what) || function_program (what)) {
4ce9832003-06-05Martin Stjernholm  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);
57b52d2003-05-31Martin Stjernholm  if (!parent_name) { ENC_MSG (" inside the thing to encode - encoding recursively\n"); return UNDEFINED; }
4ce9832003-06-05Martin Stjernholm 
57b52d2003-05-31Martin Stjernholm  else {
4ce9832003-06-05Martin Stjernholm  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); }
57b52d2003-05-31Martin Stjernholm  else {
4ce9832003-06-05Martin Stjernholm  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);
57b52d2003-05-31Martin Stjernholm  } } } error ("Failed to find name of %t %O.\n", what, what);
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
57b52d2003-05-31Martin Stjernholm  // 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)
57b52d2003-05-31Martin Stjernholm  { 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)()); }
da494f2003-11-12Henrik Grubbström (Grubba)  static void create (void|mixed encoded)
57b52d2003-05-31Martin Stjernholm  //! 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)  {
57b52d2003-05-31Martin Stjernholm  unregistered = 0;
3593292003-09-08Henrik Grubbström (Grubba)  resolv_debug("register %s\n", fname);
57b52d2003-05-31Martin Stjernholm  programs[fname]=p; if (mkobj) DEC_RETURN (objectp (objects[p]) ? objects[p] : (objects[p]=__null_program()));
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
57b52d2003-05-31Martin Stjernholm  DEC_RETURN (0); }
b4718a2003-05-31Martin Stjernholm  static mixed thingof (string|array what)
57b52d2003-05-31Martin Stjernholm  { mixed res;
b4718a2003-05-31Martin Stjernholm  array sublist;
57b52d2003-05-31Martin Stjernholm  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;
4ce9832003-06-05Martin Stjernholm  case 'r': if (zero_type (res = resolv (what[1..]))) error ("Cannot resolve %O.\n", what[1..]); break;
57b52d2003-05-31Martin Stjernholm  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; }
4ce9832003-06-05Martin Stjernholm  DEC_MSG (" got %O\n", res);
57b52d2003-05-31Martin Stjernholm  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]);
4ce9832003-06-05Martin Stjernholm  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);
57b52d2003-05-31Martin Stjernholm  } } res = subres; } return res; }
b4718a2003-05-31Martin Stjernholm  object objectof (string|array what)
57b52d2003-05-31Martin Stjernholm  { DEC_MSG ("objectof (%O)\n", what);
47a9da2003-07-30Martin Stjernholm  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 werror ("Warning: Decoded broken object identifier to zero.\n"); DEC_RETURN (0); }
4ce9832003-06-05Martin Stjernholm  DEC_RETURN ([object] thingof (what));
57b52d2003-05-31Martin Stjernholm  }
b4718a2003-05-31Martin Stjernholm  function functionof (string|array what)
57b52d2003-05-31Martin Stjernholm  { DEC_MSG ("functionof (%O)\n", what);
4ce9832003-06-05Martin Stjernholm  DEC_RETURN ([function] thingof (what));
57b52d2003-05-31Martin Stjernholm  }
b4718a2003-05-31Martin Stjernholm  program programof (string|array what)
57b52d2003-05-31Martin Stjernholm  { DEC_MSG ("programof (%O)\n", what);
4ce9832003-06-05Martin Stjernholm  DEC_RETURN ([program] thingof (what));
57b52d2003-05-31Martin Stjernholm  }
d28a712001-07-02Henrik Grubbström (Grubba)  void decode_object(object o, mixed data)
471ed91998-04-24Fredrik Hübinette (Hubbe)  {
57b52d2003-05-31Martin Stjernholm  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));
855b722003-04-29Martin Nilsson  ([function(mixed:void)]o->_decode)(data);
471ed91998-04-24Fredrik Hübinette (Hubbe)  } }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
f2d24d2003-06-05Martin Stjernholm 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); }
57b52d2003-05-31Martin Stjernholm 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.
11f0ae2003-02-27Henrik Grubbström (Grubba) {
57b52d2003-05-31Martin Stjernholm  inherit Encoder; inherit Decoder;
11f0ae2003-02-27Henrik Grubbström (Grubba) 
da494f2003-11-12Henrik Grubbström (Grubba)  static void create (void|mixed encoded)
57b52d2003-05-31Martin Stjernholm  //! The optional argument is the thing to encode; it's passed on to //! @[Encoder]. { Encoder::create (encoded); } }
11f0ae2003-02-27Henrik Grubbström (Grubba) 
4ce9832003-06-05Martin Stjernholm // The master acts as the default codec. inherit Codec;
11f0ae2003-02-27Henrik Grubbström (Grubba) 
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.
da494f2003-11-12Henrik Grubbström (Grubba)  static void create(int maj, int min)
6df5a52001-11-07Martin Nilsson  {
41d0af2003-04-10Martin Nilsson  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) 
513cbc2003-01-27Martin Nilsson #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.
513cbc2003-01-27Martin Nilsson  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 
97f8142003-09-18Henrik Grubbström (Grubba) mapping(Version:CompatResolver) compat_handler_cache = set_weak_flag( ([]), 1);
a20af62000-09-26Fredrik Hübinette (Hubbe)  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; }
3593292003-09-08Henrik Grubbström (Grubba)  CompatResolver ret;
a20af62000-09-26Fredrik Hübinette (Hubbe)  if(!zero_type(ret=compat_handler_cache[v])) return ret;
3593292003-09-08Henrik 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
3593292003-09-08Henrik 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)  {
3593292003-09-08Henrik Grubbström (Grubba)  CompatResolver compat_handler = compat_handler_cache[tmp]; if (!compat_handler) {
97f8142003-09-18Henrik Grubbström (Grubba)  // Create a new compat handler, that // falls back to the successor version. if (tmp <= Version(0, 6)) { compat_handler = Pike06Resolver(tmp, ret); } else { compat_handler = CompatResolver(tmp, ret); }
3593292003-09-08Henrik Grubbström (Grubba)  string base;
a20af62000-09-26Fredrik Hübinette (Hubbe) #if "¤lib_prefix¤"[0]!='¤'
3593292003-09-08Henrik 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]!='¤'
3593292003-09-08Henrik 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 #ifndef RESOLVER_HACK
3593292003-09-08Henrik Grubbström (Grubba)  ret = compat_handler_cache[tmp] = compat_handler;
a20af62000-09-26Fredrik Hübinette (Hubbe) #endif
3593292003-09-08Henrik 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) }