core/main.c

Go to the documentation of this file.
00001 
00425 #define _MAIN_C
00426 
00427 #include <stdlib.h>
00428 #include <string.h>
00429 #ifdef WIN32
00430 #include <direct.h>
00431 #include <process.h>
00432 #else
00433 #include <unistd.h>
00434 #endif
00435 
00436 #include "globals.h"
00437 #include "legal.h"
00438 #include "cmdarg.h"
00439 #include "module.h"
00440 #include "output.h"
00441 #include "environment.h"
00442 #include "test.h"
00443 #include "random.h"
00444 #include "realtime.h"
00445 #include "save.h"
00446 #include "local.h"
00447 #include "exec.h"
00448 #include "kml.h"
00449 
00450 #if defined WIN32 && _DEBUG 
00451 
00453 void pause_at_exit(void) 
00454 {
00455     if (global_pauseatexit)
00456         system("pause");
00457 }
00458 #endif
00459 
00460 void delete_pidfile(void)
00461 {
00462     unlink(global_pidfile);
00463 }
00464 
00475 int main(int argc, 
00476          char *argv[]) 
00477 {
00478 #if defined WIN32 && _DEBUG 
00479     atexit(pause_at_exit);
00480 #endif
00481     /* main initialization */
00482     if (!exec_init())
00483         exit(6);
00484 
00485     /* process command line arguments */
00486     if (cmdarg_load(argc,argv)==FAILED)
00487     {
00488         output_fatal("shutdown after command line rejected");
00489         exit(1);
00490     }
00491 
00492     /* pidfile */
00493     if (strcmp(global_pidfile,"")!=0)
00494     {
00495         FILE *fp = fopen(global_pidfile,"w");
00496         if (fp==NULL)
00497         {
00498             output_fatal("unable to create pidfile '%s'", global_pidfile);
00499             exit(1);
00500         }
00501 #ifdef WIN32
00502 #define getpid _getpid
00503 #endif
00504         fprintf(fp,"%d\n",getpid());
00505         output_verbose("process id %d written to %s", getpid(), global_pidfile);
00506         fclose(fp);
00507         atexit(delete_pidfile);
00508     }
00509 
00510     /* do legal stuff */
00511     if (strcmp(global_pidfile,"")==0 && legal_notice()==FAILED)
00512         exit(4);
00513 
00514     /* set up the test */
00515     if (global_test_mode)
00516     {
00517 #ifndef _NO_CPPUNIT
00518         output_message("Entering test mode");
00519         if (test_start(argc,argv)==FAILED)
00520         {
00521             output_fatal("shutdown after startup test failed");
00522             exit(3);
00523         }
00524         exit(0); /* There is no environment to speak of, so exit. */
00525 #else
00526         output_message("Unit Tests not enabled.  Recompile with _NO_CPPUNIT unset");
00527 #endif
00528     }
00529     
00530     /* start the processing environment */
00531     output_verbose("starting up %s environment", global_environment);
00532     if (environment_start(argc,argv)==FAILED)
00533     {
00534         output_fatal("environment startup failed: %s", strerror(errno));
00535         exit(2);
00536     }
00537 
00538     /* post process the test */
00539     if (global_test_mode)
00540     {
00541 #ifndef _NO_CPPUNIT
00542         output_message("Exiting test mode");
00543         if (test_end(argc,argv)==FAILED)
00544         {
00545             output_error("shutdown after end test failed");
00546             exit(3);
00547         }
00548 #endif
00549     }
00550 
00551     /* save the model */
00552     if (strcmp(global_savefile,"")!=0)
00553     {
00554         if (saveall(global_savefile)==FAILED)
00555             output_error("save to '%s' failed", global_savefile);
00556     }
00557 
00558     /* do module dumps */
00559     if (global_dumpall!=FALSE)
00560     {
00561         output_verbose("dumping module data");
00562         module_dumpall();
00563     }
00564 
00565     /* KML output */
00566     if (strcmp(global_kmlfile,"")!=0)
00567         kml_dump(global_kmlfile);
00568 
00569     /* wrap up */
00570     output_verbose("shutdown complete");
00571 
00572     /* profile results */
00573     if (global_profiler)
00574         class_profiles();
00575 
00576     /* restore locale */
00577     locale_pop();
00578 
00579     /* compute elapsed runtime */
00580     output_verbose("elapsed runtime %d seconds", realtime_runtime());
00581 
00582     exit(0);
00583 }
00584 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy