source: git/Singular/feOpt.cc @ b6f537

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