source: git/Singular/tesths.cc @ 9a11fe

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