source: git/Singular/feOpt.cc @ 829648

fieker-DuValspielwiese
Last change on this file since 829648 was 16f511, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Fixed the usage of "config.h" (if defined HAVE_CONFIG_H)
  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[c06a32]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: Implementation of option buisness
6*/
7
[16f511]8#ifdef HAVE_CONFIG_H
[bd795d]9#include "config.h"
[16f511]10#endif /* HAVE_CONFIG_H */
[bd795d]11#include <kernel/mod2.h>
12
[50cbdc]13#include <string.h>
[f3a470]14#include <stdlib.h>
[e8a9f3]15
[5ddb8d]16#ifdef HAVE_FACTORY
17#define SI_DONT_HAVE_GLOBAL_VARS
18#include <factory/factory.h>
19#endif
[e8a9f3]20
[e28fc4]21#define FE_OPT_STRUCTURE
[9680805]22#include "feOpt.h"
[e8a9f3]23
[487f79]24#if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
[0fb34ba]25#include <misc/options.h>
[487f79]26#endif
[c06a32]27
[71fcb3f]28#include "fehelp.h"
29
[c06a32]30
[adf4ba]31const char SHORT_OPTS_STRING[] = "bdhqstvxec:r:u:";
[c06a32]32
33//////////////////////////////////////////////////////////////
34//
35// Generation of feOptIndex
36//
37#ifdef GENERATE_OPTION_INDEX
38
39#include <stdio.h>
[07c5ee]40#include <unistd.h>
[5a39d12]41#include <stdlib.h>
[83c63c]42int main()
[c06a32]43{
44  FILE* fd;
45#ifdef ESINGULAR
[4df19c]46  fd = fopen("feOptES.xx", "w");
[ef0124]47#elif defined(TSINGULAR)
[4df19c]48  fd = fopen("feOptTS.xx", "w");
[a3bc95e]49#else
[e287fa]50  fd = fopen("feOpt.xx", "w");
[c06a32]51#endif
52
53  if (fd == NULL) exit(1);
54
55  int i = 0;
[3b295e]56
57  fputs("typedef enum\n{\n", fd);
58
[c06a32]59  while (feOptSpec[i].name != NULL)
60  {
61    const char* name = feOptSpec[i].name;
62    fputs("FE_OPT_", fd);
63    while (*name != 0)
64    {
[3b295e]65      if (*name == '-')
[c06a32]66      {
67        putc('_', fd);
68      }
69      else if (*name >= 97 && *name <= 122)
70      {
71        putc(*name - 32, fd);
72      }
73      else
74      {
75        putc(*name, fd);
76      }
77      name++;
78    }
79    if (i == 0)
80    {
81      fputs("=0", fd);
82    }
83    i++;
84    fputs(",\n  ", fd);
85  }
[3b295e]86
[c06a32]87  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
88  fclose(fd);
[4df19c]89#ifdef ESINGULAR
90  rename("feOptES.xx", "feOptES.inc");
91#elif defined(TSINGULAR)
92  rename("feOptTS.xx", "feOptTS.inc");
93#else
94  rename("feOpt.xx", "feOpt.inc");
95#endif
[83c63c]96  return(0);
[c06a32]97}
98
99#else // ! GENERATE_OPTION_INDEX
100
101///////////////////////////////////////////////////////////////
102//
103// Getting Values
104//
105
106feOptIndex feGetOptIndex(const char* name)
107{
108  int opt = 0;
[3b295e]109
[c06a32]110  while (opt != (int) FE_OPT_UNDEF)
111  {
[3b295e]112    if (strcmp(feOptSpec[opt].name, name) == 0)
[c06a32]113      return (feOptIndex) opt;
114    opt = opt + 1;
115  }
116  return FE_OPT_UNDEF;
117}
118
119feOptIndex feGetOptIndex(int optc)
120{
121  int opt = 0;
[3b295e]122
[c06a32]123  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
[3b295e]124
[c06a32]125  while (opt != (int) FE_OPT_UNDEF)
126  {
127    if (feOptSpec[opt].val == optc)
128      return (feOptIndex) opt;
129    opt = opt + 1;
130  }
131  return FE_OPT_UNDEF;
132}
133
[2272c33]134void* feGetOptValue(feOptIndex opt)
135{
136  return feOptSpec[(int)opt].value;
137}
138
[c06a32]139///////////////////////////////////////////////////////////////
140//
141// Setting Values
142//
143//
144// Return: NULL -- everything ok
145//         "error-string" on error
[ef0124]146#if !defined(ESINGULAR) && !defined(TSINGULAR)
[b1dfaf]147#include <omalloc/omalloc.h>
[599326]148#include <kernel/febase.h>
149#include <kernel/timer.h>
[c06a32]150
[e8a9f3]151#include "ipshell.h"
152#include "tok.h"
153#include "sdb.h"
154#include "cntrlc.h"
155
[c06a32]156#include <errno.h>
157
[0ec43a]158static const char* feOptAction(feOptIndex opt);
159const char* feSetOptValue(feOptIndex opt, char* optarg)
[c06a32]160{
161  if (opt == FE_OPT_UNDEF) return "option undefined";
[3b295e]162
[b6f537]163  if (feOptSpec[opt].type != feOptUntyped)
[c06a32]164  {
[b6f537]165    if (feOptSpec[opt].type != feOptString)
[c06a32]166    {
[b6f537]167      if (optarg != NULL)
168      {
169        errno = 0;
170        feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
171        if (errno) return "invalid integer argument";
172      }
173      else
174      {
175        feOptSpec[opt].value = (void*) 0;
176      }
[c06a32]177    }
178    else
179    {
[b6f537]180      assume(feOptSpec[opt].type == feOptString);
181      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
[c232af]182        omFree(feOptSpec[opt].value);
[b6f537]183      if (optarg != NULL)
[c232af]184        feOptSpec[opt].value = omStrDup(optarg);
[b6f537]185      else
186        feOptSpec[opt].value = NULL;
187      feOptSpec[opt].set = 1;
[c06a32]188    }
189  }
190  return feOptAction(opt);
191}
192
[0ec43a]193const char* feSetOptValue(feOptIndex opt, int optarg)
[c06a32]194{
195  if (opt == FE_OPT_UNDEF) return "option undefined";
[3b295e]196
[b6f537]197  if (feOptSpec[opt].type != feOptUntyped)
198  {
199    if (feOptSpec[opt].type == feOptString)
200      return "option value needs to be an integer";
[3b295e]201
[b6f537]202    feOptSpec[opt].value = (void*) optarg;
203  }
[c06a32]204  return feOptAction(opt);
205}
206
[0ec43a]207static const char* feOptAction(feOptIndex opt)
[c06a32]208{
209  // do some special actions
210  switch(opt)
211  {
212      case FE_OPT_BATCH:
[b6f537]213        if (feOptSpec[FE_OPT_BATCH].value)
214          fe_fgets_stdin=fe_fgets_dummy;
[c06a32]215        return NULL;
216
217      case FE_OPT_HELP:
218        feOptHelp(feArgv0);
219        return NULL;
220
221      case FE_OPT_QUIET:
[b6f537]222        if (feOptSpec[FE_OPT_QUIET].value)
[d30a399]223          si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
[b6f537]224        else
[d30a399]225          si_opt_2 |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
[c06a32]226        return NULL;
227
228      case FE_OPT_NO_TTY:
229#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
[b6f537]230        if (feOptSpec[FE_OPT_NO_TTY].value)
231          fe_fgets_stdin=fe_fgets;
[c06a32]232#endif
233        return NULL;
234
235      case FE_OPT_SDB:
[50cbdc]236      #ifdef HAVE_SDB
[b6f537]237        if (feOptSpec[FE_OPT_SDB].value)
238          sdb_flags = 1;
239        else
240          sdb_flags = 0;
[f3a470]241      #endif
[c06a32]242        return NULL;
243
244      case FE_OPT_VERSION:
[538512]245        {
246        char *s=versionString();
247        printf("%s",s);
248        omFree(s);
[c06a32]249        return NULL;
[538512]250        }
[c06a32]251
252      case FE_OPT_ECHO:
[7447d8]253        si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
[c06a32]254        if (si_echo < 0 || si_echo > 9)
255          return "argument of option is not in valid range 0..9";
256        return NULL;
[3b295e]257
[b6f537]258      case FE_OPT_RANDOM:
[7447d8]259        siRandomStart = (unsigned int) ((unsigned long)
260                                          (feOptSpec[FE_OPT_RANDOM].value));
[c06a32]261        siSeed=siRandomStart;
262#ifdef HAVE_FACTORY
263        factoryseed(siRandomStart);
264#endif
265        return NULL;
266
267      case FE_OPT_EMACS:
[b6f537]268        if (feOptSpec[FE_OPT_EMACS].value)
269        {
270          // print EmacsDir and InfoFile so that Emacs
271          // mode can pcik it up
272          Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
273                                feResource('e' /*"EmacsDir"*/) : ""));
274          Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
275                                feResource('i' /*"InfoFile"*/) : ""));
276        }
[c06a32]277        return NULL;
278
279      case FE_OPT_NO_WARN:
[b6f537]280        if (feOptSpec[FE_OPT_NO_WARN].value)
281          feWarn = FALSE;
282        else
283          feWarn = TRUE;
[c06a32]284        return NULL;
[3b295e]285
[c06a32]286      case FE_OPT_NO_OUT:
[b6f537]287        if (feOptSpec[FE_OPT_NO_OUT].value)
288          feOut = FALSE;
289        else
290          feOut = TRUE;
[c06a32]291        return NULL;
292
293      case FE_OPT_MIN_TIME:
294      {
295        double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
296        if (mintime <= 0) return "invalid float argument";
297        SetMinDisplayTime(mintime);
298        return NULL;
299      }
300
301      case FE_OPT_BROWSER:
[6123fa2]302        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
[c06a32]303
304      case FE_OPT_TICKS_PER_SEC:
305      {
[7447d8]306        int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
[c06a32]307        if (ticks <= 0)
308          return "integer argument must be larger than 0";
309        SetTimerResolution(ticks);
310        return NULL;
311      }
[b6f537]312
[c06a32]313      default:
314        return NULL;
315  }
316}
317
318// Prints usage message
319void fePrintOptValues()
320{
321  int i = 0;
322
323  while (feOptSpec[i].name != 0)
324  {
[b6f537]325    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
[c06a32]326#ifndef NDEBUG
327        && *(feOptSpec[i].help) != '/'
328#endif
329        )
330    {
331      if (feOptSpec[i].type == feOptString)
332      {
333        if (feOptSpec[i].value == NULL)
334        {
335          Print("// --%-15s\n", feOptSpec[i].name);
336        }
337        else
338        {
339          Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
340        }
341      }
342      else
343      {
[d97aa5]344        Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
[c06a32]345      }
346    }
347    i++;
348  }
349}
350
351#endif // ! ESingular
352
353// Prints help message
[bd795d]354void feOptHelp(const char* name)
[c06a32]355{
356  int i = 0;
357  char tmp[20];
358#ifdef ESINGULAR
359  printf("ESingular: A Program that starts-up Singular within emacs, for\n");
[3b295e]360#endif
[c06a32]361  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
362  printf("   %s [options] [file1 [file2 ...]]\n", name);
363  printf("Options:\n");
364
365  while (feOptSpec[i].name != 0)
366  {
[3b295e]367    if (feOptSpec[i].help != NULL
[c06a32]368#ifdef NDEBUG
369        && *(feOptSpec[i].help) != '/'
370#endif
371        )
372    {
373      if (feOptSpec[i].has_arg > 0)
374      {
375        if  (feOptSpec[i].has_arg > 1)
376          sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
377        else
378          sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
379
380        printf(" %c%c --%-19s %s\n",
381               (feOptSpec[i].val != 0 ? '-' : ' '),
382               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
383               tmp,
384               feOptSpec[i].help);
385      }
386      else
387      {
388        printf(" %c%c --%-19s %s\n",
389               (feOptSpec[i].val != 0 ? '-' : ' '),
390               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
391               feOptSpec[i].name,
392               feOptSpec[i].help);
393      }
394    }
395    i++;
396  }
397
398  printf("\nFor more information, type `help;' from within Singular or visit\n");
[f2b960]399  printf("http://www.singular.uni-kl.de or consult the\n");
400  printf("Singular manual (available as on-line info or html manual).\n");
[c06a32]401}
402
403
404
405#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.