source: git/Singular/tesths.cc @ 07f20e1

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