source: git/Singular/feOpt.cc @ 3bef0a

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