source: shared/shfuncs.c@ 32

Last change on this file since 32 was 32, checked in by Alex Taylor, 9 years ago

Move several common functions into shared directory.

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