Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

src/aclvis/Interp.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  * This program was prepared by PSI. 
00007  * All rights in the program are reserved by PSI.
00008  * Neither PSI nor the author(s)
00009  * makes any warranty, express or implied, or assumes any liability or
00010  * responsibility for the use of this software
00011  *
00012  * Visit http://www.acl.lanl.gov/POOMS for more details
00013  *
00014  ***************************************************************************/
00015 
00016 // -*- C++ -*-
00017 /***************************************************************************
00018  *
00019  * The IPPL Framework
00020  * 
00021  *
00022  * Visit http://people.web.psi.ch/adelmann/ for more details
00023  *
00024  ***************************************************************************/
00025 
00026 #ifdef IPPL_USE_STANDARD_HEADERS
00027 #include <iostream>
00028 #include <fstream>
00029 using namespace std;
00030 #else
00031 #include <iostream.h>
00032 #include <fstream.h>
00033 #endif
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <unistd.h>
00038 #include <sys/types.h>
00039 #include <sys/time.h>
00040 #include "aclvis/Interp.h"
00041 
00042 
00044 // constructor
00045 Interp::Interp(const char *nm) : InterpNamedObj(nm) { }
00046 
00047 
00049 // destructor
00050 Interp::~Interp() { }
00051 
00052 
00054 // Provide a prompt and input a command from the user.  Return only
00055 // when the command "go" is given.  If the timeout vlaue is < 0, this
00056 // routine will only return if the user types "go".  Otherwise, it will
00057 // return when a timeout occurs.
00058 void Interp::interact(int timeoutseconds) { 
00059   // prepare to read command string(s)
00060   char inbuf[2048];
00061 
00062 #ifndef IPPL_LINUX
00063   fd_set readvec;
00064   int stdin_fd = 0;
00065   struct timeval timeout;
00066   timeout.tv_sec = timeoutseconds;
00067   timeout.tv_usec = 0;
00068   FD_ZERO(&readvec);
00069   FD_SET(stdin_fd, &readvec);
00070 #endif
00071 
00072   // read in commands, until either EOF, end command, or timeout if
00073   // requested
00074   bool done = false;
00075   while (!done) {
00076     // print prompt
00077     //printf("%s", name());
00078     //fflush(stdout);
00079     cout << name() << flush;
00080 
00081     // check for command
00082 #ifndef IPPL_LINUX
00083     int ret = select(16, &readvec, 0, 0, timeoutseconds < 0 ? 0 : &timeout);
00084 #else
00085     int ret = 1;
00086 #endif
00087     if (ret == -1) {
00088       // there was an error
00089       cerr << "Interp: Error reading input." << endl;
00090       return;
00091     } else if (ret == 0) {
00092       // time out
00093       done = true;
00094     } else {
00095       // there is a command available to read in
00096       if (fgets(inbuf, 2047, stdin) == 0) {
00097         // end of file - no text available
00098         return;
00099       } else {
00100         // check if the user requests to exit interaction, or
00101         // execute the command
00102         cerr << "Read in string '" << inbuf << "'." << endl;
00103         if (!strcmp(inbuf, "go\n") || !strcmp(inbuf, "GO\n"))
00104           done = true;
00105         else 
00106           evalCommand(inbuf);
00107       }
00108     }
00109   }
00110 }
00111 
00112 /***************************************************************************
00113  * $RCSfile: Interp.cpp,v $   $Author: adelmann $
00114  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:34 $
00115  * IPPL_VERSION_ID: $Id: Interp.cpp,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $ 
00116  ***************************************************************************/

Generated on Fri Nov 2 01:25:53 2007 for IPPL by doxygen 1.3.5