source: git/Singular/feOpt.cc @ 113ed4

fieker-DuValspielwiese
Last change on this file since 113ed4 was e28fc4, checked in by Hans Schoenemann <hannes@…>, 13 years ago
fix: multiple definitions of feOptStruct
  • Property mode set to 100644
File size: 8.9 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
12#include <kernel/mod2.h>
13
14#ifdef HAVE_FACTORY
15#define SI_DONT_HAVE_GLOBAL_VARS
16#include <factory/factory.h>
17#endif
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//
141static void feOptHelp(const char* name);
142//
143// Return: NULL -- everything ok
144//         "error-string" on error
145#if !defined(ESINGULAR) && !defined(TSINGULAR)
146#include <omalloc/omalloc.h>
147#include <kernel/febase.h>
148#include <kernel/timer.h>
149
150#include "ipshell.h"
151#include "tok.h"
152#include "sdb.h"
153#include "cntrlc.h"
154
155#include <errno.h>
156
157static const char* feOptAction(feOptIndex opt);
158const char* feSetOptValue(feOptIndex opt, char* optarg)
159{
160  if (opt == FE_OPT_UNDEF) return "option undefined";
161
162  if (feOptSpec[opt].type != feOptUntyped)
163  {
164    if (feOptSpec[opt].type != feOptString)
165    {
166      if (optarg != NULL)
167      {
168        errno = 0;
169        feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
170        if (errno) return "invalid integer argument";
171      }
172      else
173      {
174        feOptSpec[opt].value = (void*) 0;
175      }
176    }
177    else
178    {
179      assume(feOptSpec[opt].type == feOptString);
180      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
181        omFree(feOptSpec[opt].value);
182      if (optarg != NULL)
183        feOptSpec[opt].value = omStrDup(optarg);
184      else
185        feOptSpec[opt].value = NULL;
186      feOptSpec[opt].set = 1;
187    }
188  }
189  return feOptAction(opt);
190}
191
192const char* feSetOptValue(feOptIndex opt, int optarg)
193{
194  if (opt == FE_OPT_UNDEF) return "option undefined";
195
196  if (feOptSpec[opt].type != feOptUntyped)
197  {
198    if (feOptSpec[opt].type == feOptString)
199      return "option value needs to be an integer";
200
201    feOptSpec[opt].value = (void*) optarg;
202  }
203  return feOptAction(opt);
204}
205
206static const char* feOptAction(feOptIndex opt)
207{
208  // do some special actions
209  switch(opt)
210  {
211      case FE_OPT_BATCH:
212        if (feOptSpec[FE_OPT_BATCH].value)
213          fe_fgets_stdin=fe_fgets_dummy;
214        return NULL;
215
216      case FE_OPT_HELP:
217        feOptHelp(feArgv0);
218        return NULL;
219
220      case FE_OPT_QUIET:
221        if (feOptSpec[FE_OPT_QUIET].value)
222          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
223        else
224          verbose |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
225        return NULL;
226
227      case FE_OPT_NO_TTY:
228#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
229        if (feOptSpec[FE_OPT_NO_TTY].value)
230          fe_fgets_stdin=fe_fgets;
231#endif
232        return NULL;
233
234      case FE_OPT_SDB:
235      #ifdef HAVE_SDB
236        if (feOptSpec[FE_OPT_SDB].value)
237          sdb_flags = 1;
238        else
239          sdb_flags = 0;
240      #endif
241        return NULL;
242
243      case FE_OPT_VERSION:
244        printf("%s",versionString());
245        return NULL;
246
247      case FE_OPT_ECHO:
248        si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
249        if (si_echo < 0 || si_echo > 9)
250          return "argument of option is not in valid range 0..9";
251        return NULL;
252
253      case FE_OPT_RANDOM:
254        siRandomStart = (unsigned int) ((unsigned long)
255                                          (feOptSpec[FE_OPT_RANDOM].value));
256        siSeed=siRandomStart;
257#ifdef HAVE_FACTORY
258        factoryseed(siRandomStart);
259#endif
260        return NULL;
261
262      case FE_OPT_EMACS:
263        if (feOptSpec[FE_OPT_EMACS].value)
264        {
265          // print EmacsDir and InfoFile so that Emacs
266          // mode can pcik it up
267          Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
268                                feResource('e' /*"EmacsDir"*/) : ""));
269          Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
270                                feResource('i' /*"InfoFile"*/) : ""));
271        }
272        return NULL;
273
274      case FE_OPT_NO_WARN:
275        if (feOptSpec[FE_OPT_NO_WARN].value)
276          feWarn = FALSE;
277        else
278          feWarn = TRUE;
279        return NULL;
280
281      case FE_OPT_NO_OUT:
282        if (feOptSpec[FE_OPT_NO_OUT].value)
283          feOut = FALSE;
284        else
285          feOut = TRUE;
286        return NULL;
287
288      case FE_OPT_MIN_TIME:
289      {
290        double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
291        if (mintime <= 0) return "invalid float argument";
292        SetMinDisplayTime(mintime);
293        return NULL;
294      }
295
296      case FE_OPT_BROWSER:
297        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
298
299      case FE_OPT_TICKS_PER_SEC:
300      {
301        int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
302        if (ticks <= 0)
303          return "integer argument must be larger than 0";
304        SetTimerResolution(ticks);
305        return NULL;
306      }
307
308      default:
309        return NULL;
310  }
311}
312
313// Prints usage message
314void fePrintOptValues()
315{
316  int i = 0;
317
318  while (feOptSpec[i].name != 0)
319  {
320    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
321#ifndef NDEBUG
322        && *(feOptSpec[i].help) != '/'
323#endif
324        )
325    {
326      if (feOptSpec[i].type == feOptString)
327      {
328        if (feOptSpec[i].value == NULL)
329        {
330          Print("// --%-15s\n", feOptSpec[i].name);
331        }
332        else
333        {
334          Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
335        }
336      }
337      else
338      {
339        Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
340      }
341    }
342    i++;
343  }
344}
345
346#endif // ! ESingular
347
348// Prints help message
349static void feOptHelp(const char* name)
350{
351  int i = 0;
352  char tmp[20];
353#ifdef ESINGULAR
354  printf("ESingular: A Program that starts-up Singular within emacs, for\n");
355#endif
356  printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
357  printf("   %s [options] [file1 [file2 ...]]\n", name);
358  printf("Options:\n");
359
360  while (feOptSpec[i].name != 0)
361  {
362    if (feOptSpec[i].help != NULL
363#ifdef NDEBUG
364        && *(feOptSpec[i].help) != '/'
365#endif
366        )
367    {
368      if (feOptSpec[i].has_arg > 0)
369      {
370        if  (feOptSpec[i].has_arg > 1)
371          sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
372        else
373          sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
374
375        printf(" %c%c --%-19s %s\n",
376               (feOptSpec[i].val != 0 ? '-' : ' '),
377               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
378               tmp,
379               feOptSpec[i].help);
380      }
381      else
382      {
383        printf(" %c%c --%-19s %s\n",
384               (feOptSpec[i].val != 0 ? '-' : ' '),
385               (feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
386               feOptSpec[i].name,
387               feOptSpec[i].help);
388      }
389    }
390    i++;
391  }
392
393  printf("\nFor more information, type `help;' from within Singular or visit\n");
394  printf("http://www.singular.uni-kl.de or consult the\n");
395  printf("Singular manual (available as on-line info or html manual).\n");
396}
397
398
399
400#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.