source: git/Singular/tesths.cc @ 1a11bc

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