~ubuntu-branches/ubuntu/quantal/psicode/quantal

« back to all changes in this revision

Viewing changes to src/bin/psirb/psirb.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck, Michael Banck, Daniel Leidert
  • Date: 2009-02-23 00:12:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223001202-rutldoy3dimfpesc
Tags: 3.4.0-1
* New upstream release.

[ Michael Banck ]
* debian/patches/01_DESTDIR.dpatch: Refreshed.
* debian/patches/02_FHS.dpatch: Removed, applied upstream.
* debian/patches/03_debian_docdir: Likewise.
* debian/patches/04_man.dpatch: Likewise.
* debian/patches/06_466828_fix_gcc_43_ftbfs.dpatch: Likewise.
* debian/patches/07_464867_move_executables: Fixed and refreshed.
* debian/patches/00list: Adjusted.
* debian/control: Improved description.
* debian/patches-held: Removed.
* debian/rules (install/psi3): Do not ship the ruby bindings for now.

[ Daniel Leidert ]
* debian/rules: Fix txtdir via DEB_MAKE_INSTALL_TARGET.
* debian/patches/01_DESTDIR.dpatch: Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*! \defgroup psirb psirb: PSI3 Ruby Input Driver */
 
2
 
 
3
/*! 
 
4
** \file
 
5
** \ingroup psirb
 
6
** \brief PSI3 Ruby driver module
 
7
*/
 
8
 
 
9
#include <getopt.h>
 
10
#include <cstdio>
 
11
#include <ruby.h>
 
12
#define MAIN
 
13
#include "psirb.h"
 
14
 
 
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();
 
22
 
 
23
// Functions defined here.
 
24
void parse_command_line(int, char**);
 
25
void redirect_output(char *szFilename, bool append=false);
 
26
void print_version();
 
27
void print_usage();
 
28
}}
 
29
 
 
30
// The following are completely ignored by psirb and psi
 
31
extern "C" {
 
32
        const char *gprgid() {
 
33
                const char *prog = "psirb";
 
34
                return prog;
 
35
        }
 
36
        char *psi_file_prefix;
 
37
};
 
38
 
 
39
int main(int argc, char *argv[])
 
40
{
 
41
        using namespace psi::psirb;
 
42
        
 
43
        // Parse the command line arguments
 
44
        parse_command_line(argc, argv);
 
45
        
 
46
        // Print the version of Psi first
 
47
        print_version();
 
48
        
 
49
        // Initialize the interpreter
 
50
        initialize_ruby();
 
51
        
 
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
 
57
        
 
58
        // Now we need to branch off. If the user did not specify -I or --irb then process the
 
59
        // input file.
 
60
        if (Globals::g_bIRB == false) {
 
61
                // Now have Ruby load in the input file and begin processing
 
62
                load_input_file_into_ruby();
 
63
                
 
64
                // Actually exist the input file.
 
65
                process_input_file();
 
66
        }
 
67
        // User requested irb (interactive Ruby) this requires other procedures.
 
68
        else {
 
69
                run_interactive_ruby();
 
70
        }
 
71
        
 
72
        // Close the interpreter
 
73
        finalize_ruby();
 
74
        
 
75
        return EXIT_SUCCESS;
 
76
}
 
77
 
 
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.
 
83
*/
 
84
void parse_command_line(int argc, char *argv[])
 
85
{
 
86
        int next_option;
 
87
        char *output_filename = NULL;
 
88
        
 
89
        // Set the default verbosity value
 
90
        Globals::g_bVerbose = false;
 
91
        
 
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' },
 
100
                { NULL,      0, NULL,  0  }
 
101
        };
 
102
        
 
103
        // Check the command line arguments
 
104
        do {
 
105
                next_option = getopt_long(argc, argv, short_options, long_options, NULL);
 
106
                
 
107
                switch (next_option) {                  
 
108
                        case 'h': // -h or --help
 
109
                        print_usage();
 
110
                        break;
 
111
                        
 
112
                        case 'I': // -I or --irb
 
113
                        Globals::g_bIRB = true;
 
114
                        break;
 
115
                        
 
116
                        case 'o': // -o or --output
 
117
                        output_filename = optarg;
 
118
                        break;
 
119
                        
 
120
                        case 'v': // -v or --verbose
 
121
                        Globals::g_bVerbose = true;
 
122
                        break;
 
123
                        
 
124
                        case 'V': // -V or --version
 
125
                        print_version();
 
126
                        break;
 
127
                        
 
128
                        case -1: // done with options
 
129
                        break;
 
130
                        
 
131
                        default: // Something else, unexpected
 
132
                        fprintf(stderr, "Unknown command line option encountered: %c\n", next_option);
 
133
                        exit(EXIT_FAILURE);
 
134
                        break;
 
135
                }
 
136
        } while (next_option != -1);
 
137
        
 
138
        // If the output needs to be redirected do so now.
 
139
        redirect_output(output_filename);
 
140
        if (output_filename)
 
141
                Globals::g_szOutputFilename = output_filename;
 
142
        
 
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";
 
147
        }
 
148
        else {
 
149
                Globals::g_szInputFile = argv[optind];
 
150
        }
 
151
}
 
152
 
 
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 
 
155
        in practice.
 
156
        \param szFilename File to redirect to.
 
157
        \param append Append to it?
 
158
*/
 
159
void redirect_output(char *szFilename, bool append)
 
160
{
 
161
        const char *szAppend;
 
162
        
 
163
        if (append == false)
 
164
                szAppend = "w+";
 
165
        else
 
166
                szAppend = "w";
 
167
                
 
168
        if (szFilename) {
 
169
                // Create the file, truncate if necessary
 
170
                Globals::g_fOutput = fopen(szFilename, szAppend);
 
171
                
 
172
                // Error?
 
173
                if (Globals::g_fOutput == NULL) {
 
174
                        fprintf(stderr, "Unable to open: %s\n", szFilename);
 
175
                        exit(EXIT_FAILURE);
 
176
                }
 
177
        }
 
178
        else {
 
179
                // Redirect to the screen
 
180
                Globals::g_fOutput = stdout;
 
181
        }
 
182
}
 
183
 
 
184
/*! Print PSI version information that is was set in configure.ac */
 
185
void print_version()
 
186
{
 
187
        fprintf(Globals::g_fOutput, "PSI %d.%d Ruby Driver\n", PSI_VERSION_MAJOR, PSI_VERSION_MINOR);
 
188
}
 
189
 
 
190
/*! Print command-line usage information. */
 
191
void print_usage()
 
192
{
 
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");
 
199
        
 
200
        exit(EXIT_FAILURE);
 
201
}
 
202
 
 
203
}} // namespace psi::psirb