00001 /* 00002 * HeyParser.hh 00003 * 00004 * Copyright (c) 2003 The University of Utah and the Flux Group. 00005 * All rights reserved. 00006 * 00007 * This file is licensed under the terms of the GNU Public License. 00008 * See the file "license.terms" for restrictions on redistribution 00009 * of this file, and for a DISCLAIMER OF ALL WARRANTIES. 00010 */ 00011 00012 /** 00013 * @file HeyParser.hh 00014 * 00015 * Header file for the HeyParser class. 00016 */ 00017 00018 #ifndef _hey_parser_hh 00019 #define _hey_parser_hh 00020 00021 #include <stack> 00022 #include <iostream> 00023 00024 /** 00025 * Base class for exceptions thrown by the parser. 00026 * 00027 * @sa HeyParser 00028 */ 00029 class HeyParserException 00030 { 00031 00032 public: 00033 00034 /** 00035 * Construct a HeyParserException object with an optional descriptive 00036 * message. 00037 * 00038 * @param msg A short message describing the problem. 00039 */ 00040 HeyParserException(const char *msg = NULL) 00041 { 00042 this->hpe_Message = msg; 00043 }; 00044 00045 /** 00046 * @return The descriptive message passed into the constructor. 00047 */ 00048 const char *getMessage() 00049 { 00050 return this->hpe_Message; 00051 }; 00052 00053 /** 00054 * Output stream operator for HeyParserException objects. Currently just 00055 * prints out the message passed into the constructor. 00056 * 00057 * @param os The destination output stream. 00058 * @param hpe The object to output. 00059 * @return The value of the 'os' parameter. 00060 */ 00061 friend ostream &operator<<(ostream &os, const HeyParserException &hpe) 00062 { 00063 return os << (hpe.hpe_Message != NULL ? hpe.hpe_Message : ""); 00064 }; 00065 00066 private: 00067 00068 /** 00069 * A short message describing the problem. 00070 */ 00071 const char *hpe_Message; 00072 00073 }; 00074 00075 /** 00076 * A parser for command-line arguments given in the pseudo-english "hey" form: 00077 * 00078 * @li hey @<server@> list 00079 * @li hey @<server@> get @<attribute@> of @<object@> 00080 * @li hey @<server@> set @<attribute@> of @<object@> to @<value@> 00081 * 00082 * @see hey_manager.cc 00083 * @see hey_strict_policy.cc 00084 */ 00085 class HeyParser 00086 { 00087 00088 public: 00089 00090 /** 00091 * An enumeration of the possible actions to perform on an object. 00092 */ 00093 typedef enum { 00094 LIST_PROPERTIES, /**< List the properties of an object. */ 00095 GET_PROPERTY, /**< Get a property from an object. */ 00096 SET_PROPERTY, /**< Set a property in an object. */ 00097 } hp_what_t; 00098 00099 /** 00100 * Construct a parser that interprets the given command-line arguments. 00101 * Most of the parsing will be done here so there are fewer surprises along 00102 * the way. 00103 * 00104 * @param argc The argument count. 00105 * @param argv The argument values. 00106 * 00107 * @throws HeyParserException if there is a problem with the argument list. 00108 */ 00109 HeyParser(int argc, const char *argv[]) 00110 throw (HeyParserException); 00111 00112 /** 00113 * Deconstruct a parser object. 00114 */ 00115 virtual ~HeyParser(); 00116 00117 /** 00118 * @return The argument that specifies which server to talk to. 00119 */ 00120 const char *who(void); 00121 00122 /** 00123 * @return The action requested by the user (i.e. what to do). 00124 */ 00125 hp_what_t what(void); 00126 00127 /** 00128 * Pop a pair from the property stack. 00129 * 00130 * @param name_out The reference where the property name should be stored. 00131 * @param value_out The reference where the property value should be 00132 * stored. 00133 * 00134 * @throws HeyParserException if there are no more values on the stack. 00135 */ 00136 void popProperty(const char *&name_out, const char *&value_out) 00137 throw (HeyParserException); 00138 00139 /** 00140 * @return The value that a property should be set to. 00141 * 00142 * @throws HeyParserException if "to" was not given on the command line. 00143 */ 00144 const char *to(void) 00145 throw (HeyParserException); 00146 00147 private: 00148 00149 /** 00150 * Helper class that stores name/value pairs. 00151 */ 00152 class Pair 00153 { 00154 00155 public: 00156 00157 /** 00158 * Construct an empty Pair object. 00159 */ 00160 Pair() 00161 { 00162 this->p_Name = NULL; 00163 this->p_Value = NULL; 00164 }; 00165 00166 /** 00167 * Construct a Pair object with the given name/value pair. 00168 * 00169 * @param name The name. 00170 * @param value The value. 00171 */ 00172 Pair(const char *name, const char *value) : 00173 p_Name(name), p_Value(value) 00174 { 00175 }; 00176 00177 /** 00178 * The name of the property. 00179 */ 00180 const char *p_Name; 00181 00182 /** 00183 * The property value. 00184 */ 00185 const char *p_Value; 00186 }; 00187 00188 /** 00189 * Consume an argument from the argument list. This method is for internal 00190 * use when initially processing the argument list. 00191 * 00192 * @return The next string in the argument list. 00193 * 00194 * @throws HeyParserException if there are no more arguments to be 00195 * consumed. 00196 */ 00197 const char *consumeArg(void) 00198 throw (HeyParserException); 00199 00200 /** 00201 * Find the next name/value pair in the argument list. This method differs 00202 * from popProperty() in that it translates from the argument list to 00203 * hp_PropertyStack. Whereas popProperty() interacts only with 00204 * hp_PropertyStack. 00205 * 00206 * @param name_out The reference where the property name should be stored. 00207 * @param value_out The reference where the property value should be 00208 * stored. 00209 * 00210 * @throws HeyParserException if there are insufficient arguments. 00211 */ 00212 void nextProperty(const char *&name_out, const char *&value_out) 00213 throw (HeyParserException); 00214 00215 /** 00216 * The arguments count. 00217 */ 00218 int hp_ArgCount; 00219 00220 /** 00221 * The argument values. 00222 */ 00223 const char **hp_ArgValue; 00224 00225 /** 00226 * The "what" value. This will only be set by the constructor when it is 00227 * initially parsing the arguments. 00228 */ 00229 hp_what_t hp_What; 00230 00231 /** 00232 * The current index in the argument list during processing. 00233 */ 00234 int hp_ArgIndex; 00235 00236 /** 00237 * The stack of property pairs. 00238 */ 00239 stack<struct Pair> hp_PropertyStack; 00240 00241 }; 00242 00243 #endif