source: git/Singular/feOpt.cc @ d2f3863

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