source: git/Singular/tesths.cc @ 473707b

spielwiese
Last change on this file since 473707b was 473707b, checked in by Hans Schönemann <hannes@…>, 24 years ago
* hannes: jet(matrix,int) introduced sdb-fixes for batch mode git-svn-id: file:///usr/local/Singular/svn/trunk@3031 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: tesths.cc,v 1.61 1999-05-07 15:41:35 Singular Exp $ */
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 "version.h"
18#include "mod2.h"
19#include "tok.h"
20#include "ipshell.h"
21#include "sing_dld.h"
22#include "febase.h"
23#include "cntrlc.h"
24#include "mmemory.h"
25#include "silink.h"
26#include "ipid.h"
27#include "timer.h"
28#include "sdb.h"
29#ifdef HAVE_FACTORY
30#define SI_DONT_HAVE_GLOBAL_VARS
31#include <factory.h>
32#endif
33#include "getopt.h"
34
35#ifndef MAXPATHLEN
36#define MAXPATHLEN 1024
37#endif
38
39// define the long option names here
40#define LON_BATCH           "batch"
41#define LON_EXECUTE         "execute"
42#define LON_ECHO            "echo"
43#define LON_HELP            "help"
44#define LON_RANDOM          "random"
45#define LON_QUIET           "quiet"
46#define LON_NO_TTY          "no-tty"
47#define LON_USER_OPTION     "user-option"
48#define LON_VERSION         "verbose"
49#define LON_MIN_TIME        "min-time"
50#define LON_NO_STDLIB       "no-stdlib"
51#define LON_NO_RC           "no-rc"
52#define LON_TICKS_PER_SEC   "ticks-per-sec"
53#define LON_TCLMODE         "tclmode"
54#define LON_MP_PORT         "MPport"
55#define LON_MP_HOST         "MPhost"
56#define LON_NO_WARN         "no-warn"
57#define LON_NO_OUT          "no-out"
58// undocumented options
59#ifdef HAVE_MPSR
60#define LON_MP_TRANSP       "MPtransp"
61#define LON_MP_MODE         "MPmode"
62#endif
63
64#define LONG_OPTION_RETURN 13
65static struct option longopts[] =
66{
67#ifdef HAVE_MPSR
68  {LON_BATCH,             0,  0,  'b'},
69#endif
70  {LON_EXECUTE,           1,  0,  'c'},
71  {LON_ECHO,              2,  0,  'e'},
72  {LON_HELP,              0,  0,  'h'},
73  {LON_QUIET,             0,  0,  'q'},
74  {LON_RANDOM,            1,  0,  'r'},
75  {LON_NO_TTY,            0,  0,  't'},
76  {LON_USER_OPTION,       1,  0,  'u'},
77  {LON_VERSION,           0,  0,  'v'},
78#ifdef HAVE_TCL
79  {LON_TCLMODE,           0,  0,  'x'},
80#endif
81  {LON_NO_STDLIB,         0,  0,  LONG_OPTION_RETURN},
82  {LON_NO_RC,             0,  0,  LONG_OPTION_RETURN},
83  {LON_NO_WARN,           0,  0,  LONG_OPTION_RETURN},
84  {LON_NO_OUT,           0,  0,  LONG_OPTION_RETURN},
85  {LON_MIN_TIME,          1,  0,  LONG_OPTION_RETURN},
86#ifdef HAVE_MPSR
87  {LON_MP_PORT,           1,  0,  LONG_OPTION_RETURN},
88  {LON_MP_HOST,           1,  0,  LONG_OPTION_RETURN},
89#endif
90  {LON_TICKS_PER_SEC,     1,  0,  LONG_OPTION_RETURN},
91// undocumented options
92#ifdef HAVE_MPSR
93  {LON_MP_TRANSP,         1,  0,  LONG_OPTION_RETURN},
94  {LON_MP_MODE,           1,  0,  LONG_OPTION_RETURN},
95#endif
96// terminator
97  { 0, 0, 0, 0 }
98};
99// #define SHORT_OPTS_STRING "bc:e::hqr:tu:v"
100#define SHORT_OPTS_STRING "bhqtvxec:r:u:"
101
102struct sing_option
103{
104  const char*   name;    // as in option
105  const char*   arg_name;// name of argument, if has_arg > 0
106  const char*   help;    // (short) help string
107  char*         val;     // (default) value of option: 0: not set 1:set
108                         // string:"" if has_arg > 0
109};
110
111// mention only documented options here
112static struct sing_option sing_longopts[] =
113{
114#ifdef HAVE_MPSR
115  {LON_BATCH,       0,          "Run in MP batch mode",                                 0},
116#endif
117  {LON_HELP,        0,          "Print help message and exit",                          0},
118  {LON_QUIET,       0,          "Do not print start-up banner and library load messages",            0},
119  {LON_NO_TTY,      0,          "Do not redefine the terminal characteristics",         0},
120  {LON_VERSION,     0,          "Print extended version and configuration info",        0},
121#ifdef HAVE_TCL
122  {LON_TCLMODE,     0,          "Run in TCL mode, i.e., with TCL user interface",       0},
123#endif
124  {LON_ECHO,        "VAL",      "Set value of variable `echo' to (integer) VAL",        ""},
125  {LON_EXECUTE,     "STRING",   "Execute STRING on start-up",                           ""},
126  {LON_RANDOM,      "SEED",     "Seed random generator with integer (integer) SEED",    ""},
127  {LON_USER_OPTION, "STRING",   "Return STRING on `system(\"--user-option\")'",         ""},
128  {LON_NO_STDLIB,   0,          "Do not load `standard.lib' on start-up",               0},
129  {LON_NO_RC,       0,          "Do not execute `.singularrc' file on start-up",        0},
130  {LON_NO_WARN,     0,          "Do not display warning messages",        0},
131  {LON_NO_OUT,      0,          "Suppress all output",        0},
132  {LON_MIN_TIME,    "SECS",     "Do not display times smaller than SECS (in seconds)",  "0.5"},
133#ifdef HAVE_MPSR
134  {LON_MP_PORT,     "PORT",     "Use PORT number for MP conections",                    ""},
135  {LON_MP_HOST,     "HOST",     "Use HOST for MP connections",                          ""},
136#endif
137  {LON_TICKS_PER_SEC, "TICKS",    "Sets unit of timer to TICKS per second",             "1"},
138  { 0, 0, 0, 0 }
139};
140
141static struct sing_option* mainGetSingOption(const char* name)
142{
143  int i = 0;
144  while (sing_longopts[i].name != 0)
145  {
146    if (strcmp(name, sing_longopts[i].name) == 0)
147      return &(sing_longopts[i]);
148    i++;
149  }
150  return NULL;
151}
152
153static void mainSetSingOptionValue(const char* name, char* value)
154{
155  sing_option* sopt = mainGetSingOption(name);
156  if (sopt != NULL)
157  {
158    sopt->val = value;
159  }
160}
161
162
163static char* mainGetSingOptionValue(const char* name)
164{
165  sing_option* sopt = mainGetSingOption(name);
166  if (sopt!=NULL)
167    return sopt->val;
168  return NULL;
169}
170
171
172// Prints help message
173static void mainHelp(const char* name)
174{
175  int i = 0;
176  struct sing_option *sopt;
177  char tmp[20];
178
179  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
180  printf("   %s [options] [file1 [file2 ...]]\n", name);
181  printf("Options:\n");
182
183  while (longopts[i].name != 0)
184  {
185    sopt = mainGetSingOption(longopts[i].name);
186    if (sopt != NULL)
187    {
188      if (longopts[i].has_arg > 0)
189      {
190        if  (longopts[i].has_arg > 1)
191          sprintf(tmp, "%s[=%s]", longopts[i].name, sopt->arg_name);
192        else
193          sprintf(tmp, "%s=%s", longopts[i].name, sopt->arg_name);
194
195        printf(" %c%c --%-19s %s\n",
196               (longopts[i].val != 0 ? '-' : ' '),
197               (longopts[i].val != 0 ? longopts[i].val : ' '),
198               tmp,
199               sopt->help);
200      }
201      else
202      {
203        printf(" %c%c --%-19s %s\n",
204               (longopts[i].val != 0 ? '-' : ' '),
205               (longopts[i].val != 0 ? longopts[i].val : ' '),
206               longopts[i].name,
207               sopt->help);
208      }
209    }
210#ifndef NDEBUG
211    else
212      printf("--%s Undocumented\n", longopts[i].name);
213#endif
214    i++;
215  }
216
217  printf("\nFor more information, type `help;' from within Singular or visit\n");
218  printf("http://www.mathematik.uni-kl.de/~zca/Singular or consult the\n");
219  printf("Singular manual (available as on-line info or printed manual).\n");
220}
221
222// Prints usage message
223static void mainUsage(char* argv0)
224{
225  printf("Use `%s --help' for a complete list of options\n", argv0);
226}
227
228#ifndef NDEBUG
229void mainOptionValues()
230{
231  int i = 0;
232  struct sing_option *sopt;
233
234  while (longopts[i].name != 0)
235  {
236    sopt = mainGetSingOption(longopts[i].name);
237    if (sopt != NULL)
238    {
239      if (sopt->val == NULL || sopt->val == (char*) 1)
240        Print("// --%-10s %d\n", sopt->name, sopt->val);
241      else
242        Print("// --%-10s %s\n", sopt->name, sopt->val);
243    }
244    else
245      Print("// --%s Undocumented \n", longopts[i].name);
246    i++;
247  }
248}
249#endif // NDEBUG
250
251BOOLEAN mainGetSingOptionValue(const char* name, char** val)
252{
253  sing_option* sopt = mainGetSingOption(name);
254  if (sopt == NULL)
255  {
256    *val = NULL;
257    return FALSE;
258  }
259  else
260  {
261    *val = sopt->val;
262    return TRUE;
263  }
264}
265
266/*0 implementation*/
267int main(          /* main entry to Singular */
268    int argc,      /* number of parameter */
269    char** argv)   /* parameter array */
270{
271  char * thisfile;
272  /* initialize components */
273  siRandomStart=inits();
274#ifdef INIT_BUG
275  jjInitTab1();
276#endif
277#ifdef GENTABLE
278  extern void ttGen1();
279  extern void ttGen2();
280  #ifndef __MWERKS__
281    extern mpsr_ttGen(); // For initialization of (CMD, MP_COP) tables
282    mpsr_ttGen();
283  #endif
284  ttGen1();
285  ttGen2();
286#else
287  thisfile = argv[0];
288  BOOLEAN load_std_lib=TRUE;
289  BOOLEAN load_rc = TRUE;
290  char* execute_string = NULL;
291  int optc, option_index;
292
293  // do this first, because -v might print version path
294#ifndef __MWERKS__
295  feInitPaths(thisfile);
296#endif
297
298  // parse command line options
299  while((optc = getopt_long(argc, argv,
300                            SHORT_OPTS_STRING, longopts, &option_index))
301        != EOF)
302  {
303    switch(optc)
304    {
305
306        case 'b':
307#ifdef HAVE_MPSR
308          feBatch=TRUE;
309          mainSetSingOptionValue(LON_BATCH, (char*) 1);
310          break;
311#else
312          printf("%s: Option `-b' not supported in this configuration\n", argv[0]);
313          mainUsage(argv[0]);
314          exit(1);
315#endif
316
317        case 'h':
318          mainHelp(argv[0]);
319          exit(0);
320
321        case 'q':
322          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
323          mainSetSingOptionValue(LON_QUIET, (char*) 1);
324          break;
325
326        case 't':
327#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
328          fe_use_fgets=TRUE;
329#endif
330          mainSetSingOptionValue(LON_NO_TTY, (char*) 1);
331          break;
332
333        case 'v':
334          printf("Singular for %s version %s  (%d)  %s %s\n",
335                 S_UNAME, S_VERSION1,
336                 SINGULAR_VERSION_ID,__DATE__,__TIME__);
337          printf("with\n");
338          printf(versionString());
339          printf("\n\n");
340          mainSetSingOptionValue(LON_VERSION, (char*) 1);
341          break;
342
343        case 'x':
344#ifdef HAVE_TCL
345          tclmode = TRUE;
346          mainSetSingOptionValue(LON_TCLMODE, (char*) 1);
347          verbose|=Sy_bit(V_SHOW_MEM);
348          break;
349#else
350          printf("%s: Option `-x' not supported in this configuration\n", argv[0]);
351          mainUsage(argv[0]);
352          exit(1);
353#endif
354
355        case 'e':
356          if (optarg != NULL)
357          {
358            errno = 0;
359            si_echo = strtol(optarg, NULL, 10);
360            if (errno)
361            {
362              printf("%s: `%s' invalid integer argument for option `--%s'\n", argv[0], optarg, LON_ECHO);
363              mainUsage(argv[0]);
364              exit(1);
365            }
366            if (si_echo < 0 || si_echo > 9)
367            {
368              printf("%s: `%s' argument for option `--%s' is not in valid range 0..9\n",
369                     argv[0], optarg, LON_ECHO);
370              mainUsage(argv[0]);
371              exit(1);
372            }
373            mainSetSingOptionValue(LON_ECHO, optarg);
374          }
375          else
376          {
377            si_echo = 1;
378            mainSetSingOptionValue(LON_ECHO, "1");
379          }
380          break;
381
382        case 'c':
383          execute_string = optarg;
384          mainSetSingOptionValue(LON_EXECUTE, optarg);
385          break;
386
387        case 'r':
388          errno = 0;
389          siRandomStart = strtol(optarg, NULL, 10);
390          if (errno || siRandomStart == 0)
391          {
392            printf("%s: `%s' invalid integer argument for option `--%s'\n", argv[0], optarg, LON_RANDOM);
393            mainUsage(argv[0]);
394            exit(1);
395          }
396          #ifdef buildin_rand
397          siSeed=siRandomStart;
398          #else
399          srand((unsigned int)siRandomStart);
400          #endif
401          #ifdef HAVE_FACTORY
402          factoryseed(siRandomStart);
403          #endif
404          mainSetSingOptionValue(LON_RANDOM, optarg);
405          break;
406
407        case 'u':
408          mainSetSingOptionValue(LON_USER_OPTION, optarg);
409          break;
410
411        case LONG_OPTION_RETURN:
412          if (strcmp(longopts[option_index].name, LON_NO_STDLIB) == 0)
413          {
414            load_std_lib = FALSE;
415            mainSetSingOptionValue(LON_NO_STDLIB, (char*) 1);
416          }
417          else if (strcmp(longopts[option_index].name, LON_NO_RC) == 0)
418          {
419            load_rc = FALSE;
420            mainSetSingOptionValue(LON_NO_RC, (char*) 1);
421          }
422          else if (strcmp(longopts[option_index].name, LON_NO_WARN) == 0)
423          {
424            feWarn = FALSE;
425            mainSetSingOptionValue(LON_NO_WARN, (char*) 1);
426          }
427          else if (strcmp(longopts[option_index].name, LON_NO_OUT) == 0)
428          {
429            feOut = FALSE;
430            mainSetSingOptionValue(LON_NO_OUT, (char*) 1);
431          }
432          else if (strcmp(longopts[option_index].name, LON_MIN_TIME) == 0)
433          {
434            double mintime = atof(optarg);
435            if (mintime <= 0)
436            {
437              printf("%s: `%s' invalid float argument for option `--%s'\n",
438                     argv[0], optarg, LON_MIN_TIME);
439              mainUsage(argv[0]);
440              exit(1);
441            }
442            SetMinDisplayTime(mintime);
443            mainSetSingOptionValue(LON_MIN_TIME, optarg);
444          }
445#ifdef HAVE_MPSR
446          else if (strcmp(longopts[option_index].name, LON_MP_PORT) == 0)
447          {
448            mainSetSingOptionValue(LON_MP_PORT, optarg);
449          }
450          else if (strcmp(longopts[option_index].name, LON_MP_HOST) == 0)
451          {
452            mainSetSingOptionValue(LON_MP_HOST, optarg);
453          }
454#endif
455          else if (strcmp(longopts[option_index].name, LON_TICKS_PER_SEC) == 0)
456          {
457            int ticks = atoi(optarg);
458            if (ticks <= 0)
459            {
460              printf("%s: `%s' invalid integer argument for option `--%s'\n",
461                     argv[0], optarg, LON_TICKS_PER_SEC);
462              mainUsage(argv[0]);
463              exit(1);
464            }
465            SetTimerResolution(ticks);
466            mainSetSingOptionValue(LON_TICKS_PER_SEC, optarg);
467          }
468          else
469            // undocumented options
470#ifdef HAVE_MPSR
471            if (strcmp(longopts[option_index].name, LON_MP_MODE) == 0 ||
472                strcmp(longopts[option_index].name, LON_MP_TRANSP) == 0)
473            {
474              /* ignore */
475            }
476          else
477#endif
478            assume(0);
479          break;
480
481        default:
482          // Error message already emmited by getopt_long
483          mainUsage(argv[0]);
484          exit(1);
485    }
486  }
487  /*. say hello */
488  {
489    idhdl h;
490    namespaceroot->push(NULL, "Top", 0, TRUE);
491#ifdef HAVE_NAMESPACES
492    h=enterid(mstrdup("Top"), 0, PACKAGE_CMD, &IDROOT, TRUE);
493    IDPACKAGE(h) = NSPACK(namespaceroot);
494    IDPACKAGE(h)->language = LANG_TOP;
495#endif /* HAVE_NAMESPACES */
496  }
497  if (BVERBOSE(0) && !feBatch)
498  {
499    printf(
500"                     SINGULAR                             /"
501#ifndef MAKE_DISTRIBUTION
502"  Development"
503#endif
504"\n"
505" A Computer Algebra System for Polynomial Computations   /   version %s\n"
506"                                                       0<\n"
507"     by: G.-M. Greuel, G. Pfister, H. Schoenemann        \\   %s\n"
508#ifdef SDRING
509"       preliminary experimental version\n"
510#endif
511"FB Mathematik der Universitaet, D-67653 Kaiserslautern    \\\n"
512, S_VERSION1,S_VERSION2);
513  }
514  else
515  {
516    sdb_flags = 0;
517    if (!feBatch)
518    {
519#ifdef __MWERKS__
520      memcpy(stderr,stdout,sizeof(FILE));
521#else
522      dup2(1,2);
523#endif
524    }
525  }
526  slStandardInit();
527  dlInit(thisfile);
528  myynest=0;
529  if (load_std_lib)
530  {
531    int vv=verbose;
532    verbose &= ~Sy_bit(V_LOAD_LIB);
533#ifdef HAVE_NAMESPACES
534    iiLibCmd(mstrdup("standard.lib"),TRUE, TRUE);
535#else /* HAVE_NAMESPACES */
536    iiLibCmd(mstrdup("standard.lib"), TRUE);
537#endif /* HAVE_NAMESPACES */
538    verbose=vv;
539  }
540  errorreported = 0;
541  mmMarkInitDBMCB();
542#ifndef macintosh
543#if defined(HAVE_READLINE)
544  fe_set_input_mode();
545#endif
546#endif
547  setjmp(si_start_jmpbuf);
548
549  // Now, put things on the stack of stuff to do
550
551  // Last thing to do is to execute given scripts
552  if (optind < argc)
553  {
554    int i = argc - 1;
555    while (i >= optind)
556    {
557      newFile(argv[i]);
558      i--;
559    }
560  }
561  else
562  {
563    currentVoice=feInitStdin();
564  }
565
566  // before scripts, we execute -c, if it was given
567  // now execute -c, if it was given
568  if (execute_string != NULL)
569    newBuffer(mstrdup(execute_string), BT_execute);
570
571  // first thing, however, is to load .singularrc
572  if (load_rc)
573  {
574    FILE * rc=myfopen(".singularrc","r");
575    if (rc!=NULL)
576    {
577      fclose(rc);
578      newFile(".singularrc");
579    }
580    else
581    {
582      char buf[MAXPATHLEN];
583      char *home = getenv("HOME");
584      if (home != NULL)
585      {
586        strcpy(buf, home);
587        strcat(buf, "/.singularrc");
588        rc = myfopen(buf, "r");
589        if (rc != NULL)
590        {
591          fclose(rc);
592          newFile(buf);
593        }
594      }
595    }
596  }
597
598  /* start shell */
599  if (feBatch)
600  {
601#ifdef HAVE_MPSR
602    extern int Batch_do(const char* port, const char* host);
603    return Batch_do(mainGetSingOptionValue(LON_MP_PORT),
604                    mainGetSingOptionValue(LON_MP_HOST));
605#else
606    assume(0);
607#endif
608  }
609  setjmp(si_start_jmpbuf);
610  yyparse();
611#endif
612  //Print("at very end\n");
613  m2_end(0);
614  return 0;
615}
Note: See TracBrowser for help on using the repository browser.