source: git/Singular/tesths.cc @ 64c6d1

spielwiese
Last change on this file since 64c6d1 was 64c6d1, checked in by Olaf Bachmann <obachman@…>, 26 years ago
* new file/dir layout of LIBs, bin, doc: LIB stays in-place, (new)doc moves to root, tests is removed * LIB loading with "~" * SearchPath is dynamically determined (see febase.cc) * info prog is searched for in bindir * correct handling of .singurlarrc * env variable SINGULARHIST is used for determining history-file * res always returns resolution * configure is more robust (i.e., complains earlier if something is wrong) git-svn-id: file:///usr/local/Singular/svn/trunk@2022 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: tesths.cc,v 1.45 1998-06-03 10:04:26 obachman 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  int i;
281  thisfile = argv[0];
282  BOOLEAN load_std_lib=TRUE;
283  BOOLEAN load_rc = TRUE;
284  char* execute_string = NULL;
285  int optc, option_index;
286
287  // do this first, because -v might print version path
288  feInitPaths(thisfile);
289
290  // parse command line options
291  while((optc = getopt_long(argc, argv,
292                            SHORT_OPTS_STRING, longopts, &option_index))
293        != EOF)
294  {
295    switch(optc)
296    {
297   
298        case 'b':
299#ifdef HAVE_MPSR   
300          feBatch=TRUE;
301          mainSetSingOptionValue(LON_BATCH, (char*) 1);
302          break;
303#else
304          printf("%s: Option `-b' not supported in this configuration\n", argv[0]);
305          mainUsage(argv[0]);
306          exit(1);
307#endif
308
309        case 'h':
310          mainHelp(argv[0]);
311          exit(0);
312
313        case 'q':
314          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
315          mainSetSingOptionValue(LON_QUIET, (char*) 1);
316          break;
317
318        case 't':
319#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
320          fe_use_fgets=TRUE;
321#endif
322          mainSetSingOptionValue(LON_NO_TTY, (char*) 1);
323          break;
324
325        case 'v':
326          printf("Singular version %s  %s  (%d)  %s %s\n",
327                 S_VERSION1,S_VERSION2,
328                 SINGULAR_VERSION_ID,__DATE__,__TIME__);
329          printf("with\n");
330          printf(versionString());
331          printf("\n\n");
332          mainSetSingOptionValue(LON_VERSION, (char*) 1);
333          break;
334
335        case 'x':
336#ifdef HAVE_TCL
337          tclmode = TRUE;
338          mainSetSingOptionValue(LON_TCLMODE, (char*) 1);
339          break;
340#else
341          printf("%s: Option `-x' not supported in this configuration\n", argv[0]);
342          mainUsage(argv[0]);
343          exit(1);
344#endif
345
346        case 'e':
347          if (optarg != NULL)
348          {
349            errno = 0;
350            si_echo = strtol(optarg, NULL, 10);
351            if (errno)
352            {
353              printf("%s: `%s' invalid integer argument for option `--%s'\n", argv[0], optarg, LON_ECHO);
354              mainUsage(argv[0]);
355              exit(1);
356            }
357            if (si_echo < 0 || si_echo > 9)
358            {
359              printf("%s: `%s' argument for option `--%s' is not in valid range 0..9\n",
360                     argv[0], optarg, LON_ECHO);
361              mainUsage(argv[0]);
362              exit(1);
363            }
364            mainSetSingOptionValue(LON_ECHO, optarg);
365          }
366          else
367          {
368            si_echo = 1;
369            mainSetSingOptionValue(LON_ECHO, "1");
370          }
371          break;
372
373        case 'c':
374          execute_string = optarg;
375          mainSetSingOptionValue(LON_EXECUTE, optarg);
376          break;
377   
378        case 'r':
379          errno = 0;
380          siRandomStart = strtol(optarg, NULL, 10);
381          if (errno || siRandomStart == 0)
382          {
383            printf("%s: `%s' invalid integer argument for option `--%s'\n", argv[0], optarg, LON_RANDOM);
384            mainUsage(argv[0]);
385            exit(1);
386          }
387          #ifdef buildin_rand
388          siSeed=siRandomStart;
389          #else
390          srand((unsigned int)siRandomStart);
391          #endif
392          #ifdef HAVE_FACTORY
393          factoryseed(siRandomStart);
394          #endif
395          mainSetSingOptionValue(LON_RANDOM, optarg);
396          break;
397   
398        case 'u':
399          mainSetSingOptionValue(LON_USER_OPTION, optarg);
400          break;
401
402        case LONG_OPTION_RETURN:
403          if (strcmp(longopts[option_index].name, LON_NO_STDLIB) == 0)
404          {
405            load_std_lib = FALSE;
406            mainSetSingOptionValue(LON_NO_STDLIB, (char*) 1);
407          }
408          else if (strcmp(longopts[option_index].name, LON_NO_RC) == 0)
409          {
410            load_rc = FALSE;
411            mainSetSingOptionValue(LON_NO_RC, (char*) 1);
412          }
413          else if (strcmp(longopts[option_index].name, LON_MIN_TIME) == 0)
414          {
415            double mintime = atof(optarg);
416            if (mintime <= 0)
417            {
418              printf("%s: `%s' invalid float argument for option `--%s'\n",
419                     argv[0], optarg, LON_MIN_TIME);
420              mainUsage(argv[0]);
421              exit(1);
422            }
423            SetMinDisplayTime(mintime);
424            mainSetSingOptionValue(LON_MIN_TIME, optarg);
425          }
426#ifdef HAVE_MPSR
427          else if (strcmp(longopts[option_index].name, LON_MP_PORT) == 0)
428          {
429            mainSetSingOptionValue(LON_MP_PORT, optarg);
430          }
431          else if (strcmp(longopts[option_index].name, LON_MP_HOST) == 0)
432          {
433            mainSetSingOptionValue(LON_MP_HOST, optarg);
434          }
435#endif   
436          else if (strcmp(longopts[option_index].name, LON_TICKS_PER_SEC) == 0)
437          {
438            int ticks = atoi(optarg);
439            if (ticks <= 0)
440            {
441              printf("%s: `%s' invalid integer argument for option `--%s'\n",
442                     argv[0], optarg, LON_TICKS_PER_SEC);
443              mainUsage(argv[0]);
444              exit(1);
445            }
446            SetTimerResolution(ticks);
447            mainSetSingOptionValue(LON_TICKS_PER_SEC, optarg);
448          }
449          else
450            // undocumented options
451#ifdef HAVE_MPSR
452            if (strcmp(longopts[option_index].name, LON_MP_MODE) == 0 ||
453                strcmp(longopts[option_index].name, LON_MP_TRANSP) == 0)
454            {
455              /* ignore */
456            }
457          else
458#endif
459            assume(0);
460          break;
461
462        default:
463          // Error message already emmited by getopt_long
464          mainUsage(argv[0]);
465          exit(1);
466    }
467  }
468  /*. say hello */
469  if (BVERBOSE(0) && !feBatch)
470  {
471    printf(
472"                     SINGULAR                             /\n"
473" a Computer Algebra System for Polynomial Computations   /   version %s\n"
474"                                                       0<\n"
475"  by: G.-M. Greuel, G. Pfister, H. Schoenemann           \\   %s\n"
476"FB Mathematik der Universitaet, D-67653 Kaiserslautern    \\\n"
477, S_VERSION1,S_VERSION2);
478  }
479  else
480  if (!feBatch)
481  {
482#ifdef __MWERKS__
483    memcpy(stderr,stdout,sizeof(FILE));
484#else
485    dup2(1,2);
486#endif
487  }
488  slStandardInit();
489  dlInit(thisfile);
490  myynest=0;
491  if (load_std_lib)
492  {
493    int vv=verbose;
494    verbose &= ~Sy_bit(V_LOAD_LIB);
495    iiLibCmd(mstrdup("standard.lib"),TRUE);
496    verbose=vv;
497  }
498  errorreported = 0;
499#ifndef macintosh
500#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
501  fe_set_input_mode();
502#endif
503#endif
504  setjmp(si_start_jmpbuf);
505
506  // Now, put things on the stack of stuff to do
507
508  // Last thing to do is to execute given scripts
509  if (optind < argc)
510  {
511    int i = argc - 1;
512    while (i >= optind)
513    {
514      newFile(argv[i]);
515      i--;
516    }
517  }
518  else
519  {
520    currentVoice=feInitStdin();
521  }
522
523  // before scripts, we execute -c, if it was given
524  // now execute -c, if it was given
525  if (execute_string != NULL)
526    newBuffer(mstrdup(execute_string), BT_execute);
527
528  // first thing, however, is to load .singularrc
529  if (load_rc)
530  {
531    FILE * rc=myfopen(".singularrc","r");
532    if (rc!=NULL)
533    {
534      fclose(rc);
535      newFile(".singularrc");
536    }
537    else
538    {
539      char buf[MAXPATHLEN];
540      char *home = getenv("HOME");
541      if (home != NULL)
542      {
543        strcpy(buf, home);
544        strcat(buf, "/.singularrc");
545        rc = myfopen(buf, "r");
546        if (rc != NULL)
547        {
548          fclose(rc);
549          newFile(buf);
550        }
551      }
552    }
553  }
554
555  /* start shell */
556  if (feBatch)
557  {
558#ifdef HAVE_MPSR
559    extern int Batch_do(const char* port, const char* host);
560    return Batch_do(mainGetSingOptionValue(LON_MP_PORT),
561                    mainGetSingOptionValue(LON_MP_HOST));
562#else
563    assume(0);
564#endif
565  }
566  setjmp(si_start_jmpbuf);
567  yyparse();
568#endif
569  //Print("at very end\n");
570  m2_end(0);
571  return 0;
572}
Note: See TracBrowser for help on using the repository browser.