rexxexit.c
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: REXXEXIT.C */
41 /* */
42 /* Description: Provides a sample call to the REXX */
43 /* interpreter, passing in an environment name, */
44 /* a file name, and a single argument string. */
45 /* */
46 /* Entry Points: main - main entry point */
47 /* */
48 /* Input: None */
49 /* */
50 /* Output: returns 0 in all cases. */
51 /* */
52 /*********************************************************************/
53 #include <windows.h>
54 #include <rexx.h> /* needed for RexxStart() */
55 #include <stdio.h> /* needed for printf() */
56 #include <string.h> /* needed for strlen() */
57 
58 //
59 // Prototypes
60 //
61 int __cdecl main(int argc, char *argv[]); /* main entry point */
62 RexxReturnCode REXXENTRY MY_IOEXIT( int ExitNumber, int Subfunction, PEXIT ParmBlock);
63 
64 //
65 // MAIN program
66 //
67 int __cdecl main(int argc, char *argv[])
68 {
69  RXSYSEXIT exit_list[9]; /* Exit list array */
70  short rexxrc = 0; /* return code from rexx */
71  int rc; /* actually running program RC */
72  CONSTRXSTRING argument; /* rexxstart argument */
73  RXSTRING rxretbuf; // program return buffer
74 
75  rc = 0; /* set default return */
76 
77  /* just one argument is accepted by this program */
78  if ((argc < 2) || (argc > 3))
79  {
80  printf("Wrong arguments: REXXEXIT program [argument]\n");
81  exit(-1);
82  }
83 
84  /*
85  * Convert the input array into a single string for the Object REXX
86  * argument string. Initialize the RXSTRING variable to point to this
87  * string. Keep the string null terminated so we can print it for debug.
88  * First argument is name of the REXX program
89  * Next argument(s) are parameters to be passed
90  */
91 
92  /* By setting the strlength of the output RXSTRING to zero, we */
93  /* force the interpreter to allocate memory and return it to us. */
94  /* We could provide a buffer for the interpreter to use instead. */
95  rxretbuf.strlength = 0L; /* initialize return to empty*/
96 
97  if (argc == 3)
98  {
99  MAKERXSTRING(argument, argv[2], strlen(argv[2]));/* create input argument */
100  }
101  else
102  MAKERXSTRING(argument, "", 0);/* create blank argument */
103 
104  // register IO exit
105  rc = RexxRegisterExitExe("MY_IOC", (REXXPFN)&MY_IOEXIT, NULL);
106 
107  /* run this via RexxStart */
108  exit_list[0].sysexit_name = "MY_IOC";
109  exit_list[0].sysexit_code = RXSIO;
110  exit_list[1].sysexit_code = RXENDLST;
111 
112  /* Here we call the interpreter. */
113  rc=REXXSTART(1, /* number of arguments */
114  &argument, /* array of arguments */
115  argv[1], /* name of REXX file */
116  NULL, /* No INSTORE used */
117  "CMD", /* Command env. name */
118  RXCOMMAND, /* Code for how invoked */
119  exit_list, /* exits for this call */
120  &rexxrc, /* Rexx program output */
121  &rxretbuf ); /* Rexx program output */
122 
123  /* free memory allocated for the return result */
124  if (rc==0)
125  {
126  RexxFreeMemory(rxretbuf.strptr);
127  }
128  RexxDeregisterExit("MY_IOC",NULL); // remove the exit in exe exit list
129  // return interpeter or rexx program return code
130  return rc ? rc : rexxrc;
131 }
132 
133 
134 RexxReturnCode REXXENTRY MY_IOEXIT(int ExitNumber, int Subfunction, PEXIT parmblock)
135 {
136  RXSIOSAY_PARM *sparm;
137  RXSIOTRC_PARM *tparm;
138  RXSIOTRD_PARM *rparm;
139  RXSIODTR_PARM *dparm;
140 
141  switch (Subfunction)
142  {
143  case RXSIOSAY:
144  sparm = ( RXSIOSAY_PARM * )parmblock ;
145  printf("%s\n",sparm->rxsio_string.strptr);
146  break;
147  case RXSIOTRC:
148  tparm = ( RXSIOTRC_PARM * )parmblock ;
149  printf("%s\n",tparm->rxsio_string.strptr);
150  break;
151  case RXSIOTRD:
152  rparm = (RXSIOTRD_PARM * )parmblock ;
153  gets_s(rparm->rxsiotrd_retc.strptr, rparm->rxsiotrd_retc.strlength);
154  rparm->rxsiotrd_retc.strlength=strlen(rparm->rxsiotrd_retc.strptr);
155  break;
156  case RXSIODTR:
157  dparm = (RXSIODTR_PARM * )parmblock ;
158  gets_s(dparm->rxsiodtr_retc.strptr, dparm->rxsiodtr_retc.strlength);
159  dparm->rxsiodtr_retc.strlength=strlen(dparm->rxsiodtr_retc.strptr);
160  break;
161  default:
162  break;
163  }
164 
165  return RXEXIT_HANDLED;
166 }
167 
168 
RexxReturnCode REXXENTRY RexxFreeMemory(void *)
RexxReturnCode REXXENTRY RexxDeregisterExit(CONSTANT_STRING, CONSTANT_STRING)
#define REXXSTART
Definition: rexx.h:465
RexxReturnCode REXXENTRY RexxRegisterExitExe(CONSTANT_STRING, REXXPFN, CONSTANT_STRING)
#define MAKERXSTRING(r, p, l)
Definition: rexx.h:182
char * PEXIT
Definition: rexx.h:215
int RexxReturnCode
Definition: rexx.h:73
#define RXCOMMAND
Definition: rexxapidefs.h:64
#define RXENDLST
Definition: rexxapidefs.h:170
#define RXSIOSAY
Definition: rexxapidefs.h:181
#define RXSIO
Definition: rexxapidefs.h:180
#define RXSIOTRC
Definition: rexxapidefs.h:182
#define RXSIOTRD
Definition: rexxapidefs.h:183
#define RXSIODTR
Definition: rexxapidefs.h:184
#define RXEXIT_HANDLED
Definition: rexxapidefs.h:145
int __cdecl main(int argc, char *argv[])
Definition: rexxexit.c:67
RexxReturnCode REXXENTRY MY_IOEXIT(int ExitNumber, int Subfunction, PEXIT ParmBlock)
Definition: rexxexit.c:134
RXSTRING rxsiodtr_retc
Definition: rexx.h:767
CONSTANT_RXSTRING rxsio_string
Definition: rexx.h:746
CONSTANT_RXSTRING rxsio_string
Definition: rexx.h:753
RXSTRING rxsiotrd_retc
Definition: rexx.h:760
size_t strlength
Definition: rexx.h:157
char * strptr
Definition: rexx.h:158
CONSTANT_STRING sysexit_name
Definition: rexx.h:191
int sysexit_code
Definition: rexx.h:192
void * REXXPFN
#define REXXENTRY