rexxasp3.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 /* rexxasp3.c Open Object Rexx samples */
41 /* */
42 /* ----------------------------------------------------------------------- */
43 /* */
44 /* Description: functions used by Open Object Rexx script */
45 /* */
46 /***************************************************************************/
47 
48 #include <string.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 
52 #include <rexx.h>
53 
54 /*********************************************************************/
55 /* Numeric Return calls */
56 /*********************************************************************/
57 
58 #define INVALID_ROUTINE 40 /* Raise Rexx error */
59 #define VALID_ROUTINE 0 /* Successful completion */
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 
66 /*********************************************************************/
67 /* AspiFncTable */
68 /* Array of names of the REXXASPI functions. */
69 /* This list is used for registration and deregistration. */
70 /*********************************************************************/
71 static const char *AspiFncTable[] =
72  {
73  "Aspi_Exchange_Data",
74  "AspiDeregFunc3",
75  "Aspi_Read_All_Variables_From_REXX_VP",
76  "Aspi_Read_All_Elements_Of_A_Specific_Stem_From_REXX_VP"
77  };
78 
79 
80 /*************************************************************************
81 * Function: AspiLoadFuncs *
82 * *
83 * Syntax: call AspiLoadFuncs *
84 * *
85 * Params: none *
86 * *
87 * Return: null string *
88 *************************************************************************/
89 
91  const char *name, /* Function name */
92  size_t numargs, /* Number of arguments */
93  CONSTRXSTRING args[], /* Argument array */
94  const char * queuename, /* Current queue */
95  PRXSTRING retstr ) /* Return RXSTRING */
96 {
97  int entries; /* Num of entries */
98  int j; /* Counter */
99 
100 
101  entries = sizeof(AspiFncTable)/sizeof(const char *);
102 
103  for (j = 0; j < entries; j++)
104  {
106  }
107  return VALID_ROUTINE;
108 }
109 
110 
111 /*************************************************************************
112 * Function: AspiDeregFunc *
113 * *
114 * Syntax: call AspiDeregFuncs *
115 * *
116 * Params: none *
117 * *
118 * Return: null string *
119 *************************************************************************/
120 
122  const char *name, /* Function name */
123  size_t numargs, /* Number of arguments */
124  CONSTRXSTRING args[], /* Argument array */
125  const char * queuename, /* Current queue */
126  PRXSTRING retstr ) /* Return RXSTRING */
127 {
128  int entries; /* Num of entries */
129  int j; /* Counter */
130 
131  retstr->strlength = 0; /* set return value */
132 
133  if (numargs > 0)
134  return INVALID_ROUTINE;
135 
136 
137  entries = sizeof(AspiFncTable)/sizeof(const char *);
138 
139  for (j = 0; j < entries; j++)
140  {
142  }
143  return VALID_ROUTINE;
144 }
145 
146 /*************************************************************************
147 * Function: Aspi_Read_All_Variables_From_REXX_VP *
148 * *
149 * Syntax: call Aspi_Read_All_Variables_From_REXX_VP *
150 * *
151 * Params: No parameter required. The function is called by ASPITEST. *
152 * It uses a while loop to read all the variables in the active*
153 * REXX-variable pool. The shared variable block request code *
154 * is RXSHV_NEXTV. Be aware that with this request code REXX *
155 * treads every Stem variable as a variable itself (not the *
156 * whole stem). This gives the calling C-routine a chance *
157 * to clear up memory which was previously allocated by REXX *
158 * for every returned variable. If you don't free memory the *
159 * system will get out of storage. *
160 * Be aware that the returned variables are NOT in any spe- *
161 * cified order. *
162 * Return: 0 - success, 1 - failure *
163 *************************************************************************/
164 
166  const char *name, /* Function name */
167  size_t numargs, /* Number of arguments */
168  CONSTRXSTRING args[], /* Argument array */
169  const char * queuename, /* Current queue */
170  PRXSTRING retstr ) /* Return RXSTRING */
171 {
172  SHVBLOCK *prxshv;
173  RexxReturnCode rc;
174  int i = 1;
175 
176  strcpy(retstr->strptr, "0");
177  retstr->strlength = strlen(retstr->strptr);
178 
179  prxshv = (PSHVBLOCK)malloc(sizeof(SHVBLOCK));
180  if (!prxshv)
181  {
182  strcpy(retstr->strptr, "Allocation error occured");
183  retstr->strlength = strlen(retstr->strptr);
184  return VALID_ROUTINE;
185  }
186  prxshv->shvnext = NULL;
187  prxshv->shvname.strlength = 0;
188  prxshv->shvname.strptr = NULL;
189  prxshv->shvvalue.strptr = NULL; /*** let Rexx allocate for me ***/
190  prxshv->shvcode = RXSHV_NEXTV;
191 
192 /* Now reading all variables from the Rexx-variable pool ***********/
193 
194  rc = RexxVariablePool(prxshv);
195  if (rc)
196  {
197  if (rc != RXSHV_LVAR)
198  {
199  printf("ERROR: shvret is %x hex after var nr. %d \n",rc,i);
200  }
201  }
202 
203  printf("Name of the variable from the Variable Pool: %s, Value: %s \n", prxshv->shvname.strptr, prxshv->shvvalue.strptr);
204  i++;
205 
206  while (!prxshv->shvret)
207  {
208 
209  prxshv->shvnext = (PSHVBLOCK)malloc(sizeof(SHVBLOCK));
210  prxshv = prxshv->shvnext;
211  if (!prxshv)
212  {
213  strcpy(retstr->strptr, "Allocation error occured");
214  retstr->strlength = strlen(retstr->strptr);
215  return VALID_ROUTINE;
216  }
217  prxshv->shvnext = NULL;
218  prxshv->shvname.strlength = 0;
219  prxshv->shvname.strptr = NULL;
220  prxshv->shvvalue.strptr = NULL; /*** let Rexx allocate for me ***/
221  prxshv->shvcode = RXSHV_NEXTV;
222  rc = RexxVariablePool(prxshv);
223  if (rc)
224  {
225  if (rc== RXSHV_MEMFL)
226  {
227  strcpy(retstr->strptr, "Allocation error occured");
228  retstr->strlength = strlen(retstr->strptr);
229  return VALID_ROUTINE;
230  }
231  else if (rc != RXSHV_LVAR)
232  {
233  printf("ERROR: shvret is %x hex after var nr. %d\n",rc,i);
234  return INVALID_ROUTINE;
235  }
236  }
237  i++;
238  printf("Name of the variable from the Variable Pool: %s, Value: %s \n", prxshv->shvname.strptr, prxshv->shvvalue.strptr);
239  RexxFreeMemory((void *)prxshv->shvname.strptr);
240  RexxFreeMemory((void *)prxshv->shvvalue.strptr);
241  }
242  return VALID_ROUTINE;
243 }
244 
245 /*************************************************************************
246 * Function: Aspi_Read_All_Elements_Of_A_Specific_Stem_From_REXX_VP *
247 * *
248 * Syntax: call Aspi_Read_All_Elements_Of_A_Specific_Stem_From_REXX_VP *
249 * with the stem variable the values should be returned *
250 * *
251 * Params: A stem where all values of the stem variables should be *
252  returned. *
253 * The shared variable block request code is RXSHV_SYFET. *
254 * Only ONE call is necessary. If the stem contains to many *
255 * variables the memory resources are exhausted and Rexx *
256 * should return RXSHV_MEMFL. There is no change for the *
257 * calling routine to handle the problem with DosFreeMem *
258 * because it doesn't get any control before Rexx terminates. *
259 * The problem is during allocating memory for the values of *
260 * the stem-variables by DosAllocMem which allocates on 64k *
261 * boundary. *
262 * Return: 0 - success, 1 - failure *
263 *************************************************************************/
264 
266  const char *name, /* Function name */
267  size_t numargs, /* Number of arguments */
268  CONSTRXSTRING args[], /* Argument array */
269  const char * queuename, /* Current queue */
270  PRXSTRING retstr ) /* Return RXSTRING */
271 {
272  SHVBLOCK *prxshv, *temp, *interim, rxshv;
273  RexxReturnCode rc;
274  char array[20], value[10];
275  char *pch, *result;
276  int chars;
277  int j, k = 0;
278  prxshv = &rxshv;
279 
280  if (numargs != 1 ) /* validate arg count */
281  return INVALID_ROUTINE;
282  strcpy(retstr->strptr, "0");
283  retstr->strlength = strlen(retstr->strptr);
284 
285  pch = (char *) malloc(strlen(args[0].strptr) +1);
286  strcpy(pch, args[0].strptr);
287 
288  prxshv->shvnext = NULL;
289  prxshv->shvname.strlength = strlen(pch);
290  prxshv->shvname.strptr = pch;
291  prxshv->shvvalue.strptr = NULL; /*** let Rexx allocate for me ***/
292  prxshv->shvcode = RXSHV_SYFET;
293 
294  rc = RexxVariablePool(prxshv);
295  if (rc)
296  {
297  strcpy(retstr->strptr, "ASPIFETCH failed \n");
298  retstr->strlength = strlen(retstr->strptr);
299  return VALID_ROUTINE;
300  }
301 
302  j = atoi(prxshv->shvvalue.strptr);
303  chars = '.';
304  result = strrchr(pch, chars);
305  result++;
306  *result = 0x00;
307 
308  temp = prxshv;
309 
310  memset(array, 0x00, sizeof(array));
311  memset(value, 0x00, sizeof(value));
312  for (k = 1;k <= j; k++)
313  {
314  temp->shvnext = (PSHVBLOCK)malloc(sizeof(SHVBLOCK));
315  temp = temp->shvnext;
316  if (!temp)
317  {
318  strcpy(retstr->strptr, "Allocation error occured");
319  retstr->strlength = strlen(retstr->strptr);
320  return VALID_ROUTINE;
321  }
322  strcpy(array, pch);
323  sprintf(value, "%d", k);
324  strcat(array, value);
325  temp->shvnext = NULL;
326  temp->shvname.strlength = strlen(array);
327  temp->shvname.strptr = (char *) malloc(strlen(array)+1);
328  strcpy(temp->shvname.strptr, array);
329  temp->shvvalue.strptr = NULL; /*** let Rexx allocate for me ***/
330  temp->shvcode = RXSHV_SYFET;
331  }
332  rc = RexxVariablePool(prxshv->shvnext);
333  if (rc)
334  {
335  if (rc== RXSHV_MEMFL)
336  {
337  strcpy(retstr->strptr, "Allocation error occured");
338  retstr->strlength = strlen(retstr->strptr);
339  return VALID_ROUTINE;
340  }
341  else if (rc != RXSHV_LVAR)
342  {
343  printf("ERROR: shvret is %x hex \n",rc);
344  return INVALID_ROUTINE;
345  }
346  }
347  temp = prxshv->shvnext;
348 
349  for (k = 1;k <= j; k++)
350  {
351  printf("Name of the Stem-variable from the Rexx Variable Pool: %s, Value: %s \n", temp->shvname.strptr, temp->shvvalue.strptr);
352  interim = temp;
353  RexxFreeMemory((void *)temp->shvname.strptr);
354  RexxFreeMemory((void *)temp->shvvalue.strptr);
355  temp = temp->shvnext;
356  free(interim);
357  }
358 
359  RexxFreeMemory((void *)prxshv->shvvalue.strptr);
360  free(pch);
361 
362  return VALID_ROUTINE;
363 }
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 
RexxReturnCode RexxEntry RexxVariablePool(PSHVBLOCK pshvblock)
RexxReturnCode REXXENTRY RexxDeregisterFunction(CONSTANT_STRING)
SHVBLOCK * PSHVBLOCK
Definition: rexx.h:213
RexxReturnCode REXXENTRY RexxFreeMemory(void *)
RexxReturnCode REXXENTRY RexxRegisterFunctionDll(CONSTANT_STRING, CONSTANT_STRING, CONSTANT_STRING)
int RexxReturnCode
Definition: rexx.h:73
#define RXSHV_NEXTV
Definition: rexxapidefs.h:103
#define RXSHV_SYFET
Definition: rexxapidefs.h:101
#define RXSHV_LVAR
Definition: rexxapidefs.h:114
#define RXSHV_MEMFL
Definition: rexxapidefs.h:117
RexxReturnCode REXXENTRY Aspi_Read_All_Variables_From_REXX_VP(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
Definition: rexxasp3.c:165
RexxReturnCode REXXENTRY AspiLoadFuncs3(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
Definition: rexxasp3.c:90
#define VALID_ROUTINE
Definition: rexxasp3.c:59
RexxReturnCode REXXENTRY Aspi_Read_All_Elements_Of_A_Specific_Stem_From_REXX_VP(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
Definition: rexxasp3.c:265
static const char * AspiFncTable[]
Definition: rexxasp3.c:71
#define INVALID_ROUTINE
Definition: rexxasp3.c:58
RexxReturnCode REXXENTRY AspiDeregFunc3(const char *name, size_t numargs, CONSTRXSTRING args[], const char *queuename, PRXSTRING retstr)
Definition: rexxasp3.c:121
size_t strlength
Definition: rexx.h:157
char * strptr
Definition: rexx.h:158
CONSTANT_RXSTRING shvname
Definition: rexx.h:206
unsigned char shvret
Definition: rexx.h:211
unsigned char shvcode
Definition: rexx.h:210
RXSTRING shvvalue
Definition: rexx.h:207
struct _SHVBLOCK * shvnext
Definition: rexx.h:205
#define REXXENTRY