e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information.
766bc82004-10-16Marcus Agehall || $Id: module.c,v 1.30 2004/10/16 07:27:29 agehall Exp $
e576bb2002-10-11Martin Nilsson */
aedfb12002-10-09Martin Nilsson 
db3e131997-02-06Fredrik Hübinette (Hubbe) #include "global.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "module.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
db3e131997-02-06Fredrik Hübinette (Hubbe) #include "builtin_functions.h" #include "main.h" #include "svalue.h" #include "interpret.h" #include "stralloc.h" #include "object.h" #include "mapping.h"
992e662000-07-06Fredrik Hübinette (Hubbe) #include "program_id.h"
37b0222002-01-31Marcus Comstedt #include "lex.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
db3e131997-02-06Fredrik Hübinette (Hubbe) #include "modules/modlist_headers.h"
f173082001-02-01Fredrik Hübinette (Hubbe) #include "post_modules/modlist_headers.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
48073c2003-02-05Henrik Grubbström (Grubba) /* Define this to trace the initialization and cleanup of static modules. */ /* #define TRACE_MODULE */ #if defined(TRACE_MAIN) || defined(TRACE_MODULE) #define TRACE(X) fprintf X #else /* !TRACE_MAIN */ #define TRACE(X) #endif /* TRACE_MAIN */
24ddc71998-03-28Henrik Grubbström (Grubba) 
db3e131997-02-06Fredrik Hübinette (Hubbe) typedef void (*modfun)(void);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
db3e131997-02-06Fredrik Hübinette (Hubbe) struct static_module
5267b71995-08-09Fredrik Hübinette (Hubbe) {
db3e131997-02-06Fredrik Hübinette (Hubbe)  char *name; modfun init; modfun exit; };
5267b71995-08-09Fredrik Hübinette (Hubbe) 
5a59872004-09-19Martin Nilsson static const struct static_module module_list[] = {
156fd51997-10-27Fredrik Hübinette (Hubbe)  { "Builtin", low_init_main, low_exit_main }
db3e131997-02-06Fredrik Hübinette (Hubbe) #include "modules/modlist.h"
f173082001-02-01Fredrik Hübinette (Hubbe) #include "post_modules/modlist.h"
db3e131997-02-06Fredrik Hübinette (Hubbe) }; void init_modules(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
1cb5d52004-04-14Henrik Grubbström (Grubba)  struct program *p = NULL;
766bc82004-10-16Marcus Agehall  volatile unsigned int e;
37b0222002-01-31Marcus Comstedt  struct lex save_lex; save_lex = lex; lex.current_line=1; lex.current_file=make_shared_string("-");
c712e71998-04-23Fredrik Hübinette (Hubbe)  start_new_program();
992e662000-07-06Fredrik Hübinette (Hubbe)  Pike_compiler->new_program->id=PROG___BUILTIN_ID;
db3e131997-02-06Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  for(e=0;e<NELEM(module_list);e++) {
37b0222002-01-31Marcus Comstedt  JMP_BUF recovery;
1cb5d52004-04-14Henrik Grubbström (Grubba)  if (!p) { start_new_program();
75876b2004-04-14Henrik Grubbström (Grubba)  p = Pike_compiler->new_program;
1cb5d52004-04-14Henrik Grubbström (Grubba)  }
37b0222002-01-31Marcus Comstedt  if(SETJMP(recovery)) {
1cb5d52004-04-14Henrik Grubbström (Grubba)  /* FIXME: We could loop here until we find p. */
37b0222002-01-31Marcus Comstedt  free_program(end_program());
1cb5d52004-04-14Henrik Grubbström (Grubba)  p = NULL;
37b0222002-01-31Marcus Comstedt  call_handle_error(); } else {
48073c2003-02-05Henrik Grubbström (Grubba)  TRACE((stderr, "Initializing static module #%d: \"%s\"...\n", e, module_list[e].name));
37b0222002-01-31Marcus Comstedt  module_list[e].init();
0bd1472004-04-15Henrik Grubbström (Grubba)  if ( #if 0 Pike_compiler->new_program->num_identifier_references #else /* !0 */ 1 #endif /* 0 */ ) {
1cb5d52004-04-14Henrik Grubbström (Grubba)  debug_end_class(module_list[e].name,strlen(module_list[e].name),0); p = NULL; } else { /* No identifier references -- Disabled module. */ }
37b0222002-01-31Marcus Comstedt  } UNSETJMP(recovery);
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
1cb5d52004-04-14Henrik Grubbström (Grubba)  if (p) free_program(end_program());
c712e71998-04-23Fredrik Hübinette (Hubbe)  push_text("_static_modules"); push_object(low_clone(p=end_program())); f_add_constant(2); free_program(p);
37b0222002-01-31Marcus Comstedt  free_string(lex.current_file); lex = save_lex;
00538d2003-03-26Martin Nilsson  init_main();
5267b71995-08-09Fredrik Hübinette (Hubbe) }
db3e131997-02-06Fredrik Hübinette (Hubbe) void exit_modules(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
37b0222002-01-31Marcus Comstedt  JMP_BUF recovery;
766bc82004-10-16Marcus Agehall  volatile int e;
00538d2003-03-26Martin Nilsson  exit_main();
db3e131997-02-06Fredrik Hübinette (Hubbe)  for(e=NELEM(module_list)-1;e>=0;e--)
37b0222002-01-31Marcus Comstedt  { if(SETJMP(recovery)) call_handle_error();
48073c2003-02-05Henrik Grubbström (Grubba)  else { TRACE((stderr, "Exiting static module #%d: \"%s\"...\n", e, module_list[e].name));
37b0222002-01-31Marcus Comstedt  module_list[e].exit();
48073c2003-02-05Henrik Grubbström (Grubba)  }
37b0222002-01-31Marcus Comstedt  UNSETJMP(recovery); }
5267b71995-08-09Fredrik Hübinette (Hubbe) }