source: git/Singular/feOpt.cc @ 7fe9e13

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