source: git/Singular/feOpt.cc @ 50cbdc

fieker-DuValspielwiese
Last change on this file since 50cbdc was 50cbdc, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: merge-2-0-2 git-svn-id: file:///usr/local/Singular/svn/trunk@5619 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: feOpt.cc,v 1.17 2001-08-27 14:46:57 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#endif
148
149  {"ticks-per-sec",     required_argument,  LONG_OPTION_RETURN,
150   "TICKS",     "Sets unit of timer to TICKS per second",               feOptInt,    (void*)1,      0},
151
152// undocumented options
153#ifdef HAVE_MPSR
154  {"MPtransp",         required_argument,   LONG_OPTION_RETURN,
155   "TRANSP",    "// Use TRANSP for MP connections",                     feOptString,    0,   0},
156
157  {"MPmode",           required_argument,   LONG_OPTION_RETURN,
158   "MODE",      "// Use MODE for MP connections",                       feOptString,    0,   0},
159#endif
160
161// terminator -- do NOT remove
162  { 0, 0, 0, 0, 0, feOptInt, 0, 0}
163};
164
165const char SHORT_OPTS_STRING[] = "bdhqtvxec:r:u:";
166
167//////////////////////////////////////////////////////////////
168//
169// Generation of feOptIndex
170//
171#ifdef GENERATE_OPTION_INDEX
172
173#include <stdio.h>
174#include <unistd.h>
175#include <stdlib.h>
176main()
177{
178  FILE* fd;
179#ifdef ESINGULAR
180  fd = fopen("feOptES.inc", "w");
181#elif defined(TSINGULAR)
182  fd = fopen("feOptTS.inc", "w");
183#else 
184  fd = fopen("feOpt.inc", "w");
185#endif
186
187  if (fd == NULL) exit(1);
188
189  int i = 0;
190
191  fputs("typedef enum\n{\n", fd);
192
193  while (feOptSpec[i].name != NULL)
194  {
195    const char* name = feOptSpec[i].name;
196    fputs("FE_OPT_", fd);
197    while (*name != 0)
198    {
199      if (*name == '-')
200      {
201        putc('_', fd);
202      }
203      else if (*name >= 97 && *name <= 122)
204      {
205        putc(*name - 32, fd);
206      }
207      else
208      {
209        putc(*name, fd);
210      }
211      name++;
212    }
213    if (i == 0)
214    {
215      fputs("=0", fd);
216    }
217    i++;
218    fputs(",\n  ", fd);
219  }
220
221  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
222  fclose(fd);
223  exit(0);
224}
225
226#else // ! GENERATE_OPTION_INDEX
227
228///////////////////////////////////////////////////////////////
229//
230// Getting Values
231//
232
233feOptIndex feGetOptIndex(const char* name)
234{
235  int opt = 0;
236
237  while (opt != (int) FE_OPT_UNDEF)
238  {
239    if (strcmp(feOptSpec[opt].name, name) == 0)
240      return (feOptIndex) opt;
241    opt = opt + 1;
242  }
243  return FE_OPT_UNDEF;
244}
245
246feOptIndex feGetOptIndex(int optc)
247{
248  int opt = 0;
249
250  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
251
252  while (opt != (int) FE_OPT_UNDEF)
253  {
254    if (feOptSpec[opt].val == optc)
255      return (feOptIndex) opt;
256    opt = opt + 1;
257  }
258  return FE_OPT_UNDEF;
259}
260
261void* feGetOptValue(feOptIndex opt)
262{
263  return feOptSpec[(int)opt].value;
264}
265
266///////////////////////////////////////////////////////////////
267//
268// Setting Values
269//
270static void feOptHelp(const char* name);
271//
272// Return: NULL -- everything ok
273//         "error-string" on error
274#if !defined(ESINGULAR) && !defined(TSINGULAR)
275#include "omalloc.h"
276#include "febase.h"
277#include "ipshell.h"
278#include "tok.h"
279#include "sdb.h"
280#include "cntrlc.h"
281#include "timer.h"
282
283#ifdef HAVE_FACTORY
284#define SI_DONT_HAVE_GLOBAL_VARS
285#include <factory.h>
286#endif
287#include <errno.h>
288
289static char* feOptAction(feOptIndex opt);
290char* feSetOptValue(feOptIndex opt, char* optarg)
291{
292  if (opt == FE_OPT_UNDEF) return "option undefined";
293
294  if (feOptSpec[opt].type != feOptUntyped)
295  {
296    if (feOptSpec[opt].type != feOptString)
297    {
298      if (optarg != NULL)
299      {
300        errno = 0;
301        feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
302        if (errno) return "invalid integer argument";
303      }
304      else
305      {
306        feOptSpec[opt].value = (void*) 0;
307      }
308    }
309    else
310    {
311      assume(feOptSpec[opt].type == feOptString);
312      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
313        omFree(feOptSpec[opt].value);
314      if (optarg != NULL)
315        feOptSpec[opt].value = omStrDup(optarg);
316      else
317        feOptSpec[opt].value = NULL;
318      feOptSpec[opt].set = 1;
319    }
320  }
321  return feOptAction(opt);
322}
323
324char* feSetOptValue(feOptIndex opt, int optarg)
325{
326  if (opt == FE_OPT_UNDEF) return "option undefined";
327
328  if (feOptSpec[opt].type != feOptUntyped)
329  {
330    if (feOptSpec[opt].type == feOptString)
331      return "option value needs to be an integer";
332
333    feOptSpec[opt].value = (void*) optarg;
334  }
335  return feOptAction(opt);
336}
337
338static char* feOptAction(feOptIndex opt)
339{
340  // do some special actions
341  switch(opt)
342  {
343#ifdef HAVE_MPSR
344      case FE_OPT_BATCH:
345        if (feOptSpec[FE_OPT_BATCH].value)
346          fe_fgets_stdin=fe_fgets_dummy;
347        return NULL;
348#endif
349
350      case FE_OPT_HELP:
351        feOptHelp(feArgv0);
352        return NULL;
353
354      case FE_OPT_QUIET:
355        if (feOptSpec[FE_OPT_QUIET].value)
356          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
357        else
358          verbose |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
359        return NULL;
360
361      case FE_OPT_NO_TTY:
362#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
363        if (feOptSpec[FE_OPT_NO_TTY].value)
364          fe_fgets_stdin=fe_fgets;
365#endif
366        return NULL;
367
368      case FE_OPT_SDB:
369      #ifdef HAVE_SDB
370        if (feOptSpec[FE_OPT_SDB].value)
371          sdb_flags = 1;
372        else
373          sdb_flags = 0;
374      #endif     
375        return NULL;
376
377      case FE_OPT_VERSION:
378        printf(versionString());
379        return NULL;
380
381#ifdef HAVE_TCL
382      case FE_OPT_TCLMODE:
383        if (feOptSpec[FE_OPT_TCLMODE].value)
384        {
385          tclmode = TRUE;
386          fe_fgets_stdin=fe_fgets_tcl;
387          verbose|=Sy_bit(V_SHOW_MEM);
388        }
389        return NULL;
390#endif
391
392      case FE_OPT_ECHO:
393        si_echo = (int) 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) feOptSpec[FE_OPT_RANDOM].value;
400#ifdef buildin_rand
401        siSeed=siRandomStart;
402#else
403        srand((unsigned int)siRandomStart);
404#endif
405#ifdef HAVE_FACTORY
406        factoryseed(siRandomStart);
407#endif
408        return NULL;
409
410      case FE_OPT_EMACS:
411        if (feOptSpec[FE_OPT_EMACS].value)
412        {
413          // print EmacsDir and InfoFile so that Emacs
414          // mode can pcik it up
415          Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
416                                feResource('e' /*"EmacsDir"*/) : ""));
417          Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
418                                feResource('i' /*"InfoFile"*/) : ""));
419        }
420        return NULL;
421
422      case FE_OPT_NO_WARN:
423        if (feOptSpec[FE_OPT_NO_WARN].value)
424          feWarn = FALSE;
425        else
426          feWarn = TRUE;
427        return NULL;
428
429      case FE_OPT_NO_OUT:
430        if (feOptSpec[FE_OPT_NO_OUT].value)
431          feOut = FALSE;
432        else
433          feOut = TRUE;
434        return NULL;
435
436      case FE_OPT_MIN_TIME:
437      {
438        double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
439        if (mintime <= 0) return "invalid float argument";
440        SetMinDisplayTime(mintime);
441        return NULL;
442      }
443
444      case FE_OPT_BROWSER:
445        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
446
447      case FE_OPT_TICKS_PER_SEC:
448      {
449        int ticks = (int) feOptSpec[FE_OPT_TICKS_PER_SEC].value;
450        if (ticks <= 0)
451          return "integer argument must be larger than 0";
452        SetTimerResolution(ticks);
453        return NULL;
454      }
455
456      default:
457        return NULL;
458  }
459}
460
461// Prints usage message
462void fePrintOptValues()
463{
464  int i = 0;
465
466  while (feOptSpec[i].name != 0)
467  {
468    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
469#ifndef NDEBUG
470        && *(feOptSpec[i].help) != '/'
471#endif
472        )
473    {
474      if (feOptSpec[i].type == feOptString)
475      {
476        if (feOptSpec[i].value == NULL)
477        {
478          Print("// --%-15s\n", feOptSpec[i].name);
479        }
480        else
481        {
482          Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
483        }
484      }
485      else
486      {
487        Print("// --%-15s %d\n", feOptSpec[i].name, feOptSpec[i].value);
488      }
489    }
490    i++;
491  }
492}
493
494#endif // ! ESingular
495
496// Prints help message
497static void feOptHelp(const char* name)
498{
499  int i = 0;
500  char tmp[20];
501#ifdef ESINGULAR
502  printf("ESingular: A Program that starts-up Singular within emacs, for\n");
503#endif
504  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
505  printf("   %s [options] [file1 [file2 ...]]\n", name);
506  printf("Options:\n");
507
508  while (feOptSpec[i].name != 0)
509  {
510    if (feOptSpec[i].help != NULL
511#ifdef NDEBUG
512        && *(feOptSpec[i].help) != '/'
513#endif
514        )
515    {
516      if (feOptSpec[i].has_arg > 0)
517      {
518        if  (feOptSpec[i].has_arg > 1)
519          sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
520        else
521          sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
522
523        printf(" %c%c --%-19s %s\n",
524               (feOptSpec[i].val != 0 ? '-' : ' '),
525               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
526               tmp,
527               feOptSpec[i].help);
528      }
529      else
530      {
531        printf(" %c%c --%-19s %s\n",
532               (feOptSpec[i].val != 0 ? '-' : ' '),
533               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
534               feOptSpec[i].name,
535               feOptSpec[i].help);
536      }
537    }
538    i++;
539  }
540
541  printf("\nFor more information, type `help;' from within Singular or visit\n");
542  printf("http://www.singular.uni-kl.de or consult the\n");
543  printf("Singular manual (available as on-line info or html manual).\n");
544}
545
546
547
548#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.