forked from Singular/Singular
/
feOpt.cc
405 lines (349 loc) · 8.95 KB
/
feOpt.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/****************************************
* Computer Algebra System SINGULAR *
****************************************/
/*
* ABSTRACT: Implementation of option buisness
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <kernel/mod2.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_FACTORY
#define SI_DONT_HAVE_GLOBAL_VARS
#include <factory/factory.h>
#endif
#define FE_OPT_STRUCTURE
#include "feOpt.h"
#if !defined(GENERATE_OPTION_INDEX) && !defined(ESINGULAR) && !defined(TSINGULAR)
#include <misc/options.h>
#endif
#include "fehelp.h"
const char SHORT_OPTS_STRING[] = "bdhqstvxec:r:u:";
//////////////////////////////////////////////////////////////
//
// Generation of feOptIndex
//
#ifdef GENERATE_OPTION_INDEX
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
FILE* fd;
#ifdef ESINGULAR
fd = fopen("feOptES.xx", "w");
#elif defined(TSINGULAR)
fd = fopen("feOptTS.xx", "w");
#else
fd = fopen("feOpt.xx", "w");
#endif
if (fd == NULL) exit(1);
int i = 0;
fputs("typedef enum\n{\n", fd);
while (feOptSpec[i].name != NULL)
{
const char* name = feOptSpec[i].name;
fputs("FE_OPT_", fd);
while (*name != 0)
{
if (*name == '-')
{
putc('_', fd);
}
else if (*name >= 97 && *name <= 122)
{
putc(*name - 32, fd);
}
else
{
putc(*name, fd);
}
name++;
}
if (i == 0)
{
fputs("=0", fd);
}
i++;
fputs(",\n ", fd);
}
fprintf(fd, "FE_OPT_UNDEF\n} feOptIndex;\n");
fclose(fd);
#ifdef ESINGULAR
rename("feOptES.xx", "feOptES.inc");
#elif defined(TSINGULAR)
rename("feOptTS.xx", "feOptTS.inc");
#else
rename("feOpt.xx", "feOpt.inc");
#endif
return(0);
}
#else // ! GENERATE_OPTION_INDEX
///////////////////////////////////////////////////////////////
//
// Getting Values
//
feOptIndex feGetOptIndex(const char* name)
{
int opt = 0;
while (opt != (int) FE_OPT_UNDEF)
{
if (strcmp(feOptSpec[opt].name, name) == 0)
return (feOptIndex) opt;
opt = opt + 1;
}
return FE_OPT_UNDEF;
}
feOptIndex feGetOptIndex(int optc)
{
int opt = 0;
if (optc == LONG_OPTION_RETURN) return FE_OPT_UNDEF;
while (opt != (int) FE_OPT_UNDEF)
{
if (feOptSpec[opt].val == optc)
return (feOptIndex) opt;
opt = opt + 1;
}
return FE_OPT_UNDEF;
}
void* feGetOptValue(feOptIndex opt)
{
return feOptSpec[(int)opt].value;
}
///////////////////////////////////////////////////////////////
//
// Setting Values
//
//
// Return: NULL -- everything ok
// "error-string" on error
#if !defined(ESINGULAR) && !defined(TSINGULAR)
#include <omalloc/omalloc.h>
#include <kernel/febase.h>
#include <kernel/timer.h>
#include "ipshell.h"
#include "tok.h"
#include "sdb.h"
#include "cntrlc.h"
#include <errno.h>
static const char* feOptAction(feOptIndex opt);
const char* feSetOptValue(feOptIndex opt, char* optarg)
{
if (opt == FE_OPT_UNDEF) return "option undefined";
if (feOptSpec[opt].type != feOptUntyped)
{
if (feOptSpec[opt].type != feOptString)
{
if (optarg != NULL)
{
errno = 0;
feOptSpec[opt].value = (void*) strtol(optarg, NULL, 10);
if (errno) return "invalid integer argument";
}
else
{
feOptSpec[opt].value = (void*) 0;
}
}
else
{
assume(feOptSpec[opt].type == feOptString);
if (feOptSpec[opt].set && feOptSpec[opt].value != NULL)
omFree(feOptSpec[opt].value);
if (optarg != NULL)
feOptSpec[opt].value = omStrDup(optarg);
else
feOptSpec[opt].value = NULL;
feOptSpec[opt].set = 1;
}
}
return feOptAction(opt);
}
const char* feSetOptValue(feOptIndex opt, int optarg)
{
if (opt == FE_OPT_UNDEF) return "option undefined";
if (feOptSpec[opt].type != feOptUntyped)
{
if (feOptSpec[opt].type == feOptString)
return "option value needs to be an integer";
feOptSpec[opt].value = (void*) optarg;
}
return feOptAction(opt);
}
static const char* feOptAction(feOptIndex opt)
{
// do some special actions
switch(opt)
{
case FE_OPT_BATCH:
if (feOptSpec[FE_OPT_BATCH].value)
fe_fgets_stdin=fe_fgets_dummy;
return NULL;
case FE_OPT_HELP:
feOptHelp(feArgv0);
return NULL;
case FE_OPT_QUIET:
if (feOptSpec[FE_OPT_QUIET].value)
si_opt_2 &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
else
si_opt_2 |= Sy_bit(V_LOAD_LIB)|Sy_bit(0);
return NULL;
case FE_OPT_NO_TTY:
#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
if (feOptSpec[FE_OPT_NO_TTY].value)
fe_fgets_stdin=fe_fgets;
#endif
return NULL;
case FE_OPT_SDB:
#ifdef HAVE_SDB
if (feOptSpec[FE_OPT_SDB].value)
sdb_flags = 1;
else
sdb_flags = 0;
#endif
return NULL;
case FE_OPT_VERSION:
{
char *s=versionString();
printf("%s",s);
omFree(s);
return NULL;
}
case FE_OPT_ECHO:
si_echo = (int) ((long)(feOptSpec[FE_OPT_ECHO].value));
if (si_echo < 0 || si_echo > 9)
return "argument of option is not in valid range 0..9";
return NULL;
case FE_OPT_RANDOM:
siRandomStart = (unsigned int) ((unsigned long)
(feOptSpec[FE_OPT_RANDOM].value));
siSeed=siRandomStart;
#ifdef HAVE_FACTORY
factoryseed(siRandomStart);
#endif
return NULL;
case FE_OPT_EMACS:
if (feOptSpec[FE_OPT_EMACS].value)
{
// print EmacsDir and InfoFile so that Emacs
// mode can pcik it up
Warn("EmacsDir: %s", (feResource('e' /*"EmacsDir"*/) != NULL ?
feResource('e' /*"EmacsDir"*/) : ""));
Warn("InfoFile: %s", (feResource('i' /*"InfoFile"*/) != NULL ?
feResource('i' /*"InfoFile"*/) : ""));
}
return NULL;
case FE_OPT_NO_WARN:
if (feOptSpec[FE_OPT_NO_WARN].value)
feWarn = FALSE;
else
feWarn = TRUE;
return NULL;
case FE_OPT_NO_OUT:
if (feOptSpec[FE_OPT_NO_OUT].value)
feOut = FALSE;
else
feOut = TRUE;
return NULL;
case FE_OPT_MIN_TIME:
{
double mintime = atof((char*) feOptSpec[FE_OPT_MIN_TIME].value);
if (mintime <= 0) return "invalid float argument";
SetMinDisplayTime(mintime);
return NULL;
}
case FE_OPT_BROWSER:
feHelpBrowser((char*) feOptSpec[FE_OPT_BROWSER].value, 1);
case FE_OPT_TICKS_PER_SEC:
{
int ticks = (int) ((long)(feOptSpec[FE_OPT_TICKS_PER_SEC].value));
if (ticks <= 0)
return "integer argument must be larger than 0";
SetTimerResolution(ticks);
return NULL;
}
default:
return NULL;
}
}
// Prints usage message
void fePrintOptValues()
{
int i = 0;
while (feOptSpec[i].name != 0)
{
if (feOptSpec[i].help != NULL && feOptSpec[i].type != feOptUntyped
#ifndef NDEBUG
&& *(feOptSpec[i].help) != '/'
#endif
)
{
if (feOptSpec[i].type == feOptString)
{
if (feOptSpec[i].value == NULL)
{
Print("// --%-15s\n", feOptSpec[i].name);
}
else
{
Print("// --%-15s \"%s\"\n", feOptSpec[i].name, (char*) feOptSpec[i].value);
}
}
else
{
Print("// --%-15s %d\n", feOptSpec[i].name, (int)(long)feOptSpec[i].value);
}
}
i++;
}
}
#endif // ! ESingular
// Prints help message
void feOptHelp(const char* name)
{
int i = 0;
char tmp[20];
#ifdef ESINGULAR
printf("ESingular: A Program that starts-up Singular within emacs, for\n");
#endif
printf("Singular version %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
printf(" %s [options] [file1 [file2 ...]]\n", name);
printf("Options:\n");
while (feOptSpec[i].name != 0)
{
if (feOptSpec[i].help != NULL
#ifdef NDEBUG
&& *(feOptSpec[i].help) != '/'
#endif
)
{
if (feOptSpec[i].has_arg > 0)
{
if (feOptSpec[i].has_arg > 1)
sprintf(tmp, "%s[=%s]", feOptSpec[i].name, feOptSpec[i].arg_name);
else
sprintf(tmp, "%s=%s", feOptSpec[i].name, feOptSpec[i].arg_name);
printf(" %c%c --%-19s %s\n",
(feOptSpec[i].val != 0 ? '-' : ' '),
(feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
tmp,
feOptSpec[i].help);
}
else
{
printf(" %c%c --%-19s %s\n",
(feOptSpec[i].val != 0 ? '-' : ' '),
(feOptSpec[i].val != 0 ? feOptSpec[i].val : ' '),
feOptSpec[i].name,
feOptSpec[i].help);
}
}
i++;
}
printf("\nFor more information, type `help;' from within Singular or visit\n");
printf("http://www.singular.uni-kl.de or consult the\n");
printf("Singular manual (available as on-line info or html manual).\n");
}
#endif // GENERATE_OPTION_INDEX