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 #include <string.h>
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 
138 #include "omalloc/omalloc.h"
139 #include "resources/feResource.h"
140 #include "kernel/oswrapper/feread.h"
141 #include "kernel/oswrapper/timer.h"
142 
143 #include "ipshell.h"
144 #include "tok.h"
145 #include "sdb.h"
146 #include "cntrlc.h"
147 
148 #include <errno.h>
149 
150 static const char* feOptAction(feOptIndex opt);
151 const char* feSetOptValue(feOptIndex opt, char* optarg)
152 {
153  if (opt == FE_OPT_UNDEF) return "option undefined";
154 
155  if (feOptSpec[opt].type != feOptUntyped)
156  {
157  if (feOptSpec[opt].type != feOptString)
158  {
159  if (optarg != NULL)
160  {
161  errno = 0;
162  feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
163  if (errno) return "invalid integer argument";
164  }
165  else
166  {
167  feOptSpec[opt].value = (void*) 0;
168  }
169  }
170  else
171  {
172  assume(feOptSpec[opt].type == feOptString);
173  if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
174  omFree(feOptSpec[opt].value);
175  if (optarg != NULL)
176  feOptSpec[opt].value = omStrDup(optarg);
177  else
178  feOptSpec[opt].value = NULL;
179  feOptSpec[opt].set = 1;
180  }
181  }
182  return feOptAction(opt);
183 }
184 
185 const char* feSetOptValue(feOptIndex opt, int optarg)
186 {
187  if (opt == FE_OPT_UNDEF) return "option undefined";
188 
189  if (feOptSpec[opt].type != feOptUntyped)
190  {
191  if (feOptSpec[opt].type == feOptString)
192  return "option value needs to be an integer";
193 
194  feOptSpec[opt].value = (void*)(long) optarg;
195  }
196  return feOptAction(opt);
197 }
198 
199 static const char* feOptAction(feOptIndex opt)
200 {
201  // do some special actions
202  switch(opt)
203  {
204  case FE_OPT_BATCH:
205  if (feOptSpec[FE_OPT_BATCH].value)
207  return NULL;
208 
209  case FE_OPT_HELP:
211  return NULL;
212 
213  case FE_OPT_PROFILE:
214  traceit=1024;
215  return NULL;
216 
217  case FE_OPT_QUIET:
218  if (feOptSpec[FE_OPT_QUIET].value)
219  si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
220  else
222  return NULL;
223 
224  case FE_OPT_NO_TTY:
225  if (feOptSpec[FE_OPT_NO_TTY].value)
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));
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  case FE_OPT_DUMP_VERSIONTUPLE:
306  {
308  return NULL;
309  }
310 
311  default:
312  return NULL;
313  }
314 }
315 
316 // Prints usage message
318 {
319  int i = 0;
320 
321  while (feOptSpec[i].name != 0)
322  {
323  if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
324 #ifndef SING_NDEBUG
325  && *(feOptSpec[i].help) != '/'
326 #endif
327  )
328  {
329  if (feOptSpec[i].type == feOptString)
330  {
331  if (feOptSpec[i].value == NULL)
332  {
333  Print("// --%-15s\n", feOptSpec[i].name);
334  }
335  else
336  {
337  Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
338  }
339  }
340  else
341  {
342  Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
343  }
344  }
345  i++;
346  }
347 }
348 
349 #endif // ! ESingular
350 
351 // Prints help message
352 void feOptHelp(const char* name)
353 {
354  int i = 0;
355  char tmp[20];
356 #if defined(ESINGULAR)
357  printf("ESingular starts up Singular within emacs;\n");
358 #elif defined(TSINGULAR)
359  printf("TSingular starts up Singular within a terminal window;\n");
360 #endif
361  printf("Singular is a Computer Algebra System (CAS) for Polynomial Computations.\n");
362  printf("Usage: %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 SING_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 --%-20s %s\n",
381  (feOptSpec[i].val != LONG_OPTION_RETURN ? '-' : ' '),
382  (feOptSpec[i].val != LONG_OPTION_RETURN ? feOptSpec[i].val : ' '),
383  tmp,
384  feOptSpec[i].help);
385  }
386  else
387  {
388  printf(" %c%c --%-20s %s\n",
389  (feOptSpec[i].val != LONG_OPTION_RETURN ? '-' : ' '),
390  (feOptSpec[i].val != LONG_OPTION_RETURN ? 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 
404 {
405  printf("%s\n",VERSION);
406 }
407 
408 #endif // GENERATE_OPTION_INDEX
feOptIndex
Definition: feOptGen.h:15
int status int fd
Definition: si_signals.h:59
const char * feHelpBrowser(char *which, int warn)
Definition: fehelp.cc:248
const CanonicalForm int s
Definition: facAbsFact.cc:55
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:34
void factoryseed(int s)
random seed initializer
Definition: cf_random.cc:176
static double mintime
Definition: timer.cc:20
int sdb_flags
Definition: sdb.cc:31
#define Print
Definition: emacs.cc:80
int main(int argc, char *argv[])
Definition: omTables.c:165
char * versionString()
Definition: misc_ip.cc:784
#define FALSE
Definition: auxiliary.h:94
BOOLEAN feOut
Definition: reporter.cc:50
#define V_LOAD_LIB
Definition: options.h:47
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:258
char * name
Definition: fegetopt.h:83
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
int siRandomStart
Definition: cntrlc.cc:96
#define TRUE
Definition: auxiliary.h:98
void * value
Definition: fegetopt.h:93
const char * feSetOptValue(feOptIndex opt, char *optarg)
Definition: feOpt.cc:151
int traceit
Definition: febase.cc:42
#define Sy_bit(x)
Definition: options.h:32
static const char * feOptAction(feOptIndex opt)
Definition: feOpt.cc:199
#define SING_NDEBUG
Definition: factoryconf.h:257
void feOptDumpVersionTuple(void)
Definition: feOpt.cc:403
int set
Definition: fegetopt.h:94
void SetTimerResolution(int res)
Definition: timer.cc:22
#define omFree(addr)
Definition: omAllocDecl.h:261
#define assume(x)
Definition: mod2.h:390
struct fe_option feOptSpec[]
void fePrintOptValues()
Definition: feOpt.cc:317
int i
Definition: cfEzgcd.cc:125
char name(const Variable &v)
Definition: factory.h:180
void SetMinDisplayTime(double mtime)
Definition: timer.cc:27
#define help
Definition: libparse.cc:1228
char * feArgv0
Definition: feResource.cc:19
#define NULL
Definition: omList.c:12
#define VERSION
Definition: mod2.h:18
#define LONG_OPTION_RETURN
Definition: feOptTab.h:4
int siSeed
Definition: sirandom.c:29
void feOptHelp(const char *name)
Definition: feOpt.cc:352
feOptIndex feGetOptIndex(const char *name)
Definition: feOpt.cc:101
BOOLEAN feWarn
Definition: reporter.cc:49
unsigned si_opt_2
Definition: options.c:6
const char SHORT_OPTS_STRING[]
Definition: feOpt.cc:26
int si_echo
Definition: febase.cc:35
#define Warn
Definition: emacs.cc:77
#define omStrDup(s)
Definition: omAllocDecl.h:263