1 /****************************************
2 *  Computer Algebra System SINGULAR     *
3 ****************************************/
4 
5 /*
6 * ABSTRACT - initialize SINGULARs components, run Script and start SHELL
7 */
8 
9 
10 #include "kernel/mod2.h"
11 #include "omalloc/omalloc.h"
12 #include "misc/options.h"
13 #include "factory/factory.h"
14 #include "kernel/oswrapper/feread.h"
15 #include "Singular/fevoices.h"
16 #include "kernel/oswrapper/timer.h"
17 
18 #include "ipshell.h"
19 #include "cntrlc.h"
20 #include "links/silink.h"
21 #include "ipid.h"
22 #include "sdb.h"
23 #include "feOpt.h"
24 #include "distrib.h"
25 #include "mmalloc.h"
26 #include "tok.h"
27 #include "fegetopt.h"
28 
29 #include "Singular/countedref.h"
30 #include "Singular/pyobject_setup.h"
31 
32 #include <unistd.h>
33 #ifdef HAVE_NTL
34 #include <NTL/config.h>
35 #endif
36 
37 
38 extern int siInit(char *);
39 #ifdef PSINGULAR
40 GLOBAL_VAR char *global_argv0;
41 #endif
42 
mmInit(void)43 int mmInit( void )
44 {
45 #ifndef X_OMALLOC
46 #if defined(OMALLOC_USES_MALLOC)
47     /* in mmstd.c, for some architectures freeSize() unconditionally uses the *system* free() */
48     /* sage ticket 5344: http://trac.sagemath.org/sage_trac/ticket/5344 */
49     /* do not rely on the default in Singular as libsingular may be different */
50     mp_set_memory_functions(omMallocFunc,omReallocSizeFunc,omFreeSizeFunc);
51 #else
52     mp_set_memory_functions(malloc,reallocSize,freeSize);
53 #endif
54 #endif
55   return 1;
56 }
57 
58 /*0 implementation*/
main(int argc,char ** argv)59 int main(          /* main entry to Singular */
60     int argc,      /* number of parameter */
61     char** argv)   /* parameter array */
62 {
63   mmInit();
64   // Don't worry: ifdef OM_NDEBUG, then all these calls are undef'ed
65   omInitRet_2_Info(argv[0]);
66   omInitGetBackTrace();
67 
68 #ifdef PSINGULAR
69   global_argv0 = argv[0];
70 #endif
71   siInit(argv[0]);
72   init_signals();
73   #ifdef HAVE_NTL
74   #if NTL_MAJOR_VERSION>=10
75   #ifdef NTL_THREAD_BOOST
76   SetNumThreads(feOptValue(FE_OPT_CPUS));
77   #endif
78   #endif
79   #endif
80 
81   // parse command line options
82   int optc, option_index;
83   const char* errormsg;
84   while((optc = fe_getopt_long(argc, argv,
85                                SHORT_OPTS_STRING, feOptSpec, &option_index))
86         != EOF)
87   {
88     if (optc == '?' || optc == 0)
89     {
90       fprintf(stderr, "Use '%s --help' for a complete list of options\n", feArgv0);
91       exit(1);
92     }
93 
94     if (optc != LONG_OPTION_RETURN)
95       option_index = feGetOptIndex(optc);
96 
97     assume(option_index >= 0 && option_index < (int) FE_OPT_UNDEF);
98 
99     if (fe_optarg == NULL &&
100         (feOptSpec[option_index].type == feOptBool ||
101          feOptSpec[option_index].has_arg == optional_argument))
102       errormsg = feSetOptValue((feOptIndex) option_index, (int) 1);
103     else
104       errormsg = feSetOptValue((feOptIndex) option_index, fe_optarg);
105 
106     if (errormsg)
107     {
108       if (fe_optarg == NULL)
109         fprintf(stderr, "Error: Option '--%s' %s\n",
110                feOptSpec[option_index].name, errormsg);
111       else
112         fprintf(stderr, "Error: Option '--%s=%s' %s\n",
113                feOptSpec[option_index].name, fe_optarg, errormsg);
114       fprintf(stderr, "Use '%s --help' for a complete list of options\n", feArgv0);
115       exit(1);
116     }
117     if (optc == 'h') exit(0);
118     switch(option_index)
119     {
120       case FE_OPT_DUMP_VERSIONTUPLE:
121         exit(0);
122         break;
123       default: ;
124     }
125   }
126 
127   /* say hello */
128 
129   if (TEST_V_QUIET)
130   {
131     (printf)(
132 "                     SINGULAR                                 /"
133 #ifndef MAKE_DISTRIBUTION
134 "  Development"
135 #endif
136 "\n"
137 " A Computer Algebra System for Polynomial Computations       /   version %s\n"
138 "                                                           0<\n"
139 " by: W. Decker, G.-M. Greuel, G. Pfister, H. Schoenemann     \\   %s\n"
140 "FB Mathematik der Universitaet, D-67653 Kaiserslautern        \\\n"
141 , VERSION, VERSION_DATE);
142   if (feOptValue(FE_OPT_NO_SHELL)) WarnS("running in restricted mode:"
143     " shell invocation and links are disallowed");
144   }
145   else
146   {
147     if (feOptValue(FE_OPT_SORT)) On(SW_USE_NTL_SORT);
148     dup2(1,2);
149     /* alternative:
150     *    memcpy(stderr,stdout,sizeof(FILE));
151     */
152   }
153 
154 #ifdef SINGULAR_PYOBJECT_SETUP_H
155    pyobject_setup();
156 #endif
157 #ifdef SI_COUNTEDREF_AUTOLOAD
158   countedref_init();
159 #endif
160   errorreported = 0;
161 
162   // -- example for "static" modules ------
163   //load_builtin("huhu.so",FALSE,(SModulFunc_t)huhu_mod_init);
164   //module_help_main("huhu.so","Help for huhu\nhaha\n");
165   //module_help_proc("huhu.so","p","Help for huhu::p\nhaha\n");
166   setjmp(si_start_jmpbuf);
167 
168   // Now, put things on the stack of stuff to do
169   // Last thing to do is to execute given scripts
170   if (fe_optind < argc)
171   {
172     int i = argc - 1;
173     FILE *fd;
174     while (i >= fe_optind)
175     {
176       if ((fd = feFopen(argv[i], "r")) == NULL)
177       {
178         Warn("Can not open %s", argv[i]);
179       }
180       else
181       {
182         fclose(fd);
183         newFile(argv[i]);
184       }
185       i--;
186     }
187   }
188   else
189   {
190     currentVoice=feInitStdin(NULL);
191   }
192 
193   // before scripts, we execute -c, if it was given
194   if (feOptValue(FE_OPT_EXECUTE) != NULL)
195     newBuffer(omStrDup((char*) feOptValue(FE_OPT_EXECUTE)), BT_execute);
196 
197   // first thing, however, is to load .singularrc from Singularpath
198   // and cwd/$HOME (in that order).
199   if (! feOptValue(FE_OPT_NO_RC))
200   {
201     char buf[MAXPATHLEN];
202     FILE * rc = feFopen("." DIR_SEPP ".singularrc", "r", buf);
203     if (rc == NULL) rc = feFopen("~" DIR_SEPP ".singularrc", "r", buf);
204     if (rc == NULL) rc = feFopen(".singularrc", "r", buf);
205 
206     if (rc != NULL)
207     {
208       if (BVERBOSE(V_LOAD_LIB))
209         Print("// ** executing %s\n", buf);
210       fclose(rc);
211       newFile(buf);
212     }
213   }
214 
215   /* start shell */
216   if (fe_fgets_stdin==fe_fgets_dummy)
217   {
218     singular_in_batchmode=TRUE;
219     char *linkname=(char*) feOptValue(FE_OPT_LINK);
220     if((linkname!=NULL)&&(strcmp(linkname,"ssi")==0))
221     {
222       return ssiBatch((char*) feOptValue(FE_OPT_MPHOST),(char*) feOptValue(FE_OPT_MPPORT));
223       //Print("batch: p:%s, h:%s\n",(char*) feOptValue(FE_OPT_MPPORT),(char*) feOptValue(FE_OPT_MPHOST));
224       //exit(0);
225     }
226     else
227     {
228       Print("** missing arguments: -b requires --link/--MPhost/--MPport\n");
229       return 1;
230     }
231   }
232   setjmp(si_start_jmpbuf);
233   yyparse();
234   m2_end(0);
235   return 0;
236 }
237 
238