source: git/Singular/feOpt.cc @ 8baa37

spielwiese
Last change on this file since 8baa37 was fc2f76, checked in by Hans Schoenemann <hannes@…>, 10 years ago
fix: removed feGetOptValue (->feOptValue)
  • Property mode set to 100644
File size: 8.8 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
132///////////////////////////////////////////////////////////////
133//
134// Setting Values
135//
136//
137// Return: NULL -- everything ok
138//         "error-string" on error
139#if !defined(ESINGULAR) && !defined(TSINGULAR)
140#include <omalloc/omalloc.h>
141#include <kernel/febase.h>
142#include <kernel/timer.h>
143
144#include "ipshell.h"
145#include "tok.h"
146#include "sdb.h"
147#include "cntrlc.h"
148
149#include <errno.h>
150
151static const char* feOptAction(feOptIndex opt);
152const char* feSetOptValue(feOptIndex opt, char* optarg)
153{
154  if (opt == FE_OPT_UNDEF) return "option undefined";
155
156  if (feOptSpec[opt].type != feOptUntyped)
157  {
158    if (feOptSpec[opt].type != feOptString)
159    {
160      if (optarg != NULL)
161      {
162        errno = 0;
163        feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
164        if (errno) return "invalid integer argument";
165      }
166      else
167      {
168        feOptSpec[opt].value = (void*) 0;
169      }
170    }
171    else
172    {
173      assume(feOptSpec[opt].type == feOptString);
174      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
175        omFree(feOptSpec[opt].value);
176      if (optarg != NULL)
177        feOptSpec[opt].value = omStrDup(optarg);
178      else
179        feOptSpec[opt].value = NULL;
180      feOptSpec[opt].set = 1;
181    }
182  }
183  return feOptAction(opt);
184}
185
186const char* feSetOptValue(feOptIndex opt, int optarg)
187{
188  if (opt == FE_OPT_UNDEF) return "option undefined";
189
190  if (feOptSpec[opt].type != feOptUntyped)
191  {
192    if (feOptSpec[opt].type == feOptString)
193      return "option value needs to be an integer";
194
195    feOptSpec[opt].value = (void*)(long) optarg;
196  }
197  return feOptAction(opt);
198}
199
200static const char* feOptAction(feOptIndex opt)
201{
202  // do some special actions
203  switch(opt)
204  {
205      case FE_OPT_BATCH:
206        if (feOptSpec[FE_OPT_BATCH].value)
207          fe_fgets_stdin=fe_fgets_dummy;
208        return NULL;
209
210      case FE_OPT_HELP:
211        feOptHelp(feArgv0);
212        return NULL;
213
214      case FE_OPT_QUIET:
215        if (feOptSpec[FE_OPT_QUIET].value)
216          si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
217        else
218          si_opt_2 |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
219        return NULL;
220
221      case FE_OPT_NO_TTY:
222#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
223        if (feOptSpec[FE_OPT_NO_TTY].value)
224          fe_fgets_stdin=fe_fgets;
225#endif
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      default:
305        return NULL;
306  }
307}
308
309// Prints usage message
310void fePrintOptValues()
311{
312  int i = 0;
313
314  while (feOptSpec[i].name != 0)
315  {
316    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
317#ifndef SING_NDEBUG
318        && *(feOptSpec[i].help) != '/'
319#endif
320        )
321    {
322      if (feOptSpec[i].type == feOptString)
323      {
324        if (feOptSpec[i].value == NULL)
325        {
326          Print("// --%-15s\n", feOptSpec[i].name);
327        }
328        else
329        {
330          Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
331        }
332      }
333      else
334      {
335        Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
336      }
337    }
338    i++;
339  }
340}
341
342#endif // ! ESingular
343
344// Prints help message
345void feOptHelp(const char* name)
346{
347  int i = 0;
348  char tmp[20];
349#ifdef ESINGULAR
350  printf("ESingular: A Program that starts-up Singular within emacs, for\n");
351#endif
352  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", VERSION);
353  printf("   %s [options] [file1 [file2 ...]]\n", name);
354  printf("Options:\n");
355
356  while (feOptSpec[i].name != 0)
357  {
358    if (feOptSpec[i].help != NULL
359#ifdef SING_NDEBUG
360        && *(feOptSpec[i].help) != '/'
361#endif
362        )
363    {
364      if (feOptSpec[i].has_arg > 0)
365      {
366        if  (feOptSpec[i].has_arg > 1)
367          sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
368        else
369          sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
370
371        printf(" %c%c --%-19s %s\n",
372               (feOptSpec[i].val != 0 ? '-' : ' '),
373               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
374               tmp,
375               feOptSpec[i].help);
376      }
377      else
378      {
379        printf(" %c%c --%-19s %s\n",
380               (feOptSpec[i].val != 0 ? '-' : ' '),
381               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
382               feOptSpec[i].name,
383               feOptSpec[i].help);
384      }
385    }
386    i++;
387  }
388
389  printf("\nFor more information, type `help;' from within Singular or visit\n");
390  printf("http://www.singular.uni-kl.de or consult the\n");
391  printf("Singular manual (available as on-line info or html manual).\n");
392}
393
394
395
396#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.