1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT - initialize SINGULARs components, run Script and start SHELL |
---|
8 | */ |
---|
9 | |
---|
10 | #include <unistd.h> |
---|
11 | #include <string.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stddef.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <time.h> |
---|
16 | #include <errno.h> |
---|
17 | #include "mod2.h" |
---|
18 | #include "tok.h" |
---|
19 | #include "options.h" |
---|
20 | #include "ipshell.h" |
---|
21 | #include "febase.h" |
---|
22 | #include "cntrlc.h" |
---|
23 | #include "omalloc.h" |
---|
24 | #include "silink.h" |
---|
25 | #include "ipid.h" |
---|
26 | #include "timer.h" |
---|
27 | #include "sdb.h" |
---|
28 | #include "fegetopt.h" |
---|
29 | #include "feOpt.h" |
---|
30 | #include "distrib.h" |
---|
31 | #include "version.h" |
---|
32 | #include "slInit.h" |
---|
33 | |
---|
34 | #ifdef HAVE_FACTORY |
---|
35 | #define SI_DONT_HAVE_GLOBAL_VARS |
---|
36 | #include <factory.h> |
---|
37 | #endif |
---|
38 | |
---|
39 | extern int iiInitArithmetic(); |
---|
40 | |
---|
41 | const char *singular_date=__DATE__ " " __TIME__; |
---|
42 | |
---|
43 | #include <si_gmp.h> |
---|
44 | |
---|
45 | int mmInit( void ); |
---|
46 | int mmIsInitialized=mmInit(); |
---|
47 | |
---|
48 | extern "C" |
---|
49 | { |
---|
50 | void omSingOutOfMemoryFunc() |
---|
51 | { |
---|
52 | fprintf(stderr, "\nSingular error: no more memory\n"); |
---|
53 | omPrintStats(stderr); |
---|
54 | m2_end(14); |
---|
55 | /* should never get here */ |
---|
56 | exit(1); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | int mmInit( void ) |
---|
61 | { |
---|
62 | if(mmIsInitialized==0) |
---|
63 | { |
---|
64 | |
---|
65 | #ifndef LIBSINGULAR |
---|
66 | #if defined(OMALLOC_USES_MALLOC) || defined(X_OMALLOC) |
---|
67 | /* in mmstd.c, for some architectures freeSize() unconditionally uses the *system* free() */ |
---|
68 | /* sage ticket 5344: http://trac.sagemath.org/sage_trac/ticket/5344 */ |
---|
69 | /* solution: correctly check OMALLOC_USES_MALLOC from omalloc.h, */ |
---|
70 | /* do not rely on the default in Singular as libsingular may be different */ |
---|
71 | mp_set_memory_functions(omMallocFunc,omReallocSizeFunc,omFreeSizeFunc); |
---|
72 | #else |
---|
73 | mp_set_memory_functions(malloc,reallocSize,freeSize); |
---|
74 | #endif |
---|
75 | #endif // ifndef LIBSINGULAR |
---|
76 | om_Opts.OutOfMemoryFunc = omSingOutOfMemoryFunc; |
---|
77 | #ifndef OM_NDEBUG |
---|
78 | om_Opts.ErrorHook = dErrorBreak; |
---|
79 | #endif |
---|
80 | omInitInfo(); |
---|
81 | #ifdef OM_SING_KEEP |
---|
82 | om_Opts.Keep = OM_SING_KEEP; |
---|
83 | #endif |
---|
84 | } |
---|
85 | mmIsInitialized=1; |
---|
86 | return 1; |
---|
87 | } |
---|
88 | |
---|
89 | #ifdef LIBSINGULAR |
---|
90 | int siInit(char *name) |
---|
91 | { |
---|
92 | // hack such that all shared' libs in the bindir are loaded correctly |
---|
93 | feInitResources(name); |
---|
94 | iiInitArithmetic(); |
---|
95 | |
---|
96 | basePack=(package)omAlloc0(sizeof(*basePack)); |
---|
97 | currPack=basePack; |
---|
98 | idhdl h; |
---|
99 | h=enterid("Top", 0, PACKAGE_CMD, &IDROOT, TRUE); |
---|
100 | IDPACKAGE(h)->language = LANG_TOP; |
---|
101 | IDPACKAGE(h)=basePack; |
---|
102 | currPackHdl=h; |
---|
103 | basePackHdl=h; |
---|
104 | |
---|
105 | slStandardInit(); |
---|
106 | myynest=0; |
---|
107 | if (! feOptValue(FE_OPT_NO_STDLIB)) |
---|
108 | { |
---|
109 | int vv=verbose; |
---|
110 | verbose &= ~Sy_bit(V_LOAD_LIB); |
---|
111 | iiLibCmd(omStrDup("standard.lib"), TRUE,TRUE,TRUE); |
---|
112 | verbose=vv; |
---|
113 | } |
---|
114 | errorreported = 0; |
---|
115 | } |
---|
116 | #endif |
---|
117 | |
---|
118 | #if ! defined(LIBSINGULAR) |
---|
119 | /*0 implementation*/ |
---|
120 | int main( /* main entry to Singular */ |
---|
121 | int argc, /* number of parameter */ |
---|
122 | char** argv) /* parameter array */ |
---|
123 | { |
---|
124 | #ifdef HAVE_FACTORY |
---|
125 | On(SW_USE_NTL); |
---|
126 | Off(SW_USE_GCD_P); |
---|
127 | On(SW_USE_NTL_GCD_0); // On -> seg11 in Old/algnorm, Old/factor... |
---|
128 | On(SW_USE_NTL_GCD_P); // On -> cyle in Short/brnoeth_s: fixed |
---|
129 | On(SW_USE_EZGCD); |
---|
130 | On(SW_USE_CHINREM_GCD); |
---|
131 | //On(SW_USE_fieldGCD); |
---|
132 | //On(SW_USE_EZGCD_P); |
---|
133 | On(SW_USE_QGCD); |
---|
134 | Off(SW_USE_NTL_SORT); // may be changed by an command line option |
---|
135 | #endif |
---|
136 | |
---|
137 | #ifdef INIT_BUG |
---|
138 | jjInitTab1(); |
---|
139 | #endif |
---|
140 | #ifdef GENTABLE |
---|
141 | extern void ttGen1(); |
---|
142 | extern void ttGen2b(); |
---|
143 | extern void ttGen4(); |
---|
144 | extern void mpsr_ttGen(); // For initialization of (CMD, MP_COP) tables |
---|
145 | extern char *iparith_inc; |
---|
146 | #ifdef HAVE_MPSR |
---|
147 | extern char *mpsr_Tok_inc; |
---|
148 | #endif |
---|
149 | mpsr_ttGen(); |
---|
150 | ttGen4(); |
---|
151 | ttGen1(); |
---|
152 | ttGen2b(); |
---|
153 | rename(iparith_inc,"iparith.inc"); |
---|
154 | rename("plural_cmd.xx","plural_cmd.inc"); |
---|
155 | #ifdef HAVE_MPSR |
---|
156 | rename(mpsr_Tok_inc,"mpsr_Tok.inc"); |
---|
157 | #endif |
---|
158 | #else |
---|
159 | // Don't worry: ifdef OM_NDEBUG, then all these calls are undef'ed |
---|
160 | omInitRet_2_Info(argv[0]); |
---|
161 | omInitGetBackTrace(); |
---|
162 | |
---|
163 | /* initialize components */ |
---|
164 | siRandomStart=inits(); |
---|
165 | feOptSpec[FE_OPT_RANDOM].value = (void*) ((long)siRandomStart); |
---|
166 | int optc, option_index; |
---|
167 | const char* errormsg; |
---|
168 | |
---|
169 | // do this first, because -v might print version path |
---|
170 | feInitResources(argv[0]); |
---|
171 | iiInitArithmetic(); |
---|
172 | |
---|
173 | // parse command line options |
---|
174 | while((optc = fe_getopt_long(argc, argv, |
---|
175 | SHORT_OPTS_STRING, feOptSpec, &option_index)) |
---|
176 | != EOF) |
---|
177 | { |
---|
178 | if (optc == '?' || optc == 0) |
---|
179 | { |
---|
180 | fprintf(stderr, "Use '%s --help' for a complete list of options\n", feArgv0); |
---|
181 | exit(1); |
---|
182 | } |
---|
183 | |
---|
184 | if (optc != LONG_OPTION_RETURN) |
---|
185 | option_index = feGetOptIndex(optc); |
---|
186 | |
---|
187 | assume(option_index >= 0 && option_index < (int) FE_OPT_UNDEF); |
---|
188 | |
---|
189 | if (fe_optarg == NULL && |
---|
190 | (feOptSpec[option_index].type == feOptBool || |
---|
191 | feOptSpec[option_index].has_arg == optional_argument)) |
---|
192 | errormsg = feSetOptValue((feOptIndex) option_index, (int) 1); |
---|
193 | else |
---|
194 | errormsg = feSetOptValue((feOptIndex) option_index, fe_optarg); |
---|
195 | |
---|
196 | if (errormsg) |
---|
197 | { |
---|
198 | if (fe_optarg == NULL) |
---|
199 | fprintf(stderr, "Error: Option '--%s' %s\n", |
---|
200 | feOptSpec[option_index].name, errormsg); |
---|
201 | else |
---|
202 | fprintf(stderr, "Error: Option '--%s=%s' %s\n", |
---|
203 | feOptSpec[option_index].name, fe_optarg, errormsg); |
---|
204 | fprintf(stderr, "Use '%s --help' for a complete list of options\n", feArgv0); |
---|
205 | exit(1); |
---|
206 | } |
---|
207 | if (optc == 'h') exit(0); |
---|
208 | } |
---|
209 | |
---|
210 | /* say hello */ |
---|
211 | { |
---|
212 | basePack=(package)omAlloc0(sizeof(*basePack)); |
---|
213 | currPack=basePack; |
---|
214 | idhdl h; |
---|
215 | h=enterid("Top", 0, PACKAGE_CMD, &IDROOT, TRUE); |
---|
216 | IDPACKAGE(h)->language = LANG_TOP; |
---|
217 | IDPACKAGE(h)=basePack; |
---|
218 | currPackHdl=h; |
---|
219 | basePackHdl=h; |
---|
220 | } |
---|
221 | if (TEST_V_QUIET) |
---|
222 | { |
---|
223 | (printf)( |
---|
224 | " SINGULAR /" |
---|
225 | #ifndef MAKE_DISTRIBUTION |
---|
226 | " Development" |
---|
227 | #endif |
---|
228 | "\n" |
---|
229 | " A Computer Algebra System for Polynomial Computations / version %s\n" |
---|
230 | " 0<\n" |
---|
231 | " by: G.-M. Greuel, G. Pfister, H. Schoenemann \\ %s\n" |
---|
232 | "FB Mathematik der Universitaet, D-67653 Kaiserslautern \\\n" |
---|
233 | , S_VERSION1,S_VERSION2); |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | #ifdef HAVE_FACTORY |
---|
238 | if (feOptValue(FE_OPT_SORT)) On(SW_USE_NTL_SORT); |
---|
239 | #endif |
---|
240 | #ifdef HAVE_SDB |
---|
241 | sdb_flags = 0; |
---|
242 | #endif |
---|
243 | dup2(1,2); |
---|
244 | /* alternative: |
---|
245 | * memcpy(stderr,stdout,sizeof(FILE)); |
---|
246 | */ |
---|
247 | } |
---|
248 | slStandardInit(); |
---|
249 | myynest=0; |
---|
250 | if (! feOptValue(FE_OPT_NO_STDLIB)) |
---|
251 | { |
---|
252 | int vv=verbose; |
---|
253 | verbose &= ~Sy_bit(V_LOAD_LIB); |
---|
254 | iiLibCmd(omStrDup("standard.lib"), TRUE,TRUE,TRUE); |
---|
255 | verbose=vv; |
---|
256 | } |
---|
257 | errorreported = 0; |
---|
258 | |
---|
259 | // and again, ifdef OM_NDEBUG this call is undef'ed |
---|
260 | // otherwise, it marks all memory allocated so far as static |
---|
261 | // i.e. as memory which is not mention on omPrintUsedAddr: |
---|
262 | //omMarkMemoryAsStatic(); |
---|
263 | |
---|
264 | setjmp(si_start_jmpbuf); |
---|
265 | |
---|
266 | // Now, put things on the stack of stuff to do |
---|
267 | // Last thing to do is to execute given scripts |
---|
268 | if (fe_optind < argc) |
---|
269 | { |
---|
270 | int i = argc - 1; |
---|
271 | FILE *fd; |
---|
272 | while (i >= fe_optind) |
---|
273 | { |
---|
274 | if ((fd = feFopen(argv[i], "r")) == NULL) |
---|
275 | { |
---|
276 | Warn("Can not open %s", argv[i]); |
---|
277 | } |
---|
278 | else |
---|
279 | { |
---|
280 | fclose(fd); |
---|
281 | newFile(argv[i]); |
---|
282 | } |
---|
283 | i--; |
---|
284 | } |
---|
285 | } |
---|
286 | else |
---|
287 | { |
---|
288 | currentVoice=feInitStdin(NULL); |
---|
289 | } |
---|
290 | |
---|
291 | // before scripts, we execute -c, if it was given |
---|
292 | if (feOptValue(FE_OPT_EXECUTE) != NULL) |
---|
293 | newBuffer(omStrDup((char*) feOptValue(FE_OPT_EXECUTE)), BT_execute); |
---|
294 | |
---|
295 | // first thing, however, is to load .singularrc from Singularpath |
---|
296 | // and cwd/$HOME (in that order). |
---|
297 | if (! feOptValue(FE_OPT_NO_RC)) |
---|
298 | { |
---|
299 | char buf[MAXPATHLEN]; |
---|
300 | FILE * rc = feFopen("." DIR_SEPP ".singularrc", "r", buf); |
---|
301 | if (rc == NULL) rc = feFopen("~" DIR_SEPP ".singularrc", "r", buf); |
---|
302 | if (rc == NULL) rc = feFopen(".singularrc", "r", buf); |
---|
303 | |
---|
304 | if (rc != NULL) |
---|
305 | { |
---|
306 | if (BVERBOSE(V_LOAD_LIB)) |
---|
307 | Print("// ** executing %s\n", buf); |
---|
308 | fclose(rc); |
---|
309 | newFile(buf); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | /* start shell */ |
---|
314 | if (fe_fgets_stdin==fe_fgets_dummy) |
---|
315 | { |
---|
316 | #ifdef HAVE_MPSR |
---|
317 | BatchDoProc batch_do = slInitMPBatchDo(); |
---|
318 | if (batch_do != NULL) |
---|
319 | return (*batch_do)((char*) feOptValue(FE_OPT_MPPORT), |
---|
320 | (char*) feOptValue(FE_OPT_MPHOST)); |
---|
321 | else |
---|
322 | return 1; |
---|
323 | #else |
---|
324 | assume(0); |
---|
325 | #endif |
---|
326 | } |
---|
327 | setjmp(si_start_jmpbuf); |
---|
328 | yyparse(); |
---|
329 | m2_end(0); |
---|
330 | #endif |
---|
331 | return 0; |
---|
332 | } |
---|
333 | #endif // not LIBSINGULAR |
---|
334 | |
---|