source: git/Singular/feOpt.cc @ 16f511

spielwiese
Last change on this file since 16f511 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
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: Implementation of option buisness
6*/
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif /* HAVE_CONFIG_H */
11#include <kernel/mod2.h>
12
13#include <string.h>
14#include <stdlib.h>
15
16#ifdef HAVE_FACTORY
17#define SI_DONT_HAVE_GLOBAL_VARS
18#include <factory/factory.h>
19#endif
20
21#define FE_OPT_STRUCTURE
22#include "feOpt.h"
23
24#if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
25#include <misc/options.h>
26#endif
27
28#include "fehelp.h"
29
30
31const char SHORT_OPTS_STRING[] = "bdhqstvxec:r:u:";
32
33//////////////////////////////////////////////////////////////
34//
35// Generation of feOptIndex
36//
37#ifdef GENERATE_OPTION_INDEX
38
39#include <stdio.h>
40#include <unistd.h>
41#include <stdlib.h>
42int main()
43{
44  FILE* fd;
45#ifdef ESINGULAR
46  fd = fopen("feOptES.xx", "w");
47#elif defined(TSINGULAR)
48  fd = fopen("feOptTS.xx", "w");
49#else
50  fd = fopen("feOpt.xx", "w");
51#endif
52
53  if (fd == NULL) exit(1);
54
55  int i = 0;
56
57  fputs("typedef enum\n{\n", fd);
58
59  while (feOptSpec[i].name != NULL)
60  {
61    const char* name = feOptSpec[i].name;
62    fputs("FE_OPT_", fd);
63    while (*name != 0)
64    {
65      if (*name == '-')
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  }
86
87  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
88  fclose(fd);
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
96  return(0);
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;
109
110  while (opt != (int) FE_OPT_UNDEF)
111  {
112    if (strcmp(feOptSpec[opt].name, name) == 0)
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;
122
123  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
124
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
134void* feGetOptValue(feOptIndex opt)
135{
136  return feOptSpec[(int)opt].value;
137}
138
139///////////////////////////////////////////////////////////////
140//
141// Setting Values
142//
143//
144// Return: NULL -- everything ok
145//         "error-string" on error
146#if !defined(ESINGULAR) && !defined(TSINGULAR)
147#include <omalloc/omalloc.h>
148#include <kernel/febase.h>
149#include <kernel/timer.h>
150
151#include "ipshell.h"
152#include "tok.h"
153#include "sdb.h"
154#include "cntrlc.h"
155
156#include <errno.h>
157
158static const char* feOptAction(feOptIndex opt);
159const char* feSetOptValue(feOptIndex opt, char* optarg)
160{
161  if (opt == FE_OPT_UNDEF) return "option undefined";
162
163  if (feOptSpec[opt].type != feOptUntyped)
164  {
165    if (feOptSpec[opt].type != feOptString)
166    {
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      }
177    }
178    else
179    {
180      assume(feOptSpec[opt].type == feOptString);
181      if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
182        omFree(feOptSpec[opt].value);
183      if (optarg != NULL)
184        feOptSpec[opt].value = omStrDup(optarg);
185      else
186        feOptSpec[opt].value = NULL;
187      feOptSpec[opt].set = 1;
188    }
189  }
190  return feOptAction(opt);
191}
192
193const char* feSetOptValue(feOptIndex opt, int optarg)
194{
195  if (opt == FE_OPT_UNDEF) return "option undefined";
196
197  if (feOptSpec[opt].type != feOptUntyped)
198  {
199    if (feOptSpec[opt].type == feOptString)
200      return "option value needs to be an integer";
201
202    feOptSpec[opt].value = (void*) optarg;
203  }
204  return feOptAction(opt);
205}
206
207static const char* feOptAction(feOptIndex opt)
208{
209  // do some special actions
210  switch(opt)
211  {
212      case FE_OPT_BATCH:
213        if (feOptSpec[FE_OPT_BATCH].value)
214          fe_fgets_stdin=fe_fgets_dummy;
215        return NULL;
216
217      case FE_OPT_HELP:
218        feOptHelp(feArgv0);
219        return NULL;
220
221      case FE_OPT_QUIET:
222        if (feOptSpec[FE_OPT_QUIET].value)
223          si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
224        else
225          si_opt_2 |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
226        return NULL;
227
228      case FE_OPT_NO_TTY:
229#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
230        if (feOptSpec[FE_OPT_NO_TTY].value)
231          fe_fgets_stdin=fe_fgets;
232#endif
233        return NULL;
234
235      case FE_OPT_SDB:
236      #ifdef HAVE_SDB
237        if (feOptSpec[FE_OPT_SDB].value)
238          sdb_flags = 1;
239        else
240          sdb_flags = 0;
241      #endif
242        return NULL;
243
244      case FE_OPT_VERSION:
245        {
246        char *s=versionString();
247        printf("%s",s);
248        omFree(s);
249        return NULL;
250        }
251
252      case FE_OPT_ECHO:
253        si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
254        if (si_echo < 0 || si_echo > 9)
255          return "argument of option is not in valid range 0..9";
256        return NULL;
257
258      case FE_OPT_RANDOM:
259        siRandomStart = (unsigned int) ((unsigned long)
260                                          (feOptSpec[FE_OPT_RANDOM].value));
261        siSeed=siRandomStart;
262#ifdef HAVE_FACTORY
263        factoryseed(siRandomStart);
264#endif
265        return NULL;
266
267      case FE_OPT_EMACS:
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        }
277        return NULL;
278
279      case FE_OPT_NO_WARN:
280        if (feOptSpec[FE_OPT_NO_WARN].value)
281          feWarn = FALSE;
282        else
283          feWarn = TRUE;
284        return NULL;
285
286      case FE_OPT_NO_OUT:
287        if (feOptSpec[FE_OPT_NO_OUT].value)
288          feOut = FALSE;
289        else
290          feOut = TRUE;
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:
302        feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
303
304      case FE_OPT_TICKS_PER_SEC:
305      {
306        int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
307        if (ticks <= 0)
308          return "integer argument must be larger than 0";
309        SetTimerResolution(ticks);
310        return NULL;
311      }
312
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  {
325    if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
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      {
344        Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
345      }
346    }
347    i++;
348  }
349}
350
351#endif // ! ESingular
352
353// Prints help message
354void feOptHelp(const char* name)
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");
360#endif
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  {
367    if (feOptSpec[i].help != NULL
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");
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");
401}
402
403
404
405#endif // GENERATE_OPTION_INDEX
Note: See TracBrowser for help on using the repository browser.