source: git/Singular/tesths.cc @ 6d9cc6

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