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