runRexxProgram.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 2008-2014 Rexx Language Association. All rights reserved. */
4 /* */
5 /* This program and the accompanying materials are made available under */
6 /* the terms of the Common Public License v1.0 which accompanies this */
7 /* distribution. A copy is also available at the following address: */
8 /* http://www.oorexx.org/license.html */
9 /* */
10 /* Redistribution and use in source and binary forms, with or */
11 /* without modification, are permitted provided that the following */
12 /* conditions are met: */
13 /* */
14 /* Redistributions of source code must retain the above copyright */
15 /* notice, this list of conditions and the following disclaimer. */
16 /* Redistributions in binary form must reproduce the above copyright */
17 /* notice, this list of conditions and the following disclaimer in */
18 /* the documentation and/or other materials provided with the distribution. */
19 /* */
20 /* Neither the name of Rexx Language Association nor the names */
21 /* of its contributors may be used to endorse or promote products */
22 /* derived from this software without specific prior written permission. */
23 /* */
24 /* THIS SOFTWARE IS PROVIDED BY THE COPYright HOLDERS AND CONTRIBUTORS */
25 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
26 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
27 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYright */
28 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
29 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
30 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
31 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
32 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
33 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
34 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
35 /* */
36 /*----------------------------------------------------------------------------*/
37 
38 /**
39  * A simple example that creates an instance of the interpreter and uses that
40  * instance to execute a Rexx program. The program name must be passed in the
41  * command line. For example:
42  *
43  * runRexxProgram HelloWorld.rex
44  * runRexxProgram backward.fnc
45  * runRexxProgram tooRecursiveTrapped.rex
46  * runRexxProgram tooRecursiveUnhandled.rex
47  *
48  * backward.fnc demonstrates how to pass arguments into the called program.
49  */
50 
51 #include "oorexxapi.h"
52 #include <stdio.h>
53 #include <string.h>
54 
55 #if defined(_WIN32)
56 #define _CDECL __cdecl
57 #else
58 #define _CDECL
59 #endif
60 
61 /* Prototypes for several simple helper functions that demonstrate usage of some
62  * of the native C++ APIs. The functions themselves are at the bottom of the
63  * file.
64  */
65 bool checkForCondition(RexxThreadContext *c, bool clear);
67 
68 int _CDECL main(int argc, char **argv)
69 {
70  char *programName = NULL;
71  if ( argc == 2 )
72  {
73  programName = argv[1];
74  }
75 
76  if ( programName == NULL || *programName == '\0' )
77  {
78  printf("You must pass in the name of a Rexx program on the command line.\n");
79  printf(" For example: %s crash.rex.\n", argv[0]);
80  return 9;
81  }
82 
83  // These are the arguments to RexxCreateInterpreter(). An array of any
84  // number of Rexx options can be passed in, but for this example we do not
85  // need any options. So, we use NULL.
86  RexxInstance *interpreter;
87  RexxThreadContext *threadContext;
88  RexxOption *options = NULL;
89 
90  if ( RexxCreateInterpreter(&interpreter, &threadContext, options) == 0 )
91  {
92  printf("Failed to create interpreter, aborting.\n");
93  exit(1);
94  }
95  printInterpreterVersion(interpreter);
96 
97  // If we want to pass arguments to the program we need to put them into an
98  // array of Rexx objects. We can pass null if there are no arguments.
99  RexxArrayObject args = NULL;
100 
101  if ( strcmp("backward.fnc", programName) == 0 )
102  {
103  RexxStringObject str = threadContext->String("These words will be swapped");
104  args = threadContext->ArrayOfOne(str);
105  }
106 
107  // Execute the program and get the result returned to us.
108  printf("Using interpreter to execute %s\n\n", programName);
109  RexxObjectPtr result = threadContext->CallProgram(programName, args);
110 
111  // During the program execution, a condition can be raised if there is an
112  // unexpected error. If an exception occurred and is pending,
113  // CheckCondtion() will return true. In this case we print out some
114  // information on the condition, otherwise we print out the return, if any,
115  // from the program.
116  if (threadContext->CheckCondition())
117  {
118  checkForCondition(threadContext, true);
119  }
120  else if (result != NULLOBJECT)
121  {
122  // Note that we use ObjectToStringValue(). That is guarenteed to return
123  // the ASCII-Z string representation of the object. If we passed in, say
124  // an .array object to the CString() function, we would get a crash.
125  printf("\nProgram result=%s\n\n", threadContext->ObjectToStringValue(result));
126  }
127 
128  // Now wait for the interpreter to terminate and we are done.
129  interpreter->Terminate();
130 
131  return 0;
132 }
133 
134 /**
135  * Below are several helper functions that demonstrate how to use some of the
136  * different C++ native APIs.
137  */
138 
139 /**
140  * Given an interpreter instance, prints out the interpreter version and
141  * language version. The documentation in the ooRexx programming guide explains
142  * the byte encoding of the version numbers.
143  */
145 {
146  wholenumber_t ver = interpreter->InterpreterVersion();
147  wholenumber_t lang = interpreter->LanguageLevel();
148  printf("Created interpreter instance version=%d.%d.%d language level=%d.%02d\n\n",
149  (ver & 0xff0000) >> 16, (ver & 0x00ff00) >> 8, ver & 0x0000ff, (lang & 0xff00) >> 8, lang & 0x00ff);
150 }
151 
152 
153 /**
154  * Given a condition object, extracts and returns as a whole number the subcode
155  * of the condition.
156  */
158 {
159  return (condition->code - (condition->rc * 1000));
160 }
161 
162 
163 /**
164  * Outputs the typical condition message. For example:
165  *
166  * 4 *-* say dt~number
167  * Error 97 running C:\work\qTest.rex line 4: Object method not found
168  * Error 97.1: Object "a DateTime" does not understand message "NUMBER"
169  *
170  * @param c The thread context we are operating in.
171  * @param condObj The condition information object. The object returned from
172  * the C++ API GetConditionInfo()
173  * @param condition The RexxCondition struct. The filled in struct from the
174  * C++ API DecodeConditionInfo().
175  *
176  * @assumes There is a condition and that condObj and condition are valid.
177  */
179 {
180  RexxObjectPtr list = c->SendMessage0(condObj, "TRACEBACK");
181  if ( list != NULLOBJECT )
182  {
183  RexxArrayObject a = (RexxArrayObject)c->SendMessage0(list, "ALLITEMS");
184  if ( a != NULLOBJECT )
185  {
186  size_t count = c->ArrayItems(a);
187  for ( size_t i = 1; i <= count; i++ )
188  {
189  RexxObjectPtr o = c->ArrayAt(a, i);
190  if ( o != NULLOBJECT )
191  {
192  printf("%s\n", c->ObjectToStringValue(o));
193  }
194  }
195  }
196  }
197  printf("Error %d running %s line %d: %s\n", condition->rc, c->CString(condition->program),
198  condition->position, c->CString(condition->errortext));
199 
200  printf("Error %d.%03d: %s\n", condition->rc, conditionSubCode(condition), c->CString(condition->message));
201 }
202 
203 
204 /**
205  * Given a thread context, checks for a raised condition, and prints out the
206  * standard condition message if there is a condition.
207  *
208  * @param c Thread context we are operating in.
209  * @param clear Whether to clear the condition or not.
210  *
211  * @return True if there was a condition, otherwise false.
212  */
214 {
215  if ( c->CheckCondition() )
216  {
217  RexxCondition condition;
218  RexxDirectoryObject condObj = c->GetConditionInfo();
219 
220  if ( condObj != NULLOBJECT )
221  {
222  c->DecodeConditionInfo(condObj, &condition);
223  standardConditionMsg(c, condObj, &condition);
224 
225  if ( clear )
226  {
227  c->ClearCondition();
228  }
229  return true;
230  }
231  }
232  return false;
233 }
RexxReturnCode RexxEntry RexxCreateInterpreter(RexxInstance **instance, RexxThreadContext **context, RexxOption *options)
struct _RexxStringObject * RexxStringObject
Definition: rexx.h:128
struct _RexxArrayObject * RexxArrayObject
Definition: rexx.h:130
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
#define NULLOBJECT
Definition: rexx.h:147
ssize_t wholenumber_t
Definition: rexx.h:230
struct _RexxDirectoryObject * RexxDirectoryObject
Definition: rexx.h:137
#define _CDECL
wholenumber_t conditionSubCode(RexxCondition *condition)
bool checkForCondition(RexxThreadContext *c, bool clear)
void standardConditionMsg(RexxThreadContext *c, RexxDirectoryObject condObj, RexxCondition *condition)
void printInterpreterVersion(RexxInstance *)
int _CDECL main(int argc, char **argv)
wholenumber_t rc
Definition: oorexxapi.h:425
wholenumber_t code
Definition: oorexxapi.h:424
size_t position
Definition: oorexxapi.h:426
RexxStringObject message
Definition: oorexxapi.h:428
RexxStringObject errortext
Definition: oorexxapi.h:429
RexxStringObject program
Definition: oorexxapi.h:430