source: git/Singular/feOpt.cc @ f69c6c

fieker-DuValspielwiese
Last change on this file since f69c6c was 8f02de, checked in by Hans Schoenemann <hannes@…>, 13 years ago
tr.263: help for -r fixed git-svn-id: file:///usr/local/Singular/svn/trunk@13875 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.5 KB
RevLine 
[c06a32]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[341696]4/* $Id$ */
[c06a32]5/*
6* ABSTRACT: Implementation of option buisness
7*/
8
[50cbdc]9#include <string.h>
[f3a470]10#include <stdlib.h>
[599326]11#include <kernel/mod2.h>
12#include <Singular/feOpt.h>
[487f79]13#if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
[599326]14#include <kernel/options.h>
[487f79]15#endif
[c06a32]16
17// Define here which cmd-line options are recognized
18struct fe_option feOptSpec[] =
19{
20//
[3b295e]21//  Has to be of the form
22//    {name, has_arg, val,
23//     arg_name, help, type, value, set}
[c06a32]24//  where:
25//
26//        name   is the name of the long option.
[3b295e]27//
[c06a32]28//        has_arg
29//               is:  no_argument (or 0) if the option does not take
30//               an argument, required_argument (or 1) if the option
31//               requires  an  argument, or optional_argument (or 2)
32//               if the option takes an optional argument.
[3b295e]33//
[c06a32]34//        val    is  the  value  to  return,  or  to  load  into the
35//               variable pointed to by flag.
36//               NEEDS TO BE LONG_OPTION_RETURN, for long option
37//                           short option char,  for short option
38//
[3b295e]39//        arg_name if set, uses this value as name for argument in
[c06a32]40//                display of help
[3b295e]41//
[c06a32]42//        help  one-line description of option
43//
[3b295e]44//        type  one of feOptUntyped (value is never set),
[b6f537]45//                     feOptBool, feOptInt, feOptString
[3b295e]46//
[c06a32]47//        value (default) value of option
[3b295e]48//
49//       set   only relevant for feOptString:
[c06a32]50//             1: if value different from default value
[3b295e]51//             0: otherwise
[c06a32]52//
[3b295e]53// The order in which options are specified is the order in which
[c06a32]54// their help is printed on -h
55//
56// Options whose hel starts with an "//" are considered undocumented,
57// i.e., their help is not printed on -h
[3b295e]58//
[ef0124]59#if defined(ESINGULAR) || defined(TSINGULAR)
[c06a32]60#ifdef ESINGULAR
61// options only relevant for ESINGULAR
[3b295e]62  {"emacs",         required_argument,      LONG_OPTION_RETURN,
[c06a32]63   "EMACS",     "Use EMACS as emacs program to run Singular",          feOptString, 0,   0},
64
[3b295e]65  {"emacs-dir",         required_argument,  LONG_OPTION_RETURN,
[c06a32]66   "DIR",       "Use DIR as directory to look for emacs lisp files",   feOptString, 0,   0},
67
[3b295e]68  {"emacs-load",        required_argument,  LONG_OPTION_RETURN,
[c06a32]69   "FILE",      "Load FILE on emacs start-up, instead of default",     feOptString, 0,   0},
[ef0124]70#else
71  {"xterm",         required_argument,      LONG_OPTION_RETURN,
[b5398a3]72   "XTERM",     "Use XTERM as terminal program to run Singular",       feOptString, 0,   0},
[a3bc95e]73#endif
[3b295e]74
75  {"singular",          required_argument,  LONG_OPTION_RETURN,
[c06a32]76   "PROG",      "Start PROG as Singular program within emacs",         feOptString, 0,   0},
77
[ef0124]78  {"no-call",     no_argument,        LONG_OPTION_RETURN,
[b5398a3]79   0,          "Do not start program. Print call to stdout",           feOptBool,   0,   0},
[c06a32]80#endif
81
[3b295e]82  {"batch",             no_argument,        'b',
[b5398a3]83   0,          "Run in batch mode",                                    feOptBool,    0,     0},
[c06a32]84
[3b295e]85  {"execute",           required_argument,  'c',
[c06a32]86   "STRING",   "Execute STRING on start-up",                           feOptString, 0,   0},
[3b295e]87
88  {"sdb",               no_argument,        'd',
[c06a32]89   0,          "Enable source code debugger (experimental)",           feOptBool,    0,      0},
90
[3b295e]91  {"echo",              optional_argument,  'e',
[c06a32]92   "VAL",       "Set value of variable `echo' to (integer) VAL",        feOptInt,    0,      0},
[3b295e]93
94  {"help",              no_argument,        'h',
[b6f537]95   0,          "Print help message and exit",                          feOptUntyped,    0,      0},
[c06a32]96
[3b295e]97  {"quiet",             no_argument,        'q',
[c06a32]98   0,          "Do not print start-up banner and lib load messages",   feOptBool,    0,      0},
[adf4ba]99  {"sort",             no_argument,        's',
100   0,          "// Sort NTL results",                                  feOptBool,    0,      0},
[c06a32]101
[3b295e]102  {"random",            required_argument,  'r',
[8f02de]103   "SEED",     "Seed random generator with (integer) SEED",            feOptInt,    0,      0},
[c06a32]104
[3b295e]105  {"no-tty",            no_argument,        't',
[c06a32]106   0,          "Do not redefine the terminal characteristics",         feOptBool,    0,      0},
107
[3b295e]108  {"user-option",       required_argument,  'u',
[c06a32]109   "STRING",   "Return STRING on `system(\"--user-option\")'",         feOptString, 0,   0},
110
[3b295e]111  {"version",           no_argument,        'v',
[b6f537]112   0,          "Print extended version and configuration info",        feOptUntyped,    0,      0},
[c06a32]113
114
[3b295e]115  {"allow-net",         no_argument,        LONG_OPTION_RETURN,
[6123fa2]116   0,          "Allow to fetch (html) help pages from the net",                feOptBool,    0,      0},
117
[3b295e]118  {"browser",           required_argument,  LONG_OPTION_RETURN,
[d5f3509]119   "BROWSER",  "Display help in BROWSER (see help.cnf)",       feOptString, 0,   0},
[c06a32]120
121#ifndef ESINGULAR
[3b295e]122  {"emacs",             no_argument,        LONG_OPTION_RETURN,
[c06a32]123   0,          "Set defaults for running within emacs",                feOptBool,    0,      0},
124#endif
125
[3b295e]126  {"no-stdlib",         no_argument,        LONG_OPTION_RETURN,
[c06a32]127   0,          "Do not load `standard.lib' on start-up",               feOptBool,    0,      0},
[3b295e]128
129  {"no-rc",             no_argument,        LONG_OPTION_RETURN,
[6123fa2]130   0,          "Do not execute `.singularrc' file(s) on start-up",     feOptBool,    0,      0},
[c06a32]131
[3b295e]132  {"no-warn",           no_argument,        LONG_OPTION_RETURN,
[c06a32]133   0,          "Do not display warning messages",                      feOptBool,    0,      0},
134
[3b295e]135  {"no-out",            no_argument,        LONG_OPTION_RETURN,
[c06a32]136   0,          "Suppress all output",                                  feOptBool,    0,      0},
137
[3b295e]138  {"min-time",          required_argument,  LONG_OPTION_RETURN,
[a70441f]139  "SECS",     "Do not display times smaller than SECS (in seconds)",   feOptString, (void*) "0.5",  0},
[c06a32]140
[3b295e]141  {"MPport",           required_argument,   LONG_OPTION_RETURN,
[425e33c]142   "PORT",     "Use PORT number for conections",                       feOptString,    0,      0},
[c06a32]143
[3b295e]144  {"MPhost",           required_argument,   LONG_OPTION_RETURN,
[425e33c]145   "HOST",     "Use HOST for connections",                             feOptString,    0,   0},
146
147  {"link",           required_argument,   LONG_OPTION_RETURN,
148   "LINK",     "Use LINK for connections",                             feOptString,    0,   0},
[d2f3863]149
[425e33c]150#ifdef HAVE_MPSR
[d2f3863]151  {"MPrsh",           required_argument,   LONG_OPTION_RETURN,
[425e33c]152   "RSH",     "Use RSH for MP connections",                            feOptString,    0,   0},
[c06a32]153#endif
154
[3b295e]155  {"ticks-per-sec",     required_argument,  LONG_OPTION_RETURN,
[c06a32]156   "TICKS",     "Sets unit of timer to TICKS per second",               feOptInt,    (void*)1,      0},
157
158// undocumented options
159#ifdef HAVE_MPSR
[3b295e]160  {"MPtransp",         required_argument,   LONG_OPTION_RETURN,
[c06a32]161   "TRANSP",    "// Use TRANSP for MP connections",                     feOptString,    0,   0},
[3b295e]162
163  {"MPmode",           required_argument,   LONG_OPTION_RETURN,
[c06a32]164   "MODE",      "// Use MODE for MP connections",                       feOptString,    0,   0},
165#endif
[3b295e]166
[c06a32]167// terminator -- do NOT remove
[3b295e]168  { 0, 0, 0, 0, 0, feOptInt, 0, 0}
[c06a32]169};
170
[adf4ba]171const char SHORT_OPTS_STRING[] = "bdhqstvxec:r:u:";
[c06a32]172
173//////////////////////////////////////////////////////////////
174//
175// Generation of feOptIndex
176//
177#ifdef GENERATE_OPTION_INDEX
178
179#include <stdio.h>
[07c5ee]180#include <unistd.h>
[5a39d12]181#include <stdlib.h>
[83c63c]182int main()
[c06a32]183{
184  FILE* fd;
185#ifdef ESINGULAR
[4df19c]186  fd = fopen("feOptES.xx", "w");
[ef0124]187#elif defined(TSINGULAR)
[4df19c]188  fd = fopen("feOptTS.xx", "w");
[a3bc95e]189#else
[e287fa]190  fd = fopen("feOpt.xx", "w");
[c06a32]191#endif
192
193  if (fd == NULL) exit(1);
194
195  int i = 0;
[3b295e]196
197  fputs("typedef enum\n{\n", fd);
198
[c06a32]199  while (feOptSpec[i].name != NULL)
200  {
201    const char* name = feOptSpec[i].name;
202    fputs("FE_OPT_", fd);
203    while (*name != 0)
204    {
[3b295e]205      if (*name == '-')
[c06a32]206      {
207        putc('_', fd);
208      }
209      else if (*name >= 97 && *name <= 122)
210      {
211        putc(*name - 32, fd);
212      }
213      else
214      {
215        putc(*name, fd);
216      }
217      name++;
218    }
219    if (i == 0)
220    {
221      fputs("=0", fd);
222    }
223    i++;
224    fputs(",\n  ", fd);
225  }
[3b295e]226
[c06a32]227  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
228  fclose(fd);
[4df19c]229#ifdef ESINGULAR
230  rename("feOptES.xx", "feOptES.inc");
231#elif defined(TSINGULAR)
232  rename("feOptTS.xx", "feOptTS.inc");
233#else
234  rename("feOpt.xx", "feOpt.inc");
235#endif
[83c63c]236  return(0);
[c06a32]237}
238
239#else // ! GENERATE_OPTION_INDEX
240
241///////////////////////////////////////////////////////////////
242//
243// Getting Values
244//
245
246feOptIndex feGetOptIndex(const char* name)
247{
248  int opt = 0;
[3b295e]249
[c06a32]250  while (opt != (int) FE_OPT_UNDEF)
251  {
[3b295e]252    if (strcmp(feOptSpec[opt].name, name) == 0)
[c06a32]253      return (feOptIndex) opt;
254    opt = opt + 1;
255  }
256  return FE_OPT_UNDEF;
257}
258
259feOptIndex feGetOptIndex(int optc)
260{
261  int opt = 0;
[3b295e]262
[c06a32]263  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
[3b295e]264
[c06a32]265  while (opt != (int) FE_OPT_UNDEF)
266  {
267    if (feOptSpec[opt].val == optc)
268      return (feOptIndex) opt;
269    opt = opt + 1;
270  }
271  return FE_OPT_UNDEF;
272}
273
[2272c33]274void* feGetOptValue(feOptIndex opt)
275{
276  return feOptSpec[(int)opt].value;
277}
278
[c06a32]279///////////////////////////////////////////////////////////////
280//
281// Setting Values
282//
283static void feOptHelp(const char* name);
284//
285// Return: NULL -- everything ok
286//         "error-string" on error
[ef0124]287#if !defined(ESINGULAR) && !defined(TSINGULAR)
[b1dfaf]288#include <omalloc/omalloc.h>
[599326]289#include <kernel/febase.h>
290#include <Singular/ipshell.h>
291#include <Singular/tok.h>
292#include <Singular/sdb.h>
293#include <Singular/cntrlc.h>
294#include <kernel/timer.h>
[c06a32]295
296#ifdef HAVE_FACTORY
297#define SI_DONT_HAVE_GLOBAL_VARS
[b1dfaf]298#include <factory/factory.h>
[c06a32]299#endif
300#include <errno.h>
301
[0ec43a]302static const char* feOptAction(feOptIndex opt);
303const char* feSetOptValue(feOptIndex opt, char* optarg)
[c06a32]304{
305  if (opt == FE_OPT_UNDEF) return "option undefined";
[3b295e]306
[b6f537]307  if (feOptSpec[opt].type != feOptUntyped)
[c06a32]308  {
[b6f537]309    if (feOptSpec[opt].type != feOptString)
[c06a32]310    {
[b6f537]311      if (optarg != NULL)
312      {
313        errno = 0;
314        feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
315        if (errno) return "invalid integer argument";
316      }
317      else
318      {
319        feOptSpec[opt].value = (void*) 0;
320      }
[c06a32]321    }
322    else
323    {
[b6f537]324      assume(feOptSpec[opt].type == feOptString);
325      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
[c232af]326        omFree(feOptSpec[opt].value);
[b6f537]327      if (optarg != NULL)
[c232af]328        feOptSpec[opt].value = omStrDup(optarg);
[b6f537]329      else
330        feOptSpec[opt].value = NULL;
331      feOptSpec[opt].set = 1;
[c06a32]332    }
333  }
334  return feOptAction(opt);
335}
336
[0ec43a]337const char* feSetOptValue(feOptIndex opt, int optarg)
[c06a32]338{
339  if (opt == FE_OPT_UNDEF) return "option undefined";
[3b295e]340
[b6f537]341  if (feOptSpec[opt].type != feOptUntyped)
342  {
343    if (feOptSpec[opt].type == feOptString)
344      return "option value needs to be an integer";
[3b295e]345
[b6f537]346    feOptSpec[opt].value = (void*) optarg;
347  }
[c06a32]348  return feOptAction(opt);
349}
350
[0ec43a]351static const char* feOptAction(feOptIndex opt)
[c06a32]352{
353  // do some special actions
354  switch(opt)
355  {
356      case FE_OPT_BATCH:
[b6f537]357        if (feOptSpec[FE_OPT_BATCH].value)
358          fe_fgets_stdin=fe_fgets_dummy;
[c06a32]359        return NULL;
360
361      case FE_OPT_HELP:
362        feOptHelp(feArgv0);
363        return NULL;
364
365      case FE_OPT_QUIET:
[b6f537]366        if (feOptSpec[FE_OPT_QUIET].value)
367          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
368        else
369          verbose |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
[c06a32]370        return NULL;
371
372      case FE_OPT_NO_TTY:
373#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
[b6f537]374        if (feOptSpec[FE_OPT_NO_TTY].value)
375          fe_fgets_stdin=fe_fgets;
[c06a32]376#endif
377        return NULL;
378
379      case FE_OPT_SDB:
[50cbdc]380      #ifdef HAVE_SDB
[b6f537]381        if (feOptSpec[FE_OPT_SDB].value)
382          sdb_flags = 1;
383        else
384          sdb_flags = 0;
[f3a470]385      #endif
[c06a32]386        return NULL;
387
388      case FE_OPT_VERSION:
[6b4ff12]389        printf("%s",versionString());
[c06a32]390        return NULL;
391
392      case FE_OPT_ECHO:
[7447d8]393        si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
[c06a32]394        if (si_echo < 0 || si_echo > 9)
395          return "argument of option is not in valid range 0..9";
396        return NULL;
[3b295e]397
[b6f537]398      case FE_OPT_RANDOM:
[7447d8]399        siRandomStart = (unsigned int) ((unsigned long)
400                                          (feOptSpec[FE_OPT_RANDOM].value));
[c06a32]401        siSeed=siRandomStart;
402#ifdef HAVE_FACTORY
403        factoryseed(siRandomStart);
404#endif
405        return NULL;
406
407      case FE_OPT_EMACS:
[b6f537]408        if (feOptSpec[FE_OPT_EMACS].value)
409        {
410          // print EmacsDir and InfoFile so that Emacs
411          // mode can pcik it up
412          Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
413                                feResource('e' /*"EmacsDir"*/) : ""));
414          Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
415                                feResource('i' /*"InfoFile"*/) : ""));
416        }
[c06a32]417        return NULL;
418
419      case FE_OPT_NO_WARN:
[b6f537]420        if (feOptSpec[FE_OPT_NO_WARN].value)
421          feWarn = FALSE;
422        else
423          feWarn = TRUE;
[c06a32]424        return NULL;
[3b295e]425
[c06a32]426      case FE_OPT_NO_OUT:
[b6f537]427        if (feOptSpec[FE_OPT_NO_OUT].value)
428          feOut = FALSE;
429        else
430          feOut = TRUE;
[c06a32]431        return NULL;
432
433      case FE_OPT_MIN_TIME:
434      {
435        double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
436        if (mintime <= 0) return "invalid float argument";
437        SetMinDisplayTime(mintime);
438        return NULL;
439      }
440
441      case FE_OPT_BROWSER:
[6123fa2]442        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
[c06a32]443
444      case FE_OPT_TICKS_PER_SEC:
445      {
[7447d8]446        int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
[c06a32]447        if (ticks <= 0)
448          return "integer argument must be larger than 0";
449        SetTimerResolution(ticks);
450        return NULL;
451      }
[b6f537]452
[c06a32]453      default:
454        return NULL;
455  }
456}
457
458// Prints usage message
459void fePrintOptValues()
460{
461  int i = 0;
462
463  while (feOptSpec[i].name != 0)
464  {
[b6f537]465    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
[c06a32]466#ifndef NDEBUG
467        && *(feOptSpec[i].help) != '/'
468#endif
469        )
470    {
471      if (feOptSpec[i].type == feOptString)
472      {
473        if (feOptSpec[i].value == NULL)
474        {
475          Print("// --%-15s\n", feOptSpec[i].name);
476        }
477        else
478        {
479          Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
480        }
481      }
482      else
483      {
[d97aa5]484        Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
[c06a32]485      }
486    }
487    i++;
488  }
489}
490
491#endif // ! ESingular
492
493// Prints help message
494static void feOptHelp(const char* name)
495{
496  int i = 0;
497  char tmp[20];
498#ifdef ESINGULAR
499  printf("ESingular: A Program that starts-up Singular within emacs, for\n");
[3b295e]500#endif
[c06a32]501  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
502  printf("   %s [options] [file1 [file2 ...]]\n", name);
503  printf("Options:\n");
504
505  while (feOptSpec[i].name != 0)
506  {
[3b295e]507    if (feOptSpec[i].help != NULL
[c06a32]508#ifdef NDEBUG
509        && *(feOptSpec[i].help) != '/'
510#endif
511        )
512    {
513      if (feOptSpec[i].has_arg > 0)
514      {
515        if  (feOptSpec[i].has_arg > 1)
516          sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
517        else
518          sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
519
520        printf(" %c%c --%-19s %s\n",
521               (feOptSpec[i].val != 0 ? '-' : ' '),
522               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
523               tmp,
524               feOptSpec[i].help);
525      }
526      else
527      {
528        printf(" %c%c --%-19s %s\n",
529               (feOptSpec[i].val != 0 ? '-' : ' '),
530               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
531               feOptSpec[i].name,
532               feOptSpec[i].help);
533      }
534    }
535    i++;
536  }
537
538  printf("\nFor more information, type `help;' from within Singular or visit\n");
[f2b960]539  printf("http://www.singular.uni-kl.de or consult the\n");
540  printf("Singular manual (available as on-line info or html manual).\n");
[c06a32]541}
542
543
544
545#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.