cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h"
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) RCSID("$Id: main.c,v 1.32 1998/01/13 22:56:45 hubbe Exp $");
5740881998-01-01Fredrik Hübinette (Hubbe) #include "fdlib.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "backend.h" #include "module.h" #include "object.h" #include "lex.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "pike_types.h" #include "builtin_functions.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "array.h" #include "stralloc.h" #include "interpret.h" #include "error.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "callback.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "signal_handler.h"
07513e1996-10-04Fredrik Hübinette (Hubbe) #include "threads.h"
9c6f7d1997-04-15Fredrik Hübinette (Hubbe) #include "dynamic_load.h" #include "gc.h" #include "mapping.h"
5740881998-01-01Fredrik Hübinette (Hubbe) #include "cpp.h"
ac173e1997-11-02Henrik Grubbström (Grubba) #include <errno.h>
5267b71995-08-09Fredrik Hübinette (Hubbe) #ifdef HAVE_LOCALE_H #include <locale.h> #endif
6930181996-02-25Fredrik Hübinette (Hubbe) 
6e37dc1996-06-21Fredrik Hübinette (Hubbe) #include "time_stuff.h"
6930181996-02-25Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe) #ifdef HAVE_SYS_RESOURCE_H #include <sys/resource.h> #endif
f8e5fe1997-01-18Henrik Grubbström (Grubba) 
5267b71995-08-09Fredrik Hübinette (Hubbe) char *master_file;
e1195f1997-03-23Fredrik Hübinette (Hubbe) char **ARGV;
5267b71995-08-09Fredrik Hübinette (Hubbe)  int d_flag=0; int c_flag=0; int t_flag=0; int a_flag=0; int l_flag=0;
06983f1996-09-22Fredrik Hübinette (Hubbe) int p_flag=0;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
81b84e1996-12-03Fredrik Hübinette (Hubbe) static struct callback_list post_master_callbacks;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
6e37dc1996-06-21Fredrik Hübinette (Hubbe) struct callback *add_post_master_callback(callback_func call,
8f4f881996-06-20Fredrik Hübinette (Hubbe)  void *arg,
6e37dc1996-06-21Fredrik Hübinette (Hubbe)  callback_func free_func)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
6e37dc1996-06-21Fredrik Hübinette (Hubbe)  return add_to_callback(&post_master_callbacks, call, arg, free_func);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
5740881998-01-01Fredrik Hübinette (Hubbe) void main(int argc, char **argv)
5267b71995-08-09Fredrik Hübinette (Hubbe) { JMP_BUF back; int e, num; char *p; struct array *a;
85f59e1998-01-08Fredrik Hübinette (Hubbe) #ifdef DECLARE_ENVIRON extern char **environ; #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) 
e1195f1997-03-23Fredrik Hübinette (Hubbe)  ARGV=argv;
5740881998-01-01Fredrik Hübinette (Hubbe)  fd_init();
5267b71995-08-09Fredrik Hübinette (Hubbe) #ifdef HAVE_SETLOCALE
2cf1a11996-09-23Fredrik Hübinette (Hubbe) #ifdef LC_NUMERIC
06983f1996-09-22Fredrik Hübinette (Hubbe)  setlocale(LC_NUMERIC, "C");
2cf1a11996-09-23Fredrik Hübinette (Hubbe) #endif #ifdef LC_CTYPE
06983f1996-09-22Fredrik Hübinette (Hubbe)  setlocale(LC_CTYPE, "");
2cf1a11996-09-23Fredrik Hübinette (Hubbe) #endif #ifdef LC_TIME
06983f1996-09-22Fredrik Hübinette (Hubbe)  setlocale(LC_TIME, "C");
2cf1a11996-09-23Fredrik Hübinette (Hubbe) #endif #ifdef LC_COLLATE
06983f1996-09-22Fredrik Hübinette (Hubbe)  setlocale(LC_COLLATE, "");
2cf1a11996-09-23Fredrik Hübinette (Hubbe) #endif #ifdef LC_MESSAGES
06983f1996-09-22Fredrik Hübinette (Hubbe)  setlocale(LC_MESSAGES, "");
2cf1a11996-09-23Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif init_backend(); master_file = 0; #ifdef HAVE_GETENV
06983f1996-09-22Fredrik Hübinette (Hubbe)  master_file = getenv("PIKE_MASTER");
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) #endif #if __NT__ { char buffer[4096]; DWORD len=sizeof(buffer)-1,type=REG_SZ; if(RegQueryValueEx(HKEY_CURRENT_USER, "SOFTWARE\\Idonex\\Pike\\0.6\\PIKE_MASTER", 0, &type, buffer, &len)==ERROR_SUCCESS || RegQueryValueEx(HKEY_CURRENT_USER, "SOFTWARE\\Idonex\\Pike\\0.6\\PIKE_MASTER", 0, &type, buffer, &len)==ERROR_SUCCESS) { master_file=strdup(buffer); } }
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif if(!master_file) master_file = DEFAULT_MASTER; for(e=1; e<argc; e++) { if(argv[e][0]=='-') { for(p=argv[e]+1; *p;) { switch(*p) { case 'D': add_predefine(p+1); p+=strlen(p); break; case 'm': if(p[1]) { master_file=p+1; p+=strlen(p); }else{ e++; if(e >= argc) { fprintf(stderr,"Missing argument to -m\n"); exit(1); } master_file=argv[e]; p+=strlen(p); } break;
9f243b1996-08-12Fredrik Hübinette (Hubbe)  case 's': if(!p[1]) { e++; if(e >= argc) { fprintf(stderr,"Missing argument to -s\n"); exit(1); } p=argv[e]; } stack_size=STRTOL(p+1,&p,0); p+=strlen(p); if(stack_size < 256) { fprintf(stderr,"Stack size must at least be 256.\n"); exit(1); } break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case 'd': if(p[1]>='0' && p[1]<='9') d_flag+=STRTOL(p+1,&p,10); else d_flag++,p++; break; case 'a': if(p[1]>='0' && p[1]<='9') a_flag+=STRTOL(p+1,&p,10); else a_flag++,p++; break; case 't': if(p[1]>='0' && p[1]<='9') t_flag+=STRTOL(p+1,&p,10); else t_flag++,p++; break;
06983f1996-09-22Fredrik Hübinette (Hubbe)  case 'p': if(p[1]>='0' && p[1]<='9') p_flag+=STRTOL(p+1,&p,10); else p_flag++,p++; break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case 'l': if(p[1]>='0' && p[1]<='9') l_flag+=STRTOL(p+1,&p,10); else l_flag++,p++; break; default:
4967071997-01-15Fredrik Hübinette (Hubbe)  p+=strlen(p);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } } }else{ break; } }
2a50961995-08-23Fredrik Hübinette (Hubbe) #if !defined(RLIMIT_NOFILE) && defined(RLIMIT_OFILE) #define RLIMIT_NOFILE RLIMIT_OFILE
e17dbc1995-08-11David Hedbor #endif
2a50961995-08-23Fredrik Hübinette (Hubbe)  #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE)
5267b71995-08-09Fredrik Hübinette (Hubbe)  { struct rlimit lim;
f90e541995-08-17Fredrik Hübinette (Hubbe)  long tmp;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(!getrlimit(RLIMIT_NOFILE, &lim)) {
5c8e891995-10-29Fredrik Hübinette (Hubbe) #ifdef RLIM_INFINITY if(lim.rlim_max == RLIM_INFINITY) lim.rlim_max=MAX_OPEN_FILEDESCRIPTORS; #endif
f90e541995-08-17Fredrik Hübinette (Hubbe)  tmp=MINIMUM(lim.rlim_max, MAX_OPEN_FILEDESCRIPTORS);
5267b71995-08-09Fredrik Hübinette (Hubbe)  lim.rlim_cur=tmp; setrlimit(RLIMIT_NOFILE, &lim); } } #endif
6e37dc1996-06-21Fredrik Hübinette (Hubbe)  GETTIMEOFDAY(&current_time);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
378b2d1997-02-06Fredrik Hübinette (Hubbe)  init_shared_string_table(); init_interpreter();
5740881998-01-01Fredrik Hübinette (Hubbe)  init_cpp();
378b2d1997-02-06Fredrik Hübinette (Hubbe)  init_lex(); init_types(); init_modules();
5267b71995-08-09Fredrik Hübinette (Hubbe)  master();
ddea551996-07-01Fredrik Hübinette (Hubbe)  call_callback(& post_master_callbacks, 0);
8f4f881996-06-20Fredrik Hübinette (Hubbe)  free_callback(& post_master_callbacks);
4967071997-01-15Fredrik Hübinette (Hubbe)  a=allocate_array_no_init(argc,0); for(num=0;num<argc;num++)
99946c1996-02-17Fredrik Hübinette (Hubbe)  {
4967071997-01-15Fredrik Hübinette (Hubbe)  ITEM(a)[num].u.string=make_shared_string(argv[num]);
99946c1996-02-17Fredrik Hübinette (Hubbe)  ITEM(a)[num].type=T_STRING; }
5267b71995-08-09Fredrik Hübinette (Hubbe)  push_array(a);
5740881998-01-01Fredrik Hübinette (Hubbe)  for(num=0;environ[num];num++);
99946c1996-02-17Fredrik Hübinette (Hubbe)  a=allocate_array_no_init(num,0);
5740881998-01-01Fredrik Hübinette (Hubbe)  for(num=0;environ[num];num++)
99946c1996-02-17Fredrik Hübinette (Hubbe)  {
5740881998-01-01Fredrik Hübinette (Hubbe)  ITEM(a)[num].u.string=make_shared_string(environ[num]);
99946c1996-02-17Fredrik Hübinette (Hubbe)  ITEM(a)[num].type=T_STRING; }
5267b71995-08-09Fredrik Hübinette (Hubbe)  push_array(a); if(SETJMP(back)) {
dceabb1996-10-09Fredrik Hübinette (Hubbe)  ONERROR tmp; SET_ONERROR(tmp,exit_on_error,"Error in handle_error in master object!");
5aa04a1996-05-16Fredrik Hübinette (Hubbe)  assign_svalue_no_free(sp, & throw_value); sp++;
5267b71995-08-09Fredrik Hübinette (Hubbe)  APPLY_MASTER("handle_error", 1); pop_stack();
dceabb1996-10-09Fredrik Hübinette (Hubbe)  UNSET_ONERROR(tmp);
5267b71995-08-09Fredrik Hübinette (Hubbe)  exit(10); } apply(master(),"_main",2); pop_stack(); UNSETJMP(back); backend(); push_int(0); f_exit(1); }
156fd51997-10-27Fredrik Hübinette (Hubbe) void low_init_main(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
378b2d1997-02-06Fredrik Hübinette (Hubbe)  th_init();
5267b71995-08-09Fredrik Hübinette (Hubbe)  init_builtin_efuns();
cb22561995-10-11Fredrik Hübinette (Hubbe)  init_signals();
ca74dd1996-10-08Fredrik Hübinette (Hubbe)  init_dynamic_load();
5267b71995-08-09Fredrik Hübinette (Hubbe) }
378b2d1997-02-06Fredrik Hübinette (Hubbe) void exit_main(void)
156fd51997-10-27Fredrik Hübinette (Hubbe) { cleanup_objects(); } void init_main(void) { } void low_exit_main(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
be478c1997-08-30Henrik Grubbström (Grubba)  void cleanup_added_efuns(void); void cleanup_pike_types(void); void cleanup_program(void);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
6d1a5e1996-10-07Fredrik Hübinette (Hubbe)  th_cleanup();
c52c6a1997-01-14Niels Möller  exit_dynamic_load();
cb22561995-10-11Fredrik Hübinette (Hubbe)  exit_signals();
5267b71995-08-09Fredrik Hübinette (Hubbe)  exit_lex();
5740881998-01-01Fredrik Hübinette (Hubbe)  exit_cpp();
5267b71995-08-09Fredrik Hübinette (Hubbe)  cleanup_interpret(); cleanup_added_efuns();
06983f1996-09-22Fredrik Hübinette (Hubbe)  cleanup_pike_types();
5267b71995-08-09Fredrik Hübinette (Hubbe)  cleanup_program();
06983f1996-09-22Fredrik Hübinette (Hubbe)  do_gc();
84c9cc1996-11-22Fredrik Hübinette (Hubbe)  cleanup_callbacks();
06983f1996-09-22Fredrik Hübinette (Hubbe)  zap_all_arrays();
cbd60b1996-12-04Fredrik Hübinette (Hubbe)  zap_all_mappings();
06983f1996-09-22Fredrik Hübinette (Hubbe)  cleanup_shared_string_table();
5267b71995-08-09Fredrik Hübinette (Hubbe) }