source: shared/shfuncs.c

Last change on this file was 34, checked in by stevenhl, 9 years ago

Avoid duplicate definitions and warnings for PSZ_ZERO and PSZ_ONE

File size: 8.7 KB
Line 
1#define INCL_DOSERRORS
2#define INCL_DOSMISC
3#ifndef OS2_INCLUDED
4 #include <os2.h>
5#endif
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#define INCL_RXSHV
12#define INCL_RXFUNC
13#include <rexxsaa.h>
14
15#include "shfuncs.h"
16
17const char *PSZ_ZERO = "0";
18const char *PSZ_ONE = "1";
19
20/* ------------------------------------------------------------------------- *
21 * SaveResultString *
22 * *
23 * Writes new string contents to the specified RXSTRING, allocating any *
24 * additional memory that may be required. *
25 * *
26 * ARGUMENTS: *
27 * PRXSTRING prsResult: Pointer to an existing RXSTRING for writing. *
28 * PCH pchBytes : The string contents to write to prsResult or NULL *
29 * ULONG ulBytes : The number of bytes in pchBytes to write 0..N. *
30 * *
31 * RETURNS: BOOL *
32 * TRUE if prsResult was successfully updated. FALSE otherwise. *
33 * ------------------------------------------------------------------------- */
34BOOL SaveResultString( PRXSTRING prsResult, PCSZ pchBytes, ULONG ulBytes )
35{
36 ULONG ulRC;
37 PCH pchNew;
38
39 // 2016-02-20 SHL Rework for easier usage
40 if (!pchBytes)
41 ulBytes = 0; // Sync for caller
42 if ( ulBytes > 256 ) {
43 // REXX provides 256 bytes by default; allocate more if necessary
44 ulRC = DosAllocMem( (PVOID) &pchNew, ulBytes, PAG_WRITE | PAG_COMMIT );
45 if ( ulRC != 0 ) {
46 WriteErrorCode( ulRC, "DosAllocMem");
47 prsResult->strlength = 0; // 2016-02-20 SHL Force result to empty string
48 return ( FALSE );
49 }
50 // 2015-06-03 SHL dropped DosFreeMem(prsResult->strptr);
51 // 2015-06-03 SHL Pointer not allocated by DosAllocMem
52 prsResult->strptr = pchNew;
53 }
54 if (ulBytes)
55 memcpy( prsResult->strptr, pchBytes, ulBytes );
56 prsResult->strlength = ulBytes;
57
58 return ( TRUE );
59}
60
61
62/* ------------------------------------------------------------------------- *
63 * WriteStemElement *
64 * *
65 * Creates a stem element (compound variable) in the calling REXX program *
66 * using the REXX shared variable pool interface. *
67 * *
68 * ARGUMENTS: *
69 * PSCZ pszStem : The name of the stem (before the '.') *
70 * ULONG ulIndex : The number of the stem element (after the '.') *
71 * PSCZ pszValue : The value to write to the compound variable. *
72 * *
73 * RETURNS: BOOL *
74 * TRUE on success, FALSE on failure. *
75 * ------------------------------------------------------------------------- */
76// 2016-02-20 SHL PSZ --> PCSZ
77BOOL WriteStemElement( PCSZ pszStem, ULONG ulIndex, PCSZ pszValue )
78{
79 SHVBLOCK shvVar; // REXX shared variable pool block
80 ULONG ulRc,
81 ulBytes;
82 CHAR szCompoundName[ US_COMPOUND_MAXZ ];
83
84 sprintf( szCompoundName, "%s.%d", pszStem, ulIndex );
85 if ( pszValue == NULL ) {
86 pszValue = "";
87 ulBytes = 0;
88 } else {
89 // 2015-06-03 SHL Was using DosAllocMem and leaking memory
90 // REXX API does not free this kind of buffer
91 ulBytes = strlen(pszValue);
92 }
93 MAKERXSTRING( shvVar.shvname, szCompoundName, strlen(szCompoundName) );
94 shvVar.shvvalue.strptr = (PCH)pszValue;
95 shvVar.shvvalue.strlength = ulBytes;
96 shvVar.shvnamelen = RXSTRLEN( shvVar.shvname );
97 shvVar.shvvaluelen = RXSTRLEN( shvVar.shvvalue );
98 shvVar.shvcode = RXSHV_SYSET;
99 shvVar.shvnext = NULL;
100 ulRc = RexxVariablePool( &shvVar );
101 if ( ulRc > 1 ) {
102 WriteErrorCode( shvVar.shvret, "RexxVariablePool (SHVBLOCK.shvret)");
103 return FALSE;
104 }
105 return TRUE;
106}
107
108
109/* ------------------------------------------------------------------------- *
110 * WriteCompoundVariable *
111 * *
112 * Creates a compound variable in the calling REXX program using the REXX *
113 * shared variable pool interface. *
114 * *
115 * ARGUMENTS: *
116 * PCSZ pszStem : The name of the stem (before the '.') *
117 * PCSZ pszTail : The name of the trailing portion (after the '.') *
118 * PCSZ pszValue : The value to write to the compound variable. *
119 * *
120 * RETURNS: BOOL *
121 * TRUE on success, FALSE on failure. *
122 * ------------------------------------------------------------------------- */
123// 2016-05-10 ALT PSZ --> PCSZ
124BOOL WriteCompoundVariable( PCSZ pszStem, PCSZ pszTail, PCSZ pszValue )
125{
126 SHVBLOCK shvVar; // REXX shared variable pool block
127 ULONG ulRc,
128 ulBytes;
129 CHAR szCompoundName[ US_COMPOUND_MAXZ ];
130
131 sprintf( szCompoundName, "%s.%s", pszStem, pszTail );
132 if ( pszValue == NULL ) {
133 pszValue = "";
134 ulBytes = 0;
135 } else {
136 ulBytes = strlen( pszValue );
137 }
138 MAKERXSTRING( shvVar.shvname, szCompoundName, strlen(szCompoundName) );
139 shvVar.shvvalue.strptr = (PCH)pszValue;
140 shvVar.shvvalue.strlength = ulBytes;
141 shvVar.shvnamelen = RXSTRLEN( shvVar.shvname );
142 shvVar.shvvaluelen = RXSTRLEN( shvVar.shvvalue );
143 shvVar.shvcode = RXSHV_SYSET;
144 shvVar.shvnext = NULL;
145 ulRc = RexxVariablePool( &shvVar );
146 if ( ulRc > 1 ) {
147 WriteErrorCode( shvVar.shvret, "RexxVariablePool (SHVBLOCK.shvret)");
148 return FALSE;
149 }
150 return TRUE;
151}
152
153
154/* ------------------------------------------------------------------------- *
155 * WriteErrorCode *
156 * *
157 * Writes an error code to a special variable in the calling REXX program *
158 * using the REXX shared variable pool interface. This is used to return *
159 * API error codes to the REXX program, since the REXX functions themselves *
160 * normally return string values. *
161 * *
162 * ARGUMENTS: *
163 * ULONG ulError : The error code returned by the failing API call. *
164 * PSZ pszContext: A string describing the API call that failed. *
165 * *
166 * RETURNS: N/A *
167 * ------------------------------------------------------------------------- */
168void WriteErrorCode( ULONG ulError, PCSZ pszContext )
169{
170 SHVBLOCK shvVar; // REXX shared variable pool block
171 ULONG ulRc;
172 CHAR szErrorText[ US_ERRSTR_MAXZ ];
173
174 if ( pszContext == NULL )
175 sprintf( szErrorText, "%u", ulError );
176 else
177 sprintf( szErrorText, "%u: %s", ulError, pszContext );
178 MAKERXSTRING( shvVar.shvname, SZ_ERROR_NAME, strlen(SZ_ERROR_NAME) );
179 MAKERXSTRING( shvVar.shvvalue, szErrorText, strlen(szErrorText) );
180 shvVar.shvnamelen = RXSTRLEN( shvVar.shvname );
181 shvVar.shvvaluelen = RXSTRLEN( shvVar.shvvalue );
182 shvVar.shvcode = RXSHV_SYSET;
183 shvVar.shvnext = NULL;
184 shvVar.shvret = 0; // 2016-02-26 SHL
185 ulRc = RexxVariablePool( &shvVar );
186 // 2016-02-26 SHL Correct if
187 if ( ulRc & ~RXSHV_NEWV )
188 printf("* Unable to set %s: shvret = 0x%x, apiret = 0x%x\n", shvVar.shvname.strptr, (UCHAR)shvVar.shvret, ulRc ); // 2016-02-26 SHL Correct formatting
189}
190
Note: See TracBrowser for help on using the repository browser.