source: git/Singular/tesths.cc @ e0d91c

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