rexxpaws.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 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: REXXPAWS.C */
41 /* */
42 /* Calling REXX and pausing at exit */
43 /*********************************************************************/
44 
45 
46 #include <windows.h>
47 #include <oorexxapi.h> /* needed for rexx stuff */
48 #include <malloc.h>
49 #include <stdio.h> /* needed for printf() */
50 #include <string.h> /* needed for strlen() */
51 
52 //
53 // Prototypes
54 //
55 int __cdecl main(int argc, char *argv[]); /* main entry point */
56 LONG REXXENTRY MY_IOEXIT( LONG ExitNumber, LONG Subfunction, PEXIT ParmBlock);
57 
58 #include "ArgumentParser.h" /* defines getArguments and freeArguments */
59 
60 /*
61  * These functions are used to allow ooRexx to display "Press ENTER key to exit..."
62  */
63 static void __cdecl do_pause_at_exit( void )
64 {
65  int ch;
66  printf("\nPress ENTER key to exit...");
67  fflush( stdout );
68  ch = getchar();
69 }
70 
71 void __cdecl set_pause_at_exit( void )
72 {
73  atexit( do_pause_at_exit );
74 }
75 
76 //
77 // MAIN program
78 //
79 int __cdecl main(int argc, char *argv[])
80 {
81  int32_t i; /* loop counter */
82  int32_t rc; /* actually running program RC */
83  const char *program_name; /* name to run */
84  char arg_buffer[8192]; /* starting argument buffer */
85 
86  rc = 0; /* set default return */
87 
88  /*
89  * Convert the input array into a single string for the Object REXX
90  * argument string. First argument is name of the REXX program Next
91  * argument(s) are parameters to be passed. Note that rexxpaws does not
92  * currently accept any options, so the parsing is straight forward.
93  */
95 
96  arg_buffer[0] = '\0'; /* default to no argument string */
97  program_name = NULL; /* no program to run yet */
98 
99  for (i = 1; i < argc; i++) /* loop through the arguments */
100  {
101  if (program_name == NULL) /* no name yet? */
102  {
103  program_name = argv[i]; /* program is first non-option */
104  }
105  else /* part of the argument string */
106  {
107  if (arg_buffer[0] != '\0') /* not the first one? */
108  {
109  strcat(arg_buffer, " "); /* add an blank */
110  }
111  strcat(arg_buffer, argv[i]); /* add this to the argument string */
112  }
113  }
114 
115  if (program_name == NULL)
116  {
117  /* give a simple error message */
118 #undef printf
119  printf("Syntax: REXXPAWS ProgramName [parameter_1....parameter_n]\n");
120  return -1;
121  }
122  else /* real program execution */
123  {
124  RexxInstance *pgmInst;
125  RexxThreadContext *pgmThrdInst;
126  RexxArrayObject rxargs, rxcargs;
128  RexxObjectPtr result;
129 
130  RexxCreateInterpreter(&pgmInst, &pgmThrdInst, NULL);
131 
132  // configure the traditional single argument string
133  if ( arg_buffer[0] != '\0' )
134  {
135  rxargs = pgmThrdInst->NewArray(1);
136  pgmThrdInst->ArrayPut(rxargs, pgmThrdInst->String(arg_buffer), 1);
137  }
138  else
139  {
140  rxargs = pgmThrdInst->NewArray(0);
141  }
142 
143  // set up the C args into the .local environment
144  dir = (RexxDirectoryObject)pgmThrdInst->GetLocalEnvironment();
145  if ( argc > 2 )
146  {
147  rxcargs = pgmThrdInst->NewArray(argc - 2);
148  }
149  else
150  {
151  rxcargs = pgmThrdInst->NewArray(0);
152  }
153  for (i = 2; i < argc; i++)
154  {
155  pgmThrdInst->ArrayPut(rxcargs,
156  pgmThrdInst->NewStringFromAsciiz(argv[i]),
157  i - 1);
158  }
159  pgmThrdInst->DirectoryPut(dir, rxcargs, "SYSCARGS");
160  // call the interpreter
161  result = pgmThrdInst->CallProgram(program_name, rxargs);
162  // display any error message if there is a condition. if there was an
163  // error, then that will be our return code. we know the return code
164  // will fit in an int32_t.
165  rc = (int32_t)pgmThrdInst->DisplayCondition();
166  if (rc != 0)
167  {
168  pgmInst->Terminate();
169  return -rc; // well, the negation of the error number is the return code
170  }
171  if (result != NULL)
172  {
173  pgmThrdInst->ObjectToInt32(result, &rc);
174  }
175 
176  pgmInst->Terminate();
177  }
178 
179  return rc;
180 }
181 
RexxReturnCode RexxEntry RexxCreateInterpreter(RexxInstance **instance, RexxThreadContext **context, RexxOption *options)
struct _RexxArrayObject * RexxArrayObject
Definition: rexx.h:130
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
char * PEXIT
Definition: rexx.h:215
struct _RexxDirectoryObject * RexxDirectoryObject
Definition: rexx.h:137
void __cdecl set_pause_at_exit(void)
Definition: rexxpaws.cpp:71
int __cdecl main(int argc, char *argv[])
Definition: rexxpaws.cpp:79
LONG REXXENTRY MY_IOEXIT(LONG ExitNumber, LONG Subfunction, PEXIT ParmBlock)
static void __cdecl do_pause_at_exit(void)
Definition: rexxpaws.cpp:63
#define REXXENTRY
int int32_t