source: git/Singular/feOpt.cc @ a70441f

spielwiese
Last change on this file since a70441f was a70441f, checked in by Olaf Bachmann <obachman@…>, 24 years ago
Windows and gcc 2.95 porting git-svn-id: file:///usr/local/Singular/svn/trunk@4273 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.10 2000-04-27 10:07:06 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, (void*) "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
251void* feGetOptValue(feOptIndex opt)
252{
253  return feOptSpec[(int)opt].value;
254}
255
256///////////////////////////////////////////////////////////////
257//
258// Setting Values
259//
260static void feOptHelp(const char* name);
261//
262// Return: NULL -- everything ok
263//         "error-string" on error
264#ifndef ESINGULAR
265#include "mmemory.h"
266#include "febase.h"
267#include "ipshell.h"
268#include "tok.h"
269#include "sdb.h"
270#include "cntrlc.h"
271#include "timer.h"
272
273#ifdef HAVE_FACTORY
274#define SI_DONT_HAVE_GLOBAL_VARS
275#include <factory.h>
276#endif
277#include <errno.h>
278
279static char* feOptAction(feOptIndex opt);
280char* feSetOptValue(feOptIndex opt, char* optarg)
281{
282  if (opt == FE_OPT_UNDEF) return "option undefined";
283
284  if (feOptSpec[opt].type != feOptUntyped)
285  {
286    if (feOptSpec[opt].type != feOptString)
287    {
288      if (optarg != NULL)
289      {
290        errno = 0;
291        feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
292        if (errno) return "invalid integer argument";
293      }
294      else
295      {
296        feOptSpec[opt].value = (void*) 0;
297      }
298    }
299    else
300    {
301      assume(feOptSpec[opt].type == feOptString);
302      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
303        FreeL(feOptSpec[opt].value);
304      if (optarg != NULL)
305        feOptSpec[opt].value = mstrdup(optarg);
306      else
307        feOptSpec[opt].value = NULL;
308      feOptSpec[opt].set = 1;
309    }
310  }
311  return feOptAction(opt);
312}
313
314char* feSetOptValue(feOptIndex opt, int optarg)
315{
316  if (opt == FE_OPT_UNDEF) return "option undefined";
317
318  if (feOptSpec[opt].type != feOptUntyped)
319  {
320    if (feOptSpec[opt].type == feOptString)
321      return "option value needs to be an integer";
322
323    feOptSpec[opt].value = (void*) optarg;
324  }
325  return feOptAction(opt);
326}
327
328static char* feOptAction(feOptIndex opt)
329{
330  // do some special actions
331  switch(opt)
332  {
333#ifdef HAVE_MPSR
334      case FE_OPT_BATCH:
335        if (feOptSpec[FE_OPT_BATCH].value)
336          fe_fgets_stdin=fe_fgets_dummy;
337        return NULL;
338#endif
339
340      case FE_OPT_HELP:
341        feOptHelp(feArgv0);
342        return NULL;
343
344      case FE_OPT_QUIET:
345        if (feOptSpec[FE_OPT_QUIET].value)
346          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
347        else
348          verbose |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
349        return NULL;
350
351      case FE_OPT_NO_TTY:
352#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
353        if (feOptSpec[FE_OPT_NO_TTY].value)
354          fe_fgets_stdin=fe_fgets;
355#endif
356        return NULL;
357
358      case FE_OPT_SDB:
359        if (feOptSpec[FE_OPT_SDB].value)
360          sdb_flags = 1;
361        else
362          sdb_flags = 0;
363        return NULL;
364
365      case FE_OPT_VERSION:
366        printf(versionString());
367        return NULL;
368
369#ifdef HAVE_TCL
370      case FE_OPT_TCLMODE:
371        if (feOptSpec[FE_OPT_TCLMODE].value)
372        {
373          tclmode = TRUE;
374          fe_fgets_stdin=fe_fgets_tcl;
375          verbose|=Sy_bit(V_SHOW_MEM);
376        }
377        return NULL;
378#endif
379
380      case FE_OPT_ECHO:
381        si_echo = (int) feOptSpec[FE_OPT_ECHO].value;
382        if (si_echo < 0 || si_echo > 9)
383          return "argument of option is not in valid range 0..9";
384        return NULL;
385
386      case FE_OPT_RANDOM:
387        siRandomStart = (unsigned int) feOptSpec[FE_OPT_RANDOM].value;
388#ifdef buildin_rand
389        siSeed=siRandomStart;
390#else
391        srand((unsigned int)siRandomStart);
392#endif
393#ifdef HAVE_FACTORY
394        factoryseed(siRandomStart);
395#endif
396        return NULL;
397
398      case FE_OPT_EMACS:
399        if (feOptSpec[FE_OPT_EMACS].value)
400        {
401          // print EmacsDir and InfoFile so that Emacs
402          // mode can pcik it up
403          Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
404                                feResource('e' /*"EmacsDir"*/) : ""));
405          Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
406                                feResource('i' /*"InfoFile"*/) : ""));
407        }
408        return NULL;
409
410      case FE_OPT_NO_WARN:
411        if (feOptSpec[FE_OPT_NO_WARN].value)
412          feWarn = FALSE;
413        else
414          feWarn = TRUE;
415        return NULL;
416
417      case FE_OPT_NO_OUT:
418        if (feOptSpec[FE_OPT_NO_OUT].value)
419          feOut = FALSE;
420        else
421          feOut = TRUE;
422        return NULL;
423
424      case FE_OPT_MIN_TIME:
425      {
426        double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
427        if (mintime <= 0) return "invalid float argument";
428        SetMinDisplayTime(mintime);
429        return NULL;
430      }
431
432      case FE_OPT_BROWSER:
433        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
434
435      case FE_OPT_TICKS_PER_SEC:
436      {
437        int ticks = (int) feOptSpec[FE_OPT_TICKS_PER_SEC].value;
438        if (ticks <= 0)
439          return "integer argument must be larger than 0";
440        SetTimerResolution(ticks);
441        return NULL;
442      }
443
444      default:
445        return NULL;
446  }
447}
448
449// Prints usage message
450void fePrintOptValues()
451{
452  int i = 0;
453
454  while (feOptSpec[i].name != 0)
455  {
456    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
457#ifndef NDEBUG
458        && *(feOptSpec[i].help) != '/'
459#endif
460        )
461    {
462      if (feOptSpec[i].type == feOptString)
463      {
464        if (feOptSpec[i].value == NULL)
465        {
466          Print("// --%-15s\n", feOptSpec[i].name);
467        }
468        else
469        {
470          Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
471        }
472      }
473      else
474      {
475        Print("// --%-15s %d\n", feOptSpec[i].name, feOptSpec[i].value);
476      }
477    }
478    i++;
479  }
480}
481
482#endif // ! ESingular
483
484// Prints help message
485static void feOptHelp(const char* name)
486{
487  int i = 0;
488  char tmp[20];
489#ifdef ESINGULAR
490  printf("ESingular: A Program that starts-up Singular within emacs, for\n");
491#endif
492  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
493  printf("   %s [options] [file1 [file2 ...]]\n", name);
494  printf("Options:\n");
495
496  while (feOptSpec[i].name != 0)
497  {
498    if (feOptSpec[i].help != NULL
499#ifdef NDEBUG
500        && *(feOptSpec[i].help) != '/'
501#endif
502        )
503    {
504      if (feOptSpec[i].has_arg > 0)
505      {
506        if  (feOptSpec[i].has_arg > 1)
507          sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
508        else
509          sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
510
511        printf(" %c%c --%-19s %s\n",
512               (feOptSpec[i].val != 0 ? '-' : ' '),
513               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
514               tmp,
515               feOptSpec[i].help);
516      }
517      else
518      {
519        printf(" %c%c --%-19s %s\n",
520               (feOptSpec[i].val != 0 ? '-' : ' '),
521               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
522               feOptSpec[i].name,
523               feOptSpec[i].help);
524      }
525    }
526    i++;
527  }
528
529  printf("\nFor more information, type `help;' from within Singular or visit\n");
530  printf("http://www.singular.uni-kl.de or consult the\n");
531  printf("Singular manual (available as on-line info or html manual).\n");
532}
533
534
535
536#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.