source: git/Singular/feOpt.cc @ 416465

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