Polaris: switches_util.cc Source File

switches_util.cc

Go to the documentation of this file.
00001 ///
00002 #include <stdlib.h>
00003 #include <fstream.h>
00004 #include <strstream.h>
00005 #include <limits.h>
00006 
00007 #include "../Collection/Database.h"
00008 #include "../Collection/KeyIterator.h"
00009 #include "../String.h"
00010 #include "../IntElem.h"
00011 #include "../macros.h"
00012 #include "../p-assert.h"
00013 
00014 #include "switches_util.h"
00015 
00016 Database<String, IntElem> switches;
00017 char polarisOutputFileName[PATH_MAX];
00018 
00019 extern "C" {
00020     int getopt(int argc, char *const *argv, const char *optstring);
00021 }
00022 
00023 int
00024 p_getsubopt( char **optionp, const char *const *tokens, char **valuep )
00025 {
00026     char *token;
00027     char *eq = 0;
00028     char *ch = *optionp; 
00029 
00030     token = *valuep = 0;
00031     if (ch == '\0') 
00032         return -1;
00033 
00034     /// ...  find the end of the option and the equal sign if possible.
00035 
00036     for ( ; *ch && *ch != ','; ++ch) {
00037         if (*ch == '=')
00038             eq = ch;
00039     }
00040 
00041     token = *optionp;
00042 
00043     if (eq != 0) {
00044         /// ...  if we have an '=' sign then record the argument and 
00045         /// ...  null terminate the token.
00046 
00047         *valuep = eq + 1;
00048         *eq = '\0';
00049     }
00050 
00051 
00052     if (*ch == '\0') 
00053         *optionp = ch;
00054     else {
00055         *ch = '\0';
00056         *optionp = ch + 1;
00057     }
00058 
00059     for (char **t = CASTAWAY(char **) tokens; *t != 0; ++t) {
00060         if (strcmp( *t, token ) == 0)
00061             return ( (CASTAWAY(char **) t) - (CASTAWAY(char **) tokens) );
00062     }
00063 
00064     return -1;
00065 }
00066 
00067 
00068 int 
00069 switch_value(char const *switch_name)
00070 {
00071 
00072     IntElem        *val = switches.find_ref(switch_name);
00073 
00074     if (!val) {
00075       cerr << "WARNING: switch " << switch_name << " does not exist\n";
00076       return 0;
00077     }
00078 
00079     return val->value();
00080 }
00081 
00082 void 
00083 define_switch(char const *switch_name, int switch_value)
00084 {
00085 
00086     String  swn = switch_name;
00087 
00088     switches.ins( swn, new IntElem(switch_value));
00089 }
00090 
00091 
00092 void 
00093 adjust_switch_value(char const *switch_name, int new_value)
00094 {
00095     switches.find_ref(switch_name)->value(new_value);
00096 }
00097 
00098 
00099 ///  parse_switches - parse the polaris command line
00100 ///
00101 ///  supported options:
00102 ///      -s <switch-name>=<value>     Assign the given switch the given value
00103 ///      -f <switch-file-name>        Use the given switch file for switch values
00104 ///      -v                           Report the current Polaris version
00105 ///      -i <intrinsic-def-filename>  Use the given file to define intrinsics
00106 ///                                   (over-rides the use of the default dummy_pgm.f)
00107 int 
00108 parse_switches(int argc, char *argv[], char const *version, 
00109            char **intrin_file_name)
00110 {
00111     *intrin_file_name = 0;
00112     polarisOutputFileName[0] = '\0';
00113 
00114     /// ...  read the POLARIS_SWITCHES database.
00115     read_switches();
00116 
00117     /// ...  Do the command line parsing.
00118     int             c;
00119     extern char    *optarg;
00120     extern int      optind;
00121 
00122     optind = 1;
00123     int             cmd_error = 0;
00124 
00125     typedef   char *char_p;
00126     char    **tokens = new char_p[ switches.entries()+2 ];
00127     int       position = 0;
00128 
00129     for (KeyIterator<String, IntElem> sw_iter = switches; 
00130                                       sw_iter.valid(); ++sw_iter) {
00131         tokens[ position++ ] 
00132             = CASTAWAY(char *) (const char *) sw_iter.current_key();
00133     }
00134     tokens[ position ] = 0;
00135 
00136     while ((c = getopt(argc, argv, "f:i:s:vo:")) != -1) {
00137         switch (c) {
00138         case 'f':
00139             {
00140                 char *file_name = optarg;
00141 
00142         read_switch_file( file_name );
00143             }
00144 
00145         break;
00146 
00147         case 'i':
00148             {
00149                 *intrin_file_name = optarg;
00150             }
00151 
00152         break;
00153 
00154     case 'o':
00155       strcpy(polarisOutputFileName, optarg);
00156       break;
00157 
00158         case 's':
00159             {
00160                 char *value, *options = optarg;
00161 
00162                 while (*options != '\0') {
00163                     char *last_option = options;
00164                     int index = p_getsubopt(&options, tokens, &value);
00165 
00166                     if (index == -1) {
00167                         cout << "unknown switch " << last_option << endl;
00168                         cmd_error++;
00169                     }
00170                     else {
00171                         int             new_value = 1;
00172 
00173                         if (value)
00174                             new_value = atoi(value);
00175 
00176                         adjust_switch_value(tokens[index], new_value);
00177                     }
00178                 }
00179             }
00180 
00181         break;
00182 
00183     case 'v':
00184         {
00185         cout << "Polaris version " << version << endl;
00186         }
00187 
00188         break;
00189         
00190         case '?':
00191             cmd_error++;
00192         }
00193 
00194         if (cmd_error) {
00195             cout << "Usage: " << argv[0] << endl;
00196             cout << " driver [-v] [-f switchfile] [-s switch1=value1,switch2=value2,...] source"
00197                  << endl;
00198             exit(1);
00199         }
00200     }
00201 
00202     delete [] tokens;
00203     return optind;
00204 }
00205 
00206 int
00207 read_switch_file( const char * file_name )
00208 {
00209   String    sw_file = file_name;
00210   ifstream        inp(sw_file);
00211 
00212     if (inp.fail()) {
00213         cout << "could not open switch file: " << sw_file << "\n";
00214         return -1;
00215     }
00216 
00217     char           *tokens[500], buffer[500];
00218     int             default_values[500];
00219 
00220     for (int i = 0; !inp.eof(); i++) {
00221 
00222         inp.getline(buffer, sizeof(buffer));
00223     if ((buffer[0] == '#') || (strlen(buffer) == 0)) {
00224         /// ...  Ignore comment and empty lines
00225     } else {
00226         istrstream str(buffer);
00227         tokens[i] = new char[1024];
00228 
00229         str >> tokens[i] >> default_values[i];
00230         if (strlen(tokens[i]) > 0) {  /// ...  Ignore whitespace lines
00231         define_switch(tokens[i], default_values[i]);
00232         }
00233         delete [] tokens[i];
00234     }
00235     }
00236 
00237     inp.close();
00238 
00239     return 0;
00240 }
00241 
00242 int 
00243 read_switches()
00244 {
00245     String          sw_file;
00246     char           *env_var = "POLARIS_SWITCHES";
00247     char           *ptr;
00248 
00249     if ((ptr = getenv( env_var )) != 0)
00250     sw_file = ptr;
00251     else {
00252     ptr = getenv( "POLARIS_ROOT" );
00253     p_assert(ptr, "Must 'setenv POLARIS_ROOT <Polaris root directory>'");
00254 
00255     sw_file = ptr;
00256     sw_file += "/switches";
00257     }
00258     return read_switch_file( sw_file );
00259 }
00260 
00261 void
00262 dump_switches( ostream &o )
00263 {
00264     const String  *id;
00265 
00266     int column = 0;
00267 
00268     for (id = switches.first_ref(); id; id = switches.successor_ref(*id)) {
00269         IntElem        *val = switches.find_ref( *id );
00270 
00271         if ((column++ % 3) == 0) {
00272             o << endl;
00273             o << "* ";
00274         }
00275         o << "    " 
00276       <<(const char *)*id 
00277       << "    "<<val->value();
00278 ///         o << "    " << form("%16s", (const char *)*id) 
00279 ///                     << form("%3d", val->value());
00280     }
00281 
00282     o << endl;
00283 }
00284 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:13 2005