source: git/Singular/feOpt.cc @ bee06d

spielwiese
Last change on this file since bee06d was d8bea6a, checked in by Hans Schoenemann <hannes@…>, 14 years ago
buildin_rand removed git-svn-id: file:///usr/local/Singular/svn/trunk@13481 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: Implementation of option buisness
7*/
8
9#include <string.h>
10#include <stdlib.h>
11#include <kernel/mod2.h>
12#include <Singular/feOpt.h>
13#if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
14#include <kernel/options.h>
15#endif
16
17// Define here which cmd-line options are recognized
18struct fe_option feOptSpec[] =
19{
20//
21//  Has to be of the form
22//    {name, has_arg, val,
23//     arg_name, help, type, value, set}
24//  where:
25//
26//        name   is the name of the long option.
27//
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.
33//
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//
39//        arg_name if set, uses this value as name for argument in
40//                display of help
41//
42//        help  one-line description of option
43//
44//        type  one of feOptUntyped (value is never set),
45//                     feOptBool, feOptInt, feOptString
46//
47//        value (default) value of option
48//
49//       set   only relevant for feOptString:
50//             1: if value different from default value
51//             0: otherwise
52//
53// The order in which options are specified is the order in which
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
58//
59#if defined(ESINGULAR) || defined(TSINGULAR)
60#ifdef ESINGULAR
61// options only relevant for ESINGULAR
62  {"emacs",         required_argument,      LONG_OPTION_RETURN,
63   "EMACS",     "Use EMACS as emacs program to run Singular",          feOptString, 0,   0},
64
65  {"emacs-dir",         required_argument,  LONG_OPTION_RETURN,
66   "DIR",       "Use DIR as directory to look for emacs lisp files",   feOptString, 0,   0},
67
68  {"emacs-load",        required_argument,  LONG_OPTION_RETURN,
69   "FILE",      "Load FILE on emacs start-up, instead of default",     feOptString, 0,   0},
70#else
71  {"xterm",         required_argument,      LONG_OPTION_RETURN,
72   "XTERM",     "Use XTERM as terminal program to run Singular",       feOptString, 0,   0},
73#endif
74
75  {"singular",          required_argument,  LONG_OPTION_RETURN,
76   "PROG",      "Start PROG as Singular program within emacs",         feOptString, 0,   0},
77
78  {"no-call",     no_argument,        LONG_OPTION_RETURN,
79   0,          "Do not start program. Print call to stdout",           feOptBool,   0,   0},
80#endif
81
82  {"batch",             no_argument,        'b',
83   0,          "Run in batch mode",                                    feOptBool,    0,     0},
84
85  {"execute",           required_argument,  'c',
86   "STRING",   "Execute STRING on start-up",                           feOptString, 0,   0},
87
88  {"sdb",               no_argument,        'd',
89   0,          "Enable source code debugger (experimental)",           feOptBool,    0,      0},
90
91  {"echo",              optional_argument,  'e',
92   "VAL",       "Set value of variable `echo' to (integer) VAL",        feOptInt,    0,      0},
93
94  {"help",              no_argument,        'h',
95   0,          "Print help message and exit",                          feOptUntyped,    0,      0},
96
97  {"quiet",             no_argument,        'q',
98   0,          "Do not print start-up banner and lib load messages",   feOptBool,    0,      0},
99  {"sort",             no_argument,        's',
100   0,          "// Sort NTL results",                                  feOptBool,    0,      0},
101
102  {"random",            required_argument,  'r',
103   "SEED",     "Seed random generator with integer (integer) SEED",    feOptInt,    0,      0},
104
105  {"no-tty",            no_argument,        't',
106   0,          "Do not redefine the terminal characteristics",         feOptBool,    0,      0},
107
108  {"user-option",       required_argument,  'u',
109   "STRING",   "Return STRING on `system(\"--user-option\")'",         feOptString, 0,   0},
110
111  {"version",           no_argument,        'v',
112   0,          "Print extended version and configuration info",        feOptUntyped,    0,      0},
113
114
115  {"allow-net",         no_argument,        LONG_OPTION_RETURN,
116   0,          "Allow to fetch (html) help pages from the net",                feOptBool,    0,      0},
117
118  {"browser",           required_argument,  LONG_OPTION_RETURN,
119   "BROWSER",  "Display help in BROWSER (see help.cnf)",       feOptString, 0,   0},
120
121#ifndef ESINGULAR
122  {"emacs",             no_argument,        LONG_OPTION_RETURN,
123   0,          "Set defaults for running within emacs",                feOptBool,    0,      0},
124#endif
125
126  {"no-stdlib",         no_argument,        LONG_OPTION_RETURN,
127   0,          "Do not load `standard.lib' on start-up",               feOptBool,    0,      0},
128
129  {"no-rc",             no_argument,        LONG_OPTION_RETURN,
130   0,          "Do not execute `.singularrc' file(s) on start-up",     feOptBool,    0,      0},
131
132  {"no-warn",           no_argument,        LONG_OPTION_RETURN,
133   0,          "Do not display warning messages",                      feOptBool,    0,      0},
134
135  {"no-out",            no_argument,        LONG_OPTION_RETURN,
136   0,          "Suppress all output",                                  feOptBool,    0,      0},
137
138  {"min-time",          required_argument,  LONG_OPTION_RETURN,
139  "SECS",     "Do not display times smaller than SECS (in seconds)",   feOptString, (void*) "0.5",  0},
140
141  {"MPport",           required_argument,   LONG_OPTION_RETURN,
142   "PORT",     "Use PORT number for conections",                       feOptString,    0,      0},
143
144  {"MPhost",           required_argument,   LONG_OPTION_RETURN,
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},
149
150#ifdef HAVE_MPSR
151  {"MPrsh",           required_argument,   LONG_OPTION_RETURN,
152   "RSH",     "Use RSH for MP connections",                            feOptString,    0,   0},
153#endif
154
155  {"ticks-per-sec",     required_argument,  LONG_OPTION_RETURN,
156   "TICKS",     "Sets unit of timer to TICKS per second",               feOptInt,    (void*)1,      0},
157
158// undocumented options
159#ifdef HAVE_MPSR
160  {"MPtransp",         required_argument,   LONG_OPTION_RETURN,
161   "TRANSP",    "// Use TRANSP for MP connections",                     feOptString,    0,   0},
162
163  {"MPmode",           required_argument,   LONG_OPTION_RETURN,
164   "MODE",      "// Use MODE for MP connections",                       feOptString,    0,   0},
165#endif
166
167// terminator -- do NOT remove
168  { 0, 0, 0, 0, 0, feOptInt, 0, 0}
169};
170
171const char SHORT_OPTS_STRING[] = "bdhqstvxec:r:u:";
172
173//////////////////////////////////////////////////////////////
174//
175// Generation of feOptIndex
176//
177#ifdef GENERATE_OPTION_INDEX
178
179#include <stdio.h>
180#include <unistd.h>
181#include <stdlib.h>
182int main()
183{
184  FILE* fd;
185#ifdef ESINGULAR
186  fd = fopen("feOptES.xx", "w");
187#elif defined(TSINGULAR)
188  fd = fopen("feOptTS.xx", "w");
189#else
190  fd = fopen("feOpt.xx", "w");
191#endif
192
193  if (fd == NULL) exit(1);
194
195  int i = 0;
196
197  fputs("typedef enum\n{\n", fd);
198
199  while (feOptSpec[i].name != NULL)
200  {
201    const char* name = feOptSpec[i].name;
202    fputs("FE_OPT_", fd);
203    while (*name != 0)
204    {
205      if (*name == '-')
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  }
226
227  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
228  fclose(fd);
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
236  return(0);
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;
249
250  while (opt != (int) FE_OPT_UNDEF)
251  {
252    if (strcmp(feOptSpec[opt].name, name) == 0)
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;
262
263  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
264
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
274void* feGetOptValue(feOptIndex opt)
275{
276  return feOptSpec[(int)opt].value;
277}
278
279///////////////////////////////////////////////////////////////
280//
281// Setting Values
282//
283static void feOptHelp(const char* name);
284//
285// Return: NULL -- everything ok
286//         "error-string" on error
287#if !defined(ESINGULAR) && !defined(TSINGULAR)
288#include <omalloc/omalloc.h>
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>
295
296#ifdef HAVE_FACTORY
297#define SI_DONT_HAVE_GLOBAL_VARS
298#include <factory/factory.h>
299#endif
300#include <errno.h>
301
302static const char* feOptAction(feOptIndex opt);
303const char* feSetOptValue(feOptIndex opt, char* optarg)
304{
305  if (opt == FE_OPT_UNDEF) return "option undefined";
306
307  if (feOptSpec[opt].type != feOptUntyped)
308  {
309    if (feOptSpec[opt].type != feOptString)
310    {
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      }
321    }
322    else
323    {
324      assume(feOptSpec[opt].type == feOptString);
325      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
326        omFree(feOptSpec[opt].value);
327      if (optarg != NULL)
328        feOptSpec[opt].value = omStrDup(optarg);
329      else
330        feOptSpec[opt].value = NULL;
331      feOptSpec[opt].set = 1;
332    }
333  }
334  return feOptAction(opt);
335}
336
337const char* feSetOptValue(feOptIndex opt, int optarg)
338{
339  if (opt == FE_OPT_UNDEF) return "option undefined";
340
341  if (feOptSpec[opt].type != feOptUntyped)
342  {
343    if (feOptSpec[opt].type == feOptString)
344      return "option value needs to be an integer";
345
346    feOptSpec[opt].value = (void*) optarg;
347  }
348  return feOptAction(opt);
349}
350
351static const char* feOptAction(feOptIndex opt)
352{
353  // do some special actions
354  switch(opt)
355  {
356      case FE_OPT_BATCH:
357        if (feOptSpec[FE_OPT_BATCH].value)
358          fe_fgets_stdin=fe_fgets_dummy;
359        return NULL;
360
361      case FE_OPT_HELP:
362        feOptHelp(feArgv0);
363        return NULL;
364
365      case FE_OPT_QUIET:
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);
370        return NULL;
371
372      case FE_OPT_NO_TTY:
373#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
374        if (feOptSpec[FE_OPT_NO_TTY].value)
375          fe_fgets_stdin=fe_fgets;
376#endif
377        return NULL;
378
379      case FE_OPT_SDB:
380      #ifdef HAVE_SDB
381        if (feOptSpec[FE_OPT_SDB].value)
382          sdb_flags = 1;
383        else
384          sdb_flags = 0;
385      #endif
386        return NULL;
387
388      case FE_OPT_VERSION:
389        printf("%s",versionString());
390        return NULL;
391
392      case FE_OPT_ECHO:
393        si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
394        if (si_echo < 0 || si_echo > 9)
395          return "argument of option is not in valid range 0..9";
396        return NULL;
397
398      case FE_OPT_RANDOM:
399        siRandomStart = (unsigned int) ((unsigned long)
400                                          (feOptSpec[FE_OPT_RANDOM].value));
401        siSeed=siRandomStart;
402#ifdef HAVE_FACTORY
403        factoryseed(siRandomStart);
404#endif
405        return NULL;
406
407      case FE_OPT_EMACS:
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        }
417        return NULL;
418
419      case FE_OPT_NO_WARN:
420        if (feOptSpec[FE_OPT_NO_WARN].value)
421          feWarn = FALSE;
422        else
423          feWarn = TRUE;
424        return NULL;
425
426      case FE_OPT_NO_OUT:
427        if (feOptSpec[FE_OPT_NO_OUT].value)
428          feOut = FALSE;
429        else
430          feOut = TRUE;
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:
442        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
443
444      case FE_OPT_TICKS_PER_SEC:
445      {
446        int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
447        if (ticks <= 0)
448          return "integer argument must be larger than 0";
449        SetTimerResolution(ticks);
450        return NULL;
451      }
452
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  {
465    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
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      {
484        Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
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");
500#endif
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  {
507    if (feOptSpec[i].help != NULL
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");
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");
541}
542
543
544
545#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.