source: git/Singular/feOpt.cc @ 0df59c8

spielwiese
Last change on this file since 0df59c8 was bd795d, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
improved build system in Singuar/: building of libparse and [TE]Singular ADD/CHG: distribute and install emacs/ (under $(datadir)/) CHG: moved fegetopt.{c,h} from kernel/ to Singular/ ADD: build and install Singular/[TE]Singular ADD: better Singular/libparse CHG: only build the static release and debug binaries Singular&Singularg by default!
  • 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 "config.h"
10#include <kernel/mod2.h>
11
12#include <string.h>
13#include <stdlib.h>
14
15#ifdef HAVE_FACTORY
16#define SI_DONT_HAVE_GLOBAL_VARS
17#include <factory/factory.h>
18#endif
19
20#define FE_OPT_STRUCTURE
21#include "feOpt.h"
22
23#if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
24#include <misc/options.h>
25#endif
26
27#include "fehelp.h"
28
29
30const char SHORT_OPTS_STRING[] = "bdhqstvxec:r:u:";
31
32//////////////////////////////////////////////////////////////
33//
34// Generation of feOptIndex
35//
36#ifdef GENERATE_OPTION_INDEX
37
38#include <stdio.h>
39#include <unistd.h>
40#include <stdlib.h>
41int main()
42{
43  FILE* fd;
44#ifdef ESINGULAR
45  fd = fopen("feOptES.xx", "w");
46#elif defined(TSINGULAR)
47  fd = fopen("feOptTS.xx", "w");
48#else
49  fd = fopen("feOpt.xx", "w");
50#endif
51
52  if (fd == NULL) exit(1);
53
54  int i = 0;
55
56  fputs("typedef enum\n{\n", fd);
57
58  while (feOptSpec[i].name != NULL)
59  {
60    const char* name = feOptSpec[i].name;
61    fputs("FE_OPT_", fd);
62    while (*name != 0)
63    {
64      if (*name == '-')
65      {
66        putc('_', fd);
67      }
68      else if (*name >= 97 && *name <= 122)
69      {
70        putc(*name - 32, fd);
71      }
72      else
73      {
74        putc(*name, fd);
75      }
76      name++;
77    }
78    if (i == 0)
79    {
80      fputs("=0", fd);
81    }
82    i++;
83    fputs(",\n  ", fd);
84  }
85
86  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
87  fclose(fd);
88#ifdef ESINGULAR
89  rename("feOptES.xx", "feOptES.inc");
90#elif defined(TSINGULAR)
91  rename("feOptTS.xx", "feOptTS.inc");
92#else
93  rename("feOpt.xx", "feOpt.inc");
94#endif
95  return(0);
96}
97
98#else // ! GENERATE_OPTION_INDEX
99
100///////////////////////////////////////////////////////////////
101//
102// Getting Values
103//
104
105feOptIndex feGetOptIndex(const char* name)
106{
107  int opt = 0;
108
109  while (opt != (int) FE_OPT_UNDEF)
110  {
111    if (strcmp(feOptSpec[opt].name, name) == 0)
112      return (feOptIndex) opt;
113    opt = opt + 1;
114  }
115  return FE_OPT_UNDEF;
116}
117
118feOptIndex feGetOptIndex(int optc)
119{
120  int opt = 0;
121
122  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
123
124  while (opt != (int) FE_OPT_UNDEF)
125  {
126    if (feOptSpec[opt].val == optc)
127      return (feOptIndex) opt;
128    opt = opt + 1;
129  }
130  return FE_OPT_UNDEF;
131}
132
133void* feGetOptValue(feOptIndex opt)
134{
135  return feOptSpec[(int)opt].value;
136}
137
138///////////////////////////////////////////////////////////////
139//
140// Setting Values
141//
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
349void 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.