1
/*! \defgroup psirb psirb: PSI3 Ruby Input Driver */
6
** \brief PSI3 Ruby driver module
15
namespace psi { namespace psirb {
16
// Functions defined in ruby.c that are only needed here
17
extern bool initialize_ruby();
18
extern void load_input_file_into_ruby();
19
extern void process_input_file();
20
extern void finalize_ruby();
21
extern void run_interactive_ruby();
23
// Functions defined here.
24
void parse_command_line(int, char**);
25
void redirect_output(char *szFilename, bool append=false);
30
// The following are completely ignored by psirb and psi
32
const char *gprgid() {
33
const char *prog = "psirb";
36
char *psi_file_prefix;
39
int main(int argc, char *argv[])
41
using namespace psi::psirb;
43
// Parse the command line arguments
44
parse_command_line(argc, argv);
46
// Print the version of Psi first
49
// Initialize the interpreter
52
// At this point the following has happened:
53
// 1. Output is being directed to where the user wants
54
// 2. Input filename has been determined
55
// 3. Ruby has been initialized
56
// 4. So far, some basic commands have been registered with Ruby
58
// Now we need to branch off. If the user did not specify -I or --irb then process the
60
if (Globals::g_bIRB == false) {
61
// Now have Ruby load in the input file and begin processing
62
load_input_file_into_ruby();
64
// Actually exist the input file.
67
// User requested irb (interactive Ruby) this requires other procedures.
69
run_interactive_ruby();
72
// Close the interpreter
78
namespace psi { namespace psirb {
79
/*! Handles the command line arguments and stores them in global variables. In some cases
80
default globals variables are set.
81
\param argc Number of command-line arguments received.
82
\param argv Value of the command-line arguments.
84
void parse_command_line(int argc, char *argv[])
87
char *output_filename = NULL;
89
// Set the default verbosity value
90
Globals::g_bVerbose = false;
92
// A string listing of valid short option letters
93
const char* const short_options = "IhvVo:";
94
const struct option long_options[] = {
95
{ "irb", 0, NULL, 'I' },
96
{ "help", 0, NULL, 'h' },
97
{ "verbose", 0, NULL, 'v' },
98
{ "version", 0, NULL, 'V' },
99
{ "output", 1, NULL, 'o' },
103
// Check the command line arguments
105
next_option = getopt_long(argc, argv, short_options, long_options, NULL);
107
switch (next_option) {
108
case 'h': // -h or --help
112
case 'I': // -I or --irb
113
Globals::g_bIRB = true;
116
case 'o': // -o or --output
117
output_filename = optarg;
120
case 'v': // -v or --verbose
121
Globals::g_bVerbose = true;
124
case 'V': // -V or --version
128
case -1: // done with options
131
default: // Something else, unexpected
132
fprintf(stderr, "Unknown command line option encountered: %c\n", next_option);
136
} while (next_option != -1);
138
// If the output needs to be redirected do so now.
139
redirect_output(output_filename);
141
Globals::g_szOutputFilename = output_filename;
143
// Check the input file name now
144
// Default to input.rb if not given
145
if (optind == argc) {
146
Globals::g_szInputFile = "input.rb";
149
Globals::g_szInputFile = argv[optind];
153
/*! Redirects the screen output from the input file and this module to the file given
154
in the argument otherwise it is sent to the screen. This doesn't quite work right
156
\param szFilename File to redirect to.
157
\param append Append to it?
159
void redirect_output(char *szFilename, bool append)
161
const char *szAppend;
169
// Create the file, truncate if necessary
170
Globals::g_fOutput = fopen(szFilename, szAppend);
173
if (Globals::g_fOutput == NULL) {
174
fprintf(stderr, "Unable to open: %s\n", szFilename);
179
// Redirect to the screen
180
Globals::g_fOutput = stdout;
184
/*! Print PSI version information that is was set in configure.ac */
187
fprintf(Globals::g_fOutput, "PSI %d.%d Ruby Driver\n", PSI_VERSION_MAJOR, PSI_VERSION_MINOR);
190
/*! Print command-line usage information. */
193
printf("Usage: psirb [options] inputfile\n");
194
printf(" -h --help Display this usage information.\n");
195
printf(" -v --verbose Print alot of information.\n");
196
printf(" -V --version Print version information.\n");
197
printf(" -o --output filename Redirect output elsewhere.\n");
198
printf(" -I --irb Run psirb in interactive mode.\n");
203
}} // namespace psi::psirb