source: git/Singular/feOpt.cc @ 5476e83

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