unix/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-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 /* REXX Unit Support */
40 /* */
41 /* Unix system specific VALUE() built-in function routine */
42 /* */
43 /******************************************************************************/
44 
45 #include "RexxCore.h"
46 #include "StringClass.hpp"
47 #include "ActivityManager.hpp"
48 #include "SystemInterpreter.hpp"
49 
50 #include <unistd.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 
54 #define SELECTOR "ENVIRONMENT" /* environment selector */
55 
56 
57 /**
58  * Process a value function call for external repositories.
59  *
60  * @param Name The variable name.
61  * @param NewValue The new value to assign to that variable.
62  * @param Selector The variable pool selector.
63  * @param result The returned existing value.
64  *
65  * @return true if this worked, false otherwise.
66  */
67 bool SystemInterpreter::valueFunction(RexxString *name, RexxObject *newValue, RexxString *selector, RexxObject *&result)
68 {
69  // we only recognize the environemnt selector
70  if (!selector->strCaselessCompare(SELECTOR))
71  {
72  return false; // we can't handle this one
73  }
74 
75  // if not there, we return a null string
76  result = OREF_NULLSTRING;
77 
78  // see if we have an existing variable first
79  const char *oldValue = getenv(name->getStringData());
80  // either return the existing variable value or a null string.
81  if (oldValue != NULL)
82  {
83  result = new_string(oldValue);
84  }
85 
86  // set the variable if we have a new value
87  if (newValue != OREF_NULL)
88  {
89  // .nil is special, it removes the variable
90  if (newValue == (RexxString *)TheNilObject)
91  {
92  unsetenv(name->getStringData());
93  }
94  // we need a string value for the set.
95  else
96  {
97  setenv(name->getStringData(), stringArgument(newValue, OREF_positional, ARG_TWO)->getStringData(), true) ;
98  }
99  }
100  return true;
101 }
102 
103 
#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)
bool strCaselessCompare(const char *s)
const char * getStringData()
static bool valueFunction(RexxString *name, RexxObject *newValue, RexxString *selector, RexxObject *&result)
#define SELECTOR