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 <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 
29 const char SHORT_OPTS_STRING[] = "bdhpqstvxec: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>
40 int 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 
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 
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 
152 static const char* feOptAction(feOptIndex opt);
153 const 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 
187 const 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 
201 static 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)
209  return NULL;
210 
211  case FE_OPT_HELP:
213  return NULL;
214 
215  case FE_OPT_PROFILE:
216  traceit=1024;
217  return NULL;
218 
219  case FE_OPT_QUIET:
220  if (feOptSpec[FE_OPT_QUIET].value)
221  si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
222  else
224  return NULL;
225 
226  case FE_OPT_NO_TTY:
227  if (feOptSpec[FE_OPT_NO_TTY].value)
229  return NULL;
230 
231  case FE_OPT_SDB:
232  #ifdef HAVE_SDB
233  if (feOptSpec[FE_OPT_SDB].value)
234  sdb_flags = 1;
235  else
236  sdb_flags = 0;
237  #endif
238  return NULL;
239 
240  case FE_OPT_VERSION:
241  {
242  char *s=versionString();
243  printf("%s",s);
244  omFree(s);
245  return NULL;
246  }
247 
248  case FE_OPT_ECHO:
249  si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
250  if (si_echo < 0 || si_echo > 9)
251  return "argument of option is not in valid range 0..9";
252  return NULL;
253 
254  case FE_OPT_RANDOM:
255  siRandomStart = (unsigned int) ((unsigned long)
256  (feOptSpec[FE_OPT_RANDOM].value));
259  return NULL;
260 
261  case FE_OPT_EMACS:
262  if (feOptSpec[FE_OPT_EMACS].value)
263  {
264  // print EmacsDir and InfoFile so that Emacs
265  // mode can pcik it up
266  Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
267  feResource('e' /*"EmacsDir"*/) : ""));
268  Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
269  feResource('i' /*"InfoFile"*/) : ""));
270  }
271  return NULL;
272 
273  case FE_OPT_NO_WARN:
274  if (feOptSpec[FE_OPT_NO_WARN].value)
275  feWarn = FALSE;
276  else
277  feWarn = TRUE;
278  return NULL;
279 
280  case FE_OPT_NO_OUT:
281  if (feOptSpec[FE_OPT_NO_OUT].value)
282  feOut = FALSE;
283  else
284  feOut = TRUE;
285  return NULL;
286 
287  case FE_OPT_MIN_TIME:
288  {
289  double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
290  if (mintime <= 0) return "invalid float argument";
292  return NULL;
293  }
294 
295  case FE_OPT_BROWSER:
296  feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
297 
298  case FE_OPT_TICKS_PER_SEC:
299  {
300  int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
301  if (ticks <= 0)
302  return "integer argument must be larger than 0";
303  SetTimerResolution(ticks);
304  return NULL;
305  }
306 
307  case FE_OPT_DUMP_VERSIONTUPLE:
308  {
310  return NULL;
311  }
312 
313  default:
314  return NULL;
315  }
316 }
317 
318 // Prints usage message
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 SING_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
354 void feOptHelp(const char* name)
355 {
356  int i = 0;
357  char tmp[20];
358 #if defined(ESINGULAR)
359  printf("ESingular starts up Singular within emacs;\n");
360 #elif defined(TSINGULAR)
361  printf("TSingular starts up Singular within a terminal window;\n");
362 #endif
363  printf("Singular is a Computer Algebra System (CAS) for Polynomial Computations.\n");
364  printf("Usage: %s [options] [file1 [file2 ...]]\n", name);
365  printf("Options:\n");
366 
367  while (feOptSpec[i].name != 0)
368  {
369  if (feOptSpec[i].help != NULL
370 #ifdef SING_NDEBUG
371  && *(feOptSpec[i].help) != '/'
372 #endif
373  )
374  {
375  if (feOptSpec[i].has_arg > 0)
376  {
377  if (feOptSpec[i].has_arg > 1)
378  sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
379  else
380  sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
381 
382  printf(" %c%c --%-20s %s\n",
383  (feOptSpec[i].val != LONG_OPTION_RETURN ? '-' : ' '),
384  (feOptSpec[i].val != LONG_OPTION_RETURN ? feOptSpec[i].val : ' '),
385  tmp,
386  feOptSpec[i].help);
387  }
388  else
389  {
390  printf(" %c%c --%-20s %s\n",
391  (feOptSpec[i].val != LONG_OPTION_RETURN ? '-' : ' '),
392  (feOptSpec[i].val != LONG_OPTION_RETURN ? feOptSpec[i].val : ' '),
393  feOptSpec[i].name,
394  feOptSpec[i].help);
395  }
396  }
397  i++;
398  }
399 
400  printf("\nFor more information, type `help;' from within Singular or visit\n");
401  printf("http://www.singular.uni-kl.de or consult the\n");
402  printf("Singular manual (available as on-line info or html manual).\n");
403 }
404 
406 {
407  printf("%s\n",VERSION);
408 }
409 
410 #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:262
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:22
int sdb_flags
Definition: sdb.cc:32
#define Print
Definition: emacs.cc:83
int main(int argc, char *argv[])
Definition: omTables.c:165
char * versionString()
Definition: misc_ip.cc:778
#define FALSE
Definition: auxiliary.h:94
BOOLEAN feOut
Definition: reporter.cc:50
#define V_LOAD_LIB
Definition: options.h:45
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:102
#define TRUE
Definition: auxiliary.h:98
void * value
Definition: fegetopt.h:93
const char * feSetOptValue(feOptIndex opt, char *optarg)
Definition: feOpt.cc:153
int traceit
Definition: febase.cc:47
#define Sy_bit(x)
Definition: options.h:30
static const char * feOptAction(feOptIndex opt)
Definition: feOpt.cc:201
#define SING_NDEBUG
Definition: factoryconf.h:276
void feOptDumpVersionTuple(void)
Definition: feOpt.cc:405
int set
Definition: fegetopt.h:94
void SetTimerResolution(int res)
Definition: timer.cc:24
#define omFree(addr)
Definition: omAllocDecl.h:261
#define assume(x)
Definition: mod2.h:394
struct fe_option feOptSpec[]
void fePrintOptValues()
Definition: feOpt.cc:319
int i
Definition: cfEzgcd.cc:123
char name(const Variable &v)
Definition: factory.h:178
void SetMinDisplayTime(double mtime)
Definition: timer.cc:29
#define help
Definition: libparse.cc:1228
char * feArgv0
Definition: feResource.cc:19
#define NULL
Definition: omList.c:10
#define VERSION
Definition: mod2.h:16
#define LONG_OPTION_RETURN
Definition: feOptTab.h:4
int siSeed
Definition: sirandom.c:29
void feOptHelp(const char *name)
Definition: feOpt.cc:354
feOptIndex feGetOptIndex(const char *name)
Definition: feOpt.cc:104
BOOLEAN feWarn
Definition: reporter.cc:49
unsigned si_opt_2
Definition: options.c:6
const char SHORT_OPTS_STRING[]
Definition: feOpt.cc:29
int si_echo
Definition: febase.cc:41
#define Warn
Definition: emacs.cc:80
#define omStrDup(s)
Definition: omAllocDecl.h:263