callrexx1.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2014 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /*********************************************************************/
39 /* */
40 /* File Name: CALLREXX1.C */
41 /* */
42 /* ----------------------------------------------------------------- */
43 /* */
44 /* Description: Samples of how to invoke the Open Object Rexx*/
45 /* interpreter. */
46 /* */
47 /* Entry Points: main - main entry point */
48 /* */
49 /* Input: None */
50 /* */
51 /* Output: returns from the Open Object Rexx programs */
52 /* */
53 /*********************************************************************/
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <sys/stat.h>
59 #include <errno.h>
60 
61 #include "rexx.h"
62 
63 char *pcharTemp;
64 
65 int main()
66 {
67  CONSTRXSTRING arg[4]; /* argument string for Rexx */
68  RXSTRING rexxretval; /* return value from Rexx */
69  RXSTRING instore[2]; /* in storage parms */
70  char *pszTemp;
71  RexxReturnCode rc = 0; /* return code from Rexx */
72  short rexxrc = 0; /* return code from function */
73 
74  char val;
75  const char *str1 = "Arg number one"; /* text to swap */
76  const char *str2 = "Arg number two"; /* text to swap */
77  const char *str3 = "Arg number three"; /* text to swap */
78  const char *str4 = "Arg number four"; /* text to swap */
79  const char *sync_tst = "call time 'Reset';" \
80  "object1 = .example~new;" \
81  "object2 = .example~new;" \
82  "object3 = .example~new;" \
83  "a.1 = object1~start('REPEAT', 4 , 'Object 1 running');" \
84  "say a.1~result;" \
85  "say 'The result method waits until the START message has completed:';" \
86  "a.2 = object2~start('REPEAT', 2, 'Object 2 running');" \
87  "a.3 = object3~start('REPEAT', 2, 'Object 3 running');" \
88  "say a.2~result;" \
89  "say a.3~result;" \
90  "say 'main ended';" \
91  "say 'Elapsed time: ' time('E');" \
92  "exit;" \
93  "::REQUIRES 'example.rex'";
94 
95  /* By setting the strlength of the Rexx output to zero, the */
96  /* interpreter allocates memory. */
97  /* We can provide a buffer for the interpreter to use instead. */
98  /* If the returned value does not fit into the buffer, Open Object Rexx */
99  /* creates a new one. */
100 
101  system("clear");
102 
103  rexxretval.strptr = NULL; /* initialize return-pointer to empty */
104  rexxretval.strlength = 0; /* initialize return-length to zero */
105 
106  printf("This is an easy sample of how to invoke the Rexx interpreter. \n");
107  printf("The Rexx commandfile which is started is named: startrx1.rex\n");
108 
109  printf("Press Enter to continue\n");
110  scanf("%c", &val);
111 
112 
113  /* This is the interpreter invocation. ---------------------------- */
114 
115  rc=RexxStart(
116  0, /* number of arguments */
117  NULL, /* array of arguments */
118  "startrx1.rex", /* name of Rexx file */
119  NULL, /* No INSTORE used */
120  "ksh", /* Command env. name */
121  RXCOMMAND, /* Code for how invoked */
122  NULL, /* No EXITs on this call */
123  &rexxrc, /* Rexx program output */
124  &rexxretval ); /* Rexx program output */
125 
126  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
127  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
128  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
129 
130  RexxFreeMemory(rexxretval.strptr);
131 
132  printf("Press Enter to continue\n");
133  scanf("%c", &val);
134 
135  system("clear");
136 
137  printf("In this case a previously defined Resultstring is \n");
138  printf("delivered to Open Object Rexx, which is large enough to \n");
139  printf("hold the Return Value of the Rexx commandfile\n");
140 
141  printf("Press Enter to continue\n");
142  scanf("%c", &val);
143 
144  rexxretval.strptr = (char *) malloc(100 * sizeof(char));
145  rexxretval.strlength = 100;
146 
147  rc=RexxStart(
148  0, /* number of arguments */
149  NULL, /* array of arguments */
150  "startrx1.rex", /* name of Rexx file */
151  NULL, /* No INSTORE used */
152  "ksh", /* Command env. name */
153  RXCOMMAND, /* Code for how invoked */
154  NULL, /* No EXITs on this call */
155  &rexxrc, /* Rexx program output */
156  &rexxretval ); /* Rexx program output */
157 
158  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
159  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
160  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
161 
162  RexxFreeMemory(rexxretval.strptr);
163 
164  printf("Press Enter to continue\n");
165  scanf("%c", &val);
166 
167  system("clear");
168 
169  printf("In this case a previously defined Resultstring is \n");
170  printf("delivered to Open Object Rexx, which is too small to\n");
171  printf("hold the Return Value of the Rexx commandfile.\n");
172  printf("Rexx reallocates the buffer which needs to be freed\n");
173  printf("in the calling program\n");
174 
175  printf("Press Enter to continue\n");
176  scanf("%c", &val);
177 
178  rexxretval.strptr = (char *) malloc(2 * sizeof(char));
179  pszTemp = rexxretval.strptr;
180  rexxretval.strlength = 2;
181 
182  printf("The Length of the Resultstring is %d\n", rexxretval.strlength);
183 
184  rc=RexxStart(
185  0, /* number of arguments */
186  NULL, /* array of arguments */
187  "startrx1.rex", /* name of Rexx file */
188  NULL, /* No INSTORE used */
189  "ksh", /* Command env. name */
190  RXCOMMAND, /* Code for how invoked */
191  NULL, /* No EXITs on this call */
192  &rexxrc, /* Rexx program output */
193  &rexxretval ); /* Rexx program output */
194 
195  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
196  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
197  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
198 
199  RexxFreeMemory(rexxretval.strptr);
200 
201  printf("Press Enter to continue\n");
202  scanf("%c", &val);
203 
204  system("clear");
205 
206  rexxretval.strptr = NULL; /* initialize return-pointer to empty */
207  rexxretval.strlength = 0; /* initialize return-length to zero */
208 
209  printf("This is a sample with 4 arguments delivered to \n");
210  printf("REXXSTART\n");
211  printf("The Rexx commandfile which is started is named: startrx2.rex\n");
212 
213  printf("Press Enter to continue\n");
214  scanf("%c", &val);
215 
216  MAKERXSTRING(arg[0], str1, strlen(str1)); /* create input argument 1*/
217  MAKERXSTRING(arg[1], str2, strlen(str2)); /* create input argument 2*/
218  MAKERXSTRING(arg[2], str3, strlen(str3)); /* create input argument 3*/
219  MAKERXSTRING(arg[3], str4, strlen(str4)); /* create input argument 4*/
220 
221  rc=RexxStart(
222  4, /* number of arguments */
223  arg, /* array of arguments */
224  "startrx2.rex", /* name of Rexx file */
225  NULL, /* No INSTORE used */
226  "ksh", /* Command env. name */
227  RXCOMMAND, /* Code for how invoked */
228  NULL, /* No EXITs on this call */
229  &rexxrc, /* Rexx program output */
230  &rexxretval ); /* Rexx program output */
231 
232  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
233  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
234  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
235 
236  RexxFreeMemory(rexxretval.strptr);
237 
238  printf("Press Enter to continue\n");
239  scanf("%c", &val);
240 
241  system("clear");
242 
243  printf("This is a sample with 2 arguments delivered to \n");
244  printf("REXXSTART\n");
245  printf("The Rexx commandfile which is started is named: startrx2.rex\n");
246 
247  printf("Press Enter to continue\n");
248  scanf("%c", &val);
249 
250  rexxretval.strptr = NULL; /* initialize return-pointer to empty */
251  rexxretval.strlength = 0; /* initialize return-length to zero */
252 
253  rc=RexxStart(
254  2, /* number of arguments */
255  arg, /* array of arguments */
256  "startrx2.rex", /* name of Rexx file */
257  NULL, /* No INSTORE used */
258  "ksh", /* Command env. name */
259  RXCOMMAND, /* Code for how invoked */
260  NULL, /* No EXITs on this call */
261  &rexxrc, /* Rexx program output */
262  &rexxretval ); /* Rexx program output */
263 
264  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
265  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
266  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
267 
268  RexxFreeMemory(rexxretval.strptr);
269 
270  printf("Press Enter to continue\n");
271  scanf("%c", &val);
272 
273  system("clear");
274 
275  printf("This is a sample where the directory listing of the \n");
276  printf("actual directory is returned by the Rexx program. The \n");
277  printf("returned ResultString is displayed\n");
278  printf("The Rexx commandfile which is started is named: startrx3.rex\n");
279 
280  printf("Press Enter to continue\n");
281  scanf("%c", &val);
282 
283  rexxretval.strptr = NULL; /* initialize return-pointer to empty */
284  rexxretval.strlength = 0; /* initialize return-length to zero */
285 
286  rc=RexxStart(
287  0, /* number of arguments */
288  NULL, /* array of arguments */
289  "startrx3.rex", /* name of Rexx file */
290  NULL, /* No INSTORE used */
291  "ksh", /* Command env. name */
292  RXCOMMAND, /* Code for how invoked */
293  NULL, /* No EXITs on this call */
294  &rexxrc, /* Rexx program output */
295  &rexxretval ); /* Rexx program output */
296 
297  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
298  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
299  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
300 
301  RexxFreeMemory(rexxretval.strptr);
302 
303  printf("Press Enter to continue\n");
304  scanf("%c", &val);
305 
306  system("clear");
307 
308  printf("This is a sample where the instore parameter [0] is \n");
309  printf("tested. Instore parameter [0] is loaded with \n");
310  printf("a small Open Object Rexx script showing the concurrency feature.\n");
311 
312  printf("Press Enter to continue\n");
313  scanf("%c", &val);
314 
315  instore[0].strptr = const_cast<char *>(sync_tst);
316  instore[0].strlength = strlen(instore[0].strptr);
317  instore[1].strptr = NULL;
318  instore[1].strlength = 0;
319 
320  rc=RexxStart(
321  0, /* number of arguments */
322  NULL, /* array of arguments */
323  NULL, /* name of Rexx file */
324  instore, /* No INSTORE used */
325  "ksh", /* Command env. name */
326  RXCOMMAND, /* Code for how invoked */
327  NULL, /* No EXITs on this call */
328  &rexxrc, /* Rexx program output */
329  &rexxretval ); /* Rexx program output */
330 
331  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
332  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
333  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
334 
335  RexxFreeMemory(rexxretval.strptr);
336 
337  printf("Press Enter to continue\n");
338  scanf("%c", &val);
339 
340  system("clear");
341 
342  printf("Now instore[1] is loaded with the content of instore[0]. \n");
343  printf("It can be used on subsequent calls. instore[0] is set to NULL \n");
344 
345  printf("Press Enter to continue\n");
346  scanf("%c", &val);
347 
348  instore[0].strptr = NULL;
349  instore[0].strlength = 0;
350 
351  rc=RexxStart(
352  0, /* number of arguments */
353  NULL, /* array of arguments */
354  NULL, /* name of Rexx file */
355  instore, /* No INSTORE used */
356  "ksh", /* Command env. name */
357  RXCOMMAND, /* Code for how invoked */
358  NULL, /* No EXITs on this call */
359  &rexxrc, /* Rexx program output */
360  &rexxretval ); /* Rexx program output */
361 
362  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
363  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
364  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
365 
366  RexxFreeMemory(rexxretval.strptr);
367  RexxFreeMemory(instore[1].strptr);
368 
369  printf("Press Enter to continue\n");
370  scanf("%c", &val);
371 
372  system("clear");
373 
374  printf("This is a sample to show how to use the Rexx MacroSpace facility. \n");
375  printf("First of all load_macro.rex is called to load \n");
376  printf("the Rexx script macros.rex into Macrospace. The Macrospace- \n");
377  printf("name is upload.rex. \n");
378 
379  printf("Press Enter to continue\n");
380  scanf("%c", &val);
381 
382  rexxretval.strptr = NULL; /* initialize return-pointer to empty */
383  rexxretval.strlength = 0; /* initialize return-length to zero */
384 
385  rc=RexxStart(
386  0, /* number of arguments */
387  NULL, /* array of arguments */
388  "load_macro.rex", /* name of Rexx file */
389  NULL, /* No INSTORE used */
390  "ksh", /* Command env. name */
391  RXCOMMAND, /* Code for how invoked */
392  NULL, /* No EXITs on this call */
393  &rexxrc, /* Rexx program output */
394  &rexxretval ); /* Rexx program output */
395 
396  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
397  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
398  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
399 
400  RexxFreeMemory(rexxretval.strptr);
401 
402  printf("Press Enter to continue\n");
403  scanf("%c", &val);
404 
405  system("clear");
406 
407  printf("Now the Open Object Rexx script macros.rex (named upload.rex) has been loaded\n");
408  printf("into Macrospace. It is now used in the name option of\n");
409  printf("the REXXSTART command. \n");
410  printf("It is very important that instore paramenter [0] and [1] are\n");
411  printf("initialized to NULL rsp. 0 and used as REXXSTART parameters\n");
412 
413  printf("Press Enter to continue\n");
414  scanf("%c", &val);
415 
416  rexxretval.strptr = NULL; /* initialize return-pointer to empty */
417  rexxretval.strlength = 0; /* initialize return-length to zero */
418 
419  instore[1].strptr = NULL;
420  instore[1].strlength = 0;
421  instore[0].strptr = NULL;
422  instore[0].strlength = 0;
423 
424  rc=RexxStart(
425  0, /* number of arguments */
426  NULL, /* array of arguments */
427  "upload.rex", /* name of Rexx file */
428  instore, /* No INSTORE used */
429  "ksh", /* Command env. name */
430  RXCOMMAND, /* Code for how invoked */
431  NULL, /* No EXITs on this call */
432  &rexxrc, /* Rexx program output */
433  &rexxretval ); /* Rexx program output */
434 
435  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
436  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
437  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
438 
439  RexxFreeMemory(rexxretval.strptr);
440 
441  printf("Press Enter to continue\n");
442  scanf("%c", &val);
443 
444  system("clear");
445 
446  printf("Finally del_macro.rex is called to delete macros.rex (named upload.rex)\n");
447  printf("out of the Open Object Rexx Macrospace.\n");
448 
449  printf("Press Enter to continue\n");
450  scanf("%c", &val);
451 
452  rc=RexxStart(
453  0, /* number of arguments */
454  NULL, /* array of arguments */
455  "del_macro.rex", /* name of Rexx file */
456  NULL, /* No INSTORE used */
457  "ksh", /* Command env. name */
458  RXCOMMAND, /* Code for how invoked */
459  NULL, /* No EXITs on this call */
460  &rexxrc, /* Rexx program output */
461  &rexxretval ); /* Rexx program output */
462 
463  printf("CALLREXX1 - Back from REXXSTART: Return Code: %d\n", rc);
464  printf("CALLREXX1 - RESULT-LENGTH: %d\n", rexxretval.strlength);
465  printf("CALLREXX1 - RESULT-Value: %s\n", rexxretval.strptr);
466 
467  RexxFreeMemory(rexxretval.strptr);
468 
469  printf("Press Enter to continue\n");
470  scanf("%c", &val);
471 
472  system("clear");
473 }
474 
int REXXENTRY RexxStart(size_t argcount, PCONSTRXSTRING arglist, const char *programname, PRXSTRING instore, const char *envname, int calltype, PRXSYSEXIT exits, short *retcode, PRXSTRING result)
char * pcharTemp
Definition: callrexx1.cpp:63
int main()
Definition: callrexx1.cpp:65
RexxReturnCode REXXENTRY RexxFreeMemory(void *)
#define MAKERXSTRING(r, p, l)
Definition: rexx.h:182
int RexxReturnCode
Definition: rexx.h:73
#define RXCOMMAND
Definition: rexxapidefs.h:64
size_t strlength
Definition: rexx.h:157
char * strptr
Definition: rexx.h:158