a2faf61999-12-01Martin Stjernholm /* -*- Pike -*- *
ddd2af2001-12-29Martin Nilsson  * $Id: master.pike.in,v 1.186 2001/12/29 22:53:23 nilsson Exp $
ce8aee1998-04-21Per Hedbor  *
ca2b071998-03-28Henrik Grubbström (Grubba)  * Master-file for Pike. * * Based on master.pike 1.67. */
c896b41998-04-29Henrik Grubbström (Grubba) // Some configurable parameters useful for debugging
a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
bec57d1999-09-06Fredrik Hübinette (Hubbe) #define PIKE_AUTORELOAD
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 //! This constant contains the maximum number of lines a backtrace may //! contain. Defaults to 200 if no BT_MAX_STRING_LEN define has been //! given.
97e5c72001-07-28Martin Nilsson 
e115cb1999-12-09Henrik Grubbström (Grubba) // Enables the out of date warning in low_find_prog(). #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;
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 
df2c632000-08-02Henrik Grubbström (Grubba) #ifndef PIKE_WARNINGS
f71a712001-01-15Fredrik Hübinette (Hubbe) #define PIKE_WARNINGS 0
df2c632000-08-02Henrik Grubbström (Grubba) #endif /* PIKE_WARNINGS */
4839dd2001-09-02Marcus Comstedt #undef PIKE_MODULE_RELOC
c896b41998-04-29Henrik Grubbström (Grubba) /* * Functions begin here. */
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 foreach(PIKE_FAKEROOT_OMIT/":", string x) if(glob(x,tmp1)) return s; #endif return PIKE_FAKEROOT+tmp1; } #else #define fakeroot(X) X #endif
4839dd2001-09-02Marcus Comstedt #ifdef PIKE_MODULE_RELOC string relocate_module(string s) { if(s[..1]=="/$" && (s+"/")[..20] == "/${PIKE_MODULE_PATH}/") { 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) { if(s[..1]=="/$" && (s+"/")[..20] == "/${PIKE_MODULE_PATH}/") return s; foreach(pike_module_path, string path) if(s == path) return "/${PIKE_MODULE_PATH}"; else { string s2 = combine_path(path, ""); if(s[..sizeof(s2)-1] == s2) return "/${PIKE_MODULE_PATH}/"+s[sizeof(s2)..]; } return s; } #ifdef fakeroot #undef fakeroot #endif #define fakeroot relocate_module #endif
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 #define IS_ABSOLUTE_PATH(X) ((X)[0]=='/')
ca2b071998-03-28Henrik Grubbström (Grubba) #endif return p[0]=='/'; }
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) { #ifdef __NT__ p=replace(p,"\\","/");
471ed91998-04-24Fredrik Hübinette (Hubbe) #define EXPLODE_PATH(X) (replace((X),"\\","/")/"/") #else #define EXPLODE_PATH(X) ((X)/"/")
ca2b071998-03-28Henrik Grubbström (Grubba) #endif return p/"/"; }
b07e962001-07-27Martin Nilsson //! @appears dirname
8f45692001-01-14Henrik Grubbström (Grubba) //! Returns all but the last segment of a path. //! //! @seealso //! @[basename()], @[explode_path()]
ca2b071998-03-28Henrik Grubbström (Grubba) string dirname(string x) {
afa1292000-02-19Martin Nilsson  array(string) tmp=EXPLODE_PATH(x);
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 DIRNAME dirname #define BASENAME(X) (EXPLODE_PATH(X)[-1])
ca2b071998-03-28Henrik Grubbström (Grubba)  #define GETCWD_CACHE #define FILE_STAT_CACHE #define UNDEFINED (([])[0])
dbe79a1999-12-07Henrik Grubbström (Grubba) #define error(X) throw( ({ (X), backtrace()/*[0..sizeof(backtrace())-2]*/ }) )
ca2b071998-03-28Henrik Grubbström (Grubba) 
97e5c72001-07-28Martin Nilsson //!
df2c632000-08-02Henrik Grubbström (Grubba) int want_warnings = PIKE_WARNINGS;
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) \ if(autoreload_on) if(mixed fnord=master_file_stat(X)) if(fnord[3]>newest) newest=fnord[3]; #define AUTORELOAD_BEGIN() \ int ___newest=newest; \ newest=0 #define AUTORELOAD_FINISH(VAR, CACHE, FILE) \ if(autoreload_on) { \ if(CACHE [ FILE ] && newest <= load_time[FILE]) { \ VAR = CACHE [ FILE ]; \ } \ } \ load_time[FILE]=time(); \ if(___newest > newest) newest=___newest; mapping(string:int) load_time=([]); #else #define AUTORELOAD_CHECK_FILE(X) #define AUTORELOAD_BEGIN() #define AUTORELOAD_FINISH(VAR,CACHE,FILE) #endif
97e5c72001-07-28Martin Nilsson //!
a20af62000-09-26Fredrik Hübinette (Hubbe) int compat_major=-1;
97e5c72001-07-28Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe) int compat_minor=-1;
b07e962001-07-27Martin Nilsson //! @appears compile_string
56cd002001-10-28Martin Nilsson //! Compile the Pike code in the string @[source] into a program.
8f45692001-01-14Henrik Grubbström (Grubba) //! If @[filename] is not specified, it will default to @tt{"-"@}. //! //! Functionally equal to @code{@[compile](@[cpp](@[source], @[filename]))@}. //! //! @seealso //! @[compile()], @[cpp()], @[compile_file()] //! program compile_string(string source, void|string filename, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) {
d2777c2001-03-26Henrik Grubbström (Grubba)  return compile(cpp(source, filename||"-", 1, handler,
8f45692001-01-14Henrik Grubbström (Grubba)  compat_major, compat_minor),
a20af62000-09-26Fredrik Hübinette (Hubbe)  handler, compat_major, compat_minor);
ca2b071998-03-28Henrik Grubbström (Grubba) }
97e5c72001-07-28Martin Nilsson //!
d8a6fc2000-03-25Fredrik Hübinette (Hubbe) string master_read_file(string file) { object o=_static_modules.files()->Fd();
26f3da2000-07-11Fredrik Hübinette (Hubbe)  if(o->open(fakeroot(file),"r"))
d8a6fc2000-03-25Fredrik Hübinette (Hubbe)  return o->read(); return 0; }
ca2b071998-03-28Henrik Grubbström (Grubba) #ifdef GETCWD_CACHE string current_path; int cd(string s) { current_path=0; return predef::cd(s); } string getcwd() { return current_path || (current_path=predef::getcwd()); } #endif 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) }
fc398b2001-05-31Fredrik Hübinette (Hubbe) #define Stat _static_modules.files.Stat
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) { string file, dir=combine_path_with_cwd(x);
471ed91998-04-24Fredrik Hübinette (Hubbe)  file=BASENAME(dir); dir=DIRNAME(dir);
ca2b071998-03-28Henrik Grubbström (Grubba)  multiset(string) d; if(time() > invalidate_time) { dir_cache=([]); invalidate_time=time()+FILE_STAT_CACHE_TIME; } if(zero_type(d=dir_cache[dir])) {
afa1292000-02-19Martin Nilsson  if(array(string) tmp=get_dir(dir))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
fc398b2001-05-31Fredrik Hübinette (Hubbe) #ifdef __NT__ tmp=map(tmp, lower_case); #endif
ca2b071998-03-28Henrik Grubbström (Grubba)  d=dir_cache[dir]=aggregate_multiset(@tmp); }else{ dir_cache[dir]=0; } }
fc398b2001-05-31Fredrik Hübinette (Hubbe) #ifdef __NT__ file=lower_case(file); #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 #endif
a71f832001-11-13Tomas Nilsson 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
8f45692001-01-14Henrik Grubbström (Grubba) //! variables will be returned. //! //! If the @[varname] argument has been given, the value of the environment //! variable with the name @[varname] will be returned. If no such //! environment variable exists, @tt{0@} (zero) will be returned. //!
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) {
a71f832001-11-13Tomas Nilsson  if(!s) return aggregate_mapping( @(values(environment)*({}) ) ); #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 //! @code{@[compile_string](@[Stdio.read_file](@[filename]), @[filename])@}. //! //! @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 //! adviced to use @[system.normalize_path] instead. 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)  mapping (string:program) programs=(["/master":object_program(this_object())]); #define capitalize(X) (upper_case((X)[..0])+(X)[1..])
6d926c1999-11-26Henrik Grubbström (Grubba) array(string) query_precompiled_names(string fname) { // Filenames of potential precompiled files in priority order. return ({ fname + ".o" }); }
6330192000-02-10Fredrik Hübinette (Hubbe) #if constant(_static_modules.Builtin.mutex) #define THREADED object compilation_mutex = _static_modules.Builtin()->mutex(); #endif
aa68b12001-03-19Fredrik Hübinette (Hubbe) static program low_findprog(string pname, string ext, object|void handler, void|int mkobj)
ca2b071998-03-28Henrik Grubbström (Grubba) { program ret;
61a4242000-08-27Mirar (Pontus Hagland)  Stat s;
ca2b071998-03-28Henrik Grubbström (Grubba)  string fname=pname+ext;
bec57d1999-09-06Fredrik Hübinette (Hubbe) 
6330192000-02-10Fredrik Hübinette (Hubbe) #ifdef THREADED
3b11422000-02-13Henrik Grubbström (Grubba)  object key; // FIXME: The catch is needed, since we might be called in // a context when threads are disabled. // (compile() disables threads).
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed err = catch {
3b11422000-02-13Henrik Grubbström (Grubba)  key=compilation_mutex->lock(2); };
098c8a2000-03-30Henrik Grubbström (Grubba)  if (err) { werror(sprintf("low_findprog: Caught spurious error:\n" "%s\n", describe_backtrace(err))); }
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 {
6e2ebb1999-12-28Martin Stjernholm  if(!zero_type (ret=programs[fname])) 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
26f3da2000-07-11Fredrik Hübinette (Hubbe)  if( (s=master_file_stat(fakeroot(fname))) && s[1]>=0 )
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
14fc641999-11-26Henrik Grubbström (Grubba)  if (load_time[fname] > s[3])
6e2ebb1999-12-28Martin Stjernholm  if (!zero_type (ret=programs[fname])) return ret; #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)))
6d926c1999-11-26Henrik Grubbström (Grubba)  { if(s2[1]>=0 && s2[3]>=s[3]) { mixed err=catch { AUTORELOAD_CHECK_FILE(oname); return programs[fname] =
d8a6fc2000-03-25Fredrik Hübinette (Hubbe)  decode_value(master_read_file(oname),
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  Codec(fname,mkobj));
6d926c1999-11-26Henrik Grubbström (Grubba)  };
5d6a562001-11-08Fredrik Hübinette (Hubbe)  m_delete(programs, fname); if (handler && handler->compile_warning) {
6d926c1999-11-26Henrik Grubbström (Grubba)  handler->compile_warning(oname, 0, sprintf("Decode failed:\n"
88a2fa2001-08-16Martin Stjernholm  "\t%s", describe_error(err)));
6d926c1999-11-26Henrik Grubbström (Grubba)  } else { compile_warning(oname, 0, sprintf("Decode failed:\n"
88a2fa2001-08-16Martin Stjernholm  "\t%s", describe_error(err)));
6d926c1999-11-26Henrik Grubbström (Grubba)  }
e115cb1999-12-09Henrik Grubbström (Grubba)  } else if (out_of_date_warning) {
6d926c1999-11-26Henrik Grubbström (Grubba)  if (handler) { handler->compile_warning(oname, 0, "Compiled file is out of date\n"); } else { compile_warning(oname, 0, "Compiled file is out of date\n"); }
10e16f1999-11-04Henrik Grubbström (Grubba)  }
b085961998-05-19Fredrik Hübinette (Hubbe)  }
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
bec57d1999-09-06Fredrik Hübinette (Hubbe) 
6330192000-02-10Fredrik Hübinette (Hubbe) // werror("2.5: %O\n",fname);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  programs[fname]=ret=__empty_program(); if ( mixed e=catch { ret=compile_file(fname,
34241f2001-05-24Henrik Grubbström (Grubba)  handler,
aa68b12001-03-19Fredrik Hübinette (Hubbe)  ret, mkobj? (objects[ret]=__null_program()) : 0); } )
ca2b071998-03-28Henrik Grubbström (Grubba)  {
6330192000-02-10Fredrik Hübinette (Hubbe) // werror("-3: %O\n",fname);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  ret=programs[fname]=0;
c46d682000-01-05Martin Stjernholm  throw(e);
ca2b071998-03-28Henrik Grubbström (Grubba)  } break; #if constant(load_module) case ".so":
caa3221998-04-15Henrik Grubbström (Grubba)  if (fname == "") {
f645f02001-10-26Henrik Grubbström (Grubba)  werror(sprintf("low_findprog(\"%s\", \"%s\") => load_module(\"\")\n"
caa3221998-04-15Henrik Grubbström (Grubba)  "%s\n", pname, ext, describe_backtrace(backtrace()))); }
26f3da2000-07-11Fredrik Hübinette (Hubbe)  ret=load_module(fakeroot(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 
aa68b12001-03-19Fredrik Hübinette (Hubbe) // werror("3: %O (%O)\n",fname, ret);
ca2b071998-03-28Henrik Grubbström (Grubba)  return programs[fname]=ret; }
6e2ebb1999-12-28Martin Stjernholm  return 0;
ca2b071998-03-28Henrik Grubbström (Grubba) }
eaa4da2001-10-04Fredrik Hübinette (Hubbe) // // This function is called by the compiler // when a delayed compilation error occurs // void unregister(program p) { if(string fname=search(programs,p)) m_delete(programs, fname);
c6cf602001-12-13Martin Stjernholm  if (zero_type(objects[p]) != 1) m_delete(objects, p); foreach (fc; string name; mixed mod) if (objectp(mod) && object_program(mod) == p) m_delete(fc, name);
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*"/"; }else{ ext=""; }
471ed91998-04-24Fredrik Hübinette (Hubbe)  if(IS_ABSOLUTE_PATH(pname))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
941de62001-06-21Marcus Wellhardh  if (programs[pname]) return programs[pname];
ca2b071998-03-28Henrik Grubbström (Grubba)  pname=combine_path("/",pname);
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return findprog(pname,ext,handler,mkobj);
ca2b071998-03-28Henrik Grubbström (Grubba)  }else{ string cwd; if(current_file) {
471ed91998-04-24Fredrik Hübinette (Hubbe)  cwd=DIRNAME(current_file);
ca2b071998-03-28Henrik Grubbström (Grubba)  }else{ 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) { return low_cast_to_program(pname, current_file, handler); }
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 { string msg = x[0]; array bt = x[1]; 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) }
8f45692001-01-14Henrik Grubbström (Grubba) //! @decl object new(string|program prog, mixed ... args)
b07e962001-07-27Martin Nilsson //! @appears new
8f45692001-01-14Henrik Grubbström (Grubba) //! //! Instantiate a program. //! //! A new instance of the class @[prog] will be created. //! All global variables in the new object be initialized, and //! then @[create()] will be called with @[args] as arguments. //! //! @note //! These two functions are considered obsolete, use //! @code{((program)@[prog])(@@@[args])@} //! instead. //! //! @seealso //! @[destruct()], @[compile_string()], @[compile_file()] //! object new(string|program prog, mixed ... args)
ca2b071998-03-28Henrik Grubbström (Grubba) { if(stringp(prog))
67276c1999-04-08Fredrik Hübinette (Hubbe)  { if(program p=cast_to_program(prog,backtrace()[-2][0])) return p(@args); else error(sprintf("new: failed to find program %s.\n",prog)); }
ca2b071998-03-28Henrik Grubbström (Grubba)  return prog(@args); }
b07e962001-07-27Martin Nilsson //! @decl object clone(string|program prog, mixed ... args) //! @appears clone //! //! @seealso //! @[new()]
c5cc2d1998-04-29Henrik Grubbström (Grubba) function clone = new; /* This array contains the names of the functions * that a replacing master-object may want to override. */ constant master_efuns = ({ "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",
c5cc2d1998-04-29Henrik Grubbström (Grubba)  "new", "clone",
4c3f7d2000-04-13Per Hedbor  "normalize_path",
c5cc2d1998-04-29Henrik Grubbström (Grubba)  "getenv", "putenv", #ifdef GETCWD_CACHE "cd", "getcwd", #endif });
ca2b071998-03-28Henrik Grubbström (Grubba) /* Note that create is called before add_precompiled_program */ void create() {
c5cc2d1998-04-29Henrik Grubbström (Grubba)  object o = this_object();
ca2b071998-03-28Henrik Grubbström (Grubba) 
c5cc2d1998-04-29Henrik Grubbström (Grubba)  foreach(master_efuns, string e) { if (o[e]) { add_constant(e, o[e]); } else { throw(({ sprintf("Function %O is missing from master.pike.\n", e), backtrace() })); } } add_constant("strlen", sizeof); add_constant("UNDEFINED", UNDEFINED); add_constant("write", _static_modules.files()->_stdout->write);
ca2b071998-03-28Henrik Grubbström (Grubba) 
87f9082001-03-12Fredrik Hübinette (Hubbe) #define CO(X) add_constant(#X,_static_modules.Builtin.__backend->X) CO(call_out); CO(_do_call_outs); CO(find_call_out); CO(remove_call_out); CO(call_out_info);
e37a3e1999-10-09Fredrik Hübinette (Hubbe) #if "¤share_prefix¤"[0]!='¤' // add path for architecture-dependant files 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
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) {
10e16f1999-11-04Henrik Grubbström (Grubba)  return cast_to_program(pname, current_file, handler);
ca2b071998-03-28Henrik Grubbström (Grubba) } mapping (program:object) objects=([object_program(this_object()):this_object()]);
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; if(!(o=objects[p])) o=objects[p]=p(); 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) { if(object o=low_cast_to_object(oname, current_file)) return o;
ac0fed2000-03-25Fredrik Hübinette (Hubbe)  error("Cast '"+oname+"' to object failed"+
b1693a2000-04-07Fredrik Hübinette (Hubbe)  ((current_file && current_file!="-")?sprintf(" for '%s'",current_file):"")+".\n");
ac0fed2000-03-25Fredrik Hübinette (Hubbe)  return 0;
ca2b071998-03-28Henrik Grubbström (Grubba) }
eaa4da2001-10-04Fredrik Hübinette (Hubbe) class dirnode
ca2b071998-03-28Henrik Grubbström (Grubba) {
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  string dirname; object|void handler;
4817482000-05-11Martin Stjernholm  constant is_resolv_dirnode = 1;
14bb592000-05-06Fredrik Hübinette (Hubbe)  mixed module=module_checker();
098c8a2000-03-30Henrik Grubbström (Grubba)  mapping(string:mixed) cache=([]); array(string) files;
ca2b071998-03-28Henrik Grubbström (Grubba) 
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  void create(string d, object|void h) {
acd1632001-12-17Henrik Grubbström (Grubba) #ifdef MODULE_TRACE werror("%*ndirnode(%O,%O);\n",sizeof(backtrace())-1,d,h); // werror(describe_backtrace( ({"HERE\n",backtrace()}))); #endif
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  dirname=d; handler=h; fc[dirname]=this_object(); if(dirname[sizeof(dirname)-5..]==".pmod") fc[dirname[..sizeof(dirname)-6]]=this_object(); }
14bb592000-05-06Fredrik Hübinette (Hubbe)  class module_checker { int `!() {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*ndirnode(%O)->module_checker()->`!()\n",sizeof(backtrace()),dirname); #endif
fd4cb12001-12-16Martin Stjernholm  if(module=findmodule(dirname+"/module", handler)) { if(mixed tmp=module->_module_value) module=tmp; /* This allows for `[] to have side effects first time * it is called. (Specifically, the Calendar module uses * this */ cache=([]); _cache_full=0;
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  }
5d6a562001-11-08Fredrik Hübinette (Hubbe) 
14bb592000-05-06Fredrik Hübinette (Hubbe)  return !module; }
52c5792000-05-23Fredrik Hübinette (Hubbe) 
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  mixed `[](string index)
cd99712001-11-09Martin Stjernholm  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*ndirnode(%O)->module_checker()[%O]\n",sizeof(backtrace()),dirname,index); #endif if(module) return module[index]; }
52c5792000-05-23Fredrik Hübinette (Hubbe)  array(string) _indices() { if(module) return indices(module); } array _values() { if(module) return values(module); }
ca2b071998-03-28Henrik Grubbström (Grubba)  }
098c8a2000-03-30Henrik Grubbström (Grubba)  static mixed ind(string index)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nDirnode(%O) ind[%O] -> ???\n",sizeof(backtrace()),dirname,index); #endif
e118831999-12-22Per Hedbor  if(module) {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nDirnode(%O) module[%O] -> ???\n",sizeof(backtrace()),dirname,index); #endif
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])) {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nDirnode(%O) module[%O] -> %O\n",sizeof(backtrace()),dirname,index, o); #endif
aa68b12001-03-19Fredrik Hübinette (Hubbe)  return o; }
a2faf61999-12-01Martin Stjernholm  }
ca2b071998-03-28Henrik Grubbström (Grubba) 
e118831999-12-22Per Hedbor  if( !files )
5be4aa2001-11-12Martin Stjernholm  if (!(files = get_dir(fakeroot(dirname)))) { werror ("Error listing module directory %O: %s\n", dirname, strerror (errno())); files = ({}); }
e118831999-12-22Per Hedbor  int ret; foreach( files, string s ) { if( search(s, index)!=-1 || search(index,s)!=-1 ) { ret=1; break; } } if(!ret) return UNDEFINED;
ca2b071998-03-28Henrik Grubbström (Grubba)  index = dirname+"/"+index;
5d6a562001-11-08Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nDirnode(%O) findmodule(%O)\n", sizeof(backtrace()), dirname, dirname+"/"+index); #endif
34241f2001-05-24Henrik Grubbström (Grubba)  if(object o=findmodule(index, handler))
ca2b071998-03-28Henrik Grubbström (Grubba)  { if(mixed tmp=o->_module_value) o=tmp; return o; }
34241f2001-05-24Henrik Grubbström (Grubba)  if (program p=cast_to_program( index, 0, handler ))
aa68b12001-03-19Fredrik Hübinette (Hubbe)  { // werror("dirnode(%O)[%O] -> %O\n",dirname,index,p); return p; }
2c0b292001-03-20Fredrik Hübinette (Hubbe) // werror("Returning UNDEFINED for %s\n",ind);
73e9551999-12-28Martin Stjernholm  return UNDEFINED;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed `[](string index)
ca2b071998-03-28Henrik Grubbström (Grubba)  { mixed ret;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nDirnode(%O) cache[%O] ?????\n",sizeof(backtrace()),dirname,index); #endif if(!zero_type(ret=cache[index]))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nDirnode(%O) cache[%O] -> %O\n",sizeof(backtrace()),dirname,index, ret); #endif
cd99712001-11-09Martin Stjernholm  if (ret != ZERO_TYPE) return ret;
ca2b071998-03-28Henrik Grubbström (Grubba)  return UNDEFINED; }
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  ret=ind(index); // Kluge! if(ret == __placeholder_object) return ret;
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) 
098c8a2000-03-30Henrik Grubbström (Grubba)  static int _cache_full;
3e4b811998-10-16Henrik Grubbström (Grubba)  void fill_cache() {
f4f08d1999-02-26Henrik Grubbström (Grubba) #ifdef RESOLV_DEBUG werror(describe_backtrace(({ sprintf("Filling cache in dirnode %O\n", dirname), backtrace() }))); #endif /* RESOLV_DEBUG */
3e4b811998-10-16Henrik Grubbström (Grubba)  if (_cache_full) { return; } if (module) { foreach(indices(module), string index) { cache[index] = module[index]; } }
e118831999-12-22Per Hedbor  if( !files )
5be4aa2001-11-12Martin Stjernholm  if (!(files = get_dir(fakeroot(dirname)))) { werror ("Error listing module directory %O: %s\n", dirname, strerror (errno())); files = ({}); }
e118831999-12-22Per Hedbor  foreach(files, string fname) {
0e68e62000-02-04Henrik Grubbström (Grubba)  mixed err = catch {
3e4b811998-10-16Henrik Grubbström (Grubba)  if (((< ".pike", ".pmod" >)[fname[sizeof(fname)-5..]]) && !zero_type(`[](fname[..sizeof(fname)-6]))) { continue;
0e68e62000-02-04Henrik Grubbström (Grubba)  } else if ((fname[sizeof(fname)-3..] == ".so") &&
3e4b811998-10-16Henrik Grubbström (Grubba)  !zero_type(`[](fname[..sizeof(fname)-4]))) { continue; } };
0e68e62000-02-04Henrik Grubbström (Grubba)  if (err) { compile_warning(dirname+"."+fname, 0, sprintf("Compilation failed:\n" "%s\n", describe_backtrace(err))); }
3e4b811998-10-16Henrik Grubbström (Grubba)  } _cache_full = 1; } array(string) _indices() {
098c8a2000-03-30Henrik Grubbström (Grubba)  // werror("indices(%O) called\n", dirname);
3e4b811998-10-16Henrik Grubbström (Grubba)  fill_cache();
cd99712001-11-09Martin Stjernholm  return indices(filter(cache, lambda(mixed x) {return cache[x] != ZERO_TYPE;}));
3e4b811998-10-16Henrik Grubbström (Grubba)  } array(mixed) _values() {
098c8a2000-03-30Henrik Grubbström (Grubba)  // werror("values(%O) called\n", dirname);
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)  string _sprintf(int as) { return sprintf("master()->dirnode(%O)",dirname); }
ca2b071998-03-28Henrik Grubbström (Grubba) };
6e2ebb1999-12-28Martin Stjernholm static class ZERO_TYPE {};
94257b1998-10-16Henrik Grubbström (Grubba) class joinnode {
4817482000-05-11Martin Stjernholm  constant is_resolv_joinnode = 1;
94257b1998-10-16Henrik Grubbström (Grubba)  array(object|mapping) joined_modules;
098c8a2000-03-30Henrik Grubbström (Grubba)  mapping(string:mixed) cache=([]);
94257b1998-10-16Henrik Grubbström (Grubba) 
52c5792000-05-23Fredrik Hübinette (Hubbe)  string _sprintf(int as) { return sprintf("master()->joinnode(%O)",joined_modules); }
94257b1998-10-16Henrik Grubbström (Grubba)  void create(array(object|mapping) _joined_modules) { joined_modules = _joined_modules; }
098c8a2000-03-30Henrik Grubbström (Grubba)  static mixed ind(string index)
94257b1998-10-16Henrik Grubbström (Grubba)  { array(mixed) res = ({});
ca051c1999-11-23Per Hedbor  foreach(joined_modules, object|mapping o) {
94257b1998-10-16Henrik Grubbström (Grubba)  mixed ret;
ca051c1999-11-23Per Hedbor  if (!zero_type(ret = o[index])) {
8b307e1998-10-17Henrik Grubbström (Grubba)  if (objectp(ret = o[index]) &&
3ad5d62000-05-13Martin Stjernholm  (ret->is_resolv_dirnode || ret->is_resolv_joinnode))
ca051c1999-11-23Per Hedbor  {
8b307e1998-10-17Henrik Grubbström (Grubba)  // Only join directorynodes (or joinnodes). res += ({ ret });
8cdeed2000-09-15Mirar (Pontus Hagland)  } else if ( !zero_type(ret) ) {
ca051c1999-11-23Per Hedbor  return (ret);
8b307e1998-10-17Henrik Grubbström (Grubba)  } else { // Ignore continue; }
94257b1998-10-16Henrik Grubbström (Grubba)  } }
ca051c1999-11-23Per Hedbor  if (sizeof(res) > 1) return joinnode(res); else if (sizeof(res))
94257b1998-10-16Henrik Grubbström (Grubba)  return res[0]; return UNDEFINED; }
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed `[](string index)
94257b1998-10-16Henrik Grubbström (Grubba)  {
098c8a2000-03-30Henrik Grubbström (Grubba)  mixed ret;
94257b1998-10-16Henrik Grubbström (Grubba)  if (!zero_type(ret = cache[index])) { if (ret != ZERO_TYPE) { return ret; } return UNDEFINED; } ret = ind(index); if (zero_type(ret)) { cache[index] = ZERO_TYPE; } else { cache[index] = ret; } return ret; }
098c8a2000-03-30Henrik Grubbström (Grubba)  static int _cache_full;
3e4b811998-10-16Henrik Grubbström (Grubba)  void fill_cache() {
f4f08d1999-02-26Henrik Grubbström (Grubba) #ifdef RESOLV_DEBUG werror(describe_backtrace(({ "Filling cache in joinnode\n", backtrace() }))); #endif /* RESOLV_DEBUG */
3e4b811998-10-16Henrik Grubbström (Grubba)  if (_cache_full) { return; }
c084262000-02-04Henrik Grubbström (Grubba)  foreach(joined_modules, object|mapping|program o) {
3e4b811998-10-16Henrik Grubbström (Grubba)  foreach(indices(o), string index) { if (zero_type(cache[index])) { `[](index); } } } _cache_full = 1; } array(string) _indices() { fill_cache();
cd99712001-11-09Martin Stjernholm  return indices(filter(cache, lambda(mixed x){ return cache[x] != ZERO_TYPE; }));
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)  }
94257b1998-10-16Henrik Grubbström (Grubba) };
1792a71998-04-10Henrik Grubbström (Grubba) // Variables mustn't be static to allow for replace_master(). // /grubba 1998-04-10 mapping(string:mixed) fc=([]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
34241f2001-05-24Henrik Grubbström (Grubba) object findmodule(string fullname, object|void handler)
ca2b071998-03-28Henrik Grubbström (Grubba) { object o;
eaa4da2001-10-04Fredrik Hübinette (Hubbe) #ifdef MODULE_TRACE werror("%*nfindmodule(%O)\n",sizeof(backtrace()),fullname); #endif
ca2b071998-03-28Henrik Grubbström (Grubba)  if(!zero_type(o=fc[fullname])) {
aa68b12001-03-19Fredrik Hübinette (Hubbe) // werror("fc[%O] -> %O\n",fullname, o);
ca2b071998-03-28Henrik Grubbström (Grubba)  return o; }
61a4242000-08-27Mirar (Pontus Hagland)  if(Stat stat=master_file_stat(fakeroot(fullname+".pmod")))
ca2b071998-03-28Henrik Grubbström (Grubba)  { if(stat[1]==-2)
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  return dirnode(fullname+".pmod", handler);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
34241f2001-05-24Henrik Grubbström (Grubba)  if(o = low_cast_to_object(fullname+".pmod", "/.", handler))
ca2b071998-03-28Henrik Grubbström (Grubba)  return fc[fullname]=o; #if constant(load_module)
26f3da2000-07-11Fredrik Hübinette (Hubbe)  if(master_file_stat(fakeroot(fullname+".so")))
34241f2001-05-24Henrik Grubbström (Grubba)  return fc[fullname] = low_cast_to_object(fullname, "/.", handler);
ca2b071998-03-28Henrik Grubbström (Grubba) #endif return fc[fullname]=UNDEFINED; }
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)  }
acd1632001-12-17Henrik Grubbström (Grubba) #ifdef MODULE_TRACE werror("%*nhandle_import(%O, %O, %O)\n", sizeof(backtrace()), what, current_file, handler); #endif /* MODULE_TRACE */
34241f2001-05-24Henrik Grubbström (Grubba)  if (handler) { return dirnode(path, handler); }
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  if(fc[path]) return fc[path]; return dirnode(path);
cc2c071998-04-19Fredrik Hübinette (Hubbe) }
a20af62000-09-26Fredrik Hübinette (Hubbe)  multiset no_resolv = (<>);
b07e962001-07-27Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe) class CompatResolver
cc2c071998-04-19Fredrik Hübinette (Hubbe) {
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(string) pike_include_path=({}); array(string) pike_module_path=({}); array(string) pike_program_path=({});
374c902001-12-20Martin Stjernholm  mapping(string:string) predefines = ([]);
a20af62000-09-26Fredrik Hübinette (Hubbe)  int want_warnings = PIKE_WARNINGS; string ver;
6df5a52001-11-07Martin Nilsson  //! The ComparResolver is initialized with a value that can be //! casted into a "%d.%d" string, e.g. a version object. void create(mixed version)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
6df5a52001-11-07Martin Nilsson  ver=(string)version;
a20af62000-09-26Fredrik Hübinette (Hubbe)  }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Add a directory to search for include files. //! //! This is the same as the command line option @tt{-I@}. //! //! @note //! Note that the added directory will only be searched when using //! < > to quote the included file. //! //! @seealso //! @[remove_include_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void add_include_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_include_path-=({tmp}); pike_include_path=({tmp})+pike_include_path; }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Remove a directory to search for include files. //! //! This function performs the reverse operation of @[add_include_path()]. //! //! @seealso //! @[add_include_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void remove_include_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_include_path-=({tmp}); }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Add a directory to search for modules. //! //! This is the same as the command line option @tt{-M@}. //! //! @seealso //! @[remove_module_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void add_module_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_module_path-=({tmp}); pike_module_path=({tmp})+pike_module_path; }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Remove a directory to search for modules. //! //! This function performs the reverse operation of @[add_module_path()]. //! //! @seealso //! @[add_module_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void remove_module_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_module_path-=({tmp}); }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Add a directory to search for programs. //! //! This is the same as the command line option @tt{-P@}. //! //! @seealso //! @[remove_program_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void add_program_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_program_path-=({tmp}); pike_program_path=({tmp})+pike_program_path; }
8f45692001-01-14Henrik Grubbström (Grubba)  //! Remove a directory to search for programs. //! //! This function performs the reverse operation of @[add_program_path()]. //! //! @seealso //! @[add_program_path()] //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  void remove_program_path(string tmp) { tmp=normalize_path(combine_path_with_cwd(tmp)); pike_program_path-=({tmp}); }
374c902001-12-20Martin Stjernholm  //! Add a define (without arguments) which will be implicitly //! defined in @[cpp] calls. void add_predefine (string name, string value) { predefines[name] = value; } //! Remove a define from the set that are implicitly defined in //! @[cpp] calls. void remove_predefine (string name) { m_delete (predefines, name); } //! mapping get_predefines() { return predefines; }
6df5a52001-11-07Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  mapping get_default_module() { /* This is an ugly kluge to avoid an infinite recursion. * The infinite recursion occurs because this function is * called for every file when the compat_major/minor is set. * This kluge could cause problems with threads if the * compiler was threaded. -Hubbe */ int saved_compat_minor=compat_minor; int saved_compat_major=compat_major; compat_minor=-1; compat_major=-1; mixed x; mixed err =catch {
acd1632001-12-17Henrik Grubbström (Grubba)  if(resolv("__default") && (x=resolv("__default.all_constants"))) x=x();
a20af62000-09-26Fredrik Hübinette (Hubbe)  }; compat_major=saved_compat_major; compat_minor=saved_compat_minor; if(err) throw(err); return x; }
6df5a52001-11-07Martin Nilsson  //!
34241f2001-05-24Henrik Grubbström (Grubba)  mixed resolv_base(string identifier, string|void current_file, object|void current_handler)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) // werror("Resolv_base(%O)\n",identifier);
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(mixed) tmp = ({});
b8cd052000-09-26Fredrik Hübinette (Hubbe)  string dir=current_file ? dirname(current_file) : "/";
34241f2001-05-24Henrik Grubbström (Grubba) 
a20af62000-09-26Fredrik Hübinette (Hubbe)  foreach(pike_module_path, string path) {
34241f2001-05-24Henrik Grubbström (Grubba)  string file=combine_path(dir, path, identifier); if(mixed ret=findmodule(file, current_handler)) {
a20af62000-09-26Fredrik Hübinette (Hubbe)  if ((objectp(ret)) && (ret->is_resolv_dirnode || ret->is_resolv_joinnode)) { if (mixed new_ret = ret->_module_value) { ret = new_ret; } tmp += ({ ret }); } else { if (mixed new_ret = ret->_module_value) { ret = new_ret; } if (!sizeof(tmp)) { return ret; } else { // Ignore werror(sprintf("Ignoring file %O: %t for identifier %O\n", file, ret, identifier)); continue; } } }
8b307e1998-10-17Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  if (sizeof(tmp)) { if (sizeof(tmp) == 1) { return(tmp[0]);
8b307e1998-10-17Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  return joinnode(tmp);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  return UNDEFINED;
94257b1998-10-16Henrik Grubbström (Grubba)  }
a20af62000-09-26Fredrik Hübinette (Hubbe)  mapping resolv_cache = set_weak_flag( ([]), 1 );
6df5a52001-11-07Martin Nilsson  //!
34241f2001-05-24Henrik Grubbström (Grubba)  mixed resolv(string identifier, string|void current_file, object|void current_handler)
a20af62000-09-26Fredrik Hübinette (Hubbe)  {
eaa4da2001-10-04Fredrik Hübinette (Hubbe) // werror("Resolv(%O)\n",identifier);
34241f2001-05-24Henrik Grubbström (Grubba)  // FIXME: Support having the cache in the handler?
a20af62000-09-26Fredrik Hübinette (Hubbe)  if( no_resolv[ identifier ] ) return UNDEFINED;
26ece22001-06-18Henrik Grubbström (Grubba)  if (current_file && !stringp(current_file)) { throw(({sprintf("resolv(%O, %O, %O): current_file is not a string!\n", identifier, current_file, current_handler), backtrace()})); }
a20af62000-09-26Fredrik Hübinette (Hubbe)  mixed ret; string id=identifier+":"+(current_file ? dirname(current_file) : "-");
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  if( zero_type (ret = resolv_cache[id]) != 1 ) { // werror("Resolv cached(%O) => %O (%d)\n",id,resolv_cache[id],zero_type(resolv_cache[id])); return ret == ZERO_TYPE ? UNDEFINED : ret; }
a20af62000-09-26Fredrik Hübinette (Hubbe)  array(string) tmp=identifier/".";
34241f2001-05-24Henrik Grubbström (Grubba)  ret=resolv_base(tmp[0], current_file, current_handler);
a20af62000-09-26Fredrik Hübinette (Hubbe)  foreach(tmp[1..],string index) ret=ret[index]; resolv_cache[id] = zero_type (ret) ? ZERO_TYPE : ret; return ret; }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever an #include directive is encountered //! it receives the argument for #include and should return the file name //! of the file to include
a20af62000-09-26Fredrik Hübinette (Hubbe)  string handle_include(string f, string current_file, int local_include) { array(string) tmp; string path; if(local_include) { tmp=EXPLODE_PATH(current_file); tmp[-1]=f; path=combine_path_with_cwd(tmp*"/"); } else { foreach(pike_include_path, path) { path=combine_path(path,f); if(master_file_stat(fakeroot(path))) break; else path=0; } } return path; }
6df5a52001-11-07Martin Nilsson  //!
a20af62000-09-26Fredrik Hübinette (Hubbe)  string read_include(string f) { AUTORELOAD_CHECK_FILE(f) return master_read_file(f); } string _sprintf() { return sprintf("CompatResolver(%s)",ver);
94257b1998-10-16Henrik Grubbström (Grubba)  }
7c11f41999-10-04Fredrik Hübinette (Hubbe) }
a20af62000-09-26Fredrik Hübinette (Hubbe) inherit CompatResolver;
97e5c72001-07-28Martin Nilsson //! These are useful if you want to start other Pike processes //! with the same options as this one was started with.
bfa62b1998-04-30Henrik Grubbström (Grubba) string _pike_file_name;
ac51841998-04-29Henrik Grubbström (Grubba) string _master_file_name;
53a32a2000-06-04Francesco Chemolli //flag. Gets set to 1 if we're in async-mode (script->main() returned <0) private int _async=0; int asyncp() { 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
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);
862fb41998-04-05Fredrik Hübinette (Hubbe)  int i,debug,trace;
ca2b071998-03-28Henrik Grubbström (Grubba)  object tmp; string a,b;
afa1292000-02-19Martin Nilsson  array q;
a6b6df2001-01-19Mirar (Pontus Hagland)  string postparseaction=0; mixed v;
ca2b071998-03-28Henrik Grubbström (Grubba) 
ddd2af2001-12-29Martin Nilsson  predefines = _static_modules.Builtin()->_take_over_initial_predefines();
bfa62b1998-04-30Henrik Grubbström (Grubba)  _pike_file_name = orig_argv[0];
45c7262001-04-17Mirar (Pontus Hagland) #if constant(thread_create) _backend_thread = this_thread(); #endif
bfa62b1998-04-30Henrik Grubbström (Grubba) 
d854161999-04-08Fredrik Hübinette (Hubbe)  foreach(env,a) { if(sscanf(a,"%s=%s",a,b)) { if(a=="") // Special hack for NT { sscanf(b,"%s=%s",a,b); a="="+a; }
a71f832001-11-13Tomas Nilsson  putenv(a, b);
d7741a1999-08-06Fredrik Hübinette (Hubbe)  }else{ werror("Broken environment var %s\n",a); }
d854161999-04-08Fredrik Hübinette (Hubbe)  }
ca2b071998-03-28Henrik Grubbström (Grubba) 
624e851999-06-02Marcus Comstedt 
e90a8b1999-06-01Mirar (Pontus Hagland) #ifndef NOT_INSTALLED q=(getenv("PIKE_INCLUDE_PATH")||"")/":"-({""}); for(i=sizeof(q)-1;i>=0;i--) add_include_path(q[i]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
e90a8b1999-06-01Mirar (Pontus Hagland)  q=(getenv("PIKE_PROGRAM_PATH")||"")/":"-({""}); for(i=sizeof(q)-1;i>=0;i--) add_program_path(q[i]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
e90a8b1999-06-01Mirar (Pontus Hagland)  q=(getenv("PIKE_MODULE_PATH")||"")/":"-({""}); for(i=sizeof(q)-1;i>=0;i--) add_module_path(q[i]); #endif
ca2b071998-03-28Henrik Grubbström (Grubba)  if(sizeof(argv)>1 && sizeof(argv[1]) && argv[1][0]=='-') { tmp=resolv("Getopt");
a7a45a1998-05-11Henrik Grubbström (Grubba)  if (!tmp) { werror("master.pike: Couldn't resolv Getopt module.\n" "Is your PIKE_MODULE_PATH environment variable set correctly?\n"); exit(1); }
ca2b071998-03-28Henrik Grubbström (Grubba)  q=tmp->find_all_options(argv,({
a20af62000-09-26Fredrik Hübinette (Hubbe)  ({"compat_version",tmp->HAS_ARG,({"-V","--compat"})}),
ca2b071998-03-28Henrik Grubbström (Grubba)  ({"version",tmp->NO_ARG,({"-v","--version"})}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  ({"help",tmp->NO_ARG,({"-h","--help"})}),
94c8552001-01-19Mirar (Pontus Hagland)  ({"features",tmp->NO_ARG,({"--features"})}),
a6b6df2001-01-19Mirar (Pontus Hagland)  ({"info",tmp->NO_ARG,({"--info"})}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  ({"execute",tmp->HAS_ARG,({"-e","--execute"})}),
3d14462000-09-24Per Hedbor  ({"debug_without",tmp->HAS_ARG,({"--debug-without"})}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  ({"preprocess",tmp->HAS_ARG,({"-E","--preprocess"})}), ({"modpath",tmp->HAS_ARG,({"-M","--module-path"})}), ({"ipath",tmp->HAS_ARG,({"-I","--include-path"})}), ({"ppath",tmp->HAS_ARG,({"-P","--program-path"})}),
2ea8261998-05-12Martin Stjernholm  ({"showpaths",tmp->NO_ARG,"--show-paths"}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  ({"warnings",tmp->NO_ARG,({"-w","--warnings"})}),
098c8a2000-03-30Henrik Grubbström (Grubba)  ({"nowarnings",tmp->NO_ARG,({"-W", "--woff", "--no-warnings"})}),
bec57d1999-09-06Fredrik Hübinette (Hubbe) #ifdef PIKE_AUTORELOAD ({"autoreload",tmp->NO_ARG,({"--autoreload"})}), #endif
ac51841998-04-29Henrik Grubbström (Grubba)  ({"master",tmp->HAS_ARG,"-m"}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  ({"compiler_trace",tmp->NO_ARG,"--compiler-trace"}),
8f45692001-01-14Henrik Grubbström (Grubba)  ({"assembler_debug",tmp->MAY_HAVE_ARG,"--assembler-debug"}),
e27a5c1999-11-19Henrik Grubbström (Grubba)  ({"optimizer_debug",tmp->MAY_HAVE_ARG,"--optimizer-debug"}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  ({"debug",tmp->MAY_HAVE_ARG,"--debug",0,1}), ({"trace",tmp->MAY_HAVE_ARG,"--trace",0,1}),
e26bdd1999-12-13Per Hedbor  ({"ignore",tmp->MAY_HAVE_ARG,"-Dqdatplr",0,1}),
ac51841998-04-29Henrik Grubbström (Grubba)  ({"ignore",tmp->HAS_ARG,"-s"}),
c5cc2d1998-04-29Henrik Grubbström (Grubba)  }), 1);
ca2b071998-03-28Henrik Grubbström (Grubba)  /* Parse -M and -I backwards */ for(i=sizeof(q)-1;i>=0;i--) { switch(q[i][0]) {
a20af62000-09-26Fredrik Hübinette (Hubbe)  case "compat_version": sscanf(q[i][1],"%d.%d",compat_major,compat_minor); break;
bec57d1999-09-06Fredrik Hübinette (Hubbe) #ifdef PIKE_AUTORELOAD case "autoreload": autoreload_on++; #endif
3d14462000-09-24Per Hedbor  case "debug_without": 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; add_constant( "thread_create", ([])[0] ); break; default: no_resolv[ feature ] = 1; break; } } break;
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) 
098c8a2000-03-30Henrik Grubbström (Grubba)  case "no-warnings": want_warnings--; break;
ac51841998-04-29Henrik Grubbström (Grubba)  case "master": _master_file_name = q[i][1]; break;
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
afa1292000-02-19Martin Nilsson  foreach(q, array opts)
ca2b071998-03-28Henrik Grubbström (Grubba)  { switch(opts[0]) { case "version":
5228132001-01-30Fredrik Hübinette (Hubbe)  werror(version() + " Copyright © 1994-2001 Roxen Internet Software\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" "COPYING and DISCLAIMER in the Pike distribution for more details.\n"); exit(0); case "help": werror("Usage: pike [-driver options] script [script arguments]\n" "Driver options include:\n" " -I --include-path=<p>: Add <p> to the include path\n" " -M --module-path=<p> : Add <p> to the module path\n" " -P --program-path=<p>: Add <p> to the program path\n" " -e --execute=<cmd> : Run the given command instead of a script.\n" " -h --help : see this message\n" " -v --version : See what version of pike you have.\n"
94c8552001-01-19Mirar (Pontus Hagland)  " --features : List Pike features.\n"
a6b6df2001-01-19Mirar (Pontus Hagland)  " --info : List information about the Pike build and setup.\n"
2ea8261998-05-12Martin Stjernholm  " --show-paths : See the paths and master that pike uses.\n"
ca2b071998-03-28Henrik Grubbström (Grubba)  " -s# : Set stack size\n" " -m <file> : Use <file> as master object.\n" " -d -d# : Increase debug (# is how much)\n" " -t -t# : Increase trace level\n" ); 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": werror("Include path : " + pike_include_path*"\n" " " + "\n" "Module path : " + pike_module_path*"\n" " " + "\n" "Program path : " + pike_program_path*"\n" " " + "\n"
3692711999-02-05Martin Stjernholm  "Master file : " + (_master_file_name || __FILE__) + "\n");
2ea8261998-05-12Martin Stjernholm  exit(0);
ca2b071998-03-28Henrik Grubbström (Grubba)  case "execute":
1808722001-08-31Fredrik Hübinette (Hubbe)  random_seed(time() ^ (getpid()<<8));
f1a1142001-02-23Fredrik Hübinette (Hubbe)  if(trace) predef::trace(trace);
a20af62000-09-26Fredrik Hübinette (Hubbe)  compile_string("mixed create(){"+opts[1]+";}")();
ca2b071998-03-28Henrik Grubbström (Grubba)  exit(0);
fcf7281998-04-28Fredrik Hübinette (Hubbe) 
c5cc2d1998-04-29Henrik Grubbström (Grubba)  case "preprocess":
d8a6fc2000-03-25Fredrik Hübinette (Hubbe)  _static_modules.files()->_stdout->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": v = resolv("Tools"); if (v) v = v["Install"]; if (v) v = v["features"]; if (!v) { werror("Could not resolv Tools.Install.features\n"); exit(1); } _static_modules.files()->_stdout->write(v()*"\n"+"\n"); exit(0); case "info": function w=_static_modules.files()->_stdout->write; w("Software......Pike\n" "Version......."+version()+"\n"
9c5c8f2001-04-07Martin Nilsson  "WWW...........http://pike.roxen.com/\n"
a6b6df2001-01-19Mirar (Pontus Hagland)  "\n"); w("pike binary..."+_pike_file_name+"\n"); w("master.pike..."+__FILE__+"\n"); w("Module path..." + pike_module_path*"\n" " " + "\n" "Include path.." + pike_include_path*"\n" " " + "\n" "Program path.." + pike_program_path*"\n" " " + "\n" "\n"); v = resolv("Tools"); if (v) v = v["Install"]; if (v) v = v["features"]; if (!v) { werror("Could not resolv Tools.Install.features\n"); exit(1); } w("Features......"+v()*"\n "+"\n"); exit(0);
94c8552001-01-19Mirar (Pontus Hagland)  }
1808722001-08-31Fredrik Hübinette (Hubbe)  random_seed(time() ^ (getpid()<<8));
9339fc2000-01-09Fredrik Hübinette (Hubbe) 
ca2b071998-03-28Henrik Grubbström (Grubba)  if(sizeof(argv)==1) {
caa3221998-04-15Henrik Grubbström (Grubba)  /* Attempt to resolv Tools.Hilfe.StdinHilfe */ tmp = resolv("Tools"); if (!tmp) { werror("Couldn't find Tools.\n"); exit(1);
ca2b071998-03-28Henrik Grubbström (Grubba)  }
caa3221998-04-15Henrik Grubbström (Grubba)  tmp = tmp["Hilfe"]; if (!tmp) { werror("Couldn't find Hilfe.\n"); exit(1); } tmp->StdinHilfe(); exit(0); } else {
ca2b071998-03-28Henrik Grubbström (Grubba)  argv=argv[1..]; }
471ed91998-04-24Fredrik Hübinette (Hubbe)  argv[0]=combine_path_with_cwd(argv[0]);
ca2b071998-03-28Henrik Grubbström (Grubba) 
586d1c2000-09-04Fredrik Hübinette (Hubbe)  program prog;
34fb3d1998-10-31Henrik Grubbström (Grubba)  mixed err = catch {
586d1c2000-09-04Fredrik Hübinette (Hubbe)  prog=(program)argv[0];
34fb3d1998-10-31Henrik Grubbström (Grubba)  }; if (err) { werror(sprintf("Pike: Failed to compile script:\n" "%s\n", stringp(err[0])?err[0]:describe_backtrace(err))); exit(1); } // FIXME: Isn't the following code dead?
586d1c2000-09-04Fredrik Hübinette (Hubbe)  if(!prog)
ca2b071998-03-28Henrik Grubbström (Grubba)  {
62962b1999-06-01Mirar (Pontus Hagland)  werror("Pike: Couldn't find script to execute\n(%O)\n",argv[0]);
ca2b071998-03-28Henrik Grubbström (Grubba)  exit(1); }
586d1c2000-09-04Fredrik Hübinette (Hubbe)  object script=prog();
ca2b071998-03-28Henrik Grubbström (Grubba) 
862fb41998-04-05Fredrik Hübinette (Hubbe) #if constant(_debug) if(debug) _debug(debug); #endif
ca2b071998-03-28Henrik Grubbström (Grubba)  if(!script->main) { werror("Error: "+argv[0]+" has no main().\n"); exit(1); }
fcf7281998-04-28Fredrik Hübinette (Hubbe)  if(trace) predef::trace(trace);
ca2b071998-03-28Henrik Grubbström (Grubba)  i=script->main(sizeof(argv),argv,env); if(i >=0) exit(i);
53a32a2000-06-04Francesco Chemolli  _async=1;
87f9082001-03-12Fredrik Hübinette (Hubbe)  while(1) { mixed err=catch { while(1) _static_modules.Builtin.__backend(3600.0); }; master()->handle_error(err); }
ca2b071998-03-28Henrik Grubbström (Grubba) }
01d3c91999-11-04Henrik Grubbström (Grubba) #if constant(thread_local) object inhibit_compile_errors = thread_local(); void set_inhibit_compile_errors(mixed f) { inhibit_compile_errors->set(f); }
71d9951999-11-29Henrik Grubbström (Grubba)  mixed get_inhibit_compile_errors() {
c2eb4a1999-11-29David Hedbor  return inhibit_compile_errors->get();
71d9951999-11-29Henrik Grubbström (Grubba) }
01d3c91999-11-04Henrik Grubbström (Grubba) #else /* !constant(thread_local) */
ca2b071998-03-28Henrik Grubbström (Grubba) mixed inhibit_compile_errors; void set_inhibit_compile_errors(mixed f) { inhibit_compile_errors=f; }
71d9951999-11-29Henrik Grubbström (Grubba)  mixed get_inhibit_compile_errors() {
c2eb4a1999-11-29David Hedbor  return inhibit_compile_errors;
71d9951999-11-29Henrik Grubbström (Grubba) }
01d3c91999-11-04Henrik Grubbström (Grubba) #endif /* constant(thread_local) */
ca2b071998-03-28Henrik Grubbström (Grubba) 
0024f92001-06-06Mirar (Pontus Hagland) static private function(string:string) _trim_file_name_cb=0;
ca2b071998-03-28Henrik Grubbström (Grubba) string trim_file_name(string s) {
4839dd2001-09-02Marcus Comstedt #ifdef PIKE_MODULE_RELOC s = relocate_module(s); #endif
e37a3e1999-10-09Fredrik Hübinette (Hubbe)  if(getenv("LONG_PIKE_ERRORS")) return s;
471ed91998-04-24Fredrik Hübinette (Hubbe)  if(getenv("SHORT_PIKE_ERRORS")) return BASENAME(s);
0024f92001-06-06Mirar (Pontus Hagland)  if (_trim_file_name_cb) return _trim_file_name_cb(s);
df2c632000-08-02Henrik Grubbström (Grubba)  /* getcwd() can fail, but since this is called from handle_error(), * we don't want to fail, so we don't care about that. */ catch { string cwd=getcwd(); if (sizeof(cwd) && (cwd[-1] != '/')) { cwd += "/"; } if(s[..sizeof(cwd)-1]==cwd) return s[sizeof(cwd)..]; };
ca2b071998-03-28Henrik Grubbström (Grubba)  return s; }
0024f92001-06-06Mirar (Pontus Hagland) function(string:string) set_trim_file_name_callback(function(string:string) s) { function(string:string) f=_trim_file_name_cb; _trim_file_name_cb=s; return f; }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever a compiling error occurs. //! Nothing strange about it. //! Note that previous_object cannot be trusted in ths function, because //! the compiler calls this function.
ca2b071998-03-28Henrik Grubbström (Grubba) void compile_error(string file,int line,string err) {
01d3c91999-11-04Henrik Grubbström (Grubba)  mixed val;
3112422000-03-27Per Hedbor  if(! (val = get_inhibit_compile_errors() ))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
58f1af1999-12-29Martin Stjernholm  werror(sprintf("%s:%s:%s\n",trim_file_name(file), line?(string)line:"-",err));
ca2b071998-03-28Henrik Grubbström (Grubba)  }
01d3c91999-11-04Henrik Grubbström (Grubba)  else if(objectp(val) || programp(val) || functionp(val))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
01d3c91999-11-04Henrik Grubbström (Grubba)  if (objectp(val) && val->compile_error) { val->compile_error(file, line, err); } else {
3112422000-03-27Per Hedbor  val(file, line, err);
01d3c91999-11-04Henrik Grubbström (Grubba)  }
ca2b071998-03-28Henrik Grubbström (Grubba)  } }
97e5c72001-07-28Martin Nilsson  //! This function is called whenever a compiling warning occurs. //! Nothing strange about it. //! Note that previous_object cannot be trusted in ths function, because //! the compiler calls this function.
ca2b071998-03-28Henrik Grubbström (Grubba) void compile_warning(string file,int line,string err) {
01d3c91999-11-04Henrik Grubbström (Grubba)  mixed val;
3112422000-03-27Per Hedbor  if(!(val = get_inhibit_compile_errors() ))
ca2b071998-03-28Henrik Grubbström (Grubba)  { if(want_warnings)
58f1af1999-12-29Martin Stjernholm  werror(sprintf("%s:%s: Warning: %s\n",trim_file_name(file), line?(string)line:"-",err));
01d3c91999-11-04Henrik Grubbström (Grubba)  } else if (objectp(val) && val->compile_warning) { 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) && (trace->is_cpp_error || trace->is_compilation_error)) // Errors thrown directly by cpp() and compile() are normally not // interesting; they've already been reported to compile_error.
5802de2001-08-16Martin Stjernholm  return 1;
8594c82001-08-15Martin Stjernholm  if (mixed val = get_inhibit_compile_errors()) { if (objectp (val) && val->compile_exception) return val->compile_exception (trace); } 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. werror ("GC warning: Garbing cycle where destroy() will be called " "in arbitrary order:\n%{ %s\n%}", map (args[0], describe_object)); break; default: werror ("%s warning: %s %O\n", capitalize (where), what, args); } }
ca2b071998-03-28Henrik Grubbström (Grubba) 
f4f08d1999-02-26Henrik Grubbström (Grubba) static mixed _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) { mixed mod = resolv("Locale"); _charset_mod = mod && mod["Charset"]; if (!_charset_mod) { compile_warning("-", 0, "No Locale.Charset module!"); return 0; } } object decoder; catch { decoder = _charset_mod->decoder(charset); }; if (!decoder) { compile_warning("-", 0, sprintf("Unknown charset %O!", charset)); return 0; } return decoder->feed(data)->drain(); }
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]++) identify_stack += indices (stuff); } else if (mappingp (stuff)) { if (!ident[stuff]++) identify_stack += indices (stuff) + values (stuff); }
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)  {
da0a822000-08-29Martin Stjernholm  if (stringp (ident[m])) return ident[m]; else if (intp (ident[m]) && ident[m] > 1) ident[m] = "@" + identcount++; string res, typ;
f645f02001-10-26Henrik Grubbström (Grubba)  catch { if (catch (typ=sprintf("%t",m))) typ = "object"; // Object with a broken _sprintf(), probably. switch(typ) {
da0a822000-08-29Martin Stjernholm  case "int": case "float": return (string)m; case "string":
2602b82000-08-29Martin Stjernholm  return describe_string (m, maxlen);
da0a822000-08-29Martin Stjernholm  case "array":
2602b82000-08-29Martin Stjernholm  res = describe_array (m, maxlen);
da0a822000-08-29Martin Stjernholm  break; case "mapping":
2602b82000-08-29Martin Stjernholm  res = describe_mapping (m, maxlen);
da0a822000-08-29Martin Stjernholm  break; case "multiset":
2602b82000-08-29Martin Stjernholm  res = describe_multiset (m, maxlen);
da0a822000-08-29Martin Stjernholm  break; case "function":
2602b82000-08-29Martin Stjernholm  if (string tmp=describe_function(m)) res = tmp; else res = typ; break;
da0a822000-08-29Martin Stjernholm  case "program":
2602b82000-08-29Martin Stjernholm  if(string tmp=describe_program(m)) res = tmp; else res = typ; break;
da0a822000-08-29Martin Stjernholm  default: if (objectp(m)) if(string tmp=describe_object(m)) { res = tmp; break; }
2602b82000-08-29Martin Stjernholm  res = typ;
f645f02001-10-26Henrik Grubbström (Grubba)  } }; if (!res) { // Extra paranoia case. res = sprintf("Instance of %O", _typeof(m));
da0a822000-08-29Martin Stjernholm  } if (stringp (ident[m])) 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) 
da0a822000-08-29Martin Stjernholm  // int loopcount=0;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  while(1) {
da0a822000-08-29Martin Stjernholm  // if(loopcount>10000) werror("len=%d\n",len); 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  // if(loopcount>10000) werror("clip=%d maxlen=%d\n",clip,maxlen); string ret = z[..clip-1]*","; // if(loopcount>10000) werror("sizeof(ret)=%d z=%O isclipped=%O done=%d\n",sizeof(ret),z[..clip-1],isclipped[..clip-1],done); if(done || sizeof(ret)<=maxlen+1) { int tmp=sizeof(x)-clip-1; // if(loopcount>10000) werror("CLIPPED::::: %O\n",isclipped); clipped=`+(0,@isclipped); if(tmp>=0) { clipped++; ret+=",,,"+tmp; } canclip++; return ret; } int last_newlen=len; int newlen; int clipsuggest; while(1) { // if(loopcount++ > 20000) return ""; // if(!(loopcount & 0xfff)) werror("GNORK\n"); 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)  {
da0a822000-08-29Martin Stjernholm  // if(loopcount>10000) werror("sizeof(z[%d])=%d len=%d\n",e,sizeof(z[e]),len);
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  // if(loopcount>10000) werror("num_large=%d maxlen=%d smallsize=%d clippsuggest=%d\n",num_large,maxlen,smallsize,clipsuggest); newlen=num_large ? (maxlen-smallsize)/num_large : 0;
ba20b11999-10-15Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  // if(loopcount>10000) werror("newlen=%d\n",newlen);
ba20b11999-10-15Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  if(newlen<8 || newlen >= last_newlen) break; last_newlen=newlen; // if(loopcount>10000) werror("len decreased, retrying.\n"); }
e8ebc01999-08-27Fredrik Hübinette (Hubbe) 
da0a822000-08-29Martin Stjernholm  if(newlen < 8 && clip) { clip-= (clip/4) || 1; if(clip > clipsuggest) clip=clipsuggest; // if(loopcount>10000) werror("clip decreased, retrying.\n"); }else{ len=newlen; done++; break; }
ba20b11999-10-15Fredrik Hübinette (Hubbe)  } }
da0a822000-08-29Martin Stjernholm  return ret; }
e8ebc01999-08-27Fredrik Hübinette (Hubbe) }
97e5c72001-07-28Martin Nilsson //!
bec57d1999-09-06Fredrik Hübinette (Hubbe) string describe_object(object o) { string s; if(!o) return 0;
80e50f2000-03-23Martin Stjernholm  if (!catch (s = sprintf("%O",o)) && s != "object") return s;
bec57d1999-09-06Fredrik Hübinette (Hubbe)  if(( s=describe_program(object_program(o)) )) return s+"()"; return 0; }
97e5c72001-07-28Martin Nilsson //!
62e87b1998-04-14Fredrik Hübinette (Hubbe) string describe_program(program p) { string s; if(!p) return 0; if(s=search(programs,p)) {
01f0272001-11-19Martin Nilsson  if(has_suffix(s, ".pmod")) return EXPLODE_PATH(s[..sizeof(s)-6])[-1];
e37a3e1999-10-09Fredrik Hübinette (Hubbe)  return trim_file_name(s);
62e87b1998-04-14Fredrik Hübinette (Hubbe)  }
e8ce5e2001-04-09Fredrik Hübinette (Hubbe)  if(mixed tmp=function_object(p)) { if(objectp(tmp)) {
337ad81998-04-15Fredrik Hübinette (Hubbe)  if(s=describe_program(object_program(tmp))) return s+"."+function_name(p);
e8ce5e2001-04-09Fredrik Hübinette (Hubbe)  } if(programp(tmp)) { if(s=describe_program(tmp)) return s+"."+function_name(p); } }
bec57d1999-09-06Fredrik Hübinette (Hubbe)  if(s=_static_modules.Builtin()->program_defined(p)) return EXPLODE_PATH(s)[-1];
62e87b1998-04-14Fredrik Hübinette (Hubbe)  return 0; }
97e5c72001-07-28Martin Nilsson //!
2602b82000-08-29Martin Stjernholm string describe_function (function f) { if (!f) return 0; string name; if(string s=search(programs,f)) {
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";
9f71092000-11-20Martin Stjernholm  object o = function_object(f);
530e2c2000-12-02Martin Stjernholm  if(objectp (o)) { // Check if it's an object in a way that (hopefully) doesn't // call any functions in it (neither `== nor `!).
2602b82000-08-29Martin Stjernholm  string s; if (!catch (s = sprintf("%O",o)) && s != "object") return s+"->"+name; } return name; }
ca2b071998-03-28Henrik Grubbström (Grubba) /* It is possible that this should be a real efun, * it is currently used by handle_error to convert a backtrace to a * readable message. */
8f45692001-01-14Henrik Grubbström (Grubba) 
b07e962001-07-27Martin Nilsson //! @appears describe_backtrace
8f45692001-01-14Henrik Grubbström (Grubba) //! Returns a string containing a readable message that describes where //! the backtrace was made. //! //! The argument @[trace] should normally be the return value from a call //! to @[backtrace()], or a caught error. //! //! @seealso //! @[backtrace()], @[describe_error()], @[catch()], @[throw()] //!
ba20b11999-10-15Fredrik Hübinette (Hubbe) string describe_backtrace(mixed trace, void|int linewidth)
ca2b071998-03-28Henrik Grubbström (Grubba) { int e;
5be4aa2001-11-12Martin Stjernholm  string ret = "";
d4fd0a1999-12-06Henrik Grubbström (Grubba)  int backtrace_len=((int)getenv("PIKE_BACKTRACE_LEN")) || bt_max_string_len;
ca2b071998-03-28Henrik Grubbström (Grubba) 
ba20b11999-10-15Fredrik Hübinette (Hubbe)  if(!linewidth) { linewidth=99999; catch { linewidth=_static_modules.files()->_stdin->tcgetattr()->columns; };
fe8fb32000-01-15Fredrik Hübinette (Hubbe)  if(linewidth<10) linewidth=99999;
ba20b11999-10-15Fredrik Hübinette (Hubbe)  }
f417dd1999-03-19Fredrik Hübinette (Hubbe)  if((arrayp(trace) && sizeof(trace)==2 && stringp(trace[0])) || (objectp(trace) && trace->is_generic_error))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
a232721999-10-06Henrik Grubbström (Grubba)  if (catch {
9798481999-12-06Henrik Grubbström (Grubba)  ret = trace[0] || "No error message!\n";
a232721999-10-06Henrik Grubbström (Grubba)  trace = trace[1]; }) { return "Error indexing backtrace!\n"; }
5be4aa2001-11-12Martin Stjernholm  if(!arrayp(trace)) return ret + "No backtrace.\n"; }else if (!arrayp (trace)) return sprintf ("Unrecognized backtrace format: %O\n", trace);
ca2b071998-03-28Henrik Grubbström (Grubba)  {
da0a822000-08-29Martin Stjernholm  Describer desc = Describer(); desc->identify_parts (trace);
9f71092000-11-20Martin Stjernholm  int end = 0; if( (sizeof(trace)>1) && arrayp(trace[0]) && (sizeof(trace[0]) > 2) && (trace[0][2] == _main)) 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;
6e343b2001-03-14Martin Stjernholm  if (mixed err=catch {
a232721999-10-06Henrik Grubbström (Grubba)  tmp = trace[e]; if(stringp(tmp))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
ba20b11999-10-15Fredrik Hübinette (Hubbe)  row=tmp;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
a232721999-10-06Henrik Grubbström (Grubba)  else if(arrayp(tmp))
ca2b071998-03-28Henrik Grubbström (Grubba)  {
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])) {
9f71092000-11-20Martin Stjernholm  string exact_pos = tmp[0] + ":" + tmp[1];
dbe79a1999-12-07Henrik Grubbström (Grubba)  pos=trim_file_name(tmp[0])+":"+tmp[1]; } else { pos = sprintf("%s:Bad line %t", trim_file_name(tmp[0]), tmp[1]); }
a232721999-10-06Henrik Grubbström (Grubba)  }else{ mixed desc="Unknown program"; if(sizeof(tmp)>=3 && functionp(tmp[2])) {
e118831999-12-22Per Hedbor  catch {
a232721999-10-06Henrik Grubbström (Grubba)  if(mixed tmp=function_object(tmp[2])) if(tmp=object_program(tmp)) if(tmp=describe_program(tmp)) desc=tmp; }; }
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])) {
2602b82000-08-29Martin Stjernholm  data = describe_function (tmp[2]);
dfca1f2000-08-22Martin Stjernholm  }
ba20b11999-10-15Fredrik Hübinette (Hubbe)  else if (stringp(tmp[2])) { data= tmp[2]; } else data ="unknown function"; data+="("+
da0a822000-08-29Martin Stjernholm  desc->describe_comma_list(tmp[3..], backtrace_len)+
ba20b11999-10-15Fredrik Hübinette (Hubbe)  ")"; if(sizeof(pos)+sizeof(data) < linewidth-4) { row=sprintf("%s: %s",pos,data); }else{ row=sprintf("%s:\n%s",pos,sprintf(" %*-/s",linewidth-6,data)); }
dbe79a1999-12-07Henrik Grubbström (Grubba)  } else { row = pos;
62e87b1998-04-14Fredrik Hübinette (Hubbe)  }
ca2b071998-03-28Henrik Grubbström (Grubba)  }
a232721999-10-06Henrik Grubbström (Grubba)  else {
9798481999-12-06Henrik Grubbström (Grubba)  if (tmp) {
e0e05f2000-03-23Martin Stjernholm  if (catch (row = sprintf("%O", tmp))) row = describe_program(object_program(tmp)) + " with broken _sprintf()";
9798481999-12-06Henrik Grubbström (Grubba)  } else { row = "Destructed object"; }
a232721999-10-06Henrik Grubbström (Grubba)  }
6e343b2001-03-14Martin Stjernholm  }) { row = sprintf("Error indexing backtrace line %d: %s (%O)!", e, err[0], err[1]); }
5d6a562001-11-08Fredrik Hübinette (Hubbe)  int dup_frame; if (!zero_type(dup_frame = prev_pos[row])) { dup_frame -= sizeof(frames); if (!loop_start) { loop_start = dup_frame; loop_next = dup_frame + 1; loops = 0; continue; } else { int new_loop = 0; if (!loop_next) loop_next = loop_start, new_loop = 1; if (dup_frame == loop_next++) { loops += new_loop; continue; } } } prev_pos[row] = sizeof(frames); if (loop_start) { array(string) tail; if (!loop_next) tail = ({}), loops++; else tail = frames[loop_start + sizeof(frames) .. loop_next - 1 + sizeof(frames)]; if (loops) frames += ({sprintf ("... last %d frames above repeated %d times ...\n", -loop_start, loops)}); frames += tail; prev_pos = ([]); loop_start = 0; }
9f71092000-11-20Martin Stjernholm  frames += ({row + "\n"}); } if (loop_start) { // Want tail to contain a full loop rather than being empty; it // looks odd when the repeat message ends the backtrace.
5d6a562001-11-08Fredrik Hübinette (Hubbe)  array(string) tail = frames[loop_start + sizeof(frames) .. loop_next - 1 + sizeof(frames)];
d48c1f2001-12-18Martin Stjernholm  if (loops) frames += ({sprintf("... last %d frames above repeated %d times ...\n", -loop_start, loops)});
9f71092000-11-20Martin Stjernholm  frames += tail;
ca2b071998-03-28Henrik Grubbström (Grubba)  }
9f71092000-11-20Martin Stjernholm  ret += frames * "";
ca2b071998-03-28Henrik Grubbström (Grubba)  }
5be4aa2001-11-12Martin Stjernholm  return ret;
ca2b071998-03-28Henrik Grubbström (Grubba) }
471ed91998-04-24Fredrik Hübinette (Hubbe) 
b07e962001-07-27Martin Nilsson //! @appears describe_error
8f45692001-01-14Henrik Grubbström (Grubba) //! Returns only the error message from a backtrace. //! //! If there is no error message in the backtrace, a fallback message //! will be returned. //! //! @seealso //! @[backtrace()], @[describe_backtrace()] //!
a66ff22000-01-11Martin Stjernholm string describe_error (mixed trace) { if((arrayp(trace) && sizeof(trace)==2 && stringp(trace[0])) || (objectp(trace) && trace->is_generic_error)) { if (catch {
51b0fb2000-03-20Martin Stjernholm  return trace[0] || "No error message.\n";
a66ff22000-01-11Martin Stjernholm  }) { return "Error indexing backtrace!\n"; } } return sprintf ("Backtrace is of unknown type %t!\n", trace); }
471ed91998-04-24Fredrik Hübinette (Hubbe)  class Codec {
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  string fname; int mkobj; program prog_to_mkobj; void create(void|string f,void|int m) { fname=f; mkobj=m; }
5d6a562001-11-08Fredrik Hübinette (Hubbe)  object __register_new_program(program p)
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  { if(fname) { programs[fname]=prog_to_mkobj=p; fname=0;
5d6a562001-11-08Fredrik Hübinette (Hubbe)  if (mkobj) return objects[p] || (objects[p]=__null_program());
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  }
5d6a562001-11-08Fredrik Hübinette (Hubbe)  return 0;
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  }
471ed91998-04-24Fredrik Hübinette (Hubbe)  mapping f=all_constants(); string nameof(mixed x) { if(mixed tmp=search(f,x)) return "efun:"+tmp;
51b0fb2000-03-20Martin Stjernholm  if (programp(x)) { if(mixed tmp=search(programs,x)) return tmp;
471ed91998-04-24Fredrik Hübinette (Hubbe) 
51b0fb2000-03-20Martin Stjernholm  if(mixed tmp=search(values(_static_modules), x)) return "_static_modules."+(indices(_static_modules)[tmp]);
471ed91998-04-24Fredrik Hübinette (Hubbe)  }
51b0fb2000-03-20Martin Stjernholm  else if (objectp(x)) if(mixed tmp=search(objects,x)) if(tmp=search(programs,tmp)) return tmp;
471ed91998-04-24Fredrik Hübinette (Hubbe)  return ([])[0]; } function functionof(string x) { if(sscanf(x,"efun:%s",x)) return f[x];
b1693a2000-04-07Fredrik Hübinette (Hubbe)  if(sscanf(x,"resolv:%s",x)) return resolv(x);
471ed91998-04-24Fredrik Hübinette (Hubbe)  return 0; } object objectof(string x) { if(sscanf(x,"efun:%s",x)) return f[x];
b1693a2000-04-07Fredrik Hübinette (Hubbe)  if(sscanf(x,"resolv:%s",x)) return resolv(x);
f848412001-08-29Marcus Comstedt  if(sscanf(x,"mpath:%s",x)) foreach(pike_module_path, string path) if(object ret=low_cast_to_object(combine_path(path,x),0)) return ret;
2f61761999-04-29Fredrik Hübinette (Hubbe)  return cast_to_object(x,0);
471ed91998-04-24Fredrik Hübinette (Hubbe)  } program programof(string x) { if(sscanf(x,"efun:%s",x)) return f[x];
b1693a2000-04-07Fredrik Hübinette (Hubbe)  if(sscanf(x,"resolv:%s",x)) return resolv(x);
f848412001-08-29Marcus Comstedt  if(sscanf(x,"mpath:%s",x)) foreach(pike_module_path, string path) if(program ret=cast_to_program(combine_path(path,x),0)) return ret;
2f61761999-04-29Fredrik Hübinette (Hubbe)  return cast_to_program(x,0);
471ed91998-04-24Fredrik Hübinette (Hubbe)  } mixed encode_object(object x) {
b1693a2000-04-07Fredrik Hübinette (Hubbe)  if(x->_encode) return x->_encode();
471ed91998-04-24Fredrik Hübinette (Hubbe)  error("Cannot encode objects yet.\n"); }
d28a712001-07-02Henrik Grubbström (Grubba)  void decode_object(object o, mixed data)
471ed91998-04-24Fredrik Hübinette (Hubbe)  {
b1693a2000-04-07Fredrik Hübinette (Hubbe)  o->_decode(data);
471ed91998-04-24Fredrik Hübinette (Hubbe)  } }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
5d6a562001-11-08Fredrik Hübinette (Hubbe) 
6df5a52001-11-07Martin Nilsson //! Contains version information about a Pike version.
a20af62000-09-26Fredrik Hübinette (Hubbe) class Version {
6df5a52001-11-07Martin Nilsson  //! The major and minor parts of the version.
a20af62000-09-26Fredrik Hübinette (Hubbe)  int major; int minor;
6df5a52001-11-07Martin Nilsson  //! @decl void create(int major, int minor) //! Set the version in the object.
a20af62000-09-26Fredrik Hübinette (Hubbe)  void create(int maj, int min)
6df5a52001-11-07Martin Nilsson  { major = maj; minor = min; }
a20af62000-09-26Fredrik Hübinette (Hubbe) 
b8cd052000-09-26Fredrik Hübinette (Hubbe) #define CMP(X) ((major - (X)->major) || (minor - (X)->minor))
a20af62000-09-26Fredrik Hübinette (Hubbe) 
6df5a52001-11-07Martin Nilsson  //! Methods define so that version objects //! can be compared and ordered.
a20af62000-09-26Fredrik Hübinette (Hubbe)  int `<(Version v) { return CMP(v) < 0; } int `>(Version v) { return CMP(v) > 0; } int `==(Version v) { return CMP(v)== 0; } int _hash() { return major * 4711 + minor ; } string _sprintf() { return sprintf("%d.%d",major,minor); }
6df5a52001-11-07Martin Nilsson  //! The version object can be casted into a string.
a20af62000-09-26Fredrik Hübinette (Hubbe)  mixed cast(string type) { switch(type) { case "string": return sprintf("%d.%d",major,minor); } } }
6df5a52001-11-07Martin Nilsson //! Version information about the current Pike version.
a20af62000-09-26Fredrik Hübinette (Hubbe) Version currentversion=Version(__MAJOR__,__MINOR__);
6df5a52001-11-07Martin Nilsson 
a20af62000-09-26Fredrik Hübinette (Hubbe) mapping(Version:CompatResolver) compat_handler_cache=set_weak_flag( ([]), 1); CompatResolver get_compilation_handler(int major, int minor) { CompatResolver ret; Version v=Version(major,minor); if(v > currentversion) { /* do we want to make an error if major.minor > __MAJOR__.__MINOR ? */ return 0; } if(!zero_type(ret=compat_handler_cache[v])) return ret;
5be4aa2001-11-12Martin Stjernholm  array 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 ret=CompatResolver(v);
b8cd052000-09-26Fredrik Hübinette (Hubbe)  /* Add default paths */ ret->pike_module_path=pike_module_path; ret->pike_include_path=pike_include_path;
a20af62000-09-26Fredrik Hübinette (Hubbe) 
b8cd052000-09-26Fredrik Hübinette (Hubbe)  foreach(reverse(available), Version tmp)
a20af62000-09-26Fredrik Hübinette (Hubbe)  { string base; #if "¤lib_prefix¤"[0]!='¤'
b8cd052000-09-26Fredrik Hübinette (Hubbe)  base=combine_path("¤lib_prefix¤",sprintf("%s",tmp));
a20af62000-09-26Fredrik Hübinette (Hubbe)  ret->add_module_path(combine_path(base,"modules")); ret->add_include_path(combine_path(base,"include")); #endif #if "¤share_prefix¤"[0]!='¤'
b8cd052000-09-26Fredrik Hübinette (Hubbe)  base=combine_path("¤share_prefix¤",sprintf("%s",tmp));
a20af62000-09-26Fredrik Hübinette (Hubbe)  ret->add_module_path(combine_path(base,"modules")); ret->add_include_path(combine_path(base,"include")); #endif }
b8cd052000-09-26Fredrik Hübinette (Hubbe)  if( v <= Version(0,6)) ret->pike_module_path+=({"."});
a20af62000-09-26Fredrik Hübinette (Hubbe)  compat_handler_cache[v] = ret; #ifndef RESOLVER_HACK compat_handler_cache[available[0]] = ret; /* may be equal to 'v' */ #endif return ret; } string _sprintf() { return "master()"; }