// GeneralCommands.cc // Kevin Karplus // 22 Nov 1995 // Commands for the distribution estimating program (and others) // that are very general. #include // for atoi #include // for strcat and strlen #include #include // for getpid for process id #include "Command/Command.h" #include "Filenames/Filenames.h" #include "Utilities/Random.h" #include "PrintInColumns/PrintInColumns.h" #include "Input/Input.h" #include "Globals.h" // for initial wall-clock and cpu times static int EstInclude(istream& in, Command* self, ostream&logfile) { char filename[200]; get_word(in, filename); gzifstream* subin=Filenames::open_input(filename); if (!subin) return 0; logfile << "# reading script from file " << filename << "\n" << flush; Command::read_script(*subin, logfile); delete subin; return 1; } static Command IncludeCommand("Include", EstInclude, "one filename as an argument.\n\ Reads the file as a script containing commands."); static int EstExit(istream& in, Command* self, ostream&logfile) { return 0; } static Command ExitCommand("Exit", EstExit, "exits from the program, ignoring all subsequent commands from the input"); static Command QuitCommand("Quit", EstExit, "exits from the program, ignoring all subsequent commands from the input"); static Command StopCommand("Stop", EstExit, "exits from the program, ignoring all subsequent commands from the input"); static int PrintTime(istream &in, Command* self, ostream&logfile) { logfile << "CPU_time= " << Globals::user_seconds() << " sec, elapsed time= " << Globals::elapsed_seconds() << " sec)\n"; return 1; } static Command TimeCommand("Time", PrintTime, "no arguments. \n\ Reports the run-time and wall-clock time since program started\n\ "); static int SetSeed(istream &in, Command* self, ostream&logfile) { char word[100]; get_word(in, word, '\n'); unsigned int seed; if (word[0]) { seed=atoi(word); } else { struct timeval wall_time; gettimeofday(&wall_time,0); pid_t pid = getpid(); seed = wall_time.tv_sec + wall_time.tv_usec+ pid; } logfile << "# Seed set to " << seed << "\n"; set_random(seed); return 1; } static Command SetSeedCommand("SetSeed", SetSeed, "optional argument.\n\ Sets the seed of the random number generator and reports it to logfile.\n\ If no seed is specified, then time of day and process id are used to\n\ set the seed.\n\ "); static int SetAlias(istream &in, Command* self, ostream&logfile) { char word[100]; get_word(in,word); char means[500]; get_word(in,means,'\n'); Command* c=Command::command(means); if (!c) { Command::remove_from_table(word); logfile << "# alias for " << word << " (if any) eliminated\n"; } else { // Command* added_alias = new Command(word, c); new Command(word, c); logfile << "# alias " << word << " to mean the same as " << c->name() << "\n"; } return 1; } static Command AliasCommand("Alias", SetAlias, "reads two words, the second of which should be an already understood\n\ keyword, and defines the first word to mean exactly the same as the\n\ second"); // CHANGE LOG: // 22 July 1997 Kevin Karplus // copied from estimate-dist, removing alphabet commands // 21 May 2004 Sol Katzman // Added time functions // 27 May 2004 Sol Katzman // added SetSeed command (copied from undertaker)