tahoe/tahoe/src/main/ExecutionManagerT.cpp1 /* $Id: ExecutionManagerT.cpp,v 1.18 2004/09/28 15:35:37 paklein Exp $ */ 2 /* created: paklein (08/27/1997) */ 3 #include "ExecutionManagerT.h" 4 5 #include <iostream.h> 6 #include <iomanip.h> 7 #include <time.h> 8 9 #include "ifstreamT.h" 10 #include "ofstreamT.h" 11 #include "StringT.h" 12 #include "CommunicatorT.h" 13 14 using namespace Tahoe; 15 16 /* maximum batch file recursion depth */ 17 const int kMaxRecursionDepth = 10; 18 19 /* Constructor */ 20 ExecutionManagerT::ExecutionManagerT(int argc, char* argv[], char job_char, char batch_char, 21 CommunicatorT& comm, 22 int jobcharputback): 23 fJobChar(job_char), 24 fBatchChar(batch_char), 25 fComm(comm), 26 fJobCharPutBack(jobcharputback), 27 fRecursionDepth(0) 28 { 29 if (fJobCharPutBack != 1 && fJobCharPutBack != 0) throw ExceptionT::kBadInputValue; 30 31 /* store command line arguments */ 32 fCommandLineOptions.Dimension(argc); 33 for (int i = 0; i < fCommandLineOptions.Length(); i++) 34 fCommandLineOptions[i] = argv[i]; 35 36 /* format standard output */ 37 ofstreamT::format_stream(cout); 38 } 39 40 /* Destructor */ 41 ExecutionManagerT::~ExecutionManagerT(void) { } 42 43 /* Prompt input files until "quit" */ 44 void ExecutionManagerT::Run(void) 45 { 46 /* get rank */ 47 int rank = fComm.Rank(); 48 49 /* file name passed on command line */ 50 int index; 51 if (CommandLineOption("-f", index)) 52 { 53 /* path name */ 54 StringT& file = fCommandLineOptions[index+1]; 55 file.ToNativePathName(); 56 57 /* open stream */ 58 ifstreamT input('#', file); 59 if (!input.is_open()) 60 { 61 cout << "\n ExecutionManagerT::Run_parallel: unable to open file: \"" 62 << file << '\"' << endl; 63 throw ExceptionT::kBadInputValue; 64 } 65 66 /* dispatch */ 67 JobOrBatch(input, cout); 68 } 69 else 70 { 71 StringT lastfilename; 72 int count = 0; 73 while (count++ < 10) 74 { 75 /* broadcast file name or command-line option */ 76 StringT line(255); 77 if (rank == 0) 78 Prompt("Enter input file path or option (\"quit\" to exit)", lastfilename, line); 79 fComm.Broadcast(0, line); 80 81 /* command line option */ 82 if (line[0] == '-') { 83 84 /* reset count */ 85 count = 0; 86 87 /* add command line option */ 88 if (AddCommandLineOption(line)) 89 cout << " added command line option: \"" << line << '\"' << endl; 90 } 91 else if (strncmp(line, "quit", 4) == 0) 92 break; 93 else /* try to open file */ 94 { 95 line.ToNativePathName(); 96 ifstreamT input('#', line); 97 if (input.is_open()) { 98 99 /* reset count */ 100 count = 0; 101 102 /* keep last file name */ 103 lastfilename = input.filename(); 104 105 /* Recursive dispatch */ 106 JobOrBatch(input, cout); 107 } 108 else 109 cout << "\nError: filename: \"" << line << "\" not found\n"; 110 } 111 } 112 113 /* exit message */ 114 if (count >= 10) cout << "\nNo valid input after " << count << " attempts." << endl; 115 } 116 } 117 118 // returns true if the option was passed on the command line, moved to public DEF 3 Aug 04 119 bool ExecutionManagerT::CommandLineOption(const char* str, int& index) const 120 { 121 for (int i = 0; i < fCommandLineOptions.Length(); i++) 122 if (fCommandLineOptions[i] == str) 123 { 124 index = i; 125 return true; 126 } 127 128 /* dummy */ 129 index = 0; 130 return false; 131 } 132 133 134 /********************************************************************** 135 * Protected 136 **********************************************************************/ 137 138 bool ExecutionManagerT::AddCommandLineOption(const char* str) 139 { 140 /* only if not present */ 141 int index; 142 if (!CommandLineOption(str, index)) 143 { 144 /* append */ 145 fCommandLineOptions.Append(str); 146 return true; 147 } 148 else return false; 149 } 150 151 bool ExecutionManagerT::RemoveCommandLineOption(const char* str) 152 { 153 /* only if not present */ 154 int index; 155 if (CommandLineOption(str, index)) 156 { 157 /* append */ 158 fCommandLineOptions.DeleteAt(index); 159 return true; 160 } 161 else return false; 162 } 163 164 /********************************************************************** 165 * Private 166 **********************************************************************/ 167 168 /* Recursive dispatch */ 169 void ExecutionManagerT::JobOrBatch(ifstreamT& in, ostream& status) 170 { 171 /* get first char */ 172 char filetypechar; 173 in >> filetypechar; 174 175 if (filetypechar != fJobChar && filetypechar != fBatchChar) 176 { 177 status << "\n ExecutionManagerT::JobOrBatch: invalid filetype character: "; 178 status << filetypechar << '\n'; 179 return; 180 } 181 182 /* check recursion depth */ 183 if (++fRecursionDepth > kMaxRecursionDepth) throw ExceptionT::kGeneralFail; 184 185 /* JOB file */ 186 if (filetypechar == fJobChar) 187 { 188 /* putback first character */ 189 if (fJobCharPutBack) in.putback(filetypechar); 190 191 /* derived */ 192 RunJob(in, status); 193 } 194 /* BATCH file */ 195 else 196 { 197 /* process batch file */ 198 RunBatch(in, status); 199 } 200 201 /* reduce depth on exit */ 202 fRecursionDepth--; 203 } 204 205 /* Batch file processing */ 206 void ExecutionManagerT::RunBatch(ifstreamT& in, ostream& status) 207 { 208 /* mark status */ 209 status << "\n Processing batch file: " << in.filename() << '\n'; 210 211 /* start day/date info */ 212 time_t starttime; 213 time(&starttime); 214 215 /* get 1st entry */ 216 StringT nextinfilename; 217 in >> nextinfilename; 218 219 /* repeat to end of file */ 220 while (in.good()) 221 { 222 /* adjusting execution options */ 223 if (nextinfilename[0] == '-') 224 AddCommandLineOption(nextinfilename); 225 else /* execute regular file */ 226 { 227 /* file path format */ 228 nextinfilename.ToNativePathName(); 229 230 /* path to source file */ 231 StringT path; 232 path.FilePath(in.filename()); 233 234 /* open new input stream */ 235 nextinfilename.Prepend(path); 236 ifstreamT nextin('#', nextinfilename); 237 238 /* process if valid */ 239 if (nextin.is_open()) 240 JobOrBatch(nextin, cout); 241 else 242 cout << " File not found: " << nextinfilename << '\n'; 243 } 244 245 /* get next entry */ 246 in >> nextinfilename; 247 } 248 249 /* stop day/date info */ 250 time_t stoptime; 251 time(&stoptime); 252 cout << "\n Batch start time : " << ctime(&starttime); 253 cout << " Batch stop time : " << ctime(&stoptime); 254 } 255 256 void ExecutionManagerT::Prompt(const char* prompt, const char* default_input, StringT& line) const 257 { 258 cout << '\n' << prompt; 259 260 /* default */ 261 if (default_input != NULL && strlen(default_input) > 0) 262 { 263 cout << "\nEnter <RETURN> for \"" << default_input << "\": "; 264 #ifdef __SGI__ 265 cout.flush(); 266 #endif 267 268 /* new filename */ 269 char test = cin.peek(); 270 if (test != '\n') 271 { 272 /* take first word */ 273 cin >> line; 274 } 275 else 276 { 277 /* copy default */ 278 line = default_input; 279 } 280 } 281 else 282 { 283 cout << ": "; 284 #ifdef __SGI__ 285 cout.flush(); 286 #endif 287 cin >> line; 288 } 289 290 /* clear to end of line */ 291 fstreamT::ClearLine(cin); 292 } 293 This page was automatically generated by the LXR engine. Visit the LXR main site for more information. |