source: git/Singular/feOpt.cc @ c232af

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