00001
00002
00003
00004
00005 #include "realmain.h"
00006 #include "MALLOC.h"
00007 #include "sciprint.h"
00008 #ifndef _MSC_VER
00009 #include "xscion.h"
00010 #include "getarg.h"
00011 #endif
00012 #include "inffic.h"
00013 #include "scirun.h"
00014 #include "sciquit.h"
00015 #include "getarg.h"
00016
00017 extern void C2F (settmpdir)(void);
00018 extern void sci_clear_and_exit(int n);
00019 extern int C2F(inisci)(int *,int *,int *);
00020 extern void sci_usr1_signal(int n);
00021 #ifdef _MSC_VER
00022 #include "ExceptionMessage.h"
00023 #else
00024 extern int IsNoInteractiveWindow(void);
00025 extern void InitXsession(void);
00026 extern void main_sci (int argc,char ** argv, char *startup, int lstartup,int memory);
00027
00028 #endif
00029
00030 static void strip_blank(char *source);
00031
00032 static int no_startup_flag=0;
00033
00034 #define BSIZE 128
00035 #ifndef PATH_MAX
00036 #define PATH_MAX 1024
00037 #endif
00038
00039 void realmain(int nowin,int no_startup_flag_l,char *initial_script,int initial_script_type,int memory)
00040 {
00041 static int ini=-1;
00042 int ierr=0;
00043 char *startup=(char*)MALLOC(sizeof(char)*PATH_MAX+1);
00044
00045
00046 Set_no_startup_flag(no_startup_flag_l);
00047
00048
00049 C2F(settmpdir)();
00050
00051 #ifdef ENABLESIG
00052 signal(SIGINT,sci_clear_and_exit);
00053 #ifdef SIGBUS
00054 signal(SIGBUS,sci_clear_and_exit);
00055 #endif
00056 signal(SIGSEGV,sci_clear_and_exit);
00057 #if SIGQUIT
00058 signal(SIGQUIT,sci_clear_and_exit);
00059 #endif
00060 #ifdef SIGHUP
00061 signal(SIGHUP,sci_clear_and_exit);
00062 #endif
00063 #ifdef SIGUSR1
00064 signal(SIGUSR1,sci_usr1_signal);
00065 #endif
00066
00067 #ifdef _MSC_VER
00068 signal(SIGILL,sci_clear_and_exit);
00069 signal(SIGFPE,sci_clear_and_exit);
00070 signal(SIGTERM,sci_clear_and_exit);
00071 signal(SIGBREAK,sci_clear_and_exit);
00072 signal(SIGABRT,sci_clear_and_exit);
00073 #endif
00074 #endif
00075
00076
00077
00078 if ( no_startup_flag_l == 0)
00079 {
00080
00081 if ( initial_script != NULL ) switch ( initial_script_type )
00082 {
00083 case 0 :
00084 sprintf(startup,"%s;exec('%s',-1)",get_sci_data_strings(1),initial_script);
00085 break;
00086 case 1 :
00087 sprintf(startup,"%s;%s;",get_sci_data_strings(1),initial_script);
00088 break;
00089 }
00090 else sprintf(startup,"%s;",get_sci_data_strings(1));
00091 }
00092 else
00093 {
00094
00095 if ( initial_script != NULL ) switch ( initial_script_type )
00096 {
00097 case 0 :
00098 sprintf(startup,"exec('%s',-1)",initial_script); break;
00099 case 1 :
00100 sprintf(startup,"%s;",initial_script); break;
00101 }
00102 else sprintf(startup," ");
00103 }
00104
00105 #ifndef _MSC_VER
00106 if ( nowin == 0 )
00107 {
00108 int argc=0;
00109 char **argv=NULL;
00110
00111 argv = create_argv(&argc);
00112
00113
00114 SetXsciOn();
00115 main_sci(argc,argv,startup,strlen(startup),memory);
00116 }
00117 else
00118 {
00119 if (! IsNoInteractiveWindow() )
00120 {
00121 InitXsession();
00122 }
00123
00124 C2F(inisci)(&ini, &memory, &ierr);
00125 if (ierr > 0) sci_exit(1) ;
00126
00127 C2F(scirun)(startup,strlen(startup));
00128 }
00129 #else
00130
00131 C2F(inisci)(&ini, &memory, &ierr);
00132 if (ierr > 0) sci_exit(1) ;
00133
00134
00135 #ifndef _DEBUG
00136 _try
00137 {
00138 C2F(scirun)(startup,strlen(startup));
00139 }
00140 _except (EXCEPTION_EXECUTE_HANDLER)
00141 {
00142 Rerun:
00143 {
00144 ExceptionMessage(GetExceptionCode(),NULL);
00145 }
00146 _try
00147 {
00148 C2F(scirun)("",strlen(""));
00149 }
00150 _except (EXCEPTION_EXECUTE_HANDLER)
00151 {
00152 goto Rerun;
00153 }
00154
00155 }
00156 #else
00157 C2F(scirun)(startup,strlen(startup));
00158 #endif
00159 #endif
00160 FREE(startup);
00161
00162 C2F(sciquit)();
00163 }
00164
00165 void Set_no_startup_flag(int start)
00166 {
00167 no_startup_flag=start;
00168 }
00169
00170 int Get_no_startup_flag(void)
00171 {
00172 return no_startup_flag;
00173 }
00174
00175 #ifndef _MSC_VER
00176 char ** create_argv(int *argc)
00177 {
00178 int i;
00179 char **argv;
00180 *argc = C2F(sciiargc)() + 1;
00181 if ( ( argv = malloc((*argc)*sizeof(char *))) == NULL) return NULL;
00182 for ( i=0 ; i < *argc ; i++)
00183 {
00184 char buf[BSIZE];
00185 C2F(scigetarg)(&i,buf,BSIZE);
00186 buf[BSIZE-1]='\0';
00187 strip_blank(buf);
00188 argv[i] = malloc((strlen(buf)+1)*sizeof(char));
00189 if ( argv[i] == NULL) return NULL;
00190 strcpy(argv[i],buf);
00191 }
00192 return argv;
00193 }
00194 #endif
00195
00196 static void strip_blank(char *source)
00197 {
00198 char *p;
00199 p = source;
00200
00201 while(*p != '\0') p++;
00202 while(p != source)
00203 {
00204 p--;
00205 if(*p != ' ') break;
00206 *p = '\0';
00207 }
00208 }
00209
00210