Changeset eea2b0 in git for Singular/tesths.cc


Ignore:
Timestamp:
Apr 16, 1998, 6:10:24 PM (26 years ago)
Author:
Olaf Bachmann <obachman@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
66be51317f9400a7183a651dba28b63c4ebbdf83
Parents:
5821c1b832ca2803db683e6554957c7aac511380
Message:
1998-04-16  Olaf Bachmann  <obachman@mathematik.uni-kl.de>

	* mpsr_PutPoly.cc: Took out normalization of rational numbers
	before they are put -- might have side effects

	* extra.cc: added system("--option") which returns value of
	option; in debug mode: system("OptionValues") lists all option
	values

	* sing_mp.cc: use long option names for MP link options
	* MPtcp:launch: Uses find_exec as default application

	*  tesths.cc: New (handling of) command line options (long
	options, usage of getopt_long): Parse more than one argument
	script, etc ..
	*  added getopt.[c,h]for parsing of command lines

	* mod2.h.in, configure.in: Got rid off str* tests


git-svn-id: file:///usr/local/Singular/svn/trunk@1387 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/tesths.cc

    r5821c1 reea2b0  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: tesths.cc,v 1.32 1998-04-06 17:59:37 obachman Exp $ */
     4/* $Id: tesths.cc,v 1.33 1998-04-16 16:10:24 obachman Exp $ */
    55
    66/*
     
    3333#endif
    3434
     35#include <getopt.h>
     36
     37// define the long option names here
     38#define LON_BATCH           "batch"
     39#define LON_EXECUTE         "execute"
     40#define LON_ECHO            "echo"
     41#define LON_HELP            "help"
     42#define LON_RANDOM          "random"
     43#define LON_QUIET           "quiet"
     44#define LON_NO_TTY          "no-tty"
     45#define LON_USER_OPTION     "user-option"
     46#define LON_VERSION         "version"
     47#define LON_MIN_TIME        "min-time"
     48#define LON_NO_STDLIB       "no-stdlib"
     49#define LON_NO_RC           "no-rc"
     50#define LON_TICKS_PER_SEC   "ticks-per-sec"
     51#define LON_TCLMODE         "tclmode"
     52#define LON_MP_PORT         "MPport"
     53#define LON_MP_HOST         "MPhost"
     54// undocumented options
     55#ifdef HAVE_MPSR
     56#define LON_MP_TRANSP       "MPtransp"
     57#define LON_MP_MODE         "MPmode"
     58#endif
     59
     60static struct option longopts[] =
     61{
     62#ifdef HAVE_MPSR
     63  {LON_BATCH,             0,  0,  'b'},   
     64#endif
     65  {LON_HELP,              0,  0,  'h'},
     66  {LON_QUIET,             0,  0,  'q'},   
     67  {LON_NO_TTY,            0,  0,  't'},
     68  {LON_VERSION,           0,  0,  'v'},
     69#ifdef HAVE_TCL
     70  {LON_TCLMODE,           0,  0,  'x'},
     71#endif
     72  {LON_ECHO,              2,  0,  'e'},
     73  {LON_EXECUTE,           1,  0,  'c'},
     74  {LON_RANDOM,            1,  0,  'r'},
     75  {LON_USER_OPTION,       1,  0,  'u'},
     76  {LON_NO_STDLIB,         0,  0,  0},
     77  {LON_NO_RC,             0,  0,  0},
     78  {LON_MIN_TIME,          1,  0,  0},
     79#ifdef HAVE_MPSR
     80  {LON_MP_PORT,           1,  0,  0},
     81  {LON_MP_HOST,           1,  0,  0},
     82#endif
     83  {LON_TICKS_PER_SEC,     1,  0,  0},
     84// undocumented options
     85#ifdef HAVE_MPSR
     86  {LON_MP_TRANSP,         1,  0,  0},
     87  {LON_MP_MODE,           1,  0,  0},
     88#endif
     89// terminator
     90  { 0, 0, 0, 0 }
     91};
     92// #define SHORT_OPTS_STRING "bc:e::hqr:tu:v"
     93#define SHORT_OPTS_STRING "bhqtvxe::c:r:u:"
     94
     95struct sing_option
     96{
     97  const char*   name;    // as in option
     98  const char*   arg_name;// name of argument, if has_arg > 0
     99  const char*   help;    // (short) help string
     100  char*         val;     // (default) value of option: 0: not set 1:set
     101                         // string:"" if has_arg > 0
     102};
     103
     104// mention only documented options here
     105static struct sing_option sing_longopts[] =
     106{
     107#ifdef HAVE_MPSR
     108  {LON_BATCH,       0,          "Run in MP batch mode",                                 0},   
     109#endif
     110  {LON_HELP,        0,          "Print help message and exit",                          0},
     111  {LON_QUIET,       0,          "Do not print start-up banner and warnings",            0},   
     112  {LON_NO_TTY,      0,          "Do not redefine the terminal characteristics",         0},
     113  {LON_VERSION,     0,          "Print extended version and configuration info",        0},
     114#ifdef HAVE_TCL
     115  {LON_TCLMODE      0,          "Run in TCL mode, i.e., with TCL user interface",       0},
     116#endif 
     117  {LON_ECHO,        "VAL",      "Set value of variable `echo' to (integer) VAL",        ""},
     118  {LON_EXECUTE,     "STRING",   "Execute STRING on start-up",                           ""},
     119  {LON_RANDOM,      "SEED",     "Seed random generator with integer (integer) SEED",    ""},
     120  {LON_USER_OPTION, "STRING",   "Return STRING on `system(\"--user-option\")'",         ""},
     121  {LON_NO_STDLIB,   0,          "Do not load `standard.lib' on start-up",               0},
     122  {LON_NO_RC,       0,          "Do not execute `.singularrc' file on start-up",        0},
     123  {LON_MIN_TIME,    "SECS",     "Do not display times smaller than SECS (in seconds)",  "0.5"},
     124#ifdef HAVE_MPSR
     125  {LON_MP_PORT,     "PORT",     "Use PORT number for MP conections",                    ""},
     126  {LON_MP_HOST,     "HOST",     "Use HOST for MP connections",                          ""},
     127#endif
     128  {LON_TICKS_PER_SEC, "TICKS",    "Sets unit of timer to TICKS per second",             "1"},
     129  { 0, 0, 0, 0 }
     130};
     131
     132static struct sing_option* mainGetSingOption(const char* name)
     133{
     134  int i = 0;
     135  while (sing_longopts[i].name != 0)
     136  {
     137    if (strcmp(name, sing_longopts[i].name) == 0)
     138      return &(sing_longopts[i]);
     139    i++;
     140  }
     141  return NULL;
     142}
     143
     144static void mainSetSingOptionValue(const char* name, char* value)
     145{
     146  sing_option* sopt = mainGetSingOption(name);
     147  if (sopt != NULL)
     148  {
     149    sopt->val = value;
     150  }
     151}
     152
     153
     154static char* mainGetSingOptionValue(const char* name)
     155{
     156  sing_option* sopt = mainGetSingOption(name);
     157  if (sopt!=NULL)
     158    return sopt->val;
     159  return NULL;
     160}
     161
     162
     163// Prints help message
     164static void mainHelp(const char* name)
     165{
     166  int i = 0;
     167  struct sing_option *sopt;
     168  char tmp[20];
     169
     170  printf("Singular %s -- a CAS for polynomial computations. Usage:\n", S_VERSION1);
     171  printf("   %s [options] [file1 [file2 ...]]\n", name);
     172  printf("Options:\n");
     173 
     174  while (longopts[i].name != 0)
     175  {
     176    sopt = mainGetSingOption(longopts[i].name);
     177    if (sopt != NULL)
     178    {
     179      if (longopts[i].has_arg > 0)
     180      {
     181        if  (longopts[i].has_arg > 1)
     182          sprintf(tmp, "%s[=%s]", longopts[i].name, sopt->arg_name);
     183        else
     184          sprintf(tmp, "%s=%s", longopts[i].name, sopt->arg_name);
     185
     186        printf(" %c%c --%-19s %s\n",
     187               (longopts[i].val != 0 ? '-' : ' '),
     188               (longopts[i].val != 0 ? longopts[i].val : ' '),
     189               tmp,
     190               sopt->help);
     191      }
     192      else
     193      {
     194        printf(" %c%c --%-19s %s\n",
     195               (longopts[i].val != 0 ? '-' : ' '),
     196               (longopts[i].val != 0 ? longopts[i].val : ' '),
     197               longopts[i].name,
     198               sopt->help);
     199      }
     200    }
     201#ifndef NDEBUG     
     202    else
     203      printf("--%s Undocumented\n", longopts[i].name);
     204#endif
     205    i++;
     206  }
     207
     208  printf("\nFor more information, type `help;' from within Singular or visit\n");
     209  printf("http://www.mathematik.uni-kl.de/~zca/Singular or consult the\n");
     210  printf("Singular manual (available as on-line info or printed manual).\n");
     211}
     212
     213// Prints usage message
     214static void mainUsage(char* argv0)
     215{
     216  printf("Use `%s --help' for a complete list of options\n", argv0);
     217}
     218
     219#ifndef NDEBUG
     220void mainOptionValues()
     221{
     222  int i = 0;
     223  struct sing_option *sopt;
     224
     225  while (longopts[i].name != 0)
     226  {
     227    sopt = mainGetSingOption(longopts[i].name);
     228    if (sopt != NULL)
     229    {
     230      if (sopt->val == NULL || sopt->val == (char*) 1)
     231        Print("// --%-10s %d\n", sopt->name, sopt->val);
     232      else
     233        Print("// --%-10s %s\n", sopt->name, sopt->val);
     234    }
     235    else
     236      Print("// --%s Undocumented \n", longopts[i].name);
     237    i++;
     238  }
     239}
     240#endif // NDEBUG
     241
     242BOOLEAN mainGetSingOptionValue(const char* name, char** val)
     243{
     244  sing_option* sopt = mainGetSingOption(name);
     245  if (sopt == NULL)
     246  {
     247    *val = NULL;
     248    return FALSE;
     249  }
     250  else
     251  {
     252    *val = sopt->val;
     253    return TRUE;
     254  }
     255}
    35256
    36257/*0 implementation*/
     
    58279  thisfile = argv[0];
    59280  BOOLEAN load_std_lib=TRUE;
     281  BOOLEAN load_rc = TRUE;
     282  char* execute_string = NULL;
     283  int optc, option_index;
     284
     285  // do this first, because -v might print version path
    60286  feGetSearchPath(thisfile);
    61  
    62   /*. process parameters */
    63   for (;(argc > 1) && (!feBatch); --argc, ++argv)
    64   {
    65     if (strcmp(argv[1], "-m") == 0)
     287
     288  // parse command line options
     289  while((optc = getopt_long(argc, argv,
     290                            SHORT_OPTS_STRING, longopts, &option_index))
     291        != EOF)
     292  {
     293    switch(optc)
    66294    {
    67       {
    68         if (argc > 2)
    69         {
    70           char* ptr = NULL;
    71 #ifdef HAVE_STRTOD
    72           double mintime = strtod(argv[2], &ptr);
    73           if (errno != ERANGE && ptr != argv[2])
     295         
     296        case 'b':
     297#ifdef HAVE_MPSR         
     298          feBatch=TRUE;
     299          mainSetSingOptionValue(LON_BATCH, (char*) 1);
     300          break;
    74301#else
    75           double mintime = 0;
    76           sscanf(argv[2],"%f", &mintime);
    77           if (mintime != 0.0)
    78 #endif
    79           {
    80             argc--;
    81             argv++;
     302          printf("%s: Option `-b' not supported in this configuration\n", argv[0]);
     303          mainUsage(argv[0]);
     304          exit(1);
     305#endif
     306
     307        case 'h':
     308          mainHelp(argv[0]);
     309          exit(0);
     310
     311        case 'q':
     312          verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
     313          mainSetSingOptionValue(LON_QUIET, (char*) 1);
     314          break;
     315
     316        case 't':
     317#if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
     318          fe_use_fgets=TRUE;
     319#endif
     320          mainSetSingOptionValue(LON_NO_TTY, (char*) 1);
     321          break;
     322
     323        case 'v':
     324          printf("Singular %s  %s  (%d)  %s %s\n",
     325                 S_VERSION1,S_VERSION2,
     326                 SINGULAR_VERSION_ID,__DATE__,__TIME__);
     327          printf("with\n");
     328          printf(versionString());
     329          printf("\n\n");
     330          mainSetSingOptionValue(LON_VERSION, (char*) 1);
     331          break;
     332
     333        case 'x':
     334#ifdef HAVE_TCL
     335          tclmode = TRUE;
     336          mainSetSingOptionValue(LON_TCL-MODE, (char*) 1);
     337          break;
     338#else
     339          printf("%s: Option `-x' not supported in this configuration\n", argv[0]);
     340          mainUsage(argv[0]);
     341          exit(1);
     342#endif
     343
     344        case 'e':
     345          if (optarg != NULL)
     346          {
     347            errno = 0;
     348            si_echo = strtol(optarg, NULL, 10);
     349            if (errno)
     350            {
     351              printf("%s: `%s' invalid integer argument for option `--%s'\n", argv[0], optarg, LON_ECHO);
     352              mainUsage(argv[0]);
     353              exit(1);
     354            }
     355            if (si_echo < 0 || si_echo > 9)
     356            {
     357              printf("%s: `%s' argument for option `--%s' is not in valid range 0..9\n",
     358                     argv[0], optarg, LON_ECHO);
     359              mainUsage(argv[0]);
     360              exit(1);
     361            }
     362            mainSetSingOptionValue(LON_ECHO, optarg);
     363          }
     364          else
     365          {
     366            si_echo = 1;
     367            mainSetSingOptionValue(LON_ECHO, "1");
     368          }
     369          break;
     370
     371        case 'c':
     372          execute_string = optarg;
     373          mainSetSingOptionValue(LON_EXECUTE, optarg);
     374          break;
     375         
     376        case 'r':
     377          errno = 0;
     378          siRandomStart = strtol(optarg, NULL, 10);
     379          if (errno)
     380          {
     381            printf("%s: `%s' invalid integer argument for option `--%s'\n", argv[0], optarg, LON_RANDOM);
     382            mainUsage(argv[0]);
     383            exit(1);
     384          }
     385          #ifdef buildin_rand
     386          siSeed=siRandomStart;
     387          #else
     388          srand((unsigned int)siRandomStart);
     389          #endif
     390          #ifdef HAVE_FACTORY
     391          factoryseed(siRandomStart);
     392          #endif
     393          mainSetSingOptionValue(LON_RANDOM, optarg);
     394          break;
     395         
     396        case 'u':
     397          mainSetSingOptionValue(LON_USER_OPTION, optarg);
     398          break;
     399
     400        case 0:
     401          if (strcmp(longopts[option_index].name, LON_NO_STDLIB) == 0)
     402          {
     403            load_std_lib = FALSE;
     404            mainSetSingOptionValue(LON_NO_STDLIB, (char*) 1);
     405          }
     406          else if (strcmp(longopts[option_index].name, LON_NO_RC) == 0)
     407          {
     408            load_rc = FALSE;
     409            mainSetSingOptionValue(LON_NO_RC, (char*) 1);
     410          }
     411          else if (strcmp(longopts[option_index].name, LON_MIN_TIME) == 0)
     412          {
     413            double mintime = atof(optarg);
     414            if (mintime <= 0)
     415            {
     416              printf("%s: `%s' invalid float argument for option `--%s'\n", 
     417                     argv[0], optarg, LON_MIN_TIME);
     418              mainUsage(argv[0]);
     419              exit(1);
     420            }
    82421            SetMinDisplayTime(mintime);
     422            mainSetSingOptionValue(LON_MIN_TIME, optarg);
     423          }
     424#ifdef HAVE_MPSR
     425          else if (strcmp(longopts[option_index].name, LON_MP_PORT) == 0)
     426          {
     427            mainSetSingOptionValue(LON_MP_PORT, optarg);
     428          }
     429          else if (strcmp(longopts[option_index].name, LON_MP_HOST) == 0)
     430          {
     431            mainSetSingOptionValue(LON_MP_HOST, optarg);
     432          }
     433#endif         
     434          else if (strcmp(longopts[option_index].name, LON_TICKS_PER_SEC) == 0)
     435          {
     436            int ticks = atoi(optarg);
     437            if (ticks <= 0)
     438            {
     439              printf("%s: `%s' invalid integer argument for option `--%s'\n",
     440                     argv[0], optarg, LON_TICKS_PER_SEC);
     441              mainUsage(argv[0]);
     442              exit(1);
     443            }
     444            SetTimerResolution(ticks);
     445            mainSetSingOptionValue(LON_TICKS_PER_SEC, optarg);
    83446          }
    84447          else
    85             fprintf(stderr, "Can not convert %s to a float\n", argv[2]);
    86         }
    87         else
    88         {
    89           fprintf(stderr, "Need a float to set mintime");
    90         }
    91       }
     448            // undocumented options
     449#ifdef HAVE_MPSR
     450            if (strcmp(longopts[option_index].name, LON_MP_MODE) == 0 ||
     451                strcmp(longopts[option_index].name, LON_MP_TRANSP) == 0)
     452            {
     453              /* ignore */
     454            }
     455          else
     456#endif
     457            assume(0);
     458          break;
     459
     460        default:
     461          // Error message already emmited by getopt_long
     462          mainUsage(argv[0]);
     463          exit(1);
    92464    }
    93     else if (strcmp(argv[1], "-d") == 0)
    94     {
    95      
    96       if (argc > 2)
    97       {
    98         char* ptr = NULL;
    99         // OLAF: try to avoid using long:
    100 #ifdef HAVE_STRTOL
    101         long res = strtol(argv[2], &ptr, 10);
    102         if (errno != ERANGE && ptr != argv[2] && res > 0)
    103 #else
    104           long res = 0;
    105         sscanf(argv[2],"%d", &res);
    106         if (res > 0)
    107 #endif
    108         {
    109           argc--;
    110           argv++;
    111           i=0;
    112           SetTimerResolution(res);
    113         }
    114         else
    115           fprintf(stderr,"Can not convert %s to an integer > 0\n", argv[2]);
    116       }
    117       else
    118       {
    119         long res=0;
    120         while ((argv[1][i+1]>='0') && (argv[1][i+1]<='9'))
    121         {
    122           i++;
    123           res = res*10+(int)(argv[1][i] - '0');
    124           SetTimerResolution(res);
    125         }
    126       }
    127     }
    128     else
    129     {
    130 
    131       if ((argv[1][0] != '-') ||(argv[1][1] == '-'))
    132         break;
    133       for (i=1;argv[1][i]!='\0';i++)
    134       {
    135         switch (argv[1][i])
    136         {
    137             case 'V':
    138             case 'v':{
    139               printf("Singular %s  %s  (%d)  %s %s\n",
    140                      S_VERSION1,S_VERSION2,
    141                      SINGULAR_VERSION_ID,__DATE__,__TIME__);
    142               printf("with\n");
    143               printf(versionString());
    144               printf("\n\n");
    145               break;
    146             }
    147             case 'e': if ((argv[1][i+1]>'0') && (argv[1][i+1]<='9'))
    148             {
    149               i++;
    150               si_echo = (int)(argv[1][i] - '0');
    151             }
    152             else si_echo = 1;
    153             break;
    154             case 'r': siRandomStart = 0;
    155               while((argv[1][i+1]>='0') && (argv[1][i+1]<='9'))
    156               {
    157                 i++;
    158                 siRandomStart = siRandomStart*10+(int)(argv[1][i] - '0');
    159               }
    160               #ifdef buildin_rand
    161                 siSeed=siRandomStart;
    162               #else
    163                 srand((unsigned int)siRandomStart);
    164               #endif
    165               #ifdef HAVE_FACTORY
    166                 factoryseed(siRandomStart);
    167               #endif
    168               break;
    169 #ifdef HAVE_TCL       
    170             case 'x': tclmode=TRUE;
    171               break;
    172 #endif       
    173 #ifdef HAVE_MPSR
    174             case 'b': feBatch=TRUE;
    175 #endif
    176             case 'q': verbose &= ~(Sy_bit(0)|Sy_bit(V_LOAD_LIB));
    177               break;
    178             case 't':
    179 #if defined(HAVE_FEREAD) || defined(HAVE_READLINE)
    180               fe_use_fgets=TRUE;
    181 #endif
    182               break;
    183             case 'n':
    184               load_std_lib=FALSE;
    185               break;
    186             default : printf("Unknown option -%c\n",argv[1][i]);
    187               printf("Usage: %s [-bemqtvx] [file]\n",thisfile);
    188               exit(1);
    189         }
    190       }
    191     }
    192   }
    193 
    194 
     465  }
    195466  /*. say hello */
    196   if (BVERBOSE(0))
     467  if (BVERBOSE(0) && !feBatch)
    197468  {
    198469    printf(
     
    207478    printf("%s  %s  (%d)",S_VERSION1,S_VERSION2, SINGULAR_VERSION_ID);
    208479    printf("\n\nPlease note:  EVERY COMMAND MUST END WITH A SEMICOLON \";"
    209            "\"\n(e.g. help; help command; help General syntax; help ring; quit;)\n\n");
     480           "\"\n(e.g., help; help command; help General syntax; help ring; quit;)\n\n");
    210481  }
    211482  else
     
    232503#endif
    233504  setjmp(si_start_jmpbuf);
    234   /* if script is given */
    235   if ((argc > 1)&&(argv[1][0]!='-'))
    236   {
    237     /* read and run the Script */
    238     argc=1;
    239     newFile(argv[1]);
     505
     506  // Now, put things on the stack of stuff to do
     507
     508  // Last thing to do is to execute given scripts
     509  if (optind < argc)
     510  {
     511    int i = argc - 1;
     512    while (i >= optind)
     513    {
     514      newFile(argv[i]);
     515      i--;
     516    }
    240517  }
    241518  else
     
    243520    currentVoice=feInitStdin();
    244521  }
    245   // set up voice for .singularc
    246   {
     522
     523  // before scripts, we execute -c, if it was given
     524  // now execute -c, if it was given
     525  if (execute_string != NULL)
     526    newBuffer(mstrdup(execute_string), BT_execute);
     527
     528  // first thing, however, is to load .singularrc
     529  if (load_rc)
     530  {
     531    // Hmm, we should look into $cwd and then into $HOME, only
    247532    char * where=(char *)AllocL(256);
    248533    FILE * rc=feFopen(".singularrc","r",where,FALSE);
     
    254539    FreeL((ADDRESS)where);
    255540  }
     541
    256542  /* start shell */
    257543  if (feBatch)
    258544  {
    259545#ifdef HAVE_MPSR
    260     extern int Batch_do(int argc, char **argv);
    261     return Batch_do(argc,argv);
     546    extern int Batch_do(const char* port, const char* host);
     547    return Batch_do(mainGetSingOptionValue(LON_MP_PORT),
     548                    mainGetSingOptionValue(LON_MP_HOST));
    262549#else
    263     fprintf(stderr, "Option -b not supported in this version");
    264     return 1;
    265 #endif // HAVE_MPSR
     550    assume(0);
     551#endif
    266552  }
    267553  setjmp(si_start_jmpbuf);
Note: See TracChangeset for help on using the changeset viewer.