My Project  debian-1:4.1.1-p2+ds-4build4
feOpt.cc
Go to the documentation of this file.
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 "factory/factory.h"
14 
15 #define FE_OPT_STRUCTURE
16 #include "feOpt.h"
17 
18 #if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
19 #include "misc/options.h"
20 #include "misc/sirandom.h"
21 #endif
22 
23 #include "fehelp.h"
24 
25 
26 const char SHORT_OPTS_STRING[] = "bdhpqstvxec:r:u:";
27 
28 //////////////////////////////////////////////////////////////
29 //
30 // Generation of feOptIndex
31 //
32 #ifdef GENERATE_OPTION_INDEX
33 
34 #include <stdio.h>
35 //#include <unistd.h>
36 //#include <stdlib.h>
37 int main()
38 {
39  FILE* fd;
40 #ifdef ESINGULAR
41  fd = fopen("feOptES.xx", "w");
42 #elif defined(TSINGULAR)
43  fd = fopen("feOptTS.xx", "w");
44 #else
45  fd = fopen("feOpt.xx", "w");
46 #endif
47 
48  if (fd == NULL) exit(1);
49 
50  int i = 0;
51 
52  fputs("typedef enum\n{\n", fd);
53 
54  while (feOptSpec[i].name != NULL)
55  {
56  const char* name = feOptSpec[i].name;
57  fputs("FE_OPT_", fd);
58  while (*name != 0)
59  {
60  if (*name == '-')
61  {
62  putc('_', fd);
63  }
64  else if (*name >= 97 && *name <= 122)
65  {
66  putc(*name - 32, fd);
67  }
68  else
69  {
70  putc(*name, fd);
71  }
72  name++;
73  }
74  if (i == 0)
75  {
76  fputs("=0", fd);
77  }
78  i++;
79  fputs(",\n ", fd);
80  }
81 
82  fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
83  fclose(fd);
84 #ifdef ESINGULAR
85  rename("feOptES.xx", "feOptES.inc");
86 #elif defined(TSINGULAR)
87  rename("feOptTS.xx", "feOptTS.inc");
88 #else
89  rename("feOpt.xx", "feOpt.inc");
90 #endif
91  return(0);
92 }
93 
94 #else // ! GENERATE_OPTION_INDEX
95 
96 ///////////////////////////////////////////////////////////////
97 //
98 // Getting Values
99 //
100 
102 {
103  int opt = 0;
104 
105  while (opt != (int) FE_OPT_UNDEF)
106  {
107  if (strcmp(feOptSpec[opt].name, name) == 0)
108  return (feOptIndex) opt;
109  opt = opt + 1;
110  }
111  return FE_OPT_UNDEF;
112 }
113 
115 {
116  int opt = 0;
117 
118  if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
119 
120  while (opt != (int) FE_OPT_UNDEF)
121  {
122  if (feOptSpec[opt].val == optc)
123  return (feOptIndex) opt;
124  opt = opt + 1;
125  }
126  return FE_OPT_UNDEF;
127 }
128 
129 ///////////////////////////////////////////////////////////////
130 //
131 // Setting Values
132 //
133 //
134 // Return: NULL -- everything ok
135 // "error-string" on error
136 #if !defined(ESINGULAR) && !defined(TSINGULAR)
137 #include "omalloc/omalloc.h"
138 #include "resources/feResource.h"
139 #include "kernel/oswrapper/feread.h"
140 #include "kernel/oswrapper/timer.h"
141 
142 #include "ipshell.h"
143 #include "tok.h"
144 #include "sdb.h"
145 #include "cntrlc.h"
146 
147 #include <errno.h>
148 
149 static const char* feOptAction(feOptIndex opt);
150 const char* feSetOptValue(feOptIndex opt, char* optarg)
151 {
152  if (opt == FE_OPT_UNDEF) return "option undefined";
153 
154  if (feOptSpec[opt].type != feOptUntyped)
155  {
156  if (feOptSpec[opt].type != feOptString)
157  {
158  if (optarg != NULL)
159  {
160  errno = 0;
161  feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
162  if (errno) return "invalid integer argument";
163  }
164  else
165  {
166  feOptSpec[opt].value = (void*) 0;
167  }
168  }
169  else
170  {
171  assume(feOptSpec[opt].type == feOptString);
172  if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
173  omFree(feOptSpec[opt].value);
174  if (optarg != NULL)
175  feOptSpec[opt].value = omStrDup(optarg);
176  else
177  feOptSpec[opt].value = NULL;
178  feOptSpec[opt].set = 1;
179  }
180  }
181  return feOptAction(opt);
182 }
183 
184 const char* feSetOptValue(feOptIndex opt, int optarg)
185 {
186  if (opt == FE_OPT_UNDEF) return "option undefined";
187 
188  if (feOptSpec[opt].type != feOptUntyped)
189  {
190  if (feOptSpec[opt].type == feOptString)
191  return "option value needs to be an integer";
192 
193  feOptSpec[opt].value = (void*)(long) optarg;
194  }
195  return feOptAction(opt);
196 }
197 
198 static const char* feOptAction(feOptIndex opt)
199 {
200  // do some special actions
201  switch(opt)
202  {
203  case FE_OPT_BATCH:
204  if (feOptSpec[FE_OPT_BATCH].value)
206  return NULL;
207 
208  case FE_OPT_HELP:
210  return NULL;
211 
212  case FE_OPT_PROFILE:
213  traceit=1024;
214  return NULL;
215 
216  case FE_OPT_QUIET:
217  if (feOptSpec[FE_OPT_QUIET].value)
218  si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
219  else
221  return NULL;
222 
223  case FE_OPT_NO_TTY:
224  if (feOptSpec[FE_OPT_NO_TTY].value)
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));
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";
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  case FE_OPT_DUMP_VERSIONTUPLE:
305  {
307  return NULL;
308  }
309 
310  default:
311  return NULL;
312  }
313 }
314 
315 // Prints usage message
317 {
318  int i = 0;
319 
320  while (feOptSpec[i].name != 0)
321  {
322  if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
323 #ifndef SING_NDEBUG
324  && *(feOptSpec[i].help) != '/'
325 #endif
326  )
327  {
328  if (feOptSpec[i].type == feOptString)
329  {
330  if (feOptSpec[i].value == NULL)
331  {
332  Print("// --%-15s\n", feOptSpec[i].name);
333  }
334  else
335  {
336  Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
337  }
338  }
339  else
340  {
341  Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
342  }
343  }
344  i++;
345  }
346 }
347 
348 #endif // ! ESingular
349 
350 // Prints help message
351 void feOptHelp(const char* name)
352 {
353  int i = 0;
354  char tmp[20];
355 #if defined(ESINGULAR)
356  printf("ESingular starts up Singular within emacs;\n");
357 #elif defined(TSINGULAR)
358  printf("TSingular starts up Singular within a terminal window;\n");
359 #endif
360  printf("Singular is a Computer Algebra System (CAS) for Polynomial Computations.\n");
361  printf("Usage: %s [options] [file1 [file2 ...]]\n", name);
362  printf("Options:\n");
363 
364  while (feOptSpec[i].name != 0)
365  {
366  if (feOptSpec[i].help != NULL
367 #ifdef SING_NDEBUG
368  && *(feOptSpec[i].help) != '/'
369 #endif
370  )
371  {
372  if (feOptSpec[i].has_arg > 0)
373  {
374  if (feOptSpec[i].has_arg > 1)
375  sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
376  else
377  sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
378 
379  printf(" %c%c --%-20s %s\n",
380  (feOptSpec[i].val != LONG_OPTION_RETURN ? '-' : ' '),
381  (feOptSpec[i].val != LONG_OPTION_RETURN ? feOptSpec[i].val : ' '),
382  tmp,
383  feOptSpec[i].help);
384  }
385  else
386  {
387  printf(" %c%c --%-20s %s\n",
388  (feOptSpec[i].val != LONG_OPTION_RETURN ? '-' : ' '),
389  (feOptSpec[i].val != LONG_OPTION_RETURN ? feOptSpec[i].val : ' '),
390  feOptSpec[i].name,
391  feOptSpec[i].help);
392  }
393  }
394  i++;
395  }
396 
397  printf("\nFor more information, type `help;' from within Singular or visit\n");
398  printf("http://www.singular.uni-kl.de or consult the\n");
399  printf("Singular manual (available as on-line info or html manual).\n");
400 }
401 
403 {
404  printf("%s\n",VERSION);
405 }
406 
407 #endif // GENERATE_OPTION_INDEX
#define TRUE
Definition: auxiliary.h:98
#define FALSE
Definition: auxiliary.h:94
int i
Definition: cfEzgcd.cc:125
void factoryseed(int s)
random seed initializer
Definition: cf_random.cc:176
int siRandomStart
Definition: cntrlc.cc:98
#define Print
Definition: emacs.cc:80
#define Warn
Definition: emacs.cc:77
const CanonicalForm int s
Definition: facAbsFact.cc:55
char name(const Variable &v)
Definition: factory.h:180
#define SING_NDEBUG
Definition: factoryconf.h:250
feOptIndex
Definition: feOptGen.h:15
@ FE_OPT_UNDEF
Definition: feOptGen.h:15
#define LONG_OPTION_RETURN
Definition: feOptTab.h:4
void fePrintOptValues()
Definition: feOpt.cc:316
const char * feSetOptValue(feOptIndex opt, char *optarg)
Definition: feOpt.cc:150
const char SHORT_OPTS_STRING[]
Definition: feOpt.cc:26
feOptIndex feGetOptIndex(const char *name)
Definition: feOpt.cc:101
static const char * feOptAction(feOptIndex opt)
Definition: feOpt.cc:198
void feOptDumpVersionTuple(void)
Definition: feOpt.cc:402
void feOptHelp(const char *name)
Definition: feOpt.cc:351
struct fe_option feOptSpec[]
char * feArgv0
Definition: feResource.cc:19
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:258
int si_echo
Definition: febase.cc:35
@ feOptUntyped
Definition: fegetopt.h:77
@ feOptString
Definition: fegetopt.h:77
int set
Definition: fegetopt.h:94
char * name
Definition: fegetopt.h:83
void * value
Definition: fegetopt.h:93
const char * feHelpBrowser(char *which, int warn)
Definition: fehelp.cc:252
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:34
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:310
char * fe_fgets_dummy(const char *, char *, int)
Definition: feread.cc:451
char * versionString()
Definition: misc_ip.cc:788
#define help
Definition: libparse.cc:1228
#define VERSION
Definition: mod2.h:18
#define assume(x)
Definition: mod2.h:390
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omFree(addr)
Definition: omAllocDecl.h:261
#define NULL
Definition: omList.c:10
int main(int argc, char *argv[])
Definition: omTables.c:165
unsigned si_opt_2
Definition: options.c:6
#define Sy_bit(x)
Definition: options.h:32
#define V_LOAD_LIB
Definition: options.h:47
BOOLEAN feWarn
Definition: reporter.cc:49
BOOLEAN feOut
Definition: reporter.cc:50
int traceit
Definition: febase.cc:42
int sdb_flags
Definition: sdb.cc:32
int status int fd
Definition: si_signals.h:59
int siSeed
Definition: sirandom.c:29
void SetMinDisplayTime(double mtime)
Definition: timer.cc:29
static double mintime
Definition: timer.cc:22
void SetTimerResolution(int res)
Definition: timer.cc:24