00001
00008 #define _MAIN_C
00009
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #ifdef WIN32
00013 #include <direct.h>
00014 #include <process.h>
00015 #else
00016 #include <unistd.h>
00017 #endif
00018
00019 #include "globals.h"
00020 #include "legal.h"
00021 #include "cmdarg.h"
00022 #include "module.h"
00023 #include "output.h"
00024 #include "environment.h"
00025 #include "test.h"
00026 #include "random.h"
00027 #include "realtime.h"
00028 #include "save.h"
00029 #include "local.h"
00030 #include "exec.h"
00031 #include "kml.h"
00032
00033 #if defined WIN32 && _DEBUG
00034
00036 void pause_at_exit(void)
00037 {
00038 if (global_pauseatexit)
00039 system("pause");
00040 }
00041 #endif
00042
00043 void delete_pidfile(void)
00044 {
00045 unlink(global_pidfile);
00046 }
00047
00058 int main(int argc,
00059 char *argv[])
00060 {
00061 int rv = 0;
00062 time_t t_load = time(NULL);
00063 global_process_id = getpid();
00064 #if defined WIN32 && _DEBUG
00065 atexit(pause_at_exit);
00066 #endif
00067
00068 if (!output_init(argc,argv) || !exec_init())
00069 exit(6);
00070
00071
00072 if (cmdarg_load(argc,argv)==FAILED)
00073 {
00074 output_fatal("shutdown after command line rejected");
00075
00076
00077
00078
00079
00080 exit(1);
00081 }
00082
00083
00084 random_init();
00085
00086
00087 if (strcmp(global_pidfile,"")!=0)
00088 {
00089 FILE *fp = fopen(global_pidfile,"w");
00090 if (fp==NULL)
00091 {
00092 output_fatal("unable to create pidfile '%s'", global_pidfile);
00093
00094
00095
00096
00097
00098 exit(1);
00099 }
00100 #ifdef WIN32
00101 #define getpid _getpid
00102 #endif
00103 fprintf(fp,"%d\n",getpid());
00104 output_verbose("process id %d written to %s", getpid(), global_pidfile);
00105 fclose(fp);
00106 atexit(delete_pidfile);
00107 }
00108
00109
00110 #ifdef LEGAL_NOTICE
00111 if (strcmp(global_pidfile,"")==0 && legal_notice()==FAILED)
00112 exit(4);
00113 #endif
00114
00115
00116 if (global_test_mode)
00117 {
00118 #ifndef _NO_CPPUNIT
00119 output_message("Entering test mode");
00120 if (test_start(argc,argv)==FAILED)
00121 {
00122 output_fatal("shutdown after startup test failed");
00123
00124
00125
00126
00127
00128
00129
00130 exit(3);
00131 }
00132 exit(0);
00133 #else
00134 output_message("Unit Tests not enabled. Recompile with _NO_CPPUNIT unset");
00135 #endif
00136 }
00137
00138
00139 output_verbose("load time: %d sec", time(NULL) - t_load);
00140 output_verbose("starting up %s environment", global_environment);
00141 if (environment_start(argc,argv)==FAILED)
00142 {
00143 output_fatal("environment startup failed: %s", strerror(errno));
00144
00145
00146
00147
00148
00149 rv = 2;
00150 }
00151
00152
00153 if (global_test_mode)
00154 {
00155 #ifndef _NO_CPPUNIT
00156 output_message("Exiting test mode");
00157 if (test_end(argc,argv)==FAILED)
00158 {
00159 output_error("shutdown after end test failed");
00160 exit(3);
00161 }
00162 #endif
00163 }
00164
00165
00166 if (strcmp(global_savefile,"")!=0)
00167 {
00168 if (saveall(global_savefile)==FAILED)
00169 output_error("save to '%s' failed", global_savefile);
00170 }
00171
00172
00173 if (global_dumpall!=FALSE)
00174 {
00175 output_verbose("dumping module data");
00176 module_dumpall();
00177 }
00178
00179
00180 if (strcmp(global_kmlfile,"")!=0)
00181 kml_dump(global_kmlfile);
00182
00183
00184 output_verbose("shutdown complete");
00185
00186
00187 if (global_profiler)
00188 class_profiles();
00189
00190
00191 locale_pop();
00192
00193
00194 output_verbose("elapsed runtime %d seconds", realtime_runtime());
00195
00196 exit(rv);
00197 }
00198