windows/ValueFunction.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 #include <windows.h>
39 #include <stdlib.h>
40 
41 #include "RexxCore.h"
42 #include "StringClass.hpp"
43 #include "ActivityManager.hpp"
44 #include "SystemInterpreter.hpp"
45 
46 #define SELECTOR "ENVIRONMENT" /* environment selector */
47 
48 /*********************************************************************/
49 /* */
50 /* Subroutine Name: SysValue */
51 /* */
52 /* Descriptive Name: set value of environment variable */
53 /* */
54 /*********************************************************************/
55 
57  RexxString * Name, /* variable name */
58  RexxObject * NewValue, /* new assigned value */
59  RexxString * Selector, /* variable selector */
60  RexxObject *&result)
61 {
62  /* GetEnvironmentVariable will not alloc memory for OldValue ! */
63  char *OldValue = NULL; /* old environment value */
64  DWORD dwSize; /* size of env. variable */
65 
66  Selector = Selector->upper(); /* upper case the selector */
67  Name = Name->upper(); /* and the name too */
68 
69  if (Selector->strCompare(SELECTOR)) {/* selector ENVIRONMENT? */
70 
71  /* get the size of the environment variable and allocate buffer */
72  dwSize = GetEnvironmentVariable(Name->getStringData(), NULL, 0); // todo m17n : works only with byte char because, here, the "A" function is called
73  if (dwSize)
74  {
75  OldValue = (char *) SystemInterpreter::allocateResultMemory(size_t(dwSize)); // todo m17n : dwSize is a number of chars. If wide char, then not good...
76  /* scan for the variable */
77  if (OldValue && GetEnvironmentVariable(Name->getStringData(),OldValue,size_t(dwSize)) ) // todo m17n : dwSize is a number of chars. If wide char, then not good...
78  {
79  /* have a value already? */
80  result = (RexxObject*) new_string(OldValue);
82  }
83  else
84  result = OREF_NULLSTRING; /* otherwise, return null */
85  }
86  else
87  result = OREF_NULLSTRING;
88 
89  if (NewValue != OREF_NULL) /* have a new value? */
90  {
91  if (NewValue == (RexxString *) TheNilObject)
92  SetEnvironmentVariable((LPCTSTR)Name->getStringData(), NULL);
93  else
94  SetEnvironmentVariable((LPCTSTR)Name->getStringData(),
95  (LPCTSTR)stringArgument(NewValue, OREF_positional, ARG_TWO)->getStringData());
96  }
97  return true;
98  }
99  return false; // we could not handle this
100 }
101 
#define OREF_NULL
Definition: RexxCore.h:60
RexxString * stringArgument(RexxObject *object, RexxString *kind, size_t position)
Definition: RexxCore.h:303
const int ARG_TWO
Definition: RexxCore.h:81
#define TheNilObject
Definition: RexxCore.h:181
RexxString * new_string(const char *s, stringsizeB_t bl, sizeC_t cl=-1)
const char * getStringData()
bool strCompare(const char *s)
RexxString * upper()
static bool valueFunction(RexxString *name, RexxObject *newValue, RexxString *selector, RexxObject *&result)
static void * allocateResultMemory(sizeB_t)
static void releaseResultMemory(void *)
#define SELECTOR