rexxhide.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: REXXHIDE.C */
41 /* */
42 /* Calling REXX without creating any console */
43 /*********************************************************************/
44 
45 #include <windows.h>
46 #include <oorexxapi.h> /* needed for RexxStart() */
47 #include <malloc.h>
48 #include <string.h> /* needed for strlen() */
49 #include <stdio.h>
50 #include <io.h>
51 
52 #include "ArgumentParser.h" /* defines getArguments and freeArguments */
53 
54 //
55 // MAIN program
56 //
57 int WINAPI WinMain(
58  HINSTANCE hInstance, // handle to current instance
59  HINSTANCE hPrevInstance, // handle to previous instance
60  LPSTR lpCmdLine, // pointer to command line
61  int nCmdShow)
62 {
63  int32_t rc; /* actually running program RC */
64  const char *program_name; /* name to run */
65  CHAR arg_buffer[1024]; /* starting argument buffer */
66  CONSTRXSTRING arguments; /* rexxstart argument */
67  size_t argcount;
68 
69  rc = 0; /* set default return */
70 
71  strcpy(arg_buffer, lpCmdLine);
72  getArguments(&program_name, arg_buffer, &argcount, &arguments);
73 
74  if (program_name == NULL)
75  {
76  /* give a simple error message */
77  MessageBox(NULL, "Syntax: REXXHIDE ProgramName [parameter_1....parameter_n]\n", "Wrong Arguments", MB_OK | MB_ICONHAND);
78  return -1;
79  }
80  else /* real program execution */
81  {
82  RexxInstance *pgmInst;
83  RexxThreadContext *pgmThrdInst;
84  RexxArrayObject rxargs, rxcargs;
86  RexxObjectPtr result;
87  int i;
88  int argc; // parsed count of arguments
89  PCHAR *argv; // parsed out arguments
90 
91  // parse the arguments into argv/argc format
92  argv = CommandLineToArgvA(lpCmdLine, &argc);
93 
94  RexxCreateInterpreter(&pgmInst, &pgmThrdInst, NULL);
95  // configure the traditional single argument string
96  if ( arguments.strptr != NULL )
97  {
98  rxargs = pgmThrdInst->NewArray(1);
99  pgmThrdInst->ArrayPut(rxargs, pgmThrdInst->NewString(arguments.strptr, arguments.strlength), 1);
100  }
101  else
102  {
103  rxargs = pgmThrdInst->NewArray(0);
104  }
105 
106  // set up the C args into the .local environment
107  dir = (RexxDirectoryObject)pgmThrdInst->GetLocalEnvironment();
108  if ( argc > 2 )
109  {
110  rxcargs = pgmThrdInst->NewArray(argc - 2);
111  }
112  else
113  {
114  rxcargs = pgmThrdInst->NewArray(0);
115  }
116 
117  for (i = 2; i < argc; i++)
118  {
119  pgmThrdInst->ArrayPut(rxcargs, pgmThrdInst->NewStringFromAsciiz(argv[i]), i - 1);
120  }
121 
122  pgmThrdInst->DirectoryPut(dir, rxcargs, "SYSCARGS");
123 
124  LocalFree(argv); // released the parsed argguments
125  // call the interpreter
126  result = pgmThrdInst->CallProgram(program_name, rxargs);
127  // display any error message if there is a condition. if there was an
128  // error, then that will be our return code. we know error code will fit
129  // in an int32_t.
130  rc = (int32_t)pgmThrdInst->DisplayCondition();
131  if (rc != 0)
132  {
133  sprintf(arg_buffer, "Open Object Rexx program execution failure: rc = %d",rc);
134  MessageBox(NULL, arg_buffer, "Execution Error", MB_OK | MB_ICONHAND);
135 
136  pgmInst->Terminate();
137  return -rc; // well, the negation of the error number is the return code
138  }
139  if (result != NULL)
140  {
141  pgmThrdInst->ObjectToInt32(result, &rc);
142  }
143 
144  pgmInst->Terminate();
145  }
146 
147  return rc;
148 }
149 
150 
151 
152 
PCONSTRXSTRING getArguments(const char **program, const char *argptr, size_t *count, PCONSTRXSTRING retarr)
PCHAR * CommandLineToArgvA(PCHAR CmdLine, int32_t *_argc)
RexxReturnCode RexxEntry RexxCreateInterpreter(RexxInstance **instance, RexxThreadContext **context, RexxOption *options)
struct _RexxArrayObject * RexxArrayObject
Definition: rexx.h:130
struct _RexxObjectPtr * RexxObjectPtr
Definition: rexx.h:127
struct _RexxDirectoryObject * RexxDirectoryObject
Definition: rexx.h:137
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
Definition: rexxhide.cpp:57
const char * strptr
Definition: rexx.h:163
size_t strlength
Definition: rexx.h:162
int int32_t