source: git/Singular/feOpt.cc @ fa167e

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