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