source: git/Singular/feOpt.cc @ 425e33c

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